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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
38 #include "gthemedicon.h"
39 #include "gfileicon.h"
40 #include <glib/gstdio.h>
46 * SECTION:gdesktopappinfo
47 * @short_description: Application information from desktop files
48 * @include: gio/gdesktopappinfo.h
50 * #GDesktopAppInfo is an implementation of #GAppInfo based on
55 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
56 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
57 #define MIME_CACHE_GROUP "MIME Cache"
59 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
61 static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
62 static void mime_info_cache_reload (const char *dir);
67 * Information about an installed application from a desktop file.
69 struct _GDesktopAppInfo
71 GObject parent_instance;
77 /* FIXME: what about GenericName ? */
91 guint startup_notify : 1;
92 /* FIXME: what about StartupWMClass ? */
95 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
96 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
97 g_desktop_app_info_iface_init))
100 search_path_init (gpointer data)
103 const char * const *data_dirs;
104 const char *user_data_dir;
107 data_dirs = g_get_system_data_dirs ();
108 length = g_strv_length ((char **) data_dirs);
110 args = g_new (char *, length + 2);
113 user_data_dir = g_get_user_data_dir ();
114 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
115 for (i = 0; i < length; i++)
116 args[j++] = g_build_filename (data_dirs[i],
117 "applications", NULL);
123 static const char * const *
124 get_applications_search_path (void)
126 static GOnce once_init = G_ONCE_INIT;
127 return g_once (&once_init, search_path_init, NULL);
131 g_desktop_app_info_finalize (GObject *object)
133 GDesktopAppInfo *info;
135 info = G_DESKTOP_APP_INFO (object);
137 g_free (info->desktop_id);
138 g_free (info->filename);
140 g_free (info->comment);
141 g_free (info->icon_name);
143 g_object_unref (info->icon);
144 g_strfreev (info->only_show_in);
145 g_strfreev (info->not_show_in);
146 g_free (info->try_exec);
148 g_free (info->binary);
151 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
155 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
157 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
159 gobject_class->finalize = g_desktop_app_info_finalize;
163 g_desktop_app_info_init (GDesktopAppInfo *local)
168 * g_desktop_app_info_new_from_filename:
169 * @filename: a string containing a file name.
171 * Creates a new #GDesktopAppInfo.
173 * Returns: a new #GDesktopAppInfo or %NULL on error.
176 g_desktop_app_info_new_from_filename (const char *filename)
178 GDesktopAppInfo *info;
184 key_file = g_key_file_new ();
186 if (!g_key_file_load_from_file (key_file,
191 g_key_file_free (key_file);
195 start_group = g_key_file_get_start_group (key_file);
196 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
198 g_free (start_group);
199 g_key_file_free (key_file);
202 g_free (start_group);
204 type = g_key_file_get_string (key_file,
205 G_KEY_FILE_DESKTOP_GROUP,
206 G_KEY_FILE_DESKTOP_KEY_TYPE,
208 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
211 g_key_file_free (key_file);
216 try_exec = g_key_file_get_string (key_file,
217 G_KEY_FILE_DESKTOP_GROUP,
218 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
223 t = g_find_program_in_path (try_exec);
227 g_key_file_free (key_file);
233 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
234 info->filename = g_strdup (filename);
236 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
237 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
238 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
239 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
240 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);
241 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);
242 info->try_exec = try_exec;
243 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
244 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
245 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
246 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
247 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
249 g_key_file_free (key_file);
254 if (g_path_is_absolute (info->icon_name))
258 file = g_file_new_for_path (info->icon_name);
259 info->icon = g_file_icon_new (file);
260 g_object_unref (file);
263 info->icon = g_themed_icon_new (info->icon_name);
274 while (*p != ' ' && *p != 0)
277 info->binary = g_strndup (start, p - start);
284 * g_desktop_app_info_new:
285 * @desktop_id: the desktop file id
287 * Creates a new #GDesktopAppInfo.
289 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
292 g_desktop_app_info_new (const char *desktop_id)
294 GDesktopAppInfo *appinfo;
295 const char * const *dirs;
299 dirs = get_applications_search_path ();
301 basename = g_strdup (desktop_id);
303 for (i = 0; dirs[i] != NULL; i++)
308 filename = g_build_filename (dirs[i], desktop_id, NULL);
309 appinfo = g_desktop_app_info_new_from_filename (filename);
315 while ((p = strchr (p, '-')) != NULL)
319 filename = g_build_filename (dirs[i], basename, NULL);
320 appinfo = g_desktop_app_info_new_from_filename (filename);
335 appinfo->desktop_id = g_strdup (desktop_id);
337 if (g_desktop_app_info_get_is_hidden (appinfo))
339 g_object_unref (appinfo);
347 g_desktop_app_info_dup (GAppInfo *appinfo)
349 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
350 GDesktopAppInfo *new_info;
352 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
354 new_info->filename = g_strdup (info->filename);
355 new_info->desktop_id = g_strdup (info->desktop_id);
357 new_info->name = g_strdup (info->name);
358 new_info->comment = g_strdup (info->comment);
359 new_info->nodisplay = info->nodisplay;
360 new_info->icon_name = g_strdup (info->icon_name);
361 new_info->icon = g_object_ref (info->icon);
362 new_info->only_show_in = g_strdupv (info->only_show_in);
363 new_info->not_show_in = g_strdupv (info->not_show_in);
364 new_info->try_exec = g_strdup (info->try_exec);
365 new_info->exec = g_strdup (info->exec);
366 new_info->binary = g_strdup (info->binary);
367 new_info->path = g_strdup (info->path);
368 new_info->hidden = info->hidden;
369 new_info->terminal = info->terminal;
370 new_info->startup_notify = info->startup_notify;
372 return G_APP_INFO (new_info);
376 g_desktop_app_info_equal (GAppInfo *appinfo1,
379 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
380 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
382 if (info1->desktop_id == NULL ||
383 info2->desktop_id == NULL)
386 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
390 g_desktop_app_info_get_id (GAppInfo *appinfo)
392 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
394 return info->desktop_id;
398 g_desktop_app_info_get_name (GAppInfo *appinfo)
400 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
402 if (info->name == NULL)
408 * g_desktop_app_info_get_is_hidden:
409 * @info: a #GDesktopAppInfo.
411 * A desktop file is hidden if the Hidden key in it is
414 * Returns: %TRUE if hidden, %FALSE otherwise.
417 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
423 g_desktop_app_info_get_description (GAppInfo *appinfo)
425 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
427 return info->comment;
431 g_desktop_app_info_get_executable (GAppInfo *appinfo)
433 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
439 g_desktop_app_info_get_icon (GAppInfo *appinfo)
441 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
447 expand_macro_single (char macro, GFile *file)
452 path = g_file_get_path (file);
453 uri = g_file_get_uri (file);
459 result = g_shell_quote (uri);
464 result = g_shell_quote (path);
469 result = g_shell_quote (g_path_get_dirname (path));
474 result = g_shell_quote (g_path_get_basename (path));
485 expand_macro (char macro,
487 GDesktopAppInfo *info,
490 GList *files = *file_list;
493 g_return_if_fail (exec != NULL);
503 expanded = expand_macro_single (macro, files->data);
506 g_string_append (exec, expanded);
520 expanded = expand_macro_single (macro, files->data);
523 g_string_append (exec, expanded);
529 if (files != NULL && expanded)
530 g_string_append_c (exec, ' ');
538 g_string_append (exec, "--icon ");
539 g_string_append (exec, info->icon_name);
545 g_string_append (exec, info->name);
550 g_string_append (exec, info->filename);
553 case 'm': /* deprecated */
557 g_string_append_c (exec, '%');
565 expand_application_parameters (GDesktopAppInfo *info,
571 GList *file_list = *files;
572 const char *p = info->exec;
573 GString *expanded_exec = g_string_new (NULL);
576 if (info->exec == NULL)
578 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
579 _("Desktop file didn't specify Exec field"));
585 if (p[0] == '%' && p[1] != '\0')
587 expand_macro (p[1], expanded_exec, info, files);
591 g_string_append_c (expanded_exec, *p);
596 /* No file substitutions */
597 if (file_list == *files && file_list != NULL)
599 /* If there is no macro default to %f. This is also what KDE does */
600 g_string_append_c (expanded_exec, ' ');
601 expand_macro ('f', expanded_exec, info, files);
604 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
605 g_string_free (expanded_exec, TRUE);
610 prepend_terminal_to_vector (int *argc,
617 char **term_argv = NULL;
622 g_return_val_if_fail (argc != NULL, FALSE);
623 g_return_val_if_fail (argv != NULL, FALSE);
631 /* compute size if not given */
634 for (i = 0; the_argv[i] != NULL; i++)
640 term_argv = g_new0 (char *, 3);
642 check = g_find_program_in_path ("gnome-terminal");
645 term_argv[0] = check;
646 /* Note that gnome-terminal takes -x and
647 * as -e in gnome-terminal is broken we use that. */
648 term_argv[1] = g_strdup ("-x");
653 check = g_find_program_in_path ("nxterm");
655 check = g_find_program_in_path ("color-xterm");
657 check = g_find_program_in_path ("rxvt");
659 check = g_find_program_in_path ("xterm");
661 check = g_find_program_in_path ("dtterm");
664 check = g_strdup ("xterm");
665 g_warning ("couldn't find a terminal, falling back to xterm");
667 term_argv[0] = check;
668 term_argv[1] = g_strdup ("-e");
671 real_argc = term_argc + *argc;
672 real_argv = g_new (char *, real_argc + 1);
674 for (i = 0; i < term_argc; i++)
675 real_argv[i] = term_argv[i];
677 for (j = 0; j < *argc; j++, i++)
678 real_argv[i] = (char *)the_argv[j];
686 /* we use g_free here as we sucked all the inner strings
687 * out from it into real_argv */
692 #endif /* G_OS_WIN32 */
695 /* '=' is the new '\0'.
696 * DO NOT CALL unless at least one string ends with '='
699 is_env (const char *a,
704 if (*a == 0 || *b == 0)
717 /* free with g_strfreev */
719 replace_env_var (char **old_environ,
721 const char *new_value)
723 int length, new_length;
724 int index, new_index;
728 /* do two things at once:
729 * - discover the length of the environment ('length')
730 * - find the location (if any) of the env var ('index')
733 for (length = 0; old_environ[length]; length++)
735 /* if we already have it in our environment, replace */
736 if (is_env (old_environ[length], env_var))
741 /* no current env var, no desired env value.
744 if (new_value == NULL && index == -1)
747 /* in all cases now, we will be using a modified environment.
748 * determine its length and allocated it.
751 * new_index = location to insert, if any
752 * new_length = length of the new array
753 * new_environ = the pointer array for the new environment
756 if (new_value == NULL && index >= 0)
758 /* in this case, we will be removing an entry */
759 new_length = length - 1;
762 else if (new_value != NULL && index < 0)
764 /* in this case, we will be adding an entry to the end */
765 new_length = length + 1;
769 /* in this case, we will be replacing the existing entry */
775 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
776 new_environ[new_length] = NULL;
778 /* now we do the copying.
779 * for each entry in the new environment, we decide what to do
783 for (new_i = 0; new_i < new_length; new_i++)
785 if (new_i == new_index)
787 /* insert our new item */
788 new_environ[new_i] = g_strconcat (env_var,
793 /* if we had an old entry, skip it now */
799 /* if this is the old DESKTOP_STARTUP_ID, skip it */
803 /* copy an old item */
804 new_environ[new_i] = g_strdup (old_environ[i]);
809 g_strfreev (old_environ);
815 dup_list_segment (GList *start,
821 while (start != NULL && start != end)
823 res = g_list_prepend (res, start->data);
827 return g_list_reverse (res);
830 #ifdef HAVE__NSGETENVIRON
831 #define environ (*_NSGetEnviron())
832 #elif !defined(G_OS_WIN32)
834 /* According to the Single Unix Specification, environ is not in
835 * * any system header, although unistd.h often declares it.
837 extern char **environ;
841 g_desktop_app_info_launch (GAppInfo *appinfo,
843 GAppLaunchContext *launch_context,
846 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
847 gboolean completed = FALSE;
849 GList *launched_files;
856 g_return_val_if_fail (appinfo != NULL, FALSE);
864 if (!expand_application_parameters (info, &files,
865 &argc, &argv, error))
868 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
870 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
871 _("Unable to find terminal required for application"));
878 launched_files = dup_list_segment (old_files, files);
880 display = g_app_launch_context_get_display (launch_context,
885 if (info->startup_notify)
886 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
890 if (display || sn_id)
894 envp = g_new0 (char *, 1);
896 envp = g_strdupv (environ);
900 envp = replace_env_var (envp,
905 envp = replace_env_var (envp,
906 "DESKTOP_STARTUP_ID",
912 g_list_free (launched_files);
915 if (!g_spawn_async (info->path, /* working directory */
918 G_SPAWN_SEARCH_PATH /* flags */,
919 NULL /* child_setup */,
921 NULL /* child_pid */,
926 g_app_launch_context_launch_failed (launch_context, sn_id);
940 while (files != NULL);
952 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
954 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
957 ((strstr (info->exec, "%u") != NULL) ||
958 (strstr (info->exec, "%U") != NULL));
962 g_desktop_app_info_supports_files (GAppInfo *appinfo)
964 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
967 ((strstr (info->exec, "%f") != NULL) ||
968 (strstr (info->exec, "%F") != NULL));
972 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
974 GAppLaunchContext *launch_context,
984 file = g_file_new_for_uri (uris->data);
986 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
989 files = g_list_prepend (files, file);
992 files = g_list_reverse (files);
994 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
996 g_list_foreach (files, (GFunc)g_object_unref, NULL);
1002 G_LOCK_DEFINE_STATIC (g_desktop_env);
1003 static gchar *g_desktop_env = NULL;
1006 * g_desktop_app_info_set_desktop_env:
1007 * @desktop_env: a string specifying what desktop this is
1009 * Sets the name of the desktop that the application is running in.
1010 * This is used by g_app_info_should_show() to evaluate the
1011 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1012 * desktop entry fields.
1014 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1015 * Menu specification</ulink> recognizes the following:
1017 * <member>GNOME</member>
1018 * <member>KDE</member>
1019 * <member>ROX</member>
1020 * <member>XFCE</member>
1021 * <member>Old</member>
1024 * Should be called only once; subsequent calls are ignored.
1027 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1029 G_LOCK (g_desktop_env);
1031 g_desktop_env = g_strdup (desktop_env);
1032 G_UNLOCK (g_desktop_env);
1036 g_desktop_app_info_should_show (GAppInfo *appinfo)
1038 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1040 const gchar *desktop_env;
1043 if (info->nodisplay)
1046 G_LOCK (g_desktop_env);
1047 desktop_env = g_desktop_env;
1048 G_UNLOCK (g_desktop_env);
1050 if (info->only_show_in)
1052 if (desktop_env == NULL)
1056 for (i = 0; info->only_show_in[i] != NULL; i++)
1058 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1068 if (info->not_show_in && desktop_env)
1070 for (i = 0; info->not_show_in[i] != NULL; i++)
1072 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1086 ensure_dir (DirType type,
1089 char *path, *display_name;
1092 if (type == APP_DIR)
1093 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1095 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1098 if (g_mkdir_with_parents (path, 0700) == 0)
1102 display_name = g_filename_display_name (path);
1103 if (type == APP_DIR)
1104 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1105 _("Can't create user application configuration folder %s: %s"),
1106 display_name, g_strerror (err));
1108 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1109 _("Can't create user MIME configuration folder %s: %s"),
1110 display_name, g_strerror (err));
1112 g_free (display_name);
1119 update_default_list (const char *desktop_id,
1120 const char *content_type,
1121 gboolean add_at_start,
1122 gboolean add_at_end,
1126 char *dirname, *filename;
1128 gboolean load_succeeded, res;
1131 gsize length, data_size;
1135 /* Don't add both at start and end */
1136 g_assert (!(add_at_start && add_at_end));
1138 dirname = ensure_dir (APP_DIR, error);
1142 filename = g_build_filename (dirname, "defaults.list", NULL);
1145 key_file = g_key_file_new ();
1146 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1147 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1149 g_key_file_free (key_file);
1150 key_file = g_key_file_new ();
1153 /* Add to the right place in the list */
1156 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1157 content_type, &length, NULL);
1159 list = g_new (char *, 1 + length + 1);
1163 list[i++] = g_strdup (desktop_id);
1166 for (j = 0; old_list[j] != NULL; j++)
1168 if (strcmp (old_list[j], desktop_id) != 0)
1169 list[i++] = g_strdup (old_list[j]);
1173 list[i++] = g_strdup (desktop_id);
1176 g_strfreev (old_list);
1178 g_key_file_set_string_list (key_file,
1179 DEFAULT_APPLICATIONS_GROUP,
1181 (const char * const *)list, i);
1185 /* Remove from removed associations group (unless remove) */
1188 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1189 content_type, &length, NULL);
1191 list = g_new (char *, 1 + length + 1);
1195 list[i++] = g_strdup (desktop_id);
1198 for (j = 0; old_list[j] != NULL; j++)
1200 if (strcmp (old_list[j], desktop_id) != 0)
1201 list[i++] = g_strdup (old_list[j]);
1206 g_strfreev (old_list);
1208 if (list[0] == NULL)
1209 g_key_file_remove_key (key_file,
1210 REMOVED_ASSOCIATIONS_GROUP,
1214 g_key_file_set_string_list (key_file,
1215 REMOVED_ASSOCIATIONS_GROUP,
1217 (const char * const *)list, i);
1222 data = g_key_file_to_data (key_file, &data_size, error);
1223 g_key_file_free (key_file);
1225 res = g_file_set_contents (filename, data, data_size, error);
1227 mime_info_cache_reload (NULL);
1236 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1237 const char *content_type,
1240 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1242 return update_default_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1246 update_program_done (GPid pid,
1250 /* Did the application exit correctly */
1251 if (WIFEXITED (status) &&
1252 WEXITSTATUS (status) == 0)
1254 /* Here we could clean out any caches in use */
1259 run_update_command (char *command,
1268 GError *error = NULL;
1271 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1273 if (g_spawn_async ("/", argv,
1275 G_SPAWN_SEARCH_PATH |
1276 G_SPAWN_STDOUT_TO_DEV_NULL |
1277 G_SPAWN_STDERR_TO_DEV_NULL |
1278 G_SPAWN_DO_NOT_REAP_CHILD,
1279 NULL, NULL, /* No setup function */
1282 g_child_watch_add (pid, update_program_done, NULL);
1285 /* If we get an error at this point, it's quite likely the user doesn't
1286 * have an installed copy of either 'update-mime-database' or
1287 * 'update-desktop-database'. I don't think we want to popup an error
1288 * dialog at this point, so we just do a g_warning to give the user a
1289 * chance of debugging it.
1291 g_warning ("%s", error->message);
1298 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1299 const char *extension,
1302 char *filename, *basename, *mimetype;
1306 dirname = ensure_dir (MIMETYPE_DIR, error);
1310 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1311 filename = g_build_filename (dirname, basename, NULL);
1315 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1317 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1322 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1323 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1324 " <mime-type type=\"%s\">\n"
1325 " <comment>%s document</comment>\n"
1326 " <glob pattern=\"*.%s\"/>\n"
1328 "</mime-info>\n", mimetype, extension, extension);
1330 g_file_set_contents (filename, contents, -1, NULL);
1333 run_update_command ("update-mime-database", "mime");
1337 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1347 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1348 const char *content_type,
1351 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1353 return update_default_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1357 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1363 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1364 const char *content_type,
1367 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1369 return update_default_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1373 * g_app_info_create_from_commandline:
1374 * @commandline: the commandline to use
1375 * @application_name: the application name, or %NULL to use @commandline
1376 * @flags: flags that can specify details of the created #GAppInfo
1377 * @error: a #GError location to store the error occuring, %NULL to ignore.
1379 * Creates a new #GAppInfo from the given information.
1381 * Returns: new #GAppInfo for given command.
1384 g_app_info_create_from_commandline (const char *commandline,
1385 const char *application_name,
1386 GAppInfoCreateFlags flags,
1392 char *basename, *exec, *filename, *comment;
1393 char *data, *desktop_id;
1396 GDesktopAppInfo *info;
1399 dirname = ensure_dir (APP_DIR, error);
1403 key_file = g_key_file_new ();
1405 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1406 "Encoding", "UTF-8");
1407 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1408 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1409 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1410 G_KEY_FILE_DESKTOP_KEY_TYPE,
1411 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1412 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1413 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1414 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1416 exec = g_strconcat (commandline, " %f", NULL);
1417 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1418 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1421 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1422 split = g_strsplit (commandline, " ", 2);
1423 basename = g_path_get_basename (split[0]);
1425 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1426 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1428 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1429 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1430 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1433 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1434 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1436 data = g_key_file_to_data (key_file, &data_size, NULL);
1437 g_key_file_free (key_file);
1439 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1441 filename = g_build_filename (dirname, desktop_id, NULL);
1442 g_free (desktop_id);
1445 fd = g_mkstemp (filename);
1450 display_name = g_filename_display_name (filename);
1451 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1452 _("Can't create user desktop file %s"), display_name);
1453 g_free (display_name);
1459 desktop_id = g_path_get_basename (filename);
1463 res = g_file_set_contents (filename, data, data_size, error);
1466 g_free (desktop_id);
1471 run_update_command ("update-desktop-database", "applications");
1473 info = g_desktop_app_info_new_from_filename (filename);
1476 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1477 _("Can't load just created desktop file"));
1479 info->desktop_id = g_strdup (desktop_id);
1481 g_free (desktop_id);
1483 return G_APP_INFO (info);
1488 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1490 iface->dup = g_desktop_app_info_dup;
1491 iface->equal = g_desktop_app_info_equal;
1492 iface->get_id = g_desktop_app_info_get_id;
1493 iface->get_name = g_desktop_app_info_get_name;
1494 iface->get_description = g_desktop_app_info_get_description;
1495 iface->get_executable = g_desktop_app_info_get_executable;
1496 iface->get_icon = g_desktop_app_info_get_icon;
1497 iface->launch = g_desktop_app_info_launch;
1498 iface->supports_uris = g_desktop_app_info_supports_uris;
1499 iface->supports_files = g_desktop_app_info_supports_files;
1500 iface->launch_uris = g_desktop_app_info_launch_uris;
1501 iface->should_show = g_desktop_app_info_should_show;
1502 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1503 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1504 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1505 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1506 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1510 app_info_in_list (GAppInfo *info,
1513 while (list != NULL)
1515 if (g_app_info_equal (info, list->data))
1524 * g_app_info_get_all_for_type:
1525 * @content_type: the content type to find a #GAppInfo for
1527 * Gets a list of all #GAppInfo s for a given content type.
1529 * Returns: #GList of #GAppInfo s for given @content_type
1530 * or %NULL on error.
1533 g_app_info_get_all_for_type (const char *content_type)
1535 GList *desktop_entries, *l;
1537 GDesktopAppInfo *info;
1539 g_return_val_if_fail (content_type != NULL, NULL);
1541 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1544 for (l = desktop_entries; l != NULL; l = l->next)
1546 char *desktop_entry = l->data;
1548 info = g_desktop_app_info_new (desktop_entry);
1551 if (app_info_in_list (G_APP_INFO (info), infos))
1552 g_object_unref (info);
1554 infos = g_list_prepend (infos, info);
1556 g_free (desktop_entry);
1559 g_list_free (desktop_entries);
1561 return g_list_reverse (infos);
1566 * g_app_info_get_default_for_type:
1567 * @content_type: the content type to find a #GAppInfo for
1568 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1571 * Gets the #GAppInfo that correspond to a given content type.
1573 * Returns: #GAppInfo for given @content_type or %NULL on error.
1576 g_app_info_get_default_for_type (const char *content_type,
1577 gboolean must_support_uris)
1579 GList *desktop_entries, *l;
1582 g_return_val_if_fail (content_type != NULL, NULL);
1584 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1587 for (l = desktop_entries; l != NULL; l = l->next)
1589 char *desktop_entry = l->data;
1591 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1594 if (must_support_uris && !g_app_info_supports_uris (info))
1596 g_object_unref (info);
1604 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1605 g_list_free (desktop_entries);
1612 * g_app_info_get_default_for_uri_scheme:
1613 * @uri_scheme: a string containing a URI scheme.
1615 * Gets the default application for launching applications
1616 * using this URI scheme.
1618 * TODO: This is currently unimplemented.
1623 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1625 /* TODO: Implement this using giomodules, reading the gconf settings
1626 * in /desktop/gnome/url-handlers
1633 get_apps_from_dir (GHashTable *apps,
1634 const char *dirname,
1638 const char *basename;
1639 char *filename, *subprefix, *desktop_id;
1641 GDesktopAppInfo *appinfo;
1643 dir = g_dir_open (dirname, 0, NULL);
1646 while ((basename = g_dir_read_name (dir)) != NULL)
1648 filename = g_build_filename (dirname, basename, NULL);
1649 if (g_str_has_suffix (basename, ".desktop"))
1651 desktop_id = g_strconcat (prefix, basename, NULL);
1653 /* Use _extended so we catch NULLs too (hidden) */
1654 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1656 appinfo = g_desktop_app_info_new_from_filename (filename);
1658 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1660 g_object_unref (appinfo);
1665 if (appinfo || hidden)
1667 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1671 /* Reuse instead of strdup here */
1672 appinfo->desktop_id = desktop_id;
1677 g_free (desktop_id);
1681 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1683 subprefix = g_strconcat (prefix, basename, "-", NULL);
1684 get_apps_from_dir (apps, filename, subprefix);
1696 * g_app_info_get_all:
1698 * Gets a list of all of the applications currently registered
1701 * For desktop files, this includes applications that have
1702 * <literal>NoDisplay=true</literal> set or are excluded from
1703 * display by means of <literal>OnlyShowIn</literal> or
1704 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1705 * The returned list does not include applications which have
1706 * the <literal>Hidden</literal> key set.
1708 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1711 g_app_info_get_all (void)
1713 const char * const *dirs;
1715 GHashTableIter iter;
1720 dirs = get_applications_search_path ();
1722 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1726 for (i = 0; dirs[i] != NULL; i++)
1727 get_apps_from_dir (apps, dirs[i], "");
1731 g_hash_table_iter_init (&iter, apps);
1732 while (g_hash_table_iter_next (&iter, NULL, &value))
1735 infos = g_list_prepend (infos, value);
1738 g_hash_table_destroy (apps);
1740 return g_list_reverse (infos);
1743 /* Cacheing of mimeinfo.cache and defaults.list files */
1747 GHashTable *mime_info_cache_map;
1748 GHashTable *defaults_list_map;
1749 GHashTable *defaults_list_removed_map;
1750 time_t mime_info_cache_timestamp;
1751 time_t defaults_list_timestamp;
1755 GList *dirs; /* mimeinfo.cache and defaults.list */
1756 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1757 time_t last_stat_time;
1758 guint should_ping_mime_monitor : 1;
1761 static MimeInfoCache *mime_info_cache = NULL;
1762 G_LOCK_DEFINE_STATIC (mime_info_cache);
1764 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1765 const char *mime_type,
1766 char **new_desktop_file_ids);
1768 static MimeInfoCache * mime_info_cache_new (void);
1771 destroy_info_cache_value (gpointer key,
1775 g_list_foreach (value, (GFunc)g_free, NULL);
1776 g_list_free (value);
1780 destroy_info_cache_map (GHashTable *info_cache_map)
1782 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1783 g_hash_table_destroy (info_cache_map);
1787 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1788 const char *cache_file,
1794 filename = g_build_filename (dir->path, cache_file, NULL);
1796 if (g_stat (filename, &buf) < 0)
1803 if (buf.st_mtime != *timestamp)
1809 /* Call with lock held */
1811 remove_all (gpointer key,
1820 mime_info_cache_blow_global_cache (void)
1822 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1827 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1831 gchar *filename, **mime_types;
1838 if (dir->mime_info_cache_map != NULL &&
1839 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1840 &dir->mime_info_cache_timestamp))
1843 if (dir->mime_info_cache_map != NULL)
1844 destroy_info_cache_map (dir->mime_info_cache_map);
1846 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1847 (GDestroyNotify) g_free,
1850 key_file = g_key_file_new ();
1852 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1854 if (g_stat (filename, &buf) < 0)
1857 if (dir->mime_info_cache_timestamp > 0)
1858 mime_info_cache->should_ping_mime_monitor = TRUE;
1860 dir->mime_info_cache_timestamp = buf.st_mtime;
1862 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1867 if (load_error != NULL)
1870 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1873 if (load_error != NULL)
1876 for (i = 0; mime_types[i] != NULL; i++)
1878 gchar **desktop_file_ids;
1879 char *unaliased_type;
1880 desktop_file_ids = g_key_file_get_string_list (key_file,
1886 if (desktop_file_ids == NULL)
1889 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1890 mime_info_cache_dir_add_desktop_entries (dir,
1893 g_free (unaliased_type);
1895 g_strfreev (desktop_file_ids);
1898 g_strfreev (mime_types);
1899 g_key_file_free (key_file);
1904 g_key_file_free (key_file);
1906 if (mime_types != NULL)
1907 g_strfreev (mime_types);
1910 g_error_free (load_error);
1914 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1918 gchar *filename, **mime_types;
1919 char *unaliased_type;
1920 char **desktop_file_ids;
1927 if (dir->defaults_list_map != NULL &&
1928 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1929 &dir->defaults_list_timestamp))
1932 if (dir->defaults_list_map != NULL)
1933 g_hash_table_destroy (dir->defaults_list_map);
1934 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1935 g_free, (GDestroyNotify)g_strfreev);
1937 if (dir->defaults_list_removed_map != NULL)
1938 g_hash_table_destroy (dir->defaults_list_removed_map);
1939 dir->defaults_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1940 g_free, (GDestroyNotify)g_strfreev);
1942 key_file = g_key_file_new ();
1944 filename = g_build_filename (dir->path, "defaults.list", NULL);
1945 if (g_stat (filename, &buf) < 0)
1948 if (dir->defaults_list_timestamp > 0)
1949 mime_info_cache->should_ping_mime_monitor = TRUE;
1951 dir->defaults_list_timestamp = buf.st_mtime;
1953 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1957 if (load_error != NULL)
1960 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1962 if (mime_types != NULL)
1964 for (i = 0; mime_types[i] != NULL; i++)
1966 desktop_file_ids = g_key_file_get_string_list (key_file,
1967 DEFAULT_APPLICATIONS_GROUP,
1971 if (desktop_file_ids == NULL)
1974 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1975 g_hash_table_replace (dir->defaults_list_map,
1980 g_strfreev (mime_types);
1983 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
1985 if (mime_types != NULL)
1987 for (i = 0; mime_types[i] != NULL; i++)
1989 desktop_file_ids = g_key_file_get_string_list (key_file,
1990 REMOVED_ASSOCIATIONS_GROUP,
1994 if (desktop_file_ids == NULL)
1997 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1998 g_hash_table_replace (dir->defaults_list_removed_map,
2003 g_strfreev (mime_types);
2006 g_key_file_free (key_file);
2011 g_key_file_free (key_file);
2013 if (mime_types != NULL)
2014 g_strfreev (mime_types);
2017 g_error_free (load_error);
2020 static MimeInfoCacheDir *
2021 mime_info_cache_dir_new (const char *path)
2023 MimeInfoCacheDir *dir;
2025 dir = g_new0 (MimeInfoCacheDir, 1);
2026 dir->path = g_strdup (path);
2032 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2037 if (dir->mime_info_cache_map != NULL)
2039 destroy_info_cache_map (dir->mime_info_cache_map);
2040 dir->mime_info_cache_map = NULL;
2044 if (dir->defaults_list_map != NULL)
2046 g_hash_table_destroy (dir->defaults_list_map);
2047 dir->defaults_list_map = NULL;
2050 if (dir->defaults_list_removed_map != NULL)
2052 g_hash_table_destroy (dir->defaults_list_removed_map);
2053 dir->defaults_list_removed_map = NULL;
2060 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2061 const char *mime_type,
2062 char **new_desktop_file_ids)
2064 GList *desktop_file_ids;
2067 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2070 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2072 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2073 desktop_file_ids = g_list_append (desktop_file_ids,
2074 g_strdup (new_desktop_file_ids[i]));
2077 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2081 mime_info_cache_init_dir_lists (void)
2083 const char * const *dirs;
2086 mime_info_cache = mime_info_cache_new ();
2088 dirs = get_applications_search_path ();
2090 for (i = 0; dirs[i] != NULL; i++)
2092 MimeInfoCacheDir *dir;
2094 dir = mime_info_cache_dir_new (dirs[i]);
2098 mime_info_cache_dir_init (dir);
2099 mime_info_cache_dir_init_defaults_list (dir);
2101 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2107 mime_info_cache_update_dir_lists (void)
2111 tmp = mime_info_cache->dirs;
2115 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2117 /* No need to do this if we had file monitors... */
2118 mime_info_cache_blow_global_cache ();
2119 mime_info_cache_dir_init (dir);
2120 mime_info_cache_dir_init_defaults_list (dir);
2127 mime_info_cache_init (void)
2129 G_LOCK (mime_info_cache);
2130 if (mime_info_cache == NULL)
2131 mime_info_cache_init_dir_lists ();
2137 if (now >= mime_info_cache->last_stat_time + 10)
2139 mime_info_cache_update_dir_lists ();
2140 mime_info_cache->last_stat_time = now;
2144 if (mime_info_cache->should_ping_mime_monitor)
2146 /* g_idle_add (emit_mime_changed, NULL); */
2147 mime_info_cache->should_ping_mime_monitor = FALSE;
2150 G_UNLOCK (mime_info_cache);
2153 static MimeInfoCache *
2154 mime_info_cache_new (void)
2156 MimeInfoCache *cache;
2158 cache = g_new0 (MimeInfoCache, 1);
2160 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2161 (GDestroyNotify) g_free,
2162 (GDestroyNotify) g_free);
2167 mime_info_cache_free (MimeInfoCache *cache)
2172 g_list_foreach (cache->dirs,
2173 (GFunc) mime_info_cache_dir_free,
2175 g_list_free (cache->dirs);
2176 g_hash_table_destroy (cache->global_defaults_cache);
2181 * mime_info_cache_reload:
2182 * @dir: directory path which needs reloading.
2184 * Reload the mime information for the @dir.
2187 mime_info_cache_reload (const char *dir)
2189 /* FIXME: just reload the dir that needs reloading,
2190 * don't blow the whole cache
2192 if (mime_info_cache != NULL)
2194 G_LOCK (mime_info_cache);
2195 mime_info_cache_free (mime_info_cache);
2196 mime_info_cache = NULL;
2197 G_UNLOCK (mime_info_cache);
2202 append_desktop_entry (GList *list,
2203 const char *desktop_entry,
2204 GList *removed_entries)
2206 /* Add if not already in list, and valid */
2207 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2208 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2209 list = g_list_prepend (list, g_strdup (desktop_entry));
2215 * get_all_desktop_entries_for_mime_type:
2216 * @mime_type: a mime type.
2218 * Returns all the desktop ids for @mime_type. The desktop files
2219 * are listed in an order so that default applications are listed before
2220 * non-default ones, and handlers for inherited mimetypes are listed
2221 * after the base ones.
2223 * Return value: a #GList containing the desktop ids which claim
2224 * to handle @mime_type.
2227 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2229 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2230 MimeInfoCacheDir *dir;
2233 char **default_entries;
2234 char **removed_associations;
2237 mime_info_cache_init ();
2239 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2240 G_LOCK (mime_info_cache);
2242 removed_entries = NULL;
2243 desktop_entries = NULL;
2244 for (i = 0; mime_types[i] != NULL; i++)
2246 mime_type = mime_types[i];
2248 /* Go through all apps listed as defaults */
2249 for (dir_list = mime_info_cache->dirs;
2251 dir_list = dir_list->next)
2253 dir = dir_list->data;
2254 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2255 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2256 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2258 removed_associations = g_hash_table_lookup (dir->defaults_list_removed_map, mime_type);
2259 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2260 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2263 /* Go through all entries that support the mimetype */
2264 for (dir_list = mime_info_cache->dirs;
2266 dir_list = dir_list->next)
2268 dir = dir_list->data;
2270 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2271 for (tmp = list; tmp != NULL; tmp = tmp->next)
2272 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2276 G_UNLOCK (mime_info_cache);
2278 g_strfreev (mime_types);
2280 g_list_free (removed_entries);
2282 desktop_entries = g_list_reverse (desktop_entries);
2284 return desktop_entries;
2287 #define __G_DESKTOP_APP_INFO_C__
2288 #include "gioaliasdef.c"