1 /* GTK - The GIMP Toolkit
2 * gtkrecentmanager.c: a manager for the recently used resources
4 * Copyright (C) 2006 Emmanuele Bassi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 #include <sys/types.h>
32 #include <glib/gstdio.h>
34 #include "gtkrecentmanager.h"
37 #include "gtkicontheme.h"
38 #include "gtktypebuiltins.h"
39 #include "gtkprivate.h"
40 #include "gtkmarshalers.h"
44 #define XDG_PREFIX _gtk_xdg
45 #include "xdgmime/xdgmime.h"
48 /* the file where we store the recently used items */
49 #define GTK_RECENTLY_USED_FILE ".recently-used.xbel"
51 /* a poll every two seconds should be enough */
52 #define POLL_DELTA 2000
54 /* return all items by default */
55 #define DEFAULT_LIMIT -1
57 /* keep in sync with xdgmime */
58 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
84 GHashTable *apps_lookup;
95 struct _GtkRecentManagerPrivate
99 guint is_screen_singleton : 1;
101 guint write_in_progress : 1;
102 guint read_in_progress : 1;
109 GBookmarkFile *recent_items;
124 static void gtk_recent_manager_finalize (GObject *object);
126 static void gtk_recent_manager_set_property (GObject *object,
130 static void gtk_recent_manager_get_property (GObject *object,
134 static void gtk_recent_manager_changed (GtkRecentManager *manager);
136 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
137 static gboolean gtk_recent_manager_poll_timeout (gpointer data);
138 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
139 const gchar *filename);
141 static void build_recent_items_list (GtkRecentManager *manager);
142 static void purge_recent_items_list (GtkRecentManager *manager,
145 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
146 static void recent_app_info_free (RecentAppInfo *app_info);
148 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
149 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
151 static guint signal_changed = 0;
153 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
156 filename_warning (const gchar *format,
157 const gchar *filename,
158 const gchar *message)
160 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
161 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
165 /* Test of haystack has the needle prefix, comparing case
166 * insensitive. haystack may be UTF-8, but needle must
167 * contain only lowercase ascii. */
169 has_case_prefix (const gchar *haystack,
174 /* Eat one character at a time. */
178 while (*n && *h && *n == g_ascii_tolower (*h))
188 gtk_recent_manager_error_quark (void)
190 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
195 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
197 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
199 gtk_recent_manager_parent_class = g_type_class_peek_parent (klass);
201 gobject_class->set_property = gtk_recent_manager_set_property;
202 gobject_class->get_property = gtk_recent_manager_get_property;
203 gobject_class->finalize = gtk_recent_manager_finalize;
206 * GtkRecentManager:filename
208 * The full path to the file to be used to store and read the recently
209 * used resources list
213 g_object_class_install_property (gobject_class,
215 g_param_spec_string ("filename",
217 P_("The full path to the file to be used to store and read the list"),
219 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_WRITABLE)));
221 * GtkRecentManager:limit
223 * The maximum number of items to be returned by the
224 * gtk_recent_manager_get_items() function.
228 g_object_class_install_property (gobject_class,
230 g_param_spec_int ("limit",
232 P_("The maximum number of items to be returned by gtk_recent_manager_get_items()"),
238 * GtkRecentManager:size
240 * The size of the recently used resources list.
244 g_object_class_install_property (gobject_class,
246 g_param_spec_int ("size",
248 P_("The size of the recently used resources list"),
255 * GtkRecentManager::changed
256 * @recent_manager: the recent manager
258 * Emitted when the current recently used resources manager changes its
264 g_signal_new (I_("changed"),
265 G_TYPE_FROM_CLASS (klass),
267 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
269 g_cclosure_marshal_VOID__VOID,
272 klass->changed = gtk_recent_manager_real_changed;
274 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
278 gtk_recent_manager_init (GtkRecentManager *manager)
280 GtkRecentManagerPrivate *priv;
282 priv = g_type_instance_get_private ((GTypeInstance *) manager,
283 GTK_TYPE_RECENT_MANAGER);
284 manager->priv = priv;
286 priv->limit = DEFAULT_LIMIT;
289 priv->is_screen_singleton = FALSE;
290 priv->is_dirty = FALSE;
291 priv->write_in_progress = FALSE;
292 priv->read_in_progress = FALSE;
296 priv->filename = g_build_filename (g_get_home_dir (),
297 GTK_RECENTLY_USED_FILE,
299 priv->poll_timeout = g_timeout_add (POLL_DELTA,
300 gtk_recent_manager_poll_timeout,
303 build_recent_items_list (manager);
307 gtk_recent_manager_set_property (GObject *object,
312 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
317 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
320 gtk_recent_manager_set_limit (recent_manager, g_value_get_int (value));
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gtk_recent_manager_get_property (GObject *object,
334 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
339 g_value_set_string (value, recent_manager->priv->filename);
342 g_value_set_int (value, recent_manager->priv->limit);
345 g_value_set_int (value, recent_manager->priv->size);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gtk_recent_manager_finalize (GObject *object)
356 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
357 GtkRecentManagerPrivate *priv = manager->priv;
359 /* remove the poll timeout */
360 if (priv->poll_timeout)
361 g_source_remove (priv->poll_timeout);
364 g_free (priv->filename);
366 if (priv->recent_items)
367 g_bookmark_file_free (priv->recent_items);
369 /* chain up parent's finalize method */
370 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
374 gtk_recent_manager_real_changed (GtkRecentManager *manager)
376 GtkRecentManagerPrivate *priv = manager->priv;
378 g_object_freeze_notify (G_OBJECT (manager));
383 struct stat stat_buf;
385 /* we are marked as dirty, so we dump the content of our
386 * recently used items list
388 g_assert (priv->filename != NULL);
390 priv->write_in_progress = TRUE;
392 /* if no container object has been defined, we create a new
393 * empty container, and dump it
395 if (!priv->recent_items)
397 priv->recent_items = g_bookmark_file_new ();
402 g_bookmark_file_to_file (priv->recent_items,
408 filename_warning ("Attempting to store changes into `%s', "
411 write_error->message);
412 g_error_free (write_error);
415 priv->write_in_progress = FALSE;
417 /* we have sync'ed our list with the storage file, so we
418 * update the file mtime in order to skip the timed check
419 * and spare us from a re-read.
421 if (g_stat (priv->filename, &stat_buf) < 0)
423 filename_warning ("Unable to stat() the recently used resources file "
428 g_object_thaw_notify (G_OBJECT (manager));
433 priv->last_mtime = stat_buf.st_mtime;
435 /* mark us as clean */
436 priv->is_dirty = FALSE;
440 /* we are not marked as dirty, so we have been called
441 * because the recently used resources file has been
442 * changed (and not from us).
444 build_recent_items_list (manager);
447 g_object_thaw_notify (G_OBJECT (manager));
450 /* timed poll()-ing of the recently used resources file.
451 * an event-based system would be more efficient.
454 gtk_recent_manager_poll_timeout (gpointer data)
456 GtkRecentManager *manager = GTK_RECENT_MANAGER (data);
457 GtkRecentManagerPrivate *priv = manager->priv;
458 struct stat stat_buf;
461 /* wait for the next timeout if we have a read/write in progress */
462 if (priv->write_in_progress || priv->read_in_progress)
465 stat_res = g_stat (priv->filename, &stat_buf);
468 /* the file does not exist, yet, so we wait */
472 filename_warning ("Unable to stat() the recently used resources file "
480 /* the file didn't change from the last poll(), so we bail out */
481 if (stat_buf.st_mtime == priv->last_mtime)
484 /* the file has been changed, hence we emit the "changed" signal */
485 gtk_recent_manager_changed (manager);
491 gtk_recent_manager_set_filename (GtkRecentManager *manager,
492 const gchar *filename)
494 GtkRecentManagerPrivate *priv;
496 g_assert (GTK_IS_RECENT_MANAGER (manager));
497 priv = manager->priv;
499 if (!filename || filename[0] == '\0')
502 g_free (manager->priv->filename);
504 if (manager->priv->poll_timeout)
506 g_source_remove (manager->priv->poll_timeout);
507 manager->priv->poll_timeout = 0;
510 priv->filename = g_strdup (filename);
511 priv->poll_timeout = g_timeout_add (POLL_DELTA,
512 gtk_recent_manager_poll_timeout,
515 /* mark us clean, so that we can re-read the list
516 * of recently used resources
518 priv->is_dirty = FALSE;
519 build_recent_items_list (manager);
522 /* reads the recently used resources file and builds the items list.
523 * we keep the items list inside the parser object, and build the
524 * RecentInfo object only on user's demand to avoid useless replication.
525 * this function resets the dirty bit of the manager.
528 build_recent_items_list (GtkRecentManager *manager)
530 GtkRecentManagerPrivate *priv;
531 struct stat stat_buf;
537 priv = manager->priv;
538 g_assert (priv->filename != NULL);
540 if (!priv->recent_items)
542 priv->recent_items = g_bookmark_file_new ();
546 stat_res = g_stat (priv->filename, &stat_buf);
549 /* the file doesn't exists, so we bail out and wait for the first
557 filename_warning ("Attempting to read the recently used resources file "
558 "at `%s', but an error occurred: %s. Aborting.",
566 /* record the last mtime, for later use */
567 priv->last_mtime = stat_buf.st_mtime;
569 priv->read_in_progress = TRUE;
571 /* the file exists, and it's valid (we hope); if not, destroy the container
572 * object and hope for a better result when the next "changed" signal is
575 res = g_bookmark_file_load_from_file (priv->recent_items,
580 filename_warning ("Attempting to read the recently used resources file "
581 "at `%s', but the parser failed: %s.",
583 read_error->message);
585 g_bookmark_file_free (priv->recent_items);
586 priv->recent_items = NULL;
588 g_error_free (read_error);
591 size = g_bookmark_file_get_size (priv->recent_items);
592 if (priv->size != size)
596 g_object_notify (G_OBJECT (manager), "size");
599 priv->read_in_progress = FALSE;
600 priv->is_dirty = FALSE;
604 /********************
606 ********************/
610 * gtk_recent_manager_new:
612 * Creates a new recent manager object. Recent manager objects are used to
613 * handle the list of recently used resources. A #GtkRecentManager object
614 * monitors the recently used resources list, and emits the "changed" signal
615 * each time something inside the list changes.
617 * #GtkRecentManager objects are expansive: be sure to create them only when
618 * needed. You should use the gtk_recent_manager_new_for_screen() or the
619 * gtk_recent_manager_get_default() functions instead.
621 * Return value: A newly created #GtkRecentManager object.
626 gtk_recent_manager_new (void)
628 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
632 * gtk_recent_manager_get_default:
634 * Gets the recent manager for the default screen. See
635 * gtk_recent_manager_get_for_screen().
637 * Return value: A unique #GtkRecentManager associated with the
638 * default screen. This recent manager is associated to the
639 * screen and can be used as long as the screen is open.
640 * Do no ref or unref it.
645 gtk_recent_manager_get_default (void)
647 return gtk_recent_manager_get_for_screen (gdk_screen_get_default ());
651 * gtk_recent_manager_get_for_screen:
652 * @screen: a #GdkScreen
654 * Gets the recent manager object associated with @screen; if this
655 * function has not previously been called for the given screen,
656 * a new recent manager object will be created and associated with
657 * the screen. Recent manager objects are fairly expensive to create,
658 * so using this function is usually a better choice than calling
659 * gtk_recent_manager_new() and setting the screen yourself; by using
660 * this function a single recent manager object will be shared between
663 * Return value: A unique #GtkRecentManager associated with the given
664 * screen. This recent manager is associated to the with the screen
665 * and can be used as long as the screen is open. Do not ref or
671 gtk_recent_manager_get_for_screen (GdkScreen *screen)
673 GtkRecentManager *manager;
675 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
676 g_return_val_if_fail (!screen->closed, NULL);
678 manager = g_object_get_data (G_OBJECT (screen), "gtk-recent-manager-default");
681 GtkRecentManagerPrivate *priv;
683 manager = gtk_recent_manager_new ();
684 gtk_recent_manager_set_screen (manager, screen);
686 priv = manager->priv;
687 priv->is_screen_singleton = TRUE;
689 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), manager);
696 display_closed (GdkDisplay *display,
698 GtkRecentManager *manager)
700 GtkRecentManagerPrivate *priv = manager->priv;
701 GdkScreen *screen = priv->screen;
702 gboolean was_screen_singleton = priv->is_screen_singleton;
704 if (was_screen_singleton)
706 g_object_set_data (G_OBJECT (screen), I_("gtk-recent-manager-default"), NULL);
707 priv->is_screen_singleton = FALSE;
710 gtk_recent_manager_set_screen (manager, NULL);
712 if (was_screen_singleton)
713 g_object_unref (manager);
717 unset_screen (GtkRecentManager *manager)
719 GtkRecentManagerPrivate *priv = manager->priv;
724 display = gdk_screen_get_display (priv->screen);
726 g_signal_handlers_disconnect_by_func (display,
727 (gpointer) display_closed,
735 * gtk_recent_manager_set_screen:
736 * @manager: a #GtkRecentManager
737 * @screen: a #GdkScreen
739 * Sets the screen for a recent manager; the screen is used to
740 * track the user's currently configured recently used documents
746 gtk_recent_manager_set_screen (GtkRecentManager *manager,
749 GtkRecentManagerPrivate *priv;
752 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
753 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
755 priv = manager->priv;
757 unset_screen (manager);
761 display = gdk_screen_get_display (screen);
763 priv->screen = screen;
765 g_signal_connect (display, "closed",
766 G_CALLBACK (display_closed), manager);
771 * gtk_recent_manager_set_limit:
772 * @manager: a #GtkRecentManager
773 * @limit: the maximum number of items to return, or -1.
775 * Sets the maximum number of item that the gtk_recent_manager_get_items()
776 * function should return. If @limit is set to -1, then return all the
782 gtk_recent_manager_set_limit (GtkRecentManager *manager,
785 GtkRecentManagerPrivate *priv;
787 g_return_if_fail (GTK_IS_RECENT_MANAGER (manager));
789 priv = manager->priv;
794 * gtk_recent_manager_get_limit:
795 * @manager: a #GtkRecentManager
797 * Gets the maximum number of items that the gtk_recent_manager_get_items()
798 * function should return.
800 * Return value: the number of items to return, or -1 for every item.
805 gtk_recent_manager_get_limit (GtkRecentManager *manager)
807 GtkRecentManagerPrivate *priv;
809 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), DEFAULT_LIMIT);
811 priv = manager->priv;
816 * gtk_recent_manager_add_item:
817 * @manager: a #GtkRecentManager
820 * Adds a new resource, pointed by @uri, into the recently used
823 * This function automatically retrieving some of the needed
824 * metadata and setting other metadata to common default values; it
825 * then feeds the data to gtk_recent_manager_add_full().
827 * See gtk_recent_manager_add_full() if you want to explicitely
828 * define the metadata for the resource pointed by @uri.
830 * Return value: %TRUE if the new item was successfully added
831 * to the recently used resources list
836 gtk_recent_manager_add_item (GtkRecentManager *manager,
839 GtkRecentData *recent_data;
843 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
844 g_return_val_if_fail (uri != NULL, FALSE);
846 recent_data = g_slice_new (GtkRecentData);
848 recent_data->display_name = NULL;
849 recent_data->description = NULL;
852 if (has_case_prefix (uri, "file:/"))
855 const gchar *mime_type;
857 filename = g_filename_from_uri (uri, NULL, NULL);
858 mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
860 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
862 recent_data->mime_type = g_strdup (mime_type);
868 recent_data->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
870 recent_data->app_name = g_strdup (g_get_application_name ());
871 recent_data->app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
873 recent_data->groups = NULL;
875 recent_data->is_private = FALSE;
878 retval = gtk_recent_manager_add_full (manager, uri, recent_data);
880 g_free (recent_data->mime_type);
881 g_free (recent_data->app_name);
882 g_free (recent_data->app_exec);
884 g_slice_free (GtkRecentData, recent_data);
890 * gtk_recent_manager_add_full:
891 * @manager: a #GtkRecentManager
893 * @recent_data: metadata of the resource
895 * Adds a new resource, pointed by @uri, into the recently used
896 * resources list, using the metadata specified inside the #GtkRecentData
897 * structure passed in @recent_data.
899 * The passed URI will be used to identify this resource inside the
902 * In order to register the new recently used resource, metadata about
903 * the resource must be passed as well as the URI; the metadata is
904 * stored in a #GtkRecentData structure, which must contain the MIME
905 * type of the resource pointed by the URI; the name of the application
906 * that is registering the item, and a command line to be used when
907 * launching the item.
909 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
910 * to be used when viewing the item instead of the last component of the
911 * URI; a short description of the item; whether the item should be
912 * considered private - that is, should be displayed only by the
913 * applications that have registered it.
915 * Return value: %TRUE if the new item was successfully added to the
916 * recently used resources list, %FALSE otherwise.
921 gtk_recent_manager_add_full (GtkRecentManager *manager,
923 const GtkRecentData *data)
925 GtkRecentManagerPrivate *priv;
927 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
928 g_return_val_if_fail (uri != NULL, FALSE);
929 g_return_val_if_fail (data != NULL, FALSE);
932 if ((data->display_name) &&
933 (!g_utf8_validate (data->display_name, -1, NULL)))
935 g_warning ("Attempting to add `%s' to the list of recently used "
936 "resources, but the display name is not a valid UTF-8 "
942 if ((data->description) &&
943 (!g_utf8_validate (data->description, -1, NULL)))
945 g_warning ("Attempting to add `%s' to the list of recently used "
946 "resources, but the description is not a valid UTF-8 "
953 if (!data->mime_type)
955 g_warning ("Attempting to add `%s' to the list of recently used "
956 "resources, but not MIME type was defined",
963 g_warning ("Attempting to add `%s' to the list of recently used "
964 "resources, but no name of the application that is "
965 "registering it was defined",
972 g_warning ("Attempting to add `%s' to the list of recently used "
973 "resources, but no command line for the application "
974 "that is registering it was defined",
979 priv = manager->priv;
981 if (!priv->recent_items)
983 priv->recent_items = g_bookmark_file_new ();
987 if (data->display_name)
988 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
990 if (data->description)
991 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
993 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
995 if (data->groups && data->groups[0] != '\0')
999 for (j = 0; (data->groups)[j] != NULL; j++)
1000 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
1003 /* register the application; this will take care of updating the
1004 * registration count and time in case the application has
1005 * already registered the same document inside the list
1007 g_bookmark_file_add_application (priv->recent_items, uri,
1011 g_bookmark_file_set_is_private (priv->recent_items, uri,
1014 /* mark us as dirty, so that when emitting the "changed" signal we
1015 * will dump our changes
1017 priv->is_dirty = TRUE;
1019 gtk_recent_manager_changed (manager);
1025 * gtk_recent_manager_remove_item:
1026 * @manager: a #GtkRecentManager
1027 * @uri: the URI of the item you wish to remove
1028 * @error: return location for a #GError, or %NULL
1030 * Removes a resource pointed by @uri from the recently used resources
1031 * list handled by a recent manager.
1033 * Return value: %TRUE if the item pointed by @uri has been successfully
1034 * removed by the recently used resources list, and %FALSE otherwise.
1039 gtk_recent_manager_remove_item (GtkRecentManager *manager,
1043 GtkRecentManagerPrivate *priv;
1044 GError *remove_error = NULL;
1046 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1047 g_return_val_if_fail (uri != NULL, FALSE);
1048 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1050 priv = manager->priv;
1052 if (!priv->recent_items)
1054 priv->recent_items = g_bookmark_file_new ();
1057 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1058 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1059 _("Unable to find an item with URI '%s'"),
1065 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1068 g_propagate_error (error, remove_error);
1073 priv->is_dirty = TRUE;
1075 gtk_recent_manager_changed (manager);
1081 * gtk_recent_manager_has_item:
1082 * @manager: a #GtkRecentManager
1085 * Checks whether there is a recently used resource registered
1086 * with @uri inside the recent manager.
1088 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1093 gtk_recent_manager_has_item (GtkRecentManager *manager,
1096 GtkRecentManagerPrivate *priv;
1098 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1099 g_return_val_if_fail (uri != NULL, FALSE);
1101 priv = manager->priv;
1102 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1104 return g_bookmark_file_has_item (priv->recent_items, uri);
1108 build_recent_info (GBookmarkFile *bookmarks,
1109 GtkRecentInfo *info)
1111 gchar **apps, **groups;
1112 gsize apps_len, groups_len, i;
1114 g_assert (bookmarks != NULL);
1115 g_assert (info != NULL);
1117 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1118 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1119 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1121 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1123 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1124 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1125 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1127 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1128 for (i = 0; i < groups_len; i++)
1130 gchar *group_name = g_strdup (groups[i]);
1132 info->groups = g_slist_append (info->groups, group_name);
1135 g_strfreev (groups);
1137 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1138 for (i = 0; i < apps_len; i++)
1140 gchar *app_name, *app_exec;
1143 RecentAppInfo *app_info;
1148 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1156 app_info = recent_app_info_new (app_name);
1157 app_info->exec = app_exec;
1158 app_info->count = count;
1159 app_info->stamp = stamp;
1161 info->applications = g_slist_append (info->applications,
1163 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1172 * gtk_recent_manager_lookup_item:
1173 * @manager: a #GtkRecentManager
1175 * @error: a return location for a #GError, or %NULL
1177 * Searches for a URI inside the recently used resources list, and
1178 * returns a structure containing informations about the resource
1179 * like its MIME type, or its display name.
1181 * Return value: a #GtkRecentInfo structure containing information
1182 * about the resource pointed by @uri, or %NULL if the URI was
1183 * not registered in the recently used resources list. Free with
1184 * gtk_recent_info_unref().
1189 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1193 GtkRecentManagerPrivate *priv;
1194 GtkRecentInfo *info = NULL;
1197 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1198 g_return_val_if_fail (uri != NULL, NULL);
1199 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1201 priv = manager->priv;
1202 if (!priv->recent_items)
1204 priv->recent_items = g_bookmark_file_new ();
1207 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1208 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1209 _("Unable to find an item with URI '%s'"),
1215 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1217 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1218 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1219 _("Unable to find an item with URI '%s'"),
1224 info = gtk_recent_info_new (uri);
1225 g_return_val_if_fail (info != NULL, NULL);
1227 /* fill the RecentInfo structure with the data retrieved by our
1228 * parser object from the storage file
1230 res = build_recent_info (priv->recent_items, info);
1233 gtk_recent_info_free (info);
1238 return gtk_recent_info_ref (info);
1242 * gtk_recent_manager_move_item:
1243 * @manager: a #GtkRecentManager
1244 * @uri: the URI of a recently used resource
1245 * @new_uri: the new URI of the recently used resource, or %NULL to
1246 * remove the item pointed by @uri in the list
1247 * @error: a return location for a #GError, or %NULL
1249 * Changes the location of a recently used resource from @uri to @new_uri.
1251 * Please note that this function will not affect the resource pointed
1252 * by the URIs, but only the URI used in the recently used resources list.
1254 * Return value: %TRUE on success.
1259 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1261 const gchar *new_uri,
1264 GtkRecentManagerPrivate *priv;
1268 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1269 g_return_val_if_fail (uri != NULL, FALSE);
1270 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1272 priv = recent_manager->priv;
1274 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1276 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1277 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1278 _("Unable to find an item with URI '%s'"),
1284 res = g_bookmark_file_move_item (priv->recent_items,
1289 g_propagate_error (error, move_error);
1293 priv->is_dirty = TRUE;
1295 gtk_recent_manager_changed (recent_manager);
1301 * gtk_recent_manager_get_items:
1302 * @manager: a #GtkRecentManager
1304 * Gets the list of recently used resources.
1306 * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1307 * gtk_recent_info_unref() on each item inside the list, and then
1308 * free the list itself using g_list_free().
1313 gtk_recent_manager_get_items (GtkRecentManager *manager)
1315 GtkRecentManagerPrivate *priv;
1316 GList *retval = NULL;
1320 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1322 priv = manager->priv;
1323 if (!priv->recent_items)
1326 if (priv->limit == 0)
1329 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1330 for (i = 0; i < uris_len; i++)
1332 GtkRecentInfo *info;
1335 info = gtk_recent_info_new (uris[i]);
1336 res = build_recent_info (priv->recent_items, info);
1339 g_warning ("Unable to create a RecentInfo object for "
1340 "item with URI `%s'",
1342 gtk_recent_info_free (info);
1347 retval = g_list_prepend (retval, info);
1352 /* clamp the list, if a limit is present */
1353 if ((priv->limit != -1) &&
1354 (g_list_length (retval) > priv->limit))
1358 clamp = g_list_nth (retval, priv->limit - 1);
1366 g_list_foreach (l, (GFunc) gtk_recent_info_free, NULL);
1374 purge_recent_items_list (GtkRecentManager *manager,
1377 GtkRecentManagerPrivate *priv = manager->priv;
1379 if (!priv->recent_items)
1382 g_bookmark_file_free (priv->recent_items);
1383 priv->recent_items = NULL;
1385 priv->recent_items = g_bookmark_file_new ();
1387 priv->is_dirty = TRUE;
1389 /* emit the changed signal, to ensure that the purge is written */
1390 gtk_recent_manager_changed (manager);
1394 * gtk_recent_manager_purge_items:
1395 * @manager: a #GtkRecentManager
1396 * @error: a return location for a #GError, or %NULL
1398 * Purges every item from the recently used resources list.
1400 * Return value: the number of items that have been removed from the
1401 * recently used resources list.
1406 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1409 GtkRecentManagerPrivate *priv;
1412 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1414 priv = manager->priv;
1415 if (!priv->recent_items)
1418 count = g_bookmark_file_get_size (priv->recent_items);
1422 purge_recent_items_list (manager, error);
1424 purged = count - g_bookmark_file_get_size (priv->recent_items);
1430 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1432 g_signal_emit (recent_manager, signal_changed, 0);
1440 gtk_recent_info_get_type (void)
1442 static GType info_type = 0;
1445 info_type = g_boxed_type_register_static ("GtkRecentInfo",
1446 (GBoxedCopyFunc) gtk_recent_info_ref,
1447 (GBoxedFreeFunc) gtk_recent_info_unref);
1451 static GtkRecentInfo *
1452 gtk_recent_info_new (const gchar *uri)
1454 GtkRecentInfo *info;
1456 g_assert (uri != NULL);
1458 info = g_new0 (GtkRecentInfo, 1);
1459 info->uri = g_strdup (uri);
1461 info->applications = NULL;
1462 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1464 info->groups = NULL;
1466 info->ref_count = 1;
1472 gtk_recent_info_free (GtkRecentInfo *recent_info)
1477 g_free (recent_info->uri);
1478 g_free (recent_info->display_name);
1479 g_free (recent_info->description);
1480 g_free (recent_info->mime_type);
1482 if (recent_info->applications)
1484 g_slist_foreach (recent_info->applications,
1485 (GFunc) recent_app_info_free,
1487 g_slist_free (recent_info->applications);
1489 recent_info->applications = NULL;
1492 if (recent_info->apps_lookup)
1493 g_hash_table_destroy (recent_info->apps_lookup);
1495 if (recent_info->groups)
1497 g_slist_foreach (recent_info->groups,
1500 g_slist_free (recent_info->groups);
1502 recent_info->groups = NULL;
1505 if (recent_info->icon)
1506 g_object_unref (recent_info->icon);
1508 g_free (recent_info);
1512 * gtk_recent_info_ref:
1513 * @info: a #GtkRecentInfo
1515 * Increases the reference count of @recent_info by one.
1517 * Return value: the recent info object with its reference count increased
1523 gtk_recent_info_ref (GtkRecentInfo *info)
1525 g_return_val_if_fail (info != NULL, NULL);
1526 g_return_val_if_fail (info->ref_count > 0, NULL);
1528 info->ref_count += 1;
1534 * gtk_recent_info_unref:
1535 * @info: a #GtkRecentInfo
1537 * Decreases the reference count of @info by one. If the reference
1538 * count reaches zero, @info is deallocated, and the memory freed.
1543 gtk_recent_info_unref (GtkRecentInfo *info)
1545 g_return_if_fail (info != NULL);
1546 g_return_if_fail (info->ref_count > 0);
1548 info->ref_count -= 1;
1550 if (info->ref_count == 0)
1551 gtk_recent_info_free (info);
1555 * gtk_recent_info_get_uri:
1556 * @info: a #GtkRecentInfo
1558 * Gets the URI of the resource.
1560 * Return value: the URI of the resource. The returned string is
1561 * owned by the recent manager, and should not be freed.
1565 G_CONST_RETURN gchar *
1566 gtk_recent_info_get_uri (GtkRecentInfo *info)
1568 g_return_val_if_fail (info != NULL, NULL);
1574 * gtk_recent_info_get_display_name:
1575 * @info: a #GtkRecentInfo
1577 * Gets the name of the resource. If none has been defined, the basename
1578 * of the resource is obtained.
1580 * Return value: the display name of the resource. The returned string
1581 * is owned by the recent manager, and should not be freed.
1585 G_CONST_RETURN gchar *
1586 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1588 g_return_val_if_fail (info != NULL, NULL);
1590 if (!info->display_name)
1591 info->display_name = gtk_recent_info_get_short_name (info);
1593 return info->display_name;
1597 * gtk_recent_info_get_description:
1598 * @info: a #GtkRecentInfo
1600 * Gets the (short) description of the resource.
1602 * Return value: the description of the resource. The returned string
1603 * is owned by the recent manager, and should not be freed.
1607 G_CONST_RETURN gchar *
1608 gtk_recent_info_get_description (GtkRecentInfo *info)
1610 g_return_val_if_fail (info != NULL, NULL);
1612 return info->description;
1616 * gtk_recent_info_get_mime_type:
1617 * @info: a #GtkRecentInfo
1619 * Gets the MIME type of the resource.
1621 * Return value: the MIME type of the resource. The returned string
1622 * is owned by the recent manager, and should not be freed.
1626 G_CONST_RETURN gchar *
1627 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1629 g_return_val_if_fail (info != NULL, NULL);
1631 if (!info->mime_type)
1632 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1634 return info->mime_type;
1638 * gtk_recent_info_get_added:
1639 * @info: a #GtkRecentInfo
1641 * Gets the timestamp (seconds from system's Epoch) when the resource
1642 * was added to the recently used resources list.
1644 * Return value: the number of seconds elapsed from system's Epoch when
1645 * the resource was added to the list, or -1 on failure.
1650 gtk_recent_info_get_added (GtkRecentInfo *info)
1652 g_return_val_if_fail (info != NULL, (time_t) -1);
1658 * gtk_recent_info_get_modified:
1659 * @info: a #GtkRecentInfo
1661 * Gets the timestamp (seconds from system's Epoch) when the resource
1662 * was last modified.
1664 * Return value: the number of seconds elapsed from system's Epoch when
1665 * the resource was last modified, or -1 on failure.
1670 gtk_recent_info_get_modified (GtkRecentInfo *info)
1672 g_return_val_if_fail (info != NULL, (time_t) -1);
1674 return info->modified;
1678 * gtk_recent_info_get_visited:
1679 * @info: a #GtkRecentInfo
1681 * Gets the timestamp (seconds from system's Epoch) when the resource
1684 * Return value: the number of seconds elapsed from system's Epoch when
1685 * the resource was last visited, or -1 on failure.
1690 gtk_recent_info_get_visited (GtkRecentInfo *info)
1692 g_return_val_if_fail (info != NULL, (time_t) -1);
1694 return info->visited;
1698 * gtk_recent_info_get_private_hint:
1699 * @info: a #GtkRecentInfo
1701 * Gets the value of the "private" flag. Resources in the recently used
1702 * list that have this flag set to %TRUE should only be displayed by the
1703 * applications that have registered them.
1705 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1710 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1712 g_return_val_if_fail (info != NULL, FALSE);
1714 return info->is_private;
1718 static RecentAppInfo *
1719 recent_app_info_new (const gchar *app_name)
1721 RecentAppInfo *app_info;
1723 g_assert (app_name != NULL);
1725 app_info = g_new0 (RecentAppInfo, 1);
1726 app_info->name = g_strdup (app_name);
1727 app_info->exec = NULL;
1728 app_info->count = 1;
1729 app_info->stamp = time (NULL);
1735 recent_app_info_free (RecentAppInfo *app_info)
1741 g_free (app_info->name);
1744 g_free (app_info->exec);
1750 * gtk_recent_info_get_application_info:
1751 * @info: a #GtkRecentInfo
1752 * @app_name: the name of the application that has registered this item
1753 * @app_exec: return location for the string containing the command line
1754 * @count: return location for the number of times this item was registered
1755 * @time_: return location for the timestamp this item was last registered
1756 * for this application
1758 * Gets the data regarding the application that has registered the resource
1761 * If the command line contains any escape characters defined inside the
1762 * storage specification, they will be expanded.
1764 * Return value: %TRUE if an application with @app_name has registered this
1765 * resource inside the recently used list, or %FALSE otherwise. You should
1766 * free the returned command line using g_free().
1771 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1772 const gchar *app_name,
1779 g_return_val_if_fail (info != NULL, FALSE);
1780 g_return_val_if_fail (app_name != NULL, FALSE);
1782 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1786 g_warning ("No registered application with name '%s' "
1787 "for item with URI '%s' found",
1794 *app_exec = ai->exec;
1806 * gtk_recent_info_get_applications:
1807 * @info: a #GtkRecentInfo
1808 * @length: return location for the length of the returned list, or %NULL
1810 * Retrieves the list of applications that have registered this resource.
1812 * Return value: a newly allocated %NULL-terminated array of strings.
1813 * Use g_strfreev() to free it.
1818 gtk_recent_info_get_applications (GtkRecentInfo *info,
1825 g_return_val_if_fail (info != NULL, NULL);
1827 if (!info->applications)
1835 n_apps = g_slist_length (info->applications);
1837 retval = g_new0 (gchar *, n_apps + 1);
1839 for (l = info->applications, i = 0;
1843 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1845 g_assert (ai != NULL);
1847 retval[i++] = g_strdup (ai->name);
1858 * gtk_recent_info_has_application:
1859 * @info: a #GtkRecentInfo
1860 * @app_name: a string containing an application name
1862 * Checks whether an application registered this resource using @app_name.
1864 * Return value: %TRUE if an application with name @app_name was found,
1870 gtk_recent_info_has_application (GtkRecentInfo *info,
1871 const gchar *app_name)
1873 g_return_val_if_fail (info != NULL, FALSE);
1874 g_return_val_if_fail (app_name != NULL, FALSE);
1876 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1880 * gtk_recent_info_last_application:
1881 * @info: a #GtkRecentInfo
1883 * Gets the name of the last application that have registered the
1884 * recently used resource represented by @info.
1886 * Return value: an application name. Use g_free() to free it.
1891 gtk_recent_info_last_application (GtkRecentInfo *info)
1894 time_t last_stamp = (time_t) -1;
1897 g_return_val_if_fail (info != NULL, NULL);
1899 for (l = info->applications; l != NULL; l = l->next)
1901 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1903 if (ai->stamp > last_stamp)
1906 last_stamp = ai->stamp;
1910 return g_strdup (name);
1920 icon_cache_element_free (IconCacheElement *element)
1922 if (element->pixbuf)
1923 g_object_unref (element->pixbuf);
1928 icon_theme_changed (GtkIconTheme *icon_theme)
1932 /* Difference from the initial creation is that we don't
1933 * reconnect the signal
1935 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1936 (GDestroyNotify)g_free,
1937 (GDestroyNotify)icon_cache_element_free);
1938 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1939 cache, (GDestroyNotify)g_hash_table_destroy);
1942 /* TODO: use the GtkFileChooser's icon cache instead of our own to reduce
1943 * the memory footprint
1946 get_cached_icon (const gchar *name,
1949 GtkIconTheme *icon_theme;
1951 IconCacheElement *element;
1953 icon_theme = gtk_icon_theme_get_default ();
1954 cache = g_object_get_data (G_OBJECT (icon_theme), "gtk-recent-icon-cache");
1958 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
1959 (GDestroyNotify)g_free,
1960 (GDestroyNotify)icon_cache_element_free);
1962 g_object_set_data_full (G_OBJECT (icon_theme), "gtk-recent-icon-cache",
1963 cache, (GDestroyNotify)g_hash_table_destroy);
1964 g_signal_connect (icon_theme, "changed",
1965 G_CALLBACK (icon_theme_changed), NULL);
1968 element = g_hash_table_lookup (cache, name);
1971 element = g_new0 (IconCacheElement, 1);
1972 g_hash_table_insert (cache, g_strdup (name), element);
1975 if (element->size != pixel_size)
1977 if (element->pixbuf)
1978 g_object_unref (element->pixbuf);
1980 element->size = pixel_size;
1981 element->pixbuf = gtk_icon_theme_load_icon (icon_theme, name,
1982 pixel_size, 0, NULL);
1985 return element->pixbuf ? g_object_ref (element->pixbuf) : NULL;
1990 get_icon_for_mime_type (const char *mime_type,
1993 const char *separator;
1997 separator = strchr (mime_type, '/');
1999 return NULL; /* maybe we should return a GError with "invalid MIME-type" */
2001 icon_name = g_string_new ("gnome-mime-");
2002 g_string_append_len (icon_name, mime_type, separator - mime_type);
2003 g_string_append_c (icon_name, '-');
2004 g_string_append (icon_name, separator + 1);
2005 pixbuf = get_cached_icon (icon_name->str, pixel_size);
2006 g_string_free (icon_name, TRUE);
2010 icon_name = g_string_new ("gnome-mime-");
2011 g_string_append_len (icon_name, mime_type, separator - mime_type);
2012 pixbuf = get_cached_icon (icon_name->str, pixel_size);
2013 g_string_free (icon_name, TRUE);
2019 get_icon_fallback (const gchar *icon_name,
2022 GtkIconTheme *icon_theme;
2025 icon_theme = gtk_icon_theme_get_default ();
2027 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
2029 GTK_ICON_LOOKUP_USE_BUILTIN,
2031 g_assert (retval != NULL);
2037 * gtk_recent_info_get_icon:
2038 * @info: a #GtkRecentInfo
2039 * @size: the size of the icon in pixels
2041 * Retrieves the icon of size @size associated to the resource MIME type.
2043 * Return value: a #GdkPixbuf containing the icon, or %NULL.
2048 gtk_recent_info_get_icon (GtkRecentInfo *info,
2051 GdkPixbuf *retval = NULL;
2053 g_return_val_if_fail (info != NULL, NULL);
2055 if (info->mime_type)
2056 retval = get_icon_for_mime_type (info->mime_type, size);
2058 /* this should never fail */
2060 retval = get_icon_fallback (GTK_STOCK_FILE, size);
2066 * gtk_recent_info_is_local:
2067 * @info: a #GtkRecentInfo
2069 * Checks whether the resource is local or not by looking at the
2070 * scheme of its URI.
2072 * Return value: %TRUE if the resource is local.
2077 gtk_recent_info_is_local (GtkRecentInfo *info)
2079 g_return_val_if_fail (info != NULL, FALSE);
2081 return has_case_prefix (info->uri, "file:/");
2085 * gtk_recent_info_exists:
2086 * @info: a #GtkRecentInfo
2088 * Checks whether the resource pointed by @info still exists. At
2089 * the moment this check is done only on resources pointing to local files.
2091 * Return value: %TRUE if the resource exists
2096 gtk_recent_info_exists (GtkRecentInfo *info)
2099 struct stat stat_buf;
2100 gboolean retval = FALSE;
2102 g_return_val_if_fail (info != NULL, FALSE);
2104 /* we guarantee only local resources */
2105 if (!gtk_recent_info_is_local (info))
2108 filename = g_filename_from_uri (info->uri, NULL, NULL);
2111 if (stat (filename, &stat_buf) == 0)
2121 * gtk_recent_info_match:
2122 * @info_a: a #GtkRecentInfo
2123 * @info_b: a #GtkRecentInfo
2125 * Checks whether two #GtkRecentInfo structures point to the same
2128 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2129 * resource, %FALSE otherwise.
2134 gtk_recent_info_match (GtkRecentInfo *info_a,
2135 GtkRecentInfo *info_b)
2137 g_return_val_if_fail (info_a != NULL, FALSE);
2138 g_return_val_if_fail (info_b != NULL, FALSE);
2140 return (0 == strcmp (info_a->uri, info_b->uri));
2143 /* taken from gnome-vfs-uri.c */
2144 static const gchar *
2145 get_method_string (const gchar *substring,
2146 gchar **method_string)
2152 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2159 !(p == substring + 1 && g_ascii_isalpha (*substring))
2163 /* Found toplevel method specification. */
2164 method = g_strndup (substring, p - substring);
2165 *method_string = g_ascii_strdown (method, -1);
2171 *method_string = g_strdup ("file");
2178 /* Stolen from gnome_vfs_make_valid_utf8() */
2180 make_valid_utf8 (const char *name)
2183 const char *remainder, *invalid;
2184 int remaining_bytes, valid_bytes;
2188 remaining_bytes = name ? strlen (name) : 0;
2190 while (remaining_bytes != 0)
2192 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2195 valid_bytes = invalid - remainder;
2198 string = g_string_sized_new (remaining_bytes);
2200 g_string_append_len (string, remainder, valid_bytes);
2201 g_string_append_c (string, '?');
2203 remaining_bytes -= valid_bytes + 1;
2204 remainder = invalid + 1;
2208 return g_strdup (name);
2210 g_string_append (string, remainder);
2211 g_assert (g_utf8_validate (string->str, -1, NULL));
2213 return g_string_free (string, FALSE);
2217 get_uri_shortname_for_display (const gchar *uri)
2220 gboolean validated = FALSE;
2222 if (has_case_prefix (uri, "file:/"))
2226 local_file = g_filename_from_uri (uri, NULL, NULL);
2230 name = g_filename_display_basename (local_file);
2234 g_free (local_file);
2243 rest = get_method_string (uri, &method);
2244 local_file = g_filename_display_basename (rest);
2246 name = g_strdup_printf ("%s: %s", method, local_file);
2248 g_free (local_file);
2252 g_assert (name != NULL);
2254 if (!validated && !g_utf8_validate (name, -1, NULL))
2258 utf8_name = make_valid_utf8 (name);
2268 * gtk_recent_info_get_short_name:
2269 * @info: an #GtkRecentInfo
2271 * Computes a valid UTF-8 string that can be used as the name of the item in a
2272 * menu or list. For example, calling this function on an item that refers to
2273 * "file:///foo/bar.txt" will yield "bar.txt".
2275 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2281 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2285 g_return_val_if_fail (info != NULL, NULL);
2287 if (info->uri == NULL)
2290 short_name = get_uri_shortname_for_display (info->uri);
2296 * gtk_recent_info_get_uri_display:
2297 * @info: a #GtkRecentInfo
2299 * Gets a displayable version of the resource's URI. If the resource
2300 * is local, it returns a local path; if the resource is not local,
2301 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2303 * Return value: a UTF-8 string containing the resource's URI or %NULL
2308 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2312 g_return_val_if_fail (info != NULL, NULL);
2315 if (gtk_recent_info_is_local (info))
2319 filename = g_filename_from_uri (info->uri, NULL, NULL);
2323 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2328 retval = make_valid_utf8 (info->uri);
2335 * gtk_recent_info_get_age:
2336 * @info: a #GtkRecentInfo
2338 * Gets the number of days elapsed since the last update of the resource
2341 * Return value: a positive integer containing the number of days elapsed
2342 * since the time this resource was last modified.
2347 gtk_recent_info_get_age (GtkRecentInfo *info)
2352 g_return_val_if_fail (info != NULL, -1);
2356 delta = now - info->modified;
2358 retval = (gint) (delta / (60 * 60 * 24));
2364 * gtk_recent_info_get_groups:
2365 * @info: a #GtkRecentInfo
2366 * @length: return location for the number of groups returned, or %NULL
2368 * Returns all groups registered for the recently used item @info. The
2369 * array of returned group names will be %NULL terminated, so length might
2370 * optionally be %NULL.
2372 * Return value: a newly allocated %NULL terminated array of strings. Use
2373 * g_strfreev() to free it.
2378 gtk_recent_info_get_groups (GtkRecentInfo *info,
2385 g_return_val_if_fail (info != NULL, NULL);
2395 n_groups = g_slist_length (info->groups);
2397 retval = g_new0 (gchar *, n_groups + 1);
2399 for (l = info->groups, i = 0;
2403 gchar *group_name = (gchar *) l->data;
2405 g_assert (group_name != NULL);
2407 retval[i++] = g_strdup (group_name);
2418 * gtk_recent_info_has_group:
2419 * @info: a #GtkRecentInfo
2420 * @group_name: name of a group
2422 * Checks whether @group_name appears inside the groups registered for the
2423 * recently used item @info.
2425 * Return value: %TRUE if the group was found.
2430 gtk_recent_info_has_group (GtkRecentInfo *info,
2431 const gchar *group_name)
2435 g_return_val_if_fail (info != NULL, FALSE);
2436 g_return_val_if_fail (group_name != NULL, FALSE);
2441 for (l = info->groups; l != NULL; l = l->next)
2443 gchar *g = (gchar *) l->data;
2445 if (strcmp (g, group_name) == 0)
2452 #define __GTK_RECENT_MANAGER_C__
2453 #include "gtkaliasdef.c"