Imported Upstream version 2.73.3
[platform/upstream/glib.git] / gio / gdesktopappinfo.c
index f97ad6d..c46a8f0 100644 (file)
@@ -3,10 +3,12 @@
  * Copyright (C) 2006-2007 Red Hat, Inc.
  * Copyright © 2007 Ryan Lortie
  *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,9 +16,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>
 
 #include "config.h"
 
+/* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
+ * in the preprocessor, we need to define this before including glib.h*/
+#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
+#endif
+
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
 #include "gfileicon.h"
 #include <glib/gstdio.h>
 #include "glibintl.h"
+#include "glib-private.h"
 #include "giomodule-priv.h"
 #include "gappinfo.h"
 #include "gappinfoprivate.h"
-#include "glocaldirectorymonitor.h"
+#include "glocalfilemonitor.h"
 
+#ifdef G_OS_UNIX
+#include "gdocumentportal.h"
+#endif
 
 /**
  * SECTION:gdesktopappinfo
@@ -60,9 +70,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"
@@ -80,13 +90,9 @@ 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);
+static gboolean g_desktop_app_info_load_file (GDesktopAppInfo *self);
 
 /**
  * GDesktopAppInfo:
@@ -139,27 +145,97 @@ 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
 {
+  gatomicrefcount             ref_count;
   gchar                      *path;
-  GLocalDirectoryMonitor     *monitor;
-  GHashTable                 *app_names;
+  gchar                      *alternatively_watching;
+  gboolean                    is_config;
   gboolean                    is_setup;
+  GFileMonitor               *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 GPtrArray      *desktop_file_dirs = NULL;
+static const gchar    *desktop_file_dirs_config_dir = NULL;
+static DesktopFileDir *desktop_file_dir_user_config = NULL;  /* (owned) */
+static DesktopFileDir *desktop_file_dir_user_data = NULL;  /* (owned) */
 static GMutex          desktop_file_dir_lock;
+static const gchar    *gio_launch_desktop_path = NULL;
 
 /* Monitor 'changed' signal handler {{{2 */
 static void desktop_file_dir_reset (DesktopFileDir *dir);
 
+static DesktopFileDir *
+desktop_file_dir_ref (DesktopFileDir *dir)
+{
+  g_atomic_ref_count_inc (&dir->ref_count);
+
+  return dir;
+}
+
+static void
+desktop_file_dir_unref (DesktopFileDir *dir)
+{
+  if (g_atomic_ref_count_dec (&dir->ref_count))
+    {
+      desktop_file_dir_reset (dir);
+      g_free (dir->path);
+      g_free (dir);
+    }
+}
+
+/*< internal >
+ * desktop_file_dir_get_alternative_dir:
+ * @dir: a #DesktopFileDir
+ *
+ * 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 gchar *
+desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
+{
+  gchar *parent;
+
+  /* If the directory itself exists then we need no alternative. */
+  if (g_access (dir->path, R_OK | X_OK) == 0)
+    return NULL;
+
+  /* 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);
+    }
+
+  return parent;
+}
+
 static void
 desktop_file_dir_changed (GFileMonitor      *monitor,
                           GFile             *file,
@@ -168,6 +244,7 @@ desktop_file_dir_changed (GFileMonitor      *monitor,
                           gpointer           user_data)
 {
   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.
@@ -175,15 +252,30 @@ desktop_file_dir_changed (GFileMonitor      *monitor,
    * 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);
 
-  desktop_file_dir_reset (dir);
+  if (dir->alternatively_watching)
+    {
+      gchar *alternative_dir;
+
+      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);
+    }
+
+  if (!do_nothing)
+    desktop_file_dir_reset (dir);
 
   g_mutex_unlock (&desktop_file_dir_lock);
 
   /* Notify anyone else who may be interested */
-  g_app_info_monitor_fire ();
+  if (!do_nothing)
+    g_app_info_monitor_fire ();
 }
 
 /* Internal utility functions {{{2 */
@@ -203,17 +295,108 @@ static gboolean
 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
                                      const gchar    *app_name)
 {
-  while (dir > desktop_file_dirs)
+  guint i;
+
+  for (i = 0; i < desktop_file_dirs->len; i++)
     {
-      dir--;
+      DesktopFileDir *i_dir = g_ptr_array_index (desktop_file_dirs, i);
 
-      if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
+      if (dir == i_dir)
+        return FALSE;
+      if (i_dir->app_names && g_hash_table_contains (i_dir->app_names, app_name))
         return TRUE;
     }
 
   return FALSE;
 }
 
+/* Not much to go on from https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
+ * so validate it as a non-empty alphanumeric ASCII string with `-` and `_` allowed.
+ *
+ * Validation is important as the desktop IDs are used to construct filenames,
+ * and may be set by an unprivileged caller if running in a setuid program. */
+static gboolean
+validate_xdg_desktop (const gchar *desktop)
+{
+  gsize i;
+
+  for (i = 0; desktop[i] != '\0'; i++)
+    if (desktop[i] != '-' && desktop[i] != '_' &&
+        !g_ascii_isalnum (desktop[i]))
+      return FALSE;
+
+  if (i == 0)
+    return FALSE;
+
+  return TRUE;
+}
+
+static char **
+get_valid_current_desktops (const char *value)
+{
+  char **tmp;
+  gsize i;
+  GPtrArray *valid_desktops;
+
+  if (value == NULL)
+    value = g_getenv ("XDG_CURRENT_DESKTOP");
+  if (value == NULL)
+    value = "";
+
+  tmp = g_strsplit (value, G_SEARCHPATH_SEPARATOR_S, 0);
+  valid_desktops = g_ptr_array_new_full (g_strv_length (tmp) + 1, g_free);
+  for (i = 0; tmp[i]; i++)
+    {
+      if (validate_xdg_desktop (tmp[i]))
+        g_ptr_array_add (valid_desktops, tmp[i]);
+      else
+        g_free (tmp[i]);
+    }
+  g_ptr_array_add (valid_desktops, NULL);
+  g_free (tmp);
+  tmp = (char **) g_ptr_array_steal (valid_desktops, NULL);
+  g_ptr_array_unref (valid_desktops);
+  return tmp;
+}
+
+static const gchar * const *
+get_lowercase_current_desktops (void)
+{
+  static gchar **result;
+
+  if (g_once_init_enter (&result))
+    {
+      char **tmp = get_valid_current_desktops (NULL);
+      gsize i, j;
+
+      for (i = 0; tmp[i]; i++)
+        {
+          /* Convert to lowercase. */
+          for (j = 0; tmp[i][j]; j++)
+            tmp[i][j] = g_ascii_tolower (tmp[i][j]);
+        }
+
+      g_once_init_leave (&result, tmp);
+    }
+
+  return (const gchar **) result;
+}
+
+static const gchar * const *
+get_current_desktops (const gchar *value)
+{
+  static gchar **result;
+
+  if (g_once_init_enter (&result))
+    {
+      char **tmp = get_valid_current_desktops (value);
+
+      g_once_init_leave (&result, tmp);
+    }
+
+  return (const gchar **) result;
+}
+
 /*< internal >
  * add_to_table_if_appropriate:
  * @apps: a string to GDesktopAppInfo hash table
@@ -249,6 +432,7 @@ add_to_table_if_appropriate (GHashTable      *apps,
 enum
 {
   DESKTOP_KEY_Comment,
+  DESKTOP_KEY_Exec,
   DESKTOP_KEY_GenericName,
   DESKTOP_KEY_Keywords,
   DESKTOP_KEY_Name,
@@ -264,10 +448,27 @@ const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
    * use the same number for the two different keys.
    */
   [DESKTOP_KEY_Name]             = 1,
-  [DESKTOP_KEY_Keywords]         = 2,
-  [DESKTOP_KEY_GenericName]      = 3,
-  [DESKTOP_KEY_X_GNOME_FullName] = 4,
-  [DESKTOP_KEY_Comment]          = 5
+  [DESKTOP_KEY_Exec]             = 2,
+  [DESKTOP_KEY_Keywords]         = 3,
+  [DESKTOP_KEY_GenericName]      = 4,
+  [DESKTOP_KEY_X_GNOME_FullName] = 5,
+  [DESKTOP_KEY_Comment]          = 6
+};
+
+/* Common prefix commands to ignore from Exec= lines */
+const char * const exec_key_match_blocklist[] = {
+  "bash",
+  "env",
+  "flatpak",
+  "gjs",
+  "pkexec",
+  "python",
+  "python2",
+  "python3",
+  "sh",
+  "wine",
+  "wine64",
+  NULL
 };
 
 static gchar *
@@ -277,14 +478,16 @@ desktop_key_get_name (guint key_id)
     {
     case DESKTOP_KEY_Comment:
       return "Comment";
+    case DESKTOP_KEY_Exec:
+      return "Exec";
     case DESKTOP_KEY_GenericName:
-      return "GenericName";
+      return GENERIC_NAME_KEY;
     case DESKTOP_KEY_Keywords:
-      return "Keywords";
+      return KEYWORDS_KEY;
     case DESKTOP_KEY_Name:
       return "Name";
     case DESKTOP_KEY_X_GNOME_FullName:
-      return "X-GNOME-FullName";
+      return FULL_NAME_KEY;
     default:
       g_assert_not_reached ();
     }
@@ -391,7 +594,8 @@ add_token_result (const gchar *app_name,
 static void
 merge_token_results (gboolean first)
 {
-  qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
+  if (static_token_results_size != 0)
+    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.
@@ -491,7 +695,8 @@ reset_total_search_results (void)
 static void
 sort_total_search_results (void)
 {
-  qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
+  if (static_total_results_size != 0)
+    qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
 }
 
 static void
@@ -505,9 +710,10 @@ merge_directory_results (void)
       static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
     }
 
-  memcpy (static_total_results + static_total_results_size,
-          static_search_results,
-          static_search_results_size * sizeof (struct search_result));
+  if (static_total_results + static_total_results_size != 0)
+    memcpy (static_total_results + static_total_results_size,
+            static_search_results,
+            static_search_results_size * sizeof (struct search_result));
 
   static_total_results_size += static_search_results_size;
 
@@ -515,7 +721,6 @@ merge_directory_results (void)
   static_search_results_size = 0;
 }
 
-
 /* Support for unindexed DesktopFileDirs {{{2 */
 static void
 get_apps_from_dir (GHashTable **apps,
@@ -562,10 +767,257 @@ get_apps_from_dir (GHashTable **apps,
   g_dir_close (dir);
 }
 
+typedef struct
+{
+  gchar **additions;
+  gchar **removals;
+  gchar **defaults;
+} UnindexedMimeTweaks;
+
+static void
+free_mime_tweaks (gpointer data)
+{
+  UnindexedMimeTweaks *tweaks = data;
+
+  g_strfreev (tweaks->additions);
+  g_strfreev (tweaks->removals);
+  g_strfreev (tweaks->defaults);
+
+  g_slice_free (UnindexedMimeTweaks, tweaks);
+}
+
+static UnindexedMimeTweaks *
+desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
+                                       const gchar    *mime_type)
+{
+  UnindexedMimeTweaks *tweaks;
+  gchar *unaliased_type;
+
+  unaliased_type = _g_unix_content_type_unalias (mime_type);
+  tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type);
+
+  if (tweaks == NULL)
+    {
+      tweaks = g_slice_new0 (UnindexedMimeTweaks);
+      g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
+    }
+  else
+    g_free (unaliased_type);
+
+  return tweaks;
+}
+
+/* consumes 'to_add' */
+static void
+expand_strv (gchar         ***strv_ptr,
+             gchar          **to_add,
+             gchar * const   *blocklist)
+{
+  guint strv_len, add_len;
+  gchar **strv;
+  guint i, j;
+
+  if (!*strv_ptr)
+    {
+      *strv_ptr = to_add;
+      return;
+    }
+
+  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 blocklisted strings */
+      if (blocklist)
+        for (j = 0; blocklist[j]; j++)
+          if (g_str_equal (to_add[i], blocklist[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);
+}
+
+static void
+desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
+                                               const gchar    *filename,
+                                               const gchar    *added_group,
+                                               gboolean        tweaks_permitted)
+{
+  UnindexedMimeTweaks *tweaks;
+  char **desktop_file_ids;
+  GKeyFile *key_file;
+  gchar **mime_types;
+  int i;
+
+  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);
+
+  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, added_group);
+      g_strfreev (mime_types);
+      mime_types = 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_group, mime_types[i], NULL, NULL);
+
+          if (desktop_file_ids)
+            {
+              tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
+              expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
+            }
+        }
+
+      g_strfreev (mime_types);
+    }
+
+  mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
+
+  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 (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)
+            {
+              tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
+              expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
+            }
+        }
+
+      g_strfreev (mime_types);
+    }
+
+  mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
+
+  if (mime_types != NULL)
+    {
+      for (i = 0; mime_types[i] != NULL; i++)
+        {
+          desktop_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 void
+desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
+{
+  const gchar * const *desktops;
+  gchar *filename;
+  gint i;
+
+  dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
+
+  /* We process in order of precedence, using a blocklisting 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);
+    }
+
+  /* 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);
+
+  /* The remaining files are only checked for in directories that might
+   * contain desktop files (ie: not the config dirs).
+   */
+  if (dir->is_config)
+    return;
+
+  /* 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);
+
+  /* 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 void
 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
 {
-  get_apps_from_dir (&dir->app_names, dir->path, "");
+  if (!dir->is_config)
+    get_apps_from_dir (&dir->app_names, dir->path, "");
+
+  desktop_file_dir_unindexed_read_mimeapps_lists (dir);
+}
+
+static GDesktopAppInfo *
+g_desktop_app_info_new_from_filename_unlocked (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_clear_object (&info);
+
+  return info;
 }
 
 static GDesktopAppInfo *
@@ -579,7 +1031,7 @@ desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
   if (!filename)
     return NULL;
 
-  return g_desktop_app_info_new_from_filename (filename);
+  return g_desktop_app_info_new_from_filename_unlocked (filename);
 }
 
 static void
@@ -599,7 +1051,7 @@ desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
       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));
+      add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename_unlocked (filename));
     }
 }
 
@@ -687,6 +1139,7 @@ desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
   gpointer app, path;
 
   dir->memory_index = memory_index_new ();
+  dir->memory_implementations = memory_index_new ();
 
   /* Nothing to search? */
   if (dir->app_names == NULL)
@@ -706,22 +1159,50 @@ desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
           !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
         {
           /* Index the interesting keys... */
-          gint i;
+          gchar **implements;
+          gsize i;
 
           for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
             {
-              gchar *value;
+              const gchar *value;
+              gchar *raw;
 
               if (!desktop_key_match_category[i])
                 continue;
 
-              value = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
+              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;
+
+                  /* Don't match on blocklisted binaries like interpreters */
+                  if (g_strv_contains (exec_key_match_blocklist, value))
+                   value = NULL;
+                }
 
               if (value)
                 memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
 
-              g_free (value);
+              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);
         }
 
       g_key_file_free (key_file);
@@ -754,46 +1235,160 @@ desktop_file_dir_unindexed_search (DesktopFileDir  *dir,
     }
 }
 
-/* DesktopFileDir "API" {{{2 */
-
-/*< 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)
+static gboolean
+array_contains (GPtrArray *array,
+                const gchar *str)
 {
-  DesktopFileDir dir = { 0, };
+  guint i;
 
-  dir.path = g_build_filename (data_dir, "applications", NULL);
+  for (i = 0; i < array->len; i++)
+    if (g_str_equal (array->pdata[i], str))
+      return TRUE;
 
-  g_array_append_val (array, dir);
+  return FALSE;
 }
 
-/*< 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)
+desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
+                                        const gchar    *mime_type,
+                                        GPtrArray      *hits,
+                                        GPtrArray      *blocklist)
 {
-  if (dir->monitor)
-    {
-      g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
-      g_object_unref (dir->monitor);
-      dir->monitor = NULL;
-    }
+  UnindexedMimeTweaks *tweaks;
+  gint i;
 
-  if (dir->app_names)
-    {
-      g_hash_table_unref (dir->app_names);
+  tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
+
+  if (!tweaks)
+    return;
+
+  if (tweaks->additions)
+    {
+      for (i = 0; tweaks->additions[i]; i++)
+        {
+          gchar *app_name = tweaks->additions[i];
+
+          if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
+              !array_contains (blocklist, app_name) && !array_contains (hits, app_name))
+            g_ptr_array_add (hits, app_name);
+        }
+    }
+
+  if (tweaks->removals)
+    {
+      for (i = 0; tweaks->removals[i]; i++)
+        {
+          gchar *app_name = tweaks->removals[i];
+
+          if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
+              !array_contains (blocklist, app_name) && !array_contains (hits, app_name))
+            g_ptr_array_add (blocklist, app_name);
+        }
+    }
+}
+
+static void
+desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir,
+                                           const gchar    *mime_type,
+                                           GPtrArray      *results)
+{
+  UnindexedMimeTweaks *tweaks;
+  gint i;
+
+  tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
+
+  if (!tweaks || !tweaks->defaults)
+    return;
+
+  for (i = 0; tweaks->defaults[i]; i++)
+    {
+      gchar *app_name = tweaks->defaults[i];
+
+      if (!array_contains (results, app_name))
+        g_ptr_array_add (results, app_name);
+    }
+}
+
+static void
+desktop_file_dir_unindexed_get_implementations (DesktopFileDir  *dir,
+                                                GList          **results,
+                                                const gchar     *interface)
+{
+  MemoryIndexEntry *mie;
+
+  if (!dir->memory_index)
+    desktop_file_dir_unindexed_setup_search (dir);
+
+  for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next)
+    *results = g_list_prepend (*results, g_strdup (mie->app_name));
+}
+
+/* DesktopFileDir "API" {{{2 */
+
+/*< internal >
+ * desktop_file_dir_new:
+ * @data_dir: an XDG_DATA_DIR
+ *
+ * Creates a #DesktopFileDir for the corresponding @data_dir.
+ */
+static DesktopFileDir *
+desktop_file_dir_new (const gchar *data_dir)
+{
+  DesktopFileDir *dir = g_new0 (DesktopFileDir, 1);
+
+  g_atomic_ref_count_init (&dir->ref_count);
+  dir->path = g_build_filename (data_dir, "applications", NULL);
+
+  return g_steal_pointer (&dir);
+}
+
+/*< internal >
+ * desktop_file_dir_new_for_config:
+ * @config_dir: an XDG_CONFIG_DIR
+ *
+ * Just the same as desktop_file_dir_new() 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 DesktopFileDir *
+desktop_file_dir_new_for_config (const gchar *config_dir)
+{
+  DesktopFileDir *dir = g_new0 (DesktopFileDir, 1);
+
+  g_atomic_ref_count_init (&dir->ref_count);
+  dir->path = g_strdup (config_dir);
+  dir->is_config = TRUE;
+
+  return g_steal_pointer (&dir);
+}
+
+/*< 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)
+{
+  if (dir->alternatively_watching)
+    {
+      g_free (dir->alternatively_watching);
+      dir->alternatively_watching = NULL;
+    }
+
+  if (dir->monitor)
+    {
+      g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
+      g_file_monitor_cancel (dir->monitor);
+      g_object_unref (dir->monitor);
+      dir->monitor = NULL;
+    }
+
+  if (dir->app_names)
+    {
+      g_hash_table_unref (dir->app_names);
       dir->app_names = NULL;
     }
 
@@ -803,9 +1398,29 @@ desktop_file_dir_reset (DesktopFileDir *dir)
       dir->memory_index = NULL;
     }
 
+  if (dir->mime_tweaks)
+    {
+      g_hash_table_unref (dir->mime_tweaks);
+      dir->mime_tweaks = NULL;
+    }
+
+  if (dir->memory_implementations)
+    {
+      g_hash_table_unref (dir->memory_implementations);
+      dir->memory_implementations = NULL;
+    }
+
   dir->is_setup = FALSE;
 }
 
+static void
+closure_notify_cb (gpointer  data,
+                   GClosure *closure)
+{
+  DesktopFileDir *dir = data;
+  desktop_file_dir_unref (dir);
+}
+
 /*< internal >
  * desktop_file_dir_init:
  * @dir: a #DesktopFileDir
@@ -817,16 +1432,26 @@ desktop_file_dir_reset (DesktopFileDir *dir)
 static void
 desktop_file_dir_init (DesktopFileDir *dir)
 {
+  const gchar *watch_dir;
+
   g_assert (!dir->is_setup);
 
+  g_assert (!dir->alternatively_watching);
   g_assert (!dir->monitor);
-  dir->monitor = g_local_directory_monitor_new_in_worker (dir->path, G_FILE_MONITOR_NONE, NULL);
 
-  if (dir->monitor)
-    {
-      g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
-      g_local_directory_monitor_start (dir->monitor);
-    }
+  dir->alternatively_watching = desktop_file_dir_get_alternative_dir (dir);
+  watch_dir = dir->alternatively_watching ? dir->alternatively_watching : dir->path;
+
+  /* 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_file_monitor_new_in_worker (watch_dir, TRUE, G_FILE_MONITOR_NONE,
+                                                     desktop_file_dir_changed,
+                                                     desktop_file_dir_ref (dir),
+                                                     closure_notify_cb, NULL);
 
   desktop_file_dir_unindexed_init (dir);
 
@@ -871,8 +1496,49 @@ desktop_file_dir_get_all (DesktopFileDir *dir,
 }
 
 /*< internal >
+ * desktop_file_dir_mime_lookup:
+ * @dir: a #DesktopFileDir
+ * @mime_type: the mime type to look up
+ * @hits: the array to store the hits
+ * @blocklist: the array to store the blocklist
+ *
+ * Does a lookup of a mimetype against one desktop file directory,
+ * recording any hits and blocklisting and "Removed" associations (so
+ * later directories don't record them as hits).
+ *
+ * The items added to @hits are duplicated, but the ones in @blocklist
+ * are weak pointers.  This facilitates simply freeing the blocklist
+ * (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      *blocklist)
+{
+  desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blocklist);
+}
+
+/*< 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);
+}
+
+/*< internal >
  * desktop_file_dir_search:
- * @dir: a #DesktopFilEDir
+ * @dir: a #DesktopFileDir
  * @term: a normalised and casefolded search term
  *
  * Finds the names of applications in @dir that match @term.
@@ -884,40 +1550,71 @@ desktop_file_dir_search (DesktopFileDir *dir,
   desktop_file_dir_unindexed_search (dir, search_token);
 }
 
+static void
+desktop_file_dir_get_implementations (DesktopFileDir  *dir,
+                                      GList          **results,
+                                      const gchar     *interface)
+{
+  desktop_file_dir_unindexed_get_implementations (dir, results, interface);
+}
+
 /* Lock/unlock and global setup API {{{2 */
 
 static void
 desktop_file_dirs_lock (void)
 {
-  gint i;
+  guint i;
+  const gchar *user_config_dir = g_get_user_config_dir ();
 
   g_mutex_lock (&desktop_file_dir_lock);
 
-  if (desktop_file_dirs == NULL)
+  /* If the XDG dirs configuration has changed (expected only during tests),
+   * clear and reload the state. */
+  if (desktop_file_dirs_config_dir != NULL &&
+      g_strcmp0 (desktop_file_dirs_config_dir, user_config_dir) != 0)
+    {
+      g_debug ("%s: Resetting desktop app info dirs from %s to %s",
+               G_STRFUNC, desktop_file_dirs_config_dir, user_config_dir);
+
+      g_ptr_array_set_size (desktop_file_dirs, 0);
+      g_clear_pointer (&desktop_file_dir_user_config, desktop_file_dir_unref);
+      g_clear_pointer (&desktop_file_dir_user_data, desktop_file_dir_unref);
+    }
+
+  if (desktop_file_dirs == NULL || desktop_file_dirs->len == 0)
     {
-      const char * const *data_dirs;
-      GArray *tmp;
+      const char * const *dirs;
       gint i;
 
-      tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
+      if (desktop_file_dirs == NULL)
+        desktop_file_dirs = g_ptr_array_new_with_free_func ((GDestroyNotify) desktop_file_dir_unref);
 
-      /* Highest priority: the user's ~/.local/share/applications */
-      desktop_file_dir_create (tmp, g_get_user_data_dir ());
+      /* First, the configs.  Highest priority: the user's ~/.config */
+      desktop_file_dir_user_config = desktop_file_dir_new_for_config (user_config_dir);
+      g_ptr_array_add (desktop_file_dirs, desktop_file_dir_ref (desktop_file_dir_user_config));
 
-      /* 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]);
+      /* Next, the system configs (/etc/xdg, and so on). */
+      dirs = g_get_system_config_dirs ();
+      for (i = 0; dirs[i]; i++)
+        g_ptr_array_add (desktop_file_dirs, desktop_file_dir_new_for_config (dirs[i]));
 
-      desktop_file_dirs = (DesktopFileDir *) tmp->data;
-      n_desktop_file_dirs = tmp->len;
+      /* Now the data.  Highest priority: the user's ~/.local/share/applications */
+      desktop_file_dir_user_data = desktop_file_dir_new (g_get_user_data_dir ());
+      g_ptr_array_add (desktop_file_dirs, desktop_file_dir_ref (desktop_file_dir_user_data));
+
+      /* Following that, XDG_DATA_DIRS/applications, in order */
+      dirs = g_get_system_data_dirs ();
+      for (i = 0; dirs[i]; i++)
+        g_ptr_array_add (desktop_file_dirs, desktop_file_dir_new (dirs[i]));
 
-      g_array_free (tmp, FALSE);
+      /* The list of directories will never change after this, unless
+       * g_get_user_config_dir() changes due to %G_TEST_OPTION_ISOLATE_DIRS. */
+      desktop_file_dirs_config_dir = user_config_dir;
     }
 
-  for (i = 0; i < n_desktop_file_dirs; i++)
-    if (!desktop_file_dirs[i].is_setup)
-      desktop_file_dir_init (&desktop_file_dirs[i]);
+  for (i = 0; i < desktop_file_dirs->len; i++)
+    if (!((DesktopFileDir *) g_ptr_array_index (desktop_file_dirs, i))->is_setup)
+      desktop_file_dir_init (g_ptr_array_index (desktop_file_dirs, i));
 }
 
 static void
@@ -927,19 +1624,23 @@ desktop_file_dirs_unlock (void)
 }
 
 static void
-desktop_file_dirs_refresh (void)
+desktop_file_dirs_invalidate_user_config (void)
 {
-  desktop_file_dirs_lock ();
-  desktop_file_dirs_unlock ();
+  g_mutex_lock (&desktop_file_dir_lock);
+
+  if (desktop_file_dir_user_config != NULL)
+    desktop_file_dir_reset (desktop_file_dir_user_config);
+
+  g_mutex_unlock (&desktop_file_dir_lock);
 }
 
 static void
-desktop_file_dirs_invalidate_user (void)
+desktop_file_dirs_invalidate_user_data (void)
 {
   g_mutex_lock (&desktop_file_dir_lock);
 
-  if (n_desktop_file_dirs)
-    desktop_file_dir_reset (&desktop_file_dirs[0]);
+  if (desktop_file_dir_user_data != NULL)
+    desktop_file_dir_reset (desktop_file_dir_user_data);
 
   g_mutex_unlock (&desktop_file_dir_lock);
 }
@@ -1071,6 +1772,56 @@ binary_from_exec (const char *exec)
   return g_strndup (start, p - start);
 }
 
+/*< internal >
+ * g_desktop_app_info_get_desktop_id_for_filename
+ * @self: #GDesktopAppInfo to get desktop id of
+ *
+ * Tries to find the desktop ID for a particular `.desktop` filename, as per the
+ * [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-
+ * entry-spec/desktop-entry-spec-latest.html#desktop-file-id).
+ *
+ * Returns: desktop id or basename if filename is unknown.
+ */
+static char *
+g_desktop_app_info_get_desktop_id_for_filename (GDesktopAppInfo *self)
+{
+  guint i;
+  gchar *desktop_id = NULL;
+
+  g_return_val_if_fail (self->filename != NULL, NULL);
+
+  for (i = 0; i < desktop_file_dirs->len; i++)
+    {
+      DesktopFileDir *dir = g_ptr_array_index (desktop_file_dirs, i);
+      GHashTable *app_names;
+      GHashTableIter iter;
+      gpointer key, value;
+
+      app_names = dir->app_names;
+
+      if (!app_names)
+        continue;
+
+      g_hash_table_iter_init (&iter, app_names);
+      while (g_hash_table_iter_next (&iter, &key, &value))
+        {
+          if (!strcmp (value, self->filename))
+            {
+              desktop_id = g_strdup (key);
+              break;
+            }
+        }
+
+      if (desktop_id)
+        break;
+    }
+
+  if (!desktop_id)
+    desktop_id = g_path_get_basename (self->filename);
+
+  return g_steal_pointer (&desktop_id);
+}
+
 static gboolean
 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
                                       GKeyFile        *key_file)
@@ -1133,6 +1884,10 @@ g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
       else
         {
           char *t;
+
+          /* Since @exec is not an empty string, there must be at least one
+           * argument, so dereferencing argv[0] should return non-NULL. */
+          g_assert (argc > 0);
           t = g_find_program_in_path (argv[0]);
           g_strfreev (argv);
 
@@ -1222,13 +1977,16 @@ g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
         {
           *last_dot = '\0';
 
-          if (g_dbus_is_interface_name (basename))
+          if (g_dbus_is_name (basename) && basename[0] != ':')
             info->app_id = g_strdup (basename);
         }
 
       g_free (basename);
     }
 
+  if (info->filename)
+    info->desktop_id = g_desktop_app_info_get_desktop_id_for_filename (info);
+
   info->keyfile = g_key_file_ref (key_file);
 
   return TRUE;
@@ -1242,8 +2000,6 @@ g_desktop_app_info_load_file (GDesktopAppInfo *self)
 
   g_return_val_if_fail (self->filename != NULL, FALSE);
 
-  self->desktop_id = g_path_get_basename (self->filename);
-
   key_file = g_key_file_new ();
 
   if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
@@ -1259,7 +2015,7 @@ g_desktop_app_info_load_file (GDesktopAppInfo *self)
  *
  * Creates a new #GDesktopAppInfo.
  *
- * Returns: a new #GDesktopAppInfo or %NULL on error.
+ * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
  *
  * Since: 2.18
  **/
@@ -1270,33 +2026,37 @@ g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
 
   info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
   info->filename = NULL;
+
+  desktop_file_dirs_lock ();
+
   if (!g_desktop_app_info_load_from_keyfile (info, key_file))
-    {
-      g_object_unref (info);
-      return NULL;
-    }
+    g_clear_object (&info);
+
+  desktop_file_dirs_unlock ();
+
   return info;
 }
 
 /**
  * g_desktop_app_info_new_from_filename:
- * @filename: the path of a desktop file, in the GLib filename encoding
+ * @filename: (type filename): the path of a desktop file, in the GLib
+ *      filename encoding
  *
  * Creates a new #GDesktopAppInfo.
  *
- * Returns: a new #GDesktopAppInfo or %NULL on error.
+ * Returns: (nullable): 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;
-    }
+  desktop_file_dirs_lock ();
+
+  info = g_desktop_app_info_new_from_filename_unlocked (filename);
+
+  desktop_file_dirs_unlock ();
+
   return info;
 }
 
@@ -1308,15 +2068,16 @@ g_desktop_app_info_new_from_filename (const char *filename)
  *
  * 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>
+ * 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
- * <filename>/usr/share/applications/kde/foo.desktop</filename>).
+ * `/usr/share/applications/kde/foo.desktop`).
  *
- * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
+ * Returns: (nullable): a new #GDesktopAppInfo, or %NULL if no desktop
+ *     file with that id exists.
  */
 GDesktopAppInfo *
 g_desktop_app_info_new (const char *desktop_id)
@@ -1326,9 +2087,9 @@ g_desktop_app_info_new (const char *desktop_id)
 
   desktop_file_dirs_lock ();
 
-  for (i = 0; i < n_desktop_file_dirs; i++)
+  for (i = 0; i < desktop_file_dirs->len; i++)
     {
-      appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
+      appinfo = desktop_file_dir_get_app (g_ptr_array_index (desktop_file_dirs, i), desktop_id);
 
       if (appinfo)
         break;
@@ -1455,7 +2216,8 @@ g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
  * 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.
+ * Returns: (nullable) (type filename): The full path to the file for @info,
+ *     or %NULL if not known.
  * Since: 2.24
  */
 const char *
@@ -1502,7 +2264,7 @@ g_desktop_app_info_get_icon (GAppInfo *appinfo)
  *
  * Gets the categories from the desktop file.
  *
- * Returns: The unparsed Categories key from the desktop file;
+ * Returns: (nullable): The unparsed Categories key from the desktop file;
  *     i.e. no attempt is made to split it by ';' or validate it.
  */
 const char *
@@ -1531,9 +2293,9 @@ g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
  * g_desktop_app_info_get_generic_name:
  * @info: a #GDesktopAppInfo
  *
- * Gets the generic name from the destkop file.
+ * Gets the generic name from the desktop file.
  *
- * Returns: The value of the GenericName key
+ * Returns: (nullable): The value of the GenericName key
  */
 const char *
 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
@@ -1547,7 +2309,7 @@ g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
  *
  * 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().
+ * %G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
  *
  * Returns: The value of the NoDisplay key
  *
@@ -1562,20 +2324,22 @@ g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
 /**
  * g_desktop_app_info_get_show_in:
  * @info: a #GDesktopAppInfo
- * @desktop_env: a string specifying a desktop name
+ * @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
- * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
+ * `OnlyShowIn` and `NotShowIn` keys.
  *
- * If @desktop_env is %NULL, then the name of the desktop set with
- * g_desktop_app_info_set_desktop_env() is used.
+ * @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
- * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
+ * `OnlyShowIn` and `NotShowIn` keys, %FALSE
  * otherwise.
  *
  * Since: 2.30
@@ -1584,51 +2348,39 @@ gboolean
 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
                                 const gchar     *desktop_env)
 {
-  gboolean found;
-  int i;
+  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) {
-    G_LOCK (g_desktop_env);
-    desktop_env = g_desktop_env;
-    G_UNLOCK (g_desktop_env);
-  }
+  if (desktop_env)
+    envs = specified_envs;
+  else
+    envs = get_current_desktops (NULL);
 
-  if (info->only_show_in)
+  for (i = 0; envs[i]; i++)
     {
-      if (desktop_env == NULL)
-        return FALSE;
+      gint j;
 
-      found = FALSE;
-      for (i = 0; info->only_show_in[i] != NULL; i++)
-        {
-          if (strcmp (info->only_show_in[i], desktop_env) == 0)
-            {
-              found = TRUE;
-              break;
-            }
-        }
-      if (!found)
-        return FALSE;
-    }
+      if (info->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 && desktop_env)
-    {
-      for (i = 0; info->not_show_in[i] != NULL; i++)
-        {
-          if (strcmp (info->not_show_in[i], desktop_env) == 0)
+      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 TRUE;
+  return info->only_show_in == NULL;
 }
 
 /* Launching... {{{2 */
 
 static char *
-expand_macro_single (char macro, char *uri)
+expand_macro_single (char macro, const char *uri)
 {
   GFile *file;
   char *result = NULL;
@@ -1677,6 +2429,29 @@ expand_macro_single (char macro, char *uri)
   return result;
 }
 
+static char *
+expand_macro_uri (char macro, const char *uri, gboolean force_file_uri, char force_file_uri_macro)
+{
+  char *expanded = NULL;
+
+  g_return_val_if_fail (uri != NULL, NULL);
+
+  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);
+    }
+
+  return expanded;
+}
+
 static void
 expand_macro (char              macro,
               GString          *exec,
@@ -1684,10 +2459,10 @@ expand_macro (char              macro,
               GList           **uri_list)
 {
   GList *uris = *uri_list;
-  char *expanded;
+  char *expanded = NULL;
   gboolean force_file_uri;
   char force_file_uri_macro;
-  char *uri;
+  const char *uri;
 
   g_return_if_fail (exec != NULL);
 
@@ -1724,19 +2499,8 @@ expand_macro (char              macro,
       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);
-            }
-
+          expanded = expand_macro_uri (macro, uri,
+                                       force_file_uri, force_file_uri_macro);
           if (expanded)
             {
               g_string_append (exec, expanded);
@@ -1754,20 +2518,8 @@ expand_macro (char              macro,
       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);
-            }
-
+          expanded = expand_macro_uri (macro, uri,
+                                       force_file_uri, force_file_uri_macro);
           if (expanded)
             {
               g_string_append (exec, expanded);
@@ -1837,7 +2589,7 @@ expand_application_parameters (GDesktopAppInfo   *info,
   if (exec_line == NULL)
     {
       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                           _("Desktop file didn't specify Exec field"));
+                           _("Desktop file didnt specify Exec field"));
       return FALSE;
     }
 
@@ -1906,29 +2658,47 @@ prepend_terminal_to_vector (int    *argc,
   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");
+      /* Since 2017, gnome-terminal has preferred `--` over `-x` or `-e`. */
+      term_argv[1] = g_strdup ("--");
     }
   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");
+        check = g_find_program_in_path ("mate-terminal");
       if (check == NULL)
+        check = g_find_program_in_path ("xfce4-terminal");
+      if (check != NULL)
         {
-          check = g_strdup ("xterm");
-          g_warning ("couldn't find a terminal, falling back to xterm");
+          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 ("tilix");
+          if (check == NULL)
+            check = g_find_program_in_path ("konsole");
+          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 ("dtterm");
+          if (check == NULL)
+            check = g_find_program_in_path ("xterm");
+          if (check == NULL)
+            {
+              g_debug ("Couldn’t find a known terminal");
+              g_free (term_argv);
+              return FALSE;
+            }
+          term_argv[0] = check;
+          term_argv[1] = g_strdup ("-e");
         }
-      term_argv[0] = check;
-      term_argv[1] = g_strdup ("-e");
     }
 
   real_argc = term_argc + *argc;
@@ -1972,41 +2742,6 @@ create_files_for_uris (GList *uris)
   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,
@@ -2072,6 +2807,26 @@ notify_desktop_launch (GDBusConnection  *session_bus,
   g_object_unref (msg);
 }
 
+static void
+emit_launch_started (GAppLaunchContext *context,
+                     GDesktopAppInfo   *info,
+                     const gchar       *startup_id)
+{
+  GVariantBuilder builder;
+  GVariant *platform_data = NULL;
+
+  if (startup_id)
+    {
+      g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
+      g_variant_builder_add (&builder, "{sv}",
+                             "startup-notification-id",
+                             g_variant_new_string (startup_id));
+      platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
+    }
+  g_signal_emit_by_name (context, "launch-started", info, platform_data);
+  g_clear_pointer (&platform_data, g_variant_unref);
+}
+
 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
 
 static gboolean
@@ -2085,13 +2840,17 @@ g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
                                            gpointer                    user_setup_data,
                                            GDesktopAppLaunchCallback   pid_callback,
                                            gpointer                    pid_callback_data,
+                                           gint                        stdin_fd,
+                                           gint                        stdout_fd,
+                                           gint                        stderr_fd,
                                            GError                    **error)
 {
   gboolean completed = FALSE;
   GList *old_uris;
+  GList *dup_uris;
+
   char **argv, **envp;
   int argc;
-  ChildSetupData data;
 
   g_return_val_if_fail (info != NULL, FALSE);
 
@@ -2102,20 +2861,27 @@ g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
   else
     envp = g_get_environ ();
 
+  /* The GList* passed to expand_application_parameters() will be modified
+   * internally by expand_macro(), so we need to pass a copy of it instead,
+   * and also use that copy to control the exit condition of the loop below.
+   */
+  dup_uris = uris;
   do
     {
       GPid pid;
       GList *launched_uris;
       GList *iter;
-      char *display, *sn_id;
+      char *sn_id = NULL;
+      char **wrapped_argv;
+      int i;
 
-      old_uris = uris;
-      if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
+      old_uris = dup_uris;
+      if (!expand_application_parameters (info, exec_line, &dup_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)
+      for (iter = old_uris; iter != NULL && iter != dup_uris; iter = iter->next)
         launched_uris = g_list_prepend (launched_uris, iter->data);
       launched_uris = g_list_reverse (launched_uris);
 
@@ -2126,62 +2892,75 @@ g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
           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;
-        }
+        envp = g_environ_setenv (envp,
+                                 "GIO_LAUNCHED_DESKTOP_FILE",
+                                 info->filename,
+                                 TRUE);
 
-      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);
+              if (sn_id)
+                envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
             }
 
           g_list_free_full (launched_files, g_object_unref);
+
+          emit_launch_started (launch_context, info, sn_id);
         }
 
-      if (!g_spawn_async (info->path,
-                          argv,
-                          envp,
-                          spawn_flags,
-                          child_setup,
-                          &data,
-                          &pid,
-                          error))
+      if (g_once_init_enter (&gio_launch_desktop_path))
+        {
+          const gchar *tmp = NULL;
+          gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
+
+          /* Allow test suite to specify path to gio-launch-desktop */
+          if (!is_setuid)
+            tmp = g_getenv ("GIO_LAUNCH_DESKTOP");
+
+          /* Allow build system to specify path to gio-launch-desktop */
+          if (tmp == NULL && g_file_test (GIO_LAUNCH_DESKTOP, G_FILE_TEST_IS_EXECUTABLE))
+            tmp = GIO_LAUNCH_DESKTOP;
+
+          /* Fall back on usual searching in $PATH */
+          if (tmp == NULL)
+            tmp = "gio-launch-desktop";
+          g_once_init_leave (&gio_launch_desktop_path, tmp);
+        }
+
+      wrapped_argv = g_new (char *, argc + 2);
+      wrapped_argv[0] = g_strdup (gio_launch_desktop_path);
+
+      for (i = 0; i < argc; i++)
+        wrapped_argv[i + 1] = g_steal_pointer (&argv[i]);
+
+      wrapped_argv[i + 1] = NULL;
+      g_free (argv);
+      argv = NULL;
+
+      if (!g_spawn_async_with_fds (info->path,
+                                   wrapped_argv,
+                                   envp,
+                                   spawn_flags,
+                                   user_setup,
+                                   user_setup_data,
+                                   &pid,
+                                   stdin_fd,
+                                   stdout_fd,
+                                   stderr_fd,
+                                   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);
 
@@ -2208,18 +2987,17 @@ g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
       notify_desktop_launch (session_bus,
                              info,
                              pid,
-                             display,
+                             NULL,
                              sn_id,
                              launched_uris);
 
-      g_free (display);
       g_free (sn_id);
       g_list_free (launched_uris);
 
-      g_strfreev (argv);
-      argv = NULL;
+      g_strfreev (wrapped_argv);
+      wrapped_argv = NULL;
     }
-  while (uris != NULL);
+  while (dup_uris != NULL);
 
   completed = TRUE;
 
@@ -2231,25 +3009,21 @@ g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo            *info,
 }
 
 static gchar *
-object_path_from_appid (const gchar *app_id)
+object_path_from_appid (const gchar *appid)
 {
-  gchar *path;
-  gint i, n;
-
-  n = strlen (app_id);
-  path = g_malloc (n + 2);
+  gchar *appid_path, *iter;
 
-  path[0] = '/';
-
-  for (i = 0; i < n; i++)
-    if (app_id[i] != '.')
-      path[i + 1] = app_id[i];
-    else
-      path[i + 1] = '/';
+  appid_path = g_strconcat ("/", appid, NULL);
+  for (iter = appid_path; *iter; iter++)
+    {
+      if (*iter == '.')
+        *iter = '/';
 
-  path[i + 1] = '\0';
+      if (*iter == '-')
+        *iter = '_';
+    }
 
-  return path;
+  return appid_path;
 }
 
 static GVariant *
@@ -2270,7 +3044,8 @@ g_desktop_app_info_make_platform_data (GDesktopAppInfo   *info,
           gchar *sn_id;
 
           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));
+          if (sn_id)
+            g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
         }
 
       g_list_free_full (launched_files, g_object_unref);
@@ -2279,16 +3054,78 @@ g_desktop_app_info_make_platform_data (GDesktopAppInfo   *info,
   return g_variant_builder_end (&builder);
 }
 
-static gboolean
-g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo    *info,
-                                          GDBusConnection    *session_bus,
-                                          GList              *uris,
-                                          GAppLaunchContext  *launch_context)
+typedef struct
+{
+  GDesktopAppInfo     *info; /* (owned) */
+  GAppLaunchContext   *launch_context; /* (owned) (nullable) */
+  GAsyncReadyCallback  callback;
+  gchar               *startup_id; /* (owned) */
+  gpointer             user_data;
+} LaunchUrisWithDBusData;
+
+static void
+launch_uris_with_dbus_data_free (LaunchUrisWithDBusData *data)
 {
+  g_clear_object (&data->info);
+  g_clear_object (&data->launch_context);
+  g_free (data->startup_id);
+
+  g_free (data);
+}
+
+static void
+launch_uris_with_dbus_signal_cb (GObject      *object,
+                                 GAsyncResult *result,
+                                 gpointer      user_data)
+{
+  LaunchUrisWithDBusData *data = user_data;
   GVariantBuilder builder;
-  gchar *object_path;
 
-  g_return_val_if_fail (info != NULL, FALSE);
+  if (data->launch_context)
+    {
+      if (g_task_had_error (G_TASK (result)))
+        g_app_launch_context_launch_failed (data->launch_context, data->startup_id);
+      else
+        {
+          GVariant *platform_data;
+
+          g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
+          /* the docs guarantee `pid` will be set, but we can’t
+           * easily know it for a D-Bus process, so set it to zero */
+          g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (0));
+          if (data->startup_id)
+            g_variant_builder_add (&builder, "{sv}",
+                                   "startup-notification-id",
+                                   g_variant_new_string (data->startup_id));
+          platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
+          g_signal_emit_by_name (data->launch_context,
+                                 "launched",
+                                 data->info,
+                                 platform_data);
+          g_variant_unref (platform_data);
+        }
+    }
+
+  if (data->callback)
+    data->callback (object, result, data->user_data);
+
+  launch_uris_with_dbus_data_free (data);
+}
+
+static void
+launch_uris_with_dbus (GDesktopAppInfo    *info,
+                       GDBusConnection    *session_bus,
+                       GList              *uris,
+                       GAppLaunchContext  *launch_context,
+                       GCancellable       *cancellable,
+                       GAsyncReadyCallback callback,
+                       gpointer            user_data)
+{
+  GVariant *platform_data;
+  GVariantBuilder builder;
+  GVariantDict dict;
+  gchar *object_path;
+  LaunchUrisWithDBusData *data;
 
   g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
 
@@ -2302,18 +3139,63 @@ g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo    *info,
       g_variant_builder_close (&builder);
     }
 
-  g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
+  platform_data = 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...
-   */
+  g_variant_builder_add_value (&builder, platform_data);
   object_path = object_path_from_appid (info->app_id);
+
+  data = g_new0 (LaunchUrisWithDBusData, 1);
+  data->info = g_object_ref (info);
+  data->callback = callback;
+  data->user_data = user_data;
+  data->launch_context = launch_context ? g_object_ref (launch_context) : NULL;
+  g_variant_dict_init (&dict, platform_data);
+  g_variant_dict_lookup (&dict, "desktop-startup-id", "s", &data->startup_id);
+
+  if (launch_context)
+    emit_launch_started (launch_context, info, data->startup_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);
+                          NULL, G_DBUS_CALL_FLAGS_NONE, -1,
+                          cancellable, launch_uris_with_dbus_signal_cb, g_steal_pointer (&data));
   g_free (object_path);
 
+  g_variant_dict_clear (&dict);
+}
+
+static gboolean
+g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo    *info,
+                                          GDBusConnection    *session_bus,
+                                          GList              *uris,
+                                          GAppLaunchContext  *launch_context,
+                                          GCancellable       *cancellable,
+                                          GAsyncReadyCallback callback,
+                                          gpointer            user_data)
+{
+  GList *ruris = uris;
+  char *app_id = NULL;
+
+  g_return_val_if_fail (info != NULL, FALSE);
+
+#ifdef G_OS_UNIX
+  app_id = g_desktop_app_info_get_string (info, "X-Flatpak");
+  if (app_id && *app_id)
+    {
+      ruris = g_document_portal_add_documents (uris, app_id, NULL);
+      if (ruris == NULL)
+        ruris = uris;
+    }
+#endif
+
+  launch_uris_with_dbus (info, session_bus, ruris, launch_context,
+                         cancellable, callback, user_data);
+
+  if (ruris != uris)
+    g_list_free_full (ruris, g_free);
+
+  g_free (app_id);
+
   return TRUE;
 }
 
@@ -2326,6 +3208,9 @@ g_desktop_app_info_launch_uris_internal (GAppInfo                   *appinfo,
                                          gpointer                    user_setup_data,
                                          GDesktopAppLaunchCallback   pid_callback,
                                          gpointer                    pid_callback_data,
+                                         gint                        stdin_fd,
+                                         gint                        stdout_fd,
+                                         gint                        stderr_fd,
                                          GError                     **error)
 {
   GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
@@ -2335,11 +3220,17 @@ g_desktop_app_info_launch_uris_internal (GAppInfo                   *appinfo,
   session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
 
   if (session_bus && info->app_id)
-    g_desktop_app_info_launch_uris_with_dbus (info, session_bus, 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...
+     */
+    g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context,
+                                              NULL, NULL, NULL);
   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);
+                                                         pid_callback, pid_callback_data,
+                                                         stdin_fd, stdout_fd, stderr_fd, error);
 
   if (session_bus != NULL)
     {
@@ -2364,9 +3255,146 @@ g_desktop_app_info_launch_uris (GAppInfo           *appinfo,
                                                   launch_context,
                                                   _SPAWN_FLAGS_DEFAULT,
                                                   NULL, NULL, NULL, NULL,
+                                                  -1, -1, -1,
                                                   error);
 }
 
+typedef struct
+{
+  GAppInfo *appinfo;
+  GList *uris;
+  GAppLaunchContext *context;
+} LaunchUrisData;
+
+static void
+launch_uris_data_free (LaunchUrisData *data)
+{
+  g_clear_object (&data->context);
+  g_list_free_full (data->uris, g_free);
+  g_free (data);
+}
+
+static void
+launch_uris_with_dbus_cb (GObject      *object,
+                          GAsyncResult *result,
+                          gpointer      user_data)
+{
+  GTask *task = G_TASK (user_data);
+  GError *error = NULL;
+
+  g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
+  if (error != NULL)
+    {
+      g_dbus_error_strip_remote_error (error);
+      g_task_return_error (task, g_steal_pointer (&error));
+    }
+  else
+    g_task_return_boolean (task, TRUE);
+
+  g_object_unref (task);
+}
+
+static void
+launch_uris_flush_cb (GObject      *object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
+{
+  GTask *task = G_TASK (user_data);
+
+  g_dbus_connection_flush_finish (G_DBUS_CONNECTION (object), result, NULL);
+  g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
+}
+
+static void
+launch_uris_bus_get_cb (GObject      *object,
+                        GAsyncResult *result,
+                        gpointer      user_data)
+{
+  GTask *task = G_TASK (user_data);
+  GDesktopAppInfo *info = G_DESKTOP_APP_INFO (g_task_get_source_object (task));
+  LaunchUrisData *data = g_task_get_task_data (task);
+  GCancellable *cancellable = g_task_get_cancellable (task);
+  GDBusConnection *session_bus;
+  GError *error = NULL;
+
+  session_bus = g_bus_get_finish (result, NULL);
+
+  if (session_bus && info->app_id)
+    {
+      /* FIXME: The g_document_portal_add_documents() function, which is called
+       * from the g_desktop_app_info_launch_uris_with_dbus() function, still
+       * uses blocking calls.
+       */
+      g_desktop_app_info_launch_uris_with_dbus (info, session_bus,
+                                                data->uris, data->context,
+                                                cancellable,
+                                                launch_uris_with_dbus_cb,
+                                                g_steal_pointer (&task));
+    }
+  else
+    {
+      /* FIXME: The D-Bus message from the notify_desktop_launch() function
+       * can be still lost even if flush is called later. See:
+       * https://gitlab.freedesktop.org/dbus/dbus/issues/72
+       */
+      g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec,
+                                                 data->uris, data->context,
+                                                 _SPAWN_FLAGS_DEFAULT, NULL,
+                                                 NULL, NULL, NULL, -1, -1, -1,
+                                                 &error);
+      if (error != NULL)
+        {
+          g_task_return_error (task, g_steal_pointer (&error));
+          g_object_unref (task);
+        }
+      else if (session_bus)
+        g_dbus_connection_flush (session_bus,
+                                 cancellable,
+                                 launch_uris_flush_cb,
+                                 g_steal_pointer (&task));
+      else
+        {
+          g_task_return_boolean (task, TRUE);
+          g_clear_object (&task);
+        }
+    }
+
+  g_clear_object (&session_bus);
+}
+
+static void
+g_desktop_app_info_launch_uris_async (GAppInfo           *appinfo,
+                                      GList              *uris,
+                                      GAppLaunchContext  *context,
+                                      GCancellable       *cancellable,
+                                      GAsyncReadyCallback callback,
+                                      gpointer            user_data)
+{
+  GTask *task;
+  LaunchUrisData *data;
+
+  task = g_task_new (appinfo, cancellable, callback, user_data);
+  g_task_set_source_tag (task, g_desktop_app_info_launch_uris_async);
+
+  data = g_new0 (LaunchUrisData, 1);
+  data->uris = g_list_copy_deep (uris, (GCopyFunc) g_strdup, NULL);
+  data->context = (context != NULL) ? g_object_ref (context) : NULL;
+  g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) launch_uris_data_free);
+
+  g_bus_get (G_BUS_TYPE_SESSION, cancellable, launch_uris_bus_get_cb, task);
+}
+
+static gboolean
+g_desktop_app_info_launch_uris_finish (GAppInfo     *appinfo,
+                                       GAsyncResult *result,
+                                       GError      **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, appinfo), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
+
 static gboolean
 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
 {
@@ -2415,16 +3443,71 @@ g_desktop_app_info_launch (GAppInfo           *appinfo,
 }
 
 /**
+ * g_desktop_app_info_launch_uris_as_manager_with_fds:
+ * @appinfo: a #GDesktopAppInfo
+ * @uris: (element-type utf8): List of URIs
+ * @launch_context: (nullable): a #GAppLaunchContext
+ * @spawn_flags: #GSpawnFlags, used for each process
+ * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
+ *     for each process.
+ * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
+ * @pid_callback: (scope call) (nullable): Callback for child processes
+ * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
+ * @stdin_fd: file descriptor to use for child's stdin, or -1
+ * @stdout_fd: file descriptor to use for child's stdout, or -1
+ * @stderr_fd: file descriptor to use for child's stderr, or -1
+ * @error: return location for a #GError, or %NULL
+ *
+ * Equivalent to g_desktop_app_info_launch_uris_as_manager() but allows
+ * you to pass in file descriptors for the stdin, stdout and stderr streams
+ * of the launched process.
+ *
+ * If application launching occurs via some non-spawn mechanism (e.g. D-Bus
+ * activation) then @stdin_fd, @stdout_fd and @stderr_fd are ignored.
+ *
+ * Returns: %TRUE on successful launch, %FALSE otherwise.
+ *
+ * Since: 2.58
+ */
+gboolean
+g_desktop_app_info_launch_uris_as_manager_with_fds (GDesktopAppInfo            *appinfo,
+                                                    GList                      *uris,
+                                                    GAppLaunchContext          *launch_context,
+                                                    GSpawnFlags                 spawn_flags,
+                                                    GSpawnChildSetupFunc        user_setup,
+                                                    gpointer                    user_setup_data,
+                                                    GDesktopAppLaunchCallback   pid_callback,
+                                                    gpointer                    pid_callback_data,
+                                                    gint                        stdin_fd,
+                                                    gint                        stdout_fd,
+                                                    gint                        stderr_fd,
+                                                    GError                    **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,
+                                                  stdin_fd,
+                                                  stdout_fd,
+                                                  stderr_fd,
+                                                  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
+ * @launch_context: (nullable): a #GAppLaunchContext
  * @spawn_flags: #GSpawnFlags, used for each process
- * @user_setup: (scope call) (allow-none): a #GSpawnChildSetupFunc, used once
+ * @user_setup: (scope async) (nullable): 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
+ * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
+ * @pid_callback: (scope call) (nullable): Callback for child processes
+ * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
  * @error: return location for a #GError, or %NULL
  *
  * This function performs the equivalent of g_app_info_launch_uris(),
@@ -2432,11 +3515,12 @@ g_desktop_app_info_launch (GAppInfo           *appinfo,
  * 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 the application is launched via GSpawn, 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. See g_spawn_async_with_pipes()
+ * for information on certain parameter conditions that can enable an
+ * optimized posix_spawn() codepath to be used.
  *
  * If application launching occurs via some other mechanism (eg: D-Bus
  * activation) then @spawn_flags, @user_setup, @user_setup_data,
@@ -2455,15 +3539,16 @@ g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo            *appinfo,
                                            gpointer                    pid_callback_data,
                                            GError                    **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);
+  return g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
+                                                             uris,
+                                                             launch_context,
+                                                             spawn_flags,
+                                                             user_setup,
+                                                             user_setup_data,
+                                                             pid_callback,
+                                                             pid_callback_data,
+                                                             -1, -1, -1,
+                                                             error);
 }
 
 /* OnlyShowIn API support {{{2 */
@@ -2475,30 +3560,18 @@ g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo            *appinfo,
  * 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>
+ * `OnlyShowIn` and `NotShowIn`
  * desktop entry fields.
  *
- * 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>
- *
  * 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)
 {
-  G_LOCK (g_desktop_env);
-  if (!g_desktop_env)
-    g_desktop_env = g_strdup (desktop_env);
-  G_UNLOCK (g_desktop_env);
+  get_current_desktops (desktop_env);
 }
 
 static gboolean
@@ -2515,6 +3588,7 @@ g_desktop_app_info_should_show (GAppInfo *appinfo)
 /* mime types/default apps support {{{2 */
 
 typedef enum {
+  CONF_DIR,
   APP_DIR,
   MIMETYPE_DIR
 } DirType;
@@ -2526,10 +3600,25 @@ ensure_dir (DirType   type,
   char *path, *display_name;
   int errsv;
 
-  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);
+  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 ();
+    }
+
+  g_debug ("%s: Ensuring %s", G_STRFUNC, path);
 
   errno = 0;
   if (g_mkdir_with_parents (path, 0700) == 0)
@@ -2539,11 +3628,11 @@ ensure_dir (DirType   type,
   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"),
+                 _("Cant 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"),
+                 _("Cant create user MIME configuration folder %s: %s"),
                  display_name, g_strerror (errsv));
 
   g_free (display_name);
@@ -2571,7 +3660,7 @@ update_mimeapps_list (const char  *desktop_id,
   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;
 
@@ -2760,9 +3849,11 @@ update_mimeapps_list (const char  *desktop_id,
   data = g_key_file_to_data (key_file, &data_size, error);
   g_key_file_free (key_file);
 
-  res = g_file_set_contents (filename, data, data_size, error);
+  res = g_file_set_contents_full (filename, data, data_size,
+                                  G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
+                                  0600, error);
 
-  mime_info_cache_reload (NULL);
+  desktop_file_dirs_invalidate_user_config ();
 
   g_free (filename);
   g_free (data);
@@ -2822,7 +3913,7 @@ update_program_done (GPid     pid,
                      gpointer data)
 {
   /* Did the application exit correctly */
-  if (g_spawn_check_exit_status (status, NULL))
+  if (g_spawn_check_wait_status (status, NULL))
     {
       /* Here we could clean out any caches in use */
     }
@@ -2862,6 +3953,7 @@ run_update_command (char *command,
              * chance of debugging it.
              */
             g_warning ("%s", error->message);
+            g_error_free (error);
           }
 
         g_free (argv[1]);
@@ -2903,7 +3995,9 @@ g_desktop_app_info_set_as_default_for_extension (GAppInfo    *appinfo,
                          " </mime-type>\n"
                          "</mime-info>\n", mimetype, extension, extension);
 
-      g_file_set_contents (filename, contents, -1, NULL);
+      g_file_set_contents_full (filename, contents, -1,
+                                G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
+                                0600, NULL);
       g_free (contents);
 
       run_update_command ("update-mime-database", "mime");
@@ -3040,7 +4134,7 @@ g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
 
       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);
+                   _("Cant create user desktop file %s"), display_name);
       g_free (display_name);
       g_free (filename);
       g_free (data);
@@ -3052,7 +4146,9 @@ g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
   /* FIXME - actually handle error */
   (void) g_close (fd, NULL);
 
-  res = g_file_set_contents (filename, data, data_size, error);
+  res = g_file_set_contents_full (filename, data, data_size,
+                                  G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING,
+                                  0600, error);
   g_free (data);
   if (!res)
     {
@@ -3073,7 +4169,7 @@ g_desktop_app_info_ensure_saved (GDesktopAppInfo  *info,
    * This means that calls directly following this will be able to see
    * the results immediately.
    */
-  desktop_file_dirs_invalidate_user ();
+  desktop_file_dirs_invalidate_user_data ();
 
   return TRUE;
 }
@@ -3120,16 +4216,16 @@ g_desktop_app_info_delete (GAppInfo *appinfo)
 /* 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
+ * @commandline: (type filename): the commandline to use
+ * @application_name: (nullable): 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
+ * [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.
  *
@@ -3195,6 +4291,8 @@ g_desktop_app_info_iface_init (GAppInfoIface *iface)
   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->launch_uris_async = g_desktop_app_info_launch_uris_async;
+  iface->launch_uris_finish = g_desktop_app_info_launch_uris_finish;
   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;
@@ -3211,17 +4309,79 @@ g_desktop_app_info_iface_init (GAppInfoIface *iface)
 
 /* Recommended applications {{{2 */
 
-static gboolean
-app_info_in_list (GAppInfo *info,
-                  GList    *list)
+/* 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)
 {
-  while (list != NULL)
+  gchar *unaliased;
+  GPtrArray *array;
+
+  array = g_ptr_array_new ();
+  unaliased = _g_unix_content_type_unalias (content_type);
+  g_ptr_array_add (array, unaliased);
+
+  if (include_fallback)
     {
-      if (g_app_info_equal (info, list->data))
-        return TRUE;
-      list = list->next;
+      guint i;
+
+      /* 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);
+        }
     }
-  return FALSE;
+
+  g_ptr_array_add (array, NULL);
+
+  return (gchar **) g_ptr_array_free (array, FALSE);
+}
+
+static gchar **
+g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
+                                                     gboolean     include_fallback)
+{
+  GPtrArray *hits, *blocklist;
+  gchar **types;
+  guint i, j;
+
+  hits = g_ptr_array_new ();
+  blocklist = g_ptr_array_new ();
+
+  types = get_list_of_mimetypes (content_type, include_fallback);
+
+  desktop_file_dirs_lock ();
+
+  for (i = 0; types[i]; i++)
+    for (j = 0; j < desktop_file_dirs->len; j++)
+      desktop_file_dir_mime_lookup (g_ptr_array_index (desktop_file_dirs, j), types[i], hits, blocklist);
+
+  /* We will keep the hits past unlocking, so we must dup them */
+  for (i = 0; i < hits->len; i++)
+    hits->pdata[i] = g_strdup (hits->pdata[i]);
+
+  desktop_file_dirs_unlock ();
+
+  g_ptr_array_add (hits, NULL);
+
+  g_ptr_array_free (blocklist, TRUE);
+  g_strfreev (types);
+
+  return (gchar **) g_ptr_array_free (hits, FALSE);
 }
 
 /**
@@ -3243,31 +4403,25 @@ app_info_in_list (GAppInfo *info,
 GList *
 g_app_info_get_recommended_for_type (const gchar *content_type)
 {
-  GList *desktop_entries, *l;
+  gchar **desktop_ids;
   GList *infos;
-  GDesktopAppInfo *info;
+  gint i;
 
   g_return_val_if_fail (content_type != NULL, NULL);
 
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
+  desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
 
   infos = NULL;
-  for (l = desktop_entries; l != NULL; l = l->next)
+  for (i = 0; desktop_ids[i]; i++)
     {
-      char *desktop_entry = l->data;
+      GDesktopAppInfo *info;
 
-      info = g_desktop_app_info_new (desktop_entry);
+      info = g_desktop_app_info_new (desktop_ids[i]);
       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);
+        infos = g_list_prepend (infos, info);
     }
 
-  g_list_free (desktop_entries);
+  g_strfreev (desktop_ids);
 
   return g_list_reverse (infos);
 }
@@ -3288,34 +4442,38 @@ g_app_info_get_recommended_for_type (const gchar *content_type)
 GList *
 g_app_info_get_fallback_for_type (const gchar *content_type)
 {
-  GList *desktop_entries, *l;
-  GList *infos, *recommended_infos;
-  GDesktopAppInfo *info;
+  gchar **recommended_ids;
+  gchar **all_ids;
+  GList *infos;
+  gint i;
 
   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);
+  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);
 
   infos = NULL;
-  for (l = desktop_entries; l != NULL; l = l->next)
+  for (i = 0; all_ids[i]; i++)
     {
-      char *desktop_entry = l->data;
+      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 (recommended_ids[j])
+        continue;
+
+      info = g_desktop_app_info_new (all_ids[i]);
 
-      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);
+        infos = g_list_prepend (infos, info);
     }
 
-  g_list_free (desktop_entries);
-  g_list_free_full (recommended_infos, g_object_unref);
+  g_strfreev (recommended_ids);
+  g_strfreev (all_ids);
 
   return g_list_reverse (infos);
 }
@@ -3335,43 +4493,25 @@ g_app_info_get_fallback_for_type (const gchar *content_type)
 GList *
 g_app_info_get_all_for_type (const char *content_type)
 {
-  GList *desktop_entries, *l;
+  gchar **desktop_ids;
   GList *infos;
-  char *user_default = NULL;
-  GDesktopAppInfo *info;
+  gint i;
 
   g_return_val_if_fail (content_type != NULL, NULL);
 
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
-  infos = NULL;
-
-  /* put the user default in front of the list, for compatibility */
-  if (user_default != NULL)
-    {
-      info = g_desktop_app_info_new (user_default);
-
-      if (info != NULL)
-        infos = g_list_prepend (infos, info);
-    }
+  desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
 
-  g_free (user_default);
-
-  for (l = desktop_entries; l != NULL; l = l->next)
+  infos = NULL;
+  for (i = 0; desktop_ids[i]; i++)
     {
-      char *desktop_entry = l->data;
+      GDesktopAppInfo *info;
 
-      info = g_desktop_app_info_new (desktop_entry);
+      info = g_desktop_app_info_new (desktop_ids[i]);
       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);
+        infos = g_list_prepend (infos, info);
     }
 
-  g_list_free (desktop_entries);
+  g_strfreev (desktop_ids);
 
   return g_list_reverse (infos);
 }
@@ -3404,66 +4544,70 @@ g_app_info_reset_type_associations (const char *content_type)
  *
  * Gets the default #GAppInfo for a given content type.
  *
- * Returns: (transfer full): #GAppInfo for given @content_type or
+ * Returns: (transfer full) (nullable): #GAppInfo for given @content_type or
  *     %NULL on error.
  */
 GAppInfo *
 g_app_info_get_default_for_type (const char *content_type,
                                  gboolean    must_support_uris)
 {
-  GList *desktop_entries, *l;
-  char *user_default = NULL;
+  GPtrArray *blocklist;
+  GPtrArray *results;
   GAppInfo *info;
+  gchar **types;
+  guint i, j, k;
 
   g_return_val_if_fail (content_type != NULL, NULL);
 
-  desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
+  types = get_list_of_mimetypes (content_type, TRUE);
 
+  blocklist = g_ptr_array_new ();
+  results = g_ptr_array_new ();
   info = NULL;
 
-  if (user_default != NULL)
+  desktop_file_dirs_lock ();
+
+  for (i = 0; types[i]; i++)
     {
-      info = (GAppInfo *) g_desktop_app_info_new (user_default);
+      /* Collect all the default apps for this type */
+      for (j = 0; j < desktop_file_dirs->len; j++)
+        desktop_file_dir_default_lookup (g_ptr_array_index (desktop_file_dirs, j), types[i], results);
 
-      if (info)
+      /* Consider the associations as well... */
+      for (j = 0; j < desktop_file_dirs->len; j++)
+        desktop_file_dir_mime_lookup (g_ptr_array_index (desktop_file_dirs, j), types[i], results, blocklist);
+
+      /* (If any), see if one of those apps is installed... */
+      for (j = 0; j < results->len; j++)
         {
-          if (must_support_uris && !g_app_info_supports_uris (info))
+          const gchar *desktop_id = g_ptr_array_index (results, j);
+
+          for (k = 0; k < desktop_file_dirs->len; k++)
             {
-              g_object_unref (info);
-              info = NULL;
+              info = (GAppInfo *) desktop_file_dir_get_app (g_ptr_array_index (desktop_file_dirs, k), desktop_id);
+
+              if (info)
+                {
+                  if (!must_support_uris || g_app_info_supports_uris (info))
+                    goto out;
+
+                  g_clear_object (&info);
+                }
             }
         }
-    }
-
-  g_free (user_default);
 
-  if (info != NULL)
-    {
-      g_list_free_full (desktop_entries, g_free);
-      return info;
+      /* Reset the list, ready to try again with the next (parent)
+       * mimetype, but keep the blocklist in place.
+       */
+      g_ptr_array_set_size (results, 0);
     }
 
-  /* pick the first from the other list that matches our URI
-   * requirements.
-   */
-  for (l = desktop_entries; l != NULL; l = l->next)
-    {
-      char *desktop_entry = l->data;
-
-      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;
-        }
-    }
+out:
+  desktop_file_dirs_unlock ();
 
-  g_list_free_full (desktop_entries, g_free);
+  g_ptr_array_unref (blocklist);
+  g_ptr_array_unref (results);
+  g_strfreev (types);
 
   return info;
 }
@@ -3477,7 +4621,8 @@ g_app_info_get_default_for_type (const char *content_type,
  * 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.
+ * Returns: (transfer full) (nullable): #GAppInfo for given @uri_scheme or
+ *     %NULL on error.
  */
 GAppInfo *
 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
@@ -3485,6 +4630,8 @@ g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
   GAppInfo *app_info;
   char *content_type, *scheme_down;
 
+  g_return_val_if_fail (uri_scheme != NULL && *uri_scheme != '\0', NULL);
+
   scheme_down = g_ascii_strdown (uri_scheme, -1);
   content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
   g_free (scheme_down);
@@ -3497,6 +4644,55 @@ g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
 /* "Get all" API {{{2 */
 
 /**
+ * g_desktop_app_info_get_implementations:
+ * @interface: the name of the interface
+ *
+ * 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)
+{
+  GList *result = NULL;
+  GList **ptr;
+  guint i;
+
+  desktop_file_dirs_lock ();
+
+  for (i = 0; i < desktop_file_dirs->len; i++)
+    desktop_file_dir_get_implementations (g_ptr_array_index (desktop_file_dirs, i), &result, interface);
+
+  desktop_file_dirs_unlock ();
+
+  ptr = &result;
+  while (*ptr)
+    {
+      gchar *name = (*ptr)->data;
+      GDesktopAppInfo *app;
+
+      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 result;
+}
+
+/**
  * g_desktop_app_info_search:
  * @search_string: the search string to use
  *
@@ -3509,6 +4705,13 @@ g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
  * The algorithm for determining matches is undefined and may change at
  * any time.
  *
+ * None of the search results are subjected to the normal validation
+ * checks performed by g_desktop_app_info_new() (for example, checking that
+ * the executable referenced by a result exists), and so it is possible for
+ * g_desktop_app_info_new() to return %NULL when passed an app ID returned by
+ * this function. It is expected that calling code will do this when
+ * subsequently creating a #GDesktopAppInfo for each result.
+ *
  * 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().
@@ -3522,6 +4725,7 @@ g_desktop_app_info_search (const gchar *search_string)
   gint n_categories = 0;
   gint start_of_category;
   gint i, j;
+  guint k;
 
   search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
 
@@ -3529,11 +4733,11 @@ g_desktop_app_info_search (const gchar *search_string)
 
   reset_total_search_results ();
 
-  for (i = 0; i < n_desktop_file_dirs; i++)
+  for (k = 0; k < desktop_file_dirs->len; k++)
     {
       for (j = 0; search_tokens[j]; j++)
         {
-          desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
+          desktop_file_dir_search (g_ptr_array_index (desktop_file_dirs, k), search_tokens[j]);
           merge_token_results (j == 0);
         }
       merge_directory_results ();
@@ -3588,13 +4792,12 @@ g_desktop_app_info_search (const gchar *search_string)
  * 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().
+ * `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 <literal>Hidden</literal> key set.
+ * the `Hidden` key set.
  *
- * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
+ * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
  **/
 GList *
 g_app_info_get_all (void)
@@ -3602,15 +4805,15 @@ g_app_info_get_all (void)
   GHashTable *apps;
   GHashTableIter iter;
   gpointer value;
-  int i;
+  guint i;
   GList *infos;
 
   apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
   desktop_file_dirs_lock ();
 
-  for (i = 0; i < n_desktop_file_dirs; i++)
-    desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
+  for (i = 0; i < desktop_file_dirs->len; i++)
+    desktop_file_dir_get_all (g_ptr_array_index (desktop_file_dirs, i), apps);
 
   desktop_file_dirs_unlock ();
 
@@ -3627,729 +4830,17 @@ g_app_info_get_all (void)
   return infos;
 }
 
-/* Caching of mimeinfo.cache and defaults.list files {{{2 */
-
-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);
-
-static void
-destroy_info_cache_value (gpointer  key,
-                          GList    *value,
-                          gpointer  data)
-{
-  g_list_free_full (value, g_free);
-}
-
-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);
-}
-
-static gboolean
-mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
-                                 const char       *cache_file,
-                                 time_t           *timestamp)
-{
-  struct stat buf;
-  char *filename;
-
-  filename = g_build_filename (dir->path, cache_file, NULL);
-
-  if (g_stat (filename, &buf) < 0)
-    {
-      g_free (filename);
-      return TRUE;
-    }
-  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)
-{
-  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);
-
-  key_file = g_key_file_new ();
-
-  filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
-
-  if (g_stat (filename, &buf) < 0)
-    goto error;
-
-  dir->mime_info_cache_timestamp = buf.st_mtime;
-
-  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
-
-  g_free (filename);
-  filename = NULL;
-
-  if (load_error != NULL)
-    goto error;
-
-  mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
-                                    NULL, &load_error);
-
-  if (load_error != NULL)
-    goto error;
-
-  for (i = 0; mime_types[i] != NULL; i++)
-    {
-      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);
-    }
-
-  g_strfreev (mime_types);
-  g_key_file_free (key_file);
-
-  return;
- error:
-  g_free (filename);
-  g_key_file_free (key_file);
-
-  if (mime_types != NULL)
-    g_strfreev (mime_types);
-
-  if (load_error)
-    g_error_free (load_error);
-}
-
-static void
-mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
-{
-  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;
-
-  dir->defaults_list_timestamp = buf.st_mtime;
-
-  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
-  g_free (filename);
-  filename = NULL;
-
-  if (load_error != NULL)
-    goto error;
-
-  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 == NULL)
-            continue;
-
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->defaults_list_map,
-                                unaliased_type,
-                                desktop_file_ids);
-        }
-
-      g_strfreev (mime_types);
-    }
-
-  g_key_file_free (key_file);
-  return;
-
- error:
-  g_free (filename);
-  g_key_file_free (key_file);
-
-  if (mime_types != NULL)
-    g_strfreev (mime_types);
-
-  if (load_error)
-    g_error_free (load_error);
-}
-
-static void
-mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
-{
-  GKeyFile *key_file;
-  GError *load_error;
-  gchar *filename, **mime_types;
-  char *unaliased_type;
-  char **desktop_file_ids;
-  char *desktop_id;
-  int i;
-  struct stat buf;
-
-  load_error = NULL;
-  mime_types = NULL;
-
-  if (dir->mimeapps_list_added_map != NULL &&
-      !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
-                                        &dir->mimeapps_list_timestamp))
-    return;
-
-  if (dir->mimeapps_list_added_map != NULL)
-    g_hash_table_destroy (dir->mimeapps_list_added_map);
-  dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                                        g_free, (GDestroyNotify)g_strfreev);
-
-  if (dir->mimeapps_list_removed_map != NULL)
-    g_hash_table_destroy (dir->mimeapps_list_removed_map);
-  dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                                          g_free, (GDestroyNotify)g_strfreev);
-
-  if (dir->mimeapps_list_defaults_map != NULL)
-    g_hash_table_destroy (dir->mimeapps_list_defaults_map);
-  dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                                           g_free, g_free);
-
-  key_file = g_key_file_new ();
-
-  filename = g_build_filename (dir->path, "mimeapps.list", NULL);
-  if (g_stat (filename, &buf) < 0)
-    goto error;
-
-  dir->mimeapps_list_timestamp = buf.st_mtime;
-
-  g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
-  g_free (filename);
-  filename = NULL;
-
-  if (load_error != NULL)
-    goto error;
-
-  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;
-
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->mimeapps_list_added_map,
-                                unaliased_type,
-                                desktop_file_ids);
-        }
-
-      g_strfreev (mime_types);
-    }
-
-  mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
-                                    NULL, NULL);
-  if (mime_types != NULL)
-    {
-      for (i = 0; mime_types[i] != NULL; i++)
-        {
-          desktop_file_ids = g_key_file_get_string_list (key_file,
-                                                         REMOVED_ASSOCIATIONS_GROUP,
-                                                         mime_types[i],
-                                                         NULL,
-                                                         NULL);
-          if (desktop_file_ids == NULL)
-            continue;
-
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->mimeapps_list_removed_map,
-                                unaliased_type,
-                                desktop_file_ids);
-        }
-
-      g_strfreev (mime_types);
-    }
-
-  mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
-                                    NULL, NULL);
-  if (mime_types != NULL)
-    {
-      for (i = 0; mime_types[i] != NULL; i++)
-        {
-          desktop_id = g_key_file_get_string (key_file,
-                                              DEFAULT_APPLICATIONS_GROUP,
-                                              mime_types[i],
-                                              NULL);
-          if (desktop_id == NULL)
-            continue;
-
-          unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
-          g_hash_table_replace (dir->mimeapps_list_defaults_map,
-                                unaliased_type,
-                                desktop_id);
-        }
-
-      g_strfreev (mime_types);
-    }
-
-  g_key_file_free (key_file);
-  return;
-
- error:
-  g_free (filename);
-  g_key_file_free (key_file);
-
-  if (mime_types != NULL)
-    g_strfreev (mime_types);
-
-  if (load_error)
-    g_error_free (load_error);
-}
-
-static MimeInfoCacheDir *
-mime_info_cache_dir_new (const char *path)
-{
-  MimeInfoCacheDir *dir;
-
-  dir = g_new0 (MimeInfoCacheDir, 1);
-  dir->path = g_strdup (path);
-
-  return dir;
-}
-
-static void
-mime_info_cache_dir_free (MimeInfoCacheDir *dir)
-{
-  if (dir == NULL)
-    return;
-
-  if (dir->mime_info_cache_map != NULL)
-    {
-      destroy_info_cache_map (dir->mime_info_cache_map);
-      dir->mime_info_cache_map = NULL;
-
-  }
-
-  if (dir->defaults_list_map != NULL)
-    {
-      g_hash_table_destroy (dir->defaults_list_map);
-      dir->defaults_list_map = NULL;
-    }
-
-  if (dir->mimeapps_list_added_map != NULL)
-    {
-      g_hash_table_destroy (dir->mimeapps_list_added_map);
-      dir->mimeapps_list_added_map = NULL;
-    }
-
-  if (dir->mimeapps_list_removed_map != NULL)
-    {
-      g_hash_table_destroy (dir->mimeapps_list_removed_map);
-      dir->mimeapps_list_removed_map = NULL;
-    }
-
-  if (dir->mimeapps_list_defaults_map != NULL)
-    {
-      g_hash_table_destroy (dir->mimeapps_list_defaults_map);
-      dir->mimeapps_list_defaults_map = NULL;
-    }
-
-  g_free (dir);
-}
-
-static void
-mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir  *dir,
-                                         const char        *mime_type,
-                                         char             **new_desktop_file_ids)
-{
-  GList *desktop_file_ids;
-  int i;
-
-  desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
-                                          mime_type);
-
-  for (i = 0; new_desktop_file_ids[i] != NULL; 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]));
-    }
-
-  g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
-}
-
-static void
-mime_info_cache_init_dir_lists (void)
-{
-  int i;
-
-  mime_info_cache = mime_info_cache_new ();
-
-  desktop_file_dirs_refresh ();
-
-  for (i = 0; i < n_desktop_file_dirs; i++)
-    {
-      MimeInfoCacheDir *dir;
-
-      dir = mime_info_cache_dir_new (desktop_file_dirs[i].path);
-
-      if (dir != NULL)
-        {
-          mime_info_cache_dir_init (dir);
-          mime_info_cache_dir_init_defaults_list (dir);
-          mime_info_cache_dir_init_mimeapps_list (dir);
-
-          mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
-        }
-    }
-}
-
-static void
-mime_info_cache_update_dir_lists (void)
-{
-  GList *tmp;
-
-  tmp = mime_info_cache->dirs;
-
-  while (tmp != 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);
-
-      tmp = tmp->next;
-    }
-}
-
-static void
-mime_info_cache_init (void)
-{
-  G_LOCK (mime_info_cache);
-  if (mime_info_cache == NULL)
-    mime_info_cache_init_dir_lists ();
-  else
-    {
-      time_t now;
-
-      time (&now);
-      if (now >= mime_info_cache->last_stat_time + 10)
-        {
-          mime_info_cache_update_dir_lists ();
-          mime_info_cache->last_stat_time = now;
-        }
-    }
-
-  G_UNLOCK (mime_info_cache);
-}
-
-static MimeInfoCache *
-mime_info_cache_new (void)
-{
-  MimeInfoCache *cache;
-
-  cache = g_new0 (MimeInfoCache, 1);
-
-  return cache;
-}
-
-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);
-}
-
-/**
- * mime_info_cache_reload:
- * @dir: directory path which needs reloading.
- *
- * Reload the mime information for the @dir.
- */
-static void
-mime_info_cache_reload (const char *dir)
-{
-  /* FIXME: just reload the dir that needs reloading,
-   * don't blow the whole cache
-   */
-  if (mime_info_cache != NULL)
-    {
-      G_LOCK (mime_info_cache);
-      mime_info_cache_free (mime_info_cache);
-      mime_info_cache = NULL;
-      G_UNLOCK (mime_info_cache);
-    }
-}
-
-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));
-
-  return list;
-}
+/* GDesktopAppInfoLookup interface {{{2 */
 
 /**
- * get_all_desktop_entries_for_mime_type:
- * @mime_type: a mime type.
- * @except: NULL or a strv list
+ * GDesktopAppInfoLookup:
  *
- * 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.
+ * #GDesktopAppInfoLookup is an opaque data structure and can only be accessed
+ * using the following functions.
  *
- * Optionally doesn't list the desktop ids given in the @except
- *
- * Return value: a #GList containing the desktop ids which claim
- *    to handle @mime_type.
- */
-static GList *
-get_all_desktop_entries_for_mime_type (const char  *base_mime_type,
-                                       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;
-
-  mime_info_cache_init ();
-
-  if (include_fallback)
-    {
-      /* collect all ancestors */
-      mime_types = _g_unix_content_type_get_parents (base_mime_type);
-      array = g_ptr_array_new ();
-      for (i = 0; mime_types[i]; i++)
-        g_ptr_array_add (array, mime_types[i]);
-      g_free (mime_types);
-      for (i = 0; i < array->len; i++)
-        {
-          anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
-          for (j = 0; anc[j]; j++)
-            {
-              for (k = 0; k < array->len; k++)
-                {
-                  if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
-                    break;
-                }
-              if (k == array->len) /* not found */
-                g_ptr_array_add (array, g_strdup (anc[j]));
-            }
-          g_strfreev (anc);
-        }
-      g_ptr_array_add (array, NULL);
-      mime_types = (char **)g_ptr_array_free (array, FALSE);
-    }
-  else
-    {
-      mime_types = g_malloc0 (2 * sizeof (gchar *));
-      mime_types[0] = g_strdup (base_mime_type);
-      mime_types[1] = NULL;
-    }
-
-  G_LOCK (mime_info_cache);
-
-  removed_entries = NULL;
-  desktop_entries = NULL;
-
-  for (i = 0; except != NULL && except[i] != NULL; i++)
-    removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
-
-  for (i = 0; mime_types[i] != NULL; i++)
-    {
-      mime_type = mime_types[i];
-
-      /* 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);
-
-      /* 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);
-                }
-            }
-
-          /* Then added associations from mimeapps.list */
-          default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
-          for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
-            desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
-
-          /* Then removed associations from mimeapps.list */
-          removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
-          for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
-            removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
-
-          /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
-          default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
-          for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
-            {
-              if (default_entry == NULL && old_default_entry == NULL && !already_found_handler)
-                old_default_entry = g_strdup (default_entries[j]);
-
-              desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
-            }
-        }
-
-      /* Go through all entries that support the mimetype */
-      for (dir_list = mime_info_cache->dirs;
-           dir_list != NULL;
-           dir_list = dir_list->next)
-        {
-          dir = dir_list->data;
-
-          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_UNLOCK (mime_info_cache);
-
-  g_strfreev (mime_types);
-
-  /* If we have no default from mimeapps.list, take it from
-   * defaults.list intead.
-   *
-   * If we do have a default from mimeapps.list, free any one that came
-   * from defaults.list.
-   */
-  if (default_entry == NULL)
-    default_entry = old_default_entry;
-  else
-    g_free (old_default_entry);
-
-  if (explicit_default != NULL)
-    *explicit_default = default_entry;
-  else
-    g_free (default_entry);
-
-  g_list_free_full (removed_entries, g_free);
-
-  desktop_entries = g_list_reverse (desktop_entries);
-
-  return desktop_entries;
-}
-
-/* GDesktopAppInfoLookup interface {{{2 */
+ * Deprecated: 2.28: The #GDesktopAppInfoLookup interface is deprecated and
+ *    unused by GIO.
+ **/
 
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 
@@ -4369,17 +4860,19 @@ g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
  * @uri_scheme: a string containing a URI scheme.
  *
  * Gets the default application for launching applications
- * using this URI scheme for a particular GDesktopAppInfoLookup
+ * using this URI scheme for a particular #GDesktopAppInfoLookup
  * implementation.
  *
- * The GDesktopAppInfoLookup interface and this function is used
+ * The #GDesktopAppInfoLookup interface and this function is used
  * to implement g_app_info_get_default_for_uri_scheme() backends
  * in a GIO module. There is no reason for applications to use it
  * directly. Applications should use g_app_info_get_default_for_uri_scheme().
  *
- * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
+ * Returns: (transfer full) (nullable): #GAppInfo for given @uri_scheme or
+ *    %NULL on error.
  *
- * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
+ * Deprecated: 2.28: The #GDesktopAppInfoLookup interface is deprecated and
+ *    unused by GIO.
  */
 GAppInfo *
 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
@@ -4406,7 +4899,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
  * WM_CLASS property of the main window of the application, if launched
  * through @info.
  *
- * Returns: (transfer none): the startup WM class, or %NULL if none is set
+ * Returns: (nullable) (transfer none): the startup WM class, or %NULL if none is set
  * in the desktop file.
  *
  * Since: 2.34
@@ -4428,7 +4921,7 @@ g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
  *
  * The @key is looked up in the "Desktop Entry" group.
  *
- * Returns: a newly allocated string, or %NULL if the key
+ * Returns: (nullable): a newly allocated string, or %NULL if the key
  *     is not found
  *
  * Since: 2.36
@@ -4444,6 +4937,33 @@ g_desktop_app_info_get_string (GDesktopAppInfo *info,
 }
 
 /**
+ * g_desktop_app_info_get_locale_string:
+ * @info: a #GDesktopAppInfo
+ * @key: the key to look up
+ *
+ * Looks up a localized string value in the keyfile backing @info
+ * translated to the current locale.
+ *
+ * The @key is looked up in the "Desktop Entry" group.
+ *
+ * Returns: (nullable): a newly allocated string, or %NULL if the key
+ *     is not found
+ *
+ * Since: 2.56
+ */
+char *
+g_desktop_app_info_get_locale_string (GDesktopAppInfo *info,
+                                      const char      *key)
+{
+  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
+  g_return_val_if_fail (key != NULL && *key != '\0', NULL);
+
+  return g_key_file_get_locale_string (info->keyfile,
+                                       G_KEY_FILE_DESKTOP_GROUP,
+                                       key, NULL, NULL);
+}
+
+/**
  * g_desktop_app_info_get_boolean:
  * @info: a #GDesktopAppInfo
  * @key: the key to look up
@@ -4468,6 +4988,33 @@ g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
 }
 
 /**
+ * g_desktop_app_info_get_string_list:
+ * @info: a #GDesktopAppInfo
+ * @key: the key to look up
+ * @length: (out) (optional): return location for the number of returned strings, or %NULL
+ *
+ * Looks up a string list value in the keyfile backing @info.
+ *
+ * The @key is looked up in the "Desktop Entry" group.
+ *
+ * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
+ *  a %NULL-terminated string array or %NULL if the specified
+ *  key cannot be found. The array should be freed with g_strfreev().
+ *
+ * Since: 2.60
+ */
+gchar **
+g_desktop_app_info_get_string_list (GDesktopAppInfo *info,
+                                    const char      *key,
+                                    gsize           *length)
+{
+  g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
+
+  return g_key_file_get_string_list (info->keyfile,
+                                     G_KEY_FILE_DESKTOP_GROUP, key, length, NULL);
+}
+
+/**
  * g_desktop_app_info_has_key:
  * @info: a #GDesktopAppInfo
  * @key: the key to look up
@@ -4477,7 +5024,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,
@@ -4573,7 +5120,7 @@ g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
  * @info: a #GDesktopAppInfo
  * @action_name: the name of the action as from
  *   g_desktop_app_info_list_actions()
- * @launch_context: (allow-none): a #GAppLaunchContext
+ * @launch_context: (nullable): a #GAppLaunchContext
  *
  * Activates the named application action.
  *
@@ -4629,7 +5176,10 @@ g_desktop_app_info_launch_action (GDesktopAppInfo   *info,
 
       if (exec_line)
         g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
-                                                   _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
+                                                   _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL,
+                                                   -1, -1, -1, NULL);
+
+      g_free (exec_line);
     }
 
   if (session_bus != NULL)