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 /* a poll approximately every five seconds */
55 /* return all items by default */
56 #define DEFAULT_LIMIT -1
58 /* keep in sync with xdgmime */
59 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
65 GDestroyNotify notify;
92 GHashTable *apps_lookup;
103 struct _GtkRecentManagerPrivate
108 guint write_in_progress : 1;
109 guint read_in_progress : 1;
114 GBookmarkFile *recent_items;
129 static void gtk_recent_manager_dispose (GObject *object);
130 static void gtk_recent_manager_finalize (GObject *object);
132 static void gtk_recent_manager_set_property (GObject *object,
136 static void gtk_recent_manager_get_property (GObject *object,
140 static void gtk_recent_manager_add_item_query_info_cb (GObject *source_object,
143 static void gtk_recent_manager_changed (GtkRecentManager *manager);
145 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
146 static gboolean gtk_recent_manager_poll_timeout (gpointer data);
147 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
148 const gchar *filename);
150 static void build_recent_items_list (GtkRecentManager *manager);
151 static void purge_recent_items_list (GtkRecentManager *manager,
154 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
155 static void recent_app_info_free (RecentAppInfo *app_info);
157 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
158 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
160 static guint signal_changed = 0;
162 static GtkRecentManager *recent_manager_singleton = NULL;
164 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
167 filename_warning (const gchar *format,
168 const gchar *filename,
169 const gchar *message)
171 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
172 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
176 /* Test of haystack has the needle prefix, comparing case
177 * insensitive. haystack may be UTF-8, but needle must
178 * contain only lowercase ascii. */
180 has_case_prefix (const gchar *haystack,
185 /* Eat one character at a time. */
189 while (*n && *h && *n == g_ascii_tolower (*h))
199 gtk_recent_manager_error_quark (void)
201 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
205 threads_dispatch (gpointer data)
207 ThreadsDispatch *dispatch = data;
208 gboolean res = FALSE;
210 GDK_THREADS_ENTER ();
212 if (!g_source_is_destroyed (g_main_current_source ()))
213 res = dispatch->func (dispatch->data);
215 GDK_THREADS_LEAVE ();
221 threads_free (gpointer data)
223 ThreadsDispatch *dispatch = data;
225 if (dispatch->notify)
226 dispatch->notify (dispatch->data);
228 g_slice_free (ThreadsDispatch, dispatch);
232 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
234 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
236 gtk_recent_manager_parent_class = g_type_class_peek_parent (klass);
238 gobject_class->set_property = gtk_recent_manager_set_property;
239 gobject_class->get_property = gtk_recent_manager_get_property;
240 gobject_class->dispose = gtk_recent_manager_dispose;
241 gobject_class->finalize = gtk_recent_manager_finalize;
244 * GtkRecentManager:filename
246 * The full path to the file to be used to store and read the recently
247 * used resources list
251 g_object_class_install_property (gobject_class,
253 g_param_spec_string ("filename",
255 P_("The full path to the file to be used to store and read the list"),
257 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE)));
259 * GtkRecentManager:limit
261 * The maximum number of items to be returned by the
262 * gtk_recent_manager_get_items() function.
266 g_object_class_install_property (gobject_class,
268 g_param_spec_int ("limit",
270 P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
276 * GtkRecentManager:size
278 * The size of the recently used resources list.
282 g_object_class_install_property (gobject_class,
284 g_param_spec_int ("size",
286 P_("The size of the recently used resources list"),
293 * GtkRecentManager::changed
294 * @recent_manager: the recent manager
296 * Emitted when the current recently used resources manager changes its
302 g_signal_new (I_("changed"),
303 G_TYPE_FROM_CLASS (klass),
305 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
307 g_cclosure_marshal_VOID__VOID,
310 klass->changed = gtk_recent_manager_real_changed;
312 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
316 gtk_recent_manager_init (GtkRecentManager *manager)
318 GtkRecentManagerPrivate *priv;
319 ThreadsDispatch *dispatch;
321 priv = g_type_instance_get_private ((GTypeInstance *) manager,
322 GTK_TYPE_RECENT_MANAGER);
323 manager->priv = priv;
325 priv->limit = DEFAULT_LIMIT;
328 priv->is_dirty = FALSE;
329 priv->write_in_progress = FALSE;
330 priv->read_in_progress = FALSE;
332 priv->filename = g_build_filename (g_get_home_dir (),
333 GTK_RECENTLY_USED_FILE,
336 dispatch = g_slice_new (ThreadsDispatch);
337 dispatch->func = gtk_recent_manager_poll_timeout;
338 dispatch->data = manager;
339 dispatch->notify = NULL;
340 priv->poll_timeout = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT + 30,
346 build_recent_items_list (manager);
350 gtk_recent_manager_set_property (GObject *object,
355 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
360 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
363 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372 gtk_recent_manager_get_property (GObject *object,
377 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
382 g_value_set_string (value, recent_manager->priv->filename);
385 g_value_set_int (value, recent_manager->priv->limit);
388 g_value_set_int (value, recent_manager->priv->size);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 gtk_recent_manager_dispose (GObject *object)
399 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
400 GtkRecentManagerPrivate *priv = manager->priv;
402 if (priv->poll_timeout)
404 g_source_remove (priv->poll_timeout);
405 priv->poll_timeout = 0;
408 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (object);
412 gtk_recent_manager_finalize (GObject *object)
414 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
415 GtkRecentManagerPrivate *priv = manager->priv;
417 g_free (priv->filename);
419 if (priv->recent_items)
420 g_bookmark_file_free (priv->recent_items);
422 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
426 gtk_recent_manager_real_changed (GtkRecentManager *manager)
428 GtkRecentManagerPrivate *priv = manager->priv;
430 g_object_freeze_notify (G_OBJECT (manager));
435 struct stat stat_buf;
437 /* we are marked as dirty, so we dump the content of our
438 * recently used items list
440 g_assert (priv->filename != NULL);
442 priv->write_in_progress = TRUE;
444 /* if no container object has been defined, we create a new
445 * empty container, and dump it
447 if (!priv->recent_items)
449 priv->recent_items = g_bookmark_file_new ();
454 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
457 filename_warning ("Attempting to store changes into `%s', "
460 write_error->message);
461 g_error_free (write_error);
464 priv->write_in_progress = FALSE;
466 /* we have sync'ed our list with the storage file, so we
467 * update the file mtime in order to skip the timed check
468 * and spare us from a re-read.
470 if (g_stat (priv->filename, &stat_buf) < 0)
472 filename_warning ("Unable to stat() the recently used resources file "
477 g_object_thaw_notify (G_OBJECT (manager));
482 priv->last_mtime = stat_buf.st_mtime;
484 /* mark us as clean */
485 priv->is_dirty = FALSE;
489 /* we are not marked as dirty, so we have been called
490 * because the recently used resources file has been
491 * changed (and not from us).
493 build_recent_items_list (manager);
496 g_object_thaw_notify (G_OBJECT (manager));
499 /* timed poll()-ing of the recently used resources file.
500 * an event-based system would be more efficient.
503 gtk_recent_manager_poll_timeout (gpointer data)
505 GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
506 GtkRecentManagerPrivate *priv = manager->priv;
507 struct stat stat_buf;
510 /* wait for the next timeout if we have a read/write in progress */
511 if (priv->write_in_progress || priv->read_in_progress)
514 stat_res = g_stat (priv->filename, &stat_buf);
517 /* the file does not exist, yet, so we wait */
521 filename_warning ("Unable to stat() the recently used resources file "
529 /* the file didn't change from the last poll, so we bail out */
530 if (stat_buf.st_mtime == priv->last_mtime)
533 /* the file has been changed, hence we emit the "changed" signal */
534 gtk_recent_manager_changed (manager);
540 gtk_recent_manager_set_filename (GtkRecentManager *manager,
541 const gchar *filename)
543 GtkRecentManagerPrivate *priv;
544 ThreadsDispatch *dispatch;
546 g_assert (GTK_IS_RECENT_MANAGER (manager));
547 priv = manager->priv;
549 if (!filename || filename[0] == '\0')
552 g_free (manager->priv->filename);
554 if (manager->priv->poll_timeout)
556 g_source_remove (manager->priv->poll_timeout);
557 manager->priv->poll_timeout = 0;
560 priv->filename = g_strdup (filename);
562 dispatch = g_slice_new (ThreadsDispatch);
563 dispatch->func = gtk_recent_manager_poll_timeout;
564 dispatch->data = manager;
565 dispatch->notify = NULL;
566 priv->poll_timeout = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT + 30,
572 /* mark us clean, so that we can re-read the list
573 * of recently used resources
575 priv->is_dirty = FALSE;
576 build_recent_items_list (manager);
579 /* reads the recently used resources file and builds the items list.
580 * we keep the items list inside the parser object, and build the
581 * RecentInfo object only on user's demand to avoid useless replication.
582 * this function resets the dirty bit of the manager.
585 build_recent_items_list (GtkRecentManager *manager)
587 GtkRecentManagerPrivate *priv;
588 struct stat stat_buf;
594 priv = manager->priv;
595 g_assert (priv->filename != NULL);
597 if (!priv->recent_items)
599 priv->recent_items = g_bookmark_file_new ();
603 stat_res = g_stat (priv->filename, &stat_buf);
606 /* the file doesn't exists, so we bail out and wait for the first
614 filename_warning ("Attempting to read the recently used resources file "
615 "at `%s', but an error occurred: %s. Aborting.",
623 /* record the last mtime, for later use */
624 priv->last_mtime = stat_buf.st_mtime;
626 priv->read_in_progress = TRUE;
628 /* the file exists, and it's valid (we hope); if not, destroy the container
629 * object and hope for a better result when the next "changed" signal is
632 res = g_bookmark_file_load_from_file (priv->recent_items,
637 filename_warning ("Attempting to read the recently used resources file "
638 "at `%s', but the parser failed: %s.",
640 read_error->message);
642 g_bookmark_file_free (priv->recent_items);
643 priv->recent_items = NULL;
645 g_error_free (read_error);
648 size = g_bookmark_file_get_size (priv->recent_items);
649 if (priv->size != size)
653 g_object_notify (G_OBJECT (manager), "size");
656 priv->read_in_progress = FALSE;
657 priv->is_dirty = FALSE;
661 /********************
663 ********************/
667 * gtk_recent_manager_new:
669 * Creates a new recent manager object. Recent manager objects are used to
670 * handle the list of recently used resources. A #GtkRecentManager object
671 * monitors the recently used resources list, and emits the "changed" signal
672 * each time something inside the list changes.
674 * #GtkRecentManager objects are expensive: be sure to create them only when
675 * needed. You should use gtk_recent_manager_get_default() instead.
677 * Return value: A newly created #GtkRecentManager object.
682 gtk_recent_manager_new (void)
684 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
688 * gtk_recent_manager_get_default:
690 * Gets a unique instance of #GtkRecentManager, that you can share
691 * in your application without caring about memory management. The
692 * returned instance will be freed when you application terminates.
694 * Return value: A unique #GtkRecentManager. Do not ref or unref it.
699 gtk_recent_manager_get_default (void)
701 if (G_UNLIKELY (!recent_manager_singleton))
702 recent_manager_singleton = gtk_recent_manager_new ();
704 return recent_manager_singleton;
708 * gtk_recent_manager_get_for_screen:
709 * @screen: a #GdkScreen
711 * Gets the recent manager object associated with @screen; if this
712 * function has not previously been called for the given screen,
713 * a new recent manager object will be created and associated with
714 * the screen. Recent manager objects are fairly expensive to create,
715 * so using this function is usually a better choice than calling
716 * gtk_recent_manager_new() and setting the screen yourself; by using
717 * this function a single recent manager object will be shared between
720 * Return value: A unique #GtkRecentManager associated with the given
721 * screen. This recent manager is associated to the with the screen
722 * and can be used as long as the screen is open. Do not ref or
725 * Deprecated: 2.12: This function has been deprecated and should
726 * not be used in newly written code. Calling this function is
727 * equivalent to calling gtk_recent_manager_get_default().
732 gtk_recent_manager_get_for_screen (GdkScreen *screen)
734 return gtk_recent_manager_get_default ();
738 * gtk_recent_manager_set_screen:
739 * @manager: a #GtkRecentManager
740 * @screen: a #GdkScreen
742 * Sets the screen for a recent manager; the screen is used to
743 * track the user's currently configured recently used documents
748 * Deprecated: 2.12: This function has been deprecated and should
749 * not be used in newly written code. Calling this function has
753 gtk_recent_manager_set_screen (GtkRecentManager *manager,
760 * gtk_recent_manager_set_limit:
761 * @manager: a #GtkRecentManager
762 * @limit: the maximum number of items to return, or -1.
764 * Sets the maximum number of item that the gtk_recent_manager_get_items()
765 * function should return. If @limit is set to -1, then return all the
771 gtk_recent_manager_set_limit (GtkRecentManager *manager,
774 GtkRecentManagerPrivate *priv;
776 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
778 priv = manager->priv;
783 * gtk_recent_manager_get_limit:
784 * @manager: a #GtkRecentManager
786 * Gets the maximum number of items that the gtk_recent_manager_get_items()
787 * function should return.
789 * Return value: the number of items to return, or -1 for every item.
794 gtk_recent_manager_get_limit (GtkRecentManager *manager)
796 GtkRecentManagerPrivate *priv;
798 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
800 priv = manager->priv;
805 gtk_recent_manager_add_item_query_info_cb (GObject *source_object,
809 GFile *file = G_FILE (source_object);
810 GtkRecentManager *manager = user_data;
811 GtkRecentData recent_data;
812 GFileInfo *file_info;
816 uri = g_file_get_uri (file);
819 file_info = g_file_query_info_finish (file, res, &error);
822 g_warning ("Unable to retrieve the file info for `%s': %s",
825 g_error_free (error);
829 recent_data.display_name = NULL;
830 recent_data.description = NULL;
834 recent_data.mime_type = g_content_type_get_mime_type (g_file_info_get_content_type (file_info));
835 g_object_unref (file_info);
838 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
840 recent_data.app_name = g_strdup (g_get_application_name ());
841 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
842 recent_data.groups = NULL;
843 recent_data.is_private = FALSE;
845 /* Ignore return value, this can't fail anyway since all required
847 gtk_recent_manager_add_full (manager, uri, &recent_data);
849 g_free (recent_data.mime_type);
850 g_free (recent_data.app_name);
851 g_free (recent_data.app_exec);
854 g_object_unref (manager);
859 * gtk_recent_manager_add_item:
860 * @manager: a #GtkRecentManager
863 * Adds a new resource, pointed by @uri, into the recently used
866 * This function automatically retrieves some of the needed
867 * metadata and setting other metadata to common default values; it
868 * then feeds the data to gtk_recent_manager_add_full().
870 * See gtk_recent_manager_add_full() if you want to explicitly
871 * define the metadata for the resource pointed by @uri.
873 * Return value: %TRUE if the new item was successfully added
874 * to the recently used resources list
879 gtk_recent_manager_add_item (GtkRecentManager *manager,
884 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
885 g_return_val_if_fail (uri != NULL, FALSE);
887 file = g_file_new_for_uri (uri);
889 g_file_query_info_async (file,
890 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
892 G_FILE_QUERY_INFO_NONE,
894 gtk_recent_manager_add_item_query_info_cb,
895 g_object_ref (manager));
897 g_object_unref (file);
903 * gtk_recent_manager_add_full:
904 * @manager: a #GtkRecentManager
906 * @recent_data: metadata of the resource
908 * Adds a new resource, pointed by @uri, into the recently used
909 * resources list, using the metadata specified inside the #GtkRecentData
910 * structure passed in @recent_data.
912 * The passed URI will be used to identify this resource inside the
915 * In order to register the new recently used resource, metadata about
916 * the resource must be passed as well as the URI; the metadata is
917 * stored in a #GtkRecentData structure, which must contain the MIME
918 * type of the resource pointed by the URI; the name of the application
919 * that is registering the item, and a command line to be used when
920 * launching the item.
922 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
923 * to be used when viewing the item instead of the last component of the
924 * URI; a short description of the item; whether the item should be
925 * considered private - that is, should be displayed only by the
926 * applications that have registered it.
928 * Return value: %TRUE if the new item was successfully added to the
929 * recently used resources list, %FALSE otherwise.
934 gtk_recent_manager_add_full (GtkRecentManager *manager,
936 const GtkRecentData *data)
938 GtkRecentManagerPrivate *priv;
940 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
941 g_return_val_if_fail (uri != NULL, FALSE);
942 g_return_val_if_fail (data != NULL, FALSE);
945 if ((data->display_name) &&
946 (!g_utf8_validate (data->display_name, -1, NULL)))
948 g_warning ("Attempting to add `%s' to the list of recently used "
949 "resources, but the display name is not a valid UTF-8 "
955 if ((data->description) &&
956 (!g_utf8_validate (data->description, -1, NULL)))
958 g_warning ("Attempting to add `%s' to the list of recently used "
959 "resources, but the description is not a valid UTF-8 "
966 if (!data->mime_type)
968 g_warning ("Attempting to add `%s' to the list of recently used "
969 "resources, but not MIME type was defined",
976 g_warning ("Attempting to add `%s' to the list of recently used "
977 "resources, but no name of the application that is "
978 "registering it was defined",
985 g_warning ("Attempting to add `%s' to the list of recently used "
986 "resources, but no command line for the application "
987 "that is registering it was defined",
992 priv = manager->priv;
994 if (!priv->recent_items)
996 priv->recent_items = g_bookmark_file_new ();
1000 if (data->display_name)
1001 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
1003 if (data->description)
1004 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
1006 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
1008 if (data->groups && data->groups[0] != '\0')
1012 for (j = 0; (data->groups)[j] != NULL; j++)
1013 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
1016 /* register the application; this will take care of updating the
1017 * registration count and time in case the application has
1018 * already registered the same document inside the list
1020 g_bookmark_file_add_application (priv->recent_items, uri,
1024 g_bookmark_file_set_is_private (priv->recent_items, uri,
1027 /* mark us as dirty, so that when emitting the "changed" signal we
1028 * will dump our changes
1030 priv->is_dirty = TRUE;
1032 gtk_recent_manager_changed (manager);
1038 * gtk_recent_manager_remove_item:
1039 * @manager: a #GtkRecentManager
1040 * @uri: the URI of the item you wish to remove
1041 * @error: return location for a #GError, or %NULL
1043 * Removes a resource pointed by @uri from the recently used resources
1044 * list handled by a recent manager.
1046 * Return value: %TRUE if the item pointed by @uri has been successfully
1047 * removed by the recently used resources list, and %FALSE otherwise.
1052 gtk_recent_manager_remove_item (GtkRecentManager *manager,
1056 GtkRecentManagerPrivate *priv;
1057 GError *remove_error = NULL;
1059 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1060 g_return_val_if_fail (uri != NULL, FALSE);
1061 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1063 priv = manager->priv;
1065 if (!priv->recent_items)
1067 priv->recent_items = g_bookmark_file_new ();
1070 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1071 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1072 _("Unable to find an item with URI '%s'"),
1078 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1081 g_propagate_error (error, remove_error);
1086 priv->is_dirty = TRUE;
1088 gtk_recent_manager_changed (manager);
1094 * gtk_recent_manager_has_item:
1095 * @manager: a #GtkRecentManager
1098 * Checks whether there is a recently used resource registered
1099 * with @uri inside the recent manager.
1101 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1106 gtk_recent_manager_has_item (GtkRecentManager *manager,
1109 GtkRecentManagerPrivate *priv;
1111 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1112 g_return_val_if_fail (uri != NULL, FALSE);
1114 priv = manager->priv;
1115 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1117 return g_bookmark_file_has_item (priv->recent_items, uri);
1121 build_recent_info (GBookmarkFile *bookmarks,
1122 GtkRecentInfo *info)
1124 gchar **apps, **groups;
1125 gsize apps_len, groups_len, i;
1127 g_assert (bookmarks != NULL);
1128 g_assert (info != NULL);
1130 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1131 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1132 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1134 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1136 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1137 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1138 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1140 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1141 for (i = 0; i < groups_len; i++)
1143 gchar *group_name = g_strdup (groups[i]);
1145 info->groups = g_slist_append (info->groups, group_name);
1148 g_strfreev (groups);
1150 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1151 for (i = 0; i < apps_len; i++)
1153 gchar *app_name, *app_exec;
1156 RecentAppInfo *app_info;
1161 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1169 app_info = recent_app_info_new (app_name);
1170 app_info->exec = app_exec;
1171 app_info->count = count;
1172 app_info->stamp = stamp;
1174 info->applications = g_slist_prepend (info->applications, app_info);
1175 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1182 * gtk_recent_manager_lookup_item:
1183 * @manager: a #GtkRecentManager
1185 * @error: a return location for a #GError, or %NULL
1187 * Searches for a URI inside the recently used resources list, and
1188 * returns a structure containing informations about the resource
1189 * like its MIME type, or its display name.
1191 * Return value: a #GtkRecentInfo structure containing information
1192 * about the resource pointed by @uri, or %NULL if the URI was
1193 * not registered in the recently used resources list. Free with
1194 * gtk_recent_info_unref().
1199 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1203 GtkRecentManagerPrivate *priv;
1204 GtkRecentInfo *info = NULL;
1206 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1207 g_return_val_if_fail (uri != NULL, NULL);
1208 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1210 priv = manager->priv;
1211 if (!priv->recent_items)
1213 priv->recent_items = g_bookmark_file_new ();
1216 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1217 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1218 _("Unable to find an item with URI '%s'"),
1224 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1226 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1227 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1228 _("Unable to find an item with URI '%s'"),
1233 info = gtk_recent_info_new (uri);
1234 g_return_val_if_fail (info != NULL, NULL);
1236 /* fill the RecentInfo structure with the data retrieved by our
1237 * parser object from the storage file
1239 build_recent_info (priv->recent_items, info);
1245 * gtk_recent_manager_move_item:
1246 * @manager: a #GtkRecentManager
1247 * @uri: the URI of a recently used resource
1248 * @new_uri: the new URI of the recently used resource, or %NULL to
1249 * remove the item pointed by @uri in the list
1250 * @error: a return location for a #GError, or %NULL
1252 * Changes the location of a recently used resource from @uri to @new_uri.
1254 * Please note that this function will not affect the resource pointed
1255 * by the URIs, but only the URI used in the recently used resources list.
1257 * Return value: %TRUE on success.
1262 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1264 const gchar *new_uri,
1267 GtkRecentManagerPrivate *priv;
1271 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1272 g_return_val_if_fail (uri != NULL, FALSE);
1273 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1275 priv = recent_manager->priv;
1277 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1279 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1280 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1281 _("Unable to find an item with URI '%s'"),
1287 res = g_bookmark_file_move_item (priv->recent_items,
1292 g_propagate_error (error, move_error);
1296 priv->is_dirty = TRUE;
1298 gtk_recent_manager_changed (recent_manager);
1304 * gtk_recent_manager_get_items:
1305 * @manager: a #GtkRecentManager
1307 * Gets the list of recently used resources.
1309 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1310 * gtk_recent_info_unref() on each item inside the list, and then
1311 * free the list itself using g_list_free().
1316 gtk_recent_manager_get_items (GtkRecentManager *manager)
1318 GtkRecentManagerPrivate *priv;
1319 GList *retval = NULL;
1323 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1325 priv = manager->priv;
1326 if (!priv->recent_items)
1329 if (priv->limit == 0)
1332 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1333 for (i = 0; i < uris_len; i++)
1335 GtkRecentInfo *info;
1337 if (priv->limit != -1 && i == priv->limit)
1340 info = gtk_recent_info_new (uris[i]);
1341 build_recent_info (priv->recent_items, info);
1343 retval = g_list_prepend (retval, info);
1352 purge_recent_items_list (GtkRecentManager *manager,
1355 GtkRecentManagerPrivate *priv = manager->priv;
1357 if (!priv->recent_items)
1360 g_bookmark_file_free (priv->recent_items);
1361 priv->recent_items = NULL;
1363 priv->recent_items = g_bookmark_file_new ();
1365 priv->is_dirty = TRUE;
1367 /* emit the changed signal, to ensure that the purge is written */
1368 gtk_recent_manager_changed (manager);
1372 * gtk_recent_manager_purge_items:
1373 * @manager: a #GtkRecentManager
1374 * @error: a return location for a #GError, or %NULL
1376 * Purges every item from the recently used resources list.
1378 * Return value: the number of items that have been removed from the
1379 * recently used resources list.
1384 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1387 GtkRecentManagerPrivate *priv;
1390 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1392 priv = manager->priv;
1393 if (!priv->recent_items)
1396 count = g_bookmark_file_get_size (priv->recent_items);
1400 purge_recent_items_list (manager, error);
1402 purged = count - g_bookmark_file_get_size (priv->recent_items);
1408 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1410 g_signal_emit (recent_manager, signal_changed, 0);
1418 gtk_recent_info_get_type (void)
1420 static GType info_type = 0;
1423 info_type = g_boxed_type_register_static (I_("GtkRecentInfo"),
1424 (GBoxedCopyFunc) gtk_recent_info_ref,
1425 (GBoxedFreeFunc) gtk_recent_info_unref);
1429 static GtkRecentInfo *
1430 gtk_recent_info_new (const gchar *uri)
1432 GtkRecentInfo *info;
1434 g_assert (uri != NULL);
1436 info = g_new0 (GtkRecentInfo, 1);
1437 info->uri = g_strdup (uri);
1439 info->applications = NULL;
1440 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1442 info->groups = NULL;
1444 info->ref_count = 1;
1450 gtk_recent_info_free (GtkRecentInfo *recent_info)
1455 g_free (recent_info->uri);
1456 g_free (recent_info->display_name);
1457 g_free (recent_info->description);
1458 g_free (recent_info->mime_type);
1460 if (recent_info->applications)
1462 g_slist_foreach (recent_info->applications,
1463 (GFunc) recent_app_info_free,
1465 g_slist_free (recent_info->applications);
1467 recent_info->applications = NULL;
1470 if (recent_info->apps_lookup)
1471 g_hash_table_destroy (recent_info->apps_lookup);
1473 if (recent_info->groups)
1475 g_slist_foreach (recent_info->groups,
1478 g_slist_free (recent_info->groups);
1480 recent_info->groups = NULL;
1483 if (recent_info->icon)
1484 g_object_unref (recent_info->icon);
1486 g_free (recent_info);
1490 * gtk_recent_info_ref:
1491 * @info: a #GtkRecentInfo
1493 * Increases the reference count of @recent_info by one.
1495 * Return value: the recent info object with its reference count increased
1501 gtk_recent_info_ref (GtkRecentInfo *info)
1503 g_return_val_if_fail (info != NULL, NULL);
1504 g_return_val_if_fail (info->ref_count > 0, NULL);
1506 info->ref_count += 1;
1512 * gtk_recent_info_unref:
1513 * @info: a #GtkRecentInfo
1515 * Decreases the reference count of @info by one. If the reference
1516 * count reaches zero, @info is deallocated, and the memory freed.
1521 gtk_recent_info_unref (GtkRecentInfo *info)
1523 g_return_if_fail (info != NULL);
1524 g_return_if_fail (info->ref_count > 0);
1526 info->ref_count -= 1;
1528 if (info->ref_count == 0)
1529 gtk_recent_info_free (info);
1533 * gtk_recent_info_get_uri:
1534 * @info: a #GtkRecentInfo
1536 * Gets the URI of the resource.
1538 * Return value: the URI of the resource. The returned string is
1539 * owned by the recent manager, and should not be freed.
1543 G_CONST_RETURN gchar *
1544 gtk_recent_info_get_uri (GtkRecentInfo *info)
1546 g_return_val_if_fail (info != NULL, NULL);
1552 * gtk_recent_info_get_display_name:
1553 * @info: a #GtkRecentInfo
1555 * Gets the name of the resource. If none has been defined, the basename
1556 * of the resource is obtained.
1558 * Return value: the display name of the resource. The returned string
1559 * is owned by the recent manager, and should not be freed.
1563 G_CONST_RETURN gchar *
1564 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1566 g_return_val_if_fail (info != NULL, NULL);
1568 if (!info->display_name)
1569 info->display_name = gtk_recent_info_get_short_name (info);
1571 return info->display_name;
1575 * gtk_recent_info_get_description:
1576 * @info: a #GtkRecentInfo
1578 * Gets the (short) description of the resource.
1580 * Return value: the description of the resource. The returned string
1581 * is owned by the recent manager, and should not be freed.
1585 G_CONST_RETURN gchar *
1586 gtk_recent_info_get_description (GtkRecentInfo *info)
1588 g_return_val_if_fail (info != NULL, NULL);
1590 return info->description;
1594 * gtk_recent_info_get_mime_type:
1595 * @info: a #GtkRecentInfo
1597 * Gets the MIME type of the resource.
1599 * Return value: the MIME type of the resource. The returned string
1600 * is owned by the recent manager, and should not be freed.
1604 G_CONST_RETURN gchar *
1605 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1607 g_return_val_if_fail (info != NULL, NULL);
1609 if (!info->mime_type)
1610 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1612 return info->mime_type;
1616 * gtk_recent_info_get_added:
1617 * @info: a #GtkRecentInfo
1619 * Gets the timestamp (seconds from system's Epoch) when the resource
1620 * was added to the recently used resources list.
1622 * Return value: the number of seconds elapsed from system's Epoch when
1623 * the resource was added to the list, or -1 on failure.
1628 gtk_recent_info_get_added (GtkRecentInfo *info)
1630 g_return_val_if_fail (info != NULL, (time_t) -1);
1636 * gtk_recent_info_get_modified:
1637 * @info: a #GtkRecentInfo
1639 * Gets the timestamp (seconds from system's Epoch) when the resource
1640 * was last modified.
1642 * Return value: the number of seconds elapsed from system's Epoch when
1643 * the resource was last modified, or -1 on failure.
1648 gtk_recent_info_get_modified (GtkRecentInfo *info)
1650 g_return_val_if_fail (info != NULL, (time_t) -1);
1652 return info->modified;
1656 * gtk_recent_info_get_visited:
1657 * @info: a #GtkRecentInfo
1659 * Gets the timestamp (seconds from system's Epoch) when the resource
1662 * Return value: the number of seconds elapsed from system's Epoch when
1663 * the resource was last visited, or -1 on failure.
1668 gtk_recent_info_get_visited (GtkRecentInfo *info)
1670 g_return_val_if_fail (info != NULL, (time_t) -1);
1672 return info->visited;
1676 * gtk_recent_info_get_private_hint:
1677 * @info: a #GtkRecentInfo
1679 * Gets the value of the "private" flag. Resources in the recently used
1680 * list that have this flag set to %TRUE should only be displayed by the
1681 * applications that have registered them.
1683 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1688 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1690 g_return_val_if_fail (info != NULL, FALSE);
1692 return info->is_private;
1696 static RecentAppInfo *
1697 recent_app_info_new (const gchar *app_name)
1699 RecentAppInfo *app_info;
1701 g_assert (app_name != NULL);
1703 app_info = g_new0 (RecentAppInfo, 1);
1704 app_info->name = g_strdup (app_name);
1705 app_info->exec = NULL;
1706 app_info->count = 1;
1707 app_info->stamp = time (NULL);
1713 recent_app_info_free (RecentAppInfo *app_info)
1718 g_free (app_info->name);
1720 g_free (app_info->exec);
1726 * gtk_recent_info_get_application_info:
1727 * @info: a #GtkRecentInfo
1728 * @app_name: the name of the application that has registered this item
1729 * @app_exec: return location for the string containing the command line
1730 * @count: return location for the number of times this item was registered
1731 * @time_: return location for the timestamp this item was last registered
1732 * for this application
1734 * Gets the data regarding the application that has registered the resource
1737 * If the command line contains any escape characters defined inside the
1738 * storage specification, they will be expanded.
1740 * Return value: %TRUE if an application with @app_name has registered this
1741 * resource inside the recently used list, or %FALSE otherwise. You should
1742 * free the returned command line using g_free().
1747 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1748 const gchar *app_name,
1755 g_return_val_if_fail (info != NULL, FALSE);
1756 g_return_val_if_fail (app_name != NULL, FALSE);
1758 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1762 g_warning ("No registered application with name '%s' "
1763 "for item with URI '%s' found",
1770 *app_exec = ai->exec;
1782 * gtk_recent_info_get_applications:
1783 * @info: a #GtkRecentInfo
1784 * @length: return location for the length of the returned list, or %NULL
1786 * Retrieves the list of applications that have registered this resource.
1788 * Return value: a newly allocated %NULL-terminated array of strings.
1789 * Use g_strfreev() to free it.
1794 gtk_recent_info_get_applications (GtkRecentInfo *info,
1801 g_return_val_if_fail (info != NULL, NULL);
1803 if (!info->applications)
1811 n_apps = g_slist_length (info->applications);
1813 retval = g_new0 (gchar *, n_apps + 1);
1815 for (l = info->applications, i = 0;
1819 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1821 g_assert (ai != NULL);
1823 retval[i++] = g_strdup (ai->name);
1834 * gtk_recent_info_has_application:
1835 * @info: a #GtkRecentInfo
1836 * @app_name: a string containing an application name
1838 * Checks whether an application registered this resource using @app_name.
1840 * Return value: %TRUE if an application with name @app_name was found,
1846 gtk_recent_info_has_application (GtkRecentInfo *info,
1847 const gchar *app_name)
1849 g_return_val_if_fail (info != NULL, FALSE);
1850 g_return_val_if_fail (app_name != NULL, FALSE);
1852 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1856 * gtk_recent_info_last_application:
1857 * @info: a #GtkRecentInfo
1859 * Gets the name of the last application that have registered the
1860 * recently used resource represented by @info.
1862 * Return value: an application name. Use g_free() to free it.
1867 gtk_recent_info_last_application (GtkRecentInfo *info)
1870 time_t last_stamp = (time_t) -1;
1873 g_return_val_if_fail (info != NULL, NULL);
1875 for (l = info->applications; l != NULL; l = l->next)
1877 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1879 if (ai->stamp > last_stamp)
1882 last_stamp = ai->stamp;
1886 return g_strdup (name);
1890 get_icon_for_mime_type (const char *mime_type,
1893 GtkIconTheme *icon_theme;
1894 const char *separator;
1898 separator = strchr (mime_type, '/');
1902 icon_theme = gtk_icon_theme_get_default ();
1904 /* try with the three icon name variants for MIME types */
1906 /* canonicalize MIME type: foo/x-bar -> foo-x-bar */
1907 icon_name = g_string_new (NULL);
1908 g_string_append_len (icon_name, mime_type, separator - mime_type);
1909 g_string_append_c (icon_name, '-');
1910 g_string_append (icon_name, separator + 1);
1911 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1915 g_string_free (icon_name, TRUE);
1919 /* canonicalize MIME type, and prepend "gnome-mime-" */
1920 icon_name = g_string_new ("gnome-mime-");
1921 g_string_append_len (icon_name, mime_type, separator - mime_type);
1922 g_string_append_c (icon_name, '-');
1923 g_string_append (icon_name, separator + 1);
1924 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1928 g_string_free (icon_name, TRUE);
1932 /* try the MIME family icon */
1933 icon_name = g_string_new ("gnome-mime-");
1934 g_string_append_len (icon_name, mime_type, separator - mime_type);
1935 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1939 g_string_free (icon_name, TRUE);
1945 get_icon_fallback (const gchar *icon_name,
1948 GtkIconTheme *icon_theme;
1951 icon_theme = gtk_icon_theme_get_default ();
1953 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1955 GTK_ICON_LOOKUP_USE_BUILTIN,
1957 g_assert (retval != NULL);
1963 * gtk_recent_info_get_icon:
1964 * @info: a #GtkRecentInfo
1965 * @size: the size of the icon in pixels
1967 * Retrieves the icon of size @size associated to the resource MIME type.
1969 * Return value: a #GdkPixbuf containing the icon, or %NULL. Use
1970 * g_object_unref() when finished using the icon.
1975 gtk_recent_info_get_icon (GtkRecentInfo *info,
1978 GdkPixbuf *retval = NULL;
1980 g_return_val_if_fail (info != NULL, NULL);
1982 if (info->mime_type)
1983 retval = get_icon_for_mime_type (info->mime_type, size);
1985 /* this function should never fail */
1988 if (info->mime_type &&
1989 strcmp (info->mime_type, "x-directory/normal") == 0)
1990 retval = get_icon_fallback (GTK_STOCK_DIRECTORY, size);
1992 retval = get_icon_fallback (GTK_STOCK_FILE, size);
1999 * gtk_recent_info_is_local:
2000 * @info: a #GtkRecentInfo
2002 * Checks whether the resource is local or not by looking at the
2003 * scheme of its URI.
2005 * Return value: %TRUE if the resource is local.
2010 gtk_recent_info_is_local (GtkRecentInfo *info)
2012 g_return_val_if_fail (info != NULL, FALSE);
2014 return has_case_prefix (info->uri, "file:/");
2018 * gtk_recent_info_exists:
2019 * @info: a #GtkRecentInfo
2021 * Checks whether the resource pointed by @info still exists. At
2022 * the moment this check is done only on resources pointing to local files.
2024 * Return value: %TRUE if the resource exists
2029 gtk_recent_info_exists (GtkRecentInfo *info)
2032 struct stat stat_buf;
2033 gboolean retval = FALSE;
2035 g_return_val_if_fail (info != NULL, FALSE);
2037 /* we guarantee only local resources */
2038 if (!gtk_recent_info_is_local (info))
2041 filename = g_filename_from_uri (info->uri, NULL, NULL);
2044 if (stat (filename, &stat_buf) == 0)
2054 * gtk_recent_info_match:
2055 * @info_a: a #GtkRecentInfo
2056 * @info_b: a #GtkRecentInfo
2058 * Checks whether two #GtkRecentInfo structures point to the same
2061 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2062 * resource, %FALSE otherwise.
2067 gtk_recent_info_match (GtkRecentInfo *info_a,
2068 GtkRecentInfo *info_b)
2070 g_return_val_if_fail (info_a != NULL, FALSE);
2071 g_return_val_if_fail (info_b != NULL, FALSE);
2073 return (0 == strcmp (info_a->uri, info_b->uri));
2076 /* taken from gnome-vfs-uri.c */
2077 static const gchar *
2078 get_method_string (const gchar *substring,
2079 gchar **method_string)
2085 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2092 !(p == substring + 1 && g_ascii_isalpha (*substring))
2096 /* Found toplevel method specification. */
2097 method = g_strndup (substring, p - substring);
2098 *method_string = g_ascii_strdown (method, -1);
2104 *method_string = g_strdup ("file");
2111 /* Stolen from gnome_vfs_make_valid_utf8() */
2113 make_valid_utf8 (const char *name)
2116 const char *remainder, *invalid;
2117 int remaining_bytes, valid_bytes;
2121 remaining_bytes = name ? strlen (name) : 0;
2123 while (remaining_bytes != 0)
2125 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2128 valid_bytes = invalid - remainder;
2131 string = g_string_sized_new (remaining_bytes);
2133 g_string_append_len (string, remainder, valid_bytes);
2134 g_string_append_c (string, '?');
2136 remaining_bytes -= valid_bytes + 1;
2137 remainder = invalid + 1;
2141 return g_strdup (name);
2143 g_string_append (string, remainder);
2144 g_assert (g_utf8_validate (string->str, -1, NULL));
2146 return g_string_free (string, FALSE);
2150 get_uri_shortname_for_display (const gchar *uri)
2153 gboolean validated = FALSE;
2155 if (has_case_prefix (uri, "file:/"))
2159 local_file = g_filename_from_uri (uri, NULL, NULL);
2163 name = g_filename_display_basename (local_file);
2167 g_free (local_file);
2176 rest = get_method_string (uri, &method);
2177 local_file = g_filename_display_basename (rest);
2179 name = g_strconcat (method, ": ", local_file, NULL);
2181 g_free (local_file);
2185 g_assert (name != NULL);
2187 if (!validated && !g_utf8_validate (name, -1, NULL))
2191 utf8_name = make_valid_utf8 (name);
2201 * gtk_recent_info_get_short_name:
2202 * @info: an #GtkRecentInfo
2204 * Computes a valid UTF-8 string that can be used as the name of the item in a
2205 * menu or list. For example, calling this function on an item that refers to
2206 * "file:///foo/bar.txt" will yield "bar.txt".
2208 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2214 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2218 g_return_val_if_fail (info != NULL, NULL);
2220 if (info->uri == NULL)
2223 short_name = get_uri_shortname_for_display (info->uri);
2229 * gtk_recent_info_get_uri_display:
2230 * @info: a #GtkRecentInfo
2232 * Gets a displayable version of the resource's URI. If the resource
2233 * is local, it returns a local path; if the resource is not local,
2234 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2236 * Return value: a newly allocated UTF-8 string containing the
2237 * resource's URI or %NULL. Use g_free() when done using it.
2242 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2246 g_return_val_if_fail (info != NULL, NULL);
2249 if (gtk_recent_info_is_local (info))
2253 filename = g_filename_from_uri (info->uri, NULL, NULL);
2257 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2262 retval = make_valid_utf8 (info->uri);
2269 * gtk_recent_info_get_age:
2270 * @info: a #GtkRecentInfo
2272 * Gets the number of days elapsed since the last update of the resource
2275 * Return value: a positive integer containing the number of days elapsed
2276 * since the time this resource was last modified.
2281 gtk_recent_info_get_age (GtkRecentInfo *info)
2286 g_return_val_if_fail (info != NULL, -1);
2290 delta = now - info->modified;
2292 retval = (gint) (delta / (60 * 60 * 24));
2298 * gtk_recent_info_get_groups:
2299 * @info: a #GtkRecentInfo
2300 * @length: return location for the number of groups returned, or %NULL
2302 * Returns all groups registered for the recently used item @info. The
2303 * array of returned group names will be %NULL terminated, so length might
2304 * optionally be %NULL.
2306 * Return value: a newly allocated %NULL terminated array of strings. Use
2307 * g_strfreev() to free it.
2312 gtk_recent_info_get_groups (GtkRecentInfo *info,
2319 g_return_val_if_fail (info != NULL, NULL);
2329 n_groups = g_slist_length (info->groups);
2331 retval = g_new0 (gchar *, n_groups + 1);
2333 for (l = info->groups, i = 0;
2337 gchar *group_name = (gchar *) l->data;
2339 g_assert (group_name != NULL);
2341 retval[i++] = g_strdup (group_name);
2352 * gtk_recent_info_has_group:
2353 * @info: a #GtkRecentInfo
2354 * @group_name: name of a group
2356 * Checks whether @group_name appears inside the groups registered for the
2357 * recently used item @info.
2359 * Return value: %TRUE if the group was found.
2364 gtk_recent_info_has_group (GtkRecentInfo *info,
2365 const gchar *group_name)
2369 g_return_val_if_fail (info != NULL, FALSE);
2370 g_return_val_if_fail (group_name != NULL, FALSE);
2375 for (l = info->groups; l != NULL; l = l->next)
2377 gchar *g = (gchar *) l->data;
2379 if (strcmp (g, group_name) == 0)
2387 * _gtk_recent_manager_sync:
2389 * Private function for synchronising the recent manager singleton.
2392 _gtk_recent_manager_sync (void)
2394 if (recent_manager_singleton)
2396 /* force a dump of the contents of the recent manager singleton */
2397 recent_manager_singleton->priv->is_dirty = TRUE;
2398 gtk_recent_manager_real_changed (recent_manager_singleton);
2402 #define __GTK_RECENT_MANAGER_C__
2403 #include "gtkaliasdef.c"