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,
190 g_key_file_free (key_file);
194 start_group = g_key_file_get_start_group (key_file);
195 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
197 g_free (start_group);
198 g_key_file_free (key_file);
201 g_free (start_group);
203 type = g_key_file_get_string (key_file,
204 G_KEY_FILE_DESKTOP_GROUP,
205 G_KEY_FILE_DESKTOP_KEY_TYPE,
207 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
210 g_key_file_free (key_file);
215 try_exec = g_key_file_get_string (key_file,
216 G_KEY_FILE_DESKTOP_GROUP,
217 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
222 t = g_find_program_in_path (try_exec);
226 g_key_file_free (key_file);
232 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
233 info->filename = g_strdup (filename);
235 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
236 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
237 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
238 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
239 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);
240 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);
241 info->try_exec = try_exec;
242 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
243 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
244 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
245 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
246 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
248 g_key_file_free (key_file);
253 if (g_path_is_absolute (info->icon_name))
257 file = g_file_new_for_path (info->icon_name);
258 info->icon = g_file_icon_new (file);
259 g_object_unref (file);
262 info->icon = g_themed_icon_new (info->icon_name);
273 while (*p != ' ' && *p != 0)
276 info->binary = g_strndup (start, p - start);
283 * g_desktop_app_info_new:
284 * @desktop_id: the desktop file id
286 * Creates a new #GDesktopAppInfo.
288 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
291 g_desktop_app_info_new (const char *desktop_id)
293 GDesktopAppInfo *appinfo;
294 const char * const *dirs;
298 dirs = get_applications_search_path ();
300 basename = g_strdup (desktop_id);
302 for (i = 0; dirs[i] != NULL; i++)
307 filename = g_build_filename (dirs[i], desktop_id, NULL);
308 appinfo = g_desktop_app_info_new_from_filename (filename);
314 while ((p = strchr (p, '-')) != NULL)
318 filename = g_build_filename (dirs[i], basename, NULL);
319 appinfo = g_desktop_app_info_new_from_filename (filename);
334 appinfo->desktop_id = g_strdup (desktop_id);
336 if (g_desktop_app_info_get_is_hidden (appinfo))
338 g_object_unref (appinfo);
346 g_desktop_app_info_dup (GAppInfo *appinfo)
348 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
349 GDesktopAppInfo *new_info;
351 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
353 new_info->filename = g_strdup (info->filename);
354 new_info->desktop_id = g_strdup (info->desktop_id);
356 new_info->name = g_strdup (info->name);
357 new_info->comment = g_strdup (info->comment);
358 new_info->nodisplay = info->nodisplay;
359 new_info->icon_name = g_strdup (info->icon_name);
360 new_info->icon = g_object_ref (info->icon);
361 new_info->only_show_in = g_strdupv (info->only_show_in);
362 new_info->not_show_in = g_strdupv (info->not_show_in);
363 new_info->try_exec = g_strdup (info->try_exec);
364 new_info->exec = g_strdup (info->exec);
365 new_info->binary = g_strdup (info->binary);
366 new_info->path = g_strdup (info->path);
367 new_info->hidden = info->hidden;
368 new_info->terminal = info->terminal;
369 new_info->startup_notify = info->startup_notify;
371 return G_APP_INFO (new_info);
375 g_desktop_app_info_equal (GAppInfo *appinfo1,
378 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
379 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
381 if (info1->desktop_id == NULL ||
382 info2->desktop_id == NULL)
385 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
389 g_desktop_app_info_get_id (GAppInfo *appinfo)
391 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
393 return info->desktop_id;
397 g_desktop_app_info_get_name (GAppInfo *appinfo)
399 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
401 if (info->name == NULL)
407 * g_desktop_app_info_get_is_hidden:
408 * @info: a #GDesktopAppInfo.
410 * A desktop file is hidden if the Hidden key in it is
413 * Returns: %TRUE if hidden, %FALSE otherwise.
416 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
422 g_desktop_app_info_get_description (GAppInfo *appinfo)
424 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
426 return info->comment;
430 g_desktop_app_info_get_executable (GAppInfo *appinfo)
432 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
438 g_desktop_app_info_get_icon (GAppInfo *appinfo)
440 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
446 expand_macro_single (char macro, GFile *file)
451 path = g_file_get_path (file);
452 uri = g_file_get_uri (file);
458 result = g_shell_quote (uri);
463 result = g_shell_quote (path);
468 result = g_shell_quote (g_path_get_dirname (path));
473 result = g_shell_quote (g_path_get_basename (path));
484 expand_macro (char macro,
486 GDesktopAppInfo *info,
489 GList *files = *file_list;
492 g_return_if_fail (exec != NULL);
502 expanded = expand_macro_single (macro, files->data);
505 g_string_append (exec, expanded);
519 expanded = expand_macro_single (macro, files->data);
522 g_string_append (exec, expanded);
528 if (files != NULL && expanded)
529 g_string_append_c (exec, ' ');
537 g_string_append (exec, "--icon ");
538 g_string_append (exec, info->icon_name);
544 g_string_append (exec, info->name);
549 g_string_append (exec, info->filename);
552 case 'm': /* deprecated */
556 g_string_append_c (exec, '%');
564 expand_application_parameters (GDesktopAppInfo *info,
570 GList *file_list = *files;
571 const char *p = info->exec;
572 GString *expanded_exec = g_string_new (NULL);
575 if (info->exec == NULL)
577 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
578 _("Desktop file didn't specify Exec field"));
584 if (p[0] == '%' && p[1] != '\0')
586 expand_macro (p[1], expanded_exec, info, files);
590 g_string_append_c (expanded_exec, *p);
595 /* No file substitutions */
596 if (file_list == *files && file_list != NULL)
598 /* If there is no macro default to %f. This is also what KDE does */
599 g_string_append_c (expanded_exec, ' ');
600 expand_macro ('f', expanded_exec, info, files);
603 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
604 g_string_free (expanded_exec, TRUE);
609 prepend_terminal_to_vector (int *argc,
616 char **term_argv = NULL;
621 g_return_val_if_fail (argc != NULL, FALSE);
622 g_return_val_if_fail (argv != NULL, FALSE);
630 /* compute size if not given */
633 for (i = 0; the_argv[i] != NULL; i++)
639 term_argv = g_new0 (char *, 3);
641 check = g_find_program_in_path ("gnome-terminal");
644 term_argv[0] = check;
645 /* Note that gnome-terminal takes -x and
646 * as -e in gnome-terminal is broken we use that. */
647 term_argv[1] = g_strdup ("-x");
652 check = g_find_program_in_path ("nxterm");
654 check = g_find_program_in_path ("color-xterm");
656 check = g_find_program_in_path ("rxvt");
658 check = g_find_program_in_path ("xterm");
660 check = g_find_program_in_path ("dtterm");
663 check = g_strdup ("xterm");
664 g_warning ("couldn't find a terminal, falling back to xterm");
666 term_argv[0] = check;
667 term_argv[1] = g_strdup ("-e");
670 real_argc = term_argc + *argc;
671 real_argv = g_new (char *, real_argc + 1);
673 for (i = 0; i < term_argc; i++)
674 real_argv[i] = term_argv[i];
676 for (j = 0; j < *argc; j++, i++)
677 real_argv[i] = (char *)the_argv[j];
685 /* we use g_free here as we sucked all the inner strings
686 * out from it into real_argv */
691 #endif /* G_OS_WIN32 */
694 /* '=' is the new '\0'.
695 * DO NOT CALL unless at least one string ends with '='
698 is_env (const char *a,
703 if (*a == 0 || *b == 0)
716 /* free with g_strfreev */
718 replace_env_var (char **old_environ,
720 const char *new_value)
722 int length, new_length;
723 int index, new_index;
727 /* do two things at once:
728 * - discover the length of the environment ('length')
729 * - find the location (if any) of the env var ('index')
732 for (length = 0; old_environ[length]; length++)
734 /* if we already have it in our environment, replace */
735 if (is_env (old_environ[length], env_var))
740 /* no current env var, no desired env value.
743 if (new_value == NULL && index == -1)
746 /* in all cases now, we will be using a modified environment.
747 * determine its length and allocated it.
750 * new_index = location to insert, if any
751 * new_length = length of the new array
752 * new_environ = the pointer array for the new environment
755 if (new_value == NULL && index >= 0)
757 /* in this case, we will be removing an entry */
758 new_length = length - 1;
761 else if (new_value != NULL && index < 0)
763 /* in this case, we will be adding an entry to the end */
764 new_length = length + 1;
768 /* in this case, we will be replacing the existing entry */
774 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
775 new_environ[new_length] = NULL;
777 /* now we do the copying.
778 * for each entry in the new environment, we decide what to do
782 for (new_i = 0; new_i < new_length; new_i++)
784 if (new_i == new_index)
786 /* insert our new item */
787 new_environ[new_i] = g_strconcat (env_var,
792 /* if we had an old entry, skip it now */
798 /* if this is the old DESKTOP_STARTUP_ID, skip it */
802 /* copy an old item */
803 new_environ[new_i] = g_strdup (old_environ[i]);
808 g_strfreev (old_environ);
814 dup_list_segment (GList *start,
820 while (start != NULL && start != end)
822 res = g_list_prepend (res, start->data);
826 return g_list_reverse (res);
829 #ifdef HAVE__NSGETENVIRON
830 #define environ (*_NSGetEnviron())
831 #elif !defined(G_OS_WIN32)
833 /* According to the Single Unix Specification, environ is not in
834 * * any system header, although unistd.h often declares it.
836 extern char **environ;
840 g_desktop_app_info_launch (GAppInfo *appinfo,
842 GAppLaunchContext *launch_context,
845 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
846 gboolean completed = FALSE;
848 GList *launched_files;
855 g_return_val_if_fail (appinfo != NULL, FALSE);
863 if (!expand_application_parameters (info, &files,
864 &argc, &argv, error))
867 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
869 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
870 _("Unable to find terminal required for application"));
877 launched_files = dup_list_segment (old_files, files);
879 display = g_app_launch_context_get_display (launch_context,
884 if (info->startup_notify)
885 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
889 if (display || sn_id)
893 envp = g_new0 (char *, 1);
895 envp = g_strdupv (environ);
899 envp = replace_env_var (envp,
904 envp = replace_env_var (envp,
905 "DESKTOP_STARTUP_ID",
911 g_list_free (launched_files);
914 if (!g_spawn_async (info->path, /* working directory */
917 G_SPAWN_SEARCH_PATH /* flags */,
918 NULL /* child_setup */,
920 NULL /* child_pid */,
925 g_app_launch_context_launch_failed (launch_context, sn_id);
939 while (files != NULL);
951 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
953 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
956 ((strstr (info->exec, "%u") != NULL) ||
957 (strstr (info->exec, "%U") != NULL));
961 g_desktop_app_info_supports_files (GAppInfo *appinfo)
963 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
966 ((strstr (info->exec, "%f") != NULL) ||
967 (strstr (info->exec, "%F") != NULL));
971 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
973 GAppLaunchContext *launch_context,
983 file = g_file_new_for_uri (uris->data);
985 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
988 files = g_list_prepend (files, file);
991 files = g_list_reverse (files);
993 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
995 g_list_foreach (files, (GFunc)g_object_unref, NULL);
1001 G_LOCK_DEFINE_STATIC (g_desktop_env);
1002 static gchar *g_desktop_env = NULL;
1005 * g_desktop_app_info_set_desktop_env:
1006 * @desktop_env: a string specifying what desktop this is
1008 * Sets the name of the desktop that the application is running in.
1009 * This is used by g_app_info_should_show() to evaluate the
1010 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1011 * desktop entry fields.
1013 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1014 * Menu specification</ulink> recognizes the following:
1016 * <member>GNOME</member>
1017 * <member>KDE</member>
1018 * <member>ROX</member>
1019 * <member>XFCE</member>
1020 * <member>Old</member>
1023 * Should be called only once; subsequent calls are ignored.
1026 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1028 G_LOCK (g_desktop_env);
1030 g_desktop_env = g_strdup (desktop_env);
1031 G_UNLOCK (g_desktop_env);
1035 g_desktop_app_info_should_show (GAppInfo *appinfo)
1037 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1039 const gchar *desktop_env;
1042 if (info->nodisplay)
1045 G_LOCK (g_desktop_env);
1046 desktop_env = g_desktop_env;
1047 G_UNLOCK (g_desktop_env);
1049 if (info->only_show_in)
1051 if (desktop_env == NULL)
1055 for (i = 0; info->only_show_in[i] != NULL; i++)
1057 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1067 if (info->not_show_in && desktop_env)
1069 for (i = 0; info->not_show_in[i] != NULL; i++)
1071 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1085 ensure_dir (DirType type,
1088 char *path, *display_name;
1091 if (type == APP_DIR)
1092 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1094 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1097 if (g_mkdir_with_parents (path, 0700) == 0)
1101 display_name = g_filename_display_name (path);
1102 if (type == APP_DIR)
1103 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1104 _("Can't create user application configuration folder %s: %s"),
1105 display_name, g_strerror (err));
1107 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1108 _("Can't create user MIME configuration folder %s: %s"),
1109 display_name, g_strerror (err));
1111 g_free (display_name);
1118 update_default_list (const char *desktop_id,
1119 const char *content_type,
1123 char *dirname, *filename;
1125 gboolean load_succeeded, res;
1128 gsize length, data_size;
1132 dirname = ensure_dir (APP_DIR, error);
1136 filename = g_build_filename (dirname, "defaults.list", NULL);
1139 key_file = g_key_file_new ();
1140 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1141 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1143 g_key_file_free (key_file);
1144 key_file = g_key_file_new ();
1148 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1149 content_type, &length, NULL);
1151 list = g_new (char *, 1 + length + 1);
1155 list[i++] = g_strdup (desktop_id);
1158 for (j = 0; old_list[j] != NULL; j++)
1160 if (strcmp (old_list[j], desktop_id) != 0)
1161 list[i++] = g_strdup (old_list[j]);
1166 g_strfreev (old_list);
1168 g_key_file_set_string_list (key_file,
1169 DEFAULT_APPLICATIONS_GROUP,
1171 (const char * const *)list, i);
1175 data = g_key_file_to_data (key_file, &data_size, error);
1176 g_key_file_free (key_file);
1178 res = g_file_set_contents (filename, data, data_size, error);
1180 mime_info_cache_reload (NULL);
1189 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1190 const char *content_type,
1193 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1195 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1198 return update_default_list (info->desktop_id, content_type, TRUE, error);
1202 update_program_done (GPid pid,
1206 /* Did the application exit correctly */
1207 if (WIFEXITED (status) &&
1208 WEXITSTATUS (status) == 0)
1210 /* Here we could clean out any caches in use */
1215 run_update_command (char *command,
1224 GError *error = NULL;
1227 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1229 if (g_spawn_async ("/", argv,
1231 G_SPAWN_SEARCH_PATH |
1232 G_SPAWN_STDOUT_TO_DEV_NULL |
1233 G_SPAWN_STDERR_TO_DEV_NULL |
1234 G_SPAWN_DO_NOT_REAP_CHILD,
1235 NULL, NULL, /* No setup function */
1238 g_child_watch_add (pid, update_program_done, NULL);
1241 /* If we get an error at this point, it's quite likely the user doesn't
1242 * have an installed copy of either 'update-mime-database' or
1243 * 'update-desktop-database'. I don't think we want to popup an error
1244 * dialog at this point, so we just do a g_warning to give the user a
1245 * chance of debugging it.
1247 g_warning ("%s", error->message);
1254 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1255 const char *extension,
1258 char *filename, *basename, *mimetype;
1262 dirname = ensure_dir (MIMETYPE_DIR, error);
1266 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1267 filename = g_build_filename (dirname, basename, NULL);
1271 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1273 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1278 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1279 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1280 " <mime-type type=\"%s\">\n"
1281 " <comment>%s document</comment>\n"
1282 " <glob pattern=\"*.%s\"/>\n"
1284 "</mime-info>\n", mimetype, extension, extension);
1286 g_file_set_contents (filename, contents, -1, NULL);
1289 run_update_command ("update-mime-database", "mime");
1293 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1303 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1304 const char *content_type,
1307 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1309 char *new_mimetypes, *old_mimetypes, *content;
1313 keyfile = g_key_file_new ();
1314 if (!g_key_file_load_from_file (keyfile, info->filename,
1315 G_KEY_FILE_KEEP_COMMENTS |
1316 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1318 g_key_file_free (keyfile);
1322 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1323 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1324 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1325 g_free (old_mimetypes);
1326 g_free (new_mimetypes);
1328 content = g_key_file_to_data (keyfile, NULL, NULL);
1329 g_key_file_free (keyfile);
1331 dirname = ensure_dir (APP_DIR, error);
1338 filename = g_build_filename (dirname, info->desktop_id, NULL);
1341 if (!g_file_set_contents (filename, content, -1, error))
1350 run_update_command ("update-desktop-database", "applications");
1355 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1357 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1360 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1361 return g_str_has_prefix (info->filename, user_dirname);
1365 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1366 const char *content_type,
1369 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1371 char *new_mimetypes, *old_mimetypes, *content;
1376 keyfile = g_key_file_new ();
1377 if (!g_key_file_load_from_file (keyfile, info->filename,
1378 G_KEY_FILE_KEEP_COMMENTS |
1379 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1381 g_key_file_free (keyfile);
1385 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1386 new_mimetypes = g_strdup (old_mimetypes);
1389 found = strstr (new_mimetypes, content_type);
1390 if (found && *(found + strlen (content_type)) == ';')
1392 char *rest = found + strlen (content_type) + 1;
1393 memmove (found, rest, strlen (rest) + 1);
1395 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1396 g_free (old_mimetypes);
1397 g_free (new_mimetypes);
1399 content = g_key_file_to_data (keyfile, NULL, NULL);
1400 g_key_file_free (keyfile);
1402 dirname = ensure_dir (APP_DIR, error);
1409 filename = g_build_filename (dirname, info->desktop_id, NULL);
1411 if (!g_file_set_contents (filename, content, -1, error))
1420 run_update_command ("update-desktop-database", "applications");
1422 return update_default_list (info->desktop_id, content_type, FALSE, error);
1426 * g_app_info_create_from_commandline:
1427 * @commandline: the commandline to use
1428 * @application_name: the application name, or %NULL to use @commandline
1429 * @flags: flags that can specify details of the created #GAppInfo
1430 * @error: a #GError location to store the error occuring, %NULL to ignore.
1432 * Creates a new #GAppInfo from the given information.
1434 * Returns: new #GAppInfo for given command.
1437 g_app_info_create_from_commandline (const char *commandline,
1438 const char *application_name,
1439 GAppInfoCreateFlags flags,
1445 char *basename, *exec, *filename, *comment;
1446 char *data, *desktop_id;
1449 GDesktopAppInfo *info;
1452 dirname = ensure_dir (APP_DIR, error);
1456 key_file = g_key_file_new ();
1458 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1459 "Encoding", "UTF-8");
1460 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1461 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1462 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1463 G_KEY_FILE_DESKTOP_KEY_TYPE,
1464 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1465 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1466 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1467 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1469 exec = g_strconcat (commandline, " %f", NULL);
1470 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1471 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1474 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1475 split = g_strsplit (commandline, " ", 2);
1476 basename = g_path_get_basename (split[0]);
1478 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1479 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1481 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1482 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1483 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1486 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1487 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1489 data = g_key_file_to_data (key_file, &data_size, NULL);
1490 g_key_file_free (key_file);
1492 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1494 filename = g_build_filename (dirname, desktop_id, NULL);
1495 g_free (desktop_id);
1498 fd = g_mkstemp (filename);
1503 display_name = g_filename_display_name (filename);
1504 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1505 _("Can't create user desktop file %s"), display_name);
1506 g_free (display_name);
1512 desktop_id = g_path_get_basename (filename);
1516 res = g_file_set_contents (filename, data, data_size, error);
1519 g_free (desktop_id);
1524 run_update_command ("update-desktop-database", "applications");
1526 info = g_desktop_app_info_new_from_filename (filename);
1529 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1530 _("Can't load just created desktop file"));
1532 info->desktop_id = g_strdup (desktop_id);
1534 g_free (desktop_id);
1536 return G_APP_INFO (info);
1541 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1543 iface->dup = g_desktop_app_info_dup;
1544 iface->equal = g_desktop_app_info_equal;
1545 iface->get_id = g_desktop_app_info_get_id;
1546 iface->get_name = g_desktop_app_info_get_name;
1547 iface->get_description = g_desktop_app_info_get_description;
1548 iface->get_executable = g_desktop_app_info_get_executable;
1549 iface->get_icon = g_desktop_app_info_get_icon;
1550 iface->launch = g_desktop_app_info_launch;
1551 iface->supports_uris = g_desktop_app_info_supports_uris;
1552 iface->supports_files = g_desktop_app_info_supports_files;
1553 iface->launch_uris = g_desktop_app_info_launch_uris;
1554 iface->should_show = g_desktop_app_info_should_show;
1555 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1556 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1557 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1558 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1559 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1563 app_info_in_list (GAppInfo *info,
1566 while (list != NULL)
1568 if (g_app_info_equal (info, list->data))
1577 * g_app_info_get_all_for_type:
1578 * @content_type: the content type to find a #GAppInfo for
1580 * Gets a list of all #GAppInfo s for a given content type.
1582 * Returns: #GList of #GAppInfo s for given @content_type
1583 * or %NULL on error.
1586 g_app_info_get_all_for_type (const char *content_type)
1588 GList *desktop_entries, *l;
1590 GDesktopAppInfo *info;
1592 g_return_val_if_fail (content_type != NULL, NULL);
1594 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1597 for (l = desktop_entries; l != NULL; l = l->next)
1599 char *desktop_entry = l->data;
1601 info = g_desktop_app_info_new (desktop_entry);
1604 if (app_info_in_list (G_APP_INFO (info), infos))
1605 g_object_unref (info);
1607 infos = g_list_prepend (infos, info);
1609 g_free (desktop_entry);
1612 g_list_free (desktop_entries);
1614 return g_list_reverse (infos);
1619 * g_app_info_get_default_for_type:
1620 * @content_type: the content type to find a #GAppInfo for
1621 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1624 * Gets the #GAppInfo that correspond to a given content type.
1626 * Returns: #GAppInfo for given @content_type or %NULL on error.
1629 g_app_info_get_default_for_type (const char *content_type,
1630 gboolean must_support_uris)
1632 GList *desktop_entries, *l;
1635 g_return_val_if_fail (content_type != NULL, NULL);
1637 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1640 for (l = desktop_entries; l != NULL; l = l->next)
1642 char *desktop_entry = l->data;
1644 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1647 if (must_support_uris && !g_app_info_supports_uris (info))
1649 g_object_unref (info);
1657 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1658 g_list_free (desktop_entries);
1665 * g_app_info_get_default_for_uri_scheme:
1666 * @uri_scheme: a string containing a URI scheme.
1668 * Gets the default application for launching applications
1669 * using this URI scheme.
1671 * TODO: This is currently unimplemented.
1676 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1678 /* TODO: Implement this using giomodules, reading the gconf settings
1679 * in /desktop/gnome/url-handlers
1686 get_apps_from_dir (GHashTable *apps,
1687 const char *dirname,
1691 const char *basename;
1692 char *filename, *subprefix, *desktop_id;
1694 GDesktopAppInfo *appinfo;
1696 dir = g_dir_open (dirname, 0, NULL);
1699 while ((basename = g_dir_read_name (dir)) != NULL)
1701 filename = g_build_filename (dirname, basename, NULL);
1702 if (g_str_has_suffix (basename, ".desktop"))
1704 desktop_id = g_strconcat (prefix, basename, NULL);
1706 /* Use _extended so we catch NULLs too (hidden) */
1707 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1709 appinfo = g_desktop_app_info_new_from_filename (filename);
1711 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1713 g_object_unref (appinfo);
1718 if (appinfo || hidden)
1720 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1724 /* Reuse instead of strdup here */
1725 appinfo->desktop_id = desktop_id;
1730 g_free (desktop_id);
1734 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1736 subprefix = g_strconcat (prefix, basename, "-", NULL);
1737 get_apps_from_dir (apps, filename, subprefix);
1749 * g_app_info_get_all:
1751 * Gets a list of all of the applications currently registered
1754 * For desktop files, this includes applications that have
1755 * <literal>NoDisplay=true</literal> set or are excluded from
1756 * display by means of <literal>OnlyShowIn</literal> or
1757 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1758 * The returned list does not include applications which have
1759 * the <literal>Hidden</literal> key set.
1761 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1764 g_app_info_get_all (void)
1766 const char * const *dirs;
1768 GHashTableIter iter;
1773 dirs = get_applications_search_path ();
1775 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1779 for (i = 0; dirs[i] != NULL; i++)
1780 get_apps_from_dir (apps, dirs[i], "");
1784 g_hash_table_iter_init (&iter, apps);
1785 while (g_hash_table_iter_next (&iter, NULL, &value))
1788 infos = g_list_prepend (infos, value);
1791 g_hash_table_destroy (apps);
1793 return g_list_reverse (infos);
1796 /* Cacheing of mimeinfo.cache and defaults.list files */
1800 GHashTable *mime_info_cache_map;
1801 GHashTable *defaults_list_map;
1802 time_t mime_info_cache_timestamp;
1803 time_t defaults_list_timestamp;
1807 GList *dirs; /* mimeinfo.cache and defaults.list */
1808 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1809 time_t last_stat_time;
1810 guint should_ping_mime_monitor : 1;
1813 static MimeInfoCache *mime_info_cache = NULL;
1814 G_LOCK_DEFINE_STATIC (mime_info_cache);
1816 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1817 const char *mime_type,
1818 char **new_desktop_file_ids);
1820 static MimeInfoCache * mime_info_cache_new (void);
1823 destroy_info_cache_value (gpointer key,
1827 g_list_foreach (value, (GFunc)g_free, NULL);
1828 g_list_free (value);
1832 destroy_info_cache_map (GHashTable *info_cache_map)
1834 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1835 g_hash_table_destroy (info_cache_map);
1839 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1840 const char *cache_file,
1846 filename = g_build_filename (dir->path, cache_file, NULL);
1848 if (g_stat (filename, &buf) < 0)
1855 if (buf.st_mtime != *timestamp)
1861 /* Call with lock held */
1863 remove_all (gpointer key,
1872 mime_info_cache_blow_global_cache (void)
1874 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1879 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1883 gchar *filename, **mime_types;
1890 if (dir->mime_info_cache_map != NULL &&
1891 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1892 &dir->mime_info_cache_timestamp))
1895 if (dir->mime_info_cache_map != NULL)
1896 destroy_info_cache_map (dir->mime_info_cache_map);
1898 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1899 (GDestroyNotify) g_free,
1902 key_file = g_key_file_new ();
1904 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1906 if (g_stat (filename, &buf) < 0)
1909 if (dir->mime_info_cache_timestamp > 0)
1910 mime_info_cache->should_ping_mime_monitor = TRUE;
1912 dir->mime_info_cache_timestamp = buf.st_mtime;
1914 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1919 if (load_error != NULL)
1922 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1925 if (load_error != NULL)
1928 for (i = 0; mime_types[i] != NULL; i++)
1930 gchar **desktop_file_ids;
1931 char *unaliased_type;
1932 desktop_file_ids = g_key_file_get_string_list (key_file,
1938 if (desktop_file_ids == NULL)
1941 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1942 mime_info_cache_dir_add_desktop_entries (dir,
1945 g_free (unaliased_type);
1947 g_strfreev (desktop_file_ids);
1950 g_strfreev (mime_types);
1951 g_key_file_free (key_file);
1956 g_key_file_free (key_file);
1958 if (mime_types != NULL)
1959 g_strfreev (mime_types);
1962 g_error_free (load_error);
1966 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1970 gchar *filename, **mime_types;
1971 char *unaliased_type;
1972 char **desktop_file_ids;
1979 if (dir->defaults_list_map != NULL &&
1980 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1981 &dir->defaults_list_timestamp))
1984 if (dir->defaults_list_map != NULL)
1985 g_hash_table_destroy (dir->defaults_list_map);
1987 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1988 g_free, (GDestroyNotify)g_strfreev);
1990 key_file = g_key_file_new ();
1992 filename = g_build_filename (dir->path, "defaults.list", NULL);
1993 if (g_stat (filename, &buf) < 0)
1996 if (dir->defaults_list_timestamp > 0)
1997 mime_info_cache->should_ping_mime_monitor = TRUE;
1999 dir->defaults_list_timestamp = buf.st_mtime;
2001 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2005 if (load_error != NULL)
2008 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2011 if (load_error != NULL)
2014 for (i = 0; mime_types[i] != NULL; i++)
2016 desktop_file_ids = g_key_file_get_string_list (key_file,
2017 DEFAULT_APPLICATIONS_GROUP,
2021 if (desktop_file_ids == NULL)
2024 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2025 g_hash_table_replace (dir->defaults_list_map,
2030 g_strfreev (mime_types);
2031 g_key_file_free (key_file);
2036 g_key_file_free (key_file);
2038 if (mime_types != NULL)
2039 g_strfreev (mime_types);
2042 g_error_free (load_error);
2045 static MimeInfoCacheDir *
2046 mime_info_cache_dir_new (const char *path)
2048 MimeInfoCacheDir *dir;
2050 dir = g_new0 (MimeInfoCacheDir, 1);
2051 dir->path = g_strdup (path);
2057 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2062 if (dir->mime_info_cache_map != NULL)
2064 destroy_info_cache_map (dir->mime_info_cache_map);
2065 dir->mime_info_cache_map = NULL;
2069 if (dir->defaults_list_map != NULL)
2071 g_hash_table_destroy (dir->defaults_list_map);
2072 dir->defaults_list_map = NULL;
2079 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2080 const char *mime_type,
2081 char **new_desktop_file_ids)
2083 GList *desktop_file_ids;
2086 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2089 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2091 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2092 desktop_file_ids = g_list_append (desktop_file_ids,
2093 g_strdup (new_desktop_file_ids[i]));
2096 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2100 mime_info_cache_init_dir_lists (void)
2102 const char * const *dirs;
2105 mime_info_cache = mime_info_cache_new ();
2107 dirs = get_applications_search_path ();
2109 for (i = 0; dirs[i] != NULL; i++)
2111 MimeInfoCacheDir *dir;
2113 dir = mime_info_cache_dir_new (dirs[i]);
2117 mime_info_cache_dir_init (dir);
2118 mime_info_cache_dir_init_defaults_list (dir);
2120 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2126 mime_info_cache_update_dir_lists (void)
2130 tmp = mime_info_cache->dirs;
2134 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2136 /* No need to do this if we had file monitors... */
2137 mime_info_cache_blow_global_cache ();
2138 mime_info_cache_dir_init (dir);
2139 mime_info_cache_dir_init_defaults_list (dir);
2146 mime_info_cache_init (void)
2148 G_LOCK (mime_info_cache);
2149 if (mime_info_cache == NULL)
2150 mime_info_cache_init_dir_lists ();
2156 if (now >= mime_info_cache->last_stat_time + 10)
2158 mime_info_cache_update_dir_lists ();
2159 mime_info_cache->last_stat_time = now;
2163 if (mime_info_cache->should_ping_mime_monitor)
2165 /* g_idle_add (emit_mime_changed, NULL); */
2166 mime_info_cache->should_ping_mime_monitor = FALSE;
2169 G_UNLOCK (mime_info_cache);
2172 static MimeInfoCache *
2173 mime_info_cache_new (void)
2175 MimeInfoCache *cache;
2177 cache = g_new0 (MimeInfoCache, 1);
2179 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2180 (GDestroyNotify) g_free,
2181 (GDestroyNotify) g_free);
2186 mime_info_cache_free (MimeInfoCache *cache)
2191 g_list_foreach (cache->dirs,
2192 (GFunc) mime_info_cache_dir_free,
2194 g_list_free (cache->dirs);
2195 g_hash_table_destroy (cache->global_defaults_cache);
2200 * mime_info_cache_reload:
2201 * @dir: directory path which needs reloading.
2203 * Reload the mime information for the @dir.
2206 mime_info_cache_reload (const char *dir)
2208 /* FIXME: just reload the dir that needs reloading,
2209 * don't blow the whole cache
2211 if (mime_info_cache != NULL)
2213 G_LOCK (mime_info_cache);
2214 mime_info_cache_free (mime_info_cache);
2215 mime_info_cache = NULL;
2216 G_UNLOCK (mime_info_cache);
2221 append_desktop_entry (GList *list,
2222 const char *desktop_entry)
2224 /* Add if not already in list, and valid */
2225 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2226 list = g_list_prepend (list, g_strdup (desktop_entry));
2232 * get_all_desktop_entries_for_mime_type:
2233 * @mime_type: a mime type.
2235 * Returns all the desktop ids for @mime_type. The desktop files
2236 * are listed in an order so that default applications are listed before
2237 * non-default ones, and handlers for inherited mimetypes are listed
2238 * after the base ones.
2240 * Return value: a #GList containing the desktop ids which claim
2241 * to handle @mime_type.
2244 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2246 GList *desktop_entries, *list, *dir_list, *tmp;
2247 MimeInfoCacheDir *dir;
2250 char **default_entries;
2253 mime_info_cache_init ();
2255 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2256 G_LOCK (mime_info_cache);
2258 desktop_entries = NULL;
2259 for (i = 0; mime_types[i] != NULL; i++)
2261 mime_type = mime_types[i];
2263 /* Go through all apps listed as defaults */
2264 for (dir_list = mime_info_cache->dirs;
2266 dir_list = dir_list->next)
2268 dir = dir_list->data;
2269 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2270 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2271 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2274 /* Go through all entries that support the mimetype */
2275 for (dir_list = mime_info_cache->dirs;
2277 dir_list = dir_list->next)
2279 dir = dir_list->data;
2281 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2282 for (tmp = list; tmp != NULL; tmp = tmp->next)
2283 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2287 G_UNLOCK (mime_info_cache);
2289 g_strfreev (mime_types);
2291 desktop_entries = g_list_reverse (desktop_entries);
2293 return desktop_entries;
2296 #define __G_DESKTOP_APP_INFO_C__
2297 #include "gioaliasdef.c"