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>
42 #include "giomodule-priv.h"
47 * SECTION:gdesktopappinfo
48 * @short_description: Application information from desktop files
49 * @include: gio/gdesktopappinfo.h
51 * #GDesktopAppInfo is an implementation of #GAppInfo based on
56 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
57 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
58 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
59 #define MIME_CACHE_GROUP "MIME Cache"
61 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
62 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type);
63 static void mime_info_cache_reload (const char *dir);
64 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
70 * Information about an installed application from a desktop file.
72 struct _GDesktopAppInfo
74 GObject parent_instance;
80 /* FIXME: what about GenericName ? */
94 guint startup_notify : 1;
95 /* FIXME: what about StartupWMClass ? */
98 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
99 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
100 g_desktop_app_info_iface_init))
103 search_path_init (gpointer data)
106 const char * const *data_dirs;
107 const char *user_data_dir;
110 data_dirs = g_get_system_data_dirs ();
111 length = g_strv_length ((char **) data_dirs);
113 args = g_new (char *, length + 2);
116 user_data_dir = g_get_user_data_dir ();
117 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
118 for (i = 0; i < length; i++)
119 args[j++] = g_build_filename (data_dirs[i],
120 "applications", NULL);
126 static const char * const *
127 get_applications_search_path (void)
129 static GOnce once_init = G_ONCE_INIT;
130 return g_once (&once_init, search_path_init, NULL);
134 g_desktop_app_info_finalize (GObject *object)
136 GDesktopAppInfo *info;
138 info = G_DESKTOP_APP_INFO (object);
140 g_free (info->desktop_id);
141 g_free (info->filename);
143 g_free (info->comment);
144 g_free (info->icon_name);
146 g_object_unref (info->icon);
147 g_strfreev (info->only_show_in);
148 g_strfreev (info->not_show_in);
149 g_free (info->try_exec);
151 g_free (info->binary);
154 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
158 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
160 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
162 gobject_class->finalize = g_desktop_app_info_finalize;
166 g_desktop_app_info_init (GDesktopAppInfo *local)
171 binary_from_exec (const char *exec)
173 const char *p, *start;
179 while (*p != ' ' && *p != 0)
182 return g_strndup (start, p - start);
187 * g_desktop_app_info_new_from_filename:
188 * @filename: a string containing a file name.
190 * Creates a new #GDesktopAppInfo.
192 * Returns: a new #GDesktopAppInfo or %NULL on error.
195 g_desktop_app_info_new_from_filename (const char *filename)
197 GDesktopAppInfo *info;
203 key_file = g_key_file_new ();
205 if (!g_key_file_load_from_file (key_file,
210 g_key_file_free (key_file);
214 start_group = g_key_file_get_start_group (key_file);
215 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
217 g_free (start_group);
218 g_key_file_free (key_file);
221 g_free (start_group);
223 type = g_key_file_get_string (key_file,
224 G_KEY_FILE_DESKTOP_GROUP,
225 G_KEY_FILE_DESKTOP_KEY_TYPE,
227 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
230 g_key_file_free (key_file);
235 try_exec = g_key_file_get_string (key_file,
236 G_KEY_FILE_DESKTOP_GROUP,
237 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
242 t = g_find_program_in_path (try_exec);
246 g_key_file_free (key_file);
252 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
253 info->filename = g_strdup (filename);
255 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
256 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
257 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
258 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
259 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);
260 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);
261 info->try_exec = try_exec;
262 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
263 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
264 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
265 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
266 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
268 g_key_file_free (key_file);
273 if (g_path_is_absolute (info->icon_name))
277 file = g_file_new_for_path (info->icon_name);
278 info->icon = g_file_icon_new (file);
279 g_object_unref (file);
282 info->icon = g_themed_icon_new (info->icon_name);
286 info->binary = binary_from_exec (info->exec);
292 * g_desktop_app_info_new:
293 * @desktop_id: the desktop file id
295 * Creates a new #GDesktopAppInfo.
297 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
300 g_desktop_app_info_new (const char *desktop_id)
302 GDesktopAppInfo *appinfo;
303 const char * const *dirs;
307 dirs = get_applications_search_path ();
309 basename = g_strdup (desktop_id);
311 for (i = 0; dirs[i] != NULL; i++)
316 filename = g_build_filename (dirs[i], desktop_id, NULL);
317 appinfo = g_desktop_app_info_new_from_filename (filename);
323 while ((p = strchr (p, '-')) != NULL)
327 filename = g_build_filename (dirs[i], basename, NULL);
328 appinfo = g_desktop_app_info_new_from_filename (filename);
343 appinfo->desktop_id = g_strdup (desktop_id);
345 if (g_desktop_app_info_get_is_hidden (appinfo))
347 g_object_unref (appinfo);
355 g_desktop_app_info_dup (GAppInfo *appinfo)
357 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
358 GDesktopAppInfo *new_info;
360 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
362 new_info->filename = g_strdup (info->filename);
363 new_info->desktop_id = g_strdup (info->desktop_id);
365 new_info->name = g_strdup (info->name);
366 new_info->comment = g_strdup (info->comment);
367 new_info->nodisplay = info->nodisplay;
368 new_info->icon_name = g_strdup (info->icon_name);
369 new_info->icon = g_object_ref (info->icon);
370 new_info->only_show_in = g_strdupv (info->only_show_in);
371 new_info->not_show_in = g_strdupv (info->not_show_in);
372 new_info->try_exec = g_strdup (info->try_exec);
373 new_info->exec = g_strdup (info->exec);
374 new_info->binary = g_strdup (info->binary);
375 new_info->path = g_strdup (info->path);
376 new_info->hidden = info->hidden;
377 new_info->terminal = info->terminal;
378 new_info->startup_notify = info->startup_notify;
380 return G_APP_INFO (new_info);
384 g_desktop_app_info_equal (GAppInfo *appinfo1,
387 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
388 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
390 if (info1->desktop_id == NULL ||
391 info2->desktop_id == NULL)
392 return info1 == info2;
394 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
398 g_desktop_app_info_get_id (GAppInfo *appinfo)
400 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
402 return info->desktop_id;
406 g_desktop_app_info_get_name (GAppInfo *appinfo)
408 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
410 if (info->name == NULL)
416 * g_desktop_app_info_get_is_hidden:
417 * @info: a #GDesktopAppInfo.
419 * A desktop file is hidden if the Hidden key in it is
422 * Returns: %TRUE if hidden, %FALSE otherwise.
425 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
431 g_desktop_app_info_get_description (GAppInfo *appinfo)
433 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
435 return info->comment;
439 g_desktop_app_info_get_executable (GAppInfo *appinfo)
441 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
447 g_desktop_app_info_get_icon (GAppInfo *appinfo)
449 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
455 expand_macro_single (char macro, char *uri)
461 file = g_file_new_for_uri (uri);
462 path = g_file_get_path (file);
463 g_object_unref (file);
469 result = g_shell_quote (uri);
474 result = g_shell_quote (path);
479 result = g_shell_quote (g_path_get_dirname (path));
484 result = g_shell_quote (g_path_get_basename (path));
494 expand_macro (char macro,
496 GDesktopAppInfo *info,
499 GList *uris = *uri_list;
502 g_return_if_fail (exec != NULL);
512 expanded = expand_macro_single (macro, uris->data);
515 g_string_append (exec, expanded);
529 expanded = expand_macro_single (macro, uris->data);
532 g_string_append (exec, expanded);
538 if (uris != NULL && expanded)
539 g_string_append_c (exec, ' ');
547 g_string_append (exec, "--icon ");
548 g_string_append (exec, info->icon_name);
554 g_string_append (exec, info->name);
559 g_string_append (exec, info->filename);
562 case 'm': /* deprecated */
566 g_string_append_c (exec, '%');
574 expand_application_parameters (GDesktopAppInfo *info,
580 GList *uri_list = *uris;
581 const char *p = info->exec;
582 GString *expanded_exec = g_string_new (NULL);
585 if (info->exec == NULL)
587 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
588 _("Desktop file didn't specify Exec field"));
594 if (p[0] == '%' && p[1] != '\0')
596 expand_macro (p[1], expanded_exec, info, uris);
600 g_string_append_c (expanded_exec, *p);
605 /* No file substitutions */
606 if (uri_list == *uris && uri_list != NULL)
608 /* If there is no macro default to %f. This is also what KDE does */
609 g_string_append_c (expanded_exec, ' ');
610 expand_macro ('f', expanded_exec, info, uris);
613 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
614 g_string_free (expanded_exec, TRUE);
619 prepend_terminal_to_vector (int *argc,
626 char **term_argv = NULL;
631 g_return_val_if_fail (argc != NULL, FALSE);
632 g_return_val_if_fail (argv != NULL, FALSE);
640 /* compute size if not given */
643 for (i = 0; the_argv[i] != NULL; i++)
649 term_argv = g_new0 (char *, 3);
651 check = g_find_program_in_path ("gnome-terminal");
654 term_argv[0] = check;
655 /* Note that gnome-terminal takes -x and
656 * as -e in gnome-terminal is broken we use that. */
657 term_argv[1] = g_strdup ("-x");
662 check = g_find_program_in_path ("nxterm");
664 check = g_find_program_in_path ("color-xterm");
666 check = g_find_program_in_path ("rxvt");
668 check = g_find_program_in_path ("xterm");
670 check = g_find_program_in_path ("dtterm");
673 check = g_strdup ("xterm");
674 g_warning ("couldn't find a terminal, falling back to xterm");
676 term_argv[0] = check;
677 term_argv[1] = g_strdup ("-e");
680 real_argc = term_argc + *argc;
681 real_argv = g_new (char *, real_argc + 1);
683 for (i = 0; i < term_argc; i++)
684 real_argv[i] = term_argv[i];
686 for (j = 0; j < *argc; j++, i++)
687 real_argv[i] = (char *)the_argv[j];
695 /* we use g_free here as we sucked all the inner strings
696 * out from it into real_argv */
701 #endif /* G_OS_WIN32 */
704 /* '=' is the new '\0'.
705 * DO NOT CALL unless at least one string ends with '='
708 is_env (const char *a,
713 if (*a == 0 || *b == 0)
726 /* free with g_strfreev */
728 replace_env_var (char **old_environ,
730 const char *new_value)
732 int length, new_length;
733 int index, new_index;
737 /* do two things at once:
738 * - discover the length of the environment ('length')
739 * - find the location (if any) of the env var ('index')
742 for (length = 0; old_environ[length]; length++)
744 /* if we already have it in our environment, replace */
745 if (is_env (old_environ[length], env_var))
750 /* no current env var, no desired env value.
753 if (new_value == NULL && index == -1)
756 /* in all cases now, we will be using a modified environment.
757 * determine its length and allocated it.
760 * new_index = location to insert, if any
761 * new_length = length of the new array
762 * new_environ = the pointer array for the new environment
765 if (new_value == NULL && index >= 0)
767 /* in this case, we will be removing an entry */
768 new_length = length - 1;
771 else if (new_value != NULL && index < 0)
773 /* in this case, we will be adding an entry to the end */
774 new_length = length + 1;
778 /* in this case, we will be replacing the existing entry */
784 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
785 new_environ[new_length] = NULL;
787 /* now we do the copying.
788 * for each entry in the new environment, we decide what to do
792 for (new_i = 0; new_i < new_length; new_i++)
794 if (new_i == new_index)
796 /* insert our new item */
797 new_environ[new_i] = g_strconcat (env_var,
802 /* if we had an old entry, skip it now */
808 /* if this is the old DESKTOP_STARTUP_ID, skip it */
812 /* copy an old item */
813 new_environ[new_i] = g_strdup (old_environ[i]);
818 g_strfreev (old_environ);
824 uri_list_segment_to_files (GList *start,
831 while (start != NULL && start != end)
833 file = g_file_new_for_uri ((char *)start->data);
834 res = g_list_prepend (res, file);
838 return g_list_reverse (res);
841 #ifdef HAVE__NSGETENVIRON
842 #define environ (*_NSGetEnviron())
843 #elif !defined(G_OS_WIN32)
845 /* According to the Single Unix Specification, environ is not in
846 * * any system header, although unistd.h often declares it.
848 extern char **environ;
852 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
854 GAppLaunchContext *launch_context,
857 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
858 gboolean completed = FALSE;
860 GList *launched_files;
867 g_return_val_if_fail (appinfo != NULL, FALSE);
875 if (!expand_application_parameters (info, &uris,
876 &argc, &argv, error))
879 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
881 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
882 _("Unable to find terminal required for application"));
889 launched_files = uri_list_segment_to_files (old_uris, uris);
891 display = g_app_launch_context_get_display (launch_context,
896 if (info->startup_notify)
897 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
901 if (display || sn_id)
905 envp = g_new0 (char *, 1);
907 envp = g_strdupv (environ);
911 envp = replace_env_var (envp,
916 envp = replace_env_var (envp,
917 "DESKTOP_STARTUP_ID",
923 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
924 g_list_free (launched_files);
927 if (!g_spawn_async (info->path, /* working directory */
930 G_SPAWN_SEARCH_PATH /* flags */,
931 NULL /* child_setup */,
933 NULL /* child_pid */,
938 g_app_launch_context_launch_failed (launch_context, sn_id);
952 while (uris != NULL);
964 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
966 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
969 ((strstr (info->exec, "%u") != NULL) ||
970 (strstr (info->exec, "%U") != NULL));
974 g_desktop_app_info_supports_files (GAppInfo *appinfo)
976 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
979 ((strstr (info->exec, "%f") != NULL) ||
980 (strstr (info->exec, "%F") != NULL));
984 g_desktop_app_info_launch (GAppInfo *appinfo,
986 GAppLaunchContext *launch_context,
996 uri = g_file_get_uri (files->data);
997 uris = g_list_prepend (uris, uri);
1001 uris = g_list_reverse (uris);
1003 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1005 g_list_foreach (uris, (GFunc)g_free, NULL);
1011 G_LOCK_DEFINE_STATIC (g_desktop_env);
1012 static gchar *g_desktop_env = NULL;
1015 * g_desktop_app_info_set_desktop_env:
1016 * @desktop_env: a string specifying what desktop this is
1018 * Sets the name of the desktop that the application is running in.
1019 * This is used by g_app_info_should_show() to evaluate the
1020 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1021 * desktop entry fields.
1023 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1024 * Menu specification</ulink> recognizes the following:
1026 * <member>GNOME</member>
1027 * <member>KDE</member>
1028 * <member>ROX</member>
1029 * <member>XFCE</member>
1030 * <member>Old</member>
1033 * Should be called only once; subsequent calls are ignored.
1036 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1038 G_LOCK (g_desktop_env);
1040 g_desktop_env = g_strdup (desktop_env);
1041 G_UNLOCK (g_desktop_env);
1045 g_desktop_app_info_should_show (GAppInfo *appinfo)
1047 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1049 const gchar *desktop_env;
1052 if (info->nodisplay)
1055 G_LOCK (g_desktop_env);
1056 desktop_env = g_desktop_env;
1057 G_UNLOCK (g_desktop_env);
1059 if (info->only_show_in)
1061 if (desktop_env == NULL)
1065 for (i = 0; info->only_show_in[i] != NULL; i++)
1067 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1077 if (info->not_show_in && desktop_env)
1079 for (i = 0; info->not_show_in[i] != NULL; i++)
1081 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1095 ensure_dir (DirType type,
1098 char *path, *display_name;
1101 if (type == APP_DIR)
1102 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1104 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1107 if (g_mkdir_with_parents (path, 0700) == 0)
1111 display_name = g_filename_display_name (path);
1112 if (type == APP_DIR)
1113 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1114 _("Can't create user application configuration folder %s: %s"),
1115 display_name, g_strerror (err));
1117 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1118 _("Can't create user MIME configuration folder %s: %s"),
1119 display_name, g_strerror (err));
1121 g_free (display_name);
1128 update_mimeapps_list (const char *desktop_id,
1129 const char *content_type,
1130 gboolean add_at_start,
1131 gboolean add_at_end,
1135 char *dirname, *filename;
1137 gboolean load_succeeded, res;
1140 gsize length, data_size;
1144 /* Don't add both at start and end */
1145 g_assert (!(add_at_start && add_at_end));
1147 dirname = ensure_dir (APP_DIR, error);
1151 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1154 key_file = g_key_file_new ();
1155 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1156 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1158 g_key_file_free (key_file);
1159 key_file = g_key_file_new ();
1162 /* Add to the right place in the list */
1165 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1166 content_type, &length, NULL);
1168 list = g_new (char *, 1 + length + 1);
1172 list[i++] = g_strdup (desktop_id);
1175 for (j = 0; old_list[j] != NULL; j++)
1177 if (strcmp (old_list[j], desktop_id) != 0)
1178 list[i++] = g_strdup (old_list[j]);
1182 list[i++] = g_strdup (desktop_id);
1185 g_strfreev (old_list);
1187 g_key_file_set_string_list (key_file,
1188 ADDED_ASSOCIATIONS_GROUP,
1190 (const char * const *)list, i);
1194 /* Remove from removed associations group (unless remove) */
1197 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1198 content_type, &length, NULL);
1200 list = g_new (char *, 1 + length + 1);
1204 list[i++] = g_strdup (desktop_id);
1207 for (j = 0; old_list[j] != NULL; j++)
1209 if (strcmp (old_list[j], desktop_id) != 0)
1210 list[i++] = g_strdup (old_list[j]);
1215 g_strfreev (old_list);
1217 if (list[0] == NULL)
1218 g_key_file_remove_key (key_file,
1219 REMOVED_ASSOCIATIONS_GROUP,
1223 g_key_file_set_string_list (key_file,
1224 REMOVED_ASSOCIATIONS_GROUP,
1226 (const char * const *)list, i);
1231 data = g_key_file_to_data (key_file, &data_size, error);
1232 g_key_file_free (key_file);
1234 res = g_file_set_contents (filename, data, data_size, error);
1236 mime_info_cache_reload (NULL);
1245 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1246 const char *content_type,
1249 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1251 if (!g_desktop_app_info_ensure_saved (info, error))
1254 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1258 update_program_done (GPid pid,
1262 /* Did the application exit correctly */
1263 if (WIFEXITED (status) &&
1264 WEXITSTATUS (status) == 0)
1266 /* Here we could clean out any caches in use */
1271 run_update_command (char *command,
1280 GError *error = NULL;
1283 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1285 if (g_spawn_async ("/", argv,
1287 G_SPAWN_SEARCH_PATH |
1288 G_SPAWN_STDOUT_TO_DEV_NULL |
1289 G_SPAWN_STDERR_TO_DEV_NULL |
1290 G_SPAWN_DO_NOT_REAP_CHILD,
1291 NULL, NULL, /* No setup function */
1294 g_child_watch_add (pid, update_program_done, NULL);
1297 /* If we get an error at this point, it's quite likely the user doesn't
1298 * have an installed copy of either 'update-mime-database' or
1299 * 'update-desktop-database'. I don't think we want to popup an error
1300 * dialog at this point, so we just do a g_warning to give the user a
1301 * chance of debugging it.
1303 g_warning ("%s", error->message);
1310 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1311 const char *extension,
1314 char *filename, *basename, *mimetype;
1318 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1321 dirname = ensure_dir (MIMETYPE_DIR, error);
1325 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1326 filename = g_build_filename (dirname, basename, NULL);
1330 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1332 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1337 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1338 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1339 " <mime-type type=\"%s\">\n"
1340 " <comment>%s document</comment>\n"
1341 " <glob pattern=\"*.%s\"/>\n"
1343 "</mime-info>\n", mimetype, extension, extension);
1345 g_file_set_contents (filename, contents, -1, NULL);
1348 run_update_command ("update-mime-database", "mime");
1352 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1362 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1363 const char *content_type,
1366 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1368 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1371 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1375 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1381 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1382 const char *content_type,
1385 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1387 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1390 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1394 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1400 char *data, *desktop_id;
1405 if (info->filename != NULL)
1408 /* This is only used for object created with
1409 * g_app_info_create_from_commandline. All other
1410 * object should have a filename
1413 dirname = ensure_dir (APP_DIR, error);
1417 key_file = g_key_file_new ();
1419 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1420 "Encoding", "UTF-8");
1421 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1422 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1423 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1424 G_KEY_FILE_DESKTOP_KEY_TYPE,
1425 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1427 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1428 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1430 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1431 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1433 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1434 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1436 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1437 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1439 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1440 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1442 data = g_key_file_to_data (key_file, &data_size, NULL);
1443 g_key_file_free (key_file);
1445 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1446 filename = g_build_filename (dirname, desktop_id, NULL);
1447 g_free (desktop_id);
1450 fd = g_mkstemp (filename);
1455 display_name = g_filename_display_name (filename);
1456 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1457 _("Can't create user desktop file %s"), display_name);
1458 g_free (display_name);
1464 desktop_id = g_path_get_basename (filename);
1468 res = g_file_set_contents (filename, data, data_size, error);
1471 g_free (desktop_id);
1476 info->filename = filename;
1477 info->desktop_id = desktop_id;
1479 run_update_command ("update-desktop-database", "applications");
1485 * g_app_info_create_from_commandline:
1486 * @commandline: the commandline to use
1487 * @application_name: the application name, or %NULL to use @commandline
1488 * @flags: flags that can specify details of the created #GAppInfo
1489 * @error: a #GError location to store the error occuring, %NULL to ignore.
1491 * Creates a new #GAppInfo from the given information.
1493 * Returns: new #GAppInfo for given command.
1496 g_app_info_create_from_commandline (const char *commandline,
1497 const char *application_name,
1498 GAppInfoCreateFlags flags,
1503 GDesktopAppInfo *info;
1505 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1507 info->filename = NULL;
1508 info->desktop_id = NULL;
1510 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1511 info->startup_notify = FALSE;
1512 info->hidden = FALSE;
1513 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1514 info->exec = g_strconcat (commandline, " %u", NULL);
1516 info->exec = g_strconcat (commandline, " %f", NULL);
1517 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1518 info->nodisplay = TRUE;
1519 info->binary = binary_from_exec (info->exec);
1521 if (application_name)
1522 info->name = g_strdup (application_name);
1525 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1526 split = g_strsplit (commandline, " ", 2);
1527 basename = g_path_get_basename (split[0]);
1529 info->name = basename;
1530 if (info->name == NULL)
1531 info->name = g_strdup ("custom");
1534 return G_APP_INFO (info);
1538 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1540 iface->dup = g_desktop_app_info_dup;
1541 iface->equal = g_desktop_app_info_equal;
1542 iface->get_id = g_desktop_app_info_get_id;
1543 iface->get_name = g_desktop_app_info_get_name;
1544 iface->get_description = g_desktop_app_info_get_description;
1545 iface->get_executable = g_desktop_app_info_get_executable;
1546 iface->get_icon = g_desktop_app_info_get_icon;
1547 iface->launch = g_desktop_app_info_launch;
1548 iface->supports_uris = g_desktop_app_info_supports_uris;
1549 iface->supports_files = g_desktop_app_info_supports_files;
1550 iface->launch_uris = g_desktop_app_info_launch_uris;
1551 iface->should_show = g_desktop_app_info_should_show;
1552 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1553 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1554 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1555 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1556 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1560 app_info_in_list (GAppInfo *info,
1563 while (list != NULL)
1565 if (g_app_info_equal (info, list->data))
1574 * g_app_info_get_all_for_type:
1575 * @content_type: the content type to find a #GAppInfo for
1577 * Gets a list of all #GAppInfo s for a given content type.
1579 * Returns: #GList of #GAppInfo s for given @content_type
1580 * or %NULL on error.
1583 g_app_info_get_all_for_type (const char *content_type)
1585 GList *desktop_entries, *l;
1587 GDesktopAppInfo *info;
1589 g_return_val_if_fail (content_type != NULL, NULL);
1591 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1594 for (l = desktop_entries; l != NULL; l = l->next)
1596 char *desktop_entry = l->data;
1598 info = g_desktop_app_info_new (desktop_entry);
1601 if (app_info_in_list (G_APP_INFO (info), infos))
1602 g_object_unref (info);
1604 infos = g_list_prepend (infos, info);
1606 g_free (desktop_entry);
1609 g_list_free (desktop_entries);
1611 return g_list_reverse (infos);
1616 * g_app_info_get_default_for_type:
1617 * @content_type: the content type to find a #GAppInfo for
1618 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1621 * Gets the #GAppInfo that correspond to a given content type.
1623 * Returns: #GAppInfo for given @content_type or %NULL on error.
1626 g_app_info_get_default_for_type (const char *content_type,
1627 gboolean must_support_uris)
1629 GList *desktop_entries, *l;
1632 g_return_val_if_fail (content_type != NULL, NULL);
1634 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1637 for (l = desktop_entries; l != NULL; l = l->next)
1639 char *desktop_entry = l->data;
1641 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1644 if (must_support_uris && !g_app_info_supports_uris (info))
1646 g_object_unref (info);
1654 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1655 g_list_free (desktop_entries);
1661 * g_app_info_get_default_for_uri_scheme:
1662 * @uri_scheme: a string containing a URI scheme.
1664 * Gets the default application for launching applications
1665 * using this URI scheme.
1667 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1670 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1672 static gsize lookup = 0;
1674 if (g_once_init_enter (&lookup))
1676 gsize setup_value = 1;
1677 GDesktopAppInfoLookup *lookup_instance;
1678 const char *use_this;
1679 GIOExtensionPoint *ep;
1680 GIOExtension *extension;
1683 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1685 /* Ensure vfs in modules loaded */
1686 _g_io_modules_ensure_loaded ();
1688 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1690 lookup_instance = NULL;
1693 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1695 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1698 if (lookup_instance == NULL)
1700 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1702 extension = l->data;
1703 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1704 if (lookup_instance != NULL)
1709 if (lookup_instance != NULL)
1710 setup_value = (gsize)lookup_instance;
1712 g_once_init_leave (&lookup, setup_value);
1718 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1724 get_apps_from_dir (GHashTable *apps,
1725 const char *dirname,
1729 const char *basename;
1730 char *filename, *subprefix, *desktop_id;
1732 GDesktopAppInfo *appinfo;
1734 dir = g_dir_open (dirname, 0, NULL);
1737 while ((basename = g_dir_read_name (dir)) != NULL)
1739 filename = g_build_filename (dirname, basename, NULL);
1740 if (g_str_has_suffix (basename, ".desktop"))
1742 desktop_id = g_strconcat (prefix, basename, NULL);
1744 /* Use _extended so we catch NULLs too (hidden) */
1745 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1747 appinfo = g_desktop_app_info_new_from_filename (filename);
1749 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1751 g_object_unref (appinfo);
1756 if (appinfo || hidden)
1758 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1762 /* Reuse instead of strdup here */
1763 appinfo->desktop_id = desktop_id;
1768 g_free (desktop_id);
1772 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1774 subprefix = g_strconcat (prefix, basename, "-", NULL);
1775 get_apps_from_dir (apps, filename, subprefix);
1787 * g_app_info_get_all:
1789 * Gets a list of all of the applications currently registered
1792 * For desktop files, this includes applications that have
1793 * <literal>NoDisplay=true</literal> set or are excluded from
1794 * display by means of <literal>OnlyShowIn</literal> or
1795 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1796 * The returned list does not include applications which have
1797 * the <literal>Hidden</literal> key set.
1799 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1802 g_app_info_get_all (void)
1804 const char * const *dirs;
1806 GHashTableIter iter;
1811 dirs = get_applications_search_path ();
1813 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1817 for (i = 0; dirs[i] != NULL; i++)
1818 get_apps_from_dir (apps, dirs[i], "");
1822 g_hash_table_iter_init (&iter, apps);
1823 while (g_hash_table_iter_next (&iter, NULL, &value))
1826 infos = g_list_prepend (infos, value);
1829 g_hash_table_destroy (apps);
1831 return g_list_reverse (infos);
1834 /* Cacheing of mimeinfo.cache and defaults.list files */
1838 GHashTable *mime_info_cache_map;
1839 GHashTable *defaults_list_map;
1840 GHashTable *mimeapps_list_added_map;
1841 GHashTable *mimeapps_list_removed_map;
1842 time_t mime_info_cache_timestamp;
1843 time_t defaults_list_timestamp;
1844 time_t mimeapps_list_timestamp;
1848 GList *dirs; /* mimeinfo.cache and defaults.list */
1849 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1850 time_t last_stat_time;
1851 guint should_ping_mime_monitor : 1;
1854 static MimeInfoCache *mime_info_cache = NULL;
1855 G_LOCK_DEFINE_STATIC (mime_info_cache);
1857 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1858 const char *mime_type,
1859 char **new_desktop_file_ids);
1861 static MimeInfoCache * mime_info_cache_new (void);
1864 destroy_info_cache_value (gpointer key,
1868 g_list_foreach (value, (GFunc)g_free, NULL);
1869 g_list_free (value);
1873 destroy_info_cache_map (GHashTable *info_cache_map)
1875 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1876 g_hash_table_destroy (info_cache_map);
1880 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1881 const char *cache_file,
1887 filename = g_build_filename (dir->path, cache_file, NULL);
1889 if (g_stat (filename, &buf) < 0)
1896 if (buf.st_mtime != *timestamp)
1902 /* Call with lock held */
1904 remove_all (gpointer key,
1913 mime_info_cache_blow_global_cache (void)
1915 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1920 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1924 gchar *filename, **mime_types;
1931 if (dir->mime_info_cache_map != NULL &&
1932 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1933 &dir->mime_info_cache_timestamp))
1936 if (dir->mime_info_cache_map != NULL)
1937 destroy_info_cache_map (dir->mime_info_cache_map);
1939 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1940 (GDestroyNotify) g_free,
1943 key_file = g_key_file_new ();
1945 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1947 if (g_stat (filename, &buf) < 0)
1950 if (dir->mime_info_cache_timestamp > 0)
1951 mime_info_cache->should_ping_mime_monitor = TRUE;
1953 dir->mime_info_cache_timestamp = buf.st_mtime;
1955 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1960 if (load_error != NULL)
1963 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1966 if (load_error != NULL)
1969 for (i = 0; mime_types[i] != NULL; i++)
1971 gchar **desktop_file_ids;
1972 char *unaliased_type;
1973 desktop_file_ids = g_key_file_get_string_list (key_file,
1979 if (desktop_file_ids == NULL)
1982 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1983 mime_info_cache_dir_add_desktop_entries (dir,
1986 g_free (unaliased_type);
1988 g_strfreev (desktop_file_ids);
1991 g_strfreev (mime_types);
1992 g_key_file_free (key_file);
1997 g_key_file_free (key_file);
1999 if (mime_types != NULL)
2000 g_strfreev (mime_types);
2003 g_error_free (load_error);
2007 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2011 gchar *filename, **mime_types;
2012 char *unaliased_type;
2013 char **desktop_file_ids;
2020 if (dir->defaults_list_map != NULL &&
2021 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2022 &dir->defaults_list_timestamp))
2025 if (dir->defaults_list_map != NULL)
2026 g_hash_table_destroy (dir->defaults_list_map);
2027 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2028 g_free, (GDestroyNotify)g_strfreev);
2031 key_file = g_key_file_new ();
2033 filename = g_build_filename (dir->path, "defaults.list", NULL);
2034 if (g_stat (filename, &buf) < 0)
2037 if (dir->defaults_list_timestamp > 0)
2038 mime_info_cache->should_ping_mime_monitor = TRUE;
2040 dir->defaults_list_timestamp = buf.st_mtime;
2042 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2046 if (load_error != NULL)
2049 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2051 if (mime_types != NULL)
2053 for (i = 0; mime_types[i] != NULL; i++)
2055 desktop_file_ids = g_key_file_get_string_list (key_file,
2056 DEFAULT_APPLICATIONS_GROUP,
2060 if (desktop_file_ids == NULL)
2063 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2064 g_hash_table_replace (dir->defaults_list_map,
2069 g_strfreev (mime_types);
2072 g_key_file_free (key_file);
2077 g_key_file_free (key_file);
2079 if (mime_types != NULL)
2080 g_strfreev (mime_types);
2083 g_error_free (load_error);
2087 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2091 gchar *filename, **mime_types;
2092 char *unaliased_type;
2093 char **desktop_file_ids;
2100 if (dir->mimeapps_list_added_map != NULL &&
2101 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2102 &dir->mimeapps_list_timestamp))
2105 if (dir->mimeapps_list_added_map != NULL)
2106 g_hash_table_destroy (dir->mimeapps_list_added_map);
2107 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2108 g_free, (GDestroyNotify)g_strfreev);
2110 if (dir->mimeapps_list_removed_map != NULL)
2111 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2112 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2113 g_free, (GDestroyNotify)g_strfreev);
2115 key_file = g_key_file_new ();
2117 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2118 if (g_stat (filename, &buf) < 0)
2121 if (dir->mimeapps_list_timestamp > 0)
2122 mime_info_cache->should_ping_mime_monitor = TRUE;
2124 dir->mimeapps_list_timestamp = buf.st_mtime;
2126 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2130 if (load_error != NULL)
2133 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2135 if (mime_types != NULL)
2137 for (i = 0; mime_types[i] != NULL; i++)
2139 desktop_file_ids = g_key_file_get_string_list (key_file,
2140 ADDED_ASSOCIATIONS_GROUP,
2144 if (desktop_file_ids == NULL)
2147 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2148 g_hash_table_replace (dir->mimeapps_list_added_map,
2153 g_strfreev (mime_types);
2156 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2158 if (mime_types != NULL)
2160 for (i = 0; mime_types[i] != NULL; i++)
2162 desktop_file_ids = g_key_file_get_string_list (key_file,
2163 REMOVED_ASSOCIATIONS_GROUP,
2167 if (desktop_file_ids == NULL)
2170 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2171 g_hash_table_replace (dir->mimeapps_list_removed_map,
2176 g_strfreev (mime_types);
2179 g_key_file_free (key_file);
2184 g_key_file_free (key_file);
2186 if (mime_types != NULL)
2187 g_strfreev (mime_types);
2190 g_error_free (load_error);
2193 static MimeInfoCacheDir *
2194 mime_info_cache_dir_new (const char *path)
2196 MimeInfoCacheDir *dir;
2198 dir = g_new0 (MimeInfoCacheDir, 1);
2199 dir->path = g_strdup (path);
2205 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2210 if (dir->mime_info_cache_map != NULL)
2212 destroy_info_cache_map (dir->mime_info_cache_map);
2213 dir->mime_info_cache_map = NULL;
2217 if (dir->defaults_list_map != NULL)
2219 g_hash_table_destroy (dir->defaults_list_map);
2220 dir->defaults_list_map = NULL;
2223 if (dir->mimeapps_list_added_map != NULL)
2225 g_hash_table_destroy (dir->mimeapps_list_added_map);
2226 dir->mimeapps_list_added_map = NULL;
2229 if (dir->mimeapps_list_removed_map != NULL)
2231 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2232 dir->mimeapps_list_removed_map = NULL;
2239 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2240 const char *mime_type,
2241 char **new_desktop_file_ids)
2243 GList *desktop_file_ids;
2246 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2249 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2251 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2252 desktop_file_ids = g_list_append (desktop_file_ids,
2253 g_strdup (new_desktop_file_ids[i]));
2256 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2260 mime_info_cache_init_dir_lists (void)
2262 const char * const *dirs;
2265 mime_info_cache = mime_info_cache_new ();
2267 dirs = get_applications_search_path ();
2269 for (i = 0; dirs[i] != NULL; i++)
2271 MimeInfoCacheDir *dir;
2273 dir = mime_info_cache_dir_new (dirs[i]);
2277 mime_info_cache_dir_init (dir);
2278 mime_info_cache_dir_init_defaults_list (dir);
2279 mime_info_cache_dir_init_mimeapps_list (dir);
2281 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2287 mime_info_cache_update_dir_lists (void)
2291 tmp = mime_info_cache->dirs;
2295 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2297 /* No need to do this if we had file monitors... */
2298 mime_info_cache_blow_global_cache ();
2299 mime_info_cache_dir_init (dir);
2300 mime_info_cache_dir_init_defaults_list (dir);
2301 mime_info_cache_dir_init_mimeapps_list (dir);
2308 mime_info_cache_init (void)
2310 G_LOCK (mime_info_cache);
2311 if (mime_info_cache == NULL)
2312 mime_info_cache_init_dir_lists ();
2318 if (now >= mime_info_cache->last_stat_time + 10)
2320 mime_info_cache_update_dir_lists ();
2321 mime_info_cache->last_stat_time = now;
2325 if (mime_info_cache->should_ping_mime_monitor)
2327 /* g_idle_add (emit_mime_changed, NULL); */
2328 mime_info_cache->should_ping_mime_monitor = FALSE;
2331 G_UNLOCK (mime_info_cache);
2334 static MimeInfoCache *
2335 mime_info_cache_new (void)
2337 MimeInfoCache *cache;
2339 cache = g_new0 (MimeInfoCache, 1);
2341 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2342 (GDestroyNotify) g_free,
2343 (GDestroyNotify) g_free);
2348 mime_info_cache_free (MimeInfoCache *cache)
2353 g_list_foreach (cache->dirs,
2354 (GFunc) mime_info_cache_dir_free,
2356 g_list_free (cache->dirs);
2357 g_hash_table_destroy (cache->global_defaults_cache);
2362 * mime_info_cache_reload:
2363 * @dir: directory path which needs reloading.
2365 * Reload the mime information for the @dir.
2368 mime_info_cache_reload (const char *dir)
2370 /* FIXME: just reload the dir that needs reloading,
2371 * don't blow the whole cache
2373 if (mime_info_cache != NULL)
2375 G_LOCK (mime_info_cache);
2376 mime_info_cache_free (mime_info_cache);
2377 mime_info_cache = NULL;
2378 G_UNLOCK (mime_info_cache);
2383 append_desktop_entry (GList *list,
2384 const char *desktop_entry,
2385 GList *removed_entries)
2387 /* Add if not already in list, and valid */
2388 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2389 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2390 list = g_list_prepend (list, g_strdup (desktop_entry));
2396 * get_all_desktop_entries_for_mime_type:
2397 * @mime_type: a mime type.
2399 * Returns all the desktop ids for @mime_type. The desktop files
2400 * are listed in an order so that default applications are listed before
2401 * non-default ones, and handlers for inherited mimetypes are listed
2402 * after the base ones.
2404 * Return value: a #GList containing the desktop ids which claim
2405 * to handle @mime_type.
2408 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2410 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2411 MimeInfoCacheDir *dir;
2414 char **default_entries;
2415 char **removed_associations;
2418 mime_info_cache_init ();
2420 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2421 G_LOCK (mime_info_cache);
2423 removed_entries = NULL;
2424 desktop_entries = NULL;
2425 for (i = 0; mime_types[i] != NULL; i++)
2427 mime_type = mime_types[i];
2429 /* Go through all apps listed as defaults */
2430 for (dir_list = mime_info_cache->dirs;
2432 dir_list = dir_list->next)
2434 dir = dir_list->data;
2436 /* First added associations from mimeapps.list */
2437 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2438 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2439 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2441 /* Then removed assiciations from mimeapps.list */
2442 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2443 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2444 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2446 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2447 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2448 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2449 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2452 /* Go through all entries that support the mimetype */
2453 for (dir_list = mime_info_cache->dirs;
2455 dir_list = dir_list->next)
2457 dir = dir_list->data;
2459 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2460 for (tmp = list; tmp != NULL; tmp = tmp->next)
2461 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2465 G_UNLOCK (mime_info_cache);
2467 g_strfreev (mime_types);
2469 g_list_free (removed_entries);
2471 desktop_entries = g_list_reverse (desktop_entries);
2473 return desktop_entries;
2476 /* GDesktopAppInfoLookup interface: */
2478 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2479 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2480 gpointer class_data);
2483 g_desktop_app_info_lookup_get_type (void)
2485 static GType desktop_app_info_lookup_type = 0;
2487 if (! desktop_app_info_lookup_type)
2489 static const GTypeInfo desktop_app_info_lookup_info =
2491 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2492 g_desktop_app_info_lookup_base_init, /* base_init */
2493 NULL, /* base_finalize */
2494 g_desktop_app_info_lookup_class_init,
2495 NULL, /* class_finalize */
2496 NULL, /* class_data */
2498 0, /* n_preallocs */
2502 desktop_app_info_lookup_type =
2503 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2504 &desktop_app_info_lookup_info, 0);
2506 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2509 return desktop_app_info_lookup_type;
2513 g_desktop_app_info_lookup_class_init (gpointer g_class,
2514 gpointer class_data)
2519 g_desktop_app_info_lookup_base_init (gpointer g_class)
2524 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2525 const char *uri_scheme)
2527 GDesktopAppInfoLookupIface *iface;
2529 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), FALSE);
2531 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2533 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2536 #define __G_DESKTOP_APP_INFO_C__
2537 #include "gioaliasdef.c"