]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilefilter.c
Use xdg_mime_mime_type_subclass() to match mime types. This also gives use
[~andy/gtk] / gtk / gtkfilefilter.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilefilter.c: Filters for selecting a file subset
3  * Copyright (C) 2003, Red Hat, Inc.
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 "gtkalias.h"
25 #include "gtkfilefilter.h"
26 #include "gtkobject.h"
27 #include "gtkprivate.h"
28
29 #define XDG_PREFIX _gtk_xdg
30 #include "xdgmime/xdgmime.h"
31
32 typedef struct _GtkFileFilterClass GtkFileFilterClass;
33 typedef struct _FilterRule FilterRule;
34
35 #define GTK_FILE_FILTER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_FILTER, GtkFileFilterClass))
36 #define GTK_IS_FILE_FILTER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_FILTER))
37 #define GTK_FILE_FILTER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_FILTER, GtkFileFilterClass))
38
39 typedef enum {
40   FILTER_RULE_PATTERN,
41   FILTER_RULE_MIME_TYPE,
42   FILTER_RULE_PIXBUF_FORMATS,
43   FILTER_RULE_CUSTOM
44 } FilterRuleType;
45
46 struct _GtkFileFilterClass
47 {
48   GtkObjectClass parent_class;
49 };
50
51 struct _GtkFileFilter
52 {
53   GtkObject parent_instance;
54   
55   gchar *name;
56   GSList *rules;
57
58   GtkFileFilterFlags needed;
59 };
60
61 struct _FilterRule
62 {
63   FilterRuleType type;
64   GtkFileFilterFlags needed;
65   
66   union {
67     gchar *pattern;
68     gchar *mime_type;
69     GSList *pixbuf_formats;
70     struct {
71       GtkFileFilterFunc func;
72       gpointer data;
73       GDestroyNotify notify;
74     } custom;
75   } u;
76 };
77
78 static void gtk_file_filter_class_init (GtkFileFilterClass *class);
79 static void gtk_file_filter_finalize   (GObject            *object);
80
81 static GObjectClass *parent_class;
82
83 GType
84 gtk_file_filter_get_type (void)
85 {
86   static GType file_filter_type = 0;
87
88   if (!file_filter_type)
89     {
90       static const GTypeInfo file_filter_info =
91       {
92         sizeof (GtkFileFilterClass),
93         NULL,           /* base_init */
94         NULL,           /* base_finalize */
95         (GClassInitFunc) gtk_file_filter_class_init,
96         NULL,           /* class_finalize */
97         NULL,           /* class_data */
98         sizeof (GtkFileFilter),
99         0,              /* n_preallocs */
100         NULL            /* init */
101       };
102       
103       file_filter_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkFileFilter",
104                                                  &file_filter_info, 0);
105     }
106
107   return file_filter_type;
108 }
109
110 static void
111 gtk_file_filter_class_init (GtkFileFilterClass *class)
112 {
113   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
114
115   parent_class = g_type_class_peek_parent (class);
116
117   gobject_class->finalize = gtk_file_filter_finalize;
118 }
119
120 static void
121 filter_rule_free (FilterRule *rule)
122 {
123   switch (rule->type)
124     {
125     case FILTER_RULE_MIME_TYPE:
126       g_free (rule->u.mime_type);
127       break;
128     case FILTER_RULE_PATTERN:
129       g_free (rule->u.pattern);
130       break;
131     case FILTER_RULE_CUSTOM:
132       if (rule->u.custom.notify)
133         rule->u.custom.notify (rule->u.custom.data);
134       break;
135     case FILTER_RULE_PIXBUF_FORMATS:
136       g_slist_free (rule->u.pixbuf_formats);
137       break;
138     default:
139       g_assert_not_reached ();
140     }
141
142   g_free (rule);
143 }
144
145 static void
146 gtk_file_filter_finalize (GObject  *object)
147 {
148   GtkFileFilter *filter = GTK_FILE_FILTER (object);
149
150   g_slist_foreach (filter->rules, (GFunc)filter_rule_free, NULL);
151   g_slist_free (filter->rules);
152
153   if (filter->name)
154     g_free (filter->name);
155
156   parent_class->finalize (object);
157 }
158
159 /**
160  * gtk_file_filter_new:
161  * 
162  * Creates a new #GtkFileFilter with no rules added to it.
163  * Such a filter doesn't accept any files, so is not
164  * particularly useful until you add rules with
165  * gtk_file_filter_add_mime_type(), gtk_file_filter_add_pattern(),
166  * or gtk_file_filter_add_custom(). To create a filter
167  * that accepts any file, use:
168  *
169  * <informalexample><programlisting>
170  * GtkFileFilter *filter = gtk_file_filter_new (<!-- -->);
171  * gtk_file_filter_add_pattern (filter, "*");
172  * </programlisting></informalexample>
173  * 
174  * Return value: a new #GtkFileFilter
175  * 
176  * Since: 2.4
177  **/
178 GtkFileFilter *
179 gtk_file_filter_new (void)
180 {
181   return g_object_new (GTK_TYPE_FILE_FILTER, NULL);
182 }
183
184 /**
185  * gtk_file_filter_set_name:
186  * @filter: a #GtkFileFilter
187  * @name: the human-readable-name for the filter, or %NULL
188  *   to remove any existing name.
189  * 
190  * Sets the human-readable name of the filter; this is the string
191  * that will be displayed in the file selector user interface if
192  * there is a selectable list of filters.
193  * 
194  * Since: 2.4
195  **/
196 void
197 gtk_file_filter_set_name (GtkFileFilter *filter,
198                           const gchar   *name)
199 {
200   g_return_if_fail (GTK_IS_FILE_FILTER (filter));
201   
202   if (filter->name)
203     g_free (filter->name);
204
205   filter->name = g_strdup (name);
206 }
207
208 /**
209  * gtk_file_filter_get_name:
210  * @filter: a #GtkFileFilter
211  * 
212  * Gets the human-readable name for the filter. See gtk_file_filter_set_name().
213  * 
214  * Return value: The human-readable name of the filter,
215  *   or %NULL. This value is owned by GTK+ and must not
216  *   be modified or freed.
217  * 
218  * Since: 2.4
219  **/
220 G_CONST_RETURN gchar *
221 gtk_file_filter_get_name (GtkFileFilter *filter)
222 {
223   g_return_val_if_fail (GTK_IS_FILE_FILTER (filter), NULL);
224   
225   return filter->name;
226 }
227
228 static void
229 file_filter_add_rule (GtkFileFilter *filter,
230                       FilterRule    *rule)
231 {
232   filter->needed |= rule->needed;
233   filter->rules = g_slist_append (filter->rules, rule);
234 }
235
236 /**
237  * gtk_file_filter_add_mime_type:
238  * @filter: A #GtkFileFilter
239  * @mime_type: name of a MIME type
240  * 
241  * Adds a rule allowing a given mime type to @filter.
242  * 
243  * Since: 2.4
244  **/
245 void
246 gtk_file_filter_add_mime_type (GtkFileFilter *filter,
247                                const gchar   *mime_type)
248 {
249   FilterRule *rule;
250   
251   g_return_if_fail (GTK_IS_FILE_FILTER (filter));
252   g_return_if_fail (mime_type != NULL);
253
254   rule = g_new (FilterRule, 1);
255   rule->type = FILTER_RULE_MIME_TYPE;
256   rule->needed = GTK_FILE_FILTER_MIME_TYPE;
257   rule->u.mime_type = g_strdup (mime_type);
258
259   file_filter_add_rule (filter, rule);
260 }
261
262 /**
263  * gtk_file_filter_add_pattern:
264  * @filter: a #GtkFileFilter
265  * @pattern: a shell style glob
266  * 
267  * Adds a rule allowing a shell style glob to a filter.
268  * 
269  * Since: 2.4
270  **/
271 void
272 gtk_file_filter_add_pattern (GtkFileFilter *filter,
273                              const gchar   *pattern)
274 {
275   FilterRule *rule;
276   
277   g_return_if_fail (GTK_IS_FILE_FILTER (filter));
278   g_return_if_fail (pattern != NULL);
279
280   rule = g_new (FilterRule, 1);
281   rule->type = FILTER_RULE_PATTERN;
282   rule->needed = GTK_FILE_FILTER_DISPLAY_NAME;
283   rule->u.pattern = g_strdup (pattern);
284
285   file_filter_add_rule (filter, rule);
286 }
287
288 /**
289  * gtk_file_filter_add_pixbuf_formats:
290  * @filter: a #GtkFileFilter
291  * 
292  * Adds a rule allowing image files in the formats supported
293  * by GdkPixbuf.
294  * 
295  * Since: 2.6
296  **/
297 void
298 gtk_file_filter_add_pixbuf_formats (GtkFileFilter *filter)
299 {
300   FilterRule *rule;
301   
302   g_return_if_fail (GTK_IS_FILE_FILTER (filter));
303
304   rule = g_new (FilterRule, 1);
305   rule->type = FILTER_RULE_PIXBUF_FORMATS;
306   rule->needed = GTK_FILE_FILTER_MIME_TYPE;
307   rule->u.pixbuf_formats = gdk_pixbuf_get_formats ();
308   file_filter_add_rule (filter, rule);
309 }
310
311
312 /**
313  * gtk_file_filter_add_custom:
314  * @filter: a #GtkFileFilter
315  * @needed: bitfield of flags indicating the information that the custom
316  *          filter function needs.
317  * @func: callback function; if the function returns %TRUE, then
318  *   the file will be displayed.
319  * @data: data to pass to @func
320  * @notify: function to call to free @data when it is no longer needed.
321  * 
322  * Adds rule to a filter that allows files based on a custom callback
323  * function. The bitfield @needed which is passed in provides information
324  * about what sorts of information that the filter function needs;
325  * this allows GTK+ to avoid retrieving expensive information when
326  * it isn't needed by the filter.
327  * 
328  * Since: 2.4
329  **/
330 void
331 gtk_file_filter_add_custom (GtkFileFilter         *filter,
332                             GtkFileFilterFlags     needed,
333                             GtkFileFilterFunc      func,
334                             gpointer               data,
335                             GDestroyNotify         notify)
336 {
337   FilterRule *rule;
338   
339   g_return_if_fail (GTK_IS_FILE_FILTER (filter));
340   g_return_if_fail (func != NULL);
341
342   rule = g_new (FilterRule, 1);
343   rule->type = FILTER_RULE_CUSTOM;
344   rule->needed = needed;
345   rule->u.custom.func = func;
346   rule->u.custom.data = data;
347   rule->u.custom.notify = notify;
348
349   file_filter_add_rule (filter, rule);
350 }
351
352 /**
353  * gtk_file_filter_get_needed:
354  * @filter: a #GtkFileFilter
355  * 
356  * Gets the fields that need to be filled in for the structure
357  * passed to gtk_file_filter_filter()
358  * 
359  * This function will not typically be used by applications; it
360  * is intended principally for use in the implementation of
361  * #GtkFileChooser.
362  * 
363  * Return value: bitfield of flags indicating needed fields when
364  *   calling gtk_file_filter_filter()
365  * 
366  * Since: 2.4
367  **/
368 GtkFileFilterFlags
369 gtk_file_filter_get_needed (GtkFileFilter *filter)
370 {
371   return filter->needed;
372 }
373
374 /**
375  * gtk_file_filter_filter:
376  * @filter: a #GtkFileFilter
377  * @filter_info: a #GtkFileFilterInfo structure containing information
378  *  about a file.
379  * 
380  * Tests whether a file should be displayed according to @filter.
381  * The #GtkFileFilterInfo structure @filter_info should include
382  * the fields returned feom gtk_file_filter_get_needed().
383  *
384  * This function will not typically be used by applications; it
385  * is intended principally for use in the implementation of
386  * #GtkFileChooser.
387  * 
388  * Return value: %TRUE if the file should be displayed
389  * 
390  * Since: 2.4
391  **/
392 gboolean
393 gtk_file_filter_filter (GtkFileFilter           *filter,
394                         const GtkFileFilterInfo *filter_info)
395 {
396   GSList *tmp_list;
397
398   for (tmp_list = filter->rules; tmp_list; tmp_list = tmp_list->next)
399     {
400       FilterRule *rule = tmp_list->data;
401
402       if ((filter_info->contains & rule->needed) != rule->needed)
403         continue;
404       
405       switch (rule->type)
406         {
407         case FILTER_RULE_MIME_TYPE:
408           if (filter_info->mime_type != NULL
409               && xdg_mime_mime_type_subclass (filter_info->mime_type, rule->u.mime_type))
410             return TRUE;
411           break;
412         case FILTER_RULE_PATTERN:
413           if (filter_info->display_name != NULL &&
414               _gtk_fnmatch (rule->u.pattern, filter_info->display_name, FALSE))
415             return TRUE;
416           break;
417         case FILTER_RULE_PIXBUF_FORMATS:
418           {
419             GSList *list;
420
421             for (list = rule->u.pixbuf_formats; list; list = list->next)
422               {
423                 int i;
424                 gchar **mime_types;
425
426                 mime_types = gdk_pixbuf_format_get_mime_types (list->data);
427
428                 for (i = 0; mime_types[i] != NULL; i++)
429                   {
430                     if (strcmp (mime_types[i], filter_info->mime_type) == 0)
431                       {
432                         g_strfreev (mime_types);
433                         return TRUE;
434                       }
435                   }
436
437                 g_strfreev (mime_types);
438               }
439             break;
440           }
441         case FILTER_RULE_CUSTOM:
442           if (rule->u.custom.func (filter_info, rule->u.custom.data))
443             return TRUE;
444           break;
445         }
446     }
447
448   return FALSE;
449 }