[glib/wip/oholy/gappinfo-async: 5/5] gio-tool-open: Use g_app_info_launch_default_for_uri_async()



commit 33021040047426c95f3e16538651f51c30605345
Author: Ondrej Holy <oholy redhat com>
Date:   Tue Jan 22 16:29:20 2019 +0100

    gio-tool-open: Use g_app_info_launch_default_for_uri_async()
    
    The recent changes of the g_app_info_launch_default_for_uri_async()
    function ensures that the callback is not called before DBus-activated
    applications start. Let's use g_app_info_launch_default_for_uri_async()
    and remove the workarounds for DBus-activated applications.
    
    Closes: https://gitlab.gnome.org/GNOME/glib/issues/1249

 gio/gio-tool-open.c | 123 +++++++++++-----------------------------------------
 1 file changed, 26 insertions(+), 97 deletions(-)
---
diff --git a/gio/gio-tool-open.c b/gio/gio-tool-open.c
index 73863c7c5..fd07e8ac5 100644
--- a/gio/gio-tool-open.c
+++ b/gio/gio-tool-open.c
@@ -29,73 +29,32 @@
 
 #include "gio-tool.h"
 
+static int n_outstanding = 0;
+static gboolean success = TRUE;
 
 static const GOptionEntry entries[] = {
   { NULL }
 };
 
-#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
-static gboolean
-get_bus_name_and_path_from_uri (const char *uri,
-                                char **bus_name_out,
-                                char **object_path_out)
+static void
+launch_default_for_uri_cb (GObject *source_object,
+                           GAsyncResult *res,
+                           gpointer user_data)
 {
-  GAppInfo *app_info = NULL;
-  char *bus_name = NULL;
-  char *object_path = NULL;
-  char *uri_scheme;
-  const char *filename;
-  char *basename = NULL;
-  char *p;
-  gboolean got_name = FALSE;
-
-  uri_scheme = g_uri_parse_scheme (uri);
-  if (uri_scheme && uri_scheme[0] != '\0')
-    app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
-  g_free (uri_scheme);
-
-  if (app_info == NULL)
-    {
-      GFile *file;
+  GError *error = NULL;
+  gchar *uri = user_data;
 
-      file = g_file_new_for_uri (uri);
-      app_info = g_file_query_default_handler (file, NULL, NULL);
-      g_object_unref (file);
+  if (!g_app_info_launch_default_for_uri_finish (res, &error))
+    {
+       print_error ("%s: %s", uri, error->message);
+       g_clear_error (&error);
+       success = FALSE;
     }
 
-  if (app_info == NULL || !G_IS_DESKTOP_APP_INFO (app_info) ||
-      !g_desktop_app_info_get_boolean (G_DESKTOP_APP_INFO (app_info), "DBusActivatable"))
-    goto out;
-
-  filename = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (app_info));
-  if (filename == NULL)
-    goto out;
-
-  basename = g_path_get_basename (filename);
-  if (!g_str_has_suffix (basename, ".desktop"))
-    goto out;
-
-  basename[strlen (basename) - strlen (".desktop")] = '\0';
-  if (!g_dbus_is_name (basename))
-    goto out;
-
-  bus_name = g_strdup (basename);
-  object_path = g_strdup_printf ("/%s", bus_name);
-  for (p = object_path; *p != '\0'; p++)
-    if (*p == '.')
-      *p = '/';
-
-  *bus_name_out = g_steal_pointer (&bus_name);
-  *object_path_out = g_steal_pointer (&object_path);
-  got_name = TRUE;
+  n_outstanding--;
 
-out:
-  g_clear_object (&app_info);
-  g_clear_pointer (&basename, g_free);
-
-  return got_name;
+  g_free (uri);
 }
-#endif
 
 int
 handle_open (int argc, char *argv[], gboolean do_help)
@@ -104,8 +63,6 @@ handle_open (int argc, char *argv[], gboolean do_help)
   gchar *param;
   GError *error = NULL;
   int i;
-  gboolean success;
-  gboolean res;
 
   g_set_prgname ("gio open");
 
@@ -143,7 +100,6 @@ handle_open (int argc, char *argv[], gboolean do_help)
 
   g_option_context_free (context);
 
-  success = TRUE;
   for (i = 1; i < argc; i++)
     {
       char *uri = NULL;
@@ -162,47 +118,20 @@ handle_open (int argc, char *argv[], gboolean do_help)
           uri = g_file_get_uri (file);
           g_object_unref (file);
         }
+      else
+        uri = g_strdup (argv[i]);
       g_free (uri_scheme);
 
-      res = g_app_info_launch_default_for_uri (uri ? uri : argv[i], NULL, &error);
-      if (!res)
-       {
-          print_error ("%s: %s", uri ? uri : argv[i], error->message);
-         g_clear_error (&error);
-         success = FALSE;
-       }
-
-#if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
-      /* FIXME: This chunk of madness is a workaround for a dbus-daemon bug.
-       * See https://bugzilla.gnome.org/show_bug.cgi?id=780296
-       */
-      if (res)
-        {
-          char *bus_name = NULL;
-          char *object_path = NULL;
-
-          if (get_bus_name_and_path_from_uri (uri ? uri : argv[i], &bus_name, &object_path))
-            {
-              GDBusConnection *connection;
-              connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
-
-              if (connection)
-                g_dbus_connection_call_sync (connection,
-                                             bus_name,
-                                             object_path,
-                                             "org.freedesktop.DBus.Peer",
-                                             "Ping",
-                                             NULL, NULL,
-                                             G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
-              g_clear_object (&connection);
-              g_free (bus_name);
-              g_free (object_path);
-            }
-        }
-#endif
-
-      g_free (uri);
+      g_app_info_launch_default_for_uri_async (uri,
+                                               NULL,
+                                               NULL,
+                                               launch_default_for_uri_cb,
+                                               g_steal_pointer (&uri));
+      n_outstanding++;
     }
 
+  while (n_outstanding > 0)
+    g_main_context_iteration (NULL, TRUE);
+
   return success ? 0 : 2;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]