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
This commit is contained in:
Ondrej Holy 2019-01-22 16:29:20 +01:00
parent 904bb264e9
commit 051c6ba4e7

View File

@ -29,73 +29,32 @@
#include "gio-tool.h" #include "gio-tool.h"
static int n_outstanding = 0;
static gboolean success = TRUE;
static const GOptionEntry entries[] = { static const GOptionEntry entries[] = {
{ NULL } { NULL }
}; };
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA) static void
static gboolean launch_default_for_uri_cb (GObject *source_object,
get_bus_name_and_path_from_uri (const char *uri, GAsyncResult *res,
char **bus_name_out, gpointer user_data)
char **object_path_out)
{ {
GAppInfo *app_info = NULL; GError *error = NULL;
char *bus_name = NULL; gchar *uri = user_data;
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 (!g_app_info_launch_default_for_uri_finish (res, &error))
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; print_error ("%s: %s", uri, error->message);
g_clear_error (&error);
file = g_file_new_for_uri (uri); success = FALSE;
app_info = g_file_query_default_handler (file, NULL, NULL);
g_object_unref (file);
} }
if (app_info == NULL || !G_IS_DESKTOP_APP_INFO (app_info) || n_outstanding--;
!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)); g_free (uri);
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;
out:
g_clear_object (&app_info);
g_clear_pointer (&basename, g_free);
return got_name;
} }
#endif
int int
handle_open (int argc, char *argv[], gboolean do_help) handle_open (int argc, char *argv[], gboolean do_help)
@ -104,8 +63,6 @@ handle_open (int argc, char *argv[], gboolean do_help)
gchar *param; gchar *param;
GError *error = NULL; GError *error = NULL;
int i; int i;
gboolean success;
gboolean res;
g_set_prgname ("gio open"); g_set_prgname ("gio open");
@ -143,7 +100,6 @@ handle_open (int argc, char *argv[], gboolean do_help)
g_option_context_free (context); g_option_context_free (context);
success = TRUE;
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
char *uri = NULL; char *uri = NULL;
@ -162,47 +118,23 @@ handle_open (int argc, char *argv[], gboolean do_help)
uri = g_file_get_uri (file); uri = g_file_get_uri (file);
g_object_unref (file); g_object_unref (file);
} }
else
uri = g_strdup (argv[i]);
g_free (uri_scheme); g_free (uri_scheme);
res = g_app_info_launch_default_for_uri (uri ? uri : argv[i], NULL, &error); g_app_info_launch_default_for_uri_async (uri,
if (!res) NULL,
{ NULL,
print_error ("%s: %s", uri ? uri : argv[i], error->message); launch_default_for_uri_cb,
g_clear_error (&error); g_strdup (uri));
success = FALSE;
}
#if defined(G_OS_UNIX) && !defined(HAVE_COCOA) n_outstanding++;
/* 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_free (uri);
} }
while (n_outstanding > 0)
g_main_context_iteration (NULL, TRUE);
return success ? 0 : 2; return success ? 0 : 2;
} }