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 MIME_CACHE_GROUP "MIME Cache"
58 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
60 static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
61 static void mime_info_cache_reload (const char *dir);
66 * Information about an installed application from a desktop file.
68 struct _GDesktopAppInfo
70 GObject parent_instance;
76 /* FIXME: what about GenericName ? */
90 guint startup_notify : 1;
91 /* FIXME: what about StartupWMClass ? */
94 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
95 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
96 g_desktop_app_info_iface_init))
99 search_path_init (gpointer data)
102 const char * const *data_dirs;
103 const char *user_data_dir;
106 data_dirs = g_get_system_data_dirs ();
107 length = g_strv_length ((char **) data_dirs);
109 args = g_new (char *, length + 2);
112 user_data_dir = g_get_user_data_dir ();
113 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
114 for (i = 0; i < length; i++)
115 args[j++] = g_build_filename (data_dirs[i],
116 "applications", NULL);
122 static const char * const *
123 get_applications_search_path (void)
125 static GOnce once_init = G_ONCE_INIT;
126 return g_once (&once_init, search_path_init, NULL);
130 g_desktop_app_info_finalize (GObject *object)
132 GDesktopAppInfo *info;
134 info = G_DESKTOP_APP_INFO (object);
136 g_free (info->desktop_id);
137 g_free (info->filename);
139 g_free (info->comment);
140 g_free (info->icon_name);
142 g_object_unref (info->icon);
143 g_strfreev (info->only_show_in);
144 g_strfreev (info->not_show_in);
145 g_free (info->try_exec);
147 g_free (info->binary);
150 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
154 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
156 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
158 gobject_class->finalize = g_desktop_app_info_finalize;
162 g_desktop_app_info_init (GDesktopAppInfo *local)
167 * g_desktop_app_info_new_from_filename:
168 * @filename: a string containing a file name.
170 * Creates a new #GDesktopAppInfo.
172 * Returns: a new #GDesktopAppInfo or %NULL on error.
175 g_desktop_app_info_new_from_filename (const char *filename)
177 GDesktopAppInfo *info;
183 key_file = g_key_file_new ();
185 if (!g_key_file_load_from_file (key_file,
191 start_group = g_key_file_get_start_group (key_file);
192 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
194 g_free (start_group);
197 g_free (start_group);
199 type = g_key_file_get_string (key_file,
200 G_KEY_FILE_DESKTOP_GROUP,
201 G_KEY_FILE_DESKTOP_KEY_TYPE,
203 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
210 try_exec = g_key_file_get_string (key_file,
211 G_KEY_FILE_DESKTOP_GROUP,
212 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
217 t = g_find_program_in_path (try_exec);
226 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
227 info->filename = g_strdup (filename);
229 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
230 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
231 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
232 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
233 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);
234 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);
235 info->try_exec = try_exec;
236 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
237 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
238 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
239 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
240 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
245 if (g_path_is_absolute (info->icon_name))
249 file = g_file_new_for_path (info->icon_name);
250 info->icon = g_file_icon_new (file);
251 g_object_unref (file);
254 info->icon = g_themed_icon_new (info->icon_name);
265 while (*p != ' ' && *p != 0)
268 info->binary = g_strndup (start, p - start);
275 * g_desktop_app_info_new:
276 * @desktop_id: the desktop file id
278 * Creates a new #GDesktopAppInfo.
280 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
283 g_desktop_app_info_new (const char *desktop_id)
285 GDesktopAppInfo *appinfo;
286 const char * const *dirs;
289 dirs = get_applications_search_path ();
291 for (i = 0; dirs[i] != NULL; i++)
297 filename = g_build_filename (dirs[i], desktop_id, NULL);
298 appinfo = g_desktop_app_info_new_from_filename (filename);
305 basename = g_strdup (desktop_id);
307 while ((p = strchr (p, '-')) != NULL)
311 filename = g_build_filename (dirs[i], basename, NULL);
312 appinfo = g_desktop_app_info_new_from_filename (filename);
327 appinfo->desktop_id = g_strdup (desktop_id);
329 if (g_desktop_app_info_get_is_hidden (appinfo))
331 g_object_unref (appinfo);
339 g_desktop_app_info_dup (GAppInfo *appinfo)
341 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
342 GDesktopAppInfo *new_info;
344 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
346 new_info->filename = g_strdup (info->filename);
347 new_info->desktop_id = g_strdup (info->desktop_id);
349 new_info->name = g_strdup (info->name);
350 new_info->comment = g_strdup (info->comment);
351 new_info->nodisplay = info->nodisplay;
352 new_info->icon_name = g_strdup (info->icon_name);
353 new_info->icon = g_object_ref (info->icon);
354 new_info->only_show_in = g_strdupv (info->only_show_in);
355 new_info->not_show_in = g_strdupv (info->not_show_in);
356 new_info->try_exec = g_strdup (info->try_exec);
357 new_info->exec = g_strdup (info->exec);
358 new_info->binary = g_strdup (info->binary);
359 new_info->path = g_strdup (info->path);
360 new_info->hidden = info->hidden;
361 new_info->terminal = info->terminal;
362 new_info->startup_notify = info->startup_notify;
364 return G_APP_INFO (new_info);
368 g_desktop_app_info_equal (GAppInfo *appinfo1,
371 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
372 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
374 if (info1->desktop_id == NULL ||
375 info2->desktop_id == NULL)
378 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
382 g_desktop_app_info_get_id (GAppInfo *appinfo)
384 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
386 return info->desktop_id;
390 g_desktop_app_info_get_name (GAppInfo *appinfo)
392 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
394 if (info->name == NULL)
400 * g_desktop_app_info_get_is_hidden:
401 * @info: a #GDesktopAppInfo.
403 * A desktop file is hidden if the Hidden key in it is
406 * Returns: %TRUE if hidden, %FALSE otherwise.
409 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
415 g_desktop_app_info_get_description (GAppInfo *appinfo)
417 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
419 return info->comment;
423 g_desktop_app_info_get_executable (GAppInfo *appinfo)
425 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
431 g_desktop_app_info_get_icon (GAppInfo *appinfo)
433 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
439 expand_macro_single (char macro, GFile *file)
444 path = g_file_get_path (file);
445 uri = g_file_get_uri (file);
451 result = g_shell_quote (uri);
456 result = g_shell_quote (path);
461 result = g_shell_quote (g_path_get_dirname (path));
466 result = g_shell_quote (g_path_get_basename (path));
477 expand_macro (char macro,
479 GDesktopAppInfo *info,
482 GList *files = *file_list;
485 g_return_if_fail (exec != NULL);
495 expanded = expand_macro_single (macro, files->data);
498 g_string_append (exec, expanded);
512 expanded = expand_macro_single (macro, files->data);
515 g_string_append (exec, expanded);
521 if (files != NULL && expanded)
522 g_string_append_c (exec, ' ');
530 g_string_append (exec, "--icon ");
531 g_string_append (exec, info->icon_name);
537 g_string_append (exec, info->name);
542 g_string_append (exec, info->filename);
545 case 'm': /* deprecated */
549 g_string_append_c (exec, '%');
557 expand_application_parameters (GDesktopAppInfo *info,
563 GList *file_list = *files;
564 const char *p = info->exec;
565 GString *expanded_exec = g_string_new (NULL);
568 if (info->exec == NULL)
570 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
571 _("Desktop file didn't specify Exec field"));
577 if (p[0] == '%' && p[1] != '\0')
579 expand_macro (p[1], expanded_exec, info, files);
583 g_string_append_c (expanded_exec, *p);
588 /* No file substitutions */
589 if (file_list == *files && file_list != NULL)
591 /* If there is no macro default to %f. This is also what KDE does */
592 g_string_append_c (expanded_exec, ' ');
593 expand_macro ('f', expanded_exec, info, files);
596 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
597 g_string_free (expanded_exec, TRUE);
602 prepend_terminal_to_vector (int *argc,
609 char **term_argv = NULL;
614 g_return_val_if_fail (argc != NULL, FALSE);
615 g_return_val_if_fail (argv != NULL, FALSE);
623 /* compute size if not given */
626 for (i = 0; the_argv[i] != NULL; i++)
632 term_argv = g_new0 (char *, 3);
634 check = g_find_program_in_path ("gnome-terminal");
637 term_argv[0] = check;
638 /* Note that gnome-terminal takes -x and
639 * as -e in gnome-terminal is broken we use that. */
640 term_argv[1] = g_strdup ("-x");
645 check = g_find_program_in_path ("nxterm");
647 check = g_find_program_in_path ("color-xterm");
649 check = g_find_program_in_path ("rxvt");
651 check = g_find_program_in_path ("xterm");
653 check = g_find_program_in_path ("dtterm");
656 check = g_strdup ("xterm");
657 g_warning ("couldn't find a terminal, falling back to xterm");
659 term_argv[0] = check;
660 term_argv[1] = g_strdup ("-e");
663 real_argc = term_argc + *argc;
664 real_argv = g_new (char *, real_argc + 1);
666 for (i = 0; i < term_argc; i++)
667 real_argv[i] = term_argv[i];
669 for (j = 0; j < *argc; j++, i++)
670 real_argv[i] = (char *)the_argv[j];
678 /* we use g_free here as we sucked all the inner strings
679 * out from it into real_argv */
684 #endif /* G_OS_WIN32 */
687 /* '=' is the new '\0'.
688 * DO NOT CALL unless at least one string ends with '='
691 is_env (const char *a,
696 if (*a == 0 || *b == 0)
709 /* free with g_strfreev */
711 replace_env_var (char **old_environ,
713 const char *new_value)
715 int length, new_length;
716 int index, new_index;
720 /* do two things at once:
721 * - discover the length of the environment ('length')
722 * - find the location (if any) of the env var ('index')
725 for (length = 0; old_environ[length]; length++)
727 /* if we already have it in our environment, replace */
728 if (is_env (old_environ[length], env_var))
733 /* no current env var, no desired env value.
736 if (new_value == NULL && index == -1)
739 /* in all cases now, we will be using a modified environment.
740 * determine its length and allocated it.
743 * new_index = location to insert, if any
744 * new_length = length of the new array
745 * new_environ = the pointer array for the new environment
748 if (new_value == NULL && index >= 0)
750 /* in this case, we will be removing an entry */
751 new_length = length - 1;
754 else if (new_value != NULL && index < 0)
756 /* in this case, we will be adding an entry to the end */
757 new_length = length + 1;
761 /* in this case, we will be replacing the existing entry */
767 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
768 new_environ[new_length] = NULL;
770 /* now we do the copying.
771 * for each entry in the new environment, we decide what to do
775 for (new_i = 0; new_i < new_length; new_i++)
777 if (new_i == new_index)
779 /* insert our new item */
780 new_environ[new_i] = g_strconcat (env_var,
785 /* if we had an old entry, skip it now */
791 /* if this is the old DESKTOP_STARTUP_ID, skip it */
795 /* copy an old item */
796 new_environ[new_i] = g_strdup (old_environ[i]);
801 g_strfreev (old_environ);
807 dup_list_segment (GList *start,
813 while (start != NULL && start != end)
815 res = g_list_prepend (res, start->data);
819 return g_list_reverse (res);
822 #ifdef HAVE__NSGETENVIRON
823 #define environ (*_NSGetEnviron())
824 #elif !defined(G_OS_WIN32)
826 /* According to the Single Unix Specification, environ is not in
827 * * any system header, although unistd.h often declares it.
829 extern char **environ;
833 g_desktop_app_info_launch (GAppInfo *appinfo,
835 GAppLaunchContext *launch_context,
838 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
839 gboolean completed = FALSE;
841 GList *launched_files;
848 g_return_val_if_fail (appinfo != NULL, FALSE);
856 if (!expand_application_parameters (info, &files,
857 &argc, &argv, error))
860 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
862 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
863 _("Unable to find terminal required for application"));
870 launched_files = dup_list_segment (old_files, files);
872 display = g_app_launch_context_get_display (launch_context,
877 if (info->startup_notify)
878 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
882 if (display || sn_id)
886 envp = g_new0 (char *, 1);
888 envp = g_strdupv (environ);
892 envp = replace_env_var (envp,
897 envp = replace_env_var (envp,
898 "DESKTOP_STARTUP_ID",
904 g_list_free (launched_files);
907 if (!g_spawn_async (info->path, /* working directory */
910 G_SPAWN_SEARCH_PATH /* flags */,
911 NULL /* child_setup */,
913 NULL /* child_pid */,
918 g_app_launch_context_launch_failed (launch_context, sn_id);
932 while (files != NULL);
944 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
946 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
949 ((strstr (info->exec, "%u") != NULL) ||
950 (strstr (info->exec, "%U") != NULL));
954 g_desktop_app_info_supports_files (GAppInfo *appinfo)
956 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
959 ((strstr (info->exec, "%f") != NULL) ||
960 (strstr (info->exec, "%F") != NULL));
964 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
966 GAppLaunchContext *launch_context,
976 file = g_file_new_for_uri (uris->data);
978 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
981 files = g_list_prepend (files, file);
984 files = g_list_reverse (files);
986 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
988 g_list_foreach (files, (GFunc)g_object_unref, NULL);
994 G_LOCK_DEFINE_STATIC (g_desktop_env);
995 static gchar *g_desktop_env = NULL;
998 * g_desktop_app_info_set_desktop_env:
999 * @desktop_env: a string specifying what desktop this is
1001 * Sets the name of the desktop that the application is running in.
1002 * This is used by g_app_info_should_show() to evaluate the
1003 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1004 * desktop entry fields.
1006 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1007 * Menu specification</ulink> recognizes the following:
1009 * <member>GNOME</member>
1010 * <member>KDE</member>
1011 * <member>ROX</member>
1012 * <member>XFCE</member>
1013 * <member>Old</member>
1016 * Should be called only once; subsequent calls are ignored.
1019 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1021 G_LOCK (g_desktop_env);
1023 g_desktop_env = g_strdup (desktop_env);
1024 G_UNLOCK (g_desktop_env);
1028 g_desktop_app_info_should_show (GAppInfo *appinfo)
1030 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1032 const gchar *desktop_env;
1035 if (info->nodisplay)
1038 G_LOCK (g_desktop_env);
1039 desktop_env = g_desktop_env;
1040 G_UNLOCK (g_desktop_env);
1042 if (info->only_show_in)
1044 if (desktop_env == NULL)
1048 for (i = 0; info->only_show_in[i] != NULL; i++)
1050 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1060 if (info->not_show_in && desktop_env)
1062 for (i = 0; info->not_show_in[i] != NULL; i++)
1064 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1078 ensure_dir (DirType type,
1081 char *path, *display_name;
1084 if (type == APP_DIR)
1085 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1087 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1090 if (g_mkdir_with_parents (path, 0700) == 0)
1094 display_name = g_filename_display_name (path);
1095 if (type == APP_DIR)
1096 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1097 _("Can't create user application configuration folder %s: %s"),
1098 display_name, g_strerror (err));
1100 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1101 _("Can't create user MIME configuration folder %s: %s"),
1102 display_name, g_strerror (err));
1104 g_free (display_name);
1111 update_default_list (const char *desktop_id,
1112 const char *content_type,
1116 char *dirname, *filename;
1118 gboolean load_succeeded, res;
1121 gsize length, data_size;
1125 dirname = ensure_dir (APP_DIR, error);
1129 filename = g_build_filename (dirname, "defaults.list", NULL);
1132 key_file = g_key_file_new ();
1133 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1134 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1136 g_key_file_free (key_file);
1137 key_file = g_key_file_new ();
1141 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1142 content_type, &length, NULL);
1144 list = g_new (char *, 1 + length + 1);
1148 list[i++] = g_strdup (desktop_id);
1151 for (j = 0; old_list[j] != NULL; j++)
1153 if (strcmp (old_list[j], desktop_id) != 0)
1154 list[i++] = g_strdup (old_list[j]);
1159 g_strfreev (old_list);
1161 g_key_file_set_string_list (key_file,
1162 DEFAULT_APPLICATIONS_GROUP,
1164 (const char * const *)list, i);
1168 data = g_key_file_to_data (key_file, &data_size, error);
1169 g_key_file_free (key_file);
1171 res = g_file_set_contents (filename, data, data_size, error);
1173 mime_info_cache_reload (NULL);
1182 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1183 const char *content_type,
1186 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1188 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1191 return update_default_list (info->desktop_id, content_type, TRUE, error);
1195 update_program_done (GPid pid,
1199 /* Did the application exit correctly */
1200 if (WIFEXITED (status) &&
1201 WEXITSTATUS (status) == 0)
1203 /* Here we could clean out any caches in use */
1208 run_update_command (char *command,
1217 GError *error = NULL;
1220 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1222 if (g_spawn_async ("/", argv,
1224 G_SPAWN_SEARCH_PATH |
1225 G_SPAWN_STDOUT_TO_DEV_NULL |
1226 G_SPAWN_STDERR_TO_DEV_NULL |
1227 G_SPAWN_DO_NOT_REAP_CHILD,
1228 NULL, NULL, /* No setup function */
1231 g_child_watch_add (pid, update_program_done, NULL);
1234 /* If we get an error at this point, it's quite likely the user doesn't
1235 * have an installed copy of either 'update-mime-database' or
1236 * 'update-desktop-database'. I don't think we want to popup an error
1237 * dialog at this point, so we just do a g_warning to give the user a
1238 * chance of debugging it.
1240 g_warning ("%s", error->message);
1247 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1248 const char *extension,
1251 char *filename, *basename, *mimetype;
1255 dirname = ensure_dir (MIMETYPE_DIR, error);
1259 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1260 filename = g_build_filename (dirname, basename, NULL);
1264 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1266 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1271 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1272 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1273 " <mime-type type=\"%s\">\n"
1274 " <comment>%s document</comment>\n"
1275 " <glob pattern=\"*.%s\"/>\n"
1277 "</mime-info>\n", mimetype, extension, extension);
1279 g_file_set_contents (filename, contents, -1, NULL);
1282 run_update_command ("update-mime-database", "mime");
1286 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1296 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1297 const char *content_type,
1300 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1302 char *new_mimetypes, *old_mimetypes, *content;
1306 keyfile = g_key_file_new ();
1307 if (!g_key_file_load_from_file (keyfile, info->filename,
1308 G_KEY_FILE_KEEP_COMMENTS |
1309 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1311 g_key_file_free (keyfile);
1315 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1316 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1317 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1318 g_free (old_mimetypes);
1319 g_free (new_mimetypes);
1321 content = g_key_file_to_data (keyfile, NULL, NULL);
1322 g_key_file_free (keyfile);
1324 dirname = ensure_dir (APP_DIR, error);
1331 filename = g_build_filename (dirname, info->desktop_id, NULL);
1334 if (!g_file_set_contents (filename, content, -1, error))
1343 run_update_command ("update-desktop-database", "applications");
1348 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1350 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1353 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1354 return g_str_has_prefix (info->filename, user_dirname);
1358 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1359 const char *content_type,
1362 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1364 char *new_mimetypes, *old_mimetypes, *content;
1369 keyfile = g_key_file_new ();
1370 if (!g_key_file_load_from_file (keyfile, info->filename,
1371 G_KEY_FILE_KEEP_COMMENTS |
1372 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1374 g_key_file_free (keyfile);
1378 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1379 new_mimetypes = g_strdup (old_mimetypes);
1382 found = strstr (new_mimetypes, content_type);
1383 if (found && *(found + strlen (content_type)) == ';')
1385 char *rest = found + strlen (content_type) + 1;
1386 memmove (found, rest, strlen (rest) + 1);
1388 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1389 g_free (old_mimetypes);
1390 g_free (new_mimetypes);
1392 content = g_key_file_to_data (keyfile, NULL, NULL);
1393 g_key_file_free (keyfile);
1395 dirname = ensure_dir (APP_DIR, error);
1402 filename = g_build_filename (dirname, info->desktop_id, NULL);
1404 if (!g_file_set_contents (filename, content, -1, error))
1413 run_update_command ("update-desktop-database", "applications");
1415 return update_default_list (info->desktop_id, content_type, FALSE, error);
1419 * g_app_info_create_from_commandline:
1420 * @commandline: the commandline to use
1421 * @application_name: the application name, or %NULL to use @commandline
1422 * @flags: flags that can specify details of the created #GAppInfo
1423 * @error: a #GError location to store the error occuring, %NULL to ignore.
1425 * Creates a new #GAppInfo from the given information.
1427 * Returns: new #GAppInfo for given command.
1430 g_app_info_create_from_commandline (const char *commandline,
1431 const char *application_name,
1432 GAppInfoCreateFlags flags,
1438 char *basename, *exec, *filename, *comment;
1439 char *data, *desktop_id;
1442 GDesktopAppInfo *info;
1445 dirname = ensure_dir (APP_DIR, error);
1449 key_file = g_key_file_new ();
1451 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1452 "Encoding", "UTF-8");
1453 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1454 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1455 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1456 G_KEY_FILE_DESKTOP_KEY_TYPE,
1457 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1458 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1459 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1460 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1462 exec = g_strconcat (commandline, " %f", NULL);
1463 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1464 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1467 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1468 split = g_strsplit (commandline, " ", 2);
1469 basename = g_path_get_basename (split[0]);
1471 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1472 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1474 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1475 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1476 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1479 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1480 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1482 data = g_key_file_to_data (key_file, &data_size, NULL);
1483 g_key_file_free (key_file);
1485 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1487 filename = g_build_filename (dirname, desktop_id, NULL);
1488 g_free (desktop_id);
1491 fd = g_mkstemp (filename);
1496 display_name = g_filename_display_name (filename);
1497 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1498 _("Can't create user desktop file %s"), display_name);
1499 g_free (display_name);
1505 desktop_id = g_path_get_basename (filename);
1509 res = g_file_set_contents (filename, data, data_size, error);
1512 g_free (desktop_id);
1517 run_update_command ("update-desktop-database", "applications");
1519 info = g_desktop_app_info_new_from_filename (filename);
1522 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1523 _("Can't load just created desktop file"));
1525 info->desktop_id = g_strdup (desktop_id);
1527 g_free (desktop_id);
1529 return G_APP_INFO (info);
1534 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1536 iface->dup = g_desktop_app_info_dup;
1537 iface->equal = g_desktop_app_info_equal;
1538 iface->get_id = g_desktop_app_info_get_id;
1539 iface->get_name = g_desktop_app_info_get_name;
1540 iface->get_description = g_desktop_app_info_get_description;
1541 iface->get_executable = g_desktop_app_info_get_executable;
1542 iface->get_icon = g_desktop_app_info_get_icon;
1543 iface->launch = g_desktop_app_info_launch;
1544 iface->supports_uris = g_desktop_app_info_supports_uris;
1545 iface->supports_files = g_desktop_app_info_supports_files;
1546 iface->launch_uris = g_desktop_app_info_launch_uris;
1547 iface->should_show = g_desktop_app_info_should_show;
1548 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1549 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1550 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1551 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1552 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1556 app_info_in_list (GAppInfo *info,
1559 while (list != NULL)
1561 if (g_app_info_equal (info, list->data))
1570 * g_app_info_get_all_for_type:
1571 * @content_type: the content type to find a #GAppInfo for
1573 * Gets a list of all #GAppInfo s for a given content type.
1575 * Returns: #GList of #GAppInfo s for given @content_type.
1578 g_app_info_get_all_for_type (const char *content_type)
1580 GList *desktop_entries, *l;
1582 GDesktopAppInfo *info;
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 = g_desktop_app_info_new (desktop_entry);
1594 if (app_info_in_list (G_APP_INFO (info), infos))
1595 g_object_unref (info);
1597 infos = g_list_prepend (infos, info);
1599 g_free (desktop_entry);
1602 g_list_free (desktop_entries);
1604 return g_list_reverse (infos);
1609 * g_app_info_get_default_for_type:
1610 * @content_type: the content type to find a #GAppInfo for
1611 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1614 * Gets the #GAppInfo that correspond to a given content type.
1616 * Returns: #GAppInfo for given @content_type.
1619 g_app_info_get_default_for_type (const char *content_type,
1620 gboolean must_support_uris)
1622 GList *desktop_entries, *l;
1625 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1628 for (l = desktop_entries; l != NULL; l = l->next)
1630 char *desktop_entry = l->data;
1632 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1635 if (must_support_uris && !g_app_info_supports_uris (info))
1637 g_object_unref (info);
1645 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1646 g_list_free (desktop_entries);
1653 * g_app_info_get_default_for_uri_scheme:
1654 * @uri_scheme: a string containing a URI scheme.
1656 * Gets the default application for launching applications
1657 * using this URI scheme.
1659 * TODO: This is currently unimplemented.
1664 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1666 /* TODO: Implement this using giomodules, reading the gconf settings
1667 * in /desktop/gnome/url-handlers
1674 get_apps_from_dir (GHashTable *apps,
1675 const char *dirname,
1679 const char *basename;
1680 char *filename, *subprefix, *desktop_id;
1682 GDesktopAppInfo *appinfo;
1684 dir = g_dir_open (dirname, 0, NULL);
1687 while ((basename = g_dir_read_name (dir)) != NULL)
1689 filename = g_build_filename (dirname, basename, NULL);
1690 if (g_str_has_suffix (basename, ".desktop"))
1692 desktop_id = g_strconcat (prefix, basename, NULL);
1694 /* Use _extended so we catch NULLs too (hidden) */
1695 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1697 appinfo = g_desktop_app_info_new_from_filename (filename);
1699 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1701 g_object_unref (appinfo);
1706 if (appinfo || hidden)
1708 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1712 /* Reuse instead of strdup here */
1713 appinfo->desktop_id = desktop_id;
1718 g_free (desktop_id);
1722 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1724 subprefix = g_strconcat (prefix, basename, "-", NULL);
1725 get_apps_from_dir (apps, filename, subprefix);
1737 * g_app_info_get_all:
1739 * Gets a list of all of the applications currently registered
1742 * For desktop files, this includes applications that have
1743 * <literal>NoDisplay=true</literal> set or are excluded from
1744 * display by means of <literal>OnlyShowIn</literal> or
1745 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1746 * The returned list does not include applications which have
1747 * the <literal>Hidden</literal> key set.
1749 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1752 g_app_info_get_all (void)
1754 const char * const *dirs;
1756 GHashTableIter iter;
1761 dirs = get_applications_search_path ();
1763 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1767 for (i = 0; dirs[i] != NULL; i++)
1768 get_apps_from_dir (apps, dirs[i], "");
1772 g_hash_table_iter_init (&iter, apps);
1773 while (g_hash_table_iter_next (&iter, NULL, &value))
1776 infos = g_list_prepend (infos, value);
1779 g_hash_table_destroy (apps);
1781 return g_list_reverse (infos);
1784 /* Cacheing of mimeinfo.cache and defaults.list files */
1788 GHashTable *mime_info_cache_map;
1789 GHashTable *defaults_list_map;
1790 time_t mime_info_cache_timestamp;
1791 time_t defaults_list_timestamp;
1795 GList *dirs; /* mimeinfo.cache and defaults.list */
1796 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1797 time_t last_stat_time;
1798 guint should_ping_mime_monitor : 1;
1801 static MimeInfoCache *mime_info_cache = NULL;
1802 G_LOCK_DEFINE_STATIC (mime_info_cache);
1804 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1805 const char *mime_type,
1806 char **new_desktop_file_ids);
1808 static MimeInfoCache * mime_info_cache_new (void);
1811 destroy_info_cache_value (gpointer key,
1815 g_list_foreach (value, (GFunc)g_free, NULL);
1816 g_list_free (value);
1820 destroy_info_cache_map (GHashTable *info_cache_map)
1822 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1823 g_hash_table_destroy (info_cache_map);
1827 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1828 const char *cache_file,
1834 filename = g_build_filename (dir->path, cache_file, NULL);
1836 if (g_stat (filename, &buf) < 0)
1843 if (buf.st_mtime != *timestamp)
1849 /* Call with lock held */
1851 remove_all (gpointer key,
1860 mime_info_cache_blow_global_cache (void)
1862 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1867 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1871 gchar *filename, **mime_types;
1878 if (dir->mime_info_cache_map != NULL &&
1879 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1880 &dir->mime_info_cache_timestamp))
1883 if (dir->mime_info_cache_map != NULL)
1884 destroy_info_cache_map (dir->mime_info_cache_map);
1886 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1887 (GDestroyNotify) g_free,
1890 key_file = g_key_file_new ();
1892 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1894 if (g_stat (filename, &buf) < 0)
1897 if (dir->mime_info_cache_timestamp > 0)
1898 mime_info_cache->should_ping_mime_monitor = TRUE;
1900 dir->mime_info_cache_timestamp = buf.st_mtime;
1902 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1907 if (load_error != NULL)
1910 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1913 if (load_error != NULL)
1916 for (i = 0; mime_types[i] != NULL; i++)
1918 gchar **desktop_file_ids;
1919 char *unaliased_type;
1920 desktop_file_ids = g_key_file_get_string_list (key_file,
1926 if (desktop_file_ids == NULL)
1929 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1930 mime_info_cache_dir_add_desktop_entries (dir,
1933 g_free (unaliased_type);
1935 g_strfreev (desktop_file_ids);
1938 g_strfreev (mime_types);
1939 g_key_file_free (key_file);
1944 g_key_file_free (key_file);
1946 if (mime_types != NULL)
1947 g_strfreev (mime_types);
1950 g_error_free (load_error);
1954 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1958 gchar *filename, **mime_types;
1959 char *unaliased_type;
1960 char **desktop_file_ids;
1967 if (dir->defaults_list_map != NULL &&
1968 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1969 &dir->defaults_list_timestamp))
1972 if (dir->defaults_list_map != NULL)
1973 g_hash_table_destroy (dir->defaults_list_map);
1975 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1976 g_free, (GDestroyNotify)g_strfreev);
1978 key_file = g_key_file_new ();
1980 filename = g_build_filename (dir->path, "defaults.list", NULL);
1981 if (g_stat (filename, &buf) < 0)
1984 if (dir->defaults_list_timestamp > 0)
1985 mime_info_cache->should_ping_mime_monitor = TRUE;
1987 dir->defaults_list_timestamp = buf.st_mtime;
1989 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1993 if (load_error != NULL)
1996 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1999 if (load_error != NULL)
2002 for (i = 0; mime_types[i] != NULL; i++)
2004 desktop_file_ids = g_key_file_get_string_list (key_file,
2005 DEFAULT_APPLICATIONS_GROUP,
2009 if (desktop_file_ids == NULL)
2012 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2013 g_hash_table_replace (dir->defaults_list_map,
2018 g_strfreev (mime_types);
2019 g_key_file_free (key_file);
2024 g_key_file_free (key_file);
2026 if (mime_types != NULL)
2027 g_strfreev (mime_types);
2030 g_error_free (load_error);
2033 static MimeInfoCacheDir *
2034 mime_info_cache_dir_new (const char *path)
2036 MimeInfoCacheDir *dir;
2038 dir = g_new0 (MimeInfoCacheDir, 1);
2039 dir->path = g_strdup (path);
2045 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2050 if (dir->mime_info_cache_map != NULL)
2052 destroy_info_cache_map (dir->mime_info_cache_map);
2053 dir->mime_info_cache_map = NULL;
2057 if (dir->defaults_list_map != NULL)
2059 g_hash_table_destroy (dir->defaults_list_map);
2060 dir->defaults_list_map = NULL;
2067 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2068 const char *mime_type,
2069 char **new_desktop_file_ids)
2071 GList *desktop_file_ids;
2074 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2077 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2079 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2080 desktop_file_ids = g_list_append (desktop_file_ids,
2081 g_strdup (new_desktop_file_ids[i]));
2084 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2088 mime_info_cache_init_dir_lists (void)
2090 const char * const *dirs;
2093 mime_info_cache = mime_info_cache_new ();
2095 dirs = get_applications_search_path ();
2097 for (i = 0; dirs[i] != NULL; i++)
2099 MimeInfoCacheDir *dir;
2101 dir = mime_info_cache_dir_new (dirs[i]);
2105 mime_info_cache_dir_init (dir);
2106 mime_info_cache_dir_init_defaults_list (dir);
2108 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2114 mime_info_cache_update_dir_lists (void)
2118 tmp = mime_info_cache->dirs;
2122 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2124 /* No need to do this if we had file monitors... */
2125 mime_info_cache_blow_global_cache ();
2126 mime_info_cache_dir_init (dir);
2127 mime_info_cache_dir_init_defaults_list (dir);
2134 mime_info_cache_init (void)
2136 G_LOCK (mime_info_cache);
2137 if (mime_info_cache == NULL)
2138 mime_info_cache_init_dir_lists ();
2144 if (now >= mime_info_cache->last_stat_time + 10)
2146 mime_info_cache_update_dir_lists ();
2147 mime_info_cache->last_stat_time = now;
2151 if (mime_info_cache->should_ping_mime_monitor)
2153 /* g_idle_add (emit_mime_changed, NULL); */
2154 mime_info_cache->should_ping_mime_monitor = FALSE;
2157 G_UNLOCK (mime_info_cache);
2160 static MimeInfoCache *
2161 mime_info_cache_new (void)
2163 MimeInfoCache *cache;
2165 cache = g_new0 (MimeInfoCache, 1);
2167 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2168 (GDestroyNotify) g_free,
2169 (GDestroyNotify) g_free);
2174 mime_info_cache_free (MimeInfoCache *cache)
2179 g_list_foreach (cache->dirs,
2180 (GFunc) mime_info_cache_dir_free,
2182 g_list_free (cache->dirs);
2183 g_hash_table_destroy (cache->global_defaults_cache);
2188 * mime_info_cache_reload:
2189 * @dir: directory path which needs reloading.
2191 * Reload the mime information for the @dir.
2194 mime_info_cache_reload (const char *dir)
2196 /* FIXME: just reload the dir that needs reloading,
2197 * don't blow the whole cache
2199 if (mime_info_cache != NULL)
2201 G_LOCK (mime_info_cache);
2202 mime_info_cache_free (mime_info_cache);
2203 mime_info_cache = NULL;
2204 G_UNLOCK (mime_info_cache);
2209 append_desktop_entry (GList *list,
2210 const char *desktop_entry)
2212 /* Add if not already in list, and valid */
2213 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2214 list = g_list_prepend (list, g_strdup (desktop_entry));
2220 * get_all_desktop_entries_for_mime_type:
2221 * @mime_type: a mime type.
2223 * Returns all the desktop ids for @mime_type. The desktop files
2224 * are listed in an order so that default applications are listed before
2225 * non-default ones, and handlers for inherited mimetypes are listed
2226 * after the base ones.
2228 * Return value: a #GList containing the desktop ids which claim
2229 * to handle @mime_type.
2232 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2234 GList *desktop_entries, *list, *dir_list, *tmp;
2235 MimeInfoCacheDir *dir;
2238 char **default_entries;
2241 mime_info_cache_init ();
2243 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2244 G_LOCK (mime_info_cache);
2246 desktop_entries = NULL;
2247 for (i = 0; mime_types[i] != NULL; i++)
2249 mime_type = mime_types[i];
2251 /* Go through all apps listed as defaults */
2252 for (dir_list = mime_info_cache->dirs;
2254 dir_list = dir_list->next)
2256 dir = dir_list->data;
2257 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2258 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2259 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2262 /* Go through all entries that support the mimetype */
2263 for (dir_list = mime_info_cache->dirs;
2265 dir_list = dir_list->next)
2267 dir = dir_list->data;
2269 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2270 for (tmp = list; tmp != NULL; tmp = tmp->next)
2271 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2275 G_UNLOCK (mime_info_cache);
2277 g_strfreev (mime_types);
2279 desktop_entries = g_list_reverse (desktop_entries);
2281 return desktop_entries;
2284 #define __G_DESKTOP_APP_INFO_C__
2285 #include "gioaliasdef.c"