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 #include "gcontenttypeprivate.h"
32 #include "gdesktopappinfo.h"
34 #include "gthemedicon.h"
35 #include "gfileicon.h"
36 #include <glib/gstdio.h>
41 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
42 #define MIME_CACHE_GROUP "MIME Cache"
44 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
46 static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
47 static void mime_info_cache_reload (const char *dir);
49 struct _GDesktopAppInfo
51 GObject parent_instance;
57 /* FIXME: what about GenericName ? */
71 guint startup_notify : 1;
72 /* FIXME: what about StartupWMClass ? */
75 #define g_desktop_app_info_get_type _g_desktop_app_info_get_type
76 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
77 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
78 g_desktop_app_info_iface_init))
81 search_path_init (gpointer data)
84 const char * const *data_dirs;
85 const char *user_data_dir;
88 data_dirs = g_get_system_data_dirs ();
89 length = g_strv_length ((char **) data_dirs);
91 args = g_new (char *, length + 2);
94 user_data_dir = g_get_user_data_dir ();
95 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
96 for (i = 0; i < length; i++)
97 args[j++] = g_build_filename (data_dirs[i],
98 "applications", NULL);
104 static const char * const *
105 get_applications_search_path (void)
107 static GOnce once_init = G_ONCE_INIT;
108 return g_once (&once_init, search_path_init, NULL);
112 g_desktop_app_info_finalize (GObject *object)
114 GDesktopAppInfo *info;
116 info = G_DESKTOP_APP_INFO (object);
118 g_free (info->desktop_id);
119 g_free (info->filename);
121 g_free (info->comment);
122 g_free (info->icon_name);
124 g_object_unref (info->icon);
125 g_strfreev (info->only_show_in);
126 g_strfreev (info->not_show_in);
127 g_free (info->try_exec);
129 g_free (info->binary);
132 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
136 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 gobject_class->finalize = g_desktop_app_info_finalize;
144 g_desktop_app_info_init (GDesktopAppInfo *local)
149 * g_desktop_app_info_new_from_filename:
150 * @filename: a string containing a file name.
152 * Returns: a new #GDesktopAppInfo or %NULL on error.
155 _g_desktop_app_info_new_from_filename (const char *filename)
157 GDesktopAppInfo *info;
163 key_file = g_key_file_new ();
165 if (!g_key_file_load_from_file (key_file,
171 start_group = g_key_file_get_start_group (key_file);
172 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
174 g_free (start_group);
177 g_free (start_group);
179 type = g_key_file_get_string (key_file,
180 G_KEY_FILE_DESKTOP_GROUP,
181 G_KEY_FILE_DESKTOP_KEY_TYPE,
183 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
190 try_exec = g_key_file_get_string (key_file,
191 G_KEY_FILE_DESKTOP_GROUP,
192 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
197 t = g_find_program_in_path (try_exec);
206 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
207 info->filename = g_strdup (filename);
209 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
210 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
211 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
212 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
213 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);
214 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);
215 info->try_exec = try_exec;
216 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
217 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
218 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
219 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
220 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
225 if (g_path_is_absolute (info->icon_name))
229 file = g_file_new_for_path (info->icon_name);
230 info->icon = g_file_icon_new (file);
231 g_object_unref (file);
234 info->icon = g_themed_icon_new (info->icon_name);
245 while (*p != ' ' && *p != 0)
248 info->binary = g_strndup (start, p - start);
255 * g_desktop_app_info_new:
258 * Returns: a new #GDesktopAppInfo.
261 _g_desktop_app_info_new (const char *desktop_id)
263 GDesktopAppInfo *appinfo;
264 const char * const *dirs;
267 dirs = get_applications_search_path ();
269 for (i = 0; dirs[i] != NULL; i++)
275 filename = g_build_filename (dirs[i], desktop_id, NULL);
276 appinfo = _g_desktop_app_info_new_from_filename (filename);
283 basename = g_strdup (desktop_id);
285 while ((p = strchr (p, '-')) != NULL)
289 filename = g_build_filename (dirs[i], basename, NULL);
290 appinfo = _g_desktop_app_info_new_from_filename (filename);
305 appinfo->desktop_id = g_strdup (desktop_id);
307 if (_g_desktop_app_info_get_is_hidden (appinfo))
309 g_object_unref (appinfo);
317 g_desktop_app_info_dup (GAppInfo *appinfo)
319 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
320 GDesktopAppInfo *new_info;
322 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
324 new_info->filename = g_strdup (info->filename);
325 new_info->desktop_id = g_strdup (info->desktop_id);
327 new_info->name = g_strdup (info->name);
328 new_info->comment = g_strdup (info->comment);
329 new_info->nodisplay = info->nodisplay;
330 new_info->icon_name = g_strdup (info->icon_name);
331 new_info->icon = g_object_ref (info->icon);
332 new_info->only_show_in = g_strdupv (info->only_show_in);
333 new_info->not_show_in = g_strdupv (info->not_show_in);
334 new_info->try_exec = g_strdup (info->try_exec);
335 new_info->exec = g_strdup (info->exec);
336 new_info->binary = g_strdup (info->binary);
337 new_info->path = g_strdup (info->path);
338 new_info->hidden = info->hidden;
339 new_info->terminal = info->terminal;
340 new_info->startup_notify = info->startup_notify;
342 return G_APP_INFO (new_info);
346 g_desktop_app_info_equal (GAppInfo *appinfo1,
349 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
350 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
352 if (info1->desktop_id == NULL ||
353 info2->desktop_id == NULL)
356 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
360 g_desktop_app_info_get_id (GAppInfo *appinfo)
362 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
364 return info->desktop_id;
368 g_desktop_app_info_get_name (GAppInfo *appinfo)
370 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
372 if (info->name == NULL)
378 * g_desktop_app_info_get_is_hidden:
381 * Returns: %TRUE if hidden, %FALSE otherwise.
384 _g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
390 g_desktop_app_info_get_description (GAppInfo *appinfo)
392 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
394 return info->comment;
398 g_desktop_app_info_get_executable (GAppInfo *appinfo)
400 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
406 g_desktop_app_info_get_icon (GAppInfo *appinfo)
408 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
414 expand_macro_single (char macro, GFile *file)
419 path = g_file_get_path (file);
420 uri = g_file_get_uri (file);
426 result = g_shell_quote (uri);
431 result = g_shell_quote (path);
436 result = g_shell_quote (g_path_get_dirname (path));
441 result = g_shell_quote (g_path_get_basename (path));
452 expand_macro (char macro, GString *exec, GDesktopAppInfo *info, GList **file_list)
454 GList *files = *file_list;
457 g_return_if_fail (exec != NULL);
467 expanded = expand_macro_single (macro, files->data);
470 g_string_append (exec, expanded);
484 expanded = expand_macro_single (macro, files->data);
487 g_string_append (exec, expanded);
493 if (files != NULL && expanded)
494 g_string_append_c (exec, ' ');
502 g_string_append (exec, "--icon ");
503 g_string_append (exec, info->icon_name);
509 g_string_append (exec, info->name);
514 g_string_append (exec, info->filename);
517 case 'm': /* deprecated */
521 g_string_append_c (exec, '%');
529 expand_application_parameters (GDesktopAppInfo *info,
535 GList *file_list = *files;
536 const char *p = info->exec;
537 GString *expanded_exec = g_string_new (NULL);
540 if (info->exec == NULL)
542 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
543 _("Desktop file didn't specify Exec field"));
549 if (p[0] == '%' && p[1] != '\0')
551 expand_macro (p[1], expanded_exec, info, files);
555 g_string_append_c (expanded_exec, *p);
560 /* No file substitutions */
561 if (file_list == *files && file_list != NULL)
563 /* If there is no macro default to %f. This is also what KDE does */
564 g_string_append_c (expanded_exec, ' ');
565 expand_macro ('f', expanded_exec, info, files);
568 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
569 g_string_free (expanded_exec, TRUE);
574 prepend_terminal_to_vector (int *argc,
581 char **term_argv = NULL;
586 g_return_val_if_fail (argc != NULL, FALSE);
587 g_return_val_if_fail (argv != NULL, FALSE);
595 /* compute size if not given */
598 for (i = 0; the_argv[i] != NULL; i++)
604 term_argv = g_new0 (char *, 3);
606 check = g_find_program_in_path ("gnome-terminal");
609 term_argv[0] = check;
610 /* Note that gnome-terminal takes -x and
611 * as -e in gnome-terminal is broken we use that. */
612 term_argv[1] = g_strdup ("-x");
617 check = g_find_program_in_path ("nxterm");
619 check = g_find_program_in_path ("color-xterm");
621 check = g_find_program_in_path ("rxvt");
623 check = g_find_program_in_path ("xterm");
625 check = g_find_program_in_path ("dtterm");
628 check = g_strdup ("xterm");
629 g_warning ("couldn't find a terminal, falling back to xterm");
631 term_argv[0] = check;
632 term_argv[1] = g_strdup ("-e");
635 real_argc = term_argc + *argc;
636 real_argv = g_new (char *, real_argc + 1);
638 for (i = 0; i < term_argc; i++)
639 real_argv[i] = term_argv[i];
641 for (j = 0; j < *argc; j++, i++)
642 real_argv[i] = (char *)the_argv[j];
650 /* we use g_free here as we sucked all the inner strings
651 * out from it into real_argv */
656 #endif /* G_OS_WIN32 */
659 /* '=' is the new '\0'.
660 * DO NOT CALL unless at least one string ends with '='
663 is_env (const char *a,
668 if (*a == 0 || *b == 0)
681 /* free with g_strfreev */
683 replace_env_var (char **old_environ,
685 const char *new_value)
687 int length, new_length;
688 int index, new_index;
692 /* do two things at once:
693 * - discover the length of the environment ('length')
694 * - find the location (if any) of the env var ('index')
697 for (length = 0; old_environ[length]; length++)
699 /* if we already have it in our environment, replace */
700 if (is_env (old_environ[length], env_var))
705 /* no current env var, no desired env value.
708 if (new_value == NULL && index == -1)
711 /* in all cases now, we will be using a modified environment.
712 * determine its length and allocated it.
715 * new_index = location to insert, if any
716 * new_length = length of the new array
717 * new_environ = the pointer array for the new environment
720 if (new_value == NULL && index >= 0)
722 /* in this case, we will be removing an entry */
723 new_length = length - 1;
726 else if (new_value != NULL && index < 0)
728 /* in this case, we will be adding an entry to the end */
729 new_length = length + 1;
733 /* in this case, we will be replacing the existing entry */
739 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
740 new_environ[new_length] = NULL;
742 /* now we do the copying.
743 * for each entry in the new environment, we decide what to do
747 for (new_i = 0; new_i < new_length; new_i++)
749 if (new_i == new_index)
751 /* insert our new item */
752 new_environ[new_i] = g_strconcat (env_var,
757 /* if we had an old entry, skip it now */
763 /* if this is the old DESKTOP_STARTUP_ID, skip it */
767 /* copy an old item */
768 new_environ[new_i] = g_strdup (old_environ[i]);
773 g_strfreev (old_environ);
779 dup_list_segment (GList *start,
785 while (start != NULL && start != end)
787 res = g_list_prepend (res, start->data);
791 return g_list_reverse (res);
795 g_desktop_app_info_launch (GAppInfo *appinfo,
797 GAppLaunchContext *launch_context,
800 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
801 gboolean completed = FALSE;
803 GList *launched_files;
810 g_return_val_if_fail (appinfo != NULL, FALSE);
818 if (!expand_application_parameters (info, &files,
819 &argc, &argv, error))
822 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
824 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
825 _("Unable to find terminal required for application"));
832 launched_files = dup_list_segment (old_files, files);
834 display = g_app_launch_context_get_display (launch_context,
839 if (info->startup_notify)
840 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
844 if (display || sn_id)
849 envp = replace_env_var (envp,
854 envp = replace_env_var (envp,
855 "DESKTOP_STARTUP_ID",
861 g_list_free (launched_files);
864 if (!g_spawn_async (info->path, /* working directory */
867 G_SPAWN_SEARCH_PATH /* flags */,
868 NULL /* child_setup */,
870 NULL /* child_pid */,
875 g_app_launch_context_launch_failed (launch_context, sn_id);
889 while (files != NULL);
901 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
903 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
906 (strstr (info->exec, "%u") != NULL) ||
907 (strstr (info->exec, "%U") != NULL);
911 g_desktop_app_info_supports_xdg_startup_notify (GAppInfo *appinfo)
913 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
915 return info->startup_notify;
919 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
921 GAppLaunchContext *launch_context,
931 file = g_file_new_for_uri (uris->data);
933 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
936 files = g_list_prepend (files, file);
939 files = g_list_reverse (files);
941 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
943 g_list_foreach (files, (GFunc)g_object_unref, NULL);
950 g_desktop_app_info_should_show (GAppInfo *appinfo,
951 const char *desktop_env)
953 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
960 if (info->only_show_in)
962 if (desktop_env == NULL)
966 for (i = 0; info->only_show_in[i] != NULL; i++)
968 if (strcmp (info->only_show_in[i], desktop_env) == 0)
978 if (info->not_show_in && desktop_env)
980 for (i = 0; info->not_show_in[i] != NULL; i++)
982 if (strcmp (info->not_show_in[i], desktop_env) == 0)
996 ensure_dir (DirType type,
999 char *path, *display_name;
1002 if (type == APP_DIR)
1004 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1008 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1012 if (g_mkdir_with_parents (path, 0700) == 0)
1016 display_name = g_filename_display_name (path);
1017 if (type == APP_DIR)
1019 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1020 _("Can't create user application configuration folder %s: %s"),
1021 display_name, g_strerror (err));
1025 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1026 _("Can't create user MIME configuration folder %s: %s"),
1027 display_name, g_strerror (err));
1030 g_free (display_name);
1037 update_default_list (const char *desktop_id, const char *content_type, gboolean add, GError **error)
1039 char *dirname, *filename;
1041 gboolean load_succeeded, res;
1044 gsize length, data_size;
1048 dirname = ensure_dir (APP_DIR, error);
1052 filename = g_build_filename (dirname, "defaults.list", NULL);
1055 key_file = g_key_file_new ();
1056 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1057 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1059 g_key_file_free (key_file);
1060 key_file = g_key_file_new ();
1064 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1065 content_type, &length, NULL);
1067 list = g_new (char *, 1 + length + 1);
1071 list[i++] = g_strdup (desktop_id);
1074 for (j = 0; old_list[j] != NULL; j++)
1076 if (strcmp (old_list[j], desktop_id) != 0)
1077 list[i++] = g_strdup (old_list[j]);
1082 g_strfreev (old_list);
1084 g_key_file_set_string_list (key_file,
1085 DEFAULT_APPLICATIONS_GROUP,
1087 (const char * const *)list, i);
1091 data = g_key_file_to_data (key_file, &data_size, error);
1092 g_key_file_free (key_file);
1094 res = g_file_set_contents (filename, data, data_size, error);
1096 mime_info_cache_reload (NULL);
1105 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1106 const char *content_type,
1109 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1111 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1114 return update_default_list (info->desktop_id, content_type, TRUE, error);
1118 update_program_done (GPid pid,
1122 /* Did the application exit correctly */
1123 if (WIFEXITED (status) &&
1124 WEXITSTATUS (status) == 0)
1126 /* Here we could clean out any caches in use */
1131 run_update_command (char *command,
1140 GError *error = NULL;
1143 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1145 if (g_spawn_async ("/", argv,
1147 G_SPAWN_SEARCH_PATH |
1148 G_SPAWN_STDOUT_TO_DEV_NULL |
1149 G_SPAWN_STDERR_TO_DEV_NULL |
1150 G_SPAWN_DO_NOT_REAP_CHILD,
1151 NULL, NULL, /* No setup function */
1154 g_child_watch_add (pid, update_program_done, NULL);
1157 /* If we get an error at this point, it's quite likely the user doesn't
1158 * have an installed copy of either 'update-mime-database' or
1159 * 'update-desktop-database'. I don't think we want to popup an error
1160 * dialog at this point, so we just do a g_warning to give the user a
1161 * chance of debugging it.
1163 g_warning ("%s", error->message);
1170 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1171 const char *extension,
1174 char *filename, *basename, *mimetype;
1178 dirname = ensure_dir (MIMETYPE_DIR, error);
1182 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1183 filename = g_build_filename (dirname, basename, NULL);
1187 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1189 if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
1193 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1194 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1195 " <mime-type type=\"%s\">\n"
1196 " <comment>%s document</comment>\n"
1197 " <glob pattern=\"*.%s\"/>\n"
1199 "</mime-info>\n", mimetype, extension, extension);
1201 g_file_set_contents (filename, contents, -1, NULL);
1204 run_update_command ("update-mime-database", "mime");
1208 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1218 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1219 const char *content_type,
1222 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1224 char *new_mimetypes, *old_mimetypes, *content;
1228 keyfile = g_key_file_new ();
1229 if (!g_key_file_load_from_file (keyfile, info->filename,
1230 G_KEY_FILE_KEEP_COMMENTS |
1231 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1233 g_key_file_free (keyfile);
1237 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1238 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1239 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1240 g_free (old_mimetypes);
1241 g_free (new_mimetypes);
1243 content = g_key_file_to_data (keyfile, NULL, NULL);
1244 g_key_file_free (keyfile);
1246 dirname = ensure_dir (APP_DIR, error);
1253 filename = g_build_filename (dirname, info->desktop_id, NULL);
1256 if (!g_file_set_contents (filename, content, -1, error))
1265 run_update_command ("update-desktop-database", "applications");
1270 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1272 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1275 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1276 return g_str_has_prefix (info->filename, user_dirname);
1280 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1281 const char *content_type,
1284 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1286 char *new_mimetypes, *old_mimetypes, *content;
1291 keyfile = g_key_file_new ();
1292 if (!g_key_file_load_from_file (keyfile, info->filename,
1293 G_KEY_FILE_KEEP_COMMENTS |
1294 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1296 g_key_file_free (keyfile);
1300 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1301 new_mimetypes = g_strdup (old_mimetypes);
1304 found = strstr (new_mimetypes, content_type);
1305 if (found && *(found + strlen (content_type)) == ';')
1307 char *rest = found + strlen (content_type) + 1;
1308 memmove (found, rest, strlen (rest) + 1);
1310 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1311 g_free (old_mimetypes);
1312 g_free (new_mimetypes);
1314 content = g_key_file_to_data (keyfile, NULL, NULL);
1315 g_key_file_free (keyfile);
1317 dirname = ensure_dir (APP_DIR, error);
1324 filename = g_build_filename (dirname, info->desktop_id, NULL);
1326 if (!g_file_set_contents (filename, content, -1, error))
1335 run_update_command ("update-desktop-database", "applications");
1337 return update_default_list (info->desktop_id, content_type, FALSE, error);
1341 * g_app_info_create_from_commandline:
1342 * @commandline: the commandline to use
1343 * @application_name: the application name, or %NULL to use @commandline
1344 * @flags: flags that can specify details of the created #GAppInfo
1345 * @error: a #GError location to store the error occuring, %NULL to ignore.
1347 * Creates a new #GAppInfo from the given information.
1349 * Returns: new #GAppInfo for given command.
1352 g_app_info_create_from_commandline (const char *commandline,
1353 const char *application_name,
1354 GAppInfoCreateFlags flags,
1360 char *basename, *exec, *filename, *comment;
1361 char *data, *desktop_id;
1364 GDesktopAppInfo *info;
1367 dirname = ensure_dir (APP_DIR, error);
1371 key_file = g_key_file_new ();
1373 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1374 "Encoding", "UTF-8");
1375 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1376 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1377 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1378 G_KEY_FILE_DESKTOP_KEY_TYPE,
1379 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1380 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1381 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1382 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1384 exec = g_strconcat (commandline, " %f", NULL);
1385 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1386 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1389 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1390 split = g_strsplit (commandline, " ", 2);
1391 basename = g_path_get_basename (split[0]);
1393 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1394 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1396 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1397 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1398 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1401 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1402 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1404 data = g_key_file_to_data (key_file, &data_size, NULL);
1405 g_key_file_free (key_file);
1407 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1409 filename = g_build_filename (dirname, desktop_id, NULL);
1410 g_free (desktop_id);
1413 fd = g_mkstemp (filename);
1418 display_name = g_filename_display_name (filename);
1419 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1420 _("Can't create user desktop file %s"), display_name);
1421 g_free (display_name);
1427 desktop_id = g_path_get_basename (filename);
1431 res = g_file_set_contents (filename, data, data_size, error);
1434 g_free (desktop_id);
1439 run_update_command ("update-desktop-database", "applications");
1441 info = _g_desktop_app_info_new_from_filename (filename);
1444 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1445 _("Can't load just created desktop file"));
1447 info->desktop_id = g_strdup (desktop_id);
1449 g_free (desktop_id);
1451 return G_APP_INFO (info);
1456 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1458 iface->dup = g_desktop_app_info_dup;
1459 iface->equal = g_desktop_app_info_equal;
1460 iface->get_id = g_desktop_app_info_get_id;
1461 iface->get_name = g_desktop_app_info_get_name;
1462 iface->get_description = g_desktop_app_info_get_description;
1463 iface->get_executable = g_desktop_app_info_get_executable;
1464 iface->get_icon = g_desktop_app_info_get_icon;
1465 iface->launch = g_desktop_app_info_launch;
1466 iface->supports_uris = g_desktop_app_info_supports_uris;
1467 iface->supports_xdg_startup_notify = g_desktop_app_info_supports_xdg_startup_notify;
1468 iface->launch_uris = g_desktop_app_info_launch_uris;
1469 iface->should_show = g_desktop_app_info_should_show;
1470 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1471 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1472 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1473 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1474 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1478 app_info_in_list (GAppInfo *info, GList *l)
1482 if (g_app_info_equal (info, l->data))
1491 * g_app_info_get_all_for_type:
1492 * @content_type: the content type to find a #GAppInfo for
1494 * Gets a list of all #GAppInfo s for a given content type.
1496 * Returns: #GList of #GAppInfo s for given @content_type.
1499 g_app_info_get_all_for_type (const char *content_type)
1501 GList *desktop_entries, *l;
1503 GDesktopAppInfo *info;
1505 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1508 for (l = desktop_entries; l != NULL; l = l->next)
1510 char *desktop_entry = l->data;
1512 info = _g_desktop_app_info_new (desktop_entry);
1515 if (app_info_in_list (G_APP_INFO (info), infos))
1516 g_object_unref (info);
1518 infos = g_list_prepend (infos, info);
1520 g_free (desktop_entry);
1523 g_list_free (desktop_entries);
1525 return g_list_reverse (infos);
1530 * g_app_info_get_default_for_type:
1531 * @content_type: the content type to find a #GAppInfo for
1532 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1535 * Gets the #GAppInfo that correspond to a given content type.
1537 * Returns: #GAppInfo for given @content_type.
1540 g_app_info_get_default_for_type (const char *content_type,
1541 gboolean must_support_uris)
1543 GList *desktop_entries, *l;
1546 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1549 for (l = desktop_entries; l != NULL; l = l->next)
1551 char *desktop_entry = l->data;
1553 info = (GAppInfo *)_g_desktop_app_info_new (desktop_entry);
1556 if (must_support_uris && !g_app_info_supports_uris (info))
1558 g_object_unref (info);
1566 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1567 g_list_free (desktop_entries);
1574 * g_app_info_get_default_for_uri_scheme:
1577 * Returns: #GAppInfo
1580 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1582 /* TODO: Implement this using giomodules, reading the gconf settings
1583 * in /desktop/gnome/url-handlers
1590 get_apps_from_dir (GHashTable *apps, const char *dirname, const char *prefix)
1593 const char *basename;
1594 char *filename, *subprefix, *desktop_id;
1596 GDesktopAppInfo *appinfo;
1598 dir = g_dir_open (dirname, 0, NULL);
1601 while ((basename = g_dir_read_name (dir)) != NULL)
1603 filename = g_build_filename (dirname, basename, NULL);
1604 if (g_str_has_suffix (basename, ".desktop"))
1606 desktop_id = g_strconcat (prefix, basename, NULL);
1608 /* Use _extended so we catch NULLs too (hidden) */
1609 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1611 appinfo = _g_desktop_app_info_new_from_filename (filename);
1613 /* Don't return apps that don't take arguments */
1615 _g_desktop_app_info_get_is_hidden (appinfo) &&
1616 strstr (appinfo->exec,"%U") == NULL &&
1617 strstr (appinfo->exec,"%u") == NULL &&
1618 strstr (appinfo->exec,"%f") == NULL &&
1619 strstr (appinfo->exec,"%F") == NULL)
1621 g_object_unref (appinfo);
1626 if (appinfo != NULL || hidden)
1628 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1632 /* Reuse instead of strdup here */
1633 appinfo->desktop_id = desktop_id;
1638 g_free (desktop_id);
1642 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1644 subprefix = g_strconcat (prefix, basename, "-", NULL);
1645 get_apps_from_dir (apps, filename, subprefix);
1656 collect_apps (gpointer key,
1660 GList **infos = user_data;
1663 *infos = g_list_prepend (*infos, value);
1668 * g_app_info_get_all:
1670 * Returns: a newly allocated #GList of references to #GAppInfo s.
1673 g_app_info_get_all (void)
1675 const char * const *dirs;
1680 dirs = get_applications_search_path ();
1682 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1686 for (i = 0; dirs[i] != NULL; i++)
1687 get_apps_from_dir (apps, dirs[i], "");
1691 g_hash_table_foreach (apps,
1695 g_hash_table_destroy (apps);
1697 return g_list_reverse (infos);
1700 /* Cacheing of mimeinfo.cache and defaults.list files */
1704 GHashTable *mime_info_cache_map;
1705 GHashTable *defaults_list_map;
1706 time_t mime_info_cache_timestamp;
1707 time_t defaults_list_timestamp;
1711 GList *dirs; /* mimeinfo.cache and defaults.list */
1712 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1713 time_t last_stat_time;
1714 guint should_ping_mime_monitor : 1;
1717 static MimeInfoCache *mime_info_cache = NULL;
1718 G_LOCK_DEFINE_STATIC (mime_info_cache);
1720 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1721 const char *mime_type,
1722 char **new_desktop_file_ids);
1724 static MimeInfoCache * mime_info_cache_new (void);
1727 destroy_info_cache_value (gpointer key, GList *value, gpointer data)
1729 g_list_foreach (value, (GFunc)g_free, NULL);
1730 g_list_free (value);
1734 destroy_info_cache_map (GHashTable *info_cache_map)
1736 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1737 g_hash_table_destroy (info_cache_map);
1741 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1742 const char *cache_file,
1748 filename = g_build_filename (dir->path, cache_file, NULL);
1750 if (g_stat (filename, &buf) < 0)
1757 if (buf.st_mtime != *timestamp)
1763 /* Call with lock held */
1765 remove_all (gpointer key,
1774 mime_info_cache_blow_global_cache (void)
1776 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1781 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1785 gchar *filename, **mime_types;
1792 if (dir->mime_info_cache_map != NULL &&
1793 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1794 &dir->mime_info_cache_timestamp))
1797 if (dir->mime_info_cache_map != NULL)
1798 destroy_info_cache_map (dir->mime_info_cache_map);
1800 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1801 (GDestroyNotify) g_free,
1804 key_file = g_key_file_new ();
1806 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1808 if (g_stat (filename, &buf) < 0)
1811 if (dir->mime_info_cache_timestamp > 0)
1812 mime_info_cache->should_ping_mime_monitor = TRUE;
1814 dir->mime_info_cache_timestamp = buf.st_mtime;
1816 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1821 if (load_error != NULL)
1824 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1827 if (load_error != NULL)
1830 for (i = 0; mime_types[i] != NULL; i++)
1832 gchar **desktop_file_ids;
1833 char *unaliased_type;
1834 desktop_file_ids = g_key_file_get_string_list (key_file,
1840 if (desktop_file_ids == NULL)
1843 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1844 mime_info_cache_dir_add_desktop_entries (dir,
1847 g_free (unaliased_type);
1849 g_strfreev (desktop_file_ids);
1852 g_strfreev (mime_types);
1853 g_key_file_free (key_file);
1858 g_key_file_free (key_file);
1860 if (mime_types != NULL)
1861 g_strfreev (mime_types);
1864 g_error_free (load_error);
1868 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1872 gchar *filename, **mime_types;
1873 char *unaliased_type;
1874 char **desktop_file_ids;
1881 if (dir->defaults_list_map != NULL &&
1882 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1883 &dir->defaults_list_timestamp))
1886 if (dir->defaults_list_map != NULL)
1887 g_hash_table_destroy (dir->defaults_list_map);
1889 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1890 g_free, (GDestroyNotify)g_strfreev);
1892 key_file = g_key_file_new ();
1894 filename = g_build_filename (dir->path, "defaults.list", NULL);
1895 if (g_stat (filename, &buf) < 0)
1898 if (dir->defaults_list_timestamp > 0)
1899 mime_info_cache->should_ping_mime_monitor = TRUE;
1901 dir->defaults_list_timestamp = buf.st_mtime;
1903 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1907 if (load_error != NULL)
1910 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1913 if (load_error != NULL)
1916 for (i = 0; mime_types[i] != NULL; i++)
1918 desktop_file_ids = g_key_file_get_string_list (key_file,
1919 DEFAULT_APPLICATIONS_GROUP,
1923 if (desktop_file_ids == NULL)
1926 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1927 g_hash_table_replace (dir->defaults_list_map,
1932 g_strfreev (mime_types);
1933 g_key_file_free (key_file);
1938 g_key_file_free (key_file);
1940 if (mime_types != NULL)
1941 g_strfreev (mime_types);
1944 g_error_free (load_error);
1947 static MimeInfoCacheDir *
1948 mime_info_cache_dir_new (const char *path)
1950 MimeInfoCacheDir *dir;
1952 dir = g_new0 (MimeInfoCacheDir, 1);
1953 dir->path = g_strdup (path);
1959 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1964 if (dir->mime_info_cache_map != NULL)
1966 destroy_info_cache_map (dir->mime_info_cache_map);
1967 dir->mime_info_cache_map = NULL;
1971 if (dir->defaults_list_map != NULL)
1973 g_hash_table_destroy (dir->defaults_list_map);
1974 dir->defaults_list_map = NULL;
1981 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1982 const char *mime_type,
1983 char **new_desktop_file_ids)
1985 GList *desktop_file_ids;
1988 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1991 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
1993 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
1994 desktop_file_ids = g_list_append (desktop_file_ids,
1995 g_strdup (new_desktop_file_ids[i]));
1998 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2002 mime_info_cache_init_dir_lists (void)
2004 const char * const *dirs;
2007 mime_info_cache = mime_info_cache_new ();
2009 dirs = get_applications_search_path ();
2011 for (i = 0; dirs[i] != NULL; i++)
2013 MimeInfoCacheDir *dir;
2015 dir = mime_info_cache_dir_new (dirs[i]);
2019 mime_info_cache_dir_init (dir);
2020 mime_info_cache_dir_init_defaults_list (dir);
2022 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2028 mime_info_cache_update_dir_lists (void)
2032 tmp = mime_info_cache->dirs;
2036 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2038 /* No need to do this if we had file monitors... */
2039 mime_info_cache_blow_global_cache ();
2040 mime_info_cache_dir_init (dir);
2041 mime_info_cache_dir_init_defaults_list (dir);
2048 mime_info_cache_init (void)
2050 G_LOCK (mime_info_cache);
2051 if (mime_info_cache == NULL)
2052 mime_info_cache_init_dir_lists ();
2058 if (now >= mime_info_cache->last_stat_time + 10)
2060 mime_info_cache_update_dir_lists ();
2061 mime_info_cache->last_stat_time = now;
2065 if (mime_info_cache->should_ping_mime_monitor)
2067 /* g_idle_add (emit_mime_changed, NULL); */
2068 mime_info_cache->should_ping_mime_monitor = FALSE;
2071 G_UNLOCK (mime_info_cache);
2074 static MimeInfoCache *
2075 mime_info_cache_new (void)
2077 MimeInfoCache *cache;
2079 cache = g_new0 (MimeInfoCache, 1);
2081 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2082 (GDestroyNotify) g_free,
2083 (GDestroyNotify) g_free);
2088 mime_info_cache_free (MimeInfoCache *cache)
2093 g_list_foreach (cache->dirs,
2094 (GFunc) mime_info_cache_dir_free,
2096 g_list_free (cache->dirs);
2097 g_hash_table_destroy (cache->global_defaults_cache);
2102 * mime_info_cache_reload:
2103 * @dir: directory path which needs reloading.
2105 * Reload the mime information for the @dir.
2108 mime_info_cache_reload (const char *dir)
2110 /* FIXME: just reload the dir that needs reloading,
2111 * don't blow the whole cache
2113 if (mime_info_cache != NULL)
2115 G_LOCK (mime_info_cache);
2116 mime_info_cache_free (mime_info_cache);
2117 mime_info_cache = NULL;
2118 G_UNLOCK (mime_info_cache);
2123 append_desktop_entry (GList *list, const char *desktop_entry)
2125 /* Add if not already in list, and valid */
2126 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2127 list = g_list_prepend (list, g_strdup (desktop_entry));
2133 * get_all_desktop_entries_for_mime_type:
2134 * @mime_type: a mime type.
2136 * Returns all the desktop filenames for @mime_type. The desktop files
2137 * are listed in an order so that default applications are listed before
2138 * non-default ones, and handlers for inherited mimetypes are listed
2139 * after the base ones.
2141 * Return value: a #GList containing the desktop filenames containing the
2145 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2147 GList *desktop_entries, *list, *dir_list, *tmp;
2148 MimeInfoCacheDir *dir;
2151 char **default_entries;
2154 mime_info_cache_init ();
2156 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2157 G_LOCK (mime_info_cache);
2159 desktop_entries = NULL;
2160 for (i = 0; mime_types[i] != NULL; i++)
2162 mime_type = mime_types[i];
2164 /* Go through all apps listed as defaults */
2165 for (dir_list = mime_info_cache->dirs;
2167 dir_list = dir_list->next)
2169 dir = dir_list->data;
2170 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2171 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2172 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2175 /* Go through all entries that support the mimetype */
2176 for (dir_list = mime_info_cache->dirs;
2178 dir_list = dir_list->next) {
2179 dir = dir_list->data;
2181 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2182 for (tmp = list; tmp != NULL; tmp = tmp->next) {
2183 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2188 G_UNLOCK (mime_info_cache);
2190 g_strfreev (mime_types);
2192 desktop_entries = g_list_reverse (desktop_entries);
2194 return desktop_entries;
2197 #define __G_DESKTOP_APP_INFO_C__
2198 #include "gioaliasdef.c"