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"
39 #include "gthemedicon.h"
40 #include "gfileicon.h"
41 #include <glib/gstdio.h>
43 #include "giomodule-priv.h"
49 * SECTION:gdesktopappinfo
50 * @short_description: Application information from desktop files
51 * @include: gio/gdesktopappinfo.h
53 * #GDesktopAppInfo is an implementation of #GAppInfo based on
58 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
59 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
60 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
61 #define MIME_CACHE_GROUP "MIME Cache"
63 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
64 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type);
65 static void mime_info_cache_reload (const char *dir);
66 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
72 * Information about an installed application from a desktop file.
74 struct _GDesktopAppInfo
76 GObject parent_instance;
82 /* FIXME: what about GenericName ? */
96 guint startup_notify : 1;
97 /* FIXME: what about StartupWMClass ? */
100 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
101 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
102 g_desktop_app_info_iface_init))
105 search_path_init (gpointer data)
108 const char * const *data_dirs;
109 const char *user_data_dir;
112 data_dirs = g_get_system_data_dirs ();
113 length = g_strv_length ((char **) data_dirs);
115 args = g_new (char *, length + 2);
118 user_data_dir = g_get_user_data_dir ();
119 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
120 for (i = 0; i < length; i++)
121 args[j++] = g_build_filename (data_dirs[i],
122 "applications", NULL);
128 static const char * const *
129 get_applications_search_path (void)
131 static GOnce once_init = G_ONCE_INIT;
132 return g_once (&once_init, search_path_init, NULL);
136 g_desktop_app_info_finalize (GObject *object)
138 GDesktopAppInfo *info;
140 info = G_DESKTOP_APP_INFO (object);
142 g_free (info->desktop_id);
143 g_free (info->filename);
145 g_free (info->comment);
146 g_free (info->icon_name);
148 g_object_unref (info->icon);
149 g_strfreev (info->only_show_in);
150 g_strfreev (info->not_show_in);
151 g_free (info->try_exec);
153 g_free (info->binary);
156 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
160 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164 gobject_class->finalize = g_desktop_app_info_finalize;
168 g_desktop_app_info_init (GDesktopAppInfo *local)
173 binary_from_exec (const char *exec)
175 const char *p, *start;
181 while (*p != ' ' && *p != 0)
184 return g_strndup (start, p - start);
189 * g_desktop_app_info_new_from_filename:
190 * @filename: a string containing a file name.
192 * Creates a new #GDesktopAppInfo.
194 * Returns: a new #GDesktopAppInfo or %NULL on error.
197 g_desktop_app_info_new_from_filename (const char *filename)
199 GDesktopAppInfo *info;
205 key_file = g_key_file_new ();
207 if (!g_key_file_load_from_file (key_file,
212 g_key_file_free (key_file);
216 start_group = g_key_file_get_start_group (key_file);
217 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
219 g_free (start_group);
220 g_key_file_free (key_file);
223 g_free (start_group);
225 type = g_key_file_get_string (key_file,
226 G_KEY_FILE_DESKTOP_GROUP,
227 G_KEY_FILE_DESKTOP_KEY_TYPE,
229 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
232 g_key_file_free (key_file);
237 try_exec = g_key_file_get_string (key_file,
238 G_KEY_FILE_DESKTOP_GROUP,
239 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
241 if (try_exec && try_exec[0] != '\0')
244 t = g_find_program_in_path (try_exec);
248 g_key_file_free (key_file);
254 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
255 info->filename = g_strdup (filename);
257 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
258 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
259 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
260 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
261 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);
262 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);
263 info->try_exec = try_exec;
264 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
265 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
266 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
267 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
268 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
270 g_key_file_free (key_file);
275 if (g_path_is_absolute (info->icon_name))
279 file = g_file_new_for_path (info->icon_name);
280 info->icon = g_file_icon_new (file);
281 g_object_unref (file);
284 info->icon = g_themed_icon_new (info->icon_name);
288 info->binary = binary_from_exec (info->exec);
294 * g_desktop_app_info_new:
295 * @desktop_id: the desktop file id
297 * Creates a new #GDesktopAppInfo.
299 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
302 g_desktop_app_info_new (const char *desktop_id)
304 GDesktopAppInfo *appinfo;
305 const char * const *dirs;
309 dirs = get_applications_search_path ();
311 basename = g_strdup (desktop_id);
313 for (i = 0; dirs[i] != NULL; i++)
318 filename = g_build_filename (dirs[i], desktop_id, NULL);
319 appinfo = g_desktop_app_info_new_from_filename (filename);
325 while ((p = strchr (p, '-')) != NULL)
329 filename = g_build_filename (dirs[i], basename, NULL);
330 appinfo = g_desktop_app_info_new_from_filename (filename);
345 appinfo->desktop_id = g_strdup (desktop_id);
347 if (g_desktop_app_info_get_is_hidden (appinfo))
349 g_object_unref (appinfo);
357 g_desktop_app_info_dup (GAppInfo *appinfo)
359 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
360 GDesktopAppInfo *new_info;
362 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
364 new_info->filename = g_strdup (info->filename);
365 new_info->desktop_id = g_strdup (info->desktop_id);
367 new_info->name = g_strdup (info->name);
368 new_info->comment = g_strdup (info->comment);
369 new_info->nodisplay = info->nodisplay;
370 new_info->icon_name = g_strdup (info->icon_name);
371 new_info->icon = g_object_ref (info->icon);
372 new_info->only_show_in = g_strdupv (info->only_show_in);
373 new_info->not_show_in = g_strdupv (info->not_show_in);
374 new_info->try_exec = g_strdup (info->try_exec);
375 new_info->exec = g_strdup (info->exec);
376 new_info->binary = g_strdup (info->binary);
377 new_info->path = g_strdup (info->path);
378 new_info->hidden = info->hidden;
379 new_info->terminal = info->terminal;
380 new_info->startup_notify = info->startup_notify;
382 return G_APP_INFO (new_info);
386 g_desktop_app_info_equal (GAppInfo *appinfo1,
389 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
390 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
392 if (info1->desktop_id == NULL ||
393 info2->desktop_id == NULL)
394 return info1 == info2;
396 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
400 g_desktop_app_info_get_id (GAppInfo *appinfo)
402 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
404 return info->desktop_id;
408 g_desktop_app_info_get_name (GAppInfo *appinfo)
410 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
412 if (info->name == NULL)
418 * g_desktop_app_info_get_is_hidden:
419 * @info: a #GDesktopAppInfo.
421 * A desktop file is hidden if the Hidden key in it is
424 * Returns: %TRUE if hidden, %FALSE otherwise.
427 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
433 g_desktop_app_info_get_description (GAppInfo *appinfo)
435 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
437 return info->comment;
441 g_desktop_app_info_get_executable (GAppInfo *appinfo)
443 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
449 g_desktop_app_info_get_icon (GAppInfo *appinfo)
451 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
457 expand_macro_single (char macro, char *uri)
463 file = g_file_new_for_uri (uri);
464 path = g_file_get_path (file);
465 g_object_unref (file);
471 result = g_shell_quote (uri);
476 result = g_shell_quote (path);
481 result = g_shell_quote (g_path_get_dirname (path));
486 result = g_shell_quote (g_path_get_basename (path));
496 expand_macro (char macro,
498 GDesktopAppInfo *info,
501 GList *uris = *uri_list;
504 g_return_if_fail (exec != NULL);
514 expanded = expand_macro_single (macro, uris->data);
517 g_string_append (exec, expanded);
531 expanded = expand_macro_single (macro, uris->data);
534 g_string_append (exec, expanded);
540 if (uris != NULL && expanded)
541 g_string_append_c (exec, ' ');
549 g_string_append (exec, "--icon ");
550 g_string_append (exec, info->icon_name);
556 g_string_append (exec, info->name);
561 g_string_append (exec, info->filename);
564 case 'm': /* deprecated */
568 g_string_append_c (exec, '%');
576 expand_application_parameters (GDesktopAppInfo *info,
582 GList *uri_list = *uris;
583 const char *p = info->exec;
584 GString *expanded_exec = g_string_new (NULL);
587 if (info->exec == NULL)
589 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
590 _("Desktop file didn't specify Exec field"));
596 if (p[0] == '%' && p[1] != '\0')
598 expand_macro (p[1], expanded_exec, info, uris);
602 g_string_append_c (expanded_exec, *p);
607 /* No file substitutions */
608 if (uri_list == *uris && uri_list != NULL)
610 /* If there is no macro default to %f. This is also what KDE does */
611 g_string_append_c (expanded_exec, ' ');
612 expand_macro ('f', expanded_exec, info, uris);
615 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
616 g_string_free (expanded_exec, TRUE);
621 prepend_terminal_to_vector (int *argc,
628 char **term_argv = NULL;
633 g_return_val_if_fail (argc != NULL, FALSE);
634 g_return_val_if_fail (argv != NULL, FALSE);
642 /* compute size if not given */
645 for (i = 0; the_argv[i] != NULL; i++)
651 term_argv = g_new0 (char *, 3);
653 check = g_find_program_in_path ("gnome-terminal");
656 term_argv[0] = check;
657 /* Note that gnome-terminal takes -x and
658 * as -e in gnome-terminal is broken we use that. */
659 term_argv[1] = g_strdup ("-x");
664 check = g_find_program_in_path ("nxterm");
666 check = g_find_program_in_path ("color-xterm");
668 check = g_find_program_in_path ("rxvt");
670 check = g_find_program_in_path ("xterm");
672 check = g_find_program_in_path ("dtterm");
675 check = g_strdup ("xterm");
676 g_warning ("couldn't find a terminal, falling back to xterm");
678 term_argv[0] = check;
679 term_argv[1] = g_strdup ("-e");
682 real_argc = term_argc + *argc;
683 real_argv = g_new (char *, real_argc + 1);
685 for (i = 0; i < term_argc; i++)
686 real_argv[i] = term_argv[i];
688 for (j = 0; j < *argc; j++, i++)
689 real_argv[i] = (char *)the_argv[j];
697 /* we use g_free here as we sucked all the inner strings
698 * out from it into real_argv */
703 #endif /* G_OS_WIN32 */
706 /* '=' is the new '\0'.
707 * DO NOT CALL unless at least one string ends with '='
710 is_env (const char *a,
715 if (*a == 0 || *b == 0)
728 /* free with g_strfreev */
730 replace_env_var (char **old_environ,
732 const char *new_value)
734 int length, new_length;
735 int index, new_index;
739 /* do two things at once:
740 * - discover the length of the environment ('length')
741 * - find the location (if any) of the env var ('index')
744 for (length = 0; old_environ[length]; length++)
746 /* if we already have it in our environment, replace */
747 if (is_env (old_environ[length], env_var))
752 /* no current env var, no desired env value.
755 if (new_value == NULL && index == -1)
758 /* in all cases now, we will be using a modified environment.
759 * determine its length and allocated it.
762 * new_index = location to insert, if any
763 * new_length = length of the new array
764 * new_environ = the pointer array for the new environment
767 if (new_value == NULL && index >= 0)
769 /* in this case, we will be removing an entry */
770 new_length = length - 1;
773 else if (new_value != NULL && index < 0)
775 /* in this case, we will be adding an entry to the end */
776 new_length = length + 1;
780 /* in this case, we will be replacing the existing entry */
786 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
787 new_environ[new_length] = NULL;
789 /* now we do the copying.
790 * for each entry in the new environment, we decide what to do
794 for (new_i = 0; new_i < new_length; new_i++)
796 if (new_i == new_index)
798 /* insert our new item */
799 new_environ[new_i] = g_strconcat (env_var,
804 /* if we had an old entry, skip it now */
810 /* if this is the old DESKTOP_STARTUP_ID, skip it */
814 /* copy an old item */
815 new_environ[new_i] = g_strdup (old_environ[i]);
820 g_strfreev (old_environ);
826 uri_list_segment_to_files (GList *start,
833 while (start != NULL && start != end)
835 file = g_file_new_for_uri ((char *)start->data);
836 res = g_list_prepend (res, file);
840 return g_list_reverse (res);
843 #ifdef HAVE__NSGETENVIRON
844 #define environ (*_NSGetEnviron())
845 #elif !defined(G_OS_WIN32)
847 /* According to the Single Unix Specification, environ is not in
848 * * any system header, although unistd.h often declares it.
850 extern char **environ;
854 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
856 GAppLaunchContext *launch_context,
859 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
860 gboolean completed = FALSE;
862 GList *launched_files;
869 g_return_val_if_fail (appinfo != NULL, FALSE);
877 if (!expand_application_parameters (info, &uris,
878 &argc, &argv, error))
881 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
883 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
884 _("Unable to find terminal required for application"));
891 launched_files = uri_list_segment_to_files (old_uris, uris);
893 display = g_app_launch_context_get_display (launch_context,
898 if (info->startup_notify)
899 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
903 if (display || sn_id)
907 envp = g_new0 (char *, 1);
909 envp = g_strdupv (environ);
913 envp = replace_env_var (envp,
918 envp = replace_env_var (envp,
919 "DESKTOP_STARTUP_ID",
925 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
926 g_list_free (launched_files);
929 if (!g_spawn_async (info->path, /* working directory */
932 G_SPAWN_SEARCH_PATH /* flags */,
933 NULL /* child_setup */,
935 NULL /* child_pid */,
940 g_app_launch_context_launch_failed (launch_context, sn_id);
954 while (uris != NULL);
966 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
968 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
971 ((strstr (info->exec, "%u") != NULL) ||
972 (strstr (info->exec, "%U") != NULL));
976 g_desktop_app_info_supports_files (GAppInfo *appinfo)
978 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
981 ((strstr (info->exec, "%f") != NULL) ||
982 (strstr (info->exec, "%F") != NULL));
986 g_desktop_app_info_launch (GAppInfo *appinfo,
988 GAppLaunchContext *launch_context,
998 uri = g_file_get_uri (files->data);
999 uris = g_list_prepend (uris, uri);
1000 files = files->next;
1003 uris = g_list_reverse (uris);
1005 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1007 g_list_foreach (uris, (GFunc)g_free, NULL);
1013 G_LOCK_DEFINE_STATIC (g_desktop_env);
1014 static gchar *g_desktop_env = NULL;
1017 * g_desktop_app_info_set_desktop_env:
1018 * @desktop_env: a string specifying what desktop this is
1020 * Sets the name of the desktop that the application is running in.
1021 * This is used by g_app_info_should_show() to evaluate the
1022 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1023 * desktop entry fields.
1025 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1026 * Menu specification</ulink> recognizes the following:
1028 * <member>GNOME</member>
1029 * <member>KDE</member>
1030 * <member>ROX</member>
1031 * <member>XFCE</member>
1032 * <member>Old</member>
1035 * Should be called only once; subsequent calls are ignored.
1038 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1040 G_LOCK (g_desktop_env);
1042 g_desktop_env = g_strdup (desktop_env);
1043 G_UNLOCK (g_desktop_env);
1047 g_desktop_app_info_should_show (GAppInfo *appinfo)
1049 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1051 const gchar *desktop_env;
1054 if (info->nodisplay)
1057 G_LOCK (g_desktop_env);
1058 desktop_env = g_desktop_env;
1059 G_UNLOCK (g_desktop_env);
1061 if (info->only_show_in)
1063 if (desktop_env == NULL)
1067 for (i = 0; info->only_show_in[i] != NULL; i++)
1069 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1079 if (info->not_show_in && desktop_env)
1081 for (i = 0; info->not_show_in[i] != NULL; i++)
1083 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1097 ensure_dir (DirType type,
1100 char *path, *display_name;
1103 if (type == APP_DIR)
1104 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1106 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1109 if (g_mkdir_with_parents (path, 0700) == 0)
1113 display_name = g_filename_display_name (path);
1114 if (type == APP_DIR)
1115 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1116 _("Can't create user application configuration folder %s: %s"),
1117 display_name, g_strerror (errsv));
1119 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1120 _("Can't create user MIME configuration folder %s: %s"),
1121 display_name, g_strerror (errsv));
1123 g_free (display_name);
1130 update_mimeapps_list (const char *desktop_id,
1131 const char *content_type,
1132 gboolean add_at_start,
1133 gboolean add_at_end,
1137 char *dirname, *filename;
1139 gboolean load_succeeded, res;
1142 gsize length, data_size;
1146 /* Don't add both at start and end */
1147 g_assert (!(add_at_start && add_at_end));
1149 dirname = ensure_dir (APP_DIR, error);
1153 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1156 key_file = g_key_file_new ();
1157 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1158 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1160 g_key_file_free (key_file);
1161 key_file = g_key_file_new ();
1164 /* Add to the right place in the list */
1167 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1168 content_type, &length, NULL);
1170 list = g_new (char *, 1 + length + 1);
1174 list[i++] = g_strdup (desktop_id);
1177 for (j = 0; old_list[j] != NULL; j++)
1179 if (strcmp (old_list[j], desktop_id) != 0)
1180 list[i++] = g_strdup (old_list[j]);
1184 list[i++] = g_strdup (desktop_id);
1187 g_strfreev (old_list);
1189 g_key_file_set_string_list (key_file,
1190 ADDED_ASSOCIATIONS_GROUP,
1192 (const char * const *)list, i);
1196 /* Remove from removed associations group (unless remove) */
1199 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1200 content_type, &length, NULL);
1202 list = g_new (char *, 1 + length + 1);
1206 list[i++] = g_strdup (desktop_id);
1209 for (j = 0; old_list[j] != NULL; j++)
1211 if (strcmp (old_list[j], desktop_id) != 0)
1212 list[i++] = g_strdup (old_list[j]);
1217 g_strfreev (old_list);
1219 if (list[0] == NULL)
1220 g_key_file_remove_key (key_file,
1221 REMOVED_ASSOCIATIONS_GROUP,
1225 g_key_file_set_string_list (key_file,
1226 REMOVED_ASSOCIATIONS_GROUP,
1228 (const char * const *)list, i);
1233 data = g_key_file_to_data (key_file, &data_size, error);
1234 g_key_file_free (key_file);
1236 res = g_file_set_contents (filename, data, data_size, error);
1238 mime_info_cache_reload (NULL);
1247 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1248 const char *content_type,
1251 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1253 if (!g_desktop_app_info_ensure_saved (info, error))
1256 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1260 update_program_done (GPid pid,
1264 /* Did the application exit correctly */
1265 if (WIFEXITED (status) &&
1266 WEXITSTATUS (status) == 0)
1268 /* Here we could clean out any caches in use */
1273 run_update_command (char *command,
1282 GError *error = NULL;
1285 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1287 if (g_spawn_async ("/", argv,
1289 G_SPAWN_SEARCH_PATH |
1290 G_SPAWN_STDOUT_TO_DEV_NULL |
1291 G_SPAWN_STDERR_TO_DEV_NULL |
1292 G_SPAWN_DO_NOT_REAP_CHILD,
1293 NULL, NULL, /* No setup function */
1296 g_child_watch_add (pid, update_program_done, NULL);
1299 /* If we get an error at this point, it's quite likely the user doesn't
1300 * have an installed copy of either 'update-mime-database' or
1301 * 'update-desktop-database'. I don't think we want to popup an error
1302 * dialog at this point, so we just do a g_warning to give the user a
1303 * chance of debugging it.
1305 g_warning ("%s", error->message);
1312 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1313 const char *extension,
1316 char *filename, *basename, *mimetype;
1320 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1323 dirname = ensure_dir (MIMETYPE_DIR, error);
1327 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1328 filename = g_build_filename (dirname, basename, NULL);
1332 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1334 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1339 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1340 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1341 " <mime-type type=\"%s\">\n"
1342 " <comment>%s document</comment>\n"
1343 " <glob pattern=\"*.%s\"/>\n"
1345 "</mime-info>\n", mimetype, extension, extension);
1347 g_file_set_contents (filename, contents, -1, NULL);
1350 run_update_command ("update-mime-database", "mime");
1354 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1364 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1365 const char *content_type,
1368 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1370 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1373 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1377 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1383 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1384 const char *content_type,
1387 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1389 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1392 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1396 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1402 char *data, *desktop_id;
1407 if (info->filename != NULL)
1410 /* This is only used for object created with
1411 * g_app_info_create_from_commandline. All other
1412 * object should have a filename
1415 dirname = ensure_dir (APP_DIR, error);
1419 key_file = g_key_file_new ();
1421 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1422 "Encoding", "UTF-8");
1423 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1424 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1425 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1426 G_KEY_FILE_DESKTOP_KEY_TYPE,
1427 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1429 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1430 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1432 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1433 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1435 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1436 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1438 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1439 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1441 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1442 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1444 data = g_key_file_to_data (key_file, &data_size, NULL);
1445 g_key_file_free (key_file);
1447 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1448 filename = g_build_filename (dirname, desktop_id, NULL);
1449 g_free (desktop_id);
1452 fd = g_mkstemp (filename);
1457 display_name = g_filename_display_name (filename);
1458 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1459 _("Can't create user desktop file %s"), display_name);
1460 g_free (display_name);
1466 desktop_id = g_path_get_basename (filename);
1470 res = g_file_set_contents (filename, data, data_size, error);
1473 g_free (desktop_id);
1478 info->filename = filename;
1479 info->desktop_id = desktop_id;
1481 run_update_command ("update-desktop-database", "applications");
1487 * g_app_info_create_from_commandline:
1488 * @commandline: the commandline to use
1489 * @application_name: the application name, or %NULL to use @commandline
1490 * @flags: flags that can specify details of the created #GAppInfo
1491 * @error: a #GError location to store the error occuring, %NULL to ignore.
1493 * Creates a new #GAppInfo from the given information.
1495 * Returns: new #GAppInfo for given command.
1498 g_app_info_create_from_commandline (const char *commandline,
1499 const char *application_name,
1500 GAppInfoCreateFlags flags,
1505 GDesktopAppInfo *info;
1507 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1509 info->filename = NULL;
1510 info->desktop_id = NULL;
1512 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1513 info->startup_notify = FALSE;
1514 info->hidden = FALSE;
1515 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1516 info->exec = g_strconcat (commandline, " %u", NULL);
1518 info->exec = g_strconcat (commandline, " %f", NULL);
1519 info->nodisplay = TRUE;
1520 info->binary = binary_from_exec (info->exec);
1522 if (application_name)
1523 info->name = g_strdup (application_name);
1526 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1527 split = g_strsplit (commandline, " ", 2);
1528 basename = g_path_get_basename (split[0]);
1530 info->name = basename;
1531 if (info->name == NULL)
1532 info->name = g_strdup ("custom");
1534 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1536 return G_APP_INFO (info);
1540 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1542 iface->dup = g_desktop_app_info_dup;
1543 iface->equal = g_desktop_app_info_equal;
1544 iface->get_id = g_desktop_app_info_get_id;
1545 iface->get_name = g_desktop_app_info_get_name;
1546 iface->get_description = g_desktop_app_info_get_description;
1547 iface->get_executable = g_desktop_app_info_get_executable;
1548 iface->get_icon = g_desktop_app_info_get_icon;
1549 iface->launch = g_desktop_app_info_launch;
1550 iface->supports_uris = g_desktop_app_info_supports_uris;
1551 iface->supports_files = g_desktop_app_info_supports_files;
1552 iface->launch_uris = g_desktop_app_info_launch_uris;
1553 iface->should_show = g_desktop_app_info_should_show;
1554 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1555 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1556 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1557 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1558 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1562 app_info_in_list (GAppInfo *info,
1565 while (list != NULL)
1567 if (g_app_info_equal (info, list->data))
1576 * g_app_info_get_all_for_type:
1577 * @content_type: the content type to find a #GAppInfo for
1579 * Gets a list of all #GAppInfo s for a given content type.
1581 * Returns: #GList of #GAppInfo s for given @content_type
1582 * or %NULL on error.
1585 g_app_info_get_all_for_type (const char *content_type)
1587 GList *desktop_entries, *l;
1589 GDesktopAppInfo *info;
1591 g_return_val_if_fail (content_type != NULL, NULL);
1593 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1596 for (l = desktop_entries; l != NULL; l = l->next)
1598 char *desktop_entry = l->data;
1600 info = g_desktop_app_info_new (desktop_entry);
1603 if (app_info_in_list (G_APP_INFO (info), infos))
1604 g_object_unref (info);
1606 infos = g_list_prepend (infos, info);
1608 g_free (desktop_entry);
1611 g_list_free (desktop_entries);
1613 return g_list_reverse (infos);
1618 * g_app_info_get_default_for_type:
1619 * @content_type: the content type to find a #GAppInfo for
1620 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1623 * Gets the #GAppInfo that correspond to a given content type.
1625 * Returns: #GAppInfo for given @content_type or %NULL on error.
1628 g_app_info_get_default_for_type (const char *content_type,
1629 gboolean must_support_uris)
1631 GList *desktop_entries, *l;
1634 g_return_val_if_fail (content_type != NULL, NULL);
1636 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1639 for (l = desktop_entries; l != NULL; l = l->next)
1641 char *desktop_entry = l->data;
1643 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1646 if (must_support_uris && !g_app_info_supports_uris (info))
1648 g_object_unref (info);
1656 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1657 g_list_free (desktop_entries);
1663 * g_app_info_get_default_for_uri_scheme:
1664 * @uri_scheme: a string containing a URI scheme.
1666 * Gets the default application for launching applications
1667 * using this URI scheme. A URI scheme is the initial part
1668 * of the URI, up to but not including the ':', e.g. "http",
1671 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1674 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1676 static gsize lookup = 0;
1678 if (g_once_init_enter (&lookup))
1680 gsize setup_value = 1;
1681 GDesktopAppInfoLookup *lookup_instance;
1682 const char *use_this;
1683 GIOExtensionPoint *ep;
1684 GIOExtension *extension;
1687 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1689 /* Ensure vfs in modules loaded */
1690 _g_io_modules_ensure_loaded ();
1692 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1694 lookup_instance = NULL;
1697 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1699 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1702 if (lookup_instance == NULL)
1704 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1706 extension = l->data;
1707 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1708 if (lookup_instance != NULL)
1713 if (lookup_instance != NULL)
1714 setup_value = (gsize)lookup_instance;
1716 g_once_init_leave (&lookup, setup_value);
1722 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1728 get_apps_from_dir (GHashTable *apps,
1729 const char *dirname,
1733 const char *basename;
1734 char *filename, *subprefix, *desktop_id;
1736 GDesktopAppInfo *appinfo;
1738 dir = g_dir_open (dirname, 0, NULL);
1741 while ((basename = g_dir_read_name (dir)) != NULL)
1743 filename = g_build_filename (dirname, basename, NULL);
1744 if (g_str_has_suffix (basename, ".desktop"))
1746 desktop_id = g_strconcat (prefix, basename, NULL);
1748 /* Use _extended so we catch NULLs too (hidden) */
1749 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1751 appinfo = g_desktop_app_info_new_from_filename (filename);
1753 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1755 g_object_unref (appinfo);
1760 if (appinfo || hidden)
1762 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1766 /* Reuse instead of strdup here */
1767 appinfo->desktop_id = desktop_id;
1772 g_free (desktop_id);
1776 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1778 subprefix = g_strconcat (prefix, basename, "-", NULL);
1779 get_apps_from_dir (apps, filename, subprefix);
1791 * g_app_info_get_all:
1793 * Gets a list of all of the applications currently registered
1796 * For desktop files, this includes applications that have
1797 * <literal>NoDisplay=true</literal> set or are excluded from
1798 * display by means of <literal>OnlyShowIn</literal> or
1799 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1800 * The returned list does not include applications which have
1801 * the <literal>Hidden</literal> key set.
1803 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1806 g_app_info_get_all (void)
1808 const char * const *dirs;
1810 GHashTableIter iter;
1815 dirs = get_applications_search_path ();
1817 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1821 for (i = 0; dirs[i] != NULL; i++)
1822 get_apps_from_dir (apps, dirs[i], "");
1826 g_hash_table_iter_init (&iter, apps);
1827 while (g_hash_table_iter_next (&iter, NULL, &value))
1830 infos = g_list_prepend (infos, value);
1833 g_hash_table_destroy (apps);
1835 return g_list_reverse (infos);
1838 /* Cacheing of mimeinfo.cache and defaults.list files */
1842 GHashTable *mime_info_cache_map;
1843 GHashTable *defaults_list_map;
1844 GHashTable *mimeapps_list_added_map;
1845 GHashTable *mimeapps_list_removed_map;
1846 time_t mime_info_cache_timestamp;
1847 time_t defaults_list_timestamp;
1848 time_t mimeapps_list_timestamp;
1852 GList *dirs; /* mimeinfo.cache and defaults.list */
1853 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1854 time_t last_stat_time;
1855 guint should_ping_mime_monitor : 1;
1858 static MimeInfoCache *mime_info_cache = NULL;
1859 G_LOCK_DEFINE_STATIC (mime_info_cache);
1861 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1862 const char *mime_type,
1863 char **new_desktop_file_ids);
1865 static MimeInfoCache * mime_info_cache_new (void);
1868 destroy_info_cache_value (gpointer key,
1872 g_list_foreach (value, (GFunc)g_free, NULL);
1873 g_list_free (value);
1877 destroy_info_cache_map (GHashTable *info_cache_map)
1879 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1880 g_hash_table_destroy (info_cache_map);
1884 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1885 const char *cache_file,
1891 filename = g_build_filename (dir->path, cache_file, NULL);
1893 if (g_stat (filename, &buf) < 0)
1900 if (buf.st_mtime != *timestamp)
1906 /* Call with lock held */
1908 remove_all (gpointer key,
1917 mime_info_cache_blow_global_cache (void)
1919 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1924 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1928 gchar *filename, **mime_types;
1935 if (dir->mime_info_cache_map != NULL &&
1936 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1937 &dir->mime_info_cache_timestamp))
1940 if (dir->mime_info_cache_map != NULL)
1941 destroy_info_cache_map (dir->mime_info_cache_map);
1943 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1944 (GDestroyNotify) g_free,
1947 key_file = g_key_file_new ();
1949 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1951 if (g_stat (filename, &buf) < 0)
1954 if (dir->mime_info_cache_timestamp > 0)
1955 mime_info_cache->should_ping_mime_monitor = TRUE;
1957 dir->mime_info_cache_timestamp = buf.st_mtime;
1959 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1964 if (load_error != NULL)
1967 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1970 if (load_error != NULL)
1973 for (i = 0; mime_types[i] != NULL; i++)
1975 gchar **desktop_file_ids;
1976 char *unaliased_type;
1977 desktop_file_ids = g_key_file_get_string_list (key_file,
1983 if (desktop_file_ids == NULL)
1986 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1987 mime_info_cache_dir_add_desktop_entries (dir,
1990 g_free (unaliased_type);
1992 g_strfreev (desktop_file_ids);
1995 g_strfreev (mime_types);
1996 g_key_file_free (key_file);
2001 g_key_file_free (key_file);
2003 if (mime_types != NULL)
2004 g_strfreev (mime_types);
2007 g_error_free (load_error);
2011 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2015 gchar *filename, **mime_types;
2016 char *unaliased_type;
2017 char **desktop_file_ids;
2024 if (dir->defaults_list_map != NULL &&
2025 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2026 &dir->defaults_list_timestamp))
2029 if (dir->defaults_list_map != NULL)
2030 g_hash_table_destroy (dir->defaults_list_map);
2031 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2032 g_free, (GDestroyNotify)g_strfreev);
2035 key_file = g_key_file_new ();
2037 filename = g_build_filename (dir->path, "defaults.list", NULL);
2038 if (g_stat (filename, &buf) < 0)
2041 if (dir->defaults_list_timestamp > 0)
2042 mime_info_cache->should_ping_mime_monitor = TRUE;
2044 dir->defaults_list_timestamp = buf.st_mtime;
2046 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2050 if (load_error != NULL)
2053 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2055 if (mime_types != NULL)
2057 for (i = 0; mime_types[i] != NULL; i++)
2059 desktop_file_ids = g_key_file_get_string_list (key_file,
2060 DEFAULT_APPLICATIONS_GROUP,
2064 if (desktop_file_ids == NULL)
2067 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2068 g_hash_table_replace (dir->defaults_list_map,
2073 g_strfreev (mime_types);
2076 g_key_file_free (key_file);
2081 g_key_file_free (key_file);
2083 if (mime_types != NULL)
2084 g_strfreev (mime_types);
2087 g_error_free (load_error);
2091 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2095 gchar *filename, **mime_types;
2096 char *unaliased_type;
2097 char **desktop_file_ids;
2104 if (dir->mimeapps_list_added_map != NULL &&
2105 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2106 &dir->mimeapps_list_timestamp))
2109 if (dir->mimeapps_list_added_map != NULL)
2110 g_hash_table_destroy (dir->mimeapps_list_added_map);
2111 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2112 g_free, (GDestroyNotify)g_strfreev);
2114 if (dir->mimeapps_list_removed_map != NULL)
2115 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2116 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2117 g_free, (GDestroyNotify)g_strfreev);
2119 key_file = g_key_file_new ();
2121 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2122 if (g_stat (filename, &buf) < 0)
2125 if (dir->mimeapps_list_timestamp > 0)
2126 mime_info_cache->should_ping_mime_monitor = TRUE;
2128 dir->mimeapps_list_timestamp = buf.st_mtime;
2130 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2134 if (load_error != NULL)
2137 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2139 if (mime_types != NULL)
2141 for (i = 0; mime_types[i] != NULL; i++)
2143 desktop_file_ids = g_key_file_get_string_list (key_file,
2144 ADDED_ASSOCIATIONS_GROUP,
2148 if (desktop_file_ids == NULL)
2151 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2152 g_hash_table_replace (dir->mimeapps_list_added_map,
2157 g_strfreev (mime_types);
2160 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2162 if (mime_types != NULL)
2164 for (i = 0; mime_types[i] != NULL; i++)
2166 desktop_file_ids = g_key_file_get_string_list (key_file,
2167 REMOVED_ASSOCIATIONS_GROUP,
2171 if (desktop_file_ids == NULL)
2174 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2175 g_hash_table_replace (dir->mimeapps_list_removed_map,
2180 g_strfreev (mime_types);
2183 g_key_file_free (key_file);
2188 g_key_file_free (key_file);
2190 if (mime_types != NULL)
2191 g_strfreev (mime_types);
2194 g_error_free (load_error);
2197 static MimeInfoCacheDir *
2198 mime_info_cache_dir_new (const char *path)
2200 MimeInfoCacheDir *dir;
2202 dir = g_new0 (MimeInfoCacheDir, 1);
2203 dir->path = g_strdup (path);
2209 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2214 if (dir->mime_info_cache_map != NULL)
2216 destroy_info_cache_map (dir->mime_info_cache_map);
2217 dir->mime_info_cache_map = NULL;
2221 if (dir->defaults_list_map != NULL)
2223 g_hash_table_destroy (dir->defaults_list_map);
2224 dir->defaults_list_map = NULL;
2227 if (dir->mimeapps_list_added_map != NULL)
2229 g_hash_table_destroy (dir->mimeapps_list_added_map);
2230 dir->mimeapps_list_added_map = NULL;
2233 if (dir->mimeapps_list_removed_map != NULL)
2235 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2236 dir->mimeapps_list_removed_map = NULL;
2243 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2244 const char *mime_type,
2245 char **new_desktop_file_ids)
2247 GList *desktop_file_ids;
2250 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2253 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2255 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2256 desktop_file_ids = g_list_append (desktop_file_ids,
2257 g_strdup (new_desktop_file_ids[i]));
2260 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2264 mime_info_cache_init_dir_lists (void)
2266 const char * const *dirs;
2269 mime_info_cache = mime_info_cache_new ();
2271 dirs = get_applications_search_path ();
2273 for (i = 0; dirs[i] != NULL; i++)
2275 MimeInfoCacheDir *dir;
2277 dir = mime_info_cache_dir_new (dirs[i]);
2281 mime_info_cache_dir_init (dir);
2282 mime_info_cache_dir_init_defaults_list (dir);
2283 mime_info_cache_dir_init_mimeapps_list (dir);
2285 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2291 mime_info_cache_update_dir_lists (void)
2295 tmp = mime_info_cache->dirs;
2299 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2301 /* No need to do this if we had file monitors... */
2302 mime_info_cache_blow_global_cache ();
2303 mime_info_cache_dir_init (dir);
2304 mime_info_cache_dir_init_defaults_list (dir);
2305 mime_info_cache_dir_init_mimeapps_list (dir);
2312 mime_info_cache_init (void)
2314 G_LOCK (mime_info_cache);
2315 if (mime_info_cache == NULL)
2316 mime_info_cache_init_dir_lists ();
2322 if (now >= mime_info_cache->last_stat_time + 10)
2324 mime_info_cache_update_dir_lists ();
2325 mime_info_cache->last_stat_time = now;
2329 if (mime_info_cache->should_ping_mime_monitor)
2331 /* g_idle_add (emit_mime_changed, NULL); */
2332 mime_info_cache->should_ping_mime_monitor = FALSE;
2335 G_UNLOCK (mime_info_cache);
2338 static MimeInfoCache *
2339 mime_info_cache_new (void)
2341 MimeInfoCache *cache;
2343 cache = g_new0 (MimeInfoCache, 1);
2345 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2346 (GDestroyNotify) g_free,
2347 (GDestroyNotify) g_free);
2352 mime_info_cache_free (MimeInfoCache *cache)
2357 g_list_foreach (cache->dirs,
2358 (GFunc) mime_info_cache_dir_free,
2360 g_list_free (cache->dirs);
2361 g_hash_table_destroy (cache->global_defaults_cache);
2366 * mime_info_cache_reload:
2367 * @dir: directory path which needs reloading.
2369 * Reload the mime information for the @dir.
2372 mime_info_cache_reload (const char *dir)
2374 /* FIXME: just reload the dir that needs reloading,
2375 * don't blow the whole cache
2377 if (mime_info_cache != NULL)
2379 G_LOCK (mime_info_cache);
2380 mime_info_cache_free (mime_info_cache);
2381 mime_info_cache = NULL;
2382 G_UNLOCK (mime_info_cache);
2387 append_desktop_entry (GList *list,
2388 const char *desktop_entry,
2389 GList *removed_entries)
2391 /* Add if not already in list, and valid */
2392 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2393 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2394 list = g_list_prepend (list, g_strdup (desktop_entry));
2400 * get_all_desktop_entries_for_mime_type:
2401 * @mime_type: a mime type.
2403 * Returns all the desktop ids for @mime_type. The desktop files
2404 * are listed in an order so that default applications are listed before
2405 * non-default ones, and handlers for inherited mimetypes are listed
2406 * after the base ones.
2408 * Return value: a #GList containing the desktop ids which claim
2409 * to handle @mime_type.
2412 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2414 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2415 MimeInfoCacheDir *dir;
2418 char **default_entries;
2419 char **removed_associations;
2422 mime_info_cache_init ();
2424 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2425 G_LOCK (mime_info_cache);
2427 removed_entries = NULL;
2428 desktop_entries = NULL;
2429 for (i = 0; mime_types[i] != NULL; i++)
2431 mime_type = mime_types[i];
2433 /* Go through all apps listed as defaults */
2434 for (dir_list = mime_info_cache->dirs;
2436 dir_list = dir_list->next)
2438 dir = dir_list->data;
2440 /* First added associations from mimeapps.list */
2441 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2442 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2443 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2445 /* Then removed assiciations from mimeapps.list */
2446 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2447 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2448 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2450 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2451 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2452 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2453 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2456 /* Go through all entries that support the mimetype */
2457 for (dir_list = mime_info_cache->dirs;
2459 dir_list = dir_list->next)
2461 dir = dir_list->data;
2463 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2464 for (tmp = list; tmp != NULL; tmp = tmp->next)
2465 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2469 G_UNLOCK (mime_info_cache);
2471 g_strfreev (mime_types);
2473 g_list_free (removed_entries);
2475 desktop_entries = g_list_reverse (desktop_entries);
2477 return desktop_entries;
2480 /* GDesktopAppInfoLookup interface: */
2482 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2483 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2484 gpointer class_data);
2487 g_desktop_app_info_lookup_get_type (void)
2489 static GType desktop_app_info_lookup_type = 0;
2491 if (! desktop_app_info_lookup_type)
2493 static const GTypeInfo desktop_app_info_lookup_info =
2495 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2496 g_desktop_app_info_lookup_base_init, /* base_init */
2497 NULL, /* base_finalize */
2498 g_desktop_app_info_lookup_class_init,
2499 NULL, /* class_finalize */
2500 NULL, /* class_data */
2502 0, /* n_preallocs */
2506 desktop_app_info_lookup_type =
2507 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2508 &desktop_app_info_lookup_info, 0);
2510 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2513 return desktop_app_info_lookup_type;
2517 g_desktop_app_info_lookup_class_init (gpointer g_class,
2518 gpointer class_data)
2523 g_desktop_app_info_lookup_base_init (gpointer g_class)
2528 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2529 * @lookup: a #GDesktopAppInfoLookup
2530 * @uri_scheme: a string containing a URI scheme.
2532 * Gets the default application for launching applications
2533 * using this URI scheme for a particular GDesktopAppInfoLookup
2536 * The GDesktopAppInfoLookup interface and this function is used
2537 * to implement g_app_info_get_default_for_uri_scheme() backends
2538 * in a GIO module. There is no reason for applications to use it
2539 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2541 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2544 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2545 const char *uri_scheme)
2547 GDesktopAppInfoLookupIface *iface;
2549 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2551 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2553 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2556 #define __G_DESKTOP_APP_INFO_C__
2557 #include "gioaliasdef.c"