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 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->filename = g_build_filename (g_get_home_dir (),
287 GTK_RECENTLY_USED_FILE,
290 priv->limit = DEFAULT_LIMIT;
293 priv->is_screen_singleton = FALSE;
294 priv->is_dirty = FALSE;
295 priv->write_in_progress = FALSE;
296 priv->read_in_progress = FALSE;
300 build_recent_items_list (manager);
304 gtk_recent_manager_set_property (GObject *object,
309 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
314 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
317 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326 gtk_recent_manager_get_property (GObject *object,
331 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
336 g_value_set_string (value, recent_manager->priv->filename);
339 g_value_set_int (value, recent_manager->priv->limit);
342 g_value_set_int (value, recent_manager->priv->size);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 gtk_recent_manager_finalize (GObject *object)
353 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
354 GtkRecentManagerPrivate *priv = manager->priv;
356 /* remove the poll timeout */
357 if (priv->poll_timeout)
358 g_source_remove (priv->poll_timeout);
361 g_free (priv->filename);
363 if (priv->recent_items)
364 g_bookmark_file_free (priv->recent_items);
366 /* chain up parent's finalize method */
367 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
371 gtk_recent_manager_real_changed (GtkRecentManager *manager)
373 GtkRecentManagerPrivate *priv = manager->priv;
375 g_object_freeze_notify (G_OBJECT (manager));
380 struct stat stat_buf;
382 /* we are marked as dirty, so we dump the content of our
383 * recently used items list
385 g_assert (priv->filename != NULL);
387 priv->write_in_progress = TRUE;
389 /* if no container object has been defined, we create a new
390 * empty container, and dump it
392 if (!priv->recent_items)
394 priv->recent_items = g_bookmark_file_new ();
399 g_bookmark_file_to_file (priv->recent_items,
404 filename_warning ("Attempting to store changes into `%s', "
407 write_error->message);
408 g_error_free (write_error);
411 /* we have sync'ed our list with the storage file, so we
412 * update the file mtime in order to skip the timed check
413 * and spare us from a re-read.
415 if (g_stat (priv->filename, &stat_buf) < 0)
417 filename_warning ("Unable to stat() the recently used resources file "
422 priv->write_in_progress = FALSE;
424 g_object_thaw_notify (G_OBJECT (manager));
429 priv->last_mtime = stat_buf.st_mtime;
431 /* mark us as clean */
432 priv->is_dirty = FALSE;
436 /* we are not marked as dirty, so we have been called
437 * because the recently used resources file has been
438 * changed (and not from us).
440 build_recent_items_list (manager);
443 g_object_thaw_notify (G_OBJECT (manager));
446 /* timed poll()-ing of the recently used resources file.
447 * an event-based system would be more efficient.
450 gtk_recent_manager_poll_timeout (gpointer data)
452 GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
453 GtkRecentManagerPrivate *priv = manager->priv;
454 struct stat stat_buf;
457 /* wait for the next timeout if we have a read/write in progress */
458 if (priv->write_in_progress || priv->read_in_progress)
461 stat_res = g_stat (priv->filename, &stat_buf);
464 /* the file does not exist, yet, so we wait */
468 filename_warning ("Unable to stat() the recently used resources file "
476 /* the file didn't change from the last poll(), so we bail out */
477 if (stat_buf.st_mtime == priv->last_mtime)
480 /* the file has been changed, hence we emit the "changed" signal */
481 gtk_recent_manager_changed (manager);
487 gtk_recent_manager_set_filename (GtkRecentManager *manager,
488 const gchar *filename)
490 GtkRecentManagerPrivate *priv;
492 g_assert (GTK_IS_RECENT_MANAGER (manager));
493 priv = manager->priv;
495 if (!filename || filename[0] == '\0')
498 g_free (manager->priv->filename);
500 if (manager->priv->poll_timeout)
502 g_source_remove (manager->priv->poll_timeout);
503 manager->priv->poll_timeout = 0;
506 priv->filename = g_strdup (filename);
507 priv->poll_timeout = g_timeout_add (POLL_DELTA,
508 gtk_recent_manager_poll_timeout,
511 /* mark us clean, so that we can re-read the list
512 * of recently used resources
514 priv->is_dirty = FALSE;
515 build_recent_items_list (manager);
518 /* reads the recently used resources file and builds the items list.
519 * we keep the items list inside the parser object, and build the
520 * RecentInfo object only on user's demand to avoid useless replication.
521 * this function resets the dirty bit of the manager.
524 build_recent_items_list (GtkRecentManager *manager)
526 GtkRecentManagerPrivate *priv;
527 struct stat stat_buf;
533 priv = manager->priv;
534 g_assert (priv->filename != NULL);
536 if (!priv->recent_items)
538 priv->recent_items = g_bookmark_file_new ();
542 stat_res = g_stat (priv->filename, &stat_buf);
545 /* the file doesn't exists, so we bail out and wait for the first
553 filename_warning ("Attempting to read the recently used resources file "
554 "at `%s', but an error occurred: %s. Aborting.",
562 /* record the last mtime, for later use */
563 priv->last_mtime = stat_buf.st_mtime;
565 priv->read_in_progress = TRUE;
567 /* the file exists, and it's valid (we hope); if not, destroy the container
568 * object and hope for a better result when the next "changed" signal is
571 res = g_bookmark_file_load_from_file (priv->recent_items,
576 filename_warning ("Attempting to read the recently used resources file "
577 "at `%s', but the parser failed: %s.",
579 read_error->message);
581 g_bookmark_file_free (priv->recent_items);
582 priv->recent_items = NULL;
584 g_error_free (read_error);
587 size = g_bookmark_file_get_size (priv->recent_items);
588 if (priv->size != size)
592 g_object_notify (G_OBJECT (manager), "size");
595 priv->read_in_progress = FALSE;
596 priv->is_dirty = FALSE;
600 /********************
602 ********************/
606 * gtk_recent_manager_new:
608 * Creates a new recent manager object. Recent manager objects are used to
609 * handle the list of recently used resources. A #GtkRecentManager object
610 * monitors the recently used resources list, and emits the "changed" signal
611 * each time something inside the list changes.
613 * #GtkRecentManager objects are expansive: be sure to create them only when
614 * needed. You should use the gtk_recent_manager_new_for_screen() or the
615 * gtk_recent_manager_get_default() functions instead.
617 * Return value: A newly created #GtkRecentManager object.
622 gtk_recent_manager_new (void)
624 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
628 * gtk_recent_manager_get_default:
630 * Gets the recent manager for the default screen. See
631 * gtk_recent_manager_get_for_screen().
633 * Return value: A unique #GtkRecentManager associated with the
634 * default screen. This recent manager is associated to the
635 * screen and can be used as long as the screen is open.
636 * Do no ref or unref it.
641 gtk_recent_manager_get_default (void)
643 return gtk_recent_manager_get_for_screen (gdk_screen_get_default ());
647 * gtk_recent_manager_get_for_screen:
648 * @screen: a #GdkScreen
650 * Gets the recent manager object associated with @screen; if this
651 * function has not previously been called for the given screen,
652 * a new recent manager object will be created and associated with
653 * the screen. Recent manager objects are fairly expensive to create,
654 * so using this function is usually a better choice than calling
655 * gtk_recent_manager_new() and setting the screen yourself; by using
656 * this function a single recent manager object will be shared between
659 * Return value: A unique #GtkRecentManager associated with the given
660 * screen. This recent manager is associated to the with the screen
661 * and can be used as long as the screen is open. Do not ref or
667 gtk_recent_manager_get_for_screen (GdkScreen *screen)
669 GtkRecentManager *manager;
671 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
672 g_return_val_if_fail (!screen->closed, NULL);
674 manager = g_object_get_data (G_OBJECT (screen), "gtk-recent-manager-default");
677 GtkRecentManagerPrivate *priv;
679 manager = gtk_recent_manager_new ();
680 gtk_recent_manager_set_screen (manager, screen);
682 priv = manager->priv;
683 priv->is_screen_singleton = TRUE;
685 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), manager);
692 display_closed (GdkDisplay *display,
694 GtkRecentManager *manager)
696 GtkRecentManagerPrivate *priv = manager->priv;
697 GdkScreen *screen = priv->screen;
698 gboolean was_screen_singleton = priv->is_screen_singleton;
700 if (was_screen_singleton)
702 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), NULL);
703 priv->is_screen_singleton = FALSE;
706 gtk_recent_manager_set_screen (manager, NULL);
708 if (was_screen_singleton)
709 g_object_unref (manager);
713 unset_screen (GtkRecentManager *manager)
715 GtkRecentManagerPrivate *priv = manager->priv;
720 display = gdk_screen_get_display (priv->screen);
722 g_signal_handlers_disconnect_by_func (display,
723 (gpointer) display_closed,
731 * gtk_recent_manager_set_screen:
732 * @manager: a #GtkRecentManager
733 * @screen: a #GdkScreen
735 * Sets the screen for a recent manager; the screen is used to
736 * track the user's currently configured recently used documents
742 gtk_recent_manager_set_screen (GtkRecentManager *manager,
745 GtkRecentManagerPrivate *priv;
748 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
749 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
751 priv = manager->priv;
753 unset_screen (manager);
757 display = gdk_screen_get_display (screen);
759 priv->screen = screen;
761 g_signal_connect (display, "closed",
762 G_CALLBACK (display_closed), manager);
767 * gtk_recent_manager_set_limit:
768 * @manager: a #GtkRecentManager
769 * @limit: the maximum number of items to return, or -1.
771 * Sets the maximum number of item that the gtk_recent_manager_get_items()
772 * function should return. If @limit is set to -1, then return all the
778 gtk_recent_manager_set_limit (GtkRecentManager *manager,
781 GtkRecentManagerPrivate *priv;
783 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
785 priv = manager->priv;
790 * gtk_recent_manager_get_limit:
791 * @manager: a #GtkRecentManager
793 * Gets the maximum number of items that the gtk_recent_manager_get_items()
794 * function should return.
796 * Return value: the number of items to return, or -1 for every item.
801 gtk_recent_manager_get_limit (GtkRecentManager *manager)
803 GtkRecentManagerPrivate *priv;
805 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
807 priv = manager->priv;
812 * gtk_recent_manager_add_item:
813 * @manager: a #GtkRecentManager
816 * Adds a new resource, pointed by @uri, into the recently used
819 * This function automatically retrieving some of the needed
820 * metadata and setting other metadata to common default values; it
821 * then feeds the data to gtk_recent_manager_add_full().
823 * See gtk_recent_manager_add_full() if you want to explicitely
824 * define the metadata for the resource pointed by @uri.
826 * Return value: %TRUE if the new item was successfully added
827 * to the recently used resources list
832 gtk_recent_manager_add_item (GtkRecentManager *manager,
835 GtkRecentData *recent_data;
839 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
840 g_return_val_if_fail (uri != NULL, FALSE);
842 recent_data = g_slice_new (GtkRecentData);
844 recent_data->display_name = NULL;
845 recent_data->description = NULL;
848 if (has_case_prefix (uri, "file:/"))
851 const gchar *mime_type;
853 filename = g_filename_from_uri (uri, NULL, NULL);
854 mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
856 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
858 recent_data->mime_type = g_strdup (mime_type);
864 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
866 recent_data->app_name = g_strdup (g_get_application_name ());
867 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
869 recent_data->groups = NULL;
871 recent_data->is_private = FALSE;
874 retval = gtk_recent_manager_add_full (manager, uri, recent_data);
876 g_free (recent_data->mime_type);
877 g_free (recent_data->app_name);
878 g_free (recent_data->app_exec);
880 g_slice_free (GtkRecentData, recent_data);
886 * gtk_recent_manager_add_full:
887 * @manager: a #GtkRecentManager
889 * @recent_data: metadata of the resource
891 * Adds a new resource, pointed by @uri, into the recently used
892 * resources list, using the metadata specified inside the #GtkRecentData
893 * structure passed in @recent_data.
895 * The passed URI will be used to identify this resource inside the
898 * In order to register the new recently used resource, metadata about
899 * the resource must be passed as well as the URI; the metadata is
900 * stored in a #GtkRecentData structure, which must contain the MIME
901 * type of the resource pointed by the URI; the name of the application
902 * that is registering the item, and a command line to be used when
903 * launching the item.
905 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
906 * to be used when viewing the item instead of the last component of the
907 * URI; a short description of the item; whether the item should be
908 * considered private - that is, should be displayed only by the
909 * applications that have registered it.
911 * Return value: %TRUE if the new item was successfully added to the
912 * recently used resources list, %FALSE otherwise.
917 gtk_recent_manager_add_full (GtkRecentManager *manager,
919 const GtkRecentData *data)
921 GtkRecentManagerPrivate *priv;
923 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
924 g_return_val_if_fail (uri != NULL, FALSE);
925 g_return_val_if_fail (data != NULL, FALSE);
928 if ((data->display_name) &&
929 (!g_utf8_validate (data->display_name, -1, NULL)))
931 g_warning ("Attempting to add `%s' to the list of recently used "
932 "resources, but the display name is not a valid UTF-8 "
938 if ((data->description) &&
939 (!g_utf8_validate (data->description, -1, NULL)))
941 g_warning ("Attempting to add `%s' to the list of recently used "
942 "resources, but the description is not a valid UTF-8 "
949 if (!data->mime_type)
951 g_warning ("Attempting to add `%s' to the list of recently used "
952 "resources, but not MIME type was defined",
959 g_warning ("Attempting to add `%s' to the list of recently used "
960 "resources, but no name of the application that is "
961 "registering it was defined",
968 g_warning ("Attempting to add `%s' to the list of recently used "
969 "resources, but no command line for the application "
970 "that is registering it was defined",
975 priv = manager->priv;
977 if (!priv->recent_items)
979 priv->recent_items = g_bookmark_file_new ();
983 if (data->display_name)
984 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
986 if (data->description)
987 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
989 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
991 if (data->groups && data->groups[0] != '\0')
995 for (j = 0; (data->groups)[j] != NULL; j++)
996 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
999 /* register the application; this will take care of updating the
1000 * registration count and time in case the application has
1001 * already registered the same document inside the list
1003 g_bookmark_file_add_application (priv->recent_items, uri,
1007 g_bookmark_file_set_is_private (priv->recent_items, uri,
1010 /* mark us as dirty, so that when emitting the "changed" signal we
1011 * will dump our changes
1013 priv->is_dirty = TRUE;
1015 gtk_recent_manager_changed (manager);
1021 * gtk_recent_manager_remove_item:
1022 * @manager: a #GtkRecentManager
1023 * @uri: the URI of the item you wish to remove
1024 * @error: return location for a #GError, or %NULL
1026 * Removes a resource pointed by @uri from the recently used resources
1027 * list handled by a recent manager.
1029 * Return value: %TRUE if the item pointed by @uri has been successfully
1030 * removed by the recently used resources list, and %FALSE otherwise.
1035 gtk_recent_manager_remove_item (GtkRecentManager *manager,
1039 GtkRecentManagerPrivate *priv;
1040 GError *remove_error = NULL;
1042 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1043 g_return_val_if_fail (uri != NULL, FALSE);
1044 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1046 priv = manager->priv;
1048 if (!priv->recent_items)
1050 priv->recent_items = g_bookmark_file_new ();
1053 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1054 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1055 _("Unable to find an item with URI '%s'"),
1061 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1064 g_propagate_error (error, remove_error);
1069 priv->is_dirty = TRUE;
1071 gtk_recent_manager_changed (manager);
1077 * gtk_recent_manager_has_item:
1078 * @manager: a #GtkRecentManager
1081 * Checks whether there is a recently used resource registered
1082 * with @uri inside the recent manager.
1084 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1089 gtk_recent_manager_has_item (GtkRecentManager *manager,
1092 GtkRecentManagerPrivate *priv;
1094 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1095 g_return_val_if_fail (uri != NULL, FALSE);
1097 priv = manager->priv;
1098 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1100 return g_bookmark_file_has_item (priv->recent_items, uri);
1104 build_recent_info (GBookmarkFile *bookmarks,
1105 GtkRecentInfo *info)
1107 gchar **apps, **groups;
1108 gsize apps_len, groups_len, i;
1110 g_assert (bookmarks != NULL);
1111 g_assert (info != NULL);
1113 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1114 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1115 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1117 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1119 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1120 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1121 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1123 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1124 for (i = 0; i < groups_len; i++)
1126 gchar *group_name = g_strdup (groups[i]);
1128 info->groups = g_slist_append (info->groups, group_name);
1131 g_strfreev (groups);
1133 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1134 for (i = 0; i < apps_len; i++)
1136 gchar *app_name, *app_exec;
1139 RecentAppInfo *app_info;
1144 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1152 app_info = recent_app_info_new (app_name);
1153 app_info->exec = app_exec;
1154 app_info->count = count;
1155 app_info->stamp = stamp;
1157 info->applications = g_slist_append (info->applications,
1159 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1168 * gtk_recent_manager_lookup_item:
1169 * @manager: a #GtkRecentManager
1171 * @error: a return location for a #GError, or %NULL
1173 * Searches for a URI inside the recently used resources list, and
1174 * returns a structure containing informations about the resource
1175 * like its MIME type, or its display name.
1177 * Return value: a #GtkRecentInfo structure containing information
1178 * about the resource pointed by @uri, or %NULL if the URI was
1179 * not registered in the recently used resources list. Free with
1180 * gtk_recent_info_unref().
1183 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1187 GtkRecentManagerPrivate *priv;
1188 GtkRecentInfo *info = NULL;
1191 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1192 g_return_val_if_fail (uri != NULL, NULL);
1193 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1195 priv = manager->priv;
1196 if (!priv->recent_items)
1198 priv->recent_items = g_bookmark_file_new ();
1201 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1202 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1203 _("Unable to find an item with URI '%s'"),
1209 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1211 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1212 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1213 _("Unable to find an item with URI '%s'"),
1218 info = gtk_recent_info_new (uri);
1219 g_return_val_if_fail (info != NULL, NULL);
1221 /* fill the RecentInfo structure with the data retrieved by our
1222 * parser object from the storage file
1224 res = build_recent_info (priv->recent_items, info);
1227 gtk_recent_info_free (info);
1232 return gtk_recent_info_ref (info);
1236 * gtk_recent_manager_move_item:
1237 * @manager: a #GtkRecentManager
1238 * @uri: the URI of a recently used resource
1239 * @new_uri: the new URI of the recently used resource, or %NULL to
1240 * remove the item pointed by @uri in the list
1241 * @error: a return location for a #GError, or %NULL
1243 * Changes the location of a recently used resource from @uri to @new_uri.
1245 * Please note that this function will not affect the resource pointed
1246 * by the URIs, but only the URI used in the recently used resources list.
1248 * Return value: %TRUE on success.
1253 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1255 const gchar *new_uri,
1258 GtkRecentManagerPrivate *priv;
1262 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1263 g_return_val_if_fail (uri != NULL, FALSE);
1264 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1266 priv = recent_manager->priv;
1268 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1270 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1271 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1272 _("Unable to find an item with URI '%s'"),
1278 res = g_bookmark_file_move_item (priv->recent_items,
1283 g_propagate_error (error, move_error);
1287 priv->is_dirty = TRUE;
1289 gtk_recent_manager_changed (recent_manager);
1295 * gtk_recent_manager_get_items:
1296 * @manager: a #GtkRecentManager
1298 * Gets the list of recently used resources.
1300 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1301 * gtk_recent_info_unref() on each item inside the list, and then
1302 * free the list itself using g_list_free().
1307 gtk_recent_manager_get_items (GtkRecentManager *manager)
1309 GtkRecentManagerPrivate *priv;
1310 GList *retval = NULL;
1314 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1316 priv = manager->priv;
1317 if (!priv->recent_items)
1320 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1321 for (i = 0; i < uris_len; i++)
1323 GtkRecentInfo *info;
1326 info = gtk_recent_info_new (uris[i]);
1327 res = build_recent_info (priv->recent_items, info);
1330 g_warning ("Unable to create a RecentInfo object for "
1331 "item with URI `%s'",
1333 gtk_recent_info_free (info);
1338 retval = g_list_prepend (retval, info);
1343 /* clamp the list, if a limit is present */
1344 if ((priv->limit != -1) &&
1345 (g_list_length (retval) > priv->limit))
1349 clamp = g_list_nth (retval, priv->limit - 1);
1357 g_list_foreach (l, (GFunc) gtk_recent_info_free, NULL);
1365 purge_recent_items_list (GtkRecentManager *manager,
1368 GtkRecentManagerPrivate *priv = manager->priv;
1370 if (!priv->recent_items)
1373 g_bookmark_file_free (priv->recent_items);
1374 priv->recent_items = NULL;
1376 priv->recent_items = g_bookmark_file_new ();
1378 priv->is_dirty = TRUE;
1380 /* emit the changed signal, to ensure that the purge is written */
1381 gtk_recent_manager_changed (manager);
1385 * gtk_recent_manager_purge_items:
1386 * @manager: a #GtkRecentManager
1387 * @error: a return location for a #GError, or %NULL
1389 * Purges every item from the recently used resources list.
1391 * Return value: the number of items that have been removed from the
1392 * recently used resources list.
1397 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1400 GtkRecentManagerPrivate *priv;
1403 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1405 priv = manager->priv;
1406 if (!priv->recent_items)
1409 count = g_bookmark_file_get_size (priv->recent_items);
1413 purge_recent_items_list (manager, error);
1415 purged = count - g_bookmark_file_get_size (priv->recent_items);
1421 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1423 g_signal_emit (recent_manager, signal_changed, 0);
1431 gtk_recent_info_get_type (void)
1433 static GType info_type = 0;
1436 info_type = g_boxed_type_register_static ("GtkRecentInfo",
1437 (GBoxedCopyFunc) gtk_recent_info_ref,
1438 (GBoxedFreeFunc) gtk_recent_info_unref);
1442 static GtkRecentInfo *
1443 gtk_recent_info_new (const gchar *uri)
1445 GtkRecentInfo *info;
1447 g_assert (uri != NULL);
1449 info = g_new0 (GtkRecentInfo, 1);
1450 info->uri = g_strdup (uri);
1452 info->applications = NULL;
1453 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1455 info->groups = NULL;
1457 info->ref_count = 1;
1463 gtk_recent_info_free (GtkRecentInfo *recent_info)
1468 g_free (recent_info->uri);
1469 g_free (recent_info->display_name);
1470 g_free (recent_info->description);
1471 g_free (recent_info->mime_type);
1473 if (recent_info->applications)
1475 g_slist_foreach (recent_info->applications,
1476 (GFunc) recent_app_info_free,
1478 g_slist_free (recent_info->applications);
1480 recent_info->applications = NULL;
1483 if (recent_info->apps_lookup)
1484 g_hash_table_destroy (recent_info->apps_lookup);
1486 if (recent_info->groups)
1488 g_slist_foreach (recent_info->groups,
1491 g_slist_free (recent_info->groups);
1493 recent_info->groups = NULL;
1496 if (recent_info->icon)
1497 g_object_unref (recent_info->icon);
1499 g_free (recent_info);
1503 * gtk_recent_info_ref:
1504 * @info: a #GtkRecentInfo
1506 * Increases the reference count of @recent_info by one.
1508 * Return value: the recent info object with its reference count increased
1514 gtk_recent_info_ref (GtkRecentInfo *info)
1516 g_return_val_if_fail (info != NULL, NULL);
1517 g_return_val_if_fail (info->ref_count > 0, NULL);
1519 info->ref_count += 1;
1525 * gtk_recent_info_unref:
1526 * @info: a #GtkRecentInfo
1528 * Decreases the reference count of @info by one. If the reference
1529 * count reaches zero, @info is deallocated, and the memory freed.
1534 gtk_recent_info_unref (GtkRecentInfo *info)
1536 g_return_if_fail (info != NULL);
1537 g_return_if_fail (info->ref_count > 0);
1539 info->ref_count -= 1;
1541 if (info->ref_count == 0)
1542 gtk_recent_info_free (info);
1546 * gtk_recent_info_get_uri:
1547 * @info: a #GtkRecentInfo
1549 * Gets the URI of the resource.
1551 * Return value: the URI of the resource. The returned string is
1552 * owned by the recent manager, and should not be freed.
1556 G_CONST_RETURN gchar *
1557 gtk_recent_info_get_uri (GtkRecentInfo *info)
1559 g_return_val_if_fail (info != NULL, NULL);
1565 * gtk_recent_info_get_display_name:
1566 * @info: a #GtkRecentInfo
1568 * Gets the name of the resource. If none has been defined, the basename
1569 * of the resource is obtained.
1571 * Return value: the display name of the resource. The returned string
1572 * is owned by the recent manager, and should not be freed.
1576 G_CONST_RETURN gchar *
1577 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1579 g_return_val_if_fail (info != NULL, NULL);
1581 if (!info->display_name)
1582 info->display_name = gtk_recent_info_get_short_name (info);
1584 return info->display_name;
1588 * gtk_recent_info_get_description:
1589 * @info: a #GtkRecentInfo
1591 * Gets the (short) description of the resource.
1593 * Return value: the description of the resource. The returned string
1594 * is owned by the recent manager, and should not be freed.
1598 G_CONST_RETURN gchar *
1599 gtk_recent_info_get_description (GtkRecentInfo *info)
1601 g_return_val_if_fail (info != NULL, NULL);
1603 return info->description;
1607 * gtk_recent_info_get_mime_type:
1608 * @info: a #GtkRecentInfo
1610 * Gets the MIME type of the resource.
1612 * Return value: the MIME type of the resource. The returned string
1613 * is owned by the recent manager, and should not be freed.
1617 G_CONST_RETURN gchar *
1618 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1620 g_return_val_if_fail (info != NULL, NULL);
1622 if (!info->mime_type)
1623 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1625 return info->mime_type;
1629 * gtk_recent_info_get_added:
1630 * @info: a #GtkRecentInfo
1632 * Gets the timestamp (seconds from system's Epoch) when the resource
1633 * was added to the recently used resources list.
1635 * Return value: the number of seconds elapsed from system's Epoch when
1636 * the resource was added to the list, or -1 on failure.
1641 gtk_recent_info_get_added (GtkRecentInfo *info)
1643 g_return_val_if_fail (info != NULL, (time_t) -1);
1649 * gtk_recent_info_get_modified:
1650 * @info: a #GtkRecentInfo
1652 * Gets the timestamp (seconds from system's Epoch) when the resource
1653 * was last modified.
1655 * Return value: the number of seconds elapsed from system's Epoch when
1656 * the resource was last modified, or -1 on failure.
1661 gtk_recent_info_get_modified (GtkRecentInfo *info)
1663 g_return_val_if_fail (info != NULL, (time_t) -1);
1665 return info->modified;
1669 * gtk_recent_info_get_visited:
1670 * @info: a #GtkRecentInfo
1672 * Gets the timestamp (seconds from system's Epoch) when the resource
1675 * Return value: the number of seconds elapsed from system's Epoch when
1676 * the resource was last visited, or -1 on failure.
1681 gtk_recent_info_get_visited (GtkRecentInfo *info)
1683 g_return_val_if_fail (info != NULL, (time_t) -1);
1685 return info->visited;
1689 * gtk_recent_info_get_private_hint:
1690 * @info: a #GtkRecentInfo
1692 * Gets the value of the "private" flag. Resources in the recently used
1693 * list that have this flag set to %TRUE should only be displayed by the
1694 * applications that have registered them.
1696 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1701 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1703 g_return_val_if_fail (info != NULL, FALSE);
1705 return info->is_private;
1709 static RecentAppInfo *
1710 recent_app_info_new (const gchar *app_name)
1712 RecentAppInfo *app_info;
1714 g_assert (app_name != NULL);
1716 app_info = g_new0 (RecentAppInfo, 1);
1717 app_info->name = g_strdup (app_name);
1718 app_info->exec = NULL;
1719 app_info->count = 1;
1720 app_info->stamp = time (NULL);
1726 recent_app_info_free (RecentAppInfo *app_info)
1732 g_free (app_info->name);
1735 g_free (app_info->exec);
1741 * gtk_recent_info_get_application_info:
1742 * @info: a #GtkRecentInfo
1743 * @app_name: the name of the application that has registered this item
1744 * @app_exec: return location for the string containing the command line
1745 * @count: return location for the number of times this item was registered
1746 * @time_: return location for the timestamp this item was last registered
1747 * for this application
1749 * Gets the data regarding the application that has registered the resource
1752 * If the command line contains any escape characters defined inside the
1753 * storage specification, they will be expanded.
1755 * Return value: %TRUE if an application with @app_name has registered this
1756 * resource inside the recently used list, or %FALSE otherwise. You should
1757 * free the returned command line using g_free().
1762 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1763 const gchar *app_name,
1770 g_return_val_if_fail (info != NULL, FALSE);
1771 g_return_val_if_fail (app_name != NULL, FALSE);
1773 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1777 g_warning ("No registered application with name '%s' "
1778 "for item with URI '%s' found",
1785 *app_exec = ai->exec;
1797 * gtk_recent_info_get_applications:
1798 * @info: a #GtkRecentInfo
1799 * @length: return location for the length of the returned list, or %NULL
1801 * Retrieves the list of applications that have registered this resource.
1803 * Return value: a newly allocated %NULL-terminated array of strings.
1804 * Use g_strfreev() to free it.
1809 gtk_recent_info_get_applications (GtkRecentInfo *info,
1816 g_return_val_if_fail (info != NULL, NULL);
1818 if (!info->applications)
1826 n_apps = g_slist_length (info->applications);
1828 retval = g_new0 (gchar *, n_apps + 1);
1830 for (l = info->applications, i = 0;
1834 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1836 g_assert (ai != NULL);
1838 retval[i++] = g_strdup (ai->name);
1849 * gtk_recent_info_has_application:
1850 * @info: a #GtkRecentInfo
1851 * @app_name: a string containing an application name
1853 * Checks whether an application registered this resource using @app_name.
1855 * Return value: %TRUE if an application with name @app_name was found,
1861 gtk_recent_info_has_application (GtkRecentInfo *info,
1862 const gchar *app_name)
1864 g_return_val_if_fail (info != NULL, FALSE);
1865 g_return_val_if_fail (app_name != NULL, FALSE);
1867 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1871 * gtk_recent_info_last_application:
1872 * @info: a #GtkRecentInfo
1874 * Gets the name of the last application that have registered the
1875 * recently used resource represented by @info.
1877 * Return value: an application name. Use g_free() to free it.
1882 gtk_recent_info_last_application (GtkRecentInfo *info)
1885 time_t last_stamp = (time_t) -1;
1888 g_return_val_if_fail (info != NULL, NULL);
1890 for (l = info->applications; l != NULL; l = l->next)
1892 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1894 if (ai->stamp > last_stamp)
1897 last_stamp = ai->stamp;
1901 return g_strdup (name);
1911 icon_cache_element_free (IconCacheElement *element)
1913 if (element->pixbuf)
1914 g_object_unref (element->pixbuf);
1919 icon_theme_changed (GtkIconTheme *icon_theme)
1923 /* Difference from the initial creation is that we don't
1924 * reconnect the signal
1926 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1927 (GDestroyNotify)g_free,
1928 (GDestroyNotify)icon_cache_element_free);
1929 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1930 cache, (GDestroyNotify)g_hash_table_destroy);
1933 /* TODO: use the GtkFileChooser's icon cache instead of our own to reduce
1934 * the memory footprint
1937 get_cached_icon (const gchar *name,
1940 GtkIconTheme *icon_theme;
1942 IconCacheElement *element;
1944 icon_theme = gtk_icon_theme_get_default ();
1945 cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-recent-icon-cache");
1949 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1950 (GDestroyNotify)g_free,
1951 (GDestroyNotify)icon_cache_element_free);
1953 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1954 cache, (GDestroyNotify)g_hash_table_destroy);
1955 g_signal_connect (icon_theme, "changed",
1956 G_CALLBACK (icon_theme_changed), NULL);
1959 element = g_hash_table_lookup (cache, name);
1962 element = g_new0 (IconCacheElement, 1);
1963 g_hash_table_insert (cache, g_strdup (name), element);
1966 if (element->size != pixel_size)
1968 if (element->pixbuf)
1969 g_object_unref (element->pixbuf);
1971 element->size = pixel_size;
1972 element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
1973 pixel_size, 0, NULL);
1976 return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
1981 get_icon_for_mime_type (const char *mime_type,
1984 const char *separator;
1988 separator = strchr (mime_type, '/');
1990 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
1992 icon_name = g_string_new ("gnome-mime-");
1993 g_string_append_len (icon_name, mime_type, separator - mime_type);
1994 g_string_append_c (icon_name, '-');
1995 g_string_append (icon_name, separator + 1);
1996 pixbuf = get_cached_icon (icon_name->str, pixel_size);
1997 g_string_free (icon_name, TRUE);
2001 icon_name = g_string_new ("gnome-mime-");
2002 g_string_append_len (icon_name, mime_type, separator - mime_type);
2003 pixbuf = get_cached_icon (icon_name->str, pixel_size);
2004 g_string_free (icon_name, TRUE);
2010 get_icon_fallback (const gchar *icon_name,
2013 GtkIconTheme *icon_theme;
2016 icon_theme = gtk_icon_theme_get_default ();
2018 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
2020 GTK_ICON_LOOKUP_USE_BUILTIN,
2022 g_assert (retval != NULL);
2028 * gtk_recent_info_get_icon:
2029 * @info: a #GtkRecentInfo
2030 * @size: the size of the icon in pixels
2032 * Retrieves the icon of size @size associated to the resource MIME type.
2034 * Return value: a #GdkPixbuf containing the icon, or %NULL.
2039 gtk_recent_info_get_icon (GtkRecentInfo *info,
2042 GdkPixbuf *retval = NULL;
2044 g_return_val_if_fail (info != NULL, NULL);
2046 if (info->mime_type)
2047 retval = get_icon_for_mime_type (info->mime_type, size);
2049 /* this should never fail */
2051 retval = get_icon_fallback (GTK_STOCK_FILE, size);
2057 * gtk_recent_info_is_local:
2058 * @info: a #GtkRecentInfo
2060 * Checks whether the resource is local or not by looking at the
2061 * scheme of its URI.
2063 * Return value: %TRUE if the resource is local.
2068 gtk_recent_info_is_local (GtkRecentInfo *info)
2070 g_return_val_if_fail (info != NULL, FALSE);
2072 return has_case_prefix (info->uri, "file:/");
2076 * gtk_recent_info_exists:
2077 * @info: a #GtkRecentInfo
2079 * Checks whether the resource pointed by @info still exists. At
2080 * the moment this check is done only on resources pointing to local files.
2082 * Return value: %TRUE if the resource exists
2087 gtk_recent_info_exists (GtkRecentInfo *info)
2090 struct stat stat_buf;
2091 gboolean retval = FALSE;
2093 g_return_val_if_fail (info != NULL, FALSE);
2095 /* we guarantee only local resources */
2096 if (!gtk_recent_info_is_local (info))
2099 filename = g_filename_from_uri (info->uri, NULL, NULL);
2102 if (stat (filename, &stat_buf) == 0)
2112 * gtk_recent_info_match:
2113 * @info_a: a #GtkRecentInfo
2114 * @info_b: a #GtkRecentInfo
2116 * Checks whether two #GtkRecentInfo structures point to the same
2119 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2120 * resource, %FALSE otherwise.
2125 gtk_recent_info_match (GtkRecentInfo *info_a,
2126 GtkRecentInfo *info_b)
2128 g_return_val_if_fail (info_a != NULL, FALSE);
2129 g_return_val_if_fail (info_b != NULL, FALSE);
2131 return (0 == strcmp (info_a->uri, info_b->uri));
2134 /* taken from gnome-vfs-uri.c */
2135 static const gchar *
2136 get_method_string (const gchar *substring,
2137 gchar **method_string)
2143 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2150 !(p == substring + 1 && g_ascii_isalpha (*substring))
2154 /* Found toplevel method specification. */
2155 method = g_strndup (substring, p - substring);
2156 *method_string = g_ascii_strdown (method, -1);
2162 *method_string = g_strdup ("file");
2169 /* Stolen from gnome_vfs_make_valid_utf8() */
2171 make_valid_utf8 (const char *name)
2174 const char *remainder, *invalid;
2175 int remaining_bytes, valid_bytes;
2179 remaining_bytes = name ? strlen (name) : 0;
2181 while (remaining_bytes != 0)
2183 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2186 valid_bytes = invalid - remainder;
2189 string = g_string_sized_new (remaining_bytes);
2191 g_string_append_len (string, remainder, valid_bytes);
2192 g_string_append_c (string, '?');
2194 remaining_bytes -= valid_bytes + 1;
2195 remainder = invalid + 1;
2199 return g_strdup (name);
2201 g_string_append (string, remainder);
2202 g_assert (g_utf8_validate (string->str, -1, NULL));
2204 return g_string_free (string, FALSE);
2208 get_uri_shortname_for_display (const gchar *uri)
2211 gboolean validated = FALSE;
2213 if (has_case_prefix (uri, "file:/"))
2217 local_file = g_filename_from_uri (uri, NULL, NULL);
2219 if (local_file != NULL)
2221 name = g_filename_display_basename (local_file);
2225 g_free (local_file);
2233 rest = get_method_string (uri, &method);
2234 local_file = g_filename_display_basename (rest);
2236 name = g_strdup_printf ("%s: %s", method, local_file);
2238 g_free (local_file);
2242 g_assert (name != NULL);
2244 if (!validated && !g_utf8_validate (name, -1, NULL))
2248 utf8_name = make_valid_utf8 (name);
2258 * gtk_recent_info_get_short_name:
2259 * @info: an #GtkRecentInfo
2261 * Computes a valid UTF-8 string that can be used as the name of the item in a
2262 * menu or list. For example, calling this function on an item that refers to
2263 * "file:///foo/bar.txt" will yield "bar.txt".
2265 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2271 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2275 g_return_val_if_fail (info != NULL, NULL);
2277 if (info->uri == NULL)
2280 short_name = get_uri_shortname_for_display (info->uri);
2286 * gtk_recent_info_get_uri_display:
2287 * @info: a #GtkRecentInfo
2289 * Gets a displayable version of the resource's URI.
2291 * Return value: a UTF-8 string containing the resource's URI or %NULL
2296 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2298 gchar *filename, *filename_utf8;
2300 g_return_val_if_fail (info != NULL, NULL);
2302 filename = g_filename_from_uri (info->uri, NULL, NULL);
2306 filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2309 return filename_utf8;
2313 * gtk_recent_info_get_age:
2314 * @info: a #GtkRecentInfo
2316 * Gets the number of days elapsed since the last update of the resource
2319 * Return value: a positive integer containing the number of days elapsed
2320 * since the time this resource was last modified.
2325 gtk_recent_info_get_age (GtkRecentInfo *info)
2330 g_return_val_if_fail (info != NULL, -1);
2334 delta = now - info->modified;
2336 retval = (gint) (delta / (60 * 60 * 24));
2342 * gtk_recent_info_get_groups:
2343 * @info: a #GtkRecentInfo
2344 * @length: return location for the number of groups returned, or %NULL
2346 * Returns all groups registered for the recently used item @info. The
2347 * array of returned group names will be %NULL terminated, so length might
2348 * optionally be %NULL.
2350 * Return value: a newly allocated %NULL terminated array of strings. Use
2351 * g_strfreev() to free it.
2356 gtk_recent_info_get_groups (GtkRecentInfo *info,
2363 g_return_val_if_fail (info != NULL, NULL);
2373 n_groups = g_slist_length (info->groups);
2375 retval = g_new0 (gchar *, n_groups + 1);
2377 for (l = info->groups, i = 0;
2381 gchar *group_name = (gchar *) l->data;
2383 g_assert (group_name != NULL);
2385 retval[i++] = g_strdup (group_name);
2396 * gtk_recent_info_has_group:
2397 * @info: a #GtkRecentInfo
2398 * @group_name: name of a group
2400 * Checks whether @group_name appears inside the groups registered for the
2401 * recently used item @info.
2403 * Return value: %TRUE if the group was found.
2408 gtk_recent_info_has_group (GtkRecentInfo *info,
2409 const gchar *group_name)
2413 g_return_val_if_fail (info != NULL, FALSE);
2414 g_return_val_if_fail (group_name != NULL, FALSE);
2419 for (l = info->groups; l != NULL; l = l->next)
2421 gchar *g = (gchar *) l->data;
2423 if (strcmp (g, group_name) == 0)
2430 #define __GTK_RECENT_MANAGER_C__
2431 #include "gtkaliasdef.c"