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