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);
504 name = g_path_get_dirname (path);
505 result = g_shell_quote (name);
513 name = g_path_get_basename (path);
514 result = g_shell_quote (name);
526 expand_macro (char macro,
528 GDesktopAppInfo *info,
531 GList *uris = *uri_list;
533 gboolean force_file_uri;
534 char force_file_uri_macro;
536 g_return_if_fail (exec != NULL);
538 /* On %u and %U, pass POSIX file path pointing to the URI via
539 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
540 * running or the URI doesn't have a POSIX file path via FUSE
541 * we'll just pass the URI.
546 force_file_uri_macro = 'f';
547 force_file_uri = TRUE;
550 force_file_uri_macro = 'F';
551 force_file_uri = TRUE;
554 force_file_uri_macro = macro;
555 force_file_uri = FALSE;
569 expanded = expand_macro_single (macro, uris->data);
573 expanded = expand_macro_single (force_file_uri_macro, uris->data);
574 if (expanded == NULL)
575 expanded = expand_macro_single (macro, uris->data);
580 g_string_append (exec, expanded);
596 expanded = expand_macro_single (macro, uris->data);
600 expanded = expand_macro_single (force_file_uri_macro, uris->data);
601 if (expanded == NULL)
602 expanded = expand_macro_single (macro, uris->data);
607 g_string_append (exec, expanded);
613 if (uris != NULL && expanded)
614 g_string_append_c (exec, ' ');
622 g_string_append (exec, "--icon ");
623 g_string_append (exec, info->icon_name);
629 g_string_append (exec, info->name);
634 g_string_append (exec, info->filename);
637 case 'm': /* deprecated */
641 g_string_append_c (exec, '%');
649 expand_application_parameters (GDesktopAppInfo *info,
655 GList *uri_list = *uris;
656 const char *p = info->exec;
657 GString *expanded_exec = g_string_new (NULL);
660 if (info->exec == NULL)
662 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
663 _("Desktop file didn't specify Exec field"));
669 if (p[0] == '%' && p[1] != '\0')
671 expand_macro (p[1], expanded_exec, info, uris);
675 g_string_append_c (expanded_exec, *p);
680 /* No file substitutions */
681 if (uri_list == *uris && uri_list != NULL)
683 /* If there is no macro default to %f. This is also what KDE does */
684 g_string_append_c (expanded_exec, ' ');
685 expand_macro ('f', expanded_exec, info, uris);
688 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
689 g_string_free (expanded_exec, TRUE);
694 prepend_terminal_to_vector (int *argc,
701 char **term_argv = NULL;
706 g_return_val_if_fail (argc != NULL, FALSE);
707 g_return_val_if_fail (argv != NULL, FALSE);
715 /* compute size if not given */
718 for (i = 0; the_argv[i] != NULL; i++)
724 term_argv = g_new0 (char *, 3);
726 check = g_find_program_in_path ("gnome-terminal");
729 term_argv[0] = check;
730 /* Note that gnome-terminal takes -x and
731 * as -e in gnome-terminal is broken we use that. */
732 term_argv[1] = g_strdup ("-x");
737 check = g_find_program_in_path ("nxterm");
739 check = g_find_program_in_path ("color-xterm");
741 check = g_find_program_in_path ("rxvt");
743 check = g_find_program_in_path ("xterm");
745 check = g_find_program_in_path ("dtterm");
748 check = g_strdup ("xterm");
749 g_warning ("couldn't find a terminal, falling back to xterm");
751 term_argv[0] = check;
752 term_argv[1] = g_strdup ("-e");
755 real_argc = term_argc + *argc;
756 real_argv = g_new (char *, real_argc + 1);
758 for (i = 0; i < term_argc; i++)
759 real_argv[i] = term_argv[i];
761 for (j = 0; j < *argc; j++, i++)
762 real_argv[i] = (char *)the_argv[j];
770 /* we use g_free here as we sucked all the inner strings
771 * out from it into real_argv */
776 #endif /* G_OS_WIN32 */
779 /* '=' is the new '\0'.
780 * DO NOT CALL unless at least one string ends with '='
783 is_env (const char *a,
788 if (*a == 0 || *b == 0)
801 /* free with g_strfreev */
803 replace_env_var (char **old_environ,
805 const char *new_value)
807 int length, new_length;
808 int index, new_index;
812 /* do two things at once:
813 * - discover the length of the environment ('length')
814 * - find the location (if any) of the env var ('index')
817 for (length = 0; old_environ[length]; length++)
819 /* if we already have it in our environment, replace */
820 if (is_env (old_environ[length], env_var))
825 /* no current env var, no desired env value.
828 if (new_value == NULL && index == -1)
831 /* in all cases now, we will be using a modified environment.
832 * determine its length and allocated it.
835 * new_index = location to insert, if any
836 * new_length = length of the new array
837 * new_environ = the pointer array for the new environment
840 if (new_value == NULL && index >= 0)
842 /* in this case, we will be removing an entry */
843 new_length = length - 1;
846 else if (new_value != NULL && index < 0)
848 /* in this case, we will be adding an entry to the end */
849 new_length = length + 1;
853 /* in this case, we will be replacing the existing entry */
859 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
860 new_environ[new_length] = NULL;
862 /* now we do the copying.
863 * for each entry in the new environment, we decide what to do
867 for (new_i = 0; new_i < new_length; new_i++)
869 if (new_i == new_index)
871 /* insert our new item */
872 new_environ[new_i] = g_strconcat (env_var,
877 /* if we had an old entry, skip it now */
883 /* if this is the old DESKTOP_STARTUP_ID, skip it */
887 /* copy an old item */
888 new_environ[new_i] = g_strdup (old_environ[i]);
893 g_strfreev (old_environ);
899 uri_list_segment_to_files (GList *start,
906 while (start != NULL && start != end)
908 file = g_file_new_for_uri ((char *)start->data);
909 res = g_list_prepend (res, file);
913 return g_list_reverse (res);
916 #ifdef HAVE__NSGETENVIRON
917 #define environ (*_NSGetEnviron())
918 #elif !defined(G_OS_WIN32)
920 /* According to the Single Unix Specification, environ is not in
921 * * any system header, although unistd.h often declares it.
923 extern char **environ;
927 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
929 GAppLaunchContext *launch_context,
932 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
933 gboolean completed = FALSE;
935 GList *launched_files;
942 g_return_val_if_fail (appinfo != NULL, FALSE);
950 if (!expand_application_parameters (info, &uris,
951 &argc, &argv, error))
954 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
956 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
957 _("Unable to find terminal required for application"));
964 launched_files = uri_list_segment_to_files (old_uris, uris);
966 display = g_app_launch_context_get_display (launch_context,
971 if (info->startup_notify)
972 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
976 if (display || sn_id)
980 envp = g_new0 (char *, 1);
982 envp = g_strdupv (environ);
986 envp = replace_env_var (envp,
991 envp = replace_env_var (envp,
992 "DESKTOP_STARTUP_ID",
998 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
999 g_list_free (launched_files);
1002 if (!g_spawn_async (info->path, /* working directory */
1005 G_SPAWN_SEARCH_PATH /* flags */,
1006 NULL /* child_setup */,
1008 NULL /* child_pid */,
1013 g_app_launch_context_launch_failed (launch_context, sn_id);
1027 while (uris != NULL);
1039 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1041 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1043 return info->exec &&
1044 ((strstr (info->exec, "%u") != NULL) ||
1045 (strstr (info->exec, "%U") != NULL));
1049 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1051 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1053 return info->exec &&
1054 ((strstr (info->exec, "%f") != NULL) ||
1055 (strstr (info->exec, "%F") != NULL));
1059 g_desktop_app_info_launch (GAppInfo *appinfo,
1061 GAppLaunchContext *launch_context,
1071 uri = g_file_get_uri (files->data);
1072 uris = g_list_prepend (uris, uri);
1073 files = files->next;
1076 uris = g_list_reverse (uris);
1078 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1080 g_list_foreach (uris, (GFunc)g_free, NULL);
1086 G_LOCK_DEFINE_STATIC (g_desktop_env);
1087 static gchar *g_desktop_env = NULL;
1090 * g_desktop_app_info_set_desktop_env:
1091 * @desktop_env: a string specifying what desktop this is
1093 * Sets the name of the desktop that the application is running in.
1094 * This is used by g_app_info_should_show() to evaluate the
1095 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1096 * desktop entry fields.
1098 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1099 * Menu specification</ulink> recognizes the following:
1101 * <member>GNOME</member>
1102 * <member>KDE</member>
1103 * <member>ROX</member>
1104 * <member>XFCE</member>
1105 * <member>Old</member>
1108 * Should be called only once; subsequent calls are ignored.
1111 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1113 G_LOCK (g_desktop_env);
1115 g_desktop_env = g_strdup (desktop_env);
1116 G_UNLOCK (g_desktop_env);
1120 g_desktop_app_info_should_show (GAppInfo *appinfo)
1122 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1124 const gchar *desktop_env;
1127 if (info->nodisplay)
1130 G_LOCK (g_desktop_env);
1131 desktop_env = g_desktop_env;
1132 G_UNLOCK (g_desktop_env);
1134 if (info->only_show_in)
1136 if (desktop_env == NULL)
1140 for (i = 0; info->only_show_in[i] != NULL; i++)
1142 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1152 if (info->not_show_in && desktop_env)
1154 for (i = 0; info->not_show_in[i] != NULL; i++)
1156 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1170 ensure_dir (DirType type,
1173 char *path, *display_name;
1176 if (type == APP_DIR)
1177 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1179 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1182 if (g_mkdir_with_parents (path, 0700) == 0)
1186 display_name = g_filename_display_name (path);
1187 if (type == APP_DIR)
1188 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1189 _("Can't create user application configuration folder %s: %s"),
1190 display_name, g_strerror (errsv));
1192 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1193 _("Can't create user MIME configuration folder %s: %s"),
1194 display_name, g_strerror (errsv));
1196 g_free (display_name);
1203 update_mimeapps_list (const char *desktop_id,
1204 const char *content_type,
1205 gboolean add_at_start,
1206 gboolean add_at_end,
1210 char *dirname, *filename;
1212 gboolean load_succeeded, res;
1215 gsize length, data_size;
1218 char **content_types;
1220 /* Don't add both at start and end */
1221 g_assert (!(add_at_start && add_at_end));
1223 dirname = ensure_dir (APP_DIR, error);
1227 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1230 key_file = g_key_file_new ();
1231 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1232 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1234 g_key_file_free (key_file);
1235 key_file = g_key_file_new ();
1240 content_types = g_new (char *, 2);
1241 content_types[0] = g_strdup (content_type);
1242 content_types[1] = NULL;
1246 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1249 for (k = 0; content_types && content_types[k]; k++)
1251 /* Add to the right place in the list */
1254 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1255 content_types[k], &length, NULL);
1257 list = g_new (char *, 1 + length + 1);
1261 list[i++] = g_strdup (desktop_id);
1264 for (j = 0; old_list[j] != NULL; j++)
1266 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1267 list[i++] = g_strdup (old_list[j]);
1271 list[i++] = g_strdup (desktop_id);
1274 g_strfreev (old_list);
1276 if (list[0] == NULL || desktop_id == NULL)
1277 g_key_file_remove_key (key_file,
1278 ADDED_ASSOCIATIONS_GROUP,
1282 g_key_file_set_string_list (key_file,
1283 ADDED_ASSOCIATIONS_GROUP,
1285 (const char * const *)list, i);
1292 /* reuse the list from above */
1296 g_strfreev (content_types);
1297 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1300 for (k = 0; content_types && content_types[k]; k++)
1302 /* Remove from removed associations group (unless remove) */
1305 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1306 content_types[k], &length, NULL);
1308 list = g_new (char *, 1 + length + 1);
1312 list[i++] = g_strdup (desktop_id);
1315 for (j = 0; old_list[j] != NULL; j++)
1317 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1318 list[i++] = g_strdup (old_list[j]);
1323 g_strfreev (old_list);
1325 if (list[0] == NULL || desktop_id == NULL)
1326 g_key_file_remove_key (key_file,
1327 REMOVED_ASSOCIATIONS_GROUP,
1331 g_key_file_set_string_list (key_file,
1332 REMOVED_ASSOCIATIONS_GROUP,
1334 (const char * const *)list, i);
1339 g_strfreev (content_types);
1341 data = g_key_file_to_data (key_file, &data_size, error);
1342 g_key_file_free (key_file);
1344 res = g_file_set_contents (filename, data, data_size, error);
1346 mime_info_cache_reload (NULL);
1355 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1356 const char *content_type,
1359 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1361 if (!g_desktop_app_info_ensure_saved (info, error))
1364 return update_mimeapps_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1368 update_program_done (GPid pid,
1372 /* Did the application exit correctly */
1373 if (WIFEXITED (status) &&
1374 WEXITSTATUS (status) == 0)
1376 /* Here we could clean out any caches in use */
1381 run_update_command (char *command,
1390 GError *error = NULL;
1393 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1395 if (g_spawn_async ("/", argv,
1397 G_SPAWN_SEARCH_PATH |
1398 G_SPAWN_STDOUT_TO_DEV_NULL |
1399 G_SPAWN_STDERR_TO_DEV_NULL |
1400 G_SPAWN_DO_NOT_REAP_CHILD,
1401 NULL, NULL, /* No setup function */
1404 g_child_watch_add (pid, update_program_done, NULL);
1407 /* If we get an error at this point, it's quite likely the user doesn't
1408 * have an installed copy of either 'update-mime-database' or
1409 * 'update-desktop-database'. I don't think we want to popup an error
1410 * dialog at this point, so we just do a g_warning to give the user a
1411 * chance of debugging it.
1413 g_warning ("%s", error->message);
1420 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1421 const char *extension,
1424 char *filename, *basename, *mimetype;
1428 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1431 dirname = ensure_dir (MIMETYPE_DIR, error);
1435 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1436 filename = g_build_filename (dirname, basename, NULL);
1440 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1442 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1447 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1448 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1449 " <mime-type type=\"%s\">\n"
1450 " <comment>%s document</comment>\n"
1451 " <glob pattern=\"*.%s\"/>\n"
1453 "</mime-info>\n", mimetype, extension, extension);
1455 g_file_set_contents (filename, contents, -1, NULL);
1458 run_update_command ("update-mime-database", "mime");
1462 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1472 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1473 const char *content_type,
1476 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1478 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1481 return update_mimeapps_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1485 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1491 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1492 const char *content_type,
1495 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1497 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1500 return update_mimeapps_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1504 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1510 char *data, *desktop_id;
1515 if (info->filename != NULL)
1518 /* This is only used for object created with
1519 * g_app_info_create_from_commandline. All other
1520 * object should have a filename
1523 dirname = ensure_dir (APP_DIR, error);
1527 key_file = g_key_file_new ();
1529 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1530 "Encoding", "UTF-8");
1531 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1532 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1533 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1534 G_KEY_FILE_DESKTOP_KEY_TYPE,
1535 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1537 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1538 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1540 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1541 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1543 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1544 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1546 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1547 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1549 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1550 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1552 data = g_key_file_to_data (key_file, &data_size, NULL);
1553 g_key_file_free (key_file);
1555 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1556 filename = g_build_filename (dirname, desktop_id, NULL);
1557 g_free (desktop_id);
1560 fd = g_mkstemp (filename);
1565 display_name = g_filename_display_name (filename);
1566 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1567 _("Can't create user desktop file %s"), display_name);
1568 g_free (display_name);
1574 desktop_id = g_path_get_basename (filename);
1578 res = g_file_set_contents (filename, data, data_size, error);
1581 g_free (desktop_id);
1586 info->filename = filename;
1587 info->desktop_id = desktop_id;
1589 run_update_command ("update-desktop-database", "applications");
1595 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1597 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1600 return g_access (info->filename, W_OK) == 0;
1606 g_desktop_app_info_delete (GAppInfo *appinfo)
1608 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1612 if (g_remove (info->filename) == 0)
1614 update_mimeapps_list (info->desktop_id, NULL, FALSE, FALSE, FALSE, NULL);
1616 g_free (info->filename);
1617 info->filename = NULL;
1618 g_free (info->desktop_id);
1619 info->desktop_id = NULL;
1629 * g_app_info_create_from_commandline:
1630 * @commandline: the commandline to use
1631 * @application_name: the application name, or %NULL to use @commandline
1632 * @flags: flags that can specify details of the created #GAppInfo
1633 * @error: a #GError location to store the error occuring, %NULL to ignore.
1635 * Creates a new #GAppInfo from the given information.
1637 * Returns: new #GAppInfo for given command.
1640 g_app_info_create_from_commandline (const char *commandline,
1641 const char *application_name,
1642 GAppInfoCreateFlags flags,
1647 GDesktopAppInfo *info;
1649 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1651 info->filename = NULL;
1652 info->desktop_id = NULL;
1654 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1655 info->startup_notify = FALSE;
1656 info->hidden = FALSE;
1657 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1658 info->exec = g_strconcat (commandline, " %u", NULL);
1660 info->exec = g_strconcat (commandline, " %f", NULL);
1661 info->nodisplay = TRUE;
1662 info->binary = binary_from_exec (info->exec);
1664 if (application_name)
1665 info->name = g_strdup (application_name);
1668 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1669 split = g_strsplit (commandline, " ", 2);
1670 basename = g_path_get_basename (split[0]);
1672 info->name = basename;
1673 if (info->name == NULL)
1674 info->name = g_strdup ("custom");
1676 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1678 return G_APP_INFO (info);
1682 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1684 iface->dup = g_desktop_app_info_dup;
1685 iface->equal = g_desktop_app_info_equal;
1686 iface->get_id = g_desktop_app_info_get_id;
1687 iface->get_name = g_desktop_app_info_get_name;
1688 iface->get_description = g_desktop_app_info_get_description;
1689 iface->get_executable = g_desktop_app_info_get_executable;
1690 iface->get_icon = g_desktop_app_info_get_icon;
1691 iface->launch = g_desktop_app_info_launch;
1692 iface->supports_uris = g_desktop_app_info_supports_uris;
1693 iface->supports_files = g_desktop_app_info_supports_files;
1694 iface->launch_uris = g_desktop_app_info_launch_uris;
1695 iface->should_show = g_desktop_app_info_should_show;
1696 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1697 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1698 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1699 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1700 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1701 iface->can_delete = g_desktop_app_info_can_delete;
1702 iface->do_delete = g_desktop_app_info_delete;
1706 app_info_in_list (GAppInfo *info,
1709 while (list != NULL)
1711 if (g_app_info_equal (info, list->data))
1720 * g_app_info_get_all_for_type:
1721 * @content_type: the content type to find a #GAppInfo for
1723 * Gets a list of all #GAppInfo s for a given content type.
1725 * Returns: #GList of #GAppInfo s for given @content_type
1726 * or %NULL on error.
1729 g_app_info_get_all_for_type (const char *content_type)
1731 GList *desktop_entries, *l;
1733 GDesktopAppInfo *info;
1735 g_return_val_if_fail (content_type != NULL, NULL);
1737 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1740 for (l = desktop_entries; l != NULL; l = l->next)
1742 char *desktop_entry = l->data;
1744 info = g_desktop_app_info_new (desktop_entry);
1747 if (app_info_in_list (G_APP_INFO (info), infos))
1748 g_object_unref (info);
1750 infos = g_list_prepend (infos, info);
1752 g_free (desktop_entry);
1755 g_list_free (desktop_entries);
1757 return g_list_reverse (infos);
1761 * g_app_info_reset_type_associations:
1762 * content_type: a content type
1764 * Removes all changes to the type associations done by
1765 * g_app_info_set_as_default_for_type(),
1766 * g_app_info_set_as_default_for_extension(),
1767 * g_app_info_add_supports_type() of g_app_info_remove_supports_type().
1772 g_app_info_reset_type_associations (const char *content_type)
1774 update_mimeapps_list (NULL, content_type, FALSE, FALSE, FALSE, NULL);
1778 * g_app_info_get_default_for_type:
1779 * @content_type: the content type to find a #GAppInfo for
1780 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1783 * Gets the #GAppInfo that correspond to a given content type.
1785 * Returns: #GAppInfo for given @content_type or %NULL on error.
1788 g_app_info_get_default_for_type (const char *content_type,
1789 gboolean must_support_uris)
1791 GList *desktop_entries, *l;
1794 g_return_val_if_fail (content_type != NULL, NULL);
1796 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1799 for (l = desktop_entries; l != NULL; l = l->next)
1801 char *desktop_entry = l->data;
1803 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1806 if (must_support_uris && !g_app_info_supports_uris (info))
1808 g_object_unref (info);
1816 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1817 g_list_free (desktop_entries);
1823 * g_app_info_get_default_for_uri_scheme:
1824 * @uri_scheme: a string containing a URI scheme.
1826 * Gets the default application for launching applications
1827 * using this URI scheme. A URI scheme is the initial part
1828 * of the URI, up to but not including the ':', e.g. "http",
1831 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
1834 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1836 static gsize lookup = 0;
1838 if (g_once_init_enter (&lookup))
1840 gsize setup_value = 1;
1841 GDesktopAppInfoLookup *lookup_instance;
1842 const char *use_this;
1843 GIOExtensionPoint *ep;
1844 GIOExtension *extension;
1847 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1849 /* Ensure vfs in modules loaded */
1850 _g_io_modules_ensure_loaded ();
1852 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1854 lookup_instance = NULL;
1857 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1859 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1862 if (lookup_instance == NULL)
1864 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1866 extension = l->data;
1867 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1868 if (lookup_instance != NULL)
1873 if (lookup_instance != NULL)
1874 setup_value = (gsize)lookup_instance;
1876 g_once_init_leave (&lookup, setup_value);
1882 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1888 get_apps_from_dir (GHashTable *apps,
1889 const char *dirname,
1893 const char *basename;
1894 char *filename, *subprefix, *desktop_id;
1896 GDesktopAppInfo *appinfo;
1898 dir = g_dir_open (dirname, 0, NULL);
1901 while ((basename = g_dir_read_name (dir)) != NULL)
1903 filename = g_build_filename (dirname, basename, NULL);
1904 if (g_str_has_suffix (basename, ".desktop"))
1906 desktop_id = g_strconcat (prefix, basename, NULL);
1908 /* Use _extended so we catch NULLs too (hidden) */
1909 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1911 appinfo = g_desktop_app_info_new_from_filename (filename);
1913 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1915 g_object_unref (appinfo);
1920 if (appinfo || hidden)
1922 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1926 /* Reuse instead of strdup here */
1927 appinfo->desktop_id = desktop_id;
1932 g_free (desktop_id);
1936 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1938 subprefix = g_strconcat (prefix, basename, "-", NULL);
1939 get_apps_from_dir (apps, filename, subprefix);
1951 * g_app_info_get_all:
1953 * Gets a list of all of the applications currently registered
1956 * For desktop files, this includes applications that have
1957 * <literal>NoDisplay=true</literal> set or are excluded from
1958 * display by means of <literal>OnlyShowIn</literal> or
1959 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1960 * The returned list does not include applications which have
1961 * the <literal>Hidden</literal> key set.
1963 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1966 g_app_info_get_all (void)
1968 const char * const *dirs;
1970 GHashTableIter iter;
1975 dirs = get_applications_search_path ();
1977 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1981 for (i = 0; dirs[i] != NULL; i++)
1982 get_apps_from_dir (apps, dirs[i], "");
1986 g_hash_table_iter_init (&iter, apps);
1987 while (g_hash_table_iter_next (&iter, NULL, &value))
1990 infos = g_list_prepend (infos, value);
1993 g_hash_table_destroy (apps);
1995 return g_list_reverse (infos);
1998 /* Cacheing of mimeinfo.cache and defaults.list files */
2002 GHashTable *mime_info_cache_map;
2003 GHashTable *defaults_list_map;
2004 GHashTable *mimeapps_list_added_map;
2005 GHashTable *mimeapps_list_removed_map;
2006 time_t mime_info_cache_timestamp;
2007 time_t defaults_list_timestamp;
2008 time_t mimeapps_list_timestamp;
2012 GList *dirs; /* mimeinfo.cache and defaults.list */
2013 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2014 time_t last_stat_time;
2015 guint should_ping_mime_monitor : 1;
2018 static MimeInfoCache *mime_info_cache = NULL;
2019 G_LOCK_DEFINE_STATIC (mime_info_cache);
2021 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2022 const char *mime_type,
2023 char **new_desktop_file_ids);
2025 static MimeInfoCache * mime_info_cache_new (void);
2028 destroy_info_cache_value (gpointer key,
2032 g_list_foreach (value, (GFunc)g_free, NULL);
2033 g_list_free (value);
2037 destroy_info_cache_map (GHashTable *info_cache_map)
2039 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2040 g_hash_table_destroy (info_cache_map);
2044 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2045 const char *cache_file,
2051 filename = g_build_filename (dir->path, cache_file, NULL);
2053 if (g_stat (filename, &buf) < 0)
2060 if (buf.st_mtime != *timestamp)
2066 /* Call with lock held */
2068 remove_all (gpointer key,
2077 mime_info_cache_blow_global_cache (void)
2079 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2084 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2088 gchar *filename, **mime_types;
2095 if (dir->mime_info_cache_map != NULL &&
2096 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2097 &dir->mime_info_cache_timestamp))
2100 if (dir->mime_info_cache_map != NULL)
2101 destroy_info_cache_map (dir->mime_info_cache_map);
2103 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2104 (GDestroyNotify) g_free,
2107 key_file = g_key_file_new ();
2109 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2111 if (g_stat (filename, &buf) < 0)
2114 if (dir->mime_info_cache_timestamp > 0)
2115 mime_info_cache->should_ping_mime_monitor = TRUE;
2117 dir->mime_info_cache_timestamp = buf.st_mtime;
2119 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2124 if (load_error != NULL)
2127 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2130 if (load_error != NULL)
2133 for (i = 0; mime_types[i] != NULL; i++)
2135 gchar **desktop_file_ids;
2136 char *unaliased_type;
2137 desktop_file_ids = g_key_file_get_string_list (key_file,
2143 if (desktop_file_ids == NULL)
2146 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2147 mime_info_cache_dir_add_desktop_entries (dir,
2150 g_free (unaliased_type);
2152 g_strfreev (desktop_file_ids);
2155 g_strfreev (mime_types);
2156 g_key_file_free (key_file);
2161 g_key_file_free (key_file);
2163 if (mime_types != NULL)
2164 g_strfreev (mime_types);
2167 g_error_free (load_error);
2171 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2175 gchar *filename, **mime_types;
2176 char *unaliased_type;
2177 char **desktop_file_ids;
2184 if (dir->defaults_list_map != NULL &&
2185 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2186 &dir->defaults_list_timestamp))
2189 if (dir->defaults_list_map != NULL)
2190 g_hash_table_destroy (dir->defaults_list_map);
2191 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2192 g_free, (GDestroyNotify)g_strfreev);
2195 key_file = g_key_file_new ();
2197 filename = g_build_filename (dir->path, "defaults.list", NULL);
2198 if (g_stat (filename, &buf) < 0)
2201 if (dir->defaults_list_timestamp > 0)
2202 mime_info_cache->should_ping_mime_monitor = TRUE;
2204 dir->defaults_list_timestamp = buf.st_mtime;
2206 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2210 if (load_error != NULL)
2213 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2215 if (mime_types != NULL)
2217 for (i = 0; mime_types[i] != NULL; i++)
2219 desktop_file_ids = g_key_file_get_string_list (key_file,
2220 DEFAULT_APPLICATIONS_GROUP,
2224 if (desktop_file_ids == NULL)
2227 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2228 g_hash_table_replace (dir->defaults_list_map,
2233 g_strfreev (mime_types);
2236 g_key_file_free (key_file);
2241 g_key_file_free (key_file);
2243 if (mime_types != NULL)
2244 g_strfreev (mime_types);
2247 g_error_free (load_error);
2251 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2255 gchar *filename, **mime_types;
2256 char *unaliased_type;
2257 char **desktop_file_ids;
2264 if (dir->mimeapps_list_added_map != NULL &&
2265 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2266 &dir->mimeapps_list_timestamp))
2269 if (dir->mimeapps_list_added_map != NULL)
2270 g_hash_table_destroy (dir->mimeapps_list_added_map);
2271 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2272 g_free, (GDestroyNotify)g_strfreev);
2274 if (dir->mimeapps_list_removed_map != NULL)
2275 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2276 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2277 g_free, (GDestroyNotify)g_strfreev);
2279 key_file = g_key_file_new ();
2281 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2282 if (g_stat (filename, &buf) < 0)
2285 if (dir->mimeapps_list_timestamp > 0)
2286 mime_info_cache->should_ping_mime_monitor = TRUE;
2288 dir->mimeapps_list_timestamp = buf.st_mtime;
2290 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2294 if (load_error != NULL)
2297 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2299 if (mime_types != NULL)
2301 for (i = 0; mime_types[i] != NULL; i++)
2303 desktop_file_ids = g_key_file_get_string_list (key_file,
2304 ADDED_ASSOCIATIONS_GROUP,
2308 if (desktop_file_ids == NULL)
2311 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2312 g_hash_table_replace (dir->mimeapps_list_added_map,
2317 g_strfreev (mime_types);
2320 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2322 if (mime_types != NULL)
2324 for (i = 0; mime_types[i] != NULL; i++)
2326 desktop_file_ids = g_key_file_get_string_list (key_file,
2327 REMOVED_ASSOCIATIONS_GROUP,
2331 if (desktop_file_ids == NULL)
2334 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2335 g_hash_table_replace (dir->mimeapps_list_removed_map,
2340 g_strfreev (mime_types);
2343 g_key_file_free (key_file);
2348 g_key_file_free (key_file);
2350 if (mime_types != NULL)
2351 g_strfreev (mime_types);
2354 g_error_free (load_error);
2357 static MimeInfoCacheDir *
2358 mime_info_cache_dir_new (const char *path)
2360 MimeInfoCacheDir *dir;
2362 dir = g_new0 (MimeInfoCacheDir, 1);
2363 dir->path = g_strdup (path);
2369 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2374 if (dir->mime_info_cache_map != NULL)
2376 destroy_info_cache_map (dir->mime_info_cache_map);
2377 dir->mime_info_cache_map = NULL;
2381 if (dir->defaults_list_map != NULL)
2383 g_hash_table_destroy (dir->defaults_list_map);
2384 dir->defaults_list_map = NULL;
2387 if (dir->mimeapps_list_added_map != NULL)
2389 g_hash_table_destroy (dir->mimeapps_list_added_map);
2390 dir->mimeapps_list_added_map = NULL;
2393 if (dir->mimeapps_list_removed_map != NULL)
2395 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2396 dir->mimeapps_list_removed_map = NULL;
2403 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2404 const char *mime_type,
2405 char **new_desktop_file_ids)
2407 GList *desktop_file_ids;
2410 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2413 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2415 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2416 desktop_file_ids = g_list_append (desktop_file_ids,
2417 g_strdup (new_desktop_file_ids[i]));
2420 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2424 mime_info_cache_init_dir_lists (void)
2426 const char * const *dirs;
2429 mime_info_cache = mime_info_cache_new ();
2431 dirs = get_applications_search_path ();
2433 for (i = 0; dirs[i] != NULL; i++)
2435 MimeInfoCacheDir *dir;
2437 dir = mime_info_cache_dir_new (dirs[i]);
2441 mime_info_cache_dir_init (dir);
2442 mime_info_cache_dir_init_defaults_list (dir);
2443 mime_info_cache_dir_init_mimeapps_list (dir);
2445 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2451 mime_info_cache_update_dir_lists (void)
2455 tmp = mime_info_cache->dirs;
2459 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2461 /* No need to do this if we had file monitors... */
2462 mime_info_cache_blow_global_cache ();
2463 mime_info_cache_dir_init (dir);
2464 mime_info_cache_dir_init_defaults_list (dir);
2465 mime_info_cache_dir_init_mimeapps_list (dir);
2472 mime_info_cache_init (void)
2474 G_LOCK (mime_info_cache);
2475 if (mime_info_cache == NULL)
2476 mime_info_cache_init_dir_lists ();
2482 if (now >= mime_info_cache->last_stat_time + 10)
2484 mime_info_cache_update_dir_lists ();
2485 mime_info_cache->last_stat_time = now;
2489 if (mime_info_cache->should_ping_mime_monitor)
2491 /* g_idle_add (emit_mime_changed, NULL); */
2492 mime_info_cache->should_ping_mime_monitor = FALSE;
2495 G_UNLOCK (mime_info_cache);
2498 static MimeInfoCache *
2499 mime_info_cache_new (void)
2501 MimeInfoCache *cache;
2503 cache = g_new0 (MimeInfoCache, 1);
2505 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2506 (GDestroyNotify) g_free,
2507 (GDestroyNotify) g_free);
2512 mime_info_cache_free (MimeInfoCache *cache)
2517 g_list_foreach (cache->dirs,
2518 (GFunc) mime_info_cache_dir_free,
2520 g_list_free (cache->dirs);
2521 g_hash_table_destroy (cache->global_defaults_cache);
2526 * mime_info_cache_reload:
2527 * @dir: directory path which needs reloading.
2529 * Reload the mime information for the @dir.
2532 mime_info_cache_reload (const char *dir)
2534 /* FIXME: just reload the dir that needs reloading,
2535 * don't blow the whole cache
2537 if (mime_info_cache != NULL)
2539 G_LOCK (mime_info_cache);
2540 mime_info_cache_free (mime_info_cache);
2541 mime_info_cache = NULL;
2542 G_UNLOCK (mime_info_cache);
2547 append_desktop_entry (GList *list,
2548 const char *desktop_entry,
2549 GList *removed_entries)
2551 /* Add if not already in list, and valid */
2552 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2553 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2554 list = g_list_prepend (list, g_strdup (desktop_entry));
2560 * get_all_desktop_entries_for_mime_type:
2561 * @mime_type: a mime type.
2563 * Returns all the desktop ids for @mime_type. The desktop files
2564 * are listed in an order so that default applications are listed before
2565 * non-default ones, and handlers for inherited mimetypes are listed
2566 * after the base ones.
2568 * Return value: a #GList containing the desktop ids which claim
2569 * to handle @mime_type.
2572 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2574 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2575 MimeInfoCacheDir *dir;
2578 char **default_entries;
2579 char **removed_associations;
2584 mime_info_cache_init ();
2586 /* collect all ancestors */
2587 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2588 array = g_ptr_array_new ();
2589 for (i = 0; mime_types[i]; i++)
2590 g_ptr_array_add (array, mime_types[i]);
2591 g_free (mime_types);
2592 for (i = 0; i < array->len; i++)
2594 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2595 for (j = 0; anc[j]; j++)
2597 for (k = 0; k < array->len; k++)
2599 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2602 if (k == array->len) /* not found */
2603 g_ptr_array_add (array, g_strdup (anc[j]));
2607 g_ptr_array_add (array, NULL);
2608 mime_types = (char **)g_ptr_array_free (array, FALSE);
2610 G_LOCK (mime_info_cache);
2612 removed_entries = NULL;
2613 desktop_entries = NULL;
2614 for (i = 0; mime_types[i] != NULL; i++)
2616 mime_type = mime_types[i];
2618 /* Go through all apps listed as defaults */
2619 for (dir_list = mime_info_cache->dirs;
2621 dir_list = dir_list->next)
2623 dir = dir_list->data;
2625 /* First added associations from mimeapps.list */
2626 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2627 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2628 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2630 /* Then removed associations from mimeapps.list */
2631 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2632 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2633 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2635 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2636 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2637 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2638 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2641 /* Go through all entries that support the mimetype */
2642 for (dir_list = mime_info_cache->dirs;
2644 dir_list = dir_list->next)
2646 dir = dir_list->data;
2648 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2649 for (tmp = list; tmp != NULL; tmp = tmp->next)
2650 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2654 G_UNLOCK (mime_info_cache);
2656 g_strfreev (mime_types);
2658 g_list_free (removed_entries);
2660 desktop_entries = g_list_reverse (desktop_entries);
2662 return desktop_entries;
2665 /* GDesktopAppInfoLookup interface: */
2667 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2668 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2669 gpointer class_data);
2672 g_desktop_app_info_lookup_get_type (void)
2674 static volatile gsize g_define_type_id__volatile = 0;
2676 if (g_once_init_enter (&g_define_type_id__volatile))
2678 const GTypeInfo desktop_app_info_lookup_info =
2680 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2681 g_desktop_app_info_lookup_base_init, /* base_init */
2682 NULL, /* base_finalize */
2683 g_desktop_app_info_lookup_class_init,
2684 NULL, /* class_finalize */
2685 NULL, /* class_data */
2687 0, /* n_preallocs */
2690 GType g_define_type_id =
2691 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2692 &desktop_app_info_lookup_info, 0);
2694 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
2696 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
2699 return g_define_type_id__volatile;
2703 g_desktop_app_info_lookup_class_init (gpointer g_class,
2704 gpointer class_data)
2709 g_desktop_app_info_lookup_base_init (gpointer g_class)
2714 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2715 * @lookup: a #GDesktopAppInfoLookup
2716 * @uri_scheme: a string containing a URI scheme.
2718 * Gets the default application for launching applications
2719 * using this URI scheme for a particular GDesktopAppInfoLookup
2722 * The GDesktopAppInfoLookup interface and this function is used
2723 * to implement g_app_info_get_default_for_uri_scheme() backends
2724 * in a GIO module. There is no reason for applications to use it
2725 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2727 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2730 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2731 const char *uri_scheme)
2733 GDesktopAppInfoLookupIface *iface;
2735 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2737 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2739 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2742 #define __G_DESKTOP_APP_INFO_C__
2743 #include "gioaliasdef.c"