summaryrefslogtreecommitdiff
path: root/gtk/gtkrecentchooserutils.c
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2007-03-14 10:33:36 +0000
committerEmmanuele Bassi <ebassi@src.gnome.org>2007-03-14 10:33:36 +0000
commit86ea351bd71aaf1c420b9a3b1cee74649f197b8a (patch)
treebbe071490825a78940a36ce00a1d6f457900bde5 /gtk/gtkrecentchooserutils.c
parent4b3364ccc947230f62be8339ad7476d7151bfb51 (diff)
downloadgtk+-86ea351bd71aaf1c420b9a3b1cee74649f197b8a.tar.gz
Various clean ups in the GtkRecent code. (see #338843)
2007-03-14 Emmanuele Bassi <ebassi@gnome.org> Various clean ups in the GtkRecent code. (see #338843) * gtk/gtkrecentchooserdefault.c: * gtk/gtkrecentchoosermenu.c: * gtk/gtkrecentchooserprivate.h: * gtk/gtkrecentchooserutils.c: Move the recent chooser function for getting the sorted and clamped list of recent files from the manager outside the implementations. * gtk/gtkrecentchooserdefault.c (chooser_set_sort_type): Repopulate the list when the sorting order changes. (gtk_recent_chooser_default_dispose), (gtk_recent_chooser_default_finalize): Move object unref and source removal from finalize to dispose. * gtk/gtkrecentchooser.c (gtk_recent_chooser_type_init): Relax the prerequisite for the GtkRecentChooser interface implementations, from GtkObject to GObject. (gtk_recent_chooser_class_init): Use GTK_PARAM_* instead of G_PARAM_* svn path=/trunk/; revision=17514
Diffstat (limited to 'gtk/gtkrecentchooserutils.c')
-rw-r--r--gtk/gtkrecentchooserutils.c136
1 files changed, 136 insertions, 0 deletions
diff --git a/gtk/gtkrecentchooserutils.c b/gtk/gtkrecentchooserutils.c
index f931486c6c..64fc81e331 100644
--- a/gtk/gtkrecentchooserutils.c
+++ b/gtk/gtkrecentchooserutils.c
@@ -294,3 +294,139 @@ delegate_item_activated (GtkRecentChooser *receiver,
{
_gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
}
+
+static gint
+sort_recent_items_mru (GtkRecentInfo *a,
+ GtkRecentInfo *b,
+ gpointer unused)
+{
+ g_assert (a != NULL && b != NULL);
+
+ return (gtk_recent_info_get_modified (a) < gtk_recent_info_get_modified (b));
+}
+
+static gint
+sort_recent_items_lru (GtkRecentInfo *a,
+ GtkRecentInfo *b,
+ gpointer unused)
+{
+ g_assert (a != NULL && b != NULL);
+
+ return (gtk_recent_info_get_modified (a) > gtk_recent_info_get_modified (b));
+}
+
+typedef struct
+{
+ GtkRecentSortFunc func;
+ gpointer data;
+} SortRecentData;
+
+/* our proxy sorting function */
+static gint
+sort_recent_items_proxy (gpointer *a,
+ gpointer *b,
+ gpointer user_data)
+{
+ GtkRecentInfo *info_a = (GtkRecentInfo *) a;
+ GtkRecentInfo *info_b = (GtkRecentInfo *) b;
+ SortRecentData *sort_recent = user_data;
+
+ if (sort_recent->func)
+ return (* sort_recent->func) (info_a,
+ info_b,
+ sort_recent->data);
+
+ /* fallback */
+ return 0;
+}
+
+/*
+ * _gtk_recent_chooser_get_items:
+ * @chooser: a #GtkRecentChooser
+ * @sort_func: sorting function, or %NULL
+ * @sort_data: sorting function data, or %NULL
+ *
+ * Default implementation for getting the (sorted and clamped) list
+ * of recently used resources from a #GtkRecentChooser. This function
+ * should be used by implementations of the #GtkRecentChooser
+ * interface inside the GtkRecentChooser::get_items vfunc.
+ *
+ * Return value: a list of #GtkRecentInfo objects
+ */
+GList *
+_gtk_recent_chooser_get_items (GtkRecentChooser *chooser,
+ GtkRecentSortFunc sort_func,
+ gpointer sort_data)
+{
+ GtkRecentManager *manager;
+ gint limit;
+ GtkRecentSortType sort_type;
+ GList *items;
+ GCompareDataFunc compare_func;
+ gint length;
+
+ g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
+
+ manager = _gtk_recent_chooser_get_recent_manager (chooser);
+ if (!manager)
+ return NULL;
+
+ items = gtk_recent_manager_get_items (manager);
+ if (!items)
+ return NULL;
+
+ limit = gtk_recent_chooser_get_limit (chooser);
+ if (limit == 0)
+ return NULL;
+
+ sort_type = gtk_recent_chooser_get_sort_type (chooser);
+ switch (sort_type)
+ {
+ case GTK_RECENT_SORT_NONE:
+ compare_func = NULL;
+ break;
+ case GTK_RECENT_SORT_MRU:
+ compare_func = (GCompareDataFunc) sort_recent_items_mru;
+ break;
+ case GTK_RECENT_SORT_LRU:
+ compare_func = (GCompareDataFunc) sort_recent_items_lru;
+ break;
+ case GTK_RECENT_SORT_CUSTOM:
+ compare_func = (GCompareDataFunc) sort_recent_items_proxy;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ if (compare_func)
+ {
+ SortRecentData *sort_recent;
+
+ sort_recent = g_slice_new (SortRecentData);
+ sort_recent->func = sort_func;
+ sort_recent->data = sort_data;
+
+ items = g_list_sort_with_data (items, compare_func, sort_recent);
+
+ g_slice_free (SortRecentData, sort_recent);
+ }
+
+ length = g_list_length (items);
+ if ((limit != -1) && (length > limit))
+ {
+ GList *clamp, *l;
+
+ clamp = g_list_nth (items, limit - 1);
+ if (!clamp)
+ return items;
+
+ l = clamp->next;
+ clamp->next = NULL;
+
+ g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
+ g_list_free (l);
+ }
+
+ return items;
+}