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, see <http://www.gnu.org/licenses/>.
21 * SECTION:gtkrecentmanager
22 * @Title: GtkRecentManager
23 * @short_description: Managing recently used files
24 * @See_Also: #GBookmarkFile, #GtkSettings, #GtkRecentChooser
26 * #GtkRecentManager provides a facility for adding, removing and
27 * looking up recently used files. Each recently used file is
28 * identified by its URI, and has meta-data associated to it, like
29 * the names and command lines of the applications that have
30 * registered it, the number of time each application has registered
31 * the same file, the mime type of the file and whether the file
32 * should be displayed only by the applications that have
35 * <note><para>The recently used files list is per user.</para></note>
37 * The #GtkRecentManager acts like a database of all the recently
38 * used files. You can create new #GtkRecentManager objects, but
39 * it is more efficient to use the default manager created by GTK+.
41 * Adding a new recently used file is as simple as:
44 * GtkRecentManager *manager;
46 * manager = gtk_recent_manager_get_default ();
47 * gtk_recent_manager_add_item (manager, file_uri);
50 * The #GtkRecentManager will try to gather all the needed information
51 * from the file itself through GIO.
53 * Looking up the meta-data associated with a recently used file
54 * given its URI requires calling gtk_recent_manager_lookup_item():
57 * GtkRecentManager *manager;
58 * GtkRecentInfo *info;
59 * GError *error = NULL;
61 * manager = gtk_recent_manager_get_default ();
62 * info = gtk_recent_manager_lookup_item (manager, file_uri, &error);
65 * g_warning ("Could not find the file: %s", error->message);
66 * g_error_free (error);
70 * /* Use the info object */
71 * gtk_recent_info_unref (info);
75 * In order to retrieve the list of recently used files, you can use
76 * gtk_recent_manager_get_items(), which returns a list of #GtkRecentInfo
79 * A #GtkRecentManager is the model used to populate the contents of
80 * one, or more #GtkRecentChooser implementations.
82 * <note><para>The maximum age of the recently used files list is
83 * controllable through the #GtkSettings:gtk-recent-files-max-age
84 * property.</para></note>
86 * Recently used files are supported since GTK+ 2.10.
91 #include <sys/types.h>
100 #include <glib/gstdio.h>
103 #include "gtkrecentmanager.h"
105 #include "gtksettings.h"
106 #include "gtkstock.h"
107 #include "gtkicontheme.h"
108 #include "gtktypebuiltins.h"
109 #include "gtkprivate.h"
110 #include "gtkmarshalers.h"
112 /* the file where we store the recently used items */
113 #define GTK_RECENTLY_USED_FILE "recently-used.xbel"
115 /* return all items by default */
116 #define DEFAULT_LIMIT -1
118 /* keep in sync with xdgmime */
119 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
134 * #GtkRecentInfo is an opaque data structure
135 * whose members can only be accessed using the provided API.
137 * #GtkRecentInfo constains all the meta-data
138 * associated with an entry in the recently used files list.
142 struct _GtkRecentInfo
155 GSList *applications;
156 GHashTable *apps_lookup;
167 struct _GtkRecentManagerPrivate
175 GBookmarkFile *recent_items;
177 GFileMonitor *monitor;
179 guint changed_timeout;
192 static void gtk_recent_manager_dispose (GObject *object);
193 static void gtk_recent_manager_finalize (GObject *object);
194 static void gtk_recent_manager_set_property (GObject *object,
198 static void gtk_recent_manager_get_property (GObject *object,
202 static void gtk_recent_manager_add_item_query_info (GObject *source_object,
205 static void gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
208 GFileMonitorEvent event_type,
210 static void gtk_recent_manager_changed (GtkRecentManager *manager);
211 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
212 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
213 const gchar *filename);
214 static void gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
218 static void build_recent_items_list (GtkRecentManager *manager);
219 static void purge_recent_items_list (GtkRecentManager *manager,
222 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
223 static void recent_app_info_free (RecentAppInfo *app_info);
225 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
226 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
228 static guint signal_changed = 0;
230 static GtkRecentManager *recent_manager_singleton = NULL;
232 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
235 filename_warning (const gchar *format,
236 const gchar *filename,
237 const gchar *message)
239 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
240 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
244 /* Test of haystack has the needle prefix, comparing case
245 * insensitive. haystack may be UTF-8, but needle must
246 * contain only lowercase ascii. */
248 has_case_prefix (const gchar *haystack,
253 /* Eat one character at a time. */
257 while (*n && *h && *n == g_ascii_tolower (*h))
267 gtk_recent_manager_error_quark (void)
269 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
273 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
275 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
277 gobject_class->set_property = gtk_recent_manager_set_property;
278 gobject_class->get_property = gtk_recent_manager_get_property;
279 gobject_class->dispose = gtk_recent_manager_dispose;
280 gobject_class->finalize = gtk_recent_manager_finalize;
283 * GtkRecentManager:filename:
285 * The full path to the file to be used to store and read the recently
286 * used resources list
290 g_object_class_install_property (gobject_class,
292 g_param_spec_string ("filename",
294 P_("The full path to the file to be used to store and read the list"),
296 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
299 * GtkRecentManager:size:
301 * The size of the recently used resources list.
305 g_object_class_install_property (gobject_class,
307 g_param_spec_int ("size",
309 P_("The size of the recently used resources list"),
316 * GtkRecentManager::changed:
317 * @recent_manager: the recent manager
319 * Emitted when the current recently used resources manager changes its
320 * contents, either by calling gtk_recent_manager_add_item() or by another
326 g_signal_new (I_("changed"),
327 G_TYPE_FROM_CLASS (klass),
329 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
331 g_cclosure_marshal_VOID__VOID,
334 klass->changed = gtk_recent_manager_real_changed;
336 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
340 gtk_recent_manager_init (GtkRecentManager *manager)
342 GtkRecentManagerPrivate *priv;
344 manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
345 GTK_TYPE_RECENT_MANAGER,
346 GtkRecentManagerPrivate);
347 priv = manager->priv;
350 priv->filename = NULL;
354 gtk_recent_manager_set_property (GObject *object,
359 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
364 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gtk_recent_manager_get_property (GObject *object,
378 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
383 g_value_set_string (value, recent_manager->priv->filename);
386 g_value_set_int (value, recent_manager->priv->size);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
395 gtk_recent_manager_finalize (GObject *object)
397 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
398 GtkRecentManagerPrivate *priv = manager->priv;
400 g_free (priv->filename);
402 if (priv->recent_items != NULL)
403 g_bookmark_file_free (priv->recent_items);
405 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
409 gtk_recent_manager_dispose (GObject *gobject)
411 GtkRecentManager *manager = GTK_RECENT_MANAGER (gobject);
412 GtkRecentManagerPrivate *priv = manager->priv;
414 if (priv->monitor != NULL)
416 g_signal_handlers_disconnect_by_func (priv->monitor,
417 G_CALLBACK (gtk_recent_manager_monitor_changed),
419 g_object_unref (priv->monitor);
420 priv->monitor = NULL;
423 if (priv->changed_timeout != 0)
425 g_source_remove (priv->changed_timeout);
426 priv->changed_timeout = 0;
427 priv->changed_age = 0;
432 g_object_ref (manager);
433 g_signal_emit (manager, signal_changed, 0);
434 g_object_unref (manager);
437 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (gobject);
441 gtk_recent_manager_real_changed (GtkRecentManager *manager)
443 GtkRecentManagerPrivate *priv = manager->priv;
445 g_object_freeze_notify (G_OBJECT (manager));
451 /* we are marked as dirty, so we dump the content of our
452 * recently used items list
454 g_assert (priv->filename != NULL);
456 if (!priv->recent_items)
458 /* if no container object has been defined, we create a new
459 * empty container, and dump it
461 priv->recent_items = g_bookmark_file_new ();
466 GtkSettings *settings = gtk_settings_get_default ();
469 g_object_get (G_OBJECT (settings), "gtk-recent-files-max-age", &age, NULL);
471 gtk_recent_manager_clamp_to_age (manager, age);
474 g_bookmark_file_free (priv->recent_items);
475 priv->recent_items = g_bookmark_file_new ();
480 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
483 filename_warning ("Attempting to store changes into `%s', "
486 write_error->message);
487 g_error_free (write_error);
490 if (g_chmod (priv->filename, 0600) < 0)
492 filename_warning ("Attempting to set the permissions of `%s', "
498 /* mark us as clean */
499 priv->is_dirty = FALSE;
503 /* we are not marked as dirty, so we have been called
504 * because the recently used resources file has been
505 * changed (and not from us).
507 build_recent_items_list (manager);
510 g_object_thaw_notify (G_OBJECT (manager));
514 gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
517 GFileMonitorEvent event_type,
520 GtkRecentManager *manager = user_data;
524 case G_FILE_MONITOR_EVENT_CHANGED:
525 case G_FILE_MONITOR_EVENT_CREATED:
526 gdk_threads_enter ();
527 gtk_recent_manager_changed (manager);
528 gdk_threads_leave ();
531 case G_FILE_MONITOR_EVENT_DELETED:
540 get_default_filename (void)
542 if (g_mkdir_with_parents (g_get_user_data_dir (), 0755) == -1)
544 int saved_errno = errno;
546 g_critical ("Unable to create user data directory '%s' for storing "
547 "the recently used files list: %s",
548 g_get_user_data_dir (),
549 g_strerror (saved_errno));
554 return g_build_filename (g_get_user_data_dir (),
555 GTK_RECENTLY_USED_FILE,
560 gtk_recent_manager_set_filename (GtkRecentManager *manager,
561 const gchar *filename)
563 GtkRecentManagerPrivate *priv;
567 g_assert (GTK_IS_RECENT_MANAGER (manager));
569 priv = manager->priv;
571 /* if a filename is already set and filename is not NULL, then copy
572 * it and reset the monitor; otherwise, if it's NULL we're being
573 * called from the finalization sequence, so we simply disconnect the
574 * monitoring and return.
576 * if no filename is set and filename is NULL, use the default.
580 g_free (priv->filename);
584 g_signal_handlers_disconnect_by_func (priv->monitor,
585 G_CALLBACK (gtk_recent_manager_monitor_changed),
587 g_object_unref (priv->monitor);
588 priv->monitor = NULL;
591 if (!filename || *filename == '\0')
594 priv->filename = g_strdup (filename);
598 if (!filename || *filename == '\0')
599 priv->filename = get_default_filename ();
601 priv->filename = g_strdup (filename);
604 g_assert (priv->filename != NULL);
605 file = g_file_new_for_path (priv->filename);
608 priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
611 filename_warning ("Unable to monitor `%s': %s\n"
612 "The GtkRecentManager will not update its contents "
613 "if the file is changed from other instances",
616 g_error_free (error);
619 g_signal_connect (priv->monitor, "changed",
620 G_CALLBACK (gtk_recent_manager_monitor_changed),
623 g_object_unref (file);
625 priv->is_dirty = FALSE;
626 build_recent_items_list (manager);
629 /* reads the recently used resources file and builds the items list.
630 * we keep the items list inside the parser object, and build the
631 * RecentInfo object only on user's demand to avoid useless replication.
632 * this function resets the dirty bit of the manager.
635 build_recent_items_list (GtkRecentManager *manager)
637 GtkRecentManagerPrivate *priv = manager->priv;
641 g_assert (priv->filename != NULL);
643 if (!priv->recent_items)
645 priv->recent_items = g_bookmark_file_new ();
649 /* the file exists, and it's valid (we hope); if not, destroy the container
650 * object and hope for a better result when the next "changed" signal is
653 g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
656 /* if the file does not exist we just wait for the first write
657 * operation on this recent manager instance, to avoid creating
658 * empty files and leading to spurious file system events (Sabayon
659 * will not be happy about those)
661 if (read_error->domain == G_FILE_ERROR &&
662 read_error->code != G_FILE_ERROR_NOENT)
663 filename_warning ("Attempting to read the recently used resources "
664 "file at `%s', but the parser failed: %s.",
666 read_error->message);
668 g_bookmark_file_free (priv->recent_items);
669 priv->recent_items = NULL;
671 g_error_free (read_error);
675 size = g_bookmark_file_get_size (priv->recent_items);
676 if (priv->size != size)
680 g_object_notify (G_OBJECT (manager), "size");
684 priv->is_dirty = FALSE;
688 /********************
690 ********************/
694 * gtk_recent_manager_new:
696 * Creates a new recent manager object. Recent manager objects are used to
697 * handle the list of recently used resources. A #GtkRecentManager object
698 * monitors the recently used resources list, and emits the "changed" signal
699 * each time something inside the list changes.
701 * #GtkRecentManager objects are expensive: be sure to create them only when
702 * needed. You should use gtk_recent_manager_get_default() instead.
704 * Return value: A newly created #GtkRecentManager object.
709 gtk_recent_manager_new (void)
711 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
715 * gtk_recent_manager_get_default:
717 * Gets a unique instance of #GtkRecentManager, that you can share
718 * in your application without caring about memory management.
720 * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
725 gtk_recent_manager_get_default (void)
727 if (G_UNLIKELY (!recent_manager_singleton))
728 recent_manager_singleton = gtk_recent_manager_new ();
730 return recent_manager_singleton;
735 gtk_recent_manager_add_item_query_info (GObject *source_object,
739 GFile *file = G_FILE (source_object);
740 GtkRecentManager *manager = user_data;
741 GtkRecentData recent_data;
742 GFileInfo *file_info;
743 gchar *uri, *basename;
745 uri = g_file_get_uri (file);
747 file_info = g_file_query_info_finish (file, res, NULL); /* NULL-GError */
749 recent_data.display_name = NULL;
750 recent_data.description = NULL;
756 content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
758 if (G_LIKELY (content_type))
759 recent_data.mime_type = g_content_type_get_mime_type (content_type);
761 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
763 g_free (content_type);
764 g_object_unref (file_info);
768 basename = g_file_get_basename (file);
769 recent_data.mime_type = g_content_type_guess (basename, NULL, 0, NULL);
773 recent_data.app_name = g_strdup (g_get_application_name ());
774 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
775 recent_data.groups = NULL;
776 recent_data.is_private = FALSE;
778 gdk_threads_enter ();
780 /* Ignore return value, this can't fail anyway since all required
782 gtk_recent_manager_add_full (manager, uri, &recent_data);
784 manager->priv->is_dirty = TRUE;
785 gtk_recent_manager_changed (manager);
787 gdk_threads_leave ();
789 g_free (recent_data.mime_type);
790 g_free (recent_data.app_name);
791 g_free (recent_data.app_exec);
793 g_object_unref (manager);
798 * gtk_recent_manager_add_item:
799 * @manager: a #GtkRecentManager
802 * Adds a new resource, pointed by @uri, into the recently used
805 * This function automatically retrieves some of the needed
806 * metadata and setting other metadata to common default values; it
807 * then feeds the data to gtk_recent_manager_add_full().
809 * See gtk_recent_manager_add_full() if you want to explicitly
810 * define the metadata for the resource pointed by @uri.
812 * Return value: %TRUE if the new item was successfully added
813 * to the recently used resources list
818 gtk_recent_manager_add_item (GtkRecentManager *manager,
823 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
824 g_return_val_if_fail (uri != NULL, FALSE);
826 file = g_file_new_for_uri (uri);
828 g_file_query_info_async (file,
829 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
831 G_FILE_QUERY_INFO_NONE,
833 gtk_recent_manager_add_item_query_info,
834 g_object_ref (manager));
836 g_object_unref (file);
842 * gtk_recent_manager_add_full:
843 * @manager: a #GtkRecentManager
845 * @recent_data: metadata of the resource
847 * Adds a new resource, pointed by @uri, into the recently used
848 * resources list, using the metadata specified inside the #GtkRecentData
849 * structure passed in @recent_data.
851 * The passed URI will be used to identify this resource inside the
854 * In order to register the new recently used resource, metadata about
855 * the resource must be passed as well as the URI; the metadata is
856 * stored in a #GtkRecentData structure, which must contain the MIME
857 * type of the resource pointed by the URI; the name of the application
858 * that is registering the item, and a command line to be used when
859 * launching the item.
861 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
862 * to be used when viewing the item instead of the last component of the
863 * URI; a short description of the item; whether the item should be
864 * considered private - that is, should be displayed only by the
865 * applications that have registered it.
867 * Return value: %TRUE if the new item was successfully added to the
868 * recently used resources list, %FALSE otherwise.
873 gtk_recent_manager_add_full (GtkRecentManager *manager,
875 const GtkRecentData *data)
877 GtkRecentManagerPrivate *priv;
879 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
880 g_return_val_if_fail (uri != NULL, FALSE);
881 g_return_val_if_fail (data != NULL, FALSE);
884 if ((data->display_name) &&
885 (!g_utf8_validate (data->display_name, -1, NULL)))
887 g_warning ("Attempting to add `%s' to the list of recently used "
888 "resources, but the display name is not a valid UTF-8 "
894 if ((data->description) &&
895 (!g_utf8_validate (data->description, -1, NULL)))
897 g_warning ("Attempting to add `%s' to the list of recently used "
898 "resources, but the description is not a valid UTF-8 "
905 if (!data->mime_type)
907 g_warning ("Attempting to add `%s' to the list of recently used "
908 "resources, but not MIME type was defined",
915 g_warning ("Attempting to add `%s' to the list of recently used "
916 "resources, but no name of the application that is "
917 "registering it was defined",
924 g_warning ("Attempting to add `%s' to the list of recently used "
925 "resources, but no command line for the application "
926 "that is registering it was defined",
931 priv = manager->priv;
933 if (!priv->recent_items)
935 priv->recent_items = g_bookmark_file_new ();
939 if (data->display_name)
940 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
942 if (data->description)
943 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
945 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
947 if (data->groups && data->groups[0] != '\0')
951 for (j = 0; (data->groups)[j] != NULL; j++)
952 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
955 /* register the application; this will take care of updating the
956 * registration count and time in case the application has
957 * already registered the same document inside the list
959 g_bookmark_file_add_application (priv->recent_items, uri,
963 g_bookmark_file_set_is_private (priv->recent_items, uri,
966 /* mark us as dirty, so that when emitting the "changed" signal we
967 * will dump our changes
969 priv->is_dirty = TRUE;
970 gtk_recent_manager_changed (manager);
976 * gtk_recent_manager_remove_item:
977 * @manager: a #GtkRecentManager
978 * @uri: the URI of the item you wish to remove
979 * @error: (allow-none): return location for a #GError, or %NULL
981 * Removes a resource pointed by @uri from the recently used resources
982 * list handled by a recent manager.
984 * Return value: %TRUE if the item pointed by @uri has been successfully
985 * removed by the recently used resources list, and %FALSE otherwise.
990 gtk_recent_manager_remove_item (GtkRecentManager *manager,
994 GtkRecentManagerPrivate *priv;
995 GError *remove_error = NULL;
997 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
998 g_return_val_if_fail (uri != NULL, FALSE);
999 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1001 priv = manager->priv;
1003 if (!priv->recent_items)
1005 priv->recent_items = g_bookmark_file_new ();
1008 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1009 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1010 _("Unable to find an item with URI '%s'"),
1016 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1019 g_error_free (remove_error);
1021 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1022 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1023 _("Unable to find an item with URI '%s'"),
1029 priv->is_dirty = TRUE;
1030 gtk_recent_manager_changed (manager);
1036 * gtk_recent_manager_has_item:
1037 * @manager: a #GtkRecentManager
1040 * Checks whether there is a recently used resource registered
1041 * with @uri inside the recent manager.
1043 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1048 gtk_recent_manager_has_item (GtkRecentManager *manager,
1051 GtkRecentManagerPrivate *priv;
1053 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1054 g_return_val_if_fail (uri != NULL, FALSE);
1056 priv = manager->priv;
1057 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1059 return g_bookmark_file_has_item (priv->recent_items, uri);
1063 build_recent_info (GBookmarkFile *bookmarks,
1064 GtkRecentInfo *info)
1066 gchar **apps, **groups;
1067 gsize apps_len, groups_len, i;
1069 g_assert (bookmarks != NULL);
1070 g_assert (info != NULL);
1072 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1073 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1074 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1076 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1078 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1079 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1080 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1082 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1083 for (i = 0; i < groups_len; i++)
1085 gchar *group_name = g_strdup (groups[i]);
1087 info->groups = g_slist_append (info->groups, group_name);
1090 g_strfreev (groups);
1092 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1093 for (i = 0; i < apps_len; i++)
1095 gchar *app_name, *app_exec;
1098 RecentAppInfo *app_info;
1103 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1111 app_info = recent_app_info_new (app_name);
1112 app_info->exec = app_exec;
1113 app_info->count = count;
1114 app_info->stamp = stamp;
1116 info->applications = g_slist_prepend (info->applications, app_info);
1117 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1124 * gtk_recent_manager_lookup_item:
1125 * @manager: a #GtkRecentManager
1127 * @error: (allow-none): a return location for a #GError, or %NULL
1129 * Searches for a URI inside the recently used resources list, and
1130 * returns a structure containing informations about the resource
1131 * like its MIME type, or its display name.
1133 * Return value: a #GtkRecentInfo structure containing information
1134 * about the resource pointed by @uri, or %NULL if the URI was
1135 * not registered in the recently used resources list. Free with
1136 * gtk_recent_info_unref().
1141 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1145 GtkRecentManagerPrivate *priv;
1146 GtkRecentInfo *info = NULL;
1148 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1149 g_return_val_if_fail (uri != NULL, NULL);
1150 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1152 priv = manager->priv;
1153 if (!priv->recent_items)
1155 priv->recent_items = g_bookmark_file_new ();
1158 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1159 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1160 _("Unable to find an item with URI '%s'"),
1166 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1168 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1169 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1170 _("Unable to find an item with URI '%s'"),
1175 info = gtk_recent_info_new (uri);
1176 g_return_val_if_fail (info != NULL, NULL);
1178 /* fill the RecentInfo structure with the data retrieved by our
1179 * parser object from the storage file
1181 build_recent_info (priv->recent_items, info);
1187 * gtk_recent_manager_move_item:
1188 * @manager: a #GtkRecentManager
1189 * @uri: the URI of a recently used resource
1190 * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1191 * remove the item pointed by @uri in the list
1192 * @error: (allow-none): a return location for a #GError, or %NULL
1194 * Changes the location of a recently used resource from @uri to @new_uri.
1196 * Please note that this function will not affect the resource pointed
1197 * by the URIs, but only the URI used in the recently used resources list.
1199 * Return value: %TRUE on success.
1204 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1206 const gchar *new_uri,
1209 GtkRecentManagerPrivate *priv;
1212 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1213 g_return_val_if_fail (uri != NULL, FALSE);
1214 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1216 priv = recent_manager->priv;
1218 if (!priv->recent_items)
1220 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1221 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1222 _("Unable to find an item with URI '%s'"),
1227 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1229 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1230 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1231 _("Unable to find an item with URI '%s'"),
1237 if (!g_bookmark_file_move_item (priv->recent_items,
1242 g_error_free (move_error);
1244 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1245 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1246 _("Unable to find an item with URI '%s'"),
1251 priv->is_dirty = TRUE;
1252 gtk_recent_manager_changed (recent_manager);
1258 * gtk_recent_manager_get_items:
1259 * @manager: a #GtkRecentManager
1261 * Gets the list of recently used resources.
1263 * Return value: (element-type GtkRecentInfo) (transfer full): a list of
1264 * newly allocated #GtkRecentInfo objects. Use
1265 * gtk_recent_info_unref() on each item inside the list, and then
1266 * free the list itself using g_list_free().
1271 gtk_recent_manager_get_items (GtkRecentManager *manager)
1273 GtkRecentManagerPrivate *priv;
1274 GList *retval = NULL;
1278 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1280 priv = manager->priv;
1281 if (!priv->recent_items)
1284 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1285 for (i = 0; i < uris_len; i++)
1287 GtkRecentInfo *info;
1289 info = gtk_recent_info_new (uris[i]);
1290 build_recent_info (priv->recent_items, info);
1292 retval = g_list_prepend (retval, info);
1301 purge_recent_items_list (GtkRecentManager *manager,
1304 GtkRecentManagerPrivate *priv = manager->priv;
1306 if (priv->recent_items == NULL)
1309 g_bookmark_file_free (priv->recent_items);
1310 priv->recent_items = g_bookmark_file_new ();
1313 /* emit the changed signal, to ensure that the purge is written */
1314 priv->is_dirty = TRUE;
1315 gtk_recent_manager_changed (manager);
1319 * gtk_recent_manager_purge_items:
1320 * @manager: a #GtkRecentManager
1321 * @error: (allow-none): a return location for a #GError, or %NULL
1323 * Purges every item from the recently used resources list.
1325 * Return value: the number of items that have been removed from the
1326 * recently used resources list.
1331 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1334 GtkRecentManagerPrivate *priv;
1337 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1339 priv = manager->priv;
1340 if (!priv->recent_items)
1343 count = g_bookmark_file_get_size (priv->recent_items);
1347 purge_recent_items_list (manager, error);
1349 purged = count - g_bookmark_file_get_size (priv->recent_items);
1355 emit_manager_changed (gpointer data)
1357 GtkRecentManager *manager = data;
1359 manager->priv->changed_age = 0;
1360 manager->priv->changed_timeout = 0;
1362 g_signal_emit (manager, signal_changed, 0);
1368 gtk_recent_manager_changed (GtkRecentManager *manager)
1370 /* coalesce consecutive changes
1372 * we schedule a write in 250 msecs immediately; if we get more than one
1373 * request per millisecond before the timeout has a chance to run, we
1374 * schedule an emission immediately.
1376 if (manager->priv->changed_timeout == 0)
1377 manager->priv->changed_timeout = gdk_threads_add_timeout (250, emit_manager_changed, manager);
1380 manager->priv->changed_age += 1;
1382 if (manager->priv->changed_age > 250)
1384 g_source_remove (manager->priv->changed_timeout);
1385 g_signal_emit (manager, signal_changed, 0);
1387 manager->priv->changed_age = 0;
1388 manager->priv->changed_timeout = 0;
1394 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1397 GtkRecentManagerPrivate *priv = manager->priv;
1402 if (G_UNLIKELY (!priv->recent_items))
1407 uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1409 for (i = 0; i < n_uris; i++)
1411 const gchar *uri = uris[i];
1415 modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1416 item_age = (gint) ((now - modified) / (60 * 60 * 24));
1418 g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1428 G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
1429 gtk_recent_info_ref,
1430 gtk_recent_info_unref)
1432 static GtkRecentInfo *
1433 gtk_recent_info_new (const gchar *uri)
1435 GtkRecentInfo *info;
1437 g_assert (uri != NULL);
1439 info = g_new0 (GtkRecentInfo, 1);
1440 info->uri = g_strdup (uri);
1442 info->applications = NULL;
1443 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1445 info->groups = NULL;
1447 info->ref_count = 1;
1453 gtk_recent_info_free (GtkRecentInfo *recent_info)
1458 g_free (recent_info->uri);
1459 g_free (recent_info->display_name);
1460 g_free (recent_info->description);
1461 g_free (recent_info->mime_type);
1463 if (recent_info->applications)
1465 g_slist_foreach (recent_info->applications,
1466 (GFunc) recent_app_info_free,
1468 g_slist_free (recent_info->applications);
1470 recent_info->applications = NULL;
1473 if (recent_info->apps_lookup)
1474 g_hash_table_destroy (recent_info->apps_lookup);
1476 if (recent_info->groups)
1478 g_slist_foreach (recent_info->groups,
1481 g_slist_free (recent_info->groups);
1483 recent_info->groups = NULL;
1486 if (recent_info->icon)
1487 g_object_unref (recent_info->icon);
1489 g_free (recent_info);
1493 * gtk_recent_info_ref:
1494 * @info: a #GtkRecentInfo
1496 * Increases the reference count of @recent_info by one.
1498 * Return value: the recent info object with its reference count increased
1504 gtk_recent_info_ref (GtkRecentInfo *info)
1506 g_return_val_if_fail (info != NULL, NULL);
1507 g_return_val_if_fail (info->ref_count > 0, NULL);
1509 info->ref_count += 1;
1515 * gtk_recent_info_unref:
1516 * @info: a #GtkRecentInfo
1518 * Decreases the reference count of @info by one. If the reference
1519 * count reaches zero, @info is deallocated, and the memory freed.
1524 gtk_recent_info_unref (GtkRecentInfo *info)
1526 g_return_if_fail (info != NULL);
1527 g_return_if_fail (info->ref_count > 0);
1529 info->ref_count -= 1;
1531 if (info->ref_count == 0)
1532 gtk_recent_info_free (info);
1536 * gtk_recent_info_get_uri:
1537 * @info: a #GtkRecentInfo
1539 * Gets the URI of the resource.
1541 * Return value: the URI of the resource. The returned string is
1542 * owned by the recent manager, and should not be freed.
1547 gtk_recent_info_get_uri (GtkRecentInfo *info)
1549 g_return_val_if_fail (info != NULL, NULL);
1555 * gtk_recent_info_get_display_name:
1556 * @info: a #GtkRecentInfo
1558 * Gets the name of the resource. If none has been defined, the basename
1559 * of the resource is obtained.
1561 * Return value: the display name of the resource. The returned string
1562 * is owned by the recent manager, and should not be freed.
1567 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1569 g_return_val_if_fail (info != NULL, NULL);
1571 if (!info->display_name)
1572 info->display_name = gtk_recent_info_get_short_name (info);
1574 return info->display_name;
1578 * gtk_recent_info_get_description:
1579 * @info: a #GtkRecentInfo
1581 * Gets the (short) description of the resource.
1583 * Return value: the description of the resource. The returned string
1584 * is owned by the recent manager, and should not be freed.
1589 gtk_recent_info_get_description (GtkRecentInfo *info)
1591 g_return_val_if_fail (info != NULL, NULL);
1593 return info->description;
1597 * gtk_recent_info_get_mime_type:
1598 * @info: a #GtkRecentInfo
1600 * Gets the MIME type of the resource.
1602 * Return value: the MIME type of the resource. The returned string
1603 * is owned by the recent manager, and should not be freed.
1608 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1610 g_return_val_if_fail (info != NULL, NULL);
1612 if (!info->mime_type)
1613 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1615 return info->mime_type;
1619 * gtk_recent_info_get_added:
1620 * @info: a #GtkRecentInfo
1622 * Gets the timestamp (seconds from system's Epoch) when the resource
1623 * was added to the recently used resources list.
1625 * Return value: the number of seconds elapsed from system's Epoch when
1626 * the resource was added to the list, or -1 on failure.
1631 gtk_recent_info_get_added (GtkRecentInfo *info)
1633 g_return_val_if_fail (info != NULL, (time_t) -1);
1639 * gtk_recent_info_get_modified:
1640 * @info: a #GtkRecentInfo
1642 * Gets the timestamp (seconds from system's Epoch) when the resource
1643 * was last modified.
1645 * Return value: the number of seconds elapsed from system's Epoch when
1646 * the resource was last modified, or -1 on failure.
1651 gtk_recent_info_get_modified (GtkRecentInfo *info)
1653 g_return_val_if_fail (info != NULL, (time_t) -1);
1655 return info->modified;
1659 * gtk_recent_info_get_visited:
1660 * @info: a #GtkRecentInfo
1662 * Gets the timestamp (seconds from system's Epoch) when the resource
1665 * Return value: the number of seconds elapsed from system's Epoch when
1666 * the resource was last visited, or -1 on failure.
1671 gtk_recent_info_get_visited (GtkRecentInfo *info)
1673 g_return_val_if_fail (info != NULL, (time_t) -1);
1675 return info->visited;
1679 * gtk_recent_info_get_private_hint:
1680 * @info: a #GtkRecentInfo
1682 * Gets the value of the "private" flag. Resources in the recently used
1683 * list that have this flag set to %TRUE should only be displayed by the
1684 * applications that have registered them.
1686 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1691 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1693 g_return_val_if_fail (info != NULL, FALSE);
1695 return info->is_private;
1699 static RecentAppInfo *
1700 recent_app_info_new (const gchar *app_name)
1702 RecentAppInfo *app_info;
1704 g_assert (app_name != NULL);
1706 app_info = g_slice_new0 (RecentAppInfo);
1707 app_info->name = g_strdup (app_name);
1708 app_info->exec = NULL;
1709 app_info->count = 1;
1710 app_info->stamp = 0;
1716 recent_app_info_free (RecentAppInfo *app_info)
1721 g_free (app_info->name);
1722 g_free (app_info->exec);
1724 g_slice_free (RecentAppInfo, app_info);
1728 * gtk_recent_info_get_application_info:
1729 * @info: a #GtkRecentInfo
1730 * @app_name: the name of the application that has registered this item
1731 * @app_exec: (transfer none) (out): return location for the string containing the command line
1732 * @count: (out): return location for the number of times this item was registered
1733 * @time_: (out): return location for the timestamp this item was last registered
1734 * for this application
1736 * Gets the data regarding the application that has registered the resource
1739 * If the command line contains any escape characters defined inside the
1740 * storage specification, they will be expanded.
1742 * Return value: %TRUE if an application with @app_name has registered this
1743 * resource inside the recently used list, or %FALSE otherwise. The
1744 * @app_exec string is owned by the #GtkRecentInfo and should not be
1750 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1751 const gchar *app_name,
1752 const gchar **app_exec,
1758 g_return_val_if_fail (info != NULL, FALSE);
1759 g_return_val_if_fail (app_name != NULL, FALSE);
1761 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1765 g_warning ("No registered application with name '%s' "
1766 "for item with URI '%s' found",
1773 *app_exec = ai->exec;
1785 * gtk_recent_info_get_applications:
1786 * @info: a #GtkRecentInfo
1787 * @length: (out) (allow-none): return location for the length of the returned list
1789 * Retrieves the list of applications that have registered this resource.
1791 * Return value: (array length=length zero-terminated=1) (transfer full):
1792 * a newly allocated %NULL-terminated array of strings.
1793 * Use g_strfreev() to free it.
1798 gtk_recent_info_get_applications (GtkRecentInfo *info,
1805 g_return_val_if_fail (info != NULL, NULL);
1807 if (!info->applications)
1815 n_apps = g_slist_length (info->applications);
1817 retval = g_new0 (gchar *, n_apps + 1);
1819 for (l = info->applications, i = 0;
1823 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1825 g_assert (ai != NULL);
1827 retval[i++] = g_strdup (ai->name);
1838 * gtk_recent_info_has_application:
1839 * @info: a #GtkRecentInfo
1840 * @app_name: a string containing an application name
1842 * Checks whether an application registered this resource using @app_name.
1844 * Return value: %TRUE if an application with name @app_name was found,
1850 gtk_recent_info_has_application (GtkRecentInfo *info,
1851 const gchar *app_name)
1853 g_return_val_if_fail (info != NULL, FALSE);
1854 g_return_val_if_fail (app_name != NULL, FALSE);
1856 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1860 * gtk_recent_info_last_application:
1861 * @info: a #GtkRecentInfo
1863 * Gets the name of the last application that have registered the
1864 * recently used resource represented by @info.
1866 * Return value: an application name. Use g_free() to free it.
1871 gtk_recent_info_last_application (GtkRecentInfo *info)
1874 time_t last_stamp = (time_t) -1;
1877 g_return_val_if_fail (info != NULL, NULL);
1879 for (l = info->applications; l != NULL; l = l->next)
1881 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1883 if (ai->stamp > last_stamp)
1886 last_stamp = ai->stamp;
1890 return g_strdup (name);
1894 get_icon_for_mime_type (const char *mime_type,
1897 GtkIconTheme *icon_theme;
1903 icon_theme = gtk_icon_theme_get_default ();
1905 content_type = g_content_type_from_mime_type (mime_type);
1910 icon = g_content_type_get_icon (content_type);
1911 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1914 GTK_ICON_LOOKUP_USE_BUILTIN);
1915 g_free (content_type);
1916 g_object_unref (icon);
1921 pixbuf = gtk_icon_info_load_icon (info, NULL);
1922 gtk_icon_info_free (info);
1928 get_icon_fallback (const gchar *icon_name,
1931 GtkIconTheme *icon_theme;
1934 icon_theme = gtk_icon_theme_get_default ();
1936 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1938 GTK_ICON_LOOKUP_USE_BUILTIN,
1940 g_assert (retval != NULL);
1946 * gtk_recent_info_get_icon:
1947 * @info: a #GtkRecentInfo
1948 * @size: the size of the icon in pixels
1950 * Retrieves the icon of size @size associated to the resource MIME type.
1952 * Return value: (transfer full): a #GdkPixbuf containing the icon,
1953 * or %NULL. Use g_object_unref() when finished using the icon.
1958 gtk_recent_info_get_icon (GtkRecentInfo *info,
1961 GdkPixbuf *retval = NULL;
1963 g_return_val_if_fail (info != NULL, NULL);
1965 if (info->mime_type)
1966 retval = get_icon_for_mime_type (info->mime_type, size);
1968 /* this function should never fail */
1971 if (info->mime_type &&
1972 strcmp (info->mime_type, "x-directory/normal") == 0)
1973 retval = get_icon_fallback ("folder", size);
1975 retval = get_icon_fallback ("text-x-generic", size);
1982 * gtk_recent_info_get_gicon:
1983 * @info: a #GtkRecentInfo
1985 * Retrieves the icon associated to the resource MIME type.
1987 * Return value: (transfer full): a #GIcon containing the icon, or %NULL. Use
1988 * g_object_unref() when finished using the icon
1993 gtk_recent_info_get_gicon (GtkRecentInfo *info)
1996 gchar *content_type;
1998 g_return_val_if_fail (info != NULL, NULL);
2000 if (info->mime_type != NULL &&
2001 (content_type = g_content_type_from_mime_type (info->mime_type)) != NULL)
2003 icon = g_content_type_get_icon (content_type);
2004 g_free (content_type);
2011 * gtk_recent_info_is_local:
2012 * @info: a #GtkRecentInfo
2014 * Checks whether the resource is local or not by looking at the
2015 * scheme of its URI.
2017 * Return value: %TRUE if the resource is local.
2022 gtk_recent_info_is_local (GtkRecentInfo *info)
2024 g_return_val_if_fail (info != NULL, FALSE);
2026 return has_case_prefix (info->uri, "file:/");
2030 * gtk_recent_info_exists:
2031 * @info: a #GtkRecentInfo
2033 * Checks whether the resource pointed by @info still exists. At
2034 * the moment this check is done only on resources pointing to local files.
2036 * Return value: %TRUE if the resource exists
2041 gtk_recent_info_exists (GtkRecentInfo *info)
2045 gboolean retval = FALSE;
2047 g_return_val_if_fail (info != NULL, FALSE);
2049 /* we guarantee only local resources */
2050 if (!gtk_recent_info_is_local (info))
2053 filename = g_filename_from_uri (info->uri, NULL, NULL);
2056 if (g_stat (filename, &stat_buf) == 0)
2066 * gtk_recent_info_match:
2067 * @info_a: a #GtkRecentInfo
2068 * @info_b: a #GtkRecentInfo
2070 * Checks whether two #GtkRecentInfo structures point to the same
2073 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2074 * resource, %FALSE otherwise.
2079 gtk_recent_info_match (GtkRecentInfo *info_a,
2080 GtkRecentInfo *info_b)
2082 g_return_val_if_fail (info_a != NULL, FALSE);
2083 g_return_val_if_fail (info_b != NULL, FALSE);
2085 return (0 == strcmp (info_a->uri, info_b->uri));
2088 /* taken from gnome-vfs-uri.c */
2089 static const gchar *
2090 get_method_string (const gchar *substring,
2091 gchar **method_string)
2097 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2104 !(p == substring + 1 && g_ascii_isalpha (*substring))
2108 /* Found toplevel method specification. */
2109 method = g_strndup (substring, p - substring);
2110 *method_string = g_ascii_strdown (method, -1);
2116 *method_string = g_strdup ("file");
2123 /* Stolen from gnome_vfs_make_valid_utf8() */
2125 make_valid_utf8 (const char *name)
2128 const char *remainder, *invalid;
2129 int remaining_bytes, valid_bytes;
2133 remaining_bytes = name ? strlen (name) : 0;
2135 while (remaining_bytes != 0)
2137 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2140 valid_bytes = invalid - remainder;
2143 string = g_string_sized_new (remaining_bytes);
2145 g_string_append_len (string, remainder, valid_bytes);
2146 g_string_append_c (string, '?');
2148 remaining_bytes -= valid_bytes + 1;
2149 remainder = invalid + 1;
2153 return g_strdup (name);
2155 g_string_append (string, remainder);
2156 g_assert (g_utf8_validate (string->str, -1, NULL));
2158 return g_string_free (string, FALSE);
2162 get_uri_shortname_for_display (const gchar *uri)
2165 gboolean validated = FALSE;
2167 if (has_case_prefix (uri, "file:/"))
2171 local_file = g_filename_from_uri (uri, NULL, NULL);
2175 name = g_filename_display_basename (local_file);
2179 g_free (local_file);
2188 rest = get_method_string (uri, &method);
2189 local_file = g_filename_display_basename (rest);
2191 name = g_strconcat (method, ": ", local_file, NULL);
2193 g_free (local_file);
2197 g_assert (name != NULL);
2199 if (!validated && !g_utf8_validate (name, -1, NULL))
2203 utf8_name = make_valid_utf8 (name);
2213 * gtk_recent_info_get_short_name:
2214 * @info: an #GtkRecentInfo
2216 * Computes a valid UTF-8 string that can be used as the name of the item in a
2217 * menu or list. For example, calling this function on an item that refers to
2218 * "file:///foo/bar.txt" will yield "bar.txt".
2220 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2226 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2230 g_return_val_if_fail (info != NULL, NULL);
2232 if (info->uri == NULL)
2235 short_name = get_uri_shortname_for_display (info->uri);
2241 * gtk_recent_info_get_uri_display:
2242 * @info: a #GtkRecentInfo
2244 * Gets a displayable version of the resource's URI. If the resource
2245 * is local, it returns a local path; if the resource is not local,
2246 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2248 * Return value: a newly allocated UTF-8 string containing the
2249 * resource's URI or %NULL. Use g_free() when done using it.
2254 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2258 g_return_val_if_fail (info != NULL, NULL);
2261 if (gtk_recent_info_is_local (info))
2265 filename = g_filename_from_uri (info->uri, NULL, NULL);
2269 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2274 retval = make_valid_utf8 (info->uri);
2281 * gtk_recent_info_get_age:
2282 * @info: a #GtkRecentInfo
2284 * Gets the number of days elapsed since the last update of the resource
2287 * Return value: a positive integer containing the number of days elapsed
2288 * since the time this resource was last modified.
2293 gtk_recent_info_get_age (GtkRecentInfo *info)
2298 g_return_val_if_fail (info != NULL, -1);
2302 delta = now - info->modified;
2304 retval = (gint) (delta / (60 * 60 * 24));
2310 * gtk_recent_info_get_groups:
2311 * @info: a #GtkRecentInfo
2312 * @length: (out) (allow-none): return location for the number of groups returned
2314 * Returns all groups registered for the recently used item @info. The
2315 * array of returned group names will be %NULL terminated, so length might
2316 * optionally be %NULL.
2318 * Return value: (array length=length zero-terminated=1) (transfer full):
2319 * a newly allocated %NULL terminated array of strings.
2320 * Use g_strfreev() to free it.
2325 gtk_recent_info_get_groups (GtkRecentInfo *info,
2332 g_return_val_if_fail (info != NULL, NULL);
2342 n_groups = g_slist_length (info->groups);
2344 retval = g_new0 (gchar *, n_groups + 1);
2346 for (l = info->groups, i = 0;
2350 gchar *group_name = (gchar *) l->data;
2352 g_assert (group_name != NULL);
2354 retval[i++] = g_strdup (group_name);
2365 * gtk_recent_info_has_group:
2366 * @info: a #GtkRecentInfo
2367 * @group_name: name of a group
2369 * Checks whether @group_name appears inside the groups registered for the
2370 * recently used item @info.
2372 * Return value: %TRUE if the group was found.
2377 gtk_recent_info_has_group (GtkRecentInfo *info,
2378 const gchar *group_name)
2382 g_return_val_if_fail (info != NULL, FALSE);
2383 g_return_val_if_fail (group_name != NULL, FALSE);
2388 for (l = info->groups; l != NULL; l = l->next)
2390 gchar *g = (gchar *) l->data;
2392 if (strcmp (g, group_name) == 0)
2400 * gtk_recent_info_create_app_info:
2401 * @info: a #GtkRecentInfo
2402 * @app_name: (allow-none): the name of the application that should
2403 * be mapped to a #GAppInfo; if %NULL is used then the default
2404 * application for the MIME type is used
2405 * @error: (allow-none): return location for a #GError, or %NULL
2407 * Creates a #GAppInfo for the specified #GtkRecentInfo
2409 * Return value: (transfer full): the newly created #GAppInfo, or %NULL.
2410 * In case of error, @error will be set either with a
2411 * %GTK_RECENT_MANAGER_ERROR or a %G_IO_ERROR
2414 gtk_recent_info_create_app_info (GtkRecentInfo *info,
2415 const gchar *app_name,
2420 GError *internal_error = NULL;
2422 g_return_val_if_fail (info != NULL, NULL);
2424 if (app_name == NULL || *app_name == '\0')
2428 if (info->mime_type == NULL)
2431 content_type = g_content_type_from_mime_type (info->mime_type);
2432 if (content_type == NULL)
2435 app_info = g_app_info_get_default_for_type (content_type, TRUE);
2436 g_free (content_type);
2441 ai = g_hash_table_lookup (info->apps_lookup, app_name);
2444 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
2445 GTK_RECENT_MANAGER_ERROR_NOT_REGISTERED,
2446 _("No registered application with name '%s' for item with URI '%s' found"),
2452 internal_error = NULL;
2453 app_info = g_app_info_create_from_commandline (ai->exec, ai->name,
2454 G_APP_INFO_CREATE_NONE,
2456 if (internal_error != NULL)
2458 g_propagate_error (error, internal_error);
2466 * _gtk_recent_manager_sync:
2468 * Private function for synchronising the recent manager singleton.
2471 _gtk_recent_manager_sync (void)
2473 if (recent_manager_singleton)
2475 /* force a dump of the contents of the recent manager singleton */
2476 recent_manager_singleton->priv->is_dirty = TRUE;
2477 gtk_recent_manager_real_changed (recent_manager_singleton);