]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentfilter.c
a37104254235870f85cd680441f465919c314454
[~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   g_free (filter->name);
132   
133   if (filter->rules)
134     {
135       g_slist_foreach (filter->rules,
136                        (GFunc) filter_rule_free,
137                        NULL);
138       g_slist_free (filter->rules);
139     }
140   
141   G_OBJECT_CLASS (gtk_recent_filter_parent_class)->finalize (object);
142 }
143
144 static void
145 gtk_recent_filter_class_init (GtkRecentFilterClass *klass)
146 {
147   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
148   
149   gobject_class->finalize = gtk_recent_filter_finalize;
150 }
151
152 static void
153 gtk_recent_filter_init (GtkRecentFilter *filter)
154 {
155
156 }
157
158 /*
159  * Public API
160  */
161  
162 /**
163  * gtk_recent_filter_new:
164  *
165  * Creates a new #GtkRecentFilter with no rules added to it.
166  * Such filter does not accept any recently used resources, so is not
167  * particularly useful until you add rules with
168  * gtk_recent_filter_add_pattern(), gtk_recent_filter_add_mime_type(),
169  * gtk_recent_filter_add_application(), gtk_recent_filter_add_age().
170  * To create a filter that accepts any recently used resource, use:
171  *
172  * <informalexample><programlisting>
173  * GtkRecentFilter *filter = gtk_recent_filter_new (<!-- -->);
174  * gtk_recent_filter_add_pattern (filter, "*");
175  * </programlisting></informalexample>
176  *
177  * Return value: a new #GtkRecentFilter
178  *
179  * Since: 2.10
180  */
181 GtkRecentFilter *
182 gtk_recent_filter_new (void)
183 {
184   return g_object_new (GTK_TYPE_RECENT_FILTER, NULL);
185 }
186
187 /**
188  * gtk_recent_filter_set_name:
189  * @filter: a #GtkRecentFilter
190  * @name: then human readable name of @filter
191  *
192  * Sets the human-readable name of the filter; this is the string
193  * that will be displayed in the recently used resources selector
194  * user interface if there is a selectable list of filters.
195  *
196  * Since: 2.10
197  */
198 void
199 gtk_recent_filter_set_name (GtkRecentFilter *filter,
200                             const gchar     *name)
201 {
202   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
203   
204   g_free (filter->name);
205   
206   if (name)
207     filter->name = g_strdup (name);
208 }
209
210 /**
211  * gtk_recent_filter_get_name:
212  * @filter: a #GtkRecentFilter
213  *
214  * Gets the human-readable name for the filter.
215  * See gtk_recent_filter_set_name().
216  *
217  * Return value: the name of the filter, or %NULL.  The returned string
218  *   is owned by the filter object and should not be freed.
219  *
220  * Since: 2.10
221  */
222 G_CONST_RETURN gchar *
223 gtk_recent_filter_get_name (GtkRecentFilter *filter)
224 {
225   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), NULL);
226   
227   return filter->name;
228 }
229
230 /**
231  * gtk_recent_filter_get_needed:
232  * @filter: a #GtkRecentFilter
233  *
234  * Gets the fields that need to be filled in for the structure
235  * passed to gtk_recent_filter_filter()
236  * 
237  * This function will not typically be used by applications; it
238  * is intended principally for use in the implementation of
239  * #GtkRecentChooser.
240  * 
241  * Return value: bitfield of flags indicating needed fields when
242  *   calling gtk_recent_filter_filter()
243  *
244  * Since: 2.10
245  */
246 GtkRecentFilterFlags
247 gtk_recent_filter_get_needed (GtkRecentFilter *filter)
248 {
249   return filter->needed;
250 }
251
252 static void
253 recent_filter_add_rule (GtkRecentFilter *filter,
254                         FilterRule      *rule)
255 {
256   filter->needed |= rule->needed;
257   filter->rules = g_slist_append (filter->rules, rule);
258 }
259
260 /**
261  * gtk_recent_filter_add_mime_type:
262  * @filter: a #GtkRecentFilter
263  * @mime_type: a MIME type
264  *
265  * Adds a rule that allows resources based on their registered MIME type.
266  *
267  * Since: 2.10
268  */
269 void
270 gtk_recent_filter_add_mime_type (GtkRecentFilter *filter,
271                                  const gchar     *mime_type)
272 {
273   FilterRule *rule;
274   
275   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
276   g_return_if_fail (mime_type != NULL);
277   
278   rule = g_new0 (FilterRule, 1);
279   rule->type = FILTER_RULE_MIME_TYPE;
280   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
281   rule->u.mime_type = g_strdup (mime_type);
282   
283   recent_filter_add_rule (filter, rule);
284 }
285
286 /**
287  * gtk_recent_filter_add_pattern:
288  * @filter: a #GtkRecentFilter
289  * @pattern: a file pattern
290  *
291  * Adds a rule that allows resources based on a pattern matching their
292  * display name.
293  *
294  * Since: 2.10
295  */
296 void
297 gtk_recent_filter_add_pattern (GtkRecentFilter *filter,
298                                const gchar     *pattern)
299 {
300   FilterRule *rule;
301   
302   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
303   g_return_if_fail (pattern != NULL);
304   
305   rule = g_new0 (FilterRule, 1);
306   rule->type = FILTER_RULE_DISPLAY_NAME;
307   rule->needed = GTK_RECENT_FILTER_DISPLAY_NAME;
308   rule->u.pattern = g_strdup (pattern);
309   
310   recent_filter_add_rule (filter, rule);
311 }
312
313 /**
314  * gtk_recent_filter_add_pixbuf_formats:
315  * @filter: a #GtkRecentFilter
316  *
317  * Adds a rule allowing image files in the formats supported
318  * by GdkPixbuf.
319  *
320  * Since: 2.10
321  */
322 void
323 gtk_recent_filter_add_pixbuf_formats (GtkRecentFilter *filter)
324 {
325   FilterRule *rule;
326
327   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
328
329   rule = g_new0 (FilterRule, 1);
330   rule->type = FILTER_RULE_PIXBUF_FORMATS;
331   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
332   rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
333   
334   recent_filter_add_rule (filter, rule);
335 }
336
337 /**
338  * gtk_recent_filter_add_application:
339  * @filter: a #GtkRecentFilter
340  * @application: an application name
341  *
342  * Adds a rule that allows resources based on the name of the application
343  * that has registered them.
344  *
345  * Since: 2.10
346  */
347 void
348 gtk_recent_filter_add_application (GtkRecentFilter *filter,
349                                    const gchar     *application)
350 {
351   FilterRule *rule;
352   
353   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
354   g_return_if_fail (application != NULL);
355   
356   rule = g_new0 (FilterRule, 1);
357   rule->type = FILTER_RULE_APPLICATION;
358   rule->needed = GTK_RECENT_FILTER_APPLICATION;
359   rule->u.application = g_strdup (application);
360   
361   recent_filter_add_rule (filter, rule);
362 }
363
364 /**
365  * gtk_recent_filter_add_group:
366  * @filter: a #GtkRecentFilter
367  * @group: a group name
368  *
369  * Adds a rule that allows resources based on the name of the group
370  * to which they belong
371  *
372  * Since: 2.10
373  */
374 void
375 gtk_recent_filter_add_group (GtkRecentFilter *filter,
376                              const gchar     *group)
377 {
378   FilterRule *rule;
379   
380   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
381   g_return_if_fail (group != NULL);
382   
383   rule = g_new0 (FilterRule, 1);
384   rule->type = FILTER_RULE_GROUP;
385   rule->needed = GTK_RECENT_FILTER_GROUP;
386   rule->u.group = g_strdup (group);
387   
388   recent_filter_add_rule (filter, rule);
389 }
390
391 /**
392  * gtk_recent_filter_add_age:
393  * @filter: a #GtkRecentFilter
394  * @days: number of days
395  *
396  * Adds a rule that allows resources based on their age - that is, the number
397  * of days elapsed since they were last modified.
398  *
399  * Since: 2.10
400  */
401 void
402 gtk_recent_filter_add_age (GtkRecentFilter *filter,
403                            gint             days)
404 {
405   FilterRule *rule;
406   
407   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
408   
409   rule = g_new0 (FilterRule, 1);
410   rule->type = FILTER_RULE_AGE;
411   rule->needed = GTK_RECENT_FILTER_AGE;
412   rule->u.age = days;
413   
414   recent_filter_add_rule (filter, rule);
415 }
416
417 /**
418  * gtk_recent_filter_add_custom:
419  * @filter: a #GtkRecentFilter
420  * @needed: bitfield of flags indicating the information that the custom
421  *          filter function needs.
422  * @func: callback function; if the function returns %TRUE, then
423  *   the file will be displayed.
424  * @data: data to pass to @func
425  * @data_destroy: function to call to free @data when it is no longer needed.
426  * 
427  * Adds a rule to a filter that allows resources based on a custom callback
428  * function. The bitfield @needed which is passed in provides information
429  * about what sorts of information that the filter function needs;
430  * this allows GTK+ to avoid retrieving expensive information when
431  * it isn't needed by the filter.
432  * 
433  * Since: 2.10
434  **/
435 void
436 gtk_recent_filter_add_custom (GtkRecentFilter      *filter,
437                               GtkRecentFilterFlags  needed,
438                               GtkRecentFilterFunc   func,
439                               gpointer              data,
440                               GDestroyNotify        data_destroy)
441 {
442   FilterRule *rule;
443   
444   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
445   g_return_if_fail (func != NULL);
446
447   rule = g_new0 (FilterRule, 1);
448   rule->type = FILTER_RULE_CUSTOM;
449   rule->needed = needed;
450   rule->u.custom.func = func;
451   rule->u.custom.data = data;
452   rule->u.custom.data_destroy = data_destroy;
453
454   recent_filter_add_rule (filter, rule);
455 }
456
457
458 /**
459  * gtk_recent_filter_filter:
460  * @filter: a #GtkRecentFilter
461  * @filter_info: a #GtkRecentFilterInfo structure containing information
462  *   about a recently used resource
463  *
464  * Tests whether a file should be displayed according to @filter.
465  * The #GtkRecentFilterInfo structure @filter_info should include
466  * the fields returned from gtk_recent_filter_get_needed().
467  *
468  * This function will not typically be used by applications; it
469  * is intended principally for use in the implementation of
470  * #GtkRecentChooser.
471  * 
472  * Return value: %TRUE if the file should be displayed
473  *
474  * Since: 2.10
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"