* 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
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
#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 const guint desktop_file_dir_user_config_index = 0;
-static guint desktop_file_dir_user_data_index;
+static GPtrArray *desktop_file_dirs = NULL;
+static const gchar *desktop_file_dirs_config_dir = NULL;
+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;
-
- envvar = g_getenv ("XDG_CURRENT_DESKTOP");
+ char **tmp = get_valid_current_desktops (NULL);
+ gsize i, j;
- 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);
}
[DESKTOP_KEY_Comment] = 6
};
+/* Common prefix commands to ignore from Exec= lines */
+const char * const exec_key_match_blocklist[] = {
+ "bash",
+ "env",
+ "flatpak",
+ "gjs",
+ "pkexec",
+ "python",
+ "python2",
+ "python3",
+ "sh",
+ "wine",
+ "wine64",
+ NULL
+};
+
static gchar *
desktop_key_get_name (guint key_id)
{
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++)
{
/* Skip the pathname, if any */
if ((slash = strrchr (raw, '/')))
value = slash + 1;
+
+ /* Don't match on blocklisted binaries like interpreters */
+ if (g_strv_contains (exec_key_match_blocklist, value))
+ value = NULL;
}
if (value)
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 (desktop_file_dirs == NULL)
+ /* If the XDG dirs configuration has changed (expected only during tests),
+ * clear and reload the state. */
+ 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);
+
+ 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 || 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, g_get_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. */
- desktop_file_dirs = (DesktopFileDir *) tmp->data;
- n_desktop_file_dirs = tmp->len;
-
- g_array_free (tmp, FALSE);
+ /* 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_config_dir = user_config_dir;
}
- 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))
*
* Creates a new #GDesktopAppInfo.
*
- * Returns: a new #GDesktopAppInfo or %NULL on error.
+ * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
*
* Since: 2.18
**/
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;
}
*
* Creates a new #GDesktopAppInfo.
*
- * Returns: a new #GDesktopAppInfo or %NULL on error.
+ * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
**/
GDesktopAppInfo *
g_desktop_app_info_new_from_filename (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_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;
}
* (i.e. a desktop id of kde-foo.desktop will match
* `/usr/share/applications/kde/foo.desktop`).
*
- * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
+ * Returns: (nullable): a new #GDesktopAppInfo, or %NULL if no desktop
+ * file with that id exists.
*/
GDesktopAppInfo *
g_desktop_app_info_new (const char *desktop_id)
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
*
/* Launching... {{{2 */
static char *
-expand_macro_single (char macro, char *uri)
+expand_macro_single (char macro, const char *uri)
{
GFile *file;
char *result = NULL;
return result;
}
+static char *
+expand_macro_uri (char macro, const char *uri, gboolean force_file_uri, char force_file_uri_macro)
+{
+ char *expanded = NULL;
+
+ g_return_val_if_fail (uri != NULL, NULL);
+
+ if (!force_file_uri ||
+ /* Pass URI if it contains an anchor */
+ strchr (uri, '#') != NULL)
+ {
+ expanded = expand_macro_single (macro, uri);
+ }
+ else
+ {
+ expanded = expand_macro_single (force_file_uri_macro, uri);
+ if (expanded == NULL)
+ expanded = expand_macro_single (macro, uri);
+ }
+
+ return expanded;
+}
+
static void
expand_macro (char macro,
GString *exec,
GList **uri_list)
{
GList *uris = *uri_list;
- char *expanded;
+ char *expanded = NULL;
gboolean force_file_uri;
char force_file_uri_macro;
- char *uri;
+ const char *uri;
g_return_if_fail (exec != NULL);
if (uris)
{
uri = uris->data;
- if (!force_file_uri ||
- /* Pass URI if it contains an anchor */
- strchr (uri, '#') != NULL)
- {
- expanded = expand_macro_single (macro, uri);
- }
- else
- {
- expanded = expand_macro_single (force_file_uri_macro, uri);
- if (expanded == NULL)
- expanded = expand_macro_single (macro, uri);
- }
-
+ expanded = expand_macro_uri (macro, uri,
+ force_file_uri, force_file_uri_macro);
if (expanded)
{
g_string_append (exec, expanded);
while (uris)
{
uri = uris->data;
-
- if (!force_file_uri ||
- /* Pass URI if it contains an anchor */
- strchr (uri, '#') != NULL)
- {
- expanded = expand_macro_single (macro, uri);
- }
- else
- {
- expanded = expand_macro_single (force_file_uri_macro, uri);
- if (expanded == NULL)
- expanded = expand_macro_single (macro, uri);
- }
-
+ expanded = expand_macro_uri (macro, uri,
+ force_file_uri, force_file_uri_macro);
if (expanded)
{
g_string_append (exec, expanded);
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 ("xterm");
- 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_warning ("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;
return g_list_reverse (res);
}
-typedef struct
-{
- GSpawnChildSetupFunc user_setup;
- gpointer user_setup_data;
-
- char *pid_envvar;
-} ChildSetupData;
-
-static void
-child_setup (gpointer user_data)
-{
- ChildSetupData *data = user_data;
-
- if (data->pid_envvar)
- {
- pid_t pid = getpid ();
- char buf[20];
- int i;
-
- /* Write the pid into the space already reserved for it in the
- * environment array. We can't use sprintf because it might
- * malloc, so we do it by hand. It's simplest to write the pid
- * out backwards first, then copy it over.
- */
- for (i = 0; pid; i++, pid /= 10)
- buf[i] = (pid % 10) + '0';
- for (i--; i >= 0; i--)
- *(data->pid_envvar++) = buf[i];
- *data->pid_envvar = '\0';
- }
-
- if (data->user_setup)
- data->user_setup (data->user_setup_data);
-}
-
static void
notify_desktop_launch (GDBusConnection *session_bus,
GDesktopAppInfo *info,
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
gpointer user_setup_data,
GDesktopAppLaunchCallback pid_callback,
gpointer pid_callback_data,
+ gint stdin_fd,
+ gint stdout_fd,
+ gint stderr_fd,
GError **error)
{
gboolean completed = FALSE;
GList *old_uris;
+ GList *dup_uris;
+
char **argv, **envp;
int argc;
- ChildSetupData data;
g_return_val_if_fail (info != NULL, FALSE);
else
envp = g_get_environ ();
+ /* The GList* passed to expand_application_parameters() will be modified
+ * 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 = uris;
do
{
GPid pid;
GList *launched_uris;
GList *iter;
char *sn_id = NULL;
+ char **wrapped_argv;
+ int i;
- old_uris = uris;
- if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
+ old_uris = dup_uris;
+ if (!expand_application_parameters (info, exec_line, &dup_uris, &argc, &argv, error))
goto out;
/* Get the subset of URIs we're launching with this process */
launched_uris = NULL;
- for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
+ for (iter = old_uris; iter != NULL && iter != dup_uris; iter = iter->next)
launched_uris = g_list_prepend (launched_uris, iter->data);
launched_uris = g_list_reverse (launched_uris);
goto out;
}
- data.user_setup = user_setup;
- data.user_setup_data = user_setup_data;
-
if (info->filename)
- {
- envp = g_environ_setenv (envp,
- "GIO_LAUNCHED_DESKTOP_FILE",
- info->filename,
- TRUE);
- envp = g_environ_setenv (envp,
- "GIO_LAUNCHED_DESKTOP_FILE_PID",
- "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
- TRUE);
- data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
- }
- else
- {
- data.pid_envvar = NULL;
- }
+ envp = g_environ_setenv (envp,
+ "GIO_LAUNCHED_DESKTOP_FILE",
+ info->filename,
+ TRUE);
sn_id = NULL;
if (launch_context)
}
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 = 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);
}
- if (!g_spawn_async (info->path,
- argv,
- envp,
- spawn_flags,
- child_setup,
- &data,
- &pid,
- error))
+ 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 + 1] = g_steal_pointer (&argv[i]);
+
+ wrapped_argv[i + 1] = NULL;
+ g_free (argv);
+ argv = NULL;
+
+ if (!g_spawn_async_with_fds (info->path,
+ wrapped_argv,
+ envp,
+ spawn_flags,
+ user_setup,
+ user_setup_data,
+ &pid,
+ stdin_fd,
+ stdout_fd,
+ stderr_fd,
+ error))
{
if (sn_id)
g_app_launch_context_launch_failed (launch_context, sn_id);
g_free (sn_id);
g_list_free (launched_uris);
- g_strfreev (argv);
- argv = NULL;
+ g_strfreev (wrapped_argv);
+ wrapped_argv = NULL;
}
- while (uris != NULL);
+ while (dup_uris != NULL);
completed = TRUE;
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,
GList *uris,
- GAppLaunchContext *launch_context)
+ GAppLaunchContext *launch_context,
+ GCancellable *cancellable,
+ 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);
- /* This is non-blocking API. Similar to launching via fork()/exec()
- * we don't wait around to see if the program crashed during startup.
- * This is what startup-notification's job is...
- */
+ 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, NULL, NULL, NULL);
+ NULL, G_DBUS_CALL_FLAGS_NONE, -1,
+ cancellable, launch_uris_with_dbus_signal_cb, g_steal_pointer (&data));
g_free (object_path);
+
+ g_variant_dict_clear (&dict);
}
static gboolean
g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
GDBusConnection *session_bus,
GList *uris,
- GAppLaunchContext *launch_context)
+ GAppLaunchContext *launch_context,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
GList *ruris = uris;
- g_autofree char *app_id = NULL;
+ char *app_id = NULL;
g_return_val_if_fail (info != NULL, FALSE);
}
#endif
- launch_uris_with_dbus (info, session_bus, ruris, launch_context);
+ launch_uris_with_dbus (info, session_bus, ruris, launch_context,
+ cancellable, callback, user_data);
if (ruris != uris)
g_list_free_full (ruris, g_free);
+ g_free (app_id);
+
return TRUE;
}
gpointer user_setup_data,
GDesktopAppLaunchCallback pid_callback,
gpointer pid_callback_data,
+ gint stdin_fd,
+ gint stdout_fd,
+ gint stderr_fd,
GError **error)
{
GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
if (session_bus && info->app_id)
- g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
+ /* This is non-blocking API. Similar to launching via fork()/exec()
+ * we don't wait around to see if the program crashed during startup.
+ * This is what startup-notification's job is...
+ */
+ g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context,
+ NULL, NULL, NULL);
else
success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
spawn_flags, user_setup, user_setup_data,
- pid_callback, pid_callback_data, error);
+ pid_callback, pid_callback_data,
+ stdin_fd, stdout_fd, stderr_fd, error);
if (session_bus != NULL)
{
launch_context,
_SPAWN_FLAGS_DEFAULT,
NULL, NULL, NULL, NULL,
+ -1, -1, -1,
error);
}
+typedef struct
+{
+ GAppInfo *appinfo;
+ GList *uris;
+ GAppLaunchContext *context;
+} LaunchUrisData;
+
+static void
+launch_uris_data_free (LaunchUrisData *data)
+{
+ g_clear_object (&data->context);
+ g_list_free_full (data->uris, g_free);
+ g_free (data);
+}
+
+static void
+launch_uris_with_dbus_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GTask *task = G_TASK (user_data);
+ GError *error = NULL;
+
+ g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
+ if (error != NULL)
+ {
+ g_dbus_error_strip_remote_error (error);
+ g_task_return_error (task, g_steal_pointer (&error));
+ }
+ else
+ g_task_return_boolean (task, TRUE);
+
+ g_object_unref (task);
+}
+
+static void
+launch_uris_flush_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GTask *task = G_TASK (user_data);
+
+ g_dbus_connection_flush_finish (G_DBUS_CONNECTION (object), result, NULL);
+ g_task_return_boolean (task, TRUE);
+ g_object_unref (task);
+}
+
+static void
+launch_uris_bus_get_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GTask *task = G_TASK (user_data);
+ GDesktopAppInfo *info = G_DESKTOP_APP_INFO (g_task_get_source_object (task));
+ LaunchUrisData *data = g_task_get_task_data (task);
+ GCancellable *cancellable = g_task_get_cancellable (task);
+ GDBusConnection *session_bus;
+ GError *error = NULL;
+
+ session_bus = g_bus_get_finish (result, NULL);
+
+ if (session_bus && info->app_id)
+ {
+ /* FIXME: The g_document_portal_add_documents() function, which is called
+ * from the g_desktop_app_info_launch_uris_with_dbus() function, still
+ * uses blocking calls.
+ */
+ g_desktop_app_info_launch_uris_with_dbus (info, session_bus,
+ data->uris, data->context,
+ cancellable,
+ launch_uris_with_dbus_cb,
+ g_steal_pointer (&task));
+ }
+ else
+ {
+ /* FIXME: The D-Bus message from the notify_desktop_launch() function
+ * can be still lost even if flush is called later. See:
+ * https://gitlab.freedesktop.org/dbus/dbus/issues/72
+ */
+ g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec,
+ data->uris, data->context,
+ _SPAWN_FLAGS_DEFAULT, NULL,
+ NULL, NULL, NULL, -1, -1, -1,
+ &error);
+ if (error != NULL)
+ {
+ g_task_return_error (task, g_steal_pointer (&error));
+ g_object_unref (task);
+ }
+ 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);
+}
+
+static void
+g_desktop_app_info_launch_uris_async (GAppInfo *appinfo,
+ GList *uris,
+ GAppLaunchContext *context,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GTask *task;
+ LaunchUrisData *data;
+
+ task = g_task_new (appinfo, cancellable, callback, user_data);
+ g_task_set_source_tag (task, g_desktop_app_info_launch_uris_async);
+
+ data = g_new0 (LaunchUrisData, 1);
+ data->uris = g_list_copy_deep (uris, (GCopyFunc) g_strdup, NULL);
+ data->context = (context != NULL) ? g_object_ref (context) : NULL;
+ g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) launch_uris_data_free);
+
+ g_bus_get (G_BUS_TYPE_SESSION, cancellable, launch_uris_bus_get_cb, task);
+}
+
+static gboolean
+g_desktop_app_info_launch_uris_finish (GAppInfo *appinfo,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (g_task_is_valid (result, appinfo), FALSE);
+
+ return g_task_propagate_boolean (G_TASK (result), error);
+}
+
static gboolean
g_desktop_app_info_supports_uris (GAppInfo *appinfo)
{
}
/**
+ * g_desktop_app_info_launch_uris_as_manager_with_fds:
+ * @appinfo: a #GDesktopAppInfo
+ * @uris: (element-type utf8): List of URIs
+ * @launch_context: (nullable): a #GAppLaunchContext
+ * @spawn_flags: #GSpawnFlags, used for each process
+ * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
+ * for each process.
+ * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
+ * @pid_callback: (scope call) (nullable): Callback for child processes
+ * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
+ * @stdin_fd: file descriptor to use for child's stdin, or -1
+ * @stdout_fd: file descriptor to use for child's stdout, or -1
+ * @stderr_fd: file descriptor to use for child's stderr, or -1
+ * @error: return location for a #GError, or %NULL
+ *
+ * Equivalent to g_desktop_app_info_launch_uris_as_manager() but allows
+ * you to pass in file descriptors for the stdin, stdout and stderr streams
+ * of the launched process.
+ *
+ * If application launching occurs via some non-spawn mechanism (e.g. D-Bus
+ * activation) then @stdin_fd, @stdout_fd and @stderr_fd are ignored.
+ *
+ * Returns: %TRUE on successful launch, %FALSE otherwise.
+ *
+ * Since: 2.58
+ */
+gboolean
+g_desktop_app_info_launch_uris_as_manager_with_fds (GDesktopAppInfo *appinfo,
+ GList *uris,
+ GAppLaunchContext *launch_context,
+ GSpawnFlags spawn_flags,
+ GSpawnChildSetupFunc user_setup,
+ gpointer user_setup_data,
+ GDesktopAppLaunchCallback pid_callback,
+ gpointer pid_callback_data,
+ gint stdin_fd,
+ gint stdout_fd,
+ gint stderr_fd,
+ GError **error)
+{
+ return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
+ uris,
+ launch_context,
+ spawn_flags,
+ user_setup,
+ user_setup_data,
+ pid_callback,
+ pid_callback_data,
+ stdin_fd,
+ stdout_fd,
+ stderr_fd,
+ error);
+}
+
+/**
* g_desktop_app_info_launch_uris_as_manager:
* @appinfo: a #GDesktopAppInfo
* @uris: (element-type utf8): List of URIs
* @launch_context: (nullable): a #GAppLaunchContext
* @spawn_flags: #GSpawnFlags, used for each process
- * @user_setup: (scope call) (nullable): a #GSpawnChildSetupFunc, used once
+ * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
* for each process.
* @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
* @pid_callback: (scope call) (nullable): Callback for child processes
* launch applications. Ordinary applications should use
* g_app_info_launch_uris().
*
- * If the application is launched via traditional UNIX fork()/exec()
- * then @spawn_flags, @user_setup and @user_setup_data are used for the
- * call to g_spawn_async(). Additionally, @pid_callback (with
- * @pid_callback_data) will be called to inform about the PID of the
- * created process.
+ * If the application is launched via GSpawn, then @spawn_flags, @user_setup
+ * and @user_setup_data are used for the call to g_spawn_async().
+ * Additionally, @pid_callback (with @pid_callback_data) will be called to
+ * inform about the PID of the created process. See g_spawn_async_with_pipes()
+ * for information on certain parameter conditions that can enable an
+ * optimized posix_spawn() codepath to be used.
*
* If application launching occurs via some other mechanism (eg: D-Bus
* activation) then @spawn_flags, @user_setup, @user_setup_data,
gpointer pid_callback_data,
GError **error)
{
- return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
- uris,
- launch_context,
- spawn_flags,
- user_setup,
- user_setup_data,
- pid_callback,
- pid_callback_data,
- error);
+ return g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
+ uris,
+ launch_context,
+ spawn_flags,
+ user_setup,
+ user_setup_data,
+ pid_callback,
+ pid_callback_data,
+ -1, -1, -1,
+ error);
}
/* OnlyShowIn API support {{{2 */
g_assert_not_reached ();
}
+ g_debug ("%s: Ensuring %s", G_STRFUNC, path);
+
errno = 0;
if (g_mkdir_with_parents (path, 0700) == 0)
return path;
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)
{
/* Create for commandline {{{2 */
/**
* g_app_info_create_from_commandline:
- * @commandline: the commandline to use
+ * @commandline: (type filename): the commandline to use
* @application_name: (nullable): the application name, or %NULL to use @commandline
* @flags: flags that can specify details of the created #GAppInfo
* @error: a #GError location to store the error occurring, %NULL to ignore.
iface->supports_uris = g_desktop_app_info_supports_uris;
iface->supports_files = g_desktop_app_info_supports_files;
iface->launch_uris = g_desktop_app_info_launch_uris;
+ iface->launch_uris_async = g_desktop_app_info_launch_uris_async;
+ iface->launch_uris_finish = g_desktop_app_info_launch_uris_finish;
iface->should_show = g_desktop_app_info_should_show;
iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
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
}
/**
+ * g_desktop_app_info_get_locale_string:
+ * @info: a #GDesktopAppInfo
+ * @key: the key to look up
+ *
+ * Looks up a localized string value in the keyfile backing @info
+ * translated to the current locale.
+ *
+ * The @key is looked up in the "Desktop Entry" group.
+ *
+ * Returns: (nullable): a newly allocated string, or %NULL if the key
+ * is not found
+ *
+ * Since: 2.56
+ */
+char *
+g_desktop_app_info_get_locale_string (GDesktopAppInfo *info,
+ const char *key)
+{
+ g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
+ g_return_val_if_fail (key != NULL && *key != '\0', NULL);
+
+ return g_key_file_get_locale_string (info->keyfile,
+ G_KEY_FILE_DESKTOP_GROUP,
+ key, NULL, NULL);
+}
+
+/**
* g_desktop_app_info_get_boolean:
* @info: a #GDesktopAppInfo
* @key: the key to look up
}
/**
+ * g_desktop_app_info_get_string_list:
+ * @info: a #GDesktopAppInfo
+ * @key: the key to look up
+ * @length: (out) (optional): return location for the number of returned strings, or %NULL
+ *
+ * Looks up a string list value in the keyfile backing @info.
+ *
+ * The @key is looked up in the "Desktop Entry" group.
+ *
+ * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
+ * 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
+ */
+gchar **
+g_desktop_app_info_get_string_list (GDesktopAppInfo *info,
+ const char *key,
+ gsize *length)
+{
+ g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
+
+ return g_key_file_get_string_list (info->keyfile,
+ G_KEY_FILE_DESKTOP_GROUP, key, length, NULL);
+}
+
+/**
* g_desktop_app_info_has_key:
* @info: a #GDesktopAppInfo
* @key: the key to look up
if (exec_line)
g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
- _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
+ _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL,
+ -1, -1, -1, NULL);
+
+ g_free (exec_line);
}
if (session_bus != NULL)