1 /* GTK - The GIMP Toolkit
2 * gtkrecentfilter.h - Filter object for recently used resources
3 * Copyright (C) 2006, Emmanuele Bassi
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.
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.
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.
22 * SECTION:gtkrecentfilter
23 * @Short_Description: A filter for selecting a subset of recently used files
24 * @Title: GtkRecentFilter
26 * A #GtkRecentFilter can be used to restrict the files being shown
27 * in a #GtkRecentChooser. Files can be filtered based on their name
28 * (with gtk_recent_filter_add_pattern()), on their mime type (with
29 * gtk_file_filter_add_mime_type()), on the application that has
30 * registered them (with gtk_recent_filter_add_application()), or by
31 * a custom filter function (with gtk_recent_filter_add_custom()).
33 * Filtering by mime type handles aliasing and subclassing of mime
34 * types; e.g. a filter for text/plain also matches a file with mime
35 * type application/rtf, since application/rtf is a subclass of text/plain.
36 * Note that #GtkRecentFilter allows wildcards for the subtype of a
37 * mime type, so you can e.g. filter for image/<!-- -->*.
39 * Normally, filters are used by adding them to a #GtkRecentChooser,
40 * see gtk_recent_chooser_add_filter(), but it is also possible to
41 * manually use a filter on a file with gtk_recent_filter_filter().
43 * Recently used files are supported since GTK+ 2.10.
45 * <refsect2 id="GtkRecentFilter-BUILDER-UI">
46 * <title>GtkRecentFilter as GtkBuildable</title>
48 * The GtkRecentFilter implementation of the GtkBuildable interface
49 * supports adding rules using the <mime-types>, <patterns> and
50 * <applications> elements and listing the rules within. Specifying
51 * a <mime-type>, <pattern> or <application> is the same
52 * as calling gtk_recent_filter_add_mime_type(), gtk_recent_filter_add_pattern()
53 * or gtk_recent_filter_add_application().
56 * <title>A UI definition fragment specifying GtkRecentFilter rules</title>
57 * <programlisting><![CDATA[
58 * <object class="GtkRecentFilter">
60 * <mime-type>text/plain</mime-type>
61 * <mime-type>image/png</mime-type>
64 * <pattern>*.txt</pattern>
65 * <pattern>*.png</pattern>
68 * <application>gimp</application>
69 * <application>gedit</application>
70 * <application>glade</application>
73 * ]]></programlisting>
82 #include <gdk-pixbuf/gdk-pixbuf.h>
84 #include "gtkrecentfilter.h"
85 #include "gtkbuildable.h"
87 #include "gtkprivate.h"
89 static void gtk_recent_filter_buildable_init (GtkBuildableIface *iface);
90 static gboolean gtk_recent_filter_buildable_custom_tag_start (GtkBuildable *buildable,
94 GMarkupParser *parser,
96 static void gtk_recent_filter_buildable_custom_tag_end (GtkBuildable *buildable,
102 typedef struct _GtkRecentFilterClass GtkRecentFilterClass;
103 typedef struct _FilterRule FilterRule;
107 FILTER_RULE_DISPLAY_NAME,
108 FILTER_RULE_MIME_TYPE,
109 FILTER_RULE_PIXBUF_FORMATS,
110 FILTER_RULE_APPLICATION,
116 struct _GtkRecentFilter
118 GInitiallyUnowned parent_instance;
123 GtkRecentFilterFlags needed;
126 struct _GtkRecentFilterClass
128 GInitiallyUnownedClass parent_class;
134 GtkRecentFilterFlags needed;
140 GSList *pixbuf_formats;
145 GtkRecentFilterFunc func;
147 GDestroyNotify data_destroy;
152 G_DEFINE_TYPE_WITH_CODE (GtkRecentFilter, gtk_recent_filter, G_TYPE_INITIALLY_UNOWNED,
153 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
154 gtk_recent_filter_buildable_init))
158 filter_rule_free (FilterRule *rule)
162 case FILTER_RULE_MIME_TYPE:
163 g_free (rule->u.mime_type);
165 case FILTER_RULE_URI:
166 g_free (rule->u.uri);
168 case FILTER_RULE_DISPLAY_NAME:
169 g_free (rule->u.pattern);
171 case FILTER_RULE_PIXBUF_FORMATS:
172 g_slist_free (rule->u.pixbuf_formats);
174 case FILTER_RULE_AGE:
176 case FILTER_RULE_APPLICATION:
177 g_free (rule->u.application);
179 case FILTER_RULE_GROUP:
180 g_free (rule->u.group);
182 case FILTER_RULE_CUSTOM:
183 if (rule->u.custom.data_destroy)
184 rule->u.custom.data_destroy (rule->u.custom.data);
187 g_assert_not_reached ();
195 gtk_recent_filter_finalize (GObject *object)
197 GtkRecentFilter *filter = GTK_RECENT_FILTER (object);
199 g_free (filter->name);
203 g_slist_foreach (filter->rules,
204 (GFunc) filter_rule_free,
206 g_slist_free (filter->rules);
209 G_OBJECT_CLASS (gtk_recent_filter_parent_class)->finalize (object);
213 gtk_recent_filter_class_init (GtkRecentFilterClass *klass)
215 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
217 gobject_class->finalize = gtk_recent_filter_finalize;
221 gtk_recent_filter_init (GtkRecentFilter *filter)
228 * GtkBuildable implementation
231 gtk_recent_filter_buildable_init (GtkBuildableIface *iface)
233 iface->custom_tag_start = gtk_recent_filter_buildable_custom_tag_start;
234 iface->custom_tag_end = gtk_recent_filter_buildable_custom_tag_end;
245 GtkRecentFilter *filter;
252 parser_start_element (GMarkupParseContext *context,
253 const gchar *element_name,
255 const gchar **values,
259 SubParserData *parser_data = (SubParserData*)user_data;
261 if (strcmp (element_name, "mime-types") == 0)
263 else if (strcmp (element_name, "mime-type") == 0)
265 parser_data->parsing = TRUE;
268 else if (strcmp (element_name, "patterns") == 0)
270 else if (strcmp (element_name, "pattern") == 0)
272 parser_data->parsing = TRUE;
275 else if (strcmp (element_name, "applications") == 0)
277 else if (strcmp (element_name, "application") == 0)
279 parser_data->parsing = TRUE;
283 g_warning ("Unsupported tag for GtkRecentFilter: %s\n", element_name);
287 parser_text_element (GMarkupParseContext *context,
293 SubParserData *parser_data = (SubParserData*)user_data;
295 if (parser_data->parsing)
296 g_string_append_len (parser_data->string, text, text_len);
300 parser_end_element (GMarkupParseContext *context,
301 const gchar *element_name,
305 SubParserData *parser_data = (SubParserData*)user_data;
307 if (parser_data->string)
309 switch (parser_data->type)
311 case PARSE_MIME_TYPES:
312 gtk_recent_filter_add_mime_type (parser_data->filter, parser_data->string->str);
315 gtk_recent_filter_add_pattern (parser_data->filter, parser_data->string->str);
317 case PARSE_APPLICATIONS:
318 gtk_recent_filter_add_application (parser_data->filter, parser_data->string->str);
325 g_string_set_size (parser_data->string, 0);
326 parser_data->parsing = FALSE;
329 static const GMarkupParser sub_parser =
331 parser_start_element,
337 gtk_recent_filter_buildable_custom_tag_start (GtkBuildable *buildable,
340 const gchar *tagname,
341 GMarkupParser *parser,
344 SubParserData *parser_data = NULL;
346 if (strcmp (tagname, "mime-types") == 0)
348 parser_data = g_slice_new0 (SubParserData);
349 parser_data->string = g_string_new ("");
350 parser_data->type = PARSE_MIME_TYPES;
351 parser_data->filter = GTK_RECENT_FILTER (buildable);
353 *parser = sub_parser;
356 else if (strcmp (tagname, "patterns") == 0)
358 parser_data = g_slice_new0 (SubParserData);
359 parser_data->string = g_string_new ("");
360 parser_data->type = PARSE_PATTERNS;
361 parser_data->filter = GTK_RECENT_FILTER (buildable);
363 *parser = sub_parser;
366 else if (strcmp (tagname, "applications") == 0)
368 parser_data = g_slice_new0 (SubParserData);
369 parser_data->string = g_string_new ("");
370 parser_data->type = PARSE_APPLICATIONS;
371 parser_data->filter = GTK_RECENT_FILTER (buildable);
373 *parser = sub_parser;
377 return parser_data != NULL;
381 gtk_recent_filter_buildable_custom_tag_end (GtkBuildable *buildable,
384 const gchar *tagname,
387 if (strcmp (tagname, "mime-types") == 0 ||
388 strcmp (tagname, "patterns") == 0 ||
389 strcmp (tagname, "applications") == 0)
391 SubParserData *parser_data = (SubParserData*)data;
393 g_string_free (parser_data->string, TRUE);
394 g_slice_free (SubParserData, parser_data);
403 * gtk_recent_filter_new:
405 * Creates a new #GtkRecentFilter with no rules added to it.
406 * Such filter does not accept any recently used resources, so is not
407 * particularly useful until you add rules with
408 * gtk_recent_filter_add_pattern(), gtk_recent_filter_add_mime_type(),
409 * gtk_recent_filter_add_application(), gtk_recent_filter_add_age().
410 * To create a filter that accepts any recently used resource, use:
412 * GtkRecentFilter *filter = gtk_recent_filter_new ();
413 * gtk_recent_filter_add_pattern (filter, "*");
416 * Return value: a new #GtkRecentFilter
421 gtk_recent_filter_new (void)
423 return g_object_new (GTK_TYPE_RECENT_FILTER, NULL);
427 * gtk_recent_filter_set_name:
428 * @filter: a #GtkRecentFilter
429 * @name: then human readable name of @filter
431 * Sets the human-readable name of the filter; this is the string
432 * that will be displayed in the recently used resources selector
433 * user interface if there is a selectable list of filters.
438 gtk_recent_filter_set_name (GtkRecentFilter *filter,
441 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
443 g_free (filter->name);
446 filter->name = g_strdup (name);
450 * gtk_recent_filter_get_name:
451 * @filter: a #GtkRecentFilter
453 * Gets the human-readable name for the filter.
454 * See gtk_recent_filter_set_name().
456 * Return value: the name of the filter, or %NULL. The returned string
457 * is owned by the filter object and should not be freed.
461 G_CONST_RETURN gchar *
462 gtk_recent_filter_get_name (GtkRecentFilter *filter)
464 g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), NULL);
470 * gtk_recent_filter_get_needed:
471 * @filter: a #GtkRecentFilter
473 * Gets the fields that need to be filled in for the structure
474 * passed to gtk_recent_filter_filter()
476 * This function will not typically be used by applications; it
477 * is intended principally for use in the implementation of
480 * Return value: bitfield of flags indicating needed fields when
481 * calling gtk_recent_filter_filter()
486 gtk_recent_filter_get_needed (GtkRecentFilter *filter)
488 return filter->needed;
492 recent_filter_add_rule (GtkRecentFilter *filter,
495 filter->needed |= rule->needed;
496 filter->rules = g_slist_append (filter->rules, rule);
500 * gtk_recent_filter_add_mime_type:
501 * @filter: a #GtkRecentFilter
502 * @mime_type: a MIME type
504 * Adds a rule that allows resources based on their registered MIME type.
509 gtk_recent_filter_add_mime_type (GtkRecentFilter *filter,
510 const gchar *mime_type)
514 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
515 g_return_if_fail (mime_type != NULL);
517 rule = g_new0 (FilterRule, 1);
518 rule->type = FILTER_RULE_MIME_TYPE;
519 rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
520 rule->u.mime_type = g_strdup (mime_type);
522 recent_filter_add_rule (filter, rule);
526 * gtk_recent_filter_add_pattern:
527 * @filter: a #GtkRecentFilter
528 * @pattern: a file pattern
530 * Adds a rule that allows resources based on a pattern matching their
536 gtk_recent_filter_add_pattern (GtkRecentFilter *filter,
537 const gchar *pattern)
541 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
542 g_return_if_fail (pattern != NULL);
544 rule = g_new0 (FilterRule, 1);
545 rule->type = FILTER_RULE_DISPLAY_NAME;
546 rule->needed = GTK_RECENT_FILTER_DISPLAY_NAME;
547 rule->u.pattern = g_strdup (pattern);
549 recent_filter_add_rule (filter, rule);
553 * gtk_recent_filter_add_pixbuf_formats:
554 * @filter: a #GtkRecentFilter
556 * Adds a rule allowing image files in the formats supported
562 gtk_recent_filter_add_pixbuf_formats (GtkRecentFilter *filter)
566 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
568 rule = g_new0 (FilterRule, 1);
569 rule->type = FILTER_RULE_PIXBUF_FORMATS;
570 rule->needed = GTK_RECENT_FILTER_MIME_TYPE;
571 rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
573 recent_filter_add_rule (filter, rule);
577 * gtk_recent_filter_add_application:
578 * @filter: a #GtkRecentFilter
579 * @application: an application name
581 * Adds a rule that allows resources based on the name of the application
582 * that has registered them.
587 gtk_recent_filter_add_application (GtkRecentFilter *filter,
588 const gchar *application)
592 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
593 g_return_if_fail (application != NULL);
595 rule = g_new0 (FilterRule, 1);
596 rule->type = FILTER_RULE_APPLICATION;
597 rule->needed = GTK_RECENT_FILTER_APPLICATION;
598 rule->u.application = g_strdup (application);
600 recent_filter_add_rule (filter, rule);
604 * gtk_recent_filter_add_group:
605 * @filter: a #GtkRecentFilter
606 * @group: a group name
608 * Adds a rule that allows resources based on the name of the group
609 * to which they belong
614 gtk_recent_filter_add_group (GtkRecentFilter *filter,
619 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
620 g_return_if_fail (group != NULL);
622 rule = g_new0 (FilterRule, 1);
623 rule->type = FILTER_RULE_GROUP;
624 rule->needed = GTK_RECENT_FILTER_GROUP;
625 rule->u.group = g_strdup (group);
627 recent_filter_add_rule (filter, rule);
631 * gtk_recent_filter_add_age:
632 * @filter: a #GtkRecentFilter
633 * @days: number of days
635 * Adds a rule that allows resources based on their age - that is, the number
636 * of days elapsed since they were last modified.
641 gtk_recent_filter_add_age (GtkRecentFilter *filter,
646 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
648 rule = g_new0 (FilterRule, 1);
649 rule->type = FILTER_RULE_AGE;
650 rule->needed = GTK_RECENT_FILTER_AGE;
653 recent_filter_add_rule (filter, rule);
657 * gtk_recent_filter_add_custom:
658 * @filter: a #GtkRecentFilter
659 * @needed: bitfield of flags indicating the information that the custom
660 * filter function needs.
661 * @func: callback function; if the function returns %TRUE, then
662 * the file will be displayed.
663 * @data: data to pass to @func
664 * @data_destroy: function to call to free @data when it is no longer needed.
666 * Adds a rule to a filter that allows resources based on a custom callback
667 * function. The bitfield @needed which is passed in provides information
668 * about what sorts of information that the filter function needs;
669 * this allows GTK+ to avoid retrieving expensive information when
670 * it isn't needed by the filter.
675 gtk_recent_filter_add_custom (GtkRecentFilter *filter,
676 GtkRecentFilterFlags needed,
677 GtkRecentFilterFunc func,
679 GDestroyNotify data_destroy)
683 g_return_if_fail (GTK_IS_RECENT_FILTER (filter));
684 g_return_if_fail (func != NULL);
686 rule = g_new0 (FilterRule, 1);
687 rule->type = FILTER_RULE_CUSTOM;
688 rule->needed = needed;
689 rule->u.custom.func = func;
690 rule->u.custom.data = data;
691 rule->u.custom.data_destroy = data_destroy;
693 recent_filter_add_rule (filter, rule);
698 * gtk_recent_filter_filter:
699 * @filter: a #GtkRecentFilter
700 * @filter_info: a #GtkRecentFilterInfo structure containing information
701 * about a recently used resource
703 * Tests whether a file should be displayed according to @filter.
704 * The #GtkRecentFilterInfo structure @filter_info should include
705 * the fields returned from gtk_recent_filter_get_needed().
707 * This function will not typically be used by applications; it
708 * is intended principally for use in the implementation of
711 * Return value: %TRUE if the file should be displayed
716 gtk_recent_filter_filter (GtkRecentFilter *filter,
717 const GtkRecentFilterInfo *filter_info)
721 g_return_val_if_fail (GTK_IS_RECENT_FILTER (filter), FALSE);
722 g_return_val_if_fail (filter_info != NULL, FALSE);
724 for (l = filter->rules; l != NULL; l = l->next)
726 FilterRule *rule = (FilterRule *) l->data;
728 if ((filter_info->contains & rule->needed) != rule->needed)
733 case FILTER_RULE_MIME_TYPE:
734 if (filter_info->mime_type != NULL &&
735 g_content_type_is_a (filter_info->mime_type, rule->u.mime_type))
738 case FILTER_RULE_APPLICATION:
739 if (filter_info->applications)
743 for (i = 0; filter_info->applications[i] != NULL; i++)
745 if (strcmp (filter_info->applications[i], rule->u.application) == 0)
750 case FILTER_RULE_GROUP:
751 if (filter_info->groups)
755 for (i = 0; filter_info->groups[i] != NULL; i++)
757 if (strcmp (filter_info->groups[i], rule->u.group) == 0)
762 case FILTER_RULE_PIXBUF_FORMATS:
765 if (!filter_info->mime_type)
768 for (list = rule->u.pixbuf_formats; list; list = list->next)
773 mime_types = gdk_pixbuf_format_get_mime_types (list->data);
775 for (i = 0; mime_types[i] != NULL; i++)
777 if (strcmp (mime_types[i], filter_info->mime_type) == 0)
779 g_strfreev (mime_types);
784 g_strfreev (mime_types);
788 case FILTER_RULE_URI:
789 if ((filter_info->uri != NULL) &&
790 _gtk_fnmatch (rule->u.uri, filter_info->uri, FALSE))
793 case FILTER_RULE_DISPLAY_NAME:
794 if ((filter_info->display_name != NULL) &&
795 _gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE))
798 case FILTER_RULE_AGE:
799 if ((filter_info->age != -1) &&
800 (filter_info->age < rule->u.age))
803 case FILTER_RULE_CUSTOM:
804 if (rule->u.custom.func (filter_info, rule->u.custom.data))