regex: Update included PCRE to 8.30
[glib.git] / gio / gdesktopappinfo.c
blob449c1145965de896e37eff770cabec9724123bde
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>
24 #include "config.h"
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/wait.h>
31 #ifdef HAVE_CRT_EXTERNS_H
32 #include <crt_externs.h>
33 #endif
35 #include "gcontenttypeprivate.h"
36 #include "gdesktopappinfo.h"
37 #include "gfile.h"
38 #include "gioerror.h"
39 #include "gthemedicon.h"
40 #include "gfileicon.h"
41 #include <glib/gstdio.h>
42 #include "glibintl.h"
43 #include "giomodule-priv.h"
44 #include "gappinfo.h"
47 /**
48 * SECTION:gdesktopappinfo
49 * @title: GDesktopAppInfo
50 * @short_description: Application information from desktop files
51 * @include: gio/gdesktopappinfo.h
53 * #GDesktopAppInfo is an implementation of #GAppInfo based on
54 * desktop files.
56 * Note that <filename>&lt;gio/gdesktopappinfo.h&gt;</filename> belongs to
57 * the UNIX-specific GIO interfaces, thus you have to use the
58 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
61 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
62 #define ADDED_ASSOCIATIONS_GROUP "Added Associations"
63 #define REMOVED_ASSOCIATIONS_GROUP "Removed Associations"
64 #define MIME_CACHE_GROUP "MIME Cache"
65 #define GENERIC_NAME_KEY "GenericName"
66 #define FULL_NAME_KEY "X-GNOME-FullName"
67 #define KEYWORDS_KEY "Keywords"
69 enum {
70 PROP_0,
71 PROP_FILENAME
74 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
75 static GList * get_all_desktop_entries_for_mime_type (const char *base_mime_type,
76 const char **except,
77 gboolean include_fallback,
78 char **explicit_default);
79 static void mime_info_cache_reload (const char *dir);
80 static gboolean g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
81 GError **error);
83 /**
84 * GDesktopAppInfo:
86 * Information about an installed application from a desktop file.
88 struct _GDesktopAppInfo
90 GObject parent_instance;
92 char *desktop_id;
93 char *filename;
95 char *name;
96 char *generic_name;
97 char *fullname;
98 char *comment;
99 char *icon_name;
100 GIcon *icon;
101 char **keywords;
102 char **only_show_in;
103 char **not_show_in;
104 char *try_exec;
105 char *exec;
106 char *binary;
107 char *path;
108 char *categories;
110 guint nodisplay : 1;
111 guint hidden : 1;
112 guint terminal : 1;
113 guint startup_notify : 1;
114 guint no_fuse : 1;
115 /* FIXME: what about StartupWMClass ? */
118 typedef enum {
119 UPDATE_MIME_NONE = 1 << 0,
120 UPDATE_MIME_SET_DEFAULT = 1 << 1,
121 UPDATE_MIME_SET_NON_DEFAULT = 1 << 2,
122 UPDATE_MIME_REMOVE = 1 << 3,
123 UPDATE_MIME_SET_LAST_USED = 1 << 4,
124 } UpdateMimeFlags;
126 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
127 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
128 g_desktop_app_info_iface_init))
130 G_LOCK_DEFINE_STATIC (g_desktop_env);
131 static gchar *g_desktop_env = NULL;
133 static gpointer
134 search_path_init (gpointer data)
136 char **args = NULL;
137 const char * const *data_dirs;
138 const char *user_data_dir;
139 int i, length, j;
141 data_dirs = g_get_system_data_dirs ();
142 length = g_strv_length ((char **) data_dirs);
144 args = g_new (char *, length + 2);
146 j = 0;
147 user_data_dir = g_get_user_data_dir ();
148 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
149 for (i = 0; i < length; i++)
150 args[j++] = g_build_filename (data_dirs[i],
151 "applications", NULL);
152 args[j++] = NULL;
154 return args;
157 static const char * const *
158 get_applications_search_path (void)
160 static GOnce once_init = G_ONCE_INIT;
161 return g_once (&once_init, search_path_init, NULL);
164 static void
165 g_desktop_app_info_finalize (GObject *object)
167 GDesktopAppInfo *info;
169 info = G_DESKTOP_APP_INFO (object);
171 g_free (info->desktop_id);
172 g_free (info->filename);
173 g_free (info->name);
174 g_free (info->generic_name);
175 g_free (info->fullname);
176 g_free (info->comment);
177 g_free (info->icon_name);
178 if (info->icon)
179 g_object_unref (info->icon);
180 g_strfreev (info->keywords);
181 g_strfreev (info->only_show_in);
182 g_strfreev (info->not_show_in);
183 g_free (info->try_exec);
184 g_free (info->exec);
185 g_free (info->binary);
186 g_free (info->path);
187 g_free (info->categories);
189 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
192 static void
193 g_desktop_app_info_set_property(GObject *object,
194 guint prop_id,
195 const GValue *value,
196 GParamSpec *pspec)
198 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
200 switch (prop_id)
202 case PROP_FILENAME:
203 self->filename = g_value_dup_string (value);
204 break;
206 default:
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 break;
212 static void
213 g_desktop_app_info_get_property(GObject *object,
214 guint prop_id,
215 GValue *value,
216 GParamSpec *pspec)
218 GDesktopAppInfo *self = G_DESKTOP_APP_INFO (object);
220 switch (prop_id)
222 case PROP_FILENAME:
223 g_value_set_string (value, self->filename);
224 break;
225 default:
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 break;
231 static void
232 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
234 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 gobject_class->get_property = g_desktop_app_info_get_property;
237 gobject_class->set_property = g_desktop_app_info_set_property;
238 gobject_class->finalize = g_desktop_app_info_finalize;
241 * GDesktopAppInfo:filename
243 * The origin filename of this #GDesktopAppInfo
245 g_object_class_install_property (gobject_class,
246 PROP_FILENAME,
247 g_param_spec_string ("filename", "Filename", "",
248 NULL,
249 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
252 static void
253 g_desktop_app_info_init (GDesktopAppInfo *local)
257 static char *
258 binary_from_exec (const char *exec)
260 const char *p, *start;
262 p = exec;
263 while (*p == ' ')
264 p++;
265 start = p;
266 while (*p != ' ' && *p != 0)
267 p++;
269 return g_strndup (start, p - start);
273 static gboolean
274 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
275 GKeyFile *key_file)
277 char *start_group;
278 char *type;
279 char *try_exec;
281 start_group = g_key_file_get_start_group (key_file);
282 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
284 g_free (start_group);
285 return FALSE;
287 g_free (start_group);
289 type = g_key_file_get_string (key_file,
290 G_KEY_FILE_DESKTOP_GROUP,
291 G_KEY_FILE_DESKTOP_KEY_TYPE,
292 NULL);
293 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
295 g_free (type);
296 return FALSE;
298 g_free (type);
300 try_exec = g_key_file_get_string (key_file,
301 G_KEY_FILE_DESKTOP_GROUP,
302 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
303 NULL);
304 if (try_exec && try_exec[0] != '\0')
306 char *t;
307 t = g_find_program_in_path (try_exec);
308 if (t == NULL)
310 g_free (try_exec);
311 return FALSE;
313 g_free (t);
316 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
317 info->generic_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, GENERIC_NAME_KEY, NULL, NULL);
318 info->fullname = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, FULL_NAME_KEY, NULL, NULL);
319 info->keywords = g_key_file_get_locale_string_list (key_file, G_KEY_FILE_DESKTOP_GROUP, KEYWORDS_KEY, NULL, NULL, NULL);
320 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
321 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
322 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
323 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);
324 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);
325 info->try_exec = try_exec;
326 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
327 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
328 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
329 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
330 info->no_fuse = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GIO-NoFuse", NULL) != FALSE;
331 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
332 info->categories = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_CATEGORIES, NULL);
334 info->icon = NULL;
335 if (info->icon_name)
337 if (g_path_is_absolute (info->icon_name))
339 GFile *file;
341 file = g_file_new_for_path (info->icon_name);
342 info->icon = g_file_icon_new (file);
343 g_object_unref (file);
345 else
347 char *p;
349 /* Work around a common mistake in desktop files */
350 if ((p = strrchr (info->icon_name, '.')) != NULL &&
351 (strcmp (p, ".png") == 0 ||
352 strcmp (p, ".xpm") == 0 ||
353 strcmp (p, ".svg") == 0))
354 *p = 0;
356 info->icon = g_themed_icon_new (info->icon_name);
360 if (info->exec)
361 info->binary = binary_from_exec (info->exec);
363 if (info->path && info->path[0] == '\0')
365 g_free (info->path);
366 info->path = NULL;
369 return TRUE;
372 static gboolean
373 g_desktop_app_info_load_file (GDesktopAppInfo *self)
375 GKeyFile *key_file;
376 gboolean retval = FALSE;
378 g_return_val_if_fail (self->filename != NULL, FALSE);
380 key_file = g_key_file_new ();
382 if (g_key_file_load_from_file (key_file,
383 self->filename,
384 G_KEY_FILE_NONE,
385 NULL))
387 retval = g_desktop_app_info_load_from_keyfile (self, key_file);
390 g_key_file_free (key_file);
391 return retval;
395 * g_desktop_app_info_new_from_keyfile:
396 * @key_file: an opened #GKeyFile
398 * Creates a new #GDesktopAppInfo.
400 * Returns: a new #GDesktopAppInfo or %NULL on error.
402 * Since: 2.18
404 GDesktopAppInfo *
405 g_desktop_app_info_new_from_keyfile (GKeyFile *key_file)
407 GDesktopAppInfo *info;
409 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
410 info->filename = NULL;
411 if (!g_desktop_app_info_load_from_keyfile (info, key_file))
413 g_object_unref (info);
414 return NULL;
416 return info;
420 * g_desktop_app_info_new_from_filename:
421 * @filename: the path of a desktop file, in the GLib filename encoding
423 * Creates a new #GDesktopAppInfo.
425 * Returns: a new #GDesktopAppInfo or %NULL on error.
427 GDesktopAppInfo *
428 g_desktop_app_info_new_from_filename (const char *filename)
430 GDesktopAppInfo *info = NULL;
432 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, "filename", filename, NULL);
433 if (!g_desktop_app_info_load_file (info))
435 g_object_unref (info);
436 return NULL;
438 return info;
442 * g_desktop_app_info_new:
443 * @desktop_id: the desktop file id
445 * Creates a new #GDesktopAppInfo based on a desktop file id.
447 * A desktop file id is the basename of the desktop file, including the
448 * .desktop extension. GIO is looking for a desktop file with this name
449 * in the <filename>applications</filename> subdirectories of the XDG data
450 * directories (i.e. the directories specified in the
451 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
452 * variables). GIO also supports the prefix-to-subdirectory mapping that is
453 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
454 * (i.e. a desktop id of kde-foo.desktop will match
455 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
457 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
459 GDesktopAppInfo *
460 g_desktop_app_info_new (const char *desktop_id)
462 GDesktopAppInfo *appinfo;
463 const char * const *dirs;
464 char *basename;
465 int i;
467 dirs = get_applications_search_path ();
469 basename = g_strdup (desktop_id);
471 for (i = 0; dirs[i] != NULL; i++)
473 char *filename;
474 char *p;
476 filename = g_build_filename (dirs[i], desktop_id, NULL);
477 appinfo = g_desktop_app_info_new_from_filename (filename);
478 g_free (filename);
479 if (appinfo != NULL)
480 goto found;
482 p = basename;
483 while ((p = strchr (p, '-')) != NULL)
485 *p = '/';
487 filename = g_build_filename (dirs[i], basename, NULL);
488 appinfo = g_desktop_app_info_new_from_filename (filename);
489 g_free (filename);
490 if (appinfo != NULL)
491 goto found;
492 *p = '-';
493 p++;
497 g_free (basename);
498 return NULL;
500 found:
501 g_free (basename);
503 appinfo->desktop_id = g_strdup (desktop_id);
505 if (g_desktop_app_info_get_is_hidden (appinfo))
507 g_object_unref (appinfo);
508 appinfo = NULL;
511 return appinfo;
514 static GAppInfo *
515 g_desktop_app_info_dup (GAppInfo *appinfo)
517 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
518 GDesktopAppInfo *new_info;
520 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
522 new_info->filename = g_strdup (info->filename);
523 new_info->desktop_id = g_strdup (info->desktop_id);
525 new_info->name = g_strdup (info->name);
526 new_info->generic_name = g_strdup (info->generic_name);
527 new_info->fullname = g_strdup (info->fullname);
528 new_info->keywords = g_strdupv (info->keywords);
529 new_info->comment = g_strdup (info->comment);
530 new_info->nodisplay = info->nodisplay;
531 new_info->icon_name = g_strdup (info->icon_name);
532 if (info->icon)
533 new_info->icon = g_object_ref (info->icon);
534 new_info->only_show_in = g_strdupv (info->only_show_in);
535 new_info->not_show_in = g_strdupv (info->not_show_in);
536 new_info->try_exec = g_strdup (info->try_exec);
537 new_info->exec = g_strdup (info->exec);
538 new_info->binary = g_strdup (info->binary);
539 new_info->path = g_strdup (info->path);
540 new_info->hidden = info->hidden;
541 new_info->terminal = info->terminal;
542 new_info->startup_notify = info->startup_notify;
544 return G_APP_INFO (new_info);
547 static gboolean
548 g_desktop_app_info_equal (GAppInfo *appinfo1,
549 GAppInfo *appinfo2)
551 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
552 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
554 if (info1->desktop_id == NULL ||
555 info2->desktop_id == NULL)
556 return info1 == info2;
558 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
561 static const char *
562 g_desktop_app_info_get_id (GAppInfo *appinfo)
564 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
566 return info->desktop_id;
569 static const char *
570 g_desktop_app_info_get_name (GAppInfo *appinfo)
572 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
574 if (info->name == NULL)
575 return _("Unnamed");
576 return info->name;
579 static const char *
580 g_desktop_app_info_get_display_name (GAppInfo *appinfo)
582 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
584 if (info->fullname == NULL)
585 return g_desktop_app_info_get_name (appinfo);
586 return info->fullname;
590 * g_desktop_app_info_get_is_hidden:
591 * @info: a #GDesktopAppInfo.
593 * A desktop file is hidden if the Hidden key in it is
594 * set to True.
596 * Returns: %TRUE if hidden, %FALSE otherwise.
598 gboolean
599 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
601 return info->hidden;
605 * g_desktop_app_info_get_filename:
606 * @info: a #GDesktopAppInfo
608 * When @info was created from a known filename, return it. In some
609 * situations such as the #GDesktopAppInfo returned from
610 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
612 * Returns: The full path to the file for @info, or %NULL if not known.
613 * Since: 2.24
615 const char *
616 g_desktop_app_info_get_filename (GDesktopAppInfo *info)
618 return info->filename;
621 static const char *
622 g_desktop_app_info_get_description (GAppInfo *appinfo)
624 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
626 return info->comment;
629 static const char *
630 g_desktop_app_info_get_executable (GAppInfo *appinfo)
632 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
634 return info->binary;
637 static const char *
638 g_desktop_app_info_get_commandline (GAppInfo *appinfo)
640 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
642 return info->exec;
645 static GIcon *
646 g_desktop_app_info_get_icon (GAppInfo *appinfo)
648 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
650 return info->icon;
654 * g_desktop_app_info_get_categories:
655 * @info: a #GDesktopAppInfo
657 * Gets the categories from the desktop file.
659 * Returns: The unparsed Categories key from the desktop file;
660 * i.e. no attempt is made to split it by ';' or validate it.
662 const char *
663 g_desktop_app_info_get_categories (GDesktopAppInfo *info)
665 return info->categories;
669 * g_desktop_app_info_get_keywords:
670 * @info: a #GDesktopAppInfo
672 * Gets the keywords from the desktop file.
674 * Returns: (transfer none): The value of the Keywords key
676 * Since: 2.32
678 const char * const *
679 g_desktop_app_info_get_keywords (GDesktopAppInfo *info)
681 return (const char * const *)info->keywords;
685 * g_desktop_app_info_get_generic_name:
686 * @info: a #GDesktopAppInfo
688 * Gets the generic name from the destkop file.
690 * Returns: The value of the GenericName key
692 const char *
693 g_desktop_app_info_get_generic_name (GDesktopAppInfo *info)
695 return info->generic_name;
699 * g_desktop_app_info_get_nodisplay:
700 * @info: a #GDesktopAppInfo
702 * Gets the value of the NoDisplay key, which helps determine if the
703 * application info should be shown in menus. See
704 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
706 * Returns: The value of the NoDisplay key
708 * Since: 2.30
710 gboolean
711 g_desktop_app_info_get_nodisplay (GDesktopAppInfo *info)
713 return info->nodisplay;
717 * g_desktop_app_info_get_show_in:
718 * @info: a #GDesktopAppInfo
719 * @desktop_env: a string specifying a desktop name
721 * Checks if the application info should be shown in menus that list available
722 * applications for a specific name of the desktop, based on the
723 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys.
725 * If @desktop_env is %NULL, then the name of the desktop set with
726 * g_desktop_app_info_set_desktop_env() is used.
728 * Note that g_app_info_should_show() for @info will include this check (with
729 * %NULL for @desktop_env) as well as additional checks.
731 * Returns: %TRUE if the @info should be shown in @desktop_env according to the
732 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal> keys, %FALSE
733 * otherwise.
735 * Since: 2.30
737 gboolean
738 g_desktop_app_info_get_show_in (GDesktopAppInfo *info,
739 const gchar *desktop_env)
741 gboolean found;
742 int i;
744 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO (info), FALSE);
746 if (!desktop_env) {
747 G_LOCK (g_desktop_env);
748 desktop_env = g_desktop_env;
749 G_UNLOCK (g_desktop_env);
752 if (info->only_show_in)
754 if (desktop_env == NULL)
755 return FALSE;
757 found = FALSE;
758 for (i = 0; info->only_show_in[i] != NULL; i++)
760 if (strcmp (info->only_show_in[i], desktop_env) == 0)
762 found = TRUE;
763 break;
766 if (!found)
767 return FALSE;
770 if (info->not_show_in && desktop_env)
772 for (i = 0; info->not_show_in[i] != NULL; i++)
774 if (strcmp (info->not_show_in[i], desktop_env) == 0)
775 return FALSE;
779 return TRUE;
782 static char *
783 expand_macro_single (char macro, char *uri)
785 GFile *file;
786 char *result = NULL;
787 char *path, *name;
789 file = g_file_new_for_uri (uri);
790 path = g_file_get_path (file);
791 g_object_unref (file);
793 switch (macro)
795 case 'u':
796 case 'U':
797 result = g_shell_quote (uri);
798 break;
799 case 'f':
800 case 'F':
801 if (path)
802 result = g_shell_quote (path);
803 break;
804 case 'd':
805 case 'D':
806 if (path)
808 name = g_path_get_dirname (path);
809 result = g_shell_quote (name);
810 g_free (name);
812 break;
813 case 'n':
814 case 'N':
815 if (path)
817 name = g_path_get_basename (path);
818 result = g_shell_quote (name);
819 g_free (name);
821 break;
824 g_free (path);
826 return result;
829 static void
830 expand_macro (char macro,
831 GString *exec,
832 GDesktopAppInfo *info,
833 GList **uri_list)
835 GList *uris = *uri_list;
836 char *expanded;
837 gboolean force_file_uri;
838 char force_file_uri_macro;
839 char *uri;
841 g_return_if_fail (exec != NULL);
843 /* On %u and %U, pass POSIX file path pointing to the URI via
844 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
845 * running or the URI doesn't have a POSIX file path via FUSE
846 * we'll just pass the URI.
848 force_file_uri_macro = macro;
849 force_file_uri = FALSE;
850 if (!info->no_fuse)
852 switch (macro)
854 case 'u':
855 force_file_uri_macro = 'f';
856 force_file_uri = TRUE;
857 break;
858 case 'U':
859 force_file_uri_macro = 'F';
860 force_file_uri = TRUE;
861 break;
862 default:
863 break;
867 switch (macro)
869 case 'u':
870 case 'f':
871 case 'd':
872 case 'n':
873 if (uris)
875 uri = uris->data;
876 if (!force_file_uri ||
877 /* Pass URI if it contains an anchor */
878 strchr (uri, '#') != NULL)
880 expanded = expand_macro_single (macro, uri);
882 else
884 expanded = expand_macro_single (force_file_uri_macro, uri);
885 if (expanded == NULL)
886 expanded = expand_macro_single (macro, uri);
889 if (expanded)
891 g_string_append (exec, expanded);
892 g_free (expanded);
894 uris = uris->next;
897 break;
899 case 'U':
900 case 'F':
901 case 'D':
902 case 'N':
903 while (uris)
905 uri = uris->data;
907 if (!force_file_uri ||
908 /* Pass URI if it contains an anchor */
909 strchr (uri, '#') != NULL)
911 expanded = expand_macro_single (macro, uri);
913 else
915 expanded = expand_macro_single (force_file_uri_macro, uri);
916 if (expanded == NULL)
917 expanded = expand_macro_single (macro, uri);
920 if (expanded)
922 g_string_append (exec, expanded);
923 g_free (expanded);
926 uris = uris->next;
928 if (uris != NULL && expanded)
929 g_string_append_c (exec, ' ');
932 break;
934 case 'i':
935 if (info->icon_name)
937 g_string_append (exec, "--icon ");
938 expanded = g_shell_quote (info->icon_name);
939 g_string_append (exec, expanded);
940 g_free (expanded);
942 break;
944 case 'c':
945 if (info->name)
947 expanded = g_shell_quote (info->name);
948 g_string_append (exec, expanded);
949 g_free (expanded);
951 break;
953 case 'k':
954 if (info->filename)
956 expanded = g_shell_quote (info->filename);
957 g_string_append (exec, expanded);
958 g_free (expanded);
960 break;
962 case 'm': /* deprecated */
963 break;
965 case '%':
966 g_string_append_c (exec, '%');
967 break;
970 *uri_list = uris;
973 static gboolean
974 expand_application_parameters (GDesktopAppInfo *info,
975 GList **uris,
976 int *argc,
977 char ***argv,
978 GError **error)
980 GList *uri_list = *uris;
981 const char *p = info->exec;
982 GString *expanded_exec;
983 gboolean res;
985 if (info->exec == NULL)
987 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
988 _("Desktop file didn't specify Exec field"));
989 return FALSE;
992 expanded_exec = g_string_new (NULL);
994 while (*p)
996 if (p[0] == '%' && p[1] != '\0')
998 expand_macro (p[1], expanded_exec, info, uris);
999 p++;
1001 else
1002 g_string_append_c (expanded_exec, *p);
1004 p++;
1007 /* No file substitutions */
1008 if (uri_list == *uris && uri_list != NULL)
1010 /* If there is no macro default to %f. This is also what KDE does */
1011 g_string_append_c (expanded_exec, ' ');
1012 expand_macro ('f', expanded_exec, info, uris);
1015 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
1016 g_string_free (expanded_exec, TRUE);
1017 return res;
1020 static gboolean
1021 prepend_terminal_to_vector (int *argc,
1022 char ***argv)
1024 #ifndef G_OS_WIN32
1025 char **real_argv;
1026 int real_argc;
1027 int i, j;
1028 char **term_argv = NULL;
1029 int term_argc = 0;
1030 char *check;
1031 char **the_argv;
1033 g_return_val_if_fail (argc != NULL, FALSE);
1034 g_return_val_if_fail (argv != NULL, FALSE);
1036 /* sanity */
1037 if(*argv == NULL)
1038 *argc = 0;
1040 the_argv = *argv;
1042 /* compute size if not given */
1043 if (*argc < 0)
1045 for (i = 0; the_argv[i] != NULL; i++)
1047 *argc = i;
1050 term_argc = 2;
1051 term_argv = g_new0 (char *, 3);
1053 check = g_find_program_in_path ("gnome-terminal");
1054 if (check != NULL)
1056 term_argv[0] = check;
1057 /* Note that gnome-terminal takes -x and
1058 * as -e in gnome-terminal is broken we use that. */
1059 term_argv[1] = g_strdup ("-x");
1061 else
1063 if (check == NULL)
1064 check = g_find_program_in_path ("nxterm");
1065 if (check == NULL)
1066 check = g_find_program_in_path ("color-xterm");
1067 if (check == NULL)
1068 check = g_find_program_in_path ("rxvt");
1069 if (check == NULL)
1070 check = g_find_program_in_path ("xterm");
1071 if (check == NULL)
1072 check = g_find_program_in_path ("dtterm");
1073 if (check == NULL)
1075 check = g_strdup ("xterm");
1076 g_warning ("couldn't find a terminal, falling back to xterm");
1078 term_argv[0] = check;
1079 term_argv[1] = g_strdup ("-e");
1082 real_argc = term_argc + *argc;
1083 real_argv = g_new (char *, real_argc + 1);
1085 for (i = 0; i < term_argc; i++)
1086 real_argv[i] = term_argv[i];
1088 for (j = 0; j < *argc; j++, i++)
1089 real_argv[i] = (char *)the_argv[j];
1091 real_argv[i] = NULL;
1093 g_free (*argv);
1094 *argv = real_argv;
1095 *argc = real_argc;
1097 /* we use g_free here as we sucked all the inner strings
1098 * out from it into real_argv */
1099 g_free (term_argv);
1100 return TRUE;
1101 #else
1102 return FALSE;
1103 #endif /* G_OS_WIN32 */
1106 static GList *
1107 create_files_for_uris (GList *uris)
1109 GList *res;
1110 GList *iter;
1112 res = NULL;
1114 for (iter = uris; iter; iter = iter->next)
1116 GFile *file = g_file_new_for_uri ((char *)iter->data);
1117 res = g_list_prepend (res, file);
1120 return g_list_reverse (res);
1123 typedef struct
1125 GSpawnChildSetupFunc user_setup;
1126 gpointer user_setup_data;
1128 char *pid_envvar;
1129 } ChildSetupData;
1131 static void
1132 child_setup (gpointer user_data)
1134 ChildSetupData *data = user_data;
1136 if (data->pid_envvar)
1138 pid_t pid = getpid ();
1139 char buf[20];
1140 int i;
1142 /* Write the pid into the space already reserved for it in the
1143 * environment array. We can't use sprintf because it might
1144 * malloc, so we do it by hand. It's simplest to write the pid
1145 * out backwards first, then copy it over.
1147 for (i = 0; pid; i++, pid /= 10)
1148 buf[i] = (pid % 10) + '0';
1149 for (i--; i >= 0; i--)
1150 *(data->pid_envvar++) = buf[i];
1151 *data->pid_envvar = '\0';
1154 if (data->user_setup)
1155 data->user_setup (data->user_setup_data);
1158 static void
1159 notify_desktop_launch (GDBusConnection *session_bus,
1160 GDesktopAppInfo *info,
1161 long pid,
1162 const char *display,
1163 const char *sn_id,
1164 GList *uris)
1166 GDBusMessage *msg;
1167 GVariantBuilder uri_variant;
1168 GVariantBuilder extras_variant;
1169 GList *iter;
1170 const char *desktop_file_id;
1171 const char *gio_desktop_file;
1173 if (session_bus == NULL)
1174 return;
1176 g_variant_builder_init (&uri_variant, G_VARIANT_TYPE ("as"));
1177 for (iter = uris; iter; iter = iter->next)
1178 g_variant_builder_add (&uri_variant, "s", iter->data);
1180 g_variant_builder_init (&extras_variant, G_VARIANT_TYPE ("a{sv}"));
1181 if (sn_id != NULL && g_utf8_validate (sn_id, -1, NULL))
1182 g_variant_builder_add (&extras_variant, "{sv}",
1183 "startup-id",
1184 g_variant_new ("s",
1185 sn_id));
1186 gio_desktop_file = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1187 if (gio_desktop_file != NULL)
1188 g_variant_builder_add (&extras_variant, "{sv}",
1189 "origin-desktop-file",
1190 g_variant_new_bytestring (gio_desktop_file));
1191 if (g_get_prgname () != NULL)
1192 g_variant_builder_add (&extras_variant, "{sv}",
1193 "origin-prgname",
1194 g_variant_new_bytestring (g_get_prgname ()));
1195 g_variant_builder_add (&extras_variant, "{sv}",
1196 "origin-pid",
1197 g_variant_new ("x",
1198 (gint64)getpid ()));
1200 if (info->filename)
1201 desktop_file_id = info->filename;
1202 else if (info->desktop_id)
1203 desktop_file_id = info->desktop_id;
1204 else
1205 desktop_file_id = "";
1207 msg = g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1208 "org.gtk.gio.DesktopAppInfo",
1209 "Launched");
1210 g_dbus_message_set_body (msg, g_variant_new ("(@aysxasa{sv})",
1211 g_variant_new_bytestring (desktop_file_id),
1212 display ? display : "",
1213 (gint64)pid,
1214 &uri_variant,
1215 &extras_variant));
1216 g_dbus_connection_send_message (session_bus,
1217 msg, 0,
1218 NULL,
1219 NULL);
1220 g_object_unref (msg);
1223 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1225 static gboolean
1226 _g_desktop_app_info_launch_uris_internal (GAppInfo *appinfo,
1227 GList *uris,
1228 GAppLaunchContext *launch_context,
1229 GSpawnFlags spawn_flags,
1230 GSpawnChildSetupFunc user_setup,
1231 gpointer user_setup_data,
1232 GDesktopAppLaunchCallback pid_callback,
1233 gpointer pid_callback_data,
1234 GError **error)
1236 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1237 GDBusConnection *session_bus;
1238 gboolean completed = FALSE;
1239 GList *old_uris;
1240 char **argv, **envp;
1241 int argc;
1242 ChildSetupData data;
1244 g_return_val_if_fail (appinfo != NULL, FALSE);
1246 argv = NULL;
1248 session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
1250 if (launch_context)
1251 envp = g_app_launch_context_get_environment (launch_context);
1252 else
1253 envp = g_get_environ ();
1257 GPid pid;
1258 GList *launched_uris;
1259 GList *iter;
1260 char *display, *sn_id;
1262 old_uris = uris;
1263 if (!expand_application_parameters (info, &uris,
1264 &argc, &argv, error))
1265 goto out;
1267 /* Get the subset of URIs we're launching with this process */
1268 launched_uris = NULL;
1269 for (iter = old_uris; iter != NULL && iter != uris; iter = iter->next)
1270 launched_uris = g_list_prepend (launched_uris, iter->data);
1271 launched_uris = g_list_reverse (launched_uris);
1273 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
1275 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1276 _("Unable to find terminal required for application"));
1277 goto out;
1280 data.user_setup = user_setup;
1281 data.user_setup_data = user_setup_data;
1283 if (info->filename)
1285 envp = g_environ_setenv (envp,
1286 "GIO_LAUNCHED_DESKTOP_FILE",
1287 info->filename,
1288 TRUE);
1289 envp = g_environ_setenv (envp,
1290 "GIO_LAUNCHED_DESKTOP_FILE_PID",
1291 "XXXXXXXXXXXXXXXXXXXX", /* filled in child_setup */
1292 TRUE);
1293 data.pid_envvar = (char *)g_environ_getenv (envp, "GIO_LAUNCHED_DESKTOP_FILE_PID");
1295 else
1297 data.pid_envvar = NULL;
1300 display = NULL;
1301 sn_id = NULL;
1302 if (launch_context)
1304 GList *launched_files = create_files_for_uris (launched_uris);
1306 display = g_app_launch_context_get_display (launch_context,
1307 appinfo,
1308 launched_files);
1309 envp = g_environ_setenv (envp, "DISPLAY", display, TRUE);
1311 if (info->startup_notify)
1313 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
1314 appinfo,
1315 launched_files);
1316 envp = g_environ_setenv (envp, "DESKTOP_STARTUP_ID", sn_id, TRUE);
1319 g_list_free_full (launched_files, g_object_unref);
1322 if (!g_spawn_async (info->path,
1323 argv,
1324 envp,
1325 spawn_flags,
1326 child_setup,
1327 &data,
1328 &pid,
1329 error))
1331 if (sn_id)
1332 g_app_launch_context_launch_failed (launch_context, sn_id);
1334 g_free (display);
1335 g_free (sn_id);
1336 g_list_free (launched_uris);
1338 goto out;
1341 if (pid_callback != NULL)
1342 pid_callback (info, pid, pid_callback_data);
1344 notify_desktop_launch (session_bus,
1345 info,
1346 pid,
1347 display,
1348 sn_id,
1349 launched_uris);
1351 g_free (display);
1352 g_free (sn_id);
1353 g_list_free (launched_uris);
1355 g_strfreev (argv);
1356 argv = NULL;
1358 while (uris != NULL);
1360 /* TODO - need to handle the process exiting immediately
1361 * after launching an app. See http://bugzilla.gnome.org/606960
1363 if (session_bus != NULL)
1365 /* This asynchronous flush holds a reference until it completes,
1366 * which ensures that the following unref won't immediately kill
1367 * the connection if we were the initial owner.
1369 g_dbus_connection_flush (session_bus, NULL, NULL, NULL);
1370 g_object_unref (session_bus);
1373 completed = TRUE;
1375 out:
1376 g_strfreev (argv);
1377 g_strfreev (envp);
1379 return completed;
1382 static gboolean
1383 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
1384 GList *uris,
1385 GAppLaunchContext *launch_context,
1386 GError **error)
1388 return _g_desktop_app_info_launch_uris_internal (appinfo, uris,
1389 launch_context,
1390 _SPAWN_FLAGS_DEFAULT,
1391 NULL, NULL, NULL, NULL,
1392 error);
1395 static gboolean
1396 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
1398 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1400 return info->exec &&
1401 ((strstr (info->exec, "%u") != NULL) ||
1402 (strstr (info->exec, "%U") != NULL));
1405 static gboolean
1406 g_desktop_app_info_supports_files (GAppInfo *appinfo)
1408 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1410 return info->exec &&
1411 ((strstr (info->exec, "%f") != NULL) ||
1412 (strstr (info->exec, "%F") != NULL));
1415 static gboolean
1416 g_desktop_app_info_launch (GAppInfo *appinfo,
1417 GList *files,
1418 GAppLaunchContext *launch_context,
1419 GError **error)
1421 GList *uris;
1422 char *uri;
1423 gboolean res;
1425 uris = NULL;
1426 while (files)
1428 uri = g_file_get_uri (files->data);
1429 uris = g_list_prepend (uris, uri);
1430 files = files->next;
1433 uris = g_list_reverse (uris);
1435 res = g_desktop_app_info_launch_uris (appinfo, uris, launch_context, error);
1437 g_list_free_full (uris, g_free);
1439 return res;
1443 * g_desktop_app_info_launch_uris_as_manager:
1444 * @appinfo: a #GDesktopAppInfo
1445 * @uris: (element-type utf8): List of URIs
1446 * @launch_context: a #GAppLaunchContext
1447 * @spawn_flags: #GSpawnFlags, used for each process
1448 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1449 * each process.
1450 * @user_setup_data: (closure user_setup): User data for @user_setup
1451 * @pid_callback: (scope call): Callback for child processes
1452 * @pid_callback_data: (closure pid_callback): User data for @callback
1453 * @error: return location for a #GError, or %NULL
1455 * This function performs the equivalent of g_app_info_launch_uris(),
1456 * but is intended primarily for operating system components that
1457 * launch applications. Ordinary applications should use
1458 * g_app_info_launch_uris().
1460 * In contrast to g_app_info_launch_uris(), all processes created will
1461 * always be run directly as children as if by the UNIX fork()/exec()
1462 * calls.
1464 * This guarantee allows additional control over the exact environment
1465 * of the child processes, which is provided via a setup function
1466 * @user_setup, as well as the process identifier of each child process
1467 * via @pid_callback. See g_spawn_async() for more information about the
1468 * semantics of the @user_setup function.
1470 * Returns: %TRUE on successful launch, %FALSE otherwise.
1472 gboolean
1473 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo *appinfo,
1474 GList *uris,
1475 GAppLaunchContext *launch_context,
1476 GSpawnFlags spawn_flags,
1477 GSpawnChildSetupFunc user_setup,
1478 gpointer user_setup_data,
1479 GDesktopAppLaunchCallback pid_callback,
1480 gpointer pid_callback_data,
1481 GError **error)
1483 return _g_desktop_app_info_launch_uris_internal ((GAppInfo*)appinfo,
1484 uris,
1485 launch_context,
1486 spawn_flags,
1487 user_setup,
1488 user_setup_data,
1489 pid_callback,
1490 pid_callback_data,
1491 error);
1495 * g_desktop_app_info_set_desktop_env:
1496 * @desktop_env: a string specifying what desktop this is
1498 * Sets the name of the desktop that the application is running in.
1499 * This is used by g_app_info_should_show() and
1500 * g_desktop_app_info_get_show_in() to evaluate the
1501 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1502 * desktop entry fields.
1504 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1505 * Menu specification</ulink> recognizes the following:
1506 * <simplelist>
1507 * <member>GNOME</member>
1508 * <member>KDE</member>
1509 * <member>ROX</member>
1510 * <member>XFCE</member>
1511 * <member>LXDE</member>
1512 * <member>Unity</member>
1513 * <member>Old</member>
1514 * </simplelist>
1516 * Should be called only once; subsequent calls are ignored.
1518 void
1519 g_desktop_app_info_set_desktop_env (const gchar *desktop_env)
1521 G_LOCK (g_desktop_env);
1522 if (!g_desktop_env)
1523 g_desktop_env = g_strdup (desktop_env);
1524 G_UNLOCK (g_desktop_env);
1527 static gboolean
1528 g_desktop_app_info_should_show (GAppInfo *appinfo)
1530 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1532 if (info->nodisplay)
1533 return FALSE;
1535 return g_desktop_app_info_get_show_in (info, NULL);
1538 typedef enum {
1539 APP_DIR,
1540 MIMETYPE_DIR
1541 } DirType;
1543 static char *
1544 ensure_dir (DirType type,
1545 GError **error)
1547 char *path, *display_name;
1548 int errsv;
1550 if (type == APP_DIR)
1551 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1552 else
1553 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1555 errno = 0;
1556 if (g_mkdir_with_parents (path, 0700) == 0)
1557 return path;
1559 errsv = errno;
1560 display_name = g_filename_display_name (path);
1561 if (type == APP_DIR)
1562 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1563 _("Can't create user application configuration folder %s: %s"),
1564 display_name, g_strerror (errsv));
1565 else
1566 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
1567 _("Can't create user MIME configuration folder %s: %s"),
1568 display_name, g_strerror (errsv));
1570 g_free (display_name);
1571 g_free (path);
1573 return NULL;
1576 static gboolean
1577 update_mimeapps_list (const char *desktop_id,
1578 const char *content_type,
1579 UpdateMimeFlags flags,
1580 GError **error)
1582 char *dirname, *filename, *string;
1583 GKeyFile *key_file;
1584 gboolean load_succeeded, res;
1585 char **old_list, **list;
1586 gsize length, data_size;
1587 char *data;
1588 int i, j, k;
1589 char **content_types;
1591 /* Don't add both at start and end */
1592 g_assert (!((flags & UPDATE_MIME_SET_DEFAULT) &&
1593 (flags & UPDATE_MIME_SET_NON_DEFAULT)));
1595 dirname = ensure_dir (APP_DIR, error);
1596 if (!dirname)
1597 return FALSE;
1599 filename = g_build_filename (dirname, "mimeapps.list", NULL);
1600 g_free (dirname);
1602 key_file = g_key_file_new ();
1603 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1604 if (!load_succeeded ||
1605 (!g_key_file_has_group (key_file, ADDED_ASSOCIATIONS_GROUP) &&
1606 !g_key_file_has_group (key_file, REMOVED_ASSOCIATIONS_GROUP) &&
1607 !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP)))
1609 g_key_file_free (key_file);
1610 key_file = g_key_file_new ();
1613 if (content_type)
1615 content_types = g_new (char *, 2);
1616 content_types[0] = g_strdup (content_type);
1617 content_types[1] = NULL;
1619 else
1621 content_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP, NULL, NULL);
1624 for (k = 0; content_types && content_types[k]; k++)
1626 /* set as default, if requested so */
1627 string = g_key_file_get_string (key_file,
1628 DEFAULT_APPLICATIONS_GROUP,
1629 content_types[k],
1630 NULL);
1632 if (g_strcmp0 (string, desktop_id) != 0 &&
1633 (flags & UPDATE_MIME_SET_DEFAULT))
1635 g_free (string);
1636 string = g_strdup (desktop_id);
1638 /* add in the non-default list too, if it's not already there */
1639 flags |= UPDATE_MIME_SET_NON_DEFAULT;
1642 if (string == NULL || desktop_id == NULL)
1643 g_key_file_remove_key (key_file,
1644 DEFAULT_APPLICATIONS_GROUP,
1645 content_types[k],
1646 NULL);
1647 else
1648 g_key_file_set_string (key_file,
1649 DEFAULT_APPLICATIONS_GROUP,
1650 content_types[k],
1651 string);
1653 g_free (string);
1656 if (content_type)
1658 /* reuse the list from above */
1660 else
1662 g_strfreev (content_types);
1663 content_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP, NULL, NULL);
1666 for (k = 0; content_types && content_types[k]; k++)
1668 /* Add to the right place in the list */
1670 length = 0;
1671 old_list = g_key_file_get_string_list (key_file, ADDED_ASSOCIATIONS_GROUP,
1672 content_types[k], &length, NULL);
1674 list = g_new (char *, 1 + length + 1);
1676 i = 0;
1678 /* if we're adding a last-used hint, just put the application in front of the list */
1679 if (flags & UPDATE_MIME_SET_LAST_USED)
1681 /* avoid adding this again as non-default later */
1682 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1683 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1685 list[i++] = g_strdup (desktop_id);
1688 if (old_list)
1690 for (j = 0; old_list[j] != NULL; j++)
1692 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1694 /* rewrite other entries if they're different from the new one */
1695 list[i++] = g_strdup (old_list[j]);
1697 else if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1699 /* we encountered an old entry which is equal to the one we're adding as non-default,
1700 * don't change its position in the list.
1702 flags ^= UPDATE_MIME_SET_NON_DEFAULT;
1703 list[i++] = g_strdup (old_list[j]);
1708 /* add it at the end of the list */
1709 if (flags & UPDATE_MIME_SET_NON_DEFAULT)
1710 list[i++] = g_strdup (desktop_id);
1712 list[i] = NULL;
1714 g_strfreev (old_list);
1716 if (list[0] == NULL || desktop_id == NULL)
1717 g_key_file_remove_key (key_file,
1718 ADDED_ASSOCIATIONS_GROUP,
1719 content_types[k],
1720 NULL);
1721 else
1722 g_key_file_set_string_list (key_file,
1723 ADDED_ASSOCIATIONS_GROUP,
1724 content_types[k],
1725 (const char * const *)list, i);
1727 g_strfreev (list);
1730 if (content_type)
1732 /* reuse the list from above */
1734 else
1736 g_strfreev (content_types);
1737 content_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP, NULL, NULL);
1740 for (k = 0; content_types && content_types[k]; k++)
1742 /* Remove from removed associations group (unless remove) */
1744 length = 0;
1745 old_list = g_key_file_get_string_list (key_file, REMOVED_ASSOCIATIONS_GROUP,
1746 content_types[k], &length, NULL);
1748 list = g_new (char *, 1 + length + 1);
1750 i = 0;
1751 if (flags & UPDATE_MIME_REMOVE)
1752 list[i++] = g_strdup (desktop_id);
1753 if (old_list)
1755 for (j = 0; old_list[j] != NULL; j++)
1757 if (g_strcmp0 (old_list[j], desktop_id) != 0)
1758 list[i++] = g_strdup (old_list[j]);
1761 list[i] = NULL;
1763 g_strfreev (old_list);
1765 if (list[0] == NULL || desktop_id == NULL)
1766 g_key_file_remove_key (key_file,
1767 REMOVED_ASSOCIATIONS_GROUP,
1768 content_types[k],
1769 NULL);
1770 else
1771 g_key_file_set_string_list (key_file,
1772 REMOVED_ASSOCIATIONS_GROUP,
1773 content_types[k],
1774 (const char * const *)list, i);
1776 g_strfreev (list);
1779 g_strfreev (content_types);
1781 data = g_key_file_to_data (key_file, &data_size, error);
1782 g_key_file_free (key_file);
1784 res = g_file_set_contents (filename, data, data_size, error);
1786 mime_info_cache_reload (NULL);
1788 g_free (filename);
1789 g_free (data);
1791 return res;
1794 static gboolean
1795 g_desktop_app_info_set_as_last_used_for_type (GAppInfo *appinfo,
1796 const char *content_type,
1797 GError **error)
1799 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1801 if (!g_desktop_app_info_ensure_saved (info, error))
1802 return FALSE;
1804 if (!info->desktop_id)
1806 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1807 _("Application information lacks an identifier"));
1808 return FALSE;
1811 /* both add support for the content type and set as last used */
1812 return update_mimeapps_list (info->desktop_id, content_type,
1813 UPDATE_MIME_SET_NON_DEFAULT |
1814 UPDATE_MIME_SET_LAST_USED,
1815 error);
1818 static gboolean
1819 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1820 const char *content_type,
1821 GError **error)
1823 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1825 if (!g_desktop_app_info_ensure_saved (info, error))
1826 return FALSE;
1828 if (!info->desktop_id)
1830 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1831 _("Application information lacks an identifier"));
1832 return FALSE;
1835 return update_mimeapps_list (info->desktop_id, content_type,
1836 UPDATE_MIME_SET_DEFAULT,
1837 error);
1840 static void
1841 update_program_done (GPid pid,
1842 gint status,
1843 gpointer data)
1845 /* Did the application exit correctly */
1846 if (WIFEXITED (status) &&
1847 WEXITSTATUS (status) == 0)
1849 /* Here we could clean out any caches in use */
1853 static void
1854 run_update_command (char *command,
1855 char *subdir)
1857 char *argv[3] = {
1858 NULL,
1859 NULL,
1860 NULL,
1862 GPid pid = 0;
1863 GError *error = NULL;
1865 argv[0] = command;
1866 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1868 if (g_spawn_async ("/", argv,
1869 NULL, /* envp */
1870 G_SPAWN_SEARCH_PATH |
1871 G_SPAWN_STDOUT_TO_DEV_NULL |
1872 G_SPAWN_STDERR_TO_DEV_NULL |
1873 G_SPAWN_DO_NOT_REAP_CHILD,
1874 NULL, NULL, /* No setup function */
1875 &pid,
1876 &error))
1877 g_child_watch_add (pid, update_program_done, NULL);
1878 else
1880 /* If we get an error at this point, it's quite likely the user doesn't
1881 * have an installed copy of either 'update-mime-database' or
1882 * 'update-desktop-database'. I don't think we want to popup an error
1883 * dialog at this point, so we just do a g_warning to give the user a
1884 * chance of debugging it.
1886 g_warning ("%s", error->message);
1889 g_free (argv[1]);
1892 static gboolean
1893 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1894 const char *extension,
1895 GError **error)
1897 char *filename, *basename, *mimetype;
1898 char *dirname;
1899 gboolean res;
1901 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo), error))
1902 return FALSE;
1904 dirname = ensure_dir (MIMETYPE_DIR, error);
1905 if (!dirname)
1906 return FALSE;
1908 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1909 filename = g_build_filename (dirname, basename, NULL);
1910 g_free (basename);
1911 g_free (dirname);
1913 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1915 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1917 char *contents;
1919 contents =
1920 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1921 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1922 " <mime-type type=\"%s\">\n"
1923 " <comment>%s document</comment>\n"
1924 " <glob pattern=\"*.%s\"/>\n"
1925 " </mime-type>\n"
1926 "</mime-info>\n", mimetype, extension, extension);
1928 g_file_set_contents (filename, contents, -1, NULL);
1929 g_free (contents);
1931 run_update_command ("update-mime-database", "mime");
1933 g_free (filename);
1935 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1936 mimetype,
1937 error);
1939 g_free (mimetype);
1941 return res;
1944 static gboolean
1945 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1946 const char *content_type,
1947 GError **error)
1949 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1951 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1952 return FALSE;
1954 return update_mimeapps_list (info->desktop_id, content_type,
1955 UPDATE_MIME_SET_NON_DEFAULT,
1956 error);
1959 static gboolean
1960 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1962 return TRUE;
1965 static gboolean
1966 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1967 const char *content_type,
1968 GError **error)
1970 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1972 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info), error))
1973 return FALSE;
1975 return update_mimeapps_list (info->desktop_id, content_type,
1976 UPDATE_MIME_REMOVE,
1977 error);
1980 static gboolean
1981 g_desktop_app_info_ensure_saved (GDesktopAppInfo *info,
1982 GError **error)
1984 GKeyFile *key_file;
1985 char *dirname;
1986 char *filename;
1987 char *data, *desktop_id;
1988 gsize data_size;
1989 int fd;
1990 gboolean res;
1992 if (info->filename != NULL)
1993 return TRUE;
1995 /* This is only used for object created with
1996 * g_app_info_create_from_commandline. All other
1997 * object should have a filename
2000 dirname = ensure_dir (APP_DIR, error);
2001 if (!dirname)
2002 return FALSE;
2004 key_file = g_key_file_new ();
2006 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2007 "Encoding", "UTF-8");
2008 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2009 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
2010 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2011 G_KEY_FILE_DESKTOP_KEY_TYPE,
2012 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
2013 if (info->terminal)
2014 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2015 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
2016 if (info->nodisplay)
2017 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2018 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2020 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2021 G_KEY_FILE_DESKTOP_KEY_EXEC, info->exec);
2023 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2024 G_KEY_FILE_DESKTOP_KEY_NAME, info->name);
2026 if (info->generic_name != NULL)
2027 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2028 GENERIC_NAME_KEY, info->generic_name);
2030 if (info->fullname != NULL)
2031 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2032 FULL_NAME_KEY, info->fullname);
2034 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
2035 G_KEY_FILE_DESKTOP_KEY_COMMENT, info->comment);
2037 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
2038 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
2040 data = g_key_file_to_data (key_file, &data_size, NULL);
2041 g_key_file_free (key_file);
2043 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", info->name);
2044 filename = g_build_filename (dirname, desktop_id, NULL);
2045 g_free (desktop_id);
2046 g_free (dirname);
2048 fd = g_mkstemp (filename);
2049 if (fd == -1)
2051 char *display_name;
2053 display_name = g_filename_display_name (filename);
2054 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
2055 _("Can't create user desktop file %s"), display_name);
2056 g_free (display_name);
2057 g_free (filename);
2058 g_free (data);
2059 return FALSE;
2062 desktop_id = g_path_get_basename (filename);
2064 close (fd);
2066 res = g_file_set_contents (filename, data, data_size, error);
2067 if (!res)
2069 g_free (desktop_id);
2070 g_free (filename);
2071 return FALSE;
2074 info->filename = filename;
2075 info->desktop_id = desktop_id;
2077 run_update_command ("update-desktop-database", "applications");
2079 return TRUE;
2082 static gboolean
2083 g_desktop_app_info_can_delete (GAppInfo *appinfo)
2085 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2087 if (info->filename)
2089 if (strstr (info->filename, "/userapp-"))
2090 return g_access (info->filename, W_OK) == 0;
2093 return FALSE;
2096 static gboolean
2097 g_desktop_app_info_delete (GAppInfo *appinfo)
2099 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
2101 if (info->filename)
2103 if (g_remove (info->filename) == 0)
2105 update_mimeapps_list (info->desktop_id, NULL,
2106 UPDATE_MIME_NONE,
2107 NULL);
2109 g_free (info->filename);
2110 info->filename = NULL;
2111 g_free (info->desktop_id);
2112 info->desktop_id = NULL;
2114 return TRUE;
2118 return FALSE;
2122 * g_app_info_create_from_commandline:
2123 * @commandline: the commandline to use
2124 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2125 * @flags: flags that can specify details of the created #GAppInfo
2126 * @error: a #GError location to store the error occurring, %NULL to ignore.
2128 * Creates a new #GAppInfo from the given information.
2130 * Returns: (transfer full): new #GAppInfo for given command.
2132 GAppInfo *
2133 g_app_info_create_from_commandline (const char *commandline,
2134 const char *application_name,
2135 GAppInfoCreateFlags flags,
2136 GError **error)
2138 char **split;
2139 char *basename;
2140 GDesktopAppInfo *info;
2142 g_return_val_if_fail (commandline, NULL);
2144 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
2146 info->filename = NULL;
2147 info->desktop_id = NULL;
2149 info->terminal = (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL) != 0;
2150 info->startup_notify = (flags & G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION) != 0;
2151 info->hidden = FALSE;
2152 if ((flags & G_APP_INFO_CREATE_SUPPORTS_URIS) != 0)
2153 info->exec = g_strconcat (commandline, " %u", NULL);
2154 else
2155 info->exec = g_strconcat (commandline, " %f", NULL);
2156 info->nodisplay = TRUE;
2157 info->binary = binary_from_exec (info->exec);
2159 if (application_name)
2160 info->name = g_strdup (application_name);
2161 else
2163 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2164 split = g_strsplit (commandline, " ", 2);
2165 basename = split[0] ? g_path_get_basename (split[0]) : NULL;
2166 g_strfreev (split);
2167 info->name = basename;
2168 if (info->name == NULL)
2169 info->name = g_strdup ("custom");
2171 info->comment = g_strdup_printf (_("Custom definition for %s"), info->name);
2173 return G_APP_INFO (info);
2176 static void
2177 g_desktop_app_info_iface_init (GAppInfoIface *iface)
2179 iface->dup = g_desktop_app_info_dup;
2180 iface->equal = g_desktop_app_info_equal;
2181 iface->get_id = g_desktop_app_info_get_id;
2182 iface->get_name = g_desktop_app_info_get_name;
2183 iface->get_description = g_desktop_app_info_get_description;
2184 iface->get_executable = g_desktop_app_info_get_executable;
2185 iface->get_icon = g_desktop_app_info_get_icon;
2186 iface->launch = g_desktop_app_info_launch;
2187 iface->supports_uris = g_desktop_app_info_supports_uris;
2188 iface->supports_files = g_desktop_app_info_supports_files;
2189 iface->launch_uris = g_desktop_app_info_launch_uris;
2190 iface->should_show = g_desktop_app_info_should_show;
2191 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
2192 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
2193 iface->add_supports_type = g_desktop_app_info_add_supports_type;
2194 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
2195 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
2196 iface->can_delete = g_desktop_app_info_can_delete;
2197 iface->do_delete = g_desktop_app_info_delete;
2198 iface->get_commandline = g_desktop_app_info_get_commandline;
2199 iface->get_display_name = g_desktop_app_info_get_display_name;
2200 iface->set_as_last_used_for_type = g_desktop_app_info_set_as_last_used_for_type;
2203 static gboolean
2204 app_info_in_list (GAppInfo *info,
2205 GList *list)
2207 while (list != NULL)
2209 if (g_app_info_equal (info, list->data))
2210 return TRUE;
2211 list = list->next;
2213 return FALSE;
2217 * g_app_info_get_recommended_for_type:
2218 * @content_type: the content type to find a #GAppInfo for
2220 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2221 * those applications which claim to support the given content type exactly,
2222 * and not by MIME type subclassing.
2223 * Note that the first application of the list is the last used one, i.e.
2224 * the last one for which g_app_info_set_as_last_used_for_type() has been
2225 * called.
2227 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2228 * for given @content_type or %NULL on error.
2230 * Since: 2.28
2232 GList *
2233 g_app_info_get_recommended_for_type (const gchar *content_type)
2235 GList *desktop_entries, *l;
2236 GList *infos;
2237 GDesktopAppInfo *info;
2239 g_return_val_if_fail (content_type != NULL, NULL);
2241 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, FALSE, NULL);
2243 infos = NULL;
2244 for (l = desktop_entries; l != NULL; l = l->next)
2246 char *desktop_entry = l->data;
2248 info = g_desktop_app_info_new (desktop_entry);
2249 if (info)
2251 if (app_info_in_list (G_APP_INFO (info), infos))
2252 g_object_unref (info);
2253 else
2254 infos = g_list_prepend (infos, info);
2256 g_free (desktop_entry);
2259 g_list_free (desktop_entries);
2261 return g_list_reverse (infos);
2265 * g_app_info_get_fallback_for_type:
2266 * @content_type: the content type to find a #GAppInfo for
2268 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2269 * those applications which claim to support the given content type
2270 * by MIME type subclassing and not directly.
2272 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2273 * for given @content_type or %NULL on error.
2275 * Since: 2.28
2277 GList *
2278 g_app_info_get_fallback_for_type (const gchar *content_type)
2280 GList *desktop_entries, *l;
2281 GList *infos, *recommended_infos;
2282 GDesktopAppInfo *info;
2284 g_return_val_if_fail (content_type != NULL, NULL);
2286 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, NULL);
2287 recommended_infos = g_app_info_get_recommended_for_type (content_type);
2289 infos = NULL;
2290 for (l = desktop_entries; l != NULL; l = l->next)
2292 char *desktop_entry = l->data;
2294 info = g_desktop_app_info_new (desktop_entry);
2295 if (info)
2297 if (app_info_in_list (G_APP_INFO (info), infos) ||
2298 app_info_in_list (G_APP_INFO (info), recommended_infos))
2299 g_object_unref (info);
2300 else
2301 infos = g_list_prepend (infos, info);
2303 g_free (desktop_entry);
2306 g_list_free (desktop_entries);
2307 g_list_free_full (recommended_infos, g_object_unref);
2309 return g_list_reverse (infos);
2313 * g_app_info_get_all_for_type:
2314 * @content_type: the content type to find a #GAppInfo for
2316 * Gets a list of all #GAppInfos for a given content type,
2317 * including the recommended and fallback #GAppInfos. See
2318 * g_app_info_get_recommended_for_type() and
2319 * g_app_info_get_fallback_for_type().
2321 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2322 * for given @content_type or %NULL on error.
2324 GList *
2325 g_app_info_get_all_for_type (const char *content_type)
2327 GList *desktop_entries, *l;
2328 GList *infos;
2329 char *user_default = NULL;
2330 GDesktopAppInfo *info;
2332 g_return_val_if_fail (content_type != NULL, NULL);
2334 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2335 infos = NULL;
2337 /* put the user default in front of the list, for compatibility */
2338 if (user_default != NULL)
2340 info = g_desktop_app_info_new (user_default);
2342 if (info != NULL)
2343 infos = g_list_prepend (infos, info);
2346 g_free (user_default);
2348 for (l = desktop_entries; l != NULL; l = l->next)
2350 char *desktop_entry = l->data;
2352 info = g_desktop_app_info_new (desktop_entry);
2353 if (info)
2355 if (app_info_in_list (G_APP_INFO (info), infos))
2356 g_object_unref (info);
2357 else
2358 infos = g_list_prepend (infos, info);
2360 g_free (desktop_entry);
2363 g_list_free (desktop_entries);
2365 return g_list_reverse (infos);
2369 * g_app_info_reset_type_associations:
2370 * @content_type: a content type
2372 * Removes all changes to the type associations done by
2373 * g_app_info_set_as_default_for_type(),
2374 * g_app_info_set_as_default_for_extension(),
2375 * g_app_info_add_supports_type() or
2376 * g_app_info_remove_supports_type().
2378 * Since: 2.20
2380 void
2381 g_app_info_reset_type_associations (const char *content_type)
2383 update_mimeapps_list (NULL, content_type,
2384 UPDATE_MIME_NONE,
2385 NULL);
2389 * g_app_info_get_default_for_type:
2390 * @content_type: the content type to find a #GAppInfo for
2391 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2392 * support URIs
2394 * Gets the default #GAppInfo for a given content type.
2396 * Returns: (transfer full): #GAppInfo for given @content_type or
2397 * %NULL on error.
2399 GAppInfo *
2400 g_app_info_get_default_for_type (const char *content_type,
2401 gboolean must_support_uris)
2403 GList *desktop_entries, *l;
2404 char *user_default = NULL;
2405 GAppInfo *info;
2407 g_return_val_if_fail (content_type != NULL, NULL);
2409 desktop_entries = get_all_desktop_entries_for_mime_type (content_type, NULL, TRUE, &user_default);
2411 info = NULL;
2413 if (user_default != NULL)
2415 info = (GAppInfo *) g_desktop_app_info_new (user_default);
2417 if (info)
2419 if (must_support_uris && !g_app_info_supports_uris (info))
2421 g_object_unref (info);
2422 info = NULL;
2427 g_free (user_default);
2429 if (info != NULL)
2431 g_list_free_full (desktop_entries, g_free);
2432 return info;
2435 /* pick the first from the other list that matches our URI
2436 * requirements.
2438 for (l = desktop_entries; l != NULL; l = l->next)
2440 char *desktop_entry = l->data;
2442 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
2443 if (info)
2445 if (must_support_uris && !g_app_info_supports_uris (info))
2447 g_object_unref (info);
2448 info = NULL;
2450 else
2451 break;
2455 g_list_free_full (desktop_entries, g_free);
2457 return info;
2461 * g_app_info_get_default_for_uri_scheme:
2462 * @uri_scheme: a string containing a URI scheme.
2464 * Gets the default application for handling URIs with
2465 * the given URI scheme. A URI scheme is the initial part
2466 * of the URI, up to but not including the ':', e.g. "http",
2467 * "ftp" or "sip".
2469 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2471 GAppInfo *
2472 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
2474 GAppInfo *app_info;
2475 char *content_type, *scheme_down;
2477 scheme_down = g_ascii_strdown (uri_scheme, -1);
2478 content_type = g_strdup_printf ("x-scheme-handler/%s", scheme_down);
2479 g_free (scheme_down);
2480 app_info = g_app_info_get_default_for_type (content_type, FALSE);
2481 g_free (content_type);
2483 return app_info;
2486 static void
2487 get_apps_from_dir (GHashTable *apps,
2488 const char *dirname,
2489 const char *prefix)
2491 GDir *dir;
2492 const char *basename;
2493 char *filename, *subprefix, *desktop_id;
2494 gboolean hidden;
2495 GDesktopAppInfo *appinfo;
2497 dir = g_dir_open (dirname, 0, NULL);
2498 if (dir)
2500 while ((basename = g_dir_read_name (dir)) != NULL)
2502 filename = g_build_filename (dirname, basename, NULL);
2503 if (g_str_has_suffix (basename, ".desktop"))
2505 desktop_id = g_strconcat (prefix, basename, NULL);
2507 /* Use _extended so we catch NULLs too (hidden) */
2508 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
2510 appinfo = g_desktop_app_info_new_from_filename (filename);
2511 hidden = FALSE;
2513 if (appinfo && g_desktop_app_info_get_is_hidden (appinfo))
2515 g_object_unref (appinfo);
2516 appinfo = NULL;
2517 hidden = TRUE;
2520 if (appinfo || hidden)
2522 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
2524 if (appinfo)
2526 /* Reuse instead of strdup here */
2527 appinfo->desktop_id = desktop_id;
2528 desktop_id = NULL;
2532 g_free (desktop_id);
2534 else
2536 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
2538 subprefix = g_strconcat (prefix, basename, "-", NULL);
2539 get_apps_from_dir (apps, filename, subprefix);
2540 g_free (subprefix);
2543 g_free (filename);
2545 g_dir_close (dir);
2551 * g_app_info_get_all:
2553 * Gets a list of all of the applications currently registered
2554 * on this system.
2556 * For desktop files, this includes applications that have
2557 * <literal>NoDisplay=true</literal> set or are excluded from
2558 * display by means of <literal>OnlyShowIn</literal> or
2559 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2560 * The returned list does not include applications which have
2561 * the <literal>Hidden</literal> key set.
2563 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2565 GList *
2566 g_app_info_get_all (void)
2568 const char * const *dirs;
2569 GHashTable *apps;
2570 GHashTableIter iter;
2571 gpointer value;
2572 int i;
2573 GList *infos;
2575 dirs = get_applications_search_path ();
2577 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
2578 g_free, NULL);
2581 for (i = 0; dirs[i] != NULL; i++)
2582 get_apps_from_dir (apps, dirs[i], "");
2585 infos = NULL;
2586 g_hash_table_iter_init (&iter, apps);
2587 while (g_hash_table_iter_next (&iter, NULL, &value))
2589 if (value)
2590 infos = g_list_prepend (infos, value);
2593 g_hash_table_destroy (apps);
2595 return g_list_reverse (infos);
2598 /* Cacheing of mimeinfo.cache and defaults.list files */
2600 typedef struct {
2601 char *path;
2602 GHashTable *mime_info_cache_map;
2603 GHashTable *defaults_list_map;
2604 GHashTable *mimeapps_list_added_map;
2605 GHashTable *mimeapps_list_removed_map;
2606 GHashTable *mimeapps_list_defaults_map;
2607 time_t mime_info_cache_timestamp;
2608 time_t defaults_list_timestamp;
2609 time_t mimeapps_list_timestamp;
2610 } MimeInfoCacheDir;
2612 typedef struct {
2613 GList *dirs; /* mimeinfo.cache and defaults.list */
2614 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
2615 time_t last_stat_time;
2616 guint should_ping_mime_monitor : 1;
2617 } MimeInfoCache;
2619 static MimeInfoCache *mime_info_cache = NULL;
2620 G_LOCK_DEFINE_STATIC (mime_info_cache);
2622 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
2623 const char *mime_type,
2624 char **new_desktop_file_ids);
2626 static MimeInfoCache * mime_info_cache_new (void);
2628 static void
2629 destroy_info_cache_value (gpointer key,
2630 GList *value,
2631 gpointer data)
2633 g_list_free_full (value, g_free);
2636 static void
2637 destroy_info_cache_map (GHashTable *info_cache_map)
2639 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
2640 g_hash_table_destroy (info_cache_map);
2643 static gboolean
2644 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
2645 const char *cache_file,
2646 time_t *timestamp)
2648 struct stat buf;
2649 char *filename;
2651 filename = g_build_filename (dir->path, cache_file, NULL);
2653 if (g_stat (filename, &buf) < 0)
2655 g_free (filename);
2656 return TRUE;
2658 g_free (filename);
2660 if (buf.st_mtime != *timestamp)
2661 return TRUE;
2663 return FALSE;
2666 /* Call with lock held */
2667 static gboolean
2668 remove_all (gpointer key,
2669 gpointer value,
2670 gpointer user_data)
2672 return TRUE;
2676 static void
2677 mime_info_cache_blow_global_cache (void)
2679 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
2680 remove_all, NULL);
2683 static void
2684 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
2686 GError *load_error;
2687 GKeyFile *key_file;
2688 gchar *filename, **mime_types;
2689 int i;
2690 struct stat buf;
2692 load_error = NULL;
2693 mime_types = NULL;
2695 if (dir->mime_info_cache_map != NULL &&
2696 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
2697 &dir->mime_info_cache_timestamp))
2698 return;
2700 if (dir->mime_info_cache_map != NULL)
2701 destroy_info_cache_map (dir->mime_info_cache_map);
2703 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2704 (GDestroyNotify) g_free,
2705 NULL);
2707 key_file = g_key_file_new ();
2709 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
2711 if (g_stat (filename, &buf) < 0)
2712 goto error;
2714 if (dir->mime_info_cache_timestamp > 0)
2715 mime_info_cache->should_ping_mime_monitor = TRUE;
2717 dir->mime_info_cache_timestamp = buf.st_mtime;
2719 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2721 g_free (filename);
2722 filename = NULL;
2724 if (load_error != NULL)
2725 goto error;
2727 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
2728 NULL, &load_error);
2730 if (load_error != NULL)
2731 goto error;
2733 for (i = 0; mime_types[i] != NULL; i++)
2735 gchar **desktop_file_ids;
2736 char *unaliased_type;
2737 desktop_file_ids = g_key_file_get_string_list (key_file,
2738 MIME_CACHE_GROUP,
2739 mime_types[i],
2740 NULL,
2741 NULL);
2743 if (desktop_file_ids == NULL)
2744 continue;
2746 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2747 mime_info_cache_dir_add_desktop_entries (dir,
2748 unaliased_type,
2749 desktop_file_ids);
2750 g_free (unaliased_type);
2752 g_strfreev (desktop_file_ids);
2755 g_strfreev (mime_types);
2756 g_key_file_free (key_file);
2758 return;
2759 error:
2760 g_free (filename);
2761 g_key_file_free (key_file);
2763 if (mime_types != NULL)
2764 g_strfreev (mime_types);
2766 if (load_error)
2767 g_error_free (load_error);
2770 static void
2771 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
2773 GKeyFile *key_file;
2774 GError *load_error;
2775 gchar *filename, **mime_types;
2776 char *unaliased_type;
2777 char **desktop_file_ids;
2778 int i;
2779 struct stat buf;
2781 load_error = NULL;
2782 mime_types = NULL;
2784 if (dir->defaults_list_map != NULL &&
2785 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
2786 &dir->defaults_list_timestamp))
2787 return;
2789 if (dir->defaults_list_map != NULL)
2790 g_hash_table_destroy (dir->defaults_list_map);
2791 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2792 g_free, (GDestroyNotify)g_strfreev);
2795 key_file = g_key_file_new ();
2797 filename = g_build_filename (dir->path, "defaults.list", NULL);
2798 if (g_stat (filename, &buf) < 0)
2799 goto error;
2801 if (dir->defaults_list_timestamp > 0)
2802 mime_info_cache->should_ping_mime_monitor = TRUE;
2804 dir->defaults_list_timestamp = buf.st_mtime;
2806 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2807 g_free (filename);
2808 filename = NULL;
2810 if (load_error != NULL)
2811 goto error;
2813 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2814 NULL, NULL);
2815 if (mime_types != NULL)
2817 for (i = 0; mime_types[i] != NULL; i++)
2819 desktop_file_ids = g_key_file_get_string_list (key_file,
2820 DEFAULT_APPLICATIONS_GROUP,
2821 mime_types[i],
2822 NULL,
2823 NULL);
2824 if (desktop_file_ids == NULL)
2825 continue;
2827 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2828 g_hash_table_replace (dir->defaults_list_map,
2829 unaliased_type,
2830 desktop_file_ids);
2833 g_strfreev (mime_types);
2836 g_key_file_free (key_file);
2837 return;
2839 error:
2840 g_free (filename);
2841 g_key_file_free (key_file);
2843 if (mime_types != NULL)
2844 g_strfreev (mime_types);
2846 if (load_error)
2847 g_error_free (load_error);
2850 static void
2851 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir *dir)
2853 GKeyFile *key_file;
2854 GError *load_error;
2855 gchar *filename, **mime_types;
2856 char *unaliased_type;
2857 char **desktop_file_ids;
2858 char *desktop_id;
2859 int i;
2860 struct stat buf;
2862 load_error = NULL;
2863 mime_types = NULL;
2865 if (dir->mimeapps_list_added_map != NULL &&
2866 !mime_info_cache_dir_out_of_date (dir, "mimeapps.list",
2867 &dir->mimeapps_list_timestamp))
2868 return;
2870 if (dir->mimeapps_list_added_map != NULL)
2871 g_hash_table_destroy (dir->mimeapps_list_added_map);
2872 dir->mimeapps_list_added_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2873 g_free, (GDestroyNotify)g_strfreev);
2875 if (dir->mimeapps_list_removed_map != NULL)
2876 g_hash_table_destroy (dir->mimeapps_list_removed_map);
2877 dir->mimeapps_list_removed_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2878 g_free, (GDestroyNotify)g_strfreev);
2880 if (dir->mimeapps_list_defaults_map != NULL)
2881 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
2882 dir->mimeapps_list_defaults_map = g_hash_table_new_full (g_str_hash, g_str_equal,
2883 g_free, g_free);
2885 key_file = g_key_file_new ();
2887 filename = g_build_filename (dir->path, "mimeapps.list", NULL);
2888 if (g_stat (filename, &buf) < 0)
2889 goto error;
2891 if (dir->mimeapps_list_timestamp > 0)
2892 mime_info_cache->should_ping_mime_monitor = TRUE;
2894 dir->mimeapps_list_timestamp = buf.st_mtime;
2896 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
2897 g_free (filename);
2898 filename = NULL;
2900 if (load_error != NULL)
2901 goto error;
2903 mime_types = g_key_file_get_keys (key_file, ADDED_ASSOCIATIONS_GROUP,
2904 NULL, NULL);
2905 if (mime_types != NULL)
2907 for (i = 0; mime_types[i] != NULL; i++)
2909 desktop_file_ids = g_key_file_get_string_list (key_file,
2910 ADDED_ASSOCIATIONS_GROUP,
2911 mime_types[i],
2912 NULL,
2913 NULL);
2914 if (desktop_file_ids == NULL)
2915 continue;
2917 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2918 g_hash_table_replace (dir->mimeapps_list_added_map,
2919 unaliased_type,
2920 desktop_file_ids);
2923 g_strfreev (mime_types);
2926 mime_types = g_key_file_get_keys (key_file, REMOVED_ASSOCIATIONS_GROUP,
2927 NULL, NULL);
2928 if (mime_types != NULL)
2930 for (i = 0; mime_types[i] != NULL; i++)
2932 desktop_file_ids = g_key_file_get_string_list (key_file,
2933 REMOVED_ASSOCIATIONS_GROUP,
2934 mime_types[i],
2935 NULL,
2936 NULL);
2937 if (desktop_file_ids == NULL)
2938 continue;
2940 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2941 g_hash_table_replace (dir->mimeapps_list_removed_map,
2942 unaliased_type,
2943 desktop_file_ids);
2946 g_strfreev (mime_types);
2949 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
2950 NULL, NULL);
2951 if (mime_types != NULL)
2953 for (i = 0; mime_types[i] != NULL; i++)
2955 desktop_id = g_key_file_get_string (key_file,
2956 DEFAULT_APPLICATIONS_GROUP,
2957 mime_types[i],
2958 NULL);
2959 if (desktop_id == NULL)
2960 continue;
2962 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
2963 g_hash_table_replace (dir->mimeapps_list_defaults_map,
2964 unaliased_type,
2965 desktop_id);
2968 g_strfreev (mime_types);
2971 g_key_file_free (key_file);
2972 return;
2974 error:
2975 g_free (filename);
2976 g_key_file_free (key_file);
2978 if (mime_types != NULL)
2979 g_strfreev (mime_types);
2981 if (load_error)
2982 g_error_free (load_error);
2985 static MimeInfoCacheDir *
2986 mime_info_cache_dir_new (const char *path)
2988 MimeInfoCacheDir *dir;
2990 dir = g_new0 (MimeInfoCacheDir, 1);
2991 dir->path = g_strdup (path);
2993 return dir;
2996 static void
2997 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
2999 if (dir == NULL)
3000 return;
3002 if (dir->mime_info_cache_map != NULL)
3004 destroy_info_cache_map (dir->mime_info_cache_map);
3005 dir->mime_info_cache_map = NULL;
3009 if (dir->defaults_list_map != NULL)
3011 g_hash_table_destroy (dir->defaults_list_map);
3012 dir->defaults_list_map = NULL;
3015 if (dir->mimeapps_list_added_map != NULL)
3017 g_hash_table_destroy (dir->mimeapps_list_added_map);
3018 dir->mimeapps_list_added_map = NULL;
3021 if (dir->mimeapps_list_removed_map != NULL)
3023 g_hash_table_destroy (dir->mimeapps_list_removed_map);
3024 dir->mimeapps_list_removed_map = NULL;
3027 if (dir->mimeapps_list_defaults_map != NULL)
3029 g_hash_table_destroy (dir->mimeapps_list_defaults_map);
3030 dir->mimeapps_list_defaults_map = NULL;
3033 g_free (dir);
3036 static void
3037 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
3038 const char *mime_type,
3039 char **new_desktop_file_ids)
3041 GList *desktop_file_ids;
3042 int i;
3044 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
3045 mime_type);
3047 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
3049 if (!g_list_find_custom (desktop_file_ids, new_desktop_file_ids[i], (GCompareFunc) strcmp))
3050 desktop_file_ids = g_list_append (desktop_file_ids,
3051 g_strdup (new_desktop_file_ids[i]));
3054 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
3057 static void
3058 mime_info_cache_init_dir_lists (void)
3060 const char * const *dirs;
3061 int i;
3063 mime_info_cache = mime_info_cache_new ();
3065 dirs = get_applications_search_path ();
3067 for (i = 0; dirs[i] != NULL; i++)
3069 MimeInfoCacheDir *dir;
3071 dir = mime_info_cache_dir_new (dirs[i]);
3073 if (dir != NULL)
3075 mime_info_cache_dir_init (dir);
3076 mime_info_cache_dir_init_defaults_list (dir);
3077 mime_info_cache_dir_init_mimeapps_list (dir);
3079 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
3084 static void
3085 mime_info_cache_update_dir_lists (void)
3087 GList *tmp;
3089 tmp = mime_info_cache->dirs;
3091 while (tmp != NULL)
3093 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
3095 /* No need to do this if we had file monitors... */
3096 mime_info_cache_blow_global_cache ();
3097 mime_info_cache_dir_init (dir);
3098 mime_info_cache_dir_init_defaults_list (dir);
3099 mime_info_cache_dir_init_mimeapps_list (dir);
3101 tmp = tmp->next;
3105 static void
3106 mime_info_cache_init (void)
3108 G_LOCK (mime_info_cache);
3109 if (mime_info_cache == NULL)
3110 mime_info_cache_init_dir_lists ();
3111 else
3113 time_t now;
3115 time (&now);
3116 if (now >= mime_info_cache->last_stat_time + 10)
3118 mime_info_cache_update_dir_lists ();
3119 mime_info_cache->last_stat_time = now;
3123 if (mime_info_cache->should_ping_mime_monitor)
3125 /* g_idle_add (emit_mime_changed, NULL); */
3126 mime_info_cache->should_ping_mime_monitor = FALSE;
3129 G_UNLOCK (mime_info_cache);
3132 static MimeInfoCache *
3133 mime_info_cache_new (void)
3135 MimeInfoCache *cache;
3137 cache = g_new0 (MimeInfoCache, 1);
3139 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
3140 (GDestroyNotify) g_free,
3141 (GDestroyNotify) g_free);
3142 return cache;
3145 static void
3146 mime_info_cache_free (MimeInfoCache *cache)
3148 if (cache == NULL)
3149 return;
3151 g_list_free_full (cache->dirs, (GDestroyNotify) mime_info_cache_dir_free);
3152 g_hash_table_destroy (cache->global_defaults_cache);
3153 g_free (cache);
3157 * mime_info_cache_reload:
3158 * @dir: directory path which needs reloading.
3160 * Reload the mime information for the @dir.
3162 static void
3163 mime_info_cache_reload (const char *dir)
3165 /* FIXME: just reload the dir that needs reloading,
3166 * don't blow the whole cache
3168 if (mime_info_cache != NULL)
3170 G_LOCK (mime_info_cache);
3171 mime_info_cache_free (mime_info_cache);
3172 mime_info_cache = NULL;
3173 G_UNLOCK (mime_info_cache);
3177 static GList *
3178 append_desktop_entry (GList *list,
3179 const char *desktop_entry,
3180 GList *removed_entries)
3182 /* Add if not already in list, and valid */
3183 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp) &&
3184 !g_list_find_custom (removed_entries, desktop_entry, (GCompareFunc) strcmp))
3185 list = g_list_prepend (list, g_strdup (desktop_entry));
3187 return list;
3191 * get_all_desktop_entries_for_mime_type:
3192 * @mime_type: a mime type.
3193 * @except: NULL or a strv list
3195 * Returns all the desktop ids for @mime_type. The desktop files
3196 * are listed in an order so that default applications are listed before
3197 * non-default ones, and handlers for inherited mimetypes are listed
3198 * after the base ones.
3200 * Optionally doesn't list the desktop ids given in the @except
3202 * Return value: a #GList containing the desktop ids which claim
3203 * to handle @mime_type.
3205 static GList *
3206 get_all_desktop_entries_for_mime_type (const char *base_mime_type,
3207 const char **except,
3208 gboolean include_fallback,
3209 char **explicit_default)
3211 GList *desktop_entries, *removed_entries, *list, *dir_list, *tmp;
3212 MimeInfoCacheDir *dir;
3213 char *mime_type, *default_entry = NULL;
3214 char *old_default_entry = NULL;
3215 const char *entry;
3216 char **mime_types;
3217 char **default_entries;
3218 char **removed_associations;
3219 int i, j, k;
3220 GPtrArray *array;
3221 char **anc;
3223 mime_info_cache_init ();
3225 if (include_fallback)
3227 /* collect all ancestors */
3228 mime_types = _g_unix_content_type_get_parents (base_mime_type);
3229 array = g_ptr_array_new ();
3230 for (i = 0; mime_types[i]; i++)
3231 g_ptr_array_add (array, mime_types[i]);
3232 g_free (mime_types);
3233 for (i = 0; i < array->len; i++)
3235 anc = _g_unix_content_type_get_parents (g_ptr_array_index (array, i));
3236 for (j = 0; anc[j]; j++)
3238 for (k = 0; k < array->len; k++)
3240 if (strcmp (anc[j], g_ptr_array_index (array, k)) == 0)
3241 break;
3243 if (k == array->len) /* not found */
3244 g_ptr_array_add (array, g_strdup (anc[j]));
3246 g_strfreev (anc);
3248 g_ptr_array_add (array, NULL);
3249 mime_types = (char **)g_ptr_array_free (array, FALSE);
3251 else
3253 mime_types = g_malloc0 (2 * sizeof (gchar *));
3254 mime_types[0] = g_strdup (base_mime_type);
3255 mime_types[1] = NULL;
3258 G_LOCK (mime_info_cache);
3260 removed_entries = NULL;
3261 desktop_entries = NULL;
3263 for (i = 0; except != NULL && except[i] != NULL; i++)
3264 removed_entries = g_list_prepend (removed_entries, g_strdup (except[i]));
3266 for (i = 0; mime_types[i] != NULL; i++)
3268 mime_type = mime_types[i];
3270 /* Go through all apps listed in user and system dirs */
3271 for (dir_list = mime_info_cache->dirs;
3272 dir_list != NULL;
3273 dir_list = dir_list->next)
3275 dir = dir_list->data;
3277 /* Pick the explicit default application if we got no result earlier
3278 * (ie, for more specific mime types)
3280 if (desktop_entries == NULL)
3282 entry = g_hash_table_lookup (dir->mimeapps_list_defaults_map, mime_type);
3284 if (entry != NULL)
3286 /* Save the default entry if it's the first one we encounter */
3287 if (default_entry == NULL)
3288 default_entry = g_strdup (entry);
3292 /* Then added associations from mimeapps.list */
3293 default_entries = g_hash_table_lookup (dir->mimeapps_list_added_map, mime_type);
3294 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3295 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3297 /* Then removed associations from mimeapps.list */
3298 removed_associations = g_hash_table_lookup (dir->mimeapps_list_removed_map, mime_type);
3299 for (j = 0; removed_associations != NULL && removed_associations[j] != NULL; j++)
3300 removed_entries = append_desktop_entry (removed_entries, removed_associations[j], NULL);
3302 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3303 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
3304 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
3306 if (default_entry == NULL && old_default_entry == NULL)
3307 old_default_entry = g_strdup (default_entries[j]);
3309 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j], removed_entries);
3313 /* Go through all entries that support the mimetype */
3314 for (dir_list = mime_info_cache->dirs;
3315 dir_list != NULL;
3316 dir_list = dir_list->next)
3318 dir = dir_list->data;
3320 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
3321 for (tmp = list; tmp != NULL; tmp = tmp->next)
3322 desktop_entries = append_desktop_entry (desktop_entries, tmp->data, removed_entries);
3326 G_UNLOCK (mime_info_cache);
3328 g_strfreev (mime_types);
3330 /* If we have no default from mimeapps.list, take it from
3331 * defaults.list intead.
3333 * If we do have a default from mimeapps.list, free any one that came
3334 * from defaults.list.
3336 if (default_entry == NULL)
3337 default_entry = old_default_entry;
3338 else
3339 g_free (old_default_entry);
3341 if (explicit_default != NULL)
3342 *explicit_default = default_entry;
3343 else
3344 g_free (default_entry);
3346 g_list_free_full (removed_entries, g_free);
3348 desktop_entries = g_list_reverse (desktop_entries);
3350 return desktop_entries;
3353 /* GDesktopAppInfoLookup interface: */
3355 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
3357 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface;
3358 G_DEFINE_INTERFACE (GDesktopAppInfoLookup, g_desktop_app_info_lookup, G_TYPE_OBJECT)
3360 static void
3361 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface *iface)
3366 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3367 * @lookup: a #GDesktopAppInfoLookup
3368 * @uri_scheme: a string containing a URI scheme.
3370 * Gets the default application for launching applications
3371 * using this URI scheme for a particular GDesktopAppInfoLookup
3372 * implementation.
3374 * The GDesktopAppInfoLookup interface and this function is used
3375 * to implement g_app_info_get_default_for_uri_scheme() backends
3376 * in a GIO module. There is no reason for applications to use it
3377 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3379 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3381 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3383 GAppInfo *
3384 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
3385 const char *uri_scheme)
3387 GDesktopAppInfoLookupIface *iface;
3389 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup), NULL);
3391 iface = G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup);
3393 return (* iface->get_default_for_uri_scheme) (lookup, uri_scheme);
3396 G_GNUC_END_IGNORE_DEPRECATIONS