1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright © 2007 Ryan Lortie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Alexander Larsson <alexl@redhat.com>
20 * Ryan Lortie <desrt@desrt.ca>
27 /* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
28 * in the preprocessor, we need to define this before including glib.h*/
29 #define GLIB_DISABLE_DEPRECATION_WARNINGS
35 #ifdef HAVE_CRT_EXTERNS_H
36 #include <crt_externs.h>
39 #include "gcontenttypeprivate.h"
40 #include "gdesktopappinfo.h"
42 #include "glib-unix.h"
46 #include "gthemedicon.h"
47 #include "gfileicon.h"
48 #include <glib/gstdio.h>
50 #include "giomodule-priv.h"
52 #include "gappinfoprivate.h"
53 #include "glocalfilemonitor.h"
56 #include "gdocumentportal.h"
60 * SECTION:gdesktopappinfo
61 * @title: GDesktopAppInfo
62 * @short_description: Application information from desktop files
63 * @include: gio/gdesktopappinfo.h
65 * #GDesktopAppInfo is an implementation of #GAppInfo based on
68 * Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific
69 * GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
73 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
74 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
75 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
76 #define MIME_CACHE_GROUP "MIME Cache"
77 #define GENERIC_NAME_KEY "GenericName"
78 #define FULL_NAME_KEY "X-GNOME-FullName"
79 #define KEYWORDS_KEY "Keywords"
80 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
87 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
88 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
94 * Information about an installed application from a desktop file.
96 struct _GDesktopAppInfo
98 GObject parent_instance;
120 char *startup_wm_class;
127 guint startup_notify : 1;
132 UPDATE_MIME_NONE = 1 << 0,
133 UPDATE_MIME_SET_DEFAULT = 1 << 1,
134 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
135 UPDATE_MIME_REMOVE = 1 << 3,
136 UPDATE_MIME_SET_LAST_USED = 1 << 4,
139 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
140 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
142 /* DesktopFileDir implementation {{{1 */
147 gchar *alternatively_watching;
150 GFileMonitor *monitor;
151 GHashTable *app_names;
152 GHashTable *mime_tweaks;
153 GHashTable *memory_index;
154 GHashTable *memory_implementations;
157 static DesktopFileDir *desktop_file_dirs;
158 static guint n_desktop_file_dirs;
159 static const gchar *desktop_file_dirs_config_dir = NULL;
160 static const guint desktop_file_dir_user_config_index = 0;
161 static guint desktop_file_dir_user_data_index;
162 static GMutex desktop_file_dir_lock;
163 static const gchar *gio_launch_desktop_path = NULL;
165 /* Monitor 'changed' signal handler {{{2 */
166 static void desktop_file_dir_reset (DesktopFileDir *dir);
169 * desktop_file_dir_get_alternative_dir:
170 * @dir: a #DesktopFileDir
172 * Gets the "alternative" directory to monitor in case the path
175 * If the path exists this will return NULL, otherwise it will return a
176 * parent directory of the path.
178 * This is used to avoid inotify on a non-existent directory (which
179 * results in polling).
181 * See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info.
184 desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
188 /* If the directory itself exists then we need no alternative. */
189 if (g_access (dir->path, R_OK | X_OK) == 0)
192 /* Otherwise, try the parent directories until we find one. */
193 parent = g_path_get_dirname (dir->path);
195 while (g_access (parent, R_OK | X_OK) != 0)
199 parent = g_path_get_dirname (tmp);
201 /* If somehow we get to '/' or '.' then just stop... */
202 if (g_str_equal (parent, tmp))
215 desktop_file_dir_changed (GFileMonitor *monitor,
218 GFileMonitorEvent event_type,
221 DesktopFileDir *dir = user_data;
222 gboolean do_nothing = FALSE;
224 /* We are not interested in receiving notifications forever just
225 * because someone asked about one desktop file once.
227 * After we receive the first notification, reset the dir, destroying
228 * the monitor. We will take this as a hint, next time that we are
229 * asked, that we need to check if everything is up to date.
231 * If this is a notification for a parent directory (because the
232 * desktop directory didn't exist) then we shouldn't fire the signal
233 * unless something actually changed.
235 g_mutex_lock (&desktop_file_dir_lock);
237 if (dir->alternatively_watching)
239 gchar *alternative_dir;
241 alternative_dir = desktop_file_dir_get_alternative_dir (dir);
242 do_nothing = alternative_dir && g_str_equal (dir->alternatively_watching, alternative_dir);
243 g_free (alternative_dir);
247 desktop_file_dir_reset (dir);
249 g_mutex_unlock (&desktop_file_dir_lock);
251 /* Notify anyone else who may be interested */
253 g_app_info_monitor_fire ();
256 /* Internal utility functions {{{2 */
259 * desktop_file_dir_app_name_is_masked:
260 * @dir: a #DesktopFileDir
261 * @app_name: an application ID
263 * Checks if @app_name is masked for @dir.
265 * An application is masked if a similarly-named desktop file exists in
266 * a desktop file directory with higher precedence. Masked desktop
267 * files should be ignored.
270 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
271 const gchar *app_name)
273 while (dir > desktop_file_dirs)
277 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
284 static const gchar * const *
285 get_lowercase_current_desktops (void)
287 static gchar **result;
289 if (g_once_init_enter (&result))
294 envvar = g_getenv ("XDG_CURRENT_DESKTOP");
300 tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
302 for (i = 0; tmp[i]; i++)
303 for (j = 0; tmp[i][j]; j++)
304 tmp[i][j] = g_ascii_tolower (tmp[i][j]);
307 tmp = g_new0 (gchar *, 0 + 1);
309 g_once_init_leave (&result, tmp);
312 return (const gchar **) result;
315 static const gchar * const *
316 get_current_desktops (const gchar *value)
318 static gchar **result;
320 if (g_once_init_enter (&result))
325 value = g_getenv ("XDG_CURRENT_DESKTOP");
330 tmp = g_strsplit (value, ":", 0);
332 g_once_init_leave (&result, tmp);
335 return (const gchar **) result;
339 * add_to_table_if_appropriate:
340 * @apps: a string to GDesktopAppInfo hash table
341 * @app_name: the name of the application
342 * @info: a #GDesktopAppInfo, or NULL
344 * If @info is non-%NULL and non-hidden, then add it to @apps, using
345 * @app_name as a key.
347 * If @info is non-%NULL then this function will consume the passed-in
351 add_to_table_if_appropriate (GHashTable *apps,
352 const gchar *app_name,
353 GDesktopAppInfo *info)
360 g_object_unref (info);
364 g_free (info->desktop_id);
365 info->desktop_id = g_strdup (app_name);
367 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
374 DESKTOP_KEY_GenericName,
375 DESKTOP_KEY_Keywords,
377 DESKTOP_KEY_X_GNOME_FullName,
382 const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
383 /* Note: lower numbers are a better match.
385 * In case we want two keys to match at the same level, we can just
386 * use the same number for the two different keys.
388 [DESKTOP_KEY_Name] = 1,
389 [DESKTOP_KEY_Exec] = 2,
390 [DESKTOP_KEY_Keywords] = 3,
391 [DESKTOP_KEY_GenericName] = 4,
392 [DESKTOP_KEY_X_GNOME_FullName] = 5,
393 [DESKTOP_KEY_Comment] = 6
396 /* Common prefix commands to ignore from Exec= lines */
397 const char * const exec_key_match_blacklist[] = {
413 desktop_key_get_name (guint key_id)
417 case DESKTOP_KEY_Comment:
419 case DESKTOP_KEY_Exec:
421 case DESKTOP_KEY_GenericName:
422 return GENERIC_NAME_KEY;
423 case DESKTOP_KEY_Keywords:
425 case DESKTOP_KEY_Name:
427 case DESKTOP_KEY_X_GNOME_FullName:
428 return FULL_NAME_KEY;
430 g_assert_not_reached ();
434 /* Search global state {{{2
436 * We only ever search under a global lock, so we can use (and reuse)
437 * some global data to reduce allocations made while searching.
439 * In short, we keep around arrays of results that we expand as needed
440 * (and never shrink).
442 * static_token_results: this is where we append the results for each
443 * token within a given desktop directory, as we handle it (which is
444 * a union of all matches for this term)
446 * static_search_results: this is where we build the complete results
447 * for a single directory (which is an intersection of the matches
448 * found for each term)
450 * static_total_results: this is where we build the complete results
451 * across all directories (which is a union of the matches found in
454 * The app_names that enter these tables are always pointer-unique (in
455 * the sense that string equality is the same as pointer equality).
456 * This can be guaranteed for two reasons:
458 * - we mask appids so that a given appid will only ever appear within
459 * the highest-precedence directory that contains it. We never
460 * return search results from a lower-level directory if a desktop
461 * file exists in a higher-level one.
463 * - within a given directory, the string is unique because it's the
464 * key in the hashtable of all app_ids for that directory.
466 * We perform a merging of the results in merge_token_results(). This
467 * works by ordering the two lists and moving through each of them (at
468 * the same time) looking for common elements, rejecting uncommon ones.
469 * "Order" here need not mean any particular thing, as long as it is
470 * some order. Because of the uniqueness of our strings, we can use
471 * pointer order. That's what's going on in compare_results() below.
475 const gchar *app_name;
479 static struct search_result *static_token_results;
480 static gint static_token_results_size;
481 static gint static_token_results_allocated;
482 static struct search_result *static_search_results;
483 static gint static_search_results_size;
484 static gint static_search_results_allocated;
485 static struct search_result *static_total_results;
486 static gint static_total_results_size;
487 static gint static_total_results_allocated;
489 /* And some functions for performing nice operations against it */
491 compare_results (gconstpointer a,
494 const struct search_result *ra = a;
495 const struct search_result *rb = b;
497 if (ra->app_name < rb->app_name)
500 else if (ra->app_name > rb->app_name)
504 return ra->category - rb->category;
508 compare_categories (gconstpointer a,
511 const struct search_result *ra = a;
512 const struct search_result *rb = b;
514 return ra->category - rb->category;
518 add_token_result (const gchar *app_name,
521 if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
523 static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
524 static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
527 static_token_results[static_token_results_size].app_name = app_name;
528 static_token_results[static_token_results_size].category = category;
529 static_token_results_size++;
533 merge_token_results (gboolean first)
535 if (static_token_results_size != 0)
536 qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
538 /* If this is the first token then we are basically merging a list with
539 * itself -- we only perform de-duplication.
541 * If this is not the first token then we are doing a real merge.
545 const gchar *last_name = NULL;
548 /* We must de-duplicate, but we do so by taking the best category
551 * The final list can be as large as the input here, so make sure
552 * we have enough room (even if it's too much room).
555 if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
557 static_search_results_allocated = static_token_results_allocated;
558 static_search_results = g_renew (struct search_result,
559 static_search_results,
560 static_search_results_allocated);
563 for (i = 0; i < static_token_results_size; i++)
565 /* The list is sorted so that the best match for a given id
566 * will be at the front, so once we have copied an id, skip
567 * the rest of the entries for the same id.
569 if (static_token_results[i].app_name == last_name)
572 last_name = static_token_results[i].app_name;
574 static_search_results[static_search_results_size++] = static_token_results[i];
579 const gchar *last_name = NULL;
583 /* We only ever remove items from the results list, so no need to
584 * resize to ensure that we have enough room.
586 for (i = 0; i < static_token_results_size; i++)
588 if (static_token_results[i].app_name == last_name)
591 last_name = static_token_results[i].app_name;
593 /* Now we only want to have a result in static_search_results
594 * if we already have it there *and* we have it in
595 * static_token_results as well. The category will be the
598 * Skip past the results in static_search_results that are not
599 * going to be matches.
601 while (k < static_search_results_size &&
602 static_search_results[k].app_name < static_token_results[i].app_name)
605 if (k < static_search_results_size &&
606 static_search_results[k].app_name == static_token_results[i].app_name)
610 * Category should be the worse of the two (ie:
611 * numerically larger).
613 static_search_results[j].app_name = static_search_results[k].app_name;
614 static_search_results[j].category = MAX (static_search_results[k].category,
615 static_token_results[i].category);
620 static_search_results_size = j;
623 /* Clear it out for next time... */
624 static_token_results_size = 0;
628 reset_total_search_results (void)
630 static_total_results_size = 0;
634 sort_total_search_results (void)
636 if (static_total_results_size != 0)
637 qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
641 merge_directory_results (void)
643 if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
645 static_total_results_allocated = MAX (16, static_total_results_allocated);
646 while (static_total_results_allocated < static_total_results_size + static_search_results_size)
647 static_total_results_allocated *= 2;
648 static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
651 if (static_total_results + static_total_results_size != 0)
652 memcpy (static_total_results + static_total_results_size,
653 static_search_results,
654 static_search_results_size * sizeof (struct search_result));
656 static_total_results_size += static_search_results_size;
658 /* Clear it out for next time... */
659 static_search_results_size = 0;
662 /* Support for unindexed DesktopFileDirs {{{2 */
664 get_apps_from_dir (GHashTable **apps,
668 const char *basename;
671 dir = g_dir_open (dirname, 0, NULL);
676 while ((basename = g_dir_read_name (dir)) != NULL)
680 filename = g_build_filename (dirname, basename, NULL);
682 if (g_str_has_suffix (basename, ".desktop"))
686 app_name = g_strconcat (prefix, basename, NULL);
689 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
691 g_hash_table_insert (*apps, app_name, g_strdup (filename));
693 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
697 subprefix = g_strconcat (prefix, basename, "-", NULL);
698 get_apps_from_dir (apps, filename, subprefix);
713 } UnindexedMimeTweaks;
716 free_mime_tweaks (gpointer data)
718 UnindexedMimeTweaks *tweaks = data;
720 g_strfreev (tweaks->additions);
721 g_strfreev (tweaks->removals);
722 g_strfreev (tweaks->defaults);
724 g_slice_free (UnindexedMimeTweaks, tweaks);
727 static UnindexedMimeTweaks *
728 desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
729 const gchar *mime_type)
731 UnindexedMimeTweaks *tweaks;
732 gchar *unaliased_type;
734 unaliased_type = _g_unix_content_type_unalias (mime_type);
735 tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type);
739 tweaks = g_slice_new0 (UnindexedMimeTweaks);
740 g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
743 g_free (unaliased_type);
748 /* consumes 'to_add' */
750 expand_strv (gchar ***strv_ptr,
752 gchar * const *blacklist)
754 guint strv_len, add_len;
765 strv_len = g_strv_length (strv);
766 add_len = g_strv_length (to_add);
767 strv = g_renew (gchar *, strv, strv_len + add_len + 1);
769 for (i = 0; to_add[i]; i++)
771 /* Don't add blacklisted strings */
773 for (j = 0; blacklist[j]; j++)
774 if (g_str_equal (to_add[i], blacklist[j]))
777 /* Don't add duplicates already in the list */
778 for (j = 0; j < strv_len; j++)
779 if (g_str_equal (to_add[i], strv[j]))
782 strv[strv_len++] = to_add[i];
789 strv[strv_len] = NULL;
796 desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
797 const gchar *filename,
798 const gchar *added_group,
799 gboolean tweaks_permitted)
801 UnindexedMimeTweaks *tweaks;
802 char **desktop_file_ids;
807 key_file = g_key_file_new ();
808 if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
810 g_key_file_free (key_file);
814 mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL);
816 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
818 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
819 "mimeapps.list file may add or remove associations.", filename, added_group);
820 g_strfreev (mime_types);
824 if (mime_types != NULL)
826 for (i = 0; mime_types[i] != NULL; i++)
828 desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL);
830 if (desktop_file_ids)
832 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
833 expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
837 g_strfreev (mime_types);
840 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
842 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
844 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
845 "mimeapps.list file may add or remove associations.", filename, REMOVED_ASSOCIATIONS_GROUP);
846 g_strfreev (mime_types);
850 if (mime_types != NULL)
852 for (i = 0; mime_types[i] != NULL; i++)
854 desktop_file_ids = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP, mime_types[i], NULL, NULL);
856 if (desktop_file_ids)
858 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
859 expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
863 g_strfreev (mime_types);
866 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
868 if (mime_types != NULL)
870 for (i = 0; mime_types[i] != NULL; i++)
872 desktop_file_ids = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP, mime_types[i], NULL, NULL);
874 if (desktop_file_ids)
876 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
877 expand_strv (&tweaks->defaults, desktop_file_ids, NULL);
881 g_strfreev (mime_types);
884 g_key_file_free (key_file);
888 desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
890 const gchar * const *desktops;
894 dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
896 /* We process in order of precedence, using a blacklisting approach to
897 * avoid recording later instructions that conflict with ones we found
900 * We first start with the XDG_CURRENT_DESKTOP files, in precedence
903 desktops = get_lowercase_current_desktops ();
904 for (i = 0; desktops[i]; i++)
906 filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]);
907 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
911 /* Next, the non-desktop-specific mimeapps.list */
912 filename = g_strdup_printf ("%s/mimeapps.list", dir->path);
913 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, TRUE);
916 /* The remaining files are only checked for in directories that might
917 * contain desktop files (ie: not the config dirs).
922 /* We have 'defaults.list' which was only ever understood by GLib. It
923 * exists widely, but it has never been part of any spec and it should
924 * be treated as deprecated. This will be removed in a future
927 filename = g_strdup_printf ("%s/defaults.list", dir->path);
928 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
931 /* Finally, the mimeinfo.cache, which is just a cached copy of what we
932 * would find in the MimeTypes= lines of all of the desktop files.
934 filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path);
935 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, MIME_CACHE_GROUP, TRUE);
940 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
943 get_apps_from_dir (&dir->app_names, dir->path, "");
945 desktop_file_dir_unindexed_read_mimeapps_lists (dir);
948 static GDesktopAppInfo *
949 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
950 const gchar *desktop_id)
952 const gchar *filename;
954 filename = g_hash_table_lookup (dir->app_names, desktop_id);
959 return g_desktop_app_info_new_from_filename (filename);
963 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
970 if (dir->app_names == NULL)
973 g_hash_table_iter_init (&iter, dir->app_names);
974 while (g_hash_table_iter_next (&iter, &app_name, &filename))
976 if (desktop_file_dir_app_name_is_masked (dir, app_name))
979 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
983 typedef struct _MemoryIndexEntry MemoryIndexEntry;
984 typedef GHashTable MemoryIndex;
986 struct _MemoryIndexEntry
988 const gchar *app_name; /* pointer to the hashtable key */
990 MemoryIndexEntry *next;
994 memory_index_entry_free (gpointer data)
996 MemoryIndexEntry *mie = data;
1000 MemoryIndexEntry *next = mie->next;
1002 g_slice_free (MemoryIndexEntry, mie);
1008 memory_index_add_token (MemoryIndex *mi,
1010 gint match_category,
1011 const gchar *app_name)
1013 MemoryIndexEntry *mie, *first;
1015 mie = g_slice_new (MemoryIndexEntry);
1016 mie->app_name = app_name;
1017 mie->match_category = match_category;
1019 first = g_hash_table_lookup (mi, token);
1023 mie->next = first->next;
1029 g_hash_table_insert (mi, g_strdup (token), mie);
1034 memory_index_add_string (MemoryIndex *mi,
1035 const gchar *string,
1036 gint match_category,
1037 const gchar *app_name)
1039 gchar **tokens, **alternates;
1042 tokens = g_str_tokenize_and_fold (string, NULL, &alternates);
1044 for (i = 0; tokens[i]; i++)
1045 memory_index_add_token (mi, tokens[i], match_category, app_name);
1047 for (i = 0; alternates[i]; i++)
1048 memory_index_add_token (mi, alternates[i], match_category, app_name);
1050 g_strfreev (alternates);
1051 g_strfreev (tokens);
1054 static MemoryIndex *
1055 memory_index_new (void)
1057 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free);
1061 desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
1063 GHashTableIter iter;
1066 dir->memory_index = memory_index_new ();
1067 dir->memory_implementations = memory_index_new ();
1069 /* Nothing to search? */
1070 if (dir->app_names == NULL)
1073 g_hash_table_iter_init (&iter, dir->app_names);
1074 while (g_hash_table_iter_next (&iter, &app, &path))
1078 if (desktop_file_dir_app_name_is_masked (dir, app))
1081 key_file = g_key_file_new ();
1083 if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) &&
1084 !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
1086 /* Index the interesting keys... */
1090 for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
1095 if (!desktop_key_match_category[i])
1098 raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
1101 if (i == DESKTOP_KEY_Exec && raw != NULL)
1103 /* Special handling: only match basename of first field */
1107 /* Remove extra arguments, if any */
1108 space = raw + strcspn (raw, " \t\n"); /* IFS */
1111 /* Skip the pathname, if any */
1112 if ((slash = strrchr (raw, '/')))
1115 /* Don't match on blacklisted binaries like interpreters */
1116 if (g_strv_contains (exec_key_match_blacklist, value))
1121 memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
1126 /* Make note of the Implements= line */
1127 implements = g_key_file_get_string_list (key_file, "Desktop Entry", "Implements", NULL, NULL);
1128 for (i = 0; implements && implements[i]; i++)
1129 memory_index_add_token (dir->memory_implementations, implements[i], 0, app);
1130 g_strfreev (implements);
1133 g_key_file_free (key_file);
1138 desktop_file_dir_unindexed_search (DesktopFileDir *dir,
1139 const gchar *search_token)
1141 GHashTableIter iter;
1142 gpointer key, value;
1144 if (!dir->memory_index)
1145 desktop_file_dir_unindexed_setup_search (dir);
1147 g_hash_table_iter_init (&iter, dir->memory_index);
1148 while (g_hash_table_iter_next (&iter, &key, &value))
1150 MemoryIndexEntry *mie = value;
1152 if (!g_str_has_prefix (key, search_token))
1157 add_token_result (mie->app_name, mie->match_category);
1164 array_contains (GPtrArray *array,
1169 for (i = 0; i < array->len; i++)
1170 if (g_str_equal (array->pdata[i], str))
1177 desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
1178 const gchar *mime_type,
1180 GPtrArray *blacklist)
1182 UnindexedMimeTweaks *tweaks;
1185 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1190 if (tweaks->additions)
1192 for (i = 0; tweaks->additions[i]; i++)
1194 gchar *app_name = tweaks->additions[i];
1196 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1197 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1198 g_ptr_array_add (hits, app_name);
1202 if (tweaks->removals)
1204 for (i = 0; tweaks->removals[i]; i++)
1206 gchar *app_name = tweaks->removals[i];
1208 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1209 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1210 g_ptr_array_add (blacklist, app_name);
1216 desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir,
1217 const gchar *mime_type,
1220 UnindexedMimeTweaks *tweaks;
1223 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1225 if (!tweaks || !tweaks->defaults)
1228 for (i = 0; tweaks->defaults[i]; i++)
1230 gchar *app_name = tweaks->defaults[i];
1232 if (!array_contains (results, app_name))
1233 g_ptr_array_add (results, app_name);
1238 desktop_file_dir_unindexed_get_implementations (DesktopFileDir *dir,
1240 const gchar *interface)
1242 MemoryIndexEntry *mie;
1244 if (!dir->memory_index)
1245 desktop_file_dir_unindexed_setup_search (dir);
1247 for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next)
1248 *results = g_list_prepend (*results, g_strdup (mie->app_name));
1251 /* DesktopFileDir "API" {{{2 */
1254 * desktop_file_dir_create:
1255 * @array: the #GArray to add a new item to
1256 * @data_dir: an XDG_DATA_DIR
1258 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
1262 desktop_file_dir_create (GArray *array,
1263 const gchar *data_dir)
1265 DesktopFileDir dir = { 0, };
1267 dir.path = g_build_filename (data_dir, "applications", NULL);
1269 g_array_append_val (array, dir);
1273 * desktop_file_dir_create:
1274 * @array: the #GArray to add a new item to
1275 * @config_dir: an XDG_CONFIG_DIR
1277 * Just the same as desktop_file_dir_create() except that it does not
1278 * add the "applications" directory. It also marks the directory as
1279 * config-only, which prevents us from attempting to find desktop files
1283 desktop_file_dir_create_for_config (GArray *array,
1284 const gchar *config_dir)
1286 DesktopFileDir dir = { 0, };
1288 dir.path = g_strdup (config_dir);
1289 dir.is_config = TRUE;
1291 g_array_append_val (array, dir);
1295 * desktop_file_dir_reset:
1296 * @dir: a #DesktopFileDir
1298 * Cleans up @dir, releasing most resources that it was using.
1301 desktop_file_dir_reset (DesktopFileDir *dir)
1303 if (dir->alternatively_watching)
1305 g_free (dir->alternatively_watching);
1306 dir->alternatively_watching = NULL;
1311 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
1312 g_object_unref (dir->monitor);
1313 dir->monitor = NULL;
1318 g_hash_table_unref (dir->app_names);
1319 dir->app_names = NULL;
1322 if (dir->memory_index)
1324 g_hash_table_unref (dir->memory_index);
1325 dir->memory_index = NULL;
1328 if (dir->mime_tweaks)
1330 g_hash_table_unref (dir->mime_tweaks);
1331 dir->mime_tweaks = NULL;
1334 if (dir->memory_implementations)
1336 g_hash_table_unref (dir->memory_implementations);
1337 dir->memory_implementations = NULL;
1340 dir->is_setup = FALSE;
1344 * desktop_file_dir_init:
1345 * @dir: a #DesktopFileDir
1347 * Does initial setup for @dir
1349 * You should only call this if @dir is not already setup.
1352 desktop_file_dir_init (DesktopFileDir *dir)
1354 const gchar *watch_dir;
1356 g_assert (!dir->is_setup);
1358 g_assert (!dir->alternatively_watching);
1359 g_assert (!dir->monitor);
1361 dir->alternatively_watching = desktop_file_dir_get_alternative_dir (dir);
1362 watch_dir = dir->alternatively_watching ? dir->alternatively_watching : dir->path;
1364 /* There is a very thin race here if the watch_dir has been _removed_
1365 * between when we checked for it and when we establish the watch.
1366 * Removes probably don't happen in usual operation, and even if it
1367 * does (and we catch the unlikely race), the only degradation is that
1368 * we will fall back to polling.
1370 dir->monitor = g_local_file_monitor_new_in_worker (watch_dir, TRUE, G_FILE_MONITOR_NONE,
1371 desktop_file_dir_changed, dir, NULL);
1373 desktop_file_dir_unindexed_init (dir);
1375 dir->is_setup = TRUE;
1379 * desktop_file_dir_get_app:
1380 * @dir: a DesktopFileDir
1381 * @desktop_id: the desktop ID to load
1383 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
1384 * within @dir, even if it is hidden.
1386 * This function does not check if @desktop_id would be masked by a
1387 * directory with higher precedence. The caller must do so.
1389 static GDesktopAppInfo *
1390 desktop_file_dir_get_app (DesktopFileDir *dir,
1391 const gchar *desktop_id)
1393 if (!dir->app_names)
1396 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
1400 * desktop_file_dir_get_all:
1401 * @dir: a DesktopFileDir
1402 * @apps: a #GHashTable<string, GDesktopAppInfo>
1404 * Loads all desktop files in @dir and adds them to @apps, careful to
1405 * ensure we don't add any files masked by a similarly-named file in a
1406 * higher-precedence directory.
1409 desktop_file_dir_get_all (DesktopFileDir *dir,
1412 desktop_file_dir_unindexed_get_all (dir, apps);
1416 * desktop_file_dir_mime_lookup:
1417 * @dir: a #DesktopFileDir
1418 * @mime_type: the mime type to look up
1419 * @hits: the array to store the hits
1420 * @blacklist: the array to store the blacklist
1422 * Does a lookup of a mimetype against one desktop file directory,
1423 * recording any hits and blacklisting and "Removed" associations (so
1424 * later directories don't record them as hits).
1426 * The items added to @hits are duplicated, but the ones in @blacklist
1427 * are weak pointers. This facilitates simply freeing the blacklist
1428 * (which is only used for internal bookkeeping) but using the pdata of
1429 * @hits as the result of the operation.
1432 desktop_file_dir_mime_lookup (DesktopFileDir *dir,
1433 const gchar *mime_type,
1435 GPtrArray *blacklist)
1437 desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blacklist);
1441 * desktop_file_dir_default_lookup:
1442 * @dir: a #DesktopFileDir
1443 * @mime_type: the mime type to look up
1444 * @results: an array to store the results in
1446 * Collects the "default" applications for a given mime type from @dir.
1449 desktop_file_dir_default_lookup (DesktopFileDir *dir,
1450 const gchar *mime_type,
1453 desktop_file_dir_unindexed_default_lookup (dir, mime_type, results);
1457 * desktop_file_dir_search:
1458 * @dir: a #DesktopFileDir
1459 * @term: a normalised and casefolded search term
1461 * Finds the names of applications in @dir that match @term.
1464 desktop_file_dir_search (DesktopFileDir *dir,
1465 const gchar *search_token)
1467 desktop_file_dir_unindexed_search (dir, search_token);
1471 desktop_file_dir_get_implementations (DesktopFileDir *dir,
1473 const gchar *interface)
1475 desktop_file_dir_unindexed_get_implementations (dir, results, interface);
1478 /* Lock/unlock and global setup API {{{2 */
1481 desktop_file_dirs_lock (void)
1484 const gchar *user_config_dir = g_get_user_config_dir ();
1486 g_mutex_lock (&desktop_file_dir_lock);
1488 /* If the XDG dirs configuration has changed (expected only during tests),
1489 * clear and reload the state. */
1490 if (g_strcmp0 (desktop_file_dirs_config_dir, user_config_dir) != 0)
1492 g_debug ("%s: Resetting desktop app info dirs from %s to %s",
1493 G_STRFUNC, desktop_file_dirs_config_dir, user_config_dir);
1495 for (i = 0; i < n_desktop_file_dirs; i++)
1496 desktop_file_dir_reset (&desktop_file_dirs[i]);
1497 g_clear_pointer (&desktop_file_dirs, g_free);
1498 n_desktop_file_dirs = 0;
1499 desktop_file_dir_user_data_index = 0;
1502 if (desktop_file_dirs == NULL)
1504 const char * const *dirs;
1508 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
1510 /* First, the configs. Highest priority: the user's ~/.config */
1511 desktop_file_dir_create_for_config (tmp, user_config_dir);
1513 /* Next, the system configs (/etc/xdg, and so on). */
1514 dirs = g_get_system_config_dirs ();
1515 for (i = 0; dirs[i]; i++)
1516 desktop_file_dir_create_for_config (tmp, dirs[i]);
1518 /* Now the data. Highest priority: the user's ~/.local/share/applications */
1519 desktop_file_dir_user_data_index = tmp->len;
1520 desktop_file_dir_create (tmp, g_get_user_data_dir ());
1522 /* Following that, XDG_DATA_DIRS/applications, in order */
1523 dirs = g_get_system_data_dirs ();
1524 for (i = 0; dirs[i]; i++)
1525 desktop_file_dir_create (tmp, dirs[i]);
1527 /* The list of directories will never change after this, unless
1528 * g_get_user_config_dir() changes due to %G_TEST_OPTION_ISOLATE_DIRS. */
1529 desktop_file_dirs = (DesktopFileDir *) tmp->data;
1530 n_desktop_file_dirs = tmp->len;
1531 desktop_file_dirs_config_dir = user_config_dir;
1533 g_array_free (tmp, FALSE);
1536 for (i = 0; i < n_desktop_file_dirs; i++)
1537 if (!desktop_file_dirs[i].is_setup)
1538 desktop_file_dir_init (&desktop_file_dirs[i]);
1542 desktop_file_dirs_unlock (void)
1544 g_mutex_unlock (&desktop_file_dir_lock);
1548 desktop_file_dirs_invalidate_user_config (void)
1550 g_mutex_lock (&desktop_file_dir_lock);
1552 if (n_desktop_file_dirs)
1553 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_config_index]);
1555 g_mutex_unlock (&desktop_file_dir_lock);
1559 desktop_file_dirs_invalidate_user_data (void)
1561 g_mutex_lock (&desktop_file_dir_lock);
1563 if (n_desktop_file_dirs)
1564 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_data_index]);
1566 g_mutex_unlock (&desktop_file_dir_lock);
1569 /* GDesktopAppInfo implementation {{{1 */
1570 /* GObject implementation {{{2 */
1572 g_desktop_app_info_finalize (GObject *object)
1574 GDesktopAppInfo *info;
1576 info = G_DESKTOP_APP_INFO (object);
1578 g_free (info->desktop_id);
1579 g_free (info->filename);
1582 g_key_file_unref (info->keyfile);
1584 g_free (info->name);
1585 g_free (info->generic_name);
1586 g_free (info->fullname);
1587 g_free (info->comment);
1588 g_free (info->icon_name);
1590 g_object_unref (info->icon);
1591 g_strfreev (info->keywords);
1592 g_strfreev (info->only_show_in);
1593 g_strfreev (info->not_show_in);
1594 g_free (info->try_exec);
1595 g_free (info->exec);
1596 g_free (info->binary);
1597 g_free (info->path);
1598 g_free (info->categories);
1599 g_free (info->startup_wm_class);
1600 g_strfreev (info->mime_types);
1601 g_free (info->app_id);
1602 g_strfreev (info->actions);
1604 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
1608 g_desktop_app_info_set_property (GObject *object,
1610 const GValue *value,
1613 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1618 self->filename = g_value_dup_string (value);
1622 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1628 g_desktop_app_info_get_property (GObject *object,
1633 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1638 g_value_set_string (value, self->filename);
1641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1647 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
1649 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1651 gobject_class->get_property = g_desktop_app_info_get_property;
1652 gobject_class->set_property = g_desktop_app_info_set_property;
1653 gobject_class->finalize = g_desktop_app_info_finalize;
1656 * GDesktopAppInfo:filename:
1658 * The origin filename of this #GDesktopAppInfo
1660 g_object_class_install_property (gobject_class,
1662 g_param_spec_string ("filename", "Filename", "", NULL,
1663 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1667 g_desktop_app_info_init (GDesktopAppInfo *local)
1671 /* Construction... {{{2 */
1675 * @exec: an exec line
1677 * Returns the first word in an exec line (ie: the binary name).
1679 * If @exec is " progname --foo %F" then returns "progname".
1682 binary_from_exec (const char *exec)
1684 const char *p, *start;
1690 while (*p != ' ' && *p != 0)
1693 return g_strndup (start, p - start);
1697 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
1704 gboolean bus_activatable;
1706 start_group = g_key_file_get_start_group (key_file);
1707 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
1709 g_free (start_group);
1712 g_free (start_group);
1714 type = g_key_file_get_string (key_file,
1715 G_KEY_FILE_DESKTOP_GROUP,
1716 G_KEY_FILE_DESKTOP_KEY_TYPE,
1718 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
1725 try_exec = g_key_file_get_string (key_file,
1726 G_KEY_FILE_DESKTOP_GROUP,
1727 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
1729 if (try_exec && try_exec[0] != '\0')
1732 t = g_find_program_in_path (try_exec);
1741 exec = g_key_file_get_string (key_file,
1742 G_KEY_FILE_DESKTOP_GROUP,
1743 G_KEY_FILE_DESKTOP_KEY_EXEC,
1745 if (exec && exec[0] != '\0')
1749 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
1758 t = g_find_program_in_path (argv[0]);
1771 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
1772 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
1773 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
1774 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
1775 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
1776 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
1777 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
1778 info->only_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN, NULL, NULL);
1779 info->not_show_in = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN, NULL, NULL);
1780 info->try_exec = try_exec;
1782 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
1783 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
1784 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
1785 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
1786 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
1787 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
1788 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
1789 info->mime_types = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL);
1790 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
1791 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
1793 /* Remove the special-case: no Actions= key just means 0 extra actions */
1794 if (info->actions == NULL)
1795 info->actions = g_new0 (gchar *, 0 + 1);
1798 if (info->icon_name)
1800 if (g_path_is_absolute (info->icon_name))
1804 file = g_file_new_for_path (info->icon_name);
1805 info->icon = g_file_icon_new (file);
1806 g_object_unref (file);
1812 /* Work around a common mistake in desktop files */
1813 if ((p = strrchr (info->icon_name, '.')) != NULL &&
1814 (strcmp (p, ".png") == 0 ||
1815 strcmp (p, ".xpm") == 0 ||
1816 strcmp (p, ".svg") == 0))
1819 info->icon = g_themed_icon_new (info->icon_name);
1824 info->binary = binary_from_exec (info->exec);
1826 if (info->path && info->path[0] == '\0')
1828 g_free (info->path);
1832 /* Can only be DBusActivatable if we know the filename, which means
1833 * that this won't work for the load-from-keyfile case.
1835 if (bus_activatable && info->filename)
1840 basename = g_path_get_basename (info->filename);
1841 last_dot = strrchr (basename, '.');
1843 if (last_dot && g_str_equal (last_dot, ".desktop"))
1847 if (g_dbus_is_name (basename) && basename[0] != ':')
1848 info->app_id = g_strdup (basename);
1854 info->keyfile = g_key_file_ref (key_file);
1860 g_desktop_app_info_load_file (GDesktopAppInfo *self)
1863 gboolean retval = FALSE;
1865 g_return_val_if_fail (self->filename != NULL, FALSE);
1867 self->desktop_id = g_path_get_basename (self->filename);
1869 key_file = g_key_file_new ();
1871 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
1872 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
1874 g_key_file_unref (key_file);
1879 * g_desktop_app_info_new_from_keyfile:
1880 * @key_file: an opened #GKeyFile
1882 * Creates a new #GDesktopAppInfo.
1884 * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
1889 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
1891 GDesktopAppInfo *info;
1893 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1894 info->filename = NULL;
1895 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
1897 g_object_unref (info);
1904 * g_desktop_app_info_new_from_filename:
1905 * @filename: (type filename): the path of a desktop file, in the GLib
1908 * Creates a new #GDesktopAppInfo.
1910 * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
1913 g_desktop_app_info_new_from_filename (const char *filename)
1915 GDesktopAppInfo *info = NULL;
1917 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
1918 if (!g_desktop_app_info_load_file (info))
1920 g_object_unref (info);
1927 * g_desktop_app_info_new:
1928 * @desktop_id: the desktop file id
1930 * Creates a new #GDesktopAppInfo based on a desktop file id.
1932 * A desktop file id is the basename of the desktop file, including the
1933 * .desktop extension. GIO is looking for a desktop file with this name
1934 * in the `applications` subdirectories of the XDG
1935 * data directories (i.e. the directories specified in the `XDG_DATA_HOME`
1936 * and `XDG_DATA_DIRS` environment variables). GIO also supports the
1937 * prefix-to-subdirectory mapping that is described in the
1938 * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/)
1939 * (i.e. a desktop id of kde-foo.desktop will match
1940 * `/usr/share/applications/kde/foo.desktop`).
1942 * Returns: (nullable): a new #GDesktopAppInfo, or %NULL if no desktop
1943 * file with that id exists.
1946 g_desktop_app_info_new (const char *desktop_id)
1948 GDesktopAppInfo *appinfo = NULL;
1951 desktop_file_dirs_lock ();
1953 for (i = 0; i < n_desktop_file_dirs; i++)
1955 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
1961 desktop_file_dirs_unlock ();
1963 if (appinfo == NULL)
1966 g_free (appinfo->desktop_id);
1967 appinfo->desktop_id = g_strdup (desktop_id);
1969 if (g_desktop_app_info_get_is_hidden (appinfo))
1971 g_object_unref (appinfo);
1979 g_desktop_app_info_dup (GAppInfo *appinfo)
1981 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1982 GDesktopAppInfo *new_info;
1984 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1986 new_info->filename = g_strdup (info->filename);
1987 new_info->desktop_id = g_strdup (info->desktop_id);
1990 new_info->keyfile = g_key_file_ref (info->keyfile);
1992 new_info->name = g_strdup (info->name);
1993 new_info->generic_name = g_strdup (info->generic_name);
1994 new_info->fullname = g_strdup (info->fullname);
1995 new_info->keywords = g_strdupv (info->keywords);
1996 new_info->comment = g_strdup (info->comment);
1997 new_info->nodisplay = info->nodisplay;
1998 new_info->icon_name = g_strdup (info->icon_name);
2000 new_info->icon = g_object_ref (info->icon);
2001 new_info->only_show_in = g_strdupv (info->only_show_in);
2002 new_info->not_show_in = g_strdupv (info->not_show_in);
2003 new_info->try_exec = g_strdup (info->try_exec);
2004 new_info->exec = g_strdup (info->exec);
2005 new_info->binary = g_strdup (info->binary);
2006 new_info->path = g_strdup (info->path);
2007 new_info->app_id = g_strdup (info->app_id);
2008 new_info->hidden = info->hidden;
2009 new_info->terminal = info->terminal;
2010 new_info->startup_notify = info->startup_notify;
2012 return G_APP_INFO (new_info);
2015 /* GAppInfo interface implementation functions {{{2 */
2018 g_desktop_app_info_equal (GAppInfo *appinfo1,
2021 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
2022 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
2024 if (info1->desktop_id == NULL ||
2025 info2->desktop_id == NULL)
2026 return info1 == info2;
2028 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
2032 g_desktop_app_info_get_id (GAppInfo *appinfo)
2034 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2036 return info->desktop_id;
2040 g_desktop_app_info_get_name (GAppInfo *appinfo)
2042 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2044 if (info->name == NULL)
2045 return _("Unnamed");
2050 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
2052 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2054 if (info->fullname == NULL)
2055 return g_desktop_app_info_get_name (appinfo);
2056 return info->fullname;
2060 * g_desktop_app_info_get_is_hidden:
2061 * @info: a #GDesktopAppInfo.
2063 * A desktop file is hidden if the Hidden key in it is
2066 * Returns: %TRUE if hidden, %FALSE otherwise.
2069 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
2071 return info->hidden;
2075 * g_desktop_app_info_get_filename:
2076 * @info: a #GDesktopAppInfo
2078 * When @info was created from a known filename, return it. In some
2079 * situations such as the #GDesktopAppInfo returned from
2080 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
2082 * Returns: (type filename): The full path to the file for @info,
2083 * or %NULL if not known.
2087 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
2089 return info->filename;
2093 g_desktop_app_info_get_description (GAppInfo *appinfo)
2095 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2097 return info->comment;
2101 g_desktop_app_info_get_executable (GAppInfo *appinfo)
2103 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2105 return info->binary;
2109 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
2111 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2117 g_desktop_app_info_get_icon (GAppInfo *appinfo)
2119 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2125 * g_desktop_app_info_get_categories:
2126 * @info: a #GDesktopAppInfo
2128 * Gets the categories from the desktop file.
2130 * Returns: The unparsed Categories key from the desktop file;
2131 * i.e. no attempt is made to split it by ';' or validate it.
2134 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
2136 return info->categories;
2140 * g_desktop_app_info_get_keywords:
2141 * @info: a #GDesktopAppInfo
2143 * Gets the keywords from the desktop file.
2145 * Returns: (transfer none): The value of the Keywords key
2149 const char * const *
2150 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
2152 return (const char * const *)info->keywords;
2156 * g_desktop_app_info_get_generic_name:
2157 * @info: a #GDesktopAppInfo
2159 * Gets the generic name from the destkop file.
2161 * Returns: The value of the GenericName key
2164 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
2166 return info->generic_name;
2170 * g_desktop_app_info_get_nodisplay:
2171 * @info: a #GDesktopAppInfo
2173 * Gets the value of the NoDisplay key, which helps determine if the
2174 * application info should be shown in menus. See
2175 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
2177 * Returns: The value of the NoDisplay key
2182 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
2184 return info->nodisplay;
2188 * g_desktop_app_info_get_show_in:
2189 * @info: a #GDesktopAppInfo
2190 * @desktop_env: (nullable): a string specifying a desktop name
2192 * Checks if the application info should be shown in menus that list available
2193 * applications for a specific name of the desktop, based on the
2194 * `OnlyShowIn` and `NotShowIn` keys.
2196 * @desktop_env should typically be given as %NULL, in which case the
2197 * `XDG_CURRENT_DESKTOP` environment variable is consulted. If you want
2198 * to override the default mechanism then you may specify @desktop_env,
2199 * but this is not recommended.
2201 * Note that g_app_info_should_show() for @info will include this check (with
2202 * %NULL for @desktop_env) as well as additional checks.
2204 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
2205 * `OnlyShowIn` and `NotShowIn` keys, %FALSE
2211 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
2212 const gchar *desktop_env)
2214 const gchar *specified_envs[] = { desktop_env, NULL };
2215 const gchar * const *envs;
2218 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
2221 envs = specified_envs;
2223 envs = get_current_desktops (NULL);
2225 for (i = 0; envs[i]; i++)
2229 if (info->only_show_in)
2230 for (j = 0; info->only_show_in[j]; j++)
2231 if (g_str_equal (info->only_show_in[j], envs[i]))
2234 if (info->not_show_in)
2235 for (j = 0; info->not_show_in[j]; j++)
2236 if (g_str_equal (info->not_show_in[j], envs[i]))
2240 return info->only_show_in == NULL;
2243 /* Launching... {{{2 */
2246 expand_macro_single (char macro, const char *uri)
2249 char *result = NULL;
2253 file = g_file_new_for_uri (uri);
2259 result = g_shell_quote (uri);
2263 path = g_file_get_path (file);
2265 result = g_shell_quote (path);
2269 path = g_file_get_path (file);
2272 name = g_path_get_dirname (path);
2273 result = g_shell_quote (name);
2279 path = g_file_get_path (file);
2282 name = g_path_get_basename (path);
2283 result = g_shell_quote (name);
2289 g_object_unref (file);
2296 expand_macro_uri (char macro, const char *uri, gboolean force_file_uri, char force_file_uri_macro)
2298 char *expanded = NULL;
2300 g_return_val_if_fail (uri != NULL, NULL);
2302 if (!force_file_uri ||
2303 /* Pass URI if it contains an anchor */
2304 strchr (uri, '#') != NULL)
2306 expanded = expand_macro_single (macro, uri);
2310 expanded = expand_macro_single (force_file_uri_macro, uri);
2311 if (expanded == NULL)
2312 expanded = expand_macro_single (macro, uri);
2319 expand_macro (char macro,
2321 GDesktopAppInfo *info,
2324 GList *uris = *uri_list;
2325 char *expanded = NULL;
2326 gboolean force_file_uri;
2327 char force_file_uri_macro;
2330 g_return_if_fail (exec != NULL);
2332 /* On %u and %U, pass POSIX file path pointing to the URI via
2333 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
2334 * running or the URI doesn't have a POSIX file path via FUSE
2335 * we'll just pass the URI.
2337 force_file_uri_macro = macro;
2338 force_file_uri = FALSE;
2344 force_file_uri_macro = 'f';
2345 force_file_uri = TRUE;
2348 force_file_uri_macro = 'F';
2349 force_file_uri = TRUE;
2365 expanded = expand_macro_uri (macro, uri,
2366 force_file_uri, force_file_uri_macro);
2369 g_string_append (exec, expanded);
2384 expanded = expand_macro_uri (macro, uri,
2385 force_file_uri, force_file_uri_macro);
2388 g_string_append (exec, expanded);
2394 if (uris != NULL && expanded)
2395 g_string_append_c (exec, ' ');
2401 if (info->icon_name)
2403 g_string_append (exec, "--icon ");
2404 expanded = g_shell_quote (info->icon_name);
2405 g_string_append (exec, expanded);
2413 expanded = g_shell_quote (info->name);
2414 g_string_append (exec, expanded);
2422 expanded = g_shell_quote (info->filename);
2423 g_string_append (exec, expanded);
2428 case 'm': /* deprecated */
2432 g_string_append_c (exec, '%');
2440 expand_application_parameters (GDesktopAppInfo *info,
2441 const gchar *exec_line,
2447 GList *uri_list = *uris;
2448 const char *p = exec_line;
2449 GString *expanded_exec;
2452 if (exec_line == NULL)
2454 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2455 _("Desktop file didn’t specify Exec field"));
2459 expanded_exec = g_string_new (NULL);
2463 if (p[0] == '%' && p[1] != '\0')
2465 expand_macro (p[1], expanded_exec, info, uris);
2469 g_string_append_c (expanded_exec, *p);
2474 /* No file substitutions */
2475 if (uri_list == *uris && uri_list != NULL)
2477 /* If there is no macro default to %f. This is also what KDE does */
2478 g_string_append_c (expanded_exec, ' ');
2479 expand_macro ('f', expanded_exec, info, uris);
2482 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
2483 g_string_free (expanded_exec, TRUE);
2488 prepend_terminal_to_vector (int *argc,
2495 char **term_argv = NULL;
2500 g_return_val_if_fail (argc != NULL, FALSE);
2501 g_return_val_if_fail (argv != NULL, FALSE);
2509 /* compute size if not given */
2512 for (i = 0; the_argv[i] != NULL; i++)
2518 term_argv = g_new0 (char *, 3);
2520 check = g_find_program_in_path ("gnome-terminal");
2523 term_argv[0] = check;
2524 /* Since 2017, gnome-terminal has preferred `--` over `-x` or `-e`. */
2525 term_argv[1] = g_strdup ("--");
2530 check = g_find_program_in_path ("mate-terminal");
2532 check = g_find_program_in_path ("xfce4-terminal");
2535 term_argv[0] = check;
2536 /* Note that gnome-terminal takes -x and
2537 * as -e in gnome-terminal is broken we use that. */
2538 term_argv[1] = g_strdup ("-x");
2543 check = g_find_program_in_path ("nxterm");
2545 check = g_find_program_in_path ("color-xterm");
2547 check = g_find_program_in_path ("rxvt");
2549 check = g_find_program_in_path ("dtterm");
2552 check = g_strdup ("xterm");
2553 g_debug ("Couldn’t find a terminal: falling back to xterm");
2555 term_argv[0] = check;
2556 term_argv[1] = g_strdup ("-e");
2560 real_argc = term_argc + *argc;
2561 real_argv = g_new (char *, real_argc + 1);
2563 for (i = 0; i < term_argc; i++)
2564 real_argv[i] = term_argv[i];
2566 for (j = 0; j < *argc; j++, i++)
2567 real_argv[i] = (char *)the_argv[j];
2569 real_argv[i] = NULL;
2575 /* we use g_free here as we sucked all the inner strings
2576 * out from it into real_argv */
2581 #endif /* G_OS_WIN32 */
2585 create_files_for_uris (GList *uris)
2592 for (iter = uris; iter; iter = iter->next)
2594 GFile *file = g_file_new_for_uri ((char *)iter->data);
2595 res = g_list_prepend (res, file);
2598 return g_list_reverse (res);
2602 notify_desktop_launch (GDBusConnection *session_bus,
2603 GDesktopAppInfo *info,
2605 const char *display,
2610 GVariantBuilder uri_variant;
2611 GVariantBuilder extras_variant;
2613 const char *desktop_file_id;
2614 const char *gio_desktop_file;
2616 if (session_bus == NULL)
2619 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
2620 for (iter = uris; iter; iter = iter->next)
2621 g_variant_builder_add (&uri_variant, "s", iter->data);
2623 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
2624 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
2625 g_variant_builder_add (&extras_variant, "{sv}",
2629 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
2630 if (gio_desktop_file != NULL)
2631 g_variant_builder_add (&extras_variant, "{sv}",
2632 "origin-desktop-file",
2633 g_variant_new_bytestring (gio_desktop_file));
2634 if (g_get_prgname () != NULL)
2635 g_variant_builder_add (&extras_variant, "{sv}",
2637 g_variant_new_bytestring (g_get_prgname ()));
2638 g_variant_builder_add (&extras_variant, "{sv}",
2641 (gint64)getpid ()));
2644 desktop_file_id = info->filename;
2645 else if (info->desktop_id)
2646 desktop_file_id = info->desktop_id;
2648 desktop_file_id = "";
2650 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
2651 "org.gtk.gio.DesktopAppInfo",
2653 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
2654 g_variant_new_bytestring (desktop_file_id),
2655 display ? display : "",
2659 g_dbus_connection_send_message (session_bus,
2663 g_object_unref (msg);
2666 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
2669 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
2670 GDBusConnection *session_bus,
2671 const gchar *exec_line,
2673 GAppLaunchContext *launch_context,
2674 GSpawnFlags spawn_flags,
2675 GSpawnChildSetupFunc user_setup,
2676 gpointer user_setup_data,
2677 GDesktopAppLaunchCallback pid_callback,
2678 gpointer pid_callback_data,
2684 gboolean completed = FALSE;
2688 char **argv, **envp;
2691 g_return_val_if_fail (info != NULL, FALSE);
2696 envp = g_app_launch_context_get_environment (launch_context);
2698 envp = g_get_environ ();
2700 /* The GList* passed to expand_application_parameters() will be modified
2701 * internally by expand_macro(), so we need to pass a copy of it instead,
2702 * and also use that copy to control the exit condition of the loop below.
2704 dup_uris = g_list_copy (uris);
2708 GList *launched_uris;
2711 char **wrapped_argv;
2714 old_uris = dup_uris;
2715 if (!expand_application_parameters (info, exec_line, &dup_uris, &argc, &argv, error))
2718 /* Get the subset of URIs we're launching with this process */
2719 launched_uris = NULL;
2720 for (iter = old_uris; iter != NULL && iter != dup_uris; iter = iter->next)
2721 launched_uris = g_list_prepend (launched_uris, iter->data);
2722 launched_uris = g_list_reverse (launched_uris);
2724 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
2726 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2727 _("Unable to find terminal required for application"));
2732 envp = g_environ_setenv (envp,
2733 "GIO_LAUNCHED_DESKTOP_FILE",
2740 GList *launched_files = create_files_for_uris (launched_uris);
2742 if (info->startup_notify)
2744 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
2748 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
2751 g_list_free_full (launched_files, g_object_unref);
2754 if (g_once_init_enter (&gio_launch_desktop_path))
2758 /* Allow test suite to specify path to gio-launch-desktop */
2759 tmp = g_getenv ("GIO_LAUNCH_DESKTOP");
2761 /* Fall back on usual searching in $PATH */
2763 tmp = "gio-launch-desktop";
2764 g_once_init_leave (&gio_launch_desktop_path, tmp);
2767 wrapped_argv = g_new (char *, argc + 2);
2768 wrapped_argv[0] = g_strdup (gio_launch_desktop_path);
2770 for (i = 0; i < argc; i++)
2771 wrapped_argv[i + 1] = g_steal_pointer (&argv[i]);
2773 wrapped_argv[i + 1] = NULL;
2777 if (!g_spawn_async_with_fds (info->path,
2790 g_app_launch_context_launch_failed (launch_context, sn_id);
2793 g_list_free (launched_uris);
2798 if (pid_callback != NULL)
2799 pid_callback (info, pid, pid_callback_data);
2801 if (launch_context != NULL)
2803 GVariantBuilder builder;
2804 GVariant *platform_data;
2806 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2807 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
2809 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
2810 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
2811 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
2812 g_variant_unref (platform_data);
2815 notify_desktop_launch (session_bus,
2823 g_list_free (launched_uris);
2825 g_strfreev (wrapped_argv);
2826 wrapped_argv = NULL;
2828 while (dup_uris != NULL);
2833 g_list_free (dup_uris);
2841 object_path_from_appid (const gchar *appid)
2843 gchar *appid_path, *iter;
2845 appid_path = g_strconcat ("/", appid, NULL);
2846 for (iter = appid_path; *iter; iter++)
2859 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
2861 GAppLaunchContext *launch_context)
2863 GVariantBuilder builder;
2865 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
2869 GList *launched_files = create_files_for_uris (uris);
2871 if (info->startup_notify)
2875 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
2877 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
2880 g_list_free_full (launched_files, g_object_unref);
2883 return g_variant_builder_end (&builder);
2887 launch_uris_with_dbus (GDesktopAppInfo *info,
2888 GDBusConnection *session_bus,
2890 GAppLaunchContext *launch_context,
2891 GCancellable *cancellable,
2892 GAsyncReadyCallback callback,
2895 GVariantBuilder builder;
2898 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2904 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
2905 for (iter = uris; iter; iter = iter->next)
2906 g_variant_builder_add (&builder, "s", iter->data);
2907 g_variant_builder_close (&builder);
2910 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
2912 object_path = object_path_from_appid (info->app_id);
2913 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
2914 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
2915 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
2916 cancellable, callback, user_data);
2917 g_free (object_path);
2921 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
2922 GDBusConnection *session_bus,
2924 GAppLaunchContext *launch_context,
2925 GCancellable *cancellable,
2926 GAsyncReadyCallback callback,
2929 GList *ruris = uris;
2930 char *app_id = NULL;
2932 g_return_val_if_fail (info != NULL, FALSE);
2935 app_id = g_desktop_app_info_get_string (info, "X-Flatpak");
2936 if (app_id && *app_id)
2938 ruris = g_document_portal_add_documents (uris, app_id, NULL);
2944 launch_uris_with_dbus (info, session_bus, ruris, launch_context,
2945 cancellable, callback, user_data);
2948 g_list_free_full (ruris, g_free);
2956 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
2958 GAppLaunchContext *launch_context,
2959 GSpawnFlags spawn_flags,
2960 GSpawnChildSetupFunc user_setup,
2961 gpointer user_setup_data,
2962 GDesktopAppLaunchCallback pid_callback,
2963 gpointer pid_callback_data,
2969 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2970 GDBusConnection *session_bus;
2971 gboolean success = TRUE;
2973 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
2975 if (session_bus && info->app_id)
2976 /* This is non-blocking API. Similar to launching via fork()/exec()
2977 * we don't wait around to see if the program crashed during startup.
2978 * This is what startup-notification's job is...
2980 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context,
2983 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
2984 spawn_flags, user_setup, user_setup_data,
2985 pid_callback, pid_callback_data,
2986 stdin_fd, stdout_fd, stderr_fd, error);
2988 if (session_bus != NULL)
2990 /* This asynchronous flush holds a reference until it completes,
2991 * which ensures that the following unref won't immediately kill
2992 * the connection if we were the initial owner.
2994 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
2995 g_object_unref (session_bus);
3002 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
3004 GAppLaunchContext *launch_context,
3007 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
3009 _SPAWN_FLAGS_DEFAULT,
3010 NULL, NULL, NULL, NULL,
3019 GAppLaunchContext *context;
3023 launch_uris_data_free (LaunchUrisData *data)
3025 g_clear_object (&data->context);
3026 g_list_free_full (data->uris, g_free);
3031 launch_uris_with_dbus_cb (GObject *object,
3032 GAsyncResult *result,
3035 GTask *task = G_TASK (user_data);
3036 GError *error = NULL;
3038 g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
3041 g_dbus_error_strip_remote_error (error);
3042 g_task_return_error (task, g_steal_pointer (&error));
3045 g_task_return_boolean (task, TRUE);
3047 g_object_unref (task);
3051 launch_uris_flush_cb (GObject *object,
3052 GAsyncResult *result,
3055 GTask *task = G_TASK (user_data);
3057 g_dbus_connection_flush_finish (G_DBUS_CONNECTION (object), result, NULL);
3058 g_task_return_boolean (task, TRUE);
3059 g_object_unref (task);
3063 launch_uris_bus_get_cb (GObject *object,
3064 GAsyncResult *result,
3067 GTask *task = G_TASK (user_data);
3068 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (g_task_get_source_object (task));
3069 LaunchUrisData *data = g_task_get_task_data (task);
3070 GCancellable *cancellable = g_task_get_cancellable (task);
3071 GDBusConnection *session_bus;
3072 GError *error = NULL;
3074 session_bus = g_bus_get_finish (result, NULL);
3076 if (session_bus && info->app_id)
3078 /* FIXME: The g_document_portal_add_documents() function, which is called
3079 * from the g_desktop_app_info_launch_uris_with_dbus() function, still
3080 * uses blocking calls.
3082 g_desktop_app_info_launch_uris_with_dbus (info, session_bus,
3083 data->uris, data->context,
3085 launch_uris_with_dbus_cb,
3086 g_steal_pointer (&task));
3090 /* FIXME: The D-Bus message from the notify_desktop_launch() function
3091 * can be still lost even if flush is called later. See:
3092 * https://gitlab.freedesktop.org/dbus/dbus/issues/72
3094 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec,
3095 data->uris, data->context,
3096 _SPAWN_FLAGS_DEFAULT, NULL,
3097 NULL, NULL, NULL, -1, -1, -1,
3101 g_task_return_error (task, g_steal_pointer (&error));
3102 g_object_unref (task);
3105 g_dbus_connection_flush (session_bus,
3107 launch_uris_flush_cb,
3108 g_steal_pointer (&task));
3111 g_clear_object (&session_bus);
3115 g_desktop_app_info_launch_uris_async (GAppInfo *appinfo,
3117 GAppLaunchContext *context,
3118 GCancellable *cancellable,
3119 GAsyncReadyCallback callback,
3123 LaunchUrisData *data;
3125 task = g_task_new (appinfo, cancellable, callback, user_data);
3126 g_task_set_source_tag (task, g_desktop_app_info_launch_uris_async);
3128 data = g_new0 (LaunchUrisData, 1);
3129 data->uris = g_list_copy_deep (uris, (GCopyFunc) g_strdup, NULL);
3130 data->context = (context != NULL) ? g_object_ref (context) : NULL;
3131 g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) launch_uris_data_free);
3133 g_bus_get (G_BUS_TYPE_SESSION, cancellable, launch_uris_bus_get_cb, task);
3137 g_desktop_app_info_launch_uris_finish (GAppInfo *appinfo,
3138 GAsyncResult *result,
3141 g_return_val_if_fail (g_task_is_valid (result, appinfo), FALSE);
3143 return g_task_propagate_boolean (G_TASK (result), error);
3147 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
3149 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3151 return info->exec &&
3152 ((strstr (info->exec, "%u") != NULL) ||
3153 (strstr (info->exec, "%U") != NULL));
3157 g_desktop_app_info_supports_files (GAppInfo *appinfo)
3159 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3161 return info->exec &&
3162 ((strstr (info->exec, "%f") != NULL) ||
3163 (strstr (info->exec, "%F") != NULL));
3167 g_desktop_app_info_launch (GAppInfo *appinfo,
3169 GAppLaunchContext *launch_context,
3179 uri = g_file_get_uri (files->data);
3180 uris = g_list_prepend (uris, uri);
3181 files = files->next;
3184 uris = g_list_reverse (uris);
3186 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
3188 g_list_free_full (uris, g_free);
3194 * g_desktop_app_info_launch_uris_as_manager_with_fds:
3195 * @appinfo: a #GDesktopAppInfo
3196 * @uris: (element-type utf8): List of URIs
3197 * @launch_context: (nullable): a #GAppLaunchContext
3198 * @spawn_flags: #GSpawnFlags, used for each process
3199 * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
3201 * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
3202 * @pid_callback: (scope call) (nullable): Callback for child processes
3203 * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
3204 * @stdin_fd: file descriptor to use for child's stdin, or -1
3205 * @stdout_fd: file descriptor to use for child's stdout, or -1
3206 * @stderr_fd: file descriptor to use for child's stderr, or -1
3207 * @error: return location for a #GError, or %NULL
3209 * Equivalent to g_desktop_app_info_launch_uris_as_manager() but allows
3210 * you to pass in file descriptors for the stdin, stdout and stderr streams
3211 * of the launched process.
3213 * If application launching occurs via some non-spawn mechanism (e.g. D-Bus
3214 * activation) then @stdin_fd, @stdout_fd and @stderr_fd are ignored.
3216 * Returns: %TRUE on successful launch, %FALSE otherwise.
3221 g_desktop_app_info_launch_uris_as_manager_with_fds (GDesktopAppInfo *appinfo,
3223 GAppLaunchContext *launch_context,
3224 GSpawnFlags spawn_flags,
3225 GSpawnChildSetupFunc user_setup,
3226 gpointer user_setup_data,
3227 GDesktopAppLaunchCallback pid_callback,
3228 gpointer pid_callback_data,
3234 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
3249 * g_desktop_app_info_launch_uris_as_manager:
3250 * @appinfo: a #GDesktopAppInfo
3251 * @uris: (element-type utf8): List of URIs
3252 * @launch_context: (nullable): a #GAppLaunchContext
3253 * @spawn_flags: #GSpawnFlags, used for each process
3254 * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
3256 * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
3257 * @pid_callback: (scope call) (nullable): Callback for child processes
3258 * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
3259 * @error: return location for a #GError, or %NULL
3261 * This function performs the equivalent of g_app_info_launch_uris(),
3262 * but is intended primarily for operating system components that
3263 * launch applications. Ordinary applications should use
3264 * g_app_info_launch_uris().
3266 * If the application is launched via GSpawn, then @spawn_flags, @user_setup
3267 * and @user_setup_data are used for the call to g_spawn_async().
3268 * Additionally, @pid_callback (with @pid_callback_data) will be called to
3269 * inform about the PID of the created process. See g_spawn_async_with_pipes()
3270 * for information on certain parameter conditions that can enable an
3271 * optimized posix_spawn() codepath to be used.
3273 * If application launching occurs via some other mechanism (eg: D-Bus
3274 * activation) then @spawn_flags, @user_setup, @user_setup_data,
3275 * @pid_callback and @pid_callback_data are ignored.
3277 * Returns: %TRUE on successful launch, %FALSE otherwise.
3280 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
3282 GAppLaunchContext *launch_context,
3283 GSpawnFlags spawn_flags,
3284 GSpawnChildSetupFunc user_setup,
3285 gpointer user_setup_data,
3286 GDesktopAppLaunchCallback pid_callback,
3287 gpointer pid_callback_data,
3290 return g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
3302 /* OnlyShowIn API support {{{2 */
3305 * g_desktop_app_info_set_desktop_env:
3306 * @desktop_env: a string specifying what desktop this is
3308 * Sets the name of the desktop that the application is running in.
3309 * This is used by g_app_info_should_show() and
3310 * g_desktop_app_info_get_show_in() to evaluate the
3311 * `OnlyShowIn` and `NotShowIn`
3312 * desktop entry fields.
3314 * Should be called only once; subsequent calls are ignored.
3316 * Deprecated:2.42:do not use this API. Since 2.42 the value of the
3317 * `XDG_CURRENT_DESKTOP` environment variable will be used.
3320 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
3322 get_current_desktops (desktop_env);
3326 g_desktop_app_info_should_show (GAppInfo *appinfo)
3328 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3330 if (info->nodisplay)
3333 return g_desktop_app_info_get_show_in (info, NULL);
3336 /* mime types/default apps support {{{2 */
3345 ensure_dir (DirType type,
3348 char *path, *display_name;
3354 path = g_build_filename (g_get_user_config_dir (), NULL);
3358 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
3362 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
3366 g_assert_not_reached ();
3369 g_debug ("%s: Ensuring %s", G_STRFUNC, path);
3372 if (g_mkdir_with_parents (path, 0700) == 0)
3376 display_name = g_filename_display_name (path);
3377 if (type == APP_DIR)
3378 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3379 _("Can’t create user application configuration folder %s: %s"),
3380 display_name, g_strerror (errsv));
3382 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3383 _("Can’t create user MIME configuration folder %s: %s"),
3384 display_name, g_strerror (errsv));
3386 g_free (display_name);
3393 update_mimeapps_list (const char *desktop_id,
3394 const char *content_type,
3395 UpdateMimeFlags flags,
3398 char *dirname, *filename, *string;
3400 gboolean load_succeeded, res;
3401 char **old_list, **list;
3402 gsize length, data_size;
3405 char **content_types;
3407 /* Don't add both at start and end */
3408 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
3409 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
3411 dirname = ensure_dir (CONF_DIR, error);
3415 filename = g_build_filename (dirname, "mimeapps.list", NULL);
3418 key_file = g_key_file_new ();
3419 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
3420 if (!load_succeeded ||
3421 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
3422 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
3423 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
3425 g_key_file_free (key_file);
3426 key_file = g_key_file_new ();
3431 content_types = g_new (char *, 2);
3432 content_types[0] = g_strdup (content_type);
3433 content_types[1] = NULL;
3437 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
3440 for (k = 0; content_types && content_types[k]; k++)
3442 /* set as default, if requested so */
3443 string = g_key_file_get_string (key_file,
3444 DEFAULT_APPLICATIONS_GROUP,
3448 if (g_strcmp0 (string, desktop_id) != 0 &&
3449 (flags & UPDATE_MIME_SET_DEFAULT))
3452 string = g_strdup (desktop_id);
3454 /* add in the non-default list too, if it's not already there */
3455 flags |= UPDATE_MIME_SET_NON_DEFAULT;
3458 if (string == NULL || desktop_id == NULL)
3459 g_key_file_remove_key (key_file,
3460 DEFAULT_APPLICATIONS_GROUP,
3464 g_key_file_set_string (key_file,
3465 DEFAULT_APPLICATIONS_GROUP,
3474 /* reuse the list from above */
3478 g_strfreev (content_types);
3479 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
3482 for (k = 0; content_types && content_types[k]; k++)
3484 /* Add to the right place in the list */
3487 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
3488 content_types[k], &length, NULL);
3490 list = g_new (char *, 1 + length + 1);
3494 /* if we're adding a last-used hint, just put the application in front of the list */
3495 if (flags & UPDATE_MIME_SET_LAST_USED)
3497 /* avoid adding this again as non-default later */
3498 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3499 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3501 list[i++] = g_strdup (desktop_id);
3506 for (j = 0; old_list[j] != NULL; j++)
3508 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3510 /* rewrite other entries if they're different from the new one */
3511 list[i++] = g_strdup (old_list[j]);
3513 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3515 /* we encountered an old entry which is equal to the one we're adding as non-default,
3516 * don't change its position in the list.
3518 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3519 list[i++] = g_strdup (old_list[j]);
3524 /* add it at the end of the list */
3525 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3526 list[i++] = g_strdup (desktop_id);
3530 g_strfreev (old_list);
3532 if (list[0] == NULL || desktop_id == NULL)
3533 g_key_file_remove_key (key_file,
3534 ADDED_ASSOCIATIONS_GROUP,
3538 g_key_file_set_string_list (key_file,
3539 ADDED_ASSOCIATIONS_GROUP,
3541 (const char * const *)list, i);
3548 /* reuse the list from above */
3552 g_strfreev (content_types);
3553 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
3556 for (k = 0; content_types && content_types[k]; k++)
3558 /* Remove from removed associations group (unless remove) */
3561 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
3562 content_types[k], &length, NULL);
3564 list = g_new (char *, 1 + length + 1);
3567 if (flags & UPDATE_MIME_REMOVE)
3568 list[i++] = g_strdup (desktop_id);
3571 for (j = 0; old_list[j] != NULL; j++)
3573 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3574 list[i++] = g_strdup (old_list[j]);
3579 g_strfreev (old_list);
3581 if (list[0] == NULL || desktop_id == NULL)
3582 g_key_file_remove_key (key_file,
3583 REMOVED_ASSOCIATIONS_GROUP,
3587 g_key_file_set_string_list (key_file,
3588 REMOVED_ASSOCIATIONS_GROUP,
3590 (const char * const *)list, i);
3595 g_strfreev (content_types);
3597 data = g_key_file_to_data (key_file, &data_size, error);
3598 g_key_file_free (key_file);
3600 res = g_file_set_contents (filename, data, data_size, error);
3602 desktop_file_dirs_invalidate_user_config ();
3611 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
3612 const char *content_type,
3615 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3617 if (!g_desktop_app_info_ensure_saved (info, error))
3620 if (!info->desktop_id)
3622 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3623 _("Application information lacks an identifier"));
3627 /* both add support for the content type and set as last used */
3628 return update_mimeapps_list (info->desktop_id, content_type,
3629 UPDATE_MIME_SET_NON_DEFAULT |
3630 UPDATE_MIME_SET_LAST_USED,
3635 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
3636 const char *content_type,
3639 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3641 if (!g_desktop_app_info_ensure_saved (info, error))
3644 if (!info->desktop_id)
3646 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3647 _("Application information lacks an identifier"));
3651 return update_mimeapps_list (info->desktop_id, content_type,
3652 UPDATE_MIME_SET_DEFAULT,
3657 update_program_done (GPid pid,
3661 /* Did the application exit correctly */
3662 if (g_spawn_check_exit_status (status, NULL))
3664 /* Here we could clean out any caches in use */
3669 run_update_command (char *command,
3678 GError *error = NULL;
3681 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
3683 if (g_spawn_async ("/", argv,
3685 G_SPAWN_SEARCH_PATH |
3686 G_SPAWN_STDOUT_TO_DEV_NULL |
3687 G_SPAWN_STDERR_TO_DEV_NULL |
3688 G_SPAWN_DO_NOT_REAP_CHILD,
3689 NULL, NULL, /* No setup function */
3692 g_child_watch_add (pid, update_program_done, NULL);
3695 /* If we get an error at this point, it's quite likely the user doesn't
3696 * have an installed copy of either 'update-mime-database' or
3697 * 'update-desktop-database'. I don't think we want to popup an error
3698 * dialog at this point, so we just do a g_warning to give the user a
3699 * chance of debugging it.
3701 g_warning ("%s", error->message);
3702 g_error_free (error);
3709 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
3710 const char *extension,
3713 char *filename, *basename, *mimetype;
3717 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
3720 dirname = ensure_dir (MIMETYPE_DIR, error);
3724 basename = g_strdup_printf ("user-extension-%s.xml", extension);
3725 filename = g_build_filename (dirname, basename, NULL);
3729 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
3731 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
3736 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3737 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
3738 " <mime-type type=\"%s\">\n"
3739 " <comment>%s document</comment>\n"
3740 " <glob pattern=\"*.%s\"/>\n"
3742 "</mime-info>\n", mimetype, extension, extension);
3744 g_file_set_contents (filename, contents, -1, NULL);
3747 run_update_command ("update-mime-database", "mime");
3751 res = g_desktop_app_info_set_as_default_for_type (appinfo,
3761 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
3762 const char *content_type,
3765 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3767 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3770 return update_mimeapps_list (info->desktop_id, content_type,
3771 UPDATE_MIME_SET_NON_DEFAULT,
3776 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
3782 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
3783 const char *content_type,
3786 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3788 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3791 return update_mimeapps_list (info->desktop_id, content_type,
3796 static const char **
3797 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
3799 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3801 return (const char**) info->mime_types;
3804 /* Saving and deleting {{{2 */
3807 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
3813 char *data, *desktop_id;
3818 if (info->filename != NULL)
3821 /* This is only used for object created with
3822 * g_app_info_create_from_commandline. All other
3823 * object should have a filename
3826 dirname = ensure_dir (APP_DIR, error);
3830 key_file = g_key_file_new ();
3832 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3833 "Encoding", "UTF-8");
3834 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3835 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
3836 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3837 G_KEY_FILE_DESKTOP_KEY_TYPE,
3838 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
3840 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3841 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
3842 if (info->nodisplay)
3843 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3844 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3846 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3847 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
3849 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3850 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
3852 if (info->generic_name != NULL)
3853 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3854 GENERIC_NAME_KEY, info->generic_name);
3856 if (info->fullname != NULL)
3857 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3858 FULL_NAME_KEY, info->fullname);
3860 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3861 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
3863 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3864 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3866 data = g_key_file_to_data (key_file, &data_size, NULL);
3867 g_key_file_free (key_file);
3869 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
3870 filename = g_build_filename (dirname, desktop_id, NULL);
3871 g_free (desktop_id);
3874 fd = g_mkstemp (filename);
3879 display_name = g_filename_display_name (filename);
3880 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3881 _("Can’t create user desktop file %s"), display_name);
3882 g_free (display_name);
3888 desktop_id = g_path_get_basename (filename);
3890 /* FIXME - actually handle error */
3891 (void) g_close (fd, NULL);
3893 res = g_file_set_contents (filename, data, data_size, error);
3897 g_free (desktop_id);
3902 info->filename = filename;
3903 info->desktop_id = desktop_id;
3905 run_update_command ("update-desktop-database", "applications");
3907 /* We just dropped a file in the user's desktop file directory. Save
3908 * the monitor the bother of having to notice it and invalidate
3911 * This means that calls directly following this will be able to see
3912 * the results immediately.
3914 desktop_file_dirs_invalidate_user_data ();
3920 g_desktop_app_info_can_delete (GAppInfo *appinfo)
3922 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3926 if (strstr (info->filename, "/userapp-"))
3927 return g_access (info->filename, W_OK) == 0;
3934 g_desktop_app_info_delete (GAppInfo *appinfo)
3936 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3940 if (g_remove (info->filename) == 0)
3942 update_mimeapps_list (info->desktop_id, NULL,
3946 g_free (info->filename);
3947 info->filename = NULL;
3948 g_free (info->desktop_id);
3949 info->desktop_id = NULL;
3958 /* Create for commandline {{{2 */
3960 * g_app_info_create_from_commandline:
3961 * @commandline: (type filename): the commandline to use
3962 * @application_name: (nullable): the application name, or %NULL to use @commandline
3963 * @flags: flags that can specify details of the created #GAppInfo
3964 * @error: a #GError location to store the error occurring, %NULL to ignore.
3966 * Creates a new #GAppInfo from the given information.
3968 * Note that for @commandline, the quoting rules of the Exec key of the
3969 * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
3970 * are applied. For example, if the @commandline contains
3971 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
3972 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
3974 * Returns: (transfer full): new #GAppInfo for given command.
3977 g_app_info_create_from_commandline (const char *commandline,
3978 const char *application_name,
3979 GAppInfoCreateFlags flags,
3984 GDesktopAppInfo *info;
3986 g_return_val_if_fail (commandline, NULL);
3988 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
3990 info->filename = NULL;
3991 info->desktop_id = NULL;
3993 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
3994 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
3995 info->hidden = FALSE;
3996 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
3997 info->exec = g_strconcat (commandline, " %u", NULL);
3999 info->exec = g_strconcat (commandline, " %f", NULL);
4000 info->nodisplay = TRUE;
4001 info->binary = binary_from_exec (info->exec);
4003 if (application_name)
4004 info->name = g_strdup (application_name);
4007 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
4008 split = g_strsplit (commandline, " ", 2);
4009 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
4011 info->name = basename;
4012 if (info->name == NULL)
4013 info->name = g_strdup ("custom");
4015 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
4017 return G_APP_INFO (info);
4020 /* GAppInfo interface init */
4023 g_desktop_app_info_iface_init (GAppInfoIface *iface)
4025 iface->dup = g_desktop_app_info_dup;
4026 iface->equal = g_desktop_app_info_equal;
4027 iface->get_id = g_desktop_app_info_get_id;
4028 iface->get_name = g_desktop_app_info_get_name;
4029 iface->get_description = g_desktop_app_info_get_description;
4030 iface->get_executable = g_desktop_app_info_get_executable;
4031 iface->get_icon = g_desktop_app_info_get_icon;
4032 iface->launch = g_desktop_app_info_launch;
4033 iface->supports_uris = g_desktop_app_info_supports_uris;
4034 iface->supports_files = g_desktop_app_info_supports_files;
4035 iface->launch_uris = g_desktop_app_info_launch_uris;
4036 iface->launch_uris_async = g_desktop_app_info_launch_uris_async;
4037 iface->launch_uris_finish = g_desktop_app_info_launch_uris_finish;
4038 iface->should_show = g_desktop_app_info_should_show;
4039 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
4040 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
4041 iface->add_supports_type = g_desktop_app_info_add_supports_type;
4042 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
4043 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
4044 iface->can_delete = g_desktop_app_info_can_delete;
4045 iface->do_delete = g_desktop_app_info_delete;
4046 iface->get_commandline = g_desktop_app_info_get_commandline;
4047 iface->get_display_name = g_desktop_app_info_get_display_name;
4048 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
4049 iface->get_supported_types = g_desktop_app_info_get_supported_types;
4052 /* Recommended applications {{{2 */
4054 /* Converts content_type into a list of itself with all of its parent
4055 * types (if include_fallback is enabled) or just returns a single-item
4056 * list with the unaliased content type.
4059 get_list_of_mimetypes (const gchar *content_type,
4060 gboolean include_fallback)
4065 array = g_ptr_array_new ();
4066 unaliased = _g_unix_content_type_unalias (content_type);
4067 g_ptr_array_add (array, unaliased);
4069 if (include_fallback)
4073 /* Iterate the array as we grow it, until we have nothing more to add */
4074 for (i = 0; i < array->len; i++)
4076 gchar **parents = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
4079 for (j = 0; parents[j]; j++)
4080 /* Don't add duplicates */
4081 if (!array_contains (array, parents[j]))
4082 g_ptr_array_add (array, parents[j]);
4084 g_free (parents[j]);
4086 /* We already stole or freed each element. Free the container. */
4091 g_ptr_array_add (array, NULL);
4093 return (gchar **) g_ptr_array_free (array, FALSE);
4097 g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
4098 gboolean include_fallback)
4100 GPtrArray *hits, *blacklist;
4104 hits = g_ptr_array_new ();
4105 blacklist = g_ptr_array_new ();
4107 types = get_list_of_mimetypes (content_type, include_fallback);
4109 desktop_file_dirs_lock ();
4111 for (i = 0; types[i]; i++)
4112 for (j = 0; j < n_desktop_file_dirs; j++)
4113 desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], hits, blacklist);
4115 /* We will keep the hits past unlocking, so we must dup them */
4116 for (i = 0; i < hits->len; i++)
4117 hits->pdata[i] = g_strdup (hits->pdata[i]);
4119 desktop_file_dirs_unlock ();
4121 g_ptr_array_add (hits, NULL);
4123 g_ptr_array_free (blacklist, TRUE);
4126 return (gchar **) g_ptr_array_free (hits, FALSE);
4130 * g_app_info_get_recommended_for_type:
4131 * @content_type: the content type to find a #GAppInfo for
4133 * Gets a list of recommended #GAppInfos for a given content type, i.e.
4134 * those applications which claim to support the given content type exactly,
4135 * and not by MIME type subclassing.
4136 * Note that the first application of the list is the last used one, i.e.
4137 * the last one for which g_app_info_set_as_last_used_for_type() has been
4140 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
4141 * for given @content_type or %NULL on error.
4146 g_app_info_get_recommended_for_type (const gchar *content_type)
4148 gchar **desktop_ids;
4152 g_return_val_if_fail (content_type != NULL, NULL);
4154 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
4157 for (i = 0; desktop_ids[i]; i++)
4159 GDesktopAppInfo *info;
4161 info = g_desktop_app_info_new (desktop_ids[i]);
4163 infos = g_list_prepend (infos, info);
4166 g_strfreev (desktop_ids);
4168 return g_list_reverse (infos);
4172 * g_app_info_get_fallback_for_type:
4173 * @content_type: the content type to find a #GAppInfo for
4175 * Gets a list of fallback #GAppInfos for a given content type, i.e.
4176 * those applications which claim to support the given content type
4177 * by MIME type subclassing and not directly.
4179 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
4180 * for given @content_type or %NULL on error.
4185 g_app_info_get_fallback_for_type (const gchar *content_type)
4187 gchar **recommended_ids;
4192 g_return_val_if_fail (content_type != NULL, NULL);
4194 recommended_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
4195 all_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
4198 for (i = 0; all_ids[i]; i++)
4200 GDesktopAppInfo *info;
4203 /* Don't return the ones on the recommended list */
4204 for (j = 0; recommended_ids[j]; j++)
4205 if (g_str_equal (all_ids[i], recommended_ids[j]))
4208 if (recommended_ids[j])
4211 info = g_desktop_app_info_new (all_ids[i]);
4214 infos = g_list_prepend (infos, info);
4217 g_strfreev (recommended_ids);
4218 g_strfreev (all_ids);
4220 return g_list_reverse (infos);
4224 * g_app_info_get_all_for_type:
4225 * @content_type: the content type to find a #GAppInfo for
4227 * Gets a list of all #GAppInfos for a given content type,
4228 * including the recommended and fallback #GAppInfos. See
4229 * g_app_info_get_recommended_for_type() and
4230 * g_app_info_get_fallback_for_type().
4232 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
4233 * for given @content_type or %NULL on error.
4236 g_app_info_get_all_for_type (const char *content_type)
4238 gchar **desktop_ids;
4242 g_return_val_if_fail (content_type != NULL, NULL);
4244 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
4247 for (i = 0; desktop_ids[i]; i++)
4249 GDesktopAppInfo *info;
4251 info = g_desktop_app_info_new (desktop_ids[i]);
4253 infos = g_list_prepend (infos, info);
4256 g_strfreev (desktop_ids);
4258 return g_list_reverse (infos);
4262 * g_app_info_reset_type_associations:
4263 * @content_type: a content type
4265 * Removes all changes to the type associations done by
4266 * g_app_info_set_as_default_for_type(),
4267 * g_app_info_set_as_default_for_extension(),
4268 * g_app_info_add_supports_type() or
4269 * g_app_info_remove_supports_type().
4274 g_app_info_reset_type_associations (const char *content_type)
4276 update_mimeapps_list (NULL, content_type,
4282 * g_app_info_get_default_for_type:
4283 * @content_type: the content type to find a #GAppInfo for
4284 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
4287 * Gets the default #GAppInfo for a given content type.
4289 * Returns: (transfer full): #GAppInfo for given @content_type or
4293 g_app_info_get_default_for_type (const char *content_type,
4294 gboolean must_support_uris)
4296 GPtrArray *blacklist;
4302 g_return_val_if_fail (content_type != NULL, NULL);
4304 types = get_list_of_mimetypes (content_type, TRUE);
4306 blacklist = g_ptr_array_new ();
4307 results = g_ptr_array_new ();
4310 desktop_file_dirs_lock ();
4312 for (i = 0; types[i]; i++)
4314 /* Collect all the default apps for this type */
4315 for (j = 0; j < n_desktop_file_dirs; j++)
4316 desktop_file_dir_default_lookup (&desktop_file_dirs[j], types[i], results);
4318 /* Consider the associations as well... */
4319 for (j = 0; j < n_desktop_file_dirs; j++)
4320 desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], results, blacklist);
4322 /* (If any), see if one of those apps is installed... */
4323 for (j = 0; j < results->len; j++)
4325 const gchar *desktop_id = g_ptr_array_index (results, j);
4327 for (k = 0; k < n_desktop_file_dirs; k++)
4329 info = (GAppInfo *) desktop_file_dir_get_app (&desktop_file_dirs[k], desktop_id);
4333 if (!must_support_uris || g_app_info_supports_uris (info))
4336 g_clear_object (&info);
4341 /* Reset the list, ready to try again with the next (parent)
4342 * mimetype, but keep the blacklist in place.
4344 g_ptr_array_set_size (results, 0);
4348 desktop_file_dirs_unlock ();
4350 g_ptr_array_unref (blacklist);
4351 g_ptr_array_unref (results);
4358 * g_app_info_get_default_for_uri_scheme:
4359 * @uri_scheme: a string containing a URI scheme.
4361 * Gets the default application for handling URIs with
4362 * the given URI scheme. A URI scheme is the initial part
4363 * of the URI, up to but not including the ':', e.g. "http",
4366 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4369 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
4372 char *content_type, *scheme_down;
4374 scheme_down = g_ascii_strdown (uri_scheme, -1);
4375 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
4376 g_free (scheme_down);
4377 app_info = g_app_info_get_default_for_type (content_type, FALSE);
4378 g_free (content_type);
4383 /* "Get all" API {{{2 */
4386 * g_desktop_app_info_get_implementations:
4387 * @interface: the name of the interface
4389 * Gets all applications that implement @interface.
4391 * An application implements an interface if that interface is listed in
4392 * the Implements= line of the desktop file of the application.
4394 * Returns: (element-type GDesktopAppInfo) (transfer full): a list of #GDesktopAppInfo
4400 g_desktop_app_info_get_implementations (const gchar *interface)
4402 GList *result = NULL;
4406 desktop_file_dirs_lock ();
4408 for (i = 0; i < n_desktop_file_dirs; i++)
4409 desktop_file_dir_get_implementations (&desktop_file_dirs[i], &result, interface);
4411 desktop_file_dirs_unlock ();
4416 gchar *name = (*ptr)->data;
4417 GDesktopAppInfo *app;
4419 app = g_desktop_app_info_new (name);
4425 ptr = &(*ptr)->next;
4428 *ptr = g_list_delete_link (*ptr, *ptr);
4435 * g_desktop_app_info_search:
4436 * @search_string: the search string to use
4438 * Searches desktop files for ones that match @search_string.
4440 * The return value is an array of strvs. Each strv contains a list of
4441 * applications that matched @search_string with an equal score. The
4442 * outer list is sorted by score so that the first strv contains the
4443 * best-matching applications, and so on.
4444 * The algorithm for determining matches is undefined and may change at
4447 * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
4448 * list of strvs. Free each item with g_strfreev() and free the outer
4449 * list with g_free().
4452 g_desktop_app_info_search (const gchar *search_string)
4454 gchar **search_tokens;
4455 gint last_category = -1;
4457 gint n_categories = 0;
4458 gint start_of_category;
4461 search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
4463 desktop_file_dirs_lock ();
4465 reset_total_search_results ();
4467 for (i = 0; i < n_desktop_file_dirs; i++)
4469 for (j = 0; search_tokens[j]; j++)
4471 desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
4472 merge_token_results (j == 0);
4474 merge_directory_results ();
4477 sort_total_search_results ();
4479 /* Count the total number of unique categories */
4480 for (i = 0; i < static_total_results_size; i++)
4481 if (static_total_results[i].category != last_category)
4483 last_category = static_total_results[i].category;
4487 results = g_new (gchar **, n_categories + 1);
4489 /* Start loading into the results list */
4490 start_of_category = 0;
4491 for (i = 0; i < n_categories; i++)
4493 gint n_items_in_category = 0;
4497 this_category = static_total_results[start_of_category].category;
4499 while (start_of_category + n_items_in_category < static_total_results_size &&
4500 static_total_results[start_of_category + n_items_in_category].category == this_category)
4501 n_items_in_category++;
4503 results[i] = g_new (gchar *, n_items_in_category + 1);
4504 for (j = 0; j < n_items_in_category; j++)
4505 results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
4506 results[i][j] = NULL;
4508 start_of_category += n_items_in_category;
4512 desktop_file_dirs_unlock ();
4514 g_strfreev (search_tokens);
4520 * g_app_info_get_all:
4522 * Gets a list of all of the applications currently registered
4525 * For desktop files, this includes applications that have
4526 * `NoDisplay=true` set or are excluded from display by means
4527 * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
4528 * The returned list does not include applications which have
4529 * the `Hidden` key set.
4531 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
4534 g_app_info_get_all (void)
4537 GHashTableIter iter;
4542 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
4544 desktop_file_dirs_lock ();
4546 for (i = 0; i < n_desktop_file_dirs; i++)
4547 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
4549 desktop_file_dirs_unlock ();
4552 g_hash_table_iter_init (&iter, apps);
4553 while (g_hash_table_iter_next (&iter, NULL, &value))
4556 infos = g_list_prepend (infos, value);
4559 g_hash_table_destroy (apps);
4564 /* GDesktopAppInfoLookup interface {{{2 */
4567 * GDesktopAppInfoLookup:
4569 * #GDesktopAppInfoLookup is an opaque data structure and can only be accessed
4570 * using the following functions.
4572 * Deprecated: 2.28: The #GDesktopAppInfoLookup interface is deprecated and
4576 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4578 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
4579 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
4582 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
4586 /* "Get for mime type" APIs {{{2 */
4589 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
4590 * @lookup: a #GDesktopAppInfoLookup
4591 * @uri_scheme: a string containing a URI scheme.
4593 * Gets the default application for launching applications
4594 * using this URI scheme for a particular #GDesktopAppInfoLookup
4597 * The #GDesktopAppInfoLookup interface and this function is used
4598 * to implement g_app_info_get_default_for_uri_scheme() backends
4599 * in a GIO module. There is no reason for applications to use it
4600 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
4602 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4604 * Deprecated: 2.28: The #GDesktopAppInfoLookup interface is deprecated and
4608 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
4609 const char *uri_scheme)
4611 GDesktopAppInfoLookupIface *iface;
4613 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
4615 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
4617 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
4620 G_GNUC_END_IGNORE_DEPRECATIONS
4622 /* Misc getter APIs {{{2 */
4625 * g_desktop_app_info_get_startup_wm_class:
4626 * @info: a #GDesktopAppInfo that supports startup notify
4628 * Retrieves the StartupWMClass field from @info. This represents the
4629 * WM_CLASS property of the main window of the application, if launched
4632 * Returns: (transfer none): the startup WM class, or %NULL if none is set
4633 * in the desktop file.
4638 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
4640 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4642 return info->startup_wm_class;
4646 * g_desktop_app_info_get_string:
4647 * @info: a #GDesktopAppInfo
4648 * @key: the key to look up
4650 * Looks up a string value in the keyfile backing @info.
4652 * The @key is looked up in the "Desktop Entry" group.
4654 * Returns: a newly allocated string, or %NULL if the key
4660 g_desktop_app_info_get_string (GDesktopAppInfo *info,
4663 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4665 return g_key_file_get_string (info->keyfile,
4666 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4670 * g_desktop_app_info_get_locale_string:
4671 * @info: a #GDesktopAppInfo
4672 * @key: the key to look up
4674 * Looks up a localized string value in the keyfile backing @info
4675 * translated to the current locale.
4677 * The @key is looked up in the "Desktop Entry" group.
4679 * Returns: (nullable): a newly allocated string, or %NULL if the key
4685 g_desktop_app_info_get_locale_string (GDesktopAppInfo *info,
4688 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4689 g_return_val_if_fail (key != NULL && *key != '\0', NULL);
4691 return g_key_file_get_locale_string (info->keyfile,
4692 G_KEY_FILE_DESKTOP_GROUP,
4697 * g_desktop_app_info_get_boolean:
4698 * @info: a #GDesktopAppInfo
4699 * @key: the key to look up
4701 * Looks up a boolean value in the keyfile backing @info.
4703 * The @key is looked up in the "Desktop Entry" group.
4705 * Returns: the boolean value, or %FALSE if the key
4711 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
4714 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4716 return g_key_file_get_boolean (info->keyfile,
4717 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4721 * g_desktop_app_info_get_string_list:
4722 * @info: a #GDesktopAppInfo
4723 * @key: the key to look up
4724 * @length: (out) (optional): return location for the number of returned strings, or %NULL
4726 * Looks up a string list value in the keyfile backing @info.
4728 * The @key is looked up in the "Desktop Entry" group.
4730 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
4731 * a %NULL-terminated string array or %NULL if the specified
4732 * key cannot be found. The array should be freed with g_strfreev().
4737 g_desktop_app_info_get_string_list (GDesktopAppInfo *info,
4741 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4743 return g_key_file_get_string_list (info->keyfile,
4744 G_KEY_FILE_DESKTOP_GROUP, key, length, NULL);
4748 * g_desktop_app_info_has_key:
4749 * @info: a #GDesktopAppInfo
4750 * @key: the key to look up
4752 * Returns whether @key exists in the "Desktop Entry" group
4753 * of the keyfile backing @info.
4755 * Returns: %TRUE if the @key exists
4760 g_desktop_app_info_has_key (GDesktopAppInfo *info,
4763 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4765 return g_key_file_has_key (info->keyfile,
4766 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4769 /* Desktop actions support {{{2 */
4772 * g_desktop_app_info_list_actions:
4773 * @info: a #GDesktopAppInfo
4775 * Returns the list of "additional application actions" supported on the
4776 * desktop file, as per the desktop file specification.
4778 * As per the specification, this is the list of actions that are
4779 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
4781 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
4785 const gchar * const *
4786 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
4788 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4790 return (const gchar **) info->actions;
4794 app_info_has_action (GDesktopAppInfo *info,
4795 const gchar *action_name)
4799 for (i = 0; info->actions[i]; i++)
4800 if (g_str_equal (info->actions[i], action_name))
4807 * g_desktop_app_info_get_action_name:
4808 * @info: a #GDesktopAppInfo
4809 * @action_name: the name of the action as from
4810 * g_desktop_app_info_list_actions()
4812 * Gets the user-visible display name of the "additional application
4813 * action" specified by @action_name.
4815 * This corresponds to the "Name" key within the keyfile group for the
4818 * Returns: (transfer full): the locale-specific action name
4823 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4824 const gchar *action_name)
4829 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4830 g_return_val_if_fail (action_name != NULL, NULL);
4831 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4833 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4834 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4835 g_free (group_name);
4837 /* The spec says that the Name field must be given.
4839 * If it's not, let's follow the behaviour of our get_name()
4840 * implementation above and never return %NULL.
4843 result = g_strdup (_("Unnamed"));
4849 * g_desktop_app_info_launch_action:
4850 * @info: a #GDesktopAppInfo
4851 * @action_name: the name of the action as from
4852 * g_desktop_app_info_list_actions()
4853 * @launch_context: (nullable): a #GAppLaunchContext
4855 * Activates the named application action.
4857 * You may only call this function on action names that were
4858 * returned from g_desktop_app_info_list_actions().
4860 * Note that if the main entry of the desktop file indicates that the
4861 * application supports startup notification, and @launch_context is
4862 * non-%NULL, then startup notification will be used when activating the
4863 * action (and as such, invocation of the action on the receiving side
4864 * must signal the end of startup notification when it is completed).
4865 * This is the expected behaviour of applications declaring additional
4866 * actions, as per the desktop file specification.
4868 * As with g_app_info_launch() there is no way to detect failures that
4869 * occur while using this function.
4874 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4875 const gchar *action_name,
4876 GAppLaunchContext *launch_context)
4878 GDBusConnection *session_bus;
4880 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4881 g_return_if_fail (action_name != NULL);
4882 g_return_if_fail (app_info_has_action (info, action_name));
4884 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4886 if (session_bus && info->app_id)
4890 object_path = object_path_from_appid (info->app_id);
4891 g_dbus_connection_call (session_bus, info->app_id, object_path,
4892 "org.freedesktop.Application", "ActivateAction",
4893 g_variant_new ("(sav@a{sv})", action_name, NULL,
4894 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4895 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4896 g_free (object_path);
4903 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4904 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4905 g_free (group_name);
4908 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4909 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL,
4915 if (session_bus != NULL)
4917 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4918 g_object_unref (session_bus);
4923 /* vim:set foldmethod=marker: */