Add missing GI annotation to g_content_type_guess_for_tree
[platform/upstream/glib.git] / gio / gcontenttype.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Author: Alexander Larsson <alexl@redhat.com>
23  */
24
25 #include "config.h"
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include "gcontenttypeprivate.h"
31 #include "gthemedicon.h"
32 #include "gicon.h"
33 #include "gfile.h"
34 #include "gfileenumerator.h"
35 #include "gfileinfo.h"
36 #include "glibintl.h"
37
38
39 /**
40  * SECTION:gcontenttype
41  * @short_description: Platform-specific content typing
42  * @include: gio/gio.h
43  *
44  * A content type is a platform specific string that defines the type
45  * of a file. On unix it is a mime type, on win32 it is an extension string
46  * like ".doc", ".txt" or a percieved string like "audio". Such strings
47  * can be looked up in the registry at HKEY_CLASSES_ROOT.
48  **/
49
50 #ifdef G_OS_WIN32
51
52 #include <windows.h>
53
54 static char *
55 get_registry_classes_key (const char    *subdir,
56                           const wchar_t *key_name)
57 {
58   wchar_t *wc_key;
59   HKEY reg_key = NULL;
60   DWORD key_type;
61   DWORD nbytes;
62   char *value_utf8;
63
64   value_utf8 = NULL;
65   
66   nbytes = 0;
67   wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
68   if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
69                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS &&
70       RegQueryValueExW (reg_key, key_name, 0,
71                         &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
72       (key_type == REG_SZ || key_type == REG_EXPAND_SZ))
73     {
74       wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
75       RegQueryValueExW (reg_key, key_name, 0,
76                         &key_type, (LPBYTE) wc_temp, &nbytes);
77       wc_temp[nbytes/2] = '\0';
78       if (key_type == REG_EXPAND_SZ)
79         {
80           wchar_t dummy[1];
81           int len = ExpandEnvironmentStringsW (wc_temp, dummy, 1);
82           if (len > 0)
83             {
84               wchar_t *wc_temp_expanded = g_new (wchar_t, len);
85               if (ExpandEnvironmentStringsW (wc_temp, wc_temp_expanded, len) == len)
86                 value_utf8 = g_utf16_to_utf8 (wc_temp_expanded, -1, NULL, NULL, NULL);
87               g_free (wc_temp_expanded);
88             }
89         }
90       else
91         {
92           value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
93         }
94       g_free (wc_temp);
95       
96     }
97   g_free (wc_key);
98   
99   if (reg_key != NULL)
100     RegCloseKey (reg_key);
101
102   return value_utf8;
103 }
104
105 gboolean
106 g_content_type_equals (const gchar *type1,
107                        const gchar *type2)
108 {
109   char *progid1, *progid2;
110   gboolean res;
111   
112   g_return_val_if_fail (type1 != NULL, FALSE);
113   g_return_val_if_fail (type2 != NULL, FALSE);
114
115   if (g_ascii_strcasecmp (type1, type2) == 0)
116     return TRUE;
117
118   res = FALSE;
119   progid1 = get_registry_classes_key (type1, NULL);
120   progid2 = get_registry_classes_key (type2, NULL);
121   if (progid1 != NULL && progid2 != NULL &&
122       strcmp (progid1, progid2) == 0)
123     res = TRUE;
124   g_free (progid1);
125   g_free (progid2);
126   
127   return res;
128 }
129
130 gboolean
131 g_content_type_is_a (const gchar *type,
132                      const gchar *supertype)
133 {
134   gboolean res;
135   char *value_utf8;
136
137   g_return_val_if_fail (type != NULL, FALSE);
138   g_return_val_if_fail (supertype != NULL, FALSE);
139
140   if (g_content_type_equals (type, supertype))
141     return TRUE;
142
143   res = FALSE;
144   value_utf8 = get_registry_classes_key (type, L"PerceivedType");
145   if (value_utf8 && strcmp (value_utf8, supertype) == 0)
146     res = TRUE;
147   g_free (value_utf8);
148   
149   return res;
150 }
151
152 gboolean
153 g_content_type_is_unknown (const gchar *type)
154 {
155   g_return_val_if_fail (type != NULL, FALSE);
156
157   return strcmp ("*", type) == 0;
158 }
159
160 gchar *
161 g_content_type_get_description (const gchar *type)
162 {
163   char *progid;
164   char *description;
165
166   g_return_val_if_fail (type != NULL, NULL);
167
168   progid = get_registry_classes_key (type, NULL);
169   if (progid)
170     {
171       description = get_registry_classes_key (progid, NULL);
172       g_free (progid);
173
174       if (description)
175         return description;
176     }
177
178   if (g_content_type_is_unknown (type))
179     return g_strdup (_("Unknown type"));
180   return g_strdup_printf (_("%s filetype"), type);
181 }
182
183 gchar *
184 g_content_type_get_mime_type (const gchar *type)
185 {
186   char *mime;
187
188   g_return_val_if_fail (type != NULL, NULL);
189
190   mime = get_registry_classes_key (type, L"Content Type");
191   if (mime)
192     return mime;
193   else if (g_content_type_is_unknown (type))
194     return g_strdup ("application/octet-stream");
195   else if (*type == '.')
196     return g_strdup_printf ("application/x-ext-%s", type+1);
197   /* TODO: Map "image" to "image/ *", etc? */
198
199   return g_strdup ("application/octet-stream");
200 }
201
202 G_LOCK_DEFINE_STATIC (_type_icons);
203 static GHashTable *_type_icons = NULL;
204
205 GIcon *
206 g_content_type_get_icon (const gchar *type)
207 {
208   GIcon *themed_icon;
209   char *name = NULL;
210
211   g_return_val_if_fail (type != NULL, NULL);
212
213   /* In the Registry icons are the default value of
214      HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
215      <type>: <value>
216      REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
217      REG_SZ: shimgvw.dll,3
218   */
219   G_LOCK (_type_icons);
220   if (!_type_icons)
221     _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
222   name = g_hash_table_lookup (_type_icons, type);
223   if (!name && type[0] == '.')
224     {
225       /* double lookup by extension */
226       gchar *key = get_registry_classes_key (type, NULL);
227       if (!key)
228         key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
229       else
230         {
231           gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
232           g_free (key);
233           key = key2;
234         }
235       name = get_registry_classes_key (key, NULL);
236       if (name && strcmp (name, "%1") == 0)
237         {
238           g_free (name);
239           name = NULL;
240         }
241       if (name)
242         g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
243       g_free (key);
244     }
245
246   /* icon-name similar to how it was with gtk-2-12 */
247   if (name)
248     {
249       themed_icon = g_themed_icon_new (name);
250     }
251   else
252     {
253       /* if not found an icon fall back to gtk-builtins */
254       name = strcmp (type, "inode/directory") == 0 ? "gtk-directory" : 
255                            g_content_type_can_be_executable (type) ? "gtk-execute" : "gtk-file";
256       g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
257       themed_icon = g_themed_icon_new_with_default_fallbacks (name);
258     }
259   G_UNLOCK (_type_icons);
260
261   return G_ICON (themed_icon);
262 }
263
264 gboolean
265 g_content_type_can_be_executable (const gchar *type)
266 {
267   g_return_val_if_fail (type != NULL, FALSE);
268
269   if (strcmp (type, ".exe") == 0 ||
270       strcmp (type, ".com") == 0 ||
271       strcmp (type, ".bat") == 0)
272     return TRUE;
273
274   /* TODO: Also look at PATHEXT, which lists the extensions for
275    * "scripts" in addition to those for true binary executables.
276    *
277    * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
278    * right now, for instance). And in a sense, all associated file
279    * types are "executable" on Windows... You can just type foo.jpg as
280    * a command name in cmd.exe, and it will run the application
281    * associated with .jpg. Hard to say what this API actually means
282    * with "executable".
283    */
284
285   return FALSE;
286 }
287
288 static gboolean
289 looks_like_text (const guchar *data, 
290                  gsize         data_size)
291 {
292   gsize i;
293   guchar c;
294   for (i = 0; i < data_size; i++)
295     {
296       c = data[i];
297       if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
298         return FALSE;
299     }
300   return TRUE;
301 }
302
303 gchar *
304 g_content_type_from_mime_type (const gchar *mime_type)
305 {
306   char *key, *content_type;
307
308   g_return_val_if_fail (mime_type != NULL, NULL);
309
310   key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
311   content_type = get_registry_classes_key (key, L"Extension");
312   g_free (key);
313
314   return content_type;
315 }
316
317 gchar *
318 g_content_type_guess (const gchar  *filename,
319                       const guchar *data,
320                       gsize         data_size,
321                       gboolean     *result_uncertain)
322 {
323   char *basename;
324   char *type;
325   char *dot;
326
327   type = NULL;
328
329   if (result_uncertain)
330     *result_uncertain = FALSE;
331
332   if (filename)
333     {
334       basename = g_path_get_basename (filename);
335       dot = strrchr (basename, '.');
336       if (dot)
337         type = g_strdup (dot);
338       g_free (basename);
339     }
340
341   if (type)
342     return type;
343
344   if (data && looks_like_text (data, data_size))
345     return g_strdup (".txt");
346
347   return g_strdup ("*");
348 }
349
350 GList *
351 g_content_types_get_registered (void)
352 {
353   DWORD index;
354   wchar_t keyname[256];
355   DWORD key_len;
356   char *key_utf8;
357   GList *types;
358
359   types = NULL;
360   index = 0;
361   key_len = 256;
362   while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
363                        index,
364                        keyname,
365                        &key_len,
366                        NULL,
367                        NULL,
368                        NULL,
369                        NULL) == ERROR_SUCCESS)
370     {
371       key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
372       if (key_utf8)
373         {
374           if (*key_utf8 == '.')
375             types = g_list_prepend (types, key_utf8);
376           else
377             g_free (key_utf8);
378         }
379       index++;
380       key_len = 256;
381     }
382   
383   return g_list_reverse (types);
384 }
385
386 gchar **
387 g_content_type_guess_for_tree (GFile *root)
388 {
389   /* FIXME: implement */
390   return NULL;
391 }
392
393 #else /* !G_OS_WIN32 - Unix specific version */
394
395 #include <dirent.h>
396
397 #define XDG_PREFIX _gio_xdg
398 #include "xdgmime/xdgmime.h"
399
400 /* We lock this mutex whenever we modify global state in this module.  */
401 G_LOCK_DEFINE_STATIC (gio_xdgmime);
402
403 gsize
404 _g_unix_content_type_get_sniff_len (void)
405 {
406   gsize size;
407
408   G_LOCK (gio_xdgmime);
409   size = xdg_mime_get_max_buffer_extents ();
410   G_UNLOCK (gio_xdgmime);
411
412   return size;
413 }
414
415 gchar *
416 _g_unix_content_type_unalias (const gchar *type)
417 {
418   gchar *res;
419
420   G_LOCK (gio_xdgmime);
421   res = g_strdup (xdg_mime_unalias_mime_type (type));
422   G_UNLOCK (gio_xdgmime);
423
424   return res;
425 }
426
427 gchar **
428 _g_unix_content_type_get_parents (const gchar *type)
429 {
430   const gchar *umime;
431   gchar **parents;
432   GPtrArray *array;
433   int i;
434
435   array = g_ptr_array_new ();
436
437   G_LOCK (gio_xdgmime);
438
439   umime = xdg_mime_unalias_mime_type (type);
440
441   g_ptr_array_add (array, g_strdup (umime));
442
443   parents = xdg_mime_list_mime_parents (umime);
444   for (i = 0; parents && parents[i] != NULL; i++)
445     g_ptr_array_add (array, g_strdup (parents[i]));
446
447   free (parents);
448
449   G_UNLOCK (gio_xdgmime);
450
451   g_ptr_array_add (array, NULL);
452
453   return (gchar **)g_ptr_array_free (array, FALSE);
454 }
455
456 /**
457  * g_content_type_equals:
458  * @type1: a content type string
459  * @type2: a content type string
460  *
461  * Compares two content types for equality.
462  *
463  * Returns: %TRUE if the two strings are identical or equivalent,
464  *     %FALSE otherwise.
465  */
466 gboolean
467 g_content_type_equals (const gchar *type1,
468                        const gchar *type2)
469 {
470   gboolean res;
471
472   g_return_val_if_fail (type1 != NULL, FALSE);
473   g_return_val_if_fail (type2 != NULL, FALSE);
474
475   G_LOCK (gio_xdgmime);
476   res = xdg_mime_mime_type_equal (type1, type2);
477   G_UNLOCK (gio_xdgmime);
478
479   return res;
480 }
481
482 /**
483  * g_content_type_is_a:
484  * @type: a content type string
485  * @supertype: a content type string
486  *
487  * Determines if @type is a subset of @supertype.
488  *
489  * Returns: %TRUE if @type is a kind of @supertype,
490  *     %FALSE otherwise.
491  */
492 gboolean
493 g_content_type_is_a (const gchar *type,
494                      const gchar *supertype)
495 {
496   gboolean res;
497
498   g_return_val_if_fail (type != NULL, FALSE);
499   g_return_val_if_fail (supertype != NULL, FALSE);
500
501   G_LOCK (gio_xdgmime);
502   res = xdg_mime_mime_type_subclass (type, supertype);
503   G_UNLOCK (gio_xdgmime);
504
505   return res;
506 }
507
508 /**
509  * g_content_type_is_unknown:
510  * @type: a content type string
511  *
512  * Checks if the content type is the generic "unknown" type.
513  * On UNIX this is the "application/octet-stream" mimetype,
514  * while on win32 it is "*".
515  *
516  * Returns: %TRUE if the type is the unknown type.
517  */
518 gboolean
519 g_content_type_is_unknown (const gchar *type)
520 {
521   g_return_val_if_fail (type != NULL, FALSE);
522
523   return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
524 }
525
526
527 typedef enum {
528   MIME_TAG_TYPE_OTHER,
529   MIME_TAG_TYPE_COMMENT
530 } MimeTagType;
531
532 typedef struct {
533   int current_type;
534   int current_lang_level;
535   int comment_lang_level;
536   char *comment;
537 } MimeParser;
538
539
540 static int
541 language_level (const char *lang)
542 {
543   const char * const *lang_list;
544   int i;
545
546   /* The returned list is sorted from most desirable to least
547      desirable and always contains the default locale "C". */
548   lang_list = g_get_language_names ();
549
550   for (i = 0; lang_list[i]; i++)
551     if (strcmp (lang_list[i], lang) == 0)
552       return 1000-i;
553
554   return 0;
555 }
556
557 static void
558 mime_info_start_element (GMarkupParseContext  *context,
559                          const gchar          *element_name,
560                          const gchar         **attribute_names,
561                          const gchar         **attribute_values,
562                          gpointer              user_data,
563                          GError              **error)
564 {
565   int i;
566   const char *lang;
567   MimeParser *parser = user_data;
568
569   if (strcmp (element_name, "comment") == 0)
570     {
571       lang = "C";
572       for (i = 0; attribute_names[i]; i++)
573         if (strcmp (attribute_names[i], "xml:lang") == 0)
574           {
575             lang = attribute_values[i];
576             break;
577           }
578
579       parser->current_lang_level = language_level (lang);
580       parser->current_type = MIME_TAG_TYPE_COMMENT;
581     }
582   else
583     parser->current_type = MIME_TAG_TYPE_OTHER;
584 }
585
586 static void
587 mime_info_end_element (GMarkupParseContext  *context,
588                        const gchar          *element_name,
589                        gpointer              user_data,
590                        GError              **error)
591 {
592   MimeParser *parser = user_data;
593
594   parser->current_type = MIME_TAG_TYPE_OTHER;
595 }
596
597 static void
598 mime_info_text (GMarkupParseContext  *context,
599                 const gchar          *text,
600                 gsize                 text_len,
601                 gpointer              user_data,
602                 GError              **error)
603 {
604   MimeParser *parser = user_data;
605
606   if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
607       parser->current_lang_level > parser->comment_lang_level)
608     {
609       g_free (parser->comment);
610       parser->comment = g_strndup (text, text_len);
611       parser->comment_lang_level = parser->current_lang_level;
612     }
613 }
614
615 static char *
616 load_comment_for_mime_helper (const char *dir,
617                               const char *basename)
618 {
619   GMarkupParseContext *context;
620   char *filename, *data;
621   gsize len;
622   gboolean res;
623   MimeParser parse_data = {0};
624   GMarkupParser parser = {
625     mime_info_start_element,
626     mime_info_end_element,
627     mime_info_text
628   };
629
630   filename = g_build_filename (dir, "mime", basename, NULL);
631
632   res = g_file_get_contents (filename,  &data,  &len,  NULL);
633   g_free (filename);
634   if (!res)
635     return NULL;
636
637   context = g_markup_parse_context_new   (&parser, 0, &parse_data, NULL);
638   res = g_markup_parse_context_parse (context, data, len, NULL);
639   g_free (data);
640   g_markup_parse_context_free (context);
641
642   if (!res)
643     return NULL;
644
645   return parse_data.comment;
646 }
647
648
649 static char *
650 load_comment_for_mime (const char *mimetype)
651 {
652   const char * const* dirs;
653   char *basename;
654   char *comment;
655   int i;
656
657   basename = g_strdup_printf ("%s.xml", mimetype);
658
659   comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
660   if (comment)
661     {
662       g_free (basename);
663       return comment;
664     }
665
666   dirs = g_get_system_data_dirs ();
667
668   for (i = 0; dirs[i] != NULL; i++)
669     {
670       comment = load_comment_for_mime_helper (dirs[i], basename);
671       if (comment)
672         {
673           g_free (basename);
674           return comment;
675         }
676     }
677   g_free (basename);
678
679   return g_strdup_printf (_("%s type"), mimetype);
680 }
681
682 /**
683  * g_content_type_get_description:
684  * @type: a content type string
685  *
686  * Gets the human readable description of the content type.
687  *
688  * Returns: a short description of the content type @type. Free the
689  *     returned string with g_free()
690  */
691 gchar *
692 g_content_type_get_description (const gchar *type)
693 {
694   static GHashTable *type_comment_cache = NULL;
695   gchar *comment;
696
697   g_return_val_if_fail (type != NULL, NULL);
698
699   G_LOCK (gio_xdgmime);
700   type = xdg_mime_unalias_mime_type (type);
701
702   if (type_comment_cache == NULL)
703     type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
704
705   comment = g_hash_table_lookup (type_comment_cache, type);
706   comment = g_strdup (comment);
707   G_UNLOCK (gio_xdgmime);
708
709   if (comment != NULL)
710     return comment;
711
712   comment = load_comment_for_mime (type);
713
714   G_LOCK (gio_xdgmime);
715   g_hash_table_insert (type_comment_cache,
716                        g_strdup (type),
717                        g_strdup (comment));
718   G_UNLOCK (gio_xdgmime);
719
720   return comment;
721 }
722
723 /**
724  * g_content_type_get_mime_type:
725  * @type: a content type string
726  *
727  * Gets the mime type for the content type, if one is registered.
728  *
729  * Returns: (allow-none): the registered mime type for the given @type,
730  *     or %NULL if unknown.
731  */
732 char *
733 g_content_type_get_mime_type (const char *type)
734 {
735   g_return_val_if_fail (type != NULL, NULL);
736
737   return g_strdup (type);
738 }
739
740 /**
741  * g_content_type_get_icon:
742  * @type: a content type string
743  *
744  * Gets the icon for a content type.
745  *
746  * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
747  *     object with g_object_unref()
748  */
749 GIcon *
750 g_content_type_get_icon (const gchar *type)
751 {
752   char *mimetype_icon, *generic_mimetype_icon, *q;
753   char *xdg_mimetype_icon, *legacy_mimetype_icon;
754   char *xdg_mimetype_generic_icon;
755   char *icon_names[5];
756   int n = 0;
757   const char *p;
758   GIcon *themed_icon;
759
760   g_return_val_if_fail (type != NULL, NULL);
761
762   G_LOCK (gio_xdgmime);
763   xdg_mimetype_icon = g_strdup (xdg_mime_get_icon (type));
764   xdg_mimetype_generic_icon = g_strdup (xdg_mime_get_generic_icon (type));
765   G_UNLOCK (gio_xdgmime);
766
767   mimetype_icon = g_strdup (type);
768
769   while ((q = strchr (mimetype_icon, '/')) != NULL)
770     *q = '-';
771
772   p = strchr (type, '/');
773   if (p == NULL)
774     p = type + strlen (type);
775
776   /* Not all icons have migrated to the new icon theme spec, look for old names too */
777   legacy_mimetype_icon = g_strconcat ("gnome-mime-", mimetype_icon, NULL);
778
779   generic_mimetype_icon = g_malloc (p - type + strlen ("-x-generic") + 1);
780   memcpy (generic_mimetype_icon, type, p - type);
781   memcpy (generic_mimetype_icon + (p - type), "-x-generic", strlen ("-x-generic"));
782   generic_mimetype_icon[(p - type) + strlen ("-x-generic")] = 0;
783
784   if (xdg_mimetype_icon)
785     icon_names[n++] = xdg_mimetype_icon;
786
787   icon_names[n++] = mimetype_icon;
788   icon_names[n++] = legacy_mimetype_icon;
789
790   if (xdg_mimetype_generic_icon)
791     icon_names[n++] = xdg_mimetype_generic_icon;
792
793   icon_names[n++] = generic_mimetype_icon;
794
795   themed_icon = g_themed_icon_new_from_names (icon_names, n);
796
797   g_free (xdg_mimetype_icon);
798   g_free (xdg_mimetype_generic_icon);
799   g_free (mimetype_icon);
800   g_free (legacy_mimetype_icon);
801   g_free (generic_mimetype_icon);
802
803   return themed_icon;
804 }
805
806 /**
807  * g_content_type_can_be_executable:
808  * @type: a content type string
809  *
810  * Checks if a content type can be executable. Note that for instance
811  * things like text files can be executables (i.e. scripts and batch files).
812  *
813  * Returns: %TRUE if the file type corresponds to a type that
814  *     can be executable, %FALSE otherwise.
815  */
816 gboolean
817 g_content_type_can_be_executable (const gchar *type)
818 {
819   g_return_val_if_fail (type != NULL, FALSE);
820
821   if (g_content_type_is_a (type, "application/x-executable")  ||
822       g_content_type_is_a (type, "text/plain"))
823     return TRUE;
824
825   return FALSE;
826 }
827
828 static gboolean
829 looks_like_text (const guchar *data, gsize data_size)
830 {
831   gsize i;
832   char c;
833
834   for (i = 0; i < data_size; i++)
835     {
836       c = data[i];
837
838       if (g_ascii_iscntrl (c) &&
839           !g_ascii_isspace (c) &&
840           c != '\b')
841         return FALSE;
842     }
843   return TRUE;
844 }
845
846 /**
847  * g_content_type_from_mime_type:
848  * @mime_type: a mime type string
849  *
850  * Tries to find a content type based on the mime type name.
851  *
852  * Returns: (allow-none): Newly allocated string with content type
853  *     or %NULL. Free with g_free()
854  *
855  * Since: 2.18
856  **/
857 gchar *
858 g_content_type_from_mime_type (const gchar *mime_type)
859 {
860   char *umime;
861
862   g_return_val_if_fail (mime_type != NULL, NULL);
863
864   G_LOCK (gio_xdgmime);
865   /* mime type and content type are same on unixes */
866   umime = g_strdup (xdg_mime_unalias_mime_type (mime_type));
867   G_UNLOCK (gio_xdgmime);
868
869   return umime;
870 }
871
872 /**
873  * g_content_type_guess:
874  * @filename: (allow-none): a string, or %NULL
875  * @data: (allow-none) (array length=data_size): a stream of data, or %NULL
876  * @data_size: the size of @data
877  * @result_uncertain: (allow-none) (out): return location for the certainty
878  *     of the result, or %NULL
879  *
880  * Guesses the content type based on example data. If the function is
881  * uncertain, @result_uncertain will be set to %TRUE. Either @filename
882  * or @data may be %NULL, in which case the guess will be based solely
883  * on the other argument.
884  *
885  * Returns: a string indicating a guessed content type for the
886  *     given data. Free with g_free()
887  */
888 gchar *
889 g_content_type_guess (const gchar  *filename,
890                       const guchar *data,
891                       gsize         data_size,
892                       gboolean     *result_uncertain)
893 {
894   char *basename;
895   const char *name_mimetypes[10], *sniffed_mimetype;
896   char *mimetype;
897   int i;
898   int n_name_mimetypes;
899   int sniffed_prio;
900
901   sniffed_prio = 0;
902   n_name_mimetypes = 0;
903   sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
904
905   if (result_uncertain)
906     *result_uncertain = FALSE;
907
908   G_LOCK (gio_xdgmime);
909
910   if (filename)
911     {
912       i = strlen (filename);
913       if (filename[i - 1] == '/')
914         {
915           name_mimetypes[0] = "inode/directory";
916           name_mimetypes[1] = NULL;
917           n_name_mimetypes = 1;
918           if (result_uncertain)
919             *result_uncertain = TRUE;
920         }
921       else
922         {
923           basename = g_path_get_basename (filename);
924           n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
925           g_free (basename);
926         }
927     }
928
929   /* Got an extension match, and no conflicts. This is it. */
930   if (n_name_mimetypes == 1)
931     {
932       G_UNLOCK (gio_xdgmime);
933       return g_strdup (name_mimetypes[0]);
934     }
935
936   if (data)
937     {
938       sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
939       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
940           data &&
941           looks_like_text (data, data_size))
942         sniffed_mimetype = "text/plain";
943
944       /* For security reasons we don't ever want to sniff desktop files
945        * where we know the filename and it doesn't have a .desktop extension.
946        * This is because desktop files allow executing any application and
947        * we don't want to make it possible to hide them looking like something
948        * else.
949        */
950       if (filename != NULL &&
951           strcmp (sniffed_mimetype, "application/x-desktop") == 0)
952         sniffed_mimetype = "text/plain";
953     }
954
955   if (n_name_mimetypes == 0)
956     {
957       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
958           result_uncertain)
959         *result_uncertain = TRUE;
960
961       mimetype = g_strdup (sniffed_mimetype);
962     }
963   else
964     {
965       mimetype = NULL;
966       if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
967         {
968           if (sniffed_prio >= 80) /* High priority sniffing match, use that */
969             mimetype = g_strdup (sniffed_mimetype);
970           else
971             {
972               /* There are conflicts between the name matches and we
973                * have a sniffed type, use that as a tie breaker.
974                */
975               for (i = 0; i < n_name_mimetypes; i++)
976                 {
977                   if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
978                     {
979                       /* This nametype match is derived from (or the same as)
980                        * the sniffed type). This is probably it.
981                        */
982                       mimetype = g_strdup (name_mimetypes[i]);
983                       break;
984                     }
985                 }
986             }
987         }
988
989       if (mimetype == NULL)
990         {
991           /* Conflicts, and sniffed type was no help or not there.
992            * Guess on the first one
993            */
994           mimetype = g_strdup (name_mimetypes[0]);
995           if (result_uncertain)
996             *result_uncertain = TRUE;
997         }
998     }
999
1000   G_UNLOCK (gio_xdgmime);
1001
1002   return mimetype;
1003 }
1004
1005 static void
1006 enumerate_mimetypes_subdir (const char *dir,
1007                             const char *prefix,
1008                             GHashTable *mimetypes)
1009 {
1010   DIR *d;
1011   struct dirent *ent;
1012   char *mimetype;
1013
1014   d = opendir (dir);
1015   if (d)
1016     {
1017       while ((ent = readdir (d)) != NULL)
1018         {
1019           if (g_str_has_suffix (ent->d_name, ".xml"))
1020             {
1021               mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
1022               g_hash_table_replace (mimetypes, mimetype, NULL);
1023             }
1024         }
1025       closedir (d);
1026     }
1027 }
1028
1029 static void
1030 enumerate_mimetypes_dir (const char *dir,
1031                          GHashTable *mimetypes)
1032 {
1033   DIR *d;
1034   struct dirent *ent;
1035   char *mimedir;
1036   char *name;
1037
1038   mimedir = g_build_filename (dir, "mime", NULL);
1039
1040   d = opendir (mimedir);
1041   if (d)
1042     {
1043       while ((ent = readdir (d)) != NULL)
1044         {
1045           if (strcmp (ent->d_name, "packages") != 0)
1046             {
1047               name = g_build_filename (mimedir, ent->d_name, NULL);
1048               if (g_file_test (name, G_FILE_TEST_IS_DIR))
1049                 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
1050               g_free (name);
1051             }
1052         }
1053       closedir (d);
1054     }
1055
1056   g_free (mimedir);
1057 }
1058
1059 /**
1060  * g_content_types_get_registered:
1061  *
1062  * Gets a list of strings containing all the registered content types
1063  * known to the system. The list and its data should be freed using
1064  * <programlisting>
1065  * g_list_foreach (list, g_free, NULL);
1066  * g_list_free (list);
1067  * </programlisting>
1068  *
1069  * Returns: (element-type utf8) (transfer full): #GList of the registered content types
1070  */
1071 GList *
1072 g_content_types_get_registered (void)
1073 {
1074   const char * const* dirs;
1075   GHashTable *mimetypes;
1076   GHashTableIter iter;
1077   gpointer key;
1078   int i;
1079   GList *l;
1080
1081   mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1082
1083   enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
1084   dirs = g_get_system_data_dirs ();
1085
1086   for (i = 0; dirs[i] != NULL; i++)
1087     enumerate_mimetypes_dir (dirs[i], mimetypes);
1088
1089   l = NULL;
1090   g_hash_table_iter_init (&iter, mimetypes);
1091   while (g_hash_table_iter_next (&iter, &key, NULL))
1092     {
1093       l = g_list_prepend (l, key);
1094       g_hash_table_iter_steal (&iter);
1095     }
1096
1097   g_hash_table_destroy (mimetypes);
1098
1099   return l;
1100 }
1101
1102
1103 /* tree magic data */
1104 static GList *tree_matches = NULL;
1105 static gboolean need_reload = FALSE;
1106
1107 G_LOCK_DEFINE_STATIC (gio_treemagic);
1108
1109 typedef struct
1110 {
1111   gchar *path;
1112   GFileType type;
1113   guint match_case : 1;
1114   guint executable : 1;
1115   guint non_empty  : 1;
1116   guint on_disc    : 1;
1117   gchar *mimetype;
1118   GList *matches;
1119 } TreeMatchlet;
1120
1121 typedef struct
1122 {
1123   gchar *contenttype;
1124   gint priority;
1125   GList *matches;
1126 } TreeMatch;
1127
1128
1129 static void
1130 tree_matchlet_free (TreeMatchlet *matchlet)
1131 {
1132   g_list_foreach (matchlet->matches, (GFunc)tree_matchlet_free, NULL);
1133   g_list_free (matchlet->matches);
1134   g_free (matchlet->path);
1135   g_free (matchlet->mimetype);
1136   g_slice_free (TreeMatchlet, matchlet);
1137 }
1138
1139 static void
1140 tree_match_free (TreeMatch *match)
1141 {
1142   g_list_foreach (match->matches, (GFunc)tree_matchlet_free, NULL);
1143   g_list_free (match->matches);
1144   g_free (match->contenttype);
1145   g_slice_free (TreeMatch, match);
1146 }
1147
1148 static TreeMatch *
1149 parse_header (gchar *line)
1150 {
1151   gint len;
1152   gchar *s;
1153   TreeMatch *match;
1154
1155   len = strlen (line);
1156
1157   if (line[0] != '[' || line[len - 1] != ']')
1158     return NULL;
1159
1160   line[len - 1] = 0;
1161   s = strchr (line, ':');
1162
1163   match = g_slice_new0 (TreeMatch);
1164   match->priority = atoi (line + 1);
1165   match->contenttype = g_strdup (s + 1);
1166
1167   return match;
1168 }
1169
1170 static TreeMatchlet *
1171 parse_match_line (gchar *line,
1172                   gint  *depth)
1173 {
1174   gchar *s, *p;
1175   TreeMatchlet *matchlet;
1176   gchar **parts;
1177   gint i;
1178
1179   matchlet = g_slice_new0 (TreeMatchlet);
1180
1181   if (line[0] == '>')
1182     {
1183       *depth = 0;
1184       s = line;
1185     }
1186   else
1187     {
1188       *depth = atoi (line);
1189       s = strchr (line, '>');
1190     }
1191   s += 2;
1192   p = strchr (s, '"');
1193   *p = 0;
1194
1195   matchlet->path = g_strdup (s);
1196   s = p + 1;
1197   parts = g_strsplit (s, ",", 0);
1198   if (strcmp (parts[0], "=file") == 0)
1199     matchlet->type = G_FILE_TYPE_REGULAR;
1200   else if (strcmp (parts[0], "=directory") == 0)
1201     matchlet->type = G_FILE_TYPE_DIRECTORY;
1202   else if (strcmp (parts[0], "=link") == 0)
1203     matchlet->type = G_FILE_TYPE_SYMBOLIC_LINK;
1204   else
1205     matchlet->type = G_FILE_TYPE_UNKNOWN;
1206   for (i = 1; parts[i]; i++)
1207     {
1208       if (strcmp (parts[i], "executable") == 0)
1209         matchlet->executable = 1;
1210       else if (strcmp (parts[i], "match-case") == 0)
1211         matchlet->match_case = 1;
1212       else if (strcmp (parts[i], "non-empty") == 0)
1213         matchlet->non_empty = 1;
1214       else if (strcmp (parts[i], "on-disc") == 0)
1215         matchlet->on_disc = 1;
1216       else
1217         matchlet->mimetype = g_strdup (parts[i]);
1218     }
1219
1220   g_strfreev (parts);
1221
1222   return matchlet;
1223 }
1224
1225 static gint
1226 cmp_match (gconstpointer a, gconstpointer b)
1227 {
1228   const TreeMatch *aa = (const TreeMatch *)a;
1229   const TreeMatch *bb = (const TreeMatch *)b;
1230
1231   return bb->priority - aa->priority;
1232 }
1233
1234 static void
1235 insert_match (TreeMatch *match)
1236 {
1237   tree_matches = g_list_insert_sorted (tree_matches, match, cmp_match);
1238 }
1239
1240 static void
1241 insert_matchlet (TreeMatch    *match,
1242                  TreeMatchlet *matchlet,
1243                  gint          depth)
1244 {
1245   if (depth == 0)
1246     match->matches = g_list_append (match->matches, matchlet);
1247   else
1248     {
1249       GList *last;
1250       TreeMatchlet *m;
1251
1252       last = g_list_last (match->matches);
1253       if (!last)
1254         {
1255           tree_matchlet_free (matchlet);
1256           g_warning ("can't insert tree matchlet at depth %d", depth);
1257           return;
1258         }
1259
1260       m = (TreeMatchlet *) last->data;
1261       while (--depth > 0)
1262         {
1263           last = g_list_last (m->matches);
1264           if (!last)
1265             {
1266               tree_matchlet_free (matchlet);
1267               g_warning ("can't insert tree matchlet at depth %d", depth);
1268               return;
1269             }
1270
1271           m = (TreeMatchlet *) last->data;
1272         }
1273       m->matches = g_list_append (m->matches, matchlet);
1274     }
1275 }
1276
1277 static void
1278 read_tree_magic_from_directory (const gchar *prefix)
1279 {
1280   gchar *filename;
1281   gchar *text;
1282   gsize len;
1283   gchar **lines;
1284   gint i;
1285   TreeMatch *match;
1286   TreeMatchlet *matchlet;
1287   gint depth;
1288
1289   filename = g_build_filename (prefix, "mime", "treemagic", NULL);
1290
1291   if (g_file_get_contents (filename, &text, &len, NULL))
1292     {
1293       if (strcmp (text, "MIME-TreeMagic") == 0)
1294         {
1295           lines = g_strsplit (text + strlen ("MIME-TreeMagic") + 2, "\n", 0);
1296           match = NULL;
1297           for (i = 0; lines[i] && lines[i][0]; i++)
1298             {
1299               if (lines[i][0] == '[')
1300                 {
1301                   match = parse_header (lines[i]);
1302                   insert_match (match);
1303                 }
1304               else
1305                 {
1306                   matchlet = parse_match_line (lines[i], &depth);
1307                   insert_matchlet (match, matchlet, depth);
1308                 }
1309             }
1310
1311           g_strfreev (lines);
1312         }
1313       else
1314         g_warning ("%s: header not found, skipping\n", filename);
1315
1316       g_free (text);
1317     }
1318
1319   g_free (filename);
1320 }
1321
1322
1323 static void
1324 xdg_mime_reload (void *user_data)
1325 {
1326   need_reload = TRUE;
1327 }
1328
1329 static void
1330 tree_magic_shutdown (void)
1331 {
1332   g_list_foreach (tree_matches, (GFunc)tree_match_free, NULL);
1333   g_list_free (tree_matches);
1334   tree_matches = NULL;
1335 }
1336
1337 static void
1338 tree_magic_init (void)
1339 {
1340   static gboolean initialized = FALSE;
1341   const gchar *dir;
1342   const gchar * const * dirs;
1343   int i;
1344
1345   if (!initialized)
1346     {
1347       initialized = TRUE;
1348
1349       xdg_mime_register_reload_callback (xdg_mime_reload, NULL, NULL);
1350       need_reload = TRUE;
1351     }
1352
1353   if (need_reload)
1354     {
1355       need_reload = FALSE;
1356
1357       tree_magic_shutdown ();
1358
1359       dir = g_get_user_data_dir ();
1360       read_tree_magic_from_directory (dir);
1361       dirs = g_get_system_data_dirs ();
1362       for (i = 0; dirs[i]; i++)
1363         read_tree_magic_from_directory (dirs[i]);
1364     }
1365 }
1366
1367 /* a filtering enumerator */
1368
1369 typedef struct
1370 {
1371   gchar *path;
1372   gint depth;
1373   gboolean ignore_case;
1374   gchar **components;
1375   gchar **case_components;
1376   GFileEnumerator **enumerators;
1377   GFile **children;
1378 } Enumerator;
1379
1380 static gboolean
1381 component_match (Enumerator  *e,
1382                  gint         depth,
1383                  const gchar *name)
1384 {
1385   gchar *case_folded, *key;
1386   gboolean found;
1387
1388   if (strcmp (name, e->components[depth]) == 0)
1389     return TRUE;
1390
1391   if (!e->ignore_case)
1392     return FALSE;
1393
1394   case_folded = g_utf8_casefold (name, -1);
1395   key = g_utf8_collate_key (case_folded, -1);
1396
1397   found = strcmp (key, e->case_components[depth]) == 0;
1398
1399   g_free (case_folded);
1400   g_free (key);
1401
1402   return found;
1403 }
1404
1405 static GFile *
1406 next_match_recurse (Enumerator *e,
1407                     gint        depth)
1408 {
1409   GFile *file;
1410   GFileInfo *info;
1411   const gchar *name;
1412
1413   while (TRUE)
1414     {
1415       if (e->enumerators[depth] == NULL)
1416         {
1417           if (depth > 0)
1418             {
1419               file = next_match_recurse (e, depth - 1);
1420               if (file)
1421                 {
1422                   e->children[depth] = file;
1423                   e->enumerators[depth] = g_file_enumerate_children (file,
1424                                                                      G_FILE_ATTRIBUTE_STANDARD_NAME,
1425                                                                      G_FILE_QUERY_INFO_NONE,
1426                                                                      NULL,
1427                                                                      NULL);
1428                 }
1429             }
1430           if (e->enumerators[depth] == NULL)
1431             return NULL;
1432         }
1433
1434       while ((info = g_file_enumerator_next_file (e->enumerators[depth], NULL, NULL)))
1435         {
1436           name = g_file_info_get_name (info);
1437           if (component_match (e, depth, name))
1438             {
1439               file = g_file_get_child (e->children[depth], name);
1440               g_object_unref (info);
1441               return file;
1442             }
1443           g_object_unref (info);
1444         }
1445
1446       g_object_unref (e->enumerators[depth]);
1447       e->enumerators[depth] = NULL;
1448       g_object_unref (e->children[depth]);
1449       e->children[depth] = NULL;
1450     }
1451 }
1452
1453 static GFile *
1454 enumerator_next (Enumerator *e)
1455 {
1456   return next_match_recurse (e, e->depth - 1);
1457 }
1458
1459 static Enumerator *
1460 enumerator_new (GFile      *root,
1461                 const char *path,
1462                 gboolean    ignore_case)
1463 {
1464   Enumerator *e;
1465   gint i;
1466   gchar *case_folded;
1467
1468   e = g_new0 (Enumerator, 1);
1469   e->path = g_strdup (path);
1470   e->ignore_case = ignore_case;
1471
1472   e->components = g_strsplit (e->path, G_DIR_SEPARATOR_S, -1);
1473   e->depth = g_strv_length (e->components);
1474   if (e->ignore_case)
1475     {
1476       e->case_components = g_new0 (char *, e->depth + 1);
1477       for (i = 0; e->components[i]; i++)
1478         {
1479           case_folded = g_utf8_casefold (e->components[i], -1);
1480           e->case_components[i] = g_utf8_collate_key (case_folded, -1);
1481           g_free (case_folded);
1482         }
1483     }
1484
1485   e->children = g_new0 (GFile *, e->depth);
1486   e->children[0] = g_object_ref (root);
1487   e->enumerators = g_new0 (GFileEnumerator *, e->depth);
1488   e->enumerators[0] = g_file_enumerate_children (root,
1489                                                  G_FILE_ATTRIBUTE_STANDARD_NAME,
1490                                                  G_FILE_QUERY_INFO_NONE,
1491                                                  NULL,
1492                                                  NULL);
1493
1494   return e;
1495 }
1496
1497 static void
1498 enumerator_free (Enumerator *e)
1499 {
1500   gint i;
1501
1502   for (i = 0; i < e->depth; i++)
1503     {
1504       if (e->enumerators[i])
1505         g_object_unref (e->enumerators[i]);
1506       if (e->children[i])
1507         g_object_unref (e->children[i]);
1508     }
1509
1510   g_free (e->enumerators);
1511   g_free (e->children);
1512   g_strfreev (e->components);
1513   if (e->case_components)
1514     g_strfreev (e->case_components);
1515   g_free (e->path);
1516   g_free (e);
1517 }
1518
1519 static gboolean
1520 matchlet_match (TreeMatchlet *matchlet,
1521                 GFile        *root)
1522 {
1523   GFile *file;
1524   GFileInfo *info;
1525   gboolean result;
1526   const gchar *attrs;
1527   Enumerator *e;
1528   GList *l;
1529
1530   e = enumerator_new (root, matchlet->path, !matchlet->match_case);
1531
1532   do
1533     {
1534       file = enumerator_next (e);
1535       if (!file)
1536         {
1537           enumerator_free (e);
1538           return FALSE;
1539         }
1540
1541       if (matchlet->mimetype)
1542         attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1543                 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ","
1544                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
1545       else
1546         attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1547                 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE;
1548       info = g_file_query_info (file,
1549                                 attrs,
1550                                 G_FILE_QUERY_INFO_NONE,
1551                                 NULL,
1552                                 NULL);
1553       if (info)
1554         {
1555           result = TRUE;
1556
1557           if (matchlet->type != G_FILE_TYPE_UNKNOWN &&
1558               g_file_info_get_file_type (info) != matchlet->type)
1559             result = FALSE;
1560
1561           if (matchlet->executable &&
1562               !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
1563             result = FALSE;
1564         }
1565       else
1566         result = FALSE;
1567
1568       if (result && matchlet->non_empty)
1569         {
1570           GFileEnumerator *child_enum;
1571           GFileInfo *child_info;
1572
1573           child_enum = g_file_enumerate_children (file,
1574                                                   G_FILE_ATTRIBUTE_STANDARD_NAME,
1575                                                   G_FILE_QUERY_INFO_NONE,
1576                                                   NULL,
1577                                                   NULL);
1578
1579           if (child_enum)
1580             {
1581               child_info = g_file_enumerator_next_file (child_enum, NULL, NULL);
1582               if (child_info)
1583                 g_object_unref (child_info);
1584               else
1585                 result = FALSE;
1586               g_object_unref (child_enum);
1587             }
1588           else
1589             result = FALSE;
1590         }
1591
1592       if (result && matchlet->mimetype)
1593         {
1594           if (strcmp (matchlet->mimetype, g_file_info_get_content_type (info)) != 0)
1595             result = FALSE;
1596         }
1597
1598       g_object_unref (info);
1599       g_object_unref (file);
1600     }
1601   while (!result);
1602
1603   enumerator_free (e);
1604
1605   if (!matchlet->matches)
1606     return TRUE;
1607
1608   for (l = matchlet->matches; l; l = l->next)
1609     {
1610       TreeMatchlet *submatchlet;
1611
1612       submatchlet = l->data;
1613       if (matchlet_match (submatchlet, root))
1614         return TRUE;
1615     }
1616
1617   return FALSE;
1618 }
1619
1620 static void
1621 match_match (TreeMatch    *match,
1622              GFile        *root,
1623              GPtrArray    *types)
1624 {
1625   GList *l;
1626
1627   for (l = match->matches; l; l = l->next)
1628     {
1629       TreeMatchlet *matchlet = l->data;
1630       if (matchlet_match (matchlet, root))
1631         {
1632           g_ptr_array_add (types, g_strdup (match->contenttype));
1633           break;
1634         }
1635     }
1636 }
1637
1638 /**
1639  * g_content_type_guess_for_tree:
1640  * @root: the root of the tree to guess a type for
1641  *
1642  * Tries to guess the type of the tree with root @root, by
1643  * looking at the files it contains. The result is an array
1644  * of content types, with the best guess coming first.
1645  *
1646  * The types returned all have the form x-content/foo, e.g.
1647  * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
1648  * (for a camera memory card). See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
1649  * specification for more on x-content types.
1650  *
1651  * This function is useful in the implementation of
1652  * g_mount_guess_content_type().
1653  *
1654  * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
1655  *     array of zero or more content types, or %NULL. Free with g_strfreev()
1656  *
1657  * Since: 2.18
1658  */
1659 gchar **
1660 g_content_type_guess_for_tree (GFile *root)
1661 {
1662   GPtrArray *types;
1663   GList *l;
1664
1665   types = g_ptr_array_new ();
1666
1667   G_LOCK (gio_treemagic);
1668
1669   tree_magic_init ();
1670   for (l = tree_matches; l; l = l->next)
1671     {
1672       TreeMatch *match = l->data;
1673       match_match (match, root, types);
1674     }
1675
1676   G_UNLOCK (gio_treemagic);
1677
1678   g_ptr_array_add (types, NULL);
1679
1680   return (gchar **)g_ptr_array_free (types, FALSE);
1681 }
1682
1683 #endif /* Unix version */