cleanup
[platform/upstream/glib.git] / gio / gdesktopappinfo.c
index 141a89a..7bfc904 100644 (file)
@@ -14,9 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
  *         Ryan Lortie <desrt@desrt.ca>
@@ -47,6 +45,8 @@
 #include "glibintl.h"
 #include "giomodule-priv.h"
 #include "gappinfo.h"
+#include "gappinfoprivate.h"
+#include "glocaldirectorymonitor.h"
 
 
 /**
@@ -58,9 +58,9 @@
  * #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.
+ * Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific
+ * GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
+ * file when using it.
  */
 
 #define DEFAULT_APPLICATIONS_GROUP  "Default Applications"
@@ -78,11 +78,6 @@ enum {
 };
 
 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);
 
@@ -137,3463 +132,4178 @@ typedef enum {
 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;
-
 /* DesktopFileDir implementation {{{1 */
 
 typedef struct
 {
   gchar                      *path;
+  gchar                      *alternatively_watching;
+  gboolean                    is_config;
+  gboolean                    is_setup;
+  GLocalDirectoryMonitor     *monitor;
+  GHashTable                 *app_names;
+  GHashTable                 *mime_tweaks;
+  GHashTable                 *memory_index;
+  GHashTable                 *memory_implementations;
 } DesktopFileDir;
 
 static DesktopFileDir *desktop_file_dirs;
 static guint           n_desktop_file_dirs;
+static const guint     desktop_file_dir_user_config_index = 0;
+static guint           desktop_file_dir_user_data_index;
+static GMutex          desktop_file_dir_lock;
 
-/* DesktopFileDir "API" {{{2 */
+/* Monitor 'changed' signal handler {{{2 */
+static void desktop_file_dir_reset (DesktopFileDir *dir);
 
 /*< internal >
- * desktop_file_dir_create:
- * @array: the #GArray to add a new item to
- * @data_dir: an XDG_DATA_DIR
+ * desktop_file_dir_get_alternative_dir:
+ * @dir: a #DesktopFileDir
  *
- * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
- * to @array.
+ * Gets the "alternative" directory to monitor in case the path
+ * doesn't exist.
+ *
+ * If the path exists this will return NULL, otherwise it will return a
+ * parent directory of the path.
+ *
+ * This is used to avoid inotify on a non-existent directory (which
+ * results in polling).
+ *
+ * See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info.
  */
-static void
-desktop_file_dir_create (GArray      *array,
-                         const gchar *data_dir)
+static gchar *
+desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
 {
-  DesktopFileDir dir = { 0, };
+  gchar *parent;
 
-  dir.path = g_build_filename (data_dir, "applications", NULL);
+  /* If the directory itself exists then we need no alternative. */
+  if (g_access (dir->path, R_OK | X_OK) == 0)
+    return NULL;
 
-  g_array_append_val (array, dir);
-}
+  /* Otherwise, try the parent directories until we find one. */
+  parent = g_path_get_dirname (dir->path);
+
+  while (g_access (parent, R_OK | X_OK) != 0)
+    {
+      gchar *tmp = parent;
+
+      parent = g_path_get_dirname (tmp);
+
+      /* If somehow we get to '/' or '.' then just stop... */
+      if (g_str_equal (parent, tmp))
+        {
+          g_free (tmp);
+          break;
+        }
+
+      g_free (tmp);
+    }
 
-/* Global setup API {{{2 */
+  return parent;
+}
 
 static void
-desktop_file_dirs_refresh (void)
+desktop_file_dir_changed (GFileMonitor      *monitor,
+                          GFile             *file,
+                          GFile             *other_file,
+                          GFileMonitorEvent  event_type,
+                          gpointer           user_data)
 {
-  if (g_once_init_enter (&desktop_file_dirs))
+  DesktopFileDir *dir = user_data;
+  gboolean do_nothing = FALSE;
+
+  /* We are not interested in receiving notifications forever just
+   * because someone asked about one desktop file once.
+   *
+   * After we receive the first notification, reset the dir, destroying
+   * the monitor.  We will take this as a hint, next time that we are
+   * asked, that we need to check if everything is up to date.
+   *
+   * If this is a notification for a parent directory (because the
+   * desktop directory didn't exist) then we shouldn't fire the signal
+   * unless something actually changed.
+   */
+  g_mutex_lock (&desktop_file_dir_lock);
+
+  if (dir->alternatively_watching)
     {
-      const char * const *data_dirs;
-      GArray *tmp;
-      gint i;
+      gchar *alternative_dir;
 
-      tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
+      alternative_dir = desktop_file_dir_get_alternative_dir (dir);
+      do_nothing = alternative_dir && g_str_equal (dir->alternatively_watching, alternative_dir);
+      g_free (alternative_dir);
+    }
 
-      /* Highest priority: the user's ~/.local/share/applications */
-      desktop_file_dir_create (tmp, g_get_user_data_dir ());
+  if (!do_nothing)
+    desktop_file_dir_reset (dir);
 
-      /* Following that, XDG_DATA_DIRS/applications, in order */
-      data_dirs = g_get_system_data_dirs ();
-      for (i = 0; data_dirs[i]; i++)
-        desktop_file_dir_create (tmp, data_dirs[i]);
+  g_mutex_unlock (&desktop_file_dir_lock);
 
-      n_desktop_file_dirs = tmp->len;
+  /* Notify anyone else who may be interested */
+  if (!do_nothing)
+    g_app_info_monitor_fire ();
+}
+
+/* Internal utility functions {{{2 */
+
+/*< internal >
+ * desktop_file_dir_app_name_is_masked:
+ * @dir: a #DesktopFileDir
+ * @app_name: an application ID
+ *
+ * Checks if @app_name is masked for @dir.
+ *
+ * An application is masked if a similarly-named desktop file exists in
+ * a desktop file directory with higher precedence.  Masked desktop
+ * files should be ignored.
+ */
+static gboolean
+desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
+                                     const gchar    *app_name)
+{
+  while (dir > desktop_file_dirs)
+    {
+      dir--;
 
-      g_once_init_leave (&desktop_file_dirs, (DesktopFileDir *) g_array_free (tmp, FALSE));
+      if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
+        return TRUE;
     }
+
+  return FALSE;
 }
 
-/* GDesktopAppInfo implementation {{{1 */
-/* GObject implementation {{{2 */
-static void
-g_desktop_app_info_finalize (GObject *object)
+static const gchar * const *
+get_lowercase_current_desktops (void)
 {
-  GDesktopAppInfo *info;
+  static gchar **result;
 
-  info = G_DESKTOP_APP_INFO (object);
+  if (g_once_init_enter (&result))
+    {
+      const gchar *envvar;
+      gchar **tmp;
 
-  g_free (info->desktop_id);
-  g_free (info->filename);
+      envvar = g_getenv ("XDG_CURRENT_DESKTOP");
 
-  if (info->keyfile)
-    g_key_file_unref (info->keyfile);
+      if (envvar)
+        {
+          gint i, j;
 
-  g_free (info->name);
-  g_free (info->generic_name);
-  g_free (info->fullname);
-  g_free (info->comment);
-  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);
-  g_free (info->exec);
-  g_free (info->binary);
-  g_free (info->path);
-  g_free (info->categories);
-  g_free (info->startup_wm_class);
-  g_strfreev (info->mime_types);
-  g_free (info->app_id);
-  g_strfreev (info->actions);
+          tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
 
-  G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
+          for (i = 0; tmp[i]; i++)
+            for (j = 0; tmp[i][j]; j++)
+              tmp[i][j] = g_ascii_tolower (tmp[i][j]);
+        }
+      else
+        tmp = g_new0 (gchar *, 0 + 1);
+
+      g_once_init_leave (&result, tmp);
+    }
+
+  return (const gchar **) result;
 }
 
-static void
-g_desktop_app_info_set_property (GObject      *object,
-                                 guint         prop_id,
-                                 const GValue *value,
-                                 GParamSpec   *pspec)
+static const gchar * const *
+get_current_desktops (const gchar *value)
 {
-  GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
+  static gchar **result;
 
-  switch (prop_id)
+  if (g_once_init_enter (&result))
     {
-    case PROP_FILENAME:
-      self->filename = g_value_dup_string (value);
-      break;
+      gchar **tmp;
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
+      if (!value)
+        value = g_getenv ("XDG_CURRENT_DESKTOP");
+
+      if (!value)
+        value = "";
+
+      tmp = g_strsplit (value, ":", 0);
+
+      g_once_init_leave (&result, tmp);
     }
+
+  return (const gchar **) result;
 }
 
+/*< internal >
+ * add_to_table_if_appropriate:
+ * @apps: a string to GDesktopAppInfo hash table
+ * @app_name: the name of the application
+ * @info: a #GDesktopAppInfo, or NULL
+ *
+ * If @info is non-%NULL and non-hidden, then add it to @apps, using
+ * @app_name as a key.
+ *
+ * If @info is non-%NULL then this function will consume the passed-in
+ * reference.
+ */
 static void
-g_desktop_app_info_get_property (GObject    *object,
-                                 guint       prop_id,
-                                 GValue     *value,
-                                 GParamSpec *pspec)
+add_to_table_if_appropriate (GHashTable      *apps,
+                             const gchar     *app_name,
+                             GDesktopAppInfo *info)
 {
-  GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
+  if (!info)
+    return;
 
-  switch (prop_id)
+  if (info->hidden)
     {
-    case PROP_FILENAME:
-      g_value_set_string (value, self->filename);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
+      g_object_unref (info);
+      return;
     }
+
+  g_free (info->desktop_id);
+  info->desktop_id = g_strdup (app_name);
+
+  g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
 }
 
-static void
-g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
+enum
 {
-  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
-
-  gobject_class->get_property = g_desktop_app_info_get_property;
-  gobject_class->set_property = g_desktop_app_info_set_property;
-  gobject_class->finalize = g_desktop_app_info_finalize;
+  DESKTOP_KEY_Comment,
+  DESKTOP_KEY_Exec,
+  DESKTOP_KEY_GenericName,
+  DESKTOP_KEY_Keywords,
+  DESKTOP_KEY_Name,
+  DESKTOP_KEY_X_GNOME_FullName,
+
+  N_DESKTOP_KEYS
+};
 
-  /**
-   * GDesktopAppInfo:filename:
+const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
+  /* Note: lower numbers are a better match.
    *
-   * The origin filename of this #GDesktopAppInfo
+   * In case we want two keys to match at the same level, we can just
+   * use the same number for the two different keys.
    */
-  g_object_class_install_property (gobject_class,
-                                   PROP_FILENAME,
-                                   g_param_spec_string ("filename", "Filename", "", NULL,
-                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-}
+  [DESKTOP_KEY_Name]             = 1,
+  [DESKTOP_KEY_Exec]             = 2,
+  [DESKTOP_KEY_Keywords]         = 3,
+  [DESKTOP_KEY_GenericName]      = 4,
+  [DESKTOP_KEY_X_GNOME_FullName] = 5,
+  [DESKTOP_KEY_Comment]          = 6
+};
 
-static void
-g_desktop_app_info_init (GDesktopAppInfo *local)
+static gchar *
+desktop_key_get_name (guint key_id)
 {
+  switch (key_id)
+    {
+    case DESKTOP_KEY_Comment:
+      return "Comment";
+    case DESKTOP_KEY_Exec:
+      return "Exec";
+    case DESKTOP_KEY_GenericName:
+      return GENERIC_NAME_KEY;
+    case DESKTOP_KEY_Keywords:
+      return KEYWORDS_KEY;
+    case DESKTOP_KEY_Name:
+      return "Name";
+    case DESKTOP_KEY_X_GNOME_FullName:
+      return FULL_NAME_KEY;
+    default:
+      g_assert_not_reached ();
+    }
 }
 
-/* Construction... {{{2 */
-
-/*< internal >
- * binary_from_exec:
- * @exec: an exec line
+/* Search global state {{{2
  *
- * Returns the first word in an exec line (ie: the binary name).
+ * We only ever search under a global lock, so we can use (and reuse)
+ * some global data to reduce allocations made while searching.
  *
- * If @exec is "  progname --foo %F" then returns "progname".
+ * In short, we keep around arrays of results that we expand as needed
+ * (and never shrink).
+ *
+ * static_token_results: this is where we append the results for each
+ *     token within a given desktop directory, as we handle it (which is
+ *     a union of all matches for this term)
+ *
+ * static_search_results: this is where we build the complete results
+ *     for a single directory (which is an intersection of the matches
+ *     found for each term)
+ *
+ * static_total_results: this is where we build the complete results
+ *     across all directories (which is a union of the matches found in
+ *     each directory)
+ *
+ * The app_names that enter these tables are always pointer-unique (in
+ * the sense that string equality is the same as pointer equality).
+ * This can be guaranteed for two reasons:
+ *
+ *   - we mask appids so that a given appid will only ever appear within
+ *     the highest-precedence directory that contains it.  We never
+ *     return search results from a lower-level directory if a desktop
+ *     file exists in a higher-level one.
+ *
+ *   - within a given directory, the string is unique because it's the
+ *     key in the hashtable of all app_ids for that directory.
+ *
+ * We perform a merging of the results in merge_token_results().  This
+ * works by ordering the two lists and moving through each of them (at
+ * the same time) looking for common elements, rejecting uncommon ones.
+ * "Order" here need not mean any particular thing, as long as it is
+ * some order.  Because of the uniqueness of our strings, we can use
+ * pointer order.  That's what's going on in compare_results() below.
  */
-static char *
-binary_from_exec (const char *exec)
+struct search_result
 {
-  const char *p, *start;
+  const gchar *app_name;
+  gint         category;
+};
 
-  p = exec;
-  while (*p == ' ')
-    p++;
-  start = p;
-  while (*p != ' ' && *p != 0)
-    p++;
+static struct search_result *static_token_results;
+static gint                  static_token_results_size;
+static gint                  static_token_results_allocated;
+static struct search_result *static_search_results;
+static gint                  static_search_results_size;
+static gint                  static_search_results_allocated;
+static struct search_result *static_total_results;
+static gint                  static_total_results_size;
+static gint                  static_total_results_allocated;
+
+/* And some functions for performing nice operations against it */
+static gint
+compare_results (gconstpointer a,
+                 gconstpointer b)
+{
+  const struct search_result *ra = a;
+  const struct search_result *rb = b;
 
-  return g_strndup (start, p - start);
+  if (ra->app_name < rb->app_name)
+    return -1;
+
+  else if (ra->app_name > rb->app_name)
+    return 1;
+
+  else
+    return ra->category - rb->category;
 }
 
-static gboolean
-g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
-                                      GKeyFile        *key_file)
+static gint
+compare_categories (gconstpointer a,
+                    gconstpointer b)
 {
-  char *start_group;
-  char *type;
-  char *try_exec;
-  char *exec;
-  gboolean bus_activatable;
+  const struct search_result *ra = a;
+  const struct search_result *rb = b;
 
-  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);
-      return FALSE;
-    }
-  g_free (start_group);
+  return ra->category - rb->category;
+}
 
-  type = g_key_file_get_string (key_file,
-                                G_KEY_FILE_DESKTOP_GROUP,
-                                G_KEY_FILE_DESKTOP_KEY_TYPE,
-                                NULL);
-  if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
+static void
+add_token_result (const gchar *app_name,
+                  guint16      category)
+{
+  if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
     {
-      g_free (type);
-      return FALSE;
+      static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
+      static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
     }
-  g_free (type);
 
-  try_exec = g_key_file_get_string (key_file,
-                                    G_KEY_FILE_DESKTOP_GROUP,
-                                    G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
-                                    NULL);
-  if (try_exec && try_exec[0] != '\0')
+  static_token_results[static_token_results_size].app_name = app_name;
+  static_token_results[static_token_results_size].category = category;
+  static_token_results_size++;
+}
+
+static void
+merge_token_results (gboolean first)
+{
+  qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
+
+  /* If this is the first token then we are basically merging a list with
+   * itself -- we only perform de-duplication.
+   *
+   * If this is not the first token then we are doing a real merge.
+   */
+  if (first)
     {
-      char *t;
-      t = g_find_program_in_path (try_exec);
-      if (t == NULL)
+      const gchar *last_name = NULL;
+      gint i;
+
+      /* We must de-duplicate, but we do so by taking the best category
+       * in each case.
+       *
+       * The final list can be as large as the input here, so make sure
+       * we have enough room (even if it's too much room).
+       */
+
+      if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
         {
-          g_free (try_exec);
-          return FALSE;
+          static_search_results_allocated = static_token_results_allocated;
+          static_search_results = g_renew (struct search_result,
+                                           static_search_results,
+                                           static_search_results_allocated);
         }
-      g_free (t);
-    }
 
-  exec = g_key_file_get_string (key_file,
-                                G_KEY_FILE_DESKTOP_GROUP,
-                                G_KEY_FILE_DESKTOP_KEY_EXEC,
-                                NULL);
-  if (exec && exec[0] != '\0')
-    {
-      gint argc;
-      char **argv;
-      if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
+      for (i = 0; i < static_token_results_size; i++)
         {
-          g_free (exec);
-          g_free (try_exec);
-          return FALSE;
+          /* The list is sorted so that the best match for a given id
+           * will be at the front, so once we have copied an id, skip
+           * the rest of the entries for the same id.
+           */
+          if (static_token_results[i].app_name == last_name)
+            continue;
+
+          last_name = static_token_results[i].app_name;
+
+          static_search_results[static_search_results_size++] = static_token_results[i];
         }
-      else
+    }
+  else
+    {
+      const gchar *last_name = NULL;
+      gint i, j = 0;
+      gint k = 0;
+
+      /* We only ever remove items from the results list, so no need to
+       * resize to ensure that we have enough room.
+       */
+      for (i = 0; i < static_token_results_size; i++)
         {
-          char *t;
-          t = g_find_program_in_path (argv[0]);
-          g_strfreev (argv);
+          if (static_token_results[i].app_name == last_name)
+            continue;
 
-          if (t == NULL)
+          last_name = static_token_results[i].app_name;
+
+          /* Now we only want to have a result in static_search_results
+           * if we already have it there *and* we have it in
+           * static_token_results as well.  The category will be the
+           * lesser of the two.
+           *
+           * Skip past the results in static_search_results that are not
+           * going to be matches.
+           */
+          while (k < static_search_results_size &&
+                 static_search_results[k].app_name < static_token_results[i].app_name)
+            k++;
+
+          if (k < static_search_results_size &&
+              static_search_results[k].app_name == static_token_results[i].app_name)
             {
-              g_free (exec);
-              g_free (try_exec);
-              return FALSE;
+              /* We have a match.
+               *
+               * Category should be the worse of the two (ie:
+               * numerically larger).
+               */
+              static_search_results[j].app_name = static_search_results[k].app_name;
+              static_search_results[j].category = MAX (static_search_results[k].category,
+                                                       static_token_results[i].category);
+              j++;
             }
-          g_free (t);
         }
+
+      static_search_results_size = j;
     }
 
-  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);
-  info->only_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN, NULL, NULL);
-  info->not_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN, NULL, NULL);
-  info->try_exec = try_exec;
-  info->exec = exec;
-  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;
-  info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
-  info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
-  info->mime_types = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL);
-  bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
-  info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
+  /* Clear it out for next time... */
+  static_token_results_size = 0;
+}
 
-  /* Remove the special-case: no Actions= key just means 0 extra actions */
-  if (info->actions == NULL)
-    info->actions = g_new0 (gchar *, 0 + 1);
+static void
+reset_total_search_results (void)
+{
+  static_total_results_size = 0;
+}
 
-  info->icon = NULL;
-  if (info->icon_name)
+static void
+sort_total_search_results (void)
+{
+  qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
+}
+
+static void
+merge_directory_results (void)
+{
+  if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
     {
-      if (g_path_is_absolute (info->icon_name))
-        {
-          GFile *file;
+      static_total_results_allocated = MAX (16, static_total_results_allocated);
+      while (static_total_results_allocated < static_total_results_size + static_search_results_size)
+        static_total_results_allocated *= 2;
+      static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
+    }
 
-          file = g_file_new_for_path (info->icon_name);
-          info->icon = g_file_icon_new (file);
-          g_object_unref (file);
-        }
-      else
-        {
-          char *p;
+  memcpy (static_total_results + static_total_results_size,
+          static_search_results,
+          static_search_results_size * sizeof (struct search_result));
 
-          /* 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;
+  static_total_results_size += static_search_results_size;
 
-          info->icon = g_themed_icon_new (info->icon_name);
-        }
-    }
+  /* Clear it out for next time... */
+  static_search_results_size = 0;
+}
 
-  if (info->exec)
-    info->binary = binary_from_exec (info->exec);
+/* Support for unindexed DesktopFileDirs {{{2 */
+static void
+get_apps_from_dir (GHashTable **apps,
+                   const char  *dirname,
+                   const char  *prefix)
+{
+  const char *basename;
+  GDir *dir;
 
-  if (info->path && info->path[0] == '\0')
-    {
-      g_free (info->path);
-      info->path = NULL;
-    }
+  dir = g_dir_open (dirname, 0, NULL);
 
-  /* Can only be DBusActivatable if we know the filename, which means
-   * that this won't work for the load-from-keyfile case.
-   */
-  if (bus_activatable && info->filename)
+  if (dir == NULL)
+    return;
+
+  while ((basename = g_dir_read_name (dir)) != NULL)
     {
-      gchar *basename;
-      gchar *last_dot;
+      gchar *filename;
 
-      basename = g_path_get_basename (info->filename);
-      last_dot = strrchr (basename, '.');
+      filename = g_build_filename (dirname, basename, NULL);
 
-      if (last_dot && g_str_equal (last_dot, ".desktop"))
+      if (g_str_has_suffix (basename, ".desktop"))
         {
-          *last_dot = '\0';
+          gchar *app_name;
 
-          if (g_dbus_is_interface_name (basename))
-            info->app_id = g_strdup (basename);
+          app_name = g_strconcat (prefix, basename, NULL);
+
+          if (*apps == NULL)
+            *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+          g_hash_table_insert (*apps, app_name, g_strdup (filename));
         }
+      else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
+        {
+          gchar *subprefix;
 
-      g_free (basename);
-    }
+          subprefix = g_strconcat (prefix, basename, "-", NULL);
+          get_apps_from_dir (apps, filename, subprefix);
+          g_free (subprefix);
+        }
 
-  info->keyfile = g_key_file_ref (key_file);
+      g_free (filename);
+    }
 
-  return TRUE;
+  g_dir_close (dir);
 }
 
-static gboolean
-g_desktop_app_info_load_file (GDesktopAppInfo *self)
+typedef struct
 {
-  GKeyFile *key_file;
-  gboolean retval = FALSE;
-
-  g_return_val_if_fail (self->filename != NULL, FALSE);
-
-  self->desktop_id = g_path_get_basename (self->filename);
+  gchar **additions;
+  gchar **removals;
+  gchar **defaults;
+} UnindexedMimeTweaks;
 
-  key_file = g_key_file_new ();
+static void
+free_mime_tweaks (gpointer data)
+{
+  UnindexedMimeTweaks *tweaks = data;
 
-  if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
-    retval = g_desktop_app_info_load_from_keyfile (self, key_file);
+  g_strfreev (tweaks->additions);
+  g_strfreev (tweaks->removals);
+  g_strfreev (tweaks->defaults);
 
-  g_key_file_unref (key_file);
-  return retval;
+  g_slice_free (UnindexedMimeTweaks, tweaks);
 }
 
-/**
- * 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_keyfile (GKeyFile *key_file)
+static UnindexedMimeTweaks *
+desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
+                                       const gchar    *mime_type)
 {
-  GDesktopAppInfo *info;
+  UnindexedMimeTweaks *tweaks;
+  gchar *unaliased_type;
 
-  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
-  info->filename = NULL;
-  if (!g_desktop_app_info_load_from_keyfile (info, key_file))
+  unaliased_type = _g_unix_content_type_unalias (mime_type);
+  tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type);
+
+  if (tweaks == NULL)
     {
-      g_object_unref (info);
-      return NULL;
+      tweaks = g_slice_new0 (UnindexedMimeTweaks);
+      g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
     }
-  return info;
+  else
+    g_free (unaliased_type);
+
+  return tweaks;
 }
 
-/**
- * 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)
+/* consumes 'to_add' */
+static void
+expand_strv (gchar         ***strv_ptr,
+             gchar          **to_add,
+             gchar * const   *blacklist)
 {
-  GDesktopAppInfo *info = NULL;
+  guint strv_len, add_len;
+  gchar **strv;
+  guint i, j;
 
-  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
-  if (!g_desktop_app_info_load_file (info))
+  if (!*strv_ptr)
     {
-      g_object_unref (info);
-      return NULL;
+      *strv_ptr = to_add;
+      return;
     }
-  return info;
+
+  strv = *strv_ptr;
+  strv_len = g_strv_length (strv);
+  add_len = g_strv_length (to_add);
+  strv = g_renew (gchar *, strv, strv_len + add_len + 1);
+
+  for (i = 0; to_add[i]; i++)
+    {
+      /* Don't add blacklisted strings */
+      if (blacklist)
+        for (j = 0; blacklist[j]; j++)
+          if (g_str_equal (to_add[i], blacklist[j]))
+            goto no_add;
+
+      /* Don't add duplicates already in the list */
+      for (j = 0; j < strv_len; j++)
+        if (g_str_equal (to_add[i], strv[j]))
+          goto no_add;
+
+      strv[strv_len++] = to_add[i];
+      continue;
+
+no_add:
+      g_free (to_add[i]);
+    }
+
+  strv[strv_len] = NULL;
+  *strv_ptr = strv;
+
+  g_free (to_add);
 }
 
-/**
- * g_desktop_app_info_new:
- * @desktop_id: the desktop file id
- *
- * 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)
+static void
+desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
+                                               const gchar    *filename,
+                                               const gchar    *added_group,
+                                               gboolean        tweaks_permitted)
 {
-  GDesktopAppInfo *appinfo;
-  char *basename;
+  UnindexedMimeTweaks *tweaks;
+  char **desktop_file_ids;
+  GKeyFile *key_file;
+  gchar **mime_types;
   int i;
 
-  desktop_file_dirs_refresh ();
+  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;
+    }
+
+  mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL);
 
-  basename = g_strdup (desktop_id);
-  
-  for (i = 0; i < n_desktop_file_dirs; i++)
+  if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
     {
-      const gchar *path = desktop_file_dirs[i].path;
-      char *filename;
-      char *p;
+      g_warning ("%s contains a [%s] group, but it is not permitted here.  Only the non-desktop-specific "
+                 "mimeapps.list file may add or remove associations.", filename, added_group);
+      g_strfreev (mime_types);
+      mime_types = NULL;
+    }
 
-      filename = g_build_filename (path, desktop_id, NULL);
-      appinfo = g_desktop_app_info_new_from_filename (filename);
-      g_free (filename);
-      if (appinfo != NULL)
-       goto found;
+  if (mime_types != NULL)
+    {
+      for (i = 0; mime_types[i] != NULL; i++)
+        {
+          desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL);
 
-      p = basename;
-      while ((p = strchr (p, '-')) != NULL)
-       {
-         *p = '/';
+          if (desktop_file_ids)
+            {
+              tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
+              expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
+            }
+        }
 
-         filename = g_build_filename (path, basename, NULL);
-         appinfo = g_desktop_app_info_new_from_filename (filename);
-         g_free (filename);
-         if (appinfo != NULL)
-           goto found;
-         *p = '-';
-         p++;
-       }
+      g_strfreev (mime_types);
     }
 
-  g_free (basename);
-  return NULL;
+  mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
 
- found:
-  g_free (basename);
-  
-  g_free (appinfo->desktop_id);
-  appinfo->desktop_id = g_strdup (desktop_id);
+  if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
+    {
+      g_warning ("%s contains a [%s] group, but it is not permitted here.  Only the non-desktop-specific "
+                 "mimeapps.list file may add or remove associations.", filename, REMOVED_ASSOCIATIONS_GROUP);
+      g_strfreev (mime_types);
+      mime_types = NULL;
+    }
 
-  if (g_desktop_app_info_get_is_hidden (appinfo))
+  if (mime_types != NULL)
     {
-      g_object_unref (appinfo);
-      appinfo = 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)
+            {
+              tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
+              expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
+            }
+        }
+
+      g_strfreev (mime_types);
     }
 
-  return appinfo;
+  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_file_ids = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP, mime_types[i], NULL, NULL);
+
+          if (desktop_file_ids)
+            {
+              tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
+              expand_strv (&tweaks->defaults, desktop_file_ids, NULL);
+            }
+        }
+
+      g_strfreev (mime_types);
+    }
+
+  g_key_file_free (key_file);
 }
 
-static GAppInfo *
-g_desktop_app_info_dup (GAppInfo *appinfo)
+static void
+desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-  GDesktopAppInfo *new_info;
+  const gchar * const *desktops;
+  gchar *filename;
+  gint i;
 
-  new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
+  dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
 
-  new_info->filename = g_strdup (info->filename);
-  new_info->desktop_id = g_strdup (info->desktop_id);
+  /* We process in order of precedence, using a blacklisting approach to
+   * avoid recording later instructions that conflict with ones we found
+   * earlier.
+   *
+   * We first start with the XDG_CURRENT_DESKTOP files, in precedence
+   * order.
+   */
+  desktops = get_lowercase_current_desktops ();
+  for (i = 0; desktops[i]; i++)
+    {
+      filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]);
+      desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
+      g_free (filename);
+    }
 
-  if (info->keyfile)
-    new_info->keyfile = g_key_file_ref (info->keyfile);
+  /* Next, the non-desktop-specific mimeapps.list */
+  filename = g_strdup_printf ("%s/mimeapps.list", dir->path);
+  desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, TRUE);
+  g_free (filename);
 
-  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);
-  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);
-  new_info->exec = g_strdup (info->exec);
-  new_info->binary = g_strdup (info->binary);
-  new_info->path = g_strdup (info->path);
-  new_info->app_id = g_strdup (info->app_id);
-  new_info->hidden = info->hidden;
-  new_info->terminal = info->terminal;
-  new_info->startup_notify = info->startup_notify;
+  /* The remaining files are only checked for in directories that might
+   * contain desktop files (ie: not the config dirs).
+   */
+  if (dir->is_config)
+    return;
 
-  return G_APP_INFO (new_info);
-}
+  /* We have 'defaults.list' which was only ever understood by GLib.  It
+   * exists widely, but it has never been part of any spec and it should
+   * be treated as deprecated.  This will be removed in a future
+   * version.
+   */
+  filename = g_strdup_printf ("%s/defaults.list", dir->path);
+  desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
+  g_free (filename);
 
-/* GAppInfo interface implementation functions {{{2 */
+  /* Finally, the mimeinfo.cache, which is just a cached copy of what we
+   * would find in the MimeTypes= lines of all of the desktop files.
+   */
+  filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path);
+  desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, MIME_CACHE_GROUP, TRUE);
+  g_free (filename);
+}
 
-static gboolean
-g_desktop_app_info_equal (GAppInfo *appinfo1,
-                          GAppInfo *appinfo2)
+static void
+desktop_file_dir_unindexed_init (DesktopFileDir *dir)
 {
-  GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
-  GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
-
-  if (info1->desktop_id == NULL ||
-      info2->desktop_id == NULL)
-    return info1 == info2;
+  if (!dir->is_config)
+    get_apps_from_dir (&dir->app_names, dir->path, "");
 
-  return strcmp (info1->desktop_id, info2->desktop_id) == 0;
+  desktop_file_dir_unindexed_read_mimeapps_lists (dir);
 }
 
-static const char *
-g_desktop_app_info_get_id (GAppInfo *appinfo)
+static GDesktopAppInfo *
+desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
+                                    const gchar    *desktop_id)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  const gchar *filename;
 
-  return info->desktop_id;
-}
+  filename = g_hash_table_lookup (dir->app_names, desktop_id);
 
-static const char *
-g_desktop_app_info_get_name (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  if (!filename)
+    return NULL;
 
-  if (info->name == NULL)
-    return _("Unnamed");
-  return info->name;
+  return g_desktop_app_info_new_from_filename (filename);
 }
 
-static const char *
-g_desktop_app_info_get_display_name (GAppInfo *appinfo)
+static void
+desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
+                                    GHashTable     *apps)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  GHashTableIter iter;
+  gpointer app_name;
+  gpointer filename;
 
-  if (info->fullname == NULL)
-    return g_desktop_app_info_get_name (appinfo);
-  return info->fullname;
-}
+  if (dir->app_names == NULL)
+    return;
 
-/**
- * g_desktop_app_info_get_is_hidden:
- * @info: a #GDesktopAppInfo.
- *
- * A desktop file is hidden if the Hidden key in it is
- * set to True.
- *
- * Returns: %TRUE if hidden, %FALSE otherwise.
- **/
-gboolean
-g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
-{
-  return info->hidden;
+  g_hash_table_iter_init (&iter, dir->app_names);
+  while (g_hash_table_iter_next (&iter, &app_name, &filename))
+    {
+      if (desktop_file_dir_app_name_is_masked (dir, app_name))
+        continue;
+
+      add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
+    }
 }
 
-/**
- * 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)
+typedef struct _MemoryIndexEntry MemoryIndexEntry;
+typedef GHashTable MemoryIndex;
+
+struct _MemoryIndexEntry
 {
-  return info->filename;
-}
+  const gchar      *app_name; /* pointer to the hashtable key */
+  gint              match_category;
+  MemoryIndexEntry *next;
+};
 
-static const char *
-g_desktop_app_info_get_description (GAppInfo *appinfo)
+static void
+memory_index_entry_free (gpointer data)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  MemoryIndexEntry *mie = data;
 
-  return info->comment;
+  while (mie)
+    {
+      MemoryIndexEntry *next = mie->next;
+
+      g_slice_free (MemoryIndexEntry, mie);
+      mie = next;
+    }
 }
 
-static const char *
-g_desktop_app_info_get_executable (GAppInfo *appinfo)
+static void
+memory_index_add_token (MemoryIndex *mi,
+                        const gchar *token,
+                        gint         match_category,
+                        const gchar *app_name)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  MemoryIndexEntry *mie, *first;
 
-  return info->binary;
-}
+  mie = g_slice_new (MemoryIndexEntry);
+  mie->app_name = app_name;
+  mie->match_category = match_category;
 
-static const char *
-g_desktop_app_info_get_commandline (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  first = g_hash_table_lookup (mi, token);
 
-  return info->exec;
+  if (first)
+    {
+      mie->next = first->next;
+      first->next = mie;
+    }
+  else
+    {
+      mie->next = NULL;
+      g_hash_table_insert (mi, g_strdup (token), mie);
+    }
 }
 
-static GIcon *
-g_desktop_app_info_get_icon (GAppInfo *appinfo)
+static void
+memory_index_add_string (MemoryIndex *mi,
+                         const gchar *string,
+                         gint         match_category,
+                         const gchar *app_name)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  gchar **tokens, **alternates;
+  gint i;
 
-  return info->icon;
-}
+  tokens = g_str_tokenize_and_fold (string, NULL, &alternates);
 
-/**
- * g_desktop_app_info_get_categories:
- * @info: a #GDesktopAppInfo
- *
- * 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)
-{
-  return info->categories;
-}
+  for (i = 0; tokens[i]; i++)
+    memory_index_add_token (mi, tokens[i], match_category, app_name);
 
-/**
- * g_desktop_app_info_get_keywords:
- * @info: a #GDesktopAppInfo
- *
- * Gets the keywords from the desktop file.
- *
- * Returns: (transfer none): The value of the Keywords key
- *
- * Since: 2.32
- */
-const char * const *
-g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
-{
-  return (const char * const *)info->keywords;
-}
+  for (i = 0; alternates[i]; i++)
+    memory_index_add_token (mi, alternates[i], match_category, app_name);
 
-/**
- * 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)
-{
-  return info->generic_name;
+  g_strfreev (alternates);
+  g_strfreev (tokens);
 }
 
-/**
- * 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)
+static MemoryIndex *
+memory_index_new (void)
 {
-  return info->nodisplay;
+  return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free);
 }
 
-/**
- * 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)
+static void
+desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
 {
-  gboolean found;
-  int i;
+  GHashTableIter iter;
+  gpointer app, path;
 
-  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
+  dir->memory_index = memory_index_new ();
+  dir->memory_implementations = memory_index_new ();
 
-  if (!desktop_env) {
-    G_LOCK (g_desktop_env);
-    desktop_env = g_desktop_env;
-    G_UNLOCK (g_desktop_env);
-  }
+  /* Nothing to search? */
+  if (dir->app_names == NULL)
+    return;
 
-  if (info->only_show_in)
+  g_hash_table_iter_init (&iter, dir->app_names);
+  while (g_hash_table_iter_next (&iter, &app, &path))
     {
-      if (desktop_env == NULL)
-        return FALSE;
+      GKeyFile *key_file;
+
+      if (desktop_file_dir_app_name_is_masked (dir, app))
+        continue;
+
+      key_file = g_key_file_new ();
 
-      found = FALSE;
-      for (i = 0; info->only_show_in[i] != NULL; i++)
+      if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) &&
+          !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
         {
-          if (strcmp (info->only_show_in[i], desktop_env) == 0)
+          /* Index the interesting keys... */
+          gchar **implements;
+          gint i;
+
+          for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
             {
-              found = TRUE;
-              break;
+              const gchar *value;
+              gchar *raw;
+
+              if (!desktop_key_match_category[i])
+                continue;
+
+              raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
+              value = raw;
+
+              if (i == DESKTOP_KEY_Exec && raw != NULL)
+                {
+                  /* Special handling: only match basename of first field */
+                  gchar *space;
+                  gchar *slash;
+
+                  /* Remove extra arguments, if any */
+                  space = raw + strcspn (raw, " \t\n"); /* IFS */
+                  *space = '\0';
+
+                  /* Skip the pathname, if any */
+                  if ((slash = strrchr (raw, '/')))
+                    value = slash + 1;
+                }
+
+              if (value)
+                memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
+
+              g_free (raw);
             }
+
+          /* Make note of the Implements= line */
+          implements = g_key_file_get_string_list (key_file, "Desktop Entry", "Implements", NULL, NULL);
+          for (i = 0; implements && implements[i]; i++)
+            memory_index_add_token (dir->memory_implementations, implements[i], 0, app);
+          g_strfreev (implements);
         }
-      if (!found)
-        return FALSE;
+
+      g_key_file_free (key_file);
     }
+}
+
+static void
+desktop_file_dir_unindexed_search (DesktopFileDir  *dir,
+                                   const gchar     *search_token)
+{
+  GHashTableIter iter;
+  gpointer key, value;
+
+  if (!dir->memory_index)
+    desktop_file_dir_unindexed_setup_search (dir);
 
-  if (info->not_show_in && desktop_env)
+  g_hash_table_iter_init (&iter, dir->memory_index);
+  while (g_hash_table_iter_next (&iter, &key, &value))
     {
-      for (i = 0; info->not_show_in[i] != NULL; i++)
+      MemoryIndexEntry *mie = value;
+
+      if (!g_str_has_prefix (key, search_token))
+        continue;
+
+      while (mie)
         {
-          if (strcmp (info->not_show_in[i], desktop_env) == 0)
-            return FALSE;
+          add_token_result (mie->app_name, mie->match_category);
+          mie = mie->next;
         }
     }
-
-  return TRUE;
 }
 
-/* Launching... {{{2 */
+static gboolean
+array_contains (GPtrArray *array,
+                const gchar *str)
+{
+  gint i;
 
-static char *
-expand_macro_single (char macro, char *uri)
+  for (i = 0; i < array->len; i++)
+    if (g_str_equal (array->pdata[i], str))
+      return TRUE;
+
+  return FALSE;
+}
+
+static void
+desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
+                                        const gchar    *mime_type,
+                                        GPtrArray      *hits,
+                                        GPtrArray      *blacklist)
 {
-  GFile *file;
-  char *result = NULL;
-  char *path = NULL;
-  char *name;
+  UnindexedMimeTweaks *tweaks;
+  gint i;
 
-  file = g_file_new_for_uri (uri);
+  tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
 
-  switch (macro)
+  if (!tweaks)
+    return;
+
+  if (tweaks->additions)
     {
-    case 'u':
-    case 'U':
-      result = g_shell_quote (uri);
-      break;
-    case 'f':
-    case 'F':
-      path = g_file_get_path (file);
-      if (path)
-        result = g_shell_quote (path);
-      break;
-    case 'd':
-    case 'D':
-      path = g_file_get_path (file);
-      if (path)
-        {
-          name = g_path_get_dirname (path);
-          result = g_shell_quote (name);
-          g_free (name);
-        }
-      break;
-    case 'n':
-    case 'N':
-      path = g_file_get_path (file);
-      if (path)
+      for (i = 0; tweaks->additions[i]; i++)
         {
-          name = g_path_get_basename (path);
-          result = g_shell_quote (name);
-          g_free (name);
+          gchar *app_name = tweaks->additions[i];
+
+          if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
+              !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
+            g_ptr_array_add (hits, g_strdup (app_name));
         }
-      break;
     }
 
-  g_object_unref (file);
-  g_free (path);
+  if (tweaks->removals)
+    {
+      for (i = 0; tweaks->removals[i]; i++)
+        {
+          gchar *app_name = tweaks->removals[i];
 
-  return result;
+          if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
+              !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
+            g_ptr_array_add (blacklist, app_name);
+        }
+    }
 }
 
 static void
-expand_macro (char              macro,
-              GString          *exec,
-              GDesktopAppInfo  *info,
-              GList           **uri_list)
+desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir,
+                                           const gchar    *mime_type,
+                                           GPtrArray      *results)
 {
-  GList *uris = *uri_list;
-  char *expanded;
-  gboolean force_file_uri;
-  char force_file_uri_macro;
-  char *uri;
+  UnindexedMimeTweaks *tweaks;
+  gint i;
 
-  g_return_if_fail (exec != NULL);
+  tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
 
-  /* 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;
-        }
-    }
+  if (!tweaks || !tweaks->defaults)
+    return;
 
-  switch (macro)
+  for (i = 0; tweaks->defaults[i]; i++)
     {
-    case 'u':
-    case 'f':
-    case 'd':
-    case 'n':
-      if (uris)
-        {
-          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);
-            }
-          uris = uris->next;
-        }
-
-      break;
+      gchar *app_name = tweaks->defaults[i];
 
-    case 'U':
-    case 'F':
-    case 'D':
-    case 'N':
-      while (uris)
-        {
-          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 (!array_contains (results, app_name))
+        g_ptr_array_add (results, g_strdup (app_name));
+    }
+}
 
-          if (expanded)
-            {
-              g_string_append (exec, expanded);
-              g_free (expanded);
-            }
+static void
+desktop_file_dir_unindexed_get_implementations (DesktopFileDir  *dir,
+                                                GList          **results,
+                                                const gchar     *interface)
+{
+  MemoryIndexEntry *mie;
 
-          uris = uris->next;
+  if (!dir->memory_index)
+    desktop_file_dir_unindexed_setup_search (dir);
 
-          if (uris != NULL && expanded)
-            g_string_append_c (exec, ' ');
-        }
+  for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next)
+    *results = g_list_prepend (*results, g_strdup (mie->app_name));
+}
 
-      break;
+/* DesktopFileDir "API" {{{2 */
 
-    case 'i':
-      if (info->icon_name)
-        {
-          g_string_append (exec, "--icon ");
-          expanded = g_shell_quote (info->icon_name);
-          g_string_append (exec, expanded);
-          g_free (expanded);
-        }
-      break;
+/*< internal >
+ * desktop_file_dir_create:
+ * @array: the #GArray to add a new item to
+ * @data_dir: an XDG_DATA_DIR
+ *
+ * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
+ * to @array.
+ */
+static void
+desktop_file_dir_create (GArray      *array,
+                         const gchar *data_dir)
+{
+  DesktopFileDir dir = { 0, };
 
-    case 'c':
-      if (info->name)
-        {
-          expanded = g_shell_quote (info->name);
-          g_string_append (exec, expanded);
-          g_free (expanded);
-        }
-      break;
+  dir.path = g_build_filename (data_dir, "applications", NULL);
 
-    case 'k':
-      if (info->filename)
-        {
-          expanded = g_shell_quote (info->filename);
-          g_string_append (exec, expanded);
-          g_free (expanded);
-        }
-      break;
+  g_array_append_val (array, dir);
+}
 
-    case 'm': /* deprecated */
-      break;
+/*< internal >
+ * desktop_file_dir_create:
+ * @array: the #GArray to add a new item to
+ * @config_dir: an XDG_CONFIG_DIR
+ *
+ * Just the same as desktop_file_dir_create() except that it does not
+ * add the "applications" directory.  It also marks the directory as
+ * config-only, which prevents us from attempting to find desktop files
+ * here.
+ */
+static void
+desktop_file_dir_create_for_config (GArray      *array,
+                                    const gchar *config_dir)
+{
+  DesktopFileDir dir = { 0, };
 
-    case '%':
-      g_string_append_c (exec, '%');
-      break;
-    }
+  dir.path = g_strdup (config_dir);
+  dir.is_config = TRUE;
 
-  *uri_list = uris;
+  g_array_append_val (array, dir);
 }
 
-static gboolean
-expand_application_parameters (GDesktopAppInfo   *info,
-                               const gchar       *exec_line,
-                               GList            **uris,
-                               int               *argc,
-                               char            ***argv,
-                               GError           **error)
+/*< internal >
+ * desktop_file_dir_reset:
+ * @dir: a #DesktopFileDir
+ *
+ * Cleans up @dir, releasing most resources that it was using.
+ */
+static void
+desktop_file_dir_reset (DesktopFileDir *dir)
 {
-  GList *uri_list = *uris;
-  const char *p = exec_line;
-  GString *expanded_exec;
-  gboolean res;
+  if (dir->alternatively_watching)
+    {
+      g_free (dir->alternatively_watching);
+      dir->alternatively_watching = NULL;
+    }
 
-  if (exec_line == NULL)
+  if (dir->monitor)
     {
-      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                           _("Desktop file didn't specify Exec field"));
-      return FALSE;
+      g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
+      g_object_unref (dir->monitor);
+      dir->monitor = NULL;
     }
 
-  expanded_exec = g_string_new (NULL);
+  if (dir->app_names)
+    {
+      g_hash_table_unref (dir->app_names);
+      dir->app_names = NULL;
+    }
 
-  while (*p)
+  if (dir->memory_index)
     {
-      if (p[0] == '%' && p[1] != '\0')
-        {
-          expand_macro (p[1], expanded_exec, info, uris);
-          p++;
-        }
-      else
-        g_string_append_c (expanded_exec, *p);
+      g_hash_table_unref (dir->memory_index);
+      dir->memory_index = NULL;
+    }
 
-      p++;
+  if (dir->mime_tweaks)
+    {
+      g_hash_table_unref (dir->mime_tweaks);
+      dir->mime_tweaks = NULL;
     }
 
-  /* No file substitutions */
-  if (uri_list == *uris && uri_list != NULL)
+  if (dir->memory_implementations)
     {
-      /* 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, uris);
+      g_hash_table_unref (dir->memory_implementations);
+      dir->memory_implementations = NULL;
     }
 
-  res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
-  g_string_free (expanded_exec, TRUE);
-  return res;
+  dir->is_setup = FALSE;
 }
 
-static gboolean
-prepend_terminal_to_vector (int    *argc,
-                            char ***argv)
+/*< internal >
+ * desktop_file_dir_init:
+ * @dir: a #DesktopFileDir
+ *
+ * Does initial setup for @dir
+ *
+ * You should only call this if @dir is not already setup.
+ */
+static void
+desktop_file_dir_init (DesktopFileDir *dir)
 {
-#ifndef G_OS_WIN32
-  char **real_argv;
-  int real_argc;
-  int i, j;
-  char **term_argv = NULL;
-  int term_argc = 0;
-  char *check;
-  char **the_argv;
+  const gchar *watch_dir;
 
-  g_return_val_if_fail (argc != NULL, FALSE);
-  g_return_val_if_fail (argv != NULL, FALSE);
+  g_assert (!dir->is_setup);
 
-  /* sanity */
-  if(*argv == NULL)
-    *argc = 0;
+  g_assert (!dir->alternatively_watching);
+  g_assert (!dir->monitor);
 
-  the_argv = *argv;
+  dir->alternatively_watching = desktop_file_dir_get_alternative_dir (dir);
+  watch_dir = dir->alternatively_watching ? dir->alternatively_watching : dir->path;
 
-  /* compute size if not given */
-  if (*argc < 0)
-    {
-      for (i = 0; the_argv[i] != NULL; i++)
-        ;
-      *argc = i;
-    }
-
-  term_argc = 2;
-  term_argv = g_new0 (char *, 3);
+  /* There is a very thin race here if the watch_dir has been _removed_
+   * between when we checked for it and when we establish the watch.
+   * Removes probably don't happen in usual operation, and even if it
+   * does (and we catch the unlikely race), the only degradation is that
+   * we will fall back to polling.
+   */
+  dir->monitor = g_local_directory_monitor_new_in_worker (watch_dir, G_FILE_MONITOR_NONE, NULL);
 
-  check = g_find_program_in_path ("gnome-terminal");
-  if (check != NULL)
-    {
-      term_argv[0] = check;
-      /* Note that gnome-terminal takes -x and
-       * as -e in gnome-terminal is broken we use that. */
-      term_argv[1] = g_strdup ("-x");
-    }
-  else
+  if (dir->monitor)
     {
-      if (check == NULL)
-        check = g_find_program_in_path ("nxterm");
-      if (check == NULL)
-        check = g_find_program_in_path ("color-xterm");
-      if (check == NULL)
-        check = g_find_program_in_path ("rxvt");
-      if (check == NULL)
-        check = g_find_program_in_path ("xterm");
-      if (check == NULL)
-        check = g_find_program_in_path ("dtterm");
-      if (check == NULL)
-        {
-          check = g_strdup ("xterm");
-          g_warning ("couldn't find a terminal, falling back to xterm");
-        }
-      term_argv[0] = check;
-      term_argv[1] = g_strdup ("-e");
+      g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
+      g_local_directory_monitor_start (dir->monitor);
     }
 
-  real_argc = term_argc + *argc;
-  real_argv = g_new (char *, real_argc + 1);
-
-  for (i = 0; i < term_argc; i++)
-    real_argv[i] = term_argv[i];
-
-  for (j = 0; j < *argc; j++, i++)
-    real_argv[i] = (char *)the_argv[j];
+  desktop_file_dir_unindexed_init (dir);
 
-  real_argv[i] = NULL;
+  dir->is_setup = TRUE;
+}
 
-  g_free (*argv);
-  *argv = real_argv;
-  *argc = real_argc;
+/*< internal >
+ * desktop_file_dir_get_app:
+ * @dir: a DesktopFileDir
+ * @desktop_id: the desktop ID to load
+ *
+ * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
+ * within @dir, even if it is hidden.
+ *
+ * This function does not check if @desktop_id would be masked by a
+ * directory with higher precedence.  The caller must do so.
+ */
+static GDesktopAppInfo *
+desktop_file_dir_get_app (DesktopFileDir *dir,
+                          const gchar    *desktop_id)
+{
+  if (!dir->app_names)
+    return NULL;
 
-  /* we use g_free here as we sucked all the inner strings
-   * out from it into real_argv */
-  g_free (term_argv);
-  return TRUE;
-#else
-  return FALSE;
-#endif /* G_OS_WIN32 */
+  return desktop_file_dir_unindexed_get_app (dir, desktop_id);
 }
 
-static GList *
-create_files_for_uris (GList *uris)
+/*< internal >
+ * desktop_file_dir_get_all:
+ * @dir: a DesktopFileDir
+ * @apps: a #GHashTable<string, GDesktopAppInfo>
+ *
+ * Loads all desktop files in @dir and adds them to @apps, careful to
+ * ensure we don't add any files masked by a similarly-named file in a
+ * higher-precedence directory.
+ */
+static void
+desktop_file_dir_get_all (DesktopFileDir *dir,
+                          GHashTable     *apps)
 {
-  GList *res;
-  GList *iter;
+  desktop_file_dir_unindexed_get_all (dir, apps);
+}
 
-  res = NULL;
+/*< internal >
+ * desktop_file_dir_mime_lookup:
+ * @dir: a #DesktopFileDir
+ * @mime_type: the mime type to look up
+ * @hits: the array to store the hits
+ * @blacklist: the array to store the blacklist
+ *
+ * Does a lookup of a mimetype against one desktop file directory,
+ * recording any hits and blacklisting and "Removed" associations (so
+ * later directories don't record them as hits).
+ *
+ * The items added to @hits are duplicated, but the ones in @blacklist
+ * are weak pointers.  This facilitates simply freeing the blacklist
+ * (which is only used for internal bookkeeping) but using the pdata of
+ * @hits as the result of the operation.
+ */
+static void
+desktop_file_dir_mime_lookup (DesktopFileDir *dir,
+                              const gchar    *mime_type,
+                              GPtrArray      *hits,
+                              GPtrArray      *blacklist)
+{
+  desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blacklist);
+}
 
-  for (iter = uris; iter; iter = iter->next)
-    {
-      GFile *file = g_file_new_for_uri ((char *)iter->data);
-      res = g_list_prepend (res, file);
-    }
+/*< internal >
+ * desktop_file_dir_default_lookup:
+ * @dir: a #DesktopFileDir
+ * @mime_type: the mime type to look up
+ * @results: an array to store the results in
+ *
+ * Collects the "default" applications for a given mime type from @dir.
+ */
+static void
+desktop_file_dir_default_lookup (DesktopFileDir *dir,
+                                 const gchar    *mime_type,
+                                 GPtrArray      *results)
+{
+  desktop_file_dir_unindexed_default_lookup (dir, mime_type, results);
+}
 
-  return g_list_reverse (res);
+/*< internal >
+ * desktop_file_dir_search:
+ * @dir: a #DesktopFileDir
+ * @term: a normalised and casefolded search term
+ *
+ * Finds the names of applications in @dir that match @term.
+ */
+static void
+desktop_file_dir_search (DesktopFileDir *dir,
+                         const gchar    *search_token)
+{
+  desktop_file_dir_unindexed_search (dir, search_token);
 }
 
-typedef struct
+static void
+desktop_file_dir_get_implementations (DesktopFileDir  *dir,
+                                      GList          **results,
+                                      const gchar     *interface)
 {
-  GSpawnChildSetupFunc user_setup;
-  gpointer user_setup_data;
+  desktop_file_dir_unindexed_get_implementations (dir, results, interface);
+}
 
-  char *pid_envvar;
-} ChildSetupData;
+/* Lock/unlock and global setup API {{{2 */
 
 static void
-child_setup (gpointer user_data)
+desktop_file_dirs_lock (void)
 {
-  ChildSetupData *data = user_data;
+  gint i;
 
-  if (data->pid_envvar)
+  g_mutex_lock (&desktop_file_dir_lock);
+
+  if (desktop_file_dirs == NULL)
     {
-      pid_t pid = getpid ();
-      char buf[20];
-      int i;
+      const char * const *dirs;
+      GArray *tmp;
+      gint i;
 
-      /* Write the pid into the space already reserved for it in the
-       * environment array. We can't use sprintf because it might
-       * malloc, so we do it by hand. It's simplest to write the pid
-       * out backwards first, then copy it over.
-       */
-      for (i = 0; pid; i++, pid /= 10)
-        buf[i] = (pid % 10) + '0';
-      for (i--; i >= 0; i--)
-        *(data->pid_envvar++) = buf[i];
-      *data->pid_envvar = '\0';
-    }
+      tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
 
-  if (data->user_setup)
-    data->user_setup (data->user_setup_data);
-}
+      /* First, the configs.  Highest priority: the user's ~/.config */
+      desktop_file_dir_create_for_config (tmp, g_get_user_config_dir ());
 
-static void
-notify_desktop_launch (GDBusConnection  *session_bus,
-                       GDesktopAppInfo  *info,
-                       long              pid,
-                       const char       *display,
-                       const char       *sn_id,
-                       GList            *uris)
-{
-  GDBusMessage *msg;
-  GVariantBuilder uri_variant;
-  GVariantBuilder extras_variant;
-  GList *iter;
-  const char *desktop_file_id;
-  const char *gio_desktop_file;
+      /* Next, the system configs (/etc/xdg, and so on). */
+      dirs = g_get_system_config_dirs ();
+      for (i = 0; dirs[i]; i++)
+        desktop_file_dir_create_for_config (tmp, dirs[i]);
 
-  if (session_bus == NULL)
-    return;
+      /* Now the data.  Highest priority: the user's ~/.local/share/applications */
+      desktop_file_dir_user_data_index = tmp->len;
+      desktop_file_dir_create (tmp, g_get_user_data_dir ());
 
-  g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
-  for (iter = uris; iter; iter = iter->next)
-    g_variant_builder_add (&uri_variant, "s", iter->data);
+      /* Following that, XDG_DATA_DIRS/applications, in order */
+      dirs = g_get_system_data_dirs ();
+      for (i = 0; dirs[i]; i++)
+        desktop_file_dir_create (tmp, dirs[i]);
 
-  g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
-  if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
-    g_variant_builder_add (&extras_variant, "{sv}",
-                           "startup-id",
-                           g_variant_new ("s",
-                                          sn_id));
-  gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
-  if (gio_desktop_file != NULL)
-    g_variant_builder_add (&extras_variant, "{sv}",
-                           "origin-desktop-file",
-                           g_variant_new_bytestring (gio_desktop_file));
-  if (g_get_prgname () != NULL)
-    g_variant_builder_add (&extras_variant, "{sv}",
-                           "origin-prgname",
-                           g_variant_new_bytestring (g_get_prgname ()));
-  g_variant_builder_add (&extras_variant, "{sv}",
-                         "origin-pid",
-                         g_variant_new ("x",
-                                        (gint64)getpid ()));
+      /* The list of directories will never change after this. */
+      desktop_file_dirs = (DesktopFileDir *) tmp->data;
+      n_desktop_file_dirs = tmp->len;
 
-  if (info->filename)
-    desktop_file_id = info->filename;
-  else if (info->desktop_id)
-    desktop_file_id = info->desktop_id;
-  else
-    desktop_file_id = "";
+      g_array_free (tmp, FALSE);
+    }
 
-  msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
-                                   "org.gtk.gio.DesktopAppInfo",
-                                   "Launched");
-  g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
-                                               g_variant_new_bytestring (desktop_file_id),
-                                               display ? display : "",
-                                               (gint64)pid,
-                                               &uri_variant,
-                                               &extras_variant));
-  g_dbus_connection_send_message (session_bus,
-                                  msg, 0,
-                                  NULL,
-                                  NULL);
-  g_object_unref (msg);
+  for (i = 0; i < n_desktop_file_dirs; i++)
+    if (!desktop_file_dirs[i].is_setup)
+      desktop_file_dir_init (&desktop_file_dirs[i]);
 }
 
-#define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
+static void
+desktop_file_dirs_unlock (void)
+{
+  g_mutex_unlock (&desktop_file_dir_lock);
+}
 
-static gboolean
-g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
-                                           GDBusConnection            *session_bus,
-                                           const gchar                *exec_line,
-                                           GList                      *uris,
-                                           GAppLaunchContext          *launch_context,
-                                           GSpawnFlags                 spawn_flags,
-                                           GSpawnChildSetupFunc        user_setup,
-                                           gpointer                    user_setup_data,
-                                           GDesktopAppLaunchCallback   pid_callback,
-                                           gpointer                    pid_callback_data,
-                                           GError                    **error)
+static void
+desktop_file_dirs_invalidate_user_config (void)
 {
-  gboolean completed = FALSE;
-  GList *old_uris;
-  char **argv, **envp;
-  int argc;
-  ChildSetupData data;
+  g_mutex_lock (&desktop_file_dir_lock);
 
-  g_return_val_if_fail (info != NULL, FALSE);
+  if (n_desktop_file_dirs)
+    desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_config_index]);
 
-  argv = NULL;
+  g_mutex_unlock (&desktop_file_dir_lock);
+}
 
-  if (launch_context)
-    envp = g_app_launch_context_get_environment (launch_context);
-  else
-    envp = g_get_environ ();
+static void
+desktop_file_dirs_invalidate_user_data (void)
+{
+  g_mutex_lock (&desktop_file_dir_lock);
 
-  do
-    {
-      GPid pid;
-      GList *launched_uris;
-      GList *iter;
-      char *display, *sn_id;
+  if (n_desktop_file_dirs)
+    desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_data_index]);
 
-      old_uris = uris;
-      if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
-        goto out;
+  g_mutex_unlock (&desktop_file_dir_lock);
+}
 
-      /* Get the subset of URIs we're launching with this process */
-      launched_uris = NULL;
-      for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
-        launched_uris = g_list_prepend (launched_uris, iter->data);
-      launched_uris = g_list_reverse (launched_uris);
+/* GDesktopAppInfo implementation {{{1 */
+/* GObject implementation {{{2 */
+static void
+g_desktop_app_info_finalize (GObject *object)
+{
+  GDesktopAppInfo *info;
 
-      if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
-        {
-          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                               _("Unable to find terminal required for application"));
-          goto out;
-        }
+  info = G_DESKTOP_APP_INFO (object);
 
-      data.user_setup = user_setup;
-      data.user_setup_data = user_setup_data;
+  g_free (info->desktop_id);
+  g_free (info->filename);
 
-      if (info->filename)
-        {
-          envp = g_environ_setenv (envp,
-                                   "GIO_LAUNCHED_DESKTOP_FILE",
-                                   info->filename,
-                                   TRUE);
-          envp = g_environ_setenv (envp,
-                                   "GIO_LAUNCHED_DESKTOP_FILE_PID",
-                                   "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
-                                   TRUE);
-          data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
-        }
-      else
-        {
-          data.pid_envvar = NULL;
-        }
-
-      display = NULL;
-      sn_id = NULL;
-      if (launch_context)
-        {
-          GList *launched_files = create_files_for_uris (launched_uris);
-
-          display = g_app_launch_context_get_display (launch_context,
-                                                      G_APP_INFO (info),
-                                                      launched_files);
-          if (display)
-            envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
-
-          if (info->startup_notify)
-            {
-              sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
-                                                                  G_APP_INFO (info),
-                                                                  launched_files);
-              envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
-            }
-
-          g_list_free_full (launched_files, g_object_unref);
-        }
-
-      if (!g_spawn_async (info->path,
-                          argv,
-                          envp,
-                          spawn_flags,
-                          child_setup,
-                          &data,
-                          &pid,
-                          error))
-        {
-          if (sn_id)
-            g_app_launch_context_launch_failed (launch_context, sn_id);
-
-          g_free (display);
-          g_free (sn_id);
-          g_list_free (launched_uris);
-
-          goto out;
-        }
-
-      if (pid_callback != NULL)
-        pid_callback (info, pid, pid_callback_data);
+  if (info->keyfile)
+    g_key_file_unref (info->keyfile);
 
-      if (launch_context != NULL)
-        {
-          GVariantBuilder builder;
-          GVariant *platform_data;
+  g_free (info->name);
+  g_free (info->generic_name);
+  g_free (info->fullname);
+  g_free (info->comment);
+  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);
+  g_free (info->exec);
+  g_free (info->binary);
+  g_free (info->path);
+  g_free (info->categories);
+  g_free (info->startup_wm_class);
+  g_strfreev (info->mime_types);
+  g_free (info->app_id);
+  g_strfreev (info->actions);
 
-          g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
-          g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
-          if (sn_id)
-            g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
-          platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
-          g_signal_emit_by_name (launch_context, "launched", info, platform_data);
-          g_variant_unref (platform_data);
-        }
+  G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
+}
 
-      notify_desktop_launch (session_bus,
-                             info,
-                             pid,
-                             display,
-                             sn_id,
-                             launched_uris);
+static void
+g_desktop_app_info_set_property (GObject      *object,
+                                 guint         prop_id,
+                                 const GValue *value,
+                                 GParamSpec   *pspec)
+{
+  GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
 
-      g_free (display);
-      g_free (sn_id);
-      g_list_free (launched_uris);
+  switch (prop_id)
+    {
+    case PROP_FILENAME:
+      self->filename = g_value_dup_string (value);
+      break;
 
-      g_strfreev (argv);
-      argv = NULL;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
     }
-  while (uris != NULL);
-
-  completed = TRUE;
-
- out:
-  g_strfreev (argv);
-  g_strfreev (envp);
-
-  return completed;
 }
 
-static gchar *
-object_path_from_appid (const gchar *app_id)
+static void
+g_desktop_app_info_get_property (GObject    *object,
+                                 guint       prop_id,
+                                 GValue     *value,
+                                 GParamSpec *pspec)
 {
-  gchar *path;
-  gint i, n;
-
-  n = strlen (app_id);
-  path = g_malloc (n + 2);
+  GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
 
-  path[0] = '/';
+  switch (prop_id)
+    {
+    case PROP_FILENAME:
+      g_value_set_string (value, self->filename);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
 
-  for (i = 0; i < n; i++)
-    if (app_id[i] != '.')
-      path[i + 1] = app_id[i];
-    else
-      path[i + 1] = '/';
+static void
+g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
-  path[i + 1] = '\0';
+  gobject_class->get_property = g_desktop_app_info_get_property;
+  gobject_class->set_property = g_desktop_app_info_set_property;
+  gobject_class->finalize = g_desktop_app_info_finalize;
 
-  return path;
+  /**
+   * GDesktopAppInfo:filename:
+   *
+   * The origin filename of this #GDesktopAppInfo
+   */
+  g_object_class_install_property (gobject_class,
+                                   PROP_FILENAME,
+                                   g_param_spec_string ("filename", "Filename", "", NULL,
+                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 }
 
-static GVariant *
-g_desktop_app_info_make_platform_data (GDesktopAppInfo   *info,
-                                       GList             *uris,
-                                       GAppLaunchContext *launch_context)
+static void
+g_desktop_app_info_init (GDesktopAppInfo *local)
 {
-  GVariantBuilder builder;
-
-  g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
-
-  if (launch_context)
-    {
-      GList *launched_files = create_files_for_uris (uris);
+}
 
-      if (info->startup_notify)
-        {
-          gchar *sn_id;
+/* Construction... {{{2 */
 
-          sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
-          g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
-        }
+/*< internal >
+ * binary_from_exec:
+ * @exec: an exec line
+ *
+ * Returns the first word in an exec line (ie: the binary name).
+ *
+ * If @exec is "  progname --foo %F" then returns "progname".
+ */
+static char *
+binary_from_exec (const char *exec)
+{
+  const char *p, *start;
 
-      g_list_free_full (launched_files, g_object_unref);
-    }
+  p = exec;
+  while (*p == ' ')
+    p++;
+  start = p;
+  while (*p != ' ' && *p != 0)
+    p++;
 
-  return g_variant_builder_end (&builder);
+  return g_strndup (start, p - start);
 }
 
 static gboolean
-g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo    *info,
-                                          GDBusConnection    *session_bus,
-                                          GList              *uris,
-                                          GAppLaunchContext  *launch_context)
+g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
+                                      GKeyFile        *key_file)
 {
-  GVariantBuilder builder;
-  gchar *object_path;
-
-  g_return_val_if_fail (info != NULL, FALSE);
-
-  g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
+  char *start_group;
+  char *type;
+  char *try_exec;
+  char *exec;
+  gboolean bus_activatable;
 
-  if (uris)
+  start_group = g_key_file_get_start_group (key_file);
+  if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
     {
-      GList *iter;
-
-      g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
-      for (iter = uris; iter; iter = iter->next)
-        g_variant_builder_add (&builder, "s", iter->data);
-      g_variant_builder_close (&builder);
+      g_free (start_group);
+      return FALSE;
     }
+  g_free (start_group);
 
-  g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
+  type = g_key_file_get_string (key_file,
+                                G_KEY_FILE_DESKTOP_GROUP,
+                                G_KEY_FILE_DESKTOP_KEY_TYPE,
+                                NULL);
+  if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
+    {
+      g_free (type);
+      return FALSE;
+    }
+  g_free (type);
 
-  /* This is non-blocking API.  Similar to launching via fork()/exec()
-   * we don't wait around to see if the program crashed during startup.
-   * This is what startup-notification's job is...
-   */
-  object_path = object_path_from_appid (info->app_id);
-  g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
-                          uris ? "Open" : "Activate", g_variant_builder_end (&builder),
-                          NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
-  g_free (object_path);
+  try_exec = g_key_file_get_string (key_file,
+                                    G_KEY_FILE_DESKTOP_GROUP,
+                                    G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
+                                    NULL);
+  if (try_exec && try_exec[0] != '\0')
+    {
+      char *t;
+      t = g_find_program_in_path (try_exec);
+      if (t == NULL)
+        {
+          g_free (try_exec);
+          return FALSE;
+        }
+      g_free (t);
+    }
 
-  return TRUE;
-}
+  exec = g_key_file_get_string (key_file,
+                                G_KEY_FILE_DESKTOP_GROUP,
+                                G_KEY_FILE_DESKTOP_KEY_EXEC,
+                                NULL);
+  if (exec && exec[0] != '\0')
+    {
+      gint argc;
+      char **argv;
+      if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
+        {
+          g_free (exec);
+          g_free (try_exec);
+          return FALSE;
+        }
+      else
+        {
+          char *t;
+          t = g_find_program_in_path (argv[0]);
+          g_strfreev (argv);
 
-static gboolean
-g_desktop_app_info_launch_uris_internal (GAppInfo                   *appinfo,
-                                         GList                      *uris,
-                                         GAppLaunchContext          *launch_context,
-                                         GSpawnFlags                 spawn_flags,
-                                         GSpawnChildSetupFunc        user_setup,
-                                         gpointer                    user_setup_data,
-                                         GDesktopAppLaunchCallback   pid_callback,
-                                         gpointer                    pid_callback_data,
-                                         GError                     **error)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-  GDBusConnection *session_bus;
-  gboolean success = TRUE;
+          if (t == NULL)
+            {
+              g_free (exec);
+              g_free (try_exec);
+              return FALSE;
+            }
+          g_free (t);
+        }
+    }
 
-  session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, 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->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);
+  info->only_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN, NULL, NULL);
+  info->not_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN, NULL, NULL);
+  info->try_exec = try_exec;
+  info->exec = exec;
+  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;
+  info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
+  info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
+  info->mime_types = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL);
+  bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
+  info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
 
-  if (session_bus && info->app_id)
-    g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
-  else
-    success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
-                                                         spawn_flags, user_setup, user_setup_data,
-                                                         pid_callback, pid_callback_data, error);
+  /* Remove the special-case: no Actions= key just means 0 extra actions */
+  if (info->actions == NULL)
+    info->actions = g_new0 (gchar *, 0 + 1);
 
-  if (session_bus != NULL)
+  info->icon = NULL;
+  if (info->icon_name)
     {
-      /* This asynchronous flush holds a reference until it completes,
-       * which ensures that the following unref won't immediately kill
-       * the connection if we were the initial owner.
-       */
-      g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
-      g_object_unref (session_bus);
+      if (g_path_is_absolute (info->icon_name))
+        {
+          GFile *file;
+
+          file = g_file_new_for_path (info->icon_name);
+          info->icon = g_file_icon_new (file);
+          g_object_unref (file);
+        }
+      else
+        {
+          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);
+        }
     }
 
-  return success;
-}
+  if (info->exec)
+    info->binary = binary_from_exec (info->exec);
 
-static gboolean
-g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
-                                GList              *uris,
-                                GAppLaunchContext  *launch_context,
-                                GError            **error)
-{
-  return g_desktop_app_info_launch_uris_internal (appinfo, uris,
-                                                  launch_context,
-                                                  _SPAWN_FLAGS_DEFAULT,
-                                                  NULL, NULL, NULL, NULL,
-                                                  error);
-}
+  if (info->path && info->path[0] == '\0')
+    {
+      g_free (info->path);
+      info->path = NULL;
+    }
 
-static gboolean
-g_desktop_app_info_supports_uris (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  /* Can only be DBusActivatable if we know the filename, which means
+   * that this won't work for the load-from-keyfile case.
+   */
+  if (bus_activatable && info->filename)
+    {
+      gchar *basename;
+      gchar *last_dot;
 
-  return info->exec &&
-    ((strstr (info->exec, "%u") != NULL) ||
-     (strstr (info->exec, "%U") != NULL));
-}
+      basename = g_path_get_basename (info->filename);
+      last_dot = strrchr (basename, '.');
 
-static gboolean
-g_desktop_app_info_supports_files (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+      if (last_dot && g_str_equal (last_dot, ".desktop"))
+        {
+          *last_dot = '\0';
 
-  return info->exec &&
-    ((strstr (info->exec, "%f") != NULL) ||
-     (strstr (info->exec, "%F") != NULL));
+          if (g_dbus_is_interface_name (basename))
+            info->app_id = g_strdup (basename);
+        }
+
+      g_free (basename);
+    }
+
+  info->keyfile = g_key_file_ref (key_file);
+
+  return TRUE;
 }
 
 static gboolean
-g_desktop_app_info_launch (GAppInfo           *appinfo,
-                           GList              *files,
-                           GAppLaunchContext  *launch_context,
-                           GError            **error)
+g_desktop_app_info_load_file (GDesktopAppInfo *self)
 {
-  GList *uris;
-  char *uri;
-  gboolean res;
+  GKeyFile *key_file;
+  gboolean retval = FALSE;
 
-  uris = NULL;
-  while (files)
-    {
-      uri = g_file_get_uri (files->data);
-      uris = g_list_prepend (uris, uri);
-      files = files->next;
-    }
+  g_return_val_if_fail (self->filename != NULL, FALSE);
 
-  uris = g_list_reverse (uris);
+  self->desktop_id = g_path_get_basename (self->filename);
 
-  res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
+  key_file = g_key_file_new ();
 
-  g_list_free_full (uris, g_free);
+  if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
+    retval = g_desktop_app_info_load_from_keyfile (self, key_file);
 
-  return res;
+  g_key_file_unref (key_file);
+  return retval;
 }
 
 /**
- * g_desktop_app_info_launch_uris_as_manager:
- * @appinfo: a #GDesktopAppInfo
- * @uris: (element-type utf8): List of URIs
- * @launch_context: a #GAppLaunchContext
- * @spawn_flags: #GSpawnFlags, used for each process
- * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
- *     each process.
- * @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: 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
- * launch applications.  Ordinary applications should use
- * g_app_info_launch_uris().
+ * g_desktop_app_info_new_from_keyfile:
+ * @key_file: an opened #GKeyFile
  *
- * If the application is launched via traditional UNIX fork()/exec()
- * then @spawn_flags, @user_setup and @user_setup_data are used for the
- * call to g_spawn_async().  Additionally, @pid_callback (with
- * @pid_callback_data) will be called to inform about the PID of the
- * created process.
+ * Creates a new #GDesktopAppInfo.
  *
- * If application launching occurs via some other mechanism (eg: D-Bus
- * activation) then @spawn_flags, @user_setup, @user_setup_data,
- * @pid_callback and @pid_callback_data are ignored.
+ * Returns: a new #GDesktopAppInfo or %NULL on error.
  *
- * Returns: %TRUE on successful launch, %FALSE otherwise.
- */
-gboolean
-g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo            *appinfo,
-                                           GList                      *uris,
-                                           GAppLaunchContext          *launch_context,
-                                           GSpawnFlags                 spawn_flags,
-                                           GSpawnChildSetupFunc        user_setup,
-                                           gpointer                    user_setup_data,
-                                           GDesktopAppLaunchCallback   pid_callback,
-                                           gpointer                    pid_callback_data,
-                                           GError                    **error)
+ * Since: 2.18
+ **/
+GDesktopAppInfo *
+g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
 {
-  return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
-                                                  uris,
-                                                  launch_context,
-                                                  spawn_flags,
-                                                  user_setup,
-                                                  user_setup_data,
-                                                  pid_callback,
-                                                  pid_callback_data,
-                                                  error);
+  GDesktopAppInfo *info;
+
+  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
+  info->filename = NULL;
+  if (!g_desktop_app_info_load_from_keyfile (info, key_file))
+    {
+      g_object_unref (info);
+      return NULL;
+    }
+  return info;
 }
 
-/* OnlyShowIn API support {{{2 */
+/**
+ * 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)
+{
+  GDesktopAppInfo *info = NULL;
+
+  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
+  if (!g_desktop_app_info_load_file (info))
+    {
+      g_object_unref (info);
+      return NULL;
+    }
+  return info;
+}
 
 /**
- * g_desktop_app_info_set_desktop_env:
- * @desktop_env: a string specifying what desktop this is
+ * g_desktop_app_info_new:
+ * @desktop_id: the desktop file id
  *
- * Sets the name of the desktop that the application is running in.
- * 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.
+ * Creates a new #GDesktopAppInfo based on a desktop file id.
  *
- * 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>LXDE</member>
- *   <member>Unity</member>
- *   <member>Old</member>
- * </simplelist>
+ * 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 `applications` subdirectories of the XDG
+ * data directories (i.e. the directories specified in the `XDG_DATA_HOME`
+ * and `XDG_DATA_DIRS` environment variables). GIO also supports the
+ * prefix-to-subdirectory mapping that is described in the
+ * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/)
+ * (i.e. a desktop id of kde-foo.desktop will match
+ * `/usr/share/applications/kde/foo.desktop`).
  *
- * Should be called only once; subsequent calls are ignored.
+ * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
  */
-void
-g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
+GDesktopAppInfo *
+g_desktop_app_info_new (const char *desktop_id)
 {
-  G_LOCK (g_desktop_env);
-  if (!g_desktop_env)
-    g_desktop_env = g_strdup (desktop_env);
-  G_UNLOCK (g_desktop_env);
-}
+  GDesktopAppInfo *appinfo = NULL;
+  guint i;
 
-static gboolean
-g_desktop_app_info_should_show (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  desktop_file_dirs_lock ();
 
-  if (info->nodisplay)
-    return FALSE;
+  for (i = 0; i < n_desktop_file_dirs; i++)
+    {
+      appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
 
-  return g_desktop_app_info_get_show_in (info, NULL);
-}
+      if (appinfo)
+        break;
+    }
 
-/* mime types/default apps support {{{2 */
+  desktop_file_dirs_unlock ();
 
-typedef enum {
-  APP_DIR,
-  MIMETYPE_DIR
-} DirType;
+  if (appinfo == NULL)
+    return NULL;
 
-static char *
-ensure_dir (DirType   type,
-            GError  **error)
+  g_free (appinfo->desktop_id);
+  appinfo->desktop_id = g_strdup (desktop_id);
+
+  if (g_desktop_app_info_get_is_hidden (appinfo))
+    {
+      g_object_unref (appinfo);
+      appinfo = NULL;
+    }
+
+  return appinfo;
+}
+
+static GAppInfo *
+g_desktop_app_info_dup (GAppInfo *appinfo)
 {
-  char *path, *display_name;
-  int errsv;
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  GDesktopAppInfo *new_info;
 
-  if (type == APP_DIR)
-    path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
-  else
-    path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
+  new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
 
-  errno = 0;
-  if (g_mkdir_with_parents (path, 0700) == 0)
-    return path;
+  new_info->filename = g_strdup (info->filename);
+  new_info->desktop_id = g_strdup (info->desktop_id);
 
-  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 (errsv),
-                 _("Can't create user application configuration folder %s: %s"),
-                 display_name, g_strerror (errsv));
-  else
-    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 (errsv));
+  if (info->keyfile)
+    new_info->keyfile = g_key_file_ref (info->keyfile);
 
-  g_free (display_name);
-  g_free (path);
+  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);
+  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);
+  new_info->exec = g_strdup (info->exec);
+  new_info->binary = g_strdup (info->binary);
+  new_info->path = g_strdup (info->path);
+  new_info->app_id = g_strdup (info->app_id);
+  new_info->hidden = info->hidden;
+  new_info->terminal = info->terminal;
+  new_info->startup_notify = info->startup_notify;
 
-  return NULL;
+  return G_APP_INFO (new_info);
 }
 
+/* GAppInfo interface implementation functions {{{2 */
+
 static gboolean
-update_mimeapps_list (const char  *desktop_id,
-                      const char  *content_type,
-                      UpdateMimeFlags flags,
-                      GError     **error)
+g_desktop_app_info_equal (GAppInfo *appinfo1,
+                          GAppInfo *appinfo2)
 {
-  char *dirname, *filename, *string;
-  GKeyFile *key_file;
-  gboolean load_succeeded, res;
-  char **old_list, **list;
-  gsize length, data_size;
-  char *data;
-  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)));
+  GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
+  GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
 
-  dirname = ensure_dir (APP_DIR, error);
-  if (!dirname)
-    return FALSE;
+  if (info1->desktop_id == NULL ||
+      info2->desktop_id == NULL)
+    return info1 == info2;
 
-  filename = g_build_filename (dirname, "mimeapps.list", NULL);
-  g_free (dirname);
+  return strcmp (info1->desktop_id, info2->desktop_id) == 0;
+}
 
-  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) &&
-       !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 ();
-    }
+static const char *
+g_desktop_app_info_get_id (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  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);
-    }
+  return info->desktop_id;
+}
 
-  for (k = 0; content_types && content_types[k]; k++)
-    {
-      /* set as default, if requested so */
-      string = g_key_file_get_string (key_file,
-                                      DEFAULT_APPLICATIONS_GROUP,
-                                      content_types[k],
-                                      NULL);
+static const char *
+g_desktop_app_info_get_name (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-      if (g_strcmp0 (string, desktop_id) != 0 &&
-          (flags & UPDATE_MIME_SET_DEFAULT))
-        {
-          g_free (string);
-          string = g_strdup (desktop_id);
+  if (info->name == NULL)
+    return _("Unnamed");
+  return info->name;
+}
 
-          /* add in the non-default list too, if it's not already there */
-          flags |= UPDATE_MIME_SET_NON_DEFAULT;
-        }
+static const char *
+g_desktop_app_info_get_display_name (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-      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);
+  if (info->fullname == NULL)
+    return g_desktop_app_info_get_name (appinfo);
+  return info->fullname;
+}
 
-      g_free (string);
-    }
+/**
+ * g_desktop_app_info_get_is_hidden:
+ * @info: a #GDesktopAppInfo.
+ *
+ * A desktop file is hidden if the Hidden key in it is
+ * set to True.
+ *
+ * Returns: %TRUE if hidden, %FALSE otherwise.
+ **/
+gboolean
+g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
+{
+  return info->hidden;
+}
 
-  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);
+/**
+ * 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)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+  return info->comment;
+}
+
+static const char *
+g_desktop_app_info_get_executable (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (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)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+  return info->icon;
+}
+
+/**
+ * g_desktop_app_info_get_categories:
+ * @info: a #GDesktopAppInfo
+ *
+ * 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)
+{
+  return info->categories;
+}
+
+/**
+ * g_desktop_app_info_get_keywords:
+ * @info: a #GDesktopAppInfo
+ *
+ * Gets the keywords from the desktop file.
+ *
+ * Returns: (transfer none): The value of the 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)
+{
+  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: (nullable): 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
+ * `OnlyShowIn` and `NotShowIn` keys.
+ *
+ * @desktop_env should typically be given as %NULL, in which case the
+ * `XDG_CURRENT_DESKTOP` environment variable is consulted.  If you want
+ * to override the default mechanism then you may specify @desktop_env,
+ * but this is not recommended.
+ *
+ * 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
+ * `OnlyShowIn` and `NotShowIn` keys, %FALSE
+ * otherwise.
+ *
+ * Since: 2.30
+ */
+gboolean
+g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
+                                const gchar     *desktop_env)
+{
+  const gchar *specified_envs[] = { desktop_env, NULL };
+  const gchar * const *envs;
+  gint i;
+
+  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
+
+  if (desktop_env)
+    envs = specified_envs;
+  else
+    envs = get_current_desktops (NULL);
+
+  for (i = 0; envs[i]; i++)
+    {
+      gint j;
+
+      if (info->only_show_in)
+        for (j = 0; info->only_show_in[j]; j++)
+          if (g_str_equal (info->only_show_in[j], envs[i]))
+            return TRUE;
+
+      if (info->not_show_in)
+        for (j = 0; info->not_show_in[j]; j++)
+          if (g_str_equal (info->not_show_in[j], envs[i]))
+            return FALSE;
+    }
+
+  return info->only_show_in == NULL;
+}
+
+/* Launching... {{{2 */
+
+static char *
+expand_macro_single (char macro, char *uri)
+{
+  GFile *file;
+  char *result = NULL;
+  char *path = NULL;
+  char *name;
+
+  file = g_file_new_for_uri (uri);
+
+  switch (macro)
+    {
+    case 'u':
+    case 'U':
+      result = g_shell_quote (uri);
+      break;
+    case 'f':
+    case 'F':
+      path = g_file_get_path (file);
+      if (path)
+        result = g_shell_quote (path);
+      break;
+    case 'd':
+    case 'D':
+      path = g_file_get_path (file);
+      if (path)
+        {
+          name = g_path_get_dirname (path);
+          result = g_shell_quote (name);
+          g_free (name);
+        }
+      break;
+    case 'n':
+    case 'N':
+      path = g_file_get_path (file);
+      if (path)
+        {
+          name = g_path_get_basename (path);
+          result = g_shell_quote (name);
+          g_free (name);
+        }
+      break;
+    }
+
+  g_object_unref (file);
+  g_free (path);
+
+  return result;
+}
+
+static void
+expand_macro (char              macro,
+              GString          *exec,
+              GDesktopAppInfo  *info,
+              GList           **uri_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 (uris)
+        {
+          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);
+            }
+          uris = uris->next;
+        }
+
+      break;
+
+    case 'U':
+    case 'F':
+    case 'D':
+    case 'N':
+      while (uris)
+        {
+          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);
+            }
+
+          uris = uris->next;
+
+          if (uris != NULL && expanded)
+            g_string_append_c (exec, ' ');
+        }
+
+      break;
+
+    case 'i':
+      if (info->icon_name)
+        {
+          g_string_append (exec, "--icon ");
+          expanded = g_shell_quote (info->icon_name);
+          g_string_append (exec, expanded);
+          g_free (expanded);
+        }
+      break;
+
+    case 'c':
+      if (info->name)
+        {
+          expanded = g_shell_quote (info->name);
+          g_string_append (exec, expanded);
+          g_free (expanded);
+        }
+      break;
+
+    case 'k':
+      if (info->filename)
+        {
+          expanded = g_shell_quote (info->filename);
+          g_string_append (exec, expanded);
+          g_free (expanded);
+        }
+      break;
+
+    case 'm': /* deprecated */
+      break;
+
+    case '%':
+      g_string_append_c (exec, '%');
+      break;
+    }
+
+  *uri_list = uris;
+}
+
+static gboolean
+expand_application_parameters (GDesktopAppInfo   *info,
+                               const gchar       *exec_line,
+                               GList            **uris,
+                               int               *argc,
+                               char            ***argv,
+                               GError           **error)
+{
+  GList *uri_list = *uris;
+  const char *p = exec_line;
+  GString *expanded_exec;
+  gboolean res;
+
+  if (exec_line == NULL)
+    {
+      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, uris);
+          p++;
+        }
+      else
+        g_string_append_c (expanded_exec, *p);
+
+      p++;
+    }
+
+  /* No file substitutions */
+  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, uris);
+    }
+
+  res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
+  g_string_free (expanded_exec, TRUE);
+  return res;
+}
+
+static gboolean
+prepend_terminal_to_vector (int    *argc,
+                            char ***argv)
+{
+#ifndef G_OS_WIN32
+  char **real_argv;
+  int real_argc;
+  int i, j;
+  char **term_argv = NULL;
+  int term_argc = 0;
+  char *check;
+  char **the_argv;
+
+  g_return_val_if_fail (argc != NULL, FALSE);
+  g_return_val_if_fail (argv != NULL, FALSE);
+
+  /* sanity */
+  if(*argv == NULL)
+    *argc = 0;
+
+  the_argv = *argv;
+
+  /* compute size if not given */
+  if (*argc < 0)
+    {
+      for (i = 0; the_argv[i] != NULL; i++)
+        ;
+      *argc = i;
+    }
+
+  term_argc = 2;
+  term_argv = g_new0 (char *, 3);
+
+  check = g_find_program_in_path ("gnome-terminal");
+  if (check != NULL)
+    {
+      term_argv[0] = check;
+      /* Note that gnome-terminal takes -x and
+       * as -e in gnome-terminal is broken we use that. */
+      term_argv[1] = g_strdup ("-x");
+    }
+  else
+    {
+      if (check == NULL)
+        check = g_find_program_in_path ("nxterm");
+      if (check == NULL)
+        check = g_find_program_in_path ("color-xterm");
+      if (check == NULL)
+        check = g_find_program_in_path ("rxvt");
+      if (check == NULL)
+        check = g_find_program_in_path ("xterm");
+      if (check == NULL)
+        check = g_find_program_in_path ("dtterm");
+      if (check == NULL)
+        {
+          check = g_strdup ("xterm");
+          g_warning ("couldn't find a terminal, falling back to xterm");
+        }
+      term_argv[0] = check;
+      term_argv[1] = g_strdup ("-e");
     }
 
-  for (k = 0; content_types && content_types[k]; k++)
+  real_argc = term_argc + *argc;
+  real_argv = g_new (char *, real_argc + 1);
+
+  for (i = 0; i < term_argc; i++)
+    real_argv[i] = term_argv[i];
+
+  for (j = 0; j < *argc; j++, i++)
+    real_argv[i] = (char *)the_argv[j];
+
+  real_argv[i] = NULL;
+
+  g_free (*argv);
+  *argv = real_argv;
+  *argc = real_argc;
+
+  /* we use g_free here as we sucked all the inner strings
+   * out from it into real_argv */
+  g_free (term_argv);
+  return TRUE;
+#else
+  return FALSE;
+#endif /* G_OS_WIN32 */
+}
+
+static GList *
+create_files_for_uris (GList *uris)
+{
+  GList *res;
+  GList *iter;
+
+  res = NULL;
+
+  for (iter = uris; iter; iter = iter->next)
     {
-      /* Add to the right place in the list */
+      GFile *file = g_file_new_for_uri ((char *)iter->data);
+      res = g_list_prepend (res, file);
+    }
 
-      length = 0;
-      old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
-                                             content_types[k], &length, NULL);
+  return g_list_reverse (res);
+}
+
+typedef struct
+{
+  GSpawnChildSetupFunc user_setup;
+  gpointer user_setup_data;
+
+  char *pid_envvar;
+} ChildSetupData;
+
+static void
+child_setup (gpointer user_data)
+{
+  ChildSetupData *data = user_data;
+
+  if (data->pid_envvar)
+    {
+      pid_t pid = getpid ();
+      char buf[20];
+      int i;
+
+      /* Write the pid into the space already reserved for it in the
+       * environment array. We can't use sprintf because it might
+       * malloc, so we do it by hand. It's simplest to write the pid
+       * out backwards first, then copy it over.
+       */
+      for (i = 0; pid; i++, pid /= 10)
+        buf[i] = (pid % 10) + '0';
+      for (i--; i >= 0; i--)
+        *(data->pid_envvar++) = buf[i];
+      *data->pid_envvar = '\0';
+    }
+
+  if (data->user_setup)
+    data->user_setup (data->user_setup_data);
+}
+
+static void
+notify_desktop_launch (GDBusConnection  *session_bus,
+                       GDesktopAppInfo  *info,
+                       long              pid,
+                       const char       *display,
+                       const char       *sn_id,
+                       GList            *uris)
+{
+  GDBusMessage *msg;
+  GVariantBuilder uri_variant;
+  GVariantBuilder extras_variant;
+  GList *iter;
+  const char *desktop_file_id;
+  const char *gio_desktop_file;
+
+  if (session_bus == NULL)
+    return;
+
+  g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
+  for (iter = uris; iter; iter = iter->next)
+    g_variant_builder_add (&uri_variant, "s", iter->data);
+
+  g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
+  if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
+    g_variant_builder_add (&extras_variant, "{sv}",
+                           "startup-id",
+                           g_variant_new ("s",
+                                          sn_id));
+  gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
+  if (gio_desktop_file != NULL)
+    g_variant_builder_add (&extras_variant, "{sv}",
+                           "origin-desktop-file",
+                           g_variant_new_bytestring (gio_desktop_file));
+  if (g_get_prgname () != NULL)
+    g_variant_builder_add (&extras_variant, "{sv}",
+                           "origin-prgname",
+                           g_variant_new_bytestring (g_get_prgname ()));
+  g_variant_builder_add (&extras_variant, "{sv}",
+                         "origin-pid",
+                         g_variant_new ("x",
+                                        (gint64)getpid ()));
+
+  if (info->filename)
+    desktop_file_id = info->filename;
+  else if (info->desktop_id)
+    desktop_file_id = info->desktop_id;
+  else
+    desktop_file_id = "";
+
+  msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
+                                   "org.gtk.gio.DesktopAppInfo",
+                                   "Launched");
+  g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
+                                               g_variant_new_bytestring (desktop_file_id),
+                                               display ? display : "",
+                                               (gint64)pid,
+                                               &uri_variant,
+                                               &extras_variant));
+  g_dbus_connection_send_message (session_bus,
+                                  msg, 0,
+                                  NULL,
+                                  NULL);
+  g_object_unref (msg);
+}
+
+#define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
+
+static gboolean
+g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
+                                           GDBusConnection            *session_bus,
+                                           const gchar                *exec_line,
+                                           GList                      *uris,
+                                           GAppLaunchContext          *launch_context,
+                                           GSpawnFlags                 spawn_flags,
+                                           GSpawnChildSetupFunc        user_setup,
+                                           gpointer                    user_setup_data,
+                                           GDesktopAppLaunchCallback   pid_callback,
+                                           gpointer                    pid_callback_data,
+                                           GError                    **error)
+{
+  gboolean completed = FALSE;
+  GList *old_uris;
+  char **argv, **envp;
+  int argc;
+  ChildSetupData data;
+
+  g_return_val_if_fail (info != NULL, FALSE);
+
+  argv = NULL;
+
+  if (launch_context)
+    envp = g_app_launch_context_get_environment (launch_context);
+  else
+    envp = g_get_environ ();
+
+  do
+    {
+      GPid pid;
+      GList *launched_uris;
+      GList *iter;
+      char *display, *sn_id = NULL;
+
+      old_uris = uris;
+      if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
+        goto out;
+
+      /* Get the subset of URIs we're launching with this process */
+      launched_uris = NULL;
+      for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
+        launched_uris = g_list_prepend (launched_uris, iter->data);
+      launched_uris = g_list_reverse (launched_uris);
+
+      if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
+        {
+          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               _("Unable to find terminal required for application"));
+          goto out;
+        }
+
+      data.user_setup = user_setup;
+      data.user_setup_data = user_setup_data;
+
+      if (info->filename)
+        {
+          envp = g_environ_setenv (envp,
+                                   "GIO_LAUNCHED_DESKTOP_FILE",
+                                   info->filename,
+                                   TRUE);
+          envp = g_environ_setenv (envp,
+                                   "GIO_LAUNCHED_DESKTOP_FILE_PID",
+                                   "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
+                                   TRUE);
+          data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
+        }
+      else
+        {
+          data.pid_envvar = NULL;
+        }
+
+      display = NULL;
+      sn_id = NULL;
+      if (launch_context)
+        {
+          GList *launched_files = create_files_for_uris (launched_uris);
+
+          display = g_app_launch_context_get_display (launch_context,
+                                                      G_APP_INFO (info),
+                                                      launched_files);
+          if (display)
+            envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
 
-      list = g_new (char *, 1 + length + 1);
+          if (info->startup_notify)
+            {
+              sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
+                                                                  G_APP_INFO (info),
+                                                                  launched_files);
+              if (sn_id)
+                envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
+            }
 
-      i = 0;
+          g_list_free_full (launched_files, g_object_unref);
+        }
 
-      /* if we're adding a last-used hint, just put the application in front of the list */
-      if (flags & UPDATE_MIME_SET_LAST_USED)
+      if (!g_spawn_async (info->path,
+                          argv,
+                          envp,
+                          spawn_flags,
+                          child_setup,
+                          &data,
+                          &pid,
+                          error))
         {
-          /* avoid adding this again as non-default later */
-          if (flags & UPDATE_MIME_SET_NON_DEFAULT)
-            flags ^= UPDATE_MIME_SET_NON_DEFAULT;
+          if (sn_id)
+            g_app_launch_context_launch_failed (launch_context, sn_id);
 
-          list[i++] = g_strdup (desktop_id);
+          g_free (display);
+          g_free (sn_id);
+          g_list_free (launched_uris);
+
+          goto out;
         }
 
-      if (old_list)
+      if (pid_callback != NULL)
+        pid_callback (info, pid, pid_callback_data);
+
+      if (launch_context != NULL)
         {
-          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]);
-                }
-            }
+          GVariantBuilder builder;
+          GVariant *platform_data;
+
+          g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
+          g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
+          if (sn_id)
+            g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
+          platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
+          g_signal_emit_by_name (launch_context, "launched", info, platform_data);
+          g_variant_unref (platform_data);
         }
 
-      /* add it at the end of the list */
-      if (flags & UPDATE_MIME_SET_NON_DEFAULT)
-        list[i++] = g_strdup (desktop_id);
+      notify_desktop_launch (session_bus,
+                             info,
+                             pid,
+                             display,
+                             sn_id,
+                             launched_uris);
 
-      list[i] = NULL;
+      g_free (display);
+      g_free (sn_id);
+      g_list_free (launched_uris);
 
-      g_strfreev (old_list);
+      g_strfreev (argv);
+      argv = NULL;
+    }
+  while (uris != NULL);
 
-      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);
+  completed = TRUE;
 
-      g_strfreev (list);
-    }
+ out:
+  g_strfreev (argv);
+  g_strfreev (envp);
 
-  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);
-    }
+  return completed;
+}
 
-  for (k = 0; content_types && content_types[k]; k++)
-    {
-      /* Remove from removed associations group (unless remove) */
+static gchar *
+object_path_from_appid (const gchar *app_id)
+{
+  gchar *path;
+  gint i, n;
 
-      length = 0;
-      old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
-                                             content_types[k], &length, NULL);
+  n = strlen (app_id);
+  path = g_malloc (n + 2);
 
-      list = g_new (char *, 1 + length + 1);
+  path[0] = '/';
 
-      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;
+  for (i = 0; i < n; i++)
+    if (app_id[i] != '.')
+      path[i + 1] = app_id[i];
+    else
+      path[i + 1] = '/';
 
-      g_strfreev (old_list);
+  path[i + 1] = '\0';
 
-      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);
+  return path;
+}
 
-      g_strfreev (list);
-    }
+static GVariant *
+g_desktop_app_info_make_platform_data (GDesktopAppInfo   *info,
+                                       GList             *uris,
+                                       GAppLaunchContext *launch_context)
+{
+  GVariantBuilder builder;
 
-  g_strfreev (content_types);
+  g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
 
-  data = g_key_file_to_data (key_file, &data_size, error);
-  g_key_file_free (key_file);
+  if (launch_context)
+    {
+      GList *launched_files = create_files_for_uris (uris);
 
-  res = g_file_set_contents (filename, data, data_size, error);
+      if (info->startup_notify)
+        {
+          gchar *sn_id;
 
-  mime_info_cache_reload (NULL);
+          sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
+          if (sn_id)
+            g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
+        }
 
-  g_free (filename);
-  g_free (data);
+      g_list_free_full (launched_files, g_object_unref);
+    }
 
-  return res;
+  return g_variant_builder_end (&builder);
 }
 
 static gboolean
-g_desktop_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
-                                              const char  *content_type,
-                                              GError     **error)
+g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo    *info,
+                                          GDBusConnection    *session_bus,
+                                          GList              *uris,
+                                          GAppLaunchContext  *launch_context)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  GVariantBuilder builder;
+  gchar *object_path;
 
-  if (!g_desktop_app_info_ensure_saved (info, error))
-    return FALSE;
+  g_return_val_if_fail (info != NULL, FALSE);
 
-  if (!info->desktop_id)
+  g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
+
+  if (uris)
     {
-      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                           _("Application information lacks an identifier"));
-      return FALSE;
+      GList *iter;
+
+      g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
+      for (iter = uris; iter; iter = iter->next)
+        g_variant_builder_add (&builder, "s", iter->data);
+      g_variant_builder_close (&builder);
     }
 
-  /* 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);
+  g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
+
+  /* This is non-blocking API.  Similar to launching via fork()/exec()
+   * we don't wait around to see if the program crashed during startup.
+   * This is what startup-notification's job is...
+   */
+  object_path = object_path_from_appid (info->app_id);
+  g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
+                          uris ? "Open" : "Activate", g_variant_builder_end (&builder),
+                          NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
+  g_free (object_path);
+
+  return TRUE;
 }
 
 static gboolean
-g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
-                                            const char  *content_type,
-                                            GError     **error)
+g_desktop_app_info_launch_uris_internal (GAppInfo                   *appinfo,
+                                         GList                      *uris,
+                                         GAppLaunchContext          *launch_context,
+                                         GSpawnFlags                 spawn_flags,
+                                         GSpawnChildSetupFunc        user_setup,
+                                         gpointer                    user_setup_data,
+                                         GDesktopAppLaunchCallback   pid_callback,
+                                         gpointer                    pid_callback_data,
+                                         GError                     **error)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  GDBusConnection *session_bus;
+  gboolean success = TRUE;
 
-  if (!g_desktop_app_info_ensure_saved (info, error))
-    return FALSE;
+  session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
 
-  if (!info->desktop_id)
+  if (session_bus && info->app_id)
+    g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
+  else
+    success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
+                                                         spawn_flags, user_setup, user_setup_data,
+                                                         pid_callback, pid_callback_data, error);
+
+  if (session_bus != NULL)
     {
-      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                           _("Application information lacks an identifier"));
-      return FALSE;
+      /* This asynchronous flush holds a reference until it completes,
+       * which ensures that the following unref won't immediately kill
+       * the connection if we were the initial owner.
+       */
+      g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
+      g_object_unref (session_bus);
     }
 
-  return update_mimeapps_list (info->desktop_id, content_type,
-                               UPDATE_MIME_SET_DEFAULT,
-                               error);
+  return success;
 }
 
-static void
-update_program_done (GPid     pid,
-                     gint     status,
-                     gpointer data)
+static gboolean
+g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
+                                GList              *uris,
+                                GAppLaunchContext  *launch_context,
+                                GError            **error)
 {
-  /* Did the application exit correctly */
-  if (g_spawn_check_exit_status (status, NULL))
-    {
-      /* Here we could clean out any caches in use */
-    }
+  return g_desktop_app_info_launch_uris_internal (appinfo, uris,
+                                                  launch_context,
+                                                  _SPAWN_FLAGS_DEFAULT,
+                                                  NULL, NULL, NULL, NULL,
+                                                  error);
 }
 
-static void
-run_update_command (char *command,
-                    char *subdir)
+static gboolean
+g_desktop_app_info_supports_uris (GAppInfo *appinfo)
 {
-        char *argv[3] = {
-                NULL,
-                NULL,
-                NULL,
-        };
-        GPid pid = 0;
-        GError *error = NULL;
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-        argv[0] = command;
-        argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
+  return info->exec &&
+    ((strstr (info->exec, "%u") != NULL) ||
+     (strstr (info->exec, "%U") != NULL));
+}
 
-        if (g_spawn_async ("/", argv,
-                           NULL,       /* envp */
-                           G_SPAWN_SEARCH_PATH |
-                           G_SPAWN_STDOUT_TO_DEV_NULL |
-                           G_SPAWN_STDERR_TO_DEV_NULL |
-                           G_SPAWN_DO_NOT_REAP_CHILD,
-                           NULL, NULL, /* No setup function */
-                           &pid,
-                           &error))
-          g_child_watch_add (pid, update_program_done, NULL);
-        else
-          {
-            /* If we get an error at this point, it's quite likely the user doesn't
-             * have an installed copy of either 'update-mime-database' or
-             * 'update-desktop-database'.  I don't think we want to popup an error
-             * dialog at this point, so we just do a g_warning to give the user a
-             * chance of debugging it.
-             */
-            g_warning ("%s", error->message);
-          }
+static gboolean
+g_desktop_app_info_supports_files (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-        g_free (argv[1]);
+  return info->exec &&
+    ((strstr (info->exec, "%f") != NULL) ||
+     (strstr (info->exec, "%F") != NULL));
 }
 
 static gboolean
-g_desktop_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
-                                                 const char  *extension,
-                                                 GError     **error)
+g_desktop_app_info_launch (GAppInfo           *appinfo,
+                           GList              *files,
+                           GAppLaunchContext  *launch_context,
+                           GError            **error)
 {
-  char *filename, *basename, *mimetype;
-  char *dirname;
+  GList *uris;
+  char *uri;
   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;
-
-  basename = g_strdup_printf ("user-extension-%s.xml", extension);
-  filename = g_build_filename (dirname, basename, NULL);
-  g_free (basename);
-  g_free (dirname);
-
-  mimetype = g_strdup_printf ("application/x-extension-%s", extension);
-
-  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+  uris = NULL;
+  while (files)
     {
-      char *contents;
-
-      contents =
-        g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-                         "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
-                         " <mime-type type=\"%s\">\n"
-                         "  <comment>%s document</comment>\n"
-                         "  <glob pattern=\"*.%s\"/>\n"
-                         " </mime-type>\n"
-                         "</mime-info>\n", mimetype, extension, extension);
-
-      g_file_set_contents (filename, contents, -1, NULL);
-      g_free (contents);
-
-      run_update_command ("update-mime-database", "mime");
+      uri = g_file_get_uri (files->data);
+      uris = g_list_prepend (uris, uri);
+      files = files->next;
     }
-  g_free (filename);
 
-  res = g_desktop_app_info_set_as_default_for_type (appinfo,
-                                                    mimetype,
-                                                    error);
+  uris = g_list_reverse (uris);
 
-  g_free (mimetype);
+  res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
+
+  g_list_free_full (uris, g_free);
 
   return res;
 }
 
-static gboolean
-g_desktop_app_info_add_supports_type (GAppInfo    *appinfo,
-                                      const char  *content_type,
-                                      GError     **error)
+/**
+ * g_desktop_app_info_launch_uris_as_manager:
+ * @appinfo: a #GDesktopAppInfo
+ * @uris: (element-type utf8): List of URIs
+ * @launch_context: (allow-none): a #GAppLaunchContext
+ * @spawn_flags: #GSpawnFlags, used for each process
+ * @user_setup: (scope call) (allow-none): a #GSpawnChildSetupFunc, used once
+ *     for each process.
+ * @user_setup_data: (closure user_setup) (allow-none): User data for @user_setup
+ * @pid_callback: (scope call) (allow-none): Callback for child processes
+ * @pid_callback_data: (closure pid_callback) (allow-none): User data for @callback
+ * @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
+ * launch applications.  Ordinary applications should use
+ * g_app_info_launch_uris().
+ *
+ * If the application is launched via traditional UNIX fork()/exec()
+ * then @spawn_flags, @user_setup and @user_setup_data are used for the
+ * call to g_spawn_async().  Additionally, @pid_callback (with
+ * @pid_callback_data) will be called to inform about the PID of the
+ * created process.
+ *
+ * If application launching occurs via some other mechanism (eg: D-Bus
+ * activation) then @spawn_flags, @user_setup, @user_setup_data,
+ * @pid_callback and @pid_callback_data are ignored.
+ *
+ * Returns: %TRUE on successful launch, %FALSE otherwise.
+ */
+gboolean
+g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo            *appinfo,
+                                           GList                      *uris,
+                                           GAppLaunchContext          *launch_context,
+                                           GSpawnFlags                 spawn_flags,
+                                           GSpawnChildSetupFunc        user_setup,
+                                           gpointer                    user_setup_data,
+                                           GDesktopAppLaunchCallback   pid_callback,
+                                           gpointer                    pid_callback_data,
+                                           GError                    **error)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
-
-  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
-    return FALSE;
-
-  return update_mimeapps_list (info->desktop_id, content_type,
-                               UPDATE_MIME_SET_NON_DEFAULT,
-                               error);
+  return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
+                                                  uris,
+                                                  launch_context,
+                                                  spawn_flags,
+                                                  user_setup,
+                                                  user_setup_data,
+                                                  pid_callback,
+                                                  pid_callback_data,
+                                                  error);
 }
 
-static gboolean
-g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
+/* OnlyShowIn API support {{{2 */
+
+/**
+ * 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() and
+ * g_desktop_app_info_get_show_in() to evaluate the
+ * `OnlyShowIn` and `NotShowIn`
+ * desktop entry fields.
+ *
+ * Should be called only once; subsequent calls are ignored.
+ *
+ * Deprecated:2.42:do not use this API.  Since 2.42 the value of the
+ * `XDG_CURRENT_DESKTOP` environment variable will be used.
+ */
+void
+g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
 {
-  return TRUE;
+  get_current_desktops (desktop_env);
 }
 
 static gboolean
-g_desktop_app_info_remove_supports_type (GAppInfo    *appinfo,
-                                         const char  *content_type,
-                                         GError     **error)
+g_desktop_app_info_should_show (GAppInfo *appinfo)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
+  if (info->nodisplay)
     return FALSE;
 
-  return update_mimeapps_list (info->desktop_id, content_type,
-                               UPDATE_MIME_REMOVE,
-                               error);
+  return g_desktop_app_info_get_show_in (info, NULL);
 }
 
-static const char **
-g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
+/* mime types/default apps support {{{2 */
+
+typedef enum {
+  CONF_DIR,
+  APP_DIR,
+  MIMETYPE_DIR
+} DirType;
+
+static char *
+ensure_dir (DirType   type,
+            GError  **error)
 {
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  char *path, *display_name;
+  int errsv;
+
+  switch (type)
+    {
+    case CONF_DIR:
+      path = g_build_filename (g_get_user_config_dir (), NULL);
+      break;
+
+    case APP_DIR:
+      path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
+      break;
+
+    case MIMETYPE_DIR:
+      path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+
+  errno = 0;
+  if (g_mkdir_with_parents (path, 0700) == 0)
+    return path;
+
+  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 (errsv),
+                 _("Can't create user application configuration folder %s: %s"),
+                 display_name, g_strerror (errsv));
+  else
+    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 (errsv));
 
-  return (const char**) info->mime_types;
-}
+  g_free (display_name);
+  g_free (path);
 
-/* Saving and deleting {{{2 */
+  return NULL;
+}
 
 static gboolean
-g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
-                                 GError          **error)
+update_mimeapps_list (const char  *desktop_id,
+                      const char  *content_type,
+                      UpdateMimeFlags flags,
+                      GError     **error)
 {
+  char *dirname, *filename, *string;
   GKeyFile *key_file;
-  char *dirname;
-  char *filename;
-  char *data, *desktop_id;
-  gsize data_size;
-  int fd;
-  gboolean res;
-
-  if (info->filename != NULL)
-    return TRUE;
+  gboolean load_succeeded, res;
+  char **old_list, **list;
+  gsize length, data_size;
+  char *data;
+  int i, j, k;
+  char **content_types;
 
-  /* This is only used for object created with
-   * g_app_info_create_from_commandline. All other
-   * object should have a filename
-   */
+  /* 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);
+  dirname = ensure_dir (CONF_DIR, error);
   if (!dirname)
     return FALSE;
 
+  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, 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 ();
+    }
 
-  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                         "Encoding", "UTF-8");
-  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                         G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
-  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 (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);
+  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);
+    }
 
-  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                         G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
+  for (k = 0; content_types && content_types[k]; k++)
+    {
+      /* set as default, if requested so */
+      string = g_key_file_get_string (key_file,
+                                      DEFAULT_APPLICATIONS_GROUP,
+                                      content_types[k],
+                                      NULL);
 
-  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                         G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
+      if (g_strcmp0 (string, desktop_id) != 0 &&
+          (flags & UPDATE_MIME_SET_DEFAULT))
+        {
+          g_free (string);
+          string = g_strdup (desktop_id);
 
-  if (info->generic_name != NULL)
-    g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                           GENERIC_NAME_KEY, info->generic_name);
+          /* add in the non-default list too, if it's not already there */
+          flags |= UPDATE_MIME_SET_NON_DEFAULT;
+        }
 
-  if (info->fullname != NULL)
-    g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                           FULL_NAME_KEY, info->fullname);
+      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);
 
-  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                         G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
+      g_free (string);
+    }
 
-  g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
-                          G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
+  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, NULL);
-  g_key_file_free (key_file);
+  for (k = 0; content_types && content_types[k]; k++)
+    {
+      /* Add to the right place in the list */
 
-  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);
+      length = 0;
+      old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
+                                             content_types[k], &length, NULL);
 
-  fd = g_mkstemp (filename);
-  if (fd == -1)
-    {
-      char *display_name;
+      list = g_new (char *, 1 + length + 1);
 
-      display_name = g_filename_display_name (filename);
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   _("Can't create user desktop file %s"), display_name);
-      g_free (display_name);
-      g_free (filename);
-      g_free (data);
-      return FALSE;
-    }
+      i = 0;
 
-  desktop_id = g_path_get_basename (filename);
+      /* 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;
 
-  /* FIXME - actually handle error */
-  (void) g_close (fd, NULL);
+          list[i++] = g_strdup (desktop_id);
+        }
 
-  res = g_file_set_contents (filename, data, data_size, error);
-  g_free (data);
-  if (!res)
-    {
-      g_free (desktop_id);
-      g_free (filename);
-      return FALSE;
-    }
+      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]);
+                }
+            }
+        }
 
-  info->filename = filename;
-  info->desktop_id = desktop_id;
+      /* add it at the end of the list */
+      if (flags & UPDATE_MIME_SET_NON_DEFAULT)
+        list[i++] = g_strdup (desktop_id);
 
-  run_update_command ("update-desktop-database", "applications");
+      list[i] = NULL;
 
-  return TRUE;
-}
+      g_strfreev (old_list);
 
-static gboolean
-g_desktop_app_info_can_delete (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+      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 (info->filename)
-    {
-      if (strstr (info->filename, "/userapp-"))
-        return g_access (info->filename, W_OK) == 0;
+      g_strfreev (list);
     }
 
-  return FALSE;
-}
-
-static gboolean
-g_desktop_app_info_delete (GAppInfo *appinfo)
-{
-  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+  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);
+    }
 
-  if (info->filename)
+  for (k = 0; content_types && content_types[k]; k++)
     {
-      if (g_remove (info->filename) == 0)
-        {
-          update_mimeapps_list (info->desktop_id, NULL,
-                                UPDATE_MIME_NONE,
-                                NULL);
+      /* Remove from removed associations group (unless remove) */
 
-          g_free (info->filename);
-          info->filename = NULL;
-          g_free (info->desktop_id);
-          info->desktop_id = NULL;
+      length = 0;
+      old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
+                                             content_types[k], &length, NULL);
 
-          return TRUE;
+      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]);
+            }
         }
-    }
-
-  return FALSE;
-}
+      list[i] = NULL;
 
-/* Create for commandline {{{2 */
-/**
- * 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 occurring, %NULL to ignore.
- *
- * Creates a new #GAppInfo from the given information.
- *
- * Note that for @commandline, the quoting rules of the Exec key of the
- * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">freedesktop.org Desktop
- * Entry Specification</ulink> are applied. For example, if the @commandline contains
- * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
- * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
- *
- * 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_strfreev (old_list);
 
-  g_return_val_if_fail (commandline, NULL);
+      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);
 
-  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
+      g_strfreev (list);
+    }
 
-  info->filename = NULL;
-  info->desktop_id = NULL;
+  g_strfreev (content_types);
 
-  info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
-  info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
-  info->hidden = FALSE;
-  if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
-    info->exec = g_strconcat (commandline, " %u", NULL);
-  else
-    info->exec = g_strconcat (commandline, " %f", NULL);
-  info->nodisplay = TRUE;
-  info->binary = binary_from_exec (info->exec);
+  data = g_key_file_to_data (key_file, &data_size, error);
+  g_key_file_free (key_file);
 
-  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);
+  res = g_file_set_contents (filename, data, data_size, error);
 
-  return G_APP_INFO (info);
-}
+  desktop_file_dirs_invalidate_user_config ();
 
-/* GAppInfo interface init */
+  g_free (filename);
+  g_free (data);
 
-static void
-g_desktop_app_info_iface_init (GAppInfoIface *iface)
-{
-  iface->dup = g_desktop_app_info_dup;
-  iface->equal = g_desktop_app_info_equal;
-  iface->get_id = g_desktop_app_info_get_id;
-  iface->get_name = g_desktop_app_info_get_name;
-  iface->get_description = g_desktop_app_info_get_description;
-  iface->get_executable = g_desktop_app_info_get_executable;
-  iface->get_icon = g_desktop_app_info_get_icon;
-  iface->launch = g_desktop_app_info_launch;
-  iface->supports_uris = g_desktop_app_info_supports_uris;
-  iface->supports_files = g_desktop_app_info_supports_files;
-  iface->launch_uris = g_desktop_app_info_launch_uris;
-  iface->should_show = g_desktop_app_info_should_show;
-  iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
-  iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
-  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;
-  iface->get_supported_types = g_desktop_app_info_get_supported_types;
+  return res;
 }
 
-/* Recommended applications {{{2 */
-
 static gboolean
-app_info_in_list (GAppInfo *info,
-                  GList    *list)
+g_desktop_app_info_set_as_last_used_for_type (GAppInfo    *appinfo,
+                                              const char  *content_type,
+                                              GError     **error)
 {
-  while (list != NULL)
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
+
+  if (!g_desktop_app_info_ensure_saved (info, error))
+    return FALSE;
+
+  if (!info->desktop_id)
     {
-      if (g_app_info_equal (info, list->data))
-        return TRUE;
-      list = list->next;
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           _("Application information lacks an identifier"));
+      return FALSE;
     }
-  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);
 }
 
-/**
- * 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)
+static gboolean
+g_desktop_app_info_set_as_default_for_type (GAppInfo    *appinfo,
+                                            const char  *content_type,
+                                            GError     **error)
 {
-  GList *desktop_entries, *l;
-  GList *infos;
-  GDesktopAppInfo *info;
-
-  g_return_val_if_fail (content_type != NULL, NULL);
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
+  if (!g_desktop_app_info_ensure_saved (info, error))
+    return FALSE;
 
-  infos = NULL;
-  for (l = desktop_entries; l != NULL; l = l->next)
+  if (!info->desktop_id)
     {
-      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_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           _("Application information lacks an identifier"));
+      return FALSE;
     }
 
-  g_list_free (desktop_entries);
-
-  return g_list_reverse (infos);
+  return update_mimeapps_list (info->desktop_id, content_type,
+                               UPDATE_MIME_SET_DEFAULT,
+                               error);
 }
 
-/**
- * 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)
+static void
+update_program_done (GPid     pid,
+                     gint     status,
+                     gpointer data)
 {
-  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)
+  /* Did the application exit correctly */
+  if (g_spawn_check_exit_status (status, NULL))
     {
-      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);
+      /* Here we could clean out any caches in use */
     }
+}
 
-  g_list_free (desktop_entries);
-  g_list_free_full (recommended_infos, g_object_unref);
+static void
+run_update_command (char *command,
+                    char *subdir)
+{
+        char *argv[3] = {
+                NULL,
+                NULL,
+                NULL,
+        };
+        GPid pid = 0;
+        GError *error = NULL;
 
-  return g_list_reverse (infos);
-}
+        argv[0] = command;
+        argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
 
-/**
- * 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,
- * 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.
- **/
-GList *
-g_app_info_get_all_for_type (const char *content_type)
+        if (g_spawn_async ("/", argv,
+                           NULL,       /* envp */
+                           G_SPAWN_SEARCH_PATH |
+                           G_SPAWN_STDOUT_TO_DEV_NULL |
+                           G_SPAWN_STDERR_TO_DEV_NULL |
+                           G_SPAWN_DO_NOT_REAP_CHILD,
+                           NULL, NULL, /* No setup function */
+                           &pid,
+                           &error))
+          g_child_watch_add (pid, update_program_done, NULL);
+        else
+          {
+            /* If we get an error at this point, it's quite likely the user doesn't
+             * have an installed copy of either 'update-mime-database' or
+             * 'update-desktop-database'.  I don't think we want to popup an error
+             * dialog at this point, so we just do a g_warning to give the user a
+             * chance of debugging it.
+             */
+            g_warning ("%s", error->message);
+          }
+
+        g_free (argv[1]);
+}
+
+static gboolean
+g_desktop_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
+                                                 const char  *extension,
+                                                 GError     **error)
 {
-  GList *desktop_entries, *l;
-  GList *infos;
-  char *user_default = NULL;
-  GDesktopAppInfo *info;
+  char *filename, *basename, *mimetype;
+  char *dirname;
+  gboolean res;
 
-  g_return_val_if_fail (content_type != NULL, NULL);
+  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
+    return FALSE;
 
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
-  infos = NULL;
+  dirname = ensure_dir (MIMETYPE_DIR, error);
+  if (!dirname)
+    return FALSE;
+
+  basename = g_strdup_printf ("user-extension-%s.xml", extension);
+  filename = g_build_filename (dirname, basename, NULL);
+  g_free (basename);
+  g_free (dirname);
+
+  mimetype = g_strdup_printf ("application/x-extension-%s", extension);
 
-  /* put the user default in front of the list, for compatibility */
-  if (user_default != NULL)
+  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
     {
-      info = g_desktop_app_info_new (user_default);
+      char *contents;
 
-      if (info != NULL)
-        infos = g_list_prepend (infos, info);
+      contents =
+        g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+                         "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
+                         " <mime-type type=\"%s\">\n"
+                         "  <comment>%s document</comment>\n"
+                         "  <glob pattern=\"*.%s\"/>\n"
+                         " </mime-type>\n"
+                         "</mime-info>\n", mimetype, extension, extension);
+
+      g_file_set_contents (filename, contents, -1, NULL);
+      g_free (contents);
+
+      run_update_command ("update-mime-database", "mime");
     }
+  g_free (filename);
 
-  g_free (user_default);
+  res = g_desktop_app_info_set_as_default_for_type (appinfo,
+                                                    mimetype,
+                                                    error);
 
-  for (l = desktop_entries; l != NULL; l = l->next)
-    {
-      char *desktop_entry = l->data;
+  g_free (mimetype);
 
-      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);
-    }
+  return res;
+}
 
-  g_list_free (desktop_entries);
+static gboolean
+g_desktop_app_info_add_supports_type (GAppInfo    *appinfo,
+                                      const char  *content_type,
+                                      GError     **error)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  return g_list_reverse (infos);
+  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
+    return FALSE;
+
+  return update_mimeapps_list (info->desktop_id, content_type,
+                               UPDATE_MIME_SET_NON_DEFAULT,
+                               error);
 }
 
-/**
- * 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)
+static gboolean
+g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
 {
-  update_mimeapps_list (NULL, content_type,
-                        UPDATE_MIME_NONE,
-                        NULL);
+  return TRUE;
 }
 
-/**
- * g_app_info_get_default_for_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 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)
+static gboolean
+g_desktop_app_info_remove_supports_type (GAppInfo    *appinfo,
+                                         const char  *content_type,
+                                         GError     **error)
 {
-  GList *desktop_entries, *l;
-  char *user_default = NULL;
-  GAppInfo *info;
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  g_return_val_if_fail (content_type != NULL, NULL);
+  if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
+    return FALSE;
 
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
+  return update_mimeapps_list (info->desktop_id, content_type,
+                               UPDATE_MIME_REMOVE,
+                               error);
+}
 
-  info = NULL;
+static const char **
+g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
+{
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  if (user_default != NULL)
-    {
-      info = (GAppInfo *) g_desktop_app_info_new (user_default);
+  return (const char**) info->mime_types;
+}
 
-      if (info)
-        {
-          if (must_support_uris && !g_app_info_supports_uris (info))
-            {
-              g_object_unref (info);
-              info = NULL;
-            }
-        }
-    }
+/* Saving and deleting {{{2 */
 
-  g_free (user_default);
+static gboolean
+g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
+                                 GError          **error)
+{
+  GKeyFile *key_file;
+  char *dirname;
+  char *filename;
+  char *data, *desktop_id;
+  gsize data_size;
+  int fd;
+  gboolean res;
 
-  if (info != NULL)
-    {
-      g_list_free_full (desktop_entries, g_free);
-      return info;
-    }
+  if (info->filename != NULL)
+    return TRUE;
 
-  /* pick the first from the other list that matches our URI
-   * requirements.
+  /* This is only used for object created with
+   * g_app_info_create_from_commandline. All other
+   * object should have a filename
    */
-  for (l = desktop_entries; l != NULL; l = l->next)
-    {
-      char *desktop_entry = l->data;
 
-      info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
-      if (info)
-        {
-          if (must_support_uris && !g_app_info_supports_uris (info))
-            {
-              g_object_unref (info);
-              info = NULL;
-            }
-          else
-            break;
-        }
-    }
+  dirname = ensure_dir (APP_DIR, error);
+  if (!dirname)
+    return FALSE;
 
-  g_list_free_full (desktop_entries, g_free);
+  key_file = g_key_file_new ();
 
-  return info;
-}
+  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                         "Encoding", "UTF-8");
+  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                         G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
+  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 (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_app_info_get_default_for_uri_scheme:
- * @uri_scheme: a string containing a URI scheme.
- *
- * 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)
-{
-  GAppInfo *app_info;
-  char *content_type, *scheme_down;
+  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                         G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
 
-  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);
+  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                         G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
 
-  return app_info;
-}
+  if (info->generic_name != NULL)
+    g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                           GENERIC_NAME_KEY, info->generic_name);
 
-static void
-get_apps_from_dir (GHashTable *apps, 
-                   const char *dirname, 
-                   const char *prefix)
-{
-  GDir *dir;
-  const char *basename;
-  char *filename, *subprefix, *desktop_id;
-  gboolean hidden;
-  GDesktopAppInfo *appinfo;
-  
-  dir = g_dir_open (dirname, 0, NULL);
-  if (dir)
-    {
-      while ((basename = g_dir_read_name (dir)) != NULL)
-       {
-         filename = g_build_filename (dirname, basename, NULL);
-         if (g_str_has_suffix (basename, ".desktop"))
-           {
-             desktop_id = g_strconcat (prefix, basename, NULL);
-
-             /* Use _extended so we catch NULLs too (hidden) */
-             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))
-                   {
-                     g_object_unref (appinfo);
-                     appinfo = NULL;
-                     hidden = TRUE;
-                   }
-                                     
-                 if (appinfo || hidden)
-                   {
-                     g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
-
-                     if (appinfo)
-                       {
-                         /* Reuse instead of strdup here */
-                         appinfo->desktop_id = desktop_id;
-                         desktop_id = NULL;
-                       }
-                   }
-               }
-             g_free (desktop_id);
-           }
-         else
-           {
-             if (g_file_test (filename, G_FILE_TEST_IS_DIR))
-               {
-                 subprefix = g_strconcat (prefix, basename, "-", NULL);
-                 get_apps_from_dir (apps, filename, subprefix);
-                 g_free (subprefix);
-               }
-           }
-         g_free (filename);
-       }
-      g_dir_close (dir);
-    }
-}
+  if (info->fullname != NULL)
+    g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                           FULL_NAME_KEY, info->fullname);
 
-/* "Get all" API {{{2 */
+  g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                         G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
 
-/**
- * g_app_info_get_all:
- *
- * Gets a list of all of the applications currently registered
- * on this system.
- *
- * For desktop files, this includes applications that have
- * <literal>NoDisplay=true</literal> set or are excluded from
- * display by means of <literal>OnlyShowIn</literal> or
- * <literal>NotShowIn</literal>. See g_app_info_should_show().
- * The returned list does not include applications which have
- * the <literal>Hidden</literal> key set.
- *
- * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
- **/
-GList *
-g_app_info_get_all (void)
-{
-  GHashTable *apps;
-  GHashTableIter iter;
-  gpointer value;
-  int i;
-  GList *infos;
+  g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
+                          G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
+
+  data = g_key_file_to_data (key_file, &data_size, NULL);
+  g_key_file_free (key_file);
 
-  desktop_file_dirs_refresh ();
+  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);
 
-  apps = g_hash_table_new_full (g_str_hash, g_str_equal,
-                               g_free, NULL);
+  fd = g_mkstemp (filename);
+  if (fd == -1)
+    {
+      char *display_name;
 
+      display_name = g_filename_display_name (filename);
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   _("Can't create user desktop file %s"), display_name);
+      g_free (display_name);
+      g_free (filename);
+      g_free (data);
+      return FALSE;
+    }
 
-  for (i = 0; i < n_desktop_file_dirs; i++)
-    get_apps_from_dir (apps, desktop_file_dirs[i].path, "");
+  desktop_id = g_path_get_basename (filename);
 
+  /* FIXME - actually handle error */
+  (void) g_close (fd, NULL);
 
-  infos = NULL;
-  g_hash_table_iter_init (&iter, apps);
-  while (g_hash_table_iter_next (&iter, NULL, &value))
+  res = g_file_set_contents (filename, data, data_size, error);
+  g_free (data);
+  if (!res)
     {
-      if (value)
-        infos = g_list_prepend (infos, value);
+      g_free (desktop_id);
+      g_free (filename);
+      return FALSE;
     }
 
-  g_hash_table_destroy (apps);
-
-  return g_list_reverse (infos);
-}
-
-/* Caching of mimeinfo.cache and defaults.list files {{{2 */
+  info->filename = filename;
+  info->desktop_id = desktop_id;
 
-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 {
-  GList *dirs;                       /* mimeinfo.cache and defaults.list */
-  time_t last_stat_time;
-} MimeInfoCache;
-
-static MimeInfoCache *mime_info_cache = NULL;
-G_LOCK_DEFINE_STATIC (mime_info_cache);
-
-static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
-                                                     const char        *mime_type,
-                                                     char             **new_desktop_file_ids);
-
-static MimeInfoCache * mime_info_cache_new (void);
+  run_update_command ("update-desktop-database", "applications");
 
-static void
-destroy_info_cache_value (gpointer  key,
-                          GList    *value,
-                          gpointer  data)
-{
-  g_list_free_full (value, g_free);
-}
+  /* We just dropped a file in the user's desktop file directory.  Save
+   * the monitor the bother of having to notice it and invalidate
+   * immediately.
+   *
+   * This means that calls directly following this will be able to see
+   * the results immediately.
+   */
+  desktop_file_dirs_invalidate_user_data ();
 
-static void
-destroy_info_cache_map (GHashTable *info_cache_map)
-{
-  g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
-  g_hash_table_destroy (info_cache_map);
+  return TRUE;
 }
 
 static gboolean
-mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
-                                 const char       *cache_file,
-                                 time_t           *timestamp)
+g_desktop_app_info_can_delete (GAppInfo *appinfo)
 {
-  struct stat buf;
-  char *filename;
-
-  filename = g_build_filename (dir->path, cache_file, NULL);
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  if (g_stat (filename, &buf) < 0)
+  if (info->filename)
     {
-      g_free (filename);
-      return TRUE;
+      if (strstr (info->filename, "/userapp-"))
+        return g_access (info->filename, W_OK) == 0;
     }
-  g_free (filename);
-
-  if (buf.st_mtime != *timestamp)
-    return TRUE;
 
   return FALSE;
 }
 
-/* Call with lock held */
-static void
-mime_info_cache_dir_init (MimeInfoCacheDir *dir)
+static gboolean
+g_desktop_app_info_delete (GAppInfo *appinfo)
 {
-  GError *load_error;
-  GKeyFile *key_file;
-  gchar *filename, **mime_types;
-  int i;
-  struct stat buf;
-
-  load_error = NULL;
-  mime_types = NULL;
-
-  if (dir->mime_info_cache_map != NULL &&
-      !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
-                                        &dir->mime_info_cache_timestamp))
-    return;
-
-  if (dir->mime_info_cache_map != NULL)
-    destroy_info_cache_map (dir->mime_info_cache_map);
-
-  dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                                    (GDestroyNotify) g_free,
-                                                    NULL);
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
 
-  key_file = g_key_file_new ();
+  if (info->filename)
+    {
+      if (g_remove (info->filename) == 0)
+        {
+          update_mimeapps_list (info->desktop_id, NULL,
+                                UPDATE_MIME_NONE,
+                                NULL);
 
-  filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
+          g_free (info->filename);
+          info->filename = NULL;
+          g_free (info->desktop_id);
+          info->desktop_id = NULL;
 
-  if (g_stat (filename, &buf) < 0)
-    goto error;
+          return TRUE;
+        }
+    }
 
-  dir->mime_info_cache_timestamp = buf.st_mtime;
+  return FALSE;
+}
 
-  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
+/* Create for commandline {{{2 */
+/**
+ * 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 occurring, %NULL to ignore.
+ *
+ * Creates a new #GAppInfo from the given information.
+ *
+ * Note that for @commandline, the quoting rules of the Exec key of the
+ * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
+ * are applied. For example, if the @commandline contains
+ * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
+ * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
+ *
+ * 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_free (filename);
-  filename = NULL;
+  g_return_val_if_fail (commandline, NULL);
 
-  if (load_error != NULL)
-    goto error;
+  info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
 
-  mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
-                                    NULL, &load_error);
+  info->filename = NULL;
+  info->desktop_id = NULL;
 
-  if (load_error != NULL)
-    goto error;
+  info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
+  info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
+  info->hidden = FALSE;
+  if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
+    info->exec = g_strconcat (commandline, " %u", NULL);
+  else
+    info->exec = g_strconcat (commandline, " %f", NULL);
+  info->nodisplay = TRUE;
+  info->binary = binary_from_exec (info->exec);
 
-  for (i = 0; mime_types[i] != NULL; i++)
+  if (application_name)
+    info->name = g_strdup (application_name);
+  else
     {
-      gchar **desktop_file_ids;
-      char *unaliased_type;
-      desktop_file_ids = g_key_file_get_string_list (key_file,
-                                                     MIME_CACHE_GROUP,
-                                                     mime_types[i],
-                                                     NULL,
-                                                     NULL);
-
-      if (desktop_file_ids == NULL)
-        continue;
-
-      unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-      mime_info_cache_dir_add_desktop_entries (dir,
-                                               unaliased_type,
-                                               desktop_file_ids);
-      g_free (unaliased_type);
-
-      g_strfreev (desktop_file_ids);
+      /* 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);
 
-  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);
+  return G_APP_INFO (info);
 }
 
+/* GAppInfo interface init */
+
 static void
-mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
+g_desktop_app_info_iface_init (GAppInfoIface *iface)
 {
-  GKeyFile *key_file;
-  GError *load_error;
-  gchar *filename, **mime_types;
-  char *unaliased_type;
-  char **desktop_file_ids;
-  int i;
-  struct stat buf;
-
-  load_error = NULL;
-  mime_types = NULL;
-
-  if (dir->defaults_list_map != NULL &&
-      !mime_info_cache_dir_out_of_date (dir, "defaults.list",
-                                        &dir->defaults_list_timestamp))
-    return;
-
-  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 ();
-
-  filename = g_build_filename (dir->path, "defaults.list", NULL);
-  if (g_stat (filename, &buf) < 0)
-    goto error;
+  iface->dup = g_desktop_app_info_dup;
+  iface->equal = g_desktop_app_info_equal;
+  iface->get_id = g_desktop_app_info_get_id;
+  iface->get_name = g_desktop_app_info_get_name;
+  iface->get_description = g_desktop_app_info_get_description;
+  iface->get_executable = g_desktop_app_info_get_executable;
+  iface->get_icon = g_desktop_app_info_get_icon;
+  iface->launch = g_desktop_app_info_launch;
+  iface->supports_uris = g_desktop_app_info_supports_uris;
+  iface->supports_files = g_desktop_app_info_supports_files;
+  iface->launch_uris = g_desktop_app_info_launch_uris;
+  iface->should_show = g_desktop_app_info_should_show;
+  iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
+  iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
+  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;
+  iface->get_supported_types = g_desktop_app_info_get_supported_types;
+}
 
-  dir->defaults_list_timestamp = buf.st_mtime;
+/* Recommended applications {{{2 */
 
-  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
-  g_free (filename);
-  filename = NULL;
+/* Converts content_type into a list of itself with all of its parent
+ * types (if include_fallback is enabled) or just returns a single-item
+ * list with the unaliased content type.
+ */
+static gchar **
+get_list_of_mimetypes (const gchar *content_type,
+                       gboolean     include_fallback)
+{
+  gchar *unaliased;
+  GPtrArray *array;
 
-  if (load_error != NULL)
-    goto error;
+  array = g_ptr_array_new ();
+  unaliased = _g_unix_content_type_unalias (content_type);
+  g_ptr_array_add (array, unaliased);
 
-  mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
-                                    NULL, NULL);
-  if (mime_types != NULL)
+  if (include_fallback)
     {
-      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;
+      gint i;
 
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->defaults_list_map,
-                                unaliased_type,
-                                desktop_file_ids);
+      /* Iterate the array as we grow it, until we have nothing more to add */
+      for (i = 0; i < array->len; i++)
+        {
+          gchar **parents = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
+          gint j;
+
+          for (j = 0; parents[j]; j++)
+            /* Don't add duplicates */
+            if (!array_contains (array, parents[j]))
+              g_ptr_array_add (array, parents[j]);
+            else
+              g_free (parents[j]);
+
+          /* We already stole or freed each element.  Free the container. */
+          g_free (parents);
         }
-
-      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);
+  g_ptr_array_add (array, NULL);
 
-  if (load_error)
-    g_error_free (load_error);
+  return (gchar **) g_ptr_array_free (array, FALSE);
 }
 
-static void
-mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
+static gchar **
+g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
+                                                     gboolean     include_fallback)
 {
-  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;
+  GPtrArray *hits, *blacklist;
+  gchar **types;
+  gint i, j;
 
-  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;
+  hits = g_ptr_array_new ();
+  blacklist = g_ptr_array_new ();
 
-  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);
+  types = get_list_of_mimetypes (content_type, include_fallback);
 
-  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);
+  desktop_file_dirs_lock ();
 
-  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);
+  for (i = 0; types[i]; i++)
+    for (j = 0; j < n_desktop_file_dirs; j++)
+      desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], hits, blacklist);
 
-  key_file = g_key_file_new ();
+  desktop_file_dirs_unlock ();
 
-  filename = g_build_filename (dir->path, "mimeapps.list", NULL);
-  if (g_stat (filename, &buf) < 0)
-    goto error;
+  g_ptr_array_add (hits, NULL);
 
-  dir->mimeapps_list_timestamp = buf.st_mtime;
+  g_ptr_array_free (blacklist, TRUE);
+  g_strfreev (types);
 
-  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
-  g_free (filename);
-  filename = NULL;
+  return (gchar **) g_ptr_array_free (hits, FALSE);
+}
 
-  if (load_error != NULL)
-    goto error;
+static gchar **
+g_desktop_app_info_get_defaults_for_content_type (const gchar *content_type)
+{
+  GPtrArray *results;
+  gchar **types;
+  gint i, j;
 
-  mime_types = g_key_file_get_keys (key_file, ADDED_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,
-                                                         ADDED_ASSOCIATIONS_GROUP,
-                                                         mime_types[i],
-                                                         NULL,
-                                                         NULL);
-          if (desktop_file_ids == NULL)
-            continue;
+  types = get_list_of_mimetypes (content_type, TRUE);
+  results = g_ptr_array_new ();
 
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->mimeapps_list_added_map,
-                                unaliased_type,
-                                desktop_file_ids);
-        }
+  desktop_file_dirs_lock ();
 
-      g_strfreev (mime_types);
-    }
+  for (i = 0; types[i]; i++)
+    for (j = 0; j < n_desktop_file_dirs; j++)
+      desktop_file_dir_default_lookup (&desktop_file_dirs[j], types[i], results);
 
-  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;
+  desktop_file_dirs_unlock ();
 
-          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_ptr_array_add (results, NULL);
+  g_strfreev (types);
 
-      g_strfreev (mime_types);
-    }
+  return (gchar **) g_ptr_array_free (results, FALSE);
+}
 
-  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;
+/**
+ * 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)
+{
+  gchar **desktop_ids;
+  GList *infos;
+  gint i;
 
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->mimeapps_list_defaults_map,
-                                unaliased_type,
-                                desktop_id);
-        }
+  g_return_val_if_fail (content_type != NULL, NULL);
 
-      g_strfreev (mime_types);
-    }
+  desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
 
-  g_key_file_free (key_file);
-  return;
+  infos = NULL;
+  for (i = 0; desktop_ids[i]; i++)
+    {
+      GDesktopAppInfo *info;
 
- error:
-  g_free (filename);
-  g_key_file_free (key_file);
+      info = g_desktop_app_info_new (desktop_ids[i]);
+      if (info)
+        infos = g_list_prepend (infos, info);
+    }
 
-  if (mime_types != NULL)
-    g_strfreev (mime_types);
+  g_strfreev (desktop_ids);
 
-  if (load_error)
-    g_error_free (load_error);
+  return g_list_reverse (infos);
 }
 
-static MimeInfoCacheDir *
-mime_info_cache_dir_new (const char *path)
+/**
+ * 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)
 {
-  MimeInfoCacheDir *dir;
-
-  dir = g_new0 (MimeInfoCacheDir, 1);
-  dir->path = g_strdup (path);
+  gchar **recommended_ids;
+  gchar **all_ids;
+  GList *infos;
+  gint i;
 
-  return dir;
-}
+  g_return_val_if_fail (content_type != NULL, NULL);
 
-static void
-mime_info_cache_dir_free (MimeInfoCacheDir *dir)
-{
-  if (dir == NULL)
-    return;
+  recommended_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
+  all_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
 
-  if (dir->mime_info_cache_map != NULL)
+  infos = NULL;
+  for (i = 0; all_ids[i]; i++)
     {
-      destroy_info_cache_map (dir->mime_info_cache_map);
-      dir->mime_info_cache_map = NULL;
+      GDesktopAppInfo *info;
+      gint j;
 
-  }
+      /* Don't return the ones on the recommended list */
+      for (j = 0; recommended_ids[j]; j++)
+        if (g_str_equal (all_ids[i], recommended_ids[j]))
+          break;
 
-  if (dir->defaults_list_map != NULL)
-    {
-      g_hash_table_destroy (dir->defaults_list_map);
-      dir->defaults_list_map = NULL;
-    }
+      if (recommended_ids[j])
+        continue;
 
-  if (dir->mimeapps_list_added_map != NULL)
-    {
-      g_hash_table_destroy (dir->mimeapps_list_added_map);
-      dir->mimeapps_list_added_map = NULL;
-    }
+      info = g_desktop_app_info_new (all_ids[i]);
 
-  if (dir->mimeapps_list_removed_map != NULL)
-    {
-      g_hash_table_destroy (dir->mimeapps_list_removed_map);
-      dir->mimeapps_list_removed_map = NULL;
+      if (info)
+        infos = g_list_prepend (infos, info);
     }
 
-  if (dir->mimeapps_list_defaults_map != NULL)
-    {
-      g_hash_table_destroy (dir->mimeapps_list_defaults_map);
-      dir->mimeapps_list_defaults_map = NULL;
-    }
+  g_strfreev (recommended_ids);
+  g_strfreev (all_ids);
 
-  g_free (dir);
+  return g_list_reverse (infos);
 }
 
-static void
-mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
-                                         const char        *mime_type,
-                                         char             **new_desktop_file_ids)
+/**
+ * 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,
+ * 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.
+ **/
+GList *
+g_app_info_get_all_for_type (const char *content_type)
 {
-  GList *desktop_file_ids;
-  int i;
+  gchar **desktop_ids;
+  GList *infos;
+  gint i;
 
-  desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
-                                          mime_type);
+  g_return_val_if_fail (content_type != NULL, NULL);
+
+  desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
 
-  for (i = 0; new_desktop_file_ids[i] != NULL; i++)
+  infos = NULL;
+  for (i = 0; desktop_ids[i]; 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]));
+      GDesktopAppInfo *info;
+
+      info = g_desktop_app_info_new (desktop_ids[i]);
+      if (info)
+        infos = g_list_prepend (infos, info);
     }
 
-  g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
+  g_strfreev (desktop_ids);
+
+  return g_list_reverse (infos);
 }
 
-static void
-mime_info_cache_init_dir_lists (void)
+/**
+ * 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)
 {
-  int i;
+  update_mimeapps_list (NULL, content_type,
+                        UPDATE_MIME_NONE,
+                        NULL);
+}
 
-  mime_info_cache = mime_info_cache_new ();
+/**
+ * g_app_info_get_default_for_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 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)
+{
+  gchar **desktop_ids;
+  GAppInfo *info;
+  gint i;
 
-  desktop_file_dirs_refresh ();
+  g_return_val_if_fail (content_type != NULL, NULL);
 
-  for (i = 0; i < n_desktop_file_dirs; i++)
-    {
-      MimeInfoCacheDir *dir;
+  desktop_ids = g_desktop_app_info_get_defaults_for_content_type (content_type);
 
-      dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
+  info = NULL;
+  for (i = 0; desktop_ids[i]; i++)
+    {
+      info = (GAppInfo *) g_desktop_app_info_new (desktop_ids[i]);
 
-      if (dir != NULL)
+      if (info)
         {
-          mime_info_cache_dir_init (dir);
-          mime_info_cache_dir_init_defaults_list (dir);
-          mime_info_cache_dir_init_mimeapps_list (dir);
+          if (!must_support_uris || g_app_info_supports_uris (info))
+            break;
 
-          mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
+          g_object_unref (info);
+          info = NULL;
         }
     }
-}
-
-static void
-mime_info_cache_update_dir_lists (void)
-{
-  GList *tmp;
 
-  tmp = mime_info_cache->dirs;
+  g_strfreev (desktop_ids);
 
-  while (tmp != NULL)
+  /* If we can't find a default app for this content type, pick one from
+   * the list of all supported apps.  This will be ordered by the user's
+   * preference and by "recommended" apps first, so the first one we
+   * find is probably the best fallback.
+   */
+  if (info == NULL)
     {
-      MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
-
-      /* No need to do this if we had file monitors... */
-      mime_info_cache_dir_init (dir);
-      mime_info_cache_dir_init_defaults_list (dir);
-      mime_info_cache_dir_init_mimeapps_list (dir);
+      desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
 
-      tmp = tmp->next;
-    }
-}
+      for (i = 0; desktop_ids[i]; i++)
+        {
+          info = (GAppInfo *) g_desktop_app_info_new (desktop_ids[i]);
 
-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;
+          if (info)
+            {
+              if (!must_support_uris || g_app_info_supports_uris (info))
+                break;
 
-      time (&now);
-      if (now >= mime_info_cache->last_stat_time + 10)
-        {
-          mime_info_cache_update_dir_lists ();
-          mime_info_cache->last_stat_time = now;
+              g_object_unref (info);
+              info = NULL;
+            }
         }
+
+      g_strfreev (desktop_ids);
     }
 
-  G_UNLOCK (mime_info_cache);
+  return info;
 }
 
-static MimeInfoCache *
-mime_info_cache_new (void)
+/**
+ * g_app_info_get_default_for_uri_scheme:
+ * @uri_scheme: a string containing a URI scheme.
+ *
+ * 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)
 {
-  MimeInfoCache *cache;
+  GAppInfo *app_info;
+  char *content_type, *scheme_down;
 
-  cache = g_new0 (MimeInfoCache, 1);
+  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 cache;
+  return app_info;
 }
 
-static void
-mime_info_cache_free (MimeInfoCache *cache)
-{
-  if (cache == NULL)
-    return;
-
-  g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
-  g_free (cache);
-}
+/* "Get all" API {{{2 */
 
 /**
- * mime_info_cache_reload:
- * @dir: directory path which needs reloading.
+ * g_desktop_app_info_get_implementations:
+ * @interface: the name of the interface
  *
- * Reload the mime information for the @dir.
- */
-static void
-mime_info_cache_reload (const char *dir)
+ * Gets all applications that implement @interface.
+ *
+ * An application implements an interface if that interface is listed in
+ * the Implements= line of the desktop file of the application.
+ *
+ * Returns: (element-type GDesktopAppInfo) (transfer full): a list of #GDesktopAppInfo
+ * objects.
+ *
+ * Since: 2.42
+ **/
+GList *
+g_desktop_app_info_get_implementations (const gchar *interface)
 {
-  /* FIXME: just reload the dir that needs reloading,
-   * don't blow the whole cache
-   */
-  if (mime_info_cache != NULL)
+  GList *result = NULL;
+  GList **ptr;
+  gint i;
+
+  desktop_file_dirs_lock ();
+
+  for (i = 0; i < n_desktop_file_dirs; i++)
+    desktop_file_dir_get_implementations (&desktop_file_dirs[i], &result, interface);
+
+  desktop_file_dirs_unlock ();
+
+  ptr = &result;
+  while (*ptr)
     {
-      G_LOCK (mime_info_cache);
-      mime_info_cache_free (mime_info_cache);
-      mime_info_cache = NULL;
-      G_UNLOCK (mime_info_cache);
-    }
-}
+      gchar *name = (*ptr)->data;
+      GDesktopAppInfo *app;
 
-static GList *
-append_desktop_entry (GList      *list,
-                      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) &&
-      !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
-    list = g_list_prepend (list, g_strdup (desktop_entry));
+      app = g_desktop_app_info_new (name);
+      g_free (name);
+
+      if (app)
+        {
+          (*ptr)->data = app;
+          ptr = &(*ptr)->next;
+        }
+      else
+        *ptr = g_list_delete_link (*ptr, *ptr);
+    }
 
-  return list;
+  return result;
 }
 
 /**
- * get_all_desktop_entries_for_mime_type:
- * @mime_type: a mime type.
- * @except: NULL or a strv list
+ * g_desktop_app_info_search:
+ * @search_string: the search string to use
  *
- * 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.
+ * Searches desktop files for ones that match @search_string.
  *
- * Optionally doesn't list the desktop ids given in the @except
+ * The return value is an array of strvs.  Each strv contains a list of
+ * applications that matched @search_string with an equal score.  The
+ * outer list is sorted by score so that the first strv contains the
+ * best-matching applications, and so on.
+ * The algorithm for determining matches is undefined and may change at
+ * any time.
  *
- * Return value: a #GList containing the desktop ids which claim
- *    to handle @mime_type.
+ * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
+ *   list of strvs.  Free each item with g_strfreev() and free the outer
+ *   list with g_free().
  */
-static GList *
-get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
-                                       const char **except,
-                                       gboolean     include_fallback,
-                                       char       **explicit_default)
-{
-  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;
-  char **removed_associations;
-  gboolean already_found_handler;
-  int i, j, k;
-  GPtrArray *array;
-  char **anc;
+gchar ***
+g_desktop_app_info_search (const gchar *search_string)
+{
+  gchar **search_tokens;
+  gint last_category = -1;
+  gchar ***results;
+  gint n_categories = 0;
+  gint start_of_category;
+  gint i, j;
 
-  mime_info_cache_init ();
+  search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
 
-  if (include_fallback)
+  desktop_file_dirs_lock ();
+
+  reset_total_search_results ();
+
+  for (i = 0; i < n_desktop_file_dirs; i++)
     {
-      /* 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++)
+      for (j = 0; search_tokens[j]; j++)
         {
-          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);
+          desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
+          merge_token_results (j == 0);
         }
-      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;
+      merge_directory_results ();
     }
 
-  G_LOCK (mime_info_cache);
+  sort_total_search_results ();
 
-  removed_entries = NULL;
-  desktop_entries = NULL;
+  /* Count the total number of unique categories */
+  for (i = 0; i < static_total_results_size; i++)
+    if (static_total_results[i].category != last_category)
+      {
+        last_category = static_total_results[i].category;
+        n_categories++;
+      }
 
-  for (i = 0; except != NULL && except[i] != NULL; i++)
-    removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
+  results = g_new (gchar **, n_categories + 1);
 
-  for (i = 0; mime_types[i] != NULL; i++)
+  /* Start loading into the results list */
+  start_of_category = 0;
+  for (i = 0; i < n_categories; i++)
     {
-      mime_type = mime_types[i];
+      gint n_items_in_category = 0;
+      gint this_category;
+      gint j;
 
-      /* This is true if we already found a handler for a more specific
-         mimetype. If its set we ignore any defaults for the less specific
-         mimetypes. */
-      already_found_handler = (desktop_entries != NULL);
+      this_category = static_total_results[start_of_category].category;
 
-      /* 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 if we got no result earlier
-           * (ie, for more specific mime types)
-           */
-          if (!already_found_handler)
-            {
-              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);
-                }
-            }
+      while (start_of_category + n_items_in_category < static_total_results_size &&
+             static_total_results[start_of_category + n_items_in_category].category == this_category)
+        n_items_in_category++;
 
-          /* 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);
+      results[i] = g_new (gchar *, n_items_in_category + 1);
+      for (j = 0; j < n_items_in_category; j++)
+        results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
+      results[i][j] = NULL;
 
-          /* 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);
+      start_of_category += n_items_in_category;
+    }
+  results[i] = 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++)
-            {
-              if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
-                old_default_entry = g_strdup (default_entries[j]);
+  desktop_file_dirs_unlock ();
 
-              desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
-            }
-        }
+  g_strfreev (search_tokens);
 
-      /* Go through all entries that support the mimetype */
-      for (dir_list = mime_info_cache->dirs;
-           dir_list != NULL;
-           dir_list = dir_list->next)
-        {
-          dir = dir_list->data;
+  return results;
+}
 
-          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, removed_entries);
-        }
-    }
+/**
+ * g_app_info_get_all:
+ *
+ * Gets a list of all of the applications currently registered
+ * on this system.
+ *
+ * For desktop files, this includes applications that have
+ * `NoDisplay=true` set or are excluded from display by means
+ * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
+ * The returned list does not include applications which have
+ * the `Hidden` key set.
+ *
+ * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
+ **/
+GList *
+g_app_info_get_all (void)
+{
+  GHashTable *apps;
+  GHashTableIter iter;
+  gpointer value;
+  int i;
+  GList *infos;
 
-  G_UNLOCK (mime_info_cache);
+  apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
-  g_strfreev (mime_types);
+  desktop_file_dirs_lock ();
 
-  /* 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);
+  for (i = 0; i < n_desktop_file_dirs; i++)
+    desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
 
-  if (explicit_default != NULL)
-    *explicit_default = default_entry;
-  else
-    g_free (default_entry);
+  desktop_file_dirs_unlock ();
 
-  g_list_free_full (removed_entries, g_free);
+  infos = NULL;
+  g_hash_table_iter_init (&iter, apps);
+  while (g_hash_table_iter_next (&iter, NULL, &value))
+    {
+      if (value)
+        infos = g_list_prepend (infos, value);
+    }
 
-  desktop_entries = g_list_reverse (desktop_entries);
+  g_hash_table_destroy (apps);
 
-  return desktop_entries;
+  return infos;
 }
 
 /* GDesktopAppInfoLookup interface {{{2 */
@@ -3724,7 +4434,7 @@ g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
  *
  * Returns: %TRUE if the @key exists
  *
- * Since: 2.26
+ * Since: 2.36
  */
 gboolean
 g_desktop_app_info_has_key (GDesktopAppInfo *info,