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,
23 #include <sys/types.h>
32 #include <glib/gstdio.h>
35 #include "gtkrecentmanager.h"
38 #include "gtkicontheme.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkprivate.h"
41 #include "gtkmarshalers.h"
45 #define XDG_PREFIX _gtk_xdg
46 #include "xdgmime/xdgmime.h"
49 /* the file where we store the recently used items */
50 #define GTK_RECENTLY_USED_FILE ".recently-used.xbel"
52 /* return all items by default */
53 #define DEFAULT_LIMIT -1
55 /* keep in sync with xdgmime */
56 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
82 GHashTable *apps_lookup;
93 #define GTK_RECENT_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RECENT_MANAGER, GtkRecentManagerPrivate))
95 struct _GtkRecentManagerPrivate
104 GBookmarkFile *recent_items;
106 GFileMonitor *monitor;
118 static void gtk_recent_manager_dispose (GObject *object);
119 static void gtk_recent_manager_finalize (GObject *object);
120 static void gtk_recent_manager_set_property (GObject *object,
124 static void gtk_recent_manager_get_property (GObject *object,
128 static void gtk_recent_manager_add_item_query_info (GObject *source_object,
131 static void gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
134 GFileMonitorEvent event_type,
136 static void gtk_recent_manager_changed (GtkRecentManager *manager);
137 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
138 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
139 const gchar *filename);
140 static void gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
144 static void build_recent_items_list (GtkRecentManager *manager);
145 static void purge_recent_items_list (GtkRecentManager *manager,
148 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
149 static void recent_app_info_free (RecentAppInfo *app_info);
151 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
152 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
154 static guint signal_changed = 0;
156 static GtkRecentManager *recent_manager_singleton = NULL;
158 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
161 filename_warning (const gchar *format,
162 const gchar *filename,
163 const gchar *message)
165 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
166 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
170 /* Test of haystack has the needle prefix, comparing case
171 * insensitive. haystack may be UTF-8, but needle must
172 * contain only lowercase ascii. */
174 has_case_prefix (const gchar *haystack,
179 /* Eat one character at a time. */
183 while (*n && *h && *n == g_ascii_tolower (*h))
193 gtk_recent_manager_error_quark (void)
195 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
199 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
201 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
203 gobject_class->set_property = gtk_recent_manager_set_property;
204 gobject_class->get_property = gtk_recent_manager_get_property;
205 gobject_class->dispose = gtk_recent_manager_dispose;
206 gobject_class->finalize = gtk_recent_manager_finalize;
209 * GtkRecentManager:filename
211 * The full path to the file to be used to store and read the recently
212 * used resources list
216 g_object_class_install_property (gobject_class,
218 g_param_spec_string ("filename",
220 P_("The full path to the file to be used to store and read the list"),
222 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
224 * GtkRecentManager:limit
226 * The maximum number of items to be returned by the
227 * gtk_recent_manager_get_items() function.
231 g_object_class_install_property (gobject_class,
233 g_param_spec_int ("limit",
235 P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
241 * GtkRecentManager:size
243 * The size of the recently used resources list.
247 g_object_class_install_property (gobject_class,
249 g_param_spec_int ("size",
251 P_("The size of the recently used resources list"),
258 * GtkRecentManager::changed
259 * @recent_manager: the recent manager
261 * Emitted when the current recently used resources manager changes its
267 g_signal_new (I_("changed"),
268 G_TYPE_FROM_CLASS (klass),
270 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
272 g_cclosure_marshal_VOID__VOID,
275 klass->changed = gtk_recent_manager_real_changed;
277 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
281 gtk_recent_manager_init (GtkRecentManager *manager)
283 GtkRecentManagerPrivate *priv;
285 manager->priv = priv = GTK_RECENT_MANAGER_GET_PRIVATE (manager);
287 priv->limit = DEFAULT_LIMIT;
290 priv->filename = NULL;
294 gtk_recent_manager_set_property (GObject *object,
299 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
304 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
307 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gtk_recent_manager_get_property (GObject *object,
321 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
326 g_value_set_string (value, recent_manager->priv->filename);
329 g_value_set_int (value, recent_manager->priv->limit);
332 g_value_set_int (value, recent_manager->priv->size);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gtk_recent_manager_dispose (GObject *object)
343 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
344 GtkRecentManagerPrivate *priv = manager->priv;
348 g_signal_handlers_disconnect_by_func (priv->monitor,
349 G_CALLBACK (gtk_recent_manager_monitor_changed),
351 g_object_unref (priv->monitor);
352 priv->monitor = NULL;
355 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (object);
359 gtk_recent_manager_finalize (GObject *object)
361 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
362 GtkRecentManagerPrivate *priv = manager->priv;
364 g_free (priv->filename);
366 if (priv->recent_items)
367 g_bookmark_file_free (priv->recent_items);
369 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
373 gtk_recent_manager_real_changed (GtkRecentManager *manager)
375 GtkRecentManagerPrivate *priv = manager->priv;
377 g_object_freeze_notify (G_OBJECT (manager));
383 /* we are marked as dirty, so we dump the content of our
384 * recently used items list
386 g_assert (priv->filename != NULL);
388 if (!priv->recent_items)
390 /* if no container object has been defined, we create a new
391 * empty container, and dump it
393 priv->recent_items = g_bookmark_file_new ();
398 GtkSettings *settings = gtk_settings_get_default ();
401 g_object_get (G_OBJECT (settings), "gtk-recent-files-max-age", &age, NULL);
403 gtk_recent_manager_clamp_to_age (manager, age);
406 g_bookmark_file_free (priv->recent_items);
407 priv->recent_items = NULL;
409 priv->recent_items = g_bookmark_file_new ();
414 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
417 filename_warning ("Attempting to store changes into `%s', "
420 write_error->message);
421 g_error_free (write_error);
424 /* mark us as clean */
425 priv->is_dirty = FALSE;
429 /* we are not marked as dirty, so we have been called
430 * because the recently used resources file has been
431 * changed (and not from us).
433 build_recent_items_list (manager);
436 g_object_thaw_notify (G_OBJECT (manager));
440 gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
443 GFileMonitorEvent event_type,
446 GtkRecentManager *manager = user_data;
450 case G_FILE_MONITOR_EVENT_CHANGED:
451 case G_FILE_MONITOR_EVENT_CREATED:
452 gtk_recent_manager_changed (manager);
455 case G_FILE_MONITOR_EVENT_DELETED:
464 gtk_recent_manager_set_filename (GtkRecentManager *manager,
465 const gchar *filename)
467 GtkRecentManagerPrivate *priv;
471 g_assert (GTK_IS_RECENT_MANAGER (manager));
473 priv = manager->priv;
475 /* if a filename is already set and filename is not NULL, then copy
476 * it and reset the monitor; otherwise, if it's NULL we're being
477 * called from the finalization sequence, so we simply disconnect the
478 * monitoring and return.
480 * if no filename is set and filename is NULL, use the default.
484 g_free (priv->filename);
488 g_signal_handlers_disconnect_by_func (priv->monitor,
489 G_CALLBACK (gtk_recent_manager_monitor_changed),
491 g_object_unref (priv->monitor);
492 priv->monitor = NULL;
495 if (!filename || *filename == '\0')
498 priv->filename = g_strdup (filename);
502 if (!filename || *filename == '\0')
503 priv->filename = g_build_filename (g_get_user_data_dir (),
504 GTK_RECENTLY_USED_FILE,
507 priv->filename = g_strdup (filename);
510 g_assert (priv->filename != NULL);
511 file = g_file_new_for_path (priv->filename);
514 priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
517 filename_warning ("Unable to monitor `%s': %s\n"
518 "The GtkRecentManager will not update its contents "
519 "if the file is changed from other instances",
522 g_error_free (error);
525 g_signal_connect (priv->monitor, "changed",
526 G_CALLBACK (gtk_recent_manager_monitor_changed),
529 g_object_unref (file);
531 priv->is_dirty = FALSE;
532 build_recent_items_list (manager);
535 /* reads the recently used resources file and builds the items list.
536 * we keep the items list inside the parser object, and build the
537 * RecentInfo object only on user's demand to avoid useless replication.
538 * this function resets the dirty bit of the manager.
541 build_recent_items_list (GtkRecentManager *manager)
543 GtkRecentManagerPrivate *priv = manager->priv;
547 g_assert (priv->filename != NULL);
549 if (!priv->recent_items)
551 priv->recent_items = g_bookmark_file_new ();
555 /* the file exists, and it's valid (we hope); if not, destroy the container
556 * object and hope for a better result when the next "changed" signal is
559 g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
562 /* if the file does not exist we just wait for the first write
563 * operation on this recent manager instance, to avoid creating
564 * empty files and leading to spurious file system events (Sabayon
565 * will not be happy about those)
567 if (read_error->domain == G_FILE_ERROR &&
568 read_error->code != G_FILE_ERROR_NOENT)
569 filename_warning ("Attempting to read the recently used resources "
570 "file at `%s', but the parser failed: %s.",
572 read_error->message);
574 g_bookmark_file_free (priv->recent_items);
575 priv->recent_items = NULL;
577 g_error_free (read_error);
581 size = g_bookmark_file_get_size (priv->recent_items);
582 if (priv->size != size)
586 g_object_notify (G_OBJECT (manager), "size");
590 priv->is_dirty = FALSE;
594 /********************
596 ********************/
600 * gtk_recent_manager_new:
602 * Creates a new recent manager object. Recent manager objects are used to
603 * handle the list of recently used resources. A #GtkRecentManager object
604 * monitors the recently used resources list, and emits the "changed" signal
605 * each time something inside the list changes.
607 * #GtkRecentManager objects are expensive: be sure to create them only when
608 * needed. You should use gtk_recent_manager_get_default() instead.
610 * Return value: A newly created #GtkRecentManager object.
615 gtk_recent_manager_new (void)
617 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
621 * gtk_recent_manager_get_default:
623 * Gets a unique instance of #GtkRecentManager, that you can share
624 * in your application without caring about memory management. The
625 * returned instance will be freed when you application terminates.
627 * Return value: A unique #GtkRecentManager. Do not ref or unref it.
632 gtk_recent_manager_get_default (void)
634 if (G_UNLIKELY (!recent_manager_singleton))
635 recent_manager_singleton = gtk_recent_manager_new ();
637 return recent_manager_singleton;
641 * gtk_recent_manager_get_for_screen:
642 * @screen: a #GdkScreen
644 * Gets the recent manager object associated with @screen; if this
645 * function has not previously been called for the given screen,
646 * a new recent manager object will be created and associated with
647 * the screen. Recent manager objects are fairly expensive to create,
648 * so using this function is usually a better choice than calling
649 * gtk_recent_manager_new() and setting the screen yourself; by using
650 * this function a single recent manager object will be shared between
653 * Return value: A unique #GtkRecentManager associated with the given
654 * screen. This recent manager is associated to the with the screen
655 * and can be used as long as the screen is open. Do not ref or
658 * Deprecated: 2.12: This function has been deprecated and should
659 * not be used in newly written code. Calling this function is
660 * equivalent to calling gtk_recent_manager_get_default().
665 gtk_recent_manager_get_for_screen (GdkScreen *screen)
667 return gtk_recent_manager_get_default ();
671 * gtk_recent_manager_set_screen:
672 * @manager: a #GtkRecentManager
673 * @screen: a #GdkScreen
675 * Sets the screen for a recent manager; the screen is used to
676 * track the user's currently configured recently used documents
681 * Deprecated: 2.12: This function has been deprecated and should
682 * not be used in newly written code. Calling this function has
686 gtk_recent_manager_set_screen (GtkRecentManager *manager,
693 * gtk_recent_manager_set_limit:
694 * @manager: a #GtkRecentManager
695 * @limit: the maximum number of items to return, or -1.
697 * Sets the maximum number of item that the gtk_recent_manager_get_items()
698 * function should return. If @limit is set to -1, then return all the
704 gtk_recent_manager_set_limit (GtkRecentManager *manager,
707 GtkRecentManagerPrivate *priv;
709 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
711 priv = manager->priv;
716 * gtk_recent_manager_get_limit:
717 * @manager: a #GtkRecentManager
719 * Gets the maximum number of items that the gtk_recent_manager_get_items()
720 * function should return.
722 * Return value: the number of items to return, or -1 for every item.
727 gtk_recent_manager_get_limit (GtkRecentManager *manager)
729 GtkRecentManagerPrivate *priv;
731 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
733 priv = manager->priv;
738 gtk_recent_manager_add_item_query_info (GObject *source_object,
742 GFile *file = G_FILE (source_object);
743 GtkRecentManager *manager = user_data;
744 GtkRecentData recent_data;
745 GFileInfo *file_info;
749 uri = g_file_get_uri (file);
752 file_info = g_file_query_info_finish (file, res, &error);
755 g_warning ("Unable to retrieve the file info for `%s': %s",
758 g_error_free (error);
762 recent_data.display_name = NULL;
763 recent_data.description = NULL;
767 recent_data.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
768 g_object_unref (file_info);
771 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
773 recent_data.app_name = g_strdup (g_get_application_name ());
774 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
775 recent_data.groups = NULL;
776 recent_data.is_private = FALSE;
778 /* Ignore return value, this can't fail anyway since all required
780 gtk_recent_manager_add_full (manager, uri, &recent_data);
782 manager->priv->is_dirty = TRUE;
783 gtk_recent_manager_changed (manager);
785 g_free (recent_data.mime_type);
786 g_free (recent_data.app_name);
787 g_free (recent_data.app_exec);
790 g_object_unref (manager);
795 * gtk_recent_manager_add_item:
796 * @manager: a #GtkRecentManager
799 * Adds a new resource, pointed by @uri, into the recently used
802 * This function automatically retrieves some of the needed
803 * metadata and setting other metadata to common default values; it
804 * then feeds the data to gtk_recent_manager_add_full().
806 * See gtk_recent_manager_add_full() if you want to explicitly
807 * define the metadata for the resource pointed by @uri.
809 * Return value: %TRUE if the new item was successfully added
810 * to the recently used resources list
815 gtk_recent_manager_add_item (GtkRecentManager *manager,
820 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
821 g_return_val_if_fail (uri != NULL, FALSE);
823 file = g_file_new_for_uri (uri);
825 g_file_query_info_async (file,
826 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
828 G_FILE_QUERY_INFO_NONE,
830 gtk_recent_manager_add_item_query_info,
831 g_object_ref (manager));
833 g_object_unref (file);
839 * gtk_recent_manager_add_full:
840 * @manager: a #GtkRecentManager
842 * @recent_data: metadata of the resource
844 * Adds a new resource, pointed by @uri, into the recently used
845 * resources list, using the metadata specified inside the #GtkRecentData
846 * structure passed in @recent_data.
848 * The passed URI will be used to identify this resource inside the
851 * In order to register the new recently used resource, metadata about
852 * the resource must be passed as well as the URI; the metadata is
853 * stored in a #GtkRecentData structure, which must contain the MIME
854 * type of the resource pointed by the URI; the name of the application
855 * that is registering the item, and a command line to be used when
856 * launching the item.
858 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
859 * to be used when viewing the item instead of the last component of the
860 * URI; a short description of the item; whether the item should be
861 * considered private - that is, should be displayed only by the
862 * applications that have registered it.
864 * Return value: %TRUE if the new item was successfully added to the
865 * recently used resources list, %FALSE otherwise.
870 gtk_recent_manager_add_full (GtkRecentManager *manager,
872 const GtkRecentData *data)
874 GtkRecentManagerPrivate *priv;
876 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
877 g_return_val_if_fail (uri != NULL, FALSE);
878 g_return_val_if_fail (data != NULL, FALSE);
881 if ((data->display_name) &&
882 (!g_utf8_validate (data->display_name, -1, NULL)))
884 g_warning ("Attempting to add `%s' to the list of recently used "
885 "resources, but the display name is not a valid UTF-8 "
891 if ((data->description) &&
892 (!g_utf8_validate (data->description, -1, NULL)))
894 g_warning ("Attempting to add `%s' to the list of recently used "
895 "resources, but the description is not a valid UTF-8 "
902 if (!data->mime_type)
904 g_warning ("Attempting to add `%s' to the list of recently used "
905 "resources, but not MIME type was defined",
912 g_warning ("Attempting to add `%s' to the list of recently used "
913 "resources, but no name of the application that is "
914 "registering it was defined",
921 g_warning ("Attempting to add `%s' to the list of recently used "
922 "resources, but no command line for the application "
923 "that is registering it was defined",
928 priv = manager->priv;
930 if (!priv->recent_items)
932 priv->recent_items = g_bookmark_file_new ();
936 if (data->display_name)
937 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
939 if (data->description)
940 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
942 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
944 if (data->groups && data->groups[0] != '\0')
948 for (j = 0; (data->groups)[j] != NULL; j++)
949 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
952 /* register the application; this will take care of updating the
953 * registration count and time in case the application has
954 * already registered the same document inside the list
956 g_bookmark_file_add_application (priv->recent_items, uri,
960 g_bookmark_file_set_is_private (priv->recent_items, uri,
963 /* mark us as dirty, so that when emitting the "changed" signal we
964 * will dump our changes
966 priv->is_dirty = TRUE;
968 gtk_recent_manager_changed (manager);
974 * gtk_recent_manager_remove_item:
975 * @manager: a #GtkRecentManager
976 * @uri: the URI of the item you wish to remove
977 * @error: return location for a #GError, or %NULL
979 * Removes a resource pointed by @uri from the recently used resources
980 * list handled by a recent manager.
982 * Return value: %TRUE if the item pointed by @uri has been successfully
983 * removed by the recently used resources list, and %FALSE otherwise.
988 gtk_recent_manager_remove_item (GtkRecentManager *manager,
992 GtkRecentManagerPrivate *priv;
993 GError *remove_error = NULL;
995 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
996 g_return_val_if_fail (uri != NULL, FALSE);
997 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
999 priv = manager->priv;
1001 if (!priv->recent_items)
1003 priv->recent_items = g_bookmark_file_new ();
1006 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1007 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1008 _("Unable to find an item with URI '%s'"),
1014 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1017 g_propagate_error (error, remove_error);
1022 priv->is_dirty = TRUE;
1024 gtk_recent_manager_changed (manager);
1030 * gtk_recent_manager_has_item:
1031 * @manager: a #GtkRecentManager
1034 * Checks whether there is a recently used resource registered
1035 * with @uri inside the recent manager.
1037 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1042 gtk_recent_manager_has_item (GtkRecentManager *manager,
1045 GtkRecentManagerPrivate *priv;
1047 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1048 g_return_val_if_fail (uri != NULL, FALSE);
1050 priv = manager->priv;
1051 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1053 return g_bookmark_file_has_item (priv->recent_items, uri);
1057 build_recent_info (GBookmarkFile *bookmarks,
1058 GtkRecentInfo *info)
1060 gchar **apps, **groups;
1061 gsize apps_len, groups_len, i;
1063 g_assert (bookmarks != NULL);
1064 g_assert (info != NULL);
1066 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1067 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1068 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1070 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1072 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1073 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1074 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1076 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1077 for (i = 0; i < groups_len; i++)
1079 gchar *group_name = g_strdup (groups[i]);
1081 info->groups = g_slist_append (info->groups, group_name);
1084 g_strfreev (groups);
1086 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1087 for (i = 0; i < apps_len; i++)
1089 gchar *app_name, *app_exec;
1092 RecentAppInfo *app_info;
1097 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1105 app_info = recent_app_info_new (app_name);
1106 app_info->exec = app_exec;
1107 app_info->count = count;
1108 app_info->stamp = stamp;
1110 info->applications = g_slist_prepend (info->applications, app_info);
1111 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1118 * gtk_recent_manager_lookup_item:
1119 * @manager: a #GtkRecentManager
1121 * @error: a return location for a #GError, or %NULL
1123 * Searches for a URI inside the recently used resources list, and
1124 * returns a structure containing informations about the resource
1125 * like its MIME type, or its display name.
1127 * Return value: a #GtkRecentInfo structure containing information
1128 * about the resource pointed by @uri, or %NULL if the URI was
1129 * not registered in the recently used resources list. Free with
1130 * gtk_recent_info_unref().
1135 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1139 GtkRecentManagerPrivate *priv;
1140 GtkRecentInfo *info = NULL;
1142 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1143 g_return_val_if_fail (uri != NULL, NULL);
1144 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1146 priv = manager->priv;
1147 if (!priv->recent_items)
1149 priv->recent_items = g_bookmark_file_new ();
1152 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1153 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1154 _("Unable to find an item with URI '%s'"),
1160 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1162 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1163 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1164 _("Unable to find an item with URI '%s'"),
1169 info = gtk_recent_info_new (uri);
1170 g_return_val_if_fail (info != NULL, NULL);
1172 /* fill the RecentInfo structure with the data retrieved by our
1173 * parser object from the storage file
1175 build_recent_info (priv->recent_items, info);
1181 * gtk_recent_manager_move_item:
1182 * @manager: a #GtkRecentManager
1183 * @uri: the URI of a recently used resource
1184 * @new_uri: the new URI of the recently used resource, or %NULL to
1185 * remove the item pointed by @uri in the list
1186 * @error: a return location for a #GError, or %NULL
1188 * Changes the location of a recently used resource from @uri to @new_uri.
1190 * Please note that this function will not affect the resource pointed
1191 * by the URIs, but only the URI used in the recently used resources list.
1193 * Return value: %TRUE on success.
1198 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1200 const gchar *new_uri,
1203 GtkRecentManagerPrivate *priv;
1207 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1208 g_return_val_if_fail (uri != NULL, FALSE);
1209 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1211 priv = recent_manager->priv;
1213 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1215 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1216 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1217 _("Unable to find an item with URI '%s'"),
1223 res = g_bookmark_file_move_item (priv->recent_items,
1228 g_propagate_error (error, move_error);
1232 priv->is_dirty = TRUE;
1234 gtk_recent_manager_changed (recent_manager);
1240 * gtk_recent_manager_get_items:
1241 * @manager: a #GtkRecentManager
1243 * Gets the list of recently used resources.
1245 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1246 * gtk_recent_info_unref() on each item inside the list, and then
1247 * free the list itself using g_list_free().
1252 gtk_recent_manager_get_items (GtkRecentManager *manager)
1254 GtkRecentManagerPrivate *priv;
1255 GList *retval = NULL;
1259 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1261 priv = manager->priv;
1262 if (!priv->recent_items)
1265 if (priv->limit == 0)
1268 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1269 for (i = 0; i < uris_len; i++)
1271 GtkRecentInfo *info;
1273 if (priv->limit != -1 && i == priv->limit)
1276 info = gtk_recent_info_new (uris[i]);
1277 build_recent_info (priv->recent_items, info);
1279 retval = g_list_prepend (retval, info);
1288 purge_recent_items_list (GtkRecentManager *manager,
1291 GtkRecentManagerPrivate *priv = manager->priv;
1293 if (!priv->recent_items)
1296 g_bookmark_file_free (priv->recent_items);
1297 priv->recent_items = NULL;
1299 priv->recent_items = g_bookmark_file_new ();
1301 priv->is_dirty = TRUE;
1303 /* emit the changed signal, to ensure that the purge is written */
1304 gtk_recent_manager_changed (manager);
1308 * gtk_recent_manager_purge_items:
1309 * @manager: a #GtkRecentManager
1310 * @error: a return location for a #GError, or %NULL
1312 * Purges every item from the recently used resources list.
1314 * Return value: the number of items that have been removed from the
1315 * recently used resources list.
1320 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1323 GtkRecentManagerPrivate *priv;
1326 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1328 priv = manager->priv;
1329 if (!priv->recent_items)
1332 count = g_bookmark_file_get_size (priv->recent_items);
1336 purge_recent_items_list (manager, error);
1338 purged = count - g_bookmark_file_get_size (priv->recent_items);
1344 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1346 g_signal_emit (recent_manager, signal_changed, 0);
1350 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1353 GtkRecentManagerPrivate *priv = manager->priv;
1358 if (G_UNLIKELY (!priv->recent_items))
1363 uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1365 for (i = 0; i < n_uris; i++)
1367 const gchar *uri = uris[i];
1371 modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1372 item_age = (gint) ((now - modified) / (60 * 60 * 24));
1374 g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1385 gtk_recent_info_get_type (void)
1387 static GType info_type = 0;
1390 info_type = g_boxed_type_register_static (I_("GtkRecentInfo"),
1391 (GBoxedCopyFunc) gtk_recent_info_ref,
1392 (GBoxedFreeFunc) gtk_recent_info_unref);
1396 static GtkRecentInfo *
1397 gtk_recent_info_new (const gchar *uri)
1399 GtkRecentInfo *info;
1401 g_assert (uri != NULL);
1403 info = g_new0 (GtkRecentInfo, 1);
1404 info->uri = g_strdup (uri);
1406 info->applications = NULL;
1407 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1409 info->groups = NULL;
1411 info->ref_count = 1;
1417 gtk_recent_info_free (GtkRecentInfo *recent_info)
1422 g_free (recent_info->uri);
1423 g_free (recent_info->display_name);
1424 g_free (recent_info->description);
1425 g_free (recent_info->mime_type);
1427 if (recent_info->applications)
1429 g_slist_foreach (recent_info->applications,
1430 (GFunc) recent_app_info_free,
1432 g_slist_free (recent_info->applications);
1434 recent_info->applications = NULL;
1437 if (recent_info->apps_lookup)
1438 g_hash_table_destroy (recent_info->apps_lookup);
1440 if (recent_info->groups)
1442 g_slist_foreach (recent_info->groups,
1445 g_slist_free (recent_info->groups);
1447 recent_info->groups = NULL;
1450 if (recent_info->icon)
1451 g_object_unref (recent_info->icon);
1453 g_free (recent_info);
1457 * gtk_recent_info_ref:
1458 * @info: a #GtkRecentInfo
1460 * Increases the reference count of @recent_info by one.
1462 * Return value: the recent info object with its reference count increased
1468 gtk_recent_info_ref (GtkRecentInfo *info)
1470 g_return_val_if_fail (info != NULL, NULL);
1471 g_return_val_if_fail (info->ref_count > 0, NULL);
1473 info->ref_count += 1;
1479 * gtk_recent_info_unref:
1480 * @info: a #GtkRecentInfo
1482 * Decreases the reference count of @info by one. If the reference
1483 * count reaches zero, @info is deallocated, and the memory freed.
1488 gtk_recent_info_unref (GtkRecentInfo *info)
1490 g_return_if_fail (info != NULL);
1491 g_return_if_fail (info->ref_count > 0);
1493 info->ref_count -= 1;
1495 if (info->ref_count == 0)
1496 gtk_recent_info_free (info);
1500 * gtk_recent_info_get_uri:
1501 * @info: a #GtkRecentInfo
1503 * Gets the URI of the resource.
1505 * Return value: the URI of the resource. The returned string is
1506 * owned by the recent manager, and should not be freed.
1510 G_CONST_RETURN gchar *
1511 gtk_recent_info_get_uri (GtkRecentInfo *info)
1513 g_return_val_if_fail (info != NULL, NULL);
1519 * gtk_recent_info_get_display_name:
1520 * @info: a #GtkRecentInfo
1522 * Gets the name of the resource. If none has been defined, the basename
1523 * of the resource is obtained.
1525 * Return value: the display name 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_display_name (GtkRecentInfo *info)
1533 g_return_val_if_fail (info != NULL, NULL);
1535 if (!info->display_name)
1536 info->display_name = gtk_recent_info_get_short_name (info);
1538 return info->display_name;
1542 * gtk_recent_info_get_description:
1543 * @info: a #GtkRecentInfo
1545 * Gets the (short) description of the resource.
1547 * Return value: the description of the resource. The returned string
1548 * is owned by the recent manager, and should not be freed.
1552 G_CONST_RETURN gchar *
1553 gtk_recent_info_get_description (GtkRecentInfo *info)
1555 g_return_val_if_fail (info != NULL, NULL);
1557 return info->description;
1561 * gtk_recent_info_get_mime_type:
1562 * @info: a #GtkRecentInfo
1564 * Gets the MIME type of the resource.
1566 * Return value: the MIME type of the resource. The returned string
1567 * is owned by the recent manager, and should not be freed.
1571 G_CONST_RETURN gchar *
1572 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1574 g_return_val_if_fail (info != NULL, NULL);
1576 if (!info->mime_type)
1577 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1579 return info->mime_type;
1583 * gtk_recent_info_get_added:
1584 * @info: a #GtkRecentInfo
1586 * Gets the timestamp (seconds from system's Epoch) when the resource
1587 * was added to the recently used resources list.
1589 * Return value: the number of seconds elapsed from system's Epoch when
1590 * the resource was added to the list, or -1 on failure.
1595 gtk_recent_info_get_added (GtkRecentInfo *info)
1597 g_return_val_if_fail (info != NULL, (time_t) -1);
1603 * gtk_recent_info_get_modified:
1604 * @info: a #GtkRecentInfo
1606 * Gets the timestamp (seconds from system's Epoch) when the resource
1607 * was last modified.
1609 * Return value: the number of seconds elapsed from system's Epoch when
1610 * the resource was last modified, or -1 on failure.
1615 gtk_recent_info_get_modified (GtkRecentInfo *info)
1617 g_return_val_if_fail (info != NULL, (time_t) -1);
1619 return info->modified;
1623 * gtk_recent_info_get_visited:
1624 * @info: a #GtkRecentInfo
1626 * Gets the timestamp (seconds from system's Epoch) when the resource
1629 * Return value: the number of seconds elapsed from system's Epoch when
1630 * the resource was last visited, or -1 on failure.
1635 gtk_recent_info_get_visited (GtkRecentInfo *info)
1637 g_return_val_if_fail (info != NULL, (time_t) -1);
1639 return info->visited;
1643 * gtk_recent_info_get_private_hint:
1644 * @info: a #GtkRecentInfo
1646 * Gets the value of the "private" flag. Resources in the recently used
1647 * list that have this flag set to %TRUE should only be displayed by the
1648 * applications that have registered them.
1650 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1655 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1657 g_return_val_if_fail (info != NULL, FALSE);
1659 return info->is_private;
1663 static RecentAppInfo *
1664 recent_app_info_new (const gchar *app_name)
1666 RecentAppInfo *app_info;
1668 g_assert (app_name != NULL);
1670 app_info = g_new0 (RecentAppInfo, 1);
1671 app_info->name = g_strdup (app_name);
1672 app_info->exec = NULL;
1673 app_info->count = 1;
1674 app_info->stamp = time (NULL);
1680 recent_app_info_free (RecentAppInfo *app_info)
1685 g_free (app_info->name);
1687 g_free (app_info->exec);
1693 * gtk_recent_info_get_application_info:
1694 * @info: a #GtkRecentInfo
1695 * @app_name: the name of the application that has registered this item
1696 * @app_exec: return location for the string containing the command line
1697 * @count: return location for the number of times this item was registered
1698 * @time_: return location for the timestamp this item was last registered
1699 * for this application
1701 * Gets the data regarding the application that has registered the resource
1704 * If the command line contains any escape characters defined inside the
1705 * storage specification, they will be expanded.
1707 * Return value: %TRUE if an application with @app_name has registered this
1708 * resource inside the recently used list, or %FALSE otherwise. You should
1709 * free the returned command line using g_free().
1714 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1715 const gchar *app_name,
1722 g_return_val_if_fail (info != NULL, FALSE);
1723 g_return_val_if_fail (app_name != NULL, FALSE);
1725 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1729 g_warning ("No registered application with name '%s' "
1730 "for item with URI '%s' found",
1737 *app_exec = ai->exec;
1749 * gtk_recent_info_get_applications:
1750 * @info: a #GtkRecentInfo
1751 * @length: return location for the length of the returned list, or %NULL
1753 * Retrieves the list of applications that have registered this resource.
1755 * Return value: a newly allocated %NULL-terminated array of strings.
1756 * Use g_strfreev() to free it.
1761 gtk_recent_info_get_applications (GtkRecentInfo *info,
1768 g_return_val_if_fail (info != NULL, NULL);
1770 if (!info->applications)
1778 n_apps = g_slist_length (info->applications);
1780 retval = g_new0 (gchar *, n_apps + 1);
1782 for (l = info->applications, i = 0;
1786 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1788 g_assert (ai != NULL);
1790 retval[i++] = g_strdup (ai->name);
1801 * gtk_recent_info_has_application:
1802 * @info: a #GtkRecentInfo
1803 * @app_name: a string containing an application name
1805 * Checks whether an application registered this resource using @app_name.
1807 * Return value: %TRUE if an application with name @app_name was found,
1813 gtk_recent_info_has_application (GtkRecentInfo *info,
1814 const gchar *app_name)
1816 g_return_val_if_fail (info != NULL, FALSE);
1817 g_return_val_if_fail (app_name != NULL, FALSE);
1819 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1823 * gtk_recent_info_last_application:
1824 * @info: a #GtkRecentInfo
1826 * Gets the name of the last application that have registered the
1827 * recently used resource represented by @info.
1829 * Return value: an application name. Use g_free() to free it.
1834 gtk_recent_info_last_application (GtkRecentInfo *info)
1837 time_t last_stamp = (time_t) -1;
1840 g_return_val_if_fail (info != NULL, NULL);
1842 for (l = info->applications; l != NULL; l = l->next)
1844 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1846 if (ai->stamp > last_stamp)
1849 last_stamp = ai->stamp;
1853 return g_strdup (name);
1857 get_icon_for_mime_type (const char *mime_type,
1860 GtkIconTheme *icon_theme;
1861 const char *separator;
1865 separator = strchr (mime_type, '/');
1869 icon_theme = gtk_icon_theme_get_default ();
1871 /* try with the three icon name variants for MIME types */
1873 /* canonicalize MIME type: foo/x-bar -> foo-x-bar */
1874 icon_name = g_string_new (NULL);
1875 g_string_append_len (icon_name, mime_type, separator - mime_type);
1876 g_string_append_c (icon_name, '-');
1877 g_string_append (icon_name, separator + 1);
1878 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1882 g_string_free (icon_name, TRUE);
1886 /* canonicalize MIME type, and prepend "gnome-mime-" */
1887 icon_name = g_string_new ("gnome-mime-");
1888 g_string_append_len (icon_name, mime_type, separator - mime_type);
1889 g_string_append_c (icon_name, '-');
1890 g_string_append (icon_name, separator + 1);
1891 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1895 g_string_free (icon_name, TRUE);
1899 /* try the MIME family icon */
1900 icon_name = g_string_new ("gnome-mime-");
1901 g_string_append_len (icon_name, mime_type, separator - mime_type);
1902 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1906 g_string_free (icon_name, TRUE);
1912 get_icon_fallback (const gchar *icon_name,
1915 GtkIconTheme *icon_theme;
1918 icon_theme = gtk_icon_theme_get_default ();
1920 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1922 GTK_ICON_LOOKUP_USE_BUILTIN,
1924 g_assert (retval != NULL);
1930 * gtk_recent_info_get_icon:
1931 * @info: a #GtkRecentInfo
1932 * @size: the size of the icon in pixels
1934 * Retrieves the icon of size @size associated to the resource MIME type.
1936 * Return value: a #GdkPixbuf containing the icon, or %NULL. Use
1937 * g_object_unref() when finished using the icon.
1942 gtk_recent_info_get_icon (GtkRecentInfo *info,
1945 GdkPixbuf *retval = NULL;
1947 g_return_val_if_fail (info != NULL, NULL);
1949 if (info->mime_type)
1950 retval = get_icon_for_mime_type (info->mime_type, size);
1952 /* this function should never fail */
1955 if (info->mime_type &&
1956 strcmp (info->mime_type, "x-directory/normal") == 0)
1957 retval = get_icon_fallback (GTK_STOCK_DIRECTORY, size);
1959 retval = get_icon_fallback (GTK_STOCK_FILE, size);
1966 * gtk_recent_info_is_local:
1967 * @info: a #GtkRecentInfo
1969 * Checks whether the resource is local or not by looking at the
1970 * scheme of its URI.
1972 * Return value: %TRUE if the resource is local.
1977 gtk_recent_info_is_local (GtkRecentInfo *info)
1979 g_return_val_if_fail (info != NULL, FALSE);
1981 return has_case_prefix (info->uri, "file:/");
1985 * gtk_recent_info_exists:
1986 * @info: a #GtkRecentInfo
1988 * Checks whether the resource pointed by @info still exists. At
1989 * the moment this check is done only on resources pointing to local files.
1991 * Return value: %TRUE if the resource exists
1996 gtk_recent_info_exists (GtkRecentInfo *info)
1999 struct stat stat_buf;
2000 gboolean retval = FALSE;
2002 g_return_val_if_fail (info != NULL, FALSE);
2004 /* we guarantee only local resources */
2005 if (!gtk_recent_info_is_local (info))
2008 filename = g_filename_from_uri (info->uri, NULL, NULL);
2011 if (stat (filename, &stat_buf) == 0)
2021 * gtk_recent_info_match:
2022 * @info_a: a #GtkRecentInfo
2023 * @info_b: a #GtkRecentInfo
2025 * Checks whether two #GtkRecentInfo structures point to the same
2028 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2029 * resource, %FALSE otherwise.
2034 gtk_recent_info_match (GtkRecentInfo *info_a,
2035 GtkRecentInfo *info_b)
2037 g_return_val_if_fail (info_a != NULL, FALSE);
2038 g_return_val_if_fail (info_b != NULL, FALSE);
2040 return (0 == strcmp (info_a->uri, info_b->uri));
2043 /* taken from gnome-vfs-uri.c */
2044 static const gchar *
2045 get_method_string (const gchar *substring,
2046 gchar **method_string)
2052 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2059 !(p == substring + 1 && g_ascii_isalpha (*substring))
2063 /* Found toplevel method specification. */
2064 method = g_strndup (substring, p - substring);
2065 *method_string = g_ascii_strdown (method, -1);
2071 *method_string = g_strdup ("file");
2078 /* Stolen from gnome_vfs_make_valid_utf8() */
2080 make_valid_utf8 (const char *name)
2083 const char *remainder, *invalid;
2084 int remaining_bytes, valid_bytes;
2088 remaining_bytes = name ? strlen (name) : 0;
2090 while (remaining_bytes != 0)
2092 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2095 valid_bytes = invalid - remainder;
2098 string = g_string_sized_new (remaining_bytes);
2100 g_string_append_len (string, remainder, valid_bytes);
2101 g_string_append_c (string, '?');
2103 remaining_bytes -= valid_bytes + 1;
2104 remainder = invalid + 1;
2108 return g_strdup (name);
2110 g_string_append (string, remainder);
2111 g_assert (g_utf8_validate (string->str, -1, NULL));
2113 return g_string_free (string, FALSE);
2117 get_uri_shortname_for_display (const gchar *uri)
2120 gboolean validated = FALSE;
2122 if (has_case_prefix (uri, "file:/"))
2126 local_file = g_filename_from_uri (uri, NULL, NULL);
2130 name = g_filename_display_basename (local_file);
2134 g_free (local_file);
2143 rest = get_method_string (uri, &method);
2144 local_file = g_filename_display_basename (rest);
2146 name = g_strconcat (method, ": ", local_file, NULL);
2148 g_free (local_file);
2152 g_assert (name != NULL);
2154 if (!validated && !g_utf8_validate (name, -1, NULL))
2158 utf8_name = make_valid_utf8 (name);
2168 * gtk_recent_info_get_short_name:
2169 * @info: an #GtkRecentInfo
2171 * Computes a valid UTF-8 string that can be used as the name of the item in a
2172 * menu or list. For example, calling this function on an item that refers to
2173 * "file:///foo/bar.txt" will yield "bar.txt".
2175 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2181 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2185 g_return_val_if_fail (info != NULL, NULL);
2187 if (info->uri == NULL)
2190 short_name = get_uri_shortname_for_display (info->uri);
2196 * gtk_recent_info_get_uri_display:
2197 * @info: a #GtkRecentInfo
2199 * Gets a displayable version of the resource's URI. If the resource
2200 * is local, it returns a local path; if the resource is not local,
2201 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2203 * Return value: a newly allocated UTF-8 string containing the
2204 * resource's URI or %NULL. Use g_free() when done using it.
2209 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2213 g_return_val_if_fail (info != NULL, NULL);
2216 if (gtk_recent_info_is_local (info))
2220 filename = g_filename_from_uri (info->uri, NULL, NULL);
2224 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2229 retval = make_valid_utf8 (info->uri);
2236 * gtk_recent_info_get_age:
2237 * @info: a #GtkRecentInfo
2239 * Gets the number of days elapsed since the last update of the resource
2242 * Return value: a positive integer containing the number of days elapsed
2243 * since the time this resource was last modified.
2248 gtk_recent_info_get_age (GtkRecentInfo *info)
2253 g_return_val_if_fail (info != NULL, -1);
2257 delta = now - info->modified;
2259 retval = (gint) (delta / (60 * 60 * 24));
2265 * gtk_recent_info_get_groups:
2266 * @info: a #GtkRecentInfo
2267 * @length: return location for the number of groups returned, or %NULL
2269 * Returns all groups registered for the recently used item @info. The
2270 * array of returned group names will be %NULL terminated, so length might
2271 * optionally be %NULL.
2273 * Return value: a newly allocated %NULL terminated array of strings. Use
2274 * g_strfreev() to free it.
2279 gtk_recent_info_get_groups (GtkRecentInfo *info,
2286 g_return_val_if_fail (info != NULL, NULL);
2296 n_groups = g_slist_length (info->groups);
2298 retval = g_new0 (gchar *, n_groups + 1);
2300 for (l = info->groups, i = 0;
2304 gchar *group_name = (gchar *) l->data;
2306 g_assert (group_name != NULL);
2308 retval[i++] = g_strdup (group_name);
2319 * gtk_recent_info_has_group:
2320 * @info: a #GtkRecentInfo
2321 * @group_name: name of a group
2323 * Checks whether @group_name appears inside the groups registered for the
2324 * recently used item @info.
2326 * Return value: %TRUE if the group was found.
2331 gtk_recent_info_has_group (GtkRecentInfo *info,
2332 const gchar *group_name)
2336 g_return_val_if_fail (info != NULL, FALSE);
2337 g_return_val_if_fail (group_name != NULL, FALSE);
2342 for (l = info->groups; l != NULL; l = l->next)
2344 gchar *g = (gchar *) l->data;
2346 if (strcmp (g, group_name) == 0)
2354 * _gtk_recent_manager_sync:
2356 * Private function for synchronising the recent manager singleton.
2359 _gtk_recent_manager_sync (void)
2361 if (recent_manager_singleton)
2363 /* force a dump of the contents of the recent manager singleton */
2364 recent_manager_singleton->priv->is_dirty = TRUE;
2365 gtk_recent_manager_real_changed (recent_manager_singleton);
2369 #define __GTK_RECENT_MANAGER_C__
2370 #include "gtkaliasdef.c"