]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentfilter.c
Require gtk-doc 1.8
[~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  * GtkRecentFilter *filter = gtk_recent_filter_new ();
173  * gtk_recent_filter_add_pattern (filter, "*");
174  * ]|
175  *
176  * Return value: a new #GtkRecentFilter
177  *
178  * Since: 2.10
179  */
180 GtkRecentFilter *
181 gtk_recent_filter_new (void)
182 {
183   return g_object_new (GTK_TYPE_RECENT_FILTER, NULL);
184 }
185
186 /**
187  * gtk_recent_filter_set_name:
188  * @filter: a #GtkRecentFilter
189  * @name: then human readable name of @filter
190  *
191  * Sets the human-readable name of the filter; this is the string
192  * that will be displayed in the recently used resources selector
193  * user interface if there is a selectable list of filters.
194  *
195  * Since: 2.10
196  */
197 void
198 gtk_recent_filter_set_name (GtkRecentFilter *filter,
199                             const gchar     *name)
200 {
201   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
202   
203   g_free (filter->name);
204   
205   if (name)
206     filter->name = g_strdup (name);
207 }
208
209 /**
210  * gtk_recent_filter_get_name:
211  * @filter: a #GtkRecentFilter
212  *
213  * Gets the human-readable name for the filter.
214  * See gtk_recent_filter_set_name().
215  *
216  * Return value: the name of the filter, or %NULL.  The returned string
217  *   is owned by the filter object and should not be freed.
218  *
219  * Since: 2.10
220  */
221 G_CONST_RETURN gchar *
222 gtk_recent_filter_get_name (GtkRecentFilter *filter)
223 {
224   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), NULL);
225   
226   return filter->name;
227 }
228
229 /**
230  * gtk_recent_filter_get_needed:
231  * @filter: a #GtkRecentFilter
232  *
233  * Gets the fields that need to be filled in for the structure
234  * passed to gtk_recent_filter_filter()
235  * 
236  * This function will not typically be used by applications; it
237  * is intended principally for use in the implementation of
238  * #GtkRecentChooser.
239  * 
240  * Return value: bitfield of flags indicating needed fields when
241  *   calling gtk_recent_filter_filter()
242  *
243  * Since: 2.10
244  */
245 GtkRecentFilterFlags
246 gtk_recent_filter_get_needed (GtkRecentFilter *filter)
247 {
248   return filter->needed;
249 }
250
251 static void
252 recent_filter_add_rule (GtkRecentFilter *filter,
253                         FilterRule      *rule)
254 {
255   filter->needed |= rule->needed;
256   filter->rules = g_slist_append (filter->rules, rule);
257 }
258
259 /**
260  * gtk_recent_filter_add_mime_type:
261  * @filter: a #GtkRecentFilter
262  * @mime_type: a MIME type
263  *
264  * Adds a rule that allows resources based on their registered MIME type.
265  *
266  * Since: 2.10
267  */
268 void
269 gtk_recent_filter_add_mime_type (GtkRecentFilter *filter,
270                                  const gchar     *mime_type)
271 {
272   FilterRule *rule;
273   
274   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
275   g_return_if_fail (mime_type != NULL);
276   
277   rule = g_new0 (FilterRule, 1);
278   rule->type = FILTER_RULE_MIME_TYPE;
279   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
280   rule->u.mime_type = g_strdup (mime_type);
281   
282   recent_filter_add_rule (filter, rule);
283 }
284
285 /**
286  * gtk_recent_filter_add_pattern:
287  * @filter: a #GtkRecentFilter
288  * @pattern: a file pattern
289  *
290  * Adds a rule that allows resources based on a pattern matching their
291  * display name.
292  *
293  * Since: 2.10
294  */
295 void
296 gtk_recent_filter_add_pattern (GtkRecentFilter *filter,
297                                const gchar     *pattern)
298 {
299   FilterRule *rule;
300   
301   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
302   g_return_if_fail (pattern != NULL);
303   
304   rule = g_new0 (FilterRule, 1);
305   rule->type = FILTER_RULE_DISPLAY_NAME;
306   rule->needed = GTK_RECENT_FILTER_DISPLAY_NAME;
307   rule->u.pattern = g_strdup (pattern);
308   
309   recent_filter_add_rule (filter, rule);
310 }
311
312 /**
313  * gtk_recent_filter_add_pixbuf_formats:
314  * @filter: a #GtkRecentFilter
315  *
316  * Adds a rule allowing image files in the formats supported
317  * by GdkPixbuf.
318  *
319  * Since: 2.10
320  */
321 void
322 gtk_recent_filter_add_pixbuf_formats (GtkRecentFilter *filter)
323 {
324   FilterRule *rule;
325
326   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
327
328   rule = g_new0 (FilterRule, 1);
329   rule->type = FILTER_RULE_PIXBUF_FORMATS;
330   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
331   rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
332   
333   recent_filter_add_rule (filter, rule);
334 }
335
336 /**
337  * gtk_recent_filter_add_application:
338  * @filter: a #GtkRecentFilter
339  * @application: an application name
340  *
341  * Adds a rule that allows resources based on the name of the application
342  * that has registered them.
343  *
344  * Since: 2.10
345  */
346 void
347 gtk_recent_filter_add_application (GtkRecentFilter *filter,
348                                    const gchar     *application)
349 {
350   FilterRule *rule;
351   
352   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
353   g_return_if_fail (application != NULL);
354   
355   rule = g_new0 (FilterRule, 1);
356   rule->type = FILTER_RULE_APPLICATION;
357   rule->needed = GTK_RECENT_FILTER_APPLICATION;
358   rule->u.application = g_strdup (application);
359   
360   recent_filter_add_rule (filter, rule);
361 }
362
363 /**
364  * gtk_recent_filter_add_group:
365  * @filter: a #GtkRecentFilter
366  * @group: a group name
367  *
368  * Adds a rule that allows resources based on the name of the group
369  * to which they belong
370  *
371  * Since: 2.10
372  */
373 void
374 gtk_recent_filter_add_group (GtkRecentFilter *filter,
375                              const gchar     *group)
376 {
377   FilterRule *rule;
378   
379   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
380   g_return_if_fail (group != NULL);
381   
382   rule = g_new0 (FilterRule, 1);
383   rule->type = FILTER_RULE_GROUP;
384   rule->needed = GTK_RECENT_FILTER_GROUP;
385   rule->u.group = g_strdup (group);
386   
387   recent_filter_add_rule (filter, rule);
388 }
389
390 /**
391  * gtk_recent_filter_add_age:
392  * @filter: a #GtkRecentFilter
393  * @days: number of days
394  *
395  * Adds a rule that allows resources based on their age - that is, the number
396  * of days elapsed since they were last modified.
397  *
398  * Since: 2.10
399  */
400 void
401 gtk_recent_filter_add_age (GtkRecentFilter *filter,
402                            gint             days)
403 {
404   FilterRule *rule;
405   
406   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
407   
408   rule = g_new0 (FilterRule, 1);
409   rule->type = FILTER_RULE_AGE;
410   rule->needed = GTK_RECENT_FILTER_AGE;
411   rule->u.age = days;
412   
413   recent_filter_add_rule (filter, rule);
414 }
415
416 /**
417  * gtk_recent_filter_add_custom:
418  * @filter: a #GtkRecentFilter
419  * @needed: bitfield of flags indicating the information that the custom
420  *          filter function needs.
421  * @func: callback function; if the function returns %TRUE, then
422  *   the file will be displayed.
423  * @data: data to pass to @func
424  * @data_destroy: function to call to free @data when it is no longer needed.
425  * 
426  * Adds a rule to a filter that allows resources based on a custom callback
427  * function. The bitfield @needed which is passed in provides information
428  * about what sorts of information that the filter function needs;
429  * this allows GTK+ to avoid retrieving expensive information when
430  * it isn't needed by the filter.
431  * 
432  * Since: 2.10
433  **/
434 void
435 gtk_recent_filter_add_custom (GtkRecentFilter      *filter,
436                               GtkRecentFilterFlags  needed,
437                               GtkRecentFilterFunc   func,
438                               gpointer              data,
439                               GDestroyNotify        data_destroy)
440 {
441   FilterRule *rule;
442   
443   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
444   g_return_if_fail (func != NULL);
445
446   rule = g_new0 (FilterRule, 1);
447   rule->type = FILTER_RULE_CUSTOM;
448   rule->needed = needed;
449   rule->u.custom.func = func;
450   rule->u.custom.data = data;
451   rule->u.custom.data_destroy = data_destroy;
452
453   recent_filter_add_rule (filter, rule);
454 }
455
456
457 /**
458  * gtk_recent_filter_filter:
459  * @filter: a #GtkRecentFilter
460  * @filter_info: a #GtkRecentFilterInfo structure containing information
461  *   about a recently used resource
462  *
463  * Tests whether a file should be displayed according to @filter.
464  * The #GtkRecentFilterInfo structure @filter_info should include
465  * the fields returned from gtk_recent_filter_get_needed().
466  *
467  * This function will not typically be used by applications; it
468  * is intended principally for use in the implementation of
469  * #GtkRecentChooser.
470  * 
471  * Return value: %TRUE if the file should be displayed
472  *
473  * Since: 2.10
474  */
475 gboolean
476 gtk_recent_filter_filter (GtkRecentFilter           *filter,
477                           const GtkRecentFilterInfo *filter_info)
478 {
479   GSList *l;
480   
481   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), FALSE);
482   g_return_val_if_fail (filter_info != NULL, FALSE);
483   
484   for (l = filter->rules; l != NULL; l = l->next)
485     {
486       FilterRule *rule = (FilterRule *) l->data;
487
488       if ((filter_info->contains & rule->needed) != rule->needed)
489         continue;
490
491       switch (rule->type)
492         {
493         case FILTER_RULE_MIME_TYPE:
494           if ((filter_info->mime_type != NULL)
495 #ifdef G_OS_UNIX
496               && (xdg_mime_mime_type_subclass (filter_info->mime_type, rule->u.mime_type)))
497 #else
498               && (strcmp (filter_info->mime_type, rule->u.mime_type) == 0))
499 #endif
500             return TRUE;
501           break;
502         case FILTER_RULE_APPLICATION:
503           if (filter_info->applications)
504             {
505               gint i;
506               
507               for (i = 0; filter_info->applications[i] != NULL; i++)
508                 {
509                   if (strcmp (filter_info->applications[i], rule->u.application) == 0)
510                     return TRUE;
511                 }
512             }
513           break;
514         case FILTER_RULE_GROUP:
515           if (filter_info->groups)
516             {
517               gint i;
518
519               for (i = 0; filter_info->groups[i] != NULL; i++)
520                 {
521                   if (strcmp (filter_info->groups[i], rule->u.group) == 0)
522                     return TRUE;
523                 }
524             }
525           break;
526         case FILTER_RULE_PIXBUF_FORMATS:
527           {
528             GSList *list;
529             if (!filter_info->mime_type)
530               break;
531
532             for (list = rule->u.pixbuf_formats; list; list = list->next)
533               {
534                 gint i;
535                 gchar **mime_types;
536
537                 mime_types = gdk_pixbuf_format_get_mime_types (list->data);
538
539                 for (i = 0; mime_types[i] != NULL; i++)
540                   {
541                     if (strcmp (mime_types[i], filter_info->mime_type) == 0)
542                       {
543                         g_strfreev (mime_types);
544                         return TRUE;
545                       }
546                   }
547
548                 g_strfreev (mime_types);
549               }
550             break;
551           }
552         case FILTER_RULE_URI:
553           if ((filter_info->uri != NULL) &&
554               _gtk_fnmatch (rule->u.uri, filter_info->uri, FALSE))
555             return TRUE;
556           break;
557         case FILTER_RULE_DISPLAY_NAME:
558           if ((filter_info->display_name != NULL) &&
559               _gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE))
560             return TRUE;
561           break;
562         case FILTER_RULE_AGE:
563           if ((filter_info->age != -1) &&
564               (filter_info->age < rule->u.age))
565             return TRUE;
566           break;
567         case FILTER_RULE_CUSTOM:
568           if (rule->u.custom.func (filter_info, rule->u.custom.data))
569             return TRUE;
570           break;
571         }
572     }
573   
574   return FALSE;
575 }
576
577 #define __GTK_RECENT_FILTER_C__
578 #include "gtkaliasdef.c"