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,
216 static void gtk_recent_manager_enabled_changed (GtkRecentManager *manager);
219 static void build_recent_items_list (GtkRecentManager *manager);
220 static void purge_recent_items_list (GtkRecentManager *manager,
223 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
224 static void recent_app_info_free (RecentAppInfo *app_info);
226 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
227 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
229 static guint signal_changed = 0;
231 static GtkRecentManager *recent_manager_singleton = NULL;
233 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
236 filename_warning (const gchar *format,
237 const gchar *filename,
238 const gchar *message)
240 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
241 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
245 /* Test of haystack has the needle prefix, comparing case
246 * insensitive. haystack may be UTF-8, but needle must
247 * contain only lowercase ascii. */
249 has_case_prefix (const gchar *haystack,
254 /* Eat one character at a time. */
258 while (*n && *h && *n == g_ascii_tolower (*h))
268 gtk_recent_manager_error_quark (void)
270 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
274 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
276 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
278 gobject_class->set_property = gtk_recent_manager_set_property;
279 gobject_class->get_property = gtk_recent_manager_get_property;
280 gobject_class->dispose = gtk_recent_manager_dispose;
281 gobject_class->finalize = gtk_recent_manager_finalize;
284 * GtkRecentManager:filename:
286 * The full path to the file to be used to store and read the recently
287 * used resources list
291 g_object_class_install_property (gobject_class,
293 g_param_spec_string ("filename",
295 P_("The full path to the file to be used to store and read the list"),
297 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
300 * GtkRecentManager:size:
302 * The size of the recently used resources list.
306 g_object_class_install_property (gobject_class,
308 g_param_spec_int ("size",
310 P_("The size of the recently used resources list"),
317 * GtkRecentManager::changed:
318 * @recent_manager: the recent manager
320 * Emitted when the current recently used resources manager changes its
321 * contents, either by calling gtk_recent_manager_add_item() or by another
327 g_signal_new (I_("changed"),
328 G_TYPE_FROM_CLASS (klass),
330 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
332 g_cclosure_marshal_VOID__VOID,
335 klass->changed = gtk_recent_manager_real_changed;
337 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
341 gtk_recent_manager_init (GtkRecentManager *manager)
343 GtkRecentManagerPrivate *priv;
344 GtkSettings *settings;
346 manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
347 GTK_TYPE_RECENT_MANAGER,
348 GtkRecentManagerPrivate);
349 priv = manager->priv;
352 priv->filename = NULL;
354 settings = gtk_settings_get_default ();
355 g_signal_connect_swapped (settings, "notify::gtk-recent-files-enabled",
356 G_CALLBACK (gtk_recent_manager_enabled_changed), manager);
360 gtk_recent_manager_set_property (GObject *object,
365 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
370 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
379 gtk_recent_manager_get_property (GObject *object,
384 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
389 g_value_set_string (value, recent_manager->priv->filename);
392 g_value_set_int (value, recent_manager->priv->size);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 gtk_recent_manager_finalize (GObject *object)
403 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
404 GtkRecentManagerPrivate *priv = manager->priv;
406 g_free (priv->filename);
408 if (priv->recent_items != NULL)
409 g_bookmark_file_free (priv->recent_items);
411 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
415 gtk_recent_manager_dispose (GObject *gobject)
417 GtkRecentManager *manager = GTK_RECENT_MANAGER (gobject);
418 GtkRecentManagerPrivate *priv = manager->priv;
420 if (priv->monitor != NULL)
422 g_signal_handlers_disconnect_by_func (priv->monitor,
423 G_CALLBACK (gtk_recent_manager_monitor_changed),
425 g_object_unref (priv->monitor);
426 priv->monitor = NULL;
429 if (priv->changed_timeout != 0)
431 g_source_remove (priv->changed_timeout);
432 priv->changed_timeout = 0;
433 priv->changed_age = 0;
438 g_object_ref (manager);
439 g_signal_emit (manager, signal_changed, 0);
440 g_object_unref (manager);
443 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (gobject);
447 gtk_recent_manager_enabled_changed (GtkRecentManager *manager)
449 manager->priv->is_dirty = TRUE;
450 gtk_recent_manager_changed (manager);
454 gtk_recent_manager_real_changed (GtkRecentManager *manager)
456 GtkRecentManagerPrivate *priv = manager->priv;
458 g_object_freeze_notify (G_OBJECT (manager));
464 /* we are marked as dirty, so we dump the content of our
465 * recently used items list
467 g_assert (priv->filename != NULL);
469 if (!priv->recent_items)
471 /* if no container object has been defined, we create a new
472 * empty container, and dump it
474 priv->recent_items = g_bookmark_file_new ();
479 GtkSettings *settings = gtk_settings_get_default ();
483 g_object_get (G_OBJECT (settings),
484 "gtk-recent-files-max-age", &age,
485 "gtk-recent-files-enabled", &enabled,
488 if (age == 0 || !enabled)
490 g_bookmark_file_free (priv->recent_items);
491 priv->recent_items = g_bookmark_file_new ();
494 gtk_recent_manager_clamp_to_age (manager, age);
498 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
501 filename_warning ("Attempting to store changes into `%s', "
504 write_error->message);
505 g_error_free (write_error);
508 if (g_chmod (priv->filename, 0600) < 0)
510 filename_warning ("Attempting to set the permissions of `%s', "
516 /* mark us as clean */
517 priv->is_dirty = FALSE;
521 /* we are not marked as dirty, so we have been called
522 * because the recently used resources file has been
523 * changed (and not from us).
525 build_recent_items_list (manager);
528 g_object_thaw_notify (G_OBJECT (manager));
532 gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
535 GFileMonitorEvent event_type,
538 GtkRecentManager *manager = user_data;
542 case G_FILE_MONITOR_EVENT_CHANGED:
543 case G_FILE_MONITOR_EVENT_CREATED:
544 gdk_threads_enter ();
545 gtk_recent_manager_changed (manager);
546 gdk_threads_leave ();
549 case G_FILE_MONITOR_EVENT_DELETED:
558 get_default_filename (void)
560 if (g_mkdir_with_parents (g_get_user_data_dir (), 0755) == -1)
562 int saved_errno = errno;
564 g_critical ("Unable to create user data directory '%s' for storing "
565 "the recently used files list: %s",
566 g_get_user_data_dir (),
567 g_strerror (saved_errno));
572 return g_build_filename (g_get_user_data_dir (),
573 GTK_RECENTLY_USED_FILE,
578 gtk_recent_manager_set_filename (GtkRecentManager *manager,
579 const gchar *filename)
581 GtkRecentManagerPrivate *priv;
585 g_assert (GTK_IS_RECENT_MANAGER (manager));
587 priv = manager->priv;
589 /* if a filename is already set and filename is not NULL, then copy
590 * it and reset the monitor; otherwise, if it's NULL we're being
591 * called from the finalization sequence, so we simply disconnect the
592 * monitoring and return.
594 * if no filename is set and filename is NULL, use the default.
598 g_free (priv->filename);
602 g_signal_handlers_disconnect_by_func (priv->monitor,
603 G_CALLBACK (gtk_recent_manager_monitor_changed),
605 g_object_unref (priv->monitor);
606 priv->monitor = NULL;
609 if (!filename || *filename == '\0')
612 priv->filename = g_strdup (filename);
616 if (!filename || *filename == '\0')
617 priv->filename = get_default_filename ();
619 priv->filename = g_strdup (filename);
622 g_assert (priv->filename != NULL);
623 file = g_file_new_for_path (priv->filename);
626 priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
629 filename_warning ("Unable to monitor `%s': %s\n"
630 "The GtkRecentManager will not update its contents "
631 "if the file is changed from other instances",
634 g_error_free (error);
637 g_signal_connect (priv->monitor, "changed",
638 G_CALLBACK (gtk_recent_manager_monitor_changed),
641 g_object_unref (file);
643 priv->is_dirty = FALSE;
644 build_recent_items_list (manager);
647 /* reads the recently used resources file and builds the items list.
648 * we keep the items list inside the parser object, and build the
649 * RecentInfo object only on user's demand to avoid useless replication.
650 * this function resets the dirty bit of the manager.
653 build_recent_items_list (GtkRecentManager *manager)
655 GtkRecentManagerPrivate *priv = manager->priv;
659 g_assert (priv->filename != NULL);
661 if (!priv->recent_items)
663 priv->recent_items = g_bookmark_file_new ();
667 /* the file exists, and it's valid (we hope); if not, destroy the container
668 * object and hope for a better result when the next "changed" signal is
671 g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
674 /* if the file does not exist we just wait for the first write
675 * operation on this recent manager instance, to avoid creating
676 * empty files and leading to spurious file system events (Sabayon
677 * will not be happy about those)
679 if (read_error->domain == G_FILE_ERROR &&
680 read_error->code != G_FILE_ERROR_NOENT)
681 filename_warning ("Attempting to read the recently used resources "
682 "file at `%s', but the parser failed: %s.",
684 read_error->message);
686 g_bookmark_file_free (priv->recent_items);
687 priv->recent_items = NULL;
689 g_error_free (read_error);
693 size = g_bookmark_file_get_size (priv->recent_items);
694 if (priv->size != size)
698 g_object_notify (G_OBJECT (manager), "size");
702 priv->is_dirty = FALSE;
706 /********************
708 ********************/
712 * gtk_recent_manager_new:
714 * Creates a new recent manager object. Recent manager objects are used to
715 * handle the list of recently used resources. A #GtkRecentManager object
716 * monitors the recently used resources list, and emits the "changed" signal
717 * each time something inside the list changes.
719 * #GtkRecentManager objects are expensive: be sure to create them only when
720 * needed. You should use gtk_recent_manager_get_default() instead.
722 * Return value: A newly created #GtkRecentManager object.
727 gtk_recent_manager_new (void)
729 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
733 * gtk_recent_manager_get_default:
735 * Gets a unique instance of #GtkRecentManager, that you can share
736 * in your application without caring about memory management.
738 * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
743 gtk_recent_manager_get_default (void)
745 if (G_UNLIKELY (!recent_manager_singleton))
746 recent_manager_singleton = gtk_recent_manager_new ();
748 return recent_manager_singleton;
753 gtk_recent_manager_add_item_query_info (GObject *source_object,
757 GFile *file = G_FILE (source_object);
758 GtkRecentManager *manager = user_data;
759 GtkRecentData recent_data;
760 GFileInfo *file_info;
761 gchar *uri, *basename;
763 uri = g_file_get_uri (file);
765 file_info = g_file_query_info_finish (file, res, NULL); /* NULL-GError */
767 recent_data.display_name = NULL;
768 recent_data.description = NULL;
774 content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
776 if (G_LIKELY (content_type))
777 recent_data.mime_type = g_content_type_get_mime_type (content_type);
779 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
781 g_free (content_type);
782 g_object_unref (file_info);
786 basename = g_file_get_basename (file);
787 recent_data.mime_type = g_content_type_guess (basename, NULL, 0, NULL);
791 recent_data.app_name = g_strdup (g_get_application_name ());
792 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
793 recent_data.groups = NULL;
794 recent_data.is_private = FALSE;
796 gdk_threads_enter ();
798 /* Ignore return value, this can't fail anyway since all required
800 gtk_recent_manager_add_full (manager, uri, &recent_data);
802 manager->priv->is_dirty = TRUE;
803 gtk_recent_manager_changed (manager);
805 gdk_threads_leave ();
807 g_free (recent_data.mime_type);
808 g_free (recent_data.app_name);
809 g_free (recent_data.app_exec);
811 g_object_unref (manager);
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 retrieves 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 explicitly
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,
841 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
842 g_return_val_if_fail (uri != NULL, FALSE);
844 file = g_file_new_for_uri (uri);
846 g_file_query_info_async (file,
847 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
849 G_FILE_QUERY_INFO_NONE,
851 gtk_recent_manager_add_item_query_info,
852 g_object_ref (manager));
854 g_object_unref (file);
860 * gtk_recent_manager_add_full:
861 * @manager: a #GtkRecentManager
863 * @recent_data: metadata of the resource
865 * Adds a new resource, pointed by @uri, into the recently used
866 * resources list, using the metadata specified inside the #GtkRecentData
867 * structure passed in @recent_data.
869 * The passed URI will be used to identify this resource inside the
872 * In order to register the new recently used resource, metadata about
873 * the resource must be passed as well as the URI; the metadata is
874 * stored in a #GtkRecentData structure, which must contain the MIME
875 * type of the resource pointed by the URI; the name of the application
876 * that is registering the item, and a command line to be used when
877 * launching the item.
879 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
880 * to be used when viewing the item instead of the last component of the
881 * URI; a short description of the item; whether the item should be
882 * considered private - that is, should be displayed only by the
883 * applications that have registered it.
885 * Return value: %TRUE if the new item was successfully added to the
886 * recently used resources list, %FALSE otherwise.
891 gtk_recent_manager_add_full (GtkRecentManager *manager,
893 const GtkRecentData *data)
895 GtkRecentManagerPrivate *priv;
896 GtkSettings *settings;
899 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
900 g_return_val_if_fail (uri != NULL, FALSE);
901 g_return_val_if_fail (data != NULL, FALSE);
904 if ((data->display_name) &&
905 (!g_utf8_validate (data->display_name, -1, NULL)))
907 g_warning ("Attempting to add `%s' to the list of recently used "
908 "resources, but the display name is not a valid UTF-8 "
914 if ((data->description) &&
915 (!g_utf8_validate (data->description, -1, NULL)))
917 g_warning ("Attempting to add `%s' to the list of recently used "
918 "resources, but the description is not a valid UTF-8 "
925 if (!data->mime_type)
927 g_warning ("Attempting to add `%s' to the list of recently used "
928 "resources, but not MIME type was defined",
935 g_warning ("Attempting to add `%s' to the list of recently used "
936 "resources, but no name of the application that is "
937 "registering it was defined",
944 g_warning ("Attempting to add `%s' to the list of recently used "
945 "resources, but no command line for the application "
946 "that is registering it was defined",
951 settings = gtk_settings_get_default ();
952 g_object_get (G_OBJECT (settings), "gtk-recent-files-enabled", &enabled, NULL);
956 priv = manager->priv;
958 if (!priv->recent_items)
960 priv->recent_items = g_bookmark_file_new ();
964 if (data->display_name)
965 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
967 if (data->description)
968 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
970 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
972 if (data->groups && data->groups[0] != '\0')
976 for (j = 0; (data->groups)[j] != NULL; j++)
977 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
980 /* register the application; this will take care of updating the
981 * registration count and time in case the application has
982 * already registered the same document inside the list
984 g_bookmark_file_add_application (priv->recent_items, uri,
988 g_bookmark_file_set_is_private (priv->recent_items, uri,
991 /* mark us as dirty, so that when emitting the "changed" signal we
992 * will dump our changes
994 priv->is_dirty = TRUE;
995 gtk_recent_manager_changed (manager);
1001 * gtk_recent_manager_remove_item:
1002 * @manager: a #GtkRecentManager
1003 * @uri: the URI of the item you wish to remove
1004 * @error: (allow-none): return location for a #GError, or %NULL
1006 * Removes a resource pointed by @uri from the recently used resources
1007 * list handled by a recent manager.
1009 * Return value: %TRUE if the item pointed by @uri has been successfully
1010 * removed by the recently used resources list, and %FALSE otherwise.
1015 gtk_recent_manager_remove_item (GtkRecentManager *manager,
1019 GtkRecentManagerPrivate *priv;
1020 GError *remove_error = NULL;
1022 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1023 g_return_val_if_fail (uri != NULL, FALSE);
1024 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1026 priv = manager->priv;
1028 if (!priv->recent_items)
1030 priv->recent_items = g_bookmark_file_new ();
1033 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1034 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1035 _("Unable to find an item with URI '%s'"),
1041 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1044 g_error_free (remove_error);
1046 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1047 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1048 _("Unable to find an item with URI '%s'"),
1054 priv->is_dirty = TRUE;
1055 gtk_recent_manager_changed (manager);
1061 * gtk_recent_manager_has_item:
1062 * @manager: a #GtkRecentManager
1065 * Checks whether there is a recently used resource registered
1066 * with @uri inside the recent manager.
1068 * Return value: %TRUE if the resource was found, %FALSE otherwise.
1073 gtk_recent_manager_has_item (GtkRecentManager *manager,
1076 GtkRecentManagerPrivate *priv;
1078 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1079 g_return_val_if_fail (uri != NULL, FALSE);
1081 priv = manager->priv;
1082 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1084 return g_bookmark_file_has_item (priv->recent_items, uri);
1088 build_recent_info (GBookmarkFile *bookmarks,
1089 GtkRecentInfo *info)
1091 gchar **apps, **groups;
1092 gsize apps_len, groups_len, i;
1094 g_assert (bookmarks != NULL);
1095 g_assert (info != NULL);
1097 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1098 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1099 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1101 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1103 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1104 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1105 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1107 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1108 for (i = 0; i < groups_len; i++)
1110 gchar *group_name = g_strdup (groups[i]);
1112 info->groups = g_slist_append (info->groups, group_name);
1115 g_strfreev (groups);
1117 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1118 for (i = 0; i < apps_len; i++)
1120 gchar *app_name, *app_exec;
1123 RecentAppInfo *app_info;
1128 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1136 app_info = recent_app_info_new (app_name);
1137 app_info->exec = app_exec;
1138 app_info->count = count;
1139 app_info->stamp = stamp;
1141 info->applications = g_slist_prepend (info->applications, app_info);
1142 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1149 * gtk_recent_manager_lookup_item:
1150 * @manager: a #GtkRecentManager
1152 * @error: (allow-none): a return location for a #GError, or %NULL
1154 * Searches for a URI inside the recently used resources list, and
1155 * returns a structure containing informations about the resource
1156 * like its MIME type, or its display name.
1158 * Return value: a #GtkRecentInfo structure containing information
1159 * about the resource pointed by @uri, or %NULL if the URI was
1160 * not registered in the recently used resources list. Free with
1161 * gtk_recent_info_unref().
1166 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1170 GtkRecentManagerPrivate *priv;
1171 GtkRecentInfo *info = NULL;
1173 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1174 g_return_val_if_fail (uri != NULL, NULL);
1175 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1177 priv = manager->priv;
1178 if (!priv->recent_items)
1180 priv->recent_items = g_bookmark_file_new ();
1183 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1184 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1185 _("Unable to find an item with URI '%s'"),
1191 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1193 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1194 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1195 _("Unable to find an item with URI '%s'"),
1200 info = gtk_recent_info_new (uri);
1201 g_return_val_if_fail (info != NULL, NULL);
1203 /* fill the RecentInfo structure with the data retrieved by our
1204 * parser object from the storage file
1206 build_recent_info (priv->recent_items, info);
1212 * gtk_recent_manager_move_item:
1213 * @manager: a #GtkRecentManager
1214 * @uri: the URI of a recently used resource
1215 * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1216 * remove the item pointed by @uri in the list
1217 * @error: (allow-none): a return location for a #GError, or %NULL
1219 * Changes the location of a recently used resource from @uri to @new_uri.
1221 * Please note that this function will not affect the resource pointed
1222 * by the URIs, but only the URI used in the recently used resources list.
1224 * Return value: %TRUE on success.
1229 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1231 const gchar *new_uri,
1234 GtkRecentManagerPrivate *priv;
1237 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1238 g_return_val_if_fail (uri != NULL, FALSE);
1239 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1241 priv = recent_manager->priv;
1243 if (!priv->recent_items)
1245 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1246 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1247 _("Unable to find an item with URI '%s'"),
1252 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1254 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1255 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1256 _("Unable to find an item with URI '%s'"),
1262 if (!g_bookmark_file_move_item (priv->recent_items,
1267 g_error_free (move_error);
1269 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1270 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1271 _("Unable to find an item with URI '%s'"),
1276 priv->is_dirty = TRUE;
1277 gtk_recent_manager_changed (recent_manager);
1283 * gtk_recent_manager_get_items:
1284 * @manager: a #GtkRecentManager
1286 * Gets the list of recently used resources.
1288 * Return value: (element-type GtkRecentInfo) (transfer full): a list of
1289 * newly allocated #GtkRecentInfo objects. Use
1290 * gtk_recent_info_unref() on each item inside the list, and then
1291 * free the list itself using g_list_free().
1296 gtk_recent_manager_get_items (GtkRecentManager *manager)
1298 GtkRecentManagerPrivate *priv;
1299 GList *retval = NULL;
1303 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1305 priv = manager->priv;
1306 if (!priv->recent_items)
1309 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1310 for (i = 0; i < uris_len; i++)
1312 GtkRecentInfo *info;
1314 info = gtk_recent_info_new (uris[i]);
1315 build_recent_info (priv->recent_items, info);
1317 retval = g_list_prepend (retval, info);
1326 purge_recent_items_list (GtkRecentManager *manager,
1329 GtkRecentManagerPrivate *priv = manager->priv;
1331 if (priv->recent_items == NULL)
1334 g_bookmark_file_free (priv->recent_items);
1335 priv->recent_items = g_bookmark_file_new ();
1338 /* emit the changed signal, to ensure that the purge is written */
1339 priv->is_dirty = TRUE;
1340 gtk_recent_manager_changed (manager);
1344 * gtk_recent_manager_purge_items:
1345 * @manager: a #GtkRecentManager
1346 * @error: (allow-none): a return location for a #GError, or %NULL
1348 * Purges every item from the recently used resources list.
1350 * Return value: the number of items that have been removed from the
1351 * recently used resources list.
1356 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1359 GtkRecentManagerPrivate *priv;
1362 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1364 priv = manager->priv;
1365 if (!priv->recent_items)
1368 count = g_bookmark_file_get_size (priv->recent_items);
1372 purge_recent_items_list (manager, error);
1374 purged = count - g_bookmark_file_get_size (priv->recent_items);
1380 emit_manager_changed (gpointer data)
1382 GtkRecentManager *manager = data;
1384 manager->priv->changed_age = 0;
1385 manager->priv->changed_timeout = 0;
1387 g_signal_emit (manager, signal_changed, 0);
1393 gtk_recent_manager_changed (GtkRecentManager *manager)
1395 /* coalesce consecutive changes
1397 * we schedule a write in 250 msecs immediately; if we get more than one
1398 * request per millisecond before the timeout has a chance to run, we
1399 * schedule an emission immediately.
1401 if (manager->priv->changed_timeout == 0)
1402 manager->priv->changed_timeout = gdk_threads_add_timeout (250, emit_manager_changed, manager);
1405 manager->priv->changed_age += 1;
1407 if (manager->priv->changed_age > 250)
1409 g_source_remove (manager->priv->changed_timeout);
1410 g_signal_emit (manager, signal_changed, 0);
1412 manager->priv->changed_age = 0;
1413 manager->priv->changed_timeout = 0;
1419 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1422 GtkRecentManagerPrivate *priv = manager->priv;
1427 if (G_UNLIKELY (!priv->recent_items))
1432 uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1434 for (i = 0; i < n_uris; i++)
1436 const gchar *uri = uris[i];
1440 modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1441 item_age = (gint) ((now - modified) / (60 * 60 * 24));
1443 g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1453 G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
1454 gtk_recent_info_ref,
1455 gtk_recent_info_unref)
1457 static GtkRecentInfo *
1458 gtk_recent_info_new (const gchar *uri)
1460 GtkRecentInfo *info;
1462 g_assert (uri != NULL);
1464 info = g_new0 (GtkRecentInfo, 1);
1465 info->uri = g_strdup (uri);
1467 info->applications = NULL;
1468 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1470 info->groups = NULL;
1472 info->ref_count = 1;
1478 gtk_recent_info_free (GtkRecentInfo *recent_info)
1483 g_free (recent_info->uri);
1484 g_free (recent_info->display_name);
1485 g_free (recent_info->description);
1486 g_free (recent_info->mime_type);
1488 if (recent_info->applications)
1490 g_slist_foreach (recent_info->applications,
1491 (GFunc) recent_app_info_free,
1493 g_slist_free (recent_info->applications);
1495 recent_info->applications = NULL;
1498 if (recent_info->apps_lookup)
1499 g_hash_table_destroy (recent_info->apps_lookup);
1501 if (recent_info->groups)
1503 g_slist_foreach (recent_info->groups,
1506 g_slist_free (recent_info->groups);
1508 recent_info->groups = NULL;
1511 if (recent_info->icon)
1512 g_object_unref (recent_info->icon);
1514 g_free (recent_info);
1518 * gtk_recent_info_ref:
1519 * @info: a #GtkRecentInfo
1521 * Increases the reference count of @recent_info by one.
1523 * Return value: the recent info object with its reference count increased
1529 gtk_recent_info_ref (GtkRecentInfo *info)
1531 g_return_val_if_fail (info != NULL, NULL);
1532 g_return_val_if_fail (info->ref_count > 0, NULL);
1534 info->ref_count += 1;
1540 * gtk_recent_info_unref:
1541 * @info: a #GtkRecentInfo
1543 * Decreases the reference count of @info by one. If the reference
1544 * count reaches zero, @info is deallocated, and the memory freed.
1549 gtk_recent_info_unref (GtkRecentInfo *info)
1551 g_return_if_fail (info != NULL);
1552 g_return_if_fail (info->ref_count > 0);
1554 info->ref_count -= 1;
1556 if (info->ref_count == 0)
1557 gtk_recent_info_free (info);
1561 * gtk_recent_info_get_uri:
1562 * @info: a #GtkRecentInfo
1564 * Gets the URI of the resource.
1566 * Return value: the URI of the resource. The returned string is
1567 * owned by the recent manager, and should not be freed.
1572 gtk_recent_info_get_uri (GtkRecentInfo *info)
1574 g_return_val_if_fail (info != NULL, NULL);
1580 * gtk_recent_info_get_display_name:
1581 * @info: a #GtkRecentInfo
1583 * Gets the name of the resource. If none has been defined, the basename
1584 * of the resource is obtained.
1586 * Return value: the display name of the resource. The returned string
1587 * is owned by the recent manager, and should not be freed.
1592 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1594 g_return_val_if_fail (info != NULL, NULL);
1596 if (!info->display_name)
1597 info->display_name = gtk_recent_info_get_short_name (info);
1599 return info->display_name;
1603 * gtk_recent_info_get_description:
1604 * @info: a #GtkRecentInfo
1606 * Gets the (short) description of the resource.
1608 * Return value: the description of the resource. The returned string
1609 * is owned by the recent manager, and should not be freed.
1614 gtk_recent_info_get_description (GtkRecentInfo *info)
1616 g_return_val_if_fail (info != NULL, NULL);
1618 return info->description;
1622 * gtk_recent_info_get_mime_type:
1623 * @info: a #GtkRecentInfo
1625 * Gets the MIME type of the resource.
1627 * Return value: the MIME type of the resource. The returned string
1628 * is owned by the recent manager, and should not be freed.
1633 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1635 g_return_val_if_fail (info != NULL, NULL);
1637 if (!info->mime_type)
1638 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1640 return info->mime_type;
1644 * gtk_recent_info_get_added:
1645 * @info: a #GtkRecentInfo
1647 * Gets the timestamp (seconds from system's Epoch) when the resource
1648 * was added to the recently used resources list.
1650 * Return value: the number of seconds elapsed from system's Epoch when
1651 * the resource was added to the list, or -1 on failure.
1656 gtk_recent_info_get_added (GtkRecentInfo *info)
1658 g_return_val_if_fail (info != NULL, (time_t) -1);
1664 * gtk_recent_info_get_modified:
1665 * @info: a #GtkRecentInfo
1667 * Gets the timestamp (seconds from system's Epoch) when the resource
1668 * was last modified.
1670 * Return value: the number of seconds elapsed from system's Epoch when
1671 * the resource was last modified, or -1 on failure.
1676 gtk_recent_info_get_modified (GtkRecentInfo *info)
1678 g_return_val_if_fail (info != NULL, (time_t) -1);
1680 return info->modified;
1684 * gtk_recent_info_get_visited:
1685 * @info: a #GtkRecentInfo
1687 * Gets the timestamp (seconds from system's Epoch) when the resource
1690 * Return value: the number of seconds elapsed from system's Epoch when
1691 * the resource was last visited, or -1 on failure.
1696 gtk_recent_info_get_visited (GtkRecentInfo *info)
1698 g_return_val_if_fail (info != NULL, (time_t) -1);
1700 return info->visited;
1704 * gtk_recent_info_get_private_hint:
1705 * @info: a #GtkRecentInfo
1707 * Gets the value of the "private" flag. Resources in the recently used
1708 * list that have this flag set to %TRUE should only be displayed by the
1709 * applications that have registered them.
1711 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1716 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1718 g_return_val_if_fail (info != NULL, FALSE);
1720 return info->is_private;
1724 static RecentAppInfo *
1725 recent_app_info_new (const gchar *app_name)
1727 RecentAppInfo *app_info;
1729 g_assert (app_name != NULL);
1731 app_info = g_slice_new0 (RecentAppInfo);
1732 app_info->name = g_strdup (app_name);
1733 app_info->exec = NULL;
1734 app_info->count = 1;
1735 app_info->stamp = 0;
1741 recent_app_info_free (RecentAppInfo *app_info)
1746 g_free (app_info->name);
1747 g_free (app_info->exec);
1749 g_slice_free (RecentAppInfo, app_info);
1753 * gtk_recent_info_get_application_info:
1754 * @info: a #GtkRecentInfo
1755 * @app_name: the name of the application that has registered this item
1756 * @app_exec: (transfer none) (out): return location for the string containing the command line
1757 * @count: (out): return location for the number of times this item was registered
1758 * @time_: (out): return location for the timestamp this item was last registered
1759 * for this application
1761 * Gets the data regarding the application that has registered the resource
1764 * If the command line contains any escape characters defined inside the
1765 * storage specification, they will be expanded.
1767 * Return value: %TRUE if an application with @app_name has registered this
1768 * resource inside the recently used list, or %FALSE otherwise. The
1769 * @app_exec string is owned by the #GtkRecentInfo and should not be
1775 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1776 const gchar *app_name,
1777 const gchar **app_exec,
1783 g_return_val_if_fail (info != NULL, FALSE);
1784 g_return_val_if_fail (app_name != NULL, FALSE);
1786 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1790 g_warning ("No registered application with name '%s' "
1791 "for item with URI '%s' found",
1798 *app_exec = ai->exec;
1810 * gtk_recent_info_get_applications:
1811 * @info: a #GtkRecentInfo
1812 * @length: (out) (allow-none): return location for the length of the returned list
1814 * Retrieves the list of applications that have registered this resource.
1816 * Return value: (array length=length zero-terminated=1) (transfer full):
1817 * a newly allocated %NULL-terminated array of strings.
1818 * Use g_strfreev() to free it.
1823 gtk_recent_info_get_applications (GtkRecentInfo *info,
1830 g_return_val_if_fail (info != NULL, NULL);
1832 if (!info->applications)
1840 n_apps = g_slist_length (info->applications);
1842 retval = g_new0 (gchar *, n_apps + 1);
1844 for (l = info->applications, i = 0;
1848 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1850 g_assert (ai != NULL);
1852 retval[i++] = g_strdup (ai->name);
1863 * gtk_recent_info_has_application:
1864 * @info: a #GtkRecentInfo
1865 * @app_name: a string containing an application name
1867 * Checks whether an application registered this resource using @app_name.
1869 * Return value: %TRUE if an application with name @app_name was found,
1875 gtk_recent_info_has_application (GtkRecentInfo *info,
1876 const gchar *app_name)
1878 g_return_val_if_fail (info != NULL, FALSE);
1879 g_return_val_if_fail (app_name != NULL, FALSE);
1881 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1885 * gtk_recent_info_last_application:
1886 * @info: a #GtkRecentInfo
1888 * Gets the name of the last application that have registered the
1889 * recently used resource represented by @info.
1891 * Return value: an application name. Use g_free() to free it.
1896 gtk_recent_info_last_application (GtkRecentInfo *info)
1899 time_t last_stamp = (time_t) -1;
1902 g_return_val_if_fail (info != NULL, NULL);
1904 for (l = info->applications; l != NULL; l = l->next)
1906 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1908 if (ai->stamp > last_stamp)
1911 last_stamp = ai->stamp;
1915 return g_strdup (name);
1919 get_icon_for_mime_type (const char *mime_type,
1922 GtkIconTheme *icon_theme;
1928 icon_theme = gtk_icon_theme_get_default ();
1930 content_type = g_content_type_from_mime_type (mime_type);
1935 icon = g_content_type_get_icon (content_type);
1936 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1939 GTK_ICON_LOOKUP_USE_BUILTIN);
1940 g_free (content_type);
1941 g_object_unref (icon);
1946 pixbuf = gtk_icon_info_load_icon (info, NULL);
1947 g_object_unref (info);
1953 get_icon_fallback (const gchar *icon_name,
1956 GtkIconTheme *icon_theme;
1959 icon_theme = gtk_icon_theme_get_default ();
1961 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1963 GTK_ICON_LOOKUP_USE_BUILTIN,
1965 g_assert (retval != NULL);
1971 * gtk_recent_info_get_icon:
1972 * @info: a #GtkRecentInfo
1973 * @size: the size of the icon in pixels
1975 * Retrieves the icon of size @size associated to the resource MIME type.
1977 * Return value: (transfer full): a #GdkPixbuf containing the icon,
1978 * or %NULL. Use g_object_unref() when finished using the icon.
1983 gtk_recent_info_get_icon (GtkRecentInfo *info,
1986 GdkPixbuf *retval = NULL;
1988 g_return_val_if_fail (info != NULL, NULL);
1990 if (info->mime_type)
1991 retval = get_icon_for_mime_type (info->mime_type, size);
1993 /* this function should never fail */
1996 if (info->mime_type &&
1997 strcmp (info->mime_type, "x-directory/normal") == 0)
1998 retval = get_icon_fallback ("folder", size);
2000 retval = get_icon_fallback ("text-x-generic", size);
2007 * gtk_recent_info_get_gicon:
2008 * @info: a #GtkRecentInfo
2010 * Retrieves the icon associated to the resource MIME type.
2012 * Return value: (transfer full): a #GIcon containing the icon, or %NULL. Use
2013 * g_object_unref() when finished using the icon
2018 gtk_recent_info_get_gicon (GtkRecentInfo *info)
2021 gchar *content_type;
2023 g_return_val_if_fail (info != NULL, NULL);
2025 if (info->mime_type != NULL &&
2026 (content_type = g_content_type_from_mime_type (info->mime_type)) != NULL)
2028 icon = g_content_type_get_icon (content_type);
2029 g_free (content_type);
2036 * gtk_recent_info_is_local:
2037 * @info: a #GtkRecentInfo
2039 * Checks whether the resource is local or not by looking at the
2040 * scheme of its URI.
2042 * Return value: %TRUE if the resource is local.
2047 gtk_recent_info_is_local (GtkRecentInfo *info)
2049 g_return_val_if_fail (info != NULL, FALSE);
2051 return has_case_prefix (info->uri, "file:/");
2055 * gtk_recent_info_exists:
2056 * @info: a #GtkRecentInfo
2058 * Checks whether the resource pointed by @info still exists. At
2059 * the moment this check is done only on resources pointing to local files.
2061 * Return value: %TRUE if the resource exists
2066 gtk_recent_info_exists (GtkRecentInfo *info)
2070 gboolean retval = FALSE;
2072 g_return_val_if_fail (info != NULL, FALSE);
2074 /* we guarantee only local resources */
2075 if (!gtk_recent_info_is_local (info))
2078 filename = g_filename_from_uri (info->uri, NULL, NULL);
2081 if (g_stat (filename, &stat_buf) == 0)
2091 * gtk_recent_info_match:
2092 * @info_a: a #GtkRecentInfo
2093 * @info_b: a #GtkRecentInfo
2095 * Checks whether two #GtkRecentInfo structures point to the same
2098 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2099 * resource, %FALSE otherwise.
2104 gtk_recent_info_match (GtkRecentInfo *info_a,
2105 GtkRecentInfo *info_b)
2107 g_return_val_if_fail (info_a != NULL, FALSE);
2108 g_return_val_if_fail (info_b != NULL, FALSE);
2110 return (0 == strcmp (info_a->uri, info_b->uri));
2113 /* taken from gnome-vfs-uri.c */
2114 static const gchar *
2115 get_method_string (const gchar *substring,
2116 gchar **method_string)
2122 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2129 !(p == substring + 1 && g_ascii_isalpha (*substring))
2133 /* Found toplevel method specification. */
2134 method = g_strndup (substring, p - substring);
2135 *method_string = g_ascii_strdown (method, -1);
2141 *method_string = g_strdup ("file");
2148 /* Stolen from gnome_vfs_make_valid_utf8() */
2150 make_valid_utf8 (const char *name)
2153 const char *remainder, *invalid;
2154 int remaining_bytes, valid_bytes;
2158 remaining_bytes = name ? strlen (name) : 0;
2160 while (remaining_bytes != 0)
2162 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2165 valid_bytes = invalid - remainder;
2168 string = g_string_sized_new (remaining_bytes);
2170 g_string_append_len (string, remainder, valid_bytes);
2171 g_string_append_c (string, '?');
2173 remaining_bytes -= valid_bytes + 1;
2174 remainder = invalid + 1;
2178 return g_strdup (name);
2180 g_string_append (string, remainder);
2181 g_assert (g_utf8_validate (string->str, -1, NULL));
2183 return g_string_free (string, FALSE);
2187 get_uri_shortname_for_display (const gchar *uri)
2190 gboolean validated = FALSE;
2192 if (has_case_prefix (uri, "file:/"))
2196 local_file = g_filename_from_uri (uri, NULL, NULL);
2200 name = g_filename_display_basename (local_file);
2204 g_free (local_file);
2213 rest = get_method_string (uri, &method);
2214 local_file = g_filename_display_basename (rest);
2216 name = g_strconcat (method, ": ", local_file, NULL);
2218 g_free (local_file);
2222 g_assert (name != NULL);
2224 if (!validated && !g_utf8_validate (name, -1, NULL))
2228 utf8_name = make_valid_utf8 (name);
2238 * gtk_recent_info_get_short_name:
2239 * @info: an #GtkRecentInfo
2241 * Computes a valid UTF-8 string that can be used as the name of the item in a
2242 * menu or list. For example, calling this function on an item that refers to
2243 * "file:///foo/bar.txt" will yield "bar.txt".
2245 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2251 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2255 g_return_val_if_fail (info != NULL, NULL);
2257 if (info->uri == NULL)
2260 short_name = get_uri_shortname_for_display (info->uri);
2266 * gtk_recent_info_get_uri_display:
2267 * @info: a #GtkRecentInfo
2269 * Gets a displayable version of the resource's URI. If the resource
2270 * is local, it returns a local path; if the resource is not local,
2271 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2273 * Return value: a newly allocated UTF-8 string containing the
2274 * resource's URI or %NULL. Use g_free() when done using it.
2279 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2283 g_return_val_if_fail (info != NULL, NULL);
2286 if (gtk_recent_info_is_local (info))
2290 filename = g_filename_from_uri (info->uri, NULL, NULL);
2294 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2299 retval = make_valid_utf8 (info->uri);
2306 * gtk_recent_info_get_age:
2307 * @info: a #GtkRecentInfo
2309 * Gets the number of days elapsed since the last update of the resource
2312 * Return value: a positive integer containing the number of days elapsed
2313 * since the time this resource was last modified.
2318 gtk_recent_info_get_age (GtkRecentInfo *info)
2323 g_return_val_if_fail (info != NULL, -1);
2327 delta = now - info->modified;
2329 retval = (gint) (delta / (60 * 60 * 24));
2335 * gtk_recent_info_get_groups:
2336 * @info: a #GtkRecentInfo
2337 * @length: (out) (allow-none): return location for the number of groups returned
2339 * Returns all groups registered for the recently used item @info. The
2340 * array of returned group names will be %NULL terminated, so length might
2341 * optionally be %NULL.
2343 * Return value: (array length=length zero-terminated=1) (transfer full):
2344 * a newly allocated %NULL terminated array of strings.
2345 * Use g_strfreev() to free it.
2350 gtk_recent_info_get_groups (GtkRecentInfo *info,
2357 g_return_val_if_fail (info != NULL, NULL);
2367 n_groups = g_slist_length (info->groups);
2369 retval = g_new0 (gchar *, n_groups + 1);
2371 for (l = info->groups, i = 0;
2375 gchar *group_name = (gchar *) l->data;
2377 g_assert (group_name != NULL);
2379 retval[i++] = g_strdup (group_name);
2390 * gtk_recent_info_has_group:
2391 * @info: a #GtkRecentInfo
2392 * @group_name: name of a group
2394 * Checks whether @group_name appears inside the groups registered for the
2395 * recently used item @info.
2397 * Return value: %TRUE if the group was found.
2402 gtk_recent_info_has_group (GtkRecentInfo *info,
2403 const gchar *group_name)
2407 g_return_val_if_fail (info != NULL, FALSE);
2408 g_return_val_if_fail (group_name != NULL, FALSE);
2413 for (l = info->groups; l != NULL; l = l->next)
2415 gchar *g = (gchar *) l->data;
2417 if (strcmp (g, group_name) == 0)
2425 * gtk_recent_info_create_app_info:
2426 * @info: a #GtkRecentInfo
2427 * @app_name: (allow-none): the name of the application that should
2428 * be mapped to a #GAppInfo; if %NULL is used then the default
2429 * application for the MIME type is used
2430 * @error: (allow-none): return location for a #GError, or %NULL
2432 * Creates a #GAppInfo for the specified #GtkRecentInfo
2434 * Return value: (transfer full): the newly created #GAppInfo, or %NULL.
2435 * In case of error, @error will be set either with a
2436 * %GTK_RECENT_MANAGER_ERROR or a %G_IO_ERROR
2439 gtk_recent_info_create_app_info (GtkRecentInfo *info,
2440 const gchar *app_name,
2445 GError *internal_error = NULL;
2447 g_return_val_if_fail (info != NULL, NULL);
2449 if (app_name == NULL || *app_name == '\0')
2453 if (info->mime_type == NULL)
2456 content_type = g_content_type_from_mime_type (info->mime_type);
2457 if (content_type == NULL)
2460 app_info = g_app_info_get_default_for_type (content_type, TRUE);
2461 g_free (content_type);
2466 ai = g_hash_table_lookup (info->apps_lookup, app_name);
2469 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
2470 GTK_RECENT_MANAGER_ERROR_NOT_REGISTERED,
2471 _("No registered application with name '%s' for item with URI '%s' found"),
2477 internal_error = NULL;
2478 app_info = g_app_info_create_from_commandline (ai->exec, ai->name,
2479 G_APP_INFO_CREATE_NONE,
2481 if (internal_error != NULL)
2483 g_propagate_error (error, internal_error);
2491 * _gtk_recent_manager_sync:
2493 * Private function for synchronising the recent manager singleton.
2496 _gtk_recent_manager_sync (void)
2498 if (recent_manager_singleton)
2500 /* force a dump of the contents of the recent manager singleton */
2501 recent_manager_singleton->priv->is_dirty = TRUE;
2502 gtk_recent_manager_real_changed (recent_manager_singleton);