* 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 "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"
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:
typedef struct
{
+ gatomicrefcount ref_count;
gchar *path;
gchar *alternatively_watching;
gboolean is_config;
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 const guint desktop_file_dir_user_config_index = 0;
-static guint desktop_file_dir_user_data_index;
+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
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)
{
if (g_once_init_enter (&result))
{
- const gchar *envvar;
- gchar **tmp;
+ char **tmp = get_valid_current_desktops (NULL);
+ gsize i, j;
- envvar = g_getenv ("XDG_CURRENT_DESKTOP");
-
- if (envvar)
+ for (i = 0; tmp[i]; i++)
{
- gint i, j;
-
- tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
-
- for (i = 0; tmp[i]; i++)
- for (j = 0; tmp[i][j]; j++)
- tmp[i][j] = g_ascii_tolower (tmp[i][j]);
+ /* Convert to lowercase. */
+ for (j = 0; tmp[i][j]; j++)
+ tmp[i][j] = g_ascii_tolower (tmp[i][j]);
}
- else
- tmp = g_new0 (gchar *, 0 + 1);
g_once_init_leave (&result, tmp);
}
if (g_once_init_enter (&result))
{
- gchar **tmp;
-
- if (!value)
- value = g_getenv ("XDG_CURRENT_DESKTOP");
-
- if (!value)
- value = "";
-
- tmp = g_strsplit (value, ":", 0);
+ char **tmp = get_valid_current_desktops (value);
g_once_init_leave (&result, tmp);
}
};
/* Common prefix commands to ignore from Exec= lines */
-const char * const exec_key_match_blacklist[] = {
+const char * const exec_key_match_blocklist[] = {
"bash",
"env",
"flatpak",
static void
expand_strv (gchar ***strv_ptr,
gchar **to_add,
- gchar * const *blacklist)
+ gchar * const *blocklist)
{
guint strv_len, add_len;
gchar **strv;
for (i = 0; to_add[i]; i++)
{
- /* Don't add blacklisted strings */
- if (blacklist)
- for (j = 0; blacklist[j]; j++)
- if (g_str_equal (to_add[i], blacklist[j]))
+ /* 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 */
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 blacklisting approach to
+ /* We process in order of precedence, using a blocklisting approach to
* avoid recording later instructions that conflict with ones we found
* earlier.
*
}
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));
}
}
{
/* Index the interesting keys... */
gchar **implements;
- gint i;
+ gsize i;
for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
{
if ((slash = strrchr (raw, '/')))
value = slash + 1;
- /* Don't match on blacklisted binaries like interpreters */
- if (g_strv_contains (exec_key_match_blacklist, value))
+ /* Don't match on blocklisted binaries like interpreters */
+ if (g_strv_contains (exec_key_match_blocklist, value))
value = NULL;
}
array_contains (GPtrArray *array,
const gchar *str)
{
- gint i;
+ guint i;
for (i = 0; i < array->len; i++)
if (g_str_equal (array->pdata[i], str))
desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
const gchar *mime_type,
GPtrArray *hits,
- GPtrArray *blacklist)
+ GPtrArray *blocklist)
{
UnindexedMimeTweaks *tweaks;
gint i;
gchar *app_name = tweaks->additions[i];
if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
- !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
+ !array_contains (blocklist, app_name) && !array_contains (hits, app_name))
g_ptr_array_add (hits, app_name);
}
}
gchar *app_name = tweaks->removals[i];
if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
- !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
- g_ptr_array_add (blacklist, app_name);
+ !array_contains (blocklist, app_name) && !array_contains (hits, app_name))
+ g_ptr_array_add (blocklist, app_name);
}
}
}
/* DesktopFileDir "API" {{{2 */
/*< internal >
- * desktop_file_dir_create:
- * @array: the #GArray to add a new item to
+ * desktop_file_dir_new:
* @data_dir: an XDG_DATA_DIR
*
- * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
- * to @array.
+ * Creates a #DesktopFileDir for the corresponding @data_dir.
*/
-static void
-desktop_file_dir_create (GArray *array,
- const gchar *data_dir)
+static DesktopFileDir *
+desktop_file_dir_new (const gchar *data_dir)
{
- DesktopFileDir dir = { 0, };
+ DesktopFileDir *dir = g_new0 (DesktopFileDir, 1);
- dir.path = g_build_filename (data_dir, "applications", NULL);
+ g_atomic_ref_count_init (&dir->ref_count);
+ dir->path = g_build_filename (data_dir, "applications", NULL);
- g_array_append_val (array, dir);
+ return g_steal_pointer (&dir);
}
/*< internal >
- * desktop_file_dir_create:
- * @array: the #GArray to add a new item to
+ * desktop_file_dir_new_for_config:
* @config_dir: an XDG_CONFIG_DIR
*
- * Just the same as desktop_file_dir_create() except that it does not
+ * 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 void
-desktop_file_dir_create_for_config (GArray *array,
- const gchar *config_dir)
+static DesktopFileDir *
+desktop_file_dir_new_for_config (const gchar *config_dir)
{
- DesktopFileDir dir = { 0, };
+ DesktopFileDir *dir = g_new0 (DesktopFileDir, 1);
- dir.path = g_strdup (config_dir);
- dir.is_config = TRUE;
+ g_atomic_ref_count_init (&dir->ref_count);
+ dir->path = g_strdup (config_dir);
+ dir->is_config = TRUE;
- g_array_append_val (array, dir);
+ return g_steal_pointer (&dir);
}
/*< internal >
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;
}
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
* 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, dir, NULL);
+ desktop_file_dir_changed,
+ desktop_file_dir_ref (dir),
+ closure_notify_cb, NULL);
desktop_file_dir_unindexed_init (dir);
* @dir: a #DesktopFileDir
* @mime_type: the mime type to look up
* @hits: the array to store the hits
- * @blacklist: the array to store the blacklist
+ * @blocklist: the array to store the blocklist
*
* Does a lookup of a mimetype against one desktop file directory,
- * recording any hits and blacklisting and "Removed" associations (so
+ * 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 @blacklist
- * are weak pointers. This facilitates simply freeing the blacklist
+ * 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.
*/
desktop_file_dir_mime_lookup (DesktopFileDir *dir,
const gchar *mime_type,
GPtrArray *hits,
- GPtrArray *blacklist)
+ GPtrArray *blocklist)
{
- desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blacklist);
+ desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blocklist);
}
/*< internal >
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 the XDG dirs configuration has changed (expected only during tests),
* clear and reload the state. */
- if (g_strcmp0 (desktop_file_dirs_config_dir, user_config_dir) != 0)
+ 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);
- for (i = 0; i < n_desktop_file_dirs; i++)
- desktop_file_dir_reset (&desktop_file_dirs[i]);
- g_clear_pointer (&desktop_file_dirs, g_free);
- n_desktop_file_dirs = 0;
- desktop_file_dir_user_data_index = 0;
+ 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)
+ if (desktop_file_dirs == NULL || desktop_file_dirs->len == 0)
{
const char * const *dirs;
- GArray *tmp;
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);
/* First, the configs. Highest priority: the user's ~/.config */
- desktop_file_dir_create_for_config (tmp, user_config_dir);
+ 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));
/* Next, the system configs (/etc/xdg, and so on). */
dirs = g_get_system_config_dirs ();
for (i = 0; dirs[i]; i++)
- desktop_file_dir_create_for_config (tmp, dirs[i]);
+ g_ptr_array_add (desktop_file_dirs, desktop_file_dir_new_for_config (dirs[i]));
/* Now the data. Highest priority: the user's ~/.local/share/applications */
- desktop_file_dir_user_data_index = tmp->len;
- desktop_file_dir_create (tmp, g_get_user_data_dir ());
+ 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++)
- desktop_file_dir_create (tmp, dirs[i]);
+ g_ptr_array_add (desktop_file_dirs, desktop_file_dir_new (dirs[i]));
/* 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 = (DesktopFileDir *) tmp->data;
- n_desktop_file_dirs = tmp->len;
desktop_file_dirs_config_dir = user_config_dir;
-
- g_array_free (tmp, FALSE);
}
- 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
{
g_mutex_lock (&desktop_file_dir_lock);
- if (n_desktop_file_dirs)
- desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_config_index]);
+ if (desktop_file_dir_user_config != NULL)
+ desktop_file_dir_reset (desktop_file_dir_user_config);
g_mutex_unlock (&desktop_file_dir_lock);
}
{
g_mutex_lock (&desktop_file_dir_lock);
- if (n_desktop_file_dirs)
- desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_data_index]);
+ if (desktop_file_dir_user_data != NULL)
+ desktop_file_dir_reset (desktop_file_dir_user_data);
g_mutex_unlock (&desktop_file_dir_lock);
}
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;
}
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;
* situations such as the #GDesktopAppInfo returned from
* g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
*
- * Returns: (type filename): The full path to the file for @info,
+ * Returns: (nullable) (type filename): The full path to the file for @info,
* or %NULL if not known.
* Since: 2.24
*/
*
* 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 *
* 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)
*
* 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
*
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 ("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_debug ("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;
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
* 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 = g_list_copy (uris);
+ dup_uris = uris;
do
{
GPid pid;
}
g_list_free_full (launched_files, g_object_unref);
+
+ emit_launch_started (launch_context, info, sn_id);
}
if (g_once_init_enter (&gio_launch_desktop_path))
{
- const gchar *tmp;
+ const gchar *tmp = NULL;
+ gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) ();
/* Allow test suite to specify path to gio-launch-desktop */
- tmp = g_getenv ("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)
completed = TRUE;
out:
- g_list_free (dup_uris);
g_strfreev (argv);
g_strfreev (envp);
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);
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);
desktop_file_dirs_invalidate_user_config ();
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 */
}
" </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");
/* 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)
{
if (include_fallback)
{
- gint i;
+ guint i;
/* Iterate the array as we grow it, until we have nothing more to add */
for (i = 0; i < array->len; i++)
g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
gboolean include_fallback)
{
- GPtrArray *hits, *blacklist;
+ GPtrArray *hits, *blocklist;
gchar **types;
- gint i, j;
+ guint i, j;
hits = g_ptr_array_new ();
- blacklist = 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 < n_desktop_file_dirs; j++)
- desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], hits, blacklist);
+ 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++)
g_ptr_array_add (hits, NULL);
- g_ptr_array_free (blacklist, TRUE);
+ g_ptr_array_free (blocklist, TRUE);
g_strfreev (types);
return (gchar **) g_ptr_array_free (hits, FALSE);
*
* 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)
{
- GPtrArray *blacklist;
+ GPtrArray *blocklist;
GPtrArray *results;
GAppInfo *info;
gchar **types;
- gint i, j, k;
+ guint i, j, k;
g_return_val_if_fail (content_type != NULL, NULL);
types = get_list_of_mimetypes (content_type, TRUE);
- blacklist = g_ptr_array_new ();
+ blocklist = g_ptr_array_new ();
results = g_ptr_array_new ();
info = NULL;
for (i = 0; types[i]; i++)
{
/* Collect all the default apps for this type */
- for (j = 0; j < n_desktop_file_dirs; j++)
- desktop_file_dir_default_lookup (&desktop_file_dirs[j], types[i], results);
+ 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);
/* Consider the associations as well... */
- for (j = 0; j < n_desktop_file_dirs; j++)
- desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], results, blacklist);
+ 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++)
{
const gchar *desktop_id = g_ptr_array_index (results, j);
- for (k = 0; k < n_desktop_file_dirs; k++)
+ for (k = 0; k < desktop_file_dirs->len; k++)
{
- info = (GAppInfo *) desktop_file_dir_get_app (&desktop_file_dirs[k], desktop_id);
+ info = (GAppInfo *) desktop_file_dir_get_app (g_ptr_array_index (desktop_file_dirs, k), desktop_id);
if (info)
{
}
/* Reset the list, ready to try again with the next (parent)
- * mimetype, but keep the blacklist in place.
+ * mimetype, but keep the blocklist in place.
*/
g_ptr_array_set_size (results, 0);
}
out:
desktop_file_dirs_unlock ();
- g_ptr_array_unref (blacklist);
+ g_ptr_array_unref (blocklist);
g_ptr_array_unref (results);
g_strfreev (types);
* 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)
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);
{
GList *result = NULL;
GList **ptr;
- gint i;
+ guint i;
desktop_file_dirs_lock ();
- for (i = 0; i < n_desktop_file_dirs; i++)
- desktop_file_dir_get_implementations (&desktop_file_dirs[i], &result, interface);
+ 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 ();
* 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().
gint n_categories = 0;
gint start_of_category;
gint i, j;
+ guint k;
search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
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 ();
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 ();
*
* #GDesktopAppInfoLookup is an opaque data structure and can only be accessed
* using the following functions.
+ *
+ * Deprecated: 2.28: The #GDesktopAppInfoLookup interface is deprecated and
+ * unused by GIO.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
* @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,
* 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
*
* 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
* 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.0
+ * Since: 2.60
*/
gchar **
g_desktop_app_info_get_string_list (GDesktopAppInfo *info,