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 #undef G_DISABLE_DEPRECATED
37 #include "gcontenttypeprivate.h"
38 #include "gdesktopappinfo.h"
41 #include "gthemedicon.h"
42 #include "gfileicon.h"
43 #include <glib/gstdio.h>
45 #include "giomodule-priv.h"
50 * SECTION:gdesktopappinfo
51 * @title: GDesktopAppInfo
52 * @short_description: Application information from desktop files
53 * @include: gio/gdesktopappinfo.h
55 * #GDesktopAppInfo is an implementation of #GAppInfo based on
58 * Note that <filename><gio/gdesktopappinfo.h></filename> belongs to
59 * the UNIX-specific GIO interfaces, thus you have to use the
60 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
64 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
65 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
66 #define MIME_CACHE_GROUP "MIME Cache"
67 #define FULL_NAME_KEY "X-GNOME-FullName"
69 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
70 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
72 gboolean include_fallback,
73 char **explicit_default);
74 static void mime_info_cache_reload (const char *dir);
75 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
81 * Information about an installed application from a desktop file.
83 struct _GDesktopAppInfo
85 GObject parent_instance;
91 /* FIXME: what about GenericName ? */
106 guint startup_notify : 1;
108 /* FIXME: what about StartupWMClass ? */
112 UPDATE_MIME_NONE = 1 << 0,
113 UPDATE_MIME_SET_DEFAULT = 1 << 1,
114 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
115 UPDATE_MIME_REMOVE = 1 << 3,
116 UPDATE_MIME_SET_LAST_USED = 1 << 4,
119 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
120 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
121 g_desktop_app_info_iface_init))
124 search_path_init (gpointer data)
127 const char * const *data_dirs;
128 const char *user_data_dir;
131 data_dirs = g_get_system_data_dirs ();
132 length = g_strv_length ((char **) data_dirs);
134 args = g_new (char *, length + 2);
137 user_data_dir = g_get_user_data_dir ();
138 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
139 for (i = 0; i < length; i++)
140 args[j++] = g_build_filename (data_dirs[i],
141 "applications", NULL);
147 static const char * const *
148 get_applications_search_path (void)
150 static GOnce once_init = G_ONCE_INIT;
151 return g_once (&once_init, search_path_init, NULL);
155 g_desktop_app_info_finalize (GObject *object)
157 GDesktopAppInfo *info;
159 info = G_DESKTOP_APP_INFO (object);
161 g_free (info->desktop_id);
162 g_free (info->filename);
164 g_free (info->fullname);
165 g_free (info->comment);
166 g_free (info->icon_name);
168 g_object_unref (info->icon);
169 g_strfreev (info->only_show_in);
170 g_strfreev (info->not_show_in);
171 g_free (info->try_exec);
173 g_free (info->binary);
176 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
180 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->finalize = g_desktop_app_info_finalize;
188 g_desktop_app_info_init (GDesktopAppInfo *local)
193 binary_from_exec (const char *exec)
195 const char *p, *start;
201 while (*p != ' ' && *p != 0)
204 return g_strndup (start, p - start);
209 * g_desktop_app_info_new_from_keyfile:
210 * @key_file: an opened #GKeyFile
212 * Creates a new #GDesktopAppInfo.
214 * Returns: a new #GDesktopAppInfo or %NULL on error.
219 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
221 GDesktopAppInfo *info;
226 start_group = g_key_file_get_start_group (key_file);
227 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
229 g_free (start_group);
232 g_free (start_group);
234 type = g_key_file_get_string (key_file,
235 G_KEY_FILE_DESKTOP_GROUP,
236 G_KEY_FILE_DESKTOP_KEY_TYPE,
238 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
245 try_exec = g_key_file_get_string (key_file,
246 G_KEY_FILE_DESKTOP_GROUP,
247 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
249 if (try_exec && try_exec[0] != '\0')
252 t = g_find_program_in_path (try_exec);
261 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
262 info->filename = NULL;
264 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
265 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
266 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
267 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
268 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
269 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);
270 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);
271 info->try_exec = try_exec;
272 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
273 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
274 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
275 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
276 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
277 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
282 if (g_path_is_absolute (info->icon_name))
286 file = g_file_new_for_path (info->icon_name);
287 info->icon = g_file_icon_new (file);
288 g_object_unref (file);
294 /* Work around a common mistake in desktop files */
295 if ((p = strrchr (info->icon_name, '.')) != NULL &&
296 (strcmp (p, ".png") == 0 ||
297 strcmp (p, ".xpm") == 0 ||
298 strcmp (p, ".svg") == 0))
301 info->icon = g_themed_icon_new (info->icon_name);
306 info->binary = binary_from_exec (info->exec);
308 if (info->path && info->path[0] == '\0')
318 * g_desktop_app_info_new_from_filename:
319 * @filename: the path of a desktop file, in the GLib filename encoding
321 * Creates a new #GDesktopAppInfo.
323 * Returns: a new #GDesktopAppInfo or %NULL on error.
326 g_desktop_app_info_new_from_filename (const char *filename)
329 GDesktopAppInfo *info = NULL;
331 key_file = g_key_file_new ();
333 if (g_key_file_load_from_file (key_file,
338 info = g_desktop_app_info_new_from_keyfile (key_file);
340 info->filename = g_strdup (filename);
343 g_key_file_free (key_file);
349 * g_desktop_app_info_new:
350 * @desktop_id: the desktop file id
352 * Creates a new #GDesktopAppInfo based on a desktop file id.
354 * A desktop file id is the basename of the desktop file, including the
355 * .desktop extension. GIO is looking for a desktop file with this name
356 * in the <filename>applications</filename> subdirectories of the XDG data
357 * directories (i.e. the directories specified in the
358 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
359 * variables). GIO also supports the prefix-to-subdirectory mapping that is
360 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
361 * (i.e. a desktop id of kde-foo.desktop will match
362 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
364 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
367 g_desktop_app_info_new (const char *desktop_id)
369 GDesktopAppInfo *appinfo;
370 const char * const *dirs;
374 dirs = get_applications_search_path ();
376 basename = g_strdup (desktop_id);
378 for (i = 0; dirs[i] != NULL; i++)
383 filename = g_build_filename (dirs[i], desktop_id, NULL);
384 appinfo = g_desktop_app_info_new_from_filename (filename);
390 while ((p = strchr (p, '-')) != NULL)
394 filename = g_build_filename (dirs[i], basename, NULL);
395 appinfo = g_desktop_app_info_new_from_filename (filename);
410 appinfo->desktop_id = g_strdup (desktop_id);
412 if (g_desktop_app_info_get_is_hidden (appinfo))
414 g_object_unref (appinfo);
422 g_desktop_app_info_dup (GAppInfo *appinfo)
424 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
425 GDesktopAppInfo *new_info;
427 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
429 new_info->filename = g_strdup (info->filename);
430 new_info->desktop_id = g_strdup (info->desktop_id);
432 new_info->name = g_strdup (info->name);
433 new_info->fullname = g_strdup (info->fullname);
434 new_info->comment = g_strdup (info->comment);
435 new_info->nodisplay = info->nodisplay;
436 new_info->icon_name = g_strdup (info->icon_name);
438 new_info->icon = g_object_ref (info->icon);
439 new_info->only_show_in = g_strdupv (info->only_show_in);
440 new_info->not_show_in = g_strdupv (info->not_show_in);
441 new_info->try_exec = g_strdup (info->try_exec);
442 new_info->exec = g_strdup (info->exec);
443 new_info->binary = g_strdup (info->binary);
444 new_info->path = g_strdup (info->path);
445 new_info->hidden = info->hidden;
446 new_info->terminal = info->terminal;
447 new_info->startup_notify = info->startup_notify;
449 return G_APP_INFO (new_info);
453 g_desktop_app_info_equal (GAppInfo *appinfo1,
456 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
457 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
459 if (info1->desktop_id == NULL ||
460 info2->desktop_id == NULL)
461 return info1 == info2;
463 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
467 g_desktop_app_info_get_id (GAppInfo *appinfo)
469 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
471 return info->desktop_id;
475 g_desktop_app_info_get_name (GAppInfo *appinfo)
477 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
479 if (info->name == NULL)
485 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
487 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
489 if (info->fullname == NULL)
490 return g_desktop_app_info_get_name (appinfo);
491 return info->fullname;
495 * g_desktop_app_info_get_is_hidden:
496 * @info: a #GDesktopAppInfo.
498 * A desktop file is hidden if the Hidden key in it is
501 * Returns: %TRUE if hidden, %FALSE otherwise.
504 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
510 * g_desktop_app_info_get_filename:
511 * @info: a #GDesktopAppInfo
513 * When @info was created from a known filename, return it. In some
514 * situations such as the #GDesktopAppInfo returned from
515 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
517 * Returns: The full path to the file for @info, or %NULL if not known.
521 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
523 return info->filename;
527 g_desktop_app_info_get_description (GAppInfo *appinfo)
529 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
531 return info->comment;
535 g_desktop_app_info_get_executable (GAppInfo *appinfo)
537 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
543 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
545 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
551 g_desktop_app_info_get_icon (GAppInfo *appinfo)
553 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
559 expand_macro_single (char macro, char *uri)
565 file = g_file_new_for_uri (uri);
566 path = g_file_get_path (file);
567 g_object_unref (file);
573 result = g_shell_quote (uri);
578 result = g_shell_quote (path);
584 name = g_path_get_dirname (path);
585 result = g_shell_quote (name);
593 name = g_path_get_basename (path);
594 result = g_shell_quote (name);
606 expand_macro (char macro,
608 GDesktopAppInfo *info,
611 GList *uris = *uri_list;
613 gboolean force_file_uri;
614 char force_file_uri_macro;
617 g_return_if_fail (exec != NULL);
619 /* On %u and %U, pass POSIX file path pointing to the URI via
620 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
621 * running or the URI doesn't have a POSIX file path via FUSE
622 * we'll just pass the URI.
624 force_file_uri_macro = macro;
625 force_file_uri = FALSE;
631 force_file_uri_macro = 'f';
632 force_file_uri = TRUE;
635 force_file_uri_macro = 'F';
636 force_file_uri = TRUE;
652 if (!force_file_uri ||
653 /* Pass URI if it contains an anchor */
654 strchr (uri, '#') != NULL)
656 expanded = expand_macro_single (macro, uri);
660 expanded = expand_macro_single (force_file_uri_macro, uri);
661 if (expanded == NULL)
662 expanded = expand_macro_single (macro, uri);
667 g_string_append (exec, expanded);
683 if (!force_file_uri ||
684 /* Pass URI if it contains an anchor */
685 strchr (uri, '#') != NULL)
687 expanded = expand_macro_single (macro, uri);
691 expanded = expand_macro_single (force_file_uri_macro, uri);
692 if (expanded == NULL)
693 expanded = expand_macro_single (macro, uri);
698 g_string_append (exec, expanded);
704 if (uris != NULL && expanded)
705 g_string_append_c (exec, ' ');
713 g_string_append (exec, "--icon ");
714 expanded = g_shell_quote (info->icon_name);
715 g_string_append (exec, expanded);
723 expanded = g_shell_quote (info->name);
724 g_string_append (exec, expanded);
732 expanded = g_shell_quote (info->filename);
733 g_string_append (exec, expanded);
738 case 'm': /* deprecated */
742 g_string_append_c (exec, '%');
750 expand_application_parameters (GDesktopAppInfo *info,
756 GList *uri_list = *uris;
757 const char *p = info->exec;
758 GString *expanded_exec;
761 if (info->exec == NULL)
763 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
764 _("Desktop file didn't specify Exec field"));
768 expanded_exec = g_string_new (NULL);
772 if (p[0] == '%' && p[1] != '\0')
774 expand_macro (p[1], expanded_exec, info, uris);
778 g_string_append_c (expanded_exec, *p);
783 /* No file substitutions */
784 if (uri_list == *uris && uri_list != NULL)
786 /* If there is no macro default to %f. This is also what KDE does */
787 g_string_append_c (expanded_exec, ' ');
788 expand_macro ('f', expanded_exec, info, uris);
791 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
792 g_string_free (expanded_exec, TRUE);
797 prepend_terminal_to_vector (int *argc,
804 char **term_argv = NULL;
809 g_return_val_if_fail (argc != NULL, FALSE);
810 g_return_val_if_fail (argv != NULL, FALSE);
818 /* compute size if not given */
821 for (i = 0; the_argv[i] != NULL; i++)
827 term_argv = g_new0 (char *, 3);
829 check = g_find_program_in_path ("gnome-terminal");
832 term_argv[0] = check;
833 /* Note that gnome-terminal takes -x and
834 * as -e in gnome-terminal is broken we use that. */
835 term_argv[1] = g_strdup ("-x");
840 check = g_find_program_in_path ("nxterm");
842 check = g_find_program_in_path ("color-xterm");
844 check = g_find_program_in_path ("rxvt");
846 check = g_find_program_in_path ("xterm");
848 check = g_find_program_in_path ("dtterm");
851 check = g_strdup ("xterm");
852 g_warning ("couldn't find a terminal, falling back to xterm");
854 term_argv[0] = check;
855 term_argv[1] = g_strdup ("-e");
858 real_argc = term_argc + *argc;
859 real_argv = g_new (char *, real_argc + 1);
861 for (i = 0; i < term_argc; i++)
862 real_argv[i] = term_argv[i];
864 for (j = 0; j < *argc; j++, i++)
865 real_argv[i] = (char *)the_argv[j];
873 /* we use g_free here as we sucked all the inner strings
874 * out from it into real_argv */
879 #endif /* G_OS_WIN32 */
883 uri_list_segment_to_files (GList *start,
890 while (start != NULL && start != end)
892 file = g_file_new_for_uri ((char *)start->data);
893 res = g_list_prepend (res, file);
897 return g_list_reverse (res);
908 child_setup (gpointer user_data)
910 ChildSetupData *data = user_data;
913 g_setenv ("DISPLAY", data->display, TRUE);
916 g_setenv ("DESKTOP_STARTUP_ID", data->sn_id, TRUE);
918 if (data->desktop_file)
922 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data->desktop_file, TRUE);
924 g_snprintf (pid, 20, "%ld", (long)getpid ());
925 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid, TRUE);
930 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
932 GAppLaunchContext *launch_context,
935 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
936 gboolean completed = FALSE;
938 GList *launched_files;
943 g_return_val_if_fail (appinfo != NULL, FALSE);
950 if (!expand_application_parameters (info, &uris,
951 &argc, &argv, error))
954 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
956 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
957 _("Unable to find terminal required for application"));
963 data.desktop_file = info->filename;
967 launched_files = uri_list_segment_to_files (old_uris, uris);
969 data.display = g_app_launch_context_get_display (launch_context,
973 if (info->startup_notify)
974 data.sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
977 g_list_foreach (launched_files, (GFunc)g_object_unref, NULL);
978 g_list_free (launched_files);
981 if (!g_spawn_async (info->path,
991 g_app_launch_context_launch_failed (launch_context, data.sn_id);
994 g_free (data.display);
1000 g_free (data.display);
1005 while (uris != NULL);
1016 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1018 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1020 return info->exec &&
1021 ((strstr (info->exec, "%u") != NULL) ||
1022 (strstr (info->exec, "%U") != NULL));
1026 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1028 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1030 return info->exec &&
1031 ((strstr (info->exec, "%f") != NULL) ||
1032 (strstr (info->exec, "%F") != NULL));
1036 g_desktop_app_info_launch (GAppInfo *appinfo,
1038 GAppLaunchContext *launch_context,
1048 uri = g_file_get_uri (files->data);
1049 uris = g_list_prepend (uris, uri);
1050 files = files->next;
1053 uris = g_list_reverse (uris);
1055 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1057 g_list_foreach (uris, (GFunc)g_free, NULL);
1063 G_LOCK_DEFINE_STATIC (g_desktop_env);
1064 static gchar *g_desktop_env = NULL;
1067 * g_desktop_app_info_set_desktop_env:
1068 * @desktop_env: a string specifying what desktop this is
1070 * Sets the name of the desktop that the application is running in.
1071 * This is used by g_app_info_should_show() to evaluate the
1072 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1073 * desktop entry fields.
1075 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1076 * Menu specification</ulink> recognizes the following:
1078 * <member>GNOME</member>
1079 * <member>KDE</member>
1080 * <member>ROX</member>
1081 * <member>XFCE</member>
1082 * <member>Old</member>
1085 * Should be called only once; subsequent calls are ignored.
1088 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1090 G_LOCK (g_desktop_env);
1092 g_desktop_env = g_strdup (desktop_env);
1093 G_UNLOCK (g_desktop_env);
1097 g_desktop_app_info_should_show (GAppInfo *appinfo)
1099 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1101 const gchar *desktop_env;
1104 if (info->nodisplay)
1107 G_LOCK (g_desktop_env);
1108 desktop_env = g_desktop_env;
1109 G_UNLOCK (g_desktop_env);
1111 if (info->only_show_in)
1113 if (desktop_env == NULL)
1117 for (i = 0; info->only_show_in[i] != NULL; i++)
1119 if (strcmp (info->only_show_in[i], desktop_env) == 0)
1129 if (info->not_show_in && desktop_env)
1131 for (i = 0; info->not_show_in[i] != NULL; i++)
1133 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1147 ensure_dir (DirType type,
1150 char *path, *display_name;
1153 if (type == APP_DIR)
1154 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1156 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1159 if (g_mkdir_with_parents (path, 0700) == 0)
1163 display_name = g_filename_display_name (path);
1164 if (type == APP_DIR)
1165 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1166 _("Can't create user application configuration folder %s: %s"),
1167 display_name, g_strerror (errsv));
1169 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1170 _("Can't create user MIME configuration folder %s: %s"),
1171 display_name, g_strerror (errsv));
1173 g_free (display_name);
1180 update_mimeapps_list (const char *desktop_id,
1181 const char *content_type,
1182 UpdateMimeFlags flags,
1185 char *dirname, *filename, *string;
1187 gboolean load_succeeded, res, explicit_default;
1188 char **old_list, **list;
1190 gsize length, data_size;
1193 char **content_types;
1195 /* Don't add both at start and end */
1196 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1197 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1199 dirname = ensure_dir (APP_DIR, error);
1203 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1206 key_file = g_key_file_new ();
1207 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1208 if (!load_succeeded || !g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP))
1210 g_key_file_free (key_file);
1211 key_file = g_key_file_new ();
1216 content_types = g_new (char *, 2);
1217 content_types[0] = g_strdup (content_type);
1218 content_types[1] = NULL;
1222 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1225 explicit_default = FALSE;
1227 for (k = 0; content_types && content_types[k]; k++)
1229 /* set as default, if requested so */
1230 string = g_key_file_get_string (key_file,
1231 DEFAULT_APPLICATIONS_GROUP,
1235 if (g_strcmp0 (string, desktop_id) != 0 &&
1236 (flags & UPDATE_MIME_SET_DEFAULT))
1239 string = g_strdup (desktop_id);
1241 /* add in the non-default list too, if it's not already there */
1242 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1245 if (string == NULL || desktop_id == NULL)
1246 g_key_file_remove_key (key_file,
1247 DEFAULT_APPLICATIONS_GROUP,
1252 g_key_file_set_string (key_file,
1253 DEFAULT_APPLICATIONS_GROUP,
1257 explicit_default = TRUE;
1265 /* reuse the list from above */
1269 g_strfreev (content_types);
1270 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1273 for (k = 0; content_types && content_types[k]; k++)
1275 /* Add to the right place in the list */
1278 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1279 content_types[k], &length, NULL);
1281 list = g_new (char *, 1 + length + 1);
1285 /* if we're adding a last-used hint, just put the application in front of the list */
1286 if (flags & UPDATE_MIME_SET_LAST_USED)
1288 /* avoid adding this again as non-default later */
1289 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1290 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1292 list[i++] = g_strdup (desktop_id);
1297 for (j = 0; old_list[j] != NULL; j++)
1299 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1301 /* rewrite other entries if they're different from the new one */
1302 list[i++] = g_strdup (old_list[j]);
1304 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1306 /* we encountered an old entry which is equal to the one we're adding as non-default,
1307 * don't change its position in the list.
1309 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1310 list[i++] = g_strdup (old_list[j]);
1315 /* add it at the end of the list */
1316 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1317 list[i++] = g_strdup (desktop_id);
1321 g_strfreev (old_list);
1323 if (list[0] == NULL || desktop_id == NULL)
1324 g_key_file_remove_key (key_file,
1325 ADDED_ASSOCIATIONS_GROUP,
1330 g_key_file_set_string_list (key_file,
1331 ADDED_ASSOCIATIONS_GROUP,
1333 (const char * const *)list, i);
1335 /* if we had no explicit default set, we should add the system default to the
1336 * list, to avoid overriding it with applications from this list.
1338 if (!explicit_default)
1340 system_list = get_all_desktop_entries_for_mime_type (content_type, (const char **) list, FALSE, NULL);
1342 if (system_list != NULL)
1344 string = system_list->data;
1346 g_key_file_set_string (key_file,
1347 DEFAULT_APPLICATIONS_GROUP,
1352 g_list_free_full (system_list, g_free);
1361 /* reuse the list from above */
1365 g_strfreev (content_types);
1366 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1369 for (k = 0; content_types && content_types[k]; k++)
1371 /* Remove from removed associations group (unless remove) */
1374 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1375 content_types[k], &length, NULL);
1377 list = g_new (char *, 1 + length + 1);
1380 if (flags & UPDATE_MIME_REMOVE)
1381 list[i++] = g_strdup (desktop_id);
1384 for (j = 0; old_list[j] != NULL; j++)
1386 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1387 list[i++] = g_strdup (old_list[j]);
1392 g_strfreev (old_list);
1394 if (list[0] == NULL || desktop_id == NULL)
1395 g_key_file_remove_key (key_file,
1396 REMOVED_ASSOCIATIONS_GROUP,
1400 g_key_file_set_string_list (key_file,
1401 REMOVED_ASSOCIATIONS_GROUP,
1403 (const char * const *)list, i);
1408 g_strfreev (content_types);
1410 data = g_key_file_to_data (key_file, &data_size, error);
1411 g_key_file_free (key_file);
1413 res = g_file_set_contents (filename, data, data_size, error);
1415 mime_info_cache_reload (NULL);
1424 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1425 const char *content_type,
1428 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1430 if (!g_desktop_app_info_ensure_saved (info, error))
1433 /* both add support for the content type and set as last used */
1434 return update_mimeapps_list (info->desktop_id, content_type,
1435 UPDATE_MIME_SET_NON_DEFAULT |
1436 UPDATE_MIME_SET_LAST_USED,
1441 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1442 const char *content_type,
1445 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1447 if (!g_desktop_app_info_ensure_saved (info, error))
1450 return update_mimeapps_list (info->desktop_id, content_type,
1451 UPDATE_MIME_SET_DEFAULT,
1456 update_program_done (GPid pid,
1460 /* Did the application exit correctly */
1461 if (WIFEXITED (status) &&
1462 WEXITSTATUS (status) == 0)
1464 /* Here we could clean out any caches in use */
1469 run_update_command (char *command,
1478 GError *error = NULL;
1481 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1483 if (g_spawn_async ("/", argv,
1485 G_SPAWN_SEARCH_PATH |
1486 G_SPAWN_STDOUT_TO_DEV_NULL |
1487 G_SPAWN_STDERR_TO_DEV_NULL |
1488 G_SPAWN_DO_NOT_REAP_CHILD,
1489 NULL, NULL, /* No setup function */
1492 g_child_watch_add (pid, update_program_done, NULL);
1495 /* If we get an error at this point, it's quite likely the user doesn't
1496 * have an installed copy of either 'update-mime-database' or
1497 * 'update-desktop-database'. I don't think we want to popup an error
1498 * dialog at this point, so we just do a g_warning to give the user a
1499 * chance of debugging it.
1501 g_warning ("%s", error->message);
1508 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1509 const char *extension,
1512 char *filename, *basename, *mimetype;
1516 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1519 dirname = ensure_dir (MIMETYPE_DIR, error);
1523 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1524 filename = g_build_filename (dirname, basename, NULL);
1528 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1530 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1535 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1536 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1537 " <mime-type type=\"%s\">\n"
1538 " <comment>%s document</comment>\n"
1539 " <glob pattern=\"*.%s\"/>\n"
1541 "</mime-info>\n", mimetype, extension, extension);
1543 g_file_set_contents (filename, contents, -1, NULL);
1546 run_update_command ("update-mime-database", "mime");
1550 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1560 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1561 const char *content_type,
1564 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1566 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1569 return update_mimeapps_list (info->desktop_id, content_type,
1570 UPDATE_MIME_SET_NON_DEFAULT,
1575 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1581 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1582 const char *content_type,
1585 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1587 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1590 return update_mimeapps_list (info->desktop_id, content_type,
1596 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1602 char *data, *desktop_id;
1607 if (info->filename != NULL)
1610 /* This is only used for object created with
1611 * g_app_info_create_from_commandline. All other
1612 * object should have a filename
1615 dirname = ensure_dir (APP_DIR, error);
1619 key_file = g_key_file_new ();
1621 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1622 "Encoding", "UTF-8");
1623 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1624 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1625 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1626 G_KEY_FILE_DESKTOP_KEY_TYPE,
1627 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1629 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1630 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1632 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1633 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
1635 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1636 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
1638 if (info->fullname != NULL)
1639 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1640 FULL_NAME_KEY, info->fullname);
1642 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1643 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
1645 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1646 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1648 data = g_key_file_to_data (key_file, &data_size, NULL);
1649 g_key_file_free (key_file);
1651 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
1652 filename = g_build_filename (dirname, desktop_id, NULL);
1653 g_free (desktop_id);
1656 fd = g_mkstemp (filename);
1661 display_name = g_filename_display_name (filename);
1662 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1663 _("Can't create user desktop file %s"), display_name);
1664 g_free (display_name);
1670 desktop_id = g_path_get_basename (filename);
1674 res = g_file_set_contents (filename, data, data_size, error);
1677 g_free (desktop_id);
1682 info->filename = filename;
1683 info->desktop_id = desktop_id;
1685 run_update_command ("update-desktop-database", "applications");
1691 g_desktop_app_info_can_delete (GAppInfo *appinfo)
1693 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1697 if (strstr (info->filename, "/userapp-"))
1698 return g_access (info->filename, W_OK) == 0;
1705 g_desktop_app_info_delete (GAppInfo *appinfo)
1707 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1711 if (g_remove (info->filename) == 0)
1713 update_mimeapps_list (info->desktop_id, NULL,
1717 g_free (info->filename);
1718 info->filename = NULL;
1719 g_free (info->desktop_id);
1720 info->desktop_id = NULL;
1730 * g_app_info_create_from_commandline:
1731 * @commandline: the commandline to use
1732 * @application_name: (allow-none): the application name, or %NULL to use @commandline
1733 * @flags: flags that can specify details of the created #GAppInfo
1734 * @error: a #GError location to store the error occuring, %NULL to ignore.
1736 * Creates a new #GAppInfo from the given information.
1738 * Returns: (transfer full): new #GAppInfo for given command.
1741 g_app_info_create_from_commandline (const char *commandline,
1742 const char *application_name,
1743 GAppInfoCreateFlags flags,
1748 GDesktopAppInfo *info;
1750 g_return_val_if_fail (commandline, NULL);
1752 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
1754 info->filename = NULL;
1755 info->desktop_id = NULL;
1757 info->terminal = flags & G_APP_INFO_CREATE_NEEDS_TERMINAL;
1758 info->startup_notify = flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION;
1759 info->hidden = FALSE;
1760 if (flags & G_APP_INFO_CREATE_SUPPORTS_URIS)
1761 info->exec = g_strconcat (commandline, " %u", NULL);
1763 info->exec = g_strconcat (commandline, " %f", NULL);
1764 info->nodisplay = TRUE;
1765 info->binary = binary_from_exec (info->exec);
1767 if (application_name)
1768 info->name = g_strdup (application_name);
1771 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1772 split = g_strsplit (commandline, " ", 2);
1773 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
1775 info->name = basename;
1776 if (info->name == NULL)
1777 info->name = g_strdup ("custom");
1779 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
1781 return G_APP_INFO (info);
1785 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1787 iface->dup = g_desktop_app_info_dup;
1788 iface->equal = g_desktop_app_info_equal;
1789 iface->get_id = g_desktop_app_info_get_id;
1790 iface->get_name = g_desktop_app_info_get_name;
1791 iface->get_description = g_desktop_app_info_get_description;
1792 iface->get_executable = g_desktop_app_info_get_executable;
1793 iface->get_icon = g_desktop_app_info_get_icon;
1794 iface->launch = g_desktop_app_info_launch;
1795 iface->supports_uris = g_desktop_app_info_supports_uris;
1796 iface->supports_files = g_desktop_app_info_supports_files;
1797 iface->launch_uris = g_desktop_app_info_launch_uris;
1798 iface->should_show = g_desktop_app_info_should_show;
1799 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1800 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1801 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1802 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1803 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1804 iface->can_delete = g_desktop_app_info_can_delete;
1805 iface->do_delete = g_desktop_app_info_delete;
1806 iface->get_commandline = g_desktop_app_info_get_commandline;
1807 iface->get_display_name = g_desktop_app_info_get_display_name;
1808 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
1812 app_info_in_list (GAppInfo *info,
1815 while (list != NULL)
1817 if (g_app_info_equal (info, list->data))
1825 * g_app_info_get_recommended_for_type:
1826 * @content_type: the content type to find a #GAppInfo for
1828 * Gets a list of recommended #GAppInfos for a given content type, i.e.
1829 * those applications which claim to support the given content type exactly,
1830 * and not by MIME type subclassing.
1831 * Note that the first application of the list is the last used one, i.e.
1832 * the last one for which #g_app_info_set_as_last_used_for_type has been
1835 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1836 * for given @content_type or %NULL on error.
1841 g_app_info_get_recommended_for_type (const gchar *content_type)
1843 GList *desktop_entries, *l;
1845 GDesktopAppInfo *info;
1847 g_return_val_if_fail (content_type != NULL, NULL);
1849 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
1852 for (l = desktop_entries; l != NULL; l = l->next)
1854 char *desktop_entry = l->data;
1856 info = g_desktop_app_info_new (desktop_entry);
1859 if (app_info_in_list (G_APP_INFO (info), infos))
1860 g_object_unref (info);
1862 infos = g_list_prepend (infos, info);
1864 g_free (desktop_entry);
1867 g_list_free (desktop_entries);
1869 return g_list_reverse (infos);
1873 * g_app_info_get_fallback_for_type:
1874 * @content_type: the content type to find a #GAppInfo for
1876 * Gets a list of fallback #GAppInfos for a given content type, i.e.
1877 * those applications which claim to support the given content type
1878 * by MIME type subclassing and not directly.
1880 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1881 * for given @content_type or %NULL on error.
1886 g_app_info_get_fallback_for_type (const gchar *content_type)
1888 GList *desktop_entries, *l;
1889 GList *infos, *recommended_infos;
1890 GDesktopAppInfo *info;
1892 g_return_val_if_fail (content_type != NULL, NULL);
1894 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
1895 recommended_infos = g_app_info_get_recommended_for_type (content_type);
1898 for (l = desktop_entries; l != NULL; l = l->next)
1900 char *desktop_entry = l->data;
1902 info = g_desktop_app_info_new (desktop_entry);
1905 if (app_info_in_list (G_APP_INFO (info), infos) ||
1906 app_info_in_list (G_APP_INFO (info), recommended_infos))
1907 g_object_unref (info);
1909 infos = g_list_prepend (infos, info);
1911 g_free (desktop_entry);
1914 g_list_free (desktop_entries);
1915 g_list_free_full (recommended_infos, g_object_unref);
1917 return g_list_reverse (infos);
1921 * g_app_info_get_all_for_type:
1922 * @content_type: the content type to find a #GAppInfo for
1924 * Gets a list of all #GAppInfos for a given content type.
1926 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
1927 * for given @content_type or %NULL on error.
1930 g_app_info_get_all_for_type (const char *content_type)
1932 GList *desktop_entries, *l;
1934 char *user_default = NULL;
1935 GDesktopAppInfo *info;
1937 g_return_val_if_fail (content_type != NULL, NULL);
1939 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
1942 /* put the user default in front of the list, for compatibility */
1943 if (user_default != NULL)
1945 info = g_desktop_app_info_new (user_default);
1948 infos = g_list_prepend (infos, info);
1951 g_free (user_default);
1953 for (l = desktop_entries; l != NULL; l = l->next)
1955 char *desktop_entry = l->data;
1957 info = g_desktop_app_info_new (desktop_entry);
1960 if (app_info_in_list (G_APP_INFO (info), infos))
1961 g_object_unref (info);
1963 infos = g_list_prepend (infos, info);
1965 g_free (desktop_entry);
1968 g_list_free (desktop_entries);
1970 return g_list_reverse (infos);
1974 * g_app_info_reset_type_associations:
1975 * @content_type: a content type
1977 * Removes all changes to the type associations done by
1978 * g_app_info_set_as_default_for_type(),
1979 * g_app_info_set_as_default_for_extension(),
1980 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
1985 g_app_info_reset_type_associations (const char *content_type)
1987 update_mimeapps_list (NULL, content_type,
1993 * g_app_info_get_default_for_type:
1994 * @content_type: the content type to find a #GAppInfo for
1995 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1998 * Gets the #GAppInfo that corresponds to a given content type.
2000 * Returns: #GAppInfo for given @content_type or %NULL on error.
2003 g_app_info_get_default_for_type (const char *content_type,
2004 gboolean must_support_uris)
2006 GList *desktop_entries, *l;
2007 char *user_default = NULL;
2010 g_return_val_if_fail (content_type != NULL, NULL);
2012 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2016 if (user_default != NULL)
2018 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2022 if (must_support_uris && !g_app_info_supports_uris (info))
2024 g_object_unref (info);
2030 g_free (user_default);
2034 g_list_free_full (desktop_entries, g_free);
2038 /* pick the first from the other list that matches our URI
2041 for (l = desktop_entries; l != NULL; l = l->next)
2043 char *desktop_entry = l->data;
2045 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2048 if (must_support_uris && !g_app_info_supports_uris (info))
2050 g_object_unref (info);
2058 g_list_free_full (desktop_entries, g_free);
2064 * g_app_info_get_default_for_uri_scheme:
2065 * @uri_scheme: a string containing a URI scheme.
2067 * Gets the default application for launching applications
2068 * using this URI scheme. A URI scheme is the initial part
2069 * of the URI, up to but not including the ':', e.g. "http",
2072 * Returns: #GAppInfo for given @uri_scheme or %NULL on error.
2075 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2078 char *content_type, *scheme_down;
2080 scheme_down = g_ascii_strdown (uri_scheme, -1);
2081 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2082 g_free (scheme_down);
2083 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2084 g_free (content_type);
2090 get_apps_from_dir (GHashTable *apps,
2091 const char *dirname,
2095 const char *basename;
2096 char *filename, *subprefix, *desktop_id;
2098 GDesktopAppInfo *appinfo;
2100 dir = g_dir_open (dirname, 0, NULL);
2103 while ((basename = g_dir_read_name (dir)) != NULL)
2105 filename = g_build_filename (dirname, basename, NULL);
2106 if (g_str_has_suffix (basename, ".desktop"))
2108 desktop_id = g_strconcat (prefix, basename, NULL);
2110 /* Use _extended so we catch NULLs too (hidden) */
2111 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2113 appinfo = g_desktop_app_info_new_from_filename (filename);
2116 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2118 g_object_unref (appinfo);
2123 if (appinfo || hidden)
2125 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2129 /* Reuse instead of strdup here */
2130 appinfo->desktop_id = desktop_id;
2135 g_free (desktop_id);
2139 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2141 subprefix = g_strconcat (prefix, basename, "-", NULL);
2142 get_apps_from_dir (apps, filename, subprefix);
2154 * g_app_info_get_all:
2156 * Gets a list of all of the applications currently registered
2159 * For desktop files, this includes applications that have
2160 * <literal>NoDisplay=true</literal> set or are excluded from
2161 * display by means of <literal>OnlyShowIn</literal> or
2162 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2163 * The returned list does not include applications which have
2164 * the <literal>Hidden</literal> key set.
2166 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2169 g_app_info_get_all (void)
2171 const char * const *dirs;
2173 GHashTableIter iter;
2178 dirs = get_applications_search_path ();
2180 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2184 for (i = 0; dirs[i] != NULL; i++)
2185 get_apps_from_dir (apps, dirs[i], "");
2189 g_hash_table_iter_init (&iter, apps);
2190 while (g_hash_table_iter_next (&iter, NULL, &value))
2193 infos = g_list_prepend (infos, value);
2196 g_hash_table_destroy (apps);
2198 return g_list_reverse (infos);
2201 /* Cacheing of mimeinfo.cache and defaults.list files */
2205 GHashTable *mime_info_cache_map;
2206 GHashTable *defaults_list_map;
2207 GHashTable *mimeapps_list_added_map;
2208 GHashTable *mimeapps_list_removed_map;
2209 GHashTable *mimeapps_list_defaults_map;
2210 time_t mime_info_cache_timestamp;
2211 time_t defaults_list_timestamp;
2212 time_t mimeapps_list_timestamp;
2216 GList *dirs; /* mimeinfo.cache and defaults.list */
2217 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2218 time_t last_stat_time;
2219 guint should_ping_mime_monitor : 1;
2222 static MimeInfoCache *mime_info_cache = NULL;
2223 G_LOCK_DEFINE_STATIC (mime_info_cache);
2225 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2226 const char *mime_type,
2227 char **new_desktop_file_ids);
2229 static MimeInfoCache * mime_info_cache_new (void);
2232 destroy_info_cache_value (gpointer key,
2236 g_list_foreach (value, (GFunc)g_free, NULL);
2237 g_list_free (value);
2241 destroy_info_cache_map (GHashTable *info_cache_map)
2243 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2244 g_hash_table_destroy (info_cache_map);
2248 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2249 const char *cache_file,
2255 filename = g_build_filename (dir->path, cache_file, NULL);
2257 if (g_stat (filename, &buf) < 0)
2264 if (buf.st_mtime != *timestamp)
2270 /* Call with lock held */
2272 remove_all (gpointer key,
2281 mime_info_cache_blow_global_cache (void)
2283 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2288 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2292 gchar *filename, **mime_types;
2299 if (dir->mime_info_cache_map != NULL &&
2300 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2301 &dir->mime_info_cache_timestamp))
2304 if (dir->mime_info_cache_map != NULL)
2305 destroy_info_cache_map (dir->mime_info_cache_map);
2307 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2308 (GDestroyNotify) g_free,
2311 key_file = g_key_file_new ();
2313 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2315 if (g_stat (filename, &buf) < 0)
2318 if (dir->mime_info_cache_timestamp > 0)
2319 mime_info_cache->should_ping_mime_monitor = TRUE;
2321 dir->mime_info_cache_timestamp = buf.st_mtime;
2323 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2328 if (load_error != NULL)
2331 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2334 if (load_error != NULL)
2337 for (i = 0; mime_types[i] != NULL; i++)
2339 gchar **desktop_file_ids;
2340 char *unaliased_type;
2341 desktop_file_ids = g_key_file_get_string_list (key_file,
2347 if (desktop_file_ids == NULL)
2350 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2351 mime_info_cache_dir_add_desktop_entries (dir,
2354 g_free (unaliased_type);
2356 g_strfreev (desktop_file_ids);
2359 g_strfreev (mime_types);
2360 g_key_file_free (key_file);
2365 g_key_file_free (key_file);
2367 if (mime_types != NULL)
2368 g_strfreev (mime_types);
2371 g_error_free (load_error);
2375 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2379 gchar *filename, **mime_types;
2380 char *unaliased_type;
2381 char **desktop_file_ids;
2388 if (dir->defaults_list_map != NULL &&
2389 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2390 &dir->defaults_list_timestamp))
2393 if (dir->defaults_list_map != NULL)
2394 g_hash_table_destroy (dir->defaults_list_map);
2395 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2396 g_free, (GDestroyNotify)g_strfreev);
2399 key_file = g_key_file_new ();
2401 filename = g_build_filename (dir->path, "defaults.list", NULL);
2402 if (g_stat (filename, &buf) < 0)
2405 if (dir->defaults_list_timestamp > 0)
2406 mime_info_cache->should_ping_mime_monitor = TRUE;
2408 dir->defaults_list_timestamp = buf.st_mtime;
2410 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2414 if (load_error != NULL)
2417 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2419 if (mime_types != NULL)
2421 for (i = 0; mime_types[i] != NULL; i++)
2423 desktop_file_ids = g_key_file_get_string_list (key_file,
2424 DEFAULT_APPLICATIONS_GROUP,
2428 if (desktop_file_ids == NULL)
2431 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2432 g_hash_table_replace (dir->defaults_list_map,
2437 g_strfreev (mime_types);
2440 g_key_file_free (key_file);
2445 g_key_file_free (key_file);
2447 if (mime_types != NULL)
2448 g_strfreev (mime_types);
2451 g_error_free (load_error);
2455 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2459 gchar *filename, **mime_types;
2460 char *unaliased_type;
2461 char **desktop_file_ids;
2469 if (dir->mimeapps_list_added_map != NULL &&
2470 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2471 &dir->mimeapps_list_timestamp))
2474 if (dir->mimeapps_list_added_map != NULL)
2475 g_hash_table_destroy (dir->mimeapps_list_added_map);
2476 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2477 g_free, (GDestroyNotify)g_strfreev);
2479 if (dir->mimeapps_list_removed_map != NULL)
2480 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2481 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2482 g_free, (GDestroyNotify)g_strfreev);
2484 if (dir->mimeapps_list_defaults_map != NULL)
2485 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2486 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2489 key_file = g_key_file_new ();
2491 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2492 if (g_stat (filename, &buf) < 0)
2495 if (dir->mimeapps_list_timestamp > 0)
2496 mime_info_cache->should_ping_mime_monitor = TRUE;
2498 dir->mimeapps_list_timestamp = buf.st_mtime;
2500 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2504 if (load_error != NULL)
2507 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2509 if (mime_types != NULL)
2511 for (i = 0; mime_types[i] != NULL; i++)
2513 desktop_file_ids = g_key_file_get_string_list (key_file,
2514 ADDED_ASSOCIATIONS_GROUP,
2518 if (desktop_file_ids == NULL)
2521 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2522 g_hash_table_replace (dir->mimeapps_list_added_map,
2527 g_strfreev (mime_types);
2530 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2532 if (mime_types != NULL)
2534 for (i = 0; mime_types[i] != NULL; i++)
2536 desktop_file_ids = g_key_file_get_string_list (key_file,
2537 REMOVED_ASSOCIATIONS_GROUP,
2541 if (desktop_file_ids == NULL)
2544 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2545 g_hash_table_replace (dir->mimeapps_list_removed_map,
2550 g_strfreev (mime_types);
2553 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2555 if (mime_types != NULL)
2557 for (i = 0; mime_types[i] != NULL; i++)
2559 desktop_id = g_key_file_get_string (key_file,
2560 DEFAULT_APPLICATIONS_GROUP,
2563 if (desktop_id == NULL)
2566 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2567 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2572 g_strfreev (mime_types);
2575 g_key_file_free (key_file);
2580 g_key_file_free (key_file);
2582 if (mime_types != NULL)
2583 g_strfreev (mime_types);
2586 g_error_free (load_error);
2589 static MimeInfoCacheDir *
2590 mime_info_cache_dir_new (const char *path)
2592 MimeInfoCacheDir *dir;
2594 dir = g_new0 (MimeInfoCacheDir, 1);
2595 dir->path = g_strdup (path);
2601 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2606 if (dir->mime_info_cache_map != NULL)
2608 destroy_info_cache_map (dir->mime_info_cache_map);
2609 dir->mime_info_cache_map = NULL;
2613 if (dir->defaults_list_map != NULL)
2615 g_hash_table_destroy (dir->defaults_list_map);
2616 dir->defaults_list_map = NULL;
2619 if (dir->mimeapps_list_added_map != NULL)
2621 g_hash_table_destroy (dir->mimeapps_list_added_map);
2622 dir->mimeapps_list_added_map = NULL;
2625 if (dir->mimeapps_list_removed_map != NULL)
2627 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2628 dir->mimeapps_list_removed_map = NULL;
2631 if (dir->mimeapps_list_defaults_map != NULL)
2633 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2634 dir->mimeapps_list_defaults_map = NULL;
2641 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2642 const char *mime_type,
2643 char **new_desktop_file_ids)
2645 GList *desktop_file_ids;
2648 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
2651 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2653 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
2654 desktop_file_ids = g_list_append (desktop_file_ids,
2655 g_strdup (new_desktop_file_ids[i]));
2658 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2662 mime_info_cache_init_dir_lists (void)
2664 const char * const *dirs;
2667 mime_info_cache = mime_info_cache_new ();
2669 dirs = get_applications_search_path ();
2671 for (i = 0; dirs[i] != NULL; i++)
2673 MimeInfoCacheDir *dir;
2675 dir = mime_info_cache_dir_new (dirs[i]);
2679 mime_info_cache_dir_init (dir);
2680 mime_info_cache_dir_init_defaults_list (dir);
2681 mime_info_cache_dir_init_mimeapps_list (dir);
2683 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2689 mime_info_cache_update_dir_lists (void)
2693 tmp = mime_info_cache->dirs;
2697 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2699 /* No need to do this if we had file monitors... */
2700 mime_info_cache_blow_global_cache ();
2701 mime_info_cache_dir_init (dir);
2702 mime_info_cache_dir_init_defaults_list (dir);
2703 mime_info_cache_dir_init_mimeapps_list (dir);
2710 mime_info_cache_init (void)
2712 G_LOCK (mime_info_cache);
2713 if (mime_info_cache == NULL)
2714 mime_info_cache_init_dir_lists ();
2720 if (now >= mime_info_cache->last_stat_time + 10)
2722 mime_info_cache_update_dir_lists ();
2723 mime_info_cache->last_stat_time = now;
2727 if (mime_info_cache->should_ping_mime_monitor)
2729 /* g_idle_add (emit_mime_changed, NULL); */
2730 mime_info_cache->should_ping_mime_monitor = FALSE;
2733 G_UNLOCK (mime_info_cache);
2736 static MimeInfoCache *
2737 mime_info_cache_new (void)
2739 MimeInfoCache *cache;
2741 cache = g_new0 (MimeInfoCache, 1);
2743 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2744 (GDestroyNotify) g_free,
2745 (GDestroyNotify) g_free);
2750 mime_info_cache_free (MimeInfoCache *cache)
2755 g_list_foreach (cache->dirs,
2756 (GFunc) mime_info_cache_dir_free,
2758 g_list_free (cache->dirs);
2759 g_hash_table_destroy (cache->global_defaults_cache);
2764 * mime_info_cache_reload:
2765 * @dir: directory path which needs reloading.
2767 * Reload the mime information for the @dir.
2770 mime_info_cache_reload (const char *dir)
2772 /* FIXME: just reload the dir that needs reloading,
2773 * don't blow the whole cache
2775 if (mime_info_cache != NULL)
2777 G_LOCK (mime_info_cache);
2778 mime_info_cache_free (mime_info_cache);
2779 mime_info_cache = NULL;
2780 G_UNLOCK (mime_info_cache);
2785 append_desktop_entry (GList *list,
2786 const char *desktop_entry,
2787 GList *removed_entries)
2789 /* Add if not already in list, and valid */
2790 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
2791 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
2792 list = g_list_prepend (list, g_strdup (desktop_entry));
2798 * get_all_desktop_entries_for_mime_type:
2799 * @mime_type: a mime type.
2800 * @except: NULL or a strv list
2802 * Returns all the desktop ids for @mime_type. The desktop files
2803 * are listed in an order so that default applications are listed before
2804 * non-default ones, and handlers for inherited mimetypes are listed
2805 * after the base ones.
2807 * Optionally doesn't list the desktop ids given in the @except
2809 * Return value: a #GList containing the desktop ids which claim
2810 * to handle @mime_type.
2813 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
2814 const char **except,
2815 gboolean include_fallback,
2816 char **explicit_default)
2818 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
2819 MimeInfoCacheDir *dir;
2820 char *mime_type, *default_entry = NULL;
2823 char **default_entries;
2824 char **removed_associations;
2829 mime_info_cache_init ();
2831 if (include_fallback)
2833 /* collect all ancestors */
2834 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2835 array = g_ptr_array_new ();
2836 for (i = 0; mime_types[i]; i++)
2837 g_ptr_array_add (array, mime_types[i]);
2838 g_free (mime_types);
2839 for (i = 0; i < array->len; i++)
2841 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
2842 for (j = 0; anc[j]; j++)
2844 for (k = 0; k < array->len; k++)
2846 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
2849 if (k == array->len) /* not found */
2850 g_ptr_array_add (array, g_strdup (anc[j]));
2854 g_ptr_array_add (array, NULL);
2855 mime_types = (char **)g_ptr_array_free (array, FALSE);
2859 mime_types = g_malloc0 (2 * sizeof (gchar *));
2860 mime_types[0] = g_strdup (base_mime_type);
2861 mime_types[1] = NULL;
2864 G_LOCK (mime_info_cache);
2866 removed_entries = NULL;
2867 desktop_entries = NULL;
2869 for (i = 0; except != NULL && except[i] != NULL; i++)
2870 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
2872 for (i = 0; mime_types[i] != NULL; i++)
2874 mime_type = mime_types[i];
2876 /* Go through all apps listed in user and system dirs */
2877 for (dir_list = mime_info_cache->dirs;
2879 dir_list = dir_list->next)
2881 dir = dir_list->data;
2883 /* Pick the explicit default application */
2884 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
2888 /* Save the default entry if it's the first one we encounter */
2889 if (default_entry == NULL)
2890 default_entry = g_strdup (entry);
2893 /* Then added associations from mimeapps.list */
2894 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
2895 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2896 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2898 /* Then removed associations from mimeapps.list */
2899 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
2900 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
2901 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
2903 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
2904 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2905 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2906 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
2909 /* Go through all entries that support the mimetype */
2910 for (dir_list = mime_info_cache->dirs;
2912 dir_list = dir_list->next)
2914 dir = dir_list->data;
2916 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2917 for (tmp = list; tmp != NULL; tmp = tmp->next)
2918 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
2922 G_UNLOCK (mime_info_cache);
2924 g_strfreev (mime_types);
2926 if (explicit_default != NULL)
2927 *explicit_default = default_entry;
2929 g_free (default_entry);
2931 g_list_foreach (removed_entries, (GFunc)g_free, NULL);
2932 g_list_free (removed_entries);
2934 desktop_entries = g_list_reverse (desktop_entries);
2936 return desktop_entries;
2939 /* GDesktopAppInfoLookup interface: */
2941 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
2942 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
2945 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
2950 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
2951 * @lookup: a #GDesktopAppInfoLookup
2952 * @uri_scheme: a string containing a URI scheme.
2954 * Gets the default application for launching applications
2955 * using this URI scheme for a particular GDesktopAppInfoLookup
2958 * The GDesktopAppInfoLookup interface and this function is used
2959 * to implement g_app_info_get_default_for_uri_scheme() backends
2960 * in a GIO module. There is no reason for applications to use it
2961 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
2963 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2965 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
2968 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
2969 const char *uri_scheme)
2971 GDesktopAppInfoLookupIface *iface;
2973 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
2975 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
2977 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);