Skip to content

Commit 5afc26a

Browse files
jeremypwJeremy Woottenzeebok
authored
Fix "Open in" for Flatpak running (#1512)
Co-authored-by: Jeremy Wootten <jeremy@Proteus-EL07R6-9b3c42bb.localdomain> Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
1 parent f253e69 commit 5afc26a

4 files changed

Lines changed: 70 additions & 44 deletions

File tree

src/Application.vala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ namespace Scratch {
2929
public class Application : Gtk.Application {
3030
public string data_home_folder_unsaved { get { return _data_home_folder_unsaved; } }
3131
public string default_font { get; set; }
32+
public bool is_running_in_flatpak { get; construct; }
33+
3234
private static string _data_home_folder_unsaved;
3335
private static bool create_new_tab = false;
3436
private static bool create_new_window = false;
37+
3538
private LocationJumpManager location_jump_manager;
3639

3740
const OptionEntry[] ENTRIES = {
@@ -48,7 +51,6 @@ namespace Scratch {
4851
_data_home_folder_unsaved = Path.build_filename (
4952
Environment.get_user_data_dir (), Constants.PROJECT_NAME, "unsaved"
5053
);
51-
5254
}
5355

5456
construct {
@@ -60,6 +62,7 @@ namespace Scratch {
6062
application_id += "." + Constants.BRANCH.replace ("/", ".").replace ("-", "_");
6163
}
6264

65+
is_running_in_flatpak = FileUtils.test ("/.flatpak-info", FileTest.IS_REGULAR);
6366
add_main_option_entries (ENTRIES);
6467

6568
// Init settings

src/FolderManager/FileView.vala

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -419,20 +419,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
419419

420420
private void action_launch_app_with_file_path (SimpleAction action, Variant? param) {
421421
var params = param.get_strv ();
422-
var path = params[0];
423-
if (path == null || path == "") {
424-
return;
425-
}
426-
427-
var app_id = params[1];
428-
if (app_id == null || app_id == "") {
429-
return;
430-
}
431-
432-
var app_info = new GLib.DesktopAppInfo (app_id);
433-
var file = GLib.File.new_for_path (path);
434-
435-
Utils.launch_app_with_file (app_info, file);
422+
Utils.launch_app_with_file (params[1], params[0]);
436423
}
437424

438425
private void action_show_app_chooser (SimpleAction action, Variant? param) {
@@ -449,7 +436,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
449436
if (dialog.run () == Gtk.ResponseType.OK) {
450437
var app_info = dialog.get_app_info ();
451438
if (app_info != null) {
452-
Utils.launch_app_with_file (app_info, file);
439+
Utils.launch_app_with_file (app_info.get_id (), path);
453440
}
454441
}
455442

src/FolderManager/ProjectFolderItem.vala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ namespace Scratch.FolderManager {
124124
}
125125

126126
public override Gtk.Menu? get_context_menu () {
127-
GLib.FileInfo info = null;
128-
unowned string? file_type = null;
129-
127+
string file_type = "";
130128
try {
131-
info = file.file.query_info (GLib.FileAttribute.STANDARD_CONTENT_TYPE, GLib.FileQueryInfoFlags.NONE);
132-
file_type = info.get_content_type ();
129+
var info = file.file.query_info (GLib.FileAttribute.STANDARD_CONTENT_TYPE, GLib.FileQueryInfoFlags.NONE);
130+
if (info.has_attribute (GLib.FileAttribute.STANDARD_CONTENT_TYPE)) {
131+
file_type = info.get_content_type ();
132+
}
133133
} catch (Error e) {
134134
warning (e.message);
135135
}

src/Utils.vala

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -201,47 +201,83 @@ namespace Scratch.Utils {
201201
return false;
202202
}
203203

204-
public GLib.Menu create_executable_app_items_for_file (GLib.File file, string file_type) {
205-
var external_apps = GLib.AppInfo.get_all_for_type (file_type);
206-
var this_id = GLib.Application.get_default ().application_id + ".desktop";
204+
public GLib.Menu? create_executable_app_items_for_file (GLib.File file, string file_type) {
205+
var scratch_app = (Scratch.Application) (GLib.Application.get_default ());
206+
var this_id = scratch_app.application_id + ".desktop";
207207
var menu = new GLib.Menu ();
208208

209-
external_apps.sort ((a, b) => {
210-
return a.get_name ().collate (b.get_name ());
211-
});
212-
213-
foreach (AppInfo app_info in external_apps) {
214-
string app_id = app_info.get_id ();
215-
if (app_id == this_id) {
216-
continue;
217-
}
218-
209+
if (scratch_app.is_running_in_flatpak) {
219210
var menu_item = new MenuItem (
220-
app_info.get_name (),
211+
///TRANSLATORS '%s' represents the quoted basename of a uri to be opened with the default app
212+
_("Show '%s' with default app").printf (file.get_basename ()),
221213
GLib.Action.print_detailed_name (
222214
Scratch.FolderManager.FileView.ACTION_PREFIX
223215
+ Scratch.FolderManager.FileView.ACTION_LAUNCH_APP_WITH_FILE_PATH,
224216
new GLib.Variant.array (
225217
GLib.VariantType.STRING,
226-
{ file.get_path (), app_id }
218+
{ file.get_path (), "" }
227219
)
228220
)
229221
);
230-
menu_item.set_icon (app_info.get_icon ());
231222
menu.append_item (menu_item);
223+
} else {
224+
List<AppInfo> external_apps = null;
225+
if (file_type == "") {
226+
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
227+
external_apps.prepend (files_appinfo);
228+
} else {
229+
external_apps = GLib.AppInfo.get_all_for_type (file_type);
230+
external_apps.sort ((a, b) => {
231+
return a.get_name ().collate (b.get_name ());
232+
});
233+
}
234+
235+
foreach (AppInfo app_info in external_apps) {
236+
string app_id = app_info.get_id ();
237+
if (app_id == this_id) {
238+
continue;
239+
}
240+
241+
var menu_item = new MenuItem (
242+
app_info.get_name (),
243+
GLib.Action.print_detailed_name (
244+
Scratch.FolderManager.FileView.ACTION_PREFIX
245+
+ Scratch.FolderManager.FileView.ACTION_LAUNCH_APP_WITH_FILE_PATH,
246+
new GLib.Variant.array (
247+
GLib.VariantType.STRING,
248+
{ file.get_path (), app_id }
249+
)
250+
)
251+
);
252+
menu_item.set_icon (app_info.get_icon ());
253+
menu.append_item (menu_item);
254+
}
232255
}
233256

234257
return menu;
235258
}
236259

237-
public void launch_app_with_file (AppInfo app_info, GLib.File file) {
238-
var file_list = new List<GLib.File> ();
239-
file_list.append (file);
260+
public void launch_app_with_file (string app_id, string path) {
261+
var scratch_app = (Scratch.Application) (GLib.Application.get_default ());
262+
if (scratch_app.is_running_in_flatpak || app_id == "") {
263+
var uri = Uri.join (UriFlags.NONE, "file", null, null, -1, path, null, null);
240264

241-
try {
242-
app_info.launch (file_list, null);
243-
} catch (Error e) {
244-
warning (e.message);
265+
try {
266+
Gtk.show_uri_on_window (scratch_app.get_active_window (), uri, Gdk.CURRENT_TIME);
267+
} catch (Error e) {
268+
warning ("Error showing uri %s, %s", uri, e.message);
269+
}
270+
} else {
271+
var app_info = new GLib.DesktopAppInfo (app_id);
272+
var file = GLib.File.new_for_path (path);
273+
var file_list = new List<GLib.File> ();
274+
file_list.append (file);
275+
276+
try {
277+
app_info.launch (file_list, null);
278+
} catch (Error e) {
279+
warning (e.message);
280+
}
245281
}
246282
}
247283

0 commit comments

Comments
 (0)