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>
34 #include "gtkrecentmanager.h"
37 #include "gtkicontheme.h"
38 #include "gtktypebuiltins.h"
39 #include "gtkprivate.h"
40 #include "gtkmarshalers.h"
44 #define XDG_PREFIX _gtk_xdg
45 #include "xdgmime/xdgmime.h"
48 /* the file where we store the recently used items */
49 #define GTK_RECENTLY_USED_FILE ".recently-used.xbel"
51 /* a poll approximately every five seconds */
54 /* return all items by default */
55 #define DEFAULT_LIMIT -1
57 /* keep in sync with xdgmime */
58 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
64 GDestroyNotify notify;
91 GHashTable *apps_lookup;
102 struct _GtkRecentManagerPrivate
107 guint write_in_progress : 1;
108 guint read_in_progress : 1;
113 GBookmarkFile *recent_items;
128 static void gtk_recent_manager_finalize (GObject *object);
130 static void gtk_recent_manager_set_property (GObject *object,
134 static void gtk_recent_manager_get_property (GObject *object,
138 static void gtk_recent_manager_changed (GtkRecentManager *manager);
140 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
141 static gboolean gtk_recent_manager_poll_timeout (gpointer data);
142 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
143 const gchar *filename);
145 static void build_recent_items_list (GtkRecentManager *manager);
146 static void purge_recent_items_list (GtkRecentManager *manager,
149 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
150 static void recent_app_info_free (RecentAppInfo *app_info);
152 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
153 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
155 static guint signal_changed = 0;
157 static GtkRecentManager *recent_manager_singleton = NULL;
159 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
162 filename_warning (const gchar *format,
163 const gchar *filename,
164 const gchar *message)
166 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
167 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
171 /* Test of haystack has the needle prefix, comparing case
172 * insensitive. haystack may be UTF-8, but needle must
173 * contain only lowercase ascii. */
175 has_case_prefix (const gchar *haystack,
180 /* Eat one character at a time. */
184 while (*n && *h && *n == g_ascii_tolower (*h))
194 gtk_recent_manager_error_quark (void)
196 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
200 threads_dispatch (gpointer data)
202 ThreadsDispatch *dispatch = data;
203 gboolean res = FALSE;
205 GDK_THREADS_ENTER ();
207 if (!g_source_is_destroyed (g_main_current_source ()))
208 res = dispatch->func (dispatch->data);
210 GDK_THREADS_LEAVE ();
216 threads_free (gpointer data)
218 ThreadsDispatch *dispatch = data;
220 if (dispatch->notify)
221 dispatch->notify (dispatch->data);
223 g_slice_free (ThreadsDispatch, dispatch);
227 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
229 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231 gtk_recent_manager_parent_class = g_type_class_peek_parent (klass);
233 gobject_class->set_property = gtk_recent_manager_set_property;
234 gobject_class->get_property = gtk_recent_manager_get_property;
235 gobject_class->finalize = gtk_recent_manager_finalize;
238 * GtkRecentManager:filename
240 * The full path to the file to be used to store and read the recently
241 * used resources list
245 g_object_class_install_property (gobject_class,
247 g_param_spec_string ("filename",
249 P_("The full path to the file to be used to store and read the list"),
251 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE)));
253 * GtkRecentManager:limit
255 * The maximum number of items to be returned by the
256 * gtk_recent_manager_get_items() function.
260 g_object_class_install_property (gobject_class,
262 g_param_spec_int ("limit",
264 P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
270 * GtkRecentManager:size
272 * The size of the recently used resources list.
276 g_object_class_install_property (gobject_class,
278 g_param_spec_int ("size",
280 P_("The size of the recently used resources list"),
287 * GtkRecentManager::changed
288 * @recent_manager: the recent manager
290 * Emitted when the current recently used resources manager changes its
296 g_signal_new (I_("changed"),
297 G_TYPE_FROM_CLASS (klass),
299 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
301 g_cclosure_marshal_VOID__VOID,
304 klass->changed = gtk_recent_manager_real_changed;
306 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
310 gtk_recent_manager_init (GtkRecentManager *manager)
312 GtkRecentManagerPrivate *priv;
313 ThreadsDispatch *dispatch;
315 priv = g_type_instance_get_private ((GTypeInstance *) manager,
316 GTK_TYPE_RECENT_MANAGER);
317 manager->priv = priv;
319 priv->limit = DEFAULT_LIMIT;
322 priv->is_dirty = FALSE;
323 priv->write_in_progress = FALSE;
324 priv->read_in_progress = FALSE;
326 priv->filename = g_build_filename (g_get_home_dir (),
327 GTK_RECENTLY_USED_FILE,
330 dispatch = g_slice_new (ThreadsDispatch);
331 dispatch->func = gtk_recent_manager_poll_timeout;
332 dispatch->data = manager;
333 dispatch->notify = NULL;
334 priv->poll_timeout = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT + 30,
340 build_recent_items_list (manager);
344 gtk_recent_manager_set_property (GObject *object,
349 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
354 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
357 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gtk_recent_manager_get_property (GObject *object,
371 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
376 g_value_set_string (value, recent_manager->priv->filename);
379 g_value_set_int (value, recent_manager->priv->limit);
382 g_value_set_int (value, recent_manager->priv->size);
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
391 gtk_recent_manager_finalize (GObject *object)
393 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
394 GtkRecentManagerPrivate *priv = manager->priv;
396 /* remove the poll timeout */
397 if (priv->poll_timeout)
398 g_source_remove (priv->poll_timeout);
400 g_free (priv->filename);
402 if (priv->recent_items)
403 g_bookmark_file_free (priv->recent_items);
405 /* chain up parent's finalize method */
406 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
410 gtk_recent_manager_real_changed (GtkRecentManager *manager)
412 GtkRecentManagerPrivate *priv = manager->priv;
414 g_object_freeze_notify (G_OBJECT (manager));
419 struct stat stat_buf;
421 /* we are marked as dirty, so we dump the content of our
422 * recently used items list
424 g_assert (priv->filename != NULL);
426 priv->write_in_progress = TRUE;
428 /* if no container object has been defined, we create a new
429 * empty container, and dump it
431 if (!priv->recent_items)
433 priv->recent_items = g_bookmark_file_new ();
438 g_bookmark_file_to_file (priv->recent_items,
444 filename_warning ("Attempting to store changes into `%s', "
447 write_error->message);
448 g_error_free (write_error);
451 priv->write_in_progress = FALSE;
453 /* we have sync'ed our list with the storage file, so we
454 * update the file mtime in order to skip the timed check
455 * and spare us from a re-read.
457 if (g_stat (priv->filename, &stat_buf) < 0)
459 filename_warning ("Unable to stat() the recently used resources file "
464 g_object_thaw_notify (G_OBJECT (manager));
469 priv->last_mtime = stat_buf.st_mtime;
471 /* mark us as clean */
472 priv->is_dirty = FALSE;
476 /* we are not marked as dirty, so we have been called
477 * because the recently used resources file has been
478 * changed (and not from us).
480 build_recent_items_list (manager);
483 g_object_thaw_notify (G_OBJECT (manager));
486 /* timed poll()-ing of the recently used resources file.
487 * an event-based system would be more efficient.
490 gtk_recent_manager_poll_timeout (gpointer data)
492 GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
493 GtkRecentManagerPrivate *priv = manager->priv;
494 struct stat stat_buf;
497 /* wait for the next timeout if we have a read/write in progress */
498 if (priv->write_in_progress || priv->read_in_progress)
501 stat_res = g_stat (priv->filename, &stat_buf);
504 /* the file does not exist, yet, so we wait */
508 filename_warning ("Unable to stat() the recently used resources file "
516 /* the file didn't change from the last poll(), so we bail out */
517 if (stat_buf.st_mtime == priv->last_mtime)
520 /* the file has been changed, hence we emit the "changed" signal */
521 gtk_recent_manager_changed (manager);
527 gtk_recent_manager_set_filename (GtkRecentManager *manager,
528 const gchar *filename)
530 GtkRecentManagerPrivate *priv;
531 ThreadsDispatch *dispatch;
533 g_assert (GTK_IS_RECENT_MANAGER (manager));
534 priv = manager->priv;
536 if (!filename || filename[0] == '\0')
539 g_free (manager->priv->filename);
541 if (manager->priv->poll_timeout)
543 g_source_remove (manager->priv->poll_timeout);
544 manager->priv->poll_timeout = 0;
547 priv->filename = g_strdup (filename);
549 dispatch = g_slice_new (ThreadsDispatch);
550 dispatch->func = gtk_recent_manager_poll_timeout;
551 dispatch->data = manager;
552 dispatch->notify = NULL;
553 priv->poll_timeout = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT + 30,
559 /* mark us clean, so that we can re-read the list
560 * of recently used resources
562 priv->is_dirty = FALSE;
563 build_recent_items_list (manager);
566 /* reads the recently used resources file and builds the items list.
567 * we keep the items list inside the parser object, and build the
568 * RecentInfo object only on user's demand to avoid useless replication.
569 * this function resets the dirty bit of the manager.
572 build_recent_items_list (GtkRecentManager *manager)
574 GtkRecentManagerPrivate *priv;
575 struct stat stat_buf;
581 priv = manager->priv;
582 g_assert (priv->filename != NULL);
584 if (!priv->recent_items)
586 priv->recent_items = g_bookmark_file_new ();
590 stat_res = g_stat (priv->filename, &stat_buf);
593 /* the file doesn't exists, so we bail out and wait for the first
601 filename_warning ("Attempting to read the recently used resources file "
602 "at `%s', but an error occurred: %s. Aborting.",
610 /* record the last mtime, for later use */
611 priv->last_mtime = stat_buf.st_mtime;
613 priv->read_in_progress = TRUE;
615 /* the file exists, and it's valid (we hope); if not, destroy the container
616 * object and hope for a better result when the next "changed" signal is
619 res = g_bookmark_file_load_from_file (priv->recent_items,
624 filename_warning ("Attempting to read the recently used resources file "
625 "at `%s', but the parser failed: %s.",
627 read_error->message);
629 g_bookmark_file_free (priv->recent_items);
630 priv->recent_items = NULL;
632 g_error_free (read_error);
635 size = g_bookmark_file_get_size (priv->recent_items);
636 if (priv->size != size)
640 g_object_notify (G_OBJECT (manager), "size");
643 priv->read_in_progress = FALSE;
644 priv->is_dirty = FALSE;
648 /********************
650 ********************/
654 * gtk_recent_manager_new:
656 * Creates a new recent manager object. Recent manager objects are used to
657 * handle the list of recently used resources. A #GtkRecentManager object
658 * monitors the recently used resources list, and emits the "changed" signal
659 * each time something inside the list changes.
661 * #GtkRecentManager objects are expensive: be sure to create them only when
662 * needed. You should use gtk_recent_manager_get_default() instead.
664 * Return value: A newly created #GtkRecentManager object.
669 gtk_recent_manager_new (void)
671 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
675 * gtk_recent_manager_get_default:
677 * Gets a unique instance of #GtkRecentManager, that you can share
678 * in your application without caring about memory management. The
679 * returned instance will be freed when you application terminates.
681 * Return value: A unique #GtkRecentManager. Do not ref or unref it.
686 gtk_recent_manager_get_default (void)
688 if (G_UNLIKELY (!recent_manager_singleton))
689 recent_manager_singleton = gtk_recent_manager_new ();
691 return recent_manager_singleton;
695 * gtk_recent_manager_get_for_screen:
696 * @screen: a #GdkScreen
698 * Gets the recent manager object associated with @screen; if this
699 * function has not previously been called for the given screen,
700 * a new recent manager object will be created and associated with
701 * the screen. Recent manager objects are fairly expensive to create,
702 * so using this function is usually a better choice than calling
703 * gtk_recent_manager_new() and setting the screen yourself; by using
704 * this function a single recent manager object will be shared between
707 * Return value: A unique #GtkRecentManager associated with the given
708 * screen. This recent manager is associated to the with the screen
709 * and can be used as long as the screen is open. Do not ref or
712 * @Deprecated: 2.12: This function has been deprecated and should
713 * not be used in newly written code. Calling this function is
714 * equivalent to calling gtk_recent_manager_get_default().
719 gtk_recent_manager_get_for_screen (GdkScreen *screen)
721 return gtk_recent_manager_get_default ();
725 * gtk_recent_manager_set_screen:
726 * @manager: a #GtkRecentManager
727 * @screen: a #GdkScreen
729 * Sets the screen for a recent manager; the screen is used to
730 * track the user's currently configured recently used documents
735 * @Deprecated: 2.12: This function has been deprecated and should
736 * not be used in newly written code. Calling this function has
740 gtk_recent_manager_set_screen (GtkRecentManager *manager,
747 * gtk_recent_manager_set_limit:
748 * @manager: a #GtkRecentManager
749 * @limit: the maximum number of items to return, or -1.
751 * Sets the maximum number of item that the gtk_recent_manager_get_items()
752 * function should return. If @limit is set to -1, then return all the
758 gtk_recent_manager_set_limit (GtkRecentManager *manager,
761 GtkRecentManagerPrivate *priv;
763 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
765 priv = manager->priv;
770 * gtk_recent_manager_get_limit:
771 * @manager: a #GtkRecentManager
773 * Gets the maximum number of items that the gtk_recent_manager_get_items()
774 * function should return.
776 * Return value: the number of items to return, or -1 for every item.
781 gtk_recent_manager_get_limit (GtkRecentManager *manager)
783 GtkRecentManagerPrivate *priv;
785 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
787 priv = manager->priv;
792 * gtk_recent_manager_add_item:
793 * @manager: a #GtkRecentManager
796 * Adds a new resource, pointed by @uri, into the recently used
799 * This function automatically retrieving some of the needed
800 * metadata and setting other metadata to common default values; it
801 * then feeds the data to gtk_recent_manager_add_full().
803 * See gtk_recent_manager_add_full() if you want to explicitely
804 * define the metadata for the resource pointed by @uri.
806 * Return value: %TRUE if the new item was successfully added
807 * to the recently used resources list
812 gtk_recent_manager_add_item (GtkRecentManager *manager,
815 GtkRecentData recent_data;
818 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
819 g_return_val_if_fail (uri != NULL, FALSE);
821 recent_data.display_name = NULL;
822 recent_data.description = NULL;
823 recent_data.mime_type = NULL;
826 if (has_case_prefix (uri, "file:/"))
829 const gchar *mime_type;
831 filename = g_filename_from_uri (uri, NULL, NULL);
834 mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
836 recent_data.mime_type = g_strdup (mime_type);
841 if (!recent_data.mime_type)
842 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
846 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
848 recent_data.app_name = g_strdup (g_get_application_name ());
849 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
850 recent_data.groups = NULL;
851 recent_data.is_private = FALSE;
853 retval = gtk_recent_manager_add_full (manager, uri, &recent_data);
855 g_free (recent_data.mime_type);
856 g_free (recent_data.app_name);
857 g_free (recent_data.app_exec);
863 * gtk_recent_manager_add_full:
864 * @manager: a #GtkRecentManager
866 * @recent_data: metadata of the resource
868 * Adds a new resource, pointed by @uri, into the recently used
869 * resources list, using the metadata specified inside the #GtkRecentData
870 * structure passed in @recent_data.
872 * The passed URI will be used to identify this resource inside the
875 * In order to register the new recently used resource, metadata about
876 * the resource must be passed as well as the URI; the metadata is
877 * stored in a #GtkRecentData structure, which must contain the MIME
878 * type of the resource pointed by the URI; the name of the application
879 * that is registering the item, and a command line to be used when
880 * launching the item.
882 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
883 * to be used when viewing the item instead of the last component of the
884 * URI; a short description of the item; whether the item should be
885 * considered private - that is, should be displayed only by the
886 * applications that have registered it.
888 * Return value: %TRUE if the new item was successfully added to the
889 * recently used resources list, %FALSE otherwise.
894 gtk_recent_manager_add_full (GtkRecentManager *manager,
896 const GtkRecentData *data)
898 GtkRecentManagerPrivate *priv;
900 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
901 g_return_val_if_fail (uri != NULL, FALSE);
902 g_return_val_if_fail (data != NULL, FALSE);
905 if ((data->display_name) &&
906 (!g_utf8_validate (data->display_name, -1, NULL)))
908 g_warning ("Attempting to add `%s' to the list of recently used "
909 "resources, but the display name is not a valid UTF-8 "
915 if ((data->description) &&
916 (!g_utf8_validate (data->description, -1, NULL)))
918 g_warning ("Attempting to add `%s' to the list of recently used "
919 "resources, but the description is not a valid UTF-8 "
926 if (!data->mime_type)
928 g_warning ("Attempting to add `%s' to the list of recently used "
929 "resources, but not MIME type was defined",
936 g_warning ("Attempting to add `%s' to the list of recently used "
937 "resources, but no name of the application that is "
938 "registering it was defined",
945 g_warning ("Attempting to add `%s' to the list of recently used "
946 "resources, but no command line for the application "
947 "that is registering it was defined",
952 priv = manager->priv;
954 if (!priv->recent_items)
956 priv->recent_items = g_bookmark_file_new ();
960 if (data->display_name)
961 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
963 if (data->description)
964 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
966 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
968 if (data->groups && data->groups[0] != '\0')
972 for (j = 0; (data->groups)[j] != NULL; j++)
973 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
976 /* register the application; this will take care of updating the
977 * registration count and time in case the application has
978 * already registered the same document inside the list
980 g_bookmark_file_add_application (priv->recent_items, uri,
984 g_bookmark_file_set_is_private (priv->recent_items, uri,
987 /* mark us as dirty, so that when emitting the "changed" signal we
988 * will dump our changes
990 priv->is_dirty = TRUE;
992 gtk_recent_manager_changed (manager);
998 * gtk_recent_manager_remove_item:
999 * @manager: a #GtkRecentManager
1000 * @uri: the URI of the item you wish to remove
1001 * @error: return location for a #GError, or %NULL
1003 * Removes a resource pointed by @uri from the recently used resources
1004 * list handled by a recent manager.
1006 * Return value: %TRUE if the item pointed by @uri has been successfully
1007 * removed by the recently used resources list, and %FALSE otherwise.
1012 gtk_recent_manager_remove_item (GtkRecentManager *manager,
1016 GtkRecentManagerPrivate *priv;
1017 GError *remove_error = NULL;
1019 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1020 g_return_val_if_fail (uri != NULL, FALSE);
1021 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1023 priv = manager->priv;
1025 if (!priv->recent_items)
1027 priv->recent_items = g_bookmark_file_new ();
1030 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1031 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1032 _("Unable to find an item with URI '%s'"),
1038 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1041 g_propagate_error (error, remove_error);
1046 priv->is_dirty = TRUE;
1048 gtk_recent_manager_changed (manager);
1054 * gtk_recent_manager_has_item:
1055 * @manager: a #GtkRecentManager
1058 * Checks whether there is a recently used resource registered
1059 * with @uri inside the recent manager.
1061 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1066 gtk_recent_manager_has_item (GtkRecentManager *manager,
1069 GtkRecentManagerPrivate *priv;
1071 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1072 g_return_val_if_fail (uri != NULL, FALSE);
1074 priv = manager->priv;
1075 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1077 return g_bookmark_file_has_item (priv->recent_items, uri);
1081 build_recent_info (GBookmarkFile *bookmarks,
1082 GtkRecentInfo *info)
1084 gchar **apps, **groups;
1085 gsize apps_len, groups_len, i;
1087 g_assert (bookmarks != NULL);
1088 g_assert (info != NULL);
1090 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1091 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1092 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1094 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1096 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1097 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1098 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1100 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1101 for (i = 0; i < groups_len; i++)
1103 gchar *group_name = g_strdup (groups[i]);
1105 info->groups = g_slist_append (info->groups, group_name);
1108 g_strfreev (groups);
1110 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1111 for (i = 0; i < apps_len; i++)
1113 gchar *app_name, *app_exec;
1116 RecentAppInfo *app_info;
1121 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1129 app_info = recent_app_info_new (app_name);
1130 app_info->exec = app_exec;
1131 app_info->count = count;
1132 app_info->stamp = stamp;
1134 info->applications = g_slist_append (info->applications,
1136 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1145 * gtk_recent_manager_lookup_item:
1146 * @manager: a #GtkRecentManager
1148 * @error: a return location for a #GError, or %NULL
1150 * Searches for a URI inside the recently used resources list, and
1151 * returns a structure containing informations about the resource
1152 * like its MIME type, or its display name.
1154 * Return value: a #GtkRecentInfo structure containing information
1155 * about the resource pointed by @uri, or %NULL if the URI was
1156 * not registered in the recently used resources list. Free with
1157 * gtk_recent_info_unref().
1162 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1166 GtkRecentManagerPrivate *priv;
1167 GtkRecentInfo *info = NULL;
1170 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1171 g_return_val_if_fail (uri != NULL, NULL);
1172 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1174 priv = manager->priv;
1175 if (!priv->recent_items)
1177 priv->recent_items = g_bookmark_file_new ();
1180 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1181 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1182 _("Unable to find an item with URI '%s'"),
1188 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1190 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1191 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1192 _("Unable to find an item with URI '%s'"),
1197 info = gtk_recent_info_new (uri);
1198 g_return_val_if_fail (info != NULL, NULL);
1200 /* fill the RecentInfo structure with the data retrieved by our
1201 * parser object from the storage file
1203 res = build_recent_info (priv->recent_items, info);
1206 gtk_recent_info_free (info);
1215 * gtk_recent_manager_move_item:
1216 * @manager: a #GtkRecentManager
1217 * @uri: the URI of a recently used resource
1218 * @new_uri: the new URI of the recently used resource, or %NULL to
1219 * remove the item pointed by @uri in the list
1220 * @error: a return location for a #GError, or %NULL
1222 * Changes the location of a recently used resource from @uri to @new_uri.
1224 * Please note that this function will not affect the resource pointed
1225 * by the URIs, but only the URI used in the recently used resources list.
1227 * Return value: %TRUE on success.
1232 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1234 const gchar *new_uri,
1237 GtkRecentManagerPrivate *priv;
1241 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1242 g_return_val_if_fail (uri != NULL, FALSE);
1243 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1245 priv = recent_manager->priv;
1247 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1249 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1250 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1251 _("Unable to find an item with URI '%s'"),
1257 res = g_bookmark_file_move_item (priv->recent_items,
1262 g_propagate_error (error, move_error);
1266 priv->is_dirty = TRUE;
1268 gtk_recent_manager_changed (recent_manager);
1274 * gtk_recent_manager_get_items:
1275 * @manager: a #GtkRecentManager
1277 * Gets the list of recently used resources.
1279 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1280 * gtk_recent_info_unref() on each item inside the list, and then
1281 * free the list itself using g_list_free().
1286 gtk_recent_manager_get_items (GtkRecentManager *manager)
1288 GtkRecentManagerPrivate *priv;
1289 GList *retval = NULL;
1293 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1295 priv = manager->priv;
1296 if (!priv->recent_items)
1299 if (priv->limit == 0)
1302 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1303 for (i = 0; i < uris_len; i++)
1305 GtkRecentInfo *info;
1308 info = gtk_recent_info_new (uris[i]);
1309 res = build_recent_info (priv->recent_items, info);
1312 g_warning ("Unable to create a RecentInfo object for "
1313 "item with URI `%s'",
1315 gtk_recent_info_free (info);
1320 retval = g_list_prepend (retval, info);
1325 /* clamp the list, if a limit is present */
1326 if ((priv->limit != -1) &&
1327 (g_list_length (retval) > priv->limit))
1331 clamp = g_list_nth (retval, priv->limit - 1);
1339 g_list_foreach (l, (GFunc) gtk_recent_info_free, NULL);
1347 purge_recent_items_list (GtkRecentManager *manager,
1350 GtkRecentManagerPrivate *priv = manager->priv;
1352 if (!priv->recent_items)
1355 g_bookmark_file_free (priv->recent_items);
1356 priv->recent_items = NULL;
1358 priv->recent_items = g_bookmark_file_new ();
1360 priv->is_dirty = TRUE;
1362 /* emit the changed signal, to ensure that the purge is written */
1363 gtk_recent_manager_changed (manager);
1367 * gtk_recent_manager_purge_items:
1368 * @manager: a #GtkRecentManager
1369 * @error: a return location for a #GError, or %NULL
1371 * Purges every item from the recently used resources list.
1373 * Return value: the number of items that have been removed from the
1374 * recently used resources list.
1379 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1382 GtkRecentManagerPrivate *priv;
1385 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1387 priv = manager->priv;
1388 if (!priv->recent_items)
1391 count = g_bookmark_file_get_size (priv->recent_items);
1395 purge_recent_items_list (manager, error);
1397 purged = count - g_bookmark_file_get_size (priv->recent_items);
1403 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1405 g_signal_emit (recent_manager, signal_changed, 0);
1413 gtk_recent_info_get_type (void)
1415 static GType info_type = 0;
1418 info_type = g_boxed_type_register_static (I_("GtkRecentInfo"),
1419 (GBoxedCopyFunc) gtk_recent_info_ref,
1420 (GBoxedFreeFunc) gtk_recent_info_unref);
1424 static GtkRecentInfo *
1425 gtk_recent_info_new (const gchar *uri)
1427 GtkRecentInfo *info;
1429 g_assert (uri != NULL);
1431 info = g_new0 (GtkRecentInfo, 1);
1432 info->uri = g_strdup (uri);
1434 info->applications = NULL;
1435 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1437 info->groups = NULL;
1439 info->ref_count = 1;
1445 gtk_recent_info_free (GtkRecentInfo *recent_info)
1450 g_free (recent_info->uri);
1451 g_free (recent_info->display_name);
1452 g_free (recent_info->description);
1453 g_free (recent_info->mime_type);
1455 if (recent_info->applications)
1457 g_slist_foreach (recent_info->applications,
1458 (GFunc) recent_app_info_free,
1460 g_slist_free (recent_info->applications);
1462 recent_info->applications = NULL;
1465 if (recent_info->apps_lookup)
1466 g_hash_table_destroy (recent_info->apps_lookup);
1468 if (recent_info->groups)
1470 g_slist_foreach (recent_info->groups,
1473 g_slist_free (recent_info->groups);
1475 recent_info->groups = NULL;
1478 if (recent_info->icon)
1479 g_object_unref (recent_info->icon);
1481 g_free (recent_info);
1485 * gtk_recent_info_ref:
1486 * @info: a #GtkRecentInfo
1488 * Increases the reference count of @recent_info by one.
1490 * Return value: the recent info object with its reference count increased
1496 gtk_recent_info_ref (GtkRecentInfo *info)
1498 g_return_val_if_fail (info != NULL, NULL);
1499 g_return_val_if_fail (info->ref_count > 0, NULL);
1501 info->ref_count += 1;
1507 * gtk_recent_info_unref:
1508 * @info: a #GtkRecentInfo
1510 * Decreases the reference count of @info by one. If the reference
1511 * count reaches zero, @info is deallocated, and the memory freed.
1516 gtk_recent_info_unref (GtkRecentInfo *info)
1518 g_return_if_fail (info != NULL);
1519 g_return_if_fail (info->ref_count > 0);
1521 info->ref_count -= 1;
1523 if (info->ref_count == 0)
1524 gtk_recent_info_free (info);
1528 * gtk_recent_info_get_uri:
1529 * @info: a #GtkRecentInfo
1531 * Gets the URI of the resource.
1533 * Return value: the URI of the resource. The returned string is
1534 * owned by the recent manager, and should not be freed.
1538 G_CONST_RETURN gchar *
1539 gtk_recent_info_get_uri (GtkRecentInfo *info)
1541 g_return_val_if_fail (info != NULL, NULL);
1547 * gtk_recent_info_get_display_name:
1548 * @info: a #GtkRecentInfo
1550 * Gets the name of the resource. If none has been defined, the basename
1551 * of the resource is obtained.
1553 * Return value: the display name of the resource. The returned string
1554 * is owned by the recent manager, and should not be freed.
1558 G_CONST_RETURN gchar *
1559 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1561 g_return_val_if_fail (info != NULL, NULL);
1563 if (!info->display_name)
1564 info->display_name = gtk_recent_info_get_short_name (info);
1566 return info->display_name;
1570 * gtk_recent_info_get_description:
1571 * @info: a #GtkRecentInfo
1573 * Gets the (short) description of the resource.
1575 * Return value: the description of the resource. The returned string
1576 * is owned by the recent manager, and should not be freed.
1580 G_CONST_RETURN gchar *
1581 gtk_recent_info_get_description (GtkRecentInfo *info)
1583 g_return_val_if_fail (info != NULL, NULL);
1585 return info->description;
1589 * gtk_recent_info_get_mime_type:
1590 * @info: a #GtkRecentInfo
1592 * Gets the MIME type of the resource.
1594 * Return value: the MIME type of the resource. The returned string
1595 * is owned by the recent manager, and should not be freed.
1599 G_CONST_RETURN gchar *
1600 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1602 g_return_val_if_fail (info != NULL, NULL);
1604 if (!info->mime_type)
1605 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1607 return info->mime_type;
1611 * gtk_recent_info_get_added:
1612 * @info: a #GtkRecentInfo
1614 * Gets the timestamp (seconds from system's Epoch) when the resource
1615 * was added to the recently used resources list.
1617 * Return value: the number of seconds elapsed from system's Epoch when
1618 * the resource was added to the list, or -1 on failure.
1623 gtk_recent_info_get_added (GtkRecentInfo *info)
1625 g_return_val_if_fail (info != NULL, (time_t) -1);
1631 * gtk_recent_info_get_modified:
1632 * @info: a #GtkRecentInfo
1634 * Gets the timestamp (seconds from system's Epoch) when the resource
1635 * was last modified.
1637 * Return value: the number of seconds elapsed from system's Epoch when
1638 * the resource was last modified, or -1 on failure.
1643 gtk_recent_info_get_modified (GtkRecentInfo *info)
1645 g_return_val_if_fail (info != NULL, (time_t) -1);
1647 return info->modified;
1651 * gtk_recent_info_get_visited:
1652 * @info: a #GtkRecentInfo
1654 * Gets the timestamp (seconds from system's Epoch) when the resource
1657 * Return value: the number of seconds elapsed from system's Epoch when
1658 * the resource was last visited, or -1 on failure.
1663 gtk_recent_info_get_visited (GtkRecentInfo *info)
1665 g_return_val_if_fail (info != NULL, (time_t) -1);
1667 return info->visited;
1671 * gtk_recent_info_get_private_hint:
1672 * @info: a #GtkRecentInfo
1674 * Gets the value of the "private" flag. Resources in the recently used
1675 * list that have this flag set to %TRUE should only be displayed by the
1676 * applications that have registered them.
1678 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1683 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1685 g_return_val_if_fail (info != NULL, FALSE);
1687 return info->is_private;
1691 static RecentAppInfo *
1692 recent_app_info_new (const gchar *app_name)
1694 RecentAppInfo *app_info;
1696 g_assert (app_name != NULL);
1698 app_info = g_new0 (RecentAppInfo, 1);
1699 app_info->name = g_strdup (app_name);
1700 app_info->exec = NULL;
1701 app_info->count = 1;
1702 app_info->stamp = time (NULL);
1708 recent_app_info_free (RecentAppInfo *app_info)
1713 g_free (app_info->name);
1715 g_free (app_info->exec);
1721 * gtk_recent_info_get_application_info:
1722 * @info: a #GtkRecentInfo
1723 * @app_name: the name of the application that has registered this item
1724 * @app_exec: return location for the string containing the command line
1725 * @count: return location for the number of times this item was registered
1726 * @time_: return location for the timestamp this item was last registered
1727 * for this application
1729 * Gets the data regarding the application that has registered the resource
1732 * If the command line contains any escape characters defined inside the
1733 * storage specification, they will be expanded.
1735 * Return value: %TRUE if an application with @app_name has registered this
1736 * resource inside the recently used list, or %FALSE otherwise. You should
1737 * free the returned command line using g_free().
1742 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1743 const gchar *app_name,
1750 g_return_val_if_fail (info != NULL, FALSE);
1751 g_return_val_if_fail (app_name != NULL, FALSE);
1753 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1757 g_warning ("No registered application with name '%s' "
1758 "for item with URI '%s' found",
1765 *app_exec = ai->exec;
1777 * gtk_recent_info_get_applications:
1778 * @info: a #GtkRecentInfo
1779 * @length: return location for the length of the returned list, or %NULL
1781 * Retrieves the list of applications that have registered this resource.
1783 * Return value: a newly allocated %NULL-terminated array of strings.
1784 * Use g_strfreev() to free it.
1789 gtk_recent_info_get_applications (GtkRecentInfo *info,
1796 g_return_val_if_fail (info != NULL, NULL);
1798 if (!info->applications)
1806 n_apps = g_slist_length (info->applications);
1808 retval = g_new0 (gchar *, n_apps + 1);
1810 for (l = info->applications, i = 0;
1814 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1816 g_assert (ai != NULL);
1818 retval[i++] = g_strdup (ai->name);
1829 * gtk_recent_info_has_application:
1830 * @info: a #GtkRecentInfo
1831 * @app_name: a string containing an application name
1833 * Checks whether an application registered this resource using @app_name.
1835 * Return value: %TRUE if an application with name @app_name was found,
1841 gtk_recent_info_has_application (GtkRecentInfo *info,
1842 const gchar *app_name)
1844 g_return_val_if_fail (info != NULL, FALSE);
1845 g_return_val_if_fail (app_name != NULL, FALSE);
1847 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1851 * gtk_recent_info_last_application:
1852 * @info: a #GtkRecentInfo
1854 * Gets the name of the last application that have registered the
1855 * recently used resource represented by @info.
1857 * Return value: an application name. Use g_free() to free it.
1862 gtk_recent_info_last_application (GtkRecentInfo *info)
1865 time_t last_stamp = (time_t) -1;
1868 g_return_val_if_fail (info != NULL, NULL);
1870 for (l = info->applications; l != NULL; l = l->next)
1872 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1874 if (ai->stamp > last_stamp)
1877 last_stamp = ai->stamp;
1881 return g_strdup (name);
1891 icon_cache_element_free (IconCacheElement *element)
1893 if (element->pixbuf)
1894 g_object_unref (element->pixbuf);
1899 icon_theme_changed (GtkIconTheme *icon_theme)
1903 /* Difference from the initial creation is that we don't
1904 * reconnect the signal
1906 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1907 (GDestroyNotify)g_free,
1908 (GDestroyNotify)icon_cache_element_free);
1909 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1910 cache, (GDestroyNotify)g_hash_table_destroy);
1913 /* TODO: use the GtkFileChooser's icon cache instead of our own to reduce
1914 * the memory footprint
1917 get_cached_icon (const gchar *name,
1920 GtkIconTheme *icon_theme;
1922 IconCacheElement *element;
1924 icon_theme = gtk_icon_theme_get_default ();
1925 cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-recent-icon-cache");
1929 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1930 (GDestroyNotify)g_free,
1931 (GDestroyNotify)icon_cache_element_free);
1933 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1934 cache, (GDestroyNotify)g_hash_table_destroy);
1935 g_signal_connect (icon_theme, "changed",
1936 G_CALLBACK (icon_theme_changed), NULL);
1939 element = g_hash_table_lookup (cache, name);
1942 element = g_new0 (IconCacheElement, 1);
1943 g_hash_table_insert (cache, g_strdup (name), element);
1946 if (element->size != pixel_size)
1948 if (element->pixbuf)
1949 g_object_unref (element->pixbuf);
1951 element->size = pixel_size;
1952 element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
1953 pixel_size, 0, NULL);
1956 return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
1961 get_icon_for_mime_type (const char *mime_type,
1964 const char *separator;
1968 separator = strchr (mime_type, '/');
1970 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
1972 icon_name = g_string_new ("gnome-mime-");
1973 g_string_append_len (icon_name, mime_type, separator - mime_type);
1974 g_string_append_c (icon_name, '-');
1975 g_string_append (icon_name, separator + 1);
1976 pixbuf = get_cached_icon (icon_name->str, pixel_size);
1977 g_string_free (icon_name, TRUE);
1981 icon_name = g_string_new ("gnome-mime-");
1982 g_string_append_len (icon_name, mime_type, separator - mime_type);
1983 pixbuf = get_cached_icon (icon_name->str, pixel_size);
1984 g_string_free (icon_name, TRUE);
1990 get_icon_fallback (const gchar *icon_name,
1993 GtkIconTheme *icon_theme;
1996 icon_theme = gtk_icon_theme_get_default ();
1998 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
2000 GTK_ICON_LOOKUP_USE_BUILTIN,
2002 g_assert (retval != NULL);
2008 * gtk_recent_info_get_icon:
2009 * @info: a #GtkRecentInfo
2010 * @size: the size of the icon in pixels
2012 * Retrieves the icon of size @size associated to the resource MIME type.
2014 * Return value: a #GdkPixbuf containing the icon, or %NULL.
2019 gtk_recent_info_get_icon (GtkRecentInfo *info,
2022 GdkPixbuf *retval = NULL;
2024 g_return_val_if_fail (info != NULL, NULL);
2026 if (info->mime_type)
2027 retval = get_icon_for_mime_type (info->mime_type, size);
2029 /* this function should never fail */
2032 if (info->mime_type && strcmp (info->mime_type, "x-directory/normal") == 0)
2033 retval = get_icon_fallback (GTK_STOCK_DIRECTORY, size);
2035 retval = get_icon_fallback (GTK_STOCK_FILE, size);
2042 * gtk_recent_info_is_local:
2043 * @info: a #GtkRecentInfo
2045 * Checks whether the resource is local or not by looking at the
2046 * scheme of its URI.
2048 * Return value: %TRUE if the resource is local.
2053 gtk_recent_info_is_local (GtkRecentInfo *info)
2055 g_return_val_if_fail (info != NULL, FALSE);
2057 return has_case_prefix (info->uri, "file:/");
2061 * gtk_recent_info_exists:
2062 * @info: a #GtkRecentInfo
2064 * Checks whether the resource pointed by @info still exists. At
2065 * the moment this check is done only on resources pointing to local files.
2067 * Return value: %TRUE if the resource exists
2072 gtk_recent_info_exists (GtkRecentInfo *info)
2075 struct stat stat_buf;
2076 gboolean retval = FALSE;
2078 g_return_val_if_fail (info != NULL, FALSE);
2080 /* we guarantee only local resources */
2081 if (!gtk_recent_info_is_local (info))
2084 filename = g_filename_from_uri (info->uri, NULL, NULL);
2087 if (stat (filename, &stat_buf) == 0)
2097 * gtk_recent_info_match:
2098 * @info_a: a #GtkRecentInfo
2099 * @info_b: a #GtkRecentInfo
2101 * Checks whether two #GtkRecentInfo structures point to the same
2104 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2105 * resource, %FALSE otherwise.
2110 gtk_recent_info_match (GtkRecentInfo *info_a,
2111 GtkRecentInfo *info_b)
2113 g_return_val_if_fail (info_a != NULL, FALSE);
2114 g_return_val_if_fail (info_b != NULL, FALSE);
2116 return (0 == strcmp (info_a->uri, info_b->uri));
2119 /* taken from gnome-vfs-uri.c */
2120 static const gchar *
2121 get_method_string (const gchar *substring,
2122 gchar **method_string)
2128 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2135 !(p == substring + 1 && g_ascii_isalpha (*substring))
2139 /* Found toplevel method specification. */
2140 method = g_strndup (substring, p - substring);
2141 *method_string = g_ascii_strdown (method, -1);
2147 *method_string = g_strdup ("file");
2154 /* Stolen from gnome_vfs_make_valid_utf8() */
2156 make_valid_utf8 (const char *name)
2159 const char *remainder, *invalid;
2160 int remaining_bytes, valid_bytes;
2164 remaining_bytes = name ? strlen (name) : 0;
2166 while (remaining_bytes != 0)
2168 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2171 valid_bytes = invalid - remainder;
2174 string = g_string_sized_new (remaining_bytes);
2176 g_string_append_len (string, remainder, valid_bytes);
2177 g_string_append_c (string, '?');
2179 remaining_bytes -= valid_bytes + 1;
2180 remainder = invalid + 1;
2184 return g_strdup (name);
2186 g_string_append (string, remainder);
2187 g_assert (g_utf8_validate (string->str, -1, NULL));
2189 return g_string_free (string, FALSE);
2193 get_uri_shortname_for_display (const gchar *uri)
2196 gboolean validated = FALSE;
2198 if (has_case_prefix (uri, "file:/"))
2202 local_file = g_filename_from_uri (uri, NULL, NULL);
2206 name = g_filename_display_basename (local_file);
2210 g_free (local_file);
2219 rest = get_method_string (uri, &method);
2220 local_file = g_filename_display_basename (rest);
2222 name = g_strdup_printf ("%s: %s", method, local_file);
2224 g_free (local_file);
2228 g_assert (name != NULL);
2230 if (!validated && !g_utf8_validate (name, -1, NULL))
2234 utf8_name = make_valid_utf8 (name);
2244 * gtk_recent_info_get_short_name:
2245 * @info: an #GtkRecentInfo
2247 * Computes a valid UTF-8 string that can be used as the name of the item in a
2248 * menu or list. For example, calling this function on an item that refers to
2249 * "file:///foo/bar.txt" will yield "bar.txt".
2251 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2257 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2261 g_return_val_if_fail (info != NULL, NULL);
2263 if (info->uri == NULL)
2266 short_name = get_uri_shortname_for_display (info->uri);
2272 * gtk_recent_info_get_uri_display:
2273 * @info: a #GtkRecentInfo
2275 * Gets a displayable version of the resource's URI. If the resource
2276 * is local, it returns a local path; if the resource is not local,
2277 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2279 * Return value: a UTF-8 string containing the resource's URI or %NULL
2284 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2288 g_return_val_if_fail (info != NULL, NULL);
2291 if (gtk_recent_info_is_local (info))
2295 filename = g_filename_from_uri (info->uri, NULL, NULL);
2299 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2304 retval = make_valid_utf8 (info->uri);
2311 * gtk_recent_info_get_age:
2312 * @info: a #GtkRecentInfo
2314 * Gets the number of days elapsed since the last update of the resource
2317 * Return value: a positive integer containing the number of days elapsed
2318 * since the time this resource was last modified.
2323 gtk_recent_info_get_age (GtkRecentInfo *info)
2328 g_return_val_if_fail (info != NULL, -1);
2332 delta = now - info->modified;
2334 retval = (gint) (delta / (60 * 60 * 24));
2340 * gtk_recent_info_get_groups:
2341 * @info: a #GtkRecentInfo
2342 * @length: return location for the number of groups returned, or %NULL
2344 * Returns all groups registered for the recently used item @info. The
2345 * array of returned group names will be %NULL terminated, so length might
2346 * optionally be %NULL.
2348 * Return value: a newly allocated %NULL terminated array of strings. Use
2349 * g_strfreev() to free it.
2354 gtk_recent_info_get_groups (GtkRecentInfo *info,
2361 g_return_val_if_fail (info != NULL, NULL);
2371 n_groups = g_slist_length (info->groups);
2373 retval = g_new0 (gchar *, n_groups + 1);
2375 for (l = info->groups, i = 0;
2379 gchar *group_name = (gchar *) l->data;
2381 g_assert (group_name != NULL);
2383 retval[i++] = g_strdup (group_name);
2394 * gtk_recent_info_has_group:
2395 * @info: a #GtkRecentInfo
2396 * @group_name: name of a group
2398 * Checks whether @group_name appears inside the groups registered for the
2399 * recently used item @info.
2401 * Return value: %TRUE if the group was found.
2406 gtk_recent_info_has_group (GtkRecentInfo *info,
2407 const gchar *group_name)
2411 g_return_val_if_fail (info != NULL, FALSE);
2412 g_return_val_if_fail (group_name != NULL, FALSE);
2417 for (l = info->groups; l != NULL; l = l->next)
2419 gchar *g = (gchar *) l->data;
2421 if (strcmp (g, group_name) == 0)
2429 * _gtk_recent_manager_sync:
2431 * Private function for synchronising the recent manager singleton.
2434 _gtk_recent_manager_sync (void)
2436 if (recent_manager_singleton)
2438 /* force a dump of the contents of the recent manager singleton */
2439 recent_manager_singleton->priv->is_dirty = TRUE;
2440 gtk_recent_manager_real_changed (recent_manager_singleton);
2444 #define __GTK_RECENT_MANAGER_C__
2445 #include "gtkaliasdef.c"