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 every two seconds should be enough */
52 #define POLL_DELTA 2000
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"
84 GHashTable *apps_lookup;
95 struct _GtkRecentManagerPrivate
99 guint is_screen_singleton : 1;
101 guint write_in_progress : 1;
102 guint read_in_progress : 1;
109 GBookmarkFile *recent_items;
124 static void gtk_recent_manager_finalize (GObject *object);
126 static void gtk_recent_manager_set_property (GObject *object,
130 static void gtk_recent_manager_get_property (GObject *object,
134 static void gtk_recent_manager_changed (GtkRecentManager *manager);
136 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
137 static gboolean gtk_recent_manager_poll_timeout (gpointer data);
138 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
139 const gchar *filename);
141 static void build_recent_items_list (GtkRecentManager *manager);
142 static void purge_recent_items_list (GtkRecentManager *manager,
145 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
146 static void recent_app_info_free (RecentAppInfo *app_info);
148 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
149 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
151 static guint signal_changed = 0;
153 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
156 filename_warning (const gchar *format,
157 const gchar *filename,
158 const gchar *message)
160 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
161 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
165 /* Test of haystack has the needle prefix, comparing case
166 * insensitive. haystack may be UTF-8, but needle must
167 * contain only lowercase ascii. */
169 has_case_prefix (const gchar *haystack,
174 /* Eat one character at a time. */
178 while (*n && *h && *n == g_ascii_tolower (*h))
188 gtk_recent_manager_error_quark (void)
190 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
195 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
197 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
199 gtk_recent_manager_parent_class = g_type_class_peek_parent (klass);
201 gobject_class->set_property = gtk_recent_manager_set_property;
202 gobject_class->get_property = gtk_recent_manager_get_property;
203 gobject_class->finalize = gtk_recent_manager_finalize;
206 * GtkRecentManager:filename
208 * The full path to the file to be used to store and read the recently
209 * used resources list
213 g_object_class_install_property (gobject_class,
215 g_param_spec_string ("filename",
217 P_("The full path to the file to be used to store and read the list"),
219 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE)));
221 * GtkRecentManager:limit
223 * The maximum number of items to be returned by the
224 * gtk_recent_manager_get_items() function.
228 g_object_class_install_property (gobject_class,
230 g_param_spec_int ("limit",
232 P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
238 * GtkRecentManager:size
240 * The size of the recently used resources list.
244 g_object_class_install_property (gobject_class,
246 g_param_spec_int ("size",
248 P_("The size of the recently used resources list"),
255 * GtkRecentManager::changed
256 * @recent_manager: the recent manager
258 * Emitted when the current recently used resources manager changes its
264 g_signal_new (I_("changed"),
265 G_TYPE_FROM_CLASS (klass),
267 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
269 g_cclosure_marshal_VOID__VOID,
272 klass->changed = gtk_recent_manager_real_changed;
274 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
278 gtk_recent_manager_init (GtkRecentManager *manager)
280 GtkRecentManagerPrivate *priv;
282 priv = g_type_instance_get_private ((GTypeInstance *) manager,
283 GTK_TYPE_RECENT_MANAGER);
284 manager->priv = priv;
286 priv->limit = DEFAULT_LIMIT;
289 priv->is_screen_singleton = FALSE;
290 priv->is_dirty = FALSE;
291 priv->write_in_progress = FALSE;
292 priv->read_in_progress = FALSE;
296 priv->filename = g_build_filename (g_get_home_dir (),
297 GTK_RECENTLY_USED_FILE,
299 priv->poll_timeout = gdk_threads_add_timeout (POLL_DELTA,
300 gtk_recent_manager_poll_timeout,
303 build_recent_items_list (manager);
307 gtk_recent_manager_set_property (GObject *object,
312 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
317 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
320 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gtk_recent_manager_get_property (GObject *object,
334 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
339 g_value_set_string (value, recent_manager->priv->filename);
342 g_value_set_int (value, recent_manager->priv->limit);
345 g_value_set_int (value, recent_manager->priv->size);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gtk_recent_manager_finalize (GObject *object)
356 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
357 GtkRecentManagerPrivate *priv = manager->priv;
359 /* remove the poll timeout */
360 if (priv->poll_timeout)
361 g_source_remove (priv->poll_timeout);
363 g_free (priv->filename);
365 if (priv->recent_items)
366 g_bookmark_file_free (priv->recent_items);
368 /* chain up parent's finalize method */
369 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
373 gtk_recent_manager_real_changed (GtkRecentManager *manager)
375 GtkRecentManagerPrivate *priv = manager->priv;
377 g_object_freeze_notify (G_OBJECT (manager));
382 struct stat stat_buf;
384 /* we are marked as dirty, so we dump the content of our
385 * recently used items list
387 g_assert (priv->filename != NULL);
389 priv->write_in_progress = TRUE;
391 /* if no container object has been defined, we create a new
392 * empty container, and dump it
394 if (!priv->recent_items)
396 priv->recent_items = g_bookmark_file_new ();
401 g_bookmark_file_to_file (priv->recent_items,
407 filename_warning ("Attempting to store changes into `%s', "
410 write_error->message);
411 g_error_free (write_error);
414 priv->write_in_progress = FALSE;
416 /* we have sync'ed our list with the storage file, so we
417 * update the file mtime in order to skip the timed check
418 * and spare us from a re-read.
420 if (g_stat (priv->filename, &stat_buf) < 0)
422 filename_warning ("Unable to stat() the recently used resources file "
427 g_object_thaw_notify (G_OBJECT (manager));
432 priv->last_mtime = stat_buf.st_mtime;
434 /* mark us as clean */
435 priv->is_dirty = FALSE;
439 /* we are not marked as dirty, so we have been called
440 * because the recently used resources file has been
441 * changed (and not from us).
443 build_recent_items_list (manager);
446 g_object_thaw_notify (G_OBJECT (manager));
449 /* timed poll()-ing of the recently used resources file.
450 * an event-based system would be more efficient.
453 gtk_recent_manager_poll_timeout (gpointer data)
455 GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
456 GtkRecentManagerPrivate *priv = manager->priv;
457 struct stat stat_buf;
460 /* wait for the next timeout if we have a read/write in progress */
461 if (priv->write_in_progress || priv->read_in_progress)
464 stat_res = g_stat (priv->filename, &stat_buf);
467 /* the file does not exist, yet, so we wait */
471 filename_warning ("Unable to stat() the recently used resources file "
479 /* the file didn't change from the last poll(), so we bail out */
480 if (stat_buf.st_mtime == priv->last_mtime)
483 /* the file has been changed, hence we emit the "changed" signal */
484 gtk_recent_manager_changed (manager);
490 gtk_recent_manager_set_filename (GtkRecentManager *manager,
491 const gchar *filename)
493 GtkRecentManagerPrivate *priv;
495 g_assert (GTK_IS_RECENT_MANAGER (manager));
496 priv = manager->priv;
498 if (!filename || filename[0] == '\0')
501 g_free (manager->priv->filename);
503 if (manager->priv->poll_timeout)
505 g_source_remove (manager->priv->poll_timeout);
506 manager->priv->poll_timeout = 0;
509 priv->filename = g_strdup (filename);
510 priv->poll_timeout = gdk_threads_add_timeout (POLL_DELTA,
511 gtk_recent_manager_poll_timeout,
514 /* mark us clean, so that we can re-read the list
515 * of recently used resources
517 priv->is_dirty = FALSE;
518 build_recent_items_list (manager);
521 /* reads the recently used resources file and builds the items list.
522 * we keep the items list inside the parser object, and build the
523 * RecentInfo object only on user's demand to avoid useless replication.
524 * this function resets the dirty bit of the manager.
527 build_recent_items_list (GtkRecentManager *manager)
529 GtkRecentManagerPrivate *priv;
530 struct stat stat_buf;
536 priv = manager->priv;
537 g_assert (priv->filename != NULL);
539 if (!priv->recent_items)
541 priv->recent_items = g_bookmark_file_new ();
545 stat_res = g_stat (priv->filename, &stat_buf);
548 /* the file doesn't exists, so we bail out and wait for the first
556 filename_warning ("Attempting to read the recently used resources file "
557 "at `%s', but an error occurred: %s. Aborting.",
565 /* record the last mtime, for later use */
566 priv->last_mtime = stat_buf.st_mtime;
568 priv->read_in_progress = TRUE;
570 /* the file exists, and it's valid (we hope); if not, destroy the container
571 * object and hope for a better result when the next "changed" signal is
574 res = g_bookmark_file_load_from_file (priv->recent_items,
579 filename_warning ("Attempting to read the recently used resources file "
580 "at `%s', but the parser failed: %s.",
582 read_error->message);
584 g_bookmark_file_free (priv->recent_items);
585 priv->recent_items = NULL;
587 g_error_free (read_error);
590 size = g_bookmark_file_get_size (priv->recent_items);
591 if (priv->size != size)
595 g_object_notify (G_OBJECT (manager), "size");
598 priv->read_in_progress = FALSE;
599 priv->is_dirty = FALSE;
603 /********************
605 ********************/
609 * gtk_recent_manager_new:
611 * Creates a new recent manager object. Recent manager objects are used to
612 * handle the list of recently used resources. A #GtkRecentManager object
613 * monitors the recently used resources list, and emits the "changed" signal
614 * each time something inside the list changes.
616 * #GtkRecentManager objects are expensive: be sure to create them only when
617 * needed. You should use the gtk_recent_manager_new_for_screen() or the
618 * gtk_recent_manager_get_default() functions instead.
620 * Return value: A newly created #GtkRecentManager object.
625 gtk_recent_manager_new (void)
627 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
631 * gtk_recent_manager_get_default:
633 * Gets the recent manager for the default screen. See
634 * gtk_recent_manager_get_for_screen().
636 * Return value: A unique #GtkRecentManager associated with the
637 * default screen. This recent manager is associated to the
638 * screen and can be used as long as the screen is open.
639 * Do no ref or unref it.
644 gtk_recent_manager_get_default (void)
646 return gtk_recent_manager_get_for_screen (gdk_screen_get_default ());
650 * gtk_recent_manager_get_for_screen:
651 * @screen: a #GdkScreen
653 * Gets the recent manager object associated with @screen; if this
654 * function has not previously been called for the given screen,
655 * a new recent manager object will be created and associated with
656 * the screen. Recent manager objects are fairly expensive to create,
657 * so using this function is usually a better choice than calling
658 * gtk_recent_manager_new() and setting the screen yourself; by using
659 * this function a single recent manager object will be shared between
662 * Return value: A unique #GtkRecentManager associated with the given
663 * screen. This recent manager is associated to the with the screen
664 * and can be used as long as the screen is open. Do not ref or
670 gtk_recent_manager_get_for_screen (GdkScreen *screen)
672 GtkRecentManager *manager;
674 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
675 g_return_val_if_fail (!screen->closed, NULL);
677 manager = g_object_get_data (G_OBJECT (screen), "gtk-recent-manager-default");
680 GtkRecentManagerPrivate *priv;
682 manager = gtk_recent_manager_new ();
683 gtk_recent_manager_set_screen (manager, screen);
685 priv = manager->priv;
686 priv->is_screen_singleton = TRUE;
688 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), manager);
695 display_closed (GdkDisplay *display,
697 GtkRecentManager *manager)
699 GtkRecentManagerPrivate *priv = manager->priv;
700 GdkScreen *screen = priv->screen;
701 gboolean was_screen_singleton = priv->is_screen_singleton;
703 if (was_screen_singleton)
705 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), NULL);
706 priv->is_screen_singleton = FALSE;
709 gtk_recent_manager_set_screen (manager, NULL);
711 if (was_screen_singleton)
712 g_object_unref (manager);
716 unset_screen (GtkRecentManager *manager)
718 GtkRecentManagerPrivate *priv = manager->priv;
723 display = gdk_screen_get_display (priv->screen);
725 g_signal_handlers_disconnect_by_func (display,
726 (gpointer) display_closed,
734 * gtk_recent_manager_set_screen:
735 * @manager: a #GtkRecentManager
736 * @screen: a #GdkScreen
738 * Sets the screen for a recent manager; the screen is used to
739 * track the user's currently configured recently used documents
745 gtk_recent_manager_set_screen (GtkRecentManager *manager,
748 GtkRecentManagerPrivate *priv;
751 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
752 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
754 priv = manager->priv;
756 unset_screen (manager);
760 display = gdk_screen_get_display (screen);
762 priv->screen = screen;
764 g_signal_connect (display, "closed",
765 G_CALLBACK (display_closed), manager);
770 * gtk_recent_manager_set_limit:
771 * @manager: a #GtkRecentManager
772 * @limit: the maximum number of items to return, or -1.
774 * Sets the maximum number of item that the gtk_recent_manager_get_items()
775 * function should return. If @limit is set to -1, then return all the
781 gtk_recent_manager_set_limit (GtkRecentManager *manager,
784 GtkRecentManagerPrivate *priv;
786 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
788 priv = manager->priv;
793 * gtk_recent_manager_get_limit:
794 * @manager: a #GtkRecentManager
796 * Gets the maximum number of items that the gtk_recent_manager_get_items()
797 * function should return.
799 * Return value: the number of items to return, or -1 for every item.
804 gtk_recent_manager_get_limit (GtkRecentManager *manager)
806 GtkRecentManagerPrivate *priv;
808 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
810 priv = manager->priv;
815 * gtk_recent_manager_add_item:
816 * @manager: a #GtkRecentManager
819 * Adds a new resource, pointed by @uri, into the recently used
822 * This function automatically retrieving some of the needed
823 * metadata and setting other metadata to common default values; it
824 * then feeds the data to gtk_recent_manager_add_full().
826 * See gtk_recent_manager_add_full() if you want to explicitely
827 * define the metadata for the resource pointed by @uri.
829 * Return value: %TRUE if the new item was successfully added
830 * to the recently used resources list
835 gtk_recent_manager_add_item (GtkRecentManager *manager,
838 GtkRecentData *recent_data;
842 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
843 g_return_val_if_fail (uri != NULL, FALSE);
845 recent_data = g_slice_new (GtkRecentData);
847 recent_data->display_name = NULL;
848 recent_data->description = NULL;
851 if (has_case_prefix (uri, "file:/"))
854 const gchar *mime_type;
856 filename = g_filename_from_uri (uri, NULL, NULL);
857 mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
859 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
861 recent_data->mime_type = g_strdup (mime_type);
867 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
869 recent_data->app_name = g_strdup (g_get_application_name ());
870 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
872 recent_data->groups = NULL;
874 recent_data->is_private = FALSE;
877 retval = gtk_recent_manager_add_full (manager, uri, recent_data);
879 g_free (recent_data->mime_type);
880 g_free (recent_data->app_name);
881 g_free (recent_data->app_exec);
883 g_slice_free (GtkRecentData, recent_data);
889 * gtk_recent_manager_add_full:
890 * @manager: a #GtkRecentManager
892 * @recent_data: metadata of the resource
894 * Adds a new resource, pointed by @uri, into the recently used
895 * resources list, using the metadata specified inside the #GtkRecentData
896 * structure passed in @recent_data.
898 * The passed URI will be used to identify this resource inside the
901 * In order to register the new recently used resource, metadata about
902 * the resource must be passed as well as the URI; the metadata is
903 * stored in a #GtkRecentData structure, which must contain the MIME
904 * type of the resource pointed by the URI; the name of the application
905 * that is registering the item, and a command line to be used when
906 * launching the item.
908 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
909 * to be used when viewing the item instead of the last component of the
910 * URI; a short description of the item; whether the item should be
911 * considered private - that is, should be displayed only by the
912 * applications that have registered it.
914 * Return value: %TRUE if the new item was successfully added to the
915 * recently used resources list, %FALSE otherwise.
920 gtk_recent_manager_add_full (GtkRecentManager *manager,
922 const GtkRecentData *data)
924 GtkRecentManagerPrivate *priv;
926 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
927 g_return_val_if_fail (uri != NULL, FALSE);
928 g_return_val_if_fail (data != NULL, FALSE);
931 if ((data->display_name) &&
932 (!g_utf8_validate (data->display_name, -1, NULL)))
934 g_warning ("Attempting to add `%s' to the list of recently used "
935 "resources, but the display name is not a valid UTF-8 "
941 if ((data->description) &&
942 (!g_utf8_validate (data->description, -1, NULL)))
944 g_warning ("Attempting to add `%s' to the list of recently used "
945 "resources, but the description is not a valid UTF-8 "
952 if (!data->mime_type)
954 g_warning ("Attempting to add `%s' to the list of recently used "
955 "resources, but not MIME type was defined",
962 g_warning ("Attempting to add `%s' to the list of recently used "
963 "resources, but no name of the application that is "
964 "registering it was defined",
971 g_warning ("Attempting to add `%s' to the list of recently used "
972 "resources, but no command line for the application "
973 "that is registering it was defined",
978 priv = manager->priv;
980 if (!priv->recent_items)
982 priv->recent_items = g_bookmark_file_new ();
986 if (data->display_name)
987 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
989 if (data->description)
990 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
992 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
994 if (data->groups && data->groups[0] != '\0')
998 for (j = 0; (data->groups)[j] != NULL; j++)
999 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
1002 /* register the application; this will take care of updating the
1003 * registration count and time in case the application has
1004 * already registered the same document inside the list
1006 g_bookmark_file_add_application (priv->recent_items, uri,
1010 g_bookmark_file_set_is_private (priv->recent_items, uri,
1013 /* mark us as dirty, so that when emitting the "changed" signal we
1014 * will dump our changes
1016 priv->is_dirty = TRUE;
1018 gtk_recent_manager_changed (manager);
1024 * gtk_recent_manager_remove_item:
1025 * @manager: a #GtkRecentManager
1026 * @uri: the URI of the item you wish to remove
1027 * @error: return location for a #GError, or %NULL
1029 * Removes a resource pointed by @uri from the recently used resources
1030 * list handled by a recent manager.
1032 * Return value: %TRUE if the item pointed by @uri has been successfully
1033 * removed by the recently used resources list, and %FALSE otherwise.
1038 gtk_recent_manager_remove_item (GtkRecentManager *manager,
1042 GtkRecentManagerPrivate *priv;
1043 GError *remove_error = NULL;
1045 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1046 g_return_val_if_fail (uri != NULL, FALSE);
1047 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1049 priv = manager->priv;
1051 if (!priv->recent_items)
1053 priv->recent_items = g_bookmark_file_new ();
1056 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1057 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1058 _("Unable to find an item with URI '%s'"),
1064 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1067 g_propagate_error (error, remove_error);
1072 priv->is_dirty = TRUE;
1074 gtk_recent_manager_changed (manager);
1080 * gtk_recent_manager_has_item:
1081 * @manager: a #GtkRecentManager
1084 * Checks whether there is a recently used resource registered
1085 * with @uri inside the recent manager.
1087 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1092 gtk_recent_manager_has_item (GtkRecentManager *manager,
1095 GtkRecentManagerPrivate *priv;
1097 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1098 g_return_val_if_fail (uri != NULL, FALSE);
1100 priv = manager->priv;
1101 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1103 return g_bookmark_file_has_item (priv->recent_items, uri);
1107 build_recent_info (GBookmarkFile *bookmarks,
1108 GtkRecentInfo *info)
1110 gchar **apps, **groups;
1111 gsize apps_len, groups_len, i;
1113 g_assert (bookmarks != NULL);
1114 g_assert (info != NULL);
1116 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1117 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1118 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1120 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1122 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1123 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1124 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1126 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1127 for (i = 0; i < groups_len; i++)
1129 gchar *group_name = g_strdup (groups[i]);
1131 info->groups = g_slist_append (info->groups, group_name);
1134 g_strfreev (groups);
1136 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1137 for (i = 0; i < apps_len; i++)
1139 gchar *app_name, *app_exec;
1142 RecentAppInfo *app_info;
1147 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1155 app_info = recent_app_info_new (app_name);
1156 app_info->exec = app_exec;
1157 app_info->count = count;
1158 app_info->stamp = stamp;
1160 info->applications = g_slist_append (info->applications,
1162 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1171 * gtk_recent_manager_lookup_item:
1172 * @manager: a #GtkRecentManager
1174 * @error: a return location for a #GError, or %NULL
1176 * Searches for a URI inside the recently used resources list, and
1177 * returns a structure containing informations about the resource
1178 * like its MIME type, or its display name.
1180 * Return value: a #GtkRecentInfo structure containing information
1181 * about the resource pointed by @uri, or %NULL if the URI was
1182 * not registered in the recently used resources list. Free with
1183 * gtk_recent_info_unref().
1188 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1192 GtkRecentManagerPrivate *priv;
1193 GtkRecentInfo *info = NULL;
1196 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1197 g_return_val_if_fail (uri != NULL, NULL);
1198 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1200 priv = manager->priv;
1201 if (!priv->recent_items)
1203 priv->recent_items = g_bookmark_file_new ();
1206 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1207 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1208 _("Unable to find an item with URI '%s'"),
1214 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1216 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1217 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1218 _("Unable to find an item with URI '%s'"),
1223 info = gtk_recent_info_new (uri);
1224 g_return_val_if_fail (info != NULL, NULL);
1226 /* fill the RecentInfo structure with the data retrieved by our
1227 * parser object from the storage file
1229 res = build_recent_info (priv->recent_items, info);
1232 gtk_recent_info_free (info);
1237 return gtk_recent_info_ref (info);
1241 * gtk_recent_manager_move_item:
1242 * @manager: a #GtkRecentManager
1243 * @uri: the URI of a recently used resource
1244 * @new_uri: the new URI of the recently used resource, or %NULL to
1245 * remove the item pointed by @uri in the list
1246 * @error: a return location for a #GError, or %NULL
1248 * Changes the location of a recently used resource from @uri to @new_uri.
1250 * Please note that this function will not affect the resource pointed
1251 * by the URIs, but only the URI used in the recently used resources list.
1253 * Return value: %TRUE on success.
1258 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1260 const gchar *new_uri,
1263 GtkRecentManagerPrivate *priv;
1267 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1268 g_return_val_if_fail (uri != NULL, FALSE);
1269 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1271 priv = recent_manager->priv;
1273 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1275 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1276 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1277 _("Unable to find an item with URI '%s'"),
1283 res = g_bookmark_file_move_item (priv->recent_items,
1288 g_propagate_error (error, move_error);
1292 priv->is_dirty = TRUE;
1294 gtk_recent_manager_changed (recent_manager);
1300 * gtk_recent_manager_get_items:
1301 * @manager: a #GtkRecentManager
1303 * Gets the list of recently used resources.
1305 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1306 * gtk_recent_info_unref() on each item inside the list, and then
1307 * free the list itself using g_list_free().
1312 gtk_recent_manager_get_items (GtkRecentManager *manager)
1314 GtkRecentManagerPrivate *priv;
1315 GList *retval = NULL;
1319 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1321 priv = manager->priv;
1322 if (!priv->recent_items)
1325 if (priv->limit == 0)
1328 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1329 for (i = 0; i < uris_len; i++)
1331 GtkRecentInfo *info;
1334 info = gtk_recent_info_new (uris[i]);
1335 res = build_recent_info (priv->recent_items, info);
1338 g_warning ("Unable to create a RecentInfo object for "
1339 "item with URI `%s'",
1341 gtk_recent_info_free (info);
1346 retval = g_list_prepend (retval, info);
1351 /* clamp the list, if a limit is present */
1352 if ((priv->limit != -1) &&
1353 (g_list_length (retval) > priv->limit))
1357 clamp = g_list_nth (retval, priv->limit - 1);
1365 g_list_foreach (l, (GFunc) gtk_recent_info_free, NULL);
1373 purge_recent_items_list (GtkRecentManager *manager,
1376 GtkRecentManagerPrivate *priv = manager->priv;
1378 if (!priv->recent_items)
1381 g_bookmark_file_free (priv->recent_items);
1382 priv->recent_items = NULL;
1384 priv->recent_items = g_bookmark_file_new ();
1386 priv->is_dirty = TRUE;
1388 /* emit the changed signal, to ensure that the purge is written */
1389 gtk_recent_manager_changed (manager);
1393 * gtk_recent_manager_purge_items:
1394 * @manager: a #GtkRecentManager
1395 * @error: a return location for a #GError, or %NULL
1397 * Purges every item from the recently used resources list.
1399 * Return value: the number of items that have been removed from the
1400 * recently used resources list.
1405 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1408 GtkRecentManagerPrivate *priv;
1411 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1413 priv = manager->priv;
1414 if (!priv->recent_items)
1417 count = g_bookmark_file_get_size (priv->recent_items);
1421 purge_recent_items_list (manager, error);
1423 purged = count - g_bookmark_file_get_size (priv->recent_items);
1429 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1431 g_signal_emit (recent_manager, signal_changed, 0);
1439 gtk_recent_info_get_type (void)
1441 static GType info_type = 0;
1444 info_type = g_boxed_type_register_static ("GtkRecentInfo",
1445 (GBoxedCopyFunc) gtk_recent_info_ref,
1446 (GBoxedFreeFunc) gtk_recent_info_unref);
1450 static GtkRecentInfo *
1451 gtk_recent_info_new (const gchar *uri)
1453 GtkRecentInfo *info;
1455 g_assert (uri != NULL);
1457 info = g_new0 (GtkRecentInfo, 1);
1458 info->uri = g_strdup (uri);
1460 info->applications = NULL;
1461 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1463 info->groups = NULL;
1465 info->ref_count = 1;
1471 gtk_recent_info_free (GtkRecentInfo *recent_info)
1476 g_free (recent_info->uri);
1477 g_free (recent_info->display_name);
1478 g_free (recent_info->description);
1479 g_free (recent_info->mime_type);
1481 if (recent_info->applications)
1483 g_slist_foreach (recent_info->applications,
1484 (GFunc) recent_app_info_free,
1486 g_slist_free (recent_info->applications);
1488 recent_info->applications = NULL;
1491 if (recent_info->apps_lookup)
1492 g_hash_table_destroy (recent_info->apps_lookup);
1494 if (recent_info->groups)
1496 g_slist_foreach (recent_info->groups,
1499 g_slist_free (recent_info->groups);
1501 recent_info->groups = NULL;
1504 if (recent_info->icon)
1505 g_object_unref (recent_info->icon);
1507 g_free (recent_info);
1511 * gtk_recent_info_ref:
1512 * @info: a #GtkRecentInfo
1514 * Increases the reference count of @recent_info by one.
1516 * Return value: the recent info object with its reference count increased
1522 gtk_recent_info_ref (GtkRecentInfo *info)
1524 g_return_val_if_fail (info != NULL, NULL);
1525 g_return_val_if_fail (info->ref_count > 0, NULL);
1527 info->ref_count += 1;
1533 * gtk_recent_info_unref:
1534 * @info: a #GtkRecentInfo
1536 * Decreases the reference count of @info by one. If the reference
1537 * count reaches zero, @info is deallocated, and the memory freed.
1542 gtk_recent_info_unref (GtkRecentInfo *info)
1544 g_return_if_fail (info != NULL);
1545 g_return_if_fail (info->ref_count > 0);
1547 info->ref_count -= 1;
1549 if (info->ref_count == 0)
1550 gtk_recent_info_free (info);
1554 * gtk_recent_info_get_uri:
1555 * @info: a #GtkRecentInfo
1557 * Gets the URI of the resource.
1559 * Return value: the URI of the resource. The returned string is
1560 * owned by the recent manager, and should not be freed.
1564 G_CONST_RETURN gchar *
1565 gtk_recent_info_get_uri (GtkRecentInfo *info)
1567 g_return_val_if_fail (info != NULL, NULL);
1573 * gtk_recent_info_get_display_name:
1574 * @info: a #GtkRecentInfo
1576 * Gets the name of the resource. If none has been defined, the basename
1577 * of the resource is obtained.
1579 * Return value: the display name of the resource. The returned string
1580 * is owned by the recent manager, and should not be freed.
1584 G_CONST_RETURN gchar *
1585 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1587 g_return_val_if_fail (info != NULL, NULL);
1589 if (!info->display_name)
1590 info->display_name = gtk_recent_info_get_short_name (info);
1592 return info->display_name;
1596 * gtk_recent_info_get_description:
1597 * @info: a #GtkRecentInfo
1599 * Gets the (short) description of the resource.
1601 * Return value: the description of the resource. The returned string
1602 * is owned by the recent manager, and should not be freed.
1606 G_CONST_RETURN gchar *
1607 gtk_recent_info_get_description (GtkRecentInfo *info)
1609 g_return_val_if_fail (info != NULL, NULL);
1611 return info->description;
1615 * gtk_recent_info_get_mime_type:
1616 * @info: a #GtkRecentInfo
1618 * Gets the MIME type of the resource.
1620 * Return value: the MIME type of the resource. The returned string
1621 * is owned by the recent manager, and should not be freed.
1625 G_CONST_RETURN gchar *
1626 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1628 g_return_val_if_fail (info != NULL, NULL);
1630 if (!info->mime_type)
1631 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1633 return info->mime_type;
1637 * gtk_recent_info_get_added:
1638 * @info: a #GtkRecentInfo
1640 * Gets the timestamp (seconds from system's Epoch) when the resource
1641 * was added to the recently used resources list.
1643 * Return value: the number of seconds elapsed from system's Epoch when
1644 * the resource was added to the list, or -1 on failure.
1649 gtk_recent_info_get_added (GtkRecentInfo *info)
1651 g_return_val_if_fail (info != NULL, (time_t) -1);
1657 * gtk_recent_info_get_modified:
1658 * @info: a #GtkRecentInfo
1660 * Gets the timestamp (seconds from system's Epoch) when the resource
1661 * was last modified.
1663 * Return value: the number of seconds elapsed from system's Epoch when
1664 * the resource was last modified, or -1 on failure.
1669 gtk_recent_info_get_modified (GtkRecentInfo *info)
1671 g_return_val_if_fail (info != NULL, (time_t) -1);
1673 return info->modified;
1677 * gtk_recent_info_get_visited:
1678 * @info: a #GtkRecentInfo
1680 * Gets the timestamp (seconds from system's Epoch) when the resource
1683 * Return value: the number of seconds elapsed from system's Epoch when
1684 * the resource was last visited, or -1 on failure.
1689 gtk_recent_info_get_visited (GtkRecentInfo *info)
1691 g_return_val_if_fail (info != NULL, (time_t) -1);
1693 return info->visited;
1697 * gtk_recent_info_get_private_hint:
1698 * @info: a #GtkRecentInfo
1700 * Gets the value of the "private" flag. Resources in the recently used
1701 * list that have this flag set to %TRUE should only be displayed by the
1702 * applications that have registered them.
1704 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1709 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1711 g_return_val_if_fail (info != NULL, FALSE);
1713 return info->is_private;
1717 static RecentAppInfo *
1718 recent_app_info_new (const gchar *app_name)
1720 RecentAppInfo *app_info;
1722 g_assert (app_name != NULL);
1724 app_info = g_new0 (RecentAppInfo, 1);
1725 app_info->name = g_strdup (app_name);
1726 app_info->exec = NULL;
1727 app_info->count = 1;
1728 app_info->stamp = time (NULL);
1734 recent_app_info_free (RecentAppInfo *app_info)
1739 g_free (app_info->name);
1741 g_free (app_info->exec);
1747 * gtk_recent_info_get_application_info:
1748 * @info: a #GtkRecentInfo
1749 * @app_name: the name of the application that has registered this item
1750 * @app_exec: return location for the string containing the command line
1751 * @count: return location for the number of times this item was registered
1752 * @time_: return location for the timestamp this item was last registered
1753 * for this application
1755 * Gets the data regarding the application that has registered the resource
1758 * If the command line contains any escape characters defined inside the
1759 * storage specification, they will be expanded.
1761 * Return value: %TRUE if an application with @app_name has registered this
1762 * resource inside the recently used list, or %FALSE otherwise. You should
1763 * free the returned command line using g_free().
1768 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1769 const gchar *app_name,
1776 g_return_val_if_fail (info != NULL, FALSE);
1777 g_return_val_if_fail (app_name != NULL, FALSE);
1779 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1783 g_warning ("No registered application with name '%s' "
1784 "for item with URI '%s' found",
1791 *app_exec = ai->exec;
1803 * gtk_recent_info_get_applications:
1804 * @info: a #GtkRecentInfo
1805 * @length: return location for the length of the returned list, or %NULL
1807 * Retrieves the list of applications that have registered this resource.
1809 * Return value: a newly allocated %NULL-terminated array of strings.
1810 * Use g_strfreev() to free it.
1815 gtk_recent_info_get_applications (GtkRecentInfo *info,
1822 g_return_val_if_fail (info != NULL, NULL);
1824 if (!info->applications)
1832 n_apps = g_slist_length (info->applications);
1834 retval = g_new0 (gchar *, n_apps + 1);
1836 for (l = info->applications, i = 0;
1840 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1842 g_assert (ai != NULL);
1844 retval[i++] = g_strdup (ai->name);
1855 * gtk_recent_info_has_application:
1856 * @info: a #GtkRecentInfo
1857 * @app_name: a string containing an application name
1859 * Checks whether an application registered this resource using @app_name.
1861 * Return value: %TRUE if an application with name @app_name was found,
1867 gtk_recent_info_has_application (GtkRecentInfo *info,
1868 const gchar *app_name)
1870 g_return_val_if_fail (info != NULL, FALSE);
1871 g_return_val_if_fail (app_name != NULL, FALSE);
1873 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1877 * gtk_recent_info_last_application:
1878 * @info: a #GtkRecentInfo
1880 * Gets the name of the last application that have registered the
1881 * recently used resource represented by @info.
1883 * Return value: an application name. Use g_free() to free it.
1888 gtk_recent_info_last_application (GtkRecentInfo *info)
1891 time_t last_stamp = (time_t) -1;
1894 g_return_val_if_fail (info != NULL, NULL);
1896 for (l = info->applications; l != NULL; l = l->next)
1898 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1900 if (ai->stamp > last_stamp)
1903 last_stamp = ai->stamp;
1907 return g_strdup (name);
1917 icon_cache_element_free (IconCacheElement *element)
1919 if (element->pixbuf)
1920 g_object_unref (element->pixbuf);
1925 icon_theme_changed (GtkIconTheme *icon_theme)
1929 /* Difference from the initial creation is that we don't
1930 * reconnect the signal
1932 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1933 (GDestroyNotify)g_free,
1934 (GDestroyNotify)icon_cache_element_free);
1935 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1936 cache, (GDestroyNotify)g_hash_table_destroy);
1939 /* TODO: use the GtkFileChooser's icon cache instead of our own to reduce
1940 * the memory footprint
1943 get_cached_icon (const gchar *name,
1946 GtkIconTheme *icon_theme;
1948 IconCacheElement *element;
1950 icon_theme = gtk_icon_theme_get_default ();
1951 cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-recent-icon-cache");
1955 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1956 (GDestroyNotify)g_free,
1957 (GDestroyNotify)icon_cache_element_free);
1959 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1960 cache, (GDestroyNotify)g_hash_table_destroy);
1961 g_signal_connect (icon_theme, "changed",
1962 G_CALLBACK (icon_theme_changed), NULL);
1965 element = g_hash_table_lookup (cache, name);
1968 element = g_new0 (IconCacheElement, 1);
1969 g_hash_table_insert (cache, g_strdup (name), element);
1972 if (element->size != pixel_size)
1974 if (element->pixbuf)
1975 g_object_unref (element->pixbuf);
1977 element->size = pixel_size;
1978 element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
1979 pixel_size, 0, NULL);
1982 return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
1987 get_icon_for_mime_type (const char *mime_type,
1990 const char *separator;
1994 separator = strchr (mime_type, '/');
1996 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
1998 icon_name = g_string_new ("gnome-mime-");
1999 g_string_append_len (icon_name, mime_type, separator - mime_type);
2000 g_string_append_c (icon_name, '-');
2001 g_string_append (icon_name, separator + 1);
2002 pixbuf = get_cached_icon (icon_name->str, pixel_size);
2003 g_string_free (icon_name, TRUE);
2007 icon_name = g_string_new ("gnome-mime-");
2008 g_string_append_len (icon_name, mime_type, separator - mime_type);
2009 pixbuf = get_cached_icon (icon_name->str, pixel_size);
2010 g_string_free (icon_name, TRUE);
2016 get_icon_fallback (const gchar *icon_name,
2019 GtkIconTheme *icon_theme;
2022 icon_theme = gtk_icon_theme_get_default ();
2024 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
2026 GTK_ICON_LOOKUP_USE_BUILTIN,
2028 g_assert (retval != NULL);
2034 * gtk_recent_info_get_icon:
2035 * @info: a #GtkRecentInfo
2036 * @size: the size of the icon in pixels
2038 * Retrieves the icon of size @size associated to the resource MIME type.
2040 * Return value: a #GdkPixbuf containing the icon, or %NULL.
2045 gtk_recent_info_get_icon (GtkRecentInfo *info,
2048 GdkPixbuf *retval = NULL;
2050 g_return_val_if_fail (info != NULL, NULL);
2052 if (info->mime_type)
2053 retval = get_icon_for_mime_type (info->mime_type, size);
2055 /* this should never fail */
2057 retval = get_icon_fallback (GTK_STOCK_FILE, size);
2063 * gtk_recent_info_is_local:
2064 * @info: a #GtkRecentInfo
2066 * Checks whether the resource is local or not by looking at the
2067 * scheme of its URI.
2069 * Return value: %TRUE if the resource is local.
2074 gtk_recent_info_is_local (GtkRecentInfo *info)
2076 g_return_val_if_fail (info != NULL, FALSE);
2078 return has_case_prefix (info->uri, "file:/");
2082 * gtk_recent_info_exists:
2083 * @info: a #GtkRecentInfo
2085 * Checks whether the resource pointed by @info still exists. At
2086 * the moment this check is done only on resources pointing to local files.
2088 * Return value: %TRUE if the resource exists
2093 gtk_recent_info_exists (GtkRecentInfo *info)
2096 struct stat stat_buf;
2097 gboolean retval = FALSE;
2099 g_return_val_if_fail (info != NULL, FALSE);
2101 /* we guarantee only local resources */
2102 if (!gtk_recent_info_is_local (info))
2105 filename = g_filename_from_uri (info->uri, NULL, NULL);
2108 if (stat (filename, &stat_buf) == 0)
2118 * gtk_recent_info_match:
2119 * @info_a: a #GtkRecentInfo
2120 * @info_b: a #GtkRecentInfo
2122 * Checks whether two #GtkRecentInfo structures point to the same
2125 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2126 * resource, %FALSE otherwise.
2131 gtk_recent_info_match (GtkRecentInfo *info_a,
2132 GtkRecentInfo *info_b)
2134 g_return_val_if_fail (info_a != NULL, FALSE);
2135 g_return_val_if_fail (info_b != NULL, FALSE);
2137 return (0 == strcmp (info_a->uri, info_b->uri));
2140 /* taken from gnome-vfs-uri.c */
2141 static const gchar *
2142 get_method_string (const gchar *substring,
2143 gchar **method_string)
2149 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2156 !(p == substring + 1 && g_ascii_isalpha (*substring))
2160 /* Found toplevel method specification. */
2161 method = g_strndup (substring, p - substring);
2162 *method_string = g_ascii_strdown (method, -1);
2168 *method_string = g_strdup ("file");
2175 /* Stolen from gnome_vfs_make_valid_utf8() */
2177 make_valid_utf8 (const char *name)
2180 const char *remainder, *invalid;
2181 int remaining_bytes, valid_bytes;
2185 remaining_bytes = name ? strlen (name) : 0;
2187 while (remaining_bytes != 0)
2189 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2192 valid_bytes = invalid - remainder;
2195 string = g_string_sized_new (remaining_bytes);
2197 g_string_append_len (string, remainder, valid_bytes);
2198 g_string_append_c (string, '?');
2200 remaining_bytes -= valid_bytes + 1;
2201 remainder = invalid + 1;
2205 return g_strdup (name);
2207 g_string_append (string, remainder);
2208 g_assert (g_utf8_validate (string->str, -1, NULL));
2210 return g_string_free (string, FALSE);
2214 get_uri_shortname_for_display (const gchar *uri)
2217 gboolean validated = FALSE;
2219 if (has_case_prefix (uri, "file:/"))
2223 local_file = g_filename_from_uri (uri, NULL, NULL);
2227 name = g_filename_display_basename (local_file);
2231 g_free (local_file);
2240 rest = get_method_string (uri, &method);
2241 local_file = g_filename_display_basename (rest);
2243 name = g_strdup_printf ("%s: %s", method, local_file);
2245 g_free (local_file);
2249 g_assert (name != NULL);
2251 if (!validated && !g_utf8_validate (name, -1, NULL))
2255 utf8_name = make_valid_utf8 (name);
2265 * gtk_recent_info_get_short_name:
2266 * @info: an #GtkRecentInfo
2268 * Computes a valid UTF-8 string that can be used as the name of the item in a
2269 * menu or list. For example, calling this function on an item that refers to
2270 * "file:///foo/bar.txt" will yield "bar.txt".
2272 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2278 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2282 g_return_val_if_fail (info != NULL, NULL);
2284 if (info->uri == NULL)
2287 short_name = get_uri_shortname_for_display (info->uri);
2293 * gtk_recent_info_get_uri_display:
2294 * @info: a #GtkRecentInfo
2296 * Gets a displayable version of the resource's URI. If the resource
2297 * is local, it returns a local path; if the resource is not local,
2298 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2300 * Return value: a UTF-8 string containing the resource's URI or %NULL
2305 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2309 g_return_val_if_fail (info != NULL, NULL);
2312 if (gtk_recent_info_is_local (info))
2316 filename = g_filename_from_uri (info->uri, NULL, NULL);
2320 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2325 retval = make_valid_utf8 (info->uri);
2332 * gtk_recent_info_get_age:
2333 * @info: a #GtkRecentInfo
2335 * Gets the number of days elapsed since the last update of the resource
2338 * Return value: a positive integer containing the number of days elapsed
2339 * since the time this resource was last modified.
2344 gtk_recent_info_get_age (GtkRecentInfo *info)
2349 g_return_val_if_fail (info != NULL, -1);
2353 delta = now - info->modified;
2355 retval = (gint) (delta / (60 * 60 * 24));
2361 * gtk_recent_info_get_groups:
2362 * @info: a #GtkRecentInfo
2363 * @length: return location for the number of groups returned, or %NULL
2365 * Returns all groups registered for the recently used item @info. The
2366 * array of returned group names will be %NULL terminated, so length might
2367 * optionally be %NULL.
2369 * Return value: a newly allocated %NULL terminated array of strings. Use
2370 * g_strfreev() to free it.
2375 gtk_recent_info_get_groups (GtkRecentInfo *info,
2382 g_return_val_if_fail (info != NULL, NULL);
2392 n_groups = g_slist_length (info->groups);
2394 retval = g_new0 (gchar *, n_groups + 1);
2396 for (l = info->groups, i = 0;
2400 gchar *group_name = (gchar *) l->data;
2402 g_assert (group_name != NULL);
2404 retval[i++] = g_strdup (group_name);
2415 * gtk_recent_info_has_group:
2416 * @info: a #GtkRecentInfo
2417 * @group_name: name of a group
2419 * Checks whether @group_name appears inside the groups registered for the
2420 * recently used item @info.
2422 * Return value: %TRUE if the group was found.
2427 gtk_recent_info_has_group (GtkRecentInfo *info,
2428 const gchar *group_name)
2432 g_return_val_if_fail (info != NULL, FALSE);
2433 g_return_val_if_fail (group_name != NULL, FALSE);
2438 for (l = info->groups; l != NULL; l = l->next)
2440 gchar *g = (gchar *) l->data;
2442 if (strcmp (g, group_name) == 0)
2449 #define __GTK_RECENT_MANAGER_C__
2450 #include "gtkaliasdef.c"