diff --git a/src/Application.vala b/src/Application.vala index 10c131d38..cc809dab3 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -29,9 +29,12 @@ namespace Scratch { public class Application : Gtk.Application { public string data_home_folder_unsaved { get { return _data_home_folder_unsaved; } } public string default_font { get; set; } + public bool is_running_in_flatpak { get; construct; } + private static string _data_home_folder_unsaved; private static bool create_new_tab = false; private static bool create_new_window = false; + private LocationJumpManager location_jump_manager; const OptionEntry[] ENTRIES = { @@ -48,7 +51,6 @@ namespace Scratch { _data_home_folder_unsaved = Path.build_filename ( Environment.get_user_data_dir (), Constants.PROJECT_NAME, "unsaved" ); - } construct { @@ -60,6 +62,7 @@ namespace Scratch { application_id += "." + Constants.BRANCH.replace ("/", ".").replace ("-", "_"); } + is_running_in_flatpak = FileUtils.test ("/.flatpak-info", FileTest.IS_REGULAR); add_main_option_entries (ENTRIES); // Init settings diff --git a/src/FolderManager/FileView.vala b/src/FolderManager/FileView.vala index c9efd85bb..313e99fa8 100644 --- a/src/FolderManager/FileView.vala +++ b/src/FolderManager/FileView.vala @@ -408,20 +408,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane private void action_launch_app_with_file_path (SimpleAction action, Variant? param) { var params = param.get_strv (); - var path = params[0]; - if (path == null || path == "") { - return; - } - - var app_id = params[1]; - if (app_id == null || app_id == "") { - return; - } - - var app_info = new GLib.DesktopAppInfo (app_id); - var file = GLib.File.new_for_path (path); - - Utils.launch_app_with_file (app_info, file); + Utils.launch_app_with_file (params[1], params[0]); } private void action_show_app_chooser (SimpleAction action, Variant? param) { @@ -438,7 +425,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane if (dialog.run () == Gtk.ResponseType.OK) { var app_info = dialog.get_app_info (); if (app_info != null) { - Utils.launch_app_with_file (app_info, file); + Utils.launch_app_with_file (app_info.get_id (), path); } } diff --git a/src/FolderManager/ProjectFolderItem.vala b/src/FolderManager/ProjectFolderItem.vala index 9998db6be..c76e8c0b6 100644 --- a/src/FolderManager/ProjectFolderItem.vala +++ b/src/FolderManager/ProjectFolderItem.vala @@ -124,12 +124,12 @@ namespace Scratch.FolderManager { } public override Gtk.Menu? get_context_menu () { - GLib.FileInfo info = null; - unowned string? file_type = null; - + string file_type = ""; try { - info = file.file.query_info (GLib.FileAttribute.STANDARD_CONTENT_TYPE, GLib.FileQueryInfoFlags.NONE); - file_type = info.get_content_type (); + var info = file.file.query_info (GLib.FileAttribute.STANDARD_CONTENT_TYPE, GLib.FileQueryInfoFlags.NONE); + if (info.has_attribute (GLib.FileAttribute.STANDARD_CONTENT_TYPE)) { + file_type = info.get_content_type (); + } } catch (Error e) { warning (e.message); } diff --git a/src/Utils.vala b/src/Utils.vala index d86d57119..276c780e4 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -201,46 +201,80 @@ namespace Scratch.Utils { return false; } - public GLib.Menu create_executable_app_items_for_file (GLib.File file, string file_type) { - List external_apps = GLib.AppInfo.get_all_for_type (file_type); - var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true); - external_apps.prepend (files_appinfo); - - string this_id = GLib.Application.get_default ().application_id + ".desktop"; + public GLib.Menu? create_executable_app_items_for_file (GLib.File file, string file_type) { + var scratch_app = (Scratch.Application) (GLib.Application.get_default ()); + var this_id = scratch_app.application_id + ".desktop"; var menu = new GLib.Menu (); - foreach (AppInfo app_info in external_apps) { - string app_id = app_info.get_id (); - if (app_id == this_id) { - continue; - } - + if (scratch_app.is_running_in_flatpak) { var menu_item = new MenuItem ( - app_info.get_name (), + ///TRANSLATORS '%s' represents the quoted basename of a uri to be opened with the default app + _("Show '%s' with default app").printf (file.get_basename ()), GLib.Action.print_detailed_name ( Scratch.FolderManager.FileView.ACTION_PREFIX + Scratch.FolderManager.FileView.ACTION_LAUNCH_APP_WITH_FILE_PATH, new GLib.Variant.array ( GLib.VariantType.STRING, - { file.get_path (), app_id } + { file.get_path (), "" } ) ) ); - menu_item.set_icon (app_info.get_icon ()); menu.append_item (menu_item); + } else { + List external_apps = null; + if (file_type == "") { + var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true); + external_apps.prepend (files_appinfo); + } else { + external_apps = GLib.AppInfo.get_all_for_type (file_type); + } + + foreach (AppInfo app_info in external_apps) { + string app_id = app_info.get_id (); + if (app_id == this_id) { + continue; + } + + var menu_item = new MenuItem ( + app_info.get_name (), + GLib.Action.print_detailed_name ( + Scratch.FolderManager.FileView.ACTION_PREFIX + + Scratch.FolderManager.FileView.ACTION_LAUNCH_APP_WITH_FILE_PATH, + new GLib.Variant.array ( + GLib.VariantType.STRING, + { file.get_path (), app_id } + ) + ) + ); + menu_item.set_icon (app_info.get_icon ()); + menu.append_item (menu_item); + } } return menu; } - public void launch_app_with_file (AppInfo app_info, GLib.File file) { - var file_list = new List (); - file_list.append (file); + public void launch_app_with_file (string app_id, string path) { + var scratch_app = (Scratch.Application) (GLib.Application.get_default ()); + if (scratch_app.is_running_in_flatpak || app_id == "") { + var uri = Uri.join (UriFlags.NONE, "file", null, null, -1, path, null, null); - try { - app_info.launch (file_list, null); - } catch (Error e) { - warning (e.message); + try { + Gtk.show_uri_on_window (scratch_app.get_active_window (), uri, Gdk.CURRENT_TIME); + } catch (Error e) { + warning ("Error showing uri %s, %s", uri, e.message); + } + } else { + var app_info = new GLib.DesktopAppInfo (app_id); + var file = GLib.File.new_for_path (path); + var file_list = new List (); + file_list.append (file); + + try { + app_info.launch (file_list, null); + } catch (Error e) { + warning (e.message); + } } }