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>
42 * SECTION:gdesktopappinfo
43 * @short_description: Application information from desktop files
44 * @include: gio/gdesktopappinfo.h
46 * #GDesktopAppInfo is an implementation of #GAppInfo based on
51 #define DEFAULT_APPLICATIONS_GROUP "Default Applications"
52 #define MIME_CACHE_GROUP "MIME Cache"
54 static void g_desktop_app_info_iface_init (GAppInfoIface *iface);
56 static GList *get_all_desktop_entries_for_mime_type (const char *base_mime_type);
57 static void mime_info_cache_reload (const char *dir);
59 struct _GDesktopAppInfo
61 GObject parent_instance;
67 /* FIXME: what about GenericName ? */
81 guint startup_notify : 1;
82 /* FIXME: what about StartupWMClass ? */
85 G_DEFINE_TYPE_WITH_CODE (GDesktopAppInfo, g_desktop_app_info, G_TYPE_OBJECT,
86 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
87 g_desktop_app_info_iface_init))
90 search_path_init (gpointer data)
93 const char * const *data_dirs;
94 const char *user_data_dir;
97 data_dirs = g_get_system_data_dirs ();
98 length = g_strv_length ((char **) data_dirs);
100 args = g_new (char *, length + 2);
103 user_data_dir = g_get_user_data_dir ();
104 args[j++] = g_build_filename (user_data_dir, "applications", NULL);
105 for (i = 0; i < length; i++)
106 args[j++] = g_build_filename (data_dirs[i],
107 "applications", NULL);
113 static const char * const *
114 get_applications_search_path (void)
116 static GOnce once_init = G_ONCE_INIT;
117 return g_once (&once_init, search_path_init, NULL);
121 g_desktop_app_info_finalize (GObject *object)
123 GDesktopAppInfo *info;
125 info = G_DESKTOP_APP_INFO (object);
127 g_free (info->desktop_id);
128 g_free (info->filename);
130 g_free (info->comment);
131 g_free (info->icon_name);
133 g_object_unref (info->icon);
134 g_strfreev (info->only_show_in);
135 g_strfreev (info->not_show_in);
136 g_free (info->try_exec);
138 g_free (info->binary);
141 G_OBJECT_CLASS (g_desktop_app_info_parent_class)->finalize (object);
145 g_desktop_app_info_class_init (GDesktopAppInfoClass *klass)
147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149 gobject_class->finalize = g_desktop_app_info_finalize;
153 g_desktop_app_info_init (GDesktopAppInfo *local)
158 * g_desktop_app_info_new_from_filename:
159 * @filename: a string containing a file name.
161 * Creates a new #GDesktopAppInfo.
163 * Returns: a new #GDesktopAppInfo or %NULL on error.
166 g_desktop_app_info_new_from_filename (const char *filename)
168 GDesktopAppInfo *info;
174 key_file = g_key_file_new ();
176 if (!g_key_file_load_from_file (key_file,
182 start_group = g_key_file_get_start_group (key_file);
183 if (start_group == NULL || strcmp (start_group, G_KEY_FILE_DESKTOP_GROUP) != 0)
185 g_free (start_group);
188 g_free (start_group);
190 type = g_key_file_get_string (key_file,
191 G_KEY_FILE_DESKTOP_GROUP,
192 G_KEY_FILE_DESKTOP_KEY_TYPE,
194 if (type == NULL || strcmp (type, G_KEY_FILE_DESKTOP_TYPE_APPLICATION) != 0)
201 try_exec = g_key_file_get_string (key_file,
202 G_KEY_FILE_DESKTOP_GROUP,
203 G_KEY_FILE_DESKTOP_KEY_TRY_EXEC,
208 t = g_find_program_in_path (try_exec);
217 info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
218 info->filename = g_strdup (filename);
220 info->name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
221 info->comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_COMMENT, NULL, NULL);
222 info->nodisplay = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, NULL) != FALSE;
223 info->icon_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL, NULL);
224 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);
225 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);
226 info->try_exec = try_exec;
227 info->exec = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, NULL);
228 info->path = g_key_file_get_string (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_PATH, NULL);
229 info->terminal = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TERMINAL, NULL) != FALSE;
230 info->startup_notify = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY, NULL) != FALSE;
231 info->hidden = g_key_file_get_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, NULL) != FALSE;
236 if (g_path_is_absolute (info->icon_name))
240 file = g_file_new_for_path (info->icon_name);
241 info->icon = g_file_icon_new (file);
242 g_object_unref (file);
245 info->icon = g_themed_icon_new (info->icon_name);
256 while (*p != ' ' && *p != 0)
259 info->binary = g_strndup (start, p - start);
266 * g_desktop_app_info_new:
267 * @desktop_id: the desktop file id
269 * Creates a new #GDesktopAppInfo.
271 * Returns: a new #GDesktopAppInfo, or %NULL if no desktop file with that id
274 g_desktop_app_info_new (const char *desktop_id)
276 GDesktopAppInfo *appinfo;
277 const char * const *dirs;
280 dirs = get_applications_search_path ();
282 for (i = 0; dirs[i] != NULL; i++)
288 filename = g_build_filename (dirs[i], desktop_id, NULL);
289 appinfo = g_desktop_app_info_new_from_filename (filename);
296 basename = g_strdup (desktop_id);
298 while ((p = strchr (p, '-')) != NULL)
302 filename = g_build_filename (dirs[i], basename, NULL);
303 appinfo = g_desktop_app_info_new_from_filename (filename);
318 appinfo->desktop_id = g_strdup (desktop_id);
320 if (g_desktop_app_info_get_is_hidden (appinfo))
322 g_object_unref (appinfo);
330 g_desktop_app_info_dup (GAppInfo *appinfo)
332 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
333 GDesktopAppInfo *new_info;
335 new_info = g_object_new (G_TYPE_DESKTOP_APP_INFO, NULL);
337 new_info->filename = g_strdup (info->filename);
338 new_info->desktop_id = g_strdup (info->desktop_id);
340 new_info->name = g_strdup (info->name);
341 new_info->comment = g_strdup (info->comment);
342 new_info->nodisplay = info->nodisplay;
343 new_info->icon_name = g_strdup (info->icon_name);
344 new_info->icon = g_object_ref (info->icon);
345 new_info->only_show_in = g_strdupv (info->only_show_in);
346 new_info->not_show_in = g_strdupv (info->not_show_in);
347 new_info->try_exec = g_strdup (info->try_exec);
348 new_info->exec = g_strdup (info->exec);
349 new_info->binary = g_strdup (info->binary);
350 new_info->path = g_strdup (info->path);
351 new_info->hidden = info->hidden;
352 new_info->terminal = info->terminal;
353 new_info->startup_notify = info->startup_notify;
355 return G_APP_INFO (new_info);
359 g_desktop_app_info_equal (GAppInfo *appinfo1,
362 GDesktopAppInfo *info1 = G_DESKTOP_APP_INFO (appinfo1);
363 GDesktopAppInfo *info2 = G_DESKTOP_APP_INFO (appinfo2);
365 if (info1->desktop_id == NULL ||
366 info2->desktop_id == NULL)
369 return strcmp (info1->desktop_id, info2->desktop_id) == 0;
373 g_desktop_app_info_get_id (GAppInfo *appinfo)
375 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
377 return info->desktop_id;
381 g_desktop_app_info_get_name (GAppInfo *appinfo)
383 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
385 if (info->name == NULL)
391 * g_desktop_app_info_get_is_hidden:
392 * @info: a #GDesktopAppInfo.
394 * A desktop file is hidden if the Hidden key in it is
397 * Returns: %TRUE if hidden, %FALSE otherwise.
400 g_desktop_app_info_get_is_hidden (GDesktopAppInfo *info)
406 g_desktop_app_info_get_description (GAppInfo *appinfo)
408 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
410 return info->comment;
414 g_desktop_app_info_get_executable (GAppInfo *appinfo)
416 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
422 g_desktop_app_info_get_icon (GAppInfo *appinfo)
424 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
430 expand_macro_single (char macro, GFile *file)
435 path = g_file_get_path (file);
436 uri = g_file_get_uri (file);
442 result = g_shell_quote (uri);
447 result = g_shell_quote (path);
452 result = g_shell_quote (g_path_get_dirname (path));
457 result = g_shell_quote (g_path_get_basename (path));
468 expand_macro (char macro,
470 GDesktopAppInfo *info,
473 GList *files = *file_list;
476 g_return_if_fail (exec != NULL);
486 expanded = expand_macro_single (macro, files->data);
489 g_string_append (exec, expanded);
503 expanded = expand_macro_single (macro, files->data);
506 g_string_append (exec, expanded);
512 if (files != NULL && expanded)
513 g_string_append_c (exec, ' ');
521 g_string_append (exec, "--icon ");
522 g_string_append (exec, info->icon_name);
528 g_string_append (exec, info->name);
533 g_string_append (exec, info->filename);
536 case 'm': /* deprecated */
540 g_string_append_c (exec, '%');
548 expand_application_parameters (GDesktopAppInfo *info,
554 GList *file_list = *files;
555 const char *p = info->exec;
556 GString *expanded_exec = g_string_new (NULL);
559 if (info->exec == NULL)
561 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
562 _("Desktop file didn't specify Exec field"));
568 if (p[0] == '%' && p[1] != '\0')
570 expand_macro (p[1], expanded_exec, info, files);
574 g_string_append_c (expanded_exec, *p);
579 /* No file substitutions */
580 if (file_list == *files && file_list != NULL)
582 /* If there is no macro default to %f. This is also what KDE does */
583 g_string_append_c (expanded_exec, ' ');
584 expand_macro ('f', expanded_exec, info, files);
587 res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
588 g_string_free (expanded_exec, TRUE);
593 prepend_terminal_to_vector (int *argc,
600 char **term_argv = NULL;
605 g_return_val_if_fail (argc != NULL, FALSE);
606 g_return_val_if_fail (argv != NULL, FALSE);
614 /* compute size if not given */
617 for (i = 0; the_argv[i] != NULL; i++)
623 term_argv = g_new0 (char *, 3);
625 check = g_find_program_in_path ("gnome-terminal");
628 term_argv[0] = check;
629 /* Note that gnome-terminal takes -x and
630 * as -e in gnome-terminal is broken we use that. */
631 term_argv[1] = g_strdup ("-x");
636 check = g_find_program_in_path ("nxterm");
638 check = g_find_program_in_path ("color-xterm");
640 check = g_find_program_in_path ("rxvt");
642 check = g_find_program_in_path ("xterm");
644 check = g_find_program_in_path ("dtterm");
647 check = g_strdup ("xterm");
648 g_warning ("couldn't find a terminal, falling back to xterm");
650 term_argv[0] = check;
651 term_argv[1] = g_strdup ("-e");
654 real_argc = term_argc + *argc;
655 real_argv = g_new (char *, real_argc + 1);
657 for (i = 0; i < term_argc; i++)
658 real_argv[i] = term_argv[i];
660 for (j = 0; j < *argc; j++, i++)
661 real_argv[i] = (char *)the_argv[j];
669 /* we use g_free here as we sucked all the inner strings
670 * out from it into real_argv */
675 #endif /* G_OS_WIN32 */
678 /* '=' is the new '\0'.
679 * DO NOT CALL unless at least one string ends with '='
682 is_env (const char *a,
687 if (*a == 0 || *b == 0)
700 /* free with g_strfreev */
702 replace_env_var (char **old_environ,
704 const char *new_value)
706 int length, new_length;
707 int index, new_index;
711 /* do two things at once:
712 * - discover the length of the environment ('length')
713 * - find the location (if any) of the env var ('index')
716 for (length = 0; old_environ[length]; length++)
718 /* if we already have it in our environment, replace */
719 if (is_env (old_environ[length], env_var))
724 /* no current env var, no desired env value.
727 if (new_value == NULL && index == -1)
730 /* in all cases now, we will be using a modified environment.
731 * determine its length and allocated it.
734 * new_index = location to insert, if any
735 * new_length = length of the new array
736 * new_environ = the pointer array for the new environment
739 if (new_value == NULL && index >= 0)
741 /* in this case, we will be removing an entry */
742 new_length = length - 1;
745 else if (new_value != NULL && index < 0)
747 /* in this case, we will be adding an entry to the end */
748 new_length = length + 1;
752 /* in this case, we will be replacing the existing entry */
758 new_environ = g_malloc (sizeof (char *) * (new_length + 1));
759 new_environ[new_length] = NULL;
761 /* now we do the copying.
762 * for each entry in the new environment, we decide what to do
766 for (new_i = 0; new_i < new_length; new_i++)
768 if (new_i == new_index)
770 /* insert our new item */
771 new_environ[new_i] = g_strconcat (env_var,
776 /* if we had an old entry, skip it now */
782 /* if this is the old DESKTOP_STARTUP_ID, skip it */
786 /* copy an old item */
787 new_environ[new_i] = g_strdup (old_environ[i]);
792 g_strfreev (old_environ);
798 dup_list_segment (GList *start,
804 while (start != NULL && start != end)
806 res = g_list_prepend (res, start->data);
810 return g_list_reverse (res);
814 g_desktop_app_info_launch (GAppInfo *appinfo,
816 GAppLaunchContext *launch_context,
819 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
820 gboolean completed = FALSE;
822 GList *launched_files;
829 g_return_val_if_fail (appinfo != NULL, FALSE);
837 if (!expand_application_parameters (info, &files,
838 &argc, &argv, error))
841 if (info->terminal && !prepend_terminal_to_vector (&argc, &argv))
843 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
844 _("Unable to find terminal required for application"));
851 launched_files = dup_list_segment (old_files, files);
853 display = g_app_launch_context_get_display (launch_context,
858 if (info->startup_notify)
859 sn_id = g_app_launch_context_get_startup_notify_id (launch_context,
863 if (display || sn_id)
868 envp = replace_env_var (envp,
873 envp = replace_env_var (envp,
874 "DESKTOP_STARTUP_ID",
880 g_list_free (launched_files);
883 if (!g_spawn_async (info->path, /* working directory */
886 G_SPAWN_SEARCH_PATH /* flags */,
887 NULL /* child_setup */,
889 NULL /* child_pid */,
894 g_app_launch_context_launch_failed (launch_context, sn_id);
908 while (files != NULL);
920 g_desktop_app_info_supports_uris (GAppInfo *appinfo)
922 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
925 (strstr (info->exec, "%u") != NULL) ||
926 (strstr (info->exec, "%U") != NULL);
930 g_desktop_app_info_launch_uris (GAppInfo *appinfo,
932 GAppLaunchContext *launch_context,
942 file = g_file_new_for_uri (uris->data);
944 g_warning ("Invalid uri passed to g_desktop_app_info_launch_uris");
947 files = g_list_prepend (files, file);
950 files = g_list_reverse (files);
952 res = g_desktop_app_info_launch (appinfo, files, launch_context, error);
954 g_list_foreach (files, (GFunc)g_object_unref, NULL);
961 g_desktop_app_info_should_show (GAppInfo *appinfo,
962 const char *desktop_env)
964 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
971 if (info->only_show_in)
973 if (desktop_env == NULL)
977 for (i = 0; info->only_show_in[i] != NULL; i++)
979 if (strcmp (info->only_show_in[i], desktop_env) == 0)
989 if (info->not_show_in && desktop_env)
991 for (i = 0; info->not_show_in[i] != NULL; i++)
993 if (strcmp (info->not_show_in[i], desktop_env) == 0)
1007 ensure_dir (DirType type,
1010 char *path, *display_name;
1013 if (type == APP_DIR)
1014 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1016 path = g_build_filename (g_get_user_data_dir (), "mime", "packages", NULL);
1019 if (g_mkdir_with_parents (path, 0700) == 0)
1023 display_name = g_filename_display_name (path);
1024 if (type == APP_DIR)
1025 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1026 _("Can't create user application configuration folder %s: %s"),
1027 display_name, g_strerror (err));
1029 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (err),
1030 _("Can't create user MIME configuration folder %s: %s"),
1031 display_name, g_strerror (err));
1033 g_free (display_name);
1040 update_default_list (const char *desktop_id,
1041 const char *content_type,
1045 char *dirname, *filename;
1047 gboolean load_succeeded, res;
1050 gsize length, data_size;
1054 dirname = ensure_dir (APP_DIR, error);
1058 filename = g_build_filename (dirname, "defaults.list", NULL);
1061 key_file = g_key_file_new ();
1062 load_succeeded = g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
1063 if (!load_succeeded || !g_key_file_has_group (key_file, DEFAULT_APPLICATIONS_GROUP))
1065 g_key_file_free (key_file);
1066 key_file = g_key_file_new ();
1070 old_list = g_key_file_get_string_list (key_file, DEFAULT_APPLICATIONS_GROUP,
1071 content_type, &length, NULL);
1073 list = g_new (char *, 1 + length + 1);
1077 list[i++] = g_strdup (desktop_id);
1080 for (j = 0; old_list[j] != NULL; j++)
1082 if (strcmp (old_list[j], desktop_id) != 0)
1083 list[i++] = g_strdup (old_list[j]);
1088 g_strfreev (old_list);
1090 g_key_file_set_string_list (key_file,
1091 DEFAULT_APPLICATIONS_GROUP,
1093 (const char * const *)list, i);
1097 data = g_key_file_to_data (key_file, &data_size, error);
1098 g_key_file_free (key_file);
1100 res = g_file_set_contents (filename, data, data_size, error);
1102 mime_info_cache_reload (NULL);
1111 g_desktop_app_info_set_as_default_for_type (GAppInfo *appinfo,
1112 const char *content_type,
1115 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1117 if (!g_app_info_add_supports_type (appinfo, content_type, error))
1120 return update_default_list (info->desktop_id, content_type, TRUE, error);
1124 update_program_done (GPid pid,
1128 /* Did the application exit correctly */
1129 if (WIFEXITED (status) &&
1130 WEXITSTATUS (status) == 0)
1132 /* Here we could clean out any caches in use */
1137 run_update_command (char *command,
1146 GError *error = NULL;
1149 argv[1] = g_build_filename (g_get_user_data_dir (), subdir, NULL);
1151 if (g_spawn_async ("/", argv,
1153 G_SPAWN_SEARCH_PATH |
1154 G_SPAWN_STDOUT_TO_DEV_NULL |
1155 G_SPAWN_STDERR_TO_DEV_NULL |
1156 G_SPAWN_DO_NOT_REAP_CHILD,
1157 NULL, NULL, /* No setup function */
1160 g_child_watch_add (pid, update_program_done, NULL);
1163 /* If we get an error at this point, it's quite likely the user doesn't
1164 * have an installed copy of either 'update-mime-database' or
1165 * 'update-desktop-database'. I don't think we want to popup an error
1166 * dialog at this point, so we just do a g_warning to give the user a
1167 * chance of debugging it.
1169 g_warning ("%s", error->message);
1176 g_desktop_app_info_set_as_default_for_extension (GAppInfo *appinfo,
1177 const char *extension,
1180 char *filename, *basename, *mimetype;
1184 dirname = ensure_dir (MIMETYPE_DIR, error);
1188 basename = g_strdup_printf ("user-extension-%s.xml", extension);
1189 filename = g_build_filename (dirname, basename, NULL);
1193 mimetype = g_strdup_printf ("application/x-extension-%s", extension);
1195 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
1200 g_strdup_printf ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1201 "<mime-info xmlns=\"http://www.freedesktop.org/standards/shared-mime-info\">\n"
1202 " <mime-type type=\"%s\">\n"
1203 " <comment>%s document</comment>\n"
1204 " <glob pattern=\"*.%s\"/>\n"
1206 "</mime-info>\n", mimetype, extension, extension);
1208 g_file_set_contents (filename, contents, -1, NULL);
1211 run_update_command ("update-mime-database", "mime");
1215 res = g_desktop_app_info_set_as_default_for_type (appinfo,
1225 g_desktop_app_info_add_supports_type (GAppInfo *appinfo,
1226 const char *content_type,
1229 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1231 char *new_mimetypes, *old_mimetypes, *content;
1235 keyfile = g_key_file_new ();
1236 if (!g_key_file_load_from_file (keyfile, info->filename,
1237 G_KEY_FILE_KEEP_COMMENTS |
1238 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1240 g_key_file_free (keyfile);
1244 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1245 new_mimetypes = g_strconcat (content_type, ";", old_mimetypes, NULL);
1246 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1247 g_free (old_mimetypes);
1248 g_free (new_mimetypes);
1250 content = g_key_file_to_data (keyfile, NULL, NULL);
1251 g_key_file_free (keyfile);
1253 dirname = ensure_dir (APP_DIR, error);
1260 filename = g_build_filename (dirname, info->desktop_id, NULL);
1263 if (!g_file_set_contents (filename, content, -1, error))
1272 run_update_command ("update-desktop-database", "applications");
1277 g_desktop_app_info_can_remove_supports_type (GAppInfo *appinfo)
1279 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1282 user_dirname = g_build_filename (g_get_user_data_dir (), "applications", NULL);
1283 return g_str_has_prefix (info->filename, user_dirname);
1287 g_desktop_app_info_remove_supports_type (GAppInfo *appinfo,
1288 const char *content_type,
1291 GDesktopAppInfo *info = G_DESKTOP_APP_INFO (appinfo);
1293 char *new_mimetypes, *old_mimetypes, *content;
1298 keyfile = g_key_file_new ();
1299 if (!g_key_file_load_from_file (keyfile, info->filename,
1300 G_KEY_FILE_KEEP_COMMENTS |
1301 G_KEY_FILE_KEEP_TRANSLATIONS, error))
1303 g_key_file_free (keyfile);
1307 old_mimetypes = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, NULL);
1308 new_mimetypes = g_strdup (old_mimetypes);
1311 found = strstr (new_mimetypes, content_type);
1312 if (found && *(found + strlen (content_type)) == ';')
1314 char *rest = found + strlen (content_type) + 1;
1315 memmove (found, rest, strlen (rest) + 1);
1317 g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_MIME_TYPE, new_mimetypes);
1318 g_free (old_mimetypes);
1319 g_free (new_mimetypes);
1321 content = g_key_file_to_data (keyfile, NULL, NULL);
1322 g_key_file_free (keyfile);
1324 dirname = ensure_dir (APP_DIR, error);
1331 filename = g_build_filename (dirname, info->desktop_id, NULL);
1333 if (!g_file_set_contents (filename, content, -1, error))
1342 run_update_command ("update-desktop-database", "applications");
1344 return update_default_list (info->desktop_id, content_type, FALSE, error);
1348 * g_app_info_create_from_commandline:
1349 * @commandline: the commandline to use
1350 * @application_name: the application name, or %NULL to use @commandline
1351 * @flags: flags that can specify details of the created #GAppInfo
1352 * @error: a #GError location to store the error occuring, %NULL to ignore.
1354 * Creates a new #GAppInfo from the given information.
1356 * Returns: new #GAppInfo for given command.
1359 g_app_info_create_from_commandline (const char *commandline,
1360 const char *application_name,
1361 GAppInfoCreateFlags flags,
1367 char *basename, *exec, *filename, *comment;
1368 char *data, *desktop_id;
1371 GDesktopAppInfo *info;
1374 dirname = ensure_dir (APP_DIR, error);
1378 key_file = g_key_file_new ();
1380 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1381 "Encoding", "UTF-8");
1382 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1383 G_KEY_FILE_DESKTOP_KEY_VERSION, "1.0");
1384 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1385 G_KEY_FILE_DESKTOP_KEY_TYPE,
1386 G_KEY_FILE_DESKTOP_TYPE_APPLICATION);
1387 if (flags & G_APP_INFO_CREATE_NEEDS_TERMINAL)
1388 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1389 G_KEY_FILE_DESKTOP_KEY_TERMINAL, TRUE);
1391 exec = g_strconcat (commandline, " %f", NULL);
1392 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1393 G_KEY_FILE_DESKTOP_KEY_EXEC, exec);
1396 /* FIXME: this should be more robust. Maybe g_shell_parse_argv and use argv[0] */
1397 split = g_strsplit (commandline, " ", 2);
1398 basename = g_path_get_basename (split[0]);
1400 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1401 G_KEY_FILE_DESKTOP_KEY_NAME, application_name?application_name:basename);
1403 comment = g_strdup_printf (_("Custom definition for %s"), basename);
1404 g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
1405 G_KEY_FILE_DESKTOP_KEY_COMMENT, comment);
1408 g_key_file_set_boolean (key_file, G_KEY_FILE_DESKTOP_GROUP,
1409 G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY, TRUE);
1411 data = g_key_file_to_data (key_file, &data_size, NULL);
1412 g_key_file_free (key_file);
1414 desktop_id = g_strdup_printf ("userapp-%s-XXXXXX.desktop", basename);
1416 filename = g_build_filename (dirname, desktop_id, NULL);
1417 g_free (desktop_id);
1420 fd = g_mkstemp (filename);
1425 display_name = g_filename_display_name (filename);
1426 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1427 _("Can't create user desktop file %s"), display_name);
1428 g_free (display_name);
1434 desktop_id = g_path_get_basename (filename);
1438 res = g_file_set_contents (filename, data, data_size, error);
1441 g_free (desktop_id);
1446 run_update_command ("update-desktop-database", "applications");
1448 info = g_desktop_app_info_new_from_filename (filename);
1451 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1452 _("Can't load just created desktop file"));
1454 info->desktop_id = g_strdup (desktop_id);
1456 g_free (desktop_id);
1458 return G_APP_INFO (info);
1463 g_desktop_app_info_iface_init (GAppInfoIface *iface)
1465 iface->dup = g_desktop_app_info_dup;
1466 iface->equal = g_desktop_app_info_equal;
1467 iface->get_id = g_desktop_app_info_get_id;
1468 iface->get_name = g_desktop_app_info_get_name;
1469 iface->get_description = g_desktop_app_info_get_description;
1470 iface->get_executable = g_desktop_app_info_get_executable;
1471 iface->get_icon = g_desktop_app_info_get_icon;
1472 iface->launch = g_desktop_app_info_launch;
1473 iface->supports_uris = g_desktop_app_info_supports_uris;
1474 iface->launch_uris = g_desktop_app_info_launch_uris;
1475 iface->should_show = g_desktop_app_info_should_show;
1476 iface->set_as_default_for_type = g_desktop_app_info_set_as_default_for_type;
1477 iface->set_as_default_for_extension = g_desktop_app_info_set_as_default_for_extension;
1478 iface->add_supports_type = g_desktop_app_info_add_supports_type;
1479 iface->can_remove_supports_type = g_desktop_app_info_can_remove_supports_type;
1480 iface->remove_supports_type = g_desktop_app_info_remove_supports_type;
1484 app_info_in_list (GAppInfo *info,
1487 while (list != NULL)
1489 if (g_app_info_equal (info, list->data))
1498 * g_app_info_get_all_for_type:
1499 * @content_type: the content type to find a #GAppInfo for
1501 * Gets a list of all #GAppInfo s for a given content type.
1503 * Returns: #GList of #GAppInfo s for given @content_type.
1506 g_app_info_get_all_for_type (const char *content_type)
1508 GList *desktop_entries, *l;
1510 GDesktopAppInfo *info;
1512 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1515 for (l = desktop_entries; l != NULL; l = l->next)
1517 char *desktop_entry = l->data;
1519 info = g_desktop_app_info_new (desktop_entry);
1522 if (app_info_in_list (G_APP_INFO (info), infos))
1523 g_object_unref (info);
1525 infos = g_list_prepend (infos, info);
1527 g_free (desktop_entry);
1530 g_list_free (desktop_entries);
1532 return g_list_reverse (infos);
1537 * g_app_info_get_default_for_type:
1538 * @content_type: the content type to find a #GAppInfo for
1539 * @must_support_uris: if %TRUE, the #GAppInfo is expected to
1542 * Gets the #GAppInfo that correspond to a given content type.
1544 * Returns: #GAppInfo for given @content_type.
1547 g_app_info_get_default_for_type (const char *content_type,
1548 gboolean must_support_uris)
1550 GList *desktop_entries, *l;
1553 desktop_entries = get_all_desktop_entries_for_mime_type (content_type);
1556 for (l = desktop_entries; l != NULL; l = l->next)
1558 char *desktop_entry = l->data;
1560 info = (GAppInfo *)g_desktop_app_info_new (desktop_entry);
1563 if (must_support_uris && !g_app_info_supports_uris (info))
1565 g_object_unref (info);
1573 g_list_foreach (desktop_entries, (GFunc)g_free, NULL);
1574 g_list_free (desktop_entries);
1581 * g_app_info_get_default_for_uri_scheme:
1582 * @uri_scheme: a string containing a URI scheme.
1584 * Gets the default application for launching applications using this URI scheme.
1586 * TODO: This is currently unimplemented.
1591 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
1593 /* TODO: Implement this using giomodules, reading the gconf settings
1594 * in /desktop/gnome/url-handlers
1601 get_apps_from_dir (GHashTable *apps,
1602 const char *dirname,
1606 const char *basename;
1607 char *filename, *subprefix, *desktop_id;
1609 GDesktopAppInfo *appinfo;
1611 dir = g_dir_open (dirname, 0, NULL);
1614 while ((basename = g_dir_read_name (dir)) != NULL)
1616 filename = g_build_filename (dirname, basename, NULL);
1617 if (g_str_has_suffix (basename, ".desktop"))
1619 desktop_id = g_strconcat (prefix, basename, NULL);
1621 /* Use _extended so we catch NULLs too (hidden) */
1622 if (!g_hash_table_lookup_extended (apps, desktop_id, NULL, NULL))
1624 appinfo = g_desktop_app_info_new_from_filename (filename);
1626 /* Don't return apps that don't take arguments */
1628 g_desktop_app_info_get_is_hidden (appinfo) &&
1629 strstr (appinfo->exec,"%U") == NULL &&
1630 strstr (appinfo->exec,"%u") == NULL &&
1631 strstr (appinfo->exec,"%f") == NULL &&
1632 strstr (appinfo->exec,"%F") == NULL)
1634 g_object_unref (appinfo);
1639 if (appinfo != NULL || hidden)
1641 g_hash_table_insert (apps, g_strdup (desktop_id), appinfo);
1645 /* Reuse instead of strdup here */
1646 appinfo->desktop_id = desktop_id;
1651 g_free (desktop_id);
1655 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
1657 subprefix = g_strconcat (prefix, basename, "-", NULL);
1658 get_apps_from_dir (apps, filename, subprefix);
1670 * g_app_info_get_all:
1672 * Gets a list of all of the applications currently registered on this system.
1674 * Returns: a newly allocated #GList of references to #GAppInfo<!---->s.
1677 g_app_info_get_all (void)
1679 const char * const *dirs;
1681 GHashTableIter iter;
1686 dirs = get_applications_search_path ();
1688 apps = g_hash_table_new_full (g_str_hash, g_str_equal,
1692 for (i = 0; dirs[i] != NULL; i++)
1693 get_apps_from_dir (apps, dirs[i], "");
1697 g_hash_table_iter_init (&iter, apps);
1698 while (g_hash_table_iter_next (&iter, &key, NULL))
1699 infos = g_list_prepend (infos, key);
1701 g_hash_table_destroy (apps);
1703 return g_list_reverse (infos);
1706 /* Cacheing of mimeinfo.cache and defaults.list files */
1710 GHashTable *mime_info_cache_map;
1711 GHashTable *defaults_list_map;
1712 time_t mime_info_cache_timestamp;
1713 time_t defaults_list_timestamp;
1717 GList *dirs; /* mimeinfo.cache and defaults.list */
1718 GHashTable *global_defaults_cache; /* global results of defaults.list lookup and validation */
1719 time_t last_stat_time;
1720 guint should_ping_mime_monitor : 1;
1723 static MimeInfoCache *mime_info_cache = NULL;
1724 G_LOCK_DEFINE_STATIC (mime_info_cache);
1726 static void mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1727 const char *mime_type,
1728 char **new_desktop_file_ids);
1730 static MimeInfoCache * mime_info_cache_new (void);
1733 destroy_info_cache_value (gpointer key,
1737 g_list_foreach (value, (GFunc)g_free, NULL);
1738 g_list_free (value);
1742 destroy_info_cache_map (GHashTable *info_cache_map)
1744 g_hash_table_foreach (info_cache_map, (GHFunc)destroy_info_cache_value, NULL);
1745 g_hash_table_destroy (info_cache_map);
1749 mime_info_cache_dir_out_of_date (MimeInfoCacheDir *dir,
1750 const char *cache_file,
1756 filename = g_build_filename (dir->path, cache_file, NULL);
1758 if (g_stat (filename, &buf) < 0)
1765 if (buf.st_mtime != *timestamp)
1771 /* Call with lock held */
1773 remove_all (gpointer key,
1782 mime_info_cache_blow_global_cache (void)
1784 g_hash_table_foreach_remove (mime_info_cache->global_defaults_cache,
1789 mime_info_cache_dir_init (MimeInfoCacheDir *dir)
1793 gchar *filename, **mime_types;
1800 if (dir->mime_info_cache_map != NULL &&
1801 !mime_info_cache_dir_out_of_date (dir, "mimeinfo.cache",
1802 &dir->mime_info_cache_timestamp))
1805 if (dir->mime_info_cache_map != NULL)
1806 destroy_info_cache_map (dir->mime_info_cache_map);
1808 dir->mime_info_cache_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1809 (GDestroyNotify) g_free,
1812 key_file = g_key_file_new ();
1814 filename = g_build_filename (dir->path, "mimeinfo.cache", NULL);
1816 if (g_stat (filename, &buf) < 0)
1819 if (dir->mime_info_cache_timestamp > 0)
1820 mime_info_cache->should_ping_mime_monitor = TRUE;
1822 dir->mime_info_cache_timestamp = buf.st_mtime;
1824 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1829 if (load_error != NULL)
1832 mime_types = g_key_file_get_keys (key_file, MIME_CACHE_GROUP,
1835 if (load_error != NULL)
1838 for (i = 0; mime_types[i] != NULL; i++)
1840 gchar **desktop_file_ids;
1841 char *unaliased_type;
1842 desktop_file_ids = g_key_file_get_string_list (key_file,
1848 if (desktop_file_ids == NULL)
1851 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1852 mime_info_cache_dir_add_desktop_entries (dir,
1855 g_free (unaliased_type);
1857 g_strfreev (desktop_file_ids);
1860 g_strfreev (mime_types);
1861 g_key_file_free (key_file);
1866 g_key_file_free (key_file);
1868 if (mime_types != NULL)
1869 g_strfreev (mime_types);
1872 g_error_free (load_error);
1876 mime_info_cache_dir_init_defaults_list (MimeInfoCacheDir *dir)
1880 gchar *filename, **mime_types;
1881 char *unaliased_type;
1882 char **desktop_file_ids;
1889 if (dir->defaults_list_map != NULL &&
1890 !mime_info_cache_dir_out_of_date (dir, "defaults.list",
1891 &dir->defaults_list_timestamp))
1894 if (dir->defaults_list_map != NULL)
1895 g_hash_table_destroy (dir->defaults_list_map);
1897 dir->defaults_list_map = g_hash_table_new_full (g_str_hash, g_str_equal,
1898 g_free, (GDestroyNotify)g_strfreev);
1900 key_file = g_key_file_new ();
1902 filename = g_build_filename (dir->path, "defaults.list", NULL);
1903 if (g_stat (filename, &buf) < 0)
1906 if (dir->defaults_list_timestamp > 0)
1907 mime_info_cache->should_ping_mime_monitor = TRUE;
1909 dir->defaults_list_timestamp = buf.st_mtime;
1911 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &load_error);
1915 if (load_error != NULL)
1918 mime_types = g_key_file_get_keys (key_file, DEFAULT_APPLICATIONS_GROUP,
1921 if (load_error != NULL)
1924 for (i = 0; mime_types[i] != NULL; i++)
1926 desktop_file_ids = g_key_file_get_string_list (key_file,
1927 DEFAULT_APPLICATIONS_GROUP,
1931 if (desktop_file_ids == NULL)
1934 unaliased_type = _g_unix_content_type_unalias (mime_types[i]);
1935 g_hash_table_replace (dir->defaults_list_map,
1940 g_strfreev (mime_types);
1941 g_key_file_free (key_file);
1946 g_key_file_free (key_file);
1948 if (mime_types != NULL)
1949 g_strfreev (mime_types);
1952 g_error_free (load_error);
1955 static MimeInfoCacheDir *
1956 mime_info_cache_dir_new (const char *path)
1958 MimeInfoCacheDir *dir;
1960 dir = g_new0 (MimeInfoCacheDir, 1);
1961 dir->path = g_strdup (path);
1967 mime_info_cache_dir_free (MimeInfoCacheDir *dir)
1972 if (dir->mime_info_cache_map != NULL)
1974 destroy_info_cache_map (dir->mime_info_cache_map);
1975 dir->mime_info_cache_map = NULL;
1979 if (dir->defaults_list_map != NULL)
1981 g_hash_table_destroy (dir->defaults_list_map);
1982 dir->defaults_list_map = NULL;
1989 mime_info_cache_dir_add_desktop_entries (MimeInfoCacheDir *dir,
1990 const char *mime_type,
1991 char **new_desktop_file_ids)
1993 GList *desktop_file_ids;
1996 desktop_file_ids = g_hash_table_lookup (dir->mime_info_cache_map,
1999 for (i = 0; new_desktop_file_ids[i] != NULL; i++)
2001 if (!g_list_find (desktop_file_ids, new_desktop_file_ids[i]))
2002 desktop_file_ids = g_list_append (desktop_file_ids,
2003 g_strdup (new_desktop_file_ids[i]));
2006 g_hash_table_insert (dir->mime_info_cache_map, g_strdup (mime_type), desktop_file_ids);
2010 mime_info_cache_init_dir_lists (void)
2012 const char * const *dirs;
2015 mime_info_cache = mime_info_cache_new ();
2017 dirs = get_applications_search_path ();
2019 for (i = 0; dirs[i] != NULL; i++)
2021 MimeInfoCacheDir *dir;
2023 dir = mime_info_cache_dir_new (dirs[i]);
2027 mime_info_cache_dir_init (dir);
2028 mime_info_cache_dir_init_defaults_list (dir);
2030 mime_info_cache->dirs = g_list_append (mime_info_cache->dirs, dir);
2036 mime_info_cache_update_dir_lists (void)
2040 tmp = mime_info_cache->dirs;
2044 MimeInfoCacheDir *dir = (MimeInfoCacheDir *) tmp->data;
2046 /* No need to do this if we had file monitors... */
2047 mime_info_cache_blow_global_cache ();
2048 mime_info_cache_dir_init (dir);
2049 mime_info_cache_dir_init_defaults_list (dir);
2056 mime_info_cache_init (void)
2058 G_LOCK (mime_info_cache);
2059 if (mime_info_cache == NULL)
2060 mime_info_cache_init_dir_lists ();
2066 if (now >= mime_info_cache->last_stat_time + 10)
2068 mime_info_cache_update_dir_lists ();
2069 mime_info_cache->last_stat_time = now;
2073 if (mime_info_cache->should_ping_mime_monitor)
2075 /* g_idle_add (emit_mime_changed, NULL); */
2076 mime_info_cache->should_ping_mime_monitor = FALSE;
2079 G_UNLOCK (mime_info_cache);
2082 static MimeInfoCache *
2083 mime_info_cache_new (void)
2085 MimeInfoCache *cache;
2087 cache = g_new0 (MimeInfoCache, 1);
2089 cache->global_defaults_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
2090 (GDestroyNotify) g_free,
2091 (GDestroyNotify) g_free);
2096 mime_info_cache_free (MimeInfoCache *cache)
2101 g_list_foreach (cache->dirs,
2102 (GFunc) mime_info_cache_dir_free,
2104 g_list_free (cache->dirs);
2105 g_hash_table_destroy (cache->global_defaults_cache);
2110 * mime_info_cache_reload:
2111 * @dir: directory path which needs reloading.
2113 * Reload the mime information for the @dir.
2116 mime_info_cache_reload (const char *dir)
2118 /* FIXME: just reload the dir that needs reloading,
2119 * don't blow the whole cache
2121 if (mime_info_cache != NULL)
2123 G_LOCK (mime_info_cache);
2124 mime_info_cache_free (mime_info_cache);
2125 mime_info_cache = NULL;
2126 G_UNLOCK (mime_info_cache);
2131 append_desktop_entry (GList *list,
2132 const char *desktop_entry)
2134 /* Add if not already in list, and valid */
2135 if (!g_list_find_custom (list, desktop_entry, (GCompareFunc) strcmp))
2136 list = g_list_prepend (list, g_strdup (desktop_entry));
2142 * get_all_desktop_entries_for_mime_type:
2143 * @mime_type: a mime type.
2145 * Returns all the desktop filenames for @mime_type. The desktop files
2146 * are listed in an order so that default applications are listed before
2147 * non-default ones, and handlers for inherited mimetypes are listed
2148 * after the base ones.
2150 * Return value: a #GList containing the desktop filenames containing the
2154 get_all_desktop_entries_for_mime_type (const char *base_mime_type)
2156 GList *desktop_entries, *list, *dir_list, *tmp;
2157 MimeInfoCacheDir *dir;
2160 char **default_entries;
2163 mime_info_cache_init ();
2165 mime_types = _g_unix_content_type_get_parents (base_mime_type);
2166 G_LOCK (mime_info_cache);
2168 desktop_entries = NULL;
2169 for (i = 0; mime_types[i] != NULL; i++)
2171 mime_type = mime_types[i];
2173 /* Go through all apps listed as defaults */
2174 for (dir_list = mime_info_cache->dirs;
2176 dir_list = dir_list->next)
2178 dir = dir_list->data;
2179 default_entries = g_hash_table_lookup (dir->defaults_list_map, mime_type);
2180 for (j = 0; default_entries != NULL && default_entries[j] != NULL; j++)
2181 desktop_entries = append_desktop_entry (desktop_entries, default_entries[j]);
2184 /* Go through all entries that support the mimetype */
2185 for (dir_list = mime_info_cache->dirs;
2187 dir_list = dir_list->next)
2189 dir = dir_list->data;
2191 list = g_hash_table_lookup (dir->mime_info_cache_map, mime_type);
2192 for (tmp = list; tmp != NULL; tmp = tmp->next)
2193 desktop_entries = append_desktop_entry (desktop_entries, tmp->data);
2197 G_UNLOCK (mime_info_cache);
2199 g_strfreev (mime_types);
2201 desktop_entries = g_list_reverse (desktop_entries);
2203 return desktop_entries;
2206 #define __G_DESKTOP_APP_INFO_C__
2207 #include "gioaliasdef.c"