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 (errsv),
1114 _("Can't create user application configuration folder %s: %s"),
1115 display_name, g_strerror (errsv));
1117 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1118 _("Can't create user MIME configuration folder %s: %s"),
1119 display_name, g_strerror (errsv));
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->nodisplay = TRUE;
1518 info->binary = binary_from_exec (info->exec);
1520 if (application_name)
1521 info->name = g_strdup (application_name);
1524 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1525 split = g_strsplit (commandline, " ", 2);
1526 basename = g_path_get_basename (split[0]);
1528 info->name = basename;
1529 if (info->name == NULL)
1530 info->name = g_strdup ("custom");
1532 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
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. A URI scheme is the initial part
1666 * of the URI, up to but not including the ':', e.g. "http",
1669 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1672 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1674 static gsize lookup = 0;
1676 if (g_once_init_enter (&lookup))
1678 gsize setup_value = 1;
1679 GDesktopAppInfoLookup *lookup_instance;
1680 const char *use_this;
1681 GIOExtensionPoint *ep;
1682 GIOExtension *extension;
1685 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1687 /* Ensure vfs in modules loaded */
1688 _g_io_modules_ensure_loaded ();
1690 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1692 lookup_instance = NULL;
1695 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1697 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1700 if (lookup_instance == NULL)
1702 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1704 extension = l->data;
1705 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1706 if (lookup_instance != NULL)
1711 if (lookup_instance != NULL)
1712 setup_value = (gsize)lookup_instance;
1714 g_once_init_leave (&lookup, setup_value);
1720 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1726 get_apps_from_dir (GHashTable *apps,
1727 const char *dirname,
1731 const char *basename;
1732 char *filename, *subprefix, *desktop_id;
1734 GDesktopAppInfo *appinfo;
1736 dir = g_dir_open (dirname, 0, NULL);
1739 while ((basename = g_dir_read_name (dir)) != NULL)
1741 filename = g_build_filename (dirname, basename, NULL);
1742 if (g_str_has_suffix (basename, ".desktop"))
1744 desktop_id = g_strconcat (prefix, basename, NULL);
1746 /* Use _extended so we catch NULLs too (hidden) */
1747 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1749 appinfo = g_desktop_app_info_new_from_filename (filename);
1751 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1753 g_object_unref (appinfo);
1758 if (appinfo || hidden)
1760 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1764 /* Reuse instead of strdup here */
1765 appinfo->desktop_id = desktop_id;
1770 g_free (desktop_id);
1774 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1776 subprefix = g_strconcat (prefix, basename, "-", NULL);
1777 get_apps_from_dir (apps, filename, subprefix);
1789 * g_app_info_get_all:
1791 * Gets a list of all of the applications currently registered
1794 * For desktop files, this includes applications that have
1795 * <literal>NoDisplay=true</literal> set or are excluded from
1796 * display by means of <literal>OnlyShowIn</literal> or
1797 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1798 * The returned list does not include applications which have
1799 * the <literal>Hidden</literal> key set.
1801 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1804 g_app_info_get_all (void)
1806 const char * const *dirs;
1808 GHashTableIter iter;
1813 dirs = get_applications_search_path ();
1815 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1819 for (i = 0; dirs[i] != NULL; i++)
1820 get_apps_from_dir (apps, dirs[i], "");
1824 g_hash_table_iter_init (&iter, apps);
1825 while (g_hash_table_iter_next (&iter, NULL, &value))
1828 infos = g_list_prepend (infos, value);
1831 g_hash_table_destroy (apps);
1833 return g_list_reverse (infos);
1836 /* Cacheing of mimeinfo.cache and defaults.list files */
1840 GHashTable *mime_info_cache_map;
1841 GHashTable *defaults_list_map;
1842 GHashTable *mimeapps_list_added_map;
1843 GHashTable *mimeapps_list_removed_map;
1844 time_t mime_info_cache_timestamp;
1845 time_t defaults_list_timestamp;
1846 time_t mimeapps_list_timestamp;
1850 GList *dirs; /* mimeinfo.cache and defaults.list */
1851 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1852 time_t last_stat_time;
1853 guint should_ping_mime_monitor : 1;
1856 static MimeInfoCache *mime_info_cache = NULL;
1857 G_LOCK_DEFINE_STATIC (mime_info_cache);
1859 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1860 const char *mime_type,
1861 char **new_desktop_file_ids);
1863 static MimeInfoCache * mime_info_cache_new (void);
1866 destroy_info_cache_value (gpointer key,
1870 g_list_foreach (value, (GFunc)g_free, NULL);
1871 g_list_free (value);
1875 destroy_info_cache_map (GHashTable *info_cache_map)
1877 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1878 g_hash_table_destroy (info_cache_map);
1882 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1883 const char *cache_file,
1889 filename = g_build_filename (dir->path, cache_file, NULL);
1891 if (g_stat (filename, &buf) < 0)
1898 if (buf.st_mtime != *timestamp)
1904 /* Call with lock held */
1906 remove_all (gpointer key,
1915 mime_info_cache_blow_global_cache (void)
1917 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1922 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1926 gchar *filename, **mime_types;
1933 if (dir->mime_info_cache_map != NULL &&
1934 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1935 &dir->mime_info_cache_timestamp))
1938 if (dir->mime_info_cache_map != NULL)
1939 destroy_info_cache_map (dir->mime_info_cache_map);
1941 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1942 (GDestroyNotify) g_free,
1945 key_file = g_key_file_new ();
1947 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1949 if (g_stat (filename, &buf) < 0)
1952 if (dir->mime_info_cache_timestamp > 0)
1953 mime_info_cache->should_ping_mime_monitor = TRUE;
1955 dir->mime_info_cache_timestamp = buf.st_mtime;
1957 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1962 if (load_error != NULL)
1965 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1968 if (load_error != NULL)
1971 for (i = 0; mime_types[i] != NULL; i++)
1973 gchar **desktop_file_ids;
1974 char *unaliased_type;
1975 desktop_file_ids = g_key_file_get_string_list (key_file,
1981 if (desktop_file_ids == NULL)
1984 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1985 mime_info_cache_dir_add_desktop_entries (dir,
1988 g_free (unaliased_type);
1990 g_strfreev (desktop_file_ids);
1993 g_strfreev (mime_types);
1994 g_key_file_free (key_file);
1999 g_key_file_free (key_file);
2001 if (mime_types != NULL)
2002 g_strfreev (mime_types);
2005 g_error_free (load_error);
2009 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2013 gchar *filename, **mime_types;
2014 char *unaliased_type;
2015 char **desktop_file_ids;
2022 if (dir->defaults_list_map != NULL &&
2023 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2024 &dir->defaults_list_timestamp))
2027 if (dir->defaults_list_map != NULL)
2028 g_hash_table_destroy (dir->defaults_list_map);
2029 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2030 g_free, (GDestroyNotify)g_strfreev);
2033 key_file = g_key_file_new ();
2035 filename = g_build_filename (dir->path, "defaults.list", NULL);
2036 if (g_stat (filename, &buf) < 0)
2039 if (dir->defaults_list_timestamp > 0)
2040 mime_info_cache->should_ping_mime_monitor = TRUE;
2042 dir->defaults_list_timestamp = buf.st_mtime;
2044 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2048 if (load_error != NULL)
2051 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2053 if (mime_types != NULL)
2055 for (i = 0; mime_types[i] != NULL; i++)
2057 desktop_file_ids = g_key_file_get_string_list (key_file,
2058 DEFAULT_APPLICATIONS_GROUP,
2062 if (desktop_file_ids == NULL)
2065 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2066 g_hash_table_replace (dir->defaults_list_map,
2071 g_strfreev (mime_types);
2074 g_key_file_free (key_file);
2079 g_key_file_free (key_file);
2081 if (mime_types != NULL)
2082 g_strfreev (mime_types);
2085 g_error_free (load_error);
2089 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2093 gchar *filename, **mime_types;
2094 char *unaliased_type;
2095 char **desktop_file_ids;
2102 if (dir->mimeapps_list_added_map != NULL &&
2103 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2104 &dir->mimeapps_list_timestamp))
2107 if (dir->mimeapps_list_added_map != NULL)
2108 g_hash_table_destroy (dir->mimeapps_list_added_map);
2109 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2110 g_free, (GDestroyNotify)g_strfreev);
2112 if (dir->mimeapps_list_removed_map != NULL)
2113 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2114 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2115 g_free, (GDestroyNotify)g_strfreev);
2117 key_file = g_key_file_new ();
2119 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2120 if (g_stat (filename, &buf) < 0)
2123 if (dir->mimeapps_list_timestamp > 0)
2124 mime_info_cache->should_ping_mime_monitor = TRUE;
2126 dir->mimeapps_list_timestamp = buf.st_mtime;
2128 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2132 if (load_error != NULL)
2135 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2137 if (mime_types != NULL)
2139 for (i = 0; mime_types[i] != NULL; i++)
2141 desktop_file_ids = g_key_file_get_string_list (key_file,
2142 ADDED_ASSOCIATIONS_GROUP,
2146 if (desktop_file_ids == NULL)
2149 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2150 g_hash_table_replace (dir->mimeapps_list_added_map,
2155 g_strfreev (mime_types);
2158 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2160 if (mime_types != NULL)
2162 for (i = 0; mime_types[i] != NULL; i++)
2164 desktop_file_ids = g_key_file_get_string_list (key_file,
2165 REMOVED_ASSOCIATIONS_GROUP,
2169 if (desktop_file_ids == NULL)
2172 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2173 g_hash_table_replace (dir->mimeapps_list_removed_map,
2178 g_strfreev (mime_types);
2181 g_key_file_free (key_file);
2186 g_key_file_free (key_file);
2188 if (mime_types != NULL)
2189 g_strfreev (mime_types);
2192 g_error_free (load_error);
2195 static MimeInfoCacheDir *
2196 mime_info_cache_dir_new (const char *path)
2198 MimeInfoCacheDir *dir;
2200 dir = g_new0 (MimeInfoCacheDir, 1);
2201 dir->path = g_strdup (path);
2207 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2212 if (dir->mime_info_cache_map != NULL)
2214 destroy_info_cache_map (dir->mime_info_cache_map);
2215 dir->mime_info_cache_map = NULL;
2219 if (dir->defaults_list_map != NULL)
2221 g_hash_table_destroy (dir->defaults_list_map);
2222 dir->defaults_list_map = NULL;
2225 if (dir->mimeapps_list_added_map != NULL)
2227 g_hash_table_destroy (dir->mimeapps_list_added_map);
2228 dir->mimeapps_list_added_map = NULL;
2231 if (dir->mimeapps_list_removed_map != NULL)
2233 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2234 dir->mimeapps_list_removed_map = NULL;
2241 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2242 const char *mime_type,
2243 char **new_desktop_file_ids)
2245 GList *desktop_file_ids;
2248 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2251 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2253 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2254 desktop_file_ids = g_list_append (desktop_file_ids,
2255 g_strdup (new_desktop_file_ids[i]));
2258 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2262 mime_info_cache_init_dir_lists (void)
2264 const char * const *dirs;
2267 mime_info_cache = mime_info_cache_new ();
2269 dirs = get_applications_search_path ();
2271 for (i = 0; dirs[i] != NULL; i++)
2273 MimeInfoCacheDir *dir;
2275 dir = mime_info_cache_dir_new (dirs[i]);
2279 mime_info_cache_dir_init (dir);
2280 mime_info_cache_dir_init_defaults_list (dir);
2281 mime_info_cache_dir_init_mimeapps_list (dir);
2283 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2289 mime_info_cache_update_dir_lists (void)
2293 tmp = mime_info_cache->dirs;
2297 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2299 /* No need to do this if we had file monitors... */
2300 mime_info_cache_blow_global_cache ();
2301 mime_info_cache_dir_init (dir);
2302 mime_info_cache_dir_init_defaults_list (dir);
2303 mime_info_cache_dir_init_mimeapps_list (dir);
2310 mime_info_cache_init (void)
2312 G_LOCK (mime_info_cache);
2313 if (mime_info_cache == NULL)
2314 mime_info_cache_init_dir_lists ();
2320 if (now >= mime_info_cache->last_stat_time + 10)
2322 mime_info_cache_update_dir_lists ();
2323 mime_info_cache->last_stat_time = now;
2327 if (mime_info_cache->should_ping_mime_monitor)
2329 /* g_idle_add (emit_mime_changed, NULL); */
2330 mime_info_cache->should_ping_mime_monitor = FALSE;
2333 G_UNLOCK (mime_info_cache);
2336 static MimeInfoCache *
2337 mime_info_cache_new (void)
2339 MimeInfoCache *cache;
2341 cache = g_new0 (MimeInfoCache, 1);
2343 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2344 (GDestroyNotify) g_free,
2345 (GDestroyNotify) g_free);
2350 mime_info_cache_free (MimeInfoCache *cache)
2355 g_list_foreach (cache->dirs,
2356 (GFunc) mime_info_cache_dir_free,
2358 g_list_free (cache->dirs);
2359 g_hash_table_destroy (cache->global_defaults_cache);
2364 * mime_info_cache_reload:
2365 * @dir: directory path which needs reloading.
2367 * Reload the mime information for the @dir.
2370 mime_info_cache_reload (const char *dir)
2372 /* FIXME: just reload the dir that needs reloading,
2373 * don't blow the whole cache
2375 if (mime_info_cache != NULL)
2377 G_LOCK (mime_info_cache);
2378 mime_info_cache_free (mime_info_cache);
2379 mime_info_cache = NULL;
2380 G_UNLOCK (mime_info_cache);
2385 append_desktop_entry (GList *list,
2386 const char *desktop_entry,
2387 GList *removed_entries)
2389 /* Add if not already in list, and valid */
2390 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2391 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2392 list = g_list_prepend (list, g_strdup (desktop_entry));
2398 * get_all_desktop_entries_for_mime_type:
2399 * @mime_type: a mime type.
2401 * Returns all the desktop ids for @mime_type. The desktop files
2402 * are listed in an order so that default applications are listed before
2403 * non-default ones, and handlers for inherited mimetypes are listed
2404 * after the base ones.
2406 * Return value: a #GList containing the desktop ids which claim
2407 * to handle @mime_type.
2410 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2412 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2413 MimeInfoCacheDir *dir;
2416 char **default_entries;
2417 char **removed_associations;
2420 mime_info_cache_init ();
2422 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2423 G_LOCK (mime_info_cache);
2425 removed_entries = NULL;
2426 desktop_entries = NULL;
2427 for (i = 0; mime_types[i] != NULL; i++)
2429 mime_type = mime_types[i];
2431 /* Go through all apps listed as defaults */
2432 for (dir_list = mime_info_cache->dirs;
2434 dir_list = dir_list->next)
2436 dir = dir_list->data;
2438 /* First added associations from mimeapps.list */
2439 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2440 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2441 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2443 /* Then removed assiciations from mimeapps.list */
2444 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2445 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2446 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2448 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2449 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2450 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2451 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2454 /* Go through all entries that support the mimetype */
2455 for (dir_list = mime_info_cache->dirs;
2457 dir_list = dir_list->next)
2459 dir = dir_list->data;
2461 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2462 for (tmp = list; tmp != NULL; tmp = tmp->next)
2463 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2467 G_UNLOCK (mime_info_cache);
2469 g_strfreev (mime_types);
2471 g_list_free (removed_entries);
2473 desktop_entries = g_list_reverse (desktop_entries);
2475 return desktop_entries;
2478 /* GDesktopAppInfoLookup interface: */
2480 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2481 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2482 gpointer class_data);
2485 g_desktop_app_info_lookup_get_type (void)
2487 static GType desktop_app_info_lookup_type = 0;
2489 if (! desktop_app_info_lookup_type)
2491 static const GTypeInfo desktop_app_info_lookup_info =
2493 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2494 g_desktop_app_info_lookup_base_init, /* base_init */
2495 NULL, /* base_finalize */
2496 g_desktop_app_info_lookup_class_init,
2497 NULL, /* class_finalize */
2498 NULL, /* class_data */
2500 0, /* n_preallocs */
2504 desktop_app_info_lookup_type =
2505 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2506 &desktop_app_info_lookup_info, 0);
2508 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2511 return desktop_app_info_lookup_type;
2515 g_desktop_app_info_lookup_class_init (gpointer g_class,
2516 gpointer class_data)
2521 g_desktop_app_info_lookup_base_init (gpointer g_class)
2526 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2527 * @lookup: a #GDesktopAppInfoLookup
2528 * @uri_scheme: a string containing a URI scheme.
2530 * Gets the default application for launching applications
2531 * using this URI scheme for a particular GDesktopAppInfoLookup
2534 * The GDesktopAppInfoLookup interface and this function is used
2535 * to implement g_app_info_get_default_for_uri_scheme() backends
2536 * in a GIO module. There is no reason for applications to use it
2537 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2539 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2542 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2543 const char *uri_scheme)
2545 GDesktopAppInfoLookupIface *iface;
2547 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2549 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2551 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2554 #define __G_DESKTOP_APP_INFO_C__
2555 #include "gioaliasdef.c"