1 /* GTK - The GIMP Toolkit
2 * gtkrecentmanager.c: a manager for the recently used resources
4 * Copyright (C) 2006 Emmanuele Bassi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 #include <sys/types.h>
32 #include <glib/gstdio.h>
35 #include "gtkrecentmanager.h"
38 #include "gtkicontheme.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkprivate.h"
41 #include "gtkmarshalers.h"
43 /* the file where we store the recently used items */
44 #define GTK_RECENTLY_USED_FILE ".recently-used.xbel"
46 /* return all items by default */
47 #define DEFAULT_LIMIT -1
49 /* keep in sync with xdgmime */
50 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
76 GHashTable *apps_lookup;
88 struct _GtkRecentManagerPrivate
96 GBookmarkFile *recent_items;
98 GFileMonitor *monitor;
110 static void gtk_recent_manager_dispose (GObject *object);
111 static void gtk_recent_manager_finalize (GObject *object);
112 static void gtk_recent_manager_set_property (GObject *object,
116 static void gtk_recent_manager_get_property (GObject *object,
120 static void gtk_recent_manager_add_item_query_info (GObject *source_object,
123 static void gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
126 GFileMonitorEvent event_type,
128 static void gtk_recent_manager_changed (GtkRecentManager *manager);
129 static void gtk_recent_manager_real_changed (GtkRecentManager *manager);
130 static void gtk_recent_manager_set_filename (GtkRecentManager *manager,
131 const gchar *filename);
132 static void gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
136 static void build_recent_items_list (GtkRecentManager *manager);
137 static void purge_recent_items_list (GtkRecentManager *manager,
140 static RecentAppInfo *recent_app_info_new (const gchar *app_name);
141 static void recent_app_info_free (RecentAppInfo *app_info);
143 static GtkRecentInfo *gtk_recent_info_new (const gchar *uri);
144 static void gtk_recent_info_free (GtkRecentInfo *recent_info);
146 static guint signal_changed = 0;
148 static GtkRecentManager *recent_manager_singleton = NULL;
150 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
153 filename_warning (const gchar *format,
154 const gchar *filename,
155 const gchar *message)
157 gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
158 g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
162 /* Test of haystack has the needle prefix, comparing case
163 * insensitive. haystack may be UTF-8, but needle must
164 * contain only lowercase ascii. */
166 has_case_prefix (const gchar *haystack,
171 /* Eat one character at a time. */
175 while (*n && *h && *n == g_ascii_tolower (*h))
185 gtk_recent_manager_error_quark (void)
187 return g_quark_from_static_string ("gtk-recent-manager-error-quark");
191 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
193 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
195 gobject_class->set_property = gtk_recent_manager_set_property;
196 gobject_class->get_property = gtk_recent_manager_get_property;
197 gobject_class->dispose = gtk_recent_manager_dispose;
198 gobject_class->finalize = gtk_recent_manager_finalize;
201 * GtkRecentManager:filename
203 * The full path to the file to be used to store and read the recently
204 * used resources list
208 g_object_class_install_property (gobject_class,
210 g_param_spec_string ("filename",
212 P_("The full path to the file to be used to store and read the list"),
214 (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
217 * GtkRecentManager:size
219 * The size of the recently used resources list.
223 g_object_class_install_property (gobject_class,
225 g_param_spec_int ("size",
227 P_("The size of the recently used resources list"),
234 * GtkRecentManager::changed
235 * @recent_manager: the recent manager
237 * Emitted when the current recently used resources manager changes its
243 g_signal_new (I_("changed"),
244 G_TYPE_FROM_CLASS (klass),
246 G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
248 g_cclosure_marshal_VOID__VOID,
251 klass->changed = gtk_recent_manager_real_changed;
253 g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
257 gtk_recent_manager_init (GtkRecentManager *manager)
259 GtkRecentManagerPrivate *priv;
261 manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
262 GTK_TYPE_RECENT_MANAGER,
263 GtkRecentManagerPrivate);
264 priv = manager->priv;
267 priv->filename = NULL;
271 gtk_recent_manager_set_property (GObject *object,
276 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
281 gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gtk_recent_manager_get_property (GObject *object,
295 GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
300 g_value_set_string (value, recent_manager->priv->filename);
303 g_value_set_int (value, recent_manager->priv->size);
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 gtk_recent_manager_dispose (GObject *object)
314 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
315 GtkRecentManagerPrivate *priv = manager->priv;
319 g_signal_handlers_disconnect_by_func (priv->monitor,
320 G_CALLBACK (gtk_recent_manager_monitor_changed),
322 g_object_unref (priv->monitor);
323 priv->monitor = NULL;
326 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (object);
330 gtk_recent_manager_finalize (GObject *object)
332 GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
333 GtkRecentManagerPrivate *priv = manager->priv;
335 g_free (priv->filename);
337 if (priv->recent_items)
338 g_bookmark_file_free (priv->recent_items);
340 G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
344 gtk_recent_manager_real_changed (GtkRecentManager *manager)
346 GtkRecentManagerPrivate *priv = manager->priv;
348 g_object_freeze_notify (G_OBJECT (manager));
354 /* we are marked as dirty, so we dump the content of our
355 * recently used items list
357 g_assert (priv->filename != NULL);
359 if (!priv->recent_items)
361 /* if no container object has been defined, we create a new
362 * empty container, and dump it
364 priv->recent_items = g_bookmark_file_new ();
369 GtkSettings *settings = gtk_settings_get_default ();
372 g_object_get (G_OBJECT (settings), "gtk-recent-files-max-age", &age, NULL);
374 gtk_recent_manager_clamp_to_age (manager, age);
377 g_bookmark_file_free (priv->recent_items);
378 priv->recent_items = NULL;
380 priv->recent_items = g_bookmark_file_new ();
385 g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
388 filename_warning ("Attempting to store changes into `%s', "
391 write_error->message);
392 g_error_free (write_error);
395 if (g_chmod (priv->filename, 0600) < 0)
397 filename_warning ("Attempting to set the permissions of `%s', "
403 /* mark us as clean */
404 priv->is_dirty = FALSE;
408 /* we are not marked as dirty, so we have been called
409 * because the recently used resources file has been
410 * changed (and not from us).
412 build_recent_items_list (manager);
415 g_object_thaw_notify (G_OBJECT (manager));
419 gtk_recent_manager_monitor_changed (GFileMonitor *monitor,
422 GFileMonitorEvent event_type,
425 GtkRecentManager *manager = user_data;
429 case G_FILE_MONITOR_EVENT_CHANGED:
430 case G_FILE_MONITOR_EVENT_CREATED:
431 gtk_recent_manager_changed (manager);
434 case G_FILE_MONITOR_EVENT_DELETED:
443 gtk_recent_manager_set_filename (GtkRecentManager *manager,
444 const gchar *filename)
446 GtkRecentManagerPrivate *priv;
450 g_assert (GTK_IS_RECENT_MANAGER (manager));
452 priv = manager->priv;
454 /* if a filename is already set and filename is not NULL, then copy
455 * it and reset the monitor; otherwise, if it's NULL we're being
456 * called from the finalization sequence, so we simply disconnect the
457 * monitoring and return.
459 * if no filename is set and filename is NULL, use the default.
463 g_free (priv->filename);
467 g_signal_handlers_disconnect_by_func (priv->monitor,
468 G_CALLBACK (gtk_recent_manager_monitor_changed),
470 g_object_unref (priv->monitor);
471 priv->monitor = NULL;
474 if (!filename || *filename == '\0')
477 priv->filename = g_strdup (filename);
481 if (!filename || *filename == '\0')
482 priv->filename = g_build_filename (g_get_home_dir (),
483 GTK_RECENTLY_USED_FILE,
486 priv->filename = g_strdup (filename);
489 g_assert (priv->filename != NULL);
490 file = g_file_new_for_path (priv->filename);
493 priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
496 filename_warning ("Unable to monitor `%s': %s\n"
497 "The GtkRecentManager will not update its contents "
498 "if the file is changed from other instances",
501 g_error_free (error);
504 g_signal_connect (priv->monitor, "changed",
505 G_CALLBACK (gtk_recent_manager_monitor_changed),
508 g_object_unref (file);
510 priv->is_dirty = FALSE;
511 build_recent_items_list (manager);
514 /* reads the recently used resources file and builds the items list.
515 * we keep the items list inside the parser object, and build the
516 * RecentInfo object only on user's demand to avoid useless replication.
517 * this function resets the dirty bit of the manager.
520 build_recent_items_list (GtkRecentManager *manager)
522 GtkRecentManagerPrivate *priv = manager->priv;
526 g_assert (priv->filename != NULL);
528 if (!priv->recent_items)
530 priv->recent_items = g_bookmark_file_new ();
534 /* the file exists, and it's valid (we hope); if not, destroy the container
535 * object and hope for a better result when the next "changed" signal is
538 g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
541 /* if the file does not exist we just wait for the first write
542 * operation on this recent manager instance, to avoid creating
543 * empty files and leading to spurious file system events (Sabayon
544 * will not be happy about those)
546 if (read_error->domain == G_FILE_ERROR &&
547 read_error->code != G_FILE_ERROR_NOENT)
548 filename_warning ("Attempting to read the recently used resources "
549 "file at `%s', but the parser failed: %s.",
551 read_error->message);
553 g_bookmark_file_free (priv->recent_items);
554 priv->recent_items = NULL;
556 g_error_free (read_error);
560 size = g_bookmark_file_get_size (priv->recent_items);
561 if (priv->size != size)
565 g_object_notify (G_OBJECT (manager), "size");
569 priv->is_dirty = FALSE;
573 /********************
575 ********************/
579 * gtk_recent_manager_new:
581 * Creates a new recent manager object. Recent manager objects are used to
582 * handle the list of recently used resources. A #GtkRecentManager object
583 * monitors the recently used resources list, and emits the "changed" signal
584 * each time something inside the list changes.
586 * #GtkRecentManager objects are expensive: be sure to create them only when
587 * needed. You should use gtk_recent_manager_get_default() instead.
589 * Return value: A newly created #GtkRecentManager object.
594 gtk_recent_manager_new (void)
596 return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
600 * gtk_recent_manager_get_default:
602 * Gets a unique instance of #GtkRecentManager, that you can share
603 * in your application without caring about memory management. The
604 * returned instance will be freed when you application terminates.
606 * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
611 gtk_recent_manager_get_default (void)
613 if (G_UNLIKELY (!recent_manager_singleton))
614 recent_manager_singleton = gtk_recent_manager_new ();
616 return recent_manager_singleton;
621 gtk_recent_manager_add_item_query_info (GObject *source_object,
625 GFile *file = G_FILE (source_object);
626 GtkRecentManager *manager = user_data;
627 GtkRecentData recent_data;
628 GFileInfo *file_info;
632 uri = g_file_get_uri (file);
635 file_info = g_file_query_info_finish (file, res, &error);
638 g_warning ("Unable to retrieve the file info for `%s': %s",
641 g_error_free (error);
645 recent_data.display_name = NULL;
646 recent_data.description = NULL;
652 content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
654 if (G_LIKELY (content_type))
655 recent_data.mime_type = g_content_type_get_mime_type (content_type);
657 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
659 g_free (content_type);
660 g_object_unref (file_info);
663 recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
665 recent_data.app_name = g_strdup (g_get_application_name ());
666 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
667 recent_data.groups = NULL;
668 recent_data.is_private = FALSE;
670 /* Ignore return value, this can't fail anyway since all required
672 gtk_recent_manager_add_full (manager, uri, &recent_data);
674 manager->priv->is_dirty = TRUE;
675 gtk_recent_manager_changed (manager);
677 g_free (recent_data.mime_type);
678 g_free (recent_data.app_name);
679 g_free (recent_data.app_exec);
682 g_object_unref (manager);
687 * gtk_recent_manager_add_item:
688 * @manager: a #GtkRecentManager
691 * Adds a new resource, pointed by @uri, into the recently used
694 * This function automatically retrieves some of the needed
695 * metadata and setting other metadata to common default values; it
696 * then feeds the data to gtk_recent_manager_add_full().
698 * See gtk_recent_manager_add_full() if you want to explicitly
699 * define the metadata for the resource pointed by @uri.
701 * Return value: %TRUE if the new item was successfully added
702 * to the recently used resources list
707 gtk_recent_manager_add_item (GtkRecentManager *manager,
712 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
713 g_return_val_if_fail (uri != NULL, FALSE);
715 file = g_file_new_for_uri (uri);
717 g_file_query_info_async (file,
718 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
720 G_FILE_QUERY_INFO_NONE,
722 gtk_recent_manager_add_item_query_info,
723 g_object_ref (manager));
725 g_object_unref (file);
731 * gtk_recent_manager_add_full:
732 * @manager: a #GtkRecentManager
734 * @recent_data: metadata of the resource
736 * Adds a new resource, pointed by @uri, into the recently used
737 * resources list, using the metadata specified inside the #GtkRecentData
738 * structure passed in @recent_data.
740 * The passed URI will be used to identify this resource inside the
743 * In order to register the new recently used resource, metadata about
744 * the resource must be passed as well as the URI; the metadata is
745 * stored in a #GtkRecentData structure, which must contain the MIME
746 * type of the resource pointed by the URI; the name of the application
747 * that is registering the item, and a command line to be used when
748 * launching the item.
750 * Optionally, a #GtkRecentData structure might contain a UTF-8 string
751 * to be used when viewing the item instead of the last component of the
752 * URI; a short description of the item; whether the item should be
753 * considered private - that is, should be displayed only by the
754 * applications that have registered it.
756 * Return value: %TRUE if the new item was successfully added to the
757 * recently used resources list, %FALSE otherwise.
762 gtk_recent_manager_add_full (GtkRecentManager *manager,
764 const GtkRecentData *data)
766 GtkRecentManagerPrivate *priv;
768 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
769 g_return_val_if_fail (uri != NULL, FALSE);
770 g_return_val_if_fail (data != NULL, FALSE);
773 if ((data->display_name) &&
774 (!g_utf8_validate (data->display_name, -1, NULL)))
776 g_warning ("Attempting to add `%s' to the list of recently used "
777 "resources, but the display name is not a valid UTF-8 "
783 if ((data->description) &&
784 (!g_utf8_validate (data->description, -1, NULL)))
786 g_warning ("Attempting to add `%s' to the list of recently used "
787 "resources, but the description is not a valid UTF-8 "
794 if (!data->mime_type)
796 g_warning ("Attempting to add `%s' to the list of recently used "
797 "resources, but not MIME type was defined",
804 g_warning ("Attempting to add `%s' to the list of recently used "
805 "resources, but no name of the application that is "
806 "registering it was defined",
813 g_warning ("Attempting to add `%s' to the list of recently used "
814 "resources, but no command line for the application "
815 "that is registering it was defined",
820 priv = manager->priv;
822 if (!priv->recent_items)
824 priv->recent_items = g_bookmark_file_new ();
828 if (data->display_name)
829 g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
831 if (data->description)
832 g_bookmark_file_set_description (priv->recent_items, uri, data->description);
834 g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
836 if (data->groups && data->groups[0] != '\0')
840 for (j = 0; (data->groups)[j] != NULL; j++)
841 g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
844 /* register the application; this will take care of updating the
845 * registration count and time in case the application has
846 * already registered the same document inside the list
848 g_bookmark_file_add_application (priv->recent_items, uri,
852 g_bookmark_file_set_is_private (priv->recent_items, uri,
855 /* mark us as dirty, so that when emitting the "changed" signal we
856 * will dump our changes
858 priv->is_dirty = TRUE;
860 gtk_recent_manager_changed (manager);
866 * gtk_recent_manager_remove_item:
867 * @manager: a #GtkRecentManager
868 * @uri: the URI of the item you wish to remove
869 * @error: (allow-none): return location for a #GError, or %NULL
871 * Removes a resource pointed by @uri from the recently used resources
872 * list handled by a recent manager.
874 * Return value: %TRUE if the item pointed by @uri has been successfully
875 * removed by the recently used resources list, and %FALSE otherwise.
880 gtk_recent_manager_remove_item (GtkRecentManager *manager,
884 GtkRecentManagerPrivate *priv;
885 GError *remove_error = NULL;
887 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
888 g_return_val_if_fail (uri != NULL, FALSE);
889 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
891 priv = manager->priv;
893 if (!priv->recent_items)
895 priv->recent_items = g_bookmark_file_new ();
898 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
899 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
900 _("Unable to find an item with URI '%s'"),
906 g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
909 g_error_free (remove_error);
911 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
912 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
913 _("Unable to find an item with URI '%s'"),
919 priv->is_dirty = TRUE;
921 gtk_recent_manager_changed (manager);
927 * gtk_recent_manager_has_item:
928 * @manager: a #GtkRecentManager
931 * Checks whether there is a recently used resource registered
932 * with @uri inside the recent manager.
934 * Return value: %TRUE if the resource was found, %FALSE otherwise.
939 gtk_recent_manager_has_item (GtkRecentManager *manager,
942 GtkRecentManagerPrivate *priv;
944 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
945 g_return_val_if_fail (uri != NULL, FALSE);
947 priv = manager->priv;
948 g_return_val_if_fail (priv->recent_items != NULL, FALSE);
950 return g_bookmark_file_has_item (priv->recent_items, uri);
954 build_recent_info (GBookmarkFile *bookmarks,
957 gchar **apps, **groups;
958 gsize apps_len, groups_len, i;
960 g_assert (bookmarks != NULL);
961 g_assert (info != NULL);
963 info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
964 info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
965 info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
967 info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
969 info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
970 info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
971 info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
973 groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
974 for (i = 0; i < groups_len; i++)
976 gchar *group_name = g_strdup (groups[i]);
978 info->groups = g_slist_append (info->groups, group_name);
983 apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
984 for (i = 0; i < apps_len; i++)
986 gchar *app_name, *app_exec;
989 RecentAppInfo *app_info;
994 res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1002 app_info = recent_app_info_new (app_name);
1003 app_info->exec = app_exec;
1004 app_info->count = count;
1005 app_info->stamp = stamp;
1007 info->applications = g_slist_prepend (info->applications, app_info);
1008 g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1015 * gtk_recent_manager_lookup_item:
1016 * @manager: a #GtkRecentManager
1018 * @error: (allow-none): a return location for a #GError, or %NULL
1020 * Searches for a URI inside the recently used resources list, and
1021 * returns a structure containing informations about the resource
1022 * like its MIME type, or its display name.
1024 * Return value: a #GtkRecentInfo structure containing information
1025 * about the resource pointed by @uri, or %NULL if the URI was
1026 * not registered in the recently used resources list. Free with
1027 * gtk_recent_info_unref().
1032 gtk_recent_manager_lookup_item (GtkRecentManager *manager,
1036 GtkRecentManagerPrivate *priv;
1037 GtkRecentInfo *info = NULL;
1039 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1040 g_return_val_if_fail (uri != NULL, NULL);
1041 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1043 priv = manager->priv;
1044 if (!priv->recent_items)
1046 priv->recent_items = g_bookmark_file_new ();
1049 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1050 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1051 _("Unable to find an item with URI '%s'"),
1057 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1059 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1060 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1061 _("Unable to find an item with URI '%s'"),
1066 info = gtk_recent_info_new (uri);
1067 g_return_val_if_fail (info != NULL, NULL);
1069 /* fill the RecentInfo structure with the data retrieved by our
1070 * parser object from the storage file
1072 build_recent_info (priv->recent_items, info);
1078 * gtk_recent_manager_move_item:
1079 * @manager: a #GtkRecentManager
1080 * @uri: the URI of a recently used resource
1081 * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1082 * remove the item pointed by @uri in the list
1083 * @error: (allow-none): a return location for a #GError, or %NULL
1085 * Changes the location of a recently used resource from @uri to @new_uri.
1087 * Please note that this function will not affect the resource pointed
1088 * by the URIs, but only the URI used in the recently used resources list.
1090 * Return value: %TRUE on success.
1095 gtk_recent_manager_move_item (GtkRecentManager *recent_manager,
1097 const gchar *new_uri,
1100 GtkRecentManagerPrivate *priv;
1104 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1105 g_return_val_if_fail (uri != NULL, FALSE);
1106 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1108 priv = recent_manager->priv;
1110 if (!priv->recent_items)
1112 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1113 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1114 _("Unable to find an item with URI '%s'"),
1119 if (!g_bookmark_file_has_item (priv->recent_items, uri))
1121 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1122 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1123 _("Unable to find an item with URI '%s'"),
1129 res = g_bookmark_file_move_item (priv->recent_items,
1134 g_error_free (move_error);
1136 g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1137 GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1138 _("Unable to find an item with URI '%s'"),
1143 priv->is_dirty = TRUE;
1145 gtk_recent_manager_changed (recent_manager);
1151 * gtk_recent_manager_get_items:
1152 * @manager: a #GtkRecentManager
1154 * Gets the list of recently used resources.
1156 * Return value: (element-type GtkRecentInfo) (transfer full): a list of
1157 * newly allocated #GtkRecentInfo objects. Use
1158 * gtk_recent_info_unref() on each item inside the list, and then
1159 * free the list itself using g_list_free().
1164 gtk_recent_manager_get_items (GtkRecentManager *manager)
1166 GtkRecentManagerPrivate *priv;
1167 GList *retval = NULL;
1171 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1173 priv = manager->priv;
1174 if (!priv->recent_items)
1177 uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1178 for (i = 0; i < uris_len; i++)
1180 GtkRecentInfo *info;
1182 info = gtk_recent_info_new (uris[i]);
1183 build_recent_info (priv->recent_items, info);
1185 retval = g_list_prepend (retval, info);
1194 purge_recent_items_list (GtkRecentManager *manager,
1197 GtkRecentManagerPrivate *priv = manager->priv;
1199 if (!priv->recent_items)
1202 g_bookmark_file_free (priv->recent_items);
1203 priv->recent_items = NULL;
1205 priv->recent_items = g_bookmark_file_new ();
1207 priv->is_dirty = TRUE;
1209 /* emit the changed signal, to ensure that the purge is written */
1210 gtk_recent_manager_changed (manager);
1214 * gtk_recent_manager_purge_items:
1215 * @manager: a #GtkRecentManager
1216 * @error: (allow-none): a return location for a #GError, or %NULL
1218 * Purges every item from the recently used resources list.
1220 * Return value: the number of items that have been removed from the
1221 * recently used resources list.
1226 gtk_recent_manager_purge_items (GtkRecentManager *manager,
1229 GtkRecentManagerPrivate *priv;
1232 g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1234 priv = manager->priv;
1235 if (!priv->recent_items)
1238 count = g_bookmark_file_get_size (priv->recent_items);
1242 purge_recent_items_list (manager, error);
1244 purged = count - g_bookmark_file_get_size (priv->recent_items);
1250 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1252 g_signal_emit (recent_manager, signal_changed, 0);
1256 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1259 GtkRecentManagerPrivate *priv = manager->priv;
1264 if (G_UNLIKELY (!priv->recent_items))
1269 uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1271 for (i = 0; i < n_uris; i++)
1273 const gchar *uri = uris[i];
1277 modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1278 item_age = (gint) ((now - modified) / (60 * 60 * 24));
1280 g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1291 gtk_recent_info_get_type (void)
1293 static GType info_type = 0;
1296 info_type = g_boxed_type_register_static (I_("GtkRecentInfo"),
1297 (GBoxedCopyFunc) gtk_recent_info_ref,
1298 (GBoxedFreeFunc) gtk_recent_info_unref);
1302 static GtkRecentInfo *
1303 gtk_recent_info_new (const gchar *uri)
1305 GtkRecentInfo *info;
1307 g_assert (uri != NULL);
1309 info = g_new0 (GtkRecentInfo, 1);
1310 info->uri = g_strdup (uri);
1312 info->applications = NULL;
1313 info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1315 info->groups = NULL;
1317 info->ref_count = 1;
1323 gtk_recent_info_free (GtkRecentInfo *recent_info)
1328 g_free (recent_info->uri);
1329 g_free (recent_info->display_name);
1330 g_free (recent_info->description);
1331 g_free (recent_info->mime_type);
1333 if (recent_info->applications)
1335 g_slist_foreach (recent_info->applications,
1336 (GFunc) recent_app_info_free,
1338 g_slist_free (recent_info->applications);
1340 recent_info->applications = NULL;
1343 if (recent_info->apps_lookup)
1344 g_hash_table_destroy (recent_info->apps_lookup);
1346 if (recent_info->groups)
1348 g_slist_foreach (recent_info->groups,
1351 g_slist_free (recent_info->groups);
1353 recent_info->groups = NULL;
1356 if (recent_info->icon)
1357 g_object_unref (recent_info->icon);
1359 g_free (recent_info);
1363 * gtk_recent_info_ref:
1364 * @info: a #GtkRecentInfo
1366 * Increases the reference count of @recent_info by one.
1368 * Return value: the recent info object with its reference count increased
1374 gtk_recent_info_ref (GtkRecentInfo *info)
1376 g_return_val_if_fail (info != NULL, NULL);
1377 g_return_val_if_fail (info->ref_count > 0, NULL);
1379 info->ref_count += 1;
1385 * gtk_recent_info_unref:
1386 * @info: a #GtkRecentInfo
1388 * Decreases the reference count of @info by one. If the reference
1389 * count reaches zero, @info is deallocated, and the memory freed.
1394 gtk_recent_info_unref (GtkRecentInfo *info)
1396 g_return_if_fail (info != NULL);
1397 g_return_if_fail (info->ref_count > 0);
1399 info->ref_count -= 1;
1401 if (info->ref_count == 0)
1402 gtk_recent_info_free (info);
1406 * gtk_recent_info_get_uri:
1407 * @info: a #GtkRecentInfo
1409 * Gets the URI of the resource.
1411 * Return value: the URI of the resource. The returned string is
1412 * owned by the recent manager, and should not be freed.
1416 G_CONST_RETURN gchar *
1417 gtk_recent_info_get_uri (GtkRecentInfo *info)
1419 g_return_val_if_fail (info != NULL, NULL);
1425 * gtk_recent_info_get_display_name:
1426 * @info: a #GtkRecentInfo
1428 * Gets the name of the resource. If none has been defined, the basename
1429 * of the resource is obtained.
1431 * Return value: the display name of the resource. The returned string
1432 * is owned by the recent manager, and should not be freed.
1436 G_CONST_RETURN gchar *
1437 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1439 g_return_val_if_fail (info != NULL, NULL);
1441 if (!info->display_name)
1442 info->display_name = gtk_recent_info_get_short_name (info);
1444 return info->display_name;
1448 * gtk_recent_info_get_description:
1449 * @info: a #GtkRecentInfo
1451 * Gets the (short) description of the resource.
1453 * Return value: the description of the resource. The returned string
1454 * is owned by the recent manager, and should not be freed.
1458 G_CONST_RETURN gchar *
1459 gtk_recent_info_get_description (GtkRecentInfo *info)
1461 g_return_val_if_fail (info != NULL, NULL);
1463 return info->description;
1467 * gtk_recent_info_get_mime_type:
1468 * @info: a #GtkRecentInfo
1470 * Gets the MIME type of the resource.
1472 * Return value: the MIME type of the resource. The returned string
1473 * is owned by the recent manager, and should not be freed.
1477 G_CONST_RETURN gchar *
1478 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1480 g_return_val_if_fail (info != NULL, NULL);
1482 if (!info->mime_type)
1483 info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1485 return info->mime_type;
1489 * gtk_recent_info_get_added:
1490 * @info: a #GtkRecentInfo
1492 * Gets the timestamp (seconds from system's Epoch) when the resource
1493 * was added to the recently used resources list.
1495 * Return value: the number of seconds elapsed from system's Epoch when
1496 * the resource was added to the list, or -1 on failure.
1501 gtk_recent_info_get_added (GtkRecentInfo *info)
1503 g_return_val_if_fail (info != NULL, (time_t) -1);
1509 * gtk_recent_info_get_modified:
1510 * @info: a #GtkRecentInfo
1512 * Gets the timestamp (seconds from system's Epoch) when the resource
1513 * was last modified.
1515 * Return value: the number of seconds elapsed from system's Epoch when
1516 * the resource was last modified, or -1 on failure.
1521 gtk_recent_info_get_modified (GtkRecentInfo *info)
1523 g_return_val_if_fail (info != NULL, (time_t) -1);
1525 return info->modified;
1529 * gtk_recent_info_get_visited:
1530 * @info: a #GtkRecentInfo
1532 * Gets the timestamp (seconds from system's Epoch) when the resource
1535 * Return value: the number of seconds elapsed from system's Epoch when
1536 * the resource was last visited, or -1 on failure.
1541 gtk_recent_info_get_visited (GtkRecentInfo *info)
1543 g_return_val_if_fail (info != NULL, (time_t) -1);
1545 return info->visited;
1549 * gtk_recent_info_get_private_hint:
1550 * @info: a #GtkRecentInfo
1552 * Gets the value of the "private" flag. Resources in the recently used
1553 * list that have this flag set to %TRUE should only be displayed by the
1554 * applications that have registered them.
1556 * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1561 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1563 g_return_val_if_fail (info != NULL, FALSE);
1565 return info->is_private;
1569 static RecentAppInfo *
1570 recent_app_info_new (const gchar *app_name)
1572 RecentAppInfo *app_info;
1574 g_assert (app_name != NULL);
1576 app_info = g_slice_new0 (RecentAppInfo);
1577 app_info->name = g_strdup (app_name);
1578 app_info->exec = NULL;
1579 app_info->count = 1;
1580 app_info->stamp = 0;
1586 recent_app_info_free (RecentAppInfo *app_info)
1591 g_free (app_info->name);
1592 g_free (app_info->exec);
1594 g_slice_free (RecentAppInfo, app_info);
1598 * gtk_recent_info_get_application_info:
1599 * @info: a #GtkRecentInfo
1600 * @app_name: the name of the application that has registered this item
1601 * @app_exec: (transfer none) (out): return location for the string containing the command line
1602 * @count: (out): return location for the number of times this item was registered
1603 * @time_: (out): return location for the timestamp this item was last registered
1604 * for this application
1606 * Gets the data regarding the application that has registered the resource
1609 * If the command line contains any escape characters defined inside the
1610 * storage specification, they will be expanded.
1612 * Return value: %TRUE if an application with @app_name has registered this
1613 * resource inside the recently used list, or %FALSE otherwise. The
1614 * @app_exec string is owned by the #GtkRecentInfo and should not be
1620 gtk_recent_info_get_application_info (GtkRecentInfo *info,
1621 const gchar *app_name,
1622 const gchar **app_exec,
1628 g_return_val_if_fail (info != NULL, FALSE);
1629 g_return_val_if_fail (app_name != NULL, FALSE);
1631 ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1635 g_warning ("No registered application with name '%s' "
1636 "for item with URI '%s' found",
1643 *app_exec = ai->exec;
1655 * gtk_recent_info_get_applications:
1656 * @info: a #GtkRecentInfo
1657 * @length: (out) (allow-none): return location for the length of the returned list
1659 * Retrieves the list of applications that have registered this resource.
1661 * Return value: (array length=length zero-terminated=1): a newly allocated
1662 * %NULL-terminated array of strings. Use g_strfreev() to free it.
1667 gtk_recent_info_get_applications (GtkRecentInfo *info,
1674 g_return_val_if_fail (info != NULL, NULL);
1676 if (!info->applications)
1684 n_apps = g_slist_length (info->applications);
1686 retval = g_new0 (gchar *, n_apps + 1);
1688 for (l = info->applications, i = 0;
1692 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1694 g_assert (ai != NULL);
1696 retval[i++] = g_strdup (ai->name);
1707 * gtk_recent_info_has_application:
1708 * @info: a #GtkRecentInfo
1709 * @app_name: a string containing an application name
1711 * Checks whether an application registered this resource using @app_name.
1713 * Return value: %TRUE if an application with name @app_name was found,
1719 gtk_recent_info_has_application (GtkRecentInfo *info,
1720 const gchar *app_name)
1722 g_return_val_if_fail (info != NULL, FALSE);
1723 g_return_val_if_fail (app_name != NULL, FALSE);
1725 return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1729 * gtk_recent_info_last_application:
1730 * @info: a #GtkRecentInfo
1732 * Gets the name of the last application that have registered the
1733 * recently used resource represented by @info.
1735 * Return value: an application name. Use g_free() to free it.
1740 gtk_recent_info_last_application (GtkRecentInfo *info)
1743 time_t last_stamp = (time_t) -1;
1746 g_return_val_if_fail (info != NULL, NULL);
1748 for (l = info->applications; l != NULL; l = l->next)
1750 RecentAppInfo *ai = (RecentAppInfo *) l->data;
1752 if (ai->stamp > last_stamp)
1755 last_stamp = ai->stamp;
1759 return g_strdup (name);
1763 get_icon_for_mime_type (const char *mime_type,
1766 GtkIconTheme *icon_theme;
1772 icon_theme = gtk_icon_theme_get_default ();
1774 content_type = g_content_type_from_mime_type (mime_type);
1779 icon = g_content_type_get_icon (content_type);
1780 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
1783 GTK_ICON_LOOKUP_USE_BUILTIN);
1784 g_free (content_type);
1785 g_object_unref (icon);
1790 pixbuf = gtk_icon_info_load_icon (info, NULL);
1791 gtk_icon_info_free (info);
1797 get_icon_fallback (const gchar *icon_name,
1800 GtkIconTheme *icon_theme;
1803 icon_theme = gtk_icon_theme_get_default ();
1805 retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1807 GTK_ICON_LOOKUP_USE_BUILTIN,
1809 g_assert (retval != NULL);
1815 * gtk_recent_info_get_icon:
1816 * @info: a #GtkRecentInfo
1817 * @size: the size of the icon in pixels
1819 * Retrieves the icon of size @size associated to the resource MIME type.
1821 * Return value: a #GdkPixbuf containing the icon, or %NULL. Use
1822 * g_object_unref() when finished using the icon.
1827 gtk_recent_info_get_icon (GtkRecentInfo *info,
1830 GdkPixbuf *retval = NULL;
1832 g_return_val_if_fail (info != NULL, NULL);
1834 if (info->mime_type)
1835 retval = get_icon_for_mime_type (info->mime_type, size);
1837 /* this function should never fail */
1840 if (info->mime_type &&
1841 strcmp (info->mime_type, "x-directory/normal") == 0)
1842 retval = get_icon_fallback (GTK_STOCK_DIRECTORY, size);
1844 retval = get_icon_fallback (GTK_STOCK_FILE, size);
1851 * gtk_recent_info_is_local:
1852 * @info: a #GtkRecentInfo
1854 * Checks whether the resource is local or not by looking at the
1855 * scheme of its URI.
1857 * Return value: %TRUE if the resource is local.
1862 gtk_recent_info_is_local (GtkRecentInfo *info)
1864 g_return_val_if_fail (info != NULL, FALSE);
1866 return has_case_prefix (info->uri, "file:/");
1870 * gtk_recent_info_exists:
1871 * @info: a #GtkRecentInfo
1873 * Checks whether the resource pointed by @info still exists. At
1874 * the moment this check is done only on resources pointing to local files.
1876 * Return value: %TRUE if the resource exists
1881 gtk_recent_info_exists (GtkRecentInfo *info)
1884 struct stat stat_buf;
1885 gboolean retval = FALSE;
1887 g_return_val_if_fail (info != NULL, FALSE);
1889 /* we guarantee only local resources */
1890 if (!gtk_recent_info_is_local (info))
1893 filename = g_filename_from_uri (info->uri, NULL, NULL);
1896 if (stat (filename, &stat_buf) == 0)
1906 * gtk_recent_info_match:
1907 * @info_a: a #GtkRecentInfo
1908 * @info_b: a #GtkRecentInfo
1910 * Checks whether two #GtkRecentInfo structures point to the same
1913 * Return value: %TRUE if both #GtkRecentInfo structures point to se same
1914 * resource, %FALSE otherwise.
1919 gtk_recent_info_match (GtkRecentInfo *info_a,
1920 GtkRecentInfo *info_b)
1922 g_return_val_if_fail (info_a != NULL, FALSE);
1923 g_return_val_if_fail (info_b != NULL, FALSE);
1925 return (0 == strcmp (info_a->uri, info_b->uri));
1928 /* taken from gnome-vfs-uri.c */
1929 static const gchar *
1930 get_method_string (const gchar *substring,
1931 gchar **method_string)
1937 g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
1944 !(p == substring + 1 && g_ascii_isalpha (*substring))
1948 /* Found toplevel method specification. */
1949 method = g_strndup (substring, p - substring);
1950 *method_string = g_ascii_strdown (method, -1);
1956 *method_string = g_strdup ("file");
1963 /* Stolen from gnome_vfs_make_valid_utf8() */
1965 make_valid_utf8 (const char *name)
1968 const char *remainder, *invalid;
1969 int remaining_bytes, valid_bytes;
1973 remaining_bytes = name ? strlen (name) : 0;
1975 while (remaining_bytes != 0)
1977 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
1980 valid_bytes = invalid - remainder;
1983 string = g_string_sized_new (remaining_bytes);
1985 g_string_append_len (string, remainder, valid_bytes);
1986 g_string_append_c (string, '?');
1988 remaining_bytes -= valid_bytes + 1;
1989 remainder = invalid + 1;
1993 return g_strdup (name);
1995 g_string_append (string, remainder);
1996 g_assert (g_utf8_validate (string->str, -1, NULL));
1998 return g_string_free (string, FALSE);
2002 get_uri_shortname_for_display (const gchar *uri)
2005 gboolean validated = FALSE;
2007 if (has_case_prefix (uri, "file:/"))
2011 local_file = g_filename_from_uri (uri, NULL, NULL);
2015 name = g_filename_display_basename (local_file);
2019 g_free (local_file);
2028 rest = get_method_string (uri, &method);
2029 local_file = g_filename_display_basename (rest);
2031 name = g_strconcat (method, ": ", local_file, NULL);
2033 g_free (local_file);
2037 g_assert (name != NULL);
2039 if (!validated && !g_utf8_validate (name, -1, NULL))
2043 utf8_name = make_valid_utf8 (name);
2053 * gtk_recent_info_get_short_name:
2054 * @info: an #GtkRecentInfo
2056 * Computes a valid UTF-8 string that can be used as the name of the item in a
2057 * menu or list. For example, calling this function on an item that refers to
2058 * "file:///foo/bar.txt" will yield "bar.txt".
2060 * Return value: A newly-allocated string in UTF-8 encoding; free it with
2066 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2070 g_return_val_if_fail (info != NULL, NULL);
2072 if (info->uri == NULL)
2075 short_name = get_uri_shortname_for_display (info->uri);
2081 * gtk_recent_info_get_uri_display:
2082 * @info: a #GtkRecentInfo
2084 * Gets a displayable version of the resource's URI. If the resource
2085 * is local, it returns a local path; if the resource is not local,
2086 * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2088 * Return value: a newly allocated UTF-8 string containing the
2089 * resource's URI or %NULL. Use g_free() when done using it.
2094 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2098 g_return_val_if_fail (info != NULL, NULL);
2101 if (gtk_recent_info_is_local (info))
2105 filename = g_filename_from_uri (info->uri, NULL, NULL);
2109 retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2114 retval = make_valid_utf8 (info->uri);
2121 * gtk_recent_info_get_age:
2122 * @info: a #GtkRecentInfo
2124 * Gets the number of days elapsed since the last update of the resource
2127 * Return value: a positive integer containing the number of days elapsed
2128 * since the time this resource was last modified.
2133 gtk_recent_info_get_age (GtkRecentInfo *info)
2138 g_return_val_if_fail (info != NULL, -1);
2142 delta = now - info->modified;
2144 retval = (gint) (delta / (60 * 60 * 24));
2150 * gtk_recent_info_get_groups:
2151 * @info: a #GtkRecentInfo
2152 * @length: (out) (allow-none): return location for the number of groups returned
2154 * Returns all groups registered for the recently used item @info. The
2155 * array of returned group names will be %NULL terminated, so length might
2156 * optionally be %NULL.
2158 * Return value: (array length=length zero-terminated=1): a newly allocated
2159 * %NULL terminated array of strings. Use g_strfreev() to free it.
2164 gtk_recent_info_get_groups (GtkRecentInfo *info,
2171 g_return_val_if_fail (info != NULL, NULL);
2181 n_groups = g_slist_length (info->groups);
2183 retval = g_new0 (gchar *, n_groups + 1);
2185 for (l = info->groups, i = 0;
2189 gchar *group_name = (gchar *) l->data;
2191 g_assert (group_name != NULL);
2193 retval[i++] = g_strdup (group_name);
2204 * gtk_recent_info_has_group:
2205 * @info: a #GtkRecentInfo
2206 * @group_name: name of a group
2208 * Checks whether @group_name appears inside the groups registered for the
2209 * recently used item @info.
2211 * Return value: %TRUE if the group was found.
2216 gtk_recent_info_has_group (GtkRecentInfo *info,
2217 const gchar *group_name)
2221 g_return_val_if_fail (info != NULL, FALSE);
2222 g_return_val_if_fail (group_name != NULL, FALSE);
2227 for (l = info->groups; l != NULL; l = l->next)
2229 gchar *g = (gchar *) l->data;
2231 if (strcmp (g, group_name) == 0)
2239 * _gtk_recent_manager_sync:
2241 * Private function for synchronising the recent manager singleton.
2244 _gtk_recent_manager_sync (void)
2246 if (recent_manager_singleton)
2248 /* force a dump of the contents of the recent manager singleton */
2249 recent_manager_singleton->priv->is_dirty = TRUE;
2250 gtk_recent_manager_real_changed (recent_manager_singleton);