1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright © 2007 Ryan Lortie
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
38 #include "gthemedicon.h"
39 #include "gfileicon.h"
40 #include <glib/gstdio.h>
46 * SECTION:gdesktopappinfo
47 * @short_description: Application information from desktop files
48 * @include: gio/gdesktopappinfo.h
50 * #GDesktopAppInfo is an implementation of #GAppInfo based on
55 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
56 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
57 #define MIME_CACHE_GROUP "MIME Cache"
59 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
60 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type);
61 static void mime_info_cache_reload (const char *dir);
62 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
68 * Information about an installed application from a desktop file.
70 struct _GDesktopAppInfo
72 GObject parent_instance;
78 /* FIXME: what about GenericName ? */
92 guint startup_notify : 1;
93 /* FIXME: what about StartupWMClass ? */
96 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
97 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
98 g_desktop_app_info_iface_init))
101 search_path_init (gpointer data)
104 const char * const *data_dirs;
105 const char *user_data_dir;
108 data_dirs = g_get_system_data_dirs ();
109 length = g_strv_length ((char **) data_dirs);
111 args = g_new (char *, length + 2);
114 user_data_dir = g_get_user_data_dir ();
115 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
116 for (i = 0; i < length; i++)
117 args[j++] = g_build_filename (data_dirs[i],
118 "applications", NULL);
124 static const char * const *
125 get_applications_search_path (void)
127 static GOnce once_init = G_ONCE_INIT;
128 return g_once (&once_init, search_path_init, NULL);
132 g_desktop_app_info_finalize (GObject *object)
134 GDesktopAppInfo *info;
136 info = G_DESKTOP_APP_INFO (object);
138 g_free (info->desktop_id);
139 g_free (info->filename);
141 g_free (info->comment);
142 g_free (info->icon_name);
144 g_object_unref (info->icon);
145 g_strfreev (info->only_show_in);
146 g_strfreev (info->not_show_in);
147 g_free (info->try_exec);
149 g_free (info->binary);
152 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
156 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
158 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
160 gobject_class->finalize = g_desktop_app_info_finalize;
164 g_desktop_app_info_init (GDesktopAppInfo *local)
169 binary_from_exec (const char *exec)
177 while (*p != ' ' && *p != 0)
180 return g_strndup (start, p - start);
185 * g_desktop_app_info_new_from_filename:
186 * @filename: a string containing a file name.
188 * Creates a new #GDesktopAppInfo.
190 * Returns: a new #GDesktopAppInfo or %NULL on error.
193 g_desktop_app_info_new_from_filename (const char *filename)
195 GDesktopAppInfo *info;
201 key_file = g_key_file_new ();
203 if (!g_key_file_load_from_file (key_file,
208 g_key_file_free (key_file);
212 start_group = g_key_file_get_start_group (key_file);
213 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
215 g_free (start_group);
216 g_key_file_free (key_file);
219 g_free (start_group);
221 type = g_key_file_get_string (key_file,
222 G_KEY_FILE_DESKTOP_GROUP,
223 G_KEY_FILE_DESKTOP_KEY_TYPE,
225 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
228 g_key_file_free (key_file);
233 try_exec = g_key_file_get_string (key_file,
234 G_KEY_FILE_DESKTOP_GROUP,
235 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
240 t = g_find_program_in_path (try_exec);
244 g_key_file_free (key_file);
250 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
251 info->filename = g_strdup (filename);
253 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
254 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
255 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
256 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
257 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);
258 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);
259 info->try_exec = try_exec;
260 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
261 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
262 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
263 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
264 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
266 g_key_file_free (key_file);
271 if (g_path_is_absolute (info->icon_name))
275 file = g_file_new_for_path (info->icon_name);
276 info->icon = g_file_icon_new (file);
277 g_object_unref (file);
280 info->icon = g_themed_icon_new (info->icon_name);
284 info->binary = binary_from_exec (info->exec);
290 * g_desktop_app_info_new:
291 * @desktop_id: the desktop file id
293 * Creates a new #GDesktopAppInfo.
295 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
298 g_desktop_app_info_new (const char *desktop_id)
300 GDesktopAppInfo *appinfo;
301 const char * const *dirs;
305 dirs = get_applications_search_path ();
307 basename = g_strdup (desktop_id);
309 for (i = 0; dirs[i] != NULL; i++)
314 filename = g_build_filename (dirs[i], desktop_id, NULL);
315 appinfo = g_desktop_app_info_new_from_filename (filename);
321 while ((p = strchr (p, '-')) != NULL)
325 filename = g_build_filename (dirs[i], basename, NULL);
326 appinfo = g_desktop_app_info_new_from_filename (filename);
341 appinfo->desktop_id = g_strdup (desktop_id);
343 if (g_desktop_app_info_get_is_hidden (appinfo))
345 g_object_unref (appinfo);
353 g_desktop_app_info_dup (GAppInfo *appinfo)
355 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
356 GDesktopAppInfo *new_info;
358 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
360 new_info->filename = g_strdup (info->filename);
361 new_info->desktop_id = g_strdup (info->desktop_id);
363 new_info->name = g_strdup (info->name);
364 new_info->comment = g_strdup (info->comment);
365 new_info->nodisplay = info->nodisplay;
366 new_info->icon_name = g_strdup (info->icon_name);
367 new_info->icon = g_object_ref (info->icon);
368 new_info->only_show_in = g_strdupv (info->only_show_in);
369 new_info->not_show_in = g_strdupv (info->not_show_in);
370 new_info->try_exec = g_strdup (info->try_exec);
371 new_info->exec = g_strdup (info->exec);
372 new_info->binary = g_strdup (info->binary);
373 new_info->path = g_strdup (info->path);
374 new_info->hidden = info->hidden;
375 new_info->terminal = info->terminal;
376 new_info->startup_notify = info->startup_notify;
378 return G_APP_INFO (new_info);
382 g_desktop_app_info_equal (GAppInfo *appinfo1,
385 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
386 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
388 if (info1->desktop_id == NULL ||
389 info2->desktop_id == NULL)
392 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
396 g_desktop_app_info_get_id (GAppInfo *appinfo)
398 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
400 return info->desktop_id;
404 g_desktop_app_info_get_name (GAppInfo *appinfo)
406 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
408 if (info->name == NULL)
414 * g_desktop_app_info_get_is_hidden:
415 * @info: a #GDesktopAppInfo.
417 * A desktop file is hidden if the Hidden key in it is
420 * Returns: %TRUE if hidden, %FALSE otherwise.
423 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
429 g_desktop_app_info_get_description (GAppInfo *appinfo)
431 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
433 return info->comment;
437 g_desktop_app_info_get_executable (GAppInfo *appinfo)
439 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
445 g_desktop_app_info_get_icon (GAppInfo *appinfo)
447 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
453 expand_macro_single (char macro, GFile *file)
458 path = g_file_get_path (file);
459 uri = g_file_get_uri (file);
465 result = g_shell_quote (uri);
470 result = g_shell_quote (path);
475 result = g_shell_quote (g_path_get_dirname (path));
480 result = g_shell_quote (g_path_get_basename (path));
491 expand_macro (char macro,
493 GDesktopAppInfo *info,
496 GList *files = *file_list;
499 g_return_if_fail (exec != NULL);
509 expanded = expand_macro_single (macro, files->data);
512 g_string_append (exec, expanded);
526 expanded = expand_macro_single (macro, files->data);
529 g_string_append (exec, expanded);
535 if (files != NULL && expanded)
536 g_string_append_c (exec, ' ');
544 g_string_append (exec, "--icon ");
545 g_string_append (exec, info->icon_name);
551 g_string_append (exec, info->name);
556 g_string_append (exec, info->filename);
559 case 'm': /* deprecated */
563 g_string_append_c (exec, '%');
571 expand_application_parameters (GDesktopAppInfo *info,
577 GList *file_list = *files;
578 const char *p = info->exec;
579 GString *expanded_exec = g_string_new (NULL);
582 if (info->exec == NULL)
584 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
585 _("Desktop file didn't specify Exec field"));
591 if (p[0] == '%' && p[1] != '\0')
593 expand_macro (p[1], expanded_exec, info, files);
597 g_string_append_c (expanded_exec, *p);
602 /* No file substitutions */
603 if (file_list == *files && file_list != NULL)
605 /* If there is no macro default to %f. This is also what KDE does */
606 g_string_append_c (expanded_exec, ' ');
607 expand_macro ('f', expanded_exec, info, files);
610 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
611 g_string_free (expanded_exec, TRUE);
616 prepend_terminal_to_vector (int *argc,
623 char **term_argv = NULL;
628 g_return_val_if_fail (argc != NULL, FALSE);
629 g_return_val_if_fail (argv != NULL, FALSE);
637 /* compute size if not given */
640 for (i = 0; the_argv[i] != NULL; i++)
646 term_argv = g_new0 (char *, 3);
648 check = g_find_program_in_path ("gnome-terminal");
651 term_argv[0] = check;
652 /* Note that gnome-terminal takes -x and
653 * as -e in gnome-terminal is broken we use that. */
654 term_argv[1] = g_strdup ("-x");
659 check = g_find_program_in_path ("nxterm");
661 check = g_find_program_in_path ("color-xterm");
663 check = g_find_program_in_path ("rxvt");
665 check = g_find_program_in_path ("xterm");
667 check = g_find_program_in_path ("dtterm");
670 check = g_strdup ("xterm");
671 g_warning ("couldn't find a terminal, falling back to xterm");
673 term_argv[0] = check;
674 term_argv[1] = g_strdup ("-e");
677 real_argc = term_argc + *argc;
678 real_argv = g_new (char *, real_argc + 1);
680 for (i = 0; i < term_argc; i++)
681 real_argv[i] = term_argv[i];
683 for (j = 0; j < *argc; j++, i++)
684 real_argv[i] = (char *)the_argv[j];
692 /* we use g_free here as we sucked all the inner strings
693 * out from it into real_argv */
698 #endif /* G_OS_WIN32 */
701 /* '=' is the new '\0'.
702 * DO NOT CALL unless at least one string ends with '='
705 is_env (const char *a,
710 if (*a == 0 || *b == 0)
723 /* free with g_strfreev */
725 replace_env_var (char **old_environ,
727 const char *new_value)
729 int length, new_length;
730 int index, new_index;
734 /* do two things at once:
735 * - discover the length of the environment ('length')
736 * - find the location (if any) of the env var ('index')
739 for (length = 0; old_environ[length]; length++)
741 /* if we already have it in our environment, replace */
742 if (is_env (old_environ[length], env_var))
747 /* no current env var, no desired env value.
750 if (new_value == NULL && index == -1)
753 /* in all cases now, we will be using a modified environment.
754 * determine its length and allocated it.
757 * new_index = location to insert, if any
758 * new_length = length of the new array
759 * new_environ = the pointer array for the new environment
762 if (new_value == NULL && index >= 0)
764 /* in this case, we will be removing an entry */
765 new_length = length - 1;
768 else if (new_value != NULL && index < 0)
770 /* in this case, we will be adding an entry to the end */
771 new_length = length + 1;
775 /* in this case, we will be replacing the existing entry */
781 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
782 new_environ[new_length] = NULL;
784 /* now we do the copying.
785 * for each entry in the new environment, we decide what to do
789 for (new_i = 0; new_i < new_length; new_i++)
791 if (new_i == new_index)
793 /* insert our new item */
794 new_environ[new_i] = g_strconcat (env_var,
799 /* if we had an old entry, skip it now */
805 /* if this is the old DESKTOP_STARTUP_ID, skip it */
809 /* copy an old item */
810 new_environ[new_i] = g_strdup (old_environ[i]);
815 g_strfreev (old_environ);
821 dup_list_segment (GList *start,
827 while (start != NULL && start != end)
829 res = g_list_prepend (res, start->data);
833 return g_list_reverse (res);
836 #ifdef HAVE__NSGETENVIRON
837 #define environ (*_NSGetEnviron())
838 #elif !defined(G_OS_WIN32)
840 /* According to the Single Unix Specification, environ is not in
841 * * any system header, although unistd.h often declares it.
843 extern char **environ;
847 g_desktop_app_info_launch (GAppInfo *appinfo,
849 GAppLaunchContext *launch_context,
852 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
853 gboolean completed = FALSE;
855 GList *launched_files;
862 g_return_val_if_fail (appinfo != NULL, FALSE);
870 if (!expand_application_parameters (info, &files,
871 &argc, &argv, error))
874 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
876 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
877 _("Unable to find terminal required for application"));
884 launched_files = dup_list_segment (old_files, files);
886 display = g_app_launch_context_get_display (launch_context,
891 if (info->startup_notify)
892 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
896 if (display || sn_id)
900 envp = g_new0 (char *, 1);
902 envp = g_strdupv (environ);
906 envp = replace_env_var (envp,
911 envp = replace_env_var (envp,
912 "DESKTOP_STARTUP_ID",
918 g_list_free (launched_files);
921 if (!g_spawn_async (info->path, /* working directory */
924 G_SPAWN_SEARCH_PATH /* flags */,
925 NULL /* child_setup */,
927 NULL /* child_pid */,
932 g_app_launch_context_launch_failed (launch_context, sn_id);
946 while (files != NULL);
958 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
960 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
963 ((strstr (info->exec, "%u") != NULL) ||
964 (strstr (info->exec, "%U") != NULL));
968 g_desktop_app_info_supports_files (GAppInfo *appinfo)
970 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
973 ((strstr (info->exec, "%f") != NULL) ||
974 (strstr (info->exec, "%F") != NULL));
978 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
980 GAppLaunchContext *launch_context,
990 file = g_file_new_for_uri (uris->data);
992 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
995 files = g_list_prepend (files, file);
998 files = g_list_reverse (files);
1000 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
1002 g_list_foreach (files, (GFunc)g_object_unref, NULL);
1003 g_list_free (files);
1008 G_LOCK_DEFINE_STATIC (g_desktop_env);
1009 static gchar *g_desktop_env = NULL;
1012 * g_desktop_app_info_set_desktop_env:
1013 * @desktop_env: a string specifying what desktop this is
1015 * Sets the name of the desktop that the application is running in.
1016 * This is used by g_app_info_should_show() to evaluate the
1017 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1018 * desktop entry fields.
1020 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1021 * Menu specification</ulink> recognizes the following:
1023 * <member>GNOME</member>
1024 * <member>KDE</member>
1025 * <member>ROX</member>
1026 * <member>XFCE</member>
1027 * <member>Old</member>
1030 * Should be called only once; subsequent calls are ignored.
1033 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1035 G_LOCK (g_desktop_env);
1037 g_desktop_env = g_strdup (desktop_env);
1038 G_UNLOCK (g_desktop_env);
1042 g_desktop_app_info_should_show (GAppInfo *appinfo)
1044 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1046 const gchar *desktop_env;
1049 if (info->nodisplay)
1052 G_LOCK (g_desktop_env);
1053 desktop_env = g_desktop_env;
1054 G_UNLOCK (g_desktop_env);
1056 if (info->only_show_in)
1058 if (desktop_env == NULL)
1062 for (i = 0; info->only_show_in[i] != NULL; i++)
1064 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1074 if (info->not_show_in && desktop_env)
1076 for (i = 0; info->not_show_in[i] != NULL; i++)
1078 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1092 ensure_dir (DirType type,
1095 char *path, *display_name;
1098 if (type == APP_DIR)
1099 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1101 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1104 if (g_mkdir_with_parents (path, 0700) == 0)
1108 display_name = g_filename_display_name (path);
1109 if (type == APP_DIR)
1110 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1111 _("Can't create user application configuration folder %s: %s"),
1112 display_name, g_strerror (err));
1114 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1115 _("Can't create user MIME configuration folder %s: %s"),
1116 display_name, g_strerror (err));
1118 g_free (display_name);
1125 update_default_list (const char *desktop_id,
1126 const char *content_type,
1127 gboolean add_at_start,
1128 gboolean add_at_end,
1132 char *dirname, *filename;
1134 gboolean load_succeeded, res;
1137 gsize length, data_size;
1141 /* Don't add both at start and end */
1142 g_assert (!(add_at_start && add_at_end));
1144 dirname = ensure_dir (APP_DIR, error);
1148 filename = g_build_filename (dirname, "defaults.list", NULL);
1151 key_file = g_key_file_new ();
1152 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1153 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1155 g_key_file_free (key_file);
1156 key_file = g_key_file_new ();
1159 /* Add to the right place in the list */
1162 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1163 content_type, &length, NULL);
1165 list = g_new (char *, 1 + length + 1);
1169 list[i++] = g_strdup (desktop_id);
1172 for (j = 0; old_list[j] != NULL; j++)
1174 if (strcmp (old_list[j], desktop_id) != 0)
1175 list[i++] = g_strdup (old_list[j]);
1179 list[i++] = g_strdup (desktop_id);
1182 g_strfreev (old_list);
1184 g_key_file_set_string_list (key_file,
1185 DEFAULT_APPLICATIONS_GROUP,
1187 (const char * const *)list, i);
1191 /* Remove from removed associations group (unless remove) */
1194 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1195 content_type, &length, NULL);
1197 list = g_new (char *, 1 + length + 1);
1201 list[i++] = g_strdup (desktop_id);
1204 for (j = 0; old_list[j] != NULL; j++)
1206 if (strcmp (old_list[j], desktop_id) != 0)
1207 list[i++] = g_strdup (old_list[j]);
1212 g_strfreev (old_list);
1214 if (list[0] == NULL)
1215 g_key_file_remove_key (key_file,
1216 REMOVED_ASSOCIATIONS_GROUP,
1220 g_key_file_set_string_list (key_file,
1221 REMOVED_ASSOCIATIONS_GROUP,
1223 (const char * const *)list, i);
1228 data = g_key_file_to_data (key_file, &data_size, error);
1229 g_key_file_free (key_file);
1231 res = g_file_set_contents (filename, data, data_size, error);
1233 mime_info_cache_reload (NULL);
1242 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1243 const char *content_type,
1246 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1248 if (!g_desktop_app_info_ensure_saved (info, error))
1251 return update_default_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1255 update_program_done (GPid pid,
1259 /* Did the application exit correctly */
1260 if (WIFEXITED (status) &&
1261 WEXITSTATUS (status) == 0)
1263 /* Here we could clean out any caches in use */
1268 run_update_command (char *command,
1277 GError *error = NULL;
1280 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1282 if (g_spawn_async ("/", argv,
1284 G_SPAWN_SEARCH_PATH |
1285 G_SPAWN_STDOUT_TO_DEV_NULL |
1286 G_SPAWN_STDERR_TO_DEV_NULL |
1287 G_SPAWN_DO_NOT_REAP_CHILD,
1288 NULL, NULL, /* No setup function */
1291 g_child_watch_add (pid, update_program_done, NULL);
1294 /* If we get an error at this point, it's quite likely the user doesn't
1295 * have an installed copy of either 'update-mime-database' or
1296 * 'update-desktop-database'. I don't think we want to popup an error
1297 * dialog at this point, so we just do a g_warning to give the user a
1298 * chance of debugging it.
1300 g_warning ("%s", error->message);
1307 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1308 const char *extension,
1311 char *filename, *basename, *mimetype;
1315 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1318 dirname = ensure_dir (MIMETYPE_DIR, error);
1322 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1323 filename = g_build_filename (dirname, basename, NULL);
1327 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1329 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1334 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1335 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1336 " <mime-type type=\"%s\">\n"
1337 " <comment>%s document</comment>\n"
1338 " <glob pattern=\"*.%s\"/>\n"
1340 "</mime-info>\n", mimetype, extension, extension);
1342 g_file_set_contents (filename, contents, -1, NULL);
1345 run_update_command ("update-mime-database", "mime");
1349 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1359 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1360 const char *content_type,
1363 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1365 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1368 return update_default_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1372 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1378 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1379 const char *content_type,
1382 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1384 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1387 return update_default_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1391 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1396 char *basename, *filename;
1397 char *data, *desktop_id;
1402 if (info->filename != NULL)
1405 /* This is only used for object created with
1406 * g_app_info_create_from_commandline. All other
1407 * object should have a filename
1410 dirname = ensure_dir (APP_DIR, error);
1414 key_file = g_key_file_new ();
1416 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1417 "Encoding", "UTF-8");
1418 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1419 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1420 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1421 G_KEY_FILE_DESKTOP_KEY_TYPE,
1422 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1424 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1425 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1427 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1428 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1430 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1431 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1433 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1434 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1436 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1437 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1439 data = g_key_file_to_data (key_file, &data_size, NULL);
1440 g_key_file_free (key_file);
1442 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1443 filename = g_build_filename (dirname, desktop_id, NULL);
1444 g_free (desktop_id);
1447 fd = g_mkstemp (filename);
1452 display_name = g_filename_display_name (filename);
1453 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1454 _("Can't create user desktop file %s"), display_name);
1455 g_free (display_name);
1461 desktop_id = g_path_get_basename (filename);
1465 res = g_file_set_contents (filename, data, data_size, error);
1468 g_free (desktop_id);
1473 info->filename = filename;
1474 info->desktop_id = desktop_id;
1476 run_update_command ("update-desktop-database", "applications");
1482 * g_app_info_create_from_commandline:
1483 * @commandline: the commandline to use
1484 * @application_name: the application name, or %NULL to use @commandline
1485 * @flags: flags that can specify details of the created #GAppInfo
1486 * @error: a #GError location to store the error occuring, %NULL to ignore.
1488 * Creates a new #GAppInfo from the given information.
1490 * Returns: new #GAppInfo for given command.
1493 g_app_info_create_from_commandline (const char *commandline,
1494 const char *application_name,
1495 GAppInfoCreateFlags flags,
1500 GDesktopAppInfo *info;
1502 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1504 info->filename = NULL;
1505 info->desktop_id = NULL;
1507 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1508 info->startup_notify = FALSE;
1509 info->hidden = FALSE;
1510 info->exec = g_strconcat (commandline, " %f", NULL);
1511 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1512 info->nodisplay = TRUE;
1513 info->binary = binary_from_exec (info->exec);
1515 if (application_name)
1516 info->name = g_strdup (application_name);
1519 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1520 split = g_strsplit (commandline, " ", 2);
1521 basename = g_path_get_basename (split[0]);
1523 info->name = basename;
1524 if (info->name == NULL)
1525 info->name = g_strdup ("custom");
1528 return G_APP_INFO (info);
1532 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1534 iface->dup = g_desktop_app_info_dup;
1535 iface->equal = g_desktop_app_info_equal;
1536 iface->get_id = g_desktop_app_info_get_id;
1537 iface->get_name = g_desktop_app_info_get_name;
1538 iface->get_description = g_desktop_app_info_get_description;
1539 iface->get_executable = g_desktop_app_info_get_executable;
1540 iface->get_icon = g_desktop_app_info_get_icon;
1541 iface->launch = g_desktop_app_info_launch;
1542 iface->supports_uris = g_desktop_app_info_supports_uris;
1543 iface->supports_files = g_desktop_app_info_supports_files;
1544 iface->launch_uris = g_desktop_app_info_launch_uris;
1545 iface->should_show = g_desktop_app_info_should_show;
1546 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1547 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1548 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1549 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1550 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1554 app_info_in_list (GAppInfo *info,
1557 while (list != NULL)
1559 if (g_app_info_equal (info, list->data))
1568 * g_app_info_get_all_for_type:
1569 * @content_type: the content type to find a #GAppInfo for
1571 * Gets a list of all #GAppInfo s for a given content type.
1573 * Returns: #GList of #GAppInfo s for given @content_type
1574 * or %NULL on error.
1577 g_app_info_get_all_for_type (const char *content_type)
1579 GList *desktop_entries, *l;
1581 GDesktopAppInfo *info;
1583 g_return_val_if_fail (content_type != NULL, NULL);
1585 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1588 for (l = desktop_entries; l != NULL; l = l->next)
1590 char *desktop_entry = l->data;
1592 info = g_desktop_app_info_new (desktop_entry);
1595 if (app_info_in_list (G_APP_INFO (info), infos))
1596 g_object_unref (info);
1598 infos = g_list_prepend (infos, info);
1600 g_free (desktop_entry);
1603 g_list_free (desktop_entries);
1605 return g_list_reverse (infos);
1610 * g_app_info_get_default_for_type:
1611 * @content_type: the content type to find a #GAppInfo for
1612 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1615 * Gets the #GAppInfo that correspond to a given content type.
1617 * Returns: #GAppInfo for given @content_type or %NULL on error.
1620 g_app_info_get_default_for_type (const char *content_type,
1621 gboolean must_support_uris)
1623 GList *desktop_entries, *l;
1626 g_return_val_if_fail (content_type != NULL, NULL);
1628 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1631 for (l = desktop_entries; l != NULL; l = l->next)
1633 char *desktop_entry = l->data;
1635 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1638 if (must_support_uris && !g_app_info_supports_uris (info))
1640 g_object_unref (info);
1648 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1649 g_list_free (desktop_entries);
1655 * g_app_info_get_default_for_uri_scheme:
1656 * @uri_scheme: a string containing a URI scheme.
1658 * Gets the default application for launching applications
1659 * using this URI scheme.
1661 * TODO: This is currently unimplemented.
1666 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1668 static gsize lookup = 0;
1670 if (g_once_init_enter (&lookup))
1672 gsize setup_value = 1;
1673 GDesktopAppInfoLookup *lookup_instance;
1674 const char *use_this;
1675 GIOExtensionPoint *ep;
1676 GIOExtension *extension;
1679 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1681 /* Ensure vfs in modules loaded */
1682 _g_io_modules_ensure_loaded ();
1684 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1686 lookup_instance = NULL;
1689 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1691 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1694 if (lookup_instance == NULL)
1696 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1698 extension = l->data;
1699 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1700 if (lookup_instance != NULL)
1705 if (lookup_instance != NULL)
1706 setup_value = (gsize)lookup_instance;
1708 g_once_init_leave (&lookup, setup_value);
1714 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1720 get_apps_from_dir (GHashTable *apps,
1721 const char *dirname,
1725 const char *basename;
1726 char *filename, *subprefix, *desktop_id;
1728 GDesktopAppInfo *appinfo;
1730 dir = g_dir_open (dirname, 0, NULL);
1733 while ((basename = g_dir_read_name (dir)) != NULL)
1735 filename = g_build_filename (dirname, basename, NULL);
1736 if (g_str_has_suffix (basename, ".desktop"))
1738 desktop_id = g_strconcat (prefix, basename, NULL);
1740 /* Use _extended so we catch NULLs too (hidden) */
1741 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1743 appinfo = g_desktop_app_info_new_from_filename (filename);
1745 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1747 g_object_unref (appinfo);
1752 if (appinfo || hidden)
1754 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1758 /* Reuse instead of strdup here */
1759 appinfo->desktop_id = desktop_id;
1764 g_free (desktop_id);
1768 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1770 subprefix = g_strconcat (prefix, basename, "-", NULL);
1771 get_apps_from_dir (apps, filename, subprefix);
1783 * g_app_info_get_all:
1785 * Gets a list of all of the applications currently registered
1788 * For desktop files, this includes applications that have
1789 * <literal>NoDisplay=true</literal> set or are excluded from
1790 * display by means of <literal>OnlyShowIn</literal> or
1791 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1792 * The returned list does not include applications which have
1793 * the <literal>Hidden</literal> key set.
1795 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1798 g_app_info_get_all (void)
1800 const char * const *dirs;
1802 GHashTableIter iter;
1807 dirs = get_applications_search_path ();
1809 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1813 for (i = 0; dirs[i] != NULL; i++)
1814 get_apps_from_dir (apps, dirs[i], "");
1818 g_hash_table_iter_init (&iter, apps);
1819 while (g_hash_table_iter_next (&iter, NULL, &value))
1822 infos = g_list_prepend (infos, value);
1825 g_hash_table_destroy (apps);
1827 return g_list_reverse (infos);
1830 /* Cacheing of mimeinfo.cache and defaults.list files */
1834 GHashTable *mime_info_cache_map;
1835 GHashTable *defaults_list_map;
1836 GHashTable *defaults_list_removed_map;
1837 time_t mime_info_cache_timestamp;
1838 time_t defaults_list_timestamp;
1842 GList *dirs; /* mimeinfo.cache and defaults.list */
1843 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1844 time_t last_stat_time;
1845 guint should_ping_mime_monitor : 1;
1848 static MimeInfoCache *mime_info_cache = NULL;
1849 G_LOCK_DEFINE_STATIC (mime_info_cache);
1851 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1852 const char *mime_type,
1853 char **new_desktop_file_ids);
1855 static MimeInfoCache * mime_info_cache_new (void);
1858 destroy_info_cache_value (gpointer key,
1862 g_list_foreach (value, (GFunc)g_free, NULL);
1863 g_list_free (value);
1867 destroy_info_cache_map (GHashTable *info_cache_map)
1869 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1870 g_hash_table_destroy (info_cache_map);
1874 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1875 const char *cache_file,
1881 filename = g_build_filename (dir->path, cache_file, NULL);
1883 if (g_stat (filename, &buf) < 0)
1890 if (buf.st_mtime != *timestamp)
1896 /* Call with lock held */
1898 remove_all (gpointer key,
1907 mime_info_cache_blow_global_cache (void)
1909 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1914 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1918 gchar *filename, **mime_types;
1925 if (dir->mime_info_cache_map != NULL &&
1926 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1927 &dir->mime_info_cache_timestamp))
1930 if (dir->mime_info_cache_map != NULL)
1931 destroy_info_cache_map (dir->mime_info_cache_map);
1933 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1934 (GDestroyNotify) g_free,
1937 key_file = g_key_file_new ();
1939 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1941 if (g_stat (filename, &buf) < 0)
1944 if (dir->mime_info_cache_timestamp > 0)
1945 mime_info_cache->should_ping_mime_monitor = TRUE;
1947 dir->mime_info_cache_timestamp = buf.st_mtime;
1949 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1954 if (load_error != NULL)
1957 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1960 if (load_error != NULL)
1963 for (i = 0; mime_types[i] != NULL; i++)
1965 gchar **desktop_file_ids;
1966 char *unaliased_type;
1967 desktop_file_ids = g_key_file_get_string_list (key_file,
1973 if (desktop_file_ids == NULL)
1976 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1977 mime_info_cache_dir_add_desktop_entries (dir,
1980 g_free (unaliased_type);
1982 g_strfreev (desktop_file_ids);
1985 g_strfreev (mime_types);
1986 g_key_file_free (key_file);
1991 g_key_file_free (key_file);
1993 if (mime_types != NULL)
1994 g_strfreev (mime_types);
1997 g_error_free (load_error);
2001 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2005 gchar *filename, **mime_types;
2006 char *unaliased_type;
2007 char **desktop_file_ids;
2014 if (dir->defaults_list_map != NULL &&
2015 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2016 &dir->defaults_list_timestamp))
2019 if (dir->defaults_list_map != NULL)
2020 g_hash_table_destroy (dir->defaults_list_map);
2021 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2022 g_free, (GDestroyNotify)g_strfreev);
2024 if (dir->defaults_list_removed_map != NULL)
2025 g_hash_table_destroy (dir->defaults_list_removed_map);
2026 dir->defaults_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2027 g_free, (GDestroyNotify)g_strfreev);
2029 key_file = g_key_file_new ();
2031 filename = g_build_filename (dir->path, "defaults.list", NULL);
2032 if (g_stat (filename, &buf) < 0)
2035 if (dir->defaults_list_timestamp > 0)
2036 mime_info_cache->should_ping_mime_monitor = TRUE;
2038 dir->defaults_list_timestamp = buf.st_mtime;
2040 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2044 if (load_error != NULL)
2047 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2049 if (mime_types != NULL)
2051 for (i = 0; mime_types[i] != NULL; i++)
2053 desktop_file_ids = g_key_file_get_string_list (key_file,
2054 DEFAULT_APPLICATIONS_GROUP,
2058 if (desktop_file_ids == NULL)
2061 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2062 g_hash_table_replace (dir->defaults_list_map,
2067 g_strfreev (mime_types);
2070 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2072 if (mime_types != NULL)
2074 for (i = 0; mime_types[i] != NULL; i++)
2076 desktop_file_ids = g_key_file_get_string_list (key_file,
2077 REMOVED_ASSOCIATIONS_GROUP,
2081 if (desktop_file_ids == NULL)
2084 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2085 g_hash_table_replace (dir->defaults_list_removed_map,
2090 g_strfreev (mime_types);
2093 g_key_file_free (key_file);
2098 g_key_file_free (key_file);
2100 if (mime_types != NULL)
2101 g_strfreev (mime_types);
2104 g_error_free (load_error);
2107 static MimeInfoCacheDir *
2108 mime_info_cache_dir_new (const char *path)
2110 MimeInfoCacheDir *dir;
2112 dir = g_new0 (MimeInfoCacheDir, 1);
2113 dir->path = g_strdup (path);
2119 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2124 if (dir->mime_info_cache_map != NULL)
2126 destroy_info_cache_map (dir->mime_info_cache_map);
2127 dir->mime_info_cache_map = NULL;
2131 if (dir->defaults_list_map != NULL)
2133 g_hash_table_destroy (dir->defaults_list_map);
2134 dir->defaults_list_map = NULL;
2137 if (dir->defaults_list_removed_map != NULL)
2139 g_hash_table_destroy (dir->defaults_list_removed_map);
2140 dir->defaults_list_removed_map = NULL;
2147 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2148 const char *mime_type,
2149 char **new_desktop_file_ids)
2151 GList *desktop_file_ids;
2154 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2157 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2159 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2160 desktop_file_ids = g_list_append (desktop_file_ids,
2161 g_strdup (new_desktop_file_ids[i]));
2164 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2168 mime_info_cache_init_dir_lists (void)
2170 const char * const *dirs;
2173 mime_info_cache = mime_info_cache_new ();
2175 dirs = get_applications_search_path ();
2177 for (i = 0; dirs[i] != NULL; i++)
2179 MimeInfoCacheDir *dir;
2181 dir = mime_info_cache_dir_new (dirs[i]);
2185 mime_info_cache_dir_init (dir);
2186 mime_info_cache_dir_init_defaults_list (dir);
2188 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2194 mime_info_cache_update_dir_lists (void)
2198 tmp = mime_info_cache->dirs;
2202 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2204 /* No need to do this if we had file monitors... */
2205 mime_info_cache_blow_global_cache ();
2206 mime_info_cache_dir_init (dir);
2207 mime_info_cache_dir_init_defaults_list (dir);
2214 mime_info_cache_init (void)
2216 G_LOCK (mime_info_cache);
2217 if (mime_info_cache == NULL)
2218 mime_info_cache_init_dir_lists ();
2224 if (now >= mime_info_cache->last_stat_time + 10)
2226 mime_info_cache_update_dir_lists ();
2227 mime_info_cache->last_stat_time = now;
2231 if (mime_info_cache->should_ping_mime_monitor)
2233 /* g_idle_add (emit_mime_changed, NULL); */
2234 mime_info_cache->should_ping_mime_monitor = FALSE;
2237 G_UNLOCK (mime_info_cache);
2240 static MimeInfoCache *
2241 mime_info_cache_new (void)
2243 MimeInfoCache *cache;
2245 cache = g_new0 (MimeInfoCache, 1);
2247 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2248 (GDestroyNotify) g_free,
2249 (GDestroyNotify) g_free);
2254 mime_info_cache_free (MimeInfoCache *cache)
2259 g_list_foreach (cache->dirs,
2260 (GFunc) mime_info_cache_dir_free,
2262 g_list_free (cache->dirs);
2263 g_hash_table_destroy (cache->global_defaults_cache);
2268 * mime_info_cache_reload:
2269 * @dir: directory path which needs reloading.
2271 * Reload the mime information for the @dir.
2274 mime_info_cache_reload (const char *dir)
2276 /* FIXME: just reload the dir that needs reloading,
2277 * don't blow the whole cache
2279 if (mime_info_cache != NULL)
2281 G_LOCK (mime_info_cache);
2282 mime_info_cache_free (mime_info_cache);
2283 mime_info_cache = NULL;
2284 G_UNLOCK (mime_info_cache);
2289 append_desktop_entry (GList *list,
2290 const char *desktop_entry,
2291 GList *removed_entries)
2293 /* Add if not already in list, and valid */
2294 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2295 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2296 list = g_list_prepend (list, g_strdup (desktop_entry));
2302 * get_all_desktop_entries_for_mime_type:
2303 * @mime_type: a mime type.
2305 * Returns all the desktop ids for @mime_type. The desktop files
2306 * are listed in an order so that default applications are listed before
2307 * non-default ones, and handlers for inherited mimetypes are listed
2308 * after the base ones.
2310 * Return value: a #GList containing the desktop ids which claim
2311 * to handle @mime_type.
2314 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2316 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2317 MimeInfoCacheDir *dir;
2320 char **default_entries;
2321 char **removed_associations;
2324 mime_info_cache_init ();
2326 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2327 G_LOCK (mime_info_cache);
2329 removed_entries = NULL;
2330 desktop_entries = NULL;
2331 for (i = 0; mime_types[i] != NULL; i++)
2333 mime_type = mime_types[i];
2335 /* Go through all apps listed as defaults */
2336 for (dir_list = mime_info_cache->dirs;
2338 dir_list = dir_list->next)
2340 dir = dir_list->data;
2341 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2342 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2343 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2345 removed_associations = g_hash_table_lookup (dir->defaults_list_removed_map, mime_type);
2346 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2347 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2350 /* Go through all entries that support the mimetype */
2351 for (dir_list = mime_info_cache->dirs;
2353 dir_list = dir_list->next)
2355 dir = dir_list->data;
2357 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2358 for (tmp = list; tmp != NULL; tmp = tmp->next)
2359 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2363 G_UNLOCK (mime_info_cache);
2365 g_strfreev (mime_types);
2367 g_list_free (removed_entries);
2369 desktop_entries = g_list_reverse (desktop_entries);
2371 return desktop_entries;
2374 /* GDesktopAppInfoLookup interface: */
2376 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2377 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2378 gpointer class_data);
2381 g_desktop_app_info_lookup_get_type (void)
2383 static GType desktop_app_info_lookup_type = 0;
2385 if (! desktop_app_info_lookup_type)
2387 static const GTypeInfo desktop_app_info_lookup_info =
2389 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2390 g_desktop_app_info_lookup_base_init, /* base_init */
2391 NULL, /* base_finalize */
2392 g_desktop_app_info_lookup_class_init,
2393 NULL, /* class_finalize */
2394 NULL, /* class_data */
2396 0, /* n_preallocs */
2400 desktop_app_info_lookup_type =
2401 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2402 &desktop_app_info_lookup_info, 0);
2404 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2407 return desktop_app_info_lookup_type;
2411 g_desktop_app_info_lookup_class_init (gpointer g_class,
2412 gpointer class_data)
2417 g_desktop_app_info_lookup_base_init (gpointer g_class)
2422 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2423 const char *uri_scheme)
2425 GDesktopAppInfoLookupIface *iface;
2427 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), FALSE);
2429 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2431 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2434 #define __G_DESKTOP_APP_INFO_C__
2435 #include "gioaliasdef.c"