From 2677b51ad51e6ef15680815bb50bbde6ae6ca85f Mon Sep 17 00:00:00 2001 From: Siegfried-Angel Gevatter Pujals Date: Sun, 29 Jan 2012 22:59:49 +0100 Subject: Fix kde-recent-document-provider to use real desktop IDs. And a bunch of moving random code around :P. --- src/desktop-launch-listener.vala | 87 +------------------------- src/kde-recent-document-provider.vala | 31 +++++++++- src/utils.vala | 112 +++++++++++++++++++++++++++++++++- 3 files changed, 142 insertions(+), 88 deletions(-) diff --git a/src/desktop-launch-listener.vala b/src/desktop-launch-listener.vala index 1c96557..25af42e 100644 --- a/src/desktop-launch-listener.vala +++ b/src/desktop-launch-listener.vala @@ -45,8 +45,6 @@ public class DesktopLaunchListener : DataProvider private GLib.DBusConnection bus; private uint launched_signal_id = 0; - private string[] prefixes; - construct { try @@ -64,41 +62,6 @@ public class DesktopLaunchListener : DataProvider DesktopAppInfo.set_desktop_env (desktop_env); return; } - - unowned string session_var = Environment.get_variable ("DESKTOP_SESSION"); - if (session_var == null) - { - // let's assume it's gnome - DesktopAppInfo.set_desktop_env ("GNOME"); - return; - } - - string desktop_session = session_var.up (); - if (desktop_session.has_prefix ("GNOME")) - { - DesktopAppInfo.set_desktop_env ("GNOME"); - } - else if (desktop_session.has_prefix ("KDE")) - { - DesktopAppInfo.set_desktop_env ("KDE"); - } - else if (desktop_session.has_prefix ("XFCE")) - { - DesktopAppInfo.set_desktop_env ("XFCE"); - } - else - { - // assume GNOME - DesktopAppInfo.set_desktop_env ("GNOME"); - } - - foreach (unowned string data_dir in Environment.get_system_data_dirs ()) - { - prefixes += Path.build_path (Path.DIR_SEPARATOR_S, - data_dir, - "applications", - Path.DIR_SEPARATOR_S, null); - } } public override void start () @@ -144,8 +107,8 @@ public class DesktopLaunchListener : DataProvider HashTable extra_params = (HashTable) dict; DesktopAppInfo? dai; - string launched_uri = get_uri_for_desktop_file (desktop_file, - out dai); + string launched_uri = Utils.get_actor_for_desktop_file (desktop_file, + out dai); if (launched_uri == null) { warning ("Unable to open desktop file '%s'", desktop_file); @@ -156,7 +119,7 @@ public class DesktopLaunchListener : DataProvider unowned Variant origin_df = extra_params.lookup ("origin-desktop-file"); if (origin_df != null) { - launcher_uri = get_uri_for_desktop_file (origin_df.get_bytestring ()); + launcher_uri = Utils.get_actor_for_desktop_file (origin_df.get_bytestring ()); } else { @@ -196,50 +159,6 @@ public class DesktopLaunchListener : DataProvider items_available (arr); } - /* - * Takes a path to a .desktop file and returns the Desktop ID for it. - * This isn't simply the basename, but may contain part of the path; - * eg. kde4-kate.desktop for /usr/share/applications/kde4/kate.desktop. - * */ - private string extract_desktop_id (string path) - { - if (!path.has_prefix ("/")) - return path; - - foreach (unowned string prefix in prefixes) - { - string without_prefix = path.substring (prefix.length); - - if (Path.DIR_SEPARATOR_S in without_prefix) - return without_prefix.replace (Path.DIR_SEPARATOR_S, "-"); - - return without_prefix; - } - - return Path.get_basename (path); - } - - private string? get_uri_for_desktop_file (string desktop_file, - out DesktopAppInfo dai = null) - { - if (Path.is_absolute (desktop_file)) - { - dai = new DesktopAppInfo.from_filename (desktop_file); - } - else - { - dai = new DesktopAppInfo (desktop_file); - } - - if (dai == null) - { - return null; - } - - string desktop_id = dai.get_id () ?? extract_desktop_id (dai.get_filename ()); - return "application://%s".printf (desktop_id); - } - public override void stop () { if (launched_signal_id != 0) diff --git a/src/kde-recent-document-provider.vala b/src/kde-recent-document-provider.vala index fcd91d4..9f4b4c0 100644 --- a/src/kde-recent-document-provider.vala +++ b/src/kde-recent-document-provider.vala @@ -154,7 +154,12 @@ public class RecentDocumentsKDE : DataProvider // only uses $HOME. uri = url_regex.replace (uri, uri.length, 0, Environment.get_home_dir ()); - string actor = "application://%s.desktop".printf (desktop_entry_name); + string? actor = get_actor_for_desktop_entry_name (desktop_entry_name); + if (actor == null) + { + warning ("Couldn't find actor for '%s'.", desktop_entry_name); + return null; + } if (actor in ignored_actors) return null; @@ -201,7 +206,29 @@ public class RecentDocumentsKDE : DataProvider return event; } - protected async void crawl_all_items () throws GLib.FileError + private string? get_actor_for_desktop_entry_name (string desktop_entry_name) + { + const string desktop_prefixes[] = { "", "kde-", "kde4-" }; + + DesktopAppInfo dae = null; + string desktop_id = null; + foreach (unowned string prefix in desktop_prefixes) + { + desktop_id = "%s%s.desktop".printf (prefix, desktop_entry_name); + dae = new DesktopAppInfo (desktop_id); + if (dae != null) + break; + } + + if (dae != null) + { + return "application://%s".printf (desktop_id); + } + + return null; + } + + private async void crawl_all_items () throws GLib.FileError { string filename; GenericArray events = new GenericArray (); diff --git a/src/utils.vala b/src/utils.vala index 554f0f6..877e5cf 100644 --- a/src/utils.vala +++ b/src/utils.vala @@ -27,6 +27,7 @@ using Zeitgeist; public class Utils : Object { private static HashTable app_to_desktop_file = null; + private static string[] desktop_file_prefixes = null; // FIXME: Do we want to make this async? // FIXME: this can throw GLib.Error, but if we use try/catch or throws @@ -55,22 +56,129 @@ public class Utils : Object return (timeval.tv_sec * 1000) + (timeval.tv_usec / 1000); } - private static void init () + /* + * Configures DesktopAppInfo and initializes the list of places where we + * may find .desktop files. + */ + private static void init_desktop_id () + { + if (desktop_file_prefixes != null) + return; + + unowned string session_var = Environment.get_variable ("DESKTOP_SESSION"); + if (session_var == null) + { + // let's assume it's gnome + DesktopAppInfo.set_desktop_env ("GNOME"); + return; + } + + string desktop_session = session_var.up (); + if (desktop_session.has_prefix ("GNOME")) + { + DesktopAppInfo.set_desktop_env ("GNOME"); + } + else if (desktop_session.has_prefix ("KDE")) + { + DesktopAppInfo.set_desktop_env ("KDE"); + } + else if (desktop_session.has_prefix ("XFCE")) + { + DesktopAppInfo.set_desktop_env ("XFCE"); + } + else + { + // assume GNOME + DesktopAppInfo.set_desktop_env ("GNOME"); + } + + foreach (unowned string data_dir in Environment.get_system_data_dirs ()) + { + desktop_file_prefixes += Path.build_path (Path.DIR_SEPARATOR_S, + data_dir, + "applications", + Path.DIR_SEPARATOR_S, null); + } + } + + /* + * Takes a path to a .desktop file and returns the Desktop ID for it. + * This isn't simply the basename, but may contain part of the path; + * eg. kde4-kate.desktop for /usr/share/applications/kde4/kate.desktop. + * */ + private static string extract_desktop_id (string path) + { + if (!path.has_prefix ("/")) + return path; + + foreach (unowned string prefix in desktop_file_prefixes) + { + string without_prefix = path.substring (prefix.length); + + if (Path.DIR_SEPARATOR_S in without_prefix) + return without_prefix.replace (Path.DIR_SEPARATOR_S, "-"); + + return without_prefix; + } + + return Path.get_basename (path); + } + + /* + * Takes the basename of a .desktop and returns the Zeitgeist actor for it. + */ + public static string? get_actor_for_desktop_file (string desktop_file, + out DesktopAppInfo dai = null) + { + init_desktop_id (); + + if (Path.is_absolute (desktop_file)) + { + dai = new DesktopAppInfo.from_filename (desktop_file); + } + else + { + dai = new DesktopAppInfo (desktop_file); + } + + if (dai == null) + { + return null; + } + + string desktop_id = dai.get_id () ?? extract_desktop_id (dai.get_filename ()); + return "application://%s".printf (desktop_id); + } + + /* + * Initialize the cache mapping application names (from GtkRecentManager) + * to Desktop IDs. + * */ + private static void init_application_cache () { if (unlikely (app_to_desktop_file == null)) app_to_desktop_file = new HashTable (str_hash, str_equal); } + /* + * Workaround for OpenOffice.org/LibreOffice. + * */ public static string? get_ooo_desktop_file_for_mimetype (string mimetype) { return find_desktop_file_for_app ("libreoffice", mimetype) ?? find_desktop_file_for_app ("ooffice", mimetype); } + /* + * Takes an application name (from GtkRecentManager) and finds + * a .desktop file that launches the given application. + * + * It returns the complete path to the .desktop file. + */ public static string? find_desktop_file_for_app (string app_name, string? mimetype = null) { - init (); + init_application_cache (); string hash_name = mimetype != null ? "%s::%s".printf (app_name, mimetype) : app_name; -- cgit v1.2.3