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 "gtkstock.h"
107 #include "gtkicontheme.h"
108 #include "gtktypebuiltins.h"
109 #include "gtkprivate.h"
110 #include "gtkmarshalers.h"
112 /* the file where we store the recently used items */
113 #define GTK_RECENTLY_USED_FILE ".recently-used.xbel"
115 /* return all items by default */
116 #define DEFAULT_LIMIT -1
118 /* keep in sync with xdgmime */
119 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
134 * #GtkRecentInfo is an opaque data structure
135 * whose members can only be accessed using the provided API.
137 * #GtkRecentInfo constains all the meta-data
138 * associated with an entry in the recently used files list.
142 struct _GtkRecentInfo
155 GSList *applications;
156 GHashTable *apps_lookup;
167 struct _GtkRecentManagerPrivate
175 GBookmarkFile *recent_items;
177 GFileMonitor *monitor;
189 static void gtk_recent_manager_dispose (GObject *object);
190 static void gtk_recent_manager_finalize (GObject *object);
191 static void gtk_recent_manager_set_property (GObject *object,
195 static void gtk_recent_manager_get_property (GObject *object,
199 static void gtk_recent_manager_add_item_query_info (GObject *source_object,
202 static void gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
205 GFileMonitorEvent event_type,
207 static void gtk_recent_manager_changed (GtkRecentManager *manager);
208 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
209 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
210 const gchar *filename);
211 static void gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
215 static void build_recent_items_list (GtkRecentManager *manager);
216 static void purge_recent_items_list (GtkRecentManager *manager,
219 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
220 static void recent_app_info_free (RecentAppInfo *app_info);
222 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
223 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
225 static guint signal_changed = 0;
227 static GtkRecentManager *recent_manager_singleton = NULL;
229 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
232 filename_warning (const gchar *format,
233 const gchar *filename,
234 const gchar *message)
236 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
237 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
241 /* Test of haystack has the needle prefix, comparing case
242 * insensitive. haystack may be UTF-8, but needle must
243 * contain only lowercase ascii. */
245 has_case_prefix (const gchar *haystack,
250 /* Eat one character at a time. */
254 while (*n && *h && *n == g_ascii_tolower (*h))
264 gtk_recent_manager_error_quark (void)
266 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
270 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
272 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
274 gobject_class->set_property = gtk_recent_manager_set_property;
275 gobject_class->get_property = gtk_recent_manager_get_property;
276 gobject_class->dispose = gtk_recent_manager_dispose;
277 gobject_class->finalize = gtk_recent_manager_finalize;
280 * GtkRecentManager:filename
282 * The full path to the file to be used to store and read the recently
283 * used resources list
287 g_object_class_install_property (gobject_class,
289 g_param_spec_string ("filename",
291 P_("The full path to the file to be used to store and read the list"),
293 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
296 * GtkRecentManager:size
298 * The size of the recently used resources list.
302 g_object_class_install_property (gobject_class,
304 g_param_spec_int ("size",
306 P_("The size of the recently used resources list"),
313 * GtkRecentManager::changed
314 * @recent_manager: the recent manager
316 * Emitted when the current recently used resources manager changes its
317 * contents, either by calling gtk_recent_manager_add_item() or by another
323 g_signal_new (I_("changed"),
324 G_TYPE_FROM_CLASS (klass),
326 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
328 g_cclosure_marshal_VOID__VOID,
331 klass->changed = gtk_recent_manager_real_changed;
333 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
337 gtk_recent_manager_init (GtkRecentManager *manager)
339 GtkRecentManagerPrivate *priv;
341 manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
342 GTK_TYPE_RECENT_MANAGER,
343 GtkRecentManagerPrivate);
344 priv = manager->priv;
347 priv->filename = NULL;
351 gtk_recent_manager_set_property (GObject *object,
356 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
361 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 gtk_recent_manager_get_property (GObject *object,
375 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
380 g_value_set_string (value, recent_manager->priv->filename);
383 g_value_set_int (value, recent_manager->priv->size);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392 gtk_recent_manager_dispose (GObject *object)
394 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
395 GtkRecentManagerPrivate *priv = manager->priv;
399 g_signal_handlers_disconnect_by_func (priv->monitor,
400 G_CALLBACK (gtk_recent_manager_monitor_changed),
402 g_object_unref (priv->monitor);
403 priv->monitor = NULL;
406 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (object);
410 gtk_recent_manager_finalize (GObject *object)
412 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
413 GtkRecentManagerPrivate *priv = manager->priv;
415 g_free (priv->filename);
417 if (priv->recent_items)
418 g_bookmark_file_free (priv->recent_items);
420 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
424 gtk_recent_manager_real_changed (GtkRecentManager *manager)
426 GtkRecentManagerPrivate *priv = manager->priv;
428 g_object_freeze_notify (G_OBJECT (manager));
434 /* we are marked as dirty, so we dump the content of our
435 * recently used items list
437 g_assert (priv->filename != NULL);
439 if (!priv->recent_items)
441 /* if no container object has been defined, we create a new
442 * empty container, and dump it
444 priv->recent_items = g_bookmark_file_new ();
449 GtkSettings *settings = gtk_settings_get_default ();
452 g_object_get (G_OBJECT (settings), "gtk-recent-files-max-age", &age, NULL);
454 gtk_recent_manager_clamp_to_age (manager, age);
457 g_bookmark_file_free (priv->recent_items);
458 priv->recent_items = NULL;
460 priv->recent_items = g_bookmark_file_new ();
465 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
468 filename_warning ("Attempting to store changes into `%s', "
471 write_error->message);
472 g_error_free (write_error);
475 if (g_chmod (priv->filename, 0600) < 0)
477 filename_warning ("Attempting to set the permissions of `%s', "
483 /* mark us as clean */
484 priv->is_dirty = FALSE;
488 /* we are not marked as dirty, so we have been called
489 * because the recently used resources file has been
490 * changed (and not from us).
492 build_recent_items_list (manager);
495 g_object_thaw_notify (G_OBJECT (manager));
499 gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
502 GFileMonitorEvent event_type,
505 GtkRecentManager *manager = user_data;
509 case G_FILE_MONITOR_EVENT_CHANGED:
510 case G_FILE_MONITOR_EVENT_CREATED:
511 gtk_recent_manager_changed (manager);
514 case G_FILE_MONITOR_EVENT_DELETED:
523 gtk_recent_manager_set_filename (GtkRecentManager *manager,
524 const gchar *filename)
526 GtkRecentManagerPrivate *priv;
530 g_assert (GTK_IS_RECENT_MANAGER (manager));
532 priv = manager->priv;
534 /* if a filename is already set and filename is not NULL, then copy
535 * it and reset the monitor; otherwise, if it's NULL we're being
536 * called from the finalization sequence, so we simply disconnect the
537 * monitoring and return.
539 * if no filename is set and filename is NULL, use the default.
543 g_free (priv->filename);
547 g_signal_handlers_disconnect_by_func (priv->monitor,
548 G_CALLBACK (gtk_recent_manager_monitor_changed),
550 g_object_unref (priv->monitor);
551 priv->monitor = NULL;
554 if (!filename || *filename == '\0')
557 priv->filename = g_strdup (filename);
561 if (!filename || *filename == '\0')
562 priv->filename = g_build_filename (g_get_home_dir (),
563 GTK_RECENTLY_USED_FILE,
566 priv->filename = g_strdup (filename);
569 g_assert (priv->filename != NULL);
570 file = g_file_new_for_path (priv->filename);
573 priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
576 filename_warning ("Unable to monitor `%s': %s\n"
577 "The GtkRecentManager will not update its contents "
578 "if the file is changed from other instances",
581 g_error_free (error);
584 g_signal_connect (priv->monitor, "changed",
585 G_CALLBACK (gtk_recent_manager_monitor_changed),
588 g_object_unref (file);
590 priv->is_dirty = FALSE;
591 build_recent_items_list (manager);
594 /* reads the recently used resources file and builds the items list.
595 * we keep the items list inside the parser object, and build the
596 * RecentInfo object only on user's demand to avoid useless replication.
597 * this function resets the dirty bit of the manager.
600 build_recent_items_list (GtkRecentManager *manager)
602 GtkRecentManagerPrivate *priv = manager->priv;
606 g_assert (priv->filename != NULL);
608 if (!priv->recent_items)
610 priv->recent_items = g_bookmark_file_new ();
614 /* the file exists, and it's valid (we hope); if not, destroy the container
615 * object and hope for a better result when the next "changed" signal is
618 g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
621 /* if the file does not exist we just wait for the first write
622 * operation on this recent manager instance, to avoid creating
623 * empty files and leading to spurious file system events (Sabayon
624 * will not be happy about those)
626 if (read_error->domain == G_FILE_ERROR &&
627 read_error->code != G_FILE_ERROR_NOENT)
628 filename_warning ("Attempting to read the recently used resources "
629 "file at `%s', but the parser failed: %s.",
631 read_error->message);
633 g_bookmark_file_free (priv->recent_items);
634 priv->recent_items = NULL;
636 g_error_free (read_error);
640 size = g_bookmark_file_get_size (priv->recent_items);
641 if (priv->size != size)
645 g_object_notify (G_OBJECT (manager), "size");
649 priv->is_dirty = FALSE;
653 /********************
655 ********************/
659 * gtk_recent_manager_new:
661 * Creates a new recent manager object. Recent manager objects are used to
662 * handle the list of recently used resources. A #GtkRecentManager object
663 * monitors the recently used resources list, and emits the "changed" signal
664 * each time something inside the list changes.
666 * #GtkRecentManager objects are expensive: be sure to create them only when
667 * needed. You should use gtk_recent_manager_get_default() instead.
669 * Return value: A newly created #GtkRecentManager object.
674 gtk_recent_manager_new (void)
676 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
680 * gtk_recent_manager_get_default:
682 * Gets a unique instance of #GtkRecentManager, that you can share
683 * in your application without caring about memory management. The
684 * returned instance will be freed when you application terminates.
686 * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
691 gtk_recent_manager_get_default (void)
693 if (G_UNLIKELY (!recent_manager_singleton))
694 recent_manager_singleton = gtk_recent_manager_new ();
696 return recent_manager_singleton;
701 gtk_recent_manager_add_item_query_info (GObject *source_object,
705 GFile *file = G_FILE (source_object);
706 GtkRecentManager *manager = user_data;
707 GtkRecentData recent_data;
708 GFileInfo *file_info;
712 uri = g_file_get_uri (file);
715 file_info = g_file_query_info_finish (file, res, &error);
718 g_warning ("Unable to retrieve the file info for `%s': %s",
721 g_error_free (error);
725 recent_data.display_name = NULL;
726 recent_data.description = NULL;
732 content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
734 if (G_LIKELY (content_type))
735 recent_data.mime_type = g_content_type_get_mime_type (content_type);
737 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
739 g_free (content_type);
740 g_object_unref (file_info);
743 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
745 recent_data.app_name = g_strdup (g_get_application_name ());
746 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
747 recent_data.groups = NULL;
748 recent_data.is_private = FALSE;
750 /* Ignore return value, this can't fail anyway since all required
752 gtk_recent_manager_add_full (manager, uri, &recent_data);
754 manager->priv->is_dirty = TRUE;
755 gtk_recent_manager_changed (manager);
757 g_free (recent_data.mime_type);
758 g_free (recent_data.app_name);
759 g_free (recent_data.app_exec);
762 g_object_unref (manager);
767 * gtk_recent_manager_add_item:
768 * @manager: a #GtkRecentManager
771 * Adds a new resource, pointed by @uri, into the recently used
774 * This function automatically retrieves some of the needed
775 * metadata and setting other metadata to common default values; it
776 * then feeds the data to gtk_recent_manager_add_full().
778 * See gtk_recent_manager_add_full() if you want to explicitly
779 * define the metadata for the resource pointed by @uri.
781 * Return value: %TRUE if the new item was successfully added
782 * to the recently used resources list
787 gtk_recent_manager_add_item (GtkRecentManager *manager,
792 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
793 g_return_val_if_fail (uri != NULL, FALSE);
795 file = g_file_new_for_uri (uri);
797 g_file_query_info_async (file,
798 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
800 G_FILE_QUERY_INFO_NONE,
802 gtk_recent_manager_add_item_query_info,
803 g_object_ref (manager));
805 g_object_unref (file);
811 * gtk_recent_manager_add_full:
812 * @manager: a #GtkRecentManager
814 * @recent_data: metadata of the resource
816 * Adds a new resource, pointed by @uri, into the recently used
817 * resources list, using the metadata specified inside the #GtkRecentData
818 * structure passed in @recent_data.
820 * The passed URI will be used to identify this resource inside the
823 * In order to register the new recently used resource, metadata about
824 * the resource must be passed as well as the URI; the metadata is
825 * stored in a #GtkRecentData structure, which must contain the MIME
826 * type of the resource pointed by the URI; the name of the application
827 * that is registering the item, and a command line to be used when
828 * launching the item.
830 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
831 * to be used when viewing the item instead of the last component of the
832 * URI; a short description of the item; whether the item should be
833 * considered private - that is, should be displayed only by the
834 * applications that have registered it.
836 * Return value: %TRUE if the new item was successfully added to the
837 * recently used resources list, %FALSE otherwise.
842 gtk_recent_manager_add_full (GtkRecentManager *manager,
844 const GtkRecentData *data)
846 GtkRecentManagerPrivate *priv;
848 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
849 g_return_val_if_fail (uri != NULL, FALSE);
850 g_return_val_if_fail (data != NULL, FALSE);
853 if ((data->display_name) &&
854 (!g_utf8_validate (data->display_name, -1, NULL)))
856 g_warning ("Attempting to add `%s' to the list of recently used "
857 "resources, but the display name is not a valid UTF-8 "
863 if ((data->description) &&
864 (!g_utf8_validate (data->description, -1, NULL)))
866 g_warning ("Attempting to add `%s' to the list of recently used "
867 "resources, but the description is not a valid UTF-8 "
874 if (!data->mime_type)
876 g_warning ("Attempting to add `%s' to the list of recently used "
877 "resources, but not MIME type was defined",
884 g_warning ("Attempting to add `%s' to the list of recently used "
885 "resources, but no name of the application that is "
886 "registering it was defined",
893 g_warning ("Attempting to add `%s' to the list of recently used "
894 "resources, but no command line for the application "
895 "that is registering it was defined",
900 priv = manager->priv;
902 if (!priv->recent_items)
904 priv->recent_items = g_bookmark_file_new ();
908 if (data->display_name)
909 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
911 if (data->description)
912 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
914 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
916 if (data->groups && data->groups[0] != '\0')
920 for (j = 0; (data->groups)[j] != NULL; j++)
921 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
924 /* register the application; this will take care of updating the
925 * registration count and time in case the application has
926 * already registered the same document inside the list
928 g_bookmark_file_add_application (priv->recent_items, uri,
932 g_bookmark_file_set_is_private (priv->recent_items, uri,
935 /* mark us as dirty, so that when emitting the "changed" signal we
936 * will dump our changes
938 priv->is_dirty = TRUE;
940 gtk_recent_manager_changed (manager);
946 * gtk_recent_manager_remove_item:
947 * @manager: a #GtkRecentManager
948 * @uri: the URI of the item you wish to remove
949 * @error: (allow-none): return location for a #GError, or %NULL
951 * Removes a resource pointed by @uri from the recently used resources
952 * list handled by a recent manager.
954 * Return value: %TRUE if the item pointed by @uri has been successfully
955 * removed by the recently used resources list, and %FALSE otherwise.
960 gtk_recent_manager_remove_item (GtkRecentManager *manager,
964 GtkRecentManagerPrivate *priv;
965 GError *remove_error = NULL;
967 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
968 g_return_val_if_fail (uri != NULL, FALSE);
969 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
971 priv = manager->priv;
973 if (!priv->recent_items)
975 priv->recent_items = g_bookmark_file_new ();
978 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
979 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
980 _("Unable to find an item with URI '%s'"),
986 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
989 g_error_free (remove_error);
991 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
992 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
993 _("Unable to find an item with URI '%s'"),
999 priv->is_dirty = TRUE;
1001 gtk_recent_manager_changed (manager);
1007 * gtk_recent_manager_has_item:
1008 * @manager: a #GtkRecentManager
1011 * Checks whether there is a recently used resource registered
1012 * with @uri inside the recent manager.
1014 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1019 gtk_recent_manager_has_item (GtkRecentManager *manager,
1022 GtkRecentManagerPrivate *priv;
1024 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1025 g_return_val_if_fail (uri != NULL, FALSE);
1027 priv = manager->priv;
1028 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1030 return g_bookmark_file_has_item (priv->recent_items, uri);
1034 build_recent_info (GBookmarkFile *bookmarks,
1035 GtkRecentInfo *info)
1037 gchar **apps, **groups;
1038 gsize apps_len, groups_len, i;
1040 g_assert (bookmarks != NULL);
1041 g_assert (info != NULL);
1043 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1044 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1045 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1047 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1049 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1050 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1051 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1053 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1054 for (i = 0; i < groups_len; i++)
1056 gchar *group_name = g_strdup (groups[i]);
1058 info->groups = g_slist_append (info->groups, group_name);
1061 g_strfreev (groups);
1063 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1064 for (i = 0; i < apps_len; i++)
1066 gchar *app_name, *app_exec;
1069 RecentAppInfo *app_info;
1074 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1082 app_info = recent_app_info_new (app_name);
1083 app_info->exec = app_exec;
1084 app_info->count = count;
1085 app_info->stamp = stamp;
1087 info->applications = g_slist_prepend (info->applications, app_info);
1088 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1095 * gtk_recent_manager_lookup_item:
1096 * @manager: a #GtkRecentManager
1098 * @error: (allow-none): a return location for a #GError, or %NULL
1100 * Searches for a URI inside the recently used resources list, and
1101 * returns a structure containing informations about the resource
1102 * like its MIME type, or its display name.
1104 * Return value: a #GtkRecentInfo structure containing information
1105 * about the resource pointed by @uri, or %NULL if the URI was
1106 * not registered in the recently used resources list. Free with
1107 * gtk_recent_info_unref().
1112 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1116 GtkRecentManagerPrivate *priv;
1117 GtkRecentInfo *info = NULL;
1119 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1120 g_return_val_if_fail (uri != NULL, NULL);
1121 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1123 priv = manager->priv;
1124 if (!priv->recent_items)
1126 priv->recent_items = g_bookmark_file_new ();
1129 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1130 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1131 _("Unable to find an item with URI '%s'"),
1137 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1139 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1140 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1141 _("Unable to find an item with URI '%s'"),
1146 info = gtk_recent_info_new (uri);
1147 g_return_val_if_fail (info != NULL, NULL);
1149 /* fill the RecentInfo structure with the data retrieved by our
1150 * parser object from the storage file
1152 build_recent_info (priv->recent_items, info);
1158 * gtk_recent_manager_move_item:
1159 * @manager: a #GtkRecentManager
1160 * @uri: the URI of a recently used resource
1161 * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1162 * remove the item pointed by @uri in the list
1163 * @error: (allow-none): a return location for a #GError, or %NULL
1165 * Changes the location of a recently used resource from @uri to @new_uri.
1167 * Please note that this function will not affect the resource pointed
1168 * by the URIs, but only the URI used in the recently used resources list.
1170 * Return value: %TRUE on success.
1175 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1177 const gchar *new_uri,
1180 GtkRecentManagerPrivate *priv;
1184 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1185 g_return_val_if_fail (uri != NULL, FALSE);
1186 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1188 priv = recent_manager->priv;
1190 if (!priv->recent_items)
1192 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1193 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1194 _("Unable to find an item with URI '%s'"),
1199 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1201 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1202 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1203 _("Unable to find an item with URI '%s'"),
1209 res = g_bookmark_file_move_item (priv->recent_items,
1214 g_error_free (move_error);
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 priv->is_dirty = TRUE;
1225 gtk_recent_manager_changed (recent_manager);
1231 * gtk_recent_manager_get_items:
1232 * @manager: a #GtkRecentManager
1234 * Gets the list of recently used resources.
1236 * Return value: (element-type GtkRecentInfo) (transfer full): a list of
1237 * newly allocated #GtkRecentInfo objects. Use
1238 * gtk_recent_info_unref() on each item inside the list, and then
1239 * free the list itself using g_list_free().
1244 gtk_recent_manager_get_items (GtkRecentManager *manager)
1246 GtkRecentManagerPrivate *priv;
1247 GList *retval = NULL;
1251 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1253 priv = manager->priv;
1254 if (!priv->recent_items)
1257 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1258 for (i = 0; i < uris_len; i++)
1260 GtkRecentInfo *info;
1262 info = gtk_recent_info_new (uris[i]);
1263 build_recent_info (priv->recent_items, info);
1265 retval = g_list_prepend (retval, info);
1274 purge_recent_items_list (GtkRecentManager *manager,
1277 GtkRecentManagerPrivate *priv = manager->priv;
1279 if (!priv->recent_items)
1282 g_bookmark_file_free (priv->recent_items);
1283 priv->recent_items = NULL;
1285 priv->recent_items = g_bookmark_file_new ();
1287 priv->is_dirty = TRUE;
1289 /* emit the changed signal, to ensure that the purge is written */
1290 gtk_recent_manager_changed (manager);
1294 * gtk_recent_manager_purge_items:
1295 * @manager: a #GtkRecentManager
1296 * @error: (allow-none): a return location for a #GError, or %NULL
1298 * Purges every item from the recently used resources list.
1300 * Return value: the number of items that have been removed from the
1301 * recently used resources list.
1306 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1309 GtkRecentManagerPrivate *priv;
1312 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1314 priv = manager->priv;
1315 if (!priv->recent_items)
1318 count = g_bookmark_file_get_size (priv->recent_items);
1322 purge_recent_items_list (manager, error);
1324 purged = count - g_bookmark_file_get_size (priv->recent_items);
1330 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1332 g_signal_emit (recent_manager, signal_changed, 0);
1336 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1339 GtkRecentManagerPrivate *priv = manager->priv;
1344 if (G_UNLIKELY (!priv->recent_items))
1349 uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1351 for (i = 0; i < n_uris; i++)
1353 const gchar *uri = uris[i];
1357 modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1358 item_age = (gint) ((now - modified) / (60 * 60 * 24));
1360 g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1370 G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
1371 gtk_recent_info_ref,
1372 gtk_recent_info_unref)
1374 static GtkRecentInfo *
1375 gtk_recent_info_new (const gchar *uri)
1377 GtkRecentInfo *info;
1379 g_assert (uri != NULL);
1381 info = g_new0 (GtkRecentInfo, 1);
1382 info->uri = g_strdup (uri);
1384 info->applications = NULL;
1385 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1387 info->groups = NULL;
1389 info->ref_count = 1;
1395 gtk_recent_info_free (GtkRecentInfo *recent_info)
1400 g_free (recent_info->uri);
1401 g_free (recent_info->display_name);
1402 g_free (recent_info->description);
1403 g_free (recent_info->mime_type);
1405 if (recent_info->applications)
1407 g_slist_foreach (recent_info->applications,
1408 (GFunc) recent_app_info_free,
1410 g_slist_free (recent_info->applications);
1412 recent_info->applications = NULL;
1415 if (recent_info->apps_lookup)
1416 g_hash_table_destroy (recent_info->apps_lookup);
1418 if (recent_info->groups)
1420 g_slist_foreach (recent_info->groups,
1423 g_slist_free (recent_info->groups);
1425 recent_info->groups = NULL;
1428 if (recent_info->icon)
1429 g_object_unref (recent_info->icon);
1431 g_free (recent_info);
1435 * gtk_recent_info_ref:
1436 * @info: a #GtkRecentInfo
1438 * Increases the reference count of @recent_info by one.
1440 * Return value: the recent info object with its reference count increased
1446 gtk_recent_info_ref (GtkRecentInfo *info)
1448 g_return_val_if_fail (info != NULL, NULL);
1449 g_return_val_if_fail (info->ref_count > 0, NULL);
1451 info->ref_count += 1;
1457 * gtk_recent_info_unref:
1458 * @info: a #GtkRecentInfo
1460 * Decreases the reference count of @info by one. If the reference
1461 * count reaches zero, @info is deallocated, and the memory freed.
1466 gtk_recent_info_unref (GtkRecentInfo *info)
1468 g_return_if_fail (info != NULL);
1469 g_return_if_fail (info->ref_count > 0);
1471 info->ref_count -= 1;
1473 if (info->ref_count == 0)
1474 gtk_recent_info_free (info);
1478 * gtk_recent_info_get_uri:
1479 * @info: a #GtkRecentInfo
1481 * Gets the URI of the resource.
1483 * Return value: the URI of the resource. The returned string is
1484 * owned by the recent manager, and should not be freed.
1488 G_CONST_RETURN gchar *
1489 gtk_recent_info_get_uri (GtkRecentInfo *info)
1491 g_return_val_if_fail (info != NULL, NULL);
1497 * gtk_recent_info_get_display_name:
1498 * @info: a #GtkRecentInfo
1500 * Gets the name of the resource. If none has been defined, the basename
1501 * of the resource is obtained.
1503 * Return value: the display name of the resource. The returned string
1504 * is owned by the recent manager, and should not be freed.
1508 G_CONST_RETURN gchar *
1509 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1511 g_return_val_if_fail (info != NULL, NULL);
1513 if (!info->display_name)
1514 info->display_name = gtk_recent_info_get_short_name (info);
1516 return info->display_name;
1520 * gtk_recent_info_get_description:
1521 * @info: a #GtkRecentInfo
1523 * Gets the (short) description of the resource.
1525 * Return value: the description of the resource. The returned string
1526 * is owned by the recent manager, and should not be freed.
1530 G_CONST_RETURN gchar *
1531 gtk_recent_info_get_description (GtkRecentInfo *info)
1533 g_return_val_if_fail (info != NULL, NULL);
1535 return info->description;
1539 * gtk_recent_info_get_mime_type:
1540 * @info: a #GtkRecentInfo
1542 * Gets the MIME type of the resource.
1544 * Return value: the MIME type of the resource. The returned string
1545 * is owned by the recent manager, and should not be freed.
1549 G_CONST_RETURN gchar *
1550 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1552 g_return_val_if_fail (info != NULL, NULL);
1554 if (!info->mime_type)
1555 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1557 return info->mime_type;
1561 * gtk_recent_info_get_added:
1562 * @info: a #GtkRecentInfo
1564 * Gets the timestamp (seconds from system's Epoch) when the resource
1565 * was added to the recently used resources list.
1567 * Return value: the number of seconds elapsed from system's Epoch when
1568 * the resource was added to the list, or -1 on failure.
1573 gtk_recent_info_get_added (GtkRecentInfo *info)
1575 g_return_val_if_fail (info != NULL, (time_t) -1);
1581 * gtk_recent_info_get_modified:
1582 * @info: a #GtkRecentInfo
1584 * Gets the timestamp (seconds from system's Epoch) when the resource
1585 * was last modified.
1587 * Return value: the number of seconds elapsed from system's Epoch when
1588 * the resource was last modified, or -1 on failure.
1593 gtk_recent_info_get_modified (GtkRecentInfo *info)
1595 g_return_val_if_fail (info != NULL, (time_t) -1);
1597 return info->modified;
1601 * gtk_recent_info_get_visited:
1602 * @info: a #GtkRecentInfo
1604 * Gets the timestamp (seconds from system's Epoch) when the resource
1607 * Return value: the number of seconds elapsed from system's Epoch when
1608 * the resource was last visited, or -1 on failure.
1613 gtk_recent_info_get_visited (GtkRecentInfo *info)
1615 g_return_val_if_fail (info != NULL, (time_t) -1);
1617 return info->visited;
1621 * gtk_recent_info_get_private_hint:
1622 * @info: a #GtkRecentInfo
1624 * Gets the value of the "private" flag. Resources in the recently used
1625 * list that have this flag set to %TRUE should only be displayed by the
1626 * applications that have registered them.
1628 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1633 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1635 g_return_val_if_fail (info != NULL, FALSE);
1637 return info->is_private;
1641 static RecentAppInfo *
1642 recent_app_info_new (const gchar *app_name)
1644 RecentAppInfo *app_info;
1646 g_assert (app_name != NULL);
1648 app_info = g_slice_new0 (RecentAppInfo);
1649 app_info->name = g_strdup (app_name);
1650 app_info->exec = NULL;
1651 app_info->count = 1;
1652 app_info->stamp = 0;
1658 recent_app_info_free (RecentAppInfo *app_info)
1663 g_free (app_info->name);
1664 g_free (app_info->exec);
1666 g_slice_free (RecentAppInfo, app_info);
1670 * gtk_recent_info_get_application_info:
1671 * @info: a #GtkRecentInfo
1672 * @app_name: the name of the application that has registered this item
1673 * @app_exec: (transfer none) (out): return location for the string containing the command line
1674 * @count: (out): return location for the number of times this item was registered
1675 * @time_: (out): return location for the timestamp this item was last registered
1676 * for this application
1678 * Gets the data regarding the application that has registered the resource
1681 * If the command line contains any escape characters defined inside the
1682 * storage specification, they will be expanded.
1684 * Return value: %TRUE if an application with @app_name has registered this
1685 * resource inside the recently used list, or %FALSE otherwise. The
1686 * @app_exec string is owned by the #GtkRecentInfo and should not be
1692 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1693 const gchar *app_name,
1694 const gchar **app_exec,
1700 g_return_val_if_fail (info != NULL, FALSE);
1701 g_return_val_if_fail (app_name != NULL, FALSE);
1703 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1707 g_warning ("No registered application with name '%s' "
1708 "for item with URI '%s' found",
1715 *app_exec = ai->exec;
1727 * gtk_recent_info_get_applications:
1728 * @info: a #GtkRecentInfo
1729 * @length: (out) (allow-none): return location for the length of the returned list
1731 * Retrieves the list of applications that have registered this resource.
1733 * Return value: (array length=length zero-terminated=1) (transfer full):
1734 * a newly allocated %NULL-terminated array of strings.
1735 * Use g_strfreev() to free it.
1740 gtk_recent_info_get_applications (GtkRecentInfo *info,
1747 g_return_val_if_fail (info != NULL, NULL);
1749 if (!info->applications)
1757 n_apps = g_slist_length (info->applications);
1759 retval = g_new0 (gchar *, n_apps + 1);
1761 for (l = info->applications, i = 0;
1765 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1767 g_assert (ai != NULL);
1769 retval[i++] = g_strdup (ai->name);
1780 * gtk_recent_info_has_application:
1781 * @info: a #GtkRecentInfo
1782 * @app_name: a string containing an application name
1784 * Checks whether an application registered this resource using @app_name.
1786 * Return value: %TRUE if an application with name @app_name was found,
1792 gtk_recent_info_has_application (GtkRecentInfo *info,
1793 const gchar *app_name)
1795 g_return_val_if_fail (info != NULL, FALSE);
1796 g_return_val_if_fail (app_name != NULL, FALSE);
1798 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1802 * gtk_recent_info_last_application:
1803 * @info: a #GtkRecentInfo
1805 * Gets the name of the last application that have registered the
1806 * recently used resource represented by @info.
1808 * Return value: an application name. Use g_free() to free it.
1813 gtk_recent_info_last_application (GtkRecentInfo *info)
1816 time_t last_stamp = (time_t) -1;
1819 g_return_val_if_fail (info != NULL, NULL);
1821 for (l = info->applications; l != NULL; l = l->next)
1823 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1825 if (ai->stamp > last_stamp)
1828 last_stamp = ai->stamp;
1832 return g_strdup (name);
1836 get_icon_for_mime_type (const char *mime_type,
1839 GtkIconTheme *icon_theme;
1845 icon_theme = gtk_icon_theme_get_default ();
1847 content_type = g_content_type_from_mime_type (mime_type);
1852 icon = g_content_type_get_icon (content_type);
1853 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1856 GTK_ICON_LOOKUP_USE_BUILTIN);
1857 g_free (content_type);
1858 g_object_unref (icon);
1863 pixbuf = gtk_icon_info_load_icon (info, NULL);
1864 gtk_icon_info_free (info);
1870 get_icon_fallback (const gchar *icon_name,
1873 GtkIconTheme *icon_theme;
1876 icon_theme = gtk_icon_theme_get_default ();
1878 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1880 GTK_ICON_LOOKUP_USE_BUILTIN,
1882 g_assert (retval != NULL);
1888 * gtk_recent_info_get_icon:
1889 * @info: a #GtkRecentInfo
1890 * @size: the size of the icon in pixels
1892 * Retrieves the icon of size @size associated to the resource MIME type.
1894 * Return value: (transfer full): a #GdkPixbuf containing the icon,
1895 * or %NULL. Use g_object_unref() when finished using the icon.
1900 gtk_recent_info_get_icon (GtkRecentInfo *info,
1903 GdkPixbuf *retval = NULL;
1905 g_return_val_if_fail (info != NULL, NULL);
1907 if (info->mime_type)
1908 retval = get_icon_for_mime_type (info->mime_type, size);
1910 /* this function should never fail */
1913 if (info->mime_type &&
1914 strcmp (info->mime_type, "x-directory/normal") == 0)
1915 retval = get_icon_fallback (GTK_STOCK_DIRECTORY, size);
1917 retval = get_icon_fallback (GTK_STOCK_FILE, size);
1924 * gtk_recent_info_is_local:
1925 * @info: a #GtkRecentInfo
1927 * Checks whether the resource is local or not by looking at the
1928 * scheme of its URI.
1930 * Return value: %TRUE if the resource is local.
1935 gtk_recent_info_is_local (GtkRecentInfo *info)
1937 g_return_val_if_fail (info != NULL, FALSE);
1939 return has_case_prefix (info->uri, "file:/");
1943 * gtk_recent_info_exists:
1944 * @info: a #GtkRecentInfo
1946 * Checks whether the resource pointed by @info still exists. At
1947 * the moment this check is done only on resources pointing to local files.
1949 * Return value: %TRUE if the resource exists
1954 gtk_recent_info_exists (GtkRecentInfo *info)
1957 struct stat stat_buf;
1958 gboolean retval = FALSE;
1960 g_return_val_if_fail (info != NULL, FALSE);
1962 /* we guarantee only local resources */
1963 if (!gtk_recent_info_is_local (info))
1966 filename = g_filename_from_uri (info->uri, NULL, NULL);
1969 if (stat (filename, &stat_buf) == 0)
1979 * gtk_recent_info_match:
1980 * @info_a: a #GtkRecentInfo
1981 * @info_b: a #GtkRecentInfo
1983 * Checks whether two #GtkRecentInfo structures point to the same
1986 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
1987 * resource, %FALSE otherwise.
1992 gtk_recent_info_match (GtkRecentInfo *info_a,
1993 GtkRecentInfo *info_b)
1995 g_return_val_if_fail (info_a != NULL, FALSE);
1996 g_return_val_if_fail (info_b != NULL, FALSE);
1998 return (0 == strcmp (info_a->uri, info_b->uri));
2001 /* taken from gnome-vfs-uri.c */
2002 static const gchar *
2003 get_method_string (const gchar *substring,
2004 gchar **method_string)
2010 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2017 !(p == substring + 1 && g_ascii_isalpha (*substring))
2021 /* Found toplevel method specification. */
2022 method = g_strndup (substring, p - substring);
2023 *method_string = g_ascii_strdown (method, -1);
2029 *method_string = g_strdup ("file");
2036 /* Stolen from gnome_vfs_make_valid_utf8() */
2038 make_valid_utf8 (const char *name)
2041 const char *remainder, *invalid;
2042 int remaining_bytes, valid_bytes;
2046 remaining_bytes = name ? strlen (name) : 0;
2048 while (remaining_bytes != 0)
2050 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2053 valid_bytes = invalid - remainder;
2056 string = g_string_sized_new (remaining_bytes);
2058 g_string_append_len (string, remainder, valid_bytes);
2059 g_string_append_c (string, '?');
2061 remaining_bytes -= valid_bytes + 1;
2062 remainder = invalid + 1;
2066 return g_strdup (name);
2068 g_string_append (string, remainder);
2069 g_assert (g_utf8_validate (string->str, -1, NULL));
2071 return g_string_free (string, FALSE);
2075 get_uri_shortname_for_display (const gchar *uri)
2078 gboolean validated = FALSE;
2080 if (has_case_prefix (uri, "file:/"))
2084 local_file = g_filename_from_uri (uri, NULL, NULL);
2088 name = g_filename_display_basename (local_file);
2092 g_free (local_file);
2101 rest = get_method_string (uri, &method);
2102 local_file = g_filename_display_basename (rest);
2104 name = g_strconcat (method, ": ", local_file, NULL);
2106 g_free (local_file);
2110 g_assert (name != NULL);
2112 if (!validated && !g_utf8_validate (name, -1, NULL))
2116 utf8_name = make_valid_utf8 (name);
2126 * gtk_recent_info_get_short_name:
2127 * @info: an #GtkRecentInfo
2129 * Computes a valid UTF-8 string that can be used as the name of the item in a
2130 * menu or list. For example, calling this function on an item that refers to
2131 * "file:///foo/bar.txt" will yield "bar.txt".
2133 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2139 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2143 g_return_val_if_fail (info != NULL, NULL);
2145 if (info->uri == NULL)
2148 short_name = get_uri_shortname_for_display (info->uri);
2154 * gtk_recent_info_get_uri_display:
2155 * @info: a #GtkRecentInfo
2157 * Gets a displayable version of the resource's URI. If the resource
2158 * is local, it returns a local path; if the resource is not local,
2159 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2161 * Return value: a newly allocated UTF-8 string containing the
2162 * resource's URI or %NULL. Use g_free() when done using it.
2167 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2171 g_return_val_if_fail (info != NULL, NULL);
2174 if (gtk_recent_info_is_local (info))
2178 filename = g_filename_from_uri (info->uri, NULL, NULL);
2182 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2187 retval = make_valid_utf8 (info->uri);
2194 * gtk_recent_info_get_age:
2195 * @info: a #GtkRecentInfo
2197 * Gets the number of days elapsed since the last update of the resource
2200 * Return value: a positive integer containing the number of days elapsed
2201 * since the time this resource was last modified.
2206 gtk_recent_info_get_age (GtkRecentInfo *info)
2211 g_return_val_if_fail (info != NULL, -1);
2215 delta = now - info->modified;
2217 retval = (gint) (delta / (60 * 60 * 24));
2223 * gtk_recent_info_get_groups:
2224 * @info: a #GtkRecentInfo
2225 * @length: (out) (allow-none): return location for the number of groups returned
2227 * Returns all groups registered for the recently used item @info. The
2228 * array of returned group names will be %NULL terminated, so length might
2229 * optionally be %NULL.
2231 * Return value: (array length=length zero-terminated=1) (transfer full):
2232 * a newly allocated %NULL terminated array of strings.
2233 * Use g_strfreev() to free it.
2238 gtk_recent_info_get_groups (GtkRecentInfo *info,
2245 g_return_val_if_fail (info != NULL, NULL);
2255 n_groups = g_slist_length (info->groups);
2257 retval = g_new0 (gchar *, n_groups + 1);
2259 for (l = info->groups, i = 0;
2263 gchar *group_name = (gchar *) l->data;
2265 g_assert (group_name != NULL);
2267 retval[i++] = g_strdup (group_name);
2278 * gtk_recent_info_has_group:
2279 * @info: a #GtkRecentInfo
2280 * @group_name: name of a group
2282 * Checks whether @group_name appears inside the groups registered for the
2283 * recently used item @info.
2285 * Return value: %TRUE if the group was found.
2290 gtk_recent_info_has_group (GtkRecentInfo *info,
2291 const gchar *group_name)
2295 g_return_val_if_fail (info != NULL, FALSE);
2296 g_return_val_if_fail (group_name != NULL, FALSE);
2301 for (l = info->groups; l != NULL; l = l->next)
2303 gchar *g = (gchar *) l->data;
2305 if (strcmp (g, group_name) == 0)
2313 * _gtk_recent_manager_sync:
2315 * Private function for synchronising the recent manager singleton.
2318 _gtk_recent_manager_sync (void)
2320 if (recent_manager_singleton)
2322 /* force a dump of the contents of the recent manager singleton */
2323 recent_manager_singleton->priv->is_dirty = TRUE;
2324 gtk_recent_manager_real_changed (recent_manager_singleton);