Add GI annotations to GAppInfo and GDesktopAppInfo
[platform/upstream/glib.git] / gio / gdesktopappinfo.c
index d20f366..61a1a48 100644 (file)
@@ -21,7 +21,7 @@
  * Author: Alexander Larsson <alexl@redhat.com>
  */
 
-#include <config.h>
+#include "config.h"
 
 #include <errno.h>
 #include <string.h>
 #include <crt_externs.h>
 #endif
 
+#undef G_DISABLE_DEPRECATED
+
 #include "gcontenttypeprivate.h"
 #include "gdesktopappinfo.h"
+#include "gfile.h"
 #include "gioerror.h"
 #include "gthemedicon.h"
 #include "gfileicon.h"
 #include <glib/gstdio.h>
 #include "glibintl.h"
+#include "giomodule-priv.h"
+#include "gappinfo.h"
 
-#include "gioalias.h"
 
 /**
  * SECTION:gdesktopappinfo
+ * @title: GDesktopAppInfo
  * @short_description: Application information from desktop files
- * @include: gio/gdesktopappinfo.h 
- * 
+ * @include: gio/gdesktopappinfo.h
+ *
  * #GDesktopAppInfo is an implementation of #GAppInfo based on
  * desktop files.
  *
- **/
+ * Note that <filename>&lt;gio/gdesktopappinfo.h&gt;</filename> belongs to
+ * the UNIX-specific GIO interfaces, thus you have to use the
+ * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
+ */
 
-#define DEFAULT_APPLICATIONS_GROUP  "Default Applications" 
+#define DEFAULT_APPLICATIONS_GROUP  "Default Applications"
+#define ADDED_ASSOCIATIONS_GROUP    "Added Associations"
+#define REMOVED_ASSOCIATIONS_GROUP  "Removed Associations"
 #define MIME_CACHE_GROUP            "MIME Cache"
+#define FULL_NAME_KEY               "X-GNOME-FullName"
 
-static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
-
-static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
-static void mime_info_cache_reload (const char *dir);
+static void     g_desktop_app_info_iface_init         (GAppInfoIface    *iface);
+static GList *  get_all_desktop_entries_for_mime_type (const char       *base_mime_type,
+                                                      const char      **except,
+                                                      gboolean          include_fallback,
+                                                       char            **explicit_default);
+static void     mime_info_cache_reload                (const char       *dir);
+static gboolean g_desktop_app_info_ensure_saved       (GDesktopAppInfo  *info,
+                                                      GError          **error);
 
 /**
  * GDesktopAppInfo:
@@ -74,6 +89,7 @@ struct _GDesktopAppInfo
 
   char *name;
   /* FIXME: what about GenericName ? */
+  char *fullname;
   char *comment;
   char *icon_name;
   GIcon *icon;
@@ -88,9 +104,18 @@ struct _GDesktopAppInfo
   guint hidden          : 1;
   guint terminal        : 1;
   guint startup_notify  : 1;
+  guint no_fuse         : 1;
   /* FIXME: what about StartupWMClass ? */
 };
 
+typedef enum {
+  UPDATE_MIME_NONE = 1 << 0,
+  UPDATE_MIME_SET_DEFAULT = 1 << 1,
+  UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
+  UPDATE_MIME_REMOVE = 1 << 3,
+  UPDATE_MIME_SET_LAST_USED = 1 << 4,
+} UpdateMimeFlags;
+
 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))
@@ -136,6 +161,7 @@ g_desktop_app_info_finalize (GObject *object)
   g_free (info->desktop_id);
   g_free (info->filename);
   g_free (info->name);
+  g_free (info->fullname);
   g_free (info->comment);
   g_free (info->icon_name);
   if (info->icon)
@@ -163,39 +189,44 @@ g_desktop_app_info_init (GDesktopAppInfo *local)
 {
 }
 
+static char *
+binary_from_exec (const char *exec)
+{
+  const char *p, *start;
+  
+  p = exec;
+  while (*p == ' ')
+    p++;
+  start = p;
+  while (*p != ' ' && *p != 0)
+    p++;
+  
+  return g_strndup (start, p - start);
+  
+}
+
 /**
- * g_desktop_app_info_new_from_filename:
- * @filename: a string containing a file name.
+ * g_desktop_app_info_new_from_keyfile:
+ * @key_file: an opened #GKeyFile
  * 
  * Creates a new #GDesktopAppInfo.
  *
  * Returns: a new #GDesktopAppInfo or %NULL on error.
+ *
+ * Since: 2.18
  **/
 GDesktopAppInfo *
-g_desktop_app_info_new_from_filename (const char *filename)
+g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
 {
   GDesktopAppInfo *info;
-  GKeyFile *key_file;
   char *start_group;
   char *type;
   char *try_exec;
   
-  key_file = g_key_file_new ();
-  
-  if (!g_key_file_load_from_file (key_file,
-                                 filename,
-                                 G_KEY_FILE_NONE,
-                                 NULL))
-    {
-      g_key_file_free (key_file);
-      return NULL;
-    }
-
   start_group = g_key_file_get_start_group (key_file);
   if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
     {
       g_free (start_group);
-      g_key_file_free (key_file);
       return NULL;
     }
   g_free (start_group);
@@ -207,7 +238,6 @@ g_desktop_app_info_new_from_filename (const char *filename)
   if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
     {
       g_free (type);
-      g_key_file_free (key_file);
       return NULL;
     }
   g_free (type);
@@ -216,23 +246,23 @@ g_desktop_app_info_new_from_filename (const char *filename)
                                     G_KEY_FILE_DESKTOP_GROUP,
                                     G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
                                     NULL);
-  if (try_exec)
+  if (try_exec && try_exec[0] != '\0')
     {
       char *t;
       t = g_find_program_in_path (try_exec);
       if (t == NULL)
        {
          g_free (try_exec);
-         g_key_file_free (key_file);
          return NULL;
        }
       g_free (t);
     }
 
   info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
-  info->filename = g_strdup (filename);
+  info->filename = NULL;
 
   info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
+  info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, 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);
@@ -243,9 +273,8 @@ g_desktop_app_info_new_from_filename (const char *filename)
   info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
   info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
   info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
+  info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
   info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
-
-  g_key_file_free (key_file);
   
   info->icon = NULL;
   if (info->icon_name)
@@ -259,23 +288,60 @@ g_desktop_app_info_new_from_filename (const char *filename)
          g_object_unref (file);
        }
       else
-       info->icon = g_themed_icon_new (info->icon_name);
+        {
+          char *p;
+
+          /* Work around a common mistake in desktop files */    
+          if ((p = strrchr (info->icon_name, '.')) != NULL &&
+              (strcmp (p, ".png") == 0 ||
+               strcmp (p, ".xpm") == 0 ||
+               strcmp (p, ".svg") == 0)) 
+            *p = 0;
+
+         info->icon = g_themed_icon_new (info->icon_name);
+        }
     }
   
   if (info->exec)
+    info->binary = binary_from_exec (info->exec);
+  
+  if (info->path && info->path[0] == '\0')
     {
-      char *p, *start;
-
-      p = info->exec;
-      while (*p == ' ')
-       p++;
-      start = p;
-      while (*p != ' ' && *p != 0)
-       p++;
-      
-      info->binary = g_strndup (start, p - start);
+      g_free (info->path);
+      info->path = NULL;
     }
+
+  return info;
+}
+
+/**
+ * g_desktop_app_info_new_from_filename:
+ * @filename: the path of a desktop file, in the GLib filename encoding
+ * 
+ * Creates a new #GDesktopAppInfo.
+ *
+ * Returns: a new #GDesktopAppInfo or %NULL on error.
+ **/
+GDesktopAppInfo *
+g_desktop_app_info_new_from_filename (const char *filename)
+{
+  GKeyFile *key_file;
+  GDesktopAppInfo *info = NULL;
+
+  key_file = g_key_file_new ();
   
+  if (g_key_file_load_from_file (key_file,
+                                filename,
+                                G_KEY_FILE_NONE,
+                                NULL))
+    {
+      info = g_desktop_app_info_new_from_keyfile (key_file);
+      if (info)
+        info->filename = g_strdup (filename);
+    }  
+
+  g_key_file_free (key_file);
+
   return info;
 }
 
@@ -283,10 +349,20 @@ g_desktop_app_info_new_from_filename (const char *filename)
  * g_desktop_app_info_new:
  * @desktop_id: the desktop file id
  * 
- * Creates a new #GDesktopAppInfo.
+ * Creates a new #GDesktopAppInfo based on a desktop file id. 
+ *
+ * A desktop file id is the basename of the desktop file, including the 
+ * .desktop extension. GIO is looking for a desktop file with this name 
+ * in the <filename>applications</filename> subdirectories of the XDG data
+ * directories (i.e. the directories specified in the 
+ * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment 
+ * variables). GIO also supports the prefix-to-subdirectory mapping that is
+ * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink> 
+ * (i.e. a desktop id of kde-foo.desktop will match
+ * <filename>/usr/share/applications/kde/foo.desktop</filename>).
  * 
  * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
- **/
+ */
 GDesktopAppInfo *
 g_desktop_app_info_new (const char *desktop_id)
 {
@@ -354,10 +430,12 @@ g_desktop_app_info_dup (GAppInfo *appinfo)
   new_info->desktop_id = g_strdup (info->desktop_id);
   
   new_info->name = g_strdup (info->name);
+  new_info->fullname = g_strdup (info->fullname);
   new_info->comment = g_strdup (info->comment);
   new_info->nodisplay = info->nodisplay;
   new_info->icon_name = g_strdup (info->icon_name);
-  new_info->icon = g_object_ref (info->icon);
+  if (info->icon)
+    new_info->icon = g_object_ref (info->icon);
   new_info->only_show_in = g_strdupv (info->only_show_in);
   new_info->not_show_in = g_strdupv (info->not_show_in);
   new_info->try_exec = g_strdup (info->try_exec);
@@ -380,7 +458,7 @@ g_desktop_app_info_equal (GAppInfo *appinfo1,
 
   if (info1->desktop_id == NULL ||
       info2->desktop_id == NULL)
-    return FALSE;
+    return info1 == info2;
 
   return strcmp (info1->desktop_id, info2->desktop_id) == 0;
 }
@@ -403,6 +481,16 @@ g_desktop_app_info_get_name (GAppInfo *appinfo)
   return info->name;
 }
 
+static const char *
+g_desktop_app_info_get_display_name (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+  if (info->fullname == NULL)
+    return g_desktop_app_info_get_name (appinfo);
+  return info->fullname;
+}
+
 /**
  * g_desktop_app_info_get_is_hidden:
  * @info: a #GDesktopAppInfo.
@@ -418,6 +506,23 @@ g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
   return info->hidden;
 }
 
+/**
+ * g_desktop_app_info_get_filename:
+ * @info: a #GDesktopAppInfo
+ *
+ * When @info was created from a known filename, return it.  In some
+ * situations such as the #GDesktopAppInfo returned from
+ * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
+ *
+ * Returns: The full path to the file for @info, or %NULL if not known.
+ * Since: 2.24
+ */
+const char *
+g_desktop_app_info_get_filename (GDesktopAppInfo *info)
+{
+  return info->filename;
+}
+
 static const char *
 g_desktop_app_info_get_description (GAppInfo *appinfo)
 {
@@ -434,6 +539,14 @@ g_desktop_app_info_get_executable (GAppInfo *appinfo)
   return info->binary;
 }
 
+static const char *
+g_desktop_app_info_get_commandline (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  
+  return info->exec;
+}
+
 static GIcon *
 g_desktop_app_info_get_icon (GAppInfo *appinfo)
 {
@@ -443,13 +556,15 @@ g_desktop_app_info_get_icon (GAppInfo *appinfo)
 }
 
 static char *
-expand_macro_single (char macro, GFile *file)
+expand_macro_single (char macro, char *uri)
 {
+  GFile *file;
   char *result = NULL;
-  char *uri, *path;
+  char *path, *name;
 
+  file = g_file_new_for_uri (uri);
   path = g_file_get_path (file);
-  uri = g_file_get_uri (file);
+  g_object_unref (file);
   
   switch (macro)
     {
@@ -465,17 +580,24 @@ expand_macro_single (char macro, GFile *file)
     case 'd':
     case 'D':
       if (path)
-       result = g_shell_quote (g_path_get_dirname (path));
+        {
+          name = g_path_get_dirname (path);
+         result = g_shell_quote (name);
+          g_free (name);
+        }
       break;
     case 'n':
     case 'N':
       if (path)
-       result = g_shell_quote (g_path_get_basename (path));
+        {
+          name = g_path_get_basename (path);
+         result = g_shell_quote (name);
+          g_free (name);
+        }
       break;
     }
 
   g_free (path);
-  g_free (uri);
   
   return result;
 }
@@ -484,28 +606,68 @@ static void
 expand_macro (char              macro, 
               GString          *exec, 
               GDesktopAppInfo  *info, 
-              GList           **file_list)
+              GList           **uri_list)
 {
-  GList *files = *file_list;
+  GList *uris = *uri_list;
   char *expanded;
-  
+  gboolean force_file_uri;
+  char force_file_uri_macro;
+  char *uri;
+
   g_return_if_fail (exec != NULL);
-  
+
+  /* On %u and %U, pass POSIX file path pointing to the URI via
+   * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
+   * running or the URI doesn't have a POSIX file path via FUSE
+   * we'll just pass the URI.
+   */
+  force_file_uri_macro = macro;
+  force_file_uri = FALSE;
+  if (!info->no_fuse)
+    {
+      switch (macro)
+       {
+       case 'u':
+         force_file_uri_macro = 'f';
+         force_file_uri = TRUE;
+         break;
+       case 'U':
+         force_file_uri_macro = 'F';
+         force_file_uri = TRUE;
+         break;
+       default:
+         break;
+       }
+    }
+
   switch (macro)
     {
     case 'u':
     case 'f':
     case 'd':
     case 'n':
-      if (files)
+      if (uris)
        {
-         expanded = expand_macro_single (macro, files->data);
+         uri = uris->data;
+          if (!force_file_uri ||
+             /* Pass URI if it contains an anchor */
+             strchr (uri, '#') != NULL)
+            {
+              expanded = expand_macro_single (macro, uri);
+            }
+          else
+            {
+              expanded = expand_macro_single (force_file_uri_macro, uri);
+              if (expanded == NULL)
+                expanded = expand_macro_single (macro, uri);
+            }
+
          if (expanded)
            {
              g_string_append (exec, expanded);
              g_free (expanded);
            }
-         files = files->next;
+         uris = uris->next;
        }
 
       break;
@@ -514,18 +676,32 @@ expand_macro (char              macro,
     case 'F':
     case 'D':
     case 'N':
-      while (files)
+      while (uris)
        {
-         expanded = expand_macro_single (macro, files->data);
+         uri = uris->data;
+         
+          if (!force_file_uri ||
+             /* Pass URI if it contains an anchor */
+             strchr (uri, '#') != NULL)
+            {
+              expanded = expand_macro_single (macro, uri);
+            }
+          else
+            {
+              expanded = expand_macro_single (force_file_uri_macro, uri);
+              if (expanded == NULL)
+                expanded = expand_macro_single (macro, uri);
+            }
+
          if (expanded)
            {
              g_string_append (exec, expanded);
              g_free (expanded);
            }
          
-         files = files->next;
+         uris = uris->next;
          
-         if (files != NULL && expanded)
+         if (uris != NULL && expanded)
            g_string_append_c (exec, ' ');
        }
 
@@ -535,18 +711,28 @@ expand_macro (char              macro,
       if (info->icon_name)
        {
          g_string_append (exec, "--icon ");
-         g_string_append (exec, info->icon_name);
+          expanded = g_shell_quote (info->icon_name);
+         g_string_append (exec, expanded);
+          g_free (expanded);
        }
       break;
 
     case 'c':
       if (info->name) 
-       g_string_append (exec, info->name);
+        {
+          expanded = g_shell_quote (info->name);
+         g_string_append (exec, expanded);
+          g_free (expanded);
+        }
       break;
 
     case 'k':
       if (info->filename) 
-       g_string_append (exec, info->filename);
+        {
+          expanded = g_shell_quote (info->filename);
+         g_string_append (exec, expanded);
+          g_free (expanded);
+        }
       break;
 
     case 'm': /* deprecated */
@@ -557,49 +743,51 @@ expand_macro (char              macro,
       break;
     }
   
-  *file_list = files;
+  *uri_list = uris;
 }
 
 static gboolean
 expand_application_parameters (GDesktopAppInfo   *info,
-                              GList            **files,
+                              GList            **uris,
                               int               *argc,
                               char            ***argv,
                               GError           **error)
 {
-  GList *file_list = *files;
+  GList *uri_list = *uris;
   const char *p = info->exec;
-  GString *expanded_exec = g_string_new (NULL);
+  GString *expanded_exec;
   gboolean res;
-  
+
   if (info->exec == NULL)
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                  _("Desktop file didn't specify Exec field"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           _("Desktop file didn't specify Exec field"));
       return FALSE;
     }
-  
+
+  expanded_exec = g_string_new (NULL);
+
   while (*p)
     {
       if (p[0] == '%' && p[1] != '\0')
        {
-         expand_macro (p[1], expanded_exec, info, files);
+         expand_macro (p[1], expanded_exec, info, uris);
          p++;
        }
       else
        g_string_append_c (expanded_exec, *p);
-      
+
       p++;
     }
-  
+
   /* No file substitutions */
-  if (file_list == *files && file_list != NULL)
+  if (uri_list == *uris && uri_list != NULL)
     {
       /* If there is no macro default to %f. This is also what KDE does */
       g_string_append_c (expanded_exec, ' ');
-      expand_macro ('f', expanded_exec, info, files);
+      expand_macro ('f', expanded_exec, info, uris);
     }
-  
+
   res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
   g_string_free (expanded_exec, TRUE);
   return res;
@@ -691,258 +879,135 @@ prepend_terminal_to_vector (int    *argc,
 #endif /* G_OS_WIN32 */
 }
 
-/* '=' is the new '\0'.
- * DO NOT CALL unless at least one string ends with '='
- */
-static gboolean
-is_env (const char *a,
-       const char *b)
+static GList *
+uri_list_segment_to_files (GList *start,
+                          GList *end)
 {
-  while (*a == *b)
-  {
-    if (*a == 0 || *b == 0)
-      return FALSE;
-    
-    if (*a == '=')
-      return TRUE;
+  GList *res;
+  GFile *file;
 
-    a++;
-    b++;
-  }
+  res = NULL;
+  while (start != NULL && start != end)
+    {
+      file = g_file_new_for_uri ((char *)start->data);
+      res = g_list_prepend (res, file);
+      start = start->next;
+    }
 
-  return FALSE;
+  return g_list_reverse (res);
 }
 
-/* free with g_strfreev */
-static char **
-replace_env_var (char       **old_environ,
-                const char  *env_var,
-                const char  *new_value)
+typedef struct
 {
-  int length, new_length;
-  int index, new_index;
-  char **new_environ;
-  int i, new_i;
+  char *display;
+  char *sn_id;
+  char *desktop_file;
+} ChildSetupData;
 
-  /* do two things at once:
-   *  - discover the length of the environment ('length')
-   *  - find the location (if any) of the env var ('index')
-   */
-  index = -1;
-  for (length = 0; old_environ[length]; length++)
-    {
-      /* if we already have it in our environment, replace */
-      if (is_env (old_environ[length], env_var))
-       index = length;
-    }
+static void
+child_setup (gpointer user_data)
+{
+  ChildSetupData *data = user_data;
 
-  
-  /* no current env var, no desired env value.
-   * this is easy :)
-   */
-  if (new_value == NULL && index == -1)
-    return old_environ;
-
-  /* in all cases now, we will be using a modified environment.
-   * determine its length and allocated it.
-   * 
-   * after this block:
-   *   new_index   = location to insert, if any
-   *   new_length  = length of the new array
-   *   new_environ = the pointer array for the new environment
-   */
-  
-  if (new_value == NULL && index >= 0)
-    {
-      /* in this case, we will be removing an entry */
-      new_length = length - 1;
-      new_index = -1;
-    }
-  else if (new_value != NULL && index < 0)
-    {
-      /* in this case, we will be adding an entry to the end */
-      new_length = length + 1;
-      new_index = length;
-    }
-  else
-    /* in this case, we will be replacing the existing entry */
-    {
-      new_length = length;
-      new_index = index;
-    }
+  if (data->display)
+    g_setenv ("DISPLAY", data->display, TRUE);
 
-  new_environ = g_malloc (sizeof (char *) * (new_length + 1));
-  new_environ[new_length] = NULL;
+  if (data->sn_id)
+    g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
 
-  /* now we do the copying.
-   * for each entry in the new environment, we decide what to do
-   */
-  
-  i = 0;
-  for (new_i = 0; new_i < new_length; new_i++)
+  if (data->desktop_file)
     {
-      if (new_i == new_index)
-       {
-         /* insert our new item */
-         new_environ[new_i] = g_strconcat (env_var,
-                                           "=",
-                                           new_value,
-                                           NULL);
-         
-         /* if we had an old entry, skip it now */
-         if (index >= 0)
-           i++;
-       }
-      else
-       {
-         /* if this is the old DESKTOP_STARTUP_ID, skip it */
-         if (i == index)
-           i++;
-         
-         /* copy an old item */
-         new_environ[new_i] = g_strdup (old_environ[i]);
-         i++;
-       }
-    }
-
-  g_strfreev (old_environ);
-  
-  return new_environ;
-}
+      gchar pid[20];
 
-static GList *
-dup_list_segment (GList *start,
-                 GList *end)
-{
-  GList *res;
+      g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
 
-  res = NULL;
-  while (start != NULL && start != end)
-    {
-      res = g_list_prepend (res, start->data);
-      start = start->next;
+      g_snprintf (pid, 20, "%ld", (long)getpid ());
+      g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
     }
-
-  return g_list_reverse (res);
 }
 
-#ifdef HAVE__NSGETENVIRON
-#define environ (*_NSGetEnviron())
-#elif !defined(G_OS_WIN32)
-
-/* According to the Single Unix Specification, environ is not in 
- *  * any system header, although unistd.h often declares it.
- *   */
-extern char **environ;
-#endif
-
 static gboolean
-g_desktop_app_info_launch (GAppInfo           *appinfo,
-                          GList              *files,
-                          GAppLaunchContext  *launch_context,
-                          GError            **error)
+g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
+                               GList              *uris,
+                               GAppLaunchContext  *launch_context,
+                               GError            **error)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
   gboolean completed = FALSE;
-  GList *old_files;
+  GList *old_uris;
   GList *launched_files;
-  char **envp;
   char **argv;
   int argc;
-  char *display;
-  char *sn_id;
+  ChildSetupData data;
 
   g_return_val_if_fail (appinfo != NULL, FALSE);
 
   argv = NULL;
-  envp = NULL;
-      
-  do 
+
+  do
     {
-      old_files = files;
-      if (!expand_application_parameters (info, &files,
+      old_uris = uris;
+      if (!expand_application_parameters (info, &uris,
                                          &argc, &argv, error))
        goto out;
       
       if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
        {
-         g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                      _("Unable to find terminal required for application"));
+         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               _("Unable to find terminal required for application"));
          goto out;
        }
 
-      sn_id = NULL;
+      data.display = NULL;
+      data.sn_id = NULL;
+      data.desktop_file = info->filename;
+
       if (launch_context)
        {
-         launched_files = dup_list_segment (old_files, files);
-         
-         display = g_app_launch_context_get_display (launch_context,
-                                                     appinfo,
-                                                     launched_files);
+         launched_files = uri_list_segment_to_files (old_uris, uris);
 
-         sn_id = NULL;
-         if (info->startup_notify)
-           sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
-                                                               appinfo,
-                                                               launched_files);
-         
-         if (display || sn_id)
-           {
-#ifdef G_OS_WIN32
-             /* FIXME */
-             envp = g_new0 (char *, 1);
-#else
-             envp = g_strdupv (environ);
-#endif
-             
-             if (display)
-               envp = replace_env_var (envp,
-                                       "DISPLAY",
-                                       display);
-             
-             if (sn_id)
-               envp = replace_env_var (envp,
-                                       "DESKTOP_STARTUP_ID",
-                                       sn_id);
-           }
+         data.display = g_app_launch_context_get_display (launch_context,
+                                                          appinfo,
+                                                          launched_files);
 
-         g_free (display);
-         
+         if (info->startup_notify)
+           data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
+                                                                    appinfo,
+                                                                    launched_files);
+         g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
          g_list_free (launched_files);
        }
-      
-      if (!g_spawn_async (info->path,  /* working directory */
+
+      if (!g_spawn_async (info->path,
                          argv,
-                         envp,
-                         G_SPAWN_SEARCH_PATH /* flags */,
-                         NULL /* child_setup */,
-                         NULL /* data */,
-                         NULL /* child_pid */,
+                         NULL,
+                         G_SPAWN_SEARCH_PATH,
+                         child_setup,
+                         &data,
+                         NULL,
                          error))
        {
-         if (sn_id)
-           {
-             g_app_launch_context_launch_failed (launch_context, sn_id);
-             g_free (sn_id);
-           }
+         if (data.sn_id)
+           g_app_launch_context_launch_failed (launch_context, data.sn_id);
+
+         g_free (data.sn_id);
+         g_free (data.display);
+
          goto out;
        }
 
-      
-      g_free (sn_id);
-      
-      g_strfreev (envp);
+      g_free (data.sn_id);
+      g_free (data.display);
+
       g_strfreev (argv);
-      envp = NULL;
       argv = NULL;
     }
-  while (files != NULL);
+  while (uris != NULL);
 
   completed = TRUE;
 
  out:
   g_strfreev (argv);
-  g_strfreev (envp);
 
   return completed;
 }
@@ -968,32 +1033,29 @@ g_desktop_app_info_supports_files (GAppInfo *appinfo)
 }
 
 static gboolean
-g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
-                               GList              *uris,
-                               GAppLaunchContext  *launch_context,
-                               GError            **error)
+g_desktop_app_info_launch (GAppInfo           *appinfo,
+                          GList              *files,
+                          GAppLaunchContext  *launch_context,
+                          GError            **error)
 {
-  GList *files;
-  GFile *file;
+  GList *uris;
+  char *uri;
   gboolean res;
 
-  files = NULL;
-  while (uris)
+  uris = NULL;
+  while (files)
     {
-      file = g_file_new_for_uri (uris->data);
-      if (file == NULL)
-       g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
-      
-      if (file)
-       files = g_list_prepend (files, file);
+      uri = g_file_get_uri (files->data);
+      uris = g_list_prepend (uris, uri);
+      files = files->next;
     }
   
-  files = g_list_reverse (files);
+  uris = g_list_reverse (uris);
   
-  res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
+  res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
   
-  g_list_foreach  (files, (GFunc)g_object_unref, NULL);
-  g_list_free (files);
+  g_list_foreach  (uris, (GFunc)g_free, NULL);
+  g_list_free (uris);
   
   return res;
 }
@@ -1086,7 +1148,7 @@ ensure_dir (DirType   type,
             GError  **error)
 {
   char *path, *display_name;
-  int err;
+  int errsv;
 
   if (type == APP_DIR)
     path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
@@ -1097,16 +1159,16 @@ ensure_dir (DirType   type,
   if (g_mkdir_with_parents (path, 0700) == 0)
     return path;
 
-  err = errno;
+  errsv = errno;
   display_name = g_filename_display_name (path);
   if (type == APP_DIR)
-    g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
+    g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
                  _("Can't create user application configuration folder %s: %s"),
-                 display_name, g_strerror (err));
+                 display_name, g_strerror (errsv));
   else
-    g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
+    g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
                  _("Can't create user MIME configuration folder %s: %s"),
-                 display_name, g_strerror (err));
+                 display_name, g_strerror (errsv));
 
   g_free (display_name);
   g_free (path);
@@ -1115,87 +1177,279 @@ ensure_dir (DirType   type,
 }
 
 static gboolean
-update_default_list (const char  *desktop_id, 
-                     const char  *content_type, 
-                     gboolean     add, 
-                     GError     **error)
+update_mimeapps_list (const char  *desktop_id, 
+                     const char  *content_type,
+                      UpdateMimeFlags flags,
+                     GError     **error)
 {
-  char *dirname, *filename;
+  char *dirname, *filename, *string;
   GKeyFile *key_file;
-  gboolean load_succeeded, res;
-  char **old_list;
-  char **list;
+  gboolean load_succeeded, res, explicit_default;
+  char **old_list, **list;
+  GList *system_list;
   gsize length, data_size;
   char *data;
-  int i, j;
+  int i, j, k;
+  char **content_types;
+
+  /* Don't add both at start and end */
+  g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
+              (flags & UPDATE_MIME_SET_NON_DEFAULT)));
 
   dirname = ensure_dir (APP_DIR, error);
   if (!dirname)
     return FALSE;
 
-  filename = g_build_filename (dirname, "defaults.list", NULL);
+  filename = g_build_filename (dirname, "mimeapps.list", NULL);
   g_free (dirname);
 
   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, DEFAULT_APPLICATIONS_GROUP))
+  if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
     {
       g_key_file_free (key_file);
       key_file = g_key_file_new ();
     }
 
-  length = 0;
-  old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
-                                        content_type, &length, NULL);
+  if (content_type)
+    {
+      content_types = g_new (char *, 2);
+      content_types[0] = g_strdup (content_type);
+      content_types[1] = NULL;
+    }
+  else
+    {
+      content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
+    }
 
-  list = g_new (char *, 1 + length + 1);
+  explicit_default = FALSE;
 
-  i = 0;
-  if (add)
-    list[i++] = g_strdup (desktop_id);
-  if (old_list)
+  for (k = 0; content_types && content_types[k]; k++)
     {
-      for (j = 0; old_list[j] != NULL; j++)
-       {
-         if (strcmp (old_list[j], desktop_id) != 0)
-           list[i++] = g_strdup (old_list[j]);
-       }
-    }
-  list[i] = NULL;
-  
-  g_strfreev (old_list);
+      /* set as default, if requested so */
+      string = g_key_file_get_string (key_file,
+                                      DEFAULT_APPLICATIONS_GROUP,
+                                      content_types[k],
+                                      NULL);
+
+      if (g_strcmp0 (string, desktop_id) != 0 &&
+          (flags & UPDATE_MIME_SET_DEFAULT))
+        {
+          g_free (string);
+          string = g_strdup (desktop_id);
+
+          /* add in the non-default list too, if it's not already there */
+          flags |= UPDATE_MIME_SET_NON_DEFAULT;
+        }
 
-  g_key_file_set_string_list (key_file,
-                             DEFAULT_APPLICATIONS_GROUP,
-                             content_type,
-                             (const char * const *)list, i);
+      if (string == NULL || desktop_id == NULL)
+        g_key_file_remove_key (key_file,
+                               DEFAULT_APPLICATIONS_GROUP,
+                               content_types[k],
+                               NULL);
+      else
+        {
+          g_key_file_set_string (key_file,
+                                 DEFAULT_APPLICATIONS_GROUP,
+                                 content_types[k],
+                                 string);
+
+          explicit_default = TRUE;
+        }
 
-  g_strfreev (list);
+      g_free (string);
+    }
+
+  if (content_type)
+    {
+      /* reuse the list from above */
+    }
+  else
+    {
+      g_strfreev (content_types);
+      content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
+    }
   
-  data = g_key_file_to_data (key_file, &data_size, error);
-  g_key_file_free (key_file);
+  for (k = 0; content_types && content_types[k]; k++)
+    { 
+      /* Add to the right place in the list */
   
-  res = g_file_set_contents (filename, data, data_size, error);
+      length = 0;
+      old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
+                                            content_types[k], &length, NULL);
 
-  mime_info_cache_reload (NULL);
-                         
-  g_free (filename);
-  g_free (data);
-  
-  return res;
-}
+      list = g_new (char *, 1 + length + 1);
 
-static gboolean
-g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
-                                           const char  *content_type,
-                                           GError     **error)
+      i = 0;
+
+      /* if we're adding a last-used hint, just put the application in front of the list */
+      if (flags & UPDATE_MIME_SET_LAST_USED)
+        {
+          /* avoid adding this again as non-default later */
+          if (flags & UPDATE_MIME_SET_NON_DEFAULT)
+            flags ^= UPDATE_MIME_SET_NON_DEFAULT;
+
+          list[i++] = g_strdup (desktop_id);
+        }
+
+      if (old_list)
+        {
+          for (j = 0; old_list[j] != NULL; j++)
+           {
+             if (g_strcmp0 (old_list[j], desktop_id) != 0)
+                {
+                  /* rewrite other entries if they're different from the new one */
+                  list[i++] = g_strdup (old_list[j]);
+                }
+             else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
+               {
+                  /* we encountered an old entry which is equal to the one we're adding as non-default,
+                   * don't change its position in the list.
+                   */
+                 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
+                 list[i++] = g_strdup (old_list[j]);
+               }
+           }
+        }
+
+      /* add it at the end of the list */
+      if (flags & UPDATE_MIME_SET_NON_DEFAULT)
+        list[i++] = g_strdup (desktop_id);
+
+      list[i] = NULL;
+  
+      g_strfreev (old_list);
+
+      if (list[0] == NULL || desktop_id == NULL)
+        g_key_file_remove_key (key_file,
+                              ADDED_ASSOCIATIONS_GROUP,
+                              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_list_free_full (system_list, g_free);
+            }
+        }
+   
+      g_strfreev (list);
+    }
+  
+  if (content_type)
+    {
+      /* reuse the list from above */
+    }
+  else
+    {
+      g_strfreev (content_types);
+      content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
+    }
+
+  for (k = 0; content_types && content_types[k]; k++) 
+    {
+      /* Remove from removed associations group (unless remove) */
+  
+      length = 0;
+      old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
+                                            content_types[k], &length, NULL);
+
+      list = g_new (char *, 1 + length + 1);
+
+      i = 0;
+      if (flags & UPDATE_MIME_REMOVE)
+        list[i++] = g_strdup (desktop_id);
+      if (old_list)
+        {
+          for (j = 0; old_list[j] != NULL; j++)
+           {
+             if (g_strcmp0 (old_list[j], desktop_id) != 0)
+               list[i++] = g_strdup (old_list[j]);
+           }
+        }
+      list[i] = NULL;
+  
+      g_strfreev (old_list);
+
+      if (list[0] == NULL || desktop_id == NULL)
+        g_key_file_remove_key (key_file,
+                              REMOVED_ASSOCIATIONS_GROUP,
+                              content_types[k],
+                              NULL);
+      else
+        g_key_file_set_string_list (key_file,
+                                   REMOVED_ASSOCIATIONS_GROUP,
+                                   content_types[k],
+                                   (const char * const *)list, i);
+
+      g_strfreev (list);
+    }
+
+  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;
+}
+
+static gboolean
+g_desktop_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
+                                              const char  *content_type,
+                                              GError     **error)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  if (!g_app_info_add_supports_type (appinfo, content_type, error))
+  if (!g_desktop_app_info_ensure_saved (info, error))
     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 |
+                               UPDATE_MIME_SET_LAST_USED,
+                               error);
+}
+
+static gboolean
+g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
+                                           const char  *content_type,
+                                           GError     **error)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+  if (!g_desktop_app_info_ensure_saved (info, error))
+    return FALSE;  
   
-  return update_default_list (info->desktop_id, content_type, TRUE, error);
+  return update_mimeapps_list (info->desktop_id, content_type,
+                               UPDATE_MIME_SET_DEFAULT,
+                               error);
 }
 
 static void
@@ -1234,7 +1488,7 @@ run_update_command (char *command,
                           G_SPAWN_DO_NOT_REAP_CHILD,
                           NULL, NULL, /* No setup function */
                           &pid,
-                          NULL)) 
+                          &error)) 
          g_child_watch_add (pid, update_program_done, NULL);
        else
          {
@@ -1259,6 +1513,9 @@ g_desktop_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
   char *dirname;
   gboolean res;
 
+  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
+    return FALSE;  
+  
   dirname = ensure_dir (MIMETYPE_DIR, error);
   if (!dirname)
     return FALSE;
@@ -1305,60 +1562,19 @@ g_desktop_app_info_add_supports_type (GAppInfo    *appinfo,
                                      GError     **error)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-  GKeyFile *keyfile;
-  char *new_mimetypes, *old_mimetypes, *content;
-  char *dirname;
-  char *filename;
-
-  keyfile = g_key_file_new ();
-  if (!g_key_file_load_from_file (keyfile, info->filename,
-                                 G_KEY_FILE_KEEP_COMMENTS |
-                                 G_KEY_FILE_KEEP_TRANSLATIONS, error))
-    {
-      g_key_file_free (keyfile);
-      return FALSE;
-    }
-
-  old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
-  new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
-  g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
-  g_free (old_mimetypes);
-  g_free (new_mimetypes);
-
-  content = g_key_file_to_data (keyfile, NULL, NULL);
-  g_key_file_free (keyfile);
 
-  dirname = ensure_dir (APP_DIR, error);
-  if (!dirname)
-    {
-      g_free (content);
-      return FALSE;
-    }
-  
-  filename = g_build_filename (dirname, info->desktop_id, NULL);
-  g_free (dirname);
+  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
+    return FALSE;  
   
-  if (!g_file_set_contents (filename, content, -1, error))
-    {
-      g_free (filename);
-      g_free (content);
-      return FALSE;
-    }
-  g_free (filename);
-  g_free (content);
-
-  run_update_command ("update-desktop-database", "applications");
-  return TRUE;
+  return update_mimeapps_list (info->desktop_id, content_type,
+                               UPDATE_MIME_SET_NON_DEFAULT,
+                               error);
 }
 
 static gboolean
 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-  char *user_dirname;
-  
-  user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
-  return g_str_has_prefix (info->filename, user_dirname);
+  return TRUE;
 }
 
 static gboolean
@@ -1367,91 +1583,38 @@ g_desktop_app_info_remove_supports_type (GAppInfo    *appinfo,
                                         GError     **error)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-  GKeyFile *keyfile;
-  char *new_mimetypes, *old_mimetypes, *content;
-  char *found;
-  char *filename;
-  char *dirname;
 
-  keyfile = g_key_file_new ();
-  if (!g_key_file_load_from_file (keyfile, info->filename,
-                                 G_KEY_FILE_KEEP_COMMENTS |
-                                 G_KEY_FILE_KEEP_TRANSLATIONS, error))
-    {
-      g_key_file_free (keyfile);
-      return FALSE;
-    }
-
-  old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
-  new_mimetypes = g_strdup (old_mimetypes);
-  found = NULL;
-  if (new_mimetypes)
-    found = strstr (new_mimetypes, content_type);
-  if (found && *(found + strlen (content_type)) == ';')
-    {
-      char *rest = found + strlen (content_type) + 1;
-      memmove (found, rest, strlen (rest) + 1);
-    }
-  g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
-  g_free (old_mimetypes);
-  g_free (new_mimetypes);
-
-  content = g_key_file_to_data (keyfile, NULL, NULL);
-  g_key_file_free (keyfile);
-
-  dirname = ensure_dir (APP_DIR, error);
-  if (!dirname)
-    {
-      g_free (content);
-      return FALSE;
-    }
+  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
+    return FALSE;
   
-  filename = g_build_filename (dirname, info->desktop_id, NULL);
-  g_free (dirname);
-  if (!g_file_set_contents (filename, content, -1, error))
-    {
-      g_free (filename);
-      g_free (content);
-      return FALSE;
-    }
-  g_free (filename);
-  g_free (content);
-
-  run_update_command ("update-desktop-database", "applications");
-
-  return update_default_list (info->desktop_id, content_type, FALSE, error);
+  return update_mimeapps_list (info->desktop_id, content_type,
+                               UPDATE_MIME_REMOVE,
+                               error);
 }
 
-/**
- * g_app_info_create_from_commandline:
- * @commandline: the commandline to use
- * @application_name: 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.
- *
- * Creates a new #GAppInfo from the given information.
- *
- * Returns: new #GAppInfo for given command.
- **/
-GAppInfo *
-g_app_info_create_from_commandline (const char           *commandline,
-                                   const char           *application_name,
-                                   GAppInfoCreateFlags   flags,
-                                   GError              **error)
+static gboolean
+g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
+                                GError          **error)
 {
   GKeyFile *key_file;
   char *dirname;
-  char **split;
-  char *basename, *exec, *filename, *comment;
+  char *filename;
   char *data, *desktop_id;
   gsize data_size;
   int fd;
-  GDesktopAppInfo *info;
   gboolean res;
+  
+  if (info->filename != NULL)
+    return TRUE;
 
+  /* This is only used for object created with
+   * g_app_info_create_from_commandline. All other
+   * object should have a filename
+   */
+  
   dirname = ensure_dir (APP_DIR, error);
   if (!dirname)
-    return NULL;
+    return FALSE;
   
   key_file = g_key_file_new ();
 
@@ -1462,26 +1625,22 @@ g_app_info_create_from_commandline (const char           *commandline,
   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
                         G_KEY_FILE_DESKTOP_KEY_TYPE,
                          G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
-  if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL
+  if (info->terminal
     g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
                            G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
 
-  exec = g_strconcat (commandline, " %f", NULL);
   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                        G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
-  g_free (exec);
+                        G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
 
-  /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
-  split = g_strsplit (commandline, " ", 2);
-  basename = g_path_get_basename (split[0]);
-  g_strfreev (split);
   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                        G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
+                        G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
+
+  if (info->fullname != NULL)
+    g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                          FULL_NAME_KEY, info->fullname);
 
-  comment = g_strdup_printf (_("Custom definition for %s"), basename);
   g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                        G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
-  g_free (comment);
+                        G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
   
   g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
                          G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
@@ -1489,8 +1648,7 @@ g_app_info_create_from_commandline (const char           *commandline,
   data = g_key_file_to_data (key_file, &data_size, NULL);
   g_key_file_free (key_file);
 
-  desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
-  g_free (basename);
+  desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
   filename = g_build_filename (dirname, desktop_id, NULL);
   g_free (desktop_id);
   g_free (dirname);
@@ -1506,7 +1664,7 @@ g_app_info_create_from_commandline (const char           *commandline,
       g_free (display_name);
       g_free (filename);
       g_free (data);
-      return NULL;
+      return FALSE;
     }
 
   desktop_id = g_path_get_basename (filename);
@@ -1518,25 +1676,111 @@ g_app_info_create_from_commandline (const char           *commandline,
     {
       g_free (desktop_id);
       g_free (filename);
-      return NULL;
+      return FALSE;
     }
 
+  info->filename = filename;
+  info->desktop_id = desktop_id;
+  
   run_update_command ("update-desktop-database", "applications");
   
-  info = g_desktop_app_info_new_from_filename (filename);
-  g_free (filename);
-  if (info == NULL) 
-    g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                _("Can't load just created desktop file"));
+  return TRUE;
+}
+
+static gboolean
+g_desktop_app_info_can_delete (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+  if (info->filename)
+    {
+      if (strstr (info->filename, "/userapp-"))
+        return g_access (info->filename, W_OK) == 0;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+g_desktop_app_info_delete (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  
+  if (info->filename)
+    { 
+      if (g_remove (info->filename) == 0)
+        {
+          update_mimeapps_list (info->desktop_id, NULL,
+                                UPDATE_MIME_NONE,
+                                NULL);
+
+          g_free (info->filename);
+          info->filename = NULL;
+          g_free (info->desktop_id);
+          info->desktop_id = NULL;
+
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
+/**
+ * g_app_info_create_from_commandline:
+ * @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.
+ *
+ * Creates a new #GAppInfo from the given information.
+ *
+ * Returns: (transfer full): new #GAppInfo for given command.
+ **/
+GAppInfo *
+g_app_info_create_from_commandline (const char           *commandline,
+                                   const char           *application_name,
+                                   GAppInfoCreateFlags   flags,
+                                   GError              **error)
+{
+  char **split;
+  char *basename;
+  GDesktopAppInfo *info;
+
+  g_return_val_if_fail (commandline, NULL);
+
+  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
+
+  info->filename = NULL;
+  info->desktop_id = NULL;
+  
+  info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
+  info->startup_notify = flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
+  info->hidden = FALSE;
+  if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
+    info->exec = g_strconcat (commandline, " %u", NULL);
   else
-    info->desktop_id = g_strdup (desktop_id);
-    
-  g_free (desktop_id);
+    info->exec = g_strconcat (commandline, " %f", NULL);
+  info->nodisplay = TRUE;
+  info->binary = binary_from_exec (info->exec);
+  
+  if (application_name)
+    info->name = g_strdup (application_name);
+  else
+    {
+      /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
+      split = g_strsplit (commandline, " ", 2);
+      basename = split[0] ? g_path_get_basename (split[0]) : NULL;
+      g_strfreev (split);
+      info->name = basename;
+      if (info->name == NULL)
+       info->name = g_strdup ("custom");
+    }
+  info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
   
   return G_APP_INFO (info);
 }
 
-
 static void
 g_desktop_app_info_iface_init (GAppInfoIface *iface)
 {
@@ -1557,6 +1801,11 @@ g_desktop_app_info_iface_init (GAppInfoIface *iface)
   iface->add_supports_type = g_desktop_app_info_add_supports_type;
   iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
   iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
+  iface->can_delete = g_desktop_app_info_can_delete;
+  iface->do_delete = g_desktop_app_info_delete;
+  iface->get_commandline = g_desktop_app_info_get_commandline;
+  iface->get_display_name = g_desktop_app_info_get_display_name;
+  iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
 }
 
 static gboolean
@@ -1572,28 +1821,135 @@ app_info_in_list (GAppInfo *info,
   return FALSE;
 }
 
+/**
+ * g_app_info_get_recommended_for_type:
+ * @content_type: the content type to find a #GAppInfo for
+ * 
+ * Gets a list of recommended #GAppInfos for a given content type, i.e.
+ * 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
+ * called.
+ *
+ * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
+ *     for given @content_type or %NULL on error.
+ *
+ * Since: 2.28
+ **/
+GList *
+g_app_info_get_recommended_for_type (const gchar *content_type)
+{
+  GList *desktop_entries, *l;
+  GList *infos;
+  GDesktopAppInfo *info;
+
+  g_return_val_if_fail (content_type != NULL, NULL);
+
+  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
+
+  infos = NULL;
+  for (l = desktop_entries; l != NULL; l = l->next)
+    {
+      char *desktop_entry = l->data;
+
+      info = g_desktop_app_info_new (desktop_entry);
+      if (info)
+       {
+         if (app_info_in_list (G_APP_INFO (info), infos))
+           g_object_unref (info);
+         else
+           infos = g_list_prepend (infos, info);
+       }
+      g_free (desktop_entry);
+    }
+
+  g_list_free (desktop_entries);
+
+  return g_list_reverse (infos);
+}
+
+/**
+ * g_app_info_get_fallback_for_type:
+ * @content_type: the content type to find a #GAppInfo for
+ * 
+ * Gets a list of fallback #GAppInfos for a given content type, i.e.
+ * those applications which claim to support the given content type
+ * by MIME type subclassing and not directly.
+ *
+ * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
+ *     for given @content_type or %NULL on error.
+ *
+ * Since: 2.28
+ **/
+GList *
+g_app_info_get_fallback_for_type (const gchar *content_type)
+{
+  GList *desktop_entries, *l;
+  GList *infos, *recommended_infos;
+  GDesktopAppInfo *info;
+
+  g_return_val_if_fail (content_type != NULL, NULL);
+
+  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
+  recommended_infos = g_app_info_get_recommended_for_type (content_type);
+
+  infos = NULL;
+  for (l = desktop_entries; l != NULL; l = l->next)
+    {
+      char *desktop_entry = l->data;
+
+      info = g_desktop_app_info_new (desktop_entry);
+      if (info)
+       {
+         if (app_info_in_list (G_APP_INFO (info), infos) ||
+             app_info_in_list (G_APP_INFO (info), recommended_infos))
+           g_object_unref (info);
+         else
+           infos = g_list_prepend (infos, info);
+       }
+      g_free (desktop_entry);
+    }
+
+  g_list_free (desktop_entries);
+  g_list_free_full (recommended_infos, g_object_unref);
+
+  return g_list_reverse (infos);
+}
 
 /**
  * g_app_info_get_all_for_type:
  * @content_type: the content type to find a #GAppInfo for
  * 
- * Gets a list of all #GAppInfo s for a given content type.
+ * Gets a list of all #GAppInfos for a given content type.
  *
- * Returns: #GList of #GAppInfo s for given @content_type
- *    or %NULL on error.
+ * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
+ *     for given @content_type or %NULL on error.
  **/
 GList *
 g_app_info_get_all_for_type (const char *content_type)
 {
   GList *desktop_entries, *l;
   GList *infos;
+  char *user_default = NULL;
   GDesktopAppInfo *info;
 
   g_return_val_if_fail (content_type != NULL, NULL);
   
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
-
+  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
   infos = NULL;
+
+  /* put the user default in front of the list, for compatibility */
+  if (user_default != NULL)
+    {
+      info = g_desktop_app_info_new (user_default);
+
+      if (info != NULL)
+        infos = g_list_prepend (infos, info);
+    }
+
+  g_free (user_default);
+
   for (l = desktop_entries; l != NULL; l = l->next)
     {
       char *desktop_entry = l->data;
@@ -1614,6 +1970,24 @@ g_app_info_get_all_for_type (const char *content_type)
   return g_list_reverse (infos);
 }
 
+/**
+ * g_app_info_reset_type_associations:
+ * @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().
+ *
+ * Since: 2.20
+ */
+void
+g_app_info_reset_type_associations (const char *content_type)
+{
+  update_mimeapps_list (NULL, content_type,
+                        UPDATE_MIME_NONE,
+                        NULL);
+}
 
 /**
  * g_app_info_get_default_for_type:
@@ -1621,22 +1995,50 @@ g_app_info_get_all_for_type (const char *content_type)
  * @must_support_uris: if %TRUE, the #GAppInfo is expected to
  *     support URIs
  * 
- * Gets the #GAppInfo that correspond to a given content type.
+ * Gets the #GAppInfo that corresponds to a given content type.
  *
- * Returns: #GAppInfo for given @content_type or %NULL on error.
+ * 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)
 {
   GList *desktop_entries, *l;
+  char *user_default = NULL;
   GAppInfo *info;
 
   g_return_val_if_fail (content_type != NULL, NULL);
   
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
+  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
 
   info = NULL;
+
+  if (user_default != NULL)
+    {
+      info = (GAppInfo *) g_desktop_app_info_new (user_default);
+
+      if (info)
+        {
+         if (must_support_uris && !g_app_info_supports_uris (info))
+           {
+             g_object_unref (info);
+             info = NULL;
+           }
+        }
+    }
+
+  g_free (user_default);
+
+  if (info != NULL)
+    {
+      g_list_free_full (desktop_entries, g_free);
+      return info;
+    }
+
+  /* pick the first from the other list that matches our URI
+   * requirements.
+   */
   for (l = desktop_entries; l != NULL; l = l->next)
     {
       char *desktop_entry = l->data;
@@ -1654,33 +2056,36 @@ g_app_info_get_default_for_type (const char *content_type,
        }
     }
   
-  g_list_foreach  (desktop_entries, (GFunc)g_free, NULL);
-  g_list_free (desktop_entries);
-  
+  g_list_free_full (desktop_entries, g_free);
+
   return info;
 }
 
-
 /**
  * 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.
- *
- * TODO: This is currently unimplemented.
+ * using this 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: %NULL.
+ * 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)
 {
-  /* TODO: Implement this using giomodules, reading the gconf settings
-   * in /desktop/gnome/url-handlers
-   */
-  return NULL;
-}
+  GAppInfo *app_info;
+  char *content_type, *scheme_down;
+
+  scheme_down = g_ascii_strdown (uri_scheme, -1);
+  content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
+  g_free (scheme_down);
+  app_info = g_app_info_get_default_for_type (content_type, FALSE);
+  g_free (content_type);
 
+  return app_info;
+}
 
 static void
 get_apps_from_dir (GHashTable *apps, 
@@ -1707,6 +2112,7 @@ get_apps_from_dir (GHashTable *apps,
              if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
                {
                  appinfo = g_desktop_app_info_new_from_filename (filename);
+                  hidden = FALSE;
 
                  if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
                    {
@@ -1758,7 +2164,7 @@ get_apps_from_dir (GHashTable *apps,
  * The returned list does not include applications which have
  * the <literal>Hidden</literal> key set. 
  * 
- * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
+ * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
  **/
 GList *
 g_app_info_get_all (void)
@@ -1799,8 +2205,12 @@ typedef struct {
   char *path;
   GHashTable *mime_info_cache_map;
   GHashTable *defaults_list_map;
+  GHashTable *mimeapps_list_added_map;
+  GHashTable *mimeapps_list_removed_map;
+  GHashTable *mimeapps_list_defaults_map;
   time_t mime_info_cache_timestamp;
   time_t defaults_list_timestamp;
+  time_t mimeapps_list_timestamp;
 } MimeInfoCacheDir;
 
 typedef struct {
@@ -1983,9 +2393,9 @@ mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
   
   if (dir->defaults_list_map != NULL)
     g_hash_table_destroy (dir->defaults_list_map);
-
   dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  g_free, (GDestroyNotify)g_strfreev);
+  
 
   key_file = g_key_file_new ();
   
@@ -2006,31 +2416,166 @@ mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
     goto error;
 
   mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
-                                   NULL, &load_error);
+                                   NULL, NULL);
+  if (mime_types != NULL)
+    {
+      for (i = 0; mime_types[i] != NULL; i++)
+       {
+         desktop_file_ids = g_key_file_get_string_list (key_file,
+                                                        DEFAULT_APPLICATIONS_GROUP,
+                                                        mime_types[i],
+                                                        NULL,
+                                                        NULL);
+         if (desktop_file_ids == NULL)
+           continue;
+         
+         unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
+         g_hash_table_replace (dir->defaults_list_map,
+                               unaliased_type,
+                               desktop_file_ids);
+       }
+      
+      g_strfreev (mime_types);
+    }
+
+  g_key_file_free (key_file);
+  return;
+  
+ error:
+  g_free (filename);
+  g_key_file_free (key_file);
+  
+  if (mime_types != NULL)
+    g_strfreev (mime_types);
+  
+  if (load_error)
+    g_error_free (load_error);
+}
+
+static void
+mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
+{
+  GKeyFile *key_file;
+  GError *load_error;
+  gchar *filename, **mime_types;
+  char *unaliased_type;
+  char **desktop_file_ids;
+  char *desktop_id;
+  int i;
+  struct stat buf;
+
+  load_error = NULL;
+  mime_types = NULL;
+
+  if (dir->mimeapps_list_added_map != NULL &&
+      !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
+                                       &dir->mimeapps_list_timestamp))
+    return;
+  
+  if (dir->mimeapps_list_added_map != NULL)
+    g_hash_table_destroy (dir->mimeapps_list_added_map);
+  dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                       g_free, (GDestroyNotify)g_strfreev);
+  
+  if (dir->mimeapps_list_removed_map != NULL)
+    g_hash_table_destroy (dir->mimeapps_list_removed_map);
+  dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                         g_free, (GDestroyNotify)g_strfreev);
+
+  if (dir->mimeapps_list_defaults_map != NULL)
+    g_hash_table_destroy (dir->mimeapps_list_defaults_map);
+  dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                           g_free, g_free);
+
+  key_file = g_key_file_new ();
+  
+  filename = g_build_filename (dir->path, "mimeapps.list", NULL);
+  if (g_stat (filename, &buf) < 0)
+    goto error;
+
+  if (dir->mimeapps_list_timestamp > 0) 
+    mime_info_cache->should_ping_mime_monitor = TRUE;
+
+  dir->mimeapps_list_timestamp = buf.st_mtime;
+
+  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
+  g_free (filename);
+  filename = NULL;
 
   if (load_error != NULL)
     goto error;
 
-  for (i = 0; mime_types[i] != NULL; i++)
+  mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
+                                   NULL, NULL);
+  if (mime_types != NULL)
     {
-      desktop_file_ids = g_key_file_get_string_list (key_file,
-                                                    DEFAULT_APPLICATIONS_GROUP,
-                                                    mime_types[i],
-                                                    NULL,
-                                                    NULL);
-      if (desktop_file_ids == NULL)
-       continue;
+      for (i = 0; mime_types[i] != NULL; i++)
+       {
+         desktop_file_ids = g_key_file_get_string_list (key_file,
+                                                        ADDED_ASSOCIATIONS_GROUP,
+                                                        mime_types[i],
+                                                        NULL,
+                                                        NULL);
+         if (desktop_file_ids == NULL)
+           continue;
+         
+         unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
+         g_hash_table_replace (dir->mimeapps_list_added_map,
+                               unaliased_type,
+                               desktop_file_ids);
+       }
+      
+      g_strfreev (mime_types);
+    }
 
-      unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-      g_hash_table_replace (dir->defaults_list_map,
-                           unaliased_type,
-                           desktop_file_ids);
+  mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
+                                   NULL, NULL);
+  if (mime_types != NULL)
+    {
+      for (i = 0; mime_types[i] != NULL; i++)
+       {
+         desktop_file_ids = g_key_file_get_string_list (key_file,
+                                                        REMOVED_ASSOCIATIONS_GROUP,
+                                                        mime_types[i],
+                                                        NULL,
+                                                        NULL);
+         if (desktop_file_ids == NULL)
+           continue;
+         
+         unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
+         g_hash_table_replace (dir->mimeapps_list_removed_map,
+                               unaliased_type,
+                               desktop_file_ids);
+       }
+      
+      g_strfreev (mime_types);
+    }
+
+  mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
+                                    NULL, NULL);
+  if (mime_types != NULL)
+    {
+      for (i = 0; mime_types[i] != NULL; i++)
+        {
+          desktop_id = g_key_file_get_string (key_file,
+                                              DEFAULT_APPLICATIONS_GROUP,
+                                              mime_types[i],
+                                              NULL);
+          if (desktop_id == NULL)
+            continue;
+
+          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
+          g_hash_table_replace (dir->mimeapps_list_defaults_map,
+                                unaliased_type,
+                                desktop_id);
+        }
+
+      g_strfreev (mime_types);
     }
 
-  g_strfreev (mime_types);
   g_key_file_free (key_file);
-  
   return;
+  
  error:
   g_free (filename);
   g_key_file_free (key_file);
@@ -2072,6 +2617,24 @@ mime_info_cache_dir_free (MimeInfoCacheDir *dir)
       dir->defaults_list_map = NULL;
     }
   
+  if (dir->mimeapps_list_added_map != NULL)
+    {
+      g_hash_table_destroy (dir->mimeapps_list_added_map);
+      dir->mimeapps_list_added_map = NULL;
+    }
+  
+  if (dir->mimeapps_list_removed_map != NULL)
+    {
+      g_hash_table_destroy (dir->mimeapps_list_removed_map);
+      dir->mimeapps_list_removed_map = NULL;
+    }
+
+  if (dir->mimeapps_list_defaults_map != NULL)
+    {
+      g_hash_table_destroy (dir->mimeapps_list_defaults_map);
+      dir->mimeapps_list_defaults_map = NULL;
+    }
+
   g_free (dir);
 }
 
@@ -2088,7 +2651,7 @@ mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
   
   for (i = 0; new_desktop_file_ids[i] != NULL; i++)
     {
-      if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
+      if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
        desktop_file_ids = g_list_append (desktop_file_ids,
                                          g_strdup (new_desktop_file_ids[i]));
     }
@@ -2116,6 +2679,7 @@ mime_info_cache_init_dir_lists (void)
        {
          mime_info_cache_dir_init (dir);
          mime_info_cache_dir_init_defaults_list (dir);
+         mime_info_cache_dir_init_mimeapps_list (dir);
          
          mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
        }
@@ -2137,6 +2701,7 @@ mime_info_cache_update_dir_lists (void)
       mime_info_cache_blow_global_cache ();
       mime_info_cache_dir_init (dir);
       mime_info_cache_dir_init_defaults_list (dir);
+      mime_info_cache_dir_init_mimeapps_list (dir);
       
       tmp = tmp->next;
     }
@@ -2145,28 +2710,28 @@ mime_info_cache_update_dir_lists (void)
 static void
 mime_info_cache_init (void)
 {
-       G_LOCK (mime_info_cache);
-       if (mime_info_cache == NULL)
-         mime_info_cache_init_dir_lists ();
-       else
-         {
-           time_t now;
-           
-           time (&now);
-           if (now >= mime_info_cache->last_stat_time + 10)
-             {
-               mime_info_cache_update_dir_lists ();
-               mime_info_cache->last_stat_time = now;
-             }
-         }
-
-       if (mime_info_cache->should_ping_mime_monitor)
-         {
-           /* g_idle_add (emit_mime_changed, NULL); */
-           mime_info_cache->should_ping_mime_monitor = FALSE;
-         }
-       
-       G_UNLOCK (mime_info_cache);
+  G_LOCK (mime_info_cache);
+  if (mime_info_cache == NULL)
+    mime_info_cache_init_dir_lists ();
+  else
+    {
+      time_t now;
+      
+      time (&now);
+      if (now >= mime_info_cache->last_stat_time + 10)
+       {
+         mime_info_cache_update_dir_lists ();
+         mime_info_cache->last_stat_time = now;
+       }
+    }
+  
+  if (mime_info_cache->should_ping_mime_monitor)
+    {
+      /* g_idle_add (emit_mime_changed, NULL); */
+      mime_info_cache->should_ping_mime_monitor = FALSE;
+    }
+  
+  G_UNLOCK (mime_info_cache);
 }
 
 static MimeInfoCache *
@@ -2219,10 +2784,12 @@ mime_info_cache_reload (const char *dir)
 
 static GList *
 append_desktop_entry (GList      *list, 
-                      const char *desktop_entry)
+                      const char *desktop_entry,
+                     GList      *removed_entries)
 {
   /* Add if not already in list, and valid */
-  if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
+  if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
+      !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
     list = g_list_prepend (list, g_strdup (desktop_entry));
   
   return list;
@@ -2231,44 +2798,113 @@ append_desktop_entry (GList      *list,
 /**
  * get_all_desktop_entries_for_mime_type:
  * @mime_type: a mime type.
+ * @except: NULL or a strv list
  *
  * Returns all the desktop ids for @mime_type. The desktop files
  * are listed in an order so that default applications are listed before
  * non-default ones, and handlers for inherited mimetypes are listed
  * after the base ones.
  *
+ * Optionally doesn't list the desktop ids given in the @except 
+ *
  * Return value: a #GList containing the desktop ids which claim
  *    to handle @mime_type.
  */
 static GList *
-get_all_desktop_entries_for_mime_type (const char *base_mime_type)
+get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
+                                      const char **except,
+                                      gboolean     include_fallback,
+                                       char       **explicit_default)
 {
-  GList *desktop_entries, *list, *dir_list, *tmp;
+  GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
   MimeInfoCacheDir *dir;
-  char *mime_type;
+  char *mime_type, *default_entry = NULL;
+  const char *entry;
   char **mime_types;
   char **default_entries;
-  int i,j;
+  char **removed_associations;
+  int i, j, k;
+  GPtrArray *array;
+  char **anc;
   
   mime_info_cache_init ();
 
-  mime_types = _g_unix_content_type_get_parents (base_mime_type);
+  if (include_fallback)
+    {
+      /* collect all ancestors */
+      mime_types = _g_unix_content_type_get_parents (base_mime_type);
+      array = g_ptr_array_new ();
+      for (i = 0; mime_types[i]; i++)
+       g_ptr_array_add (array, mime_types[i]);
+      g_free (mime_types);
+      for (i = 0; i < array->len; i++)
+       {
+         anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
+         for (j = 0; anc[j]; j++)
+           {
+             for (k = 0; k < array->len; k++)
+               {
+                 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
+                   break;
+               }
+             if (k == array->len) /* not found */
+               g_ptr_array_add (array, g_strdup (anc[j]));
+           }
+         g_strfreev (anc);
+       }
+      g_ptr_array_add (array, NULL);
+      mime_types = (char **)g_ptr_array_free (array, FALSE);
+    }
+  else
+    {
+      mime_types = g_malloc0 (2 * sizeof (gchar *));
+      mime_types[0] = g_strdup (base_mime_type);
+      mime_types[1] = NULL;
+    }
+
   G_LOCK (mime_info_cache);
   
+  removed_entries = NULL;
   desktop_entries = NULL;
+
+  for (i = 0; except != NULL && except[i] != NULL; i++)
+    removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
+  
   for (i = 0; mime_types[i] != NULL; i++)
     {
       mime_type = mime_types[i];
 
-      /* Go through all apps listed as defaults */
+      /* Go through all apps listed in user and system dirs */
       for (dir_list = mime_info_cache->dirs;
           dir_list != NULL;
           dir_list = dir_list->next)
        {
          dir = dir_list->data;
+
+          /* Pick the explicit default application */
+          entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
+
+          if (entry != NULL)
+            {
+              /* Save the default entry if it's the first one we encounter */
+              if (default_entry == NULL)
+                default_entry = g_strdup (entry);
+            }
+
+         /* Then added associations from mimeapps.list */
+         default_entries = g_hash_table_lookup (dir->mimeapps_list_added_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);
+
+         /* Then removed associations from mimeapps.list */
+         removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
+         for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
+           removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
+
+         /* 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]);
+           desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
        }
 
       /* Go through all entries that support the mimetype */
@@ -2280,18 +2916,64 @@ get_all_desktop_entries_for_mime_type (const char *base_mime_type)
        
          list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
          for (tmp = list; tmp != NULL; tmp = tmp->next)
-           desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
+           desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
         }
     }
   
   G_UNLOCK (mime_info_cache);
 
   g_strfreev (mime_types);
-  
+
+  if (explicit_default != NULL)
+    *explicit_default = default_entry;
+  else
+    g_free (default_entry);
+
+  g_list_foreach (removed_entries, (GFunc)g_free, NULL);
+  g_list_free (removed_entries);
+
   desktop_entries = g_list_reverse (desktop_entries);
   
   return desktop_entries;
 }
 
-#define __G_DESKTOP_APP_INFO_C__
-#include "gioaliasdef.c"
+/* GDesktopAppInfoLookup interface: */
+
+typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
+G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
+
+static void
+g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
+{
+}
+
+/**
+ * g_desktop_app_info_lookup_get_default_for_uri_scheme:
+ * @lookup: a #GDesktopAppInfoLookup
+ * @uri_scheme: a string containing a URI scheme.
+ *
+ * Gets the default application for launching applications 
+ * using this URI scheme for a particular GDesktopAppInfoLookup
+ * implementation.
+ *
+ * The GDesktopAppInfoLookup interface and this function is used
+ * to implement g_app_info_get_default_for_uri_scheme() backends
+ * in a GIO module. There is no reason for applications to use it
+ * directly. Applications should use g_app_info_get_default_for_uri_scheme().
+ * 
+ * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
+ *
+ * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
+ */
+GAppInfo *
+g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
+                                                     const char            *uri_scheme)
+{
+  GDesktopAppInfoLookupIface *iface;
+  
+  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
+
+  iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
+
+  return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
+}