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 GENERIC_NAME_KEY "GenericName"
68 #define FULL_NAME_KEY "X-GNOME-FullName"
75 static void g_desktop_app_info_iface_init (GAppInfoIface
*iface
);
76 static GList
* get_all_desktop_entries_for_mime_type (const char *base_mime_type
,
78 gboolean include_fallback
,
79 char **explicit_default
);
80 static void mime_info_cache_reload (const char *dir
);
81 static gboolean
g_desktop_app_info_ensure_saved (GDesktopAppInfo
*info
,
87 * Information about an installed application from a desktop file.
89 struct _GDesktopAppInfo
91 GObject parent_instance
;
113 guint startup_notify
: 1;
115 /* FIXME: what about StartupWMClass ? */
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,
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
))
131 search_path_init (gpointer data
)
134 const char * const *data_dirs
;
135 const char *user_data_dir
;
138 data_dirs
= g_get_system_data_dirs ();
139 length
= g_strv_length ((char **) data_dirs
);
141 args
= g_new (char *, length
+ 2);
144 user_data_dir
= g_get_user_data_dir ();
145 args
[j
++] = g_build_filename (user_data_dir
, "applications", NULL
);
146 for (i
= 0; i
< length
; i
++)
147 args
[j
++] = g_build_filename (data_dirs
[i
],
148 "applications", NULL
);
154 static const char * const *
155 get_applications_search_path (void)
157 static GOnce once_init
= G_ONCE_INIT
;
158 return g_once (&once_init
, search_path_init
, NULL
);
162 g_desktop_app_info_finalize (GObject
*object
)
164 GDesktopAppInfo
*info
;
166 info
= G_DESKTOP_APP_INFO (object
);
168 g_free (info
->desktop_id
);
169 g_free (info
->filename
);
171 g_free (info
->generic_name
);
172 g_free (info
->fullname
);
173 g_free (info
->comment
);
174 g_free (info
->icon_name
);
176 g_object_unref (info
->icon
);
177 g_strfreev (info
->only_show_in
);
178 g_strfreev (info
->not_show_in
);
179 g_free (info
->try_exec
);
181 g_free (info
->binary
);
183 g_free (info
->categories
);
185 G_OBJECT_CLASS (g_desktop_app_info_parent_class
)->finalize (object
);
189 g_desktop_app_info_set_property(GObject
*object
,
194 GDesktopAppInfo
*self
= G_DESKTOP_APP_INFO (object
);
199 self
->filename
= g_value_dup_string (value
);
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
209 g_desktop_app_info_get_property(GObject
*object
,
214 GDesktopAppInfo
*self
= G_DESKTOP_APP_INFO (object
);
219 g_value_set_string (value
, self
->filename
);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
228 g_desktop_app_info_class_init (GDesktopAppInfoClass
*klass
)
230 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
232 gobject_class
->get_property
= g_desktop_app_info_get_property
;
233 gobject_class
->set_property
= g_desktop_app_info_set_property
;
234 gobject_class
->finalize
= g_desktop_app_info_finalize
;
237 * GDesktopAppInfo:filename
239 * The origin filename of this #GDesktopAppInfo
241 g_object_class_install_property (gobject_class
,
243 g_param_spec_string ("filename", "Filename", "",
245 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
249 g_desktop_app_info_init (GDesktopAppInfo
*local
)
254 binary_from_exec (const char *exec
)
256 const char *p
, *start
;
262 while (*p
!= ' ' && *p
!= 0)
265 return g_strndup (start
, p
- start
);
270 g_desktop_app_info_load_from_keyfile (GDesktopAppInfo
*info
,
277 start_group
= g_key_file_get_start_group (key_file
);
278 if (start_group
== NULL
|| strcmp (start_group
, G_KEY_FILE_DESKTOP_GROUP
) != 0)
280 g_free (start_group
);
283 g_free (start_group
);
285 type
= g_key_file_get_string (key_file
,
286 G_KEY_FILE_DESKTOP_GROUP
,
287 G_KEY_FILE_DESKTOP_KEY_TYPE
,
289 if (type
== NULL
|| strcmp (type
, G_KEY_FILE_DESKTOP_TYPE_APPLICATION
) != 0)
296 try_exec
= g_key_file_get_string (key_file
,
297 G_KEY_FILE_DESKTOP_GROUP
,
298 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC
,
300 if (try_exec
&& try_exec
[0] != '\0')
303 t
= g_find_program_in_path (try_exec
);
312 info
->name
= g_key_file_get_locale_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_NAME
, NULL
, NULL
);
313 info
->generic_name
= g_key_file_get_locale_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, GENERIC_NAME_KEY
, NULL
, NULL
);
314 info
->fullname
= g_key_file_get_locale_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, FULL_NAME_KEY
, NULL
, NULL
);
315 info
->comment
= g_key_file_get_locale_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_COMMENT
, NULL
, NULL
);
316 info
->nodisplay
= g_key_file_get_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
, NULL
) != FALSE
;
317 info
->icon_name
= g_key_file_get_locale_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_ICON
, NULL
, NULL
);
318 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
);
319 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
);
320 info
->try_exec
= try_exec
;
321 info
->exec
= g_key_file_get_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_EXEC
, NULL
);
322 info
->path
= g_key_file_get_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_PATH
, NULL
);
323 info
->terminal
= g_key_file_get_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_TERMINAL
, NULL
) != FALSE
;
324 info
->startup_notify
= g_key_file_get_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY
, NULL
) != FALSE
;
325 info
->no_fuse
= g_key_file_get_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
, "X-GIO-NoFuse", NULL
) != FALSE
;
326 info
->hidden
= g_key_file_get_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_HIDDEN
, NULL
) != FALSE
;
327 info
->categories
= g_key_file_get_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
, G_KEY_FILE_DESKTOP_KEY_CATEGORIES
, NULL
);
332 if (g_path_is_absolute (info
->icon_name
))
336 file
= g_file_new_for_path (info
->icon_name
);
337 info
->icon
= g_file_icon_new (file
);
338 g_object_unref (file
);
344 /* Work around a common mistake in desktop files */
345 if ((p
= strrchr (info
->icon_name
, '.')) != NULL
&&
346 (strcmp (p
, ".png") == 0 ||
347 strcmp (p
, ".xpm") == 0 ||
348 strcmp (p
, ".svg") == 0))
351 info
->icon
= g_themed_icon_new (info
->icon_name
);
356 info
->binary
= binary_from_exec (info
->exec
);
358 if (info
->path
&& info
->path
[0] == '\0')
368 g_desktop_app_info_load_file (GDesktopAppInfo
*self
)
371 gboolean retval
= FALSE
;
373 g_return_val_if_fail (self
->filename
!= NULL
, FALSE
);
375 key_file
= g_key_file_new ();
377 if (g_key_file_load_from_file (key_file
,
382 retval
= g_desktop_app_info_load_from_keyfile (self
, key_file
);
385 g_key_file_free (key_file
);
390 * g_desktop_app_info_new_from_keyfile:
391 * @key_file: an opened #GKeyFile
393 * Creates a new #GDesktopAppInfo.
395 * Returns: a new #GDesktopAppInfo or %NULL on error.
400 g_desktop_app_info_new_from_keyfile (GKeyFile
*key_file
)
402 GDesktopAppInfo
*info
;
404 info
= g_object_new (G_TYPE_DESKTOP_APP_INFO
, NULL
);
405 info
->filename
= NULL
;
406 if (!g_desktop_app_info_load_from_keyfile (info
, key_file
))
408 g_object_unref (info
);
415 * g_desktop_app_info_new_from_filename:
416 * @filename: the path of a desktop file, in the GLib filename encoding
418 * Creates a new #GDesktopAppInfo.
420 * Returns: a new #GDesktopAppInfo or %NULL on error.
423 g_desktop_app_info_new_from_filename (const char *filename
)
425 GDesktopAppInfo
*info
= NULL
;
427 info
= g_object_new (G_TYPE_DESKTOP_APP_INFO
, "filename", filename
, NULL
);
428 if (!g_desktop_app_info_load_file (info
))
430 g_object_unref (info
);
437 * g_desktop_app_info_new:
438 * @desktop_id: the desktop file id
440 * Creates a new #GDesktopAppInfo based on a desktop file id.
442 * A desktop file id is the basename of the desktop file, including the
443 * .desktop extension. GIO is looking for a desktop file with this name
444 * in the <filename>applications</filename> subdirectories of the XDG data
445 * directories (i.e. the directories specified in the
446 * <envar>XDG_DATA_HOME</envar> and <envar>XDG_DATA_DIRS</envar> environment
447 * variables). GIO also supports the prefix-to-subdirectory mapping that is
448 * described in the <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Menu Spec</ulink>
449 * (i.e. a desktop id of kde-foo.desktop will match
450 * <filename>/usr/share/applications/kde/foo.desktop</filename>).
452 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
455 g_desktop_app_info_new (const char *desktop_id
)
457 GDesktopAppInfo
*appinfo
;
458 const char * const *dirs
;
462 dirs
= get_applications_search_path ();
464 basename
= g_strdup (desktop_id
);
466 for (i
= 0; dirs
[i
] != NULL
; i
++)
471 filename
= g_build_filename (dirs
[i
], desktop_id
, NULL
);
472 appinfo
= g_desktop_app_info_new_from_filename (filename
);
478 while ((p
= strchr (p
, '-')) != NULL
)
482 filename
= g_build_filename (dirs
[i
], basename
, NULL
);
483 appinfo
= g_desktop_app_info_new_from_filename (filename
);
498 appinfo
->desktop_id
= g_strdup (desktop_id
);
500 if (g_desktop_app_info_get_is_hidden (appinfo
))
502 g_object_unref (appinfo
);
510 g_desktop_app_info_dup (GAppInfo
*appinfo
)
512 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
513 GDesktopAppInfo
*new_info
;
515 new_info
= g_object_new (G_TYPE_DESKTOP_APP_INFO
, NULL
);
517 new_info
->filename
= g_strdup (info
->filename
);
518 new_info
->desktop_id
= g_strdup (info
->desktop_id
);
520 new_info
->name
= g_strdup (info
->name
);
521 new_info
->generic_name
= g_strdup (info
->generic_name
);
522 new_info
->fullname
= g_strdup (info
->fullname
);
523 new_info
->comment
= g_strdup (info
->comment
);
524 new_info
->nodisplay
= info
->nodisplay
;
525 new_info
->icon_name
= g_strdup (info
->icon_name
);
527 new_info
->icon
= g_object_ref (info
->icon
);
528 new_info
->only_show_in
= g_strdupv (info
->only_show_in
);
529 new_info
->not_show_in
= g_strdupv (info
->not_show_in
);
530 new_info
->try_exec
= g_strdup (info
->try_exec
);
531 new_info
->exec
= g_strdup (info
->exec
);
532 new_info
->binary
= g_strdup (info
->binary
);
533 new_info
->path
= g_strdup (info
->path
);
534 new_info
->hidden
= info
->hidden
;
535 new_info
->terminal
= info
->terminal
;
536 new_info
->startup_notify
= info
->startup_notify
;
538 return G_APP_INFO (new_info
);
542 g_desktop_app_info_equal (GAppInfo
*appinfo1
,
545 GDesktopAppInfo
*info1
= G_DESKTOP_APP_INFO (appinfo1
);
546 GDesktopAppInfo
*info2
= G_DESKTOP_APP_INFO (appinfo2
);
548 if (info1
->desktop_id
== NULL
||
549 info2
->desktop_id
== NULL
)
550 return info1
== info2
;
552 return strcmp (info1
->desktop_id
, info2
->desktop_id
) == 0;
556 g_desktop_app_info_get_id (GAppInfo
*appinfo
)
558 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
560 return info
->desktop_id
;
564 g_desktop_app_info_get_name (GAppInfo
*appinfo
)
566 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
568 if (info
->name
== NULL
)
574 g_desktop_app_info_get_display_name (GAppInfo
*appinfo
)
576 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
578 if (info
->fullname
== NULL
)
579 return g_desktop_app_info_get_name (appinfo
);
580 return info
->fullname
;
584 * g_desktop_app_info_get_is_hidden:
585 * @info: a #GDesktopAppInfo.
587 * A desktop file is hidden if the Hidden key in it is
590 * Returns: %TRUE if hidden, %FALSE otherwise.
593 g_desktop_app_info_get_is_hidden (GDesktopAppInfo
*info
)
599 * g_desktop_app_info_get_filename:
600 * @info: a #GDesktopAppInfo
602 * When @info was created from a known filename, return it. In some
603 * situations such as the #GDesktopAppInfo returned from
604 * g_desktop_app_info_new_from_keyfile(), this function will return %NULL.
606 * Returns: The full path to the file for @info, or %NULL if not known.
610 g_desktop_app_info_get_filename (GDesktopAppInfo
*info
)
612 return info
->filename
;
616 g_desktop_app_info_get_description (GAppInfo
*appinfo
)
618 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
620 return info
->comment
;
624 g_desktop_app_info_get_executable (GAppInfo
*appinfo
)
626 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
632 g_desktop_app_info_get_commandline (GAppInfo
*appinfo
)
634 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
640 g_desktop_app_info_get_icon (GAppInfo
*appinfo
)
642 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
648 * g_desktop_app_info_get_categories:
649 * @info: a #GDesktopAppInfo
651 * Gets the categories from the desktop file.
653 * Returns: The unparsed Categories key from the desktop file;
654 * i.e. no attempt is made to split it by ';' or validate it.
657 g_desktop_app_info_get_categories (GDesktopAppInfo
*info
)
659 return info
->categories
;
663 * g_desktop_app_info_get_generic_name:
664 * @info: a #GDesktopAppInfo
666 * Gets the generic name from the destkop file.
668 * Returns: The value of the GenericName key
671 g_desktop_app_info_get_generic_name (GDesktopAppInfo
*info
)
673 return info
->generic_name
;
677 * g_desktop_app_info_get_nodisplay:
678 * @info: a #GDesktopAppInfo
680 * Gets the value of the NoDisplay key, which helps determine if the
681 * application info should be shown in menus. See
682 * #G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY and g_app_info_should_show().
684 * Returns: The value of the NoDisplay key
689 g_desktop_app_info_get_nodisplay (GDesktopAppInfo
*info
)
691 return info
->nodisplay
;
695 expand_macro_single (char macro
, char *uri
)
701 file
= g_file_new_for_uri (uri
);
702 path
= g_file_get_path (file
);
703 g_object_unref (file
);
709 result
= g_shell_quote (uri
);
714 result
= g_shell_quote (path
);
720 name
= g_path_get_dirname (path
);
721 result
= g_shell_quote (name
);
729 name
= g_path_get_basename (path
);
730 result
= g_shell_quote (name
);
742 expand_macro (char macro
,
744 GDesktopAppInfo
*info
,
747 GList
*uris
= *uri_list
;
749 gboolean force_file_uri
;
750 char force_file_uri_macro
;
753 g_return_if_fail (exec
!= NULL
);
755 /* On %u and %U, pass POSIX file path pointing to the URI via
756 * the FUSE mount in ~/.gvfs. Note that if the FUSE daemon isn't
757 * running or the URI doesn't have a POSIX file path via FUSE
758 * we'll just pass the URI.
760 force_file_uri_macro
= macro
;
761 force_file_uri
= FALSE
;
767 force_file_uri_macro
= 'f';
768 force_file_uri
= TRUE
;
771 force_file_uri_macro
= 'F';
772 force_file_uri
= TRUE
;
788 if (!force_file_uri
||
789 /* Pass URI if it contains an anchor */
790 strchr (uri
, '#') != NULL
)
792 expanded
= expand_macro_single (macro
, uri
);
796 expanded
= expand_macro_single (force_file_uri_macro
, uri
);
797 if (expanded
== NULL
)
798 expanded
= expand_macro_single (macro
, uri
);
803 g_string_append (exec
, expanded
);
819 if (!force_file_uri
||
820 /* Pass URI if it contains an anchor */
821 strchr (uri
, '#') != NULL
)
823 expanded
= expand_macro_single (macro
, uri
);
827 expanded
= expand_macro_single (force_file_uri_macro
, uri
);
828 if (expanded
== NULL
)
829 expanded
= expand_macro_single (macro
, uri
);
834 g_string_append (exec
, expanded
);
840 if (uris
!= NULL
&& expanded
)
841 g_string_append_c (exec
, ' ');
849 g_string_append (exec
, "--icon ");
850 expanded
= g_shell_quote (info
->icon_name
);
851 g_string_append (exec
, expanded
);
859 expanded
= g_shell_quote (info
->name
);
860 g_string_append (exec
, expanded
);
868 expanded
= g_shell_quote (info
->filename
);
869 g_string_append (exec
, expanded
);
874 case 'm': /* deprecated */
878 g_string_append_c (exec
, '%');
886 expand_application_parameters (GDesktopAppInfo
*info
,
892 GList
*uri_list
= *uris
;
893 const char *p
= info
->exec
;
894 GString
*expanded_exec
;
897 if (info
->exec
== NULL
)
899 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
900 _("Desktop file didn't specify Exec field"));
904 expanded_exec
= g_string_new (NULL
);
908 if (p
[0] == '%' && p
[1] != '\0')
910 expand_macro (p
[1], expanded_exec
, info
, uris
);
914 g_string_append_c (expanded_exec
, *p
);
919 /* No file substitutions */
920 if (uri_list
== *uris
&& uri_list
!= NULL
)
922 /* If there is no macro default to %f. This is also what KDE does */
923 g_string_append_c (expanded_exec
, ' ');
924 expand_macro ('f', expanded_exec
, info
, uris
);
927 res
= g_shell_parse_argv (expanded_exec
->str
, argc
, argv
, error
);
928 g_string_free (expanded_exec
, TRUE
);
933 prepend_terminal_to_vector (int *argc
,
940 char **term_argv
= NULL
;
945 g_return_val_if_fail (argc
!= NULL
, FALSE
);
946 g_return_val_if_fail (argv
!= NULL
, FALSE
);
954 /* compute size if not given */
957 for (i
= 0; the_argv
[i
] != NULL
; i
++)
963 term_argv
= g_new0 (char *, 3);
965 check
= g_find_program_in_path ("gnome-terminal");
968 term_argv
[0] = check
;
969 /* Note that gnome-terminal takes -x and
970 * as -e in gnome-terminal is broken we use that. */
971 term_argv
[1] = g_strdup ("-x");
976 check
= g_find_program_in_path ("nxterm");
978 check
= g_find_program_in_path ("color-xterm");
980 check
= g_find_program_in_path ("rxvt");
982 check
= g_find_program_in_path ("xterm");
984 check
= g_find_program_in_path ("dtterm");
987 check
= g_strdup ("xterm");
988 g_warning ("couldn't find a terminal, falling back to xterm");
990 term_argv
[0] = check
;
991 term_argv
[1] = g_strdup ("-e");
994 real_argc
= term_argc
+ *argc
;
995 real_argv
= g_new (char *, real_argc
+ 1);
997 for (i
= 0; i
< term_argc
; i
++)
998 real_argv
[i
] = term_argv
[i
];
1000 for (j
= 0; j
< *argc
; j
++, i
++)
1001 real_argv
[i
] = (char *)the_argv
[j
];
1003 real_argv
[i
] = NULL
;
1009 /* we use g_free here as we sucked all the inner strings
1010 * out from it into real_argv */
1015 #endif /* G_OS_WIN32 */
1019 create_files_for_uris (GList
*uris
)
1026 for (iter
= uris
; iter
; iter
= iter
->next
)
1028 GFile
*file
= g_file_new_for_uri ((char *)iter
->data
);
1029 res
= g_list_prepend (res
, file
);
1032 return g_list_reverse (res
);
1037 GSpawnChildSetupFunc user_setup
;
1038 gpointer user_setup_data
;
1045 child_setup (gpointer user_data
)
1047 ChildSetupData
*data
= user_data
;
1050 g_setenv ("DISPLAY", data
->display
, TRUE
);
1053 g_setenv ("DESKTOP_STARTUP_ID", data
->sn_id
, TRUE
);
1055 if (data
->desktop_file
)
1059 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE", data
->desktop_file
, TRUE
);
1061 g_snprintf (pid
, 20, "%ld", (long)getpid ());
1062 g_setenv ("GIO_LAUNCHED_DESKTOP_FILE_PID", pid
, TRUE
);
1065 if (data
->user_setup
)
1066 data
->user_setup (data
->user_setup_data
);
1070 notify_desktop_launch (GDBusConnection
*session_bus
,
1071 GDesktopAppInfo
*info
,
1073 const char *display
,
1078 GVariantBuilder uri_variant
;
1079 GVariantBuilder extras_variant
;
1081 const char *desktop_file_id
;
1082 const char *gio_desktop_file
;
1084 if (session_bus
== NULL
)
1087 g_variant_builder_init (&uri_variant
, G_VARIANT_TYPE ("as"));
1088 for (iter
= uris
; iter
; iter
= iter
->next
)
1089 g_variant_builder_add (&uri_variant
, "s", iter
->data
);
1091 g_variant_builder_init (&extras_variant
, G_VARIANT_TYPE ("a{sv}"));
1092 if (sn_id
!= NULL
&& g_utf8_validate (sn_id
, -1, NULL
))
1093 g_variant_builder_add (&extras_variant
, "{sv}",
1097 gio_desktop_file
= g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
1098 if (gio_desktop_file
!= NULL
)
1099 g_variant_builder_add (&extras_variant
, "{sv}",
1100 "origin-desktop-file",
1101 g_variant_new_bytestring (gio_desktop_file
));
1102 if (g_get_prgname () != NULL
)
1103 g_variant_builder_add (&extras_variant
, "{sv}",
1105 g_variant_new_bytestring (g_get_prgname ()));
1106 g_variant_builder_add (&extras_variant
, "{sv}",
1109 (gint64
)getpid ()));
1112 desktop_file_id
= info
->filename
;
1113 else if (info
->desktop_id
)
1114 desktop_file_id
= info
->desktop_id
;
1116 desktop_file_id
= "";
1118 msg
= g_dbus_message_new_signal ("/org/gtk/gio/DesktopAppInfo",
1119 "org.gtk.gio.DesktopAppInfo",
1121 g_dbus_message_set_body (msg
, g_variant_new ("(@aysxasa{sv})",
1122 g_variant_new_bytestring (desktop_file_id
),
1123 display
? display
: "",
1127 g_dbus_connection_send_message (session_bus
,
1131 g_object_unref (msg
);
1134 #define _SPAWN_FLAGS_DEFAULT (G_SPAWN_SEARCH_PATH)
1137 _g_desktop_app_info_launch_uris_internal (GAppInfo
*appinfo
,
1139 GAppLaunchContext
*launch_context
,
1140 GSpawnFlags spawn_flags
,
1141 GSpawnChildSetupFunc user_setup
,
1142 gpointer user_setup_data
,
1143 GDesktopAppLaunchCallback pid_callback
,
1144 gpointer pid_callback_data
,
1147 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1148 GDBusConnection
*session_bus
;
1149 gboolean completed
= FALSE
;
1153 ChildSetupData data
;
1155 g_return_val_if_fail (appinfo
!= NULL
, FALSE
);
1159 session_bus
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, NULL
);
1164 GList
*launched_uris
;
1168 if (!expand_application_parameters (info
, &uris
,
1169 &argc
, &argv
, error
))
1172 /* Get the subset of URIs we're launching with this process */
1173 launched_uris
= NULL
;
1174 for (iter
= old_uris
; iter
!= NULL
&& iter
!= uris
; iter
= iter
->next
)
1175 launched_uris
= g_list_prepend (launched_uris
, iter
->data
);
1176 launched_uris
= g_list_reverse (launched_uris
);
1178 if (info
->terminal
&& !prepend_terminal_to_vector (&argc
, &argv
))
1180 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
1181 _("Unable to find terminal required for application"));
1185 data
.user_setup
= user_setup
;
1186 data
.user_setup_data
= user_setup_data
;
1187 data
.display
= NULL
;
1189 data
.desktop_file
= info
->filename
;
1193 GList
*launched_files
= create_files_for_uris (launched_uris
);
1195 data
.display
= g_app_launch_context_get_display (launch_context
,
1199 if (info
->startup_notify
)
1200 data
.sn_id
= g_app_launch_context_get_startup_notify_id (launch_context
,
1203 g_list_foreach (launched_files
, (GFunc
)g_object_unref
, NULL
);
1204 g_list_free (launched_files
);
1207 if (!g_spawn_async (info
->path
,
1217 g_app_launch_context_launch_failed (launch_context
, data
.sn_id
);
1219 g_free (data
.sn_id
);
1220 g_free (data
.display
);
1221 g_list_free (launched_uris
);
1226 if (pid_callback
!= NULL
)
1227 pid_callback (info
, pid
, pid_callback_data
);
1229 notify_desktop_launch (session_bus
,
1236 g_free (data
.sn_id
);
1237 g_free (data
.display
);
1238 g_list_free (launched_uris
);
1243 while (uris
!= NULL
);
1245 /* TODO - need to handle the process exiting immediately
1246 * after launching an app. See http://bugzilla.gnome.org/606960
1248 if (session_bus
!= NULL
)
1250 /* This asynchronous flush holds a reference until it completes,
1251 * which ensures that the following unref won't immediately kill
1252 * the connection if we were the initial owner.
1254 g_dbus_connection_flush (session_bus
, NULL
, NULL
, NULL
);
1255 g_object_unref (session_bus
);
1267 g_desktop_app_info_launch_uris (GAppInfo
*appinfo
,
1269 GAppLaunchContext
*launch_context
,
1272 return _g_desktop_app_info_launch_uris_internal (appinfo
, uris
,
1274 _SPAWN_FLAGS_DEFAULT
,
1275 NULL
, NULL
, NULL
, NULL
,
1280 g_desktop_app_info_supports_uris (GAppInfo
*appinfo
)
1282 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1284 return info
->exec
&&
1285 ((strstr (info
->exec
, "%u") != NULL
) ||
1286 (strstr (info
->exec
, "%U") != NULL
));
1290 g_desktop_app_info_supports_files (GAppInfo
*appinfo
)
1292 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1294 return info
->exec
&&
1295 ((strstr (info
->exec
, "%f") != NULL
) ||
1296 (strstr (info
->exec
, "%F") != NULL
));
1300 g_desktop_app_info_launch (GAppInfo
*appinfo
,
1302 GAppLaunchContext
*launch_context
,
1312 uri
= g_file_get_uri (files
->data
);
1313 uris
= g_list_prepend (uris
, uri
);
1314 files
= files
->next
;
1317 uris
= g_list_reverse (uris
);
1319 res
= g_desktop_app_info_launch_uris (appinfo
, uris
, launch_context
, error
);
1321 g_list_foreach (uris
, (GFunc
)g_free
, NULL
);
1328 * g_desktop_app_info_launch_uris_as_manager:
1329 * @appinfo: a #GDesktopAppInfo
1330 * @uris: (element-type utf8): List of URIs
1331 * @launch_context: a #GAppLaunchContext
1332 * @spawn_flags: #GSpawnFlags, used for each process
1333 * @user_setup: (scope call): a #GSpawnChildSetupFunc, used once for
1335 * @user_setup_data: (closure user_setup): User data for @user_setup
1336 * @pid_callback: (scope call): Callback for child processes
1337 * @pid_callback_data: (closure pid_callback): User data for @callback
1338 * @error: return location for a #GError, or %NULL
1340 * This function performs the equivalent of g_app_info_launch_uris(),
1341 * but is intended primarily for operating system components that
1342 * launch applications. Ordinary applications should use
1343 * g_app_info_launch_uris().
1345 * In contrast to g_app_info_launch_uris(), all processes created will
1346 * always be run directly as children as if by the UNIX fork()/exec()
1349 * This guarantee allows additional control over the exact environment
1350 * of the child processes, which is provided via a setup function
1351 * @setup, as well as the process identifier of each child process via
1352 * @pid_callback. See g_spawn_async() for more information about the
1353 * semantics of the @setup function.
1355 * Returns: %TRUE on successful launch, %FALSE otherwise.
1358 g_desktop_app_info_launch_uris_as_manager (GDesktopAppInfo
*appinfo
,
1360 GAppLaunchContext
*launch_context
,
1361 GSpawnFlags spawn_flags
,
1362 GSpawnChildSetupFunc user_setup
,
1363 gpointer user_setup_data
,
1364 GDesktopAppLaunchCallback pid_callback
,
1365 gpointer pid_callback_data
,
1368 return _g_desktop_app_info_launch_uris_internal ((GAppInfo
*)appinfo
,
1379 G_LOCK_DEFINE_STATIC (g_desktop_env
);
1380 static gchar
*g_desktop_env
= NULL
;
1383 * g_desktop_app_info_set_desktop_env:
1384 * @desktop_env: a string specifying what desktop this is
1386 * Sets the name of the desktop that the application is running in.
1387 * This is used by g_app_info_should_show() to evaluate the
1388 * <literal>OnlyShowIn</literal> and <literal>NotShowIn</literal>
1389 * desktop entry fields.
1391 * The <ulink url="http://standards.freedesktop.org/menu-spec/latest/">Desktop
1392 * Menu specification</ulink> recognizes the following:
1394 * <member>GNOME</member>
1395 * <member>KDE</member>
1396 * <member>ROX</member>
1397 * <member>XFCE</member>
1398 * <member>Old</member>
1401 * Should be called only once; subsequent calls are ignored.
1404 g_desktop_app_info_set_desktop_env (const gchar
*desktop_env
)
1406 G_LOCK (g_desktop_env
);
1408 g_desktop_env
= g_strdup (desktop_env
);
1409 G_UNLOCK (g_desktop_env
);
1413 g_desktop_app_info_should_show (GAppInfo
*appinfo
)
1415 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1417 const gchar
*desktop_env
;
1420 if (info
->nodisplay
)
1423 G_LOCK (g_desktop_env
);
1424 desktop_env
= g_desktop_env
;
1425 G_UNLOCK (g_desktop_env
);
1427 if (info
->only_show_in
)
1429 if (desktop_env
== NULL
)
1433 for (i
= 0; info
->only_show_in
[i
] != NULL
; i
++)
1435 if (strcmp (info
->only_show_in
[i
], desktop_env
) == 0)
1445 if (info
->not_show_in
&& desktop_env
)
1447 for (i
= 0; info
->not_show_in
[i
] != NULL
; i
++)
1449 if (strcmp (info
->not_show_in
[i
], desktop_env
) == 0)
1463 ensure_dir (DirType type
,
1466 char *path
, *display_name
;
1469 if (type
== APP_DIR
)
1470 path
= g_build_filename (g_get_user_data_dir (), "applications", NULL
);
1472 path
= g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL
);
1475 if (g_mkdir_with_parents (path
, 0700) == 0)
1479 display_name
= g_filename_display_name (path
);
1480 if (type
== APP_DIR
)
1481 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errsv
),
1482 _("Can't create user application configuration folder %s: %s"),
1483 display_name
, g_strerror (errsv
));
1485 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errsv
),
1486 _("Can't create user MIME configuration folder %s: %s"),
1487 display_name
, g_strerror (errsv
));
1489 g_free (display_name
);
1496 update_mimeapps_list (const char *desktop_id
,
1497 const char *content_type
,
1498 UpdateMimeFlags flags
,
1501 char *dirname
, *filename
, *string
;
1503 gboolean load_succeeded
, res
, explicit_default
;
1504 char **old_list
, **list
;
1506 gsize length
, data_size
;
1509 char **content_types
;
1511 /* Don't add both at start and end */
1512 g_assert (!((flags
& UPDATE_MIME_SET_DEFAULT
) &&
1513 (flags
& UPDATE_MIME_SET_NON_DEFAULT
)));
1515 dirname
= ensure_dir (APP_DIR
, error
);
1519 filename
= g_build_filename (dirname
, "mimeapps.list", NULL
);
1522 key_file
= g_key_file_new ();
1523 load_succeeded
= g_key_file_load_from_file (key_file
, filename
, G_KEY_FILE_NONE
, NULL
);
1524 if (!load_succeeded
|| !g_key_file_has_group (key_file
, ADDED_ASSOCIATIONS_GROUP
))
1526 g_key_file_free (key_file
);
1527 key_file
= g_key_file_new ();
1532 content_types
= g_new (char *, 2);
1533 content_types
[0] = g_strdup (content_type
);
1534 content_types
[1] = NULL
;
1538 content_types
= g_key_file_get_keys (key_file
, DEFAULT_APPLICATIONS_GROUP
, NULL
, NULL
);
1541 explicit_default
= FALSE
;
1543 for (k
= 0; content_types
&& content_types
[k
]; k
++)
1545 /* set as default, if requested so */
1546 string
= g_key_file_get_string (key_file
,
1547 DEFAULT_APPLICATIONS_GROUP
,
1551 if (g_strcmp0 (string
, desktop_id
) != 0 &&
1552 (flags
& UPDATE_MIME_SET_DEFAULT
))
1555 string
= g_strdup (desktop_id
);
1557 /* add in the non-default list too, if it's not already there */
1558 flags
|= UPDATE_MIME_SET_NON_DEFAULT
;
1561 if (string
== NULL
|| desktop_id
== NULL
)
1562 g_key_file_remove_key (key_file
,
1563 DEFAULT_APPLICATIONS_GROUP
,
1568 g_key_file_set_string (key_file
,
1569 DEFAULT_APPLICATIONS_GROUP
,
1573 explicit_default
= TRUE
;
1581 /* reuse the list from above */
1585 g_strfreev (content_types
);
1586 content_types
= g_key_file_get_keys (key_file
, ADDED_ASSOCIATIONS_GROUP
, NULL
, NULL
);
1589 for (k
= 0; content_types
&& content_types
[k
]; k
++)
1591 /* Add to the right place in the list */
1594 old_list
= g_key_file_get_string_list (key_file
, ADDED_ASSOCIATIONS_GROUP
,
1595 content_types
[k
], &length
, NULL
);
1597 list
= g_new (char *, 1 + length
+ 1);
1601 /* if we're adding a last-used hint, just put the application in front of the list */
1602 if (flags
& UPDATE_MIME_SET_LAST_USED
)
1604 /* avoid adding this again as non-default later */
1605 if (flags
& UPDATE_MIME_SET_NON_DEFAULT
)
1606 flags
^= UPDATE_MIME_SET_NON_DEFAULT
;
1608 list
[i
++] = g_strdup (desktop_id
);
1613 for (j
= 0; old_list
[j
] != NULL
; j
++)
1615 if (g_strcmp0 (old_list
[j
], desktop_id
) != 0)
1617 /* rewrite other entries if they're different from the new one */
1618 list
[i
++] = g_strdup (old_list
[j
]);
1620 else if (flags
& UPDATE_MIME_SET_NON_DEFAULT
)
1622 /* we encountered an old entry which is equal to the one we're adding as non-default,
1623 * don't change its position in the list.
1625 flags
^= UPDATE_MIME_SET_NON_DEFAULT
;
1626 list
[i
++] = g_strdup (old_list
[j
]);
1631 /* add it at the end of the list */
1632 if (flags
& UPDATE_MIME_SET_NON_DEFAULT
)
1633 list
[i
++] = g_strdup (desktop_id
);
1637 g_strfreev (old_list
);
1639 if (list
[0] == NULL
|| desktop_id
== NULL
)
1640 g_key_file_remove_key (key_file
,
1641 ADDED_ASSOCIATIONS_GROUP
,
1646 g_key_file_set_string_list (key_file
,
1647 ADDED_ASSOCIATIONS_GROUP
,
1649 (const char * const *)list
, i
);
1651 /* if we had no explicit default set, we should add the system default to the
1652 * list, to avoid overriding it with applications from this list.
1654 if (!explicit_default
)
1656 system_list
= get_all_desktop_entries_for_mime_type (content_type
, (const char **) list
, FALSE
, NULL
);
1658 if (system_list
!= NULL
)
1660 string
= system_list
->data
;
1662 g_key_file_set_string (key_file
,
1663 DEFAULT_APPLICATIONS_GROUP
,
1668 g_list_free_full (system_list
, g_free
);
1677 /* reuse the list from above */
1681 g_strfreev (content_types
);
1682 content_types
= g_key_file_get_keys (key_file
, REMOVED_ASSOCIATIONS_GROUP
, NULL
, NULL
);
1685 for (k
= 0; content_types
&& content_types
[k
]; k
++)
1687 /* Remove from removed associations group (unless remove) */
1690 old_list
= g_key_file_get_string_list (key_file
, REMOVED_ASSOCIATIONS_GROUP
,
1691 content_types
[k
], &length
, NULL
);
1693 list
= g_new (char *, 1 + length
+ 1);
1696 if (flags
& UPDATE_MIME_REMOVE
)
1697 list
[i
++] = g_strdup (desktop_id
);
1700 for (j
= 0; old_list
[j
] != NULL
; j
++)
1702 if (g_strcmp0 (old_list
[j
], desktop_id
) != 0)
1703 list
[i
++] = g_strdup (old_list
[j
]);
1708 g_strfreev (old_list
);
1710 if (list
[0] == NULL
|| desktop_id
== NULL
)
1711 g_key_file_remove_key (key_file
,
1712 REMOVED_ASSOCIATIONS_GROUP
,
1716 g_key_file_set_string_list (key_file
,
1717 REMOVED_ASSOCIATIONS_GROUP
,
1719 (const char * const *)list
, i
);
1724 g_strfreev (content_types
);
1726 data
= g_key_file_to_data (key_file
, &data_size
, error
);
1727 g_key_file_free (key_file
);
1729 res
= g_file_set_contents (filename
, data
, data_size
, error
);
1731 mime_info_cache_reload (NULL
);
1740 g_desktop_app_info_set_as_last_used_for_type (GAppInfo
*appinfo
,
1741 const char *content_type
,
1744 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1746 if (!g_desktop_app_info_ensure_saved (info
, error
))
1749 /* both add support for the content type and set as last used */
1750 return update_mimeapps_list (info
->desktop_id
, content_type
,
1751 UPDATE_MIME_SET_NON_DEFAULT
|
1752 UPDATE_MIME_SET_LAST_USED
,
1757 g_desktop_app_info_set_as_default_for_type (GAppInfo
*appinfo
,
1758 const char *content_type
,
1761 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1763 if (!g_desktop_app_info_ensure_saved (info
, error
))
1766 return update_mimeapps_list (info
->desktop_id
, content_type
,
1767 UPDATE_MIME_SET_DEFAULT
,
1772 update_program_done (GPid pid
,
1776 /* Did the application exit correctly */
1777 if (WIFEXITED (status
) &&
1778 WEXITSTATUS (status
) == 0)
1780 /* Here we could clean out any caches in use */
1785 run_update_command (char *command
,
1794 GError
*error
= NULL
;
1797 argv
[1] = g_build_filename (g_get_user_data_dir (), subdir
, NULL
);
1799 if (g_spawn_async ("/", argv
,
1801 G_SPAWN_SEARCH_PATH
|
1802 G_SPAWN_STDOUT_TO_DEV_NULL
|
1803 G_SPAWN_STDERR_TO_DEV_NULL
|
1804 G_SPAWN_DO_NOT_REAP_CHILD
,
1805 NULL
, NULL
, /* No setup function */
1808 g_child_watch_add (pid
, update_program_done
, NULL
);
1811 /* If we get an error at this point, it's quite likely the user doesn't
1812 * have an installed copy of either 'update-mime-database' or
1813 * 'update-desktop-database'. I don't think we want to popup an error
1814 * dialog at this point, so we just do a g_warning to give the user a
1815 * chance of debugging it.
1817 g_warning ("%s", error
->message
);
1824 g_desktop_app_info_set_as_default_for_extension (GAppInfo
*appinfo
,
1825 const char *extension
,
1828 char *filename
, *basename
, *mimetype
;
1832 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (appinfo
), error
))
1835 dirname
= ensure_dir (MIMETYPE_DIR
, error
);
1839 basename
= g_strdup_printf ("user-extension-%s.xml", extension
);
1840 filename
= g_build_filename (dirname
, basename
, NULL
);
1844 mimetype
= g_strdup_printf ("application/x-extension-%s", extension
);
1846 if (!g_file_test (filename
, G_FILE_TEST_EXISTS
))
1851 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1852 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1853 " <mime-type type=\"%s\">\n"
1854 " <comment>%s document</comment>\n"
1855 " <glob pattern=\"*.%s\"/>\n"
1857 "</mime-info>\n", mimetype
, extension
, extension
);
1859 g_file_set_contents (filename
, contents
, -1, NULL
);
1862 run_update_command ("update-mime-database", "mime");
1866 res
= g_desktop_app_info_set_as_default_for_type (appinfo
,
1876 g_desktop_app_info_add_supports_type (GAppInfo
*appinfo
,
1877 const char *content_type
,
1880 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1882 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info
), error
))
1885 return update_mimeapps_list (info
->desktop_id
, content_type
,
1886 UPDATE_MIME_SET_NON_DEFAULT
,
1891 g_desktop_app_info_can_remove_supports_type (GAppInfo
*appinfo
)
1897 g_desktop_app_info_remove_supports_type (GAppInfo
*appinfo
,
1898 const char *content_type
,
1901 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
1903 if (!g_desktop_app_info_ensure_saved (G_DESKTOP_APP_INFO (info
), error
))
1906 return update_mimeapps_list (info
->desktop_id
, content_type
,
1912 g_desktop_app_info_ensure_saved (GDesktopAppInfo
*info
,
1918 char *data
, *desktop_id
;
1923 if (info
->filename
!= NULL
)
1926 /* This is only used for object created with
1927 * g_app_info_create_from_commandline. All other
1928 * object should have a filename
1931 dirname
= ensure_dir (APP_DIR
, error
);
1935 key_file
= g_key_file_new ();
1937 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1938 "Encoding", "UTF-8");
1939 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1940 G_KEY_FILE_DESKTOP_KEY_VERSION
, "1.0");
1941 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1942 G_KEY_FILE_DESKTOP_KEY_TYPE
,
1943 G_KEY_FILE_DESKTOP_TYPE_APPLICATION
);
1945 g_key_file_set_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1946 G_KEY_FILE_DESKTOP_KEY_TERMINAL
, TRUE
);
1947 if (info
->nodisplay
)
1948 g_key_file_set_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1949 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
, TRUE
);
1951 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1952 G_KEY_FILE_DESKTOP_KEY_EXEC
, info
->exec
);
1954 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1955 G_KEY_FILE_DESKTOP_KEY_NAME
, info
->name
);
1957 if (info
->generic_name
!= NULL
)
1958 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1959 GENERIC_NAME_KEY
, info
->generic_name
);
1961 if (info
->fullname
!= NULL
)
1962 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1963 FULL_NAME_KEY
, info
->fullname
);
1965 g_key_file_set_string (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1966 G_KEY_FILE_DESKTOP_KEY_COMMENT
, info
->comment
);
1968 g_key_file_set_boolean (key_file
, G_KEY_FILE_DESKTOP_GROUP
,
1969 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
, TRUE
);
1971 data
= g_key_file_to_data (key_file
, &data_size
, NULL
);
1972 g_key_file_free (key_file
);
1974 desktop_id
= g_strdup_printf ("userapp-%s-XXXXXX.desktop", info
->name
);
1975 filename
= g_build_filename (dirname
, desktop_id
, NULL
);
1976 g_free (desktop_id
);
1979 fd
= g_mkstemp (filename
);
1984 display_name
= g_filename_display_name (filename
);
1985 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
1986 _("Can't create user desktop file %s"), display_name
);
1987 g_free (display_name
);
1993 desktop_id
= g_path_get_basename (filename
);
1997 res
= g_file_set_contents (filename
, data
, data_size
, error
);
2000 g_free (desktop_id
);
2005 info
->filename
= filename
;
2006 info
->desktop_id
= desktop_id
;
2008 run_update_command ("update-desktop-database", "applications");
2014 g_desktop_app_info_can_delete (GAppInfo
*appinfo
)
2016 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
2020 if (strstr (info
->filename
, "/userapp-"))
2021 return g_access (info
->filename
, W_OK
) == 0;
2028 g_desktop_app_info_delete (GAppInfo
*appinfo
)
2030 GDesktopAppInfo
*info
= G_DESKTOP_APP_INFO (appinfo
);
2034 if (g_remove (info
->filename
) == 0)
2036 update_mimeapps_list (info
->desktop_id
, NULL
,
2040 g_free (info
->filename
);
2041 info
->filename
= NULL
;
2042 g_free (info
->desktop_id
);
2043 info
->desktop_id
= NULL
;
2053 * g_app_info_create_from_commandline:
2054 * @commandline: the commandline to use
2055 * @application_name: (allow-none): the application name, or %NULL to use @commandline
2056 * @flags: flags that can specify details of the created #GAppInfo
2057 * @error: a #GError location to store the error occuring, %NULL to ignore.
2059 * Creates a new #GAppInfo from the given information.
2061 * Returns: (transfer full): new #GAppInfo for given command.
2064 g_app_info_create_from_commandline (const char *commandline
,
2065 const char *application_name
,
2066 GAppInfoCreateFlags flags
,
2071 GDesktopAppInfo
*info
;
2073 g_return_val_if_fail (commandline
, NULL
);
2075 info
= g_object_new (G_TYPE_DESKTOP_APP_INFO
, NULL
);
2077 info
->filename
= NULL
;
2078 info
->desktop_id
= NULL
;
2080 info
->terminal
= (flags
& G_APP_INFO_CREATE_NEEDS_TERMINAL
) != 0;
2081 info
->startup_notify
= (flags
& G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION
) != 0;
2082 info
->hidden
= FALSE
;
2083 if ((flags
& G_APP_INFO_CREATE_SUPPORTS_URIS
) != 0)
2084 info
->exec
= g_strconcat (commandline
, " %u", NULL
);
2086 info
->exec
= g_strconcat (commandline
, " %f", NULL
);
2087 info
->nodisplay
= TRUE
;
2088 info
->binary
= binary_from_exec (info
->exec
);
2090 if (application_name
)
2091 info
->name
= g_strdup (application_name
);
2094 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
2095 split
= g_strsplit (commandline
, " ", 2);
2096 basename
= split
[0] ? g_path_get_basename (split
[0]) : NULL
;
2098 info
->name
= basename
;
2099 if (info
->name
== NULL
)
2100 info
->name
= g_strdup ("custom");
2102 info
->comment
= g_strdup_printf (_("Custom definition for %s"), info
->name
);
2104 return G_APP_INFO (info
);
2108 g_desktop_app_info_iface_init (GAppInfoIface
*iface
)
2110 iface
->dup
= g_desktop_app_info_dup
;
2111 iface
->equal
= g_desktop_app_info_equal
;
2112 iface
->get_id
= g_desktop_app_info_get_id
;
2113 iface
->get_name
= g_desktop_app_info_get_name
;
2114 iface
->get_description
= g_desktop_app_info_get_description
;
2115 iface
->get_executable
= g_desktop_app_info_get_executable
;
2116 iface
->get_icon
= g_desktop_app_info_get_icon
;
2117 iface
->launch
= g_desktop_app_info_launch
;
2118 iface
->supports_uris
= g_desktop_app_info_supports_uris
;
2119 iface
->supports_files
= g_desktop_app_info_supports_files
;
2120 iface
->launch_uris
= g_desktop_app_info_launch_uris
;
2121 iface
->should_show
= g_desktop_app_info_should_show
;
2122 iface
->set_as_default_for_type
= g_desktop_app_info_set_as_default_for_type
;
2123 iface
->set_as_default_for_extension
= g_desktop_app_info_set_as_default_for_extension
;
2124 iface
->add_supports_type
= g_desktop_app_info_add_supports_type
;
2125 iface
->can_remove_supports_type
= g_desktop_app_info_can_remove_supports_type
;
2126 iface
->remove_supports_type
= g_desktop_app_info_remove_supports_type
;
2127 iface
->can_delete
= g_desktop_app_info_can_delete
;
2128 iface
->do_delete
= g_desktop_app_info_delete
;
2129 iface
->get_commandline
= g_desktop_app_info_get_commandline
;
2130 iface
->get_display_name
= g_desktop_app_info_get_display_name
;
2131 iface
->set_as_last_used_for_type
= g_desktop_app_info_set_as_last_used_for_type
;
2135 app_info_in_list (GAppInfo
*info
,
2138 while (list
!= NULL
)
2140 if (g_app_info_equal (info
, list
->data
))
2148 * g_app_info_get_recommended_for_type:
2149 * @content_type: the content type to find a #GAppInfo for
2151 * Gets a list of recommended #GAppInfos for a given content type, i.e.
2152 * those applications which claim to support the given content type exactly,
2153 * and not by MIME type subclassing.
2154 * Note that the first application of the list is the last used one, i.e.
2155 * the last one for which #g_app_info_set_as_last_used_for_type has been
2158 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2159 * for given @content_type or %NULL on error.
2164 g_app_info_get_recommended_for_type (const gchar
*content_type
)
2166 GList
*desktop_entries
, *l
;
2168 GDesktopAppInfo
*info
;
2170 g_return_val_if_fail (content_type
!= NULL
, NULL
);
2172 desktop_entries
= get_all_desktop_entries_for_mime_type (content_type
, NULL
, FALSE
, NULL
);
2175 for (l
= desktop_entries
; l
!= NULL
; l
= l
->next
)
2177 char *desktop_entry
= l
->data
;
2179 info
= g_desktop_app_info_new (desktop_entry
);
2182 if (app_info_in_list (G_APP_INFO (info
), infos
))
2183 g_object_unref (info
);
2185 infos
= g_list_prepend (infos
, info
);
2187 g_free (desktop_entry
);
2190 g_list_free (desktop_entries
);
2192 return g_list_reverse (infos
);
2196 * g_app_info_get_fallback_for_type:
2197 * @content_type: the content type to find a #GAppInfo for
2199 * Gets a list of fallback #GAppInfos for a given content type, i.e.
2200 * those applications which claim to support the given content type
2201 * by MIME type subclassing and not directly.
2203 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2204 * for given @content_type or %NULL on error.
2209 g_app_info_get_fallback_for_type (const gchar
*content_type
)
2211 GList
*desktop_entries
, *l
;
2212 GList
*infos
, *recommended_infos
;
2213 GDesktopAppInfo
*info
;
2215 g_return_val_if_fail (content_type
!= NULL
, NULL
);
2217 desktop_entries
= get_all_desktop_entries_for_mime_type (content_type
, NULL
, TRUE
, NULL
);
2218 recommended_infos
= g_app_info_get_recommended_for_type (content_type
);
2221 for (l
= desktop_entries
; l
!= NULL
; l
= l
->next
)
2223 char *desktop_entry
= l
->data
;
2225 info
= g_desktop_app_info_new (desktop_entry
);
2228 if (app_info_in_list (G_APP_INFO (info
), infos
) ||
2229 app_info_in_list (G_APP_INFO (info
), recommended_infos
))
2230 g_object_unref (info
);
2232 infos
= g_list_prepend (infos
, info
);
2234 g_free (desktop_entry
);
2237 g_list_free (desktop_entries
);
2238 g_list_free_full (recommended_infos
, g_object_unref
);
2240 return g_list_reverse (infos
);
2244 * g_app_info_get_all_for_type:
2245 * @content_type: the content type to find a #GAppInfo for
2247 * Gets a list of all #GAppInfos for a given content type.
2249 * Returns: (element-type GAppInfo) (transfer full): #GList of #GAppInfos
2250 * for given @content_type or %NULL on error.
2253 g_app_info_get_all_for_type (const char *content_type
)
2255 GList
*desktop_entries
, *l
;
2257 char *user_default
= NULL
;
2258 GDesktopAppInfo
*info
;
2260 g_return_val_if_fail (content_type
!= NULL
, NULL
);
2262 desktop_entries
= get_all_desktop_entries_for_mime_type (content_type
, NULL
, TRUE
, &user_default
);
2265 /* put the user default in front of the list, for compatibility */
2266 if (user_default
!= NULL
)
2268 info
= g_desktop_app_info_new (user_default
);
2271 infos
= g_list_prepend (infos
, info
);
2274 g_free (user_default
);
2276 for (l
= desktop_entries
; l
!= NULL
; l
= l
->next
)
2278 char *desktop_entry
= l
->data
;
2280 info
= g_desktop_app_info_new (desktop_entry
);
2283 if (app_info_in_list (G_APP_INFO (info
), infos
))
2284 g_object_unref (info
);
2286 infos
= g_list_prepend (infos
, info
);
2288 g_free (desktop_entry
);
2291 g_list_free (desktop_entries
);
2293 return g_list_reverse (infos
);
2297 * g_app_info_reset_type_associations:
2298 * @content_type: a content type
2300 * Removes all changes to the type associations done by
2301 * g_app_info_set_as_default_for_type(),
2302 * g_app_info_set_as_default_for_extension(),
2303 * g_app_info_add_supports_type() or g_app_info_remove_supports_type().
2308 g_app_info_reset_type_associations (const char *content_type
)
2310 update_mimeapps_list (NULL
, content_type
,
2316 * g_app_info_get_default_for_type:
2317 * @content_type: the content type to find a #GAppInfo for
2318 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
2321 * Gets the #GAppInfo that corresponds to a given content type.
2323 * Returns: (transfer full): #GAppInfo for given @content_type or
2327 g_app_info_get_default_for_type (const char *content_type
,
2328 gboolean must_support_uris
)
2330 GList
*desktop_entries
, *l
;
2331 char *user_default
= NULL
;
2334 g_return_val_if_fail (content_type
!= NULL
, NULL
);
2336 desktop_entries
= get_all_desktop_entries_for_mime_type (content_type
, NULL
, TRUE
, &user_default
);
2340 if (user_default
!= NULL
)
2342 info
= (GAppInfo
*) g_desktop_app_info_new (user_default
);
2346 if (must_support_uris
&& !g_app_info_supports_uris (info
))
2348 g_object_unref (info
);
2354 g_free (user_default
);
2358 g_list_free_full (desktop_entries
, g_free
);
2362 /* pick the first from the other list that matches our URI
2365 for (l
= desktop_entries
; l
!= NULL
; l
= l
->next
)
2367 char *desktop_entry
= l
->data
;
2369 info
= (GAppInfo
*)g_desktop_app_info_new (desktop_entry
);
2372 if (must_support_uris
&& !g_app_info_supports_uris (info
))
2374 g_object_unref (info
);
2382 g_list_free_full (desktop_entries
, g_free
);
2388 * g_app_info_get_default_for_uri_scheme:
2389 * @uri_scheme: a string containing a URI scheme.
2391 * Gets the default application for launching applications
2392 * using this URI scheme. A URI scheme is the initial part
2393 * of the URI, up to but not including the ':', e.g. "http",
2396 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
2399 g_app_info_get_default_for_uri_scheme (const char *uri_scheme
)
2402 char *content_type
, *scheme_down
;
2404 scheme_down
= g_ascii_strdown (uri_scheme
, -1);
2405 content_type
= g_strdup_printf ("x-scheme-handler/%s", scheme_down
);
2406 g_free (scheme_down
);
2407 app_info
= g_app_info_get_default_for_type (content_type
, FALSE
);
2408 g_free (content_type
);
2414 get_apps_from_dir (GHashTable
*apps
,
2415 const char *dirname
,
2419 const char *basename
;
2420 char *filename
, *subprefix
, *desktop_id
;
2422 GDesktopAppInfo
*appinfo
;
2424 dir
= g_dir_open (dirname
, 0, NULL
);
2427 while ((basename
= g_dir_read_name (dir
)) != NULL
)
2429 filename
= g_build_filename (dirname
, basename
, NULL
);
2430 if (g_str_has_suffix (basename
, ".desktop"))
2432 desktop_id
= g_strconcat (prefix
, basename
, NULL
);
2434 /* Use _extended so we catch NULLs too (hidden) */
2435 if (!g_hash_table_lookup_extended (apps
, desktop_id
, NULL
, NULL
))
2437 appinfo
= g_desktop_app_info_new_from_filename (filename
);
2440 if (appinfo
&& g_desktop_app_info_get_is_hidden (appinfo
))
2442 g_object_unref (appinfo
);
2447 if (appinfo
|| hidden
)
2449 g_hash_table_insert (apps
, g_strdup (desktop_id
), appinfo
);
2453 /* Reuse instead of strdup here */
2454 appinfo
->desktop_id
= desktop_id
;
2459 g_free (desktop_id
);
2463 if (g_file_test (filename
, G_FILE_TEST_IS_DIR
))
2465 subprefix
= g_strconcat (prefix
, basename
, "-", NULL
);
2466 get_apps_from_dir (apps
, filename
, subprefix
);
2478 * g_app_info_get_all:
2480 * Gets a list of all of the applications currently registered
2483 * For desktop files, this includes applications that have
2484 * <literal>NoDisplay=true</literal> set or are excluded from
2485 * display by means of <literal>OnlyShowIn</literal> or
2486 * <literal>NotShowIn</literal>. See g_app_info_should_show().
2487 * The returned list does not include applications which have
2488 * the <literal>Hidden</literal> key set.
2490 * Returns: (element-type GAppInfo) (transfer full): a newly allocated #GList of references to #GAppInfo<!---->s.
2493 g_app_info_get_all (void)
2495 const char * const *dirs
;
2497 GHashTableIter iter
;
2502 dirs
= get_applications_search_path ();
2504 apps
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
2508 for (i
= 0; dirs
[i
] != NULL
; i
++)
2509 get_apps_from_dir (apps
, dirs
[i
], "");
2513 g_hash_table_iter_init (&iter
, apps
);
2514 while (g_hash_table_iter_next (&iter
, NULL
, &value
))
2517 infos
= g_list_prepend (infos
, value
);
2520 g_hash_table_destroy (apps
);
2522 return g_list_reverse (infos
);
2525 /* Cacheing of mimeinfo.cache and defaults.list files */
2529 GHashTable
*mime_info_cache_map
;
2530 GHashTable
*defaults_list_map
;
2531 GHashTable
*mimeapps_list_added_map
;
2532 GHashTable
*mimeapps_list_removed_map
;
2533 GHashTable
*mimeapps_list_defaults_map
;
2534 time_t mime_info_cache_timestamp
;
2535 time_t defaults_list_timestamp
;
2536 time_t mimeapps_list_timestamp
;
2540 GList
*dirs
; /* mimeinfo.cache and defaults.list */
2541 GHashTable
*global_defaults_cache
; /* global results of defaults.list lookup and validation */
2542 time_t last_stat_time
;
2543 guint should_ping_mime_monitor
: 1;
2546 static MimeInfoCache
*mime_info_cache
= NULL
;
2547 G_LOCK_DEFINE_STATIC (mime_info_cache
);
2549 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir
*dir
,
2550 const char *mime_type
,
2551 char **new_desktop_file_ids
);
2553 static MimeInfoCache
* mime_info_cache_new (void);
2556 destroy_info_cache_value (gpointer key
,
2560 g_list_foreach (value
, (GFunc
)g_free
, NULL
);
2561 g_list_free (value
);
2565 destroy_info_cache_map (GHashTable
*info_cache_map
)
2567 g_hash_table_foreach (info_cache_map
, (GHFunc
)destroy_info_cache_value
, NULL
);
2568 g_hash_table_destroy (info_cache_map
);
2572 mime_info_cache_dir_out_of_date (MimeInfoCacheDir
*dir
,
2573 const char *cache_file
,
2579 filename
= g_build_filename (dir
->path
, cache_file
, NULL
);
2581 if (g_stat (filename
, &buf
) < 0)
2588 if (buf
.st_mtime
!= *timestamp
)
2594 /* Call with lock held */
2596 remove_all (gpointer key
,
2605 mime_info_cache_blow_global_cache (void)
2607 g_hash_table_foreach_remove (mime_info_cache
->global_defaults_cache
,
2612 mime_info_cache_dir_init (MimeInfoCacheDir
*dir
)
2616 gchar
*filename
, **mime_types
;
2623 if (dir
->mime_info_cache_map
!= NULL
&&
2624 !mime_info_cache_dir_out_of_date (dir
, "mimeinfo.cache",
2625 &dir
->mime_info_cache_timestamp
))
2628 if (dir
->mime_info_cache_map
!= NULL
)
2629 destroy_info_cache_map (dir
->mime_info_cache_map
);
2631 dir
->mime_info_cache_map
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
2632 (GDestroyNotify
) g_free
,
2635 key_file
= g_key_file_new ();
2637 filename
= g_build_filename (dir
->path
, "mimeinfo.cache", NULL
);
2639 if (g_stat (filename
, &buf
) < 0)
2642 if (dir
->mime_info_cache_timestamp
> 0)
2643 mime_info_cache
->should_ping_mime_monitor
= TRUE
;
2645 dir
->mime_info_cache_timestamp
= buf
.st_mtime
;
2647 g_key_file_load_from_file (key_file
, filename
, G_KEY_FILE_NONE
, &load_error
);
2652 if (load_error
!= NULL
)
2655 mime_types
= g_key_file_get_keys (key_file
, MIME_CACHE_GROUP
,
2658 if (load_error
!= NULL
)
2661 for (i
= 0; mime_types
[i
] != NULL
; i
++)
2663 gchar
**desktop_file_ids
;
2664 char *unaliased_type
;
2665 desktop_file_ids
= g_key_file_get_string_list (key_file
,
2671 if (desktop_file_ids
== NULL
)
2674 unaliased_type
= _g_unix_content_type_unalias (mime_types
[i
]);
2675 mime_info_cache_dir_add_desktop_entries (dir
,
2678 g_free (unaliased_type
);
2680 g_strfreev (desktop_file_ids
);
2683 g_strfreev (mime_types
);
2684 g_key_file_free (key_file
);
2689 g_key_file_free (key_file
);
2691 if (mime_types
!= NULL
)
2692 g_strfreev (mime_types
);
2695 g_error_free (load_error
);
2699 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir
*dir
)
2703 gchar
*filename
, **mime_types
;
2704 char *unaliased_type
;
2705 char **desktop_file_ids
;
2712 if (dir
->defaults_list_map
!= NULL
&&
2713 !mime_info_cache_dir_out_of_date (dir
, "defaults.list",
2714 &dir
->defaults_list_timestamp
))
2717 if (dir
->defaults_list_map
!= NULL
)
2718 g_hash_table_destroy (dir
->defaults_list_map
);
2719 dir
->defaults_list_map
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
2720 g_free
, (GDestroyNotify
)g_strfreev
);
2723 key_file
= g_key_file_new ();
2725 filename
= g_build_filename (dir
->path
, "defaults.list", NULL
);
2726 if (g_stat (filename
, &buf
) < 0)
2729 if (dir
->defaults_list_timestamp
> 0)
2730 mime_info_cache
->should_ping_mime_monitor
= TRUE
;
2732 dir
->defaults_list_timestamp
= buf
.st_mtime
;
2734 g_key_file_load_from_file (key_file
, filename
, G_KEY_FILE_NONE
, &load_error
);
2738 if (load_error
!= NULL
)
2741 mime_types
= g_key_file_get_keys (key_file
, DEFAULT_APPLICATIONS_GROUP
,
2743 if (mime_types
!= NULL
)
2745 for (i
= 0; mime_types
[i
] != NULL
; i
++)
2747 desktop_file_ids
= g_key_file_get_string_list (key_file
,
2748 DEFAULT_APPLICATIONS_GROUP
,
2752 if (desktop_file_ids
== NULL
)
2755 unaliased_type
= _g_unix_content_type_unalias (mime_types
[i
]);
2756 g_hash_table_replace (dir
->defaults_list_map
,
2761 g_strfreev (mime_types
);
2764 g_key_file_free (key_file
);
2769 g_key_file_free (key_file
);
2771 if (mime_types
!= NULL
)
2772 g_strfreev (mime_types
);
2775 g_error_free (load_error
);
2779 mime_info_cache_dir_init_mimeapps_list (MimeInfoCacheDir
*dir
)
2783 gchar
*filename
, **mime_types
;
2784 char *unaliased_type
;
2785 char **desktop_file_ids
;
2793 if (dir
->mimeapps_list_added_map
!= NULL
&&
2794 !mime_info_cache_dir_out_of_date (dir
, "mimeapps.list",
2795 &dir
->mimeapps_list_timestamp
))
2798 if (dir
->mimeapps_list_added_map
!= NULL
)
2799 g_hash_table_destroy (dir
->mimeapps_list_added_map
);
2800 dir
->mimeapps_list_added_map
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
2801 g_free
, (GDestroyNotify
)g_strfreev
);
2803 if (dir
->mimeapps_list_removed_map
!= NULL
)
2804 g_hash_table_destroy (dir
->mimeapps_list_removed_map
);
2805 dir
->mimeapps_list_removed_map
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
2806 g_free
, (GDestroyNotify
)g_strfreev
);
2808 if (dir
->mimeapps_list_defaults_map
!= NULL
)
2809 g_hash_table_destroy (dir
->mimeapps_list_defaults_map
);
2810 dir
->mimeapps_list_defaults_map
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
2813 key_file
= g_key_file_new ();
2815 filename
= g_build_filename (dir
->path
, "mimeapps.list", NULL
);
2816 if (g_stat (filename
, &buf
) < 0)
2819 if (dir
->mimeapps_list_timestamp
> 0)
2820 mime_info_cache
->should_ping_mime_monitor
= TRUE
;
2822 dir
->mimeapps_list_timestamp
= buf
.st_mtime
;
2824 g_key_file_load_from_file (key_file
, filename
, G_KEY_FILE_NONE
, &load_error
);
2828 if (load_error
!= NULL
)
2831 mime_types
= g_key_file_get_keys (key_file
, ADDED_ASSOCIATIONS_GROUP
,
2833 if (mime_types
!= NULL
)
2835 for (i
= 0; mime_types
[i
] != NULL
; i
++)
2837 desktop_file_ids
= g_key_file_get_string_list (key_file
,
2838 ADDED_ASSOCIATIONS_GROUP
,
2842 if (desktop_file_ids
== NULL
)
2845 unaliased_type
= _g_unix_content_type_unalias (mime_types
[i
]);
2846 g_hash_table_replace (dir
->mimeapps_list_added_map
,
2851 g_strfreev (mime_types
);
2854 mime_types
= g_key_file_get_keys (key_file
, REMOVED_ASSOCIATIONS_GROUP
,
2856 if (mime_types
!= NULL
)
2858 for (i
= 0; mime_types
[i
] != NULL
; i
++)
2860 desktop_file_ids
= g_key_file_get_string_list (key_file
,
2861 REMOVED_ASSOCIATIONS_GROUP
,
2865 if (desktop_file_ids
== NULL
)
2868 unaliased_type
= _g_unix_content_type_unalias (mime_types
[i
]);
2869 g_hash_table_replace (dir
->mimeapps_list_removed_map
,
2874 g_strfreev (mime_types
);
2877 mime_types
= g_key_file_get_keys (key_file
, DEFAULT_APPLICATIONS_GROUP
,
2879 if (mime_types
!= NULL
)
2881 for (i
= 0; mime_types
[i
] != NULL
; i
++)
2883 desktop_id
= g_key_file_get_string (key_file
,
2884 DEFAULT_APPLICATIONS_GROUP
,
2887 if (desktop_id
== NULL
)
2890 unaliased_type
= _g_unix_content_type_unalias (mime_types
[i
]);
2891 g_hash_table_replace (dir
->mimeapps_list_defaults_map
,
2896 g_strfreev (mime_types
);
2899 g_key_file_free (key_file
);
2904 g_key_file_free (key_file
);
2906 if (mime_types
!= NULL
)
2907 g_strfreev (mime_types
);
2910 g_error_free (load_error
);
2913 static MimeInfoCacheDir
*
2914 mime_info_cache_dir_new (const char *path
)
2916 MimeInfoCacheDir
*dir
;
2918 dir
= g_new0 (MimeInfoCacheDir
, 1);
2919 dir
->path
= g_strdup (path
);
2925 mime_info_cache_dir_free (MimeInfoCacheDir
*dir
)
2930 if (dir
->mime_info_cache_map
!= NULL
)
2932 destroy_info_cache_map (dir
->mime_info_cache_map
);
2933 dir
->mime_info_cache_map
= NULL
;
2937 if (dir
->defaults_list_map
!= NULL
)
2939 g_hash_table_destroy (dir
->defaults_list_map
);
2940 dir
->defaults_list_map
= NULL
;
2943 if (dir
->mimeapps_list_added_map
!= NULL
)
2945 g_hash_table_destroy (dir
->mimeapps_list_added_map
);
2946 dir
->mimeapps_list_added_map
= NULL
;
2949 if (dir
->mimeapps_list_removed_map
!= NULL
)
2951 g_hash_table_destroy (dir
->mimeapps_list_removed_map
);
2952 dir
->mimeapps_list_removed_map
= NULL
;
2955 if (dir
->mimeapps_list_defaults_map
!= NULL
)
2957 g_hash_table_destroy (dir
->mimeapps_list_defaults_map
);
2958 dir
->mimeapps_list_defaults_map
= NULL
;
2965 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir
*dir
,
2966 const char *mime_type
,
2967 char **new_desktop_file_ids
)
2969 GList
*desktop_file_ids
;
2972 desktop_file_ids
= g_hash_table_lookup (dir
->mime_info_cache_map
,
2975 for (i
= 0; new_desktop_file_ids
[i
] != NULL
; i
++)
2977 if (!g_list_find_custom (desktop_file_ids
, new_desktop_file_ids
[i
], (GCompareFunc
) strcmp
))
2978 desktop_file_ids
= g_list_append (desktop_file_ids
,
2979 g_strdup (new_desktop_file_ids
[i
]));
2982 g_hash_table_insert (dir
->mime_info_cache_map
, g_strdup (mime_type
), desktop_file_ids
);
2986 mime_info_cache_init_dir_lists (void)
2988 const char * const *dirs
;
2991 mime_info_cache
= mime_info_cache_new ();
2993 dirs
= get_applications_search_path ();
2995 for (i
= 0; dirs
[i
] != NULL
; i
++)
2997 MimeInfoCacheDir
*dir
;
2999 dir
= mime_info_cache_dir_new (dirs
[i
]);
3003 mime_info_cache_dir_init (dir
);
3004 mime_info_cache_dir_init_defaults_list (dir
);
3005 mime_info_cache_dir_init_mimeapps_list (dir
);
3007 mime_info_cache
->dirs
= g_list_append (mime_info_cache
->dirs
, dir
);
3013 mime_info_cache_update_dir_lists (void)
3017 tmp
= mime_info_cache
->dirs
;
3021 MimeInfoCacheDir
*dir
= (MimeInfoCacheDir
*) tmp
->data
;
3023 /* No need to do this if we had file monitors... */
3024 mime_info_cache_blow_global_cache ();
3025 mime_info_cache_dir_init (dir
);
3026 mime_info_cache_dir_init_defaults_list (dir
);
3027 mime_info_cache_dir_init_mimeapps_list (dir
);
3034 mime_info_cache_init (void)
3036 G_LOCK (mime_info_cache
);
3037 if (mime_info_cache
== NULL
)
3038 mime_info_cache_init_dir_lists ();
3044 if (now
>= mime_info_cache
->last_stat_time
+ 10)
3046 mime_info_cache_update_dir_lists ();
3047 mime_info_cache
->last_stat_time
= now
;
3051 if (mime_info_cache
->should_ping_mime_monitor
)
3053 /* g_idle_add (emit_mime_changed, NULL); */
3054 mime_info_cache
->should_ping_mime_monitor
= FALSE
;
3057 G_UNLOCK (mime_info_cache
);
3060 static MimeInfoCache
*
3061 mime_info_cache_new (void)
3063 MimeInfoCache
*cache
;
3065 cache
= g_new0 (MimeInfoCache
, 1);
3067 cache
->global_defaults_cache
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
3068 (GDestroyNotify
) g_free
,
3069 (GDestroyNotify
) g_free
);
3074 mime_info_cache_free (MimeInfoCache
*cache
)
3079 g_list_foreach (cache
->dirs
,
3080 (GFunc
) mime_info_cache_dir_free
,
3082 g_list_free (cache
->dirs
);
3083 g_hash_table_destroy (cache
->global_defaults_cache
);
3088 * mime_info_cache_reload:
3089 * @dir: directory path which needs reloading.
3091 * Reload the mime information for the @dir.
3094 mime_info_cache_reload (const char *dir
)
3096 /* FIXME: just reload the dir that needs reloading,
3097 * don't blow the whole cache
3099 if (mime_info_cache
!= NULL
)
3101 G_LOCK (mime_info_cache
);
3102 mime_info_cache_free (mime_info_cache
);
3103 mime_info_cache
= NULL
;
3104 G_UNLOCK (mime_info_cache
);
3109 append_desktop_entry (GList
*list
,
3110 const char *desktop_entry
,
3111 GList
*removed_entries
)
3113 /* Add if not already in list, and valid */
3114 if (!g_list_find_custom (list
, desktop_entry
, (GCompareFunc
) strcmp
) &&
3115 !g_list_find_custom (removed_entries
, desktop_entry
, (GCompareFunc
) strcmp
))
3116 list
= g_list_prepend (list
, g_strdup (desktop_entry
));
3122 * get_all_desktop_entries_for_mime_type:
3123 * @mime_type: a mime type.
3124 * @except: NULL or a strv list
3126 * Returns all the desktop ids for @mime_type. The desktop files
3127 * are listed in an order so that default applications are listed before
3128 * non-default ones, and handlers for inherited mimetypes are listed
3129 * after the base ones.
3131 * Optionally doesn't list the desktop ids given in the @except
3133 * Return value: a #GList containing the desktop ids which claim
3134 * to handle @mime_type.
3137 get_all_desktop_entries_for_mime_type (const char *base_mime_type
,
3138 const char **except
,
3139 gboolean include_fallback
,
3140 char **explicit_default
)
3142 GList
*desktop_entries
, *removed_entries
, *list
, *dir_list
, *tmp
;
3143 MimeInfoCacheDir
*dir
;
3144 char *mime_type
, *default_entry
= NULL
;
3147 char **default_entries
;
3148 char **removed_associations
;
3153 mime_info_cache_init ();
3155 if (include_fallback
)
3157 /* collect all ancestors */
3158 mime_types
= _g_unix_content_type_get_parents (base_mime_type
);
3159 array
= g_ptr_array_new ();
3160 for (i
= 0; mime_types
[i
]; i
++)
3161 g_ptr_array_add (array
, mime_types
[i
]);
3162 g_free (mime_types
);
3163 for (i
= 0; i
< array
->len
; i
++)
3165 anc
= _g_unix_content_type_get_parents (g_ptr_array_index (array
, i
));
3166 for (j
= 0; anc
[j
]; j
++)
3168 for (k
= 0; k
< array
->len
; k
++)
3170 if (strcmp (anc
[j
], g_ptr_array_index (array
, k
)) == 0)
3173 if (k
== array
->len
) /* not found */
3174 g_ptr_array_add (array
, g_strdup (anc
[j
]));
3178 g_ptr_array_add (array
, NULL
);
3179 mime_types
= (char **)g_ptr_array_free (array
, FALSE
);
3183 mime_types
= g_malloc0 (2 * sizeof (gchar
*));
3184 mime_types
[0] = g_strdup (base_mime_type
);
3185 mime_types
[1] = NULL
;
3188 G_LOCK (mime_info_cache
);
3190 removed_entries
= NULL
;
3191 desktop_entries
= NULL
;
3193 for (i
= 0; except
!= NULL
&& except
[i
] != NULL
; i
++)
3194 removed_entries
= g_list_prepend (removed_entries
, g_strdup (except
[i
]));
3196 for (i
= 0; mime_types
[i
] != NULL
; i
++)
3198 mime_type
= mime_types
[i
];
3200 /* Go through all apps listed in user and system dirs */
3201 for (dir_list
= mime_info_cache
->dirs
;
3203 dir_list
= dir_list
->next
)
3205 dir
= dir_list
->data
;
3207 /* Pick the explicit default application if we got no result earlier
3208 * (ie, for more specific mime types)
3210 if (desktop_entries
== NULL
)
3212 entry
= g_hash_table_lookup (dir
->mimeapps_list_defaults_map
, mime_type
);
3216 /* Save the default entry if it's the first one we encounter */
3217 if (default_entry
== NULL
)
3218 default_entry
= g_strdup (entry
);
3222 /* Then added associations from mimeapps.list */
3223 default_entries
= g_hash_table_lookup (dir
->mimeapps_list_added_map
, mime_type
);
3224 for (j
= 0; default_entries
!= NULL
&& default_entries
[j
] != NULL
; j
++)
3225 desktop_entries
= append_desktop_entry (desktop_entries
, default_entries
[j
], removed_entries
);
3227 /* Then removed associations from mimeapps.list */
3228 removed_associations
= g_hash_table_lookup (dir
->mimeapps_list_removed_map
, mime_type
);
3229 for (j
= 0; removed_associations
!= NULL
&& removed_associations
[j
] != NULL
; j
++)
3230 removed_entries
= append_desktop_entry (removed_entries
, removed_associations
[j
], NULL
);
3232 /* Then system defaults (or old per-user config) (using removed associations from this dir or earlier) */
3233 default_entries
= g_hash_table_lookup (dir
->defaults_list_map
, mime_type
);
3234 for (j
= 0; default_entries
!= NULL
&& default_entries
[j
] != NULL
; j
++)
3235 desktop_entries
= append_desktop_entry (desktop_entries
, default_entries
[j
], removed_entries
);
3238 /* Go through all entries that support the mimetype */
3239 for (dir_list
= mime_info_cache
->dirs
;
3241 dir_list
= dir_list
->next
)
3243 dir
= dir_list
->data
;
3245 list
= g_hash_table_lookup (dir
->mime_info_cache_map
, mime_type
);
3246 for (tmp
= list
; tmp
!= NULL
; tmp
= tmp
->next
)
3247 desktop_entries
= append_desktop_entry (desktop_entries
, tmp
->data
, removed_entries
);
3251 G_UNLOCK (mime_info_cache
);
3253 g_strfreev (mime_types
);
3255 if (explicit_default
!= NULL
)
3256 *explicit_default
= default_entry
;
3258 g_free (default_entry
);
3260 g_list_foreach (removed_entries
, (GFunc
)g_free
, NULL
);
3261 g_list_free (removed_entries
);
3263 desktop_entries
= g_list_reverse (desktop_entries
);
3265 return desktop_entries
;
3268 /* GDesktopAppInfoLookup interface: */
3270 typedef GDesktopAppInfoLookupIface GDesktopAppInfoLookupInterface
;
3271 G_DEFINE_INTERFACE (GDesktopAppInfoLookup
, g_desktop_app_info_lookup
, G_TYPE_OBJECT
)
3274 g_desktop_app_info_lookup_default_init (GDesktopAppInfoLookupInterface
*iface
)
3279 * g_desktop_app_info_lookup_get_default_for_uri_scheme:
3280 * @lookup: a #GDesktopAppInfoLookup
3281 * @uri_scheme: a string containing a URI scheme.
3283 * Gets the default application for launching applications
3284 * using this URI scheme for a particular GDesktopAppInfoLookup
3287 * The GDesktopAppInfoLookup interface and this function is used
3288 * to implement g_app_info_get_default_for_uri_scheme() backends
3289 * in a GIO module. There is no reason for applications to use it
3290 * directly. Applications should use g_app_info_get_default_for_uri_scheme().
3292 * Returns: (transfer full): #GAppInfo for given @uri_scheme or %NULL on error.
3294 * Deprecated: The #GDesktopAppInfoLookup interface is deprecated and unused by gio.
3297 g_desktop_app_info_lookup_get_default_for_uri_scheme (GDesktopAppInfoLookup
*lookup
,
3298 const char *uri_scheme
)
3300 GDesktopAppInfoLookupIface
*iface
;
3302 g_return_val_if_fail (G_IS_DESKTOP_APP_INFO_LOOKUP (lookup
), NULL
);
3304 iface
= G_DESKTOP_APP_INFO_LOOKUP_GET_IFACE (lookup
);
3306 return (* iface
->get_default_for_uri_scheme
) (lookup
, uri_scheme
);