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, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /**
20  * SECTION:gtkrecentfilter
21  * @Short_Description: A filter for selecting a subset of recently used files
22  * @Title: GtkRecentFilter
23  *
24  * A #GtkRecentFilter can be used to restrict the files being shown
25  * in a #GtkRecentChooser.  Files can be filtered based on their name
26  * (with gtk_recent_filter_add_pattern()), on their mime type (with
27  * gtk_file_filter_add_mime_type()), on the application that has
28  * registered them (with gtk_recent_filter_add_application()), or by
29  * a custom filter function (with gtk_recent_filter_add_custom()).
30  *
31  * Filtering by mime type handles aliasing and subclassing of mime
32  * types; e.g. a filter for text/plain also matches a file with mime
33  * type application/rtf, since application/rtf is a subclass of text/plain.
34  * Note that #GtkRecentFilter allows wildcards for the subtype of a
35  * mime type, so you can e.g. filter for image/\*.
36  *
37  * Normally, filters are used by adding them to a #GtkRecentChooser,
38  * see gtk_recent_chooser_add_filter(), but it is also possible to
39  * manually use a filter on a file with gtk_recent_filter_filter().
40  *
41  * Recently used files are supported since GTK+ 2.10.
42  *
43  * ## GtkRecentFilter as GtkBuildable
44  *
45  * The GtkRecentFilter implementation of the GtkBuildable interface
46  * supports adding rules using the `<mime-types>`, `<patterns>` and
47  * `<applications>` elements and listing the rules within. Specifying
48  * a `<mime-type>`, `<pattern>` or `<application>` has the same effect as
49  * calling gtk_recent_filter_add_mime_type(),
50  * gtk_recent_filter_add_pattern() or gtk_recent_filter_add_application().
51  *
52  * An example of a UI definition fragment specifying `GtkRecentFilter`
53  * rules:
54  *
55  * |[<!-- language="xml" -->
56  * <object class="GtkRecentFilter">
57  *   <mime-types>
58  *     <mime-type>text/plain</mime-type>
59  *     <mime-type>image/png</mime-type>
60  *   </mime-types>
61  *   <patterns>
62  *     <pattern>*.txt</pattern>
63  *     <pattern>*.png</pattern>
64  *   </patterns>
65  *   <applications>
66  *     <application>gimp</application>
67  *     <application>gedit</application>
68  *     <application>glade</application>
69  *   </applications>
70  * </object>
71  * ]|
72  */
73 
74 #include "config.h"
75 #include <string.h>
76 
77 #include <gdk-pixbuf/gdk-pixbuf.h>
78 
79 #include "gtkrecentfilter.h"
80 #include "gtkbuildable.h"
81 #include "gtkbuilderprivate.h"
82 #include "gtkintl.h"
83 #include "gtkprivate.h"
84 
85 static void     gtk_recent_filter_buildable_init                 (GtkBuildableIface *iface);
86 static gboolean gtk_recent_filter_buildable_custom_tag_start     (GtkBuildable  *buildable,
87 								  GtkBuilder    *builder,
88 								  GObject       *child,
89 								  const gchar   *tagname,
90 								  GMarkupParser *parser,
91 								  gpointer      *data);
92 static void     gtk_recent_filter_buildable_custom_tag_end       (GtkBuildable  *buildable,
93 								  GtkBuilder    *builder,
94 								  GObject       *child,
95 								  const gchar   *tagname,
96 								  gpointer      *data);
97 
98 typedef struct _GtkRecentFilterClass GtkRecentFilterClass;
99 typedef struct _FilterRule FilterRule;
100 
101 typedef enum {
102   FILTER_RULE_URI,
103   FILTER_RULE_DISPLAY_NAME,
104   FILTER_RULE_MIME_TYPE,
105   FILTER_RULE_PIXBUF_FORMATS,
106   FILTER_RULE_APPLICATION,
107   FILTER_RULE_AGE,
108   FILTER_RULE_GROUP,
109   FILTER_RULE_CUSTOM
110 } FilterRuleType;
111 
112 struct _GtkRecentFilter
113 {
114   GInitiallyUnowned parent_instance;
115 
116   gchar *name;
117   GSList *rules;
118 
119   GtkRecentFilterFlags needed;
120 };
121 
122 struct _GtkRecentFilterClass
123 {
124   GInitiallyUnownedClass parent_class;
125 };
126 
127 struct _FilterRule
128 {
129   FilterRuleType type;
130   GtkRecentFilterFlags needed;
131 
132   union {
133     gchar *uri;
134     gchar *pattern;
135     gchar *mime_type;
136     GSList *pixbuf_formats;
137     gchar *application;
138     gchar *group;
139     gint age;
140     struct {
141       GtkRecentFilterFunc func;
142       gpointer data;
143       GDestroyNotify data_destroy;
144     } custom;
145   } u;
146 };
147 
G_DEFINE_TYPE_WITH_CODE(GtkRecentFilter,gtk_recent_filter,G_TYPE_INITIALLY_UNOWNED,G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,gtk_recent_filter_buildable_init))148 G_DEFINE_TYPE_WITH_CODE (GtkRecentFilter, gtk_recent_filter, G_TYPE_INITIALLY_UNOWNED,
149                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
150                                                 gtk_recent_filter_buildable_init))
151 
152 
153 static void
154 filter_rule_free (FilterRule *rule)
155 {
156   switch (rule->type)
157     {
158     case FILTER_RULE_MIME_TYPE:
159       g_free (rule->u.mime_type);
160       break;
161     case FILTER_RULE_URI:
162       g_free (rule->u.uri);
163       break;
164     case FILTER_RULE_DISPLAY_NAME:
165       g_free (rule->u.pattern);
166       break;
167     case FILTER_RULE_PIXBUF_FORMATS:
168       g_slist_free (rule->u.pixbuf_formats);
169       break;
170     case FILTER_RULE_AGE:
171       break;
172     case FILTER_RULE_APPLICATION:
173       g_free (rule->u.application);
174       break;
175     case FILTER_RULE_GROUP:
176       g_free (rule->u.group);
177       break;
178     case FILTER_RULE_CUSTOM:
179       if (rule->u.custom.data_destroy)
180         rule->u.custom.data_destroy (rule->u.custom.data);
181       break;
182     default:
183       g_assert_not_reached ();
184       break;
185     }
186 
187   g_free (rule);
188 }
189 
190 static void
gtk_recent_filter_finalize(GObject * object)191 gtk_recent_filter_finalize (GObject *object)
192 {
193   GtkRecentFilter *filter = GTK_RECENT_FILTER (object);
194 
195   g_free (filter->name);
196   g_slist_free_full (filter->rules, (GDestroyNotify) filter_rule_free);
197 
198   G_OBJECT_CLASS (gtk_recent_filter_parent_class)->finalize (object);
199 }
200 
201 static void
gtk_recent_filter_class_init(GtkRecentFilterClass * klass)202 gtk_recent_filter_class_init (GtkRecentFilterClass *klass)
203 {
204   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
205 
206   gobject_class->finalize = gtk_recent_filter_finalize;
207 }
208 
209 static void
gtk_recent_filter_init(GtkRecentFilter * filter)210 gtk_recent_filter_init (GtkRecentFilter *filter)
211 {
212 
213 }
214 
215 
216 /*
217  * GtkBuildable implementation
218  */
219 static void
gtk_recent_filter_buildable_init(GtkBuildableIface * iface)220 gtk_recent_filter_buildable_init (GtkBuildableIface *iface)
221 {
222   iface->custom_tag_start = gtk_recent_filter_buildable_custom_tag_start;
223   iface->custom_tag_end = gtk_recent_filter_buildable_custom_tag_end;
224 }
225 
226 
227 typedef enum {
228   PARSE_MIME_TYPES,
229   PARSE_PATTERNS,
230   PARSE_APPLICATIONS
231 } ParserType;
232 
233 typedef struct {
234   GtkRecentFilter *filter;
235   GtkBuilder      *builder;
236   ParserType       type;
237   GString         *string;
238   gboolean         parsing;
239 } SubParserData;
240 
241 static void
parser_start_element(GMarkupParseContext * context,const gchar * element_name,const gchar ** names,const gchar ** values,gpointer user_data,GError ** error)242 parser_start_element (GMarkupParseContext *context,
243 		      const gchar         *element_name,
244 		      const gchar        **names,
245 		      const gchar        **values,
246 		      gpointer             user_data,
247 		      GError             **error)
248 {
249   SubParserData *data = (SubParserData*)user_data;
250 
251   if (!g_markup_collect_attributes (element_name, names, values, error,
252                                     G_MARKUP_COLLECT_INVALID, NULL, NULL,
253                                     G_MARKUP_COLLECT_INVALID))
254     {
255       _gtk_builder_prefix_error (data->builder, context, error);
256       return;
257     }
258 
259   if (strcmp (element_name, "mime-types") == 0 ||
260       strcmp (element_name, "patterns") == 0 ||
261       strcmp (element_name, "applications") == 0)
262     {
263       if (!_gtk_builder_check_parent (data->builder, context, "object", error))
264         return;
265     }
266   else if (strcmp (element_name, "mime-type") == 0)
267     {
268       if (!_gtk_builder_check_parent (data->builder, context, "mime-types", error))
269         return;
270 
271       data->parsing = TRUE;
272     }
273   else if (strcmp (element_name, "pattern") == 0)
274     {
275       if (!_gtk_builder_check_parent (data->builder, context, "patterns", error))
276         return;
277 
278       data->parsing = TRUE;
279     }
280   else if (strcmp (element_name, "application") == 0)
281     {
282       if (!_gtk_builder_check_parent (data->builder, context, "applications", error))
283         return;
284 
285       data->parsing = TRUE;
286     }
287   else
288     {
289       _gtk_builder_error_unhandled_tag (data->builder, context,
290                                         "GtkRecentFilter", element_name,
291                                         error);
292     }
293 }
294 
295 static void
parser_text_element(GMarkupParseContext * context,const gchar * text,gsize text_len,gpointer user_data,GError ** error)296 parser_text_element (GMarkupParseContext *context,
297 		     const gchar         *text,
298 		     gsize                text_len,
299 		     gpointer             user_data,
300 		     GError             **error)
301 {
302   SubParserData *data = (SubParserData*)user_data;
303 
304   if (data->parsing)
305     g_string_append_len (data->string, text, text_len);
306 }
307 
308 static void
parser_end_element(GMarkupParseContext * context,const gchar * element_name,gpointer user_data,GError ** error)309 parser_end_element (GMarkupParseContext *context,
310 		    const gchar         *element_name,
311 		    gpointer             user_data,
312 		    GError             **error)
313 {
314   SubParserData *data = (SubParserData*)user_data;
315 
316   if (data->string)
317     {
318       switch (data->type)
319 	{
320 	case PARSE_MIME_TYPES:
321 	  gtk_recent_filter_add_mime_type (data->filter, data->string->str);
322 	  break;
323 	case PARSE_PATTERNS:
324 	  gtk_recent_filter_add_pattern (data->filter, data->string->str);
325 	  break;
326 	case PARSE_APPLICATIONS:
327 	  gtk_recent_filter_add_application (data->filter, data->string->str);
328 	  break;
329 	default:
330 	  break;
331 	}
332     }
333 
334   g_string_set_size (data->string, 0);
335   data->parsing = FALSE;
336 }
337 
338 static const GMarkupParser sub_parser =
339   {
340     parser_start_element,
341     parser_end_element,
342     parser_text_element,
343   };
344 
345 static gboolean
gtk_recent_filter_buildable_custom_tag_start(GtkBuildable * buildable,GtkBuilder * builder,GObject * child,const gchar * tagname,GMarkupParser * parser,gpointer * parser_data)346 gtk_recent_filter_buildable_custom_tag_start (GtkBuildable  *buildable,
347                                               GtkBuilder    *builder,
348                                               GObject       *child,
349                                               const gchar   *tagname,
350                                               GMarkupParser *parser,
351                                               gpointer      *parser_data)
352 {
353   SubParserData *data = NULL;
354 
355   if (strcmp (tagname, "mime-types") == 0)
356     {
357       data = g_slice_new0 (SubParserData);
358       data->string = g_string_new ("");
359       data->type = PARSE_MIME_TYPES;
360       data->filter = GTK_RECENT_FILTER (buildable);
361       data->builder = builder;
362 
363       *parser = sub_parser;
364       *parser_data = data;
365     }
366   else if (strcmp (tagname, "patterns") == 0)
367     {
368       data = g_slice_new0 (SubParserData);
369       data->string = g_string_new ("");
370       data->type = PARSE_PATTERNS;
371       data->filter = GTK_RECENT_FILTER (buildable);
372       data->builder = builder;
373 
374       *parser = sub_parser;
375       *parser_data = data;
376     }
377   else if (strcmp (tagname, "applications") == 0)
378     {
379       data = g_slice_new0 (SubParserData);
380       data->string = g_string_new ("");
381       data->type = PARSE_APPLICATIONS;
382       data->filter = GTK_RECENT_FILTER (buildable);
383       data->builder = builder;
384 
385       *parser = sub_parser;
386       *parser_data = data;
387     }
388 
389   return data != NULL;
390 }
391 
392 static void
gtk_recent_filter_buildable_custom_tag_end(GtkBuildable * buildable,GtkBuilder * builder,GObject * child,const gchar * tagname,gpointer * parser_data)393 gtk_recent_filter_buildable_custom_tag_end (GtkBuildable *buildable,
394 					    GtkBuilder   *builder,
395 					    GObject      *child,
396 					    const gchar  *tagname,
397 					    gpointer     *parser_data)
398 {
399   if (strcmp (tagname, "mime-types") == 0 ||
400       strcmp (tagname, "patterns") == 0 ||
401       strcmp (tagname, "applications") == 0)
402     {
403       SubParserData *data = (SubParserData*)parser_data;
404 
405       g_string_free (data->string, TRUE);
406       g_slice_free (SubParserData, data);
407     }
408 }
409 
410 /*
411  * Public API
412  */
413 
414 /**
415  * gtk_recent_filter_new:
416  *
417  * Creates a new #GtkRecentFilter with no rules added to it.
418  * Such filter does not accept any recently used resources, so is not
419  * particularly useful until you add rules with
420  * gtk_recent_filter_add_pattern(), gtk_recent_filter_add_mime_type(),
421  * gtk_recent_filter_add_application(), gtk_recent_filter_add_age().
422  * To create a filter that accepts any recently used resource, use:
423  * |[<!-- language="C" -->
424  * GtkRecentFilter *filter = gtk_recent_filter_new ();
425  * gtk_recent_filter_add_pattern (filter, "*");
426  * ]|
427  *
428  * Returns: a new #GtkRecentFilter
429  *
430  * Since: 2.10
431  */
432 GtkRecentFilter *
gtk_recent_filter_new(void)433 gtk_recent_filter_new (void)
434 {
435   return g_object_new (GTK_TYPE_RECENT_FILTER, NULL);
436 }
437 
438 /**
439  * gtk_recent_filter_set_name:
440  * @filter: a #GtkRecentFilter
441  * @name: then human readable name of @filter
442  *
443  * Sets the human-readable name of the filter; this is the string
444  * that will be displayed in the recently used resources selector
445  * user interface if there is a selectable list of filters.
446  *
447  * Since: 2.10
448  */
449 void
gtk_recent_filter_set_name(GtkRecentFilter * filter,const gchar * name)450 gtk_recent_filter_set_name (GtkRecentFilter *filter,
451 			    const gchar     *name)
452 {
453   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
454 
455   g_free (filter->name);
456 
457   if (name)
458     filter->name = g_strdup (name);
459 }
460 
461 /**
462  * gtk_recent_filter_get_name:
463  * @filter: a #GtkRecentFilter
464  *
465  * Gets the human-readable name for the filter.
466  * See gtk_recent_filter_set_name().
467  *
468  * Returns: (nullable): the name of the filter, or %NULL.  The returned string
469  *   is owned by the filter object and should not be freed.
470  *
471  * Since: 2.10
472  */
473 const gchar *
gtk_recent_filter_get_name(GtkRecentFilter * filter)474 gtk_recent_filter_get_name (GtkRecentFilter *filter)
475 {
476   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), NULL);
477 
478   return filter->name;
479 }
480 
481 /**
482  * gtk_recent_filter_get_needed:
483  * @filter: a #GtkRecentFilter
484  *
485  * Gets the fields that need to be filled in for the #GtkRecentFilterInfo
486  * passed to gtk_recent_filter_filter()
487  *
488  * This function will not typically be used by applications; it
489  * is intended principally for use in the implementation of
490  * #GtkRecentChooser.
491  *
492  * Returns: bitfield of flags indicating needed fields when
493  *   calling gtk_recent_filter_filter()
494  *
495  * Since: 2.10
496  */
497 GtkRecentFilterFlags
gtk_recent_filter_get_needed(GtkRecentFilter * filter)498 gtk_recent_filter_get_needed (GtkRecentFilter *filter)
499 {
500   return filter->needed;
501 }
502 
503 static void
recent_filter_add_rule(GtkRecentFilter * filter,FilterRule * rule)504 recent_filter_add_rule (GtkRecentFilter *filter,
505 			FilterRule      *rule)
506 {
507   filter->needed |= rule->needed;
508   filter->rules = g_slist_append (filter->rules, rule);
509 }
510 
511 /**
512  * gtk_recent_filter_add_mime_type:
513  * @filter: a #GtkRecentFilter
514  * @mime_type: a MIME type
515  *
516  * Adds a rule that allows resources based on their registered MIME type.
517  *
518  * Since: 2.10
519  */
520 void
gtk_recent_filter_add_mime_type(GtkRecentFilter * filter,const gchar * mime_type)521 gtk_recent_filter_add_mime_type (GtkRecentFilter *filter,
522 				 const gchar     *mime_type)
523 {
524   FilterRule *rule;
525 
526   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
527   g_return_if_fail (mime_type != NULL);
528 
529   rule = g_new0 (FilterRule, 1);
530   rule->type = FILTER_RULE_MIME_TYPE;
531   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
532   rule->u.mime_type = g_strdup (mime_type);
533 
534   recent_filter_add_rule (filter, rule);
535 }
536 
537 /**
538  * gtk_recent_filter_add_pattern:
539  * @filter: a #GtkRecentFilter
540  * @pattern: a file pattern
541  *
542  * Adds a rule that allows resources based on a pattern matching their
543  * display name.
544  *
545  * Since: 2.10
546  */
547 void
gtk_recent_filter_add_pattern(GtkRecentFilter * filter,const gchar * pattern)548 gtk_recent_filter_add_pattern (GtkRecentFilter *filter,
549 			       const gchar     *pattern)
550 {
551   FilterRule *rule;
552 
553   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
554   g_return_if_fail (pattern != NULL);
555 
556   rule = g_new0 (FilterRule, 1);
557   rule->type = FILTER_RULE_DISPLAY_NAME;
558   rule->needed = GTK_RECENT_FILTER_DISPLAY_NAME;
559   rule->u.pattern = g_strdup (pattern);
560 
561   recent_filter_add_rule (filter, rule);
562 }
563 
564 /**
565  * gtk_recent_filter_add_pixbuf_formats:
566  * @filter: a #GtkRecentFilter
567  *
568  * Adds a rule allowing image files in the formats supported
569  * by GdkPixbuf.
570  *
571  * Since: 2.10
572  */
573 void
gtk_recent_filter_add_pixbuf_formats(GtkRecentFilter * filter)574 gtk_recent_filter_add_pixbuf_formats (GtkRecentFilter *filter)
575 {
576   FilterRule *rule;
577 
578   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
579 
580   rule = g_new0 (FilterRule, 1);
581   rule->type = FILTER_RULE_PIXBUF_FORMATS;
582   rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
583   rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
584 
585   recent_filter_add_rule (filter, rule);
586 }
587 
588 /**
589  * gtk_recent_filter_add_application:
590  * @filter: a #GtkRecentFilter
591  * @application: an application name
592  *
593  * Adds a rule that allows resources based on the name of the application
594  * that has registered them.
595  *
596  * Since: 2.10
597  */
598 void
gtk_recent_filter_add_application(GtkRecentFilter * filter,const gchar * application)599 gtk_recent_filter_add_application (GtkRecentFilter *filter,
600 				   const gchar     *application)
601 {
602   FilterRule *rule;
603 
604   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
605   g_return_if_fail (application != NULL);
606 
607   rule = g_new0 (FilterRule, 1);
608   rule->type = FILTER_RULE_APPLICATION;
609   rule->needed = GTK_RECENT_FILTER_APPLICATION;
610   rule->u.application = g_strdup (application);
611 
612   recent_filter_add_rule (filter, rule);
613 }
614 
615 /**
616  * gtk_recent_filter_add_group:
617  * @filter: a #GtkRecentFilter
618  * @group: a group name
619  *
620  * Adds a rule that allows resources based on the name of the group
621  * to which they belong
622  *
623  * Since: 2.10
624  */
625 void
gtk_recent_filter_add_group(GtkRecentFilter * filter,const gchar * group)626 gtk_recent_filter_add_group (GtkRecentFilter *filter,
627 			     const gchar     *group)
628 {
629   FilterRule *rule;
630 
631   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
632   g_return_if_fail (group != NULL);
633 
634   rule = g_new0 (FilterRule, 1);
635   rule->type = FILTER_RULE_GROUP;
636   rule->needed = GTK_RECENT_FILTER_GROUP;
637   rule->u.group = g_strdup (group);
638 
639   recent_filter_add_rule (filter, rule);
640 }
641 
642 /**
643  * gtk_recent_filter_add_age:
644  * @filter: a #GtkRecentFilter
645  * @days: number of days
646  *
647  * Adds a rule that allows resources based on their age - that is, the number
648  * of days elapsed since they were last modified.
649  *
650  * Since: 2.10
651  */
652 void
gtk_recent_filter_add_age(GtkRecentFilter * filter,gint days)653 gtk_recent_filter_add_age (GtkRecentFilter *filter,
654 			   gint             days)
655 {
656   FilterRule *rule;
657 
658   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
659 
660   rule = g_new0 (FilterRule, 1);
661   rule->type = FILTER_RULE_AGE;
662   rule->needed = GTK_RECENT_FILTER_AGE;
663   rule->u.age = days;
664 
665   recent_filter_add_rule (filter, rule);
666 }
667 
668 /**
669  * gtk_recent_filter_add_custom:
670  * @filter: a #GtkRecentFilter
671  * @needed: bitfield of flags indicating the information that the custom
672  *          filter function needs.
673  * @func: callback function; if the function returns %TRUE, then
674  *   the file will be displayed.
675  * @data: data to pass to @func
676  * @data_destroy: function to call to free @data when it is no longer needed.
677  *
678  * Adds a rule to a filter that allows resources based on a custom callback
679  * function. The bitfield @needed which is passed in provides information
680  * about what sorts of information that the filter function needs;
681  * this allows GTK+ to avoid retrieving expensive information when
682  * it isn’t needed by the filter.
683  *
684  * Since: 2.10
685  **/
686 void
gtk_recent_filter_add_custom(GtkRecentFilter * filter,GtkRecentFilterFlags needed,GtkRecentFilterFunc func,gpointer data,GDestroyNotify data_destroy)687 gtk_recent_filter_add_custom (GtkRecentFilter      *filter,
688 			      GtkRecentFilterFlags  needed,
689 			      GtkRecentFilterFunc   func,
690 			      gpointer              data,
691 			      GDestroyNotify        data_destroy)
692 {
693   FilterRule *rule;
694 
695   g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
696   g_return_if_fail (func != NULL);
697 
698   rule = g_new0 (FilterRule, 1);
699   rule->type = FILTER_RULE_CUSTOM;
700   rule->needed = needed;
701   rule->u.custom.func = func;
702   rule->u.custom.data = data;
703   rule->u.custom.data_destroy = data_destroy;
704 
705   recent_filter_add_rule (filter, rule);
706 }
707 
708 
709 /**
710  * gtk_recent_filter_filter:
711  * @filter: a #GtkRecentFilter
712  * @filter_info: a #GtkRecentFilterInfo containing information
713  *   about a recently used resource
714  *
715  * Tests whether a file should be displayed according to @filter.
716  * The #GtkRecentFilterInfo @filter_info should include
717  * the fields returned from gtk_recent_filter_get_needed(), and
718  * must set the #GtkRecentFilterInfo.contains field of @filter_info
719  * to indicate which fields have been set.
720  *
721  * This function will not typically be used by applications; it
722  * is intended principally for use in the implementation of
723  * #GtkRecentChooser.
724  *
725  * Returns: %TRUE if the file should be displayed
726  *
727  * Since: 2.10
728  */
729 gboolean
gtk_recent_filter_filter(GtkRecentFilter * filter,const GtkRecentFilterInfo * filter_info)730 gtk_recent_filter_filter (GtkRecentFilter           *filter,
731 			  const GtkRecentFilterInfo *filter_info)
732 {
733   GSList *l;
734 
735   g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), FALSE);
736   g_return_val_if_fail (filter_info != NULL, FALSE);
737 
738   for (l = filter->rules; l != NULL; l = l->next)
739     {
740       FilterRule *rule = (FilterRule *) l->data;
741 
742       if ((filter_info->contains & rule->needed) != rule->needed)
743         continue;
744 
745       switch (rule->type)
746         {
747         case FILTER_RULE_MIME_TYPE:
748           if (filter_info->mime_type != NULL)
749             {
750               gchar *filter_content_type, *rule_content_type;
751               gboolean match = FALSE;
752 
753               filter_content_type = g_content_type_from_mime_type (filter_info->mime_type);
754               rule_content_type = g_content_type_from_mime_type (rule->u.mime_type);
755               if (filter_content_type != NULL && rule_content_type != NULL)
756                 match = g_content_type_is_a (filter_content_type, rule_content_type);
757               g_free (filter_content_type);
758               g_free (rule_content_type);
759 
760               if (match)
761                 return TRUE;
762             }
763           break;
764         case FILTER_RULE_APPLICATION:
765           if (filter_info->applications)
766             {
767               gint i;
768 
769               for (i = 0; filter_info->applications[i] != NULL; i++)
770                 {
771                   if (strcmp (filter_info->applications[i], rule->u.application) == 0)
772                     return TRUE;
773                 }
774             }
775           break;
776 	case FILTER_RULE_GROUP:
777 	  if (filter_info->groups)
778             {
779 	      gint i;
780 
781 	      for (i = 0; filter_info->groups[i] != NULL; i++)
782 		{
783 		  if (strcmp (filter_info->groups[i], rule->u.group) == 0)
784 		    return TRUE;
785 		}
786 	    }
787 	  break;
788 	case FILTER_RULE_PIXBUF_FORMATS:
789 	  {
790             GSList *list;
791 	    if (!filter_info->mime_type)
792 	      break;
793 
794 	    for (list = rule->u.pixbuf_formats; list; list = list->next)
795               {
796                 gint i;
797 		gchar **mime_types;
798 
799 		mime_types = gdk_pixbuf_format_get_mime_types (list->data);
800 
801 		for (i = 0; mime_types[i] != NULL; i++)
802                   {
803                     if (strcmp (mime_types[i], filter_info->mime_type) == 0)
804                       {
805 			g_strfreev (mime_types);
806 			return TRUE;
807 		      }
808 		  }
809 
810 		g_strfreev (mime_types);
811 	      }
812 	    break;
813 	  }
814         case FILTER_RULE_URI:
815           if ((filter_info->uri != NULL) &&
816               _gtk_fnmatch (rule->u.uri, filter_info->uri, FALSE))
817             return TRUE;
818           break;
819         case FILTER_RULE_DISPLAY_NAME:
820           if ((filter_info->display_name != NULL) &&
821               _gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE))
822             return TRUE;
823           break;
824         case FILTER_RULE_AGE:
825           if ((filter_info->age != -1) &&
826               (filter_info->age < rule->u.age))
827             return TRUE;
828           break;
829         case FILTER_RULE_CUSTOM:
830           if (rule->u.custom.func (filter_info, rule->u.custom.data))
831             return TRUE;
832           break;
833         }
834     }
835 
836   return FALSE;
837 }
838