desktop-app-info: Add support for X-GNOME-Keywords
[platform/upstream/glib.git] / gio / gdesktopappinfo.c
index f72de34..381dbe4 100644 (file)
@@ -1,5 +1,5 @@
 /* GIO - GLib Input, Output and Streaming Library
- * 
+ *
  * Copyright (C) 2006-2007 Red Hat, Inc.
  * Copyright © 2007 Ryan Lortie
  *
@@ -66,6 +66,7 @@
 #define MIME_CACHE_GROUP            "MIME Cache"
 #define GENERIC_NAME_KEY            "GenericName"
 #define FULL_NAME_KEY               "X-GNOME-FullName"
+#define KEYWORDS_KEY                "X-GNOME-Keywords"
 
 enum {
   PROP_0,
@@ -99,6 +100,7 @@ struct _GDesktopAppInfo
   char *comment;
   char *icon_name;
   GIcon *icon;
+  char **keywords;
   char **only_show_in;
   char **not_show_in;
   char *try_exec;
@@ -127,6 +129,9 @@ G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
                                                g_desktop_app_info_iface_init))
 
+G_LOCK_DEFINE_STATIC (g_desktop_env);
+static gchar *g_desktop_env = NULL;
+
 static gpointer
 search_path_init (gpointer data)
 {
@@ -174,6 +179,7 @@ g_desktop_app_info_finalize (GObject *object)
   g_free (info->icon_name);
   if (info->icon)
     g_object_unref (info->icon);
+  g_strfreev (info->keywords);
   g_strfreev (info->only_show_in);
   g_strfreev (info->not_show_in);
   g_free (info->try_exec);
@@ -312,6 +318,7 @@ g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
   info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
   info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
   info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
+  info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
   info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
   info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
   info->icon_name =  g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
@@ -520,6 +527,7 @@ g_desktop_app_info_dup (GAppInfo *appinfo)
   new_info->name = g_strdup (info->name);
   new_info->generic_name = g_strdup (info->generic_name);
   new_info->fullname = g_strdup (info->fullname);
+  new_info->keywords = g_strdupv (info->keywords);
   new_info->comment = g_strdup (info->comment);
   new_info->nodisplay = info->nodisplay;
   new_info->icon_name = g_strdup (info->icon_name);
@@ -648,27 +656,131 @@ g_desktop_app_info_get_icon (GAppInfo *appinfo)
  * g_desktop_app_info_get_categories:
  * @info: a #GDesktopAppInfo
  *
- * Returns: The unparsed Categories key from the file; i.e. no attempt
- *   is made to split it by ';' or validate it.
+ * Gets the categories from the desktop file.
+ *
+ * Returns: The unparsed Categories key from the desktop file;
+ *     i.e. no attempt is made to split it by ';' or validate it.
  */
 const char *
-g_desktop_app_info_get_categories    (GDesktopAppInfo *info)
+g_desktop_app_info_get_categories (GDesktopAppInfo *info)
 {
   return info->categories;
 }
 
 /**
+ * g_desktop_app_info_get_keywords:
+ * @info: a #GDesktopAppInfo
+ *
+ * Gets the keywords from the desktop file.
+ *
+ * Returns: The value of the X-GNOME-Keywords key
+ *
+ * Since: 2.32
+ */
+const char * const *
+g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
+{
+  return (const char * const *)info->keywords;
+}
+
+/**
  * g_desktop_app_info_get_generic_name:
  * @info: a #GDesktopAppInfo
  *
+ * Gets the generic name from the destkop file.
+ *
  * Returns: The value of the GenericName key
  */
 const char *
-g_desktop_app_info_get_generic_name  (GDesktopAppInfo *info)
+g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
 {
   return info->generic_name;
 }
 
+/**
+ * g_desktop_app_info_get_nodisplay:
+ * @info: a #GDesktopAppInfo
+ *
+ * Gets the value of the NoDisplay key, which helps determine if the
+ * application info should be shown in menus. See
+ * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
+ *
+ * Returns: The value of the NoDisplay key
+ *
+ * Since: 2.30
+ */
+gboolean
+g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
+{
+  return info->nodisplay;
+}
+
+/**
+ * g_desktop_app_info_get_show_in:
+ * @info: a #GDesktopAppInfo
+ * @desktop_env: a string specifying a desktop name
+ *
+ * Checks if the application info should be shown in menus that list available
+ * applications for a specific name of the desktop, based on the
+ * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
+ *
+ * If @desktop_env is %NULL, then the name of the desktop set with
+ * g_desktop_app_info_set_desktop_env() is used.
+ *
+ * Note that g_app_info_should_show() for @info will include this check (with
+ * %NULL for @desktop_env) as well as additional checks.
+ *
+ * Returns: %TRUE if the @info should be shown in @desktop_env according to the
+ * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
+ * otherwise.
+ *
+ * Since: 2.30
+ */
+gboolean
+g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
+                                const gchar     *desktop_env)
+{
+  gboolean found;
+  int i;
+
+  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
+
+  if (!desktop_env) {
+    G_LOCK (g_desktop_env);
+    desktop_env = g_desktop_env;
+    G_UNLOCK (g_desktop_env);
+  }
+
+  if (info->only_show_in)
+    {
+      if (desktop_env == NULL)
+       return FALSE;
+
+      found = FALSE;
+      for (i = 0; info->only_show_in[i] != NULL; i++)
+       {
+         if (strcmp (info->only_show_in[i], desktop_env) == 0)
+           {
+             found = TRUE;
+             break;
+           }
+       }
+      if (!found)
+       return FALSE;
+    }
+
+  if (info->not_show_in && desktop_env)
+    {
+      for (i = 0; info->not_show_in[i] != NULL; i++)
+       {
+         if (strcmp (info->not_show_in[i], desktop_env) == 0)
+           return FALSE;
+       }
+    }
+
+  return TRUE;
+}
+
 static char *
 expand_macro_single (char macro, char *uri)
 {
@@ -1313,7 +1425,7 @@ g_desktop_app_info_launch (GAppInfo           *appinfo,
  * @user_setup_data: (closure user_setup): User data for @user_setup
  * @pid_callback: (scope call): Callback for child processes
  * @pid_callback_data: (closure pid_callback): User data for @callback
- * @error: a #GError
+ * @error: return location for a #GError, or %NULL
  *
  * This function performs the equivalent of g_app_info_launch_uris(),
  * but is intended primarily for operating system components that
@@ -1329,6 +1441,8 @@ g_desktop_app_info_launch (GAppInfo           *appinfo,
  * @setup, as well as the process identifier of each child process via
  * @pid_callback.  See g_spawn_async() for more information about the
  * semantics of the @setup function.
+ *
+ * Returns: %TRUE on successful launch, %FALSE otherwise.
  */
 gboolean
 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo            *appinfo,
@@ -1352,26 +1466,26 @@ g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo            *appinfo,
                                                   error);
 }
 
-G_LOCK_DEFINE_STATIC (g_desktop_env);
-static gchar *g_desktop_env = NULL;
-
 /**
  * g_desktop_app_info_set_desktop_env:
  * @desktop_env: a string specifying what desktop this is
  *
  * Sets the name of the desktop that the application is running in.
- * This is used by g_app_info_should_show() to evaluate the
+ * This is used by g_app_info_should_show() and
+ * g_desktop_app_info_get_show_in() to evaluate the
  * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
  * desktop entry fields.
  *
- * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop 
+ * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
  * Menu specification</ulink> recognizes the following:
  * <simplelist>
  *   <member>GNOME</member>
  *   <member>KDE</member>
  *   <member>ROX</member>
  *   <member>XFCE</member>
- *   <member>Old</member> 
+ *   <member>LXDE</member>
+ *   <member>Unity</member>
+ *   <member>Old</member>
  * </simplelist>
  *
  * Should be called only once; subsequent calls are ignored.
@@ -1389,45 +1503,11 @@ static gboolean
 g_desktop_app_info_should_show (GAppInfo *appinfo)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-  gboolean found;
-  const gchar *desktop_env;
-  int i;
 
   if (info->nodisplay)
     return FALSE;
 
-  G_LOCK (g_desktop_env);
-  desktop_env = g_desktop_env;
-  G_UNLOCK (g_desktop_env);
-
-  if (info->only_show_in)
-    {
-      if (desktop_env == NULL)
-       return FALSE;
-      
-      found = FALSE;
-      for (i = 0; info->only_show_in[i] != NULL; i++)
-       {
-         if (strcmp (info->only_show_in[i], desktop_env) == 0)
-           {
-             found = TRUE;
-             break;
-           }
-       }
-      if (!found)
-       return FALSE;
-    }
-
-  if (info->not_show_in && desktop_env)
-    {
-      for (i = 0; info->not_show_in[i] != NULL; i++)
-       {
-         if (strcmp (info->not_show_in[i], desktop_env) == 0)
-           return FALSE;
-       }
-    }
-  
-  return TRUE;
+  return g_desktop_app_info_get_show_in (info, NULL);
 }
 
 typedef enum {
@@ -1476,9 +1556,8 @@ update_mimeapps_list (const char  *desktop_id,
 {
   char *dirname, *filename, *string;
   GKeyFile *key_file;
-  gboolean load_succeeded, res, explicit_default;
+  gboolean load_succeeded, res;
   char **old_list, **list;
-  GList *system_list;
   gsize length, data_size;
   char *data;
   int i, j, k;
@@ -1497,7 +1576,10 @@ update_mimeapps_list (const char  *desktop_id,
 
   key_file = g_key_file_new ();
   load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
-  if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
+  if (!load_succeeded ||
+      (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
+       !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
+       !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
     {
       g_key_file_free (key_file);
       key_file = g_key_file_new ();
@@ -1514,8 +1596,6 @@ update_mimeapps_list (const char  *desktop_id,
       content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
     }
 
-  explicit_default = FALSE;
-
   for (k = 0; content_types && content_types[k]; k++)
     {
       /* set as default, if requested so */
@@ -1540,14 +1620,10 @@ update_mimeapps_list (const char  *desktop_id,
                                content_types[k],
                                NULL);
       else
-        {
-          g_key_file_set_string (key_file,
-                                 DEFAULT_APPLICATIONS_GROUP,
-                                 content_types[k],
-                                 string);
-
-          explicit_default = TRUE;
-        }
+        g_key_file_set_string (key_file,
+                               DEFAULT_APPLICATIONS_GROUP,
+                               content_types[k],
+                               string);
 
       g_free (string);
     }
@@ -1618,33 +1694,11 @@ update_mimeapps_list (const char  *desktop_id,
                               content_types[k],
                               NULL);
       else
-        {
-          g_key_file_set_string_list (key_file,
-                                      ADDED_ASSOCIATIONS_GROUP,
-                                      content_types[k],
-                                      (const char * const *)list, i);
-
-          /* if we had no explicit default set, we should add the system default to the
-           * list, to avoid overriding it with applications from this list.
-           */
-          if (!explicit_default)
-            {
-              system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
-
-              if (system_list != NULL)
-                {
-                  string = system_list->data;
-
-                  g_key_file_set_string (key_file,
-                                         DEFAULT_APPLICATIONS_GROUP,
-                                         content_types[k],
-                                         string);
-                }
+        g_key_file_set_string_list (key_file,
+                                    ADDED_ASSOCIATIONS_GROUP,
+                                    content_types[k],
+                                    (const char * const *)list, i);
 
-              g_list_free_full (system_list, g_free);
-            }
-        }
-   
       g_strfreev (list);
     }
   
@@ -1697,18 +1751,18 @@ update_mimeapps_list (const char  *desktop_id,
       g_strfreev (list);
     }
 
-  g_strfreev (content_types);  
+  g_strfreev (content_types);
 
   data = g_key_file_to_data (key_file, &data_size, error);
   g_key_file_free (key_file);
-  
+
   res = g_file_set_contents (filename, data, data_size, error);
 
   mime_info_cache_reload (NULL);
-                         
+
   g_free (filename);
   g_free (data);
-  
+
   return res;
 }
 
@@ -1722,6 +1776,13 @@ g_desktop_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
   if (!g_desktop_app_info_ensure_saved (info, error))
     return FALSE;
 
+  if (!info->desktop_id)
+    {
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           _("Application information lacks an identifier"));
+      return FALSE;
+    }
+
   /* both add support for the content type and set as last used */
   return update_mimeapps_list (info->desktop_id, content_type,
                                UPDATE_MIME_SET_NON_DEFAULT |
@@ -1737,8 +1798,15 @@ g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
   if (!g_desktop_app_info_ensure_saved (info, error))
-    return FALSE;  
-  
+    return FALSE;
+
+  if (!info->desktop_id)
+    {
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           _("Application information lacks an identifier"));
+      return FALSE;
+    }
+
   return update_mimeapps_list (info->desktop_id, content_type,
                                UPDATE_MIME_SET_DEFAULT,
                                error);
@@ -1920,6 +1988,9 @@ g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
   if (info->terminal) 
     g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
                            G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
+  if (info->nodisplay)
+    g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                           G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
 
   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
                         G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
@@ -2027,7 +2098,7 @@ g_desktop_app_info_delete (GAppInfo *appinfo)
  * @commandline: the commandline to use
  * @application_name: (allow-none): the application name, or %NULL to use @commandline
  * @flags: flags that can specify details of the created #GAppInfo
- * @error: a #GError location to store the error occuring, %NULL to ignore.
+ * @error: a #GError location to store the error occurring, %NULL to ignore.
  *
  * Creates a new #GAppInfo from the given information.
  *
@@ -2125,7 +2196,7 @@ app_info_in_list (GAppInfo *info,
  * those applications which claim to support the given content type exactly,
  * and not by MIME type subclassing.
  * Note that the first application of the list is the last used one, i.e.
- * the last one for which #g_app_info_set_as_last_used_for_type has been
+ * the last one for which g_app_info_set_as_last_used_for_type() has been
  * called.
  *
  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
@@ -2216,8 +2287,11 @@ g_app_info_get_fallback_for_type (const gchar *content_type)
 /**
  * g_app_info_get_all_for_type:
  * @content_type: the content type to find a #GAppInfo for
- * 
- * Gets a list of all #GAppInfos for a given content type.
+ *
+ * Gets a list of all #GAppInfos for a given content type,
+ * including the recommended and fallback #GAppInfos. See
+ * g_app_info_get_recommended_for_type() and
+ * g_app_info_get_fallback_for_type().
  *
  * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
  *     for given @content_type or %NULL on error.
@@ -2268,12 +2342,13 @@ g_app_info_get_all_for_type (const char *content_type)
 
 /**
  * g_app_info_reset_type_associations:
- * @content_type: a content type 
+ * @content_type: a content type
  *
  * Removes all changes to the type associations done by
- * g_app_info_set_as_default_for_type(), 
- * g_app_info_set_as_default_for_extension(), 
- * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
+ * g_app_info_set_as_default_for_type(),
+ * g_app_info_set_as_default_for_extension(),
+ * g_app_info_add_supports_type() or
+ * g_app_info_remove_supports_type().
  *
  * Since: 2.20
  */
@@ -2290,12 +2365,12 @@ g_app_info_reset_type_associations (const char *content_type)
  * @content_type: the content type to find a #GAppInfo for
  * @must_support_uris: if %TRUE, the #GAppInfo is expected to
  *     support URIs
- * 
- * Gets the #GAppInfo that corresponds to a given content type.
+ *
+ * Gets the default #GAppInfo for a given content type.
  *
  * Returns: (transfer full): #GAppInfo for given @content_type or
  *     %NULL on error.
- **/
+ */
 GAppInfo *
 g_app_info_get_default_for_type (const char *content_type,
                                 gboolean    must_support_uris)
@@ -2361,13 +2436,13 @@ g_app_info_get_default_for_type (const char *content_type,
  * g_app_info_get_default_for_uri_scheme:
  * @uri_scheme: a string containing a URI scheme.
  *
- * Gets the default application for launching applications 
- * using this URI scheme. A URI scheme is the initial part 
- * of the URI, up to but not including the ':', e.g. "http", 
+ * Gets the default application for handling URIs with
+ * the given URI scheme. A URI scheme is the initial part
+ * of the URI, up to but not including the ':', e.g. "http",
  * "ftp" or "sip".
- * 
+ *
  * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
- **/
+ */
 GAppInfo *
 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
 {
@@ -3115,6 +3190,7 @@ get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
   GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
   MimeInfoCacheDir *dir;
   char *mime_type, *default_entry = NULL;
+  char *old_default_entry = NULL;
   const char *entry;
   char **mime_types;
   char **default_entries;
@@ -3205,7 +3281,12 @@ get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
          /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
          default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
          for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
-           desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
+            {
+              if (default_entry == NULL && old_default_entry == NULL)
+                old_default_entry = g_strdup (default_entries[j]);
+
+              desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
+            }
        }
 
       /* Go through all entries that support the mimetype */
@@ -3225,6 +3306,17 @@ get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
 
   g_strfreev (mime_types);
 
+  /* If we have no default from mimeapps.list, take it from
+   * defaults.list intead.
+   *
+   * If we do have a default from mimeapps.list, free any one that came
+   * from defaults.list.
+   */
+  if (default_entry == NULL)
+    default_entry = old_default_entry;
+  else
+    g_free (old_default_entry);
+
   if (explicit_default != NULL)
     *explicit_default = default_entry;
   else