* 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
#include "gfileicon.h"
#include <glib/gstdio.h>
#include "glibintl.h"
+#include "glib-private.h"
#include "giomodule-priv.h"
#include "gappinfo.h"
#include "gappinfoprivate.h"
static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
GError **error);
+static gboolean g_desktop_app_info_load_file (GDesktopAppInfo *self);
/**
* GDesktopAppInfo:
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 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 *
desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
const gchar *desktop_id)
{
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
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));
}
}
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)
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);
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;
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))
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;
}
{
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;
}
*
* 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
*
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 ("dtterm");
if (check == NULL)
+ check = g_find_program_in_path ("xterm");
+ if (check == NULL)
{
- check = g_strdup ("xterm");
- g_debug ("Couldn’t find a terminal: falling back to xterm");
+ g_debug ("Couldn’t find a known terminal");
+ g_free (term_argv);
+ return FALSE;
}
term_argv[0] = check;
term_argv[1] = g_strdup ("-e");
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
char *sn_id = NULL;
char **wrapped_argv;
int i;
- gsize j;
- const gchar * const wrapper_argv[] =
- {
- "/bin/sh",
- "-e",
- "-u",
- "-c", "export GIO_LAUNCHED_DESKTOP_FILE_PID=$$; exec \"$@\"",
- "sh", /* argv[0] for sh */
- };
old_uris = dup_uris;
if (!expand_application_parameters (info, exec_line, &dup_uris, &argc, &argv, error))
}
g_list_free_full (launched_files, g_object_unref);
+
+ emit_launch_started (launch_context, info, sn_id);
}
- /* Wrap the @argv in a command which will set the
- * `GIO_LAUNCHED_DESKTOP_FILE_PID` environment variable. We can’t set this
- * in @envp along with `GIO_LAUNCHED_DESKTOP_FILE` because we need to know
- * the PID of the new forked process. We can’t use setenv() between fork()
- * and exec() because we’d rather use posix_spawn() for speed.
- *
- * `sh` should be available on all the platforms that `GDesktopAppInfo`
- * currently supports (since they are all POSIX). If additional platforms
- * need to be supported in future, it will probably have to be replaced
- * with a wrapper program (grep the GLib git history for
- * `gio-launch-desktop` for an example of this which could be
- * resurrected). */
- wrapped_argv = g_new (char *, argc + G_N_ELEMENTS (wrapper_argv) + 1);
-
- for (j = 0; j < G_N_ELEMENTS (wrapper_argv); j++)
- wrapped_argv[j] = g_strdup (wrapper_argv[j]);
+ 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 + G_N_ELEMENTS (wrapper_argv)] = g_steal_pointer (&argv[i]);
+ wrapped_argv[i + 1] = g_steal_pointer (&argv[i]);
- wrapped_argv[i + G_N_ELEMENTS (wrapper_argv)] = NULL;
+ wrapped_argv[i + 1] = NULL;
g_free (argv);
argv = NULL;
return g_variant_builder_end (&builder);
}
+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;
+
+ 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,
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);
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);
+ 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,
- cancellable, callback, user_data);
+ cancellable, launch_uris_with_dbus_signal_cb, g_steal_pointer (&data));
g_free (object_path);
+
+ g_variant_dict_clear (&dict);
}
static gboolean
g_task_return_error (task, g_steal_pointer (&error));
g_object_unref (task);
}
- else
+ 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);
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 */
}
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);