1 /* GTK - The GIMP Toolkit
2 * gtkrecentmanager.c: a manager for the recently used resources
4 * Copyright (C) 2006 Emmanuele Bassi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * SECTION:gtkrecentmanager
23 * @Title: GtkRecentManager
24 * @short_description: Managing recently used files
25 * @See_Also: #GBookmarkFile, #GtkSettings, #GtkRecentChooser
27 * #GtkRecentManager provides a facility for adding, removing and
28 * looking up recently used files. Each recently used file is
29 * identified by its URI, and has meta-data associated to it, like
30 * the names and command lines of the applications that have
31 * registered it, the number of time each application has registered
32 * the same file, the mime type of the file and whether the file
33 * should be displayed only by the applications that have
36 * <note><para>The recently used files list is per user.</para></note>
38 * The #GtkRecentManager acts like a database of all the recently
39 * used files. You can create new #GtkRecentManager objects, but
40 * it is more efficient to use the default manager created by GTK+.
42 * Adding a new recently used file is as simple as:
45 * GtkRecentManager *manager;
47 * manager = gtk_recent_manager_get_default ();
48 * gtk_recent_manager_add_item (manager, file_uri);
51 * The #GtkRecentManager will try to gather all the needed information
52 * from the file itself through GIO.
54 * Looking up the meta-data associated with a recently used file
55 * given its URI requires calling gtk_recent_manager_lookup_item():
58 * GtkRecentManager *manager;
59 * GtkRecentInfo *info;
60 * GError *error = NULL;
62 * manager = gtk_recent_manager_get_default ();
63 * info = gtk_recent_manager_lookup_item (manager, file_uri, &error);
66 * g_warning ("Could not find the file: %s", error->message);
67 * g_error_free (error);
71 * /* Use the info object */
72 * gtk_recent_info_unref (info);
76 * In order to retrieve the list of recently used files, you can use
77 * gtk_recent_manager_get_items(), which returns a list of #GtkRecentInfo
80 * A #GtkRecentManager is the model used to populate the contents of
81 * one, or more #GtkRecentChooser implementations.
83 * <note><para>The maximum age of the recently used files list is
84 * controllable through the #GtkSettings:gtk-recent-files-max-age
85 * property.</para></note>
87 * Recently used files are supported since GTK+ 2.10.
92 #include <sys/types.h>
101 #include <glib/gstdio.h>
104 #include "gtkrecentmanager.h"
106 #include "gtksettings.h"
107 #include "gtkstock.h"
108 #include "gtkicontheme.h"
109 #include "gtktypebuiltins.h"
110 #include "gtkprivate.h"
111 #include "gtkmarshalers.h"
113 /* the file where we store the recently used items */
114 #define GTK_RECENTLY_USED_FILE "recently-used.xbel"
116 /* return all items by default */
117 #define DEFAULT_LIMIT -1
119 /* keep in sync with xdgmime */
120 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
135 * #GtkRecentInfo is an opaque data structure
136 * whose members can only be accessed using the provided API.
138 * #GtkRecentInfo constains all the meta-data
139 * associated with an entry in the recently used files list.
143 struct _GtkRecentInfo
156 GSList *applications;
157 GHashTable *apps_lookup;
168 struct _GtkRecentManagerPrivate
176 GBookmarkFile *recent_items;
178 GFileMonitor *monitor;
180 guint changed_timeout;
193 static void gtk_recent_manager_dispose (GObject *object);
194 static void gtk_recent_manager_finalize (GObject *object);
195 static void gtk_recent_manager_set_property (GObject *object,
199 static void gtk_recent_manager_get_property (GObject *object,
203 static void gtk_recent_manager_add_item_query_info (GObject *source_object,
206 static void gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
209 GFileMonitorEvent event_type,
211 static void gtk_recent_manager_changed (GtkRecentManager *manager);
212 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
213 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
214 const gchar *filename);
215 static void gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
219 static void build_recent_items_list (GtkRecentManager *manager);
220 static void purge_recent_items_list (GtkRecentManager *manager,
223 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
224 static void recent_app_info_free (RecentAppInfo *app_info);
226 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
227 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
229 static guint signal_changed = 0;
231 static GtkRecentManager *recent_manager_singleton = NULL;
233 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
236 filename_warning (const gchar *format,
237 const gchar *filename,
238 const gchar *message)
240 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
241 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
245 /* Test of haystack has the needle prefix, comparing case
246 * insensitive. haystack may be UTF-8, but needle must
247 * contain only lowercase ascii. */
249 has_case_prefix (const gchar *haystack,
254 /* Eat one character at a time. */
258 while (*n && *h && *n == g_ascii_tolower (*h))
268 gtk_recent_manager_error_quark (void)
270 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
274 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
276 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
278 gobject_class->set_property = gtk_recent_manager_set_property;
279 gobject_class->get_property = gtk_recent_manager_get_property;
280 gobject_class->dispose = gtk_recent_manager_dispose;
281 gobject_class->finalize = gtk_recent_manager_finalize;
284 * GtkRecentManager:filename
286 * The full path to the file to be used to store and read the recently
287 * used resources list
291 g_object_class_install_property (gobject_class,
293 g_param_spec_string ("filename",
295 P_("The full path to the file to be used to store and read the list"),
297 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
300 * GtkRecentManager:size
302 * The size of the recently used resources list.
306 g_object_class_install_property (gobject_class,
308 g_param_spec_int ("size",
310 P_("The size of the recently used resources list"),
317 * GtkRecentManager::changed
318 * @recent_manager: the recent manager
320 * Emitted when the current recently used resources manager changes its
321 * contents, either by calling gtk_recent_manager_add_item() or by another
327 g_signal_new (I_("changed"),
328 G_TYPE_FROM_CLASS (klass),
330 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
332 g_cclosure_marshal_VOID__VOID,
335 klass->changed = gtk_recent_manager_real_changed;
337 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
341 gtk_recent_manager_init (GtkRecentManager *manager)
343 GtkRecentManagerPrivate *priv;
345 manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
346 GTK_TYPE_RECENT_MANAGER,
347 GtkRecentManagerPrivate);
348 priv = manager->priv;
351 priv->filename = NULL;
355 gtk_recent_manager_set_property (GObject *object,
360 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
365 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gtk_recent_manager_get_property (GObject *object,
379 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
384 g_value_set_string (value, recent_manager->priv->filename);
387 g_value_set_int (value, recent_manager->priv->size);
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396 gtk_recent_manager_finalize (GObject *object)
398 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
399 GtkRecentManagerPrivate *priv = manager->priv;
401 g_free (priv->filename);
403 if (priv->recent_items != NULL)
404 g_bookmark_file_free (priv->recent_items);
406 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
410 gtk_recent_manager_dispose (GObject *gobject)
412 GtkRecentManager *manager = GTK_RECENT_MANAGER (gobject);
413 GtkRecentManagerPrivate *priv = manager->priv;
415 if (priv->monitor != NULL)
417 g_signal_handlers_disconnect_by_func (priv->monitor,
418 G_CALLBACK (gtk_recent_manager_monitor_changed),
420 g_object_unref (priv->monitor);
421 priv->monitor = NULL;
424 if (priv->changed_timeout != 0)
426 g_source_remove (priv->changed_timeout);
427 priv->changed_timeout = 0;
428 priv->changed_age = 0;
433 g_object_ref (manager);
434 g_signal_emit (manager, signal_changed, 0);
435 g_object_unref (manager);
438 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (gobject);
442 gtk_recent_manager_real_changed (GtkRecentManager *manager)
444 GtkRecentManagerPrivate *priv = manager->priv;
446 g_object_freeze_notify (G_OBJECT (manager));
452 /* we are marked as dirty, so we dump the content of our
453 * recently used items list
455 g_assert (priv->filename != NULL);
457 if (!priv->recent_items)
459 /* if no container object has been defined, we create a new
460 * empty container, and dump it
462 priv->recent_items = g_bookmark_file_new ();
467 GtkSettings *settings = gtk_settings_get_default ();
470 g_object_get (G_OBJECT (settings), "gtk-recent-files-max-age", &age, NULL);
472 gtk_recent_manager_clamp_to_age (manager, age);
475 g_bookmark_file_free (priv->recent_items);
476 priv->recent_items = g_bookmark_file_new ();
481 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
484 filename_warning ("Attempting to store changes into `%s', "
487 write_error->message);
488 g_error_free (write_error);
491 if (g_chmod (priv->filename, 0600) < 0)
493 filename_warning ("Attempting to set the permissions of `%s', "
499 /* mark us as clean */
500 priv->is_dirty = FALSE;
504 /* we are not marked as dirty, so we have been called
505 * because the recently used resources file has been
506 * changed (and not from us).
508 build_recent_items_list (manager);
511 g_object_thaw_notify (G_OBJECT (manager));
515 gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
518 GFileMonitorEvent event_type,
521 GtkRecentManager *manager = user_data;
525 case G_FILE_MONITOR_EVENT_CHANGED:
526 case G_FILE_MONITOR_EVENT_CREATED:
527 gdk_threads_enter ();
528 gtk_recent_manager_changed (manager);
529 gdk_threads_leave ();
532 case G_FILE_MONITOR_EVENT_DELETED:
541 get_default_filename (void)
543 return g_build_filename (g_get_user_data_dir (),
544 GTK_RECENTLY_USED_FILE,
549 gtk_recent_manager_set_filename (GtkRecentManager *manager,
550 const gchar *filename)
552 GtkRecentManagerPrivate *priv;
556 g_assert (GTK_IS_RECENT_MANAGER (manager));
558 priv = manager->priv;
560 /* if a filename is already set and filename is not NULL, then copy
561 * it and reset the monitor; otherwise, if it's NULL we're being
562 * called from the finalization sequence, so we simply disconnect the
563 * monitoring and return.
565 * if no filename is set and filename is NULL, use the default.
569 g_free (priv->filename);
573 g_signal_handlers_disconnect_by_func (priv->monitor,
574 G_CALLBACK (gtk_recent_manager_monitor_changed),
576 g_object_unref (priv->monitor);
577 priv->monitor = NULL;
580 if (!filename || *filename == '\0')
583 priv->filename = g_strdup (filename);
587 if (!filename || *filename == '\0')
588 priv->filename = get_default_filename ();
590 priv->filename = g_strdup (filename);
593 g_assert (priv->filename != NULL);
594 file = g_file_new_for_path (priv->filename);
597 priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
600 filename_warning ("Unable to monitor `%s': %s\n"
601 "The GtkRecentManager will not update its contents "
602 "if the file is changed from other instances",
605 g_error_free (error);
608 g_signal_connect (priv->monitor, "changed",
609 G_CALLBACK (gtk_recent_manager_monitor_changed),
612 g_object_unref (file);
614 priv->is_dirty = FALSE;
615 build_recent_items_list (manager);
618 /* reads the recently used resources file and builds the items list.
619 * we keep the items list inside the parser object, and build the
620 * RecentInfo object only on user's demand to avoid useless replication.
621 * this function resets the dirty bit of the manager.
624 build_recent_items_list (GtkRecentManager *manager)
626 GtkRecentManagerPrivate *priv = manager->priv;
630 g_assert (priv->filename != NULL);
632 if (!priv->recent_items)
634 priv->recent_items = g_bookmark_file_new ();
638 /* the file exists, and it's valid (we hope); if not, destroy the container
639 * object and hope for a better result when the next "changed" signal is
642 g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
645 /* if the file does not exist we just wait for the first write
646 * operation on this recent manager instance, to avoid creating
647 * empty files and leading to spurious file system events (Sabayon
648 * will not be happy about those)
650 if (read_error->domain == G_FILE_ERROR &&
651 read_error->code != G_FILE_ERROR_NOENT)
652 filename_warning ("Attempting to read the recently used resources "
653 "file at `%s', but the parser failed: %s.",
655 read_error->message);
657 g_bookmark_file_free (priv->recent_items);
658 priv->recent_items = NULL;
660 g_error_free (read_error);
664 size = g_bookmark_file_get_size (priv->recent_items);
665 if (priv->size != size)
669 g_object_notify (G_OBJECT (manager), "size");
673 priv->is_dirty = FALSE;
677 /********************
679 ********************/
683 * gtk_recent_manager_new:
685 * Creates a new recent manager object. Recent manager objects are used to
686 * handle the list of recently used resources. A #GtkRecentManager object
687 * monitors the recently used resources list, and emits the "changed" signal
688 * each time something inside the list changes.
690 * #GtkRecentManager objects are expensive: be sure to create them only when
691 * needed. You should use gtk_recent_manager_get_default() instead.
693 * Return value: A newly created #GtkRecentManager object.
698 gtk_recent_manager_new (void)
700 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
704 * gtk_recent_manager_get_default:
706 * Gets a unique instance of #GtkRecentManager, that you can share
707 * in your application without caring about memory management.
709 * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
714 gtk_recent_manager_get_default (void)
716 if (G_UNLIKELY (!recent_manager_singleton))
717 recent_manager_singleton = gtk_recent_manager_new ();
719 return recent_manager_singleton;
724 gtk_recent_manager_add_item_query_info (GObject *source_object,
728 GFile *file = G_FILE (source_object);
729 GtkRecentManager *manager = user_data;
730 GtkRecentData recent_data;
731 GFileInfo *file_info;
735 uri = g_file_get_uri (file);
738 file_info = g_file_query_info_finish (file, res, &error);
741 g_warning ("Unable to retrieve the file info for `%s': %s",
744 g_error_free (error);
748 recent_data.display_name = NULL;
749 recent_data.description = NULL;
755 content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
757 if (G_LIKELY (content_type))
758 recent_data.mime_type = g_content_type_get_mime_type (content_type);
760 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
762 g_free (content_type);
763 g_object_unref (file_info);
766 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
768 recent_data.app_name = g_strdup (g_get_application_name ());
769 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
770 recent_data.groups = NULL;
771 recent_data.is_private = FALSE;
773 gdk_threads_enter ();
775 /* Ignore return value, this can't fail anyway since all required
777 gtk_recent_manager_add_full (manager, uri, &recent_data);
779 manager->priv->is_dirty = TRUE;
780 gtk_recent_manager_changed (manager);
782 gdk_threads_leave ();
784 g_free (recent_data.mime_type);
785 g_free (recent_data.app_name);
786 g_free (recent_data.app_exec);
789 g_object_unref (manager);
794 * gtk_recent_manager_add_item:
795 * @manager: a #GtkRecentManager
798 * Adds a new resource, pointed by @uri, into the recently used
801 * This function automatically retrieves some of the needed
802 * metadata and setting other metadata to common default values; it
803 * then feeds the data to gtk_recent_manager_add_full().
805 * See gtk_recent_manager_add_full() if you want to explicitly
806 * define the metadata for the resource pointed by @uri.
808 * Return value: %TRUE if the new item was successfully added
809 * to the recently used resources list
814 gtk_recent_manager_add_item (GtkRecentManager *manager,
819 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
820 g_return_val_if_fail (uri != NULL, FALSE);
822 file = g_file_new_for_uri (uri);
824 g_file_query_info_async (file,
825 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
827 G_FILE_QUERY_INFO_NONE,
829 gtk_recent_manager_add_item_query_info,
830 g_object_ref (manager));
832 g_object_unref (file);
838 * gtk_recent_manager_add_full:
839 * @manager: a #GtkRecentManager
841 * @recent_data: metadata of the resource
843 * Adds a new resource, pointed by @uri, into the recently used
844 * resources list, using the metadata specified inside the #GtkRecentData
845 * structure passed in @recent_data.
847 * The passed URI will be used to identify this resource inside the
850 * In order to register the new recently used resource, metadata about
851 * the resource must be passed as well as the URI; the metadata is
852 * stored in a #GtkRecentData structure, which must contain the MIME
853 * type of the resource pointed by the URI; the name of the application
854 * that is registering the item, and a command line to be used when
855 * launching the item.
857 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
858 * to be used when viewing the item instead of the last component of the
859 * URI; a short description of the item; whether the item should be
860 * considered private - that is, should be displayed only by the
861 * applications that have registered it.
863 * Return value: %TRUE if the new item was successfully added to the
864 * recently used resources list, %FALSE otherwise.
869 gtk_recent_manager_add_full (GtkRecentManager *manager,
871 const GtkRecentData *data)
873 GtkRecentManagerPrivate *priv;
875 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
876 g_return_val_if_fail (uri != NULL, FALSE);
877 g_return_val_if_fail (data != NULL, FALSE);
880 if ((data->display_name) &&
881 (!g_utf8_validate (data->display_name, -1, NULL)))
883 g_warning ("Attempting to add `%s' to the list of recently used "
884 "resources, but the display name is not a valid UTF-8 "
890 if ((data->description) &&
891 (!g_utf8_validate (data->description, -1, NULL)))
893 g_warning ("Attempting to add `%s' to the list of recently used "
894 "resources, but the description is not a valid UTF-8 "
901 if (!data->mime_type)
903 g_warning ("Attempting to add `%s' to the list of recently used "
904 "resources, but not MIME type was defined",
911 g_warning ("Attempting to add `%s' to the list of recently used "
912 "resources, but no name of the application that is "
913 "registering it was defined",
920 g_warning ("Attempting to add `%s' to the list of recently used "
921 "resources, but no command line for the application "
922 "that is registering it was defined",
927 priv = manager->priv;
929 if (!priv->recent_items)
931 priv->recent_items = g_bookmark_file_new ();
935 if (data->display_name)
936 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
938 if (data->description)
939 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
941 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
943 if (data->groups && data->groups[0] != '\0')
947 for (j = 0; (data->groups)[j] != NULL; j++)
948 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
951 /* register the application; this will take care of updating the
952 * registration count and time in case the application has
953 * already registered the same document inside the list
955 g_bookmark_file_add_application (priv->recent_items, uri,
959 g_bookmark_file_set_is_private (priv->recent_items, uri,
962 /* mark us as dirty, so that when emitting the "changed" signal we
963 * will dump our changes
965 priv->is_dirty = TRUE;
966 gtk_recent_manager_changed (manager);
972 * gtk_recent_manager_remove_item:
973 * @manager: a #GtkRecentManager
974 * @uri: the URI of the item you wish to remove
975 * @error: (allow-none): return location for a #GError, or %NULL
977 * Removes a resource pointed by @uri from the recently used resources
978 * list handled by a recent manager.
980 * Return value: %TRUE if the item pointed by @uri has been successfully
981 * removed by the recently used resources list, and %FALSE otherwise.
986 gtk_recent_manager_remove_item (GtkRecentManager *manager,
990 GtkRecentManagerPrivate *priv;
991 GError *remove_error = NULL;
993 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
994 g_return_val_if_fail (uri != NULL, FALSE);
995 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
997 priv = manager->priv;
999 if (!priv->recent_items)
1001 priv->recent_items = g_bookmark_file_new ();
1004 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1005 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1006 _("Unable to find an item with URI '%s'"),
1012 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1015 g_error_free (remove_error);
1017 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1018 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1019 _("Unable to find an item with URI '%s'"),
1025 priv->is_dirty = TRUE;
1026 gtk_recent_manager_changed (manager);
1032 * gtk_recent_manager_has_item:
1033 * @manager: a #GtkRecentManager
1036 * Checks whether there is a recently used resource registered
1037 * with @uri inside the recent manager.
1039 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1044 gtk_recent_manager_has_item (GtkRecentManager *manager,
1047 GtkRecentManagerPrivate *priv;
1049 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1050 g_return_val_if_fail (uri != NULL, FALSE);
1052 priv = manager->priv;
1053 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1055 return g_bookmark_file_has_item (priv->recent_items, uri);
1059 build_recent_info (GBookmarkFile *bookmarks,
1060 GtkRecentInfo *info)
1062 gchar **apps, **groups;
1063 gsize apps_len, groups_len, i;
1065 g_assert (bookmarks != NULL);
1066 g_assert (info != NULL);
1068 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1069 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1070 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1072 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1074 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1075 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1076 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1078 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1079 for (i = 0; i < groups_len; i++)
1081 gchar *group_name = g_strdup (groups[i]);
1083 info->groups = g_slist_append (info->groups, group_name);
1086 g_strfreev (groups);
1088 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1089 for (i = 0; i < apps_len; i++)
1091 gchar *app_name, *app_exec;
1094 RecentAppInfo *app_info;
1099 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1107 app_info = recent_app_info_new (app_name);
1108 app_info->exec = app_exec;
1109 app_info->count = count;
1110 app_info->stamp = stamp;
1112 info->applications = g_slist_prepend (info->applications, app_info);
1113 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1120 * gtk_recent_manager_lookup_item:
1121 * @manager: a #GtkRecentManager
1123 * @error: (allow-none): a return location for a #GError, or %NULL
1125 * Searches for a URI inside the recently used resources list, and
1126 * returns a structure containing informations about the resource
1127 * like its MIME type, or its display name.
1129 * Return value: a #GtkRecentInfo structure containing information
1130 * about the resource pointed by @uri, or %NULL if the URI was
1131 * not registered in the recently used resources list. Free with
1132 * gtk_recent_info_unref().
1137 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1141 GtkRecentManagerPrivate *priv;
1142 GtkRecentInfo *info = NULL;
1144 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1145 g_return_val_if_fail (uri != NULL, NULL);
1146 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1148 priv = manager->priv;
1149 if (!priv->recent_items)
1151 priv->recent_items = g_bookmark_file_new ();
1154 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1155 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1156 _("Unable to find an item with URI '%s'"),
1162 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1164 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1165 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1166 _("Unable to find an item with URI '%s'"),
1171 info = gtk_recent_info_new (uri);
1172 g_return_val_if_fail (info != NULL, NULL);
1174 /* fill the RecentInfo structure with the data retrieved by our
1175 * parser object from the storage file
1177 build_recent_info (priv->recent_items, info);
1183 * gtk_recent_manager_move_item:
1184 * @manager: a #GtkRecentManager
1185 * @uri: the URI of a recently used resource
1186 * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1187 * remove the item pointed by @uri in the list
1188 * @error: (allow-none): a return location for a #GError, or %NULL
1190 * Changes the location of a recently used resource from @uri to @new_uri.
1192 * Please note that this function will not affect the resource pointed
1193 * by the URIs, but only the URI used in the recently used resources list.
1195 * Return value: %TRUE on success.
1200 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1202 const gchar *new_uri,
1205 GtkRecentManagerPrivate *priv;
1208 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1209 g_return_val_if_fail (uri != NULL, FALSE);
1210 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1212 priv = recent_manager->priv;
1214 if (!priv->recent_items)
1216 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1217 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1218 _("Unable to find an item with URI '%s'"),
1223 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1225 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1226 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1227 _("Unable to find an item with URI '%s'"),
1233 if (!g_bookmark_file_move_item (priv->recent_items,
1238 g_error_free (move_error);
1240 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1241 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1242 _("Unable to find an item with URI '%s'"),
1247 priv->is_dirty = TRUE;
1248 gtk_recent_manager_changed (recent_manager);
1254 * gtk_recent_manager_get_items:
1255 * @manager: a #GtkRecentManager
1257 * Gets the list of recently used resources.
1259 * Return value: (element-type GtkRecentInfo) (transfer full): a list of
1260 * newly allocated #GtkRecentInfo objects. Use
1261 * gtk_recent_info_unref() on each item inside the list, and then
1262 * free the list itself using g_list_free().
1267 gtk_recent_manager_get_items (GtkRecentManager *manager)
1269 GtkRecentManagerPrivate *priv;
1270 GList *retval = NULL;
1274 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1276 priv = manager->priv;
1277 if (!priv->recent_items)
1280 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1281 for (i = 0; i < uris_len; i++)
1283 GtkRecentInfo *info;
1285 info = gtk_recent_info_new (uris[i]);
1286 build_recent_info (priv->recent_items, info);
1288 retval = g_list_prepend (retval, info);
1297 purge_recent_items_list (GtkRecentManager *manager,
1300 GtkRecentManagerPrivate *priv = manager->priv;
1302 if (priv->recent_items == NULL)
1305 g_bookmark_file_free (priv->recent_items);
1306 priv->recent_items = g_bookmark_file_new ();
1309 /* emit the changed signal, to ensure that the purge is written */
1310 priv->is_dirty = TRUE;
1311 gtk_recent_manager_changed (manager);
1315 * gtk_recent_manager_purge_items:
1316 * @manager: a #GtkRecentManager
1317 * @error: (allow-none): a return location for a #GError, or %NULL
1319 * Purges every item from the recently used resources list.
1321 * Return value: the number of items that have been removed from the
1322 * recently used resources list.
1327 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1330 GtkRecentManagerPrivate *priv;
1333 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1335 priv = manager->priv;
1336 if (!priv->recent_items)
1339 count = g_bookmark_file_get_size (priv->recent_items);
1343 purge_recent_items_list (manager, error);
1345 purged = count - g_bookmark_file_get_size (priv->recent_items);
1351 emit_manager_changed (gpointer data)
1353 GtkRecentManager *manager = data;
1355 manager->priv->changed_age = 0;
1356 manager->priv->changed_timeout = 0;
1358 g_signal_emit (manager, signal_changed, 0);
1364 gtk_recent_manager_changed (GtkRecentManager *manager)
1366 /* coalesce consecutive changes
1368 * we schedule a write in 250 msecs immediately; if we get more than one
1369 * request per millisecond before the timeout has a chance to run, we
1370 * schedule an emission immediately.
1372 if (manager->priv->changed_timeout == 0)
1373 manager->priv->changed_timeout = gdk_threads_add_timeout (250, emit_manager_changed, manager);
1376 manager->priv->changed_age += 1;
1378 if (manager->priv->changed_age > 250)
1380 g_source_remove (manager->priv->changed_timeout);
1381 g_signal_emit (manager, signal_changed, 0);
1383 manager->priv->changed_age = 0;
1384 manager->priv->changed_timeout = 0;
1390 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1393 GtkRecentManagerPrivate *priv = manager->priv;
1398 if (G_UNLIKELY (!priv->recent_items))
1403 uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1405 for (i = 0; i < n_uris; i++)
1407 const gchar *uri = uris[i];
1411 modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1412 item_age = (gint) ((now - modified) / (60 * 60 * 24));
1414 g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1424 G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
1425 gtk_recent_info_ref,
1426 gtk_recent_info_unref)
1428 static GtkRecentInfo *
1429 gtk_recent_info_new (const gchar *uri)
1431 GtkRecentInfo *info;
1433 g_assert (uri != NULL);
1435 info = g_new0 (GtkRecentInfo, 1);
1436 info->uri = g_strdup (uri);
1438 info->applications = NULL;
1439 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1441 info->groups = NULL;
1443 info->ref_count = 1;
1449 gtk_recent_info_free (GtkRecentInfo *recent_info)
1454 g_free (recent_info->uri);
1455 g_free (recent_info->display_name);
1456 g_free (recent_info->description);
1457 g_free (recent_info->mime_type);
1459 if (recent_info->applications)
1461 g_slist_foreach (recent_info->applications,
1462 (GFunc) recent_app_info_free,
1464 g_slist_free (recent_info->applications);
1466 recent_info->applications = NULL;
1469 if (recent_info->apps_lookup)
1470 g_hash_table_destroy (recent_info->apps_lookup);
1472 if (recent_info->groups)
1474 g_slist_foreach (recent_info->groups,
1477 g_slist_free (recent_info->groups);
1479 recent_info->groups = NULL;
1482 if (recent_info->icon)
1483 g_object_unref (recent_info->icon);
1485 g_free (recent_info);
1489 * gtk_recent_info_ref:
1490 * @info: a #GtkRecentInfo
1492 * Increases the reference count of @recent_info by one.
1494 * Return value: the recent info object with its reference count increased
1500 gtk_recent_info_ref (GtkRecentInfo *info)
1502 g_return_val_if_fail (info != NULL, NULL);
1503 g_return_val_if_fail (info->ref_count > 0, NULL);
1505 info->ref_count += 1;
1511 * gtk_recent_info_unref:
1512 * @info: a #GtkRecentInfo
1514 * Decreases the reference count of @info by one. If the reference
1515 * count reaches zero, @info is deallocated, and the memory freed.
1520 gtk_recent_info_unref (GtkRecentInfo *info)
1522 g_return_if_fail (info != NULL);
1523 g_return_if_fail (info->ref_count > 0);
1525 info->ref_count -= 1;
1527 if (info->ref_count == 0)
1528 gtk_recent_info_free (info);
1532 * gtk_recent_info_get_uri:
1533 * @info: a #GtkRecentInfo
1535 * Gets the URI of the resource.
1537 * Return value: the URI of the resource. The returned string is
1538 * owned by the recent manager, and should not be freed.
1543 gtk_recent_info_get_uri (GtkRecentInfo *info)
1545 g_return_val_if_fail (info != NULL, NULL);
1551 * gtk_recent_info_get_display_name:
1552 * @info: a #GtkRecentInfo
1554 * Gets the name of the resource. If none has been defined, the basename
1555 * of the resource is obtained.
1557 * Return value: the display name of the resource. The returned string
1558 * is owned by the recent manager, and should not be freed.
1563 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1565 g_return_val_if_fail (info != NULL, NULL);
1567 if (!info->display_name)
1568 info->display_name = gtk_recent_info_get_short_name (info);
1570 return info->display_name;
1574 * gtk_recent_info_get_description:
1575 * @info: a #GtkRecentInfo
1577 * Gets the (short) description of the resource.
1579 * Return value: the description of the resource. The returned string
1580 * is owned by the recent manager, and should not be freed.
1585 gtk_recent_info_get_description (GtkRecentInfo *info)
1587 g_return_val_if_fail (info != NULL, NULL);
1589 return info->description;
1593 * gtk_recent_info_get_mime_type:
1594 * @info: a #GtkRecentInfo
1596 * Gets the MIME type of the resource.
1598 * Return value: the MIME type of the resource. The returned string
1599 * is owned by the recent manager, and should not be freed.
1604 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1606 g_return_val_if_fail (info != NULL, NULL);
1608 if (!info->mime_type)
1609 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1611 return info->mime_type;
1615 * gtk_recent_info_get_added:
1616 * @info: a #GtkRecentInfo
1618 * Gets the timestamp (seconds from system's Epoch) when the resource
1619 * was added to the recently used resources list.
1621 * Return value: the number of seconds elapsed from system's Epoch when
1622 * the resource was added to the list, or -1 on failure.
1627 gtk_recent_info_get_added (GtkRecentInfo *info)
1629 g_return_val_if_fail (info != NULL, (time_t) -1);
1635 * gtk_recent_info_get_modified:
1636 * @info: a #GtkRecentInfo
1638 * Gets the timestamp (seconds from system's Epoch) when the resource
1639 * was last modified.
1641 * Return value: the number of seconds elapsed from system's Epoch when
1642 * the resource was last modified, or -1 on failure.
1647 gtk_recent_info_get_modified (GtkRecentInfo *info)
1649 g_return_val_if_fail (info != NULL, (time_t) -1);
1651 return info->modified;
1655 * gtk_recent_info_get_visited:
1656 * @info: a #GtkRecentInfo
1658 * Gets the timestamp (seconds from system's Epoch) when the resource
1661 * Return value: the number of seconds elapsed from system's Epoch when
1662 * the resource was last visited, or -1 on failure.
1667 gtk_recent_info_get_visited (GtkRecentInfo *info)
1669 g_return_val_if_fail (info != NULL, (time_t) -1);
1671 return info->visited;
1675 * gtk_recent_info_get_private_hint:
1676 * @info: a #GtkRecentInfo
1678 * Gets the value of the "private" flag. Resources in the recently used
1679 * list that have this flag set to %TRUE should only be displayed by the
1680 * applications that have registered them.
1682 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1687 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1689 g_return_val_if_fail (info != NULL, FALSE);
1691 return info->is_private;
1695 static RecentAppInfo *
1696 recent_app_info_new (const gchar *app_name)
1698 RecentAppInfo *app_info;
1700 g_assert (app_name != NULL);
1702 app_info = g_slice_new0 (RecentAppInfo);
1703 app_info->name = g_strdup (app_name);
1704 app_info->exec = NULL;
1705 app_info->count = 1;
1706 app_info->stamp = 0;
1712 recent_app_info_free (RecentAppInfo *app_info)
1717 g_free (app_info->name);
1718 g_free (app_info->exec);
1720 g_slice_free (RecentAppInfo, app_info);
1724 * gtk_recent_info_get_application_info:
1725 * @info: a #GtkRecentInfo
1726 * @app_name: the name of the application that has registered this item
1727 * @app_exec: (transfer none) (out): return location for the string containing the command line
1728 * @count: (out): return location for the number of times this item was registered
1729 * @time_: (out): return location for the timestamp this item was last registered
1730 * for this application
1732 * Gets the data regarding the application that has registered the resource
1735 * If the command line contains any escape characters defined inside the
1736 * storage specification, they will be expanded.
1738 * Return value: %TRUE if an application with @app_name has registered this
1739 * resource inside the recently used list, or %FALSE otherwise. The
1740 * @app_exec string is owned by the #GtkRecentInfo and should not be
1746 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1747 const gchar *app_name,
1748 const gchar **app_exec,
1754 g_return_val_if_fail (info != NULL, FALSE);
1755 g_return_val_if_fail (app_name != NULL, FALSE);
1757 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1761 g_warning ("No registered application with name '%s' "
1762 "for item with URI '%s' found",
1769 *app_exec = ai->exec;
1781 * gtk_recent_info_get_applications:
1782 * @info: a #GtkRecentInfo
1783 * @length: (out) (allow-none): return location for the length of the returned list
1785 * Retrieves the list of applications that have registered this resource.
1787 * Return value: (array length=length zero-terminated=1) (transfer full):
1788 * a newly allocated %NULL-terminated array of strings.
1789 * Use g_strfreev() to free it.
1794 gtk_recent_info_get_applications (GtkRecentInfo *info,
1801 g_return_val_if_fail (info != NULL, NULL);
1803 if (!info->applications)
1811 n_apps = g_slist_length (info->applications);
1813 retval = g_new0 (gchar *, n_apps + 1);
1815 for (l = info->applications, i = 0;
1819 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1821 g_assert (ai != NULL);
1823 retval[i++] = g_strdup (ai->name);
1834 * gtk_recent_info_has_application:
1835 * @info: a #GtkRecentInfo
1836 * @app_name: a string containing an application name
1838 * Checks whether an application registered this resource using @app_name.
1840 * Return value: %TRUE if an application with name @app_name was found,
1846 gtk_recent_info_has_application (GtkRecentInfo *info,
1847 const gchar *app_name)
1849 g_return_val_if_fail (info != NULL, FALSE);
1850 g_return_val_if_fail (app_name != NULL, FALSE);
1852 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1856 * gtk_recent_info_last_application:
1857 * @info: a #GtkRecentInfo
1859 * Gets the name of the last application that have registered the
1860 * recently used resource represented by @info.
1862 * Return value: an application name. Use g_free() to free it.
1867 gtk_recent_info_last_application (GtkRecentInfo *info)
1870 time_t last_stamp = (time_t) -1;
1873 g_return_val_if_fail (info != NULL, NULL);
1875 for (l = info->applications; l != NULL; l = l->next)
1877 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1879 if (ai->stamp > last_stamp)
1882 last_stamp = ai->stamp;
1886 return g_strdup (name);
1890 get_icon_for_mime_type (const char *mime_type,
1893 GtkIconTheme *icon_theme;
1899 icon_theme = gtk_icon_theme_get_default ();
1901 content_type = g_content_type_from_mime_type (mime_type);
1906 icon = g_content_type_get_icon (content_type);
1907 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1910 GTK_ICON_LOOKUP_USE_BUILTIN);
1911 g_free (content_type);
1912 g_object_unref (icon);
1917 pixbuf = gtk_icon_info_load_icon (info, NULL);
1918 gtk_icon_info_free (info);
1924 get_icon_fallback (const gchar *icon_name,
1927 GtkIconTheme *icon_theme;
1930 icon_theme = gtk_icon_theme_get_default ();
1932 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1934 GTK_ICON_LOOKUP_USE_BUILTIN,
1936 g_assert (retval != NULL);
1942 * gtk_recent_info_get_icon:
1943 * @info: a #GtkRecentInfo
1944 * @size: the size of the icon in pixels
1946 * Retrieves the icon of size @size associated to the resource MIME type.
1948 * Return value: (transfer full): a #GdkPixbuf containing the icon,
1949 * or %NULL. Use g_object_unref() when finished using the icon.
1954 gtk_recent_info_get_icon (GtkRecentInfo *info,
1957 GdkPixbuf *retval = NULL;
1959 g_return_val_if_fail (info != NULL, NULL);
1961 if (info->mime_type)
1962 retval = get_icon_for_mime_type (info->mime_type, size);
1964 /* this function should never fail */
1967 if (info->mime_type &&
1968 strcmp (info->mime_type, "x-directory/normal") == 0)
1969 retval = get_icon_fallback ("folder", size);
1971 retval = get_icon_fallback ("text-x-generic", size);
1978 * gtk_recent_info_get_gicon:
1979 * @info: a #GtkRecentInfo
1981 * Retrieves the icon associated to the resource MIME type.
1983 * Return value: (transfer full): a #GIcon containing the icon, or %NULL. Use
1984 * g_object_unref() when finished using the icon
1989 gtk_recent_info_get_gicon (GtkRecentInfo *info)
1992 gchar *content_type;
1994 g_return_val_if_fail (info != NULL, NULL);
1996 if (info->mime_type != NULL &&
1997 (content_type = g_content_type_from_mime_type (info->mime_type)) != NULL)
1999 icon = g_content_type_get_icon (content_type);
2000 g_free (content_type);
2007 * gtk_recent_info_is_local:
2008 * @info: a #GtkRecentInfo
2010 * Checks whether the resource is local or not by looking at the
2011 * scheme of its URI.
2013 * Return value: %TRUE if the resource is local.
2018 gtk_recent_info_is_local (GtkRecentInfo *info)
2020 g_return_val_if_fail (info != NULL, FALSE);
2022 return has_case_prefix (info->uri, "file:/");
2026 * gtk_recent_info_exists:
2027 * @info: a #GtkRecentInfo
2029 * Checks whether the resource pointed by @info still exists. At
2030 * the moment this check is done only on resources pointing to local files.
2032 * Return value: %TRUE if the resource exists
2037 gtk_recent_info_exists (GtkRecentInfo *info)
2040 struct stat stat_buf;
2041 gboolean retval = FALSE;
2043 g_return_val_if_fail (info != NULL, FALSE);
2045 /* we guarantee only local resources */
2046 if (!gtk_recent_info_is_local (info))
2049 filename = g_filename_from_uri (info->uri, NULL, NULL);
2052 if (stat (filename, &stat_buf) == 0)
2062 * gtk_recent_info_match:
2063 * @info_a: a #GtkRecentInfo
2064 * @info_b: a #GtkRecentInfo
2066 * Checks whether two #GtkRecentInfo structures point to the same
2069 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2070 * resource, %FALSE otherwise.
2075 gtk_recent_info_match (GtkRecentInfo *info_a,
2076 GtkRecentInfo *info_b)
2078 g_return_val_if_fail (info_a != NULL, FALSE);
2079 g_return_val_if_fail (info_b != NULL, FALSE);
2081 return (0 == strcmp (info_a->uri, info_b->uri));
2084 /* taken from gnome-vfs-uri.c */
2085 static const gchar *
2086 get_method_string (const gchar *substring,
2087 gchar **method_string)
2093 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2100 !(p == substring + 1 && g_ascii_isalpha (*substring))
2104 /* Found toplevel method specification. */
2105 method = g_strndup (substring, p - substring);
2106 *method_string = g_ascii_strdown (method, -1);
2112 *method_string = g_strdup ("file");
2119 /* Stolen from gnome_vfs_make_valid_utf8() */
2121 make_valid_utf8 (const char *name)
2124 const char *remainder, *invalid;
2125 int remaining_bytes, valid_bytes;
2129 remaining_bytes = name ? strlen (name) : 0;
2131 while (remaining_bytes != 0)
2133 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2136 valid_bytes = invalid - remainder;
2139 string = g_string_sized_new (remaining_bytes);
2141 g_string_append_len (string, remainder, valid_bytes);
2142 g_string_append_c (string, '?');
2144 remaining_bytes -= valid_bytes + 1;
2145 remainder = invalid + 1;
2149 return g_strdup (name);
2151 g_string_append (string, remainder);
2152 g_assert (g_utf8_validate (string->str, -1, NULL));
2154 return g_string_free (string, FALSE);
2158 get_uri_shortname_for_display (const gchar *uri)
2161 gboolean validated = FALSE;
2163 if (has_case_prefix (uri, "file:/"))
2167 local_file = g_filename_from_uri (uri, NULL, NULL);
2171 name = g_filename_display_basename (local_file);
2175 g_free (local_file);
2184 rest = get_method_string (uri, &method);
2185 local_file = g_filename_display_basename (rest);
2187 name = g_strconcat (method, ": ", local_file, NULL);
2189 g_free (local_file);
2193 g_assert (name != NULL);
2195 if (!validated && !g_utf8_validate (name, -1, NULL))
2199 utf8_name = make_valid_utf8 (name);
2209 * gtk_recent_info_get_short_name:
2210 * @info: an #GtkRecentInfo
2212 * Computes a valid UTF-8 string that can be used as the name of the item in a
2213 * menu or list. For example, calling this function on an item that refers to
2214 * "file:///foo/bar.txt" will yield "bar.txt".
2216 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2222 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2226 g_return_val_if_fail (info != NULL, NULL);
2228 if (info->uri == NULL)
2231 short_name = get_uri_shortname_for_display (info->uri);
2237 * gtk_recent_info_get_uri_display:
2238 * @info: a #GtkRecentInfo
2240 * Gets a displayable version of the resource's URI. If the resource
2241 * is local, it returns a local path; if the resource is not local,
2242 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2244 * Return value: a newly allocated UTF-8 string containing the
2245 * resource's URI or %NULL. Use g_free() when done using it.
2250 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2254 g_return_val_if_fail (info != NULL, NULL);
2257 if (gtk_recent_info_is_local (info))
2261 filename = g_filename_from_uri (info->uri, NULL, NULL);
2265 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2270 retval = make_valid_utf8 (info->uri);
2277 * gtk_recent_info_get_age:
2278 * @info: a #GtkRecentInfo
2280 * Gets the number of days elapsed since the last update of the resource
2283 * Return value: a positive integer containing the number of days elapsed
2284 * since the time this resource was last modified.
2289 gtk_recent_info_get_age (GtkRecentInfo *info)
2294 g_return_val_if_fail (info != NULL, -1);
2298 delta = now - info->modified;
2300 retval = (gint) (delta / (60 * 60 * 24));
2306 * gtk_recent_info_get_groups:
2307 * @info: a #GtkRecentInfo
2308 * @length: (out) (allow-none): return location for the number of groups returned
2310 * Returns all groups registered for the recently used item @info. The
2311 * array of returned group names will be %NULL terminated, so length might
2312 * optionally be %NULL.
2314 * Return value: (array length=length zero-terminated=1) (transfer full):
2315 * a newly allocated %NULL terminated array of strings.
2316 * Use g_strfreev() to free it.
2321 gtk_recent_info_get_groups (GtkRecentInfo *info,
2328 g_return_val_if_fail (info != NULL, NULL);
2338 n_groups = g_slist_length (info->groups);
2340 retval = g_new0 (gchar *, n_groups + 1);
2342 for (l = info->groups, i = 0;
2346 gchar *group_name = (gchar *) l->data;
2348 g_assert (group_name != NULL);
2350 retval[i++] = g_strdup (group_name);
2361 * gtk_recent_info_has_group:
2362 * @info: a #GtkRecentInfo
2363 * @group_name: name of a group
2365 * Checks whether @group_name appears inside the groups registered for the
2366 * recently used item @info.
2368 * Return value: %TRUE if the group was found.
2373 gtk_recent_info_has_group (GtkRecentInfo *info,
2374 const gchar *group_name)
2378 g_return_val_if_fail (info != NULL, FALSE);
2379 g_return_val_if_fail (group_name != NULL, FALSE);
2384 for (l = info->groups; l != NULL; l = l->next)
2386 gchar *g = (gchar *) l->data;
2388 if (strcmp (g, group_name) == 0)
2396 * gtk_recent_info_create_app_info:
2397 * @info: a #GtkRecentInfo
2398 * @app_name: (allow-none): the name of the application that should
2399 * be mapped to a #GAppInfo; if %NULL is used then the default
2400 * application for the MIME type is used
2401 * @error: (allow-none): return location for a #GError, or %NULL
2403 * Creates a #GAppInfo for the specified #GtkRecentInfo
2405 * Return value: (transfer full): the newly created #GAppInfo, or %NULL.
2406 * In case of error, @error will be set either with a
2407 * %GTK_RECENT_MANAGER_ERROR or a %G_IO_ERROR
2410 gtk_recent_info_create_app_info (GtkRecentInfo *info,
2411 const gchar *app_name,
2416 GError *internal_error = NULL;
2418 g_return_val_if_fail (info != NULL, NULL);
2420 if (app_name == NULL || *app_name == '\0')
2424 if (info->mime_type == NULL)
2427 content_type = g_content_type_from_mime_type (info->mime_type);
2428 if (content_type == NULL)
2431 app_info = g_app_info_get_default_for_type (content_type, TRUE);
2432 g_free (content_type);
2437 ai = g_hash_table_lookup (info->apps_lookup, app_name);
2440 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
2441 GTK_RECENT_MANAGER_ERROR_NOT_REGISTERED,
2442 _("No registered application with name '%s' for item with URI '%s' found"),
2448 internal_error = NULL;
2449 app_info = g_app_info_create_from_commandline (ai->exec, ai->name,
2450 G_APP_INFO_CREATE_NONE,
2452 if (internal_error != NULL)
2454 g_propagate_error (error, internal_error);
2462 * _gtk_recent_manager_sync:
2464 * Private function for synchronising the recent manager singleton.
2467 _gtk_recent_manager_sync (void)
2469 if (recent_manager_singleton)
2471 /* force a dump of the contents of the recent manager singleton */
2472 recent_manager_singleton->priv->is_dirty = TRUE;
2473 gtk_recent_manager_real_changed (recent_manager_singleton);