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_keyfile:
190 * @key_file: an opened #GKeyFile
192 * Creates a new #GDesktopAppInfo.
194 * Returns: a new #GDesktopAppInfo or %NULL on error.
199 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
201 GDesktopAppInfo *info;
206 start_group = g_key_file_get_start_group (key_file);
207 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
209 g_free (start_group);
212 g_free (start_group);
214 type = g_key_file_get_string (key_file,
215 G_KEY_FILE_DESKTOP_GROUP,
216 G_KEY_FILE_DESKTOP_KEY_TYPE,
218 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
225 try_exec = g_key_file_get_string (key_file,
226 G_KEY_FILE_DESKTOP_GROUP,
227 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
229 if (try_exec && try_exec[0] != '\0')
232 t = g_find_program_in_path (try_exec);
241 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
242 info->filename = NULL;
244 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
245 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
246 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
247 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
248 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);
249 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);
250 info->try_exec = try_exec;
251 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
252 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
253 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
254 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
255 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
260 if (g_path_is_absolute (info->icon_name))
264 file = g_file_new_for_path (info->icon_name);
265 info->icon = g_file_icon_new (file);
266 g_object_unref (file);
269 info->icon = g_themed_icon_new (info->icon_name);
273 info->binary = binary_from_exec (info->exec);
275 if (info->path && info->path[0] == '\0')
285 * g_desktop_app_info_new_from_filename:
286 * @filename: the path of a desktop file, in the GLib filename encoding
288 * Creates a new #GDesktopAppInfo.
290 * Returns: a new #GDesktopAppInfo or %NULL on error.
293 g_desktop_app_info_new_from_filename (const char *filename)
296 GDesktopAppInfo *info = NULL;
298 key_file = g_key_file_new ();
300 if (g_key_file_load_from_file (key_file,
305 info = g_desktop_app_info_new_from_keyfile (key_file);
307 info->filename = g_strdup (filename);
310 g_key_file_free (key_file);
316 * g_desktop_app_info_new:
317 * @desktop_id: the desktop file id
319 * Creates a new #GDesktopAppInfo.
321 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
324 g_desktop_app_info_new (const char *desktop_id)
326 GDesktopAppInfo *appinfo;
327 const char * const *dirs;
331 dirs = get_applications_search_path ();
333 basename = g_strdup (desktop_id);
335 for (i = 0; dirs[i] != NULL; i++)
340 filename = g_build_filename (dirs[i], desktop_id, NULL);
341 appinfo = g_desktop_app_info_new_from_filename (filename);
347 while ((p = strchr (p, '-')) != NULL)
351 filename = g_build_filename (dirs[i], basename, NULL);
352 appinfo = g_desktop_app_info_new_from_filename (filename);
367 appinfo->desktop_id = g_strdup (desktop_id);
369 if (g_desktop_app_info_get_is_hidden (appinfo))
371 g_object_unref (appinfo);
379 g_desktop_app_info_dup (GAppInfo *appinfo)
381 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
382 GDesktopAppInfo *new_info;
384 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
386 new_info->filename = g_strdup (info->filename);
387 new_info->desktop_id = g_strdup (info->desktop_id);
389 new_info->name = g_strdup (info->name);
390 new_info->comment = g_strdup (info->comment);
391 new_info->nodisplay = info->nodisplay;
392 new_info->icon_name = g_strdup (info->icon_name);
393 new_info->icon = g_object_ref (info->icon);
394 new_info->only_show_in = g_strdupv (info->only_show_in);
395 new_info->not_show_in = g_strdupv (info->not_show_in);
396 new_info->try_exec = g_strdup (info->try_exec);
397 new_info->exec = g_strdup (info->exec);
398 new_info->binary = g_strdup (info->binary);
399 new_info->path = g_strdup (info->path);
400 new_info->hidden = info->hidden;
401 new_info->terminal = info->terminal;
402 new_info->startup_notify = info->startup_notify;
404 return G_APP_INFO (new_info);
408 g_desktop_app_info_equal (GAppInfo *appinfo1,
411 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
412 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
414 if (info1->desktop_id == NULL ||
415 info2->desktop_id == NULL)
416 return info1 == info2;
418 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
422 g_desktop_app_info_get_id (GAppInfo *appinfo)
424 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
426 return info->desktop_id;
430 g_desktop_app_info_get_name (GAppInfo *appinfo)
432 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
434 if (info->name == NULL)
440 * g_desktop_app_info_get_is_hidden:
441 * @info: a #GDesktopAppInfo.
443 * A desktop file is hidden if the Hidden key in it is
446 * Returns: %TRUE if hidden, %FALSE otherwise.
449 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
455 g_desktop_app_info_get_description (GAppInfo *appinfo)
457 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
459 return info->comment;
463 g_desktop_app_info_get_executable (GAppInfo *appinfo)
465 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
471 g_desktop_app_info_get_icon (GAppInfo *appinfo)
473 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
479 expand_macro_single (char macro, char *uri)
485 file = g_file_new_for_uri (uri);
486 path = g_file_get_path (file);
487 g_object_unref (file);
493 result = g_shell_quote (uri);
498 result = g_shell_quote (path);
503 result = g_shell_quote (g_path_get_dirname (path));
508 result = g_shell_quote (g_path_get_basename (path));
518 expand_macro (char macro,
520 GDesktopAppInfo *info,
523 GList *uris = *uri_list;
526 g_return_if_fail (exec != NULL);
536 expanded = expand_macro_single (macro, uris->data);
539 g_string_append (exec, expanded);
553 expanded = expand_macro_single (macro, uris->data);
556 g_string_append (exec, expanded);
562 if (uris != NULL && expanded)
563 g_string_append_c (exec, ' ');
571 g_string_append (exec, "--icon ");
572 g_string_append (exec, info->icon_name);
578 g_string_append (exec, info->name);
583 g_string_append (exec, info->filename);
586 case 'm': /* deprecated */
590 g_string_append_c (exec, '%');
598 expand_application_parameters (GDesktopAppInfo *info,
604 GList *uri_list = *uris;
605 const char *p = info->exec;
606 GString *expanded_exec = g_string_new (NULL);
609 if (info->exec == NULL)
611 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
612 _("Desktop file didn't specify Exec field"));
618 if (p[0] == '%' && p[1] != '\0')
620 expand_macro (p[1], expanded_exec, info, uris);
624 g_string_append_c (expanded_exec, *p);
629 /* No file substitutions */
630 if (uri_list == *uris && uri_list != NULL)
632 /* If there is no macro default to %f. This is also what KDE does */
633 g_string_append_c (expanded_exec, ' ');
634 expand_macro ('f', expanded_exec, info, uris);
637 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
638 g_string_free (expanded_exec, TRUE);
643 prepend_terminal_to_vector (int *argc,
650 char **term_argv = NULL;
655 g_return_val_if_fail (argc != NULL, FALSE);
656 g_return_val_if_fail (argv != NULL, FALSE);
664 /* compute size if not given */
667 for (i = 0; the_argv[i] != NULL; i++)
673 term_argv = g_new0 (char *, 3);
675 check = g_find_program_in_path ("gnome-terminal");
678 term_argv[0] = check;
679 /* Note that gnome-terminal takes -x and
680 * as -e in gnome-terminal is broken we use that. */
681 term_argv[1] = g_strdup ("-x");
686 check = g_find_program_in_path ("nxterm");
688 check = g_find_program_in_path ("color-xterm");
690 check = g_find_program_in_path ("rxvt");
692 check = g_find_program_in_path ("xterm");
694 check = g_find_program_in_path ("dtterm");
697 check = g_strdup ("xterm");
698 g_warning ("couldn't find a terminal, falling back to xterm");
700 term_argv[0] = check;
701 term_argv[1] = g_strdup ("-e");
704 real_argc = term_argc + *argc;
705 real_argv = g_new (char *, real_argc + 1);
707 for (i = 0; i < term_argc; i++)
708 real_argv[i] = term_argv[i];
710 for (j = 0; j < *argc; j++, i++)
711 real_argv[i] = (char *)the_argv[j];
719 /* we use g_free here as we sucked all the inner strings
720 * out from it into real_argv */
725 #endif /* G_OS_WIN32 */
728 /* '=' is the new '\0'.
729 * DO NOT CALL unless at least one string ends with '='
732 is_env (const char *a,
737 if (*a == 0 || *b == 0)
750 /* free with g_strfreev */
752 replace_env_var (char **old_environ,
754 const char *new_value)
756 int length, new_length;
757 int index, new_index;
761 /* do two things at once:
762 * - discover the length of the environment ('length')
763 * - find the location (if any) of the env var ('index')
766 for (length = 0; old_environ[length]; length++)
768 /* if we already have it in our environment, replace */
769 if (is_env (old_environ[length], env_var))
774 /* no current env var, no desired env value.
777 if (new_value == NULL && index == -1)
780 /* in all cases now, we will be using a modified environment.
781 * determine its length and allocated it.
784 * new_index = location to insert, if any
785 * new_length = length of the new array
786 * new_environ = the pointer array for the new environment
789 if (new_value == NULL && index >= 0)
791 /* in this case, we will be removing an entry */
792 new_length = length - 1;
795 else if (new_value != NULL && index < 0)
797 /* in this case, we will be adding an entry to the end */
798 new_length = length + 1;
802 /* in this case, we will be replacing the existing entry */
808 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
809 new_environ[new_length] = NULL;
811 /* now we do the copying.
812 * for each entry in the new environment, we decide what to do
816 for (new_i = 0; new_i < new_length; new_i++)
818 if (new_i == new_index)
820 /* insert our new item */
821 new_environ[new_i] = g_strconcat (env_var,
826 /* if we had an old entry, skip it now */
832 /* if this is the old DESKTOP_STARTUP_ID, skip it */
836 /* copy an old item */
837 new_environ[new_i] = g_strdup (old_environ[i]);
842 g_strfreev (old_environ);
848 uri_list_segment_to_files (GList *start,
855 while (start != NULL && start != end)
857 file = g_file_new_for_uri ((char *)start->data);
858 res = g_list_prepend (res, file);
862 return g_list_reverse (res);
865 #ifdef HAVE__NSGETENVIRON
866 #define environ (*_NSGetEnviron())
867 #elif !defined(G_OS_WIN32)
869 /* According to the Single Unix Specification, environ is not in
870 * * any system header, although unistd.h often declares it.
872 extern char **environ;
876 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
878 GAppLaunchContext *launch_context,
881 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
882 gboolean completed = FALSE;
884 GList *launched_files;
891 g_return_val_if_fail (appinfo != NULL, FALSE);
899 if (!expand_application_parameters (info, &uris,
900 &argc, &argv, error))
903 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
905 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
906 _("Unable to find terminal required for application"));
913 launched_files = uri_list_segment_to_files (old_uris, uris);
915 display = g_app_launch_context_get_display (launch_context,
920 if (info->startup_notify)
921 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
925 if (display || sn_id)
929 envp = g_new0 (char *, 1);
931 envp = g_strdupv (environ);
935 envp = replace_env_var (envp,
940 envp = replace_env_var (envp,
941 "DESKTOP_STARTUP_ID",
947 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
948 g_list_free (launched_files);
951 if (!g_spawn_async (info->path, /* working directory */
954 G_SPAWN_SEARCH_PATH /* flags */,
955 NULL /* child_setup */,
957 NULL /* child_pid */,
962 g_app_launch_context_launch_failed (launch_context, sn_id);
976 while (uris != NULL);
988 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
990 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
993 ((strstr (info->exec, "%u") != NULL) ||
994 (strstr (info->exec, "%U") != NULL));
998 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1000 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1002 return info->exec &&
1003 ((strstr (info->exec, "%f") != NULL) ||
1004 (strstr (info->exec, "%F") != NULL));
1008 g_desktop_app_info_launch (GAppInfo *appinfo,
1010 GAppLaunchContext *launch_context,
1020 uri = g_file_get_uri (files->data);
1021 uris = g_list_prepend (uris, uri);
1022 files = files->next;
1025 uris = g_list_reverse (uris);
1027 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1029 g_list_foreach (uris, (GFunc)g_free, NULL);
1035 G_LOCK_DEFINE_STATIC (g_desktop_env);
1036 static gchar *g_desktop_env = NULL;
1039 * g_desktop_app_info_set_desktop_env:
1040 * @desktop_env: a string specifying what desktop this is
1042 * Sets the name of the desktop that the application is running in.
1043 * This is used by g_app_info_should_show() to evaluate the
1044 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1045 * desktop entry fields.
1047 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1048 * Menu specification</ulink> recognizes the following:
1050 * <member>GNOME</member>
1051 * <member>KDE</member>
1052 * <member>ROX</member>
1053 * <member>XFCE</member>
1054 * <member>Old</member>
1057 * Should be called only once; subsequent calls are ignored.
1060 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1062 G_LOCK (g_desktop_env);
1064 g_desktop_env = g_strdup (desktop_env);
1065 G_UNLOCK (g_desktop_env);
1069 g_desktop_app_info_should_show (GAppInfo *appinfo)
1071 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1073 const gchar *desktop_env;
1076 if (info->nodisplay)
1079 G_LOCK (g_desktop_env);
1080 desktop_env = g_desktop_env;
1081 G_UNLOCK (g_desktop_env);
1083 if (info->only_show_in)
1085 if (desktop_env == NULL)
1089 for (i = 0; info->only_show_in[i] != NULL; i++)
1091 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1101 if (info->not_show_in && desktop_env)
1103 for (i = 0; info->not_show_in[i] != NULL; i++)
1105 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1119 ensure_dir (DirType type,
1122 char *path, *display_name;
1125 if (type == APP_DIR)
1126 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1128 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1131 if (g_mkdir_with_parents (path, 0700) == 0)
1135 display_name = g_filename_display_name (path);
1136 if (type == APP_DIR)
1137 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1138 _("Can't create user application configuration folder %s: %s"),
1139 display_name, g_strerror (errsv));
1141 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1142 _("Can't create user MIME configuration folder %s: %s"),
1143 display_name, g_strerror (errsv));
1145 g_free (display_name);
1152 update_mimeapps_list (const char *desktop_id,
1153 const char *content_type,
1154 gboolean add_at_start,
1155 gboolean add_at_end,
1159 char *dirname, *filename;
1161 gboolean load_succeeded, res;
1164 gsize length, data_size;
1168 /* Don't add both at start and end */
1169 g_assert (!(add_at_start && add_at_end));
1171 dirname = ensure_dir (APP_DIR, error);
1175 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1178 key_file = g_key_file_new ();
1179 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1180 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1182 g_key_file_free (key_file);
1183 key_file = g_key_file_new ();
1186 /* Add to the right place in the list */
1189 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1190 content_type, &length, NULL);
1192 list = g_new (char *, 1 + length + 1);
1196 list[i++] = g_strdup (desktop_id);
1199 for (j = 0; old_list[j] != NULL; j++)
1201 if (strcmp (old_list[j], desktop_id) != 0)
1202 list[i++] = g_strdup (old_list[j]);
1206 list[i++] = g_strdup (desktop_id);
1209 g_strfreev (old_list);
1211 g_key_file_set_string_list (key_file,
1212 ADDED_ASSOCIATIONS_GROUP,
1214 (const char * const *)list, i);
1218 /* Remove from removed associations group (unless remove) */
1221 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1222 content_type, &length, NULL);
1224 list = g_new (char *, 1 + length + 1);
1228 list[i++] = g_strdup (desktop_id);
1231 for (j = 0; old_list[j] != NULL; j++)
1233 if (strcmp (old_list[j], desktop_id) != 0)
1234 list[i++] = g_strdup (old_list[j]);
1239 g_strfreev (old_list);
1241 if (list[0] == NULL)
1242 g_key_file_remove_key (key_file,
1243 REMOVED_ASSOCIATIONS_GROUP,
1247 g_key_file_set_string_list (key_file,
1248 REMOVED_ASSOCIATIONS_GROUP,
1250 (const char * const *)list, i);
1255 data = g_key_file_to_data (key_file, &data_size, error);
1256 g_key_file_free (key_file);
1258 res = g_file_set_contents (filename, data, data_size, error);
1260 mime_info_cache_reload (NULL);
1269 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1270 const char *content_type,
1273 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1275 if (!g_desktop_app_info_ensure_saved (info, error))
1278 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1282 update_program_done (GPid pid,
1286 /* Did the application exit correctly */
1287 if (WIFEXITED (status) &&
1288 WEXITSTATUS (status) == 0)
1290 /* Here we could clean out any caches in use */
1295 run_update_command (char *command,
1304 GError *error = NULL;
1307 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1309 if (g_spawn_async ("/", argv,
1311 G_SPAWN_SEARCH_PATH |
1312 G_SPAWN_STDOUT_TO_DEV_NULL |
1313 G_SPAWN_STDERR_TO_DEV_NULL |
1314 G_SPAWN_DO_NOT_REAP_CHILD,
1315 NULL, NULL, /* No setup function */
1318 g_child_watch_add (pid, update_program_done, NULL);
1321 /* If we get an error at this point, it's quite likely the user doesn't
1322 * have an installed copy of either 'update-mime-database' or
1323 * 'update-desktop-database'. I don't think we want to popup an error
1324 * dialog at this point, so we just do a g_warning to give the user a
1325 * chance of debugging it.
1327 g_warning ("%s", error->message);
1334 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1335 const char *extension,
1338 char *filename, *basename, *mimetype;
1342 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1345 dirname = ensure_dir (MIMETYPE_DIR, error);
1349 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1350 filename = g_build_filename (dirname, basename, NULL);
1354 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1356 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1361 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1362 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1363 " <mime-type type=\"%s\">\n"
1364 " <comment>%s document</comment>\n"
1365 " <glob pattern=\"*.%s\"/>\n"
1367 "</mime-info>\n", mimetype, extension, extension);
1369 g_file_set_contents (filename, contents, -1, NULL);
1372 run_update_command ("update-mime-database", "mime");
1376 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1386 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1387 const char *content_type,
1390 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1392 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1395 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1399 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1405 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1406 const char *content_type,
1409 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1411 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1414 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1418 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1424 char *data, *desktop_id;
1429 if (info->filename != NULL)
1432 /* This is only used for object created with
1433 * g_app_info_create_from_commandline. All other
1434 * object should have a filename
1437 dirname = ensure_dir (APP_DIR, error);
1441 key_file = g_key_file_new ();
1443 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1444 "Encoding", "UTF-8");
1445 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1446 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1447 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1448 G_KEY_FILE_DESKTOP_KEY_TYPE,
1449 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1451 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1452 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1454 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1455 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1457 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1458 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1460 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1461 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1463 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1464 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1466 data = g_key_file_to_data (key_file, &data_size, NULL);
1467 g_key_file_free (key_file);
1469 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1470 filename = g_build_filename (dirname, desktop_id, NULL);
1471 g_free (desktop_id);
1474 fd = g_mkstemp (filename);
1479 display_name = g_filename_display_name (filename);
1480 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1481 _("Can't create user desktop file %s"), display_name);
1482 g_free (display_name);
1488 desktop_id = g_path_get_basename (filename);
1492 res = g_file_set_contents (filename, data, data_size, error);
1495 g_free (desktop_id);
1500 info->filename = filename;
1501 info->desktop_id = desktop_id;
1503 run_update_command ("update-desktop-database", "applications");
1509 * g_app_info_create_from_commandline:
1510 * @commandline: the commandline to use
1511 * @application_name: the application name, or %NULL to use @commandline
1512 * @flags: flags that can specify details of the created #GAppInfo
1513 * @error: a #GError location to store the error occuring, %NULL to ignore.
1515 * Creates a new #GAppInfo from the given information.
1517 * Returns: new #GAppInfo for given command.
1520 g_app_info_create_from_commandline (const char *commandline,
1521 const char *application_name,
1522 GAppInfoCreateFlags flags,
1527 GDesktopAppInfo *info;
1529 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1531 info->filename = NULL;
1532 info->desktop_id = NULL;
1534 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1535 info->startup_notify = FALSE;
1536 info->hidden = FALSE;
1537 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1538 info->exec = g_strconcat (commandline, " %u", NULL);
1540 info->exec = g_strconcat (commandline, " %f", NULL);
1541 info->nodisplay = TRUE;
1542 info->binary = binary_from_exec (info->exec);
1544 if (application_name)
1545 info->name = g_strdup (application_name);
1548 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1549 split = g_strsplit (commandline, " ", 2);
1550 basename = g_path_get_basename (split[0]);
1552 info->name = basename;
1553 if (info->name == NULL)
1554 info->name = g_strdup ("custom");
1556 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1558 return G_APP_INFO (info);
1562 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1564 iface->dup = g_desktop_app_info_dup;
1565 iface->equal = g_desktop_app_info_equal;
1566 iface->get_id = g_desktop_app_info_get_id;
1567 iface->get_name = g_desktop_app_info_get_name;
1568 iface->get_description = g_desktop_app_info_get_description;
1569 iface->get_executable = g_desktop_app_info_get_executable;
1570 iface->get_icon = g_desktop_app_info_get_icon;
1571 iface->launch = g_desktop_app_info_launch;
1572 iface->supports_uris = g_desktop_app_info_supports_uris;
1573 iface->supports_files = g_desktop_app_info_supports_files;
1574 iface->launch_uris = g_desktop_app_info_launch_uris;
1575 iface->should_show = g_desktop_app_info_should_show;
1576 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1577 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1578 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1579 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1580 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1584 app_info_in_list (GAppInfo *info,
1587 while (list != NULL)
1589 if (g_app_info_equal (info, list->data))
1598 * g_app_info_get_all_for_type:
1599 * @content_type: the content type to find a #GAppInfo for
1601 * Gets a list of all #GAppInfo s for a given content type.
1603 * Returns: #GList of #GAppInfo s for given @content_type
1604 * or %NULL on error.
1607 g_app_info_get_all_for_type (const char *content_type)
1609 GList *desktop_entries, *l;
1611 GDesktopAppInfo *info;
1613 g_return_val_if_fail (content_type != NULL, NULL);
1615 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1618 for (l = desktop_entries; l != NULL; l = l->next)
1620 char *desktop_entry = l->data;
1622 info = g_desktop_app_info_new (desktop_entry);
1625 if (app_info_in_list (G_APP_INFO (info), infos))
1626 g_object_unref (info);
1628 infos = g_list_prepend (infos, info);
1630 g_free (desktop_entry);
1633 g_list_free (desktop_entries);
1635 return g_list_reverse (infos);
1640 * g_app_info_get_default_for_type:
1641 * @content_type: the content type to find a #GAppInfo for
1642 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1645 * Gets the #GAppInfo that correspond to a given content type.
1647 * Returns: #GAppInfo for given @content_type or %NULL on error.
1650 g_app_info_get_default_for_type (const char *content_type,
1651 gboolean must_support_uris)
1653 GList *desktop_entries, *l;
1656 g_return_val_if_fail (content_type != NULL, NULL);
1658 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1661 for (l = desktop_entries; l != NULL; l = l->next)
1663 char *desktop_entry = l->data;
1665 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1668 if (must_support_uris && !g_app_info_supports_uris (info))
1670 g_object_unref (info);
1678 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1679 g_list_free (desktop_entries);
1685 * g_app_info_get_default_for_uri_scheme:
1686 * @uri_scheme: a string containing a URI scheme.
1688 * Gets the default application for launching applications
1689 * using this URI scheme. A URI scheme is the initial part
1690 * of the URI, up to but not including the ':', e.g. "http",
1693 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1696 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1698 static gsize lookup = 0;
1700 if (g_once_init_enter (&lookup))
1702 gsize setup_value = 1;
1703 GDesktopAppInfoLookup *lookup_instance;
1704 const char *use_this;
1705 GIOExtensionPoint *ep;
1706 GIOExtension *extension;
1709 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1711 /* Ensure vfs in modules loaded */
1712 _g_io_modules_ensure_loaded ();
1714 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1716 lookup_instance = NULL;
1719 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1721 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1724 if (lookup_instance == NULL)
1726 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1728 extension = l->data;
1729 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1730 if (lookup_instance != NULL)
1735 if (lookup_instance != NULL)
1736 setup_value = (gsize)lookup_instance;
1738 g_once_init_leave (&lookup, setup_value);
1744 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1750 get_apps_from_dir (GHashTable *apps,
1751 const char *dirname,
1755 const char *basename;
1756 char *filename, *subprefix, *desktop_id;
1758 GDesktopAppInfo *appinfo;
1760 dir = g_dir_open (dirname, 0, NULL);
1763 while ((basename = g_dir_read_name (dir)) != NULL)
1765 filename = g_build_filename (dirname, basename, NULL);
1766 if (g_str_has_suffix (basename, ".desktop"))
1768 desktop_id = g_strconcat (prefix, basename, NULL);
1770 /* Use _extended so we catch NULLs too (hidden) */
1771 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1773 appinfo = g_desktop_app_info_new_from_filename (filename);
1775 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1777 g_object_unref (appinfo);
1782 if (appinfo || hidden)
1784 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1788 /* Reuse instead of strdup here */
1789 appinfo->desktop_id = desktop_id;
1794 g_free (desktop_id);
1798 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1800 subprefix = g_strconcat (prefix, basename, "-", NULL);
1801 get_apps_from_dir (apps, filename, subprefix);
1813 * g_app_info_get_all:
1815 * Gets a list of all of the applications currently registered
1818 * For desktop files, this includes applications that have
1819 * <literal>NoDisplay=true</literal> set or are excluded from
1820 * display by means of <literal>OnlyShowIn</literal> or
1821 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1822 * The returned list does not include applications which have
1823 * the <literal>Hidden</literal> key set.
1825 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1828 g_app_info_get_all (void)
1830 const char * const *dirs;
1832 GHashTableIter iter;
1837 dirs = get_applications_search_path ();
1839 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1843 for (i = 0; dirs[i] != NULL; i++)
1844 get_apps_from_dir (apps, dirs[i], "");
1848 g_hash_table_iter_init (&iter, apps);
1849 while (g_hash_table_iter_next (&iter, NULL, &value))
1852 infos = g_list_prepend (infos, value);
1855 g_hash_table_destroy (apps);
1857 return g_list_reverse (infos);
1860 /* Cacheing of mimeinfo.cache and defaults.list files */
1864 GHashTable *mime_info_cache_map;
1865 GHashTable *defaults_list_map;
1866 GHashTable *mimeapps_list_added_map;
1867 GHashTable *mimeapps_list_removed_map;
1868 time_t mime_info_cache_timestamp;
1869 time_t defaults_list_timestamp;
1870 time_t mimeapps_list_timestamp;
1874 GList *dirs; /* mimeinfo.cache and defaults.list */
1875 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1876 time_t last_stat_time;
1877 guint should_ping_mime_monitor : 1;
1880 static MimeInfoCache *mime_info_cache = NULL;
1881 G_LOCK_DEFINE_STATIC (mime_info_cache);
1883 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1884 const char *mime_type,
1885 char **new_desktop_file_ids);
1887 static MimeInfoCache * mime_info_cache_new (void);
1890 destroy_info_cache_value (gpointer key,
1894 g_list_foreach (value, (GFunc)g_free, NULL);
1895 g_list_free (value);
1899 destroy_info_cache_map (GHashTable *info_cache_map)
1901 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1902 g_hash_table_destroy (info_cache_map);
1906 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1907 const char *cache_file,
1913 filename = g_build_filename (dir->path, cache_file, NULL);
1915 if (g_stat (filename, &buf) < 0)
1922 if (buf.st_mtime != *timestamp)
1928 /* Call with lock held */
1930 remove_all (gpointer key,
1939 mime_info_cache_blow_global_cache (void)
1941 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1946 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1950 gchar *filename, **mime_types;
1957 if (dir->mime_info_cache_map != NULL &&
1958 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1959 &dir->mime_info_cache_timestamp))
1962 if (dir->mime_info_cache_map != NULL)
1963 destroy_info_cache_map (dir->mime_info_cache_map);
1965 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1966 (GDestroyNotify) g_free,
1969 key_file = g_key_file_new ();
1971 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1973 if (g_stat (filename, &buf) < 0)
1976 if (dir->mime_info_cache_timestamp > 0)
1977 mime_info_cache->should_ping_mime_monitor = TRUE;
1979 dir->mime_info_cache_timestamp = buf.st_mtime;
1981 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1986 if (load_error != NULL)
1989 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1992 if (load_error != NULL)
1995 for (i = 0; mime_types[i] != NULL; i++)
1997 gchar **desktop_file_ids;
1998 char *unaliased_type;
1999 desktop_file_ids = g_key_file_get_string_list (key_file,
2005 if (desktop_file_ids == NULL)
2008 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2009 mime_info_cache_dir_add_desktop_entries (dir,
2012 g_free (unaliased_type);
2014 g_strfreev (desktop_file_ids);
2017 g_strfreev (mime_types);
2018 g_key_file_free (key_file);
2023 g_key_file_free (key_file);
2025 if (mime_types != NULL)
2026 g_strfreev (mime_types);
2029 g_error_free (load_error);
2033 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2037 gchar *filename, **mime_types;
2038 char *unaliased_type;
2039 char **desktop_file_ids;
2046 if (dir->defaults_list_map != NULL &&
2047 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2048 &dir->defaults_list_timestamp))
2051 if (dir->defaults_list_map != NULL)
2052 g_hash_table_destroy (dir->defaults_list_map);
2053 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2054 g_free, (GDestroyNotify)g_strfreev);
2057 key_file = g_key_file_new ();
2059 filename = g_build_filename (dir->path, "defaults.list", NULL);
2060 if (g_stat (filename, &buf) < 0)
2063 if (dir->defaults_list_timestamp > 0)
2064 mime_info_cache->should_ping_mime_monitor = TRUE;
2066 dir->defaults_list_timestamp = buf.st_mtime;
2068 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2072 if (load_error != NULL)
2075 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2077 if (mime_types != NULL)
2079 for (i = 0; mime_types[i] != NULL; i++)
2081 desktop_file_ids = g_key_file_get_string_list (key_file,
2082 DEFAULT_APPLICATIONS_GROUP,
2086 if (desktop_file_ids == NULL)
2089 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2090 g_hash_table_replace (dir->defaults_list_map,
2095 g_strfreev (mime_types);
2098 g_key_file_free (key_file);
2103 g_key_file_free (key_file);
2105 if (mime_types != NULL)
2106 g_strfreev (mime_types);
2109 g_error_free (load_error);
2113 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2117 gchar *filename, **mime_types;
2118 char *unaliased_type;
2119 char **desktop_file_ids;
2126 if (dir->mimeapps_list_added_map != NULL &&
2127 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2128 &dir->mimeapps_list_timestamp))
2131 if (dir->mimeapps_list_added_map != NULL)
2132 g_hash_table_destroy (dir->mimeapps_list_added_map);
2133 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2134 g_free, (GDestroyNotify)g_strfreev);
2136 if (dir->mimeapps_list_removed_map != NULL)
2137 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2138 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2139 g_free, (GDestroyNotify)g_strfreev);
2141 key_file = g_key_file_new ();
2143 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2144 if (g_stat (filename, &buf) < 0)
2147 if (dir->mimeapps_list_timestamp > 0)
2148 mime_info_cache->should_ping_mime_monitor = TRUE;
2150 dir->mimeapps_list_timestamp = buf.st_mtime;
2152 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2156 if (load_error != NULL)
2159 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2161 if (mime_types != NULL)
2163 for (i = 0; mime_types[i] != NULL; i++)
2165 desktop_file_ids = g_key_file_get_string_list (key_file,
2166 ADDED_ASSOCIATIONS_GROUP,
2170 if (desktop_file_ids == NULL)
2173 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2174 g_hash_table_replace (dir->mimeapps_list_added_map,
2179 g_strfreev (mime_types);
2182 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2184 if (mime_types != NULL)
2186 for (i = 0; mime_types[i] != NULL; i++)
2188 desktop_file_ids = g_key_file_get_string_list (key_file,
2189 REMOVED_ASSOCIATIONS_GROUP,
2193 if (desktop_file_ids == NULL)
2196 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2197 g_hash_table_replace (dir->mimeapps_list_removed_map,
2202 g_strfreev (mime_types);
2205 g_key_file_free (key_file);
2210 g_key_file_free (key_file);
2212 if (mime_types != NULL)
2213 g_strfreev (mime_types);
2216 g_error_free (load_error);
2219 static MimeInfoCacheDir *
2220 mime_info_cache_dir_new (const char *path)
2222 MimeInfoCacheDir *dir;
2224 dir = g_new0 (MimeInfoCacheDir, 1);
2225 dir->path = g_strdup (path);
2231 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2236 if (dir->mime_info_cache_map != NULL)
2238 destroy_info_cache_map (dir->mime_info_cache_map);
2239 dir->mime_info_cache_map = NULL;
2243 if (dir->defaults_list_map != NULL)
2245 g_hash_table_destroy (dir->defaults_list_map);
2246 dir->defaults_list_map = NULL;
2249 if (dir->mimeapps_list_added_map != NULL)
2251 g_hash_table_destroy (dir->mimeapps_list_added_map);
2252 dir->mimeapps_list_added_map = NULL;
2255 if (dir->mimeapps_list_removed_map != NULL)
2257 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2258 dir->mimeapps_list_removed_map = NULL;
2265 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2266 const char *mime_type,
2267 char **new_desktop_file_ids)
2269 GList *desktop_file_ids;
2272 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2275 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2277 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2278 desktop_file_ids = g_list_append (desktop_file_ids,
2279 g_strdup (new_desktop_file_ids[i]));
2282 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2286 mime_info_cache_init_dir_lists (void)
2288 const char * const *dirs;
2291 mime_info_cache = mime_info_cache_new ();
2293 dirs = get_applications_search_path ();
2295 for (i = 0; dirs[i] != NULL; i++)
2297 MimeInfoCacheDir *dir;
2299 dir = mime_info_cache_dir_new (dirs[i]);
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);
2307 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2313 mime_info_cache_update_dir_lists (void)
2317 tmp = mime_info_cache->dirs;
2321 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2323 /* No need to do this if we had file monitors... */
2324 mime_info_cache_blow_global_cache ();
2325 mime_info_cache_dir_init (dir);
2326 mime_info_cache_dir_init_defaults_list (dir);
2327 mime_info_cache_dir_init_mimeapps_list (dir);
2334 mime_info_cache_init (void)
2336 G_LOCK (mime_info_cache);
2337 if (mime_info_cache == NULL)
2338 mime_info_cache_init_dir_lists ();
2344 if (now >= mime_info_cache->last_stat_time + 10)
2346 mime_info_cache_update_dir_lists ();
2347 mime_info_cache->last_stat_time = now;
2351 if (mime_info_cache->should_ping_mime_monitor)
2353 /* g_idle_add (emit_mime_changed, NULL); */
2354 mime_info_cache->should_ping_mime_monitor = FALSE;
2357 G_UNLOCK (mime_info_cache);
2360 static MimeInfoCache *
2361 mime_info_cache_new (void)
2363 MimeInfoCache *cache;
2365 cache = g_new0 (MimeInfoCache, 1);
2367 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2368 (GDestroyNotify) g_free,
2369 (GDestroyNotify) g_free);
2374 mime_info_cache_free (MimeInfoCache *cache)
2379 g_list_foreach (cache->dirs,
2380 (GFunc) mime_info_cache_dir_free,
2382 g_list_free (cache->dirs);
2383 g_hash_table_destroy (cache->global_defaults_cache);
2388 * mime_info_cache_reload:
2389 * @dir: directory path which needs reloading.
2391 * Reload the mime information for the @dir.
2394 mime_info_cache_reload (const char *dir)
2396 /* FIXME: just reload the dir that needs reloading,
2397 * don't blow the whole cache
2399 if (mime_info_cache != NULL)
2401 G_LOCK (mime_info_cache);
2402 mime_info_cache_free (mime_info_cache);
2403 mime_info_cache = NULL;
2404 G_UNLOCK (mime_info_cache);
2409 append_desktop_entry (GList *list,
2410 const char *desktop_entry,
2411 GList *removed_entries)
2413 /* Add if not already in list, and valid */
2414 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2415 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2416 list = g_list_prepend (list, g_strdup (desktop_entry));
2422 * get_all_desktop_entries_for_mime_type:
2423 * @mime_type: a mime type.
2425 * Returns all the desktop ids for @mime_type. The desktop files
2426 * are listed in an order so that default applications are listed before
2427 * non-default ones, and handlers for inherited mimetypes are listed
2428 * after the base ones.
2430 * Return value: a #GList containing the desktop ids which claim
2431 * to handle @mime_type.
2434 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2436 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2437 MimeInfoCacheDir *dir;
2440 char **default_entries;
2441 char **removed_associations;
2446 mime_info_cache_init ();
2448 /* collect all ancestors */
2449 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2450 array = g_ptr_array_new ();
2451 for (i = 0; mime_types[i]; i++)
2452 g_ptr_array_add (array, mime_types[i]);
2453 g_free (mime_types);
2454 for (i = 0; i < array->len; i++)
2456 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2457 for (j = 0; anc[j]; j++)
2459 for (k = 0; k < array->len; k++)
2461 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2464 if (k == array->len) /* not found */
2465 g_ptr_array_add (array, g_strdup (anc[j]));
2469 g_ptr_array_add (array, NULL);
2470 mime_types = g_ptr_array_free (array, FALSE);
2472 G_LOCK (mime_info_cache);
2474 removed_entries = NULL;
2475 desktop_entries = NULL;
2476 for (i = 0; mime_types[i] != NULL; i++)
2478 mime_type = mime_types[i];
2480 /* Go through all apps listed as defaults */
2481 for (dir_list = mime_info_cache->dirs;
2483 dir_list = dir_list->next)
2485 dir = dir_list->data;
2487 /* First added associations from mimeapps.list */
2488 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2489 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2490 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2492 /* Then removed associations from mimeapps.list */
2493 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2494 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2495 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2497 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2498 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2499 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2500 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2503 /* Go through all entries that support the mimetype */
2504 for (dir_list = mime_info_cache->dirs;
2506 dir_list = dir_list->next)
2508 dir = dir_list->data;
2510 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2511 for (tmp = list; tmp != NULL; tmp = tmp->next)
2512 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2516 G_UNLOCK (mime_info_cache);
2518 g_strfreev (mime_types);
2520 g_list_free (removed_entries);
2522 desktop_entries = g_list_reverse (desktop_entries);
2524 return desktop_entries;
2527 /* GDesktopAppInfoLookup interface: */
2529 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2530 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2531 gpointer class_data);
2534 g_desktop_app_info_lookup_get_type (void)
2536 static volatile gsize g_define_type_id__volatile = 0;
2538 if (g_once_init_enter (&g_define_type_id__volatile))
2540 const GTypeInfo desktop_app_info_lookup_info =
2542 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2543 g_desktop_app_info_lookup_base_init, /* base_init */
2544 NULL, /* base_finalize */
2545 g_desktop_app_info_lookup_class_init,
2546 NULL, /* class_finalize */
2547 NULL, /* class_data */
2549 0, /* n_preallocs */
2552 GType g_define_type_id =
2553 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2554 &desktop_app_info_lookup_info, 0);
2556 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
2558 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
2561 return g_define_type_id__volatile;
2565 g_desktop_app_info_lookup_class_init (gpointer g_class,
2566 gpointer class_data)
2571 g_desktop_app_info_lookup_base_init (gpointer g_class)
2576 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2577 * @lookup: a #GDesktopAppInfoLookup
2578 * @uri_scheme: a string containing a URI scheme.
2580 * Gets the default application for launching applications
2581 * using this URI scheme for a particular GDesktopAppInfoLookup
2584 * The GDesktopAppInfoLookup interface and this function is used
2585 * to implement g_app_info_get_default_for_uri_scheme() backends
2586 * in a GIO module. There is no reason for applications to use it
2587 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2589 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2592 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2593 const char *uri_scheme)
2595 GDesktopAppInfoLookupIface *iface;
2597 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2599 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2601 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2604 #define __G_DESKTOP_APP_INFO_C__
2605 #include "gioaliasdef.c"