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>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
38 #include "glib-unix.h"
42 #include "gthemedicon.h"
43 #include "gfileicon.h"
44 #include <glib/gstdio.h>
46 #include "giomodule-priv.h"
48 #include "gappinfoprivate.h"
49 #include "glocalfilemonitor.h"
52 #include "gdocumentportal.h"
56 * SECTION:gdesktopappinfo
57 * @title: GDesktopAppInfo
58 * @short_description: Application information from desktop files
59 * @include: gio/gdesktopappinfo.h
61 * #GDesktopAppInfo is an implementation of #GAppInfo based on
64 * Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific
65 * GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
69 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
70 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
71 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
72 #define MIME_CACHE_GROUP "MIME Cache"
73 #define GENERIC_NAME_KEY "GenericName"
74 #define FULL_NAME_KEY "X-GNOME-FullName"
75 #define KEYWORDS_KEY "Keywords"
76 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
83 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
84 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
90 * Information about an installed application from a desktop file.
92 struct _GDesktopAppInfo
94 GObject parent_instance;
116 char *startup_wm_class;
123 guint startup_notify : 1;
128 UPDATE_MIME_NONE = 1 << 0,
129 UPDATE_MIME_SET_DEFAULT = 1 << 1,
130 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
131 UPDATE_MIME_REMOVE = 1 << 3,
132 UPDATE_MIME_SET_LAST_USED = 1 << 4,
135 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
136 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
138 /* DesktopFileDir implementation {{{1 */
143 gchar *alternatively_watching;
146 GFileMonitor *monitor;
147 GHashTable *app_names;
148 GHashTable *mime_tweaks;
149 GHashTable *memory_index;
150 GHashTable *memory_implementations;
153 static DesktopFileDir *desktop_file_dirs;
154 static guint n_desktop_file_dirs;
155 static const gchar *desktop_file_dirs_config_dir = NULL;
156 static const guint desktop_file_dir_user_config_index = 0;
157 static guint desktop_file_dir_user_data_index;
158 static GMutex desktop_file_dir_lock;
159 static const gchar *gio_launch_desktop_path = NULL;
161 /* Monitor 'changed' signal handler {{{2 */
162 static void desktop_file_dir_reset (DesktopFileDir *dir);
165 * desktop_file_dir_get_alternative_dir:
166 * @dir: a #DesktopFileDir
168 * Gets the "alternative" directory to monitor in case the path
171 * If the path exists this will return NULL, otherwise it will return a
172 * parent directory of the path.
174 * This is used to avoid inotify on a non-existent directory (which
175 * results in polling).
177 * See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info.
180 desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
184 /* If the directory itself exists then we need no alternative. */
185 if (g_access (dir->path, R_OK | X_OK) == 0)
188 /* Otherwise, try the parent directories until we find one. */
189 parent = g_path_get_dirname (dir->path);
191 while (g_access (parent, R_OK | X_OK) != 0)
195 parent = g_path_get_dirname (tmp);
197 /* If somehow we get to '/' or '.' then just stop... */
198 if (g_str_equal (parent, tmp))
211 desktop_file_dir_changed (GFileMonitor *monitor,
214 GFileMonitorEvent event_type,
217 DesktopFileDir *dir = user_data;
218 gboolean do_nothing = FALSE;
220 /* We are not interested in receiving notifications forever just
221 * because someone asked about one desktop file once.
223 * After we receive the first notification, reset the dir, destroying
224 * the monitor. We will take this as a hint, next time that we are
225 * asked, that we need to check if everything is up to date.
227 * If this is a notification for a parent directory (because the
228 * desktop directory didn't exist) then we shouldn't fire the signal
229 * unless something actually changed.
231 g_mutex_lock (&desktop_file_dir_lock);
233 if (dir->alternatively_watching)
235 gchar *alternative_dir;
237 alternative_dir = desktop_file_dir_get_alternative_dir (dir);
238 do_nothing = alternative_dir && g_str_equal (dir->alternatively_watching, alternative_dir);
239 g_free (alternative_dir);
243 desktop_file_dir_reset (dir);
245 g_mutex_unlock (&desktop_file_dir_lock);
247 /* Notify anyone else who may be interested */
249 g_app_info_monitor_fire ();
252 /* Internal utility functions {{{2 */
255 * desktop_file_dir_app_name_is_masked:
256 * @dir: a #DesktopFileDir
257 * @app_name: an application ID
259 * Checks if @app_name is masked for @dir.
261 * An application is masked if a similarly-named desktop file exists in
262 * a desktop file directory with higher precedence. Masked desktop
263 * files should be ignored.
266 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
267 const gchar *app_name)
269 while (dir > desktop_file_dirs)
273 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
280 static const gchar * const *
281 get_lowercase_current_desktops (void)
283 static gchar **result;
285 if (g_once_init_enter (&result))
290 envvar = g_getenv ("XDG_CURRENT_DESKTOP");
296 tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
298 for (i = 0; tmp[i]; i++)
299 for (j = 0; tmp[i][j]; j++)
300 tmp[i][j] = g_ascii_tolower (tmp[i][j]);
303 tmp = g_new0 (gchar *, 0 + 1);
305 g_once_init_leave (&result, tmp);
308 return (const gchar **) result;
311 static const gchar * const *
312 get_current_desktops (const gchar *value)
314 static gchar **result;
316 if (g_once_init_enter (&result))
321 value = g_getenv ("XDG_CURRENT_DESKTOP");
326 tmp = g_strsplit (value, ":", 0);
328 g_once_init_leave (&result, tmp);
331 return (const gchar **) result;
335 * add_to_table_if_appropriate:
336 * @apps: a string to GDesktopAppInfo hash table
337 * @app_name: the name of the application
338 * @info: a #GDesktopAppInfo, or NULL
340 * If @info is non-%NULL and non-hidden, then add it to @apps, using
341 * @app_name as a key.
343 * If @info is non-%NULL then this function will consume the passed-in
347 add_to_table_if_appropriate (GHashTable *apps,
348 const gchar *app_name,
349 GDesktopAppInfo *info)
356 g_object_unref (info);
360 g_free (info->desktop_id);
361 info->desktop_id = g_strdup (app_name);
363 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
370 DESKTOP_KEY_GenericName,
371 DESKTOP_KEY_Keywords,
373 DESKTOP_KEY_X_GNOME_FullName,
378 const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
379 /* Note: lower numbers are a better match.
381 * In case we want two keys to match at the same level, we can just
382 * use the same number for the two different keys.
384 [DESKTOP_KEY_Name] = 1,
385 [DESKTOP_KEY_Exec] = 2,
386 [DESKTOP_KEY_Keywords] = 3,
387 [DESKTOP_KEY_GenericName] = 4,
388 [DESKTOP_KEY_X_GNOME_FullName] = 5,
389 [DESKTOP_KEY_Comment] = 6
392 /* Common prefix commands to ignore from Exec= lines */
393 const char * const exec_key_match_blacklist[] = {
409 desktop_key_get_name (guint key_id)
413 case DESKTOP_KEY_Comment:
415 case DESKTOP_KEY_Exec:
417 case DESKTOP_KEY_GenericName:
418 return GENERIC_NAME_KEY;
419 case DESKTOP_KEY_Keywords:
421 case DESKTOP_KEY_Name:
423 case DESKTOP_KEY_X_GNOME_FullName:
424 return FULL_NAME_KEY;
426 g_assert_not_reached ();
430 /* Search global state {{{2
432 * We only ever search under a global lock, so we can use (and reuse)
433 * some global data to reduce allocations made while searching.
435 * In short, we keep around arrays of results that we expand as needed
436 * (and never shrink).
438 * static_token_results: this is where we append the results for each
439 * token within a given desktop directory, as we handle it (which is
440 * a union of all matches for this term)
442 * static_search_results: this is where we build the complete results
443 * for a single directory (which is an intersection of the matches
444 * found for each term)
446 * static_total_results: this is where we build the complete results
447 * across all directories (which is a union of the matches found in
450 * The app_names that enter these tables are always pointer-unique (in
451 * the sense that string equality is the same as pointer equality).
452 * This can be guaranteed for two reasons:
454 * - we mask appids so that a given appid will only ever appear within
455 * the highest-precedence directory that contains it. We never
456 * return search results from a lower-level directory if a desktop
457 * file exists in a higher-level one.
459 * - within a given directory, the string is unique because it's the
460 * key in the hashtable of all app_ids for that directory.
462 * We perform a merging of the results in merge_token_results(). This
463 * works by ordering the two lists and moving through each of them (at
464 * the same time) looking for common elements, rejecting uncommon ones.
465 * "Order" here need not mean any particular thing, as long as it is
466 * some order. Because of the uniqueness of our strings, we can use
467 * pointer order. That's what's going on in compare_results() below.
471 const gchar *app_name;
475 static struct search_result *static_token_results;
476 static gint static_token_results_size;
477 static gint static_token_results_allocated;
478 static struct search_result *static_search_results;
479 static gint static_search_results_size;
480 static gint static_search_results_allocated;
481 static struct search_result *static_total_results;
482 static gint static_total_results_size;
483 static gint static_total_results_allocated;
485 /* And some functions for performing nice operations against it */
487 compare_results (gconstpointer a,
490 const struct search_result *ra = a;
491 const struct search_result *rb = b;
493 if (ra->app_name < rb->app_name)
496 else if (ra->app_name > rb->app_name)
500 return ra->category - rb->category;
504 compare_categories (gconstpointer a,
507 const struct search_result *ra = a;
508 const struct search_result *rb = b;
510 return ra->category - rb->category;
514 add_token_result (const gchar *app_name,
517 if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
519 static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
520 static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
523 static_token_results[static_token_results_size].app_name = app_name;
524 static_token_results[static_token_results_size].category = category;
525 static_token_results_size++;
529 merge_token_results (gboolean first)
531 if (static_token_results_size != 0)
532 qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
534 /* If this is the first token then we are basically merging a list with
535 * itself -- we only perform de-duplication.
537 * If this is not the first token then we are doing a real merge.
541 const gchar *last_name = NULL;
544 /* We must de-duplicate, but we do so by taking the best category
547 * The final list can be as large as the input here, so make sure
548 * we have enough room (even if it's too much room).
551 if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
553 static_search_results_allocated = static_token_results_allocated;
554 static_search_results = g_renew (struct search_result,
555 static_search_results,
556 static_search_results_allocated);
559 for (i = 0; i < static_token_results_size; i++)
561 /* The list is sorted so that the best match for a given id
562 * will be at the front, so once we have copied an id, skip
563 * the rest of the entries for the same id.
565 if (static_token_results[i].app_name == last_name)
568 last_name = static_token_results[i].app_name;
570 static_search_results[static_search_results_size++] = static_token_results[i];
575 const gchar *last_name = NULL;
579 /* We only ever remove items from the results list, so no need to
580 * resize to ensure that we have enough room.
582 for (i = 0; i < static_token_results_size; i++)
584 if (static_token_results[i].app_name == last_name)
587 last_name = static_token_results[i].app_name;
589 /* Now we only want to have a result in static_search_results
590 * if we already have it there *and* we have it in
591 * static_token_results as well. The category will be the
594 * Skip past the results in static_search_results that are not
595 * going to be matches.
597 while (k < static_search_results_size &&
598 static_search_results[k].app_name < static_token_results[i].app_name)
601 if (k < static_search_results_size &&
602 static_search_results[k].app_name == static_token_results[i].app_name)
606 * Category should be the worse of the two (ie:
607 * numerically larger).
609 static_search_results[j].app_name = static_search_results[k].app_name;
610 static_search_results[j].category = MAX (static_search_results[k].category,
611 static_token_results[i].category);
616 static_search_results_size = j;
619 /* Clear it out for next time... */
620 static_token_results_size = 0;
624 reset_total_search_results (void)
626 static_total_results_size = 0;
630 sort_total_search_results (void)
632 if (static_total_results_size != 0)
633 qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
637 merge_directory_results (void)
639 if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
641 static_total_results_allocated = MAX (16, static_total_results_allocated);
642 while (static_total_results_allocated < static_total_results_size + static_search_results_size)
643 static_total_results_allocated *= 2;
644 static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
647 if (static_total_results + static_total_results_size != 0)
648 memcpy (static_total_results + static_total_results_size,
649 static_search_results,
650 static_search_results_size * sizeof (struct search_result));
652 static_total_results_size += static_search_results_size;
654 /* Clear it out for next time... */
655 static_search_results_size = 0;
658 /* Support for unindexed DesktopFileDirs {{{2 */
660 get_apps_from_dir (GHashTable **apps,
664 const char *basename;
667 dir = g_dir_open (dirname, 0, NULL);
672 while ((basename = g_dir_read_name (dir)) != NULL)
676 filename = g_build_filename (dirname, basename, NULL);
678 if (g_str_has_suffix (basename, ".desktop"))
682 app_name = g_strconcat (prefix, basename, NULL);
685 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
687 g_hash_table_insert (*apps, app_name, g_strdup (filename));
689 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
693 subprefix = g_strconcat (prefix, basename, "-", NULL);
694 get_apps_from_dir (apps, filename, subprefix);
709 } UnindexedMimeTweaks;
712 free_mime_tweaks (gpointer data)
714 UnindexedMimeTweaks *tweaks = data;
716 g_strfreev (tweaks->additions);
717 g_strfreev (tweaks->removals);
718 g_strfreev (tweaks->defaults);
720 g_slice_free (UnindexedMimeTweaks, tweaks);
723 static UnindexedMimeTweaks *
724 desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
725 const gchar *mime_type)
727 UnindexedMimeTweaks *tweaks;
728 gchar *unaliased_type;
730 unaliased_type = _g_unix_content_type_unalias (mime_type);
731 tweaks = g_hash_table_lookup (dir->mime_tweaks, unaliased_type);
735 tweaks = g_slice_new0 (UnindexedMimeTweaks);
736 g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
739 g_free (unaliased_type);
744 /* consumes 'to_add' */
746 expand_strv (gchar ***strv_ptr,
748 gchar * const *blacklist)
750 guint strv_len, add_len;
761 strv_len = g_strv_length (strv);
762 add_len = g_strv_length (to_add);
763 strv = g_renew (gchar *, strv, strv_len + add_len + 1);
765 for (i = 0; to_add[i]; i++)
767 /* Don't add blacklisted strings */
769 for (j = 0; blacklist[j]; j++)
770 if (g_str_equal (to_add[i], blacklist[j]))
773 /* Don't add duplicates already in the list */
774 for (j = 0; j < strv_len; j++)
775 if (g_str_equal (to_add[i], strv[j]))
778 strv[strv_len++] = to_add[i];
785 strv[strv_len] = NULL;
792 desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
793 const gchar *filename,
794 const gchar *added_group,
795 gboolean tweaks_permitted)
797 UnindexedMimeTweaks *tweaks;
798 char **desktop_file_ids;
803 key_file = g_key_file_new ();
804 if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
806 g_key_file_free (key_file);
810 mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL);
812 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
814 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
815 "mimeapps.list file may add or remove associations.", filename, added_group);
816 g_strfreev (mime_types);
820 if (mime_types != NULL)
822 for (i = 0; mime_types[i] != NULL; i++)
824 desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL);
826 if (desktop_file_ids)
828 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
829 expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
833 g_strfreev (mime_types);
836 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
838 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
840 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
841 "mimeapps.list file may add or remove associations.", filename, REMOVED_ASSOCIATIONS_GROUP);
842 g_strfreev (mime_types);
846 if (mime_types != NULL)
848 for (i = 0; mime_types[i] != NULL; i++)
850 desktop_file_ids = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP, mime_types[i], NULL, NULL);
852 if (desktop_file_ids)
854 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
855 expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
859 g_strfreev (mime_types);
862 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
864 if (mime_types != NULL)
866 for (i = 0; mime_types[i] != NULL; i++)
868 desktop_file_ids = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP, mime_types[i], NULL, NULL);
870 if (desktop_file_ids)
872 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
873 expand_strv (&tweaks->defaults, desktop_file_ids, NULL);
877 g_strfreev (mime_types);
880 g_key_file_free (key_file);
884 desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
886 const gchar * const *desktops;
890 dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
892 /* We process in order of precedence, using a blacklisting approach to
893 * avoid recording later instructions that conflict with ones we found
896 * We first start with the XDG_CURRENT_DESKTOP files, in precedence
899 desktops = get_lowercase_current_desktops ();
900 for (i = 0; desktops[i]; i++)
902 filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]);
903 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
907 /* Next, the non-desktop-specific mimeapps.list */
908 filename = g_strdup_printf ("%s/mimeapps.list", dir->path);
909 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, TRUE);
912 /* The remaining files are only checked for in directories that might
913 * contain desktop files (ie: not the config dirs).
918 /* We have 'defaults.list' which was only ever understood by GLib. It
919 * exists widely, but it has never been part of any spec and it should
920 * be treated as deprecated. This will be removed in a future
923 filename = g_strdup_printf ("%s/defaults.list", dir->path);
924 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, ADDED_ASSOCIATIONS_GROUP, FALSE);
927 /* Finally, the mimeinfo.cache, which is just a cached copy of what we
928 * would find in the MimeTypes= lines of all of the desktop files.
930 filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path);
931 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, MIME_CACHE_GROUP, TRUE);
936 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
939 get_apps_from_dir (&dir->app_names, dir->path, "");
941 desktop_file_dir_unindexed_read_mimeapps_lists (dir);
944 static GDesktopAppInfo *
945 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
946 const gchar *desktop_id)
948 const gchar *filename;
950 filename = g_hash_table_lookup (dir->app_names, desktop_id);
955 return g_desktop_app_info_new_from_filename (filename);
959 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
966 if (dir->app_names == NULL)
969 g_hash_table_iter_init (&iter, dir->app_names);
970 while (g_hash_table_iter_next (&iter, &app_name, &filename))
972 if (desktop_file_dir_app_name_is_masked (dir, app_name))
975 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
979 typedef struct _MemoryIndexEntry MemoryIndexEntry;
980 typedef GHashTable MemoryIndex;
982 struct _MemoryIndexEntry
984 const gchar *app_name; /* pointer to the hashtable key */
986 MemoryIndexEntry *next;
990 memory_index_entry_free (gpointer data)
992 MemoryIndexEntry *mie = data;
996 MemoryIndexEntry *next = mie->next;
998 g_slice_free (MemoryIndexEntry, mie);
1004 memory_index_add_token (MemoryIndex *mi,
1006 gint match_category,
1007 const gchar *app_name)
1009 MemoryIndexEntry *mie, *first;
1011 mie = g_slice_new (MemoryIndexEntry);
1012 mie->app_name = app_name;
1013 mie->match_category = match_category;
1015 first = g_hash_table_lookup (mi, token);
1019 mie->next = first->next;
1025 g_hash_table_insert (mi, g_strdup (token), mie);
1030 memory_index_add_string (MemoryIndex *mi,
1031 const gchar *string,
1032 gint match_category,
1033 const gchar *app_name)
1035 gchar **tokens, **alternates;
1038 tokens = g_str_tokenize_and_fold (string, NULL, &alternates);
1040 for (i = 0; tokens[i]; i++)
1041 memory_index_add_token (mi, tokens[i], match_category, app_name);
1043 for (i = 0; alternates[i]; i++)
1044 memory_index_add_token (mi, alternates[i], match_category, app_name);
1046 g_strfreev (alternates);
1047 g_strfreev (tokens);
1050 static MemoryIndex *
1051 memory_index_new (void)
1053 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free);
1057 desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
1059 GHashTableIter iter;
1062 dir->memory_index = memory_index_new ();
1063 dir->memory_implementations = memory_index_new ();
1065 /* Nothing to search? */
1066 if (dir->app_names == NULL)
1069 g_hash_table_iter_init (&iter, dir->app_names);
1070 while (g_hash_table_iter_next (&iter, &app, &path))
1074 if (desktop_file_dir_app_name_is_masked (dir, app))
1077 key_file = g_key_file_new ();
1079 if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) &&
1080 !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
1082 /* Index the interesting keys... */
1086 for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
1091 if (!desktop_key_match_category[i])
1094 raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
1097 if (i == DESKTOP_KEY_Exec && raw != NULL)
1099 /* Special handling: only match basename of first field */
1103 /* Remove extra arguments, if any */
1104 space = raw + strcspn (raw, " \t\n"); /* IFS */
1107 /* Skip the pathname, if any */
1108 if ((slash = strrchr (raw, '/')))
1111 /* Don't match on blacklisted binaries like interpreters */
1112 if (g_strv_contains (exec_key_match_blacklist, value))
1117 memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
1122 /* Make note of the Implements= line */
1123 implements = g_key_file_get_string_list (key_file, "Desktop Entry", "Implements", NULL, NULL);
1124 for (i = 0; implements && implements[i]; i++)
1125 memory_index_add_token (dir->memory_implementations, implements[i], 0, app);
1126 g_strfreev (implements);
1129 g_key_file_free (key_file);
1134 desktop_file_dir_unindexed_search (DesktopFileDir *dir,
1135 const gchar *search_token)
1137 GHashTableIter iter;
1138 gpointer key, value;
1140 if (!dir->memory_index)
1141 desktop_file_dir_unindexed_setup_search (dir);
1143 g_hash_table_iter_init (&iter, dir->memory_index);
1144 while (g_hash_table_iter_next (&iter, &key, &value))
1146 MemoryIndexEntry *mie = value;
1148 if (!g_str_has_prefix (key, search_token))
1153 add_token_result (mie->app_name, mie->match_category);
1160 array_contains (GPtrArray *array,
1165 for (i = 0; i < array->len; i++)
1166 if (g_str_equal (array->pdata[i], str))
1173 desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
1174 const gchar *mime_type,
1176 GPtrArray *blacklist)
1178 UnindexedMimeTweaks *tweaks;
1181 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1186 if (tweaks->additions)
1188 for (i = 0; tweaks->additions[i]; i++)
1190 gchar *app_name = tweaks->additions[i];
1192 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1193 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1194 g_ptr_array_add (hits, app_name);
1198 if (tweaks->removals)
1200 for (i = 0; tweaks->removals[i]; i++)
1202 gchar *app_name = tweaks->removals[i];
1204 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1205 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1206 g_ptr_array_add (blacklist, app_name);
1212 desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir,
1213 const gchar *mime_type,
1216 UnindexedMimeTweaks *tweaks;
1219 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1221 if (!tweaks || !tweaks->defaults)
1224 for (i = 0; tweaks->defaults[i]; i++)
1226 gchar *app_name = tweaks->defaults[i];
1228 if (!array_contains (results, app_name))
1229 g_ptr_array_add (results, app_name);
1234 desktop_file_dir_unindexed_get_implementations (DesktopFileDir *dir,
1236 const gchar *interface)
1238 MemoryIndexEntry *mie;
1240 if (!dir->memory_index)
1241 desktop_file_dir_unindexed_setup_search (dir);
1243 for (mie = g_hash_table_lookup (dir->memory_implementations, interface); mie; mie = mie->next)
1244 *results = g_list_prepend (*results, g_strdup (mie->app_name));
1247 /* DesktopFileDir "API" {{{2 */
1250 * desktop_file_dir_create:
1251 * @array: the #GArray to add a new item to
1252 * @data_dir: an XDG_DATA_DIR
1254 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
1258 desktop_file_dir_create (GArray *array,
1259 const gchar *data_dir)
1261 DesktopFileDir dir = { 0, };
1263 dir.path = g_build_filename (data_dir, "applications", NULL);
1265 g_array_append_val (array, dir);
1269 * desktop_file_dir_create:
1270 * @array: the #GArray to add a new item to
1271 * @config_dir: an XDG_CONFIG_DIR
1273 * Just the same as desktop_file_dir_create() except that it does not
1274 * add the "applications" directory. It also marks the directory as
1275 * config-only, which prevents us from attempting to find desktop files
1279 desktop_file_dir_create_for_config (GArray *array,
1280 const gchar *config_dir)
1282 DesktopFileDir dir = { 0, };
1284 dir.path = g_strdup (config_dir);
1285 dir.is_config = TRUE;
1287 g_array_append_val (array, dir);
1291 * desktop_file_dir_reset:
1292 * @dir: a #DesktopFileDir
1294 * Cleans up @dir, releasing most resources that it was using.
1297 desktop_file_dir_reset (DesktopFileDir *dir)
1299 if (dir->alternatively_watching)
1301 g_free (dir->alternatively_watching);
1302 dir->alternatively_watching = NULL;
1307 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
1308 g_object_unref (dir->monitor);
1309 dir->monitor = NULL;
1314 g_hash_table_unref (dir->app_names);
1315 dir->app_names = NULL;
1318 if (dir->memory_index)
1320 g_hash_table_unref (dir->memory_index);
1321 dir->memory_index = NULL;
1324 if (dir->mime_tweaks)
1326 g_hash_table_unref (dir->mime_tweaks);
1327 dir->mime_tweaks = NULL;
1330 if (dir->memory_implementations)
1332 g_hash_table_unref (dir->memory_implementations);
1333 dir->memory_implementations = NULL;
1336 dir->is_setup = FALSE;
1340 * desktop_file_dir_init:
1341 * @dir: a #DesktopFileDir
1343 * Does initial setup for @dir
1345 * You should only call this if @dir is not already setup.
1348 desktop_file_dir_init (DesktopFileDir *dir)
1350 const gchar *watch_dir;
1352 g_assert (!dir->is_setup);
1354 g_assert (!dir->alternatively_watching);
1355 g_assert (!dir->monitor);
1357 dir->alternatively_watching = desktop_file_dir_get_alternative_dir (dir);
1358 watch_dir = dir->alternatively_watching ? dir->alternatively_watching : dir->path;
1360 /* There is a very thin race here if the watch_dir has been _removed_
1361 * between when we checked for it and when we establish the watch.
1362 * Removes probably don't happen in usual operation, and even if it
1363 * does (and we catch the unlikely race), the only degradation is that
1364 * we will fall back to polling.
1366 dir->monitor = g_local_file_monitor_new_in_worker (watch_dir, TRUE, G_FILE_MONITOR_NONE,
1367 desktop_file_dir_changed, dir, NULL);
1369 desktop_file_dir_unindexed_init (dir);
1371 dir->is_setup = TRUE;
1375 * desktop_file_dir_get_app:
1376 * @dir: a DesktopFileDir
1377 * @desktop_id: the desktop ID to load
1379 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
1380 * within @dir, even if it is hidden.
1382 * This function does not check if @desktop_id would be masked by a
1383 * directory with higher precedence. The caller must do so.
1385 static GDesktopAppInfo *
1386 desktop_file_dir_get_app (DesktopFileDir *dir,
1387 const gchar *desktop_id)
1389 if (!dir->app_names)
1392 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
1396 * desktop_file_dir_get_all:
1397 * @dir: a DesktopFileDir
1398 * @apps: a #GHashTable<string, GDesktopAppInfo>
1400 * Loads all desktop files in @dir and adds them to @apps, careful to
1401 * ensure we don't add any files masked by a similarly-named file in a
1402 * higher-precedence directory.
1405 desktop_file_dir_get_all (DesktopFileDir *dir,
1408 desktop_file_dir_unindexed_get_all (dir, apps);
1412 * desktop_file_dir_mime_lookup:
1413 * @dir: a #DesktopFileDir
1414 * @mime_type: the mime type to look up
1415 * @hits: the array to store the hits
1416 * @blacklist: the array to store the blacklist
1418 * Does a lookup of a mimetype against one desktop file directory,
1419 * recording any hits and blacklisting and "Removed" associations (so
1420 * later directories don't record them as hits).
1422 * The items added to @hits are duplicated, but the ones in @blacklist
1423 * are weak pointers. This facilitates simply freeing the blacklist
1424 * (which is only used for internal bookkeeping) but using the pdata of
1425 * @hits as the result of the operation.
1428 desktop_file_dir_mime_lookup (DesktopFileDir *dir,
1429 const gchar *mime_type,
1431 GPtrArray *blacklist)
1433 desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blacklist);
1437 * desktop_file_dir_default_lookup:
1438 * @dir: a #DesktopFileDir
1439 * @mime_type: the mime type to look up
1440 * @results: an array to store the results in
1442 * Collects the "default" applications for a given mime type from @dir.
1445 desktop_file_dir_default_lookup (DesktopFileDir *dir,
1446 const gchar *mime_type,
1449 desktop_file_dir_unindexed_default_lookup (dir, mime_type, results);
1453 * desktop_file_dir_search:
1454 * @dir: a #DesktopFileDir
1455 * @term: a normalised and casefolded search term
1457 * Finds the names of applications in @dir that match @term.
1460 desktop_file_dir_search (DesktopFileDir *dir,
1461 const gchar *search_token)
1463 desktop_file_dir_unindexed_search (dir, search_token);
1467 desktop_file_dir_get_implementations (DesktopFileDir *dir,
1469 const gchar *interface)
1471 desktop_file_dir_unindexed_get_implementations (dir, results, interface);
1474 /* Lock/unlock and global setup API {{{2 */
1477 desktop_file_dirs_lock (void)
1480 const gchar *user_config_dir = g_get_user_config_dir ();
1482 g_mutex_lock (&desktop_file_dir_lock);
1484 /* If the XDG dirs configuration has changed (expected only during tests),
1485 * clear and reload the state. */
1486 if (g_strcmp0 (desktop_file_dirs_config_dir, user_config_dir) != 0)
1488 g_debug ("%s: Resetting desktop app info dirs from %s to %s",
1489 G_STRFUNC, desktop_file_dirs_config_dir, user_config_dir);
1491 for (i = 0; i < n_desktop_file_dirs; i++)
1492 desktop_file_dir_reset (&desktop_file_dirs[i]);
1493 g_clear_pointer (&desktop_file_dirs, g_free);
1494 n_desktop_file_dirs = 0;
1495 desktop_file_dir_user_data_index = 0;
1498 if (desktop_file_dirs == NULL)
1500 const char * const *dirs;
1504 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
1506 /* First, the configs. Highest priority: the user's ~/.config */
1507 desktop_file_dir_create_for_config (tmp, user_config_dir);
1509 /* Next, the system configs (/etc/xdg, and so on). */
1510 dirs = g_get_system_config_dirs ();
1511 for (i = 0; dirs[i]; i++)
1512 desktop_file_dir_create_for_config (tmp, dirs[i]);
1514 /* Now the data. Highest priority: the user's ~/.local/share/applications */
1515 desktop_file_dir_user_data_index = tmp->len;
1516 desktop_file_dir_create (tmp, g_get_user_data_dir ());
1518 /* Following that, XDG_DATA_DIRS/applications, in order */
1519 dirs = g_get_system_data_dirs ();
1520 for (i = 0; dirs[i]; i++)
1521 desktop_file_dir_create (tmp, dirs[i]);
1523 /* The list of directories will never change after this, unless
1524 * g_get_user_config_dir() changes due to %G_TEST_OPTION_ISOLATE_DIRS. */
1525 desktop_file_dirs = (DesktopFileDir *) tmp->data;
1526 n_desktop_file_dirs = tmp->len;
1527 desktop_file_dirs_config_dir = user_config_dir;
1529 g_array_free (tmp, FALSE);
1532 for (i = 0; i < n_desktop_file_dirs; i++)
1533 if (!desktop_file_dirs[i].is_setup)
1534 desktop_file_dir_init (&desktop_file_dirs[i]);
1538 desktop_file_dirs_unlock (void)
1540 g_mutex_unlock (&desktop_file_dir_lock);
1544 desktop_file_dirs_invalidate_user_config (void)
1546 g_mutex_lock (&desktop_file_dir_lock);
1548 if (n_desktop_file_dirs)
1549 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_config_index]);
1551 g_mutex_unlock (&desktop_file_dir_lock);
1555 desktop_file_dirs_invalidate_user_data (void)
1557 g_mutex_lock (&desktop_file_dir_lock);
1559 if (n_desktop_file_dirs)
1560 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_data_index]);
1562 g_mutex_unlock (&desktop_file_dir_lock);
1565 /* GDesktopAppInfo implementation {{{1 */
1566 /* GObject implementation {{{2 */
1568 g_desktop_app_info_finalize (GObject *object)
1570 GDesktopAppInfo *info;
1572 info = G_DESKTOP_APP_INFO (object);
1574 g_free (info->desktop_id);
1575 g_free (info->filename);
1578 g_key_file_unref (info->keyfile);
1580 g_free (info->name);
1581 g_free (info->generic_name);
1582 g_free (info->fullname);
1583 g_free (info->comment);
1584 g_free (info->icon_name);
1586 g_object_unref (info->icon);
1587 g_strfreev (info->keywords);
1588 g_strfreev (info->only_show_in);
1589 g_strfreev (info->not_show_in);
1590 g_free (info->try_exec);
1591 g_free (info->exec);
1592 g_free (info->binary);
1593 g_free (info->path);
1594 g_free (info->categories);
1595 g_free (info->startup_wm_class);
1596 g_strfreev (info->mime_types);
1597 g_free (info->app_id);
1598 g_strfreev (info->actions);
1600 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
1604 g_desktop_app_info_set_property (GObject *object,
1606 const GValue *value,
1609 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1614 self->filename = g_value_dup_string (value);
1618 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1624 g_desktop_app_info_get_property (GObject *object,
1629 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1634 g_value_set_string (value, self->filename);
1637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1643 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
1645 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1647 gobject_class->get_property = g_desktop_app_info_get_property;
1648 gobject_class->set_property = g_desktop_app_info_set_property;
1649 gobject_class->finalize = g_desktop_app_info_finalize;
1652 * GDesktopAppInfo:filename:
1654 * The origin filename of this #GDesktopAppInfo
1656 g_object_class_install_property (gobject_class,
1658 g_param_spec_string ("filename", "Filename", "", NULL,
1659 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1663 g_desktop_app_info_init (GDesktopAppInfo *local)
1667 /* Construction... {{{2 */
1671 * @exec: an exec line
1673 * Returns the first word in an exec line (ie: the binary name).
1675 * If @exec is " progname --foo %F" then returns "progname".
1678 binary_from_exec (const char *exec)
1680 const char *p, *start;
1686 while (*p != ' ' && *p != 0)
1689 return g_strndup (start, p - start);
1693 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
1700 gboolean bus_activatable;
1702 start_group = g_key_file_get_start_group (key_file);
1703 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
1705 g_free (start_group);
1708 g_free (start_group);
1710 type = g_key_file_get_string (key_file,
1711 G_KEY_FILE_DESKTOP_GROUP,
1712 G_KEY_FILE_DESKTOP_KEY_TYPE,
1714 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
1721 try_exec = g_key_file_get_string (key_file,
1722 G_KEY_FILE_DESKTOP_GROUP,
1723 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
1725 if (try_exec && try_exec[0] != '\0')
1728 t = g_find_program_in_path (try_exec);
1737 exec = g_key_file_get_string (key_file,
1738 G_KEY_FILE_DESKTOP_GROUP,
1739 G_KEY_FILE_DESKTOP_KEY_EXEC,
1741 if (exec && exec[0] != '\0')
1745 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
1754 t = g_find_program_in_path (argv[0]);
1767 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
1768 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
1769 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
1770 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
1771 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
1772 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
1773 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
1774 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);
1775 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);
1776 info->try_exec = try_exec;
1778 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
1779 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
1780 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
1781 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
1782 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
1783 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
1784 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
1785 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);
1786 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
1787 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
1789 /* Remove the special-case: no Actions= key just means 0 extra actions */
1790 if (info->actions == NULL)
1791 info->actions = g_new0 (gchar *, 0 + 1);
1794 if (info->icon_name)
1796 if (g_path_is_absolute (info->icon_name))
1800 file = g_file_new_for_path (info->icon_name);
1801 info->icon = g_file_icon_new (file);
1802 g_object_unref (file);
1808 /* Work around a common mistake in desktop files */
1809 if ((p = strrchr (info->icon_name, '.')) != NULL &&
1810 (strcmp (p, ".png") == 0 ||
1811 strcmp (p, ".xpm") == 0 ||
1812 strcmp (p, ".svg") == 0))
1815 info->icon = g_themed_icon_new (info->icon_name);
1820 info->binary = binary_from_exec (info->exec);
1822 if (info->path && info->path[0] == '\0')
1824 g_free (info->path);
1828 /* Can only be DBusActivatable if we know the filename, which means
1829 * that this won't work for the load-from-keyfile case.
1831 if (bus_activatable && info->filename)
1836 basename = g_path_get_basename (info->filename);
1837 last_dot = strrchr (basename, '.');
1839 if (last_dot && g_str_equal (last_dot, ".desktop"))
1843 if (g_dbus_is_name (basename) && basename[0] != ':')
1844 info->app_id = g_strdup (basename);
1850 info->keyfile = g_key_file_ref (key_file);
1856 g_desktop_app_info_load_file (GDesktopAppInfo *self)
1859 gboolean retval = FALSE;
1861 g_return_val_if_fail (self->filename != NULL, FALSE);
1863 self->desktop_id = g_path_get_basename (self->filename);
1865 key_file = g_key_file_new ();
1867 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
1868 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
1870 g_key_file_unref (key_file);
1875 * g_desktop_app_info_new_from_keyfile:
1876 * @key_file: an opened #GKeyFile
1878 * Creates a new #GDesktopAppInfo.
1880 * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
1885 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
1887 GDesktopAppInfo *info;
1889 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1890 info->filename = NULL;
1891 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
1893 g_object_unref (info);
1900 * g_desktop_app_info_new_from_filename:
1901 * @filename: (type filename): the path of a desktop file, in the GLib
1904 * Creates a new #GDesktopAppInfo.
1906 * Returns: (nullable): a new #GDesktopAppInfo or %NULL on error.
1909 g_desktop_app_info_new_from_filename (const char *filename)
1911 GDesktopAppInfo *info = NULL;
1913 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
1914 if (!g_desktop_app_info_load_file (info))
1916 g_object_unref (info);
1923 * g_desktop_app_info_new:
1924 * @desktop_id: the desktop file id
1926 * Creates a new #GDesktopAppInfo based on a desktop file id.
1928 * A desktop file id is the basename of the desktop file, including the
1929 * .desktop extension. GIO is looking for a desktop file with this name
1930 * in the `applications` subdirectories of the XDG
1931 * data directories (i.e. the directories specified in the `XDG_DATA_HOME`
1932 * and `XDG_DATA_DIRS` environment variables). GIO also supports the
1933 * prefix-to-subdirectory mapping that is described in the
1934 * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/)
1935 * (i.e. a desktop id of kde-foo.desktop will match
1936 * `/usr/share/applications/kde/foo.desktop`).
1938 * Returns: (nullable): a new #GDesktopAppInfo, or %NULL if no desktop
1939 * file with that id exists.
1942 g_desktop_app_info_new (const char *desktop_id)
1944 GDesktopAppInfo *appinfo = NULL;
1947 desktop_file_dirs_lock ();
1949 for (i = 0; i < n_desktop_file_dirs; i++)
1951 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
1957 desktop_file_dirs_unlock ();
1959 if (appinfo == NULL)
1962 g_free (appinfo->desktop_id);
1963 appinfo->desktop_id = g_strdup (desktop_id);
1965 if (g_desktop_app_info_get_is_hidden (appinfo))
1967 g_object_unref (appinfo);
1975 g_desktop_app_info_dup (GAppInfo *appinfo)
1977 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1978 GDesktopAppInfo *new_info;
1980 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1982 new_info->filename = g_strdup (info->filename);
1983 new_info->desktop_id = g_strdup (info->desktop_id);
1986 new_info->keyfile = g_key_file_ref (info->keyfile);
1988 new_info->name = g_strdup (info->name);
1989 new_info->generic_name = g_strdup (info->generic_name);
1990 new_info->fullname = g_strdup (info->fullname);
1991 new_info->keywords = g_strdupv (info->keywords);
1992 new_info->comment = g_strdup (info->comment);
1993 new_info->nodisplay = info->nodisplay;
1994 new_info->icon_name = g_strdup (info->icon_name);
1996 new_info->icon = g_object_ref (info->icon);
1997 new_info->only_show_in = g_strdupv (info->only_show_in);
1998 new_info->not_show_in = g_strdupv (info->not_show_in);
1999 new_info->try_exec = g_strdup (info->try_exec);
2000 new_info->exec = g_strdup (info->exec);
2001 new_info->binary = g_strdup (info->binary);
2002 new_info->path = g_strdup (info->path);
2003 new_info->app_id = g_strdup (info->app_id);
2004 new_info->hidden = info->hidden;
2005 new_info->terminal = info->terminal;
2006 new_info->startup_notify = info->startup_notify;
2008 return G_APP_INFO (new_info);
2011 /* GAppInfo interface implementation functions {{{2 */
2014 g_desktop_app_info_equal (GAppInfo *appinfo1,
2017 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
2018 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
2020 if (info1->desktop_id == NULL ||
2021 info2->desktop_id == NULL)
2022 return info1 == info2;
2024 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
2028 g_desktop_app_info_get_id (GAppInfo *appinfo)
2030 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2032 return info->desktop_id;
2036 g_desktop_app_info_get_name (GAppInfo *appinfo)
2038 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2040 if (info->name == NULL)
2041 return _("Unnamed");
2046 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
2048 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2050 if (info->fullname == NULL)
2051 return g_desktop_app_info_get_name (appinfo);
2052 return info->fullname;
2056 * g_desktop_app_info_get_is_hidden:
2057 * @info: a #GDesktopAppInfo.
2059 * A desktop file is hidden if the Hidden key in it is
2062 * Returns: %TRUE if hidden, %FALSE otherwise.
2065 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
2067 return info->hidden;
2071 * g_desktop_app_info_get_filename:
2072 * @info: a #GDesktopAppInfo
2074 * When @info was created from a known filename, return it. In some
2075 * situations such as the #GDesktopAppInfo returned from
2076 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
2078 * Returns: (type filename): The full path to the file for @info,
2079 * or %NULL if not known.
2083 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
2085 return info->filename;
2089 g_desktop_app_info_get_description (GAppInfo *appinfo)
2091 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2093 return info->comment;
2097 g_desktop_app_info_get_executable (GAppInfo *appinfo)
2099 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2101 return info->binary;
2105 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
2107 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2113 g_desktop_app_info_get_icon (GAppInfo *appinfo)
2115 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2121 * g_desktop_app_info_get_categories:
2122 * @info: a #GDesktopAppInfo
2124 * Gets the categories from the desktop file.
2126 * Returns: The unparsed Categories key from the desktop file;
2127 * i.e. no attempt is made to split it by ';' or validate it.
2130 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
2132 return info->categories;
2136 * g_desktop_app_info_get_keywords:
2137 * @info: a #GDesktopAppInfo
2139 * Gets the keywords from the desktop file.
2141 * Returns: (transfer none): The value of the Keywords key
2145 const char * const *
2146 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
2148 return (const char * const *)info->keywords;
2152 * g_desktop_app_info_get_generic_name:
2153 * @info: a #GDesktopAppInfo
2155 * Gets the generic name from the destkop file.
2157 * Returns: The value of the GenericName key
2160 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
2162 return info->generic_name;
2166 * g_desktop_app_info_get_nodisplay:
2167 * @info: a #GDesktopAppInfo
2169 * Gets the value of the NoDisplay key, which helps determine if the
2170 * application info should be shown in menus. See
2171 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
2173 * Returns: The value of the NoDisplay key
2178 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
2180 return info->nodisplay;
2184 * g_desktop_app_info_get_show_in:
2185 * @info: a #GDesktopAppInfo
2186 * @desktop_env: (nullable): a string specifying a desktop name
2188 * Checks if the application info should be shown in menus that list available
2189 * applications for a specific name of the desktop, based on the
2190 * `OnlyShowIn` and `NotShowIn` keys.
2192 * @desktop_env should typically be given as %NULL, in which case the
2193 * `XDG_CURRENT_DESKTOP` environment variable is consulted. If you want
2194 * to override the default mechanism then you may specify @desktop_env,
2195 * but this is not recommended.
2197 * Note that g_app_info_should_show() for @info will include this check (with
2198 * %NULL for @desktop_env) as well as additional checks.
2200 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
2201 * `OnlyShowIn` and `NotShowIn` keys, %FALSE
2207 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
2208 const gchar *desktop_env)
2210 const gchar *specified_envs[] = { desktop_env, NULL };
2211 const gchar * const *envs;
2214 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
2217 envs = specified_envs;
2219 envs = get_current_desktops (NULL);
2221 for (i = 0; envs[i]; i++)
2225 if (info->only_show_in)
2226 for (j = 0; info->only_show_in[j]; j++)
2227 if (g_str_equal (info->only_show_in[j], envs[i]))
2230 if (info->not_show_in)
2231 for (j = 0; info->not_show_in[j]; j++)
2232 if (g_str_equal (info->not_show_in[j], envs[i]))
2236 return info->only_show_in == NULL;
2239 /* Launching... {{{2 */
2242 expand_macro_single (char macro, const char *uri)
2245 char *result = NULL;
2249 file = g_file_new_for_uri (uri);
2255 result = g_shell_quote (uri);
2259 path = g_file_get_path (file);
2261 result = g_shell_quote (path);
2265 path = g_file_get_path (file);
2268 name = g_path_get_dirname (path);
2269 result = g_shell_quote (name);
2275 path = g_file_get_path (file);
2278 name = g_path_get_basename (path);
2279 result = g_shell_quote (name);
2285 g_object_unref (file);
2292 expand_macro_uri (char macro, const char *uri, gboolean force_file_uri, char force_file_uri_macro)
2294 char *expanded = NULL;
2296 g_return_val_if_fail (uri != NULL, NULL);
2298 if (!force_file_uri ||
2299 /* Pass URI if it contains an anchor */
2300 strchr (uri, '#') != NULL)
2302 expanded = expand_macro_single (macro, uri);
2306 expanded = expand_macro_single (force_file_uri_macro, uri);
2307 if (expanded == NULL)
2308 expanded = expand_macro_single (macro, uri);
2315 expand_macro (char macro,
2317 GDesktopAppInfo *info,
2320 GList *uris = *uri_list;
2321 char *expanded = NULL;
2322 gboolean force_file_uri;
2323 char force_file_uri_macro;
2326 g_return_if_fail (exec != NULL);
2328 /* On %u and %U, pass POSIX file path pointing to the URI via
2329 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
2330 * running or the URI doesn't have a POSIX file path via FUSE
2331 * we'll just pass the URI.
2333 force_file_uri_macro = macro;
2334 force_file_uri = FALSE;
2340 force_file_uri_macro = 'f';
2341 force_file_uri = TRUE;
2344 force_file_uri_macro = 'F';
2345 force_file_uri = TRUE;
2361 expanded = expand_macro_uri (macro, uri,
2362 force_file_uri, force_file_uri_macro);
2365 g_string_append (exec, expanded);
2380 expanded = expand_macro_uri (macro, uri,
2381 force_file_uri, force_file_uri_macro);
2384 g_string_append (exec, expanded);
2390 if (uris != NULL && expanded)
2391 g_string_append_c (exec, ' ');
2397 if (info->icon_name)
2399 g_string_append (exec, "--icon ");
2400 expanded = g_shell_quote (info->icon_name);
2401 g_string_append (exec, expanded);
2409 expanded = g_shell_quote (info->name);
2410 g_string_append (exec, expanded);
2418 expanded = g_shell_quote (info->filename);
2419 g_string_append (exec, expanded);
2424 case 'm': /* deprecated */
2428 g_string_append_c (exec, '%');
2436 expand_application_parameters (GDesktopAppInfo *info,
2437 const gchar *exec_line,
2443 GList *uri_list = *uris;
2444 const char *p = exec_line;
2445 GString *expanded_exec;
2448 if (exec_line == NULL)
2450 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2451 _("Desktop file didn’t specify Exec field"));
2455 expanded_exec = g_string_new (NULL);
2459 if (p[0] == '%' && p[1] != '\0')
2461 expand_macro (p[1], expanded_exec, info, uris);
2465 g_string_append_c (expanded_exec, *p);
2470 /* No file substitutions */
2471 if (uri_list == *uris && uri_list != NULL)
2473 /* If there is no macro default to %f. This is also what KDE does */
2474 g_string_append_c (expanded_exec, ' ');
2475 expand_macro ('f', expanded_exec, info, uris);
2478 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
2479 g_string_free (expanded_exec, TRUE);
2484 prepend_terminal_to_vector (int *argc,
2491 char **term_argv = NULL;
2496 g_return_val_if_fail (argc != NULL, FALSE);
2497 g_return_val_if_fail (argv != NULL, FALSE);
2505 /* compute size if not given */
2508 for (i = 0; the_argv[i] != NULL; i++)
2514 term_argv = g_new0 (char *, 3);
2516 check = g_find_program_in_path ("gnome-terminal");
2519 term_argv[0] = check;
2520 /* Note that gnome-terminal takes -x and
2521 * as -e in gnome-terminal is broken we use that. */
2522 term_argv[1] = g_strdup ("-x");
2527 check = g_find_program_in_path ("nxterm");
2529 check = g_find_program_in_path ("color-xterm");
2531 check = g_find_program_in_path ("rxvt");
2533 check = g_find_program_in_path ("dtterm");
2536 check = g_strdup ("xterm");
2537 g_debug ("Couldn’t find a terminal: falling back to xterm");
2539 term_argv[0] = check;
2540 term_argv[1] = g_strdup ("-e");
2543 real_argc = term_argc + *argc;
2544 real_argv = g_new (char *, real_argc + 1);
2546 for (i = 0; i < term_argc; i++)
2547 real_argv[i] = term_argv[i];
2549 for (j = 0; j < *argc; j++, i++)
2550 real_argv[i] = (char *)the_argv[j];
2552 real_argv[i] = NULL;
2558 /* we use g_free here as we sucked all the inner strings
2559 * out from it into real_argv */
2564 #endif /* G_OS_WIN32 */
2568 create_files_for_uris (GList *uris)
2575 for (iter = uris; iter; iter = iter->next)
2577 GFile *file = g_file_new_for_uri ((char *)iter->data);
2578 res = g_list_prepend (res, file);
2581 return g_list_reverse (res);
2585 notify_desktop_launch (GDBusConnection *session_bus,
2586 GDesktopAppInfo *info,
2588 const char *display,
2593 GVariantBuilder uri_variant;
2594 GVariantBuilder extras_variant;
2596 const char *desktop_file_id;
2597 const char *gio_desktop_file;
2599 if (session_bus == NULL)
2602 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
2603 for (iter = uris; iter; iter = iter->next)
2604 g_variant_builder_add (&uri_variant, "s", iter->data);
2606 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
2607 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
2608 g_variant_builder_add (&extras_variant, "{sv}",
2612 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
2613 if (gio_desktop_file != NULL)
2614 g_variant_builder_add (&extras_variant, "{sv}",
2615 "origin-desktop-file",
2616 g_variant_new_bytestring (gio_desktop_file));
2617 if (g_get_prgname () != NULL)
2618 g_variant_builder_add (&extras_variant, "{sv}",
2620 g_variant_new_bytestring (g_get_prgname ()));
2621 g_variant_builder_add (&extras_variant, "{sv}",
2624 (gint64)getpid ()));
2627 desktop_file_id = info->filename;
2628 else if (info->desktop_id)
2629 desktop_file_id = info->desktop_id;
2631 desktop_file_id = "";
2633 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
2634 "org.gtk.gio.DesktopAppInfo",
2636 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
2637 g_variant_new_bytestring (desktop_file_id),
2638 display ? display : "",
2642 g_dbus_connection_send_message (session_bus,
2646 g_object_unref (msg);
2649 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
2652 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
2653 GDBusConnection *session_bus,
2654 const gchar *exec_line,
2656 GAppLaunchContext *launch_context,
2657 GSpawnFlags spawn_flags,
2658 GSpawnChildSetupFunc user_setup,
2659 gpointer user_setup_data,
2660 GDesktopAppLaunchCallback pid_callback,
2661 gpointer pid_callback_data,
2667 gboolean completed = FALSE;
2671 char **argv, **envp;
2674 g_return_val_if_fail (info != NULL, FALSE);
2679 envp = g_app_launch_context_get_environment (launch_context);
2681 envp = g_get_environ ();
2683 /* The GList* passed to expand_application_parameters() will be modified
2684 * internally by expand_macro(), so we need to pass a copy of it instead,
2685 * and also use that copy to control the exit condition of the loop below.
2687 dup_uris = g_list_copy (uris);
2691 GList *launched_uris;
2694 char **wrapped_argv;
2697 old_uris = dup_uris;
2698 if (!expand_application_parameters (info, exec_line, &dup_uris, &argc, &argv, error))
2701 /* Get the subset of URIs we're launching with this process */
2702 launched_uris = NULL;
2703 for (iter = old_uris; iter != NULL && iter != dup_uris; iter = iter->next)
2704 launched_uris = g_list_prepend (launched_uris, iter->data);
2705 launched_uris = g_list_reverse (launched_uris);
2707 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
2709 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2710 _("Unable to find terminal required for application"));
2715 envp = g_environ_setenv (envp,
2716 "GIO_LAUNCHED_DESKTOP_FILE",
2723 GList *launched_files = create_files_for_uris (launched_uris);
2725 if (info->startup_notify)
2727 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
2731 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
2734 g_list_free_full (launched_files, g_object_unref);
2737 if (g_once_init_enter (&gio_launch_desktop_path))
2741 /* Allow test suite to specify path to gio-launch-desktop */
2742 tmp = g_getenv ("GIO_LAUNCH_DESKTOP");
2744 /* Fall back on usual searching in $PATH */
2746 tmp = "gio-launch-desktop";
2747 g_once_init_leave (&gio_launch_desktop_path, tmp);
2750 wrapped_argv = g_new (char *, argc + 2);
2751 wrapped_argv[0] = g_strdup (gio_launch_desktop_path);
2753 for (i = 0; i < argc; i++)
2754 wrapped_argv[i + 1] = g_steal_pointer (&argv[i]);
2756 wrapped_argv[i + 1] = NULL;
2760 if (!g_spawn_async_with_fds (info->path,
2773 g_app_launch_context_launch_failed (launch_context, sn_id);
2776 g_list_free (launched_uris);
2781 if (pid_callback != NULL)
2782 pid_callback (info, pid, pid_callback_data);
2784 if (launch_context != NULL)
2786 GVariantBuilder builder;
2787 GVariant *platform_data;
2789 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2790 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
2792 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
2793 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
2794 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
2795 g_variant_unref (platform_data);
2798 notify_desktop_launch (session_bus,
2806 g_list_free (launched_uris);
2808 g_strfreev (wrapped_argv);
2809 wrapped_argv = NULL;
2811 while (dup_uris != NULL);
2816 g_list_free (dup_uris);
2824 object_path_from_appid (const gchar *appid)
2826 gchar *appid_path, *iter;
2828 appid_path = g_strconcat ("/", appid, NULL);
2829 for (iter = appid_path; *iter; iter++)
2842 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
2844 GAppLaunchContext *launch_context)
2846 GVariantBuilder builder;
2848 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
2852 GList *launched_files = create_files_for_uris (uris);
2854 if (info->startup_notify)
2858 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
2860 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
2863 g_list_free_full (launched_files, g_object_unref);
2866 return g_variant_builder_end (&builder);
2870 launch_uris_with_dbus (GDesktopAppInfo *info,
2871 GDBusConnection *session_bus,
2873 GAppLaunchContext *launch_context,
2874 GCancellable *cancellable,
2875 GAsyncReadyCallback callback,
2878 GVariantBuilder builder;
2881 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2887 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
2888 for (iter = uris; iter; iter = iter->next)
2889 g_variant_builder_add (&builder, "s", iter->data);
2890 g_variant_builder_close (&builder);
2893 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
2895 object_path = object_path_from_appid (info->app_id);
2896 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
2897 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
2898 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
2899 cancellable, callback, user_data);
2900 g_free (object_path);
2904 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
2905 GDBusConnection *session_bus,
2907 GAppLaunchContext *launch_context,
2908 GCancellable *cancellable,
2909 GAsyncReadyCallback callback,
2912 GList *ruris = uris;
2913 char *app_id = NULL;
2915 g_return_val_if_fail (info != NULL, FALSE);
2918 app_id = g_desktop_app_info_get_string (info, "X-Flatpak");
2919 if (app_id && *app_id)
2921 ruris = g_document_portal_add_documents (uris, app_id, NULL);
2927 launch_uris_with_dbus (info, session_bus, ruris, launch_context,
2928 cancellable, callback, user_data);
2931 g_list_free_full (ruris, g_free);
2939 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
2941 GAppLaunchContext *launch_context,
2942 GSpawnFlags spawn_flags,
2943 GSpawnChildSetupFunc user_setup,
2944 gpointer user_setup_data,
2945 GDesktopAppLaunchCallback pid_callback,
2946 gpointer pid_callback_data,
2952 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2953 GDBusConnection *session_bus;
2954 gboolean success = TRUE;
2956 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
2958 if (session_bus && info->app_id)
2959 /* This is non-blocking API. Similar to launching via fork()/exec()
2960 * we don't wait around to see if the program crashed during startup.
2961 * This is what startup-notification's job is...
2963 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context,
2966 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
2967 spawn_flags, user_setup, user_setup_data,
2968 pid_callback, pid_callback_data,
2969 stdin_fd, stdout_fd, stderr_fd, error);
2971 if (session_bus != NULL)
2973 /* This asynchronous flush holds a reference until it completes,
2974 * which ensures that the following unref won't immediately kill
2975 * the connection if we were the initial owner.
2977 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
2978 g_object_unref (session_bus);
2985 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
2987 GAppLaunchContext *launch_context,
2990 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
2992 _SPAWN_FLAGS_DEFAULT,
2993 NULL, NULL, NULL, NULL,
3002 GAppLaunchContext *context;
3006 launch_uris_data_free (LaunchUrisData *data)
3008 g_clear_object (&data->context);
3009 g_list_free_full (data->uris, g_free);
3014 launch_uris_with_dbus_cb (GObject *object,
3015 GAsyncResult *result,
3018 GTask *task = G_TASK (user_data);
3019 GError *error = NULL;
3021 g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
3024 g_dbus_error_strip_remote_error (error);
3025 g_task_return_error (task, g_steal_pointer (&error));
3028 g_task_return_boolean (task, TRUE);
3030 g_object_unref (task);
3034 launch_uris_flush_cb (GObject *object,
3035 GAsyncResult *result,
3038 GTask *task = G_TASK (user_data);
3040 g_dbus_connection_flush_finish (G_DBUS_CONNECTION (object), result, NULL);
3041 g_task_return_boolean (task, TRUE);
3042 g_object_unref (task);
3046 launch_uris_bus_get_cb (GObject *object,
3047 GAsyncResult *result,
3050 GTask *task = G_TASK (user_data);
3051 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (g_task_get_source_object (task));
3052 LaunchUrisData *data = g_task_get_task_data (task);
3053 GCancellable *cancellable = g_task_get_cancellable (task);
3054 GDBusConnection *session_bus;
3055 GError *error = NULL;
3057 session_bus = g_bus_get_finish (result, NULL);
3059 if (session_bus && info->app_id)
3061 /* FIXME: The g_document_portal_add_documents() function, which is called
3062 * from the g_desktop_app_info_launch_uris_with_dbus() function, still
3063 * uses blocking calls.
3065 g_desktop_app_info_launch_uris_with_dbus (info, session_bus,
3066 data->uris, data->context,
3068 launch_uris_with_dbus_cb,
3069 g_steal_pointer (&task));
3073 /* FIXME: The D-Bus message from the notify_desktop_launch() function
3074 * can be still lost even if flush is called later. See:
3075 * https://gitlab.freedesktop.org/dbus/dbus/issues/72
3077 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec,
3078 data->uris, data->context,
3079 _SPAWN_FLAGS_DEFAULT, NULL,
3080 NULL, NULL, NULL, -1, -1, -1,
3084 g_task_return_error (task, g_steal_pointer (&error));
3085 g_object_unref (task);
3088 g_dbus_connection_flush (session_bus,
3090 launch_uris_flush_cb,
3091 g_steal_pointer (&task));
3094 g_clear_object (&session_bus);
3098 g_desktop_app_info_launch_uris_async (GAppInfo *appinfo,
3100 GAppLaunchContext *context,
3101 GCancellable *cancellable,
3102 GAsyncReadyCallback callback,
3106 LaunchUrisData *data;
3108 task = g_task_new (appinfo, cancellable, callback, user_data);
3109 g_task_set_source_tag (task, g_desktop_app_info_launch_uris_async);
3111 data = g_new0 (LaunchUrisData, 1);
3112 data->uris = g_list_copy_deep (uris, (GCopyFunc) g_strdup, NULL);
3113 data->context = (context != NULL) ? g_object_ref (context) : NULL;
3114 g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) launch_uris_data_free);
3116 g_bus_get (G_BUS_TYPE_SESSION, cancellable, launch_uris_bus_get_cb, task);
3120 g_desktop_app_info_launch_uris_finish (GAppInfo *appinfo,
3121 GAsyncResult *result,
3124 g_return_val_if_fail (g_task_is_valid (result, appinfo), FALSE);
3126 return g_task_propagate_boolean (G_TASK (result), error);
3130 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
3132 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3134 return info->exec &&
3135 ((strstr (info->exec, "%u") != NULL) ||
3136 (strstr (info->exec, "%U") != NULL));
3140 g_desktop_app_info_supports_files (GAppInfo *appinfo)
3142 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3144 return info->exec &&
3145 ((strstr (info->exec, "%f") != NULL) ||
3146 (strstr (info->exec, "%F") != NULL));
3150 g_desktop_app_info_launch (GAppInfo *appinfo,
3152 GAppLaunchContext *launch_context,
3162 uri = g_file_get_uri (files->data);
3163 uris = g_list_prepend (uris, uri);
3164 files = files->next;
3167 uris = g_list_reverse (uris);
3169 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
3171 g_list_free_full (uris, g_free);
3177 * g_desktop_app_info_launch_uris_as_manager_with_fds:
3178 * @appinfo: a #GDesktopAppInfo
3179 * @uris: (element-type utf8): List of URIs
3180 * @launch_context: (nullable): a #GAppLaunchContext
3181 * @spawn_flags: #GSpawnFlags, used for each process
3182 * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
3184 * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
3185 * @pid_callback: (scope call) (nullable): Callback for child processes
3186 * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
3187 * @stdin_fd: file descriptor to use for child's stdin, or -1
3188 * @stdout_fd: file descriptor to use for child's stdout, or -1
3189 * @stderr_fd: file descriptor to use for child's stderr, or -1
3190 * @error: return location for a #GError, or %NULL
3192 * Equivalent to g_desktop_app_info_launch_uris_as_manager() but allows
3193 * you to pass in file descriptors for the stdin, stdout and stderr streams
3194 * of the launched process.
3196 * If application launching occurs via some non-spawn mechanism (e.g. D-Bus
3197 * activation) then @stdin_fd, @stdout_fd and @stderr_fd are ignored.
3199 * Returns: %TRUE on successful launch, %FALSE otherwise.
3204 g_desktop_app_info_launch_uris_as_manager_with_fds (GDesktopAppInfo *appinfo,
3206 GAppLaunchContext *launch_context,
3207 GSpawnFlags spawn_flags,
3208 GSpawnChildSetupFunc user_setup,
3209 gpointer user_setup_data,
3210 GDesktopAppLaunchCallback pid_callback,
3211 gpointer pid_callback_data,
3217 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
3232 * g_desktop_app_info_launch_uris_as_manager:
3233 * @appinfo: a #GDesktopAppInfo
3234 * @uris: (element-type utf8): List of URIs
3235 * @launch_context: (nullable): a #GAppLaunchContext
3236 * @spawn_flags: #GSpawnFlags, used for each process
3237 * @user_setup: (scope async) (nullable): a #GSpawnChildSetupFunc, used once
3239 * @user_setup_data: (closure user_setup) (nullable): User data for @user_setup
3240 * @pid_callback: (scope call) (nullable): Callback for child processes
3241 * @pid_callback_data: (closure pid_callback) (nullable): User data for @callback
3242 * @error: return location for a #GError, or %NULL
3244 * This function performs the equivalent of g_app_info_launch_uris(),
3245 * but is intended primarily for operating system components that
3246 * launch applications. Ordinary applications should use
3247 * g_app_info_launch_uris().
3249 * If the application is launched via GSpawn, then @spawn_flags, @user_setup
3250 * and @user_setup_data are used for the call to g_spawn_async().
3251 * Additionally, @pid_callback (with @pid_callback_data) will be called to
3252 * inform about the PID of the created process. See g_spawn_async_with_pipes()
3253 * for information on certain parameter conditions that can enable an
3254 * optimized posix_spawn() codepath to be used.
3256 * If application launching occurs via some other mechanism (eg: D-Bus
3257 * activation) then @spawn_flags, @user_setup, @user_setup_data,
3258 * @pid_callback and @pid_callback_data are ignored.
3260 * Returns: %TRUE on successful launch, %FALSE otherwise.
3263 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
3265 GAppLaunchContext *launch_context,
3266 GSpawnFlags spawn_flags,
3267 GSpawnChildSetupFunc user_setup,
3268 gpointer user_setup_data,
3269 GDesktopAppLaunchCallback pid_callback,
3270 gpointer pid_callback_data,
3273 return g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
3285 /* OnlyShowIn API support {{{2 */
3288 * g_desktop_app_info_set_desktop_env:
3289 * @desktop_env: a string specifying what desktop this is
3291 * Sets the name of the desktop that the application is running in.
3292 * This is used by g_app_info_should_show() and
3293 * g_desktop_app_info_get_show_in() to evaluate the
3294 * `OnlyShowIn` and `NotShowIn`
3295 * desktop entry fields.
3297 * Should be called only once; subsequent calls are ignored.
3299 * Deprecated:2.42:do not use this API. Since 2.42 the value of the
3300 * `XDG_CURRENT_DESKTOP` environment variable will be used.
3303 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
3305 get_current_desktops (desktop_env);
3309 g_desktop_app_info_should_show (GAppInfo *appinfo)
3311 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3313 if (info->nodisplay)
3316 return g_desktop_app_info_get_show_in (info, NULL);
3319 /* mime types/default apps support {{{2 */
3328 ensure_dir (DirType type,
3331 char *path, *display_name;
3337 path = g_build_filename (g_get_user_config_dir (), NULL);
3341 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
3345 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
3349 g_assert_not_reached ();
3352 g_debug ("%s: Ensuring %s", G_STRFUNC, path);
3355 if (g_mkdir_with_parents (path, 0700) == 0)
3359 display_name = g_filename_display_name (path);
3360 if (type == APP_DIR)
3361 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3362 _("Can’t create user application configuration folder %s: %s"),
3363 display_name, g_strerror (errsv));
3365 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3366 _("Can’t create user MIME configuration folder %s: %s"),
3367 display_name, g_strerror (errsv));
3369 g_free (display_name);
3376 update_mimeapps_list (const char *desktop_id,
3377 const char *content_type,
3378 UpdateMimeFlags flags,
3381 char *dirname, *filename, *string;
3383 gboolean load_succeeded, res;
3384 char **old_list, **list;
3385 gsize length, data_size;
3388 char **content_types;
3390 /* Don't add both at start and end */
3391 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
3392 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
3394 dirname = ensure_dir (CONF_DIR, error);
3398 filename = g_build_filename (dirname, "mimeapps.list", NULL);
3401 key_file = g_key_file_new ();
3402 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
3403 if (!load_succeeded ||
3404 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
3405 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
3406 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
3408 g_key_file_free (key_file);
3409 key_file = g_key_file_new ();
3414 content_types = g_new (char *, 2);
3415 content_types[0] = g_strdup (content_type);
3416 content_types[1] = NULL;
3420 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
3423 for (k = 0; content_types && content_types[k]; k++)
3425 /* set as default, if requested so */
3426 string = g_key_file_get_string (key_file,
3427 DEFAULT_APPLICATIONS_GROUP,
3431 if (g_strcmp0 (string, desktop_id) != 0 &&
3432 (flags & UPDATE_MIME_SET_DEFAULT))
3435 string = g_strdup (desktop_id);
3437 /* add in the non-default list too, if it's not already there */
3438 flags |= UPDATE_MIME_SET_NON_DEFAULT;
3441 if (string == NULL || desktop_id == NULL)
3442 g_key_file_remove_key (key_file,
3443 DEFAULT_APPLICATIONS_GROUP,
3447 g_key_file_set_string (key_file,
3448 DEFAULT_APPLICATIONS_GROUP,
3457 /* reuse the list from above */
3461 g_strfreev (content_types);
3462 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
3465 for (k = 0; content_types && content_types[k]; k++)
3467 /* Add to the right place in the list */
3470 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
3471 content_types[k], &length, NULL);
3473 list = g_new (char *, 1 + length + 1);
3477 /* if we're adding a last-used hint, just put the application in front of the list */
3478 if (flags & UPDATE_MIME_SET_LAST_USED)
3480 /* avoid adding this again as non-default later */
3481 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3482 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3484 list[i++] = g_strdup (desktop_id);
3489 for (j = 0; old_list[j] != NULL; j++)
3491 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3493 /* rewrite other entries if they're different from the new one */
3494 list[i++] = g_strdup (old_list[j]);
3496 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3498 /* we encountered an old entry which is equal to the one we're adding as non-default,
3499 * don't change its position in the list.
3501 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3502 list[i++] = g_strdup (old_list[j]);
3507 /* add it at the end of the list */
3508 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3509 list[i++] = g_strdup (desktop_id);
3513 g_strfreev (old_list);
3515 if (list[0] == NULL || desktop_id == NULL)
3516 g_key_file_remove_key (key_file,
3517 ADDED_ASSOCIATIONS_GROUP,
3521 g_key_file_set_string_list (key_file,
3522 ADDED_ASSOCIATIONS_GROUP,
3524 (const char * const *)list, i);
3531 /* reuse the list from above */
3535 g_strfreev (content_types);
3536 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
3539 for (k = 0; content_types && content_types[k]; k++)
3541 /* Remove from removed associations group (unless remove) */
3544 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
3545 content_types[k], &length, NULL);
3547 list = g_new (char *, 1 + length + 1);
3550 if (flags & UPDATE_MIME_REMOVE)
3551 list[i++] = g_strdup (desktop_id);
3554 for (j = 0; old_list[j] != NULL; j++)
3556 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3557 list[i++] = g_strdup (old_list[j]);
3562 g_strfreev (old_list);
3564 if (list[0] == NULL || desktop_id == NULL)
3565 g_key_file_remove_key (key_file,
3566 REMOVED_ASSOCIATIONS_GROUP,
3570 g_key_file_set_string_list (key_file,
3571 REMOVED_ASSOCIATIONS_GROUP,
3573 (const char * const *)list, i);
3578 g_strfreev (content_types);
3580 data = g_key_file_to_data (key_file, &data_size, error);
3581 g_key_file_free (key_file);
3583 res = g_file_set_contents (filename, data, data_size, error);
3585 desktop_file_dirs_invalidate_user_config ();
3594 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
3595 const char *content_type,
3598 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3600 if (!g_desktop_app_info_ensure_saved (info, error))
3603 if (!info->desktop_id)
3605 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3606 _("Application information lacks an identifier"));
3610 /* both add support for the content type and set as last used */
3611 return update_mimeapps_list (info->desktop_id, content_type,
3612 UPDATE_MIME_SET_NON_DEFAULT |
3613 UPDATE_MIME_SET_LAST_USED,
3618 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
3619 const char *content_type,
3622 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3624 if (!g_desktop_app_info_ensure_saved (info, error))
3627 if (!info->desktop_id)
3629 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3630 _("Application information lacks an identifier"));
3634 return update_mimeapps_list (info->desktop_id, content_type,
3635 UPDATE_MIME_SET_DEFAULT,
3640 update_program_done (GPid pid,
3644 /* Did the application exit correctly */
3645 if (g_spawn_check_exit_status (status, NULL))
3647 /* Here we could clean out any caches in use */
3652 run_update_command (char *command,
3661 GError *error = NULL;
3664 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
3666 if (g_spawn_async ("/", argv,
3668 G_SPAWN_SEARCH_PATH |
3669 G_SPAWN_STDOUT_TO_DEV_NULL |
3670 G_SPAWN_STDERR_TO_DEV_NULL |
3671 G_SPAWN_DO_NOT_REAP_CHILD,
3672 NULL, NULL, /* No setup function */
3675 g_child_watch_add (pid, update_program_done, NULL);
3678 /* If we get an error at this point, it's quite likely the user doesn't
3679 * have an installed copy of either 'update-mime-database' or
3680 * 'update-desktop-database'. I don't think we want to popup an error
3681 * dialog at this point, so we just do a g_warning to give the user a
3682 * chance of debugging it.
3684 g_warning ("%s", error->message);
3685 g_error_free (error);
3692 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
3693 const char *extension,
3696 char *filename, *basename, *mimetype;
3700 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
3703 dirname = ensure_dir (MIMETYPE_DIR, error);
3707 basename = g_strdup_printf ("user-extension-%s.xml", extension);
3708 filename = g_build_filename (dirname, basename, NULL);
3712 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
3714 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
3719 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3720 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
3721 " <mime-type type=\"%s\">\n"
3722 " <comment>%s document</comment>\n"
3723 " <glob pattern=\"*.%s\"/>\n"
3725 "</mime-info>\n", mimetype, extension, extension);
3727 g_file_set_contents (filename, contents, -1, NULL);
3730 run_update_command ("update-mime-database", "mime");
3734 res = g_desktop_app_info_set_as_default_for_type (appinfo,
3744 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
3745 const char *content_type,
3748 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3750 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3753 return update_mimeapps_list (info->desktop_id, content_type,
3754 UPDATE_MIME_SET_NON_DEFAULT,
3759 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
3765 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
3766 const char *content_type,
3769 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3771 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3774 return update_mimeapps_list (info->desktop_id, content_type,
3779 static const char **
3780 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
3782 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3784 return (const char**) info->mime_types;
3787 /* Saving and deleting {{{2 */
3790 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
3796 char *data, *desktop_id;
3801 if (info->filename != NULL)
3804 /* This is only used for object created with
3805 * g_app_info_create_from_commandline. All other
3806 * object should have a filename
3809 dirname = ensure_dir (APP_DIR, error);
3813 key_file = g_key_file_new ();
3815 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3816 "Encoding", "UTF-8");
3817 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3818 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
3819 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3820 G_KEY_FILE_DESKTOP_KEY_TYPE,
3821 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
3823 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3824 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
3825 if (info->nodisplay)
3826 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3827 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3829 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3830 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
3832 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3833 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
3835 if (info->generic_name != NULL)
3836 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3837 GENERIC_NAME_KEY, info->generic_name);
3839 if (info->fullname != NULL)
3840 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3841 FULL_NAME_KEY, info->fullname);
3843 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3844 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
3846 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3847 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3849 data = g_key_file_to_data (key_file, &data_size, NULL);
3850 g_key_file_free (key_file);
3852 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
3853 filename = g_build_filename (dirname, desktop_id, NULL);
3854 g_free (desktop_id);
3857 fd = g_mkstemp (filename);
3862 display_name = g_filename_display_name (filename);
3863 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3864 _("Can’t create user desktop file %s"), display_name);
3865 g_free (display_name);
3871 desktop_id = g_path_get_basename (filename);
3873 /* FIXME - actually handle error */
3874 (void) g_close (fd, NULL);
3876 res = g_file_set_contents (filename, data, data_size, error);
3880 g_free (desktop_id);
3885 info->filename = filename;
3886 info->desktop_id = desktop_id;
3888 run_update_command ("update-desktop-database", "applications");
3890 /* We just dropped a file in the user's desktop file directory. Save
3891 * the monitor the bother of having to notice it and invalidate
3894 * This means that calls directly following this will be able to see
3895 * the results immediately.
3897 desktop_file_dirs_invalidate_user_data ();
3903 g_desktop_app_info_can_delete (GAppInfo *appinfo)
3905 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3909 if (strstr (info->filename, "/userapp-"))
3910 return g_access (info->filename, W_OK) == 0;
3917 g_desktop_app_info_delete (GAppInfo *appinfo)
3919 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3923 if (g_remove (info->filename) == 0)
3925 update_mimeapps_list (info->desktop_id, NULL,
3929 g_free (info->filename);
3930 info->filename = NULL;
3931 g_free (info->desktop_id);
3932 info->desktop_id = NULL;
3941 /* Create for commandline {{{2 */
3943 * g_app_info_create_from_commandline:
3944 * @commandline: (type filename): the commandline to use
3945 * @application_name: (nullable): the application name, or %NULL to use @commandline
3946 * @flags: flags that can specify details of the created #GAppInfo
3947 * @error: a #GError location to store the error occurring, %NULL to ignore.
3949 * Creates a new #GAppInfo from the given information.
3951 * Note that for @commandline, the quoting rules of the Exec key of the
3952 * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
3953 * are applied. For example, if the @commandline contains
3954 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
3955 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
3957 * Returns: (transfer full): new #GAppInfo for given command.
3960 g_app_info_create_from_commandline (const char *commandline,
3961 const char *application_name,
3962 GAppInfoCreateFlags flags,
3967 GDesktopAppInfo *info;
3969 g_return_val_if_fail (commandline, NULL);
3971 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
3973 info->filename = NULL;
3974 info->desktop_id = NULL;
3976 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
3977 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
3978 info->hidden = FALSE;
3979 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
3980 info->exec = g_strconcat (commandline, " %u", NULL);
3982 info->exec = g_strconcat (commandline, " %f", NULL);
3983 info->nodisplay = TRUE;
3984 info->binary = binary_from_exec (info->exec);
3986 if (application_name)
3987 info->name = g_strdup (application_name);
3990 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
3991 split = g_strsplit (commandline, " ", 2);
3992 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
3994 info->name = basename;
3995 if (info->name == NULL)
3996 info->name = g_strdup ("custom");
3998 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
4000 return G_APP_INFO (info);
4003 /* GAppInfo interface init */
4006 g_desktop_app_info_iface_init (GAppInfoIface *iface)
4008 iface->dup = g_desktop_app_info_dup;
4009 iface->equal = g_desktop_app_info_equal;
4010 iface->get_id = g_desktop_app_info_get_id;
4011 iface->get_name = g_desktop_app_info_get_name;
4012 iface->get_description = g_desktop_app_info_get_description;
4013 iface->get_executable = g_desktop_app_info_get_executable;
4014 iface->get_icon = g_desktop_app_info_get_icon;
4015 iface->launch = g_desktop_app_info_launch;
4016 iface->supports_uris = g_desktop_app_info_supports_uris;
4017 iface->supports_files = g_desktop_app_info_supports_files;
4018 iface->launch_uris = g_desktop_app_info_launch_uris;
4019 iface->launch_uris_async = g_desktop_app_info_launch_uris_async;
4020 iface->launch_uris_finish = g_desktop_app_info_launch_uris_finish;
4021 iface->should_show = g_desktop_app_info_should_show;
4022 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
4023 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
4024 iface->add_supports_type = g_desktop_app_info_add_supports_type;
4025 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
4026 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
4027 iface->can_delete = g_desktop_app_info_can_delete;
4028 iface->do_delete = g_desktop_app_info_delete;
4029 iface->get_commandline = g_desktop_app_info_get_commandline;
4030 iface->get_display_name = g_desktop_app_info_get_display_name;
4031 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
4032 iface->get_supported_types = g_desktop_app_info_get_supported_types;
4035 /* Recommended applications {{{2 */
4037 /* Converts content_type into a list of itself with all of its parent
4038 * types (if include_fallback is enabled) or just returns a single-item
4039 * list with the unaliased content type.
4042 get_list_of_mimetypes (const gchar *content_type,
4043 gboolean include_fallback)
4048 array = g_ptr_array_new ();
4049 unaliased = _g_unix_content_type_unalias (content_type);
4050 g_ptr_array_add (array, unaliased);
4052 if (include_fallback)
4056 /* Iterate the array as we grow it, until we have nothing more to add */
4057 for (i = 0; i < array->len; i++)
4059 gchar **parents = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
4062 for (j = 0; parents[j]; j++)
4063 /* Don't add duplicates */
4064 if (!array_contains (array, parents[j]))
4065 g_ptr_array_add (array, parents[j]);
4067 g_free (parents[j]);
4069 /* We already stole or freed each element. Free the container. */
4074 g_ptr_array_add (array, NULL);
4076 return (gchar **) g_ptr_array_free (array, FALSE);
4080 g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
4081 gboolean include_fallback)
4083 GPtrArray *hits, *blacklist;
4087 hits = g_ptr_array_new ();
4088 blacklist = g_ptr_array_new ();
4090 types = get_list_of_mimetypes (content_type, include_fallback);
4092 desktop_file_dirs_lock ();
4094 for (i = 0; types[i]; i++)
4095 for (j = 0; j < n_desktop_file_dirs; j++)
4096 desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], hits, blacklist);
4098 /* We will keep the hits past unlocking, so we must dup them */
4099 for (i = 0; i < hits->len; i++)
4100 hits->pdata[i] = g_strdup (hits->pdata[i]);
4102 desktop_file_dirs_unlock ();
4104 g_ptr_array_add (hits, NULL);
4106 g_ptr_array_free (blacklist, TRUE);
4109 return (gchar **) g_ptr_array_free (hits, FALSE);
4113 * g_app_info_get_recommended_for_type:
4114 * @content_type: the content type to find a #GAppInfo for
4116 * Gets a list of recommended #GAppInfos for a given content type, i.e.
4117 * those applications which claim to support the given content type exactly,
4118 * and not by MIME type subclassing.
4119 * Note that the first application of the list is the last used one, i.e.
4120 * the last one for which g_app_info_set_as_last_used_for_type() has been
4123 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
4124 * for given @content_type or %NULL on error.
4129 g_app_info_get_recommended_for_type (const gchar *content_type)
4131 gchar **desktop_ids;
4135 g_return_val_if_fail (content_type != NULL, NULL);
4137 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
4140 for (i = 0; desktop_ids[i]; i++)
4142 GDesktopAppInfo *info;
4144 info = g_desktop_app_info_new (desktop_ids[i]);
4146 infos = g_list_prepend (infos, info);
4149 g_strfreev (desktop_ids);
4151 return g_list_reverse (infos);
4155 * g_app_info_get_fallback_for_type:
4156 * @content_type: the content type to find a #GAppInfo for
4158 * Gets a list of fallback #GAppInfos for a given content type, i.e.
4159 * those applications which claim to support the given content type
4160 * by MIME type subclassing and not directly.
4162 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
4163 * for given @content_type or %NULL on error.
4168 g_app_info_get_fallback_for_type (const gchar *content_type)
4170 gchar **recommended_ids;
4175 g_return_val_if_fail (content_type != NULL, NULL);
4177 recommended_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
4178 all_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
4181 for (i = 0; all_ids[i]; i++)
4183 GDesktopAppInfo *info;
4186 /* Don't return the ones on the recommended list */
4187 for (j = 0; recommended_ids[j]; j++)
4188 if (g_str_equal (all_ids[i], recommended_ids[j]))
4191 if (recommended_ids[j])
4194 info = g_desktop_app_info_new (all_ids[i]);
4197 infos = g_list_prepend (infos, info);
4200 g_strfreev (recommended_ids);
4201 g_strfreev (all_ids);
4203 return g_list_reverse (infos);
4207 * g_app_info_get_all_for_type:
4208 * @content_type: the content type to find a #GAppInfo for
4210 * Gets a list of all #GAppInfos for a given content type,
4211 * including the recommended and fallback #GAppInfos. See
4212 * g_app_info_get_recommended_for_type() and
4213 * g_app_info_get_fallback_for_type().
4215 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
4216 * for given @content_type or %NULL on error.
4219 g_app_info_get_all_for_type (const char *content_type)
4221 gchar **desktop_ids;
4225 g_return_val_if_fail (content_type != NULL, NULL);
4227 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
4230 for (i = 0; desktop_ids[i]; i++)
4232 GDesktopAppInfo *info;
4234 info = g_desktop_app_info_new (desktop_ids[i]);
4236 infos = g_list_prepend (infos, info);
4239 g_strfreev (desktop_ids);
4241 return g_list_reverse (infos);
4245 * g_app_info_reset_type_associations:
4246 * @content_type: a content type
4248 * Removes all changes to the type associations done by
4249 * g_app_info_set_as_default_for_type(),
4250 * g_app_info_set_as_default_for_extension(),
4251 * g_app_info_add_supports_type() or
4252 * g_app_info_remove_supports_type().
4257 g_app_info_reset_type_associations (const char *content_type)
4259 update_mimeapps_list (NULL, content_type,
4265 * g_app_info_get_default_for_type:
4266 * @content_type: the content type to find a #GAppInfo for
4267 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
4270 * Gets the default #GAppInfo for a given content type.
4272 * Returns: (transfer full): #GAppInfo for given @content_type or
4276 g_app_info_get_default_for_type (const char *content_type,
4277 gboolean must_support_uris)
4279 GPtrArray *blacklist;
4285 g_return_val_if_fail (content_type != NULL, NULL);
4287 types = get_list_of_mimetypes (content_type, TRUE);
4289 blacklist = g_ptr_array_new ();
4290 results = g_ptr_array_new ();
4293 desktop_file_dirs_lock ();
4295 for (i = 0; types[i]; i++)
4297 /* Collect all the default apps for this type */
4298 for (j = 0; j < n_desktop_file_dirs; j++)
4299 desktop_file_dir_default_lookup (&desktop_file_dirs[j], types[i], results);
4301 /* Consider the associations as well... */
4302 for (j = 0; j < n_desktop_file_dirs; j++)
4303 desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], results, blacklist);
4305 /* (If any), see if one of those apps is installed... */
4306 for (j = 0; j < results->len; j++)
4308 const gchar *desktop_id = g_ptr_array_index (results, j);
4310 for (k = 0; k < n_desktop_file_dirs; k++)
4312 info = (GAppInfo *) desktop_file_dir_get_app (&desktop_file_dirs[k], desktop_id);
4316 if (!must_support_uris || g_app_info_supports_uris (info))
4319 g_clear_object (&info);
4324 /* Reset the list, ready to try again with the next (parent)
4325 * mimetype, but keep the blacklist in place.
4327 g_ptr_array_set_size (results, 0);
4331 desktop_file_dirs_unlock ();
4333 g_ptr_array_unref (blacklist);
4334 g_ptr_array_unref (results);
4341 * g_app_info_get_default_for_uri_scheme:
4342 * @uri_scheme: a string containing a URI scheme.
4344 * Gets the default application for handling URIs with
4345 * the given URI scheme. A URI scheme is the initial part
4346 * of the URI, up to but not including the ':', e.g. "http",
4349 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4352 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
4355 char *content_type, *scheme_down;
4357 scheme_down = g_ascii_strdown (uri_scheme, -1);
4358 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
4359 g_free (scheme_down);
4360 app_info = g_app_info_get_default_for_type (content_type, FALSE);
4361 g_free (content_type);
4366 /* "Get all" API {{{2 */
4369 * g_desktop_app_info_get_implementations:
4370 * @interface: the name of the interface
4372 * Gets all applications that implement @interface.
4374 * An application implements an interface if that interface is listed in
4375 * the Implements= line of the desktop file of the application.
4377 * Returns: (element-type GDesktopAppInfo) (transfer full): a list of #GDesktopAppInfo
4383 g_desktop_app_info_get_implementations (const gchar *interface)
4385 GList *result = NULL;
4389 desktop_file_dirs_lock ();
4391 for (i = 0; i < n_desktop_file_dirs; i++)
4392 desktop_file_dir_get_implementations (&desktop_file_dirs[i], &result, interface);
4394 desktop_file_dirs_unlock ();
4399 gchar *name = (*ptr)->data;
4400 GDesktopAppInfo *app;
4402 app = g_desktop_app_info_new (name);
4408 ptr = &(*ptr)->next;
4411 *ptr = g_list_delete_link (*ptr, *ptr);
4418 * g_desktop_app_info_search:
4419 * @search_string: the search string to use
4421 * Searches desktop files for ones that match @search_string.
4423 * The return value is an array of strvs. Each strv contains a list of
4424 * applications that matched @search_string with an equal score. The
4425 * outer list is sorted by score so that the first strv contains the
4426 * best-matching applications, and so on.
4427 * The algorithm for determining matches is undefined and may change at
4430 * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
4431 * list of strvs. Free each item with g_strfreev() and free the outer
4432 * list with g_free().
4435 g_desktop_app_info_search (const gchar *search_string)
4437 gchar **search_tokens;
4438 gint last_category = -1;
4440 gint n_categories = 0;
4441 gint start_of_category;
4444 search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
4446 desktop_file_dirs_lock ();
4448 reset_total_search_results ();
4450 for (i = 0; i < n_desktop_file_dirs; i++)
4452 for (j = 0; search_tokens[j]; j++)
4454 desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
4455 merge_token_results (j == 0);
4457 merge_directory_results ();
4460 sort_total_search_results ();
4462 /* Count the total number of unique categories */
4463 for (i = 0; i < static_total_results_size; i++)
4464 if (static_total_results[i].category != last_category)
4466 last_category = static_total_results[i].category;
4470 results = g_new (gchar **, n_categories + 1);
4472 /* Start loading into the results list */
4473 start_of_category = 0;
4474 for (i = 0; i < n_categories; i++)
4476 gint n_items_in_category = 0;
4480 this_category = static_total_results[start_of_category].category;
4482 while (start_of_category + n_items_in_category < static_total_results_size &&
4483 static_total_results[start_of_category + n_items_in_category].category == this_category)
4484 n_items_in_category++;
4486 results[i] = g_new (gchar *, n_items_in_category + 1);
4487 for (j = 0; j < n_items_in_category; j++)
4488 results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
4489 results[i][j] = NULL;
4491 start_of_category += n_items_in_category;
4495 desktop_file_dirs_unlock ();
4497 g_strfreev (search_tokens);
4503 * g_app_info_get_all:
4505 * Gets a list of all of the applications currently registered
4508 * For desktop files, this includes applications that have
4509 * `NoDisplay=true` set or are excluded from display by means
4510 * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
4511 * The returned list does not include applications which have
4512 * the `Hidden` key set.
4514 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
4517 g_app_info_get_all (void)
4520 GHashTableIter iter;
4525 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
4527 desktop_file_dirs_lock ();
4529 for (i = 0; i < n_desktop_file_dirs; i++)
4530 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
4532 desktop_file_dirs_unlock ();
4535 g_hash_table_iter_init (&iter, apps);
4536 while (g_hash_table_iter_next (&iter, NULL, &value))
4539 infos = g_list_prepend (infos, value);
4542 g_hash_table_destroy (apps);
4547 /* GDesktopAppInfoLookup interface {{{2 */
4550 * GDesktopAppInfoLookup:
4552 * #GDesktopAppInfoLookup is an opaque data structure and can only be accessed
4553 * using the following functions.
4556 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4558 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
4559 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
4562 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
4566 /* "Get for mime type" APIs {{{2 */
4569 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
4570 * @lookup: a #GDesktopAppInfoLookup
4571 * @uri_scheme: a string containing a URI scheme.
4573 * Gets the default application for launching applications
4574 * using this URI scheme for a particular GDesktopAppInfoLookup
4577 * The GDesktopAppInfoLookup interface and this function is used
4578 * to implement g_app_info_get_default_for_uri_scheme() backends
4579 * in a GIO module. There is no reason for applications to use it
4580 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
4582 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4584 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
4587 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
4588 const char *uri_scheme)
4590 GDesktopAppInfoLookupIface *iface;
4592 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
4594 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
4596 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
4599 G_GNUC_END_IGNORE_DEPRECATIONS
4601 /* Misc getter APIs {{{2 */
4604 * g_desktop_app_info_get_startup_wm_class:
4605 * @info: a #GDesktopAppInfo that supports startup notify
4607 * Retrieves the StartupWMClass field from @info. This represents the
4608 * WM_CLASS property of the main window of the application, if launched
4611 * Returns: (transfer none): the startup WM class, or %NULL if none is set
4612 * in the desktop file.
4617 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
4619 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4621 return info->startup_wm_class;
4625 * g_desktop_app_info_get_string:
4626 * @info: a #GDesktopAppInfo
4627 * @key: the key to look up
4629 * Looks up a string value in the keyfile backing @info.
4631 * The @key is looked up in the "Desktop Entry" group.
4633 * Returns: a newly allocated string, or %NULL if the key
4639 g_desktop_app_info_get_string (GDesktopAppInfo *info,
4642 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4644 return g_key_file_get_string (info->keyfile,
4645 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4649 * g_desktop_app_info_get_locale_string:
4650 * @info: a #GDesktopAppInfo
4651 * @key: the key to look up
4653 * Looks up a localized string value in the keyfile backing @info
4654 * translated to the current locale.
4656 * The @key is looked up in the "Desktop Entry" group.
4658 * Returns: (nullable): a newly allocated string, or %NULL if the key
4664 g_desktop_app_info_get_locale_string (GDesktopAppInfo *info,
4667 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4668 g_return_val_if_fail (key != NULL && *key != '\0', NULL);
4670 return g_key_file_get_locale_string (info->keyfile,
4671 G_KEY_FILE_DESKTOP_GROUP,
4676 * g_desktop_app_info_get_boolean:
4677 * @info: a #GDesktopAppInfo
4678 * @key: the key to look up
4680 * Looks up a boolean value in the keyfile backing @info.
4682 * The @key is looked up in the "Desktop Entry" group.
4684 * Returns: the boolean value, or %FALSE if the key
4690 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
4693 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4695 return g_key_file_get_boolean (info->keyfile,
4696 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4700 * g_desktop_app_info_get_string_list:
4701 * @info: a #GDesktopAppInfo
4702 * @key: the key to look up
4703 * @length: (out) (optional): return location for the number of returned strings, or %NULL
4705 * Looks up a string list value in the keyfile backing @info.
4707 * The @key is looked up in the "Desktop Entry" group.
4709 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
4710 * a %NULL-terminated string array or %NULL if the specified
4711 * key cannot be found. The array should be freed with g_strfreev().
4716 g_desktop_app_info_get_string_list (GDesktopAppInfo *info,
4720 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4722 return g_key_file_get_string_list (info->keyfile,
4723 G_KEY_FILE_DESKTOP_GROUP, key, length, NULL);
4727 * g_desktop_app_info_has_key:
4728 * @info: a #GDesktopAppInfo
4729 * @key: the key to look up
4731 * Returns whether @key exists in the "Desktop Entry" group
4732 * of the keyfile backing @info.
4734 * Returns: %TRUE if the @key exists
4739 g_desktop_app_info_has_key (GDesktopAppInfo *info,
4742 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4744 return g_key_file_has_key (info->keyfile,
4745 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4748 /* Desktop actions support {{{2 */
4751 * g_desktop_app_info_list_actions:
4752 * @info: a #GDesktopAppInfo
4754 * Returns the list of "additional application actions" supported on the
4755 * desktop file, as per the desktop file specification.
4757 * As per the specification, this is the list of actions that are
4758 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
4760 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
4764 const gchar * const *
4765 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
4767 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4769 return (const gchar **) info->actions;
4773 app_info_has_action (GDesktopAppInfo *info,
4774 const gchar *action_name)
4778 for (i = 0; info->actions[i]; i++)
4779 if (g_str_equal (info->actions[i], action_name))
4786 * g_desktop_app_info_get_action_name:
4787 * @info: a #GDesktopAppInfo
4788 * @action_name: the name of the action as from
4789 * g_desktop_app_info_list_actions()
4791 * Gets the user-visible display name of the "additional application
4792 * action" specified by @action_name.
4794 * This corresponds to the "Name" key within the keyfile group for the
4797 * Returns: (transfer full): the locale-specific action name
4802 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4803 const gchar *action_name)
4808 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4809 g_return_val_if_fail (action_name != NULL, NULL);
4810 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4812 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4813 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4814 g_free (group_name);
4816 /* The spec says that the Name field must be given.
4818 * If it's not, let's follow the behaviour of our get_name()
4819 * implementation above and never return %NULL.
4822 result = g_strdup (_("Unnamed"));
4828 * g_desktop_app_info_launch_action:
4829 * @info: a #GDesktopAppInfo
4830 * @action_name: the name of the action as from
4831 * g_desktop_app_info_list_actions()
4832 * @launch_context: (nullable): a #GAppLaunchContext
4834 * Activates the named application action.
4836 * You may only call this function on action names that were
4837 * returned from g_desktop_app_info_list_actions().
4839 * Note that if the main entry of the desktop file indicates that the
4840 * application supports startup notification, and @launch_context is
4841 * non-%NULL, then startup notification will be used when activating the
4842 * action (and as such, invocation of the action on the receiving side
4843 * must signal the end of startup notification when it is completed).
4844 * This is the expected behaviour of applications declaring additional
4845 * actions, as per the desktop file specification.
4847 * As with g_app_info_launch() there is no way to detect failures that
4848 * occur while using this function.
4853 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4854 const gchar *action_name,
4855 GAppLaunchContext *launch_context)
4857 GDBusConnection *session_bus;
4859 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4860 g_return_if_fail (action_name != NULL);
4861 g_return_if_fail (app_info_has_action (info, action_name));
4863 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4865 if (session_bus && info->app_id)
4869 object_path = object_path_from_appid (info->app_id);
4870 g_dbus_connection_call (session_bus, info->app_id, object_path,
4871 "org.freedesktop.Application", "ActivateAction",
4872 g_variant_new ("(sav@a{sv})", action_name, NULL,
4873 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4874 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4875 g_free (object_path);
4882 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4883 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4884 g_free (group_name);
4887 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4888 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL,
4894 if (session_bus != NULL)
4896 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4897 g_object_unref (session_bus);
4902 /* vim:set foldmethod=marker: */