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);
61 static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
62 static void mime_info_cache_reload (const char *dir);
67 * Information about an installed application from a desktop file.
69 struct _GDesktopAppInfo
71 GObject parent_instance;
77 /* FIXME: what about GenericName ? */
91 guint startup_notify : 1;
92 /* FIXME: what about StartupWMClass ? */
95 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
96 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
97 g_desktop_app_info_iface_init))
100 search_path_init (gpointer data)
103 const char * const *data_dirs;
104 const char *user_data_dir;
107 data_dirs = g_get_system_data_dirs ();
108 length = g_strv_length ((char **) data_dirs);
110 args = g_new (char *, length + 2);
113 user_data_dir = g_get_user_data_dir ();
114 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
115 for (i = 0; i < length; i++)
116 args[j++] = g_build_filename (data_dirs[i],
117 "applications", NULL);
123 static const char * const *
124 get_applications_search_path (void)
126 static GOnce once_init = G_ONCE_INIT;
127 return g_once (&once_init, search_path_init, NULL);
131 g_desktop_app_info_finalize (GObject *object)
133 GDesktopAppInfo *info;
135 info = G_DESKTOP_APP_INFO (object);
137 g_free (info->desktop_id);
138 g_free (info->filename);
140 g_free (info->comment);
141 g_free (info->icon_name);
143 g_object_unref (info->icon);
144 g_strfreev (info->only_show_in);
145 g_strfreev (info->not_show_in);
146 g_free (info->try_exec);
148 g_free (info->binary);
151 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
155 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
157 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
159 gobject_class->finalize = g_desktop_app_info_finalize;
163 g_desktop_app_info_init (GDesktopAppInfo *local)
168 * g_desktop_app_info_new_from_filename:
169 * @filename: a string containing a file name.
171 * Creates a new #GDesktopAppInfo.
173 * Returns: a new #GDesktopAppInfo or %NULL on error.
176 g_desktop_app_info_new_from_filename (const char *filename)
178 GDesktopAppInfo *info;
184 key_file = g_key_file_new ();
186 if (!g_key_file_load_from_file (key_file,
191 g_key_file_free (key_file);
195 start_group = g_key_file_get_start_group (key_file);
196 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
198 g_free (start_group);
199 g_key_file_free (key_file);
202 g_free (start_group);
204 type = g_key_file_get_string (key_file,
205 G_KEY_FILE_DESKTOP_GROUP,
206 G_KEY_FILE_DESKTOP_KEY_TYPE,
208 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
211 g_key_file_free (key_file);
216 try_exec = g_key_file_get_string (key_file,
217 G_KEY_FILE_DESKTOP_GROUP,
218 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
223 t = g_find_program_in_path (try_exec);
227 g_key_file_free (key_file);
233 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
234 info->filename = g_strdup (filename);
236 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
237 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
238 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
239 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
240 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);
241 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);
242 info->try_exec = try_exec;
243 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
244 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
245 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
246 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
247 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
249 g_key_file_free (key_file);
254 if (g_path_is_absolute (info->icon_name))
258 file = g_file_new_for_path (info->icon_name);
259 info->icon = g_file_icon_new (file);
260 g_object_unref (file);
263 info->icon = g_themed_icon_new (info->icon_name);
274 while (*p != ' ' && *p != 0)
277 info->binary = g_strndup (start, p - start);
284 * g_desktop_app_info_new:
285 * @desktop_id: the desktop file id
287 * Creates a new #GDesktopAppInfo.
289 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
292 g_desktop_app_info_new (const char *desktop_id)
294 GDesktopAppInfo *appinfo;
295 const char * const *dirs;
299 dirs = get_applications_search_path ();
301 basename = g_strdup (desktop_id);
303 for (i = 0; dirs[i] != NULL; i++)
308 filename = g_build_filename (dirs[i], desktop_id, NULL);
309 appinfo = g_desktop_app_info_new_from_filename (filename);
315 while ((p = strchr (p, '-')) != NULL)
319 filename = g_build_filename (dirs[i], basename, NULL);
320 appinfo = g_desktop_app_info_new_from_filename (filename);
335 appinfo->desktop_id = g_strdup (desktop_id);
337 if (g_desktop_app_info_get_is_hidden (appinfo))
339 g_object_unref (appinfo);
347 g_desktop_app_info_dup (GAppInfo *appinfo)
349 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
350 GDesktopAppInfo *new_info;
352 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
354 new_info->filename = g_strdup (info->filename);
355 new_info->desktop_id = g_strdup (info->desktop_id);
357 new_info->name = g_strdup (info->name);
358 new_info->comment = g_strdup (info->comment);
359 new_info->nodisplay = info->nodisplay;
360 new_info->icon_name = g_strdup (info->icon_name);
361 new_info->icon = g_object_ref (info->icon);
362 new_info->only_show_in = g_strdupv (info->only_show_in);
363 new_info->not_show_in = g_strdupv (info->not_show_in);
364 new_info->try_exec = g_strdup (info->try_exec);
365 new_info->exec = g_strdup (info->exec);
366 new_info->binary = g_strdup (info->binary);
367 new_info->path = g_strdup (info->path);
368 new_info->hidden = info->hidden;
369 new_info->terminal = info->terminal;
370 new_info->startup_notify = info->startup_notify;
372 return G_APP_INFO (new_info);
376 g_desktop_app_info_equal (GAppInfo *appinfo1,
379 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
380 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
382 if (info1->desktop_id == NULL ||
383 info2->desktop_id == NULL)
386 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
390 g_desktop_app_info_get_id (GAppInfo *appinfo)
392 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
394 return info->desktop_id;
398 g_desktop_app_info_get_name (GAppInfo *appinfo)
400 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
402 if (info->name == NULL)
408 * g_desktop_app_info_get_is_hidden:
409 * @info: a #GDesktopAppInfo.
411 * A desktop file is hidden if the Hidden key in it is
414 * Returns: %TRUE if hidden, %FALSE otherwise.
417 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
423 g_desktop_app_info_get_description (GAppInfo *appinfo)
425 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
427 return info->comment;
431 g_desktop_app_info_get_executable (GAppInfo *appinfo)
433 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
439 g_desktop_app_info_get_icon (GAppInfo *appinfo)
441 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
447 expand_macro_single (char macro, GFile *file)
452 path = g_file_get_path (file);
453 uri = g_file_get_uri (file);
459 result = g_shell_quote (uri);
464 result = g_shell_quote (path);
469 result = g_shell_quote (g_path_get_dirname (path));
474 result = g_shell_quote (g_path_get_basename (path));
485 expand_macro (char macro,
487 GDesktopAppInfo *info,
490 GList *files = *file_list;
493 g_return_if_fail (exec != NULL);
503 expanded = expand_macro_single (macro, files->data);
506 g_string_append (exec, expanded);
520 expanded = expand_macro_single (macro, files->data);
523 g_string_append (exec, expanded);
529 if (files != NULL && expanded)
530 g_string_append_c (exec, ' ');
538 g_string_append (exec, "--icon ");
539 g_string_append (exec, info->icon_name);
545 g_string_append (exec, info->name);
550 g_string_append (exec, info->filename);
553 case 'm': /* deprecated */
557 g_string_append_c (exec, '%');
565 expand_application_parameters (GDesktopAppInfo *info,
571 GList *file_list = *files;
572 const char *p = info->exec;
573 GString *expanded_exec = g_string_new (NULL);
576 if (info->exec == NULL)
578 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
579 _("Desktop file didn't specify Exec field"));
585 if (p[0] == '%' && p[1] != '\0')
587 expand_macro (p[1], expanded_exec, info, files);
591 g_string_append_c (expanded_exec, *p);
596 /* No file substitutions */
597 if (file_list == *files && file_list != NULL)
599 /* If there is no macro default to %f. This is also what KDE does */
600 g_string_append_c (expanded_exec, ' ');
601 expand_macro ('f', expanded_exec, info, files);
604 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
605 g_string_free (expanded_exec, TRUE);
610 prepend_terminal_to_vector (int *argc,
617 char **term_argv = NULL;
622 g_return_val_if_fail (argc != NULL, FALSE);
623 g_return_val_if_fail (argv != NULL, FALSE);
631 /* compute size if not given */
634 for (i = 0; the_argv[i] != NULL; i++)
640 term_argv = g_new0 (char *, 3);
642 check = g_find_program_in_path ("gnome-terminal");
645 term_argv[0] = check;
646 /* Note that gnome-terminal takes -x and
647 * as -e in gnome-terminal is broken we use that. */
648 term_argv[1] = g_strdup ("-x");
653 check = g_find_program_in_path ("nxterm");
655 check = g_find_program_in_path ("color-xterm");
657 check = g_find_program_in_path ("rxvt");
659 check = g_find_program_in_path ("xterm");
661 check = g_find_program_in_path ("dtterm");
664 check = g_strdup ("xterm");
665 g_warning ("couldn't find a terminal, falling back to xterm");
667 term_argv[0] = check;
668 term_argv[1] = g_strdup ("-e");
671 real_argc = term_argc + *argc;
672 real_argv = g_new (char *, real_argc + 1);
674 for (i = 0; i < term_argc; i++)
675 real_argv[i] = term_argv[i];
677 for (j = 0; j < *argc; j++, i++)
678 real_argv[i] = (char *)the_argv[j];
686 /* we use g_free here as we sucked all the inner strings
687 * out from it into real_argv */
692 #endif /* G_OS_WIN32 */
695 /* '=' is the new '\0'.
696 * DO NOT CALL unless at least one string ends with '='
699 is_env (const char *a,
704 if (*a == 0 || *b == 0)
717 /* free with g_strfreev */
719 replace_env_var (char **old_environ,
721 const char *new_value)
723 int length, new_length;
724 int index, new_index;
728 /* do two things at once:
729 * - discover the length of the environment ('length')
730 * - find the location (if any) of the env var ('index')
733 for (length = 0; old_environ[length]; length++)
735 /* if we already have it in our environment, replace */
736 if (is_env (old_environ[length], env_var))
741 /* no current env var, no desired env value.
744 if (new_value == NULL && index == -1)
747 /* in all cases now, we will be using a modified environment.
748 * determine its length and allocated it.
751 * new_index = location to insert, if any
752 * new_length = length of the new array
753 * new_environ = the pointer array for the new environment
756 if (new_value == NULL && index >= 0)
758 /* in this case, we will be removing an entry */
759 new_length = length - 1;
762 else if (new_value != NULL && index < 0)
764 /* in this case, we will be adding an entry to the end */
765 new_length = length + 1;
769 /* in this case, we will be replacing the existing entry */
775 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
776 new_environ[new_length] = NULL;
778 /* now we do the copying.
779 * for each entry in the new environment, we decide what to do
783 for (new_i = 0; new_i < new_length; new_i++)
785 if (new_i == new_index)
787 /* insert our new item */
788 new_environ[new_i] = g_strconcat (env_var,
793 /* if we had an old entry, skip it now */
799 /* if this is the old DESKTOP_STARTUP_ID, skip it */
803 /* copy an old item */
804 new_environ[new_i] = g_strdup (old_environ[i]);
809 g_strfreev (old_environ);
815 dup_list_segment (GList *start,
821 while (start != NULL && start != end)
823 res = g_list_prepend (res, start->data);
827 return g_list_reverse (res);
830 #ifdef HAVE__NSGETENVIRON
831 #define environ (*_NSGetEnviron())
832 #elif !defined(G_OS_WIN32)
834 /* According to the Single Unix Specification, environ is not in
835 * * any system header, although unistd.h often declares it.
837 extern char **environ;
841 g_desktop_app_info_launch (GAppInfo *appinfo,
843 GAppLaunchContext *launch_context,
846 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
847 gboolean completed = FALSE;
849 GList *launched_files;
856 g_return_val_if_fail (appinfo != NULL, FALSE);
864 if (!expand_application_parameters (info, &files,
865 &argc, &argv, error))
868 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
870 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
871 _("Unable to find terminal required for application"));
878 launched_files = dup_list_segment (old_files, files);
880 display = g_app_launch_context_get_display (launch_context,
885 if (info->startup_notify)
886 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
890 if (display || sn_id)
894 envp = g_new0 (char *, 1);
896 envp = g_strdupv (environ);
900 envp = replace_env_var (envp,
905 envp = replace_env_var (envp,
906 "DESKTOP_STARTUP_ID",
912 g_list_free (launched_files);
915 if (!g_spawn_async (info->path, /* working directory */
918 G_SPAWN_SEARCH_PATH /* flags */,
919 NULL /* child_setup */,
921 NULL /* child_pid */,
926 g_app_launch_context_launch_failed (launch_context, sn_id);
940 while (files != NULL);
952 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
954 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
957 ((strstr (info->exec, "%u") != NULL) ||
958 (strstr (info->exec, "%U") != NULL));
962 g_desktop_app_info_supports_files (GAppInfo *appinfo)
964 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
967 ((strstr (info->exec, "%f") != NULL) ||
968 (strstr (info->exec, "%F") != NULL));
972 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
974 GAppLaunchContext *launch_context,
984 file = g_file_new_for_uri (uris->data);
986 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
989 files = g_list_prepend (files, file);
992 files = g_list_reverse (files);
994 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
996 g_list_foreach (files, (GFunc)g_object_unref, NULL);
1002 G_LOCK_DEFINE_STATIC (g_desktop_env);
1003 static gchar *g_desktop_env = NULL;
1006 * g_desktop_app_info_set_desktop_env:
1007 * @desktop_env: a string specifying what desktop this is
1009 * Sets the name of the desktop that the application is running in.
1010 * This is used by g_app_info_should_show() to evaluate the
1011 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1012 * desktop entry fields.
1014 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1015 * Menu specification</ulink> recognizes the following:
1017 * <member>GNOME</member>
1018 * <member>KDE</member>
1019 * <member>ROX</member>
1020 * <member>XFCE</member>
1021 * <member>Old</member>
1024 * Should be called only once; subsequent calls are ignored.
1027 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1029 G_LOCK (g_desktop_env);
1031 g_desktop_env = g_strdup (desktop_env);
1032 G_UNLOCK (g_desktop_env);
1036 g_desktop_app_info_should_show (GAppInfo *appinfo)
1038 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1040 const gchar *desktop_env;
1043 if (info->nodisplay)
1046 G_LOCK (g_desktop_env);
1047 desktop_env = g_desktop_env;
1048 G_UNLOCK (g_desktop_env);
1050 if (info->only_show_in)
1052 if (desktop_env == NULL)
1056 for (i = 0; info->only_show_in[i] != NULL; i++)
1058 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1068 if (info->not_show_in && desktop_env)
1070 for (i = 0; info->not_show_in[i] != NULL; i++)
1072 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1086 ensure_dir (DirType type,
1089 char *path, *display_name;
1092 if (type == APP_DIR)
1093 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1095 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1098 if (g_mkdir_with_parents (path, 0700) == 0)
1102 display_name = g_filename_display_name (path);
1103 if (type == APP_DIR)
1104 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1105 _("Can't create user application configuration folder %s: %s"),
1106 display_name, g_strerror (err));
1108 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1109 _("Can't create user MIME configuration folder %s: %s"),
1110 display_name, g_strerror (err));
1112 g_free (display_name);
1119 update_default_list (const char *desktop_id,
1120 const char *content_type,
1121 gboolean add_at_start,
1122 gboolean add_at_end,
1126 char *dirname, *filename;
1128 gboolean load_succeeded, res;
1131 gsize length, data_size;
1135 /* Don't add both at start and end */
1136 g_assert (!(add_at_start && add_at_end));
1138 dirname = ensure_dir (APP_DIR, error);
1142 filename = g_build_filename (dirname, "defaults.list", NULL);
1145 key_file = g_key_file_new ();
1146 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1147 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1149 g_key_file_free (key_file);
1150 key_file = g_key_file_new ();
1153 /* Add to the right place in the list */
1156 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1157 content_type, &length, NULL);
1159 list = g_new (char *, 1 + length + 1);
1163 list[i++] = g_strdup (desktop_id);
1166 for (j = 0; old_list[j] != NULL; j++)
1168 if (strcmp (old_list[j], desktop_id) != 0)
1169 list[i++] = g_strdup (old_list[j]);
1173 list[i++] = g_strdup (desktop_id);
1176 g_strfreev (old_list);
1178 g_key_file_set_string_list (key_file,
1179 DEFAULT_APPLICATIONS_GROUP,
1181 (const char * const *)list, i);
1185 /* Remove from removed associations group (unless remove) */
1188 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1189 content_type, &length, NULL);
1191 list = g_new (char *, 1 + length + 1);
1195 list[i++] = g_strdup (desktop_id);
1198 for (j = 0; old_list[j] != NULL; j++)
1200 if (strcmp (old_list[j], desktop_id) != 0)
1201 list[i++] = g_strdup (old_list[j]);
1206 g_strfreev (old_list);
1208 if (list[0] == NULL)
1209 g_key_file_remove_key (key_file,
1210 REMOVED_ASSOCIATIONS_GROUP,
1214 g_key_file_set_string_list (key_file,
1215 REMOVED_ASSOCIATIONS_GROUP,
1217 (const char * const *)list, i);
1222 data = g_key_file_to_data (key_file, &data_size, error);
1223 g_key_file_free (key_file);
1225 res = g_file_set_contents (filename, data, data_size, error);
1227 mime_info_cache_reload (NULL);
1236 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1237 const char *content_type,
1240 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1242 return update_default_list (info->desktop_id, content_type, TRUE, FALSE, FALSE, error);
1246 update_program_done (GPid pid,
1250 /* Did the application exit correctly */
1251 if (WIFEXITED (status) &&
1252 WEXITSTATUS (status) == 0)
1254 /* Here we could clean out any caches in use */
1259 run_update_command (char *command,
1268 GError *error = NULL;
1271 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1273 if (g_spawn_async ("/", argv,
1275 G_SPAWN_SEARCH_PATH |
1276 G_SPAWN_STDOUT_TO_DEV_NULL |
1277 G_SPAWN_STDERR_TO_DEV_NULL |
1278 G_SPAWN_DO_NOT_REAP_CHILD,
1279 NULL, NULL, /* No setup function */
1282 g_child_watch_add (pid, update_program_done, NULL);
1285 /* If we get an error at this point, it's quite likely the user doesn't
1286 * have an installed copy of either 'update-mime-database' or
1287 * 'update-desktop-database'. I don't think we want to popup an error
1288 * dialog at this point, so we just do a g_warning to give the user a
1289 * chance of debugging it.
1291 g_warning ("%s", error->message);
1298 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1299 const char *extension,
1302 char *filename, *basename, *mimetype;
1306 dirname = ensure_dir (MIMETYPE_DIR, error);
1310 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1311 filename = g_build_filename (dirname, basename, NULL);
1315 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1317 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1322 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1323 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1324 " <mime-type type=\"%s\">\n"
1325 " <comment>%s document</comment>\n"
1326 " <glob pattern=\"*.%s\"/>\n"
1328 "</mime-info>\n", mimetype, extension, extension);
1330 g_file_set_contents (filename, contents, -1, NULL);
1333 run_update_command ("update-mime-database", "mime");
1337 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1347 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1348 const char *content_type,
1351 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1353 return update_default_list (info->desktop_id, content_type, FALSE, TRUE, FALSE, error);
1357 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1363 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1364 const char *content_type,
1367 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1369 return update_default_list (info->desktop_id, content_type, FALSE, FALSE, TRUE, error);
1373 * g_app_info_create_from_commandline:
1374 * @commandline: the commandline to use
1375 * @application_name: the application name, or %NULL to use @commandline
1376 * @flags: flags that can specify details of the created #GAppInfo
1377 * @error: a #GError location to store the error occuring, %NULL to ignore.
1379 * Creates a new #GAppInfo from the given information.
1381 * Returns: new #GAppInfo for given command.
1384 g_app_info_create_from_commandline (const char *commandline,
1385 const char *application_name,
1386 GAppInfoCreateFlags flags,
1392 char *basename, *exec, *filename, *comment;
1393 char *data, *desktop_id;
1396 GDesktopAppInfo *info;
1399 dirname = ensure_dir (APP_DIR, error);
1403 key_file = g_key_file_new ();
1405 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1406 "Encoding", "UTF-8");
1407 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1408 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1409 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1410 G_KEY_FILE_DESKTOP_KEY_TYPE,
1411 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1412 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1413 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1414 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1416 exec = g_strconcat (commandline, " %f", NULL);
1417 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1418 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1421 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1422 split = g_strsplit (commandline, " ", 2);
1423 basename = g_path_get_basename (split[0]);
1425 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1426 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1428 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1429 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1430 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1433 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1434 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1436 data = g_key_file_to_data (key_file, &data_size, NULL);
1437 g_key_file_free (key_file);
1439 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1441 filename = g_build_filename (dirname, desktop_id, NULL);
1442 g_free (desktop_id);
1445 fd = g_mkstemp (filename);
1450 display_name = g_filename_display_name (filename);
1451 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1452 _("Can't create user desktop file %s"), display_name);
1453 g_free (display_name);
1459 desktop_id = g_path_get_basename (filename);
1463 res = g_file_set_contents (filename, data, data_size, error);
1466 g_free (desktop_id);
1471 run_update_command ("update-desktop-database", "applications");
1473 info = g_desktop_app_info_new_from_filename (filename);
1476 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1477 _("Can't load just created desktop file"));
1479 info->desktop_id = g_strdup (desktop_id);
1481 g_free (desktop_id);
1483 return G_APP_INFO (info);
1488 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1490 iface->dup = g_desktop_app_info_dup;
1491 iface->equal = g_desktop_app_info_equal;
1492 iface->get_id = g_desktop_app_info_get_id;
1493 iface->get_name = g_desktop_app_info_get_name;
1494 iface->get_description = g_desktop_app_info_get_description;
1495 iface->get_executable = g_desktop_app_info_get_executable;
1496 iface->get_icon = g_desktop_app_info_get_icon;
1497 iface->launch = g_desktop_app_info_launch;
1498 iface->supports_uris = g_desktop_app_info_supports_uris;
1499 iface->supports_files = g_desktop_app_info_supports_files;
1500 iface->launch_uris = g_desktop_app_info_launch_uris;
1501 iface->should_show = g_desktop_app_info_should_show;
1502 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1503 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1504 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1505 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1506 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1510 app_info_in_list (GAppInfo *info,
1513 while (list != NULL)
1515 if (g_app_info_equal (info, list->data))
1524 * g_app_info_get_all_for_type:
1525 * @content_type: the content type to find a #GAppInfo for
1527 * Gets a list of all #GAppInfo s for a given content type.
1529 * Returns: #GList of #GAppInfo s for given @content_type
1530 * or %NULL on error.
1533 g_app_info_get_all_for_type (const char *content_type)
1535 GList *desktop_entries, *l;
1537 GDesktopAppInfo *info;
1539 g_return_val_if_fail (content_type != NULL, NULL);
1541 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1544 for (l = desktop_entries; l != NULL; l = l->next)
1546 char *desktop_entry = l->data;
1548 info = g_desktop_app_info_new (desktop_entry);
1551 if (app_info_in_list (G_APP_INFO (info), infos))
1552 g_object_unref (info);
1554 infos = g_list_prepend (infos, info);
1556 g_free (desktop_entry);
1559 g_list_free (desktop_entries);
1561 return g_list_reverse (infos);
1566 * g_app_info_get_default_for_type:
1567 * @content_type: the content type to find a #GAppInfo for
1568 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1571 * Gets the #GAppInfo that correspond to a given content type.
1573 * Returns: #GAppInfo for given @content_type or %NULL on error.
1576 g_app_info_get_default_for_type (const char *content_type,
1577 gboolean must_support_uris)
1579 GList *desktop_entries, *l;
1582 g_return_val_if_fail (content_type != NULL, NULL);
1584 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1587 for (l = desktop_entries; l != NULL; l = l->next)
1589 char *desktop_entry = l->data;
1591 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1594 if (must_support_uris && !g_app_info_supports_uris (info))
1596 g_object_unref (info);
1604 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1605 g_list_free (desktop_entries);
1611 * g_app_info_get_default_for_uri_scheme:
1612 * @uri_scheme: a string containing a URI scheme.
1614 * Gets the default application for launching applications
1615 * using this URI scheme.
1617 * TODO: This is currently unimplemented.
1622 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1624 static gsize lookup = 0;
1626 if (g_once_init_enter (&lookup))
1628 gsize setup_value = 1;
1629 GDesktopAppInfoLookup *lookup_instance;
1630 const char *use_this;
1631 GIOExtensionPoint *ep;
1632 GIOExtension *extension;
1635 use_this = g_getenv ("GIO_USE_URI_ASSOCIATION");
1637 /* Ensure vfs in modules loaded */
1638 _g_io_modules_ensure_loaded ();
1640 ep = g_io_extension_point_lookup (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1642 lookup_instance = NULL;
1645 extension = g_io_extension_point_get_extension_by_name (ep, use_this);
1647 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1650 if (lookup_instance == NULL)
1652 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
1654 extension = l->data;
1655 lookup_instance = g_object_new (g_io_extension_get_type (extension), NULL);
1656 if (lookup_instance != NULL)
1661 if (lookup_instance != NULL)
1662 setup_value = (gsize)lookup_instance;
1664 g_once_init_leave (&lookup, setup_value);
1670 return g_desktop_app_info_lookup_get_default_for_uri_scheme (G_DESKTOP_APP_INFO_LOOKUP (lookup),
1676 get_apps_from_dir (GHashTable *apps,
1677 const char *dirname,
1681 const char *basename;
1682 char *filename, *subprefix, *desktop_id;
1684 GDesktopAppInfo *appinfo;
1686 dir = g_dir_open (dirname, 0, NULL);
1689 while ((basename = g_dir_read_name (dir)) != NULL)
1691 filename = g_build_filename (dirname, basename, NULL);
1692 if (g_str_has_suffix (basename, ".desktop"))
1694 desktop_id = g_strconcat (prefix, basename, NULL);
1696 /* Use _extended so we catch NULLs too (hidden) */
1697 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1699 appinfo = g_desktop_app_info_new_from_filename (filename);
1701 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
1703 g_object_unref (appinfo);
1708 if (appinfo || hidden)
1710 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1714 /* Reuse instead of strdup here */
1715 appinfo->desktop_id = desktop_id;
1720 g_free (desktop_id);
1724 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1726 subprefix = g_strconcat (prefix, basename, "-", NULL);
1727 get_apps_from_dir (apps, filename, subprefix);
1739 * g_app_info_get_all:
1741 * Gets a list of all of the applications currently registered
1744 * For desktop files, this includes applications that have
1745 * <literal>NoDisplay=true</literal> set or are excluded from
1746 * display by means of <literal>OnlyShowIn</literal> or
1747 * <literal>NotShowIn</literal>. See g_app_info_should_show().
1748 * The returned list does not include applications which have
1749 * the <literal>Hidden</literal> key set.
1751 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1754 g_app_info_get_all (void)
1756 const char * const *dirs;
1758 GHashTableIter iter;
1763 dirs = get_applications_search_path ();
1765 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1769 for (i = 0; dirs[i] != NULL; i++)
1770 get_apps_from_dir (apps, dirs[i], "");
1774 g_hash_table_iter_init (&iter, apps);
1775 while (g_hash_table_iter_next (&iter, NULL, &value))
1778 infos = g_list_prepend (infos, value);
1781 g_hash_table_destroy (apps);
1783 return g_list_reverse (infos);
1786 /* Cacheing of mimeinfo.cache and defaults.list files */
1790 GHashTable *mime_info_cache_map;
1791 GHashTable *defaults_list_map;
1792 GHashTable *defaults_list_removed_map;
1793 time_t mime_info_cache_timestamp;
1794 time_t defaults_list_timestamp;
1798 GList *dirs; /* mimeinfo.cache and defaults.list */
1799 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1800 time_t last_stat_time;
1801 guint should_ping_mime_monitor : 1;
1804 static MimeInfoCache *mime_info_cache = NULL;
1805 G_LOCK_DEFINE_STATIC (mime_info_cache);
1807 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1808 const char *mime_type,
1809 char **new_desktop_file_ids);
1811 static MimeInfoCache * mime_info_cache_new (void);
1814 destroy_info_cache_value (gpointer key,
1818 g_list_foreach (value, (GFunc)g_free, NULL);
1819 g_list_free (value);
1823 destroy_info_cache_map (GHashTable *info_cache_map)
1825 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1826 g_hash_table_destroy (info_cache_map);
1830 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1831 const char *cache_file,
1837 filename = g_build_filename (dir->path, cache_file, NULL);
1839 if (g_stat (filename, &buf) < 0)
1846 if (buf.st_mtime != *timestamp)
1852 /* Call with lock held */
1854 remove_all (gpointer key,
1863 mime_info_cache_blow_global_cache (void)
1865 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1870 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1874 gchar *filename, **mime_types;
1881 if (dir->mime_info_cache_map != NULL &&
1882 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1883 &dir->mime_info_cache_timestamp))
1886 if (dir->mime_info_cache_map != NULL)
1887 destroy_info_cache_map (dir->mime_info_cache_map);
1889 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1890 (GDestroyNotify) g_free,
1893 key_file = g_key_file_new ();
1895 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1897 if (g_stat (filename, &buf) < 0)
1900 if (dir->mime_info_cache_timestamp > 0)
1901 mime_info_cache->should_ping_mime_monitor = TRUE;
1903 dir->mime_info_cache_timestamp = buf.st_mtime;
1905 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1910 if (load_error != NULL)
1913 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1916 if (load_error != NULL)
1919 for (i = 0; mime_types[i] != NULL; i++)
1921 gchar **desktop_file_ids;
1922 char *unaliased_type;
1923 desktop_file_ids = g_key_file_get_string_list (key_file,
1929 if (desktop_file_ids == NULL)
1932 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1933 mime_info_cache_dir_add_desktop_entries (dir,
1936 g_free (unaliased_type);
1938 g_strfreev (desktop_file_ids);
1941 g_strfreev (mime_types);
1942 g_key_file_free (key_file);
1947 g_key_file_free (key_file);
1949 if (mime_types != NULL)
1950 g_strfreev (mime_types);
1953 g_error_free (load_error);
1957 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1961 gchar *filename, **mime_types;
1962 char *unaliased_type;
1963 char **desktop_file_ids;
1970 if (dir->defaults_list_map != NULL &&
1971 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1972 &dir->defaults_list_timestamp))
1975 if (dir->defaults_list_map != NULL)
1976 g_hash_table_destroy (dir->defaults_list_map);
1977 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1978 g_free, (GDestroyNotify)g_strfreev);
1980 if (dir->defaults_list_removed_map != NULL)
1981 g_hash_table_destroy (dir->defaults_list_removed_map);
1982 dir->defaults_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1983 g_free, (GDestroyNotify)g_strfreev);
1985 key_file = g_key_file_new ();
1987 filename = g_build_filename (dir->path, "defaults.list", NULL);
1988 if (g_stat (filename, &buf) < 0)
1991 if (dir->defaults_list_timestamp > 0)
1992 mime_info_cache->should_ping_mime_monitor = TRUE;
1994 dir->defaults_list_timestamp = buf.st_mtime;
1996 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2000 if (load_error != NULL)
2003 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2005 if (mime_types != NULL)
2007 for (i = 0; mime_types[i] != NULL; i++)
2009 desktop_file_ids = g_key_file_get_string_list (key_file,
2010 DEFAULT_APPLICATIONS_GROUP,
2014 if (desktop_file_ids == NULL)
2017 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2018 g_hash_table_replace (dir->defaults_list_map,
2023 g_strfreev (mime_types);
2026 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2028 if (mime_types != NULL)
2030 for (i = 0; mime_types[i] != NULL; i++)
2032 desktop_file_ids = g_key_file_get_string_list (key_file,
2033 REMOVED_ASSOCIATIONS_GROUP,
2037 if (desktop_file_ids == NULL)
2040 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2041 g_hash_table_replace (dir->defaults_list_removed_map,
2046 g_strfreev (mime_types);
2049 g_key_file_free (key_file);
2054 g_key_file_free (key_file);
2056 if (mime_types != NULL)
2057 g_strfreev (mime_types);
2060 g_error_free (load_error);
2063 static MimeInfoCacheDir *
2064 mime_info_cache_dir_new (const char *path)
2066 MimeInfoCacheDir *dir;
2068 dir = g_new0 (MimeInfoCacheDir, 1);
2069 dir->path = g_strdup (path);
2075 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2080 if (dir->mime_info_cache_map != NULL)
2082 destroy_info_cache_map (dir->mime_info_cache_map);
2083 dir->mime_info_cache_map = NULL;
2087 if (dir->defaults_list_map != NULL)
2089 g_hash_table_destroy (dir->defaults_list_map);
2090 dir->defaults_list_map = NULL;
2093 if (dir->defaults_list_removed_map != NULL)
2095 g_hash_table_destroy (dir->defaults_list_removed_map);
2096 dir->defaults_list_removed_map = NULL;
2103 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2104 const char *mime_type,
2105 char **new_desktop_file_ids)
2107 GList *desktop_file_ids;
2110 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2113 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2115 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2116 desktop_file_ids = g_list_append (desktop_file_ids,
2117 g_strdup (new_desktop_file_ids[i]));
2120 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2124 mime_info_cache_init_dir_lists (void)
2126 const char * const *dirs;
2129 mime_info_cache = mime_info_cache_new ();
2131 dirs = get_applications_search_path ();
2133 for (i = 0; dirs[i] != NULL; i++)
2135 MimeInfoCacheDir *dir;
2137 dir = mime_info_cache_dir_new (dirs[i]);
2141 mime_info_cache_dir_init (dir);
2142 mime_info_cache_dir_init_defaults_list (dir);
2144 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2150 mime_info_cache_update_dir_lists (void)
2154 tmp = mime_info_cache->dirs;
2158 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2160 /* No need to do this if we had file monitors... */
2161 mime_info_cache_blow_global_cache ();
2162 mime_info_cache_dir_init (dir);
2163 mime_info_cache_dir_init_defaults_list (dir);
2170 mime_info_cache_init (void)
2172 G_LOCK (mime_info_cache);
2173 if (mime_info_cache == NULL)
2174 mime_info_cache_init_dir_lists ();
2180 if (now >= mime_info_cache->last_stat_time + 10)
2182 mime_info_cache_update_dir_lists ();
2183 mime_info_cache->last_stat_time = now;
2187 if (mime_info_cache->should_ping_mime_monitor)
2189 /* g_idle_add (emit_mime_changed, NULL); */
2190 mime_info_cache->should_ping_mime_monitor = FALSE;
2193 G_UNLOCK (mime_info_cache);
2196 static MimeInfoCache *
2197 mime_info_cache_new (void)
2199 MimeInfoCache *cache;
2201 cache = g_new0 (MimeInfoCache, 1);
2203 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2204 (GDestroyNotify) g_free,
2205 (GDestroyNotify) g_free);
2210 mime_info_cache_free (MimeInfoCache *cache)
2215 g_list_foreach (cache->dirs,
2216 (GFunc) mime_info_cache_dir_free,
2218 g_list_free (cache->dirs);
2219 g_hash_table_destroy (cache->global_defaults_cache);
2224 * mime_info_cache_reload:
2225 * @dir: directory path which needs reloading.
2227 * Reload the mime information for the @dir.
2230 mime_info_cache_reload (const char *dir)
2232 /* FIXME: just reload the dir that needs reloading,
2233 * don't blow the whole cache
2235 if (mime_info_cache != NULL)
2237 G_LOCK (mime_info_cache);
2238 mime_info_cache_free (mime_info_cache);
2239 mime_info_cache = NULL;
2240 G_UNLOCK (mime_info_cache);
2245 append_desktop_entry (GList *list,
2246 const char *desktop_entry,
2247 GList *removed_entries)
2249 /* Add if not already in list, and valid */
2250 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2251 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2252 list = g_list_prepend (list, g_strdup (desktop_entry));
2258 * get_all_desktop_entries_for_mime_type:
2259 * @mime_type: a mime type.
2261 * Returns all the desktop ids for @mime_type. The desktop files
2262 * are listed in an order so that default applications are listed before
2263 * non-default ones, and handlers for inherited mimetypes are listed
2264 * after the base ones.
2266 * Return value: a #GList containing the desktop ids which claim
2267 * to handle @mime_type.
2270 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2272 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2273 MimeInfoCacheDir *dir;
2276 char **default_entries;
2277 char **removed_associations;
2280 mime_info_cache_init ();
2282 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2283 G_LOCK (mime_info_cache);
2285 removed_entries = NULL;
2286 desktop_entries = NULL;
2287 for (i = 0; mime_types[i] != NULL; i++)
2289 mime_type = mime_types[i];
2291 /* Go through all apps listed as defaults */
2292 for (dir_list = mime_info_cache->dirs;
2294 dir_list = dir_list->next)
2296 dir = dir_list->data;
2297 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2298 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2299 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2301 removed_associations = g_hash_table_lookup (dir->defaults_list_removed_map, mime_type);
2302 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2303 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2306 /* Go through all entries that support the mimetype */
2307 for (dir_list = mime_info_cache->dirs;
2309 dir_list = dir_list->next)
2311 dir = dir_list->data;
2313 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2314 for (tmp = list; tmp != NULL; tmp = tmp->next)
2315 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2319 G_UNLOCK (mime_info_cache);
2321 g_strfreev (mime_types);
2323 g_list_free (removed_entries);
2325 desktop_entries = g_list_reverse (desktop_entries);
2327 return desktop_entries;
2330 /* GDesktopAppInfoLookup interface: */
2332 static void g_desktop_app_info_lookup_base_init (gpointer g_class);
2333 static void g_desktop_app_info_lookup_class_init (gpointer g_class,
2334 gpointer class_data);
2337 g_desktop_app_info_lookup_get_type (void)
2339 static GType desktop_app_info_lookup_type = 0;
2341 if (! desktop_app_info_lookup_type)
2343 static const GTypeInfo desktop_app_info_lookup_info =
2345 sizeof (GDesktopAppInfoLookupIface), /* class_size */
2346 g_desktop_app_info_lookup_base_init, /* base_init */
2347 NULL, /* base_finalize */
2348 g_desktop_app_info_lookup_class_init,
2349 NULL, /* class_finalize */
2350 NULL, /* class_data */
2352 0, /* n_preallocs */
2356 desktop_app_info_lookup_type =
2357 g_type_register_static (G_TYPE_INTERFACE, I_("GDesktopAppInfoLookup"),
2358 &desktop_app_info_lookup_info, 0);
2360 g_type_interface_add_prerequisite (desktop_app_info_lookup_type, G_TYPE_OBJECT);
2363 return desktop_app_info_lookup_type;
2367 g_desktop_app_info_lookup_class_init (gpointer g_class,
2368 gpointer class_data)
2373 g_desktop_app_info_lookup_base_init (gpointer g_class)
2378 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2379 const char *uri_scheme)
2381 GDesktopAppInfoLookupIface *iface;
2383 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), FALSE);
2385 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2387 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
2390 #define __G_DESKTOP_APP_INFO_C__
2391 #include "gioaliasdef.c"