]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentfilter.c
Import GtkRecent* from libegg.
[~andy/gtk] / gtk / gtkrecentfilter.c
1 /* GTK - The GIMP Toolkit
2  * gtkrecentfilter.h - Filter object for recently used resources
3  * Copyright (C) 2006, Emmanuele Bassi
4  * 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include <string.h>
23
24 #include "gtkrecentfilter.h"
25 #include "gtkobject.h"
26 #include "gtkintl.h"
27 #include "gtkprivate.h"
28
29 #include "gtkalias.h"
30
31 #ifdef G_OS_UNIX
32 #define XDG_PREFIX _gtk_xdg
33 #include "xdgmime/xdgmime.h"
34 #endif
35
36 typedef struct _GtkRecentFilterClass GtkRecentFilterClass;
37 typedef struct _FilterRule FilterRule;
38
39 typedef enum {
40   FILTER_RULE_URI,
41   FILTER_RULE_DISPLAY_NAME,
42   FILTER_RULE_MIME_TYPE,
43   FILTER_RULE_PIXBUF_FORMATS,
44   FILTER_RULE_APPLICATION,
45   FILTER_RULE_AGE,
46   FILTER_RULE_GROUP,
47   FILTER_RULE_CUSTOM,
48 } FilterRuleType;
49
50 struct _GtkRecentFilter
51 {
52   GtkObject parent_instance;
53   
54   gchar *name;
55   GSList *rules;
56   
57   GtkRecentFilterFlags needed;
58 };
59
60 struct _GtkRecentFilterClass
61 {
62   GtkObjectClass parent_class;
63 };
64
65 struct _FilterRule
66 {
67   FilterRuleType type;
68   GtkRecentFilterFlags needed;
69   
70   union {
71     gchar *uri;
72     gchar *pattern;
73     gchar *mime_type;
74     GSList *pixbuf_formats;
75     gchar *application;
76     gchar *group;
77     gint age;
78     struct {
79       GtkRecentFilterFunc func;
80       gpointer data;
81       GDestroyNotify data_destroy;
82     } custom;
83   } u;
84 };
85
86 G_DEFINE_TYPE (GtkRecentFilter, gtk_recent_filter, GTK_TYPE_OBJECT);
87
88
89 static void
90 filter_rule_free (FilterRule *rule)
91 {
92   switch (rule->type)
93     {
94     case FILTER_RULE_MIME_TYPE:
95       g_free (rule->u.mime_type);
96       break;
97     case FILTER_RULE_URI:
98       g_free (rule->u.uri);
99       break;
100     case FILTER_RULE_DISPLAY_NAME:
101       g_free (rule->u.pattern);
102       break;
103     case FILTER_RULE_PIXBUF_FORMATS:
104       g_slist_free (rule->u.pixbuf_formats);
105       break;
106     case FILTER_RULE_AGE:
107       break;
108     case FILTER_RULE_APPLICATION:
109       g_free (rule->u.application);
110       break;
111     case FILTER_RULE_GROUP:
112       g_free (rule->u.group);
113       break;
114     case FILTER_RULE_CUSTOM:
115       if (rule->u.custom.data_destroy)
116         rule->u.custom.data_destroy (rule->u.custom.data);
117       break;
118     default:
119       g_assert_not_reached ();
120       break;
121     }
122   
123   g_free (rule);
124 }
125
126 static void
127 gtk_recent_filter_finalize (GObject *object)
128 {
129   GtkRecentFilter *filter = GTK_RECENT_FILTER (object);
130   
131   if (filter->name)
132     g_free (filter->name);
133   
134   if (filter->rules)
135     {
136       g_slist_foreach (filter->rules,
137                        (GFunc) filter_rule_free,
138                        NULL);
139       g_slist_free (filter->rules);
140     }
141   
142   G_OBJECT_CLASS (gtk_recent_filter_parent_class)->finalize (object);
143 }
144
145 static void
146 gtk_recent_filter_class_init (GtkRecentFilterClass *klass)
147 {
148   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149   
150   gobject_class->finalize = gtk_recent_filter_finalize;
151 }
152
153 static void
154 gtk_recent_filter_init (GtkRecentFilter *filter)
155 {
156
157 }
158
159 /*
160  * Public API
161  */
162  
163 /**
164  * gtk_recent_filter_new:
165  *
166  * Creates a new #GtkRecentFilter with no rules added to it.
167  * Such filter does not accept any recently used resources, so is not
168  * particularly useful until you add rules with
169  * gtk_recent_filter_add_pattern(), gtk_recent_filter_add_mime_type(),
170  * gtk_recent_filter_add_application(), gtk_recent_filter_add_age().
171  * To create a filter that accepts any recently used resource, use:
172  *
173  * <informalexample><programlisting>
174  * GtkRecentFilter *filter = gtk_recent_filter_new (<!-- -->);
175  * gtk_recent_filter_add_pattern (filter, "*");
176  * </programlisting></informalexample>
177  *
178  * Return value: a new #GtkRecentFilter
179  *
180  * Since: 2.10
181  */
182 GtkRecentFilter *
183 gtk_recent_filter_new (void)
184 {
185   return g_object_new (GTK_TYPE_RECENT_FILTER, NULL);
186 }
187
188 /**
189  * gtk_recent_filter_set_name:
190  * @filter: a #GtkRecentFilter
191  * @name: then human readable name of @filter
192  *
193  * Sets the human-readable name of the filter; this is the string
194  * that will be displayed in the recently used resources selector
195  * user interface if there is a selectable list of filters.
196  *
197  * Since: 2.10
198  */
199 void
200 gtk_recent_filter_set_name (GtkRecentFilter *filter,
201                             const gchar     *name)
202 {
203   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
204   
205   if (filter->name)
206     g_free (filter->name);
207   
208   if (name)
209     filter->name = g_strdup (name);
210 }
211
212 /**
213  * gtk_recent_filter_get_name:
214  * @filter: a #GtkRecentFilter
215  *
216  * Gets the human-readable name for the filter.
217  * See gtk_recent_filter_set_name().
218  *
219  * Return value: the name of the filter, or %NULL.  The returned string
220  *   is owned by the filter object and should not be freed.
221  *
222  * Since: 2.10
223  */
224 G_CONST_RETURN gchar *
225 gtk_recent_filter_get_name (GtkRecentFilter *filter)
226 {
227   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), NULL);
228   
229   return filter->name;
230 }
231
232 /**
233  * gtk_recent_filter_get_needed:
234  * @filter: a #GtkRecentFilter
235  *
236  * Gets the fields that need to be filled in for the structure
237  * passed to gtk_recent_filter_filter()
238  * 
239  * This function will not typically be used by applications; it
240  * is intended principally for use in the implementation of
241  * #GtkRecentChooser.
242  * 
243  * Return value: bitfield of flags indicating needed fields when
244  *   calling gtk_recent_filter_filter()
245  *
246  * Since: 2.10
247  */
248 GtkRecentFilterFlags
249 gtk_recent_filter_get_needed (GtkRecentFilter *filter)
250 {
251   return filter->needed;
252 }
253
254 static void
255 recent_filter_add_rule (GtkRecentFilter *filter,
256                         FilterRule      *rule)
257 {
258   filter->needed |= rule->needed;
259   filter->rules = g_slist_append (filter->rules, rule);
260 }
261
262 /**
263  * gtk_recent_filter_add_mime_type:
264  * @filter: a #GtkRecentFilter
265  * @mime_type: a MIME type
266  *
267  * Adds a rule that allows resources based on their registered MIME type.
268  *
269  * Since: 2.10
270  */
271 void
272 gtk_recent_filter_add_mime_type (GtkRecentFilter *filter,
273                                  const gchar     *mime_type)
274 {
275   FilterRule *rule;
276   
277   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
278   g_return_if_fail (mime_type != NULL);
279   
280   rule = g_new0 (FilterRule, 1);
281   rule->type = FILTER_RULE_MIME_TYPE;
282   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
283   rule->u.mime_type = g_strdup (mime_type);
284   
285   recent_filter_add_rule (filter, rule);
286 }
287
288 /**
289  * gtk_recent_filter_add_pattern:
290  * @filter: a #GtkRecentFilter
291  * @pattern: a file pattern
292  *
293  * Adds a rule that allows resources based on a pattern matching their
294  * display name.
295  *
296  * Since: 2.10
297  */
298 void
299 gtk_recent_filter_add_pattern (GtkRecentFilter *filter,
300                                const gchar     *pattern)
301 {
302   FilterRule *rule;
303   
304   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
305   g_return_if_fail (pattern != NULL);
306   
307   rule = g_new0 (FilterRule, 1);
308   rule->type = FILTER_RULE_DISPLAY_NAME;
309   rule->needed = GTK_RECENT_FILTER_DISPLAY_NAME;
310   rule->u.pattern = g_strdup (pattern);
311   
312   recent_filter_add_rule (filter, rule);
313 }
314
315 /**
316  * gtk_recent_filter_add_pixbuf_formats:
317  * @filter: a #GtkRecentFilter
318  *
319  * Adds a rule allowing image files in the formats supported
320  * by GdkPixbuf.
321  *
322  * Since: 2.10
323  */
324 void
325 gtk_recent_filter_add_pixbuf_formats (GtkRecentFilter *filter)
326 {
327   FilterRule *rule;
328
329   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
330
331   rule = g_new0 (FilterRule, 1);
332   rule->type = FILTER_RULE_PIXBUF_FORMATS;
333   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
334   rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
335   
336   recent_filter_add_rule (filter, rule);
337 }
338
339 /**
340  * gtk_recent_filter_add_application:
341  * @filter: a #GtkRecentFilter
342  * @application: an application name
343  *
344  * Adds a rule that allows resources based on the name of the application
345  * that has registered them.
346  *
347  * Since: 2.10
348  */
349 void
350 gtk_recent_filter_add_application (GtkRecentFilter *filter,
351                                    const gchar     *application)
352 {
353   FilterRule *rule;
354   
355   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
356   g_return_if_fail (application != NULL);
357   
358   rule = g_new0 (FilterRule, 1);
359   rule->type = FILTER_RULE_APPLICATION;
360   rule->needed = GTK_RECENT_FILTER_APPLICATION;
361   rule->u.application = g_strdup (application);
362   
363   recent_filter_add_rule (filter, rule);
364 }
365
366 /**
367  * gtk_recent_filter_add_group:
368  * @filter: a #GtkRecentFilter
369  * @group: a group name
370  *
371  * Adds a rule that allows resources based on the name of the group
372  * to which they belong
373  *
374  * Since: 2.10
375  */
376 void
377 gtk_recent_filter_add_group (GtkRecentFilter *filter,
378                              const gchar     *group)
379 {
380   FilterRule *rule;
381   
382   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
383   g_return_if_fail (group != NULL);
384   
385   rule = g_new0 (FilterRule, 1);
386   rule->type = FILTER_RULE_GROUP;
387   rule->needed = GTK_RECENT_FILTER_GROUP;
388   rule->u.group = g_strdup (group);
389   
390   recent_filter_add_rule (filter, rule);
391 }
392
393 /**
394  * gtk_recent_filter_add_mime_type:
395  * @filter: a #GtkRecentFilter
396  * @days: number of days
397  *
398  * Adds a rule that allows resources based on their age - that is, the number
399  * of days elapsed since they were last modified.
400  *
401  * Since: 2.10
402  */
403 void
404 gtk_recent_filter_add_age (GtkRecentFilter *filter,
405                            gint             days)
406 {
407   FilterRule *rule;
408   
409   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
410   
411   rule = g_new0 (FilterRule, 1);
412   rule->type = FILTER_RULE_AGE;
413   rule->needed = GTK_RECENT_FILTER_AGE;
414   rule->u.age = days;
415   
416   recent_filter_add_rule (filter, rule);
417 }
418
419 /**
420  * gtk_recent_filter_add_custom:
421  * @filter: a #GtkRecentFilter
422  * @needed: bitfield of flags indicating the information that the custom
423  *          filter function needs.
424  * @func: callback function; if the function returns %TRUE, then
425  *   the file will be displayed.
426  * @data: data to pass to @func
427  * @data_destroy: function to call to free @data when it is no longer needed.
428  * 
429  * Adds a rule to a filter that allows resources based on a custom callback
430  * function. The bitfield @needed which is passed in provides information
431  * about what sorts of information that the filter function needs;
432  * this allows GTK+ to avoid retrieving expensive information when
433  * it isn't needed by the filter.
434  * 
435  * Since: 2.10
436  **/
437 void
438 gtk_recent_filter_add_custom (GtkRecentFilter      *filter,
439                               GtkRecentFilterFlags  needed,
440                               GtkRecentFilterFunc   func,
441                               gpointer              data,
442                               GDestroyNotify        data_destroy)
443 {
444   FilterRule *rule;
445   
446   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
447   g_return_if_fail (func != NULL);
448
449   rule = g_new0 (FilterRule, 1);
450   rule->type = FILTER_RULE_CUSTOM;
451   rule->needed = needed;
452   rule->u.custom.func = func;
453   rule->u.custom.data = data;
454   rule->u.custom.data_destroy = data_destroy;
455
456   recent_filter_add_rule (filter, rule);
457 }
458
459
460 /**
461  * gtk_recent_filter_filter:
462  * @filter: a #GtkRecentFilter
463  * @filter_info: a #GtkRecentFilterInfo structure containing information
464  *   about a recently used resource
465  *
466  * Tests whether a file should be displayed according to @filter.
467  * The #GtkRecentFilterInfo structure @filter_info should include
468  * the fields returned from gtk_recent_filter_get_needed().
469  *
470  * This function will not typically be used by applications; it
471  * is intended principally for use in the implementation of
472  * #GtkRecentChooser.
473  * 
474  * Return value: %TRUE if the file should be displayed
475  */
476 gboolean
477 gtk_recent_filter_filter (GtkRecentFilter           *filter,
478                           const GtkRecentFilterInfo *filter_info)
479 {
480   GSList *l;
481   
482   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), FALSE);
483   g_return_val_if_fail (filter_info != NULL, FALSE);
484   
485   for (l = filter->rules; l != NULL; l = l->next)
486     {
487       FilterRule *rule = (FilterRule *) l->data;
488
489       if ((filter_info->contains & rule->needed) != rule->needed)
490         continue;
491
492       switch (rule->type)
493         {
494         case FILTER_RULE_MIME_TYPE:
495           if ((filter_info->mime_type != NULL)
496 #ifdef G_OS_UNIX
497               && (xdg_mime_mime_type_subclass (filter_info->mime_type, rule->u.mime_type)))
498 #else
499               && (strcmp (filter_info->mime_type, rule->u.mime_type) == 0))
500 #endif
501             return TRUE;
502           break;
503         case FILTER_RULE_APPLICATION:
504           if (filter_info->applications)
505             {
506               gint i;
507               
508               for (i = 0; filter_info->applications[i] != NULL; i++)
509                 {
510                   if (strcmp (filter_info->applications[i], rule->u.application) == 0)
511                     return TRUE;
512                 }
513             }
514           break;
515         case FILTER_RULE_GROUP:
516           if (filter_info->groups)
517             {
518               gint i;
519
520               for (i = 0; filter_info->groups[i] != NULL; i++)
521                 {
522                   if (strcmp (filter_info->groups[i], rule->u.group) == 0)
523                     return TRUE;
524                 }
525             }
526           break;
527         case FILTER_RULE_PIXBUF_FORMATS:
528           {
529             GSList *list;
530             if (!filter_info->mime_type)
531               break;
532
533             for (list = rule->u.pixbuf_formats; list; list = list->next)
534               {
535                 gint i;
536                 gchar **mime_types;
537
538                 mime_types = gdk_pixbuf_format_get_mime_types (list->data);
539
540                 for (i = 0; mime_types[i] != NULL; i++)
541                   {
542                     if (strcmp (mime_types[i], filter_info->mime_type) == 0)
543                       {
544                         g_strfreev (mime_types);
545                         return TRUE;
546                       }
547                   }
548
549                 g_strfreev (mime_types);
550               }
551             break;
552           }
553         case FILTER_RULE_URI:
554           if ((filter_info->uri != NULL) &&
555               _gtk_fnmatch (rule->u.uri, filter_info->uri, FALSE))
556             return TRUE;
557           break;
558         case FILTER_RULE_DISPLAY_NAME:
559           if ((filter_info->display_name != NULL) &&
560               _gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE))
561             return TRUE;
562           break;
563         case FILTER_RULE_AGE:
564           if ((filter_info->age != -1) &&
565               (filter_info->age < rule->u.age))
566             return TRUE;
567           break;
568         case FILTER_RULE_CUSTOM:
569           if (rule->u.custom.func (filter_info, rule->u.custom.data))
570             return TRUE;
571           break;
572         }
573     }
574   
575   return FALSE;
576 }
577
578 #define __GTK_RECENT_FILTER_C__
579 #include "gtkaliasdef.c"