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 ? */
91 guint startup_notify : 1;
92 /* FIXME: what about StartupWMClass ? */
95 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
96 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
97 g_desktop_app_info_iface_init))
100 search_path_init (gpointer data)
103 const char * const *data_dirs;
104 const char *user_data_dir;
107 data_dirs = g_get_system_data_dirs ();
108 length = g_strv_length ((char **) data_dirs);
110 args = g_new (char *, length + 2);
113 user_data_dir = g_get_user_data_dir ();
114 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
115 for (i = 0; i < length; i++)
116 args[j++] = g_build_filename (data_dirs[i],
117 "applications", NULL);
123 static const char * const *
124 get_applications_search_path (void)
126 static GOnce once_init = G_ONCE_INIT;
127 return g_once (&once_init, search_path_init, NULL);
131 g_desktop_app_info_finalize (GObject *object)
133 GDesktopAppInfo *info;
135 info = G_DESKTOP_APP_INFO (object);
137 g_free (info->desktop_id);
138 g_free (info->filename);
140 g_free (info->comment);
141 g_free (info->icon_name);
143 g_object_unref (info->icon);
144 g_strfreev (info->only_show_in);
145 g_strfreev (info->not_show_in);
146 g_strfreev (info->mimetypes);
147 g_free (info->try_exec);
149 g_free (info->binary);
152 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
156 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
158 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
160 gobject_class->finalize = g_desktop_app_info_finalize;
164 g_desktop_app_info_init (GDesktopAppInfo *local)
169 * g_desktop_app_info_new_from_filename:
170 * @filename: a string containing a file name.
172 * Creates a new #GDesktopAppInfo.
174 * Returns: a new #GDesktopAppInfo or %NULL on error.
177 g_desktop_app_info_new_from_filename (const char *filename)
179 GDesktopAppInfo *info;
185 key_file = g_key_file_new ();
187 if (!g_key_file_load_from_file (key_file,
192 g_key_file_free (key_file);
196 start_group = g_key_file_get_start_group (key_file);
197 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
199 g_free (start_group);
200 g_key_file_free (key_file);
203 g_free (start_group);
205 type = g_key_file_get_string (key_file,
206 G_KEY_FILE_DESKTOP_GROUP,
207 G_KEY_FILE_DESKTOP_KEY_TYPE,
209 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
212 g_key_file_free (key_file);
217 try_exec = g_key_file_get_string (key_file,
218 G_KEY_FILE_DESKTOP_GROUP,
219 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
224 t = g_find_program_in_path (try_exec);
228 g_key_file_free (key_file);
234 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
235 info->filename = g_strdup (filename);
237 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
238 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
239 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
240 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
241 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);
242 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);
243 info->mimetypes = g_key_file_get_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL, NULL);
244 info->try_exec = try_exec;
245 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
246 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
247 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
248 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
249 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
251 g_key_file_free (key_file);
256 if (g_path_is_absolute (info->icon_name))
260 file = g_file_new_for_path (info->icon_name);
261 info->icon = g_file_icon_new (file);
262 g_object_unref (file);
265 info->icon = g_themed_icon_new (info->icon_name);
276 while (*p != ' ' && *p != 0)
279 info->binary = g_strndup (start, p - start);
286 * g_desktop_app_info_new:
287 * @desktop_id: the desktop file id
289 * Creates a new #GDesktopAppInfo.
291 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
294 g_desktop_app_info_new (const char *desktop_id)
296 GDesktopAppInfo *appinfo;
297 const char * const *dirs;
301 dirs = get_applications_search_path ();
303 basename = g_strdup (desktop_id);
305 for (i = 0; dirs[i] != NULL; i++)
310 filename = g_build_filename (dirs[i], desktop_id, NULL);
311 appinfo = g_desktop_app_info_new_from_filename (filename);
317 while ((p = strchr (p, '-')) != NULL)
321 filename = g_build_filename (dirs[i], basename, NULL);
322 appinfo = g_desktop_app_info_new_from_filename (filename);
337 appinfo->desktop_id = g_strdup (desktop_id);
339 if (g_desktop_app_info_get_is_hidden (appinfo))
341 g_object_unref (appinfo);
349 g_desktop_app_info_dup (GAppInfo *appinfo)
351 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
352 GDesktopAppInfo *new_info;
354 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
356 new_info->filename = g_strdup (info->filename);
357 new_info->desktop_id = g_strdup (info->desktop_id);
359 new_info->name = g_strdup (info->name);
360 new_info->comment = g_strdup (info->comment);
361 new_info->nodisplay = info->nodisplay;
362 new_info->icon_name = g_strdup (info->icon_name);
363 new_info->icon = g_object_ref (info->icon);
364 new_info->only_show_in = g_strdupv (info->only_show_in);
365 new_info->not_show_in = g_strdupv (info->not_show_in);
366 new_info->try_exec = g_strdup (info->try_exec);
367 new_info->exec = g_strdup (info->exec);
368 new_info->binary = g_strdup (info->binary);
369 new_info->path = g_strdup (info->path);
370 new_info->hidden = info->hidden;
371 new_info->terminal = info->terminal;
372 new_info->startup_notify = info->startup_notify;
374 return G_APP_INFO (new_info);
378 g_desktop_app_info_equal (GAppInfo *appinfo1,
381 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
382 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
384 if (info1->desktop_id == NULL ||
385 info2->desktop_id == NULL)
388 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
392 g_desktop_app_info_get_id (GAppInfo *appinfo)
394 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
396 return info->desktop_id;
400 g_desktop_app_info_get_name (GAppInfo *appinfo)
402 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
404 if (info->name == NULL)
410 * g_desktop_app_info_get_is_hidden:
411 * @info: a #GDesktopAppInfo.
413 * A desktop file is hidden if the Hidden key in it is
416 * Returns: %TRUE if hidden, %FALSE otherwise.
419 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
425 g_desktop_app_info_get_description (GAppInfo *appinfo)
427 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
429 return info->comment;
433 g_desktop_app_info_get_executable (GAppInfo *appinfo)
435 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
441 g_desktop_app_info_get_icon (GAppInfo *appinfo)
443 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
449 expand_macro_single (char macro, GFile *file)
454 path = g_file_get_path (file);
455 uri = g_file_get_uri (file);
461 result = g_shell_quote (uri);
466 result = g_shell_quote (path);
471 result = g_shell_quote (g_path_get_dirname (path));
476 result = g_shell_quote (g_path_get_basename (path));
487 expand_macro (char macro,
489 GDesktopAppInfo *info,
492 GList *files = *file_list;
495 g_return_if_fail (exec != NULL);
505 expanded = expand_macro_single (macro, files->data);
508 g_string_append (exec, expanded);
522 expanded = expand_macro_single (macro, files->data);
525 g_string_append (exec, expanded);
531 if (files != NULL && expanded)
532 g_string_append_c (exec, ' ');
540 g_string_append (exec, "--icon ");
541 g_string_append (exec, info->icon_name);
547 g_string_append (exec, info->name);
552 g_string_append (exec, info->filename);
555 case 'm': /* deprecated */
559 g_string_append_c (exec, '%');
567 expand_application_parameters (GDesktopAppInfo *info,
573 GList *file_list = *files;
574 const char *p = info->exec;
575 GString *expanded_exec = g_string_new (NULL);
578 if (info->exec == NULL)
580 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
581 _("Desktop file didn't specify Exec field"));
587 if (p[0] == '%' && p[1] != '\0')
589 expand_macro (p[1], expanded_exec, info, files);
593 g_string_append_c (expanded_exec, *p);
598 /* No file substitutions */
599 if (file_list == *files && file_list != NULL)
601 /* If there is no macro default to %f. This is also what KDE does */
602 g_string_append_c (expanded_exec, ' ');
603 expand_macro ('f', expanded_exec, info, files);
606 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
607 g_string_free (expanded_exec, TRUE);
612 prepend_terminal_to_vector (int *argc,
619 char **term_argv = NULL;
624 g_return_val_if_fail (argc != NULL, FALSE);
625 g_return_val_if_fail (argv != NULL, FALSE);
633 /* compute size if not given */
636 for (i = 0; the_argv[i] != NULL; i++)
642 term_argv = g_new0 (char *, 3);
644 check = g_find_program_in_path ("gnome-terminal");
647 term_argv[0] = check;
648 /* Note that gnome-terminal takes -x and
649 * as -e in gnome-terminal is broken we use that. */
650 term_argv[1] = g_strdup ("-x");
655 check = g_find_program_in_path ("nxterm");
657 check = g_find_program_in_path ("color-xterm");
659 check = g_find_program_in_path ("rxvt");
661 check = g_find_program_in_path ("xterm");
663 check = g_find_program_in_path ("dtterm");
666 check = g_strdup ("xterm");
667 g_warning ("couldn't find a terminal, falling back to xterm");
669 term_argv[0] = check;
670 term_argv[1] = g_strdup ("-e");
673 real_argc = term_argc + *argc;
674 real_argv = g_new (char *, real_argc + 1);
676 for (i = 0; i < term_argc; i++)
677 real_argv[i] = term_argv[i];
679 for (j = 0; j < *argc; j++, i++)
680 real_argv[i] = (char *)the_argv[j];
688 /* we use g_free here as we sucked all the inner strings
689 * out from it into real_argv */
694 #endif /* G_OS_WIN32 */
697 /* '=' is the new '\0'.
698 * DO NOT CALL unless at least one string ends with '='
701 is_env (const char *a,
706 if (*a == 0 || *b == 0)
719 /* free with g_strfreev */
721 replace_env_var (char **old_environ,
723 const char *new_value)
725 int length, new_length;
726 int index, new_index;
730 /* do two things at once:
731 * - discover the length of the environment ('length')
732 * - find the location (if any) of the env var ('index')
735 for (length = 0; old_environ[length]; length++)
737 /* if we already have it in our environment, replace */
738 if (is_env (old_environ[length], env_var))
743 /* no current env var, no desired env value.
746 if (new_value == NULL && index == -1)
749 /* in all cases now, we will be using a modified environment.
750 * determine its length and allocated it.
753 * new_index = location to insert, if any
754 * new_length = length of the new array
755 * new_environ = the pointer array for the new environment
758 if (new_value == NULL && index >= 0)
760 /* in this case, we will be removing an entry */
761 new_length = length - 1;
764 else if (new_value != NULL && index < 0)
766 /* in this case, we will be adding an entry to the end */
767 new_length = length + 1;
771 /* in this case, we will be replacing the existing entry */
777 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
778 new_environ[new_length] = NULL;
780 /* now we do the copying.
781 * for each entry in the new environment, we decide what to do
785 for (new_i = 0; new_i < new_length; new_i++)
787 if (new_i == new_index)
789 /* insert our new item */
790 new_environ[new_i] = g_strconcat (env_var,
795 /* if we had an old entry, skip it now */
801 /* if this is the old DESKTOP_STARTUP_ID, skip it */
805 /* copy an old item */
806 new_environ[new_i] = g_strdup (old_environ[i]);
811 g_strfreev (old_environ);
817 dup_list_segment (GList *start,
823 while (start != NULL && start != end)
825 res = g_list_prepend (res, start->data);
829 return g_list_reverse (res);
832 #ifdef HAVE__NSGETENVIRON
833 #define environ (*_NSGetEnviron())
834 #elif !defined(G_OS_WIN32)
836 /* According to the Single Unix Specification, environ is not in
837 * * any system header, although unistd.h often declares it.
839 extern char **environ;
843 g_desktop_app_info_launch (GAppInfo *appinfo,
845 GAppLaunchContext *launch_context,
848 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
849 gboolean completed = FALSE;
851 GList *launched_files;
858 g_return_val_if_fail (appinfo != NULL, FALSE);
866 if (!expand_application_parameters (info, &files,
867 &argc, &argv, error))
870 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
872 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
873 _("Unable to find terminal required for application"));
880 launched_files = dup_list_segment (old_files, files);
882 display = g_app_launch_context_get_display (launch_context,
887 if (info->startup_notify)
888 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
892 if (display || sn_id)
896 envp = g_new0 (char *, 1);
898 envp = g_strdupv (environ);
902 envp = replace_env_var (envp,
907 envp = replace_env_var (envp,
908 "DESKTOP_STARTUP_ID",
914 g_list_free (launched_files);
917 if (!g_spawn_async (info->path, /* working directory */
920 G_SPAWN_SEARCH_PATH /* flags */,
921 NULL /* child_setup */,
923 NULL /* child_pid */,
928 g_app_launch_context_launch_failed (launch_context, sn_id);
942 while (files != NULL);
954 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
956 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
959 ((strstr (info->exec, "%u") != NULL) ||
960 (strstr (info->exec, "%U") != NULL));
964 g_desktop_app_info_supports_files (GAppInfo *appinfo)
966 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
969 ((strstr (info->exec, "%f") != NULL) ||
970 (strstr (info->exec, "%F") != NULL));
974 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
976 GAppLaunchContext *launch_context,
986 file = g_file_new_for_uri (uris->data);
988 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
991 files = g_list_prepend (files, file);
994 files = g_list_reverse (files);
996 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
998 g_list_foreach (files, (GFunc)g_object_unref, NULL);
1004 G_LOCK_DEFINE_STATIC (g_desktop_env);
1005 static gchar *g_desktop_env = NULL;
1008 * g_desktop_app_info_set_desktop_env:
1009 * @desktop_env: a string specifying what desktop this is
1011 * Sets the name of the desktop that the application is running in.
1012 * This is used by g_app_info_should_show() to evaluate the
1013 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1014 * desktop entry fields.
1016 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1017 * Menu specification</ulink> recognizes the following:
1019 * <member>GNOME</member>
1020 * <member>KDE</member>
1021 * <member>ROX</member>
1022 * <member>XFCE</member>
1023 * <member>Old</member>
1026 * Should be called only once; subsequent calls are ignored.
1029 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1031 G_LOCK (g_desktop_env);
1033 g_desktop_env = g_strdup (desktop_env);
1034 G_UNLOCK (g_desktop_env);
1038 g_desktop_app_info_should_show (GAppInfo *appinfo)
1040 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1042 const gchar *desktop_env;
1045 if (info->nodisplay)
1048 G_LOCK (g_desktop_env);
1049 desktop_env = g_desktop_env;
1050 G_UNLOCK (g_desktop_env);
1052 if (info->only_show_in)
1054 if (desktop_env == NULL)
1058 for (i = 0; info->only_show_in[i] != NULL; i++)
1060 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1070 if (info->not_show_in && desktop_env)
1072 for (i = 0; info->not_show_in[i] != NULL; i++)
1074 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1088 ensure_dir (DirType type,
1091 char *path, *display_name;
1094 if (type == APP_DIR)
1095 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1097 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1100 if (g_mkdir_with_parents (path, 0700) == 0)
1104 display_name = g_filename_display_name (path);
1105 if (type == APP_DIR)
1106 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1107 _("Can't create user application configuration folder %s: %s"),
1108 display_name, g_strerror (err));
1110 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1111 _("Can't create user MIME configuration folder %s: %s"),
1112 display_name, g_strerror (err));
1114 g_free (display_name);
1121 update_default_list (const char *desktop_id,
1122 const char *content_type,
1126 char *dirname, *filename;
1128 gboolean load_succeeded, res;
1131 gsize length, data_size;
1135 dirname = ensure_dir (APP_DIR, error);
1139 filename = g_build_filename (dirname, "defaults.list", NULL);
1142 key_file = g_key_file_new ();
1143 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1144 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1146 g_key_file_free (key_file);
1147 key_file = g_key_file_new ();
1151 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1152 content_type, &length, NULL);
1154 list = g_new (char *, 1 + length + 1);
1158 list[i++] = g_strdup (desktop_id);
1161 for (j = 0; old_list[j] != NULL; j++)
1163 if (strcmp (old_list[j], desktop_id) != 0)
1164 list[i++] = g_strdup (old_list[j]);
1169 g_strfreev (old_list);
1171 g_key_file_set_string_list (key_file,
1172 DEFAULT_APPLICATIONS_GROUP,
1174 (const char * const *)list, i);
1178 data = g_key_file_to_data (key_file, &data_size, error);
1179 g_key_file_free (key_file);
1181 res = g_file_set_contents (filename, data, data_size, error);
1183 mime_info_cache_reload (NULL);
1192 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1193 const char *content_type,
1196 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1198 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1201 return update_default_list (info->desktop_id, content_type, TRUE, error);
1205 update_program_done (GPid pid,
1209 /* Did the application exit correctly */
1210 if (WIFEXITED (status) &&
1211 WEXITSTATUS (status) == 0)
1213 /* Here we could clean out any caches in use */
1218 run_update_command (char *command,
1227 GError *error = NULL;
1230 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1232 if (g_spawn_async ("/", argv,
1234 G_SPAWN_SEARCH_PATH |
1235 G_SPAWN_STDOUT_TO_DEV_NULL |
1236 G_SPAWN_STDERR_TO_DEV_NULL |
1237 G_SPAWN_DO_NOT_REAP_CHILD,
1238 NULL, NULL, /* No setup function */
1241 g_child_watch_add (pid, update_program_done, NULL);
1244 /* If we get an error at this point, it's quite likely the user doesn't
1245 * have an installed copy of either 'update-mime-database' or
1246 * 'update-desktop-database'. I don't think we want to popup an error
1247 * dialog at this point, so we just do a g_warning to give the user a
1248 * chance of debugging it.
1250 g_warning ("%s", error->message);
1257 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1258 const char *extension,
1261 char *filename, *basename, *mimetype;
1265 dirname = ensure_dir (MIMETYPE_DIR, error);
1269 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1270 filename = g_build_filename (dirname, basename, NULL);
1274 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1276 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1281 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1282 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1283 " <mime-type type=\"%s\">\n"
1284 " <comment>%s document</comment>\n"
1285 " <glob pattern=\"*.%s\"/>\n"
1287 "</mime-info>\n", mimetype, extension, extension);
1289 g_file_set_contents (filename, contents, -1, NULL);
1292 run_update_command ("update-mime-database", "mime");
1296 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1306 g_desktop_app_info_supports_mimetype (GDesktopAppInfo *appinfo,
1307 const char *mimetype)
1311 if (appinfo->mimetypes == NULL)
1314 for (i = 0; appinfo->mimetypes[i] != NULL; i++)
1316 if (strcmp (appinfo->mimetypes[i], mimetype) == 0)
1324 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1325 const char *content_type,
1328 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1330 char *new_mimetypes, *old_mimetypes, *content;
1334 if (g_desktop_app_info_supports_mimetype (info, content_type))
1335 return TRUE; /* Already supported */
1337 keyfile = g_key_file_new ();
1338 if (!g_key_file_load_from_file (keyfile, info->filename,
1339 G_KEY_FILE_KEEP_COMMENTS |
1340 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1342 g_key_file_free (keyfile);
1346 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1347 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1348 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1349 g_free (old_mimetypes);
1350 g_free (new_mimetypes);
1352 content = g_key_file_to_data (keyfile, NULL, NULL);
1353 g_key_file_free (keyfile);
1355 dirname = ensure_dir (APP_DIR, error);
1362 filename = g_build_filename (dirname, info->desktop_id, NULL);
1365 if (!g_file_set_contents (filename, content, -1, error))
1374 run_update_command ("update-desktop-database", "applications");
1379 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1381 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1384 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1385 return g_str_has_prefix (info->filename, user_dirname);
1389 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1390 const char *content_type,
1393 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1395 char *new_mimetypes, *old_mimetypes, *content;
1400 if (!g_desktop_app_info_supports_mimetype (info, content_type))
1401 return TRUE; /* Already not supported */
1403 keyfile = g_key_file_new ();
1404 if (!g_key_file_load_from_file (keyfile, info->filename,
1405 G_KEY_FILE_KEEP_COMMENTS |
1406 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1408 g_key_file_free (keyfile);
1412 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1413 new_mimetypes = g_strdup (old_mimetypes);
1416 found = strstr (new_mimetypes, content_type);
1417 if (found && *(found + strlen (content_type)) == ';')
1419 char *rest = found + strlen (content_type) + 1;
1420 memmove (found, rest, strlen (rest) + 1);
1422 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1423 g_free (old_mimetypes);
1424 g_free (new_mimetypes);
1426 content = g_key_file_to_data (keyfile, NULL, NULL);
1427 g_key_file_free (keyfile);
1429 dirname = ensure_dir (APP_DIR, error);
1436 filename = g_build_filename (dirname, info->desktop_id, NULL);
1438 if (!g_file_set_contents (filename, content, -1, error))
1447 run_update_command ("update-desktop-database", "applications");
1449 return update_default_list (info->desktop_id, content_type, FALSE, error);
1453 * g_app_info_create_from_commandline:
1454 * @commandline: the commandline to use
1455 * @application_name: the application name, or %NULL to use @commandline
1456 * @flags: flags that can specify details of the created #GAppInfo
1457 * @error: a #GError location to store the error occuring, %NULL to ignore.
1459 * Creates a new #GAppInfo from the given information.
1461 * Returns: new #GAppInfo for given command.
1464 g_app_info_create_from_commandline (const char *commandline,
1465 const char *application_name,
1466 GAppInfoCreateFlags flags,
1472 char *basename, *exec, *filename, *comment;
1473 char *data, *desktop_id;
1476 GDesktopAppInfo *info;
1479 dirname = ensure_dir (APP_DIR, error);
1483 key_file = g_key_file_new ();
1485 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1486 "Encoding", "UTF-8");
1487 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1488 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1489 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1490 G_KEY_FILE_DESKTOP_KEY_TYPE,
1491 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1492 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1493 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1494 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1496 exec = g_strconcat (commandline, " %f", NULL);
1497 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1498 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1501 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1502 split = g_strsplit (commandline, " ", 2);
1503 basename = g_path_get_basename (split[0]);
1505 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1506 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1508 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1509 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1510 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1513 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1514 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1516 data = g_key_file_to_data (key_file, &data_size, NULL);
1517 g_key_file_free (key_file);
1519 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1521 filename = g_build_filename (dirname, desktop_id, NULL);
1522 g_free (desktop_id);
1525 fd = g_mkstemp (filename);
1530 display_name = g_filename_display_name (filename);
1531 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1532 _("Can't create user desktop file %s"), display_name);
1533 g_free (display_name);
1539 desktop_id = g_path_get_basename (filename);
1543 res = g_file_set_contents (filename, data, data_size, error);
1546 g_free (desktop_id);
1551 run_update_command ("update-desktop-database", "applications");
1553 info = g_desktop_app_info_new_from_filename (filename);
1556 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1557 _("Can't load just created desktop file"));
1559 info->desktop_id = g_strdup (desktop_id);
1561 g_free (desktop_id);
1563 return G_APP_INFO (info);
1568 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1570 iface->dup = g_desktop_app_info_dup;
1571 iface->equal = g_desktop_app_info_equal;
1572 iface->get_id = g_desktop_app_info_get_id;
1573 iface->get_name = g_desktop_app_info_get_name;
1574 iface->get_description = g_desktop_app_info_get_description;
1575 iface->get_executable = g_desktop_app_info_get_executable;
1576 iface->get_icon = g_desktop_app_info_get_icon;
1577 iface->launch = g_desktop_app_info_launch;
1578 iface->supports_uris = g_desktop_app_info_supports_uris;
1579 iface->supports_files = g_desktop_app_info_supports_files;
1580 iface->launch_uris = g_desktop_app_info_launch_uris;
1581 iface->should_show = g_desktop_app_info_should_show;
1582 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1583 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1584 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1585 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1586 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1590 app_info_in_list (GAppInfo *info,
1593 while (list != NULL)
1595 if (g_app_info_equal (info, list->data))
1604 * g_app_info_get_all_for_type:
1605 * @content_type: the content type to find a #GAppInfo for
1607 * Gets a list of all #GAppInfo s for a given content type.
1609 * Returns: #GList of #GAppInfo s for given @content_type
1610 * or %NULL on error.
1613 g_app_info_get_all_for_type (const char *content_type)
1615 GList *desktop_entries, *l;
1617 GDesktopAppInfo *info;
1619 g_return_val_if_fail (content_type != NULL, NULL);
1621 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1624 for (l = desktop_entries; l != NULL; l = l->next)
1626 char *desktop_entry = l->data;
1628 info = g_desktop_app_info_new (desktop_entry);
1631 if (app_info_in_list (G_APP_INFO (info), infos))
1632 g_object_unref (info);
1634 infos = g_list_prepend (infos, info);
1636 g_free (desktop_entry);
1639 g_list_free (desktop_entries);
1641 return g_list_reverse (infos);
1646 * g_app_info_get_default_for_type:
1647 * @content_type: the content type to find a #GAppInfo for
1648 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1651 * Gets the #GAppInfo that correspond to a given content type.
1653 * Returns: #GAppInfo for given @content_type or %NULL on error.
1656 g_app_info_get_default_for_type (const char *content_type,
1657 gboolean must_support_uris)
1659 GList *desktop_entries, *l;
1662 g_return_val_if_fail (content_type != NULL, NULL);
1664 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1667 for (l = desktop_entries; l != NULL; l = l->next)
1669 char *desktop_entry = l->data;
1671 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1674 if (must_support_uris && !g_app_info_supports_uris (info))
1676 g_object_unref (info);
1684 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1685 g_list_free (desktop_entries);
1692 * g_app_info_get_default_for_uri_scheme:
1693 * @uri_scheme: a string containing a URI scheme.
1695 * Gets the default application for launching applications
1696 * using this URI scheme.
1698 * TODO: This is currently unimplemented.
1703 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1705 /* TODO: Implement this using giomodules, reading the gconf settings
1706 * in /desktop/gnome/url-handlers
1713 get_apps_from_dir (GHashTable *apps,
1714 const char *dirname,
1718 const char *basename;
1719 char *filename, *subprefix, *desktop_id;
1721 GDesktopAppInfo *appinfo;
1723 dir = g_dir_open (dirname, 0, NULL);
1726 while ((basename = g_dir_read_name (dir)) != NULL)
1728 filename = g_build_filename (dirname, basename, NULL);
1729 if (g_str_has_suffix (basename, ".desktop"))
1731 desktop_id = g_strconcat (prefix, basename, NULL);
1733 /* Use _extended so we catch NULLs too (hidden) */
1734 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1736 appinfo = g_desktop_app_info_new_from_filename (filename);
1738 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1740 g_object_unref (appinfo);
1745 if (appinfo || hidden)
1747 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1751 /* Reuse instead of strdup here */
1752 appinfo->desktop_id = desktop_id;
1757 g_free (desktop_id);
1761 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1763 subprefix = g_strconcat (prefix, basename, "-", NULL);
1764 get_apps_from_dir (apps, filename, subprefix);
1776 * g_app_info_get_all:
1778 * Gets a list of all of the applications currently registered
1781 * For desktop files, this includes applications that have
1782 * <literal>NoDisplay=true</literal> set or are excluded from
1783 * display by means of <literal>OnlyShowIn</literal> or
1784 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1785 * The returned list does not include applications which have
1786 * the <literal>Hidden</literal> key set.
1788 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1791 g_app_info_get_all (void)
1793 const char * const *dirs;
1795 GHashTableIter iter;
1800 dirs = get_applications_search_path ();
1802 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1806 for (i = 0; dirs[i] != NULL; i++)
1807 get_apps_from_dir (apps, dirs[i], "");
1811 g_hash_table_iter_init (&iter, apps);
1812 while (g_hash_table_iter_next (&iter, NULL, &value))
1815 infos = g_list_prepend (infos, value);
1818 g_hash_table_destroy (apps);
1820 return g_list_reverse (infos);
1823 /* Cacheing of mimeinfo.cache and defaults.list files */
1827 GHashTable *mime_info_cache_map;
1828 GHashTable *defaults_list_map;
1829 time_t mime_info_cache_timestamp;
1830 time_t defaults_list_timestamp;
1834 GList *dirs; /* mimeinfo.cache and defaults.list */
1835 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1836 time_t last_stat_time;
1837 guint should_ping_mime_monitor : 1;
1840 static MimeInfoCache *mime_info_cache = NULL;
1841 G_LOCK_DEFINE_STATIC (mime_info_cache);
1843 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1844 const char *mime_type,
1845 char **new_desktop_file_ids);
1847 static MimeInfoCache * mime_info_cache_new (void);
1850 destroy_info_cache_value (gpointer key,
1854 g_list_foreach (value, (GFunc)g_free, NULL);
1855 g_list_free (value);
1859 destroy_info_cache_map (GHashTable *info_cache_map)
1861 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1862 g_hash_table_destroy (info_cache_map);
1866 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1867 const char *cache_file,
1873 filename = g_build_filename (dir->path, cache_file, NULL);
1875 if (g_stat (filename, &buf) < 0)
1882 if (buf.st_mtime != *timestamp)
1888 /* Call with lock held */
1890 remove_all (gpointer key,
1899 mime_info_cache_blow_global_cache (void)
1901 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1906 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1910 gchar *filename, **mime_types;
1917 if (dir->mime_info_cache_map != NULL &&
1918 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1919 &dir->mime_info_cache_timestamp))
1922 if (dir->mime_info_cache_map != NULL)
1923 destroy_info_cache_map (dir->mime_info_cache_map);
1925 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1926 (GDestroyNotify) g_free,
1929 key_file = g_key_file_new ();
1931 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1933 if (g_stat (filename, &buf) < 0)
1936 if (dir->mime_info_cache_timestamp > 0)
1937 mime_info_cache->should_ping_mime_monitor = TRUE;
1939 dir->mime_info_cache_timestamp = buf.st_mtime;
1941 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1946 if (load_error != NULL)
1949 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1952 if (load_error != NULL)
1955 for (i = 0; mime_types[i] != NULL; i++)
1957 gchar **desktop_file_ids;
1958 char *unaliased_type;
1959 desktop_file_ids = g_key_file_get_string_list (key_file,
1965 if (desktop_file_ids == NULL)
1968 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1969 mime_info_cache_dir_add_desktop_entries (dir,
1972 g_free (unaliased_type);
1974 g_strfreev (desktop_file_ids);
1977 g_strfreev (mime_types);
1978 g_key_file_free (key_file);
1983 g_key_file_free (key_file);
1985 if (mime_types != NULL)
1986 g_strfreev (mime_types);
1989 g_error_free (load_error);
1993 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1997 gchar *filename, **mime_types;
1998 char *unaliased_type;
1999 char **desktop_file_ids;
2006 if (dir->defaults_list_map != NULL &&
2007 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2008 &dir->defaults_list_timestamp))
2011 if (dir->defaults_list_map != NULL)
2012 g_hash_table_destroy (dir->defaults_list_map);
2014 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2015 g_free, (GDestroyNotify)g_strfreev);
2017 key_file = g_key_file_new ();
2019 filename = g_build_filename (dir->path, "defaults.list", NULL);
2020 if (g_stat (filename, &buf) < 0)
2023 if (dir->defaults_list_timestamp > 0)
2024 mime_info_cache->should_ping_mime_monitor = TRUE;
2026 dir->defaults_list_timestamp = buf.st_mtime;
2028 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2032 if (load_error != NULL)
2035 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2038 if (load_error != NULL)
2041 for (i = 0; mime_types[i] != NULL; i++)
2043 desktop_file_ids = g_key_file_get_string_list (key_file,
2044 DEFAULT_APPLICATIONS_GROUP,
2048 if (desktop_file_ids == NULL)
2051 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2052 g_hash_table_replace (dir->defaults_list_map,
2057 g_strfreev (mime_types);
2058 g_key_file_free (key_file);
2063 g_key_file_free (key_file);
2065 if (mime_types != NULL)
2066 g_strfreev (mime_types);
2069 g_error_free (load_error);
2072 static MimeInfoCacheDir *
2073 mime_info_cache_dir_new (const char *path)
2075 MimeInfoCacheDir *dir;
2077 dir = g_new0 (MimeInfoCacheDir, 1);
2078 dir->path = g_strdup (path);
2084 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2089 if (dir->mime_info_cache_map != NULL)
2091 destroy_info_cache_map (dir->mime_info_cache_map);
2092 dir->mime_info_cache_map = NULL;
2096 if (dir->defaults_list_map != NULL)
2098 g_hash_table_destroy (dir->defaults_list_map);
2099 dir->defaults_list_map = NULL;
2106 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2107 const char *mime_type,
2108 char **new_desktop_file_ids)
2110 GList *desktop_file_ids;
2113 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2116 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2118 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2119 desktop_file_ids = g_list_append (desktop_file_ids,
2120 g_strdup (new_desktop_file_ids[i]));
2123 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2127 mime_info_cache_init_dir_lists (void)
2129 const char * const *dirs;
2132 mime_info_cache = mime_info_cache_new ();
2134 dirs = get_applications_search_path ();
2136 for (i = 0; dirs[i] != NULL; i++)
2138 MimeInfoCacheDir *dir;
2140 dir = mime_info_cache_dir_new (dirs[i]);
2144 mime_info_cache_dir_init (dir);
2145 mime_info_cache_dir_init_defaults_list (dir);
2147 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2153 mime_info_cache_update_dir_lists (void)
2157 tmp = mime_info_cache->dirs;
2161 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2163 /* No need to do this if we had file monitors... */
2164 mime_info_cache_blow_global_cache ();
2165 mime_info_cache_dir_init (dir);
2166 mime_info_cache_dir_init_defaults_list (dir);
2173 mime_info_cache_init (void)
2175 G_LOCK (mime_info_cache);
2176 if (mime_info_cache == NULL)
2177 mime_info_cache_init_dir_lists ();
2183 if (now >= mime_info_cache->last_stat_time + 10)
2185 mime_info_cache_update_dir_lists ();
2186 mime_info_cache->last_stat_time = now;
2190 if (mime_info_cache->should_ping_mime_monitor)
2192 /* g_idle_add (emit_mime_changed, NULL); */
2193 mime_info_cache->should_ping_mime_monitor = FALSE;
2196 G_UNLOCK (mime_info_cache);
2199 static MimeInfoCache *
2200 mime_info_cache_new (void)
2202 MimeInfoCache *cache;
2204 cache = g_new0 (MimeInfoCache, 1);
2206 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2207 (GDestroyNotify) g_free,
2208 (GDestroyNotify) g_free);
2213 mime_info_cache_free (MimeInfoCache *cache)
2218 g_list_foreach (cache->dirs,
2219 (GFunc) mime_info_cache_dir_free,
2221 g_list_free (cache->dirs);
2222 g_hash_table_destroy (cache->global_defaults_cache);
2227 * mime_info_cache_reload:
2228 * @dir: directory path which needs reloading.
2230 * Reload the mime information for the @dir.
2233 mime_info_cache_reload (const char *dir)
2235 /* FIXME: just reload the dir that needs reloading,
2236 * don't blow the whole cache
2238 if (mime_info_cache != NULL)
2240 G_LOCK (mime_info_cache);
2241 mime_info_cache_free (mime_info_cache);
2242 mime_info_cache = NULL;
2243 G_UNLOCK (mime_info_cache);
2248 append_desktop_entry (GList *list,
2249 const char *desktop_entry)
2251 /* Add if not already in list, and valid */
2252 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2253 list = g_list_prepend (list, g_strdup (desktop_entry));
2259 * get_all_desktop_entries_for_mime_type:
2260 * @mime_type: a mime type.
2262 * Returns all the desktop ids for @mime_type. The desktop files
2263 * are listed in an order so that default applications are listed before
2264 * non-default ones, and handlers for inherited mimetypes are listed
2265 * after the base ones.
2267 * Return value: a #GList containing the desktop ids which claim
2268 * to handle @mime_type.
2271 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2273 GList *desktop_entries, *list, *dir_list, *tmp;
2274 MimeInfoCacheDir *dir;
2277 char **default_entries;
2280 mime_info_cache_init ();
2282 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2283 G_LOCK (mime_info_cache);
2285 desktop_entries = NULL;
2286 for (i = 0; mime_types[i] != NULL; i++)
2288 mime_type = mime_types[i];
2290 /* Go through all apps listed as defaults */
2291 for (dir_list = mime_info_cache->dirs;
2293 dir_list = dir_list->next)
2295 dir = dir_list->data;
2296 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2297 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2298 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2301 /* Go through all entries that support the mimetype */
2302 for (dir_list = mime_info_cache->dirs;
2304 dir_list = dir_list->next)
2306 dir = dir_list->data;
2308 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2309 for (tmp = list; tmp != NULL; tmp = tmp->next)
2310 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2314 G_UNLOCK (mime_info_cache);
2316 g_strfreev (mime_types);
2318 desktop_entries = g_list_reverse (desktop_entries);
2320 return desktop_entries;
2323 #define __G_DESKTOP_APP_INFO_C__
2324 #include "gioaliasdef.c"