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 per second should be enough */
52 #define POLL_DELTA 1000
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 gtk_recent_manager_error_quark (void)
158 static GQuark quark = 0;
160 quark = g_quark_from_static_string ("gtk-recent-manager-error-quark");
166 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170 gtk_recent_manager_parent_class = g_type_class_peek_parent (klass);
172 gobject_class->set_property = gtk_recent_manager_set_property;
173 gobject_class->get_property = gtk_recent_manager_get_property;
174 gobject_class->finalize = gtk_recent_manager_finalize;
177 * GtkRecentManager:filename
179 * The full path to the file to be used to store and read the recently
180 * used resources list
184 g_object_class_install_property (gobject_class,
186 g_param_spec_string ("filename",
188 P_("The full path to the file to be used to store and read the list"),
190 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE)));
192 * GtkRecentManager:limit
194 * The maximum number of items to be returned by the
195 * gtk_recent_manager_get_items() function.
199 g_object_class_install_property (gobject_class,
201 g_param_spec_int ("limit",
203 P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
209 * GtkRecentManager:size
211 * The size of the recently used resources list.
215 g_object_class_install_property (gobject_class,
217 g_param_spec_int ("size",
219 P_("The size of the recently used resources list"),
226 * GtkRecentManager::changed
227 * @recent_manager: the recent manager
229 * Emitted when the current recently used resources manager changes its
235 g_signal_new (I_("changed"),
236 G_TYPE_FROM_CLASS (klass),
238 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
240 g_cclosure_marshal_VOID__VOID,
243 klass->changed = gtk_recent_manager_real_changed;
245 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
249 gtk_recent_manager_init (GtkRecentManager *manager)
251 GtkRecentManagerPrivate *priv;
253 priv = g_type_instance_get_private ((GTypeInstance *) manager,
254 GTK_TYPE_RECENT_MANAGER);
255 manager->priv = priv;
257 priv->filename = g_build_filename (g_get_home_dir (),
258 GTK_RECENTLY_USED_FILE,
261 priv->limit = DEFAULT_LIMIT;
264 priv->is_screen_singleton = FALSE;
265 priv->is_dirty = FALSE;
266 priv->write_in_progress = FALSE;
267 priv->read_in_progress = FALSE;
273 gtk_recent_manager_set_property (GObject *object,
278 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
283 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
286 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 gtk_recent_manager_get_property (GObject *object,
300 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
305 g_value_set_string (value, recent_manager->priv->filename);
308 g_value_set_int (value, recent_manager->priv->limit);
311 g_value_set_int (value, recent_manager->priv->size);
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 gtk_recent_manager_finalize (GObject *object)
322 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
323 GtkRecentManagerPrivate *priv = manager->priv;
325 /* remove the poll timeout */
326 if (priv->poll_timeout)
327 g_source_remove (priv->poll_timeout);
330 g_free (priv->filename);
332 if (priv->recent_items)
333 g_bookmark_file_free (priv->recent_items);
335 /* chain up parent's finalize method */
336 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
340 gtk_recent_manager_real_changed (GtkRecentManager *manager)
342 GtkRecentManagerPrivate *priv = manager->priv;
344 g_object_freeze_notify (G_OBJECT (manager));
349 struct stat stat_buf;
351 /* we are marked as dirty, so we dump the content of our
352 * recently used items list
354 g_assert (priv->filename != NULL);
356 priv->write_in_progress = TRUE;
358 /* if no container object has been defined, we create a new
359 * empty container, and dump it
361 if (!priv->recent_items)
363 priv->recent_items = g_bookmark_file_new ();
368 g_bookmark_file_to_file (priv->recent_items,
373 g_warning ("Attempting to store changes into `%s', "
376 write_error->message);
377 g_error_free (write_error);
380 /* we have sync'ed our list with the storage file, so we
381 * update the file mtime in order to skip the timed check
382 * and spare us from a re-read.
384 if (g_stat (priv->filename, &stat_buf) < 0)
386 g_warning ("Unable to stat() the recently used resources file "
391 priv->write_in_progress = FALSE;
393 g_object_thaw_notify (G_OBJECT (manager));
398 priv->last_mtime = stat_buf.st_mtime;
400 /* mark us as clean */
401 priv->is_dirty = FALSE;
405 /* we are not marked as dirty, so we have been called
406 * because the recently used resources file has been
407 * changed (and not from us).
409 build_recent_items_list (manager);
412 g_object_thaw_notify (G_OBJECT (manager));
415 /* timed poll()-ing of the recently used resources file.
416 * an event-based system would be more efficient.
419 gtk_recent_manager_poll_timeout (gpointer data)
421 GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
422 GtkRecentManagerPrivate *priv = manager->priv;
423 struct stat stat_buf;
426 /* wait for the next timeout if we have a read/write in progress */
427 if (priv->write_in_progress || priv->read_in_progress)
430 stat_res = g_stat (priv->filename, &stat_buf);
433 /* the file does not exist, yet, so we wait */
437 g_warning ("Unable to stat() the recently used resources file "
445 /* the file didn't change from the last poll(), so we bail out */
446 if (stat_buf.st_mtime == priv->last_mtime)
449 /* the file has been changed, hence we emit the "changed" signal */
450 gtk_recent_manager_changed (manager);
456 gtk_recent_manager_set_filename (GtkRecentManager *manager,
457 const gchar *filename)
459 GtkRecentManagerPrivate *priv;
461 g_assert (GTK_IS_RECENT_MANAGER (manager));
462 priv = manager->priv;
464 if (!filename || filename[0] == '\0')
467 g_free (manager->priv->filename);
469 if (manager->priv->poll_timeout)
471 g_source_remove (manager->priv->poll_timeout);
472 manager->priv->poll_timeout = 0;
475 build_recent_items_list (manager);
477 priv->filename = g_strdup (filename);
478 priv->poll_timeout = g_timeout_add (POLL_DELTA,
479 gtk_recent_manager_poll_timeout,
482 priv->is_dirty = FALSE;
485 /* reads the recently used resources file and builds the items list.
486 * we keep the items list inside the parser object, and build the
487 * RecentInfo object only on user's demand to avoid useless replication.
490 build_recent_items_list (GtkRecentManager *manager)
492 GtkRecentManagerPrivate *priv;
493 struct stat stat_buf;
499 priv = manager->priv;
500 g_assert (priv->filename != NULL);
502 if (!priv->recent_items)
504 priv->recent_items = g_bookmark_file_new ();
508 stat_res = g_stat (priv->filename, &stat_buf);
511 /* the file doesn't exists, so we bail out and wait for the first
519 g_warning ("Attempting to read the recently used resources file "
520 "at `%s', but an error occurred: %s. Aborting.",
528 /* record the last mtime, for later use */
529 priv->last_mtime = stat_buf.st_mtime;
531 priv->read_in_progress = TRUE;
533 /* the file exists, and it's valid (we hope); if not, destroy the container
534 * object and hope for a better result when the next "changed" signal is
537 res = g_bookmark_file_load_from_file (priv->recent_items,
542 g_warning ("Attempting to read the recently used resources file "
543 "at `%s', but the parser failed: %s.",
545 read_error->message);
547 g_bookmark_file_free (priv->recent_items);
548 priv->recent_items = NULL;
550 g_error_free (read_error);
553 size = g_bookmark_file_get_size (priv->recent_items);
554 if (priv->size != size)
558 g_object_notify (G_OBJECT (manager), "size");
561 priv->read_in_progress = FALSE;
565 /********************
567 ********************/
571 * gtk_recent_manager_new:
573 * Creates a new recent manager object. Recent manager objects are used to
574 * handle the list of recently used resources. A #GtkRecentManager object
575 * monitors the recently used resources list, and emits the "changed" signal
576 * each time something inside the list changes.
578 * #GtkRecentManager objects are expansive: be sure to create them only when
579 * needed. You should use the gtk_recent_manager_new_for_screen() or the
580 * gtk_recent_manager_get_default() functions instead.
582 * Return value: A newly created #GtkRecentManager object.
587 gtk_recent_manager_new (void)
589 GtkRecentManager *retval;
592 filename = g_build_filename (g_get_home_dir (),
593 GTK_RECENTLY_USED_FILE,
596 retval = g_object_new (GTK_TYPE_RECENT_MANAGER,
597 "filename", filename,
606 * gtk_recent_manager_get_default:
608 * Gets the recent manager for the default screen. See
609 * gtk_recent_manager_get_for_screen().
611 * Return value: A unique #GtkRecentManager associated with the
612 * default screen. This recent manager is associated to the
613 * screen and can be used as long as the screen is open.
614 * Do no ref or unref it.
619 gtk_recent_manager_get_default (void)
621 return gtk_recent_manager_get_for_screen (gdk_screen_get_default ());
625 * gtk_recent_manager_get_for_screen:
626 * @screen: a #GdkScreen
628 * Gets the recent manager object associated with @screen; if this
629 * function has not previously been called for the given screen,
630 * a new recent manager object will be created and associated with
631 * the screen. Recent manager objects are fairly expensive to create,
632 * so using this function is usually a better choice than calling
633 * gtk_recent_manager_new() and setting the screen yourself; by using
634 * this function a single recent manager object will be shared between
637 * Return value: A unique #GtkRecentManager associated with the given
638 * screen. This recent manager is associated to the with the screen
639 * and can be used as long as the screen is open. Do not ref or
645 gtk_recent_manager_get_for_screen (GdkScreen *screen)
647 GtkRecentManager *manager;
649 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
650 g_return_val_if_fail (!screen->closed, NULL);
652 manager = g_object_get_data (G_OBJECT (screen), "gtk-recent-manager-default");
655 GtkRecentManagerPrivate *priv;
657 manager = gtk_recent_manager_new ();
658 gtk_recent_manager_set_screen (manager, screen);
660 priv = manager->priv;
661 priv->is_screen_singleton = TRUE;
663 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), manager);
670 display_closed (GdkDisplay *display,
672 GtkRecentManager *manager)
674 GtkRecentManagerPrivate *priv = manager->priv;
675 GdkScreen *screen = priv->screen;
676 gboolean was_screen_singleton = priv->is_screen_singleton;
678 if (was_screen_singleton)
680 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-error-quark"), NULL);
681 priv->is_screen_singleton = FALSE;
684 gtk_recent_manager_set_screen (manager, NULL);
686 if (was_screen_singleton)
687 g_object_unref (manager);
691 unset_screen (GtkRecentManager *manager)
693 GtkRecentManagerPrivate *priv = manager->priv;
698 display = gdk_screen_get_display (priv->screen);
700 g_signal_handlers_disconnect_by_func (display,
701 (gpointer) display_closed,
709 * gtk_recent_manager_set_screen:
710 * @manager: a #GtkRecentManager
711 * @screen: a #GdkScreen
713 * Sets the screen for a recent manager; the screen is used to
714 * track the user's currently configured recently used documents
720 gtk_recent_manager_set_screen (GtkRecentManager *manager,
723 GtkRecentManagerPrivate *priv;
726 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
727 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
729 priv = manager->priv;
731 unset_screen (manager);
735 display = gdk_screen_get_display (screen);
737 priv->screen = screen;
739 g_signal_connect (display, "closed",
740 G_CALLBACK (display_closed), manager);
745 * gtk_recent_manager_set_limit:
746 * @recent_manager: a #GtkRecentManager
747 * @limit: the maximum number of items to return, or -1.
749 * Sets the maximum number of item that the gtk_recent_manager_get_items()
750 * function should return. If @limit is set to -1, then return all the
756 gtk_recent_manager_set_limit (GtkRecentManager *recent_manager,
759 GtkRecentManagerPrivate *priv;
761 g_return_if_fail (GTK_IS_RECENT_MANAGER (recent_manager));
763 priv = recent_manager->priv;
768 * gtk_recent_manager_get_limit:
769 * @recent_manager: a #GtkRecentManager
771 * Gets the maximum number of items that the gtk_recent_manager_get_items()
772 * function should return.
774 * Return value: the number of items to return, or -1 for every item.
779 gtk_recent_manager_get_limit (GtkRecentManager *recent_manager)
781 GtkRecentManagerPrivate *priv;
783 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), DEFAULT_LIMIT);
785 priv = recent_manager->priv;
790 * gtk_recent_manager_add_item:
791 * @recent_manager: a #GtkRecentManager
793 * @error: return location for a #GError, or %NULL
795 * Adds a new resource, pointed by @uri, into the recently used
798 * This function automatically retrieving some of the needed
799 * metadata and setting other metadata to common default values; it
800 * then feeds the data to gtk_recent_manager_add_full().
802 * See gtk_recent_manager_add_full() if you want to explicitely
803 * define the metadata for the resource pointed by @uri.
805 * Return value: %TRUE if the new item was successfully added
806 * to the recently used resources list
811 gtk_recent_manager_add_item (GtkRecentManager *recent_manager,
815 GtkRecentData *recent_data;
819 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
820 g_return_val_if_fail (uri != NULL, FALSE);
822 recent_data = g_slice_new (GtkRecentData);
824 recent_data->display_name = NULL;
825 recent_data->description = NULL;
828 if (g_str_has_prefix (uri, "file://"))
831 const gchar *mime_type;
833 filename = g_filename_from_uri (uri, NULL, NULL);
834 mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
836 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
838 recent_data->mime_type = g_strdup (mime_type);
844 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
846 recent_data->app_name = g_strdup (g_get_application_name ());
847 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
849 recent_data->groups = NULL;
851 recent_data->is_private = FALSE;
854 retval = gtk_recent_manager_add_full (recent_manager, uri, recent_data, &add_error);
856 g_free (recent_data->mime_type);
857 g_free (recent_data->app_name);
858 g_free (recent_data->app_exec);
860 g_slice_free (GtkRecentData, recent_data);
864 g_propagate_error (error, add_error);
873 * gtk_recent_manager_add_full:
874 * @recent_manager: a #GtkRecentManager
876 * @recent_data: metadata of the resource
877 * @error: return location for a #GError, or %NULL
879 * Adds a new resource, pointed by @uri, into the recently used
880 * resources list, using the metadata specified inside the #GtkRecentData
881 * structure passed in @recent_data.
883 * The passed URI will be used to identify this resource inside the
886 * In order to register the new recently used resource, metadata about
887 * the resource must be passed as well as the URI; the metadata is
888 * stored in a #GtkRecentData structure, which must contain the MIME
889 * type of the resource pointed by the URI; the name of the application
890 * that is registering the item, and a command line to be used when
891 * launching the item.
893 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
894 * to be used when viewing the item instead of the last component of the
895 * URI; a short description of the item; whether the item should be
896 * considered private - that is, should be displayed only by the
897 * applications that have registered it.
899 * Return value: %TRUE if the new item was successfully added to the
900 * recently used resources list, %FALSE otherwise.
905 gtk_recent_manager_add_full (GtkRecentManager *recent_manager,
907 const GtkRecentData *data,
910 GtkRecentManagerPrivate *priv;
912 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
913 g_return_val_if_fail (uri != NULL, FALSE);
914 g_return_val_if_fail (data != NULL, FALSE);
917 if ((data->display_name) &&
918 (!g_utf8_validate (data->display_name, -1, NULL)))
920 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
921 GTK_RECENT_MANAGER_ERROR_INVALID_ENCODING,
922 _("The display name of the recently used resource "
923 "must be a valid UTF-8 encoded string."));
927 if ((data->description) &&
928 (!g_utf8_validate (data->description, -1, NULL)))
930 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
931 GTK_RECENT_MANAGER_ERROR_INVALID_ENCODING,
932 _("The description of the recently used resource "
933 "must by a valid UTF-8 encoded string."));
938 if (!data->mime_type)
940 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
941 GTK_RECENT_MANAGER_ERROR_INVALID_MIME,
942 _("You must specify the MIME type of the "
943 "resource pointed by `%s'"),
950 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
951 GTK_RECENT_MANAGER_ERROR_NOT_REGISTERED,
952 _("You must specify the name of the application "
953 "that is registering the recently used resource "
961 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
962 GTK_RECENT_MANAGER_ERROR_BAD_EXEC_STRING,
963 _("You must specify a command line to "
964 "be used when launching the resource "
970 priv = recent_manager->priv;
972 if (!priv->recent_items)
974 priv->recent_items = g_bookmark_file_new ();
978 if (data->display_name)
979 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
981 if (data->description)
982 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
984 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
986 if (data->groups && data->groups[0] != '\0')
990 for (j = 0; (data->groups)[j] != NULL; j++)
991 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
994 /* register the application; this will take care of updating the
995 * registration count and time in case the application has
996 * already registered the same document inside the list
998 g_bookmark_file_add_application (priv->recent_items, uri,
1002 g_bookmark_file_set_is_private (priv->recent_items, uri,
1005 /* mark us as dirty, so that when emitting the "changed" signal we
1006 * will dump our changes
1008 priv->is_dirty = TRUE;
1010 gtk_recent_manager_changed (recent_manager);
1016 * gtk_recent_manager_remove_item:
1017 * @recent_manager: a #GtkRecentManager
1018 * @uri: the URI of the item you wish to remove
1019 * @error: return location for a #GError, or %NULL
1021 * Removes a resource pointed by @uri from the recently used resources
1022 * list handled by a recent manager.
1024 * Return value: %TRUE if the item pointed by @uri has been successfully
1025 * removed by the recently used resources list, and %FALSE otherwise.
1030 gtk_recent_manager_remove_item (GtkRecentManager *recent_manager,
1034 GtkRecentManagerPrivate *priv;
1035 GError *remove_error = NULL;
1037 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1038 g_return_val_if_fail (uri != NULL, FALSE);
1039 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1041 priv = recent_manager->priv;
1043 if (!priv->recent_items)
1045 priv->recent_items = g_bookmark_file_new ();
1048 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1049 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1050 _("Unable to find an item with URI '%s'"),
1056 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1059 g_propagate_error (error, remove_error);
1064 priv->is_dirty = TRUE;
1066 gtk_recent_manager_changed (recent_manager);
1072 * gtk_recent_manager_has_item:
1073 * @recent_manager: a #GtkRecentManager
1076 * Checks whether there is a recently used resource registered
1077 * with @uri inside the recent manager.
1079 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1084 gtk_recent_manager_has_item (GtkRecentManager *recent_manager,
1087 GtkRecentManagerPrivate *priv;
1089 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1090 g_return_val_if_fail (uri != NULL, FALSE);
1092 priv = recent_manager->priv;
1093 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1095 return g_bookmark_file_has_item (priv->recent_items, uri);
1099 build_recent_info (GBookmarkFile *bookmarks,
1100 GtkRecentInfo *info)
1102 gchar **apps, **groups;
1103 gsize apps_len, groups_len, i;
1105 g_assert (bookmarks != NULL);
1106 g_assert (info != NULL);
1108 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1109 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1110 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1112 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1114 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1115 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1116 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1118 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1119 for (i = 0; i < groups_len; i++)
1121 gchar *group_name = g_strdup (groups[i]);
1123 info->groups = g_slist_append (info->groups, group_name);
1126 g_strfreev (groups);
1128 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1129 for (i = 0; i < apps_len; i++)
1131 gchar *app_name, *app_exec;
1134 RecentAppInfo *app_info;
1139 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1147 app_info = recent_app_info_new (app_name);
1148 app_info->exec = app_exec;
1149 app_info->count = count;
1150 app_info->stamp = stamp;
1152 info->applications = g_slist_append (info->applications,
1154 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1163 * gtk_recent_manager_lookup_item:
1164 * @recent_manager: a #GtkRecentManager
1166 * @error: a return location for a #GError, or %NULL
1168 * Searches for a URI inside the recently used resources list, and
1169 * returns a structure containing informations about the resource
1170 * like its MIME type, or its display name.
1172 * Return value: a #GtkRecentInfo structure containing information
1173 * about the resource pointed by @uri, or %NULL if the URI was
1174 * not registered in the recently used resources list. Free with
1175 * gtk_recent_info_unref().
1178 gtk_recent_manager_lookup_item (GtkRecentManager *recent_manager,
1182 GtkRecentManagerPrivate *priv;
1183 GtkRecentInfo *info = NULL;
1186 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), NULL);
1187 g_return_val_if_fail (uri != NULL, NULL);
1188 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1190 priv = recent_manager->priv;
1191 if (!priv->recent_items)
1193 priv->recent_items = g_bookmark_file_new ();
1196 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1197 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1198 _("Unable to find an item with URI '%s'"),
1204 if (!g_bookmark_file_has_item (priv->recent_items, uri))
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'"),
1213 info = gtk_recent_info_new (uri);
1214 g_return_val_if_fail (info != NULL, NULL);
1216 /* fill the RecentInfo structure with the data retrieved by our
1217 * parser object from the storage file
1219 res = build_recent_info (priv->recent_items, info);
1222 gtk_recent_info_free (info);
1227 return gtk_recent_info_ref (info);
1231 * gtk_recent_manager_move_item:
1232 * @manager: a #GtkRecentManager
1233 * @uri: the URI of a recently used resource
1234 * @new_uri: the new URI of the recently used resource, or %NULL to
1235 * remove the item pointed by @uri in the list
1236 * @error: a return location for a #GError, or %NULL
1238 * Changes the location of a recently used resource from @uri to @new_uri.
1240 * Please note that this function will not affect the resource pointed
1241 * by the URIs, but only the URI used in the recently used resources list.
1243 * Return value: %TRUE on success.
1248 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1250 const gchar *new_uri,
1253 GtkRecentManagerPrivate *priv;
1257 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1258 g_return_val_if_fail (uri != NULL, FALSE);
1259 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1261 priv = recent_manager->priv;
1263 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1265 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1266 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1267 _("Unable to find an item with URI '%s'"),
1273 res = g_bookmark_file_move_item (priv->recent_items,
1278 g_propagate_error (error, move_error);
1282 priv->is_dirty = TRUE;
1284 gtk_recent_manager_changed (recent_manager);
1290 * gtk_recent_manager_get_items:
1291 * @recent_manager: a #GtkRecentManager
1293 * Gets the list of recently used resources.
1295 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1296 * gtk_recent_info_unref() on each item inside the list, and then
1297 * free the list itself using g_list_free().
1302 gtk_recent_manager_get_items (GtkRecentManager *recent_manager)
1304 GtkRecentManagerPrivate *priv;
1305 GList *retval = NULL;
1309 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), NULL);
1311 priv = recent_manager->priv;
1312 if (!priv->recent_items)
1315 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1316 for (i = 0; i < uris_len; i++)
1318 GtkRecentInfo *info;
1321 info = gtk_recent_info_new (uris[i]);
1322 res = build_recent_info (priv->recent_items, info);
1325 g_warning ("Unable to create a RecentInfo object for "
1326 "item with URI `%s'",
1328 gtk_recent_info_free (info);
1333 retval = g_list_prepend (retval, info);
1338 /* clamp the list, if a limit is present */
1339 if ((priv->limit != -1) &&
1340 (g_list_length (retval) > priv->limit))
1344 clamp = g_list_nth (retval, priv->limit - 1);
1352 g_list_foreach (l, (GFunc) gtk_recent_info_free, NULL);
1360 purge_recent_items_list (GtkRecentManager *manager,
1363 GtkRecentManagerPrivate *priv = manager->priv;
1365 if (!priv->recent_items)
1368 g_bookmark_file_free (priv->recent_items);
1369 priv->recent_items = NULL;
1371 priv->recent_items = g_bookmark_file_new ();
1373 priv->is_dirty = TRUE;
1375 /* emit the changed signal, to ensure that the purge is written */
1376 gtk_recent_manager_changed (manager);
1380 * gtk_recent_manager_purge_items:
1381 * @recent_manager: a #GtkRecentManager
1382 * @error: a return location for a #GError, or %NULL
1384 * Purges every item from the recently used resources list.
1386 * Return value: the number of items that have been removed from the
1387 * recently used resources list.
1392 gtk_recent_manager_purge_items (GtkRecentManager *recent_manager,
1395 GtkRecentManagerPrivate *priv;
1398 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), -1);
1400 priv = recent_manager->priv;
1401 if (!priv->recent_items)
1404 count = g_bookmark_file_get_size (priv->recent_items);
1408 purge_recent_items_list (recent_manager, error);
1410 purged = count - g_bookmark_file_get_size (priv->recent_items);
1416 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1418 g_signal_emit (recent_manager, signal_changed, 0);
1426 gtk_recent_info_get_type (void)
1428 static GType info_type = 0;
1431 info_type = g_boxed_type_register_static ("GtkRecentInfo",
1432 (GBoxedCopyFunc) gtk_recent_info_ref,
1433 (GBoxedFreeFunc) gtk_recent_info_unref);
1437 static GtkRecentInfo *
1438 gtk_recent_info_new (const gchar *uri)
1440 GtkRecentInfo *info;
1442 g_assert (uri != NULL);
1444 info = g_new0 (GtkRecentInfo, 1);
1445 info->uri = g_strdup (uri);
1447 info->applications = NULL;
1448 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1450 info->groups = NULL;
1452 info->ref_count = 1;
1458 gtk_recent_info_free (GtkRecentInfo *recent_info)
1463 g_free (recent_info->uri);
1464 g_free (recent_info->display_name);
1465 g_free (recent_info->description);
1466 g_free (recent_info->mime_type);
1468 if (recent_info->applications)
1470 g_slist_foreach (recent_info->applications,
1471 (GFunc) recent_app_info_free,
1473 g_slist_free (recent_info->applications);
1475 recent_info->applications = NULL;
1478 if (recent_info->apps_lookup)
1479 g_hash_table_destroy (recent_info->apps_lookup);
1481 if (recent_info->groups)
1483 g_slist_foreach (recent_info->groups,
1486 g_slist_free (recent_info->groups);
1488 recent_info->groups = NULL;
1491 if (recent_info->icon)
1492 g_object_unref (recent_info->icon);
1494 g_free (recent_info);
1498 * gtk_recent_info_ref:
1499 * @info: a #GtkRecentInfo
1501 * Increases the reference count of @recent_info by one.
1503 * Return value: the recent info object with its reference count increased
1509 gtk_recent_info_ref (GtkRecentInfo *info)
1511 g_return_val_if_fail (info != NULL, NULL);
1512 g_return_val_if_fail (info->ref_count > 0, NULL);
1514 info->ref_count += 1;
1520 * gtk_recent_info_unref:
1521 * @info: a #GtkRecentInfo
1523 * Decreases the reference count of @info by one. If the reference
1524 * count reaches zero, @info is deallocated, and the memory freed.
1529 gtk_recent_info_unref (GtkRecentInfo *info)
1531 g_return_if_fail (info != NULL);
1532 g_return_if_fail (info->ref_count > 0);
1534 info->ref_count -= 1;
1536 if (info->ref_count == 0)
1537 gtk_recent_info_free (info);
1541 * gtk_recent_info_get_uri:
1542 * @info: a #GtkRecentInfo
1544 * Gets the URI of the resource.
1546 * Return value: the URI of the resource. The returned string is
1547 * owned by the recent manager, and should not be freed.
1551 G_CONST_RETURN gchar *
1552 gtk_recent_info_get_uri (GtkRecentInfo *info)
1554 g_return_val_if_fail (info != NULL, NULL);
1560 * gtk_recent_info_get_display_name:
1561 * @info: a #GtkRecentInfo
1563 * Gets the name of the resource. If none has been defined, the basename
1564 * of the resource is obtained.
1566 * Return value: the display name of the resource. The returned string
1567 * is owned by the recent manager, and should not be freed.
1571 G_CONST_RETURN gchar *
1572 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1574 g_return_val_if_fail (info != NULL, NULL);
1576 if (!info->display_name)
1577 info->display_name = gtk_recent_info_get_short_name (info);
1579 return info->display_name;
1583 * gtk_recent_info_get_description:
1584 * @info: a #GtkRecentInfo
1586 * Gets the (short) description of the resource.
1588 * Return value: the description of the resource. The returned string
1589 * is owned by the recent manager, and should not be freed.
1593 G_CONST_RETURN gchar *
1594 gtk_recent_info_get_description (GtkRecentInfo *info)
1596 g_return_val_if_fail (info != NULL, NULL);
1598 return info->description;
1602 * gtk_recent_info_get_mime_type:
1603 * @info: a #GtkRecentInfo
1605 * Gets the MIME type of the resource.
1607 * Return value: the MIME type of the resource. The returned string
1608 * is owned by the recent manager, and should not be freed.
1612 G_CONST_RETURN gchar *
1613 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1615 g_return_val_if_fail (info != NULL, NULL);
1617 if (!info->mime_type)
1618 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1620 return info->mime_type;
1624 * gtk_recent_info_get_added:
1625 * @info: a #GtkRecentInfo
1627 * Gets the timestamp (seconds from system's Epoch) when the resource
1628 * was added to the recently used resources list.
1630 * Return value: the number of seconds elapsed from system's Epoch when
1631 * the resource was added to the list, or -1 on failure.
1636 gtk_recent_info_get_added (GtkRecentInfo *info)
1638 g_return_val_if_fail (info != NULL, (time_t) -1);
1644 * gtk_recent_info_get_modified:
1645 * @info: a #GtkRecentInfo
1647 * Gets the timestamp (seconds from system's Epoch) when the resource
1648 * was last modified.
1650 * Return value: the number of seconds elapsed from system's Epoch when
1651 * the resource was last modified, or -1 on failure.
1656 gtk_recent_info_get_modified (GtkRecentInfo *info)
1658 g_return_val_if_fail (info != NULL, (time_t) -1);
1660 return info->modified;
1664 * gtk_recent_info_get_visited:
1665 * @info: a #GtkRecentInfo
1667 * Gets the timestamp (seconds from system's Epoch) when the resource
1670 * Return value: the number of seconds elapsed from system's Epoch when
1671 * the resource was last visited, or -1 on failure.
1676 gtk_recent_info_get_visited (GtkRecentInfo *info)
1678 g_return_val_if_fail (info != NULL, (time_t) -1);
1680 return info->visited;
1684 * gtk_recent_info_get_private_hint:
1685 * @info: a #GtkRecentInfo
1687 * Gets the value of the "private" flag. Resources in the recently used
1688 * list that have this flag set to %TRUE should only be displayed by the
1689 * applications that have registered them.
1691 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1696 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1698 g_return_val_if_fail (info != NULL, FALSE);
1700 return info->is_private;
1704 static RecentAppInfo *
1705 recent_app_info_new (const gchar *app_name)
1707 RecentAppInfo *app_info;
1709 g_assert (app_name != NULL);
1711 app_info = g_new0 (RecentAppInfo, 1);
1712 app_info->name = g_strdup (app_name);
1713 app_info->exec = NULL;
1714 app_info->count = 1;
1715 app_info->stamp = time (NULL);
1721 recent_app_info_free (RecentAppInfo *app_info)
1727 g_free (app_info->name);
1730 g_free (app_info->exec);
1736 * gtk_recent_info_get_application_info:
1737 * @info: a #GtkRecentInfo
1738 * @app_name: the name of the application that has registered this item
1739 * @app_exec: return location for the string containing the command line
1740 * @count: return location for the number of times this item was registered
1741 * @time: return location for the timestamp this item was last registered
1742 * for this application
1744 * Gets the data regarding the application that has registered the resource
1747 * If the command line contains any escape characters defined inside the
1748 * storage specification, they will be expanded.
1750 * Return value: %TRUE if an application with @app_name has registered this
1751 * resource inside the recently used list, or %FALSE otherwise. You should
1752 * free the returned command line using g_free().
1757 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1758 const gchar *app_name,
1765 g_return_val_if_fail (info != NULL, FALSE);
1766 g_return_val_if_fail (app_name != NULL, FALSE);
1768 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1772 g_warning ("No registered application with name '%s' "
1773 "for item with URI '%s' found",
1780 *app_exec = ai->exec;
1792 * gtk_recent_info_get_applications:
1793 * @info: a #GtkRecentInfo
1794 * @length: return location for the length of the returned list, or %NULL
1796 * Retrieves the list of applications that have registered this resource.
1798 * Return value: a newly allocated %NULL-terminated array of strings.
1799 * Use g_strfreev() to free it.
1804 gtk_recent_info_get_applications (GtkRecentInfo *info,
1811 g_return_val_if_fail (info != NULL, NULL);
1813 if (!info->applications)
1821 n_apps = g_slist_length (info->applications);
1823 retval = g_new0 (gchar *, n_apps + 1);
1825 for (l = info->applications, i = 0;
1829 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1831 g_assert (ai != NULL);
1833 retval[i++] = g_strdup (ai->name);
1844 * gtk_recent_info_has_application:
1845 * @info: a #GtkRecentInfo
1846 * @app_name: a string containing an application name
1848 * Checks whether an application registered this resource using @app_name.
1850 * Return value: %TRUE if an application with name @app_name was found,
1856 gtk_recent_info_has_application (GtkRecentInfo *info,
1857 const gchar *app_name)
1859 g_return_val_if_fail (info != NULL, FALSE);
1860 g_return_val_if_fail (app_name != NULL, FALSE);
1862 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1866 * gtk_recent_info_last_application:
1867 * @info: a #GtkRecentInfo
1869 * Gets the name of the last application that have registered the
1870 * recently used resource represented by @info.
1872 * Return value: an application name. Use g_free() to free it.
1877 gtk_recent_info_last_application (GtkRecentInfo *info)
1880 time_t last_stamp = (time_t) -1;
1883 g_return_val_if_fail (info != NULL, NULL);
1885 for (l = info->applications; l != NULL; l = l->next)
1887 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1889 if (ai->stamp > last_stamp)
1893 return g_strdup (name);
1903 icon_cache_element_free (IconCacheElement *element)
1905 if (element->pixbuf)
1906 g_object_unref (element->pixbuf);
1911 icon_theme_changed (GtkIconTheme *icon_theme)
1915 /* Difference from the initial creation is that we don't
1916 * reconnect the signal
1918 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1919 (GDestroyNotify)g_free,
1920 (GDestroyNotify)icon_cache_element_free);
1921 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1922 cache, (GDestroyNotify)g_hash_table_destroy);
1925 /* TODO: use the GtkFileChooser's icon cache instead of our own to reduce
1926 * the memory footprint
1929 get_cached_icon (const gchar *name,
1932 GtkIconTheme *icon_theme;
1934 IconCacheElement *element;
1936 icon_theme = gtk_icon_theme_get_default ();
1937 cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-recent-icon-cache");
1941 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1942 (GDestroyNotify)g_free,
1943 (GDestroyNotify)icon_cache_element_free);
1945 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1946 cache, (GDestroyNotify)g_hash_table_destroy);
1947 g_signal_connect (icon_theme, "changed",
1948 G_CALLBACK (icon_theme_changed), NULL);
1951 element = g_hash_table_lookup (cache, name);
1954 element = g_new0 (IconCacheElement, 1);
1955 g_hash_table_insert (cache, g_strdup (name), element);
1958 if (element->size != pixel_size)
1960 if (element->pixbuf)
1961 g_object_unref (element->pixbuf);
1963 element->size = pixel_size;
1964 element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
1965 pixel_size, 0, NULL);
1968 return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
1973 get_icon_for_mime_type (const char *mime_type,
1976 const char *separator;
1980 separator = strchr (mime_type, '/');
1982 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
1984 icon_name = g_string_new ("gnome-mime-");
1985 g_string_append_len (icon_name, mime_type, separator - mime_type);
1986 g_string_append_c (icon_name, '-');
1987 g_string_append (icon_name, separator + 1);
1988 pixbuf = get_cached_icon (icon_name->str, pixel_size);
1989 g_string_free (icon_name, TRUE);
1993 icon_name = g_string_new ("gnome-mime-");
1994 g_string_append_len (icon_name, mime_type, separator - mime_type);
1995 pixbuf = get_cached_icon (icon_name->str, pixel_size);
1996 g_string_free (icon_name, TRUE);
2002 get_icon_fallback (const gchar *icon_name,
2005 GtkIconTheme *icon_theme;
2008 icon_theme = gtk_icon_theme_get_default ();
2010 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
2012 GTK_ICON_LOOKUP_USE_BUILTIN,
2014 g_assert (retval != NULL);
2020 * gtk_recent_info_get_icon:
2021 * @info: a #GtkRecentInfo
2022 * @size: the size of the icon in pixels
2024 * Retrieves the icon of size @size associated to the resource MIME type.
2026 * Return value: a #GdkPixbuf containing the icon, or %NULL.
2031 gtk_recent_info_get_icon (GtkRecentInfo *info,
2034 GdkPixbuf *retval = NULL;
2036 g_return_val_if_fail (info != NULL, NULL);
2038 if (info->mime_type)
2039 retval = get_icon_for_mime_type (info->mime_type, size);
2041 /* this should never fail */
2043 retval = get_icon_fallback (GTK_STOCK_FILE, size);
2049 * gtk_recent_info_is_local:
2050 * @info: a #GtkRecentInfo
2052 * Checks whether the resource is local or not by looking at the
2053 * scheme of its URI.
2055 * Return value: %TRUE if the resource is local.
2060 gtk_recent_info_is_local (GtkRecentInfo *info)
2062 g_return_val_if_fail (info != NULL, FALSE);
2064 return g_str_has_prefix (info->uri, "file://");
2068 * gtk_recent_info_exists:
2069 * @info: a #GtkRecentInfo
2071 * Checks whether the resource pointed by @info still exists. At
2072 * the moment this check is done only on resources pointing to local files.
2074 * Return value: %TRUE if the resource exists
2079 gtk_recent_info_exists (GtkRecentInfo *info)
2082 struct stat stat_buf;
2083 gboolean retval = FALSE;
2085 g_return_val_if_fail (info != NULL, FALSE);
2087 /* we guarantee only local resources */
2088 if (!gtk_recent_info_is_local (info))
2091 filename = g_filename_from_uri (info->uri, NULL, NULL);
2094 if (stat (filename, &stat_buf) == 0)
2104 * gtk_recent_info_match:
2105 * @a: a #GtkRecentInfo
2106 * @b: a #GtkRecentInfo
2108 * Checks whether two #GtkRecentInfo structures point to the same
2111 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2112 * resource, %FALSE otherwise.
2117 gtk_recent_info_match (GtkRecentInfo *a,
2120 g_return_val_if_fail (a != NULL, FALSE);
2121 g_return_val_if_fail (b != NULL, FALSE);
2123 return (0 == strcmp (a->uri, b->uri));
2126 /* taken from gnome-vfs-uri.c */
2127 static const gchar *
2128 get_method_string (const gchar *substring, gchar **method_string)
2134 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2141 !(p == substring + 1 && g_ascii_isalpha (*substring))
2145 /* Found toplevel method specification. */
2146 method = g_strndup (substring, p - substring);
2147 *method_string = g_ascii_strdown (method, -1);
2153 *method_string = g_strdup ("file");
2160 /* Stolen from gnome_vfs_make_valid_utf8() */
2162 make_valid_utf8 (const char *name)
2165 const char *remainder, *invalid;
2166 int remaining_bytes, valid_bytes;
2170 remaining_bytes = name ? strlen (name) : 0;
2172 while (remaining_bytes != 0)
2174 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2177 valid_bytes = invalid - remainder;
2180 string = g_string_sized_new (remaining_bytes);
2182 g_string_append_len (string, remainder, valid_bytes);
2183 g_string_append_c (string, '?');
2185 remaining_bytes -= valid_bytes + 1;
2186 remainder = invalid + 1;
2190 return g_strdup (name);
2192 g_string_append (string, remainder);
2193 g_assert (g_utf8_validate (string->str, -1, NULL));
2195 return g_string_free (string, FALSE);
2199 get_uri_shortname_for_display (const gchar *uri)
2202 gboolean validated = FALSE;
2204 if (g_str_has_prefix (uri, "file://"))
2208 local_file = g_filename_from_uri (uri, NULL, NULL);
2210 if (local_file != NULL)
2212 name = g_filename_display_basename (local_file);
2216 g_free (local_file);
2224 rest = get_method_string (uri, &method);
2225 local_file = g_filename_display_basename (rest);
2227 name = g_strdup_printf ("%s: %s", method, local_file);
2229 g_free (local_file);
2233 g_assert (name != NULL);
2235 if (!validated && !g_utf8_validate (name, -1, NULL))
2239 utf8_name = make_valid_utf8 (name);
2249 * gtk_recent_info_get_short_name:
2250 * @info: an #GtkRecentInfo
2252 * Computes a valid UTF-8 string that can be used as the name of the item in a
2253 * menu or list. For example, calling this function on an item that refers to
2254 * "file:///foo/bar.txt" will yield "bar.txt".
2256 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2262 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2266 g_return_val_if_fail (info != NULL, NULL);
2268 if (info->uri == NULL)
2271 short_name = get_uri_shortname_for_display (info->uri);
2277 * gtk_recent_info_get_uri_display:
2278 * @info: a #GtkRecentInfo
2280 * Gets a displayable version of the resource's URI.
2282 * Return value: a UTF-8 string containing the resource's URI or %NULL
2287 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2289 gchar *filename, *filename_utf8;
2291 g_return_val_if_fail (info != NULL, NULL);
2293 filename = g_filename_from_uri (info->uri, NULL, NULL);
2297 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2300 return filename_utf8;
2304 * gtk_recent_info_get_age:
2305 * @info: a #GtkRecentInfo
2307 * Gets the number of days elapsed since the last update of the resource
2310 * Return value: a positive integer containing the number of days elapsed
2311 * since the time this resource was last modified. On failure, -1 is
2317 gtk_recent_info_get_age (GtkRecentInfo *info)
2322 g_return_val_if_fail (info != NULL, -1);
2326 delta = now - info->modified;
2327 g_assert (delta >= 0);
2329 retval = (gint) (delta / (60 * 60 * 24));
2335 * gtk_recent_info_get_groups:
2336 * @info: a #GtkRecentInfo
2337 * @length: return location for the number of groups returned, or %NULL
2339 * Returns all groups registered for the recently used item @info. The
2340 * array of returned group names will be %NULL terminated, so length might
2341 * optionally be %NULL.
2343 * Return value: a newly allocated %NULL terminated array of strings. Use
2344 * g_strfreev() to free it.
2349 gtk_recent_info_get_groups (GtkRecentInfo *info,
2356 g_return_val_if_fail (info != NULL, NULL);
2366 n_groups = g_slist_length (info->groups);
2368 retval = g_new0 (gchar *, n_groups + 1);
2370 for (l = info->groups, i = 0;
2374 gchar *group_name = (gchar *) l->data;
2376 g_assert (group_name != NULL);
2378 retval[i++] = g_strdup (group_name);
2389 * gtk_recent_info_has_group:
2390 * @info: a #GtkRecentInfo
2391 * @group_name: name of a group
2393 * Checks whether @group_name appears inside the groups registered for the
2394 * recently used item @info.
2396 * Return value: %TRUE if the group was found.
2401 gtk_recent_info_has_group (GtkRecentInfo *info,
2402 const gchar *group_name)
2406 g_return_val_if_fail (info != NULL, FALSE);
2407 g_return_val_if_fail (group_name != NULL, FALSE);
2412 for (l = info->groups; l != NULL; l = l->next)
2414 gchar *g = (gchar *) l->data;
2416 if (strcmp (g, group_name) == 0)
2423 #define __GTK_RECENT_MANAGER_C__
2424 #include "gtkaliasdef.c"