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 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 "glocaldirectorymonitor.h"
53 * SECTION:gdesktopappinfo
54 * @title: GDesktopAppInfo
55 * @short_description: Application information from desktop files
56 * @include: gio/gdesktopappinfo.h
58 * #GDesktopAppInfo is an implementation of #GAppInfo based on
61 * Note that `<gio/gdesktopappinfo.h>` belongs to the UNIX-specific
62 * GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
66 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
67 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
68 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
69 #define MIME_CACHE_GROUP "MIME Cache"
70 #define GENERIC_NAME_KEY "GenericName"
71 #define FULL_NAME_KEY "X-GNOME-FullName"
72 #define KEYWORDS_KEY "Keywords"
73 #define STARTUP_WM_CLASS_KEY "StartupWMClass"
80 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
81 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
87 * Information about an installed application from a desktop file.
89 struct _GDesktopAppInfo
91 GObject parent_instance;
113 char *startup_wm_class;
120 guint startup_notify : 1;
125 UPDATE_MIME_NONE = 1 << 0,
126 UPDATE_MIME_SET_DEFAULT = 1 << 1,
127 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
128 UPDATE_MIME_REMOVE = 1 << 3,
129 UPDATE_MIME_SET_LAST_USED = 1 << 4,
132 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
133 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO, g_desktop_app_info_iface_init))
135 G_LOCK_DEFINE_STATIC (g_desktop_env);
136 static gchar *g_desktop_env = NULL;
138 /* DesktopFileDir implementation {{{1 */
145 GLocalDirectoryMonitor *monitor;
146 GHashTable *app_names;
147 GHashTable *mime_tweaks;
148 GHashTable *memory_index;
151 static DesktopFileDir *desktop_file_dirs;
152 static guint n_desktop_file_dirs;
153 static const guint desktop_file_dir_user_config_index = 0;
154 static guint desktop_file_dir_user_data_index;
155 static GMutex desktop_file_dir_lock;
157 /* Monitor 'changed' signal handler {{{2 */
158 static void desktop_file_dir_reset (DesktopFileDir *dir);
161 desktop_file_dir_changed (GFileMonitor *monitor,
164 GFileMonitorEvent event_type,
167 DesktopFileDir *dir = user_data;
169 /* We are not interested in receiving notifications forever just
170 * because someone asked about one desktop file once.
172 * After we receive the first notification, reset the dir, destroying
173 * the monitor. We will take this as a hint, next time that we are
174 * asked, that we need to check if everything is up to date.
176 g_mutex_lock (&desktop_file_dir_lock);
178 desktop_file_dir_reset (dir);
180 g_mutex_unlock (&desktop_file_dir_lock);
182 /* Notify anyone else who may be interested */
183 g_app_info_monitor_fire ();
186 /* Internal utility functions {{{2 */
189 * desktop_file_dir_app_name_is_masked:
190 * @dir: a #DesktopFileDir
191 * @app_name: an application ID
193 * Checks if @app_name is masked for @dir.
195 * An application is masked if a similarly-named desktop file exists in
196 * a desktop file directory with higher precedence. Masked desktop
197 * files should be ignored.
200 desktop_file_dir_app_name_is_masked (DesktopFileDir *dir,
201 const gchar *app_name)
203 while (dir > desktop_file_dirs)
207 if (dir->app_names && g_hash_table_contains (dir->app_names, app_name))
214 static const gchar * const *
215 get_lowercase_current_desktops (void)
217 static gchar **result;
219 if (g_once_init_enter (&result))
224 envvar = g_getenv ("XDG_CURRENT_DESKTOP");
230 tmp = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0);
232 for (i = 0; tmp[i]; i++)
233 for (j = 0; tmp[i][j]; j++)
234 tmp[i][j] = g_ascii_tolower (tmp[i][j]);
237 tmp = g_new0 (gchar *, 0 + 1);
239 g_once_init_leave (&result, tmp);
242 return (const gchar **) result;
246 * add_to_table_if_appropriate:
247 * @apps: a string to GDesktopAppInfo hash table
248 * @app_name: the name of the application
249 * @info: a #GDesktopAppInfo, or NULL
251 * If @info is non-%NULL and non-hidden, then add it to @apps, using
252 * @app_name as a key.
254 * If @info is non-%NULL then this function will consume the passed-in
258 add_to_table_if_appropriate (GHashTable *apps,
259 const gchar *app_name,
260 GDesktopAppInfo *info)
267 g_object_unref (info);
271 g_free (info->desktop_id);
272 info->desktop_id = g_strdup (app_name);
274 g_hash_table_insert (apps, g_strdup (info->desktop_id), info);
281 DESKTOP_KEY_GenericName,
282 DESKTOP_KEY_Keywords,
284 DESKTOP_KEY_X_GNOME_FullName,
289 const gchar desktop_key_match_category[N_DESKTOP_KEYS] = {
290 /* Note: lower numbers are a better match.
292 * In case we want two keys to match at the same level, we can just
293 * use the same number for the two different keys.
295 [DESKTOP_KEY_Name] = 1,
296 [DESKTOP_KEY_Exec] = 2,
297 [DESKTOP_KEY_Keywords] = 3,
298 [DESKTOP_KEY_GenericName] = 4,
299 [DESKTOP_KEY_X_GNOME_FullName] = 5,
300 [DESKTOP_KEY_Comment] = 6
304 desktop_key_get_name (guint key_id)
308 case DESKTOP_KEY_Comment:
310 case DESKTOP_KEY_Exec:
312 case DESKTOP_KEY_GenericName:
313 return "GenericName";
314 case DESKTOP_KEY_Keywords:
316 case DESKTOP_KEY_Name:
318 case DESKTOP_KEY_X_GNOME_FullName:
319 return "X-GNOME-FullName";
321 g_assert_not_reached ();
325 /* Search global state {{{2
327 * We only ever search under a global lock, so we can use (and reuse)
328 * some global data to reduce allocations made while searching.
330 * In short, we keep around arrays of results that we expand as needed
331 * (and never shrink).
333 * static_token_results: this is where we append the results for each
334 * token within a given desktop directory, as we handle it (which is
335 * a union of all matches for this term)
337 * static_search_results: this is where we build the complete results
338 * for a single directory (which is an intersection of the matches
339 * found for each term)
341 * static_total_results: this is where we build the complete results
342 * across all directories (which is a union of the matches found in
345 * The app_names that enter these tables are always pointer-unique (in
346 * the sense that string equality is the same as pointer equality).
347 * This can be guaranteed for two reasons:
349 * - we mask appids so that a given appid will only ever appear within
350 * the highest-precedence directory that contains it. We never
351 * return search results from a lower-level directory if a desktop
352 * file exists in a higher-level one.
354 * - within a given directory, the string is unique because it's the
355 * key in the hashtable of all app_ids for that directory.
357 * We perform a merging of the results in merge_token_results(). This
358 * works by ordering the two lists and moving through each of them (at
359 * the same time) looking for common elements, rejecting uncommon ones.
360 * "Order" here need not mean any particular thing, as long as it is
361 * some order. Because of the uniqueness of our strings, we can use
362 * pointer order. That's what's going on in compare_results() below.
366 const gchar *app_name;
370 static struct search_result *static_token_results;
371 static gint static_token_results_size;
372 static gint static_token_results_allocated;
373 static struct search_result *static_search_results;
374 static gint static_search_results_size;
375 static gint static_search_results_allocated;
376 static struct search_result *static_total_results;
377 static gint static_total_results_size;
378 static gint static_total_results_allocated;
380 /* And some functions for performing nice operations against it */
382 compare_results (gconstpointer a,
385 const struct search_result *ra = a;
386 const struct search_result *rb = b;
388 if (ra->app_name < rb->app_name)
391 else if (ra->app_name > rb->app_name)
395 return ra->category - rb->category;
399 compare_categories (gconstpointer a,
402 const struct search_result *ra = a;
403 const struct search_result *rb = b;
405 return ra->category - rb->category;
409 add_token_result (const gchar *app_name,
412 if G_UNLIKELY (static_token_results_size == static_token_results_allocated)
414 static_token_results_allocated = MAX (16, static_token_results_allocated * 2);
415 static_token_results = g_renew (struct search_result, static_token_results, static_token_results_allocated);
418 static_token_results[static_token_results_size].app_name = app_name;
419 static_token_results[static_token_results_size].category = category;
420 static_token_results_size++;
424 merge_token_results (gboolean first)
426 qsort (static_token_results, static_token_results_size, sizeof (struct search_result), compare_results);
428 /* If this is the first token then we are basically merging a list with
429 * itself -- we only perform de-duplication.
431 * If this is not the first token then we are doing a real merge.
435 const gchar *last_name = NULL;
438 /* We must de-duplicate, but we do so by taking the best category
441 * The final list can be as large as the input here, so make sure
442 * we have enough room (even if it's too much room).
445 if G_UNLIKELY (static_search_results_allocated < static_token_results_size)
447 static_search_results_allocated = static_token_results_allocated;
448 static_search_results = g_renew (struct search_result,
449 static_search_results,
450 static_search_results_allocated);
453 for (i = 0; i < static_token_results_size; i++)
455 /* The list is sorted so that the best match for a given id
456 * will be at the front, so once we have copied an id, skip
457 * the rest of the entries for the same id.
459 if (static_token_results[i].app_name == last_name)
462 last_name = static_token_results[i].app_name;
464 static_search_results[static_search_results_size++] = static_token_results[i];
469 const gchar *last_name = NULL;
473 /* We only ever remove items from the results list, so no need to
474 * resize to ensure that we have enough room.
476 for (i = 0; i < static_token_results_size; i++)
478 if (static_token_results[i].app_name == last_name)
481 last_name = static_token_results[i].app_name;
483 /* Now we only want to have a result in static_search_results
484 * if we already have it there *and* we have it in
485 * static_token_results as well. The category will be the
488 * Skip past the results in static_search_results that are not
489 * going to be matches.
491 while (k < static_search_results_size &&
492 static_search_results[k].app_name < static_token_results[i].app_name)
495 if (k < static_search_results_size &&
496 static_search_results[k].app_name == static_token_results[i].app_name)
500 * Category should be the worse of the two (ie:
501 * numerically larger).
503 static_search_results[j].app_name = static_search_results[k].app_name;
504 static_search_results[j].category = MAX (static_search_results[k].category,
505 static_token_results[i].category);
510 static_search_results_size = j;
513 /* Clear it out for next time... */
514 static_token_results_size = 0;
518 reset_total_search_results (void)
520 static_total_results_size = 0;
524 sort_total_search_results (void)
526 qsort (static_total_results, static_total_results_size, sizeof (struct search_result), compare_categories);
530 merge_directory_results (void)
532 if G_UNLIKELY (static_total_results_size + static_search_results_size > static_total_results_allocated)
534 static_total_results_allocated = MAX (16, static_total_results_allocated);
535 while (static_total_results_allocated < static_total_results_size + static_search_results_size)
536 static_total_results_allocated *= 2;
537 static_total_results = g_renew (struct search_result, static_total_results, static_total_results_allocated);
540 memcpy (static_total_results + static_total_results_size,
541 static_search_results,
542 static_search_results_size * sizeof (struct search_result));
544 static_total_results_size += static_search_results_size;
546 /* Clear it out for next time... */
547 static_search_results_size = 0;
550 /* Support for unindexed DesktopFileDirs {{{2 */
552 get_apps_from_dir (GHashTable **apps,
556 const char *basename;
559 dir = g_dir_open (dirname, 0, NULL);
564 while ((basename = g_dir_read_name (dir)) != NULL)
568 filename = g_build_filename (dirname, basename, NULL);
570 if (g_str_has_suffix (basename, ".desktop"))
574 app_name = g_strconcat (prefix, basename, NULL);
577 *apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
579 g_hash_table_insert (*apps, app_name, g_strdup (filename));
581 else if (g_file_test (filename, G_FILE_TEST_IS_DIR))
585 subprefix = g_strconcat (prefix, basename, "-", NULL);
586 get_apps_from_dir (apps, filename, subprefix);
601 } UnindexedMimeTweaks;
604 free_mime_tweaks (gpointer data)
606 UnindexedMimeTweaks *tweaks = data;
608 g_strfreev (tweaks->additions);
609 g_strfreev (tweaks->removals);
610 g_strfreev (tweaks->defaults);
612 g_slice_free (UnindexedMimeTweaks, tweaks);
615 static UnindexedMimeTweaks *
616 desktop_file_dir_unindexed_get_tweaks (DesktopFileDir *dir,
617 const gchar *mime_type)
619 UnindexedMimeTweaks *tweaks;
620 gchar *unaliased_type;
622 unaliased_type = _g_unix_content_type_unalias (mime_type);
623 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
627 tweaks = g_slice_new0 (UnindexedMimeTweaks);
628 g_hash_table_insert (dir->mime_tweaks, unaliased_type, tweaks);
631 g_free (unaliased_type);
636 /* consumes 'to_add' */
638 expand_strv (gchar ***strv_ptr,
640 gchar * const *blacklist)
642 guint strv_len, add_len;
653 strv_len = g_strv_length (strv);
654 add_len = g_strv_length (to_add);
655 strv = g_renew (gchar *, strv, strv_len + add_len + 1);
657 for (i = 0; to_add[i]; i++)
659 /* Don't add blacklisted strings */
661 for (j = 0; blacklist[j]; j++)
662 if (g_str_equal (to_add[i], blacklist[j]))
665 /* Don't add duplicates already in the list */
666 for (j = 0; j < strv_len; j++)
667 if (g_str_equal (to_add[i], strv[j]))
670 strv[strv_len++] = to_add[i];
677 strv[strv_len] = NULL;
684 desktop_file_dir_unindexed_read_mimeapps_list (DesktopFileDir *dir,
685 const gchar *filename,
686 const gchar *added_group,
687 gboolean tweaks_permitted)
689 const gchar default_group[] = "Default Applications";
690 const gchar removed_group[] = "Removed Assocations";
691 UnindexedMimeTweaks *tweaks;
692 char **desktop_file_ids;
697 key_file = g_key_file_new ();
698 if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL))
700 g_key_file_free (key_file);
704 mime_types = g_key_file_get_keys (key_file, added_group, NULL, NULL);
706 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
708 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
709 "mimeapps.list file may add or remove associations.", filename, added_group);
710 g_strfreev (mime_types);
714 if (mime_types != NULL)
716 for (i = 0; mime_types[i] != NULL; i++)
718 desktop_file_ids = g_key_file_get_string_list (key_file, added_group, mime_types[i], NULL, NULL);
720 if (desktop_file_ids)
722 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
723 expand_strv (&tweaks->additions, desktop_file_ids, tweaks->removals);
727 g_strfreev (mime_types);
730 mime_types = g_key_file_get_keys (key_file, removed_group, NULL, NULL);
732 if G_UNLIKELY (mime_types != NULL && !tweaks_permitted)
734 g_warning ("%s contains a [%s] group, but it is not permitted here. Only the non-desktop-specific "
735 "mimeapps.list file may add or remove associations.", filename, removed_group);
736 g_strfreev (mime_types);
740 if (mime_types != NULL)
742 for (i = 0; mime_types[i] != NULL; i++)
744 desktop_file_ids = g_key_file_get_string_list (key_file, removed_group, mime_types[i], NULL, NULL);
746 if (desktop_file_ids)
748 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
749 expand_strv (&tweaks->removals, desktop_file_ids, tweaks->additions);
753 g_strfreev (mime_types);
756 mime_types = g_key_file_get_keys (key_file, default_group, NULL, NULL);
758 if (mime_types != NULL)
760 for (i = 0; mime_types[i] != NULL; i++)
762 desktop_file_ids = g_key_file_get_string_list (key_file, default_group, mime_types[i], NULL, NULL);
764 if (desktop_file_ids)
766 tweaks = desktop_file_dir_unindexed_get_tweaks (dir, mime_types[i]);
767 expand_strv (&tweaks->defaults, desktop_file_ids, NULL);
771 g_strfreev (mime_types);
774 g_key_file_free (key_file);
778 desktop_file_dir_unindexed_read_mimeapps_lists (DesktopFileDir *dir)
780 const gchar * const *desktops;
784 dir->mime_tweaks = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, free_mime_tweaks);
786 /* We process in order of precedence, using a blacklisting approach to
787 * avoid recording later instructions that conflict with ones we found
790 * We first start with the XDG_CURRENT_DESKTOP files, in precedence
793 desktops = get_lowercase_current_desktops ();
794 for (i = 0; desktops[i]; i++)
796 filename = g_strdup_printf ("%s/%s-mimeapps.list", dir->path, desktops[i]);
797 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, "Added Associations", FALSE);
801 /* Next, the non-desktop-specific mimeapps.list */
802 filename = g_strdup_printf ("%s/mimeapps.list", dir->path);
803 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, "Added Associations", TRUE);
806 /* The remaining files are only checked for in directories that might
807 * contain desktop files (ie: not the config dirs).
812 /* We have 'defaults.list' which was only ever understood by GLib. It
813 * exists widely, but it has never been part of any spec and it should
814 * be treated as deprecated. This will be removed in a future
817 filename = g_strdup_printf ("%s/defaults.list", dir->path);
818 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, "Added Associations", FALSE);
821 /* Finally, the mimeinfo.cache, which is just a cached copy of what we
822 * would find in the MimeTypes= lines of all of the desktop files.
824 filename = g_strdup_printf ("%s/mimeinfo.cache", dir->path);
825 desktop_file_dir_unindexed_read_mimeapps_list (dir, filename, "MIME Cache", TRUE);
830 desktop_file_dir_unindexed_init (DesktopFileDir *dir)
833 get_apps_from_dir (&dir->app_names, dir->path, "");
835 desktop_file_dir_unindexed_read_mimeapps_lists (dir);
838 static GDesktopAppInfo *
839 desktop_file_dir_unindexed_get_app (DesktopFileDir *dir,
840 const gchar *desktop_id)
842 const gchar *filename;
844 filename = g_hash_table_lookup (dir->app_names, desktop_id);
849 return g_desktop_app_info_new_from_filename (filename);
853 desktop_file_dir_unindexed_get_all (DesktopFileDir *dir,
860 if (dir->app_names == NULL)
863 g_hash_table_iter_init (&iter, dir->app_names);
864 while (g_hash_table_iter_next (&iter, &app_name, &filename))
866 if (desktop_file_dir_app_name_is_masked (dir, app_name))
869 add_to_table_if_appropriate (apps, app_name, g_desktop_app_info_new_from_filename (filename));
873 typedef struct _MemoryIndexEntry MemoryIndexEntry;
874 typedef GHashTable MemoryIndex;
876 struct _MemoryIndexEntry
878 const gchar *app_name; /* pointer to the hashtable key */
880 MemoryIndexEntry *next;
884 memory_index_entry_free (gpointer data)
886 MemoryIndexEntry *mie = data;
890 MemoryIndexEntry *next = mie->next;
892 g_slice_free (MemoryIndexEntry, mie);
898 memory_index_add_token (MemoryIndex *mi,
901 const gchar *app_name)
903 MemoryIndexEntry *mie, *first;
905 mie = g_slice_new (MemoryIndexEntry);
906 mie->app_name = app_name;
907 mie->match_category = match_category;
909 first = g_hash_table_lookup (mi, token);
913 mie->next = first->next;
919 g_hash_table_insert (mi, g_strdup (token), mie);
924 memory_index_add_string (MemoryIndex *mi,
927 const gchar *app_name)
929 gchar **tokens, **alternates;
932 tokens = g_str_tokenize_and_fold (string, NULL, &alternates);
934 for (i = 0; tokens[i]; i++)
935 memory_index_add_token (mi, tokens[i], match_category, app_name);
937 for (i = 0; alternates[i]; i++)
938 memory_index_add_token (mi, alternates[i], match_category, app_name);
940 g_strfreev (alternates);
945 memory_index_new (void)
947 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free, memory_index_entry_free);
951 desktop_file_dir_unindexed_setup_search (DesktopFileDir *dir)
956 dir->memory_index = memory_index_new ();
958 /* Nothing to search? */
959 if (dir->app_names == NULL)
962 g_hash_table_iter_init (&iter, dir->app_names);
963 while (g_hash_table_iter_next (&iter, &app, &path))
967 if (desktop_file_dir_app_name_is_masked (dir, app))
970 key_file = g_key_file_new ();
972 if (g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL) &&
973 !g_key_file_get_boolean (key_file, "Desktop Entry", "Hidden", NULL))
975 /* Index the interesting keys... */
978 for (i = 0; i < G_N_ELEMENTS (desktop_key_match_category); i++)
983 if (!desktop_key_match_category[i])
986 raw = g_key_file_get_locale_string (key_file, "Desktop Entry", desktop_key_get_name (i), NULL, NULL);
989 if (i == DESKTOP_KEY_Exec && raw != NULL)
991 /* Special handling: only match basename of first field */
995 /* Remove extra arguments, if any */
996 space = raw + strcspn (raw, " \t\n"); /* IFS */
999 /* Skip the pathname, if any */
1000 if ((slash = strrchr (raw, '/')))
1005 memory_index_add_string (dir->memory_index, value, desktop_key_match_category[i], app);
1011 g_key_file_free (key_file);
1016 desktop_file_dir_unindexed_search (DesktopFileDir *dir,
1017 const gchar *search_token)
1019 GHashTableIter iter;
1020 gpointer key, value;
1022 if (!dir->memory_index)
1023 desktop_file_dir_unindexed_setup_search (dir);
1025 g_hash_table_iter_init (&iter, dir->memory_index);
1026 while (g_hash_table_iter_next (&iter, &key, &value))
1028 MemoryIndexEntry *mie = value;
1030 if (!g_str_has_prefix (key, search_token))
1035 add_token_result (mie->app_name, mie->match_category);
1042 array_contains (GPtrArray *array,
1047 for (i = 0; i < array->len; i++)
1048 if (g_str_equal (array->pdata[i], str))
1055 desktop_file_dir_unindexed_mime_lookup (DesktopFileDir *dir,
1056 const gchar *mime_type,
1058 GPtrArray *blacklist)
1060 UnindexedMimeTweaks *tweaks;
1063 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1068 if (tweaks->additions)
1070 for (i = 0; tweaks->additions[i]; i++)
1072 gchar *app_name = tweaks->additions[i];
1074 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1075 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1076 g_ptr_array_add (hits, g_strdup (app_name));
1080 if (tweaks->removals)
1082 for (i = 0; tweaks->removals[i]; i++)
1084 gchar *app_name = tweaks->removals[i];
1086 if (!desktop_file_dir_app_name_is_masked (dir, app_name) &&
1087 !array_contains (blacklist, app_name) && !array_contains (hits, app_name))
1088 g_ptr_array_add (blacklist, app_name);
1094 desktop_file_dir_unindexed_default_lookup (DesktopFileDir *dir,
1095 const gchar *mime_type,
1098 UnindexedMimeTweaks *tweaks;
1101 tweaks = g_hash_table_lookup (dir->mime_tweaks, mime_type);
1103 if (!tweaks || !tweaks->defaults)
1106 for (i = 0; tweaks->defaults[i]; i++)
1108 gchar *app_name = tweaks->defaults[i];
1110 if (!array_contains (results, app_name))
1111 g_ptr_array_add (results, g_strdup (app_name));
1115 /* DesktopFileDir "API" {{{2 */
1118 * desktop_file_dir_create:
1119 * @array: the #GArray to add a new item to
1120 * @data_dir: an XDG_DATA_DIR
1122 * Creates a #DesktopFileDir for the corresponding @data_dir, adding it
1126 desktop_file_dir_create (GArray *array,
1127 const gchar *data_dir)
1129 DesktopFileDir dir = { 0, };
1131 dir.path = g_build_filename (data_dir, "applications", NULL);
1133 g_array_append_val (array, dir);
1137 * desktop_file_dir_create:
1138 * @array: the #GArray to add a new item to
1139 * @config_dir: an XDG_CONFIG_DIR
1141 * Just the same as desktop_file_dir_create() except that it does not
1142 * add the "applications" directory. It also marks the directory as
1143 * config-only, which prevents us from attempting to find desktop files
1147 desktop_file_dir_create_for_config (GArray *array,
1148 const gchar *config_dir)
1150 DesktopFileDir dir = { 0, };
1152 dir.path = g_strdup (config_dir);
1153 dir.is_config = TRUE;
1155 g_array_append_val (array, dir);
1159 * desktop_file_dir_reset:
1160 * @dir: a #DesktopFileDir
1162 * Cleans up @dir, releasing most resources that it was using.
1165 desktop_file_dir_reset (DesktopFileDir *dir)
1169 g_signal_handlers_disconnect_by_func (dir->monitor, desktop_file_dir_changed, dir);
1170 g_object_unref (dir->monitor);
1171 dir->monitor = NULL;
1176 g_hash_table_unref (dir->app_names);
1177 dir->app_names = NULL;
1180 if (dir->memory_index)
1182 g_hash_table_unref (dir->memory_index);
1183 dir->memory_index = NULL;
1186 if (dir->mime_tweaks)
1188 g_hash_table_unref (dir->mime_tweaks);
1189 dir->mime_tweaks = NULL;
1192 dir->is_setup = FALSE;
1196 * desktop_file_dir_init:
1197 * @dir: a #DesktopFileDir
1199 * Does initial setup for @dir
1201 * You should only call this if @dir is not already setup.
1204 desktop_file_dir_init (DesktopFileDir *dir)
1206 g_assert (!dir->is_setup);
1208 g_assert (!dir->monitor);
1209 dir->monitor = g_local_directory_monitor_new_in_worker (dir->path, G_FILE_MONITOR_NONE, NULL);
1213 g_signal_connect (dir->monitor, "changed", G_CALLBACK (desktop_file_dir_changed), dir);
1214 g_local_directory_monitor_start (dir->monitor);
1217 desktop_file_dir_unindexed_init (dir);
1219 dir->is_setup = TRUE;
1223 * desktop_file_dir_get_app:
1224 * @dir: a DesktopFileDir
1225 * @desktop_id: the desktop ID to load
1227 * Creates the #GDesktopAppInfo for the given @desktop_id if it exists
1228 * within @dir, even if it is hidden.
1230 * This function does not check if @desktop_id would be masked by a
1231 * directory with higher precedence. The caller must do so.
1233 static GDesktopAppInfo *
1234 desktop_file_dir_get_app (DesktopFileDir *dir,
1235 const gchar *desktop_id)
1237 if (!dir->app_names)
1240 return desktop_file_dir_unindexed_get_app (dir, desktop_id);
1244 * desktop_file_dir_get_all:
1245 * @dir: a DesktopFileDir
1246 * @apps: a #GHashTable<string, GDesktopAppInfo>
1248 * Loads all desktop files in @dir and adds them to @apps, careful to
1249 * ensure we don't add any files masked by a similarly-named file in a
1250 * higher-precedence directory.
1253 desktop_file_dir_get_all (DesktopFileDir *dir,
1256 desktop_file_dir_unindexed_get_all (dir, apps);
1260 * desktop_file_dir_mime_lookup:
1261 * @dir: a #DesktopFileDir
1262 * @mime_type: the mime type to look up
1263 * @hits: the array to store the hits
1264 * @blacklist: the array to store the blacklist
1266 * Does a lookup of a mimetype against one desktop file directory,
1267 * recording any hits and blacklisting and "Removed" associations (so
1268 * later directories don't record them as hits).
1270 * The items added to @hits are duplicated, but the ones in @blacklist
1271 * are weak pointers. This facilitates simply freeing the blacklist
1272 * (which is only used for internal bookkeeping) but using the pdata of
1273 * @hits as the result of the operation.
1276 desktop_file_dir_mime_lookup (DesktopFileDir *dir,
1277 const gchar *mime_type,
1279 GPtrArray *blacklist)
1281 desktop_file_dir_unindexed_mime_lookup (dir, mime_type, hits, blacklist);
1285 * desktop_file_dir_default_lookup:
1286 * @dir: a #DesktopFileDir
1287 * @mime_type: the mime type to look up
1288 * @results: an array to store the results in
1290 * Collects the "default" applications for a given mime type from @dir.
1293 desktop_file_dir_default_lookup (DesktopFileDir *dir,
1294 const gchar *mime_type,
1297 desktop_file_dir_unindexed_default_lookup (dir, mime_type, results);
1301 * desktop_file_dir_search:
1302 * @dir: a #DesktopFileDir
1303 * @term: a normalised and casefolded search term
1305 * Finds the names of applications in @dir that match @term.
1308 desktop_file_dir_search (DesktopFileDir *dir,
1309 const gchar *search_token)
1311 desktop_file_dir_unindexed_search (dir, search_token);
1314 /* Lock/unlock and global setup API {{{2 */
1317 desktop_file_dirs_lock (void)
1321 g_mutex_lock (&desktop_file_dir_lock);
1323 if (desktop_file_dirs == NULL)
1325 const char * const *dirs;
1329 tmp = g_array_new (FALSE, FALSE, sizeof (DesktopFileDir));
1331 /* First, the configs. Highest priority: the user's ~/.config */
1332 desktop_file_dir_create_for_config (tmp, g_get_user_config_dir ());
1334 /* Next, the system configs (/etc/xdg, and so on). */
1335 dirs = g_get_system_config_dirs ();
1336 for (i = 0; dirs[i]; i++)
1337 desktop_file_dir_create_for_config (tmp, dirs[i]);
1339 /* Now the data. Highest priority: the user's ~/.local/share/applications */
1340 desktop_file_dir_user_data_index = tmp->len;
1341 desktop_file_dir_create (tmp, g_get_user_data_dir ());
1343 /* Following that, XDG_DATA_DIRS/applications, in order */
1344 dirs = g_get_system_data_dirs ();
1345 for (i = 0; dirs[i]; i++)
1346 desktop_file_dir_create (tmp, dirs[i]);
1348 /* The list of directories will never change after this. */
1349 desktop_file_dirs = (DesktopFileDir *) tmp->data;
1350 n_desktop_file_dirs = tmp->len;
1352 g_array_free (tmp, FALSE);
1355 for (i = 0; i < n_desktop_file_dirs; i++)
1356 if (!desktop_file_dirs[i].is_setup)
1357 desktop_file_dir_init (&desktop_file_dirs[i]);
1361 desktop_file_dirs_unlock (void)
1363 g_mutex_unlock (&desktop_file_dir_lock);
1367 desktop_file_dirs_invalidate_user_config (void)
1369 g_mutex_lock (&desktop_file_dir_lock);
1371 if (n_desktop_file_dirs)
1372 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_config_index]);
1374 g_mutex_unlock (&desktop_file_dir_lock);
1378 desktop_file_dirs_invalidate_user_data (void)
1380 g_mutex_lock (&desktop_file_dir_lock);
1382 if (n_desktop_file_dirs)
1383 desktop_file_dir_reset (&desktop_file_dirs[desktop_file_dir_user_data_index]);
1385 g_mutex_unlock (&desktop_file_dir_lock);
1388 /* GDesktopAppInfo implementation {{{1 */
1389 /* GObject implementation {{{2 */
1391 g_desktop_app_info_finalize (GObject *object)
1393 GDesktopAppInfo *info;
1395 info = G_DESKTOP_APP_INFO (object);
1397 g_free (info->desktop_id);
1398 g_free (info->filename);
1401 g_key_file_unref (info->keyfile);
1403 g_free (info->name);
1404 g_free (info->generic_name);
1405 g_free (info->fullname);
1406 g_free (info->comment);
1407 g_free (info->icon_name);
1409 g_object_unref (info->icon);
1410 g_strfreev (info->keywords);
1411 g_strfreev (info->only_show_in);
1412 g_strfreev (info->not_show_in);
1413 g_free (info->try_exec);
1414 g_free (info->exec);
1415 g_free (info->binary);
1416 g_free (info->path);
1417 g_free (info->categories);
1418 g_free (info->startup_wm_class);
1419 g_strfreev (info->mime_types);
1420 g_free (info->app_id);
1421 g_strfreev (info->actions);
1423 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
1427 g_desktop_app_info_set_property (GObject *object,
1429 const GValue *value,
1432 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1437 self->filename = g_value_dup_string (value);
1441 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1447 g_desktop_app_info_get_property (GObject *object,
1452 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
1457 g_value_set_string (value, self->filename);
1460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1466 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
1468 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1470 gobject_class->get_property = g_desktop_app_info_get_property;
1471 gobject_class->set_property = g_desktop_app_info_set_property;
1472 gobject_class->finalize = g_desktop_app_info_finalize;
1475 * GDesktopAppInfo:filename:
1477 * The origin filename of this #GDesktopAppInfo
1479 g_object_class_install_property (gobject_class,
1481 g_param_spec_string ("filename", "Filename", "", NULL,
1482 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1486 g_desktop_app_info_init (GDesktopAppInfo *local)
1490 /* Construction... {{{2 */
1494 * @exec: an exec line
1496 * Returns the first word in an exec line (ie: the binary name).
1498 * If @exec is " progname --foo %F" then returns "progname".
1501 binary_from_exec (const char *exec)
1503 const char *p, *start;
1509 while (*p != ' ' && *p != 0)
1512 return g_strndup (start, p - start);
1516 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
1523 gboolean bus_activatable;
1525 start_group = g_key_file_get_start_group (key_file);
1526 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
1528 g_free (start_group);
1531 g_free (start_group);
1533 type = g_key_file_get_string (key_file,
1534 G_KEY_FILE_DESKTOP_GROUP,
1535 G_KEY_FILE_DESKTOP_KEY_TYPE,
1537 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
1544 try_exec = g_key_file_get_string (key_file,
1545 G_KEY_FILE_DESKTOP_GROUP,
1546 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
1548 if (try_exec && try_exec[0] != '\0')
1551 t = g_find_program_in_path (try_exec);
1560 exec = g_key_file_get_string (key_file,
1561 G_KEY_FILE_DESKTOP_GROUP,
1562 G_KEY_FILE_DESKTOP_KEY_EXEC,
1564 if (exec && exec[0] != '\0')
1568 if (!g_shell_parse_argv (exec, &argc, &argv, NULL))
1577 t = g_find_program_in_path (argv[0]);
1590 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
1591 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
1592 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
1593 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
1594 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
1595 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
1596 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
1597 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);
1598 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);
1599 info->try_exec = try_exec;
1601 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
1602 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
1603 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
1604 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
1605 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
1606 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
1607 info->startup_wm_class = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, STARTUP_WM_CLASS_KEY, NULL);
1608 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);
1609 bus_activatable = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE, NULL);
1610 info->actions = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ACTIONS, NULL, NULL);
1612 /* Remove the special-case: no Actions= key just means 0 extra actions */
1613 if (info->actions == NULL)
1614 info->actions = g_new0 (gchar *, 0 + 1);
1617 if (info->icon_name)
1619 if (g_path_is_absolute (info->icon_name))
1623 file = g_file_new_for_path (info->icon_name);
1624 info->icon = g_file_icon_new (file);
1625 g_object_unref (file);
1631 /* Work around a common mistake in desktop files */
1632 if ((p = strrchr (info->icon_name, '.')) != NULL &&
1633 (strcmp (p, ".png") == 0 ||
1634 strcmp (p, ".xpm") == 0 ||
1635 strcmp (p, ".svg") == 0))
1638 info->icon = g_themed_icon_new (info->icon_name);
1643 info->binary = binary_from_exec (info->exec);
1645 if (info->path && info->path[0] == '\0')
1647 g_free (info->path);
1651 /* Can only be DBusActivatable if we know the filename, which means
1652 * that this won't work for the load-from-keyfile case.
1654 if (bus_activatable && info->filename)
1659 basename = g_path_get_basename (info->filename);
1660 last_dot = strrchr (basename, '.');
1662 if (last_dot && g_str_equal (last_dot, ".desktop"))
1666 if (g_dbus_is_interface_name (basename))
1667 info->app_id = g_strdup (basename);
1673 info->keyfile = g_key_file_ref (key_file);
1679 g_desktop_app_info_load_file (GDesktopAppInfo *self)
1682 gboolean retval = FALSE;
1684 g_return_val_if_fail (self->filename != NULL, FALSE);
1686 self->desktop_id = g_path_get_basename (self->filename);
1688 key_file = g_key_file_new ();
1690 if (g_key_file_load_from_file (key_file, self->filename, G_KEY_FILE_NONE, NULL))
1691 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
1693 g_key_file_unref (key_file);
1698 * g_desktop_app_info_new_from_keyfile:
1699 * @key_file: an opened #GKeyFile
1701 * Creates a new #GDesktopAppInfo.
1703 * Returns: a new #GDesktopAppInfo or %NULL on error.
1708 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
1710 GDesktopAppInfo *info;
1712 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1713 info->filename = NULL;
1714 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
1716 g_object_unref (info);
1723 * g_desktop_app_info_new_from_filename:
1724 * @filename: the path of a desktop file, in the GLib filename encoding
1726 * Creates a new #GDesktopAppInfo.
1728 * Returns: a new #GDesktopAppInfo or %NULL on error.
1731 g_desktop_app_info_new_from_filename (const char *filename)
1733 GDesktopAppInfo *info = NULL;
1735 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
1736 if (!g_desktop_app_info_load_file (info))
1738 g_object_unref (info);
1745 * g_desktop_app_info_new:
1746 * @desktop_id: the desktop file id
1748 * Creates a new #GDesktopAppInfo based on a desktop file id.
1750 * A desktop file id is the basename of the desktop file, including the
1751 * .desktop extension. GIO is looking for a desktop file with this name
1752 * in the `applications` subdirectories of the XDG
1753 * data directories (i.e. the directories specified in the `XDG_DATA_HOME`
1754 * and `XDG_DATA_DIRS` environment variables). GIO also supports the
1755 * prefix-to-subdirectory mapping that is described in the
1756 * [Menu Spec](http://standards.freedesktop.org/menu-spec/latest/)
1757 * (i.e. a desktop id of kde-foo.desktop will match
1758 * `/usr/share/applications/kde/foo.desktop`).
1760 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
1763 g_desktop_app_info_new (const char *desktop_id)
1765 GDesktopAppInfo *appinfo = NULL;
1768 desktop_file_dirs_lock ();
1770 for (i = 0; i < n_desktop_file_dirs; i++)
1772 appinfo = desktop_file_dir_get_app (&desktop_file_dirs[i], desktop_id);
1778 desktop_file_dirs_unlock ();
1780 if (appinfo == NULL)
1783 g_free (appinfo->desktop_id);
1784 appinfo->desktop_id = g_strdup (desktop_id);
1786 if (g_desktop_app_info_get_is_hidden (appinfo))
1788 g_object_unref (appinfo);
1796 g_desktop_app_info_dup (GAppInfo *appinfo)
1798 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1799 GDesktopAppInfo *new_info;
1801 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1803 new_info->filename = g_strdup (info->filename);
1804 new_info->desktop_id = g_strdup (info->desktop_id);
1807 new_info->keyfile = g_key_file_ref (info->keyfile);
1809 new_info->name = g_strdup (info->name);
1810 new_info->generic_name = g_strdup (info->generic_name);
1811 new_info->fullname = g_strdup (info->fullname);
1812 new_info->keywords = g_strdupv (info->keywords);
1813 new_info->comment = g_strdup (info->comment);
1814 new_info->nodisplay = info->nodisplay;
1815 new_info->icon_name = g_strdup (info->icon_name);
1817 new_info->icon = g_object_ref (info->icon);
1818 new_info->only_show_in = g_strdupv (info->only_show_in);
1819 new_info->not_show_in = g_strdupv (info->not_show_in);
1820 new_info->try_exec = g_strdup (info->try_exec);
1821 new_info->exec = g_strdup (info->exec);
1822 new_info->binary = g_strdup (info->binary);
1823 new_info->path = g_strdup (info->path);
1824 new_info->app_id = g_strdup (info->app_id);
1825 new_info->hidden = info->hidden;
1826 new_info->terminal = info->terminal;
1827 new_info->startup_notify = info->startup_notify;
1829 return G_APP_INFO (new_info);
1832 /* GAppInfo interface implementation functions {{{2 */
1835 g_desktop_app_info_equal (GAppInfo *appinfo1,
1838 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
1839 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
1841 if (info1->desktop_id == NULL ||
1842 info2->desktop_id == NULL)
1843 return info1 == info2;
1845 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
1849 g_desktop_app_info_get_id (GAppInfo *appinfo)
1851 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1853 return info->desktop_id;
1857 g_desktop_app_info_get_name (GAppInfo *appinfo)
1859 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1861 if (info->name == NULL)
1862 return _("Unnamed");
1867 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
1869 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1871 if (info->fullname == NULL)
1872 return g_desktop_app_info_get_name (appinfo);
1873 return info->fullname;
1877 * g_desktop_app_info_get_is_hidden:
1878 * @info: a #GDesktopAppInfo.
1880 * A desktop file is hidden if the Hidden key in it is
1883 * Returns: %TRUE if hidden, %FALSE otherwise.
1886 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
1888 return info->hidden;
1892 * g_desktop_app_info_get_filename:
1893 * @info: a #GDesktopAppInfo
1895 * When @info was created from a known filename, return it. In some
1896 * situations such as the #GDesktopAppInfo returned from
1897 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
1899 * Returns: The full path to the file for @info, or %NULL if not known.
1903 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
1905 return info->filename;
1909 g_desktop_app_info_get_description (GAppInfo *appinfo)
1911 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1913 return info->comment;
1917 g_desktop_app_info_get_executable (GAppInfo *appinfo)
1919 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1921 return info->binary;
1925 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
1927 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1933 g_desktop_app_info_get_icon (GAppInfo *appinfo)
1935 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1941 * g_desktop_app_info_get_categories:
1942 * @info: a #GDesktopAppInfo
1944 * Gets the categories from the desktop file.
1946 * Returns: The unparsed Categories key from the desktop file;
1947 * i.e. no attempt is made to split it by ';' or validate it.
1950 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
1952 return info->categories;
1956 * g_desktop_app_info_get_keywords:
1957 * @info: a #GDesktopAppInfo
1959 * Gets the keywords from the desktop file.
1961 * Returns: (transfer none): The value of the Keywords key
1965 const char * const *
1966 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
1968 return (const char * const *)info->keywords;
1972 * g_desktop_app_info_get_generic_name:
1973 * @info: a #GDesktopAppInfo
1975 * Gets the generic name from the destkop file.
1977 * Returns: The value of the GenericName key
1980 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
1982 return info->generic_name;
1986 * g_desktop_app_info_get_nodisplay:
1987 * @info: a #GDesktopAppInfo
1989 * Gets the value of the NoDisplay key, which helps determine if the
1990 * application info should be shown in menus. See
1991 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
1993 * Returns: The value of the NoDisplay key
1998 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
2000 return info->nodisplay;
2004 * g_desktop_app_info_get_show_in:
2005 * @info: a #GDesktopAppInfo
2006 * @desktop_env: a string specifying a desktop name
2008 * Checks if the application info should be shown in menus that list available
2009 * applications for a specific name of the desktop, based on the
2010 * `OnlyShowIn` and `NotShowIn` keys.
2012 * If @desktop_env is %NULL, then the name of the desktop set with
2013 * g_desktop_app_info_set_desktop_env() is used.
2015 * Note that g_app_info_should_show() for @info will include this check (with
2016 * %NULL for @desktop_env) as well as additional checks.
2018 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
2019 * `OnlyShowIn` and `NotShowIn` keys, %FALSE
2025 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
2026 const gchar *desktop_env)
2031 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
2034 G_LOCK (g_desktop_env);
2035 desktop_env = g_desktop_env;
2036 G_UNLOCK (g_desktop_env);
2039 if (info->only_show_in)
2041 if (desktop_env == NULL)
2045 for (i = 0; info->only_show_in[i] != NULL; i++)
2047 if (strcmp (info->only_show_in[i], desktop_env) == 0)
2057 if (info->not_show_in && desktop_env)
2059 for (i = 0; info->not_show_in[i] != NULL; i++)
2061 if (strcmp (info->not_show_in[i], desktop_env) == 0)
2069 /* Launching... {{{2 */
2072 expand_macro_single (char macro, char *uri)
2075 char *result = NULL;
2079 file = g_file_new_for_uri (uri);
2085 result = g_shell_quote (uri);
2089 path = g_file_get_path (file);
2091 result = g_shell_quote (path);
2095 path = g_file_get_path (file);
2098 name = g_path_get_dirname (path);
2099 result = g_shell_quote (name);
2105 path = g_file_get_path (file);
2108 name = g_path_get_basename (path);
2109 result = g_shell_quote (name);
2115 g_object_unref (file);
2122 expand_macro (char macro,
2124 GDesktopAppInfo *info,
2127 GList *uris = *uri_list;
2129 gboolean force_file_uri;
2130 char force_file_uri_macro;
2133 g_return_if_fail (exec != NULL);
2135 /* On %u and %U, pass POSIX file path pointing to the URI via
2136 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
2137 * running or the URI doesn't have a POSIX file path via FUSE
2138 * we'll just pass the URI.
2140 force_file_uri_macro = macro;
2141 force_file_uri = FALSE;
2147 force_file_uri_macro = 'f';
2148 force_file_uri = TRUE;
2151 force_file_uri_macro = 'F';
2152 force_file_uri = TRUE;
2168 if (!force_file_uri ||
2169 /* Pass URI if it contains an anchor */
2170 strchr (uri, '#') != NULL)
2172 expanded = expand_macro_single (macro, uri);
2176 expanded = expand_macro_single (force_file_uri_macro, uri);
2177 if (expanded == NULL)
2178 expanded = expand_macro_single (macro, uri);
2183 g_string_append (exec, expanded);
2199 if (!force_file_uri ||
2200 /* Pass URI if it contains an anchor */
2201 strchr (uri, '#') != NULL)
2203 expanded = expand_macro_single (macro, uri);
2207 expanded = expand_macro_single (force_file_uri_macro, uri);
2208 if (expanded == NULL)
2209 expanded = expand_macro_single (macro, uri);
2214 g_string_append (exec, expanded);
2220 if (uris != NULL && expanded)
2221 g_string_append_c (exec, ' ');
2227 if (info->icon_name)
2229 g_string_append (exec, "--icon ");
2230 expanded = g_shell_quote (info->icon_name);
2231 g_string_append (exec, expanded);
2239 expanded = g_shell_quote (info->name);
2240 g_string_append (exec, expanded);
2248 expanded = g_shell_quote (info->filename);
2249 g_string_append (exec, expanded);
2254 case 'm': /* deprecated */
2258 g_string_append_c (exec, '%');
2266 expand_application_parameters (GDesktopAppInfo *info,
2267 const gchar *exec_line,
2273 GList *uri_list = *uris;
2274 const char *p = exec_line;
2275 GString *expanded_exec;
2278 if (exec_line == NULL)
2280 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2281 _("Desktop file didn't specify Exec field"));
2285 expanded_exec = g_string_new (NULL);
2289 if (p[0] == '%' && p[1] != '\0')
2291 expand_macro (p[1], expanded_exec, info, uris);
2295 g_string_append_c (expanded_exec, *p);
2300 /* No file substitutions */
2301 if (uri_list == *uris && uri_list != NULL)
2303 /* If there is no macro default to %f. This is also what KDE does */
2304 g_string_append_c (expanded_exec, ' ');
2305 expand_macro ('f', expanded_exec, info, uris);
2308 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
2309 g_string_free (expanded_exec, TRUE);
2314 prepend_terminal_to_vector (int *argc,
2321 char **term_argv = NULL;
2326 g_return_val_if_fail (argc != NULL, FALSE);
2327 g_return_val_if_fail (argv != NULL, FALSE);
2335 /* compute size if not given */
2338 for (i = 0; the_argv[i] != NULL; i++)
2344 term_argv = g_new0 (char *, 3);
2346 check = g_find_program_in_path ("gnome-terminal");
2349 term_argv[0] = check;
2350 /* Note that gnome-terminal takes -x and
2351 * as -e in gnome-terminal is broken we use that. */
2352 term_argv[1] = g_strdup ("-x");
2357 check = g_find_program_in_path ("nxterm");
2359 check = g_find_program_in_path ("color-xterm");
2361 check = g_find_program_in_path ("rxvt");
2363 check = g_find_program_in_path ("xterm");
2365 check = g_find_program_in_path ("dtterm");
2368 check = g_strdup ("xterm");
2369 g_warning ("couldn't find a terminal, falling back to xterm");
2371 term_argv[0] = check;
2372 term_argv[1] = g_strdup ("-e");
2375 real_argc = term_argc + *argc;
2376 real_argv = g_new (char *, real_argc + 1);
2378 for (i = 0; i < term_argc; i++)
2379 real_argv[i] = term_argv[i];
2381 for (j = 0; j < *argc; j++, i++)
2382 real_argv[i] = (char *)the_argv[j];
2384 real_argv[i] = NULL;
2390 /* we use g_free here as we sucked all the inner strings
2391 * out from it into real_argv */
2396 #endif /* G_OS_WIN32 */
2400 create_files_for_uris (GList *uris)
2407 for (iter = uris; iter; iter = iter->next)
2409 GFile *file = g_file_new_for_uri ((char *)iter->data);
2410 res = g_list_prepend (res, file);
2413 return g_list_reverse (res);
2418 GSpawnChildSetupFunc user_setup;
2419 gpointer user_setup_data;
2425 child_setup (gpointer user_data)
2427 ChildSetupData *data = user_data;
2429 if (data->pid_envvar)
2431 pid_t pid = getpid ();
2435 /* Write the pid into the space already reserved for it in the
2436 * environment array. We can't use sprintf because it might
2437 * malloc, so we do it by hand. It's simplest to write the pid
2438 * out backwards first, then copy it over.
2440 for (i = 0; pid; i++, pid /= 10)
2441 buf[i] = (pid % 10) + '0';
2442 for (i--; i >= 0; i--)
2443 *(data->pid_envvar++) = buf[i];
2444 *data->pid_envvar = '\0';
2447 if (data->user_setup)
2448 data->user_setup (data->user_setup_data);
2452 notify_desktop_launch (GDBusConnection *session_bus,
2453 GDesktopAppInfo *info,
2455 const char *display,
2460 GVariantBuilder uri_variant;
2461 GVariantBuilder extras_variant;
2463 const char *desktop_file_id;
2464 const char *gio_desktop_file;
2466 if (session_bus == NULL)
2469 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
2470 for (iter = uris; iter; iter = iter->next)
2471 g_variant_builder_add (&uri_variant, "s", iter->data);
2473 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
2474 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
2475 g_variant_builder_add (&extras_variant, "{sv}",
2479 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
2480 if (gio_desktop_file != NULL)
2481 g_variant_builder_add (&extras_variant, "{sv}",
2482 "origin-desktop-file",
2483 g_variant_new_bytestring (gio_desktop_file));
2484 if (g_get_prgname () != NULL)
2485 g_variant_builder_add (&extras_variant, "{sv}",
2487 g_variant_new_bytestring (g_get_prgname ()));
2488 g_variant_builder_add (&extras_variant, "{sv}",
2491 (gint64)getpid ()));
2494 desktop_file_id = info->filename;
2495 else if (info->desktop_id)
2496 desktop_file_id = info->desktop_id;
2498 desktop_file_id = "";
2500 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
2501 "org.gtk.gio.DesktopAppInfo",
2503 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
2504 g_variant_new_bytestring (desktop_file_id),
2505 display ? display : "",
2509 g_dbus_connection_send_message (session_bus,
2513 g_object_unref (msg);
2516 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
2519 g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
2520 GDBusConnection *session_bus,
2521 const gchar *exec_line,
2523 GAppLaunchContext *launch_context,
2524 GSpawnFlags spawn_flags,
2525 GSpawnChildSetupFunc user_setup,
2526 gpointer user_setup_data,
2527 GDesktopAppLaunchCallback pid_callback,
2528 gpointer pid_callback_data,
2531 gboolean completed = FALSE;
2533 char **argv, **envp;
2535 ChildSetupData data;
2537 g_return_val_if_fail (info != NULL, FALSE);
2542 envp = g_app_launch_context_get_environment (launch_context);
2544 envp = g_get_environ ();
2549 GList *launched_uris;
2551 char *display, *sn_id = NULL;
2554 if (!expand_application_parameters (info, exec_line, &uris, &argc, &argv, error))
2557 /* Get the subset of URIs we're launching with this process */
2558 launched_uris = NULL;
2559 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
2560 launched_uris = g_list_prepend (launched_uris, iter->data);
2561 launched_uris = g_list_reverse (launched_uris);
2563 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
2565 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2566 _("Unable to find terminal required for application"));
2570 data.user_setup = user_setup;
2571 data.user_setup_data = user_setup_data;
2575 envp = g_environ_setenv (envp,
2576 "GIO_LAUNCHED_DESKTOP_FILE",
2579 envp = g_environ_setenv (envp,
2580 "GIO_LAUNCHED_DESKTOP_FILE_PID",
2581 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
2583 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
2587 data.pid_envvar = NULL;
2594 GList *launched_files = create_files_for_uris (launched_uris);
2596 display = g_app_launch_context_get_display (launch_context,
2600 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
2602 if (info->startup_notify)
2604 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
2608 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
2611 g_list_free_full (launched_files, g_object_unref);
2614 if (!g_spawn_async (info->path,
2624 g_app_launch_context_launch_failed (launch_context, sn_id);
2628 g_list_free (launched_uris);
2633 if (pid_callback != NULL)
2634 pid_callback (info, pid, pid_callback_data);
2636 if (launch_context != NULL)
2638 GVariantBuilder builder;
2639 GVariant *platform_data;
2641 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
2642 g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 (pid));
2644 g_variant_builder_add (&builder, "{sv}", "startup-notification-id", g_variant_new_string (sn_id));
2645 platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
2646 g_signal_emit_by_name (launch_context, "launched", info, platform_data);
2647 g_variant_unref (platform_data);
2650 notify_desktop_launch (session_bus,
2659 g_list_free (launched_uris);
2664 while (uris != NULL);
2676 object_path_from_appid (const gchar *app_id)
2681 n = strlen (app_id);
2682 path = g_malloc (n + 2);
2686 for (i = 0; i < n; i++)
2687 if (app_id[i] != '.')
2688 path[i + 1] = app_id[i];
2698 g_desktop_app_info_make_platform_data (GDesktopAppInfo *info,
2700 GAppLaunchContext *launch_context)
2702 GVariantBuilder builder;
2704 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
2708 GList *launched_files = create_files_for_uris (uris);
2710 if (info->startup_notify)
2714 sn_id = g_app_launch_context_get_startup_notify_id (launch_context, G_APP_INFO (info), launched_files);
2716 g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_take_string (sn_id));
2719 g_list_free_full (launched_files, g_object_unref);
2722 return g_variant_builder_end (&builder);
2726 g_desktop_app_info_launch_uris_with_dbus (GDesktopAppInfo *info,
2727 GDBusConnection *session_bus,
2729 GAppLaunchContext *launch_context)
2731 GVariantBuilder builder;
2734 g_return_val_if_fail (info != NULL, FALSE);
2736 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
2742 g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
2743 for (iter = uris; iter; iter = iter->next)
2744 g_variant_builder_add (&builder, "s", iter->data);
2745 g_variant_builder_close (&builder);
2748 g_variant_builder_add_value (&builder, g_desktop_app_info_make_platform_data (info, uris, launch_context));
2750 /* This is non-blocking API. Similar to launching via fork()/exec()
2751 * we don't wait around to see if the program crashed during startup.
2752 * This is what startup-notification's job is...
2754 object_path = object_path_from_appid (info->app_id);
2755 g_dbus_connection_call (session_bus, info->app_id, object_path, "org.freedesktop.Application",
2756 uris ? "Open" : "Activate", g_variant_builder_end (&builder),
2757 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
2758 g_free (object_path);
2764 g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
2766 GAppLaunchContext *launch_context,
2767 GSpawnFlags spawn_flags,
2768 GSpawnChildSetupFunc user_setup,
2769 gpointer user_setup_data,
2770 GDesktopAppLaunchCallback pid_callback,
2771 gpointer pid_callback_data,
2774 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2775 GDBusConnection *session_bus;
2776 gboolean success = TRUE;
2778 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
2780 if (session_bus && info->app_id)
2781 g_desktop_app_info_launch_uris_with_dbus (info, session_bus, uris, launch_context);
2783 success = g_desktop_app_info_launch_uris_with_spawn (info, session_bus, info->exec, uris, launch_context,
2784 spawn_flags, user_setup, user_setup_data,
2785 pid_callback, pid_callback_data, error);
2787 if (session_bus != NULL)
2789 /* This asynchronous flush holds a reference until it completes,
2790 * which ensures that the following unref won't immediately kill
2791 * the connection if we were the initial owner.
2793 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
2794 g_object_unref (session_bus);
2801 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
2803 GAppLaunchContext *launch_context,
2806 return g_desktop_app_info_launch_uris_internal (appinfo, uris,
2808 _SPAWN_FLAGS_DEFAULT,
2809 NULL, NULL, NULL, NULL,
2814 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
2816 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2818 return info->exec &&
2819 ((strstr (info->exec, "%u") != NULL) ||
2820 (strstr (info->exec, "%U") != NULL));
2824 g_desktop_app_info_supports_files (GAppInfo *appinfo)
2826 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2828 return info->exec &&
2829 ((strstr (info->exec, "%f") != NULL) ||
2830 (strstr (info->exec, "%F") != NULL));
2834 g_desktop_app_info_launch (GAppInfo *appinfo,
2836 GAppLaunchContext *launch_context,
2846 uri = g_file_get_uri (files->data);
2847 uris = g_list_prepend (uris, uri);
2848 files = files->next;
2851 uris = g_list_reverse (uris);
2853 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
2855 g_list_free_full (uris, g_free);
2861 * g_desktop_app_info_launch_uris_as_manager:
2862 * @appinfo: a #GDesktopAppInfo
2863 * @uris: (element-type utf8): List of URIs
2864 * @launch_context: (allow-none): a #GAppLaunchContext
2865 * @spawn_flags: #GSpawnFlags, used for each process
2866 * @user_setup: (scope call) (allow-none): a #GSpawnChildSetupFunc, used once
2868 * @user_setup_data: (closure user_setup) (allow-none): User data for @user_setup
2869 * @pid_callback: (scope call) (allow-none): Callback for child processes
2870 * @pid_callback_data: (closure pid_callback) (allow-none): User data for @callback
2871 * @error: return location for a #GError, or %NULL
2873 * This function performs the equivalent of g_app_info_launch_uris(),
2874 * but is intended primarily for operating system components that
2875 * launch applications. Ordinary applications should use
2876 * g_app_info_launch_uris().
2878 * If the application is launched via traditional UNIX fork()/exec()
2879 * then @spawn_flags, @user_setup and @user_setup_data are used for the
2880 * call to g_spawn_async(). Additionally, @pid_callback (with
2881 * @pid_callback_data) will be called to inform about the PID of the
2884 * If application launching occurs via some other mechanism (eg: D-Bus
2885 * activation) then @spawn_flags, @user_setup, @user_setup_data,
2886 * @pid_callback and @pid_callback_data are ignored.
2888 * Returns: %TRUE on successful launch, %FALSE otherwise.
2891 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
2893 GAppLaunchContext *launch_context,
2894 GSpawnFlags spawn_flags,
2895 GSpawnChildSetupFunc user_setup,
2896 gpointer user_setup_data,
2897 GDesktopAppLaunchCallback pid_callback,
2898 gpointer pid_callback_data,
2901 return g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
2912 /* OnlyShowIn API support {{{2 */
2915 * g_desktop_app_info_set_desktop_env:
2916 * @desktop_env: a string specifying what desktop this is
2918 * Sets the name of the desktop that the application is running in.
2919 * This is used by g_app_info_should_show() and
2920 * g_desktop_app_info_get_show_in() to evaluate the
2921 * `OnlyShowIn` and `NotShowIn`
2922 * desktop entry fields.
2925 * [Desktop Menu specification](http://standards.freedesktop.org/menu-spec/latest/)
2926 * recognizes the following:
2935 * Should be called only once; subsequent calls are ignored.
2938 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
2940 G_LOCK (g_desktop_env);
2942 g_desktop_env = g_strdup (desktop_env);
2943 G_UNLOCK (g_desktop_env);
2947 g_desktop_app_info_should_show (GAppInfo *appinfo)
2949 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2951 if (info->nodisplay)
2954 return g_desktop_app_info_get_show_in (info, NULL);
2957 /* mime types/default apps support {{{2 */
2966 ensure_dir (DirType type,
2969 char *path, *display_name;
2975 path = g_build_filename (g_get_user_config_dir (), NULL);
2979 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
2983 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
2987 g_assert_not_reached ();
2991 if (g_mkdir_with_parents (path, 0700) == 0)
2995 display_name = g_filename_display_name (path);
2996 if (type == APP_DIR)
2997 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
2998 _("Can't create user application configuration folder %s: %s"),
2999 display_name, g_strerror (errsv));
3001 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3002 _("Can't create user MIME configuration folder %s: %s"),
3003 display_name, g_strerror (errsv));
3005 g_free (display_name);
3012 update_mimeapps_list (const char *desktop_id,
3013 const char *content_type,
3014 UpdateMimeFlags flags,
3017 char *dirname, *filename, *string;
3019 gboolean load_succeeded, res;
3020 char **old_list, **list;
3021 gsize length, data_size;
3024 char **content_types;
3026 /* Don't add both at start and end */
3027 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
3028 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
3030 dirname = ensure_dir (CONF_DIR, error);
3034 filename = g_build_filename (dirname, "mimeapps.list", NULL);
3037 key_file = g_key_file_new ();
3038 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
3039 if (!load_succeeded ||
3040 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
3041 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
3042 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
3044 g_key_file_free (key_file);
3045 key_file = g_key_file_new ();
3050 content_types = g_new (char *, 2);
3051 content_types[0] = g_strdup (content_type);
3052 content_types[1] = NULL;
3056 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
3059 for (k = 0; content_types && content_types[k]; k++)
3061 /* set as default, if requested so */
3062 string = g_key_file_get_string (key_file,
3063 DEFAULT_APPLICATIONS_GROUP,
3067 if (g_strcmp0 (string, desktop_id) != 0 &&
3068 (flags & UPDATE_MIME_SET_DEFAULT))
3071 string = g_strdup (desktop_id);
3073 /* add in the non-default list too, if it's not already there */
3074 flags |= UPDATE_MIME_SET_NON_DEFAULT;
3077 if (string == NULL || desktop_id == NULL)
3078 g_key_file_remove_key (key_file,
3079 DEFAULT_APPLICATIONS_GROUP,
3083 g_key_file_set_string (key_file,
3084 DEFAULT_APPLICATIONS_GROUP,
3093 /* reuse the list from above */
3097 g_strfreev (content_types);
3098 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
3101 for (k = 0; content_types && content_types[k]; k++)
3103 /* Add to the right place in the list */
3106 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
3107 content_types[k], &length, NULL);
3109 list = g_new (char *, 1 + length + 1);
3113 /* if we're adding a last-used hint, just put the application in front of the list */
3114 if (flags & UPDATE_MIME_SET_LAST_USED)
3116 /* avoid adding this again as non-default later */
3117 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3118 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3120 list[i++] = g_strdup (desktop_id);
3125 for (j = 0; old_list[j] != NULL; j++)
3127 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3129 /* rewrite other entries if they're different from the new one */
3130 list[i++] = g_strdup (old_list[j]);
3132 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3134 /* we encountered an old entry which is equal to the one we're adding as non-default,
3135 * don't change its position in the list.
3137 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
3138 list[i++] = g_strdup (old_list[j]);
3143 /* add it at the end of the list */
3144 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
3145 list[i++] = g_strdup (desktop_id);
3149 g_strfreev (old_list);
3151 if (list[0] == NULL || desktop_id == NULL)
3152 g_key_file_remove_key (key_file,
3153 ADDED_ASSOCIATIONS_GROUP,
3157 g_key_file_set_string_list (key_file,
3158 ADDED_ASSOCIATIONS_GROUP,
3160 (const char * const *)list, i);
3167 /* reuse the list from above */
3171 g_strfreev (content_types);
3172 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
3175 for (k = 0; content_types && content_types[k]; k++)
3177 /* Remove from removed associations group (unless remove) */
3180 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
3181 content_types[k], &length, NULL);
3183 list = g_new (char *, 1 + length + 1);
3186 if (flags & UPDATE_MIME_REMOVE)
3187 list[i++] = g_strdup (desktop_id);
3190 for (j = 0; old_list[j] != NULL; j++)
3192 if (g_strcmp0 (old_list[j], desktop_id) != 0)
3193 list[i++] = g_strdup (old_list[j]);
3198 g_strfreev (old_list);
3200 if (list[0] == NULL || desktop_id == NULL)
3201 g_key_file_remove_key (key_file,
3202 REMOVED_ASSOCIATIONS_GROUP,
3206 g_key_file_set_string_list (key_file,
3207 REMOVED_ASSOCIATIONS_GROUP,
3209 (const char * const *)list, i);
3214 g_strfreev (content_types);
3216 data = g_key_file_to_data (key_file, &data_size, error);
3217 g_key_file_free (key_file);
3219 res = g_file_set_contents (filename, data, data_size, error);
3221 desktop_file_dirs_invalidate_user_config ();
3230 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
3231 const char *content_type,
3234 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3236 if (!g_desktop_app_info_ensure_saved (info, error))
3239 if (!info->desktop_id)
3241 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3242 _("Application information lacks an identifier"));
3246 /* both add support for the content type and set as last used */
3247 return update_mimeapps_list (info->desktop_id, content_type,
3248 UPDATE_MIME_SET_NON_DEFAULT |
3249 UPDATE_MIME_SET_LAST_USED,
3254 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
3255 const char *content_type,
3258 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3260 if (!g_desktop_app_info_ensure_saved (info, error))
3263 if (!info->desktop_id)
3265 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3266 _("Application information lacks an identifier"));
3270 return update_mimeapps_list (info->desktop_id, content_type,
3271 UPDATE_MIME_SET_DEFAULT,
3276 update_program_done (GPid pid,
3280 /* Did the application exit correctly */
3281 if (g_spawn_check_exit_status (status, NULL))
3283 /* Here we could clean out any caches in use */
3288 run_update_command (char *command,
3297 GError *error = NULL;
3300 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
3302 if (g_spawn_async ("/", argv,
3304 G_SPAWN_SEARCH_PATH |
3305 G_SPAWN_STDOUT_TO_DEV_NULL |
3306 G_SPAWN_STDERR_TO_DEV_NULL |
3307 G_SPAWN_DO_NOT_REAP_CHILD,
3308 NULL, NULL, /* No setup function */
3311 g_child_watch_add (pid, update_program_done, NULL);
3314 /* If we get an error at this point, it's quite likely the user doesn't
3315 * have an installed copy of either 'update-mime-database' or
3316 * 'update-desktop-database'. I don't think we want to popup an error
3317 * dialog at this point, so we just do a g_warning to give the user a
3318 * chance of debugging it.
3320 g_warning ("%s", error->message);
3327 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
3328 const char *extension,
3331 char *filename, *basename, *mimetype;
3335 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
3338 dirname = ensure_dir (MIMETYPE_DIR, error);
3342 basename = g_strdup_printf ("user-extension-%s.xml", extension);
3343 filename = g_build_filename (dirname, basename, NULL);
3347 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
3349 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
3354 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3355 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
3356 " <mime-type type=\"%s\">\n"
3357 " <comment>%s document</comment>\n"
3358 " <glob pattern=\"*.%s\"/>\n"
3360 "</mime-info>\n", mimetype, extension, extension);
3362 g_file_set_contents (filename, contents, -1, NULL);
3365 run_update_command ("update-mime-database", "mime");
3369 res = g_desktop_app_info_set_as_default_for_type (appinfo,
3379 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
3380 const char *content_type,
3383 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3385 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3388 return update_mimeapps_list (info->desktop_id, content_type,
3389 UPDATE_MIME_SET_NON_DEFAULT,
3394 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
3400 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
3401 const char *content_type,
3404 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3406 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
3409 return update_mimeapps_list (info->desktop_id, content_type,
3414 static const char **
3415 g_desktop_app_info_get_supported_types (GAppInfo *appinfo)
3417 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3419 return (const char**) info->mime_types;
3422 /* Saving and deleting {{{2 */
3425 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
3431 char *data, *desktop_id;
3436 if (info->filename != NULL)
3439 /* This is only used for object created with
3440 * g_app_info_create_from_commandline. All other
3441 * object should have a filename
3444 dirname = ensure_dir (APP_DIR, error);
3448 key_file = g_key_file_new ();
3450 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3451 "Encoding", "UTF-8");
3452 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3453 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
3454 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3455 G_KEY_FILE_DESKTOP_KEY_TYPE,
3456 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
3458 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3459 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
3460 if (info->nodisplay)
3461 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3462 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3464 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3465 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
3467 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3468 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
3470 if (info->generic_name != NULL)
3471 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3472 GENERIC_NAME_KEY, info->generic_name);
3474 if (info->fullname != NULL)
3475 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3476 FULL_NAME_KEY, info->fullname);
3478 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
3479 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
3481 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
3482 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
3484 data = g_key_file_to_data (key_file, &data_size, NULL);
3485 g_key_file_free (key_file);
3487 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
3488 filename = g_build_filename (dirname, desktop_id, NULL);
3489 g_free (desktop_id);
3492 fd = g_mkstemp (filename);
3497 display_name = g_filename_display_name (filename);
3498 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
3499 _("Can't create user desktop file %s"), display_name);
3500 g_free (display_name);
3506 desktop_id = g_path_get_basename (filename);
3508 /* FIXME - actually handle error */
3509 (void) g_close (fd, NULL);
3511 res = g_file_set_contents (filename, data, data_size, error);
3515 g_free (desktop_id);
3520 info->filename = filename;
3521 info->desktop_id = desktop_id;
3523 run_update_command ("update-desktop-database", "applications");
3525 /* We just dropped a file in the user's desktop file directory. Save
3526 * the monitor the bother of having to notice it and invalidate
3529 * This means that calls directly following this will be able to see
3530 * the results immediately.
3532 desktop_file_dirs_invalidate_user_data ();
3538 g_desktop_app_info_can_delete (GAppInfo *appinfo)
3540 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3544 if (strstr (info->filename, "/userapp-"))
3545 return g_access (info->filename, W_OK) == 0;
3552 g_desktop_app_info_delete (GAppInfo *appinfo)
3554 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
3558 if (g_remove (info->filename) == 0)
3560 update_mimeapps_list (info->desktop_id, NULL,
3564 g_free (info->filename);
3565 info->filename = NULL;
3566 g_free (info->desktop_id);
3567 info->desktop_id = NULL;
3576 /* Create for commandline {{{2 */
3578 * g_app_info_create_from_commandline:
3579 * @commandline: the commandline to use
3580 * @application_name: (allow-none): the application name, or %NULL to use @commandline
3581 * @flags: flags that can specify details of the created #GAppInfo
3582 * @error: a #GError location to store the error occurring, %NULL to ignore.
3584 * Creates a new #GAppInfo from the given information.
3586 * Note that for @commandline, the quoting rules of the Exec key of the
3587 * [freedesktop.org Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
3588 * are applied. For example, if the @commandline contains
3589 * percent-encoded URIs, the percent-character must be doubled in order to prevent it from
3590 * being swallowed by Exec key unquoting. See the specification for exact quoting rules.
3592 * Returns: (transfer full): new #GAppInfo for given command.
3595 g_app_info_create_from_commandline (const char *commandline,
3596 const char *application_name,
3597 GAppInfoCreateFlags flags,
3602 GDesktopAppInfo *info;
3604 g_return_val_if_fail (commandline, NULL);
3606 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
3608 info->filename = NULL;
3609 info->desktop_id = NULL;
3611 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
3612 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
3613 info->hidden = FALSE;
3614 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
3615 info->exec = g_strconcat (commandline, " %u", NULL);
3617 info->exec = g_strconcat (commandline, " %f", NULL);
3618 info->nodisplay = TRUE;
3619 info->binary = binary_from_exec (info->exec);
3621 if (application_name)
3622 info->name = g_strdup (application_name);
3625 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
3626 split = g_strsplit (commandline, " ", 2);
3627 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
3629 info->name = basename;
3630 if (info->name == NULL)
3631 info->name = g_strdup ("custom");
3633 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
3635 return G_APP_INFO (info);
3638 /* GAppInfo interface init */
3641 g_desktop_app_info_iface_init (GAppInfoIface *iface)
3643 iface->dup = g_desktop_app_info_dup;
3644 iface->equal = g_desktop_app_info_equal;
3645 iface->get_id = g_desktop_app_info_get_id;
3646 iface->get_name = g_desktop_app_info_get_name;
3647 iface->get_description = g_desktop_app_info_get_description;
3648 iface->get_executable = g_desktop_app_info_get_executable;
3649 iface->get_icon = g_desktop_app_info_get_icon;
3650 iface->launch = g_desktop_app_info_launch;
3651 iface->supports_uris = g_desktop_app_info_supports_uris;
3652 iface->supports_files = g_desktop_app_info_supports_files;
3653 iface->launch_uris = g_desktop_app_info_launch_uris;
3654 iface->should_show = g_desktop_app_info_should_show;
3655 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
3656 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
3657 iface->add_supports_type = g_desktop_app_info_add_supports_type;
3658 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
3659 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
3660 iface->can_delete = g_desktop_app_info_can_delete;
3661 iface->do_delete = g_desktop_app_info_delete;
3662 iface->get_commandline = g_desktop_app_info_get_commandline;
3663 iface->get_display_name = g_desktop_app_info_get_display_name;
3664 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
3665 iface->get_supported_types = g_desktop_app_info_get_supported_types;
3668 /* Recommended applications {{{2 */
3670 /* Converts content_type into a list of itself with all of its parent
3671 * types (if include_fallback is enabled) or just returns a single-item
3672 * list with the unaliased content type.
3675 get_list_of_mimetypes (const gchar *content_type,
3676 gboolean include_fallback)
3681 array = g_ptr_array_new ();
3682 unaliased = _g_unix_content_type_unalias (content_type);
3683 g_ptr_array_add (array, unaliased);
3685 if (include_fallback)
3689 /* Iterate the array as we grow it, until we have nothing more to add */
3690 for (i = 0; i < array->len; i++)
3692 gchar **parents = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3695 for (j = 0; parents[j]; j++)
3696 /* Don't add duplicates */
3697 if (!array_contains (array, parents[j]))
3698 g_ptr_array_add (array, parents[j]);
3700 g_free (parents[j]);
3702 /* We already stole or freed each element. Free the container. */
3707 g_ptr_array_add (array, NULL);
3709 return (gchar **) g_ptr_array_free (array, FALSE);
3713 g_desktop_app_info_get_desktop_ids_for_content_type (const gchar *content_type,
3714 gboolean include_fallback)
3716 GPtrArray *hits, *blacklist;
3720 hits = g_ptr_array_new ();
3721 blacklist = g_ptr_array_new ();
3723 types = get_list_of_mimetypes (content_type, include_fallback);
3725 desktop_file_dirs_lock ();
3727 for (i = 0; types[i]; i++)
3728 for (j = 0; j < n_desktop_file_dirs; j++)
3729 desktop_file_dir_mime_lookup (&desktop_file_dirs[j], types[i], hits, blacklist);
3731 desktop_file_dirs_unlock ();
3733 g_ptr_array_add (hits, NULL);
3735 g_ptr_array_free (blacklist, TRUE);
3738 return (gchar **) g_ptr_array_free (hits, FALSE);
3742 g_desktop_app_info_get_defaults_for_content_type (const gchar *content_type)
3748 types = get_list_of_mimetypes (content_type, TRUE);
3749 results = g_ptr_array_new ();
3751 desktop_file_dirs_lock ();
3753 for (i = 0; types[i]; i++)
3754 for (j = 0; j < n_desktop_file_dirs; j++)
3755 desktop_file_dir_default_lookup (&desktop_file_dirs[j], types[i], results);
3757 desktop_file_dirs_unlock ();
3759 g_ptr_array_add (results, NULL);
3762 return (gchar **) g_ptr_array_free (results, FALSE);
3766 * g_app_info_get_recommended_for_type:
3767 * @content_type: the content type to find a #GAppInfo for
3769 * Gets a list of recommended #GAppInfos for a given content type, i.e.
3770 * those applications which claim to support the given content type exactly,
3771 * and not by MIME type subclassing.
3772 * Note that the first application of the list is the last used one, i.e.
3773 * the last one for which g_app_info_set_as_last_used_for_type() has been
3776 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3777 * for given @content_type or %NULL on error.
3782 g_app_info_get_recommended_for_type (const gchar *content_type)
3784 gchar **desktop_ids;
3788 g_return_val_if_fail (content_type != NULL, NULL);
3790 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
3793 for (i = 0; desktop_ids[i]; i++)
3795 GDesktopAppInfo *info;
3797 info = g_desktop_app_info_new (desktop_ids[i]);
3799 infos = g_list_prepend (infos, info);
3802 g_strfreev (desktop_ids);
3804 return g_list_reverse (infos);
3808 * g_app_info_get_fallback_for_type:
3809 * @content_type: the content type to find a #GAppInfo for
3811 * Gets a list of fallback #GAppInfos for a given content type, i.e.
3812 * those applications which claim to support the given content type
3813 * by MIME type subclassing and not directly.
3815 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3816 * for given @content_type or %NULL on error.
3821 g_app_info_get_fallback_for_type (const gchar *content_type)
3823 gchar **recommended_ids;
3828 g_return_val_if_fail (content_type != NULL, NULL);
3830 recommended_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, FALSE);
3831 all_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
3834 for (i = 0; all_ids[i]; i++)
3836 GDesktopAppInfo *info;
3839 /* Don't return the ones on the recommended list */
3840 for (j = 0; recommended_ids[j]; j++)
3841 if (g_str_equal (all_ids[i], recommended_ids[j]))
3844 if (recommended_ids[j])
3847 info = g_desktop_app_info_new (all_ids[i]);
3850 infos = g_list_prepend (infos, info);
3853 g_strfreev (recommended_ids);
3854 g_strfreev (all_ids);
3856 return g_list_reverse (infos);
3860 * g_app_info_get_all_for_type:
3861 * @content_type: the content type to find a #GAppInfo for
3863 * Gets a list of all #GAppInfos for a given content type,
3864 * including the recommended and fallback #GAppInfos. See
3865 * g_app_info_get_recommended_for_type() and
3866 * g_app_info_get_fallback_for_type().
3868 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
3869 * for given @content_type or %NULL on error.
3872 g_app_info_get_all_for_type (const char *content_type)
3874 gchar **desktop_ids;
3878 g_return_val_if_fail (content_type != NULL, NULL);
3880 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
3883 for (i = 0; desktop_ids[i]; i++)
3885 GDesktopAppInfo *info;
3887 info = g_desktop_app_info_new (desktop_ids[i]);
3889 infos = g_list_prepend (infos, info);
3892 g_strfreev (desktop_ids);
3894 return g_list_reverse (infos);
3898 * g_app_info_reset_type_associations:
3899 * @content_type: a content type
3901 * Removes all changes to the type associations done by
3902 * g_app_info_set_as_default_for_type(),
3903 * g_app_info_set_as_default_for_extension(),
3904 * g_app_info_add_supports_type() or
3905 * g_app_info_remove_supports_type().
3910 g_app_info_reset_type_associations (const char *content_type)
3912 update_mimeapps_list (NULL, content_type,
3918 * g_app_info_get_default_for_type:
3919 * @content_type: the content type to find a #GAppInfo for
3920 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
3923 * Gets the default #GAppInfo for a given content type.
3925 * Returns: (transfer full): #GAppInfo for given @content_type or
3929 g_app_info_get_default_for_type (const char *content_type,
3930 gboolean must_support_uris)
3932 gchar **desktop_ids;
3936 g_return_val_if_fail (content_type != NULL, NULL);
3938 desktop_ids = g_desktop_app_info_get_defaults_for_content_type (content_type);
3941 for (i = 0; desktop_ids[i]; i++)
3943 info = (GAppInfo *) g_desktop_app_info_new (desktop_ids[i]);
3947 if (!must_support_uris || g_app_info_supports_uris (info))
3950 g_object_unref (info);
3955 g_strfreev (desktop_ids);
3957 /* If we can't find a default app for this content type, pick one from
3958 * the list of all supported apps. This will be ordered by the user's
3959 * preference and by "recommended" apps first, so the first one we
3960 * find is probably the best fallback.
3964 desktop_ids = g_desktop_app_info_get_desktop_ids_for_content_type (content_type, TRUE);
3966 for (i = 0; desktop_ids[i]; i++)
3968 info = (GAppInfo *) g_desktop_app_info_new (desktop_ids[i]);
3972 if (!must_support_uris || g_app_info_supports_uris (info))
3975 g_object_unref (info);
3980 g_strfreev (desktop_ids);
3987 * g_app_info_get_default_for_uri_scheme:
3988 * @uri_scheme: a string containing a URI scheme.
3990 * Gets the default application for handling URIs with
3991 * the given URI scheme. A URI scheme is the initial part
3992 * of the URI, up to but not including the ':', e.g. "http",
3995 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3998 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
4001 char *content_type, *scheme_down;
4003 scheme_down = g_ascii_strdown (uri_scheme, -1);
4004 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
4005 g_free (scheme_down);
4006 app_info = g_app_info_get_default_for_type (content_type, FALSE);
4007 g_free (content_type);
4012 /* "Get all" API {{{2 */
4015 * g_desktop_app_info_search:
4016 * @search_string: the search string to use
4018 * Searches desktop files for ones that match @search_string.
4020 * The return value is an array of strvs. Each strv contains a list of
4021 * applications that matched @search_string with an equal score. The
4022 * outer list is sorted by score so that the first strv contains the
4023 * best-matching applications, and so on.
4024 * The algorithm for determining matches is undefined and may change at
4027 * Returns: (array zero-terminated=1) (element-type GStrv) (transfer full): a
4028 * list of strvs. Free each item with g_strfreev() and free the outer
4029 * list with g_free().
4032 g_desktop_app_info_search (const gchar *search_string)
4034 gchar **search_tokens;
4035 gint last_category = -1;
4037 gint n_categories = 0;
4038 gint start_of_category;
4041 search_tokens = g_str_tokenize_and_fold (search_string, NULL, NULL);
4043 desktop_file_dirs_lock ();
4045 reset_total_search_results ();
4047 for (i = 0; i < n_desktop_file_dirs; i++)
4049 for (j = 0; search_tokens[j]; j++)
4051 desktop_file_dir_search (&desktop_file_dirs[i], search_tokens[j]);
4052 merge_token_results (j == 0);
4054 merge_directory_results ();
4057 sort_total_search_results ();
4059 /* Count the total number of unique categories */
4060 for (i = 0; i < static_total_results_size; i++)
4061 if (static_total_results[i].category != last_category)
4063 last_category = static_total_results[i].category;
4067 results = g_new (gchar **, n_categories + 1);
4069 /* Start loading into the results list */
4070 start_of_category = 0;
4071 for (i = 0; i < n_categories; i++)
4073 gint n_items_in_category = 0;
4077 this_category = static_total_results[start_of_category].category;
4079 while (start_of_category + n_items_in_category < static_total_results_size &&
4080 static_total_results[start_of_category + n_items_in_category].category == this_category)
4081 n_items_in_category++;
4083 results[i] = g_new (gchar *, n_items_in_category + 1);
4084 for (j = 0; j < n_items_in_category; j++)
4085 results[i][j] = g_strdup (static_total_results[start_of_category + j].app_name);
4086 results[i][j] = NULL;
4088 start_of_category += n_items_in_category;
4092 desktop_file_dirs_unlock ();
4094 g_strfreev (search_tokens);
4100 * g_app_info_get_all:
4102 * Gets a list of all of the applications currently registered
4105 * For desktop files, this includes applications that have
4106 * `NoDisplay=true` set or are excluded from display by means
4107 * of `OnlyShowIn` or `NotShowIn`. See g_app_info_should_show().
4108 * The returned list does not include applications which have
4109 * the `Hidden` key set.
4111 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfos.
4114 g_app_info_get_all (void)
4117 GHashTableIter iter;
4122 apps = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
4124 desktop_file_dirs_lock ();
4126 for (i = 0; i < n_desktop_file_dirs; i++)
4127 desktop_file_dir_get_all (&desktop_file_dirs[i], apps);
4129 desktop_file_dirs_unlock ();
4132 g_hash_table_iter_init (&iter, apps);
4133 while (g_hash_table_iter_next (&iter, NULL, &value))
4136 infos = g_list_prepend (infos, value);
4139 g_hash_table_destroy (apps);
4144 /* GDesktopAppInfoLookup interface {{{2 */
4146 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4148 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
4149 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
4152 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
4156 /* "Get for mime type" APIs {{{2 */
4159 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
4160 * @lookup: a #GDesktopAppInfoLookup
4161 * @uri_scheme: a string containing a URI scheme.
4163 * Gets the default application for launching applications
4164 * using this URI scheme for a particular GDesktopAppInfoLookup
4167 * The GDesktopAppInfoLookup interface and this function is used
4168 * to implement g_app_info_get_default_for_uri_scheme() backends
4169 * in a GIO module. There is no reason for applications to use it
4170 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
4172 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
4174 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
4177 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
4178 const char *uri_scheme)
4180 GDesktopAppInfoLookupIface *iface;
4182 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
4184 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
4186 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
4189 G_GNUC_END_IGNORE_DEPRECATIONS
4191 /* Misc getter APIs {{{2 */
4194 * g_desktop_app_info_get_startup_wm_class:
4195 * @info: a #GDesktopAppInfo that supports startup notify
4197 * Retrieves the StartupWMClass field from @info. This represents the
4198 * WM_CLASS property of the main window of the application, if launched
4201 * Returns: (transfer none): the startup WM class, or %NULL if none is set
4202 * in the desktop file.
4207 g_desktop_app_info_get_startup_wm_class (GDesktopAppInfo *info)
4209 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4211 return info->startup_wm_class;
4215 * g_desktop_app_info_get_string:
4216 * @info: a #GDesktopAppInfo
4217 * @key: the key to look up
4219 * Looks up a string value in the keyfile backing @info.
4221 * The @key is looked up in the "Desktop Entry" group.
4223 * Returns: a newly allocated string, or %NULL if the key
4229 g_desktop_app_info_get_string (GDesktopAppInfo *info,
4232 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4234 return g_key_file_get_string (info->keyfile,
4235 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4239 * g_desktop_app_info_get_boolean:
4240 * @info: a #GDesktopAppInfo
4241 * @key: the key to look up
4243 * Looks up a boolean value in the keyfile backing @info.
4245 * The @key is looked up in the "Desktop Entry" group.
4247 * Returns: the boolean value, or %FALSE if the key
4253 g_desktop_app_info_get_boolean (GDesktopAppInfo *info,
4256 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4258 return g_key_file_get_boolean (info->keyfile,
4259 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4263 * g_desktop_app_info_has_key:
4264 * @info: a #GDesktopAppInfo
4265 * @key: the key to look up
4267 * Returns whether @key exists in the "Desktop Entry" group
4268 * of the keyfile backing @info.
4270 * Returns: %TRUE if the @key exists
4275 g_desktop_app_info_has_key (GDesktopAppInfo *info,
4278 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
4280 return g_key_file_has_key (info->keyfile,
4281 G_KEY_FILE_DESKTOP_GROUP, key, NULL);
4284 /* Desktop actions support {{{2 */
4287 * g_desktop_app_info_list_actions:
4288 * @info: a #GDesktopAppInfo
4290 * Returns the list of "additional application actions" supported on the
4291 * desktop file, as per the desktop file specification.
4293 * As per the specification, this is the list of actions that are
4294 * explicitly listed in the "Actions" key of the [Desktop Entry] group.
4296 * Returns: (array zero-terminated=1) (element-type utf8) (transfer none): a list of strings, always non-%NULL
4300 const gchar * const *
4301 g_desktop_app_info_list_actions (GDesktopAppInfo *info)
4303 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4305 return (const gchar **) info->actions;
4309 app_info_has_action (GDesktopAppInfo *info,
4310 const gchar *action_name)
4314 for (i = 0; info->actions[i]; i++)
4315 if (g_str_equal (info->actions[i], action_name))
4322 * g_desktop_app_info_get_action_name:
4323 * @info: a #GDesktopAppInfo
4324 * @action_name: the name of the action as from
4325 * g_desktop_app_info_list_actions()
4327 * Gets the user-visible display name of the "additional application
4328 * action" specified by @action_name.
4330 * This corresponds to the "Name" key within the keyfile group for the
4333 * Returns: (transfer full): the locale-specific action name
4338 g_desktop_app_info_get_action_name (GDesktopAppInfo *info,
4339 const gchar *action_name)
4344 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), NULL);
4345 g_return_val_if_fail (action_name != NULL, NULL);
4346 g_return_val_if_fail (app_info_has_action (info, action_name), NULL);
4348 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4349 result = g_key_file_get_locale_string (info->keyfile, group_name, "Name", NULL, NULL);
4350 g_free (group_name);
4352 /* The spec says that the Name field must be given.
4354 * If it's not, let's follow the behaviour of our get_name()
4355 * implementation above and never return %NULL.
4358 result = g_strdup (_("Unnamed"));
4364 * g_desktop_app_info_launch_action:
4365 * @info: a #GDesktopAppInfo
4366 * @action_name: the name of the action as from
4367 * g_desktop_app_info_list_actions()
4368 * @launch_context: (allow-none): a #GAppLaunchContext
4370 * Activates the named application action.
4372 * You may only call this function on action names that were
4373 * returned from g_desktop_app_info_list_actions().
4375 * Note that if the main entry of the desktop file indicates that the
4376 * application supports startup notification, and @launch_context is
4377 * non-%NULL, then startup notification will be used when activating the
4378 * action (and as such, invocation of the action on the receiving side
4379 * must signal the end of startup notification when it is completed).
4380 * This is the expected behaviour of applications declaring additional
4381 * actions, as per the desktop file specification.
4383 * As with g_app_info_launch() there is no way to detect failures that
4384 * occur while using this function.
4389 g_desktop_app_info_launch_action (GDesktopAppInfo *info,
4390 const gchar *action_name,
4391 GAppLaunchContext *launch_context)
4393 GDBusConnection *session_bus;
4395 g_return_if_fail (G_IS_DESKTOP_APP_INFO (info));
4396 g_return_if_fail (action_name != NULL);
4397 g_return_if_fail (app_info_has_action (info, action_name));
4399 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
4401 if (session_bus && info->app_id)
4405 object_path = object_path_from_appid (info->app_id);
4406 g_dbus_connection_call (session_bus, info->app_id, object_path,
4407 "org.freedesktop.Application", "ActivateAction",
4408 g_variant_new ("(sav@a{sv})", action_name, NULL,
4409 g_desktop_app_info_make_platform_data (info, NULL, launch_context)),
4410 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
4411 g_free (object_path);
4418 group_name = g_strdup_printf ("Desktop Action %s", action_name);
4419 exec_line = g_key_file_get_string (info->keyfile, group_name, "Exec", NULL);
4420 g_free (group_name);
4423 g_desktop_app_info_launch_uris_with_spawn (info, session_bus, exec_line, NULL, launch_context,
4424 _SPAWN_FLAGS_DEFAULT, NULL, NULL, NULL, NULL, NULL);
4427 if (session_bus != NULL)
4429 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
4430 g_object_unref (session_bus);
4435 /* vim:set foldmethod=marker: */