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;
297 dirs = get_applications_search_path ();
299 for (i = 0; dirs[i] != NULL; i++)
305 filename = g_build_filename (dirs[i], desktop_id, NULL);
306 appinfo = g_desktop_app_info_new_from_filename (filename);
313 basename = g_strdup (desktop_id);
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,
1124 char *dirname, *filename;
1126 gboolean load_succeeded, res;
1129 gsize length, data_size;
1133 dirname = ensure_dir (APP_DIR, error);
1137 filename = g_build_filename (dirname, "defaults.list", NULL);
1140 key_file = g_key_file_new ();
1141 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1142 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1144 g_key_file_free (key_file);
1145 key_file = g_key_file_new ();
1149 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1150 content_type, &length, NULL);
1152 list = g_new (char *, 1 + length + 1);
1156 list[i++] = g_strdup (desktop_id);
1159 for (j = 0; old_list[j] != NULL; j++)
1161 if (strcmp (old_list[j], desktop_id) != 0)
1162 list[i++] = g_strdup (old_list[j]);
1167 g_strfreev (old_list);
1169 g_key_file_set_string_list (key_file,
1170 DEFAULT_APPLICATIONS_GROUP,
1172 (const char * const *)list, i);
1176 data = g_key_file_to_data (key_file, &data_size, error);
1177 g_key_file_free (key_file);
1179 res = g_file_set_contents (filename, data, data_size, error);
1181 mime_info_cache_reload (NULL);
1190 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1191 const char *content_type,
1194 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1196 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1199 return update_default_list (info->desktop_id, content_type, TRUE, error);
1203 update_program_done (GPid pid,
1207 /* Did the application exit correctly */
1208 if (WIFEXITED (status) &&
1209 WEXITSTATUS (status) == 0)
1211 /* Here we could clean out any caches in use */
1216 run_update_command (char *command,
1225 GError *error = NULL;
1228 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1230 if (g_spawn_async ("/", argv,
1232 G_SPAWN_SEARCH_PATH |
1233 G_SPAWN_STDOUT_TO_DEV_NULL |
1234 G_SPAWN_STDERR_TO_DEV_NULL |
1235 G_SPAWN_DO_NOT_REAP_CHILD,
1236 NULL, NULL, /* No setup function */
1239 g_child_watch_add (pid, update_program_done, NULL);
1242 /* If we get an error at this point, it's quite likely the user doesn't
1243 * have an installed copy of either 'update-mime-database' or
1244 * 'update-desktop-database'. I don't think we want to popup an error
1245 * dialog at this point, so we just do a g_warning to give the user a
1246 * chance of debugging it.
1248 g_warning ("%s", error->message);
1255 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1256 const char *extension,
1259 char *filename, *basename, *mimetype;
1263 dirname = ensure_dir (MIMETYPE_DIR, error);
1267 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1268 filename = g_build_filename (dirname, basename, NULL);
1272 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1274 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1279 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1280 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1281 " <mime-type type=\"%s\">\n"
1282 " <comment>%s document</comment>\n"
1283 " <glob pattern=\"*.%s\"/>\n"
1285 "</mime-info>\n", mimetype, extension, extension);
1287 g_file_set_contents (filename, contents, -1, NULL);
1290 run_update_command ("update-mime-database", "mime");
1294 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1304 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1305 const char *content_type,
1308 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1310 char *new_mimetypes, *old_mimetypes, *content;
1314 keyfile = g_key_file_new ();
1315 if (!g_key_file_load_from_file (keyfile, info->filename,
1316 G_KEY_FILE_KEEP_COMMENTS |
1317 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1319 g_key_file_free (keyfile);
1323 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1324 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1325 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1326 g_free (old_mimetypes);
1327 g_free (new_mimetypes);
1329 content = g_key_file_to_data (keyfile, NULL, NULL);
1330 g_key_file_free (keyfile);
1332 dirname = ensure_dir (APP_DIR, error);
1339 filename = g_build_filename (dirname, info->desktop_id, NULL);
1342 if (!g_file_set_contents (filename, content, -1, error))
1351 run_update_command ("update-desktop-database", "applications");
1356 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1358 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1361 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1362 return g_str_has_prefix (info->filename, user_dirname);
1366 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1367 const char *content_type,
1370 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1372 char *new_mimetypes, *old_mimetypes, *content;
1377 keyfile = g_key_file_new ();
1378 if (!g_key_file_load_from_file (keyfile, info->filename,
1379 G_KEY_FILE_KEEP_COMMENTS |
1380 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1382 g_key_file_free (keyfile);
1386 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1387 new_mimetypes = g_strdup (old_mimetypes);
1390 found = strstr (new_mimetypes, content_type);
1391 if (found && *(found + strlen (content_type)) == ';')
1393 char *rest = found + strlen (content_type) + 1;
1394 memmove (found, rest, strlen (rest) + 1);
1396 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1397 g_free (old_mimetypes);
1398 g_free (new_mimetypes);
1400 content = g_key_file_to_data (keyfile, NULL, NULL);
1401 g_key_file_free (keyfile);
1403 dirname = ensure_dir (APP_DIR, error);
1410 filename = g_build_filename (dirname, info->desktop_id, NULL);
1412 if (!g_file_set_contents (filename, content, -1, error))
1421 run_update_command ("update-desktop-database", "applications");
1423 return update_default_list (info->desktop_id, content_type, FALSE, error);
1427 * g_app_info_create_from_commandline:
1428 * @commandline: the commandline to use
1429 * @application_name: the application name, or %NULL to use @commandline
1430 * @flags: flags that can specify details of the created #GAppInfo
1431 * @error: a #GError location to store the error occuring, %NULL to ignore.
1433 * Creates a new #GAppInfo from the given information.
1435 * Returns: new #GAppInfo for given command.
1438 g_app_info_create_from_commandline (const char *commandline,
1439 const char *application_name,
1440 GAppInfoCreateFlags flags,
1446 char *basename, *exec, *filename, *comment;
1447 char *data, *desktop_id;
1450 GDesktopAppInfo *info;
1453 dirname = ensure_dir (APP_DIR, error);
1457 key_file = g_key_file_new ();
1459 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1460 "Encoding", "UTF-8");
1461 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1462 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1463 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1464 G_KEY_FILE_DESKTOP_KEY_TYPE,
1465 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1466 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1467 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1468 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1470 exec = g_strconcat (commandline, " %f", NULL);
1471 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1472 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1475 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1476 split = g_strsplit (commandline, " ", 2);
1477 basename = g_path_get_basename (split[0]);
1479 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1480 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1482 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1483 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1484 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1487 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1488 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1490 data = g_key_file_to_data (key_file, &data_size, NULL);
1491 g_key_file_free (key_file);
1493 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1495 filename = g_build_filename (dirname, desktop_id, NULL);
1496 g_free (desktop_id);
1499 fd = g_mkstemp (filename);
1504 display_name = g_filename_display_name (filename);
1505 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1506 _("Can't create user desktop file %s"), display_name);
1507 g_free (display_name);
1513 desktop_id = g_path_get_basename (filename);
1517 res = g_file_set_contents (filename, data, data_size, error);
1520 g_free (desktop_id);
1525 run_update_command ("update-desktop-database", "applications");
1527 info = g_desktop_app_info_new_from_filename (filename);
1530 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1531 _("Can't load just created desktop file"));
1533 info->desktop_id = g_strdup (desktop_id);
1535 g_free (desktop_id);
1537 return G_APP_INFO (info);
1542 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1544 iface->dup = g_desktop_app_info_dup;
1545 iface->equal = g_desktop_app_info_equal;
1546 iface->get_id = g_desktop_app_info_get_id;
1547 iface->get_name = g_desktop_app_info_get_name;
1548 iface->get_description = g_desktop_app_info_get_description;
1549 iface->get_executable = g_desktop_app_info_get_executable;
1550 iface->get_icon = g_desktop_app_info_get_icon;
1551 iface->launch = g_desktop_app_info_launch;
1552 iface->supports_uris = g_desktop_app_info_supports_uris;
1553 iface->supports_files = g_desktop_app_info_supports_files;
1554 iface->launch_uris = g_desktop_app_info_launch_uris;
1555 iface->should_show = g_desktop_app_info_should_show;
1556 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1557 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1558 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1559 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1560 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1564 app_info_in_list (GAppInfo *info,
1567 while (list != NULL)
1569 if (g_app_info_equal (info, list->data))
1578 * g_app_info_get_all_for_type:
1579 * @content_type: the content type to find a #GAppInfo for
1581 * Gets a list of all #GAppInfo s for a given content type.
1583 * Returns: #GList of #GAppInfo s for given @content_type
1584 * or %NULL on error.
1587 g_app_info_get_all_for_type (const char *content_type)
1589 GList *desktop_entries, *l;
1591 GDesktopAppInfo *info;
1593 g_return_val_if_fail (content_type != NULL, NULL);
1595 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1598 for (l = desktop_entries; l != NULL; l = l->next)
1600 char *desktop_entry = l->data;
1602 info = g_desktop_app_info_new (desktop_entry);
1605 if (app_info_in_list (G_APP_INFO (info), infos))
1606 g_object_unref (info);
1608 infos = g_list_prepend (infos, info);
1610 g_free (desktop_entry);
1613 g_list_free (desktop_entries);
1615 return g_list_reverse (infos);
1620 * g_app_info_get_default_for_type:
1621 * @content_type: the content type to find a #GAppInfo for
1622 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1625 * Gets the #GAppInfo that correspond to a given content type.
1627 * Returns: #GAppInfo for given @content_type or %NULL on error.
1630 g_app_info_get_default_for_type (const char *content_type,
1631 gboolean must_support_uris)
1633 GList *desktop_entries, *l;
1636 g_return_val_if_fail (content_type != NULL, NULL);
1638 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1641 for (l = desktop_entries; l != NULL; l = l->next)
1643 char *desktop_entry = l->data;
1645 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1648 if (must_support_uris && !g_app_info_supports_uris (info))
1650 g_object_unref (info);
1658 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1659 g_list_free (desktop_entries);
1666 * g_app_info_get_default_for_uri_scheme:
1667 * @uri_scheme: a string containing a URI scheme.
1669 * Gets the default application for launching applications
1670 * using this URI scheme.
1672 * TODO: This is currently unimplemented.
1677 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1679 /* TODO: Implement this using giomodules, reading the gconf settings
1680 * in /desktop/gnome/url-handlers
1687 get_apps_from_dir (GHashTable *apps,
1688 const char *dirname,
1692 const char *basename;
1693 char *filename, *subprefix, *desktop_id;
1695 GDesktopAppInfo *appinfo;
1697 dir = g_dir_open (dirname, 0, NULL);
1700 while ((basename = g_dir_read_name (dir)) != NULL)
1702 filename = g_build_filename (dirname, basename, NULL);
1703 if (g_str_has_suffix (basename, ".desktop"))
1705 desktop_id = g_strconcat (prefix, basename, NULL);
1707 /* Use _extended so we catch NULLs too (hidden) */
1708 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1710 appinfo = g_desktop_app_info_new_from_filename (filename);
1712 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1714 g_object_unref (appinfo);
1719 if (appinfo || hidden)
1721 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1725 /* Reuse instead of strdup here */
1726 appinfo->desktop_id = desktop_id;
1731 g_free (desktop_id);
1735 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1737 subprefix = g_strconcat (prefix, basename, "-", NULL);
1738 get_apps_from_dir (apps, filename, subprefix);
1750 * g_app_info_get_all:
1752 * Gets a list of all of the applications currently registered
1755 * For desktop files, this includes applications that have
1756 * <literal>NoDisplay=true</literal> set or are excluded from
1757 * display by means of <literal>OnlyShowIn</literal> or
1758 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1759 * The returned list does not include applications which have
1760 * the <literal>Hidden</literal> key set.
1762 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1765 g_app_info_get_all (void)
1767 const char * const *dirs;
1769 GHashTableIter iter;
1774 dirs = get_applications_search_path ();
1776 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1780 for (i = 0; dirs[i] != NULL; i++)
1781 get_apps_from_dir (apps, dirs[i], "");
1785 g_hash_table_iter_init (&iter, apps);
1786 while (g_hash_table_iter_next (&iter, NULL, &value))
1789 infos = g_list_prepend (infos, value);
1792 g_hash_table_destroy (apps);
1794 return g_list_reverse (infos);
1797 /* Cacheing of mimeinfo.cache and defaults.list files */
1801 GHashTable *mime_info_cache_map;
1802 GHashTable *defaults_list_map;
1803 time_t mime_info_cache_timestamp;
1804 time_t defaults_list_timestamp;
1808 GList *dirs; /* mimeinfo.cache and defaults.list */
1809 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1810 time_t last_stat_time;
1811 guint should_ping_mime_monitor : 1;
1814 static MimeInfoCache *mime_info_cache = NULL;
1815 G_LOCK_DEFINE_STATIC (mime_info_cache);
1817 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1818 const char *mime_type,
1819 char **new_desktop_file_ids);
1821 static MimeInfoCache * mime_info_cache_new (void);
1824 destroy_info_cache_value (gpointer key,
1828 g_list_foreach (value, (GFunc)g_free, NULL);
1829 g_list_free (value);
1833 destroy_info_cache_map (GHashTable *info_cache_map)
1835 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1836 g_hash_table_destroy (info_cache_map);
1840 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1841 const char *cache_file,
1847 filename = g_build_filename (dir->path, cache_file, NULL);
1849 if (g_stat (filename, &buf) < 0)
1856 if (buf.st_mtime != *timestamp)
1862 /* Call with lock held */
1864 remove_all (gpointer key,
1873 mime_info_cache_blow_global_cache (void)
1875 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1880 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1884 gchar *filename, **mime_types;
1891 if (dir->mime_info_cache_map != NULL &&
1892 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1893 &dir->mime_info_cache_timestamp))
1896 if (dir->mime_info_cache_map != NULL)
1897 destroy_info_cache_map (dir->mime_info_cache_map);
1899 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1900 (GDestroyNotify) g_free,
1903 key_file = g_key_file_new ();
1905 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1907 if (g_stat (filename, &buf) < 0)
1910 if (dir->mime_info_cache_timestamp > 0)
1911 mime_info_cache->should_ping_mime_monitor = TRUE;
1913 dir->mime_info_cache_timestamp = buf.st_mtime;
1915 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1920 if (load_error != NULL)
1923 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1926 if (load_error != NULL)
1929 for (i = 0; mime_types[i] != NULL; i++)
1931 gchar **desktop_file_ids;
1932 char *unaliased_type;
1933 desktop_file_ids = g_key_file_get_string_list (key_file,
1939 if (desktop_file_ids == NULL)
1942 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1943 mime_info_cache_dir_add_desktop_entries (dir,
1946 g_free (unaliased_type);
1948 g_strfreev (desktop_file_ids);
1951 g_strfreev (mime_types);
1952 g_key_file_free (key_file);
1957 g_key_file_free (key_file);
1959 if (mime_types != NULL)
1960 g_strfreev (mime_types);
1963 g_error_free (load_error);
1967 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1971 gchar *filename, **mime_types;
1972 char *unaliased_type;
1973 char **desktop_file_ids;
1980 if (dir->defaults_list_map != NULL &&
1981 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1982 &dir->defaults_list_timestamp))
1985 if (dir->defaults_list_map != NULL)
1986 g_hash_table_destroy (dir->defaults_list_map);
1988 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1989 g_free, (GDestroyNotify)g_strfreev);
1991 key_file = g_key_file_new ();
1993 filename = g_build_filename (dir->path, "defaults.list", NULL);
1994 if (g_stat (filename, &buf) < 0)
1997 if (dir->defaults_list_timestamp > 0)
1998 mime_info_cache->should_ping_mime_monitor = TRUE;
2000 dir->defaults_list_timestamp = buf.st_mtime;
2002 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2006 if (load_error != NULL)
2009 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2012 if (load_error != NULL)
2015 for (i = 0; mime_types[i] != NULL; i++)
2017 desktop_file_ids = g_key_file_get_string_list (key_file,
2018 DEFAULT_APPLICATIONS_GROUP,
2022 if (desktop_file_ids == NULL)
2025 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2026 g_hash_table_replace (dir->defaults_list_map,
2031 g_strfreev (mime_types);
2032 g_key_file_free (key_file);
2037 g_key_file_free (key_file);
2039 if (mime_types != NULL)
2040 g_strfreev (mime_types);
2043 g_error_free (load_error);
2046 static MimeInfoCacheDir *
2047 mime_info_cache_dir_new (const char *path)
2049 MimeInfoCacheDir *dir;
2051 dir = g_new0 (MimeInfoCacheDir, 1);
2052 dir->path = g_strdup (path);
2058 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2063 if (dir->mime_info_cache_map != NULL)
2065 destroy_info_cache_map (dir->mime_info_cache_map);
2066 dir->mime_info_cache_map = NULL;
2070 if (dir->defaults_list_map != NULL)
2072 g_hash_table_destroy (dir->defaults_list_map);
2073 dir->defaults_list_map = NULL;
2080 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2081 const char *mime_type,
2082 char **new_desktop_file_ids)
2084 GList *desktop_file_ids;
2087 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2090 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2092 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2093 desktop_file_ids = g_list_append (desktop_file_ids,
2094 g_strdup (new_desktop_file_ids[i]));
2097 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2101 mime_info_cache_init_dir_lists (void)
2103 const char * const *dirs;
2106 mime_info_cache = mime_info_cache_new ();
2108 dirs = get_applications_search_path ();
2110 for (i = 0; dirs[i] != NULL; i++)
2112 MimeInfoCacheDir *dir;
2114 dir = mime_info_cache_dir_new (dirs[i]);
2118 mime_info_cache_dir_init (dir);
2119 mime_info_cache_dir_init_defaults_list (dir);
2121 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2127 mime_info_cache_update_dir_lists (void)
2131 tmp = mime_info_cache->dirs;
2135 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2137 /* No need to do this if we had file monitors... */
2138 mime_info_cache_blow_global_cache ();
2139 mime_info_cache_dir_init (dir);
2140 mime_info_cache_dir_init_defaults_list (dir);
2147 mime_info_cache_init (void)
2149 G_LOCK (mime_info_cache);
2150 if (mime_info_cache == NULL)
2151 mime_info_cache_init_dir_lists ();
2157 if (now >= mime_info_cache->last_stat_time + 10)
2159 mime_info_cache_update_dir_lists ();
2160 mime_info_cache->last_stat_time = now;
2164 if (mime_info_cache->should_ping_mime_monitor)
2166 /* g_idle_add (emit_mime_changed, NULL); */
2167 mime_info_cache->should_ping_mime_monitor = FALSE;
2170 G_UNLOCK (mime_info_cache);
2173 static MimeInfoCache *
2174 mime_info_cache_new (void)
2176 MimeInfoCache *cache;
2178 cache = g_new0 (MimeInfoCache, 1);
2180 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2181 (GDestroyNotify) g_free,
2182 (GDestroyNotify) g_free);
2187 mime_info_cache_free (MimeInfoCache *cache)
2192 g_list_foreach (cache->dirs,
2193 (GFunc) mime_info_cache_dir_free,
2195 g_list_free (cache->dirs);
2196 g_hash_table_destroy (cache->global_defaults_cache);
2201 * mime_info_cache_reload:
2202 * @dir: directory path which needs reloading.
2204 * Reload the mime information for the @dir.
2207 mime_info_cache_reload (const char *dir)
2209 /* FIXME: just reload the dir that needs reloading,
2210 * don't blow the whole cache
2212 if (mime_info_cache != NULL)
2214 G_LOCK (mime_info_cache);
2215 mime_info_cache_free (mime_info_cache);
2216 mime_info_cache = NULL;
2217 G_UNLOCK (mime_info_cache);
2222 append_desktop_entry (GList *list,
2223 const char *desktop_entry)
2225 /* Add if not already in list, and valid */
2226 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2227 list = g_list_prepend (list, g_strdup (desktop_entry));
2233 * get_all_desktop_entries_for_mime_type:
2234 * @mime_type: a mime type.
2236 * Returns all the desktop ids for @mime_type. The desktop files
2237 * are listed in an order so that default applications are listed before
2238 * non-default ones, and handlers for inherited mimetypes are listed
2239 * after the base ones.
2241 * Return value: a #GList containing the desktop ids which claim
2242 * to handle @mime_type.
2245 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2247 GList *desktop_entries, *list, *dir_list, *tmp;
2248 MimeInfoCacheDir *dir;
2251 char **default_entries;
2254 mime_info_cache_init ();
2256 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2257 G_LOCK (mime_info_cache);
2259 desktop_entries = NULL;
2260 for (i = 0; mime_types[i] != NULL; i++)
2262 mime_type = mime_types[i];
2264 /* Go through all apps listed as defaults */
2265 for (dir_list = mime_info_cache->dirs;
2267 dir_list = dir_list->next)
2269 dir = dir_list->data;
2270 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2271 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2272 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2275 /* Go through all entries that support the mimetype */
2276 for (dir_list = mime_info_cache->dirs;
2278 dir_list = dir_list->next)
2280 dir = dir_list->data;
2282 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2283 for (tmp = list; tmp != NULL; tmp = tmp->next)
2284 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2288 G_UNLOCK (mime_info_cache);
2290 g_strfreev (mime_types);
2292 desktop_entries = g_list_reverse (desktop_entries);
2294 return desktop_entries;
2297 #define __G_DESKTOP_APP_INFO_C__
2298 #include "gioaliasdef.c"