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"
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;
GError *error = NULL;
gchar *uri = user_data;
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)
if (!g_app_info_launch_default_for_uri_finish (res, &error))
{
GFile *file;
file = g_file_new_for_uri (uri);
app_info = g_file_query_default_handler (file, NULL, NULL);
g_object_unref (file);
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;
n_outstanding--;
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;
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,23 @@ 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;
}
g_app_info_launch_default_for_uri_async (uri,
NULL,
NULL,
launch_default_for_uri_cb,
g_strdup (uri));
#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
n_outstanding++;
g_free (uri);
}
while (n_outstanding > 0)
g_main_context_iteration (NULL, TRUE);
return success ? 0 : 2;
}