]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentchooserutils.c
Various clean ups in the GtkRecent code. (see #338843)
[~andy/gtk] / gtk / gtkrecentchooserutils.c
1 /* gtkrecentchooserutils.h - Private utility functions for implementing a
2  *                           GtkRecentChooser interface
3  *
4  * Copyright (C) 2006 Emmanuele Bassi
5  *
6  * All rights reserved
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  * Based on gtkfilechooserutils.c:
24  *      Copyright (C) 2003 Red Hat, Inc.
25  */
26
27 #include "config.h"
28
29 #include "gtkrecentchooserutils.h"
30 #include "gtkalias.h"
31
32 /* Methods */
33 static void      delegate_set_sort_func              (GtkRecentChooser  *chooser,
34                                                       GtkRecentSortFunc  sort_func,
35                                                       gpointer           sort_data,
36                                                       GDestroyNotify     data_destroy);
37 static void      delegate_add_filter                 (GtkRecentChooser  *chooser,
38                                                       GtkRecentFilter   *filter);
39 static void      delegate_remove_filter              (GtkRecentChooser  *chooser,
40                                                       GtkRecentFilter   *filter);
41 static GSList   *delegate_list_filters               (GtkRecentChooser  *chooser);
42 static gboolean  delegate_select_uri                 (GtkRecentChooser  *chooser,
43                                                       const gchar       *uri,
44                                                       GError           **error);
45 static void      delegate_unselect_uri               (GtkRecentChooser  *chooser,
46                                                       const gchar       *uri);
47 static GList    *delegate_get_items                  (GtkRecentChooser  *chooser);
48 static GtkRecentManager *delegate_get_recent_manager (GtkRecentChooser  *chooser);
49 static void      delegate_select_all                 (GtkRecentChooser  *chooser);
50 static void      delegate_unselect_all               (GtkRecentChooser  *chooser);
51 static gboolean  delegate_set_current_uri            (GtkRecentChooser  *chooser,
52                                                       const gchar       *uri,
53                                                       GError           **error);
54 static gchar *   delegate_get_current_uri            (GtkRecentChooser  *chooser);
55
56 /* Signals */
57 static void      delegate_notify            (GObject          *object,
58                                              GParamSpec       *pspec,
59                                              gpointer          user_data);
60 static void      delegate_selection_changed (GtkRecentChooser *receiver,
61                                              gpointer          user_data);
62 static void      delegate_item_activated    (GtkRecentChooser *receiver,
63                                              gpointer          user_data);
64
65 /**
66  * _gtk_recent_chooser_install_properties:
67  * @klass: the class structure for a type deriving from #GObject
68  *
69  * Installs the necessary properties for a class implementing
70  * #GtkRecentChooser. A #GtkParamSpecOverride property is installed
71  * for each property, using the values from the #GtkRecentChooserProp
72  * enumeration. The caller must make sure itself that the enumeration
73  * values don't collide with some other property values they
74  * are using.
75  */
76 void
77 _gtk_recent_chooser_install_properties (GObjectClass *klass)
78 {
79   g_object_class_override_property (klass,
80                                     GTK_RECENT_CHOOSER_PROP_RECENT_MANAGER,
81                                     "recent-manager");                                      
82   g_object_class_override_property (klass,
83                                     GTK_RECENT_CHOOSER_PROP_SHOW_PRIVATE,
84                                     "show-private");
85   g_object_class_override_property (klass,
86                                     GTK_RECENT_CHOOSER_PROP_SHOW_TIPS,
87                                     "show-tips");
88   g_object_class_override_property (klass,
89                                     GTK_RECENT_CHOOSER_PROP_SHOW_ICONS,
90                                     "show-icons");
91   g_object_class_override_property (klass,
92                                     GTK_RECENT_CHOOSER_PROP_SHOW_NOT_FOUND,
93                                     "show-not-found");
94   g_object_class_override_property (klass,
95                                     GTK_RECENT_CHOOSER_PROP_SELECT_MULTIPLE,
96                                     "select-multiple");
97   g_object_class_override_property (klass,
98                                     GTK_RECENT_CHOOSER_PROP_LIMIT,
99                                     "limit");
100   g_object_class_override_property (klass,
101                                     GTK_RECENT_CHOOSER_PROP_LOCAL_ONLY,
102                                     "local-only");
103   g_object_class_override_property (klass,
104                                     GTK_RECENT_CHOOSER_PROP_SORT_TYPE,
105                                     "sort-type");
106   g_object_class_override_property (klass,
107                                     GTK_RECENT_CHOOSER_PROP_FILTER,
108                                     "filter");
109 }
110
111 /**
112  * _gtk_recent_chooser_delegate_iface_init:
113  * @iface: a #GtkRecentChooserIface
114  *
115  * An interface-initialization function for use in cases where
116  * an object is simply delegating the methods, signals of
117  * the #GtkRecentChooser interface to another object.
118  * _gtk_recent_chooser_set_delegate() must be called on each
119  * instance of the object so that the delegate object can
120  * be found.
121  */
122 void
123 _gtk_recent_chooser_delegate_iface_init (GtkRecentChooserIface *iface)
124 {
125   iface->set_current_uri = delegate_set_current_uri;
126   iface->get_current_uri = delegate_get_current_uri;
127   iface->select_uri = delegate_select_uri;
128   iface->unselect_uri = delegate_unselect_uri;
129   iface->select_all = delegate_select_all;
130   iface->unselect_all = delegate_unselect_all;
131   iface->get_items = delegate_get_items;
132   iface->get_recent_manager = delegate_get_recent_manager;
133   iface->set_sort_func = delegate_set_sort_func;
134   iface->add_filter = delegate_add_filter;
135   iface->remove_filter = delegate_remove_filter;
136   iface->list_filters = delegate_list_filters;
137 }
138
139 /**
140  * _gtk_recent_chooser_set_delegate:
141  * @receiver: a #GObject implementing #GtkRecentChooser
142  * @delegate: another #GObject implementing #GtkRecentChooser
143  *
144  * Establishes that calls on @receiver for #GtkRecentChooser
145  * methods should be delegated to @delegate, and that
146  * #GtkRecentChooser signals emitted on @delegate should be
147  * forwarded to @receiver. Must be used in conjunction with
148  * _gtk_recent_chooser_delegate_iface_init().
149  */
150 void
151 _gtk_recent_chooser_set_delegate (GtkRecentChooser *receiver,
152                                   GtkRecentChooser *delegate)
153 {
154   g_return_if_fail (GTK_IS_RECENT_CHOOSER (receiver));
155   g_return_if_fail (GTK_IS_RECENT_CHOOSER (delegate));
156   
157   g_object_set_data (G_OBJECT (receiver),
158                     "gtk-recent-chooser-delegate", delegate);
159   
160   g_signal_connect (delegate, "notify",
161                     G_CALLBACK (delegate_notify), receiver);
162   g_signal_connect (delegate, "selection-changed",
163                     G_CALLBACK (delegate_selection_changed), receiver);
164   g_signal_connect (delegate, "item_activated",
165                     G_CALLBACK (delegate_item_activated), receiver);
166 }
167
168 GQuark
169 _gtk_recent_chooser_delegate_get_quark (void)
170 {
171   static GQuark quark = 0;
172   
173   if (G_UNLIKELY (quark == 0))
174     quark = g_quark_from_static_string ("gtk-recent-chooser-delegate");
175   
176   return quark;
177 }
178
179 static GtkRecentChooser *
180 get_delegate (GtkRecentChooser *receiver)
181 {
182   return g_object_get_qdata (G_OBJECT (receiver),
183                              GTK_RECENT_CHOOSER_DELEGATE_QUARK);
184 }
185
186 static void
187 delegate_set_sort_func (GtkRecentChooser  *chooser,
188                         GtkRecentSortFunc  sort_func,
189                         gpointer           sort_data,
190                         GDestroyNotify     data_destroy)
191 {
192   gtk_recent_chooser_set_sort_func (get_delegate (chooser),
193                                     sort_func,
194                                     sort_data,
195                                     data_destroy);
196 }
197
198 static void
199 delegate_add_filter (GtkRecentChooser *chooser,
200                      GtkRecentFilter  *filter)
201 {
202   gtk_recent_chooser_add_filter (get_delegate (chooser), filter);
203 }
204
205 static void
206 delegate_remove_filter (GtkRecentChooser *chooser,
207                         GtkRecentFilter  *filter)
208 {
209   gtk_recent_chooser_remove_filter (get_delegate (chooser), filter);
210 }
211
212 static GSList *
213 delegate_list_filters (GtkRecentChooser *chooser)
214 {
215   return gtk_recent_chooser_list_filters (get_delegate (chooser));
216 }
217
218 static gboolean
219 delegate_select_uri (GtkRecentChooser  *chooser,
220                      const gchar       *uri,
221                      GError           **error)
222 {
223   return gtk_recent_chooser_select_uri (get_delegate (chooser), uri, error);
224 }
225
226 static void
227 delegate_unselect_uri (GtkRecentChooser *chooser,
228                        const gchar      *uri)
229 {
230  gtk_recent_chooser_unselect_uri (get_delegate (chooser), uri);
231 }
232
233 static GList *
234 delegate_get_items (GtkRecentChooser *chooser)
235 {
236   return gtk_recent_chooser_get_items (get_delegate (chooser));
237 }
238
239 static GtkRecentManager *
240 delegate_get_recent_manager (GtkRecentChooser *chooser)
241 {
242   return _gtk_recent_chooser_get_recent_manager (get_delegate (chooser));
243 }
244
245 static void
246 delegate_select_all (GtkRecentChooser *chooser)
247 {
248   gtk_recent_chooser_select_all (get_delegate (chooser));
249 }
250
251 static void
252 delegate_unselect_all (GtkRecentChooser *chooser)
253 {
254   gtk_recent_chooser_unselect_all (get_delegate (chooser));
255 }
256
257 static gboolean
258 delegate_set_current_uri (GtkRecentChooser  *chooser,
259                           const gchar       *uri,
260                           GError           **error)
261 {
262   return gtk_recent_chooser_set_current_uri (get_delegate (chooser), uri, error);
263 }
264
265 static gchar *
266 delegate_get_current_uri (GtkRecentChooser *chooser)
267 {
268   return gtk_recent_chooser_get_current_uri (get_delegate (chooser));
269 }
270
271 static void
272 delegate_notify (GObject    *object,
273                  GParamSpec *pspec,
274                  gpointer    user_data)
275 {
276   gpointer iface;
277
278   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (object)),
279                                  gtk_recent_chooser_get_type ());
280   if (g_object_interface_find_property (iface, pspec->name))
281     g_object_notify (user_data, pspec->name);
282 }
283
284 static void
285 delegate_selection_changed (GtkRecentChooser *receiver,
286                             gpointer          user_data)
287 {
288   _gtk_recent_chooser_selection_changed (GTK_RECENT_CHOOSER (user_data));
289 }
290
291 static void
292 delegate_item_activated (GtkRecentChooser *receiver,
293                          gpointer          user_data)
294 {
295   _gtk_recent_chooser_item_activated (GTK_RECENT_CHOOSER (user_data));
296 }
297
298 static gint
299 sort_recent_items_mru (GtkRecentInfo *a,
300                        GtkRecentInfo *b,
301                        gpointer       unused)
302 {
303   g_assert (a != NULL && b != NULL);
304   
305   return (gtk_recent_info_get_modified (a) < gtk_recent_info_get_modified (b));
306 }
307
308 static gint
309 sort_recent_items_lru (GtkRecentInfo *a,
310                        GtkRecentInfo *b,
311                        gpointer       unused)
312 {
313   g_assert (a != NULL && b != NULL);
314   
315   return (gtk_recent_info_get_modified (a) > gtk_recent_info_get_modified (b));
316 }
317
318 typedef struct
319 {
320   GtkRecentSortFunc func;
321   gpointer data;
322 } SortRecentData;
323
324 /* our proxy sorting function */
325 static gint
326 sort_recent_items_proxy (gpointer *a,
327                          gpointer *b,
328                          gpointer  user_data)
329 {
330   GtkRecentInfo *info_a = (GtkRecentInfo *) a;
331   GtkRecentInfo *info_b = (GtkRecentInfo *) b;
332   SortRecentData *sort_recent = user_data;
333
334   if (sort_recent->func)
335     return (* sort_recent->func) (info_a,
336                                   info_b,
337                                   sort_recent->data);
338   
339   /* fallback */
340   return 0;
341 }
342
343 /*
344  * _gtk_recent_chooser_get_items:
345  * @chooser: a #GtkRecentChooser
346  * @sort_func: sorting function, or %NULL
347  * @sort_data: sorting function data, or %NULL
348  *
349  * Default implementation for getting the (sorted and clamped) list
350  * of recently used resources from a #GtkRecentChooser. This function
351  * should be used by implementations of the #GtkRecentChooser
352  * interface inside the GtkRecentChooser::get_items vfunc.
353  *
354  * Return value: a list of #GtkRecentInfo objects
355  */
356 GList *
357 _gtk_recent_chooser_get_items (GtkRecentChooser  *chooser,
358                                GtkRecentSortFunc  sort_func,
359                                gpointer           sort_data)
360 {
361   GtkRecentManager *manager;
362   gint limit;
363   GtkRecentSortType sort_type;
364   GList *items;
365   GCompareDataFunc compare_func;
366   gint length;
367
368   g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
369
370   manager = _gtk_recent_chooser_get_recent_manager (chooser);
371   if (!manager)
372     return NULL;
373
374   items = gtk_recent_manager_get_items (manager);
375   if (!items)
376     return NULL;
377
378   limit = gtk_recent_chooser_get_limit (chooser);
379   if (limit == 0)
380     return NULL;
381
382   sort_type = gtk_recent_chooser_get_sort_type (chooser);
383   switch (sort_type)
384     {
385     case GTK_RECENT_SORT_NONE:
386       compare_func = NULL;
387       break;
388     case GTK_RECENT_SORT_MRU:
389       compare_func = (GCompareDataFunc) sort_recent_items_mru;
390       break;
391     case GTK_RECENT_SORT_LRU:
392       compare_func = (GCompareDataFunc) sort_recent_items_lru;
393       break;
394     case GTK_RECENT_SORT_CUSTOM:
395       compare_func = (GCompareDataFunc) sort_recent_items_proxy;
396       break;
397     default:
398       g_assert_not_reached ();
399       break;
400     }
401
402   if (compare_func)
403     {
404       SortRecentData *sort_recent;
405
406       sort_recent = g_slice_new (SortRecentData);
407       sort_recent->func = sort_func;
408       sort_recent->data = sort_data;
409
410       items = g_list_sort_with_data (items, compare_func, sort_recent);
411
412       g_slice_free (SortRecentData, sort_recent);
413     }
414   
415   length = g_list_length (items);
416   if ((limit != -1) && (length > limit))
417     {
418       GList *clamp, *l;
419       
420       clamp = g_list_nth (items, limit - 1);
421       if (!clamp)
422         return items;
423       
424       l = clamp->next;
425       clamp->next = NULL;
426     
427       g_list_foreach (l, (GFunc) gtk_recent_info_unref, NULL);
428       g_list_free (l);
429     }
430
431   return items;
432 }