Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions app/src/main/java/com/winlator/ShortcutsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -27,6 +28,11 @@
import com.winlator.contentdialog.ContentDialog;
import com.winlator.contentdialog.ShortcutSettingsDialog;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -128,11 +134,73 @@ else if (itemId == R.id.shortcut_remove) {
loadShortcutsList();
});
}
else if (itemId == R.id.shortcut_export_to_frontend) {
exportShortcutToFrontend(shortcut);
}
return true;
});
listItemMenu.show();
}

private void exportShortcutToFrontend(Shortcut shortcut) {
// Attempt to make the frontend dir
File frontendDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Winlator/Frontend");
if (!frontendDir.exists() && !frontendDir.mkdirs()) {
Toast.makeText(getContext(), "Failed to create default frontend directory", Toast.LENGTH_SHORT).show();
return;
}

// Create the export file in the Frontend directory
File exportFile = new File(frontendDir, shortcut.file.getName());

boolean fileExists = exportFile.exists();
boolean containerIdFound = false;

try {
List<String> lines = new ArrayList<>();

// Read the original file or existing file if it exists
try (BufferedReader reader = new BufferedReader(new FileReader(shortcut.file))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("container_id=")) {
// Replace the existing container_id line
lines.add("container_id=" + shortcut.container.id);
containerIdFound = true;
} else {
lines.add(line);
}
}
}

// If no container_id was found, add it
if (!containerIdFound) {
lines.add("container_id=" + shortcut.container.id);
}

// Write the contents to the export file
try (FileWriter writer = new FileWriter(exportFile, false)) {
for (String line : lines) {
writer.write(line + "\n");
}
writer.flush();
}

// Determine the toast message
String message;
if (fileExists) {
message = "Frontend Shortcut Updated at " + exportFile.getPath();
} else {
message = "Frontend Shortcut Exported to " + exportFile.getPath();
}

// Show a toast message to the user
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getContext(), "Failed to export shortcut", Toast.LENGTH_LONG).show();
}
}

private void runFromShortcut(Shortcut shortcut) {
Activity activity = getActivity();

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/shortcut_popup_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shortcut_settings" android:title="@string/settings" android:icon="@drawable/icon_popup_menu_settings" android:iconTint="@color/colorAccent" />
<item android:id="@+id/shortcut_remove" android:title="@string/remove" android:icon="@drawable/icon_popup_menu_remove" android:iconTint="@color/colorAccent" />
<item android:id="@+id/shortcut_export_to_frontend" android:title="@string/export_for_frontend" android:icon="@drawable/icon_popup_menu_download" android:iconTint="@color/colorAccent" />
</menu>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,5 @@
<string name="msg_warning_install_wine">Warning: Installing a new version of Wine is recommended for testing purposes only.</string>
<string name="startup_selection">Startup Selection</string>
<string name="wine_debug_channel">Wine Debug Channel</string>
</resources>
<string name="export_for_frontend">Export for frontend</string>
</resources>