Imported Upstream version 2.53.3
[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.1 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, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include <sys/types.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include "gcontenttypeprivate.h"
29 #include "gthemedicon.h"
30 #include "gicon.h"
31 #include "gfile.h"
32 #include "gfileenumerator.h"
33 #include "gfileinfo.h"
34 #include "glibintl.h"
35
36
37 /**
38  * SECTION:gcontenttype
39  * @short_description: Platform-specific content typing
40  * @include: gio/gio.h
41  *
42  * A content type is a platform specific string that defines the type
43  * of a file. On UNIX it is a
44  * [mime type](http://www.wikipedia.org/wiki/Internet_media_type)
45  * like "text/plain" or "image/png".
46  * On Win32 it is an extension string like ".doc", ".txt" or a perceived
47  * string like "audio". Such strings can be looked up in the registry at
48  * HKEY_CLASSES_ROOT.
49  * On OSX it is a [Uniform Type Identifier](https://en.wikipedia.org/wiki/Uniform_Type_Identifier)
50  * such as "com.apple.application".
51  **/
52
53 #include <dirent.h>
54
55 #define XDG_PREFIX _gio_xdg
56 #include "xdgmime/xdgmime.h"
57
58 /* We lock this mutex whenever we modify global state in this module.  */
59 G_LOCK_DEFINE_STATIC (gio_xdgmime);
60
61 gsize
62 _g_unix_content_type_get_sniff_len (void)
63 {
64   gsize size;
65
66   G_LOCK (gio_xdgmime);
67   size = xdg_mime_get_max_buffer_extents ();
68   G_UNLOCK (gio_xdgmime);
69
70   return size;
71 }
72
73 gchar *
74 _g_unix_content_type_unalias (const gchar *type)
75 {
76   gchar *res;
77
78   G_LOCK (gio_xdgmime);
79   res = g_strdup (xdg_mime_unalias_mime_type (type));
80   G_UNLOCK (gio_xdgmime);
81
82   return res;
83 }
84
85 gchar **
86 _g_unix_content_type_get_parents (const gchar *type)
87 {
88   const gchar *umime;
89   gchar **parents;
90   GPtrArray *array;
91   int i;
92
93   array = g_ptr_array_new ();
94
95   G_LOCK (gio_xdgmime);
96
97   umime = xdg_mime_unalias_mime_type (type);
98
99   g_ptr_array_add (array, g_strdup (umime));
100
101   parents = xdg_mime_list_mime_parents (umime);
102   for (i = 0; parents && parents[i] != NULL; i++)
103     g_ptr_array_add (array, g_strdup (parents[i]));
104
105   free (parents);
106
107   G_UNLOCK (gio_xdgmime);
108
109   g_ptr_array_add (array, NULL);
110
111   return (gchar **)g_ptr_array_free (array, FALSE);
112 }
113
114 /**
115  * g_content_type_equals:
116  * @type1: a content type string
117  * @type2: a content type string
118  *
119  * Compares two content types for equality.
120  *
121  * Returns: %TRUE if the two strings are identical or equivalent,
122  *     %FALSE otherwise.
123  */
124 gboolean
125 g_content_type_equals (const gchar *type1,
126                        const gchar *type2)
127 {
128   gboolean res;
129
130   g_return_val_if_fail (type1 != NULL, FALSE);
131   g_return_val_if_fail (type2 != NULL, FALSE);
132
133   G_LOCK (gio_xdgmime);
134   res = xdg_mime_mime_type_equal (type1, type2);
135   G_UNLOCK (gio_xdgmime);
136
137   return res;
138 }
139
140 /**
141  * g_content_type_is_a:
142  * @type: a content type string
143  * @supertype: a content type string
144  *
145  * Determines if @type is a subset of @supertype.
146  *
147  * Returns: %TRUE if @type is a kind of @supertype,
148  *     %FALSE otherwise.
149  */
150 gboolean
151 g_content_type_is_a (const gchar *type,
152                      const gchar *supertype)
153 {
154   gboolean res;
155
156   g_return_val_if_fail (type != NULL, FALSE);
157   g_return_val_if_fail (supertype != NULL, FALSE);
158
159   G_LOCK (gio_xdgmime);
160   res = xdg_mime_mime_type_subclass (type, supertype);
161   G_UNLOCK (gio_xdgmime);
162
163   return res;
164 }
165
166 /**
167  * g_content_type_is_mime_type:
168  * @type: a content type string
169  * @mime_type: a mime type string
170  *
171  * Determines if @type is a subset of @mime_type.
172  * Convenience wrapper around g_content_type_is_a().
173  *
174  * Returns: %TRUE if @type is a kind of @mime_type,
175  *     %FALSE otherwise.
176  *
177  * Since: 2.52
178  */
179 gboolean
180 g_content_type_is_mime_type (const gchar *type,
181                              const gchar *mime_type)
182 {
183   return g_content_type_is_a (type, mime_type);
184 }
185
186 /**
187  * g_content_type_is_unknown:
188  * @type: a content type string
189  *
190  * Checks if the content type is the generic "unknown" type.
191  * On UNIX this is the "application/octet-stream" mimetype,
192  * while on win32 it is "*" and on OSX it is a dynamic type
193  * or octet-stream.
194  *
195  * Returns: %TRUE if the type is the unknown type.
196  */
197 gboolean
198 g_content_type_is_unknown (const gchar *type)
199 {
200   g_return_val_if_fail (type != NULL, FALSE);
201
202   return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
203 }
204
205
206 typedef enum {
207   MIME_TAG_TYPE_OTHER,
208   MIME_TAG_TYPE_COMMENT
209 } MimeTagType;
210
211 typedef struct {
212   int current_type;
213   int current_lang_level;
214   int comment_lang_level;
215   char *comment;
216 } MimeParser;
217
218
219 static int
220 language_level (const char *lang)
221 {
222   const char * const *lang_list;
223   int i;
224
225   /* The returned list is sorted from most desirable to least
226      desirable and always contains the default locale "C". */
227   lang_list = g_get_language_names ();
228
229   for (i = 0; lang_list[i]; i++)
230     if (strcmp (lang_list[i], lang) == 0)
231       return 1000-i;
232
233   return 0;
234 }
235
236 static void
237 mime_info_start_element (GMarkupParseContext  *context,
238                          const gchar          *element_name,
239                          const gchar         **attribute_names,
240                          const gchar         **attribute_values,
241                          gpointer              user_data,
242                          GError              **error)
243 {
244   int i;
245   const char *lang;
246   MimeParser *parser = user_data;
247
248   if (strcmp (element_name, "comment") == 0)
249     {
250       lang = "C";
251       for (i = 0; attribute_names[i]; i++)
252         if (strcmp (attribute_names[i], "xml:lang") == 0)
253           {
254             lang = attribute_values[i];
255             break;
256           }
257
258       parser->current_lang_level = language_level (lang);
259       parser->current_type = MIME_TAG_TYPE_COMMENT;
260     }
261   else
262     parser->current_type = MIME_TAG_TYPE_OTHER;
263 }
264
265 static void
266 mime_info_end_element (GMarkupParseContext  *context,
267                        const gchar          *element_name,
268                        gpointer              user_data,
269                        GError              **error)
270 {
271   MimeParser *parser = user_data;
272
273   parser->current_type = MIME_TAG_TYPE_OTHER;
274 }
275
276 static void
277 mime_info_text (GMarkupParseContext  *context,
278                 const gchar          *text,
279                 gsize                 text_len,
280                 gpointer              user_data,
281                 GError              **error)
282 {
283   MimeParser *parser = user_data;
284
285   if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
286       parser->current_lang_level > parser->comment_lang_level)
287     {
288       g_free (parser->comment);
289       parser->comment = g_strndup (text, text_len);
290       parser->comment_lang_level = parser->current_lang_level;
291     }
292 }
293
294 static char *
295 load_comment_for_mime_helper (const char *dir,
296                               const char *basename)
297 {
298   GMarkupParseContext *context;
299   char *filename, *data;
300   gsize len;
301   gboolean res;
302   MimeParser parse_data = {0};
303   GMarkupParser parser = {
304     mime_info_start_element,
305     mime_info_end_element,
306     mime_info_text
307   };
308
309   filename = g_build_filename (dir, "mime", basename, NULL);
310
311   res = g_file_get_contents (filename,  &data,  &len,  NULL);
312   g_free (filename);
313   if (!res)
314     return NULL;
315
316   context = g_markup_parse_context_new   (&parser, 0, &parse_data, NULL);
317   res = g_markup_parse_context_parse (context, data, len, NULL);
318   g_free (data);
319   g_markup_parse_context_free (context);
320
321   if (!res)
322     return NULL;
323
324   return parse_data.comment;
325 }
326
327
328 static char *
329 load_comment_for_mime (const char *mimetype)
330 {
331   const char * const* dirs;
332   char *basename;
333   char *comment;
334   int i;
335
336   basename = g_strdup_printf ("%s.xml", mimetype);
337
338   comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
339   if (comment)
340     {
341       g_free (basename);
342       return comment;
343     }
344
345   dirs = g_get_system_data_dirs ();
346
347   for (i = 0; dirs[i] != NULL; i++)
348     {
349       comment = load_comment_for_mime_helper (dirs[i], basename);
350       if (comment)
351         {
352           g_free (basename);
353           return comment;
354         }
355     }
356   g_free (basename);
357
358   return g_strdup_printf (_("%s type"), mimetype);
359 }
360
361 /**
362  * g_content_type_get_description:
363  * @type: a content type string
364  *
365  * Gets the human readable description of the content type.
366  *
367  * Returns: a short description of the content type @type. Free the
368  *     returned string with g_free()
369  */
370 gchar *
371 g_content_type_get_description (const gchar *type)
372 {
373   static GHashTable *type_comment_cache = NULL;
374   gchar *comment;
375
376   g_return_val_if_fail (type != NULL, NULL);
377
378   G_LOCK (gio_xdgmime);
379   type = xdg_mime_unalias_mime_type (type);
380
381   if (type_comment_cache == NULL)
382     type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
383
384   comment = g_hash_table_lookup (type_comment_cache, type);
385   comment = g_strdup (comment);
386   G_UNLOCK (gio_xdgmime);
387
388   if (comment != NULL)
389     return comment;
390
391   comment = load_comment_for_mime (type);
392
393   G_LOCK (gio_xdgmime);
394   g_hash_table_insert (type_comment_cache,
395                        g_strdup (type),
396                        g_strdup (comment));
397   G_UNLOCK (gio_xdgmime);
398
399   return comment;
400 }
401
402 /**
403  * g_content_type_get_mime_type:
404  * @type: a content type string
405  *
406  * Gets the mime type for the content type, if one is registered.
407  *
408  * Returns: (nullable): the registered mime type for the given @type,
409  *     or %NULL if unknown.
410  */
411 char *
412 g_content_type_get_mime_type (const char *type)
413 {
414   g_return_val_if_fail (type != NULL, NULL);
415
416   return g_strdup (type);
417 }
418
419 static GIcon *
420 g_content_type_get_icon_internal (const gchar *type,
421                                   gboolean     symbolic)
422 {
423   char *mimetype_icon;
424   char *generic_mimetype_icon = NULL;
425   char *q;
426   char *icon_names[6];
427   int n = 0;
428   GIcon *themed_icon;
429   const char  *xdg_icon;
430   int i;
431
432   g_return_val_if_fail (type != NULL, NULL);
433
434   G_LOCK (gio_xdgmime);
435   xdg_icon = xdg_mime_get_icon (type);
436   G_UNLOCK (gio_xdgmime);
437
438   if (xdg_icon)
439     icon_names[n++] = g_strdup (xdg_icon);
440
441   mimetype_icon = g_strdup (type);
442   while ((q = strchr (mimetype_icon, '/')) != NULL)
443     *q = '-';
444
445   icon_names[n++] = mimetype_icon;
446
447   generic_mimetype_icon = g_content_type_get_generic_icon_name (type);
448   if (generic_mimetype_icon)
449     icon_names[n++] = generic_mimetype_icon;
450
451   if (symbolic)
452     {
453       for (i = 0; i < n; i++)
454         {
455           icon_names[n + i] = icon_names[i];
456           icon_names[i] = g_strconcat (icon_names[i], "-symbolic", NULL);
457         }
458
459       n += n;
460     }
461
462   themed_icon = g_themed_icon_new_from_names (icon_names, n);
463
464   for (i = 0; i < n; i++)
465     g_free (icon_names[i]);
466
467   return themed_icon;
468 }
469
470 /**
471  * g_content_type_get_icon:
472  * @type: a content type string
473  *
474  * Gets the icon for a content type.
475  *
476  * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
477  *     object with g_object_unref()
478  */
479 GIcon *
480 g_content_type_get_icon (const gchar *type)
481 {
482   return g_content_type_get_icon_internal (type, FALSE);
483 }
484
485 /**
486  * g_content_type_get_symbolic_icon:
487  * @type: a content type string
488  *
489  * Gets the symbolic icon for a content type.
490  *
491  * Returns: (transfer full): symbolic #GIcon corresponding to the content type.
492  *     Free the returned object with g_object_unref()
493  *
494  * Since: 2.34
495  */
496 GIcon *
497 g_content_type_get_symbolic_icon (const gchar *type)
498 {
499   return g_content_type_get_icon_internal (type, TRUE);
500 }
501
502 /**
503  * g_content_type_get_generic_icon_name:
504  * @type: a content type string
505  *
506  * Gets the generic icon name for a content type.
507  *
508  * See the
509  * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
510  * specification for more on the generic icon name.
511  *
512  * Returns: (nullable): the registered generic icon name for the given @type,
513  *     or %NULL if unknown. Free with g_free()
514  *
515  * Since: 2.34
516  */
517 gchar *
518 g_content_type_get_generic_icon_name (const gchar *type)
519 {
520   const gchar *xdg_icon_name;
521   gchar *icon_name;
522
523   G_LOCK (gio_xdgmime);
524   xdg_icon_name = xdg_mime_get_generic_icon (type);
525   G_UNLOCK (gio_xdgmime);
526
527   if (!xdg_icon_name)
528     {
529       const char *p;
530       const char *suffix = "-x-generic";
531
532       p = strchr (type, '/');
533       if (p == NULL)
534         p = type + strlen (type);
535
536       icon_name = g_malloc (p - type + strlen (suffix) + 1);
537       memcpy (icon_name, type, p - type);
538       memcpy (icon_name + (p - type), suffix, strlen (suffix));
539       icon_name[(p - type) + strlen (suffix)] = 0;
540     }
541   else
542     {
543       icon_name = g_strdup (xdg_icon_name);
544     }
545
546   return icon_name;
547 }
548
549 /**
550  * g_content_type_can_be_executable:
551  * @type: a content type string
552  *
553  * Checks if a content type can be executable. Note that for instance
554  * things like text files can be executables (i.e. scripts and batch files).
555  *
556  * Returns: %TRUE if the file type corresponds to a type that
557  *     can be executable, %FALSE otherwise.
558  */
559 gboolean
560 g_content_type_can_be_executable (const gchar *type)
561 {
562   g_return_val_if_fail (type != NULL, FALSE);
563
564   if (g_content_type_is_a (type, "application/x-executable")  ||
565       g_content_type_is_a (type, "text/plain"))
566     return TRUE;
567
568   return FALSE;
569 }
570
571 static gboolean
572 looks_like_text (const guchar *data, gsize data_size)
573 {
574   gsize i;
575   char c;
576
577   for (i = 0; i < data_size; i++)
578     {
579       c = data[i];
580
581       if (g_ascii_iscntrl (c) &&
582           !g_ascii_isspace (c) &&
583           c != '\b')
584         return FALSE;
585     }
586   return TRUE;
587 }
588
589 /**
590  * g_content_type_from_mime_type:
591  * @mime_type: a mime type string
592  *
593  * Tries to find a content type based on the mime type name.
594  *
595  * Returns: (nullable): Newly allocated string with content type or
596  *     %NULL. Free with g_free()
597  *
598  * Since: 2.18
599  **/
600 gchar *
601 g_content_type_from_mime_type (const gchar *mime_type)
602 {
603   char *umime;
604
605   g_return_val_if_fail (mime_type != NULL, NULL);
606
607   G_LOCK (gio_xdgmime);
608   /* mime type and content type are same on unixes */
609   umime = g_strdup (xdg_mime_unalias_mime_type (mime_type));
610   G_UNLOCK (gio_xdgmime);
611
612   return umime;
613 }
614
615 /**
616  * g_content_type_guess:
617  * @filename: (nullable): a string, or %NULL
618  * @data: (nullable) (array length=data_size): a stream of data, or %NULL
619  * @data_size: the size of @data
620  * @result_uncertain: (out) (optional): return location for the certainty
621  *     of the result, or %NULL
622  *
623  * Guesses the content type based on example data. If the function is
624  * uncertain, @result_uncertain will be set to %TRUE. Either @filename
625  * or @data may be %NULL, in which case the guess will be based solely
626  * on the other argument.
627  *
628  * Returns: a string indicating a guessed content type for the
629  *     given data. Free with g_free()
630  */
631 gchar *
632 g_content_type_guess (const gchar  *filename,
633                       const guchar *data,
634                       gsize         data_size,
635                       gboolean     *result_uncertain)
636 {
637   char *basename;
638   const char *name_mimetypes[10], *sniffed_mimetype;
639   char *mimetype;
640   int i;
641   int n_name_mimetypes;
642   int sniffed_prio;
643
644   sniffed_prio = 0;
645   n_name_mimetypes = 0;
646   sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
647
648   if (result_uncertain)
649     *result_uncertain = FALSE;
650
651   /* our test suite and potentially other code used -1 in the past, which is
652    * not documented and not allowed; guard against that */
653   g_return_val_if_fail (data_size != (gsize) -1, g_strdup (XDG_MIME_TYPE_UNKNOWN));
654
655   G_LOCK (gio_xdgmime);
656
657   if (filename)
658     {
659       i = strlen (filename);
660       if (filename[i - 1] == '/')
661         {
662           name_mimetypes[0] = "inode/directory";
663           name_mimetypes[1] = NULL;
664           n_name_mimetypes = 1;
665           if (result_uncertain)
666             *result_uncertain = TRUE;
667         }
668       else
669         {
670           basename = g_path_get_basename (filename);
671           n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
672           g_free (basename);
673         }
674     }
675
676   /* Got an extension match, and no conflicts. This is it. */
677   if (n_name_mimetypes == 1)
678     {
679       gchar *s = g_strdup (name_mimetypes[0]);
680       G_UNLOCK (gio_xdgmime);
681       return s;
682     }
683
684   if (data)
685     {
686       sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
687       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
688           data &&
689           looks_like_text (data, data_size))
690         sniffed_mimetype = "text/plain";
691
692       /* For security reasons we don't ever want to sniff desktop files
693        * where we know the filename and it doesn't have a .desktop extension.
694        * This is because desktop files allow executing any application and
695        * we don't want to make it possible to hide them looking like something
696        * else.
697        */
698       if (filename != NULL &&
699           strcmp (sniffed_mimetype, "application/x-desktop") == 0)
700         sniffed_mimetype = "text/plain";
701     }
702
703   if (n_name_mimetypes == 0)
704     {
705       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
706           result_uncertain)
707         *result_uncertain = TRUE;
708
709       mimetype = g_strdup (sniffed_mimetype);
710     }
711   else
712     {
713       mimetype = NULL;
714       if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
715         {
716           if (sniffed_prio >= 80) /* High priority sniffing match, use that */
717             mimetype = g_strdup (sniffed_mimetype);
718           else
719             {
720               /* There are conflicts between the name matches and we
721                * have a sniffed type, use that as a tie breaker.
722                */
723               for (i = 0; i < n_name_mimetypes; i++)
724                 {
725                   if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
726                     {
727                       /* This nametype match is derived from (or the same as)
728                        * the sniffed type). This is probably it.
729                        */
730                       mimetype = g_strdup (name_mimetypes[i]);
731                       break;
732                     }
733                 }
734             }
735         }
736
737       if (mimetype == NULL)
738         {
739           /* Conflicts, and sniffed type was no help or not there.
740            * Guess on the first one
741            */
742           mimetype = g_strdup (name_mimetypes[0]);
743           if (result_uncertain)
744             *result_uncertain = TRUE;
745         }
746     }
747
748   G_UNLOCK (gio_xdgmime);
749
750   return mimetype;
751 }
752
753 static void
754 enumerate_mimetypes_subdir (const char *dir,
755                             const char *prefix,
756                             GHashTable *mimetypes)
757 {
758   DIR *d;
759   struct dirent *ent;
760   char *mimetype;
761
762   d = opendir (dir);
763   if (d)
764     {
765       while ((ent = readdir (d)) != NULL)
766         {
767           if (g_str_has_suffix (ent->d_name, ".xml"))
768             {
769               mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
770               g_hash_table_replace (mimetypes, mimetype, NULL);
771             }
772         }
773       closedir (d);
774     }
775 }
776
777 static void
778 enumerate_mimetypes_dir (const char *dir,
779                          GHashTable *mimetypes)
780 {
781   DIR *d;
782   struct dirent *ent;
783   char *mimedir;
784   char *name;
785
786   mimedir = g_build_filename (dir, "mime", NULL);
787
788   d = opendir (mimedir);
789   if (d)
790     {
791       while ((ent = readdir (d)) != NULL)
792         {
793           if (strcmp (ent->d_name, "packages") != 0)
794             {
795               name = g_build_filename (mimedir, ent->d_name, NULL);
796               if (g_file_test (name, G_FILE_TEST_IS_DIR))
797                 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
798               g_free (name);
799             }
800         }
801       closedir (d);
802     }
803
804   g_free (mimedir);
805 }
806
807 /**
808  * g_content_types_get_registered:
809  *
810  * Gets a list of strings containing all the registered content types
811  * known to the system. The list and its data should be freed using
812  * g_list_free_full (list, g_free).
813  *
814  * Returns: (element-type utf8) (transfer full): list of the registered
815  *     content types
816  */
817 GList *
818 g_content_types_get_registered (void)
819 {
820   const char * const* dirs;
821   GHashTable *mimetypes;
822   GHashTableIter iter;
823   gpointer key;
824   int i;
825   GList *l;
826
827   mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
828
829   enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
830   dirs = g_get_system_data_dirs ();
831
832   for (i = 0; dirs[i] != NULL; i++)
833     enumerate_mimetypes_dir (dirs[i], mimetypes);
834
835   l = NULL;
836   g_hash_table_iter_init (&iter, mimetypes);
837   while (g_hash_table_iter_next (&iter, &key, NULL))
838     {
839       l = g_list_prepend (l, key);
840       g_hash_table_iter_steal (&iter);
841     }
842
843   g_hash_table_destroy (mimetypes);
844
845   return l;
846 }
847
848
849 /* tree magic data */
850 static GList *tree_matches = NULL;
851 static gboolean need_reload = FALSE;
852
853 G_LOCK_DEFINE_STATIC (gio_treemagic);
854
855 typedef struct
856 {
857   gchar *path;
858   GFileType type;
859   guint match_case : 1;
860   guint executable : 1;
861   guint non_empty  : 1;
862   guint on_disc    : 1;
863   gchar *mimetype;
864   GList *matches;
865 } TreeMatchlet;
866
867 typedef struct
868 {
869   gchar *contenttype;
870   gint priority;
871   GList *matches;
872 } TreeMatch;
873
874
875 static void
876 tree_matchlet_free (TreeMatchlet *matchlet)
877 {
878   g_list_free_full (matchlet->matches, (GDestroyNotify) tree_matchlet_free);
879   g_free (matchlet->path);
880   g_free (matchlet->mimetype);
881   g_slice_free (TreeMatchlet, matchlet);
882 }
883
884 static void
885 tree_match_free (TreeMatch *match)
886 {
887   g_list_free_full (match->matches, (GDestroyNotify) tree_matchlet_free);
888   g_free (match->contenttype);
889   g_slice_free (TreeMatch, match);
890 }
891
892 static TreeMatch *
893 parse_header (gchar *line)
894 {
895   gint len;
896   gchar *s;
897   TreeMatch *match;
898
899   len = strlen (line);
900
901   if (line[0] != '[' || line[len - 1] != ']')
902     return NULL;
903
904   line[len - 1] = 0;
905   s = strchr (line, ':');
906
907   match = g_slice_new0 (TreeMatch);
908   match->priority = atoi (line + 1);
909   match->contenttype = g_strdup (s + 1);
910
911   return match;
912 }
913
914 static TreeMatchlet *
915 parse_match_line (gchar *line,
916                   gint  *depth)
917 {
918   gchar *s, *p;
919   TreeMatchlet *matchlet;
920   gchar **parts;
921   gint i;
922
923   matchlet = g_slice_new0 (TreeMatchlet);
924
925   if (line[0] == '>')
926     {
927       *depth = 0;
928       s = line;
929     }
930   else
931     {
932       *depth = atoi (line);
933       s = strchr (line, '>');
934     }
935   s += 2;
936   p = strchr (s, '"');
937   *p = 0;
938
939   matchlet->path = g_strdup (s);
940   s = p + 1;
941   parts = g_strsplit (s, ",", 0);
942   if (strcmp (parts[0], "=file") == 0)
943     matchlet->type = G_FILE_TYPE_REGULAR;
944   else if (strcmp (parts[0], "=directory") == 0)
945     matchlet->type = G_FILE_TYPE_DIRECTORY;
946   else if (strcmp (parts[0], "=link") == 0)
947     matchlet->type = G_FILE_TYPE_SYMBOLIC_LINK;
948   else
949     matchlet->type = G_FILE_TYPE_UNKNOWN;
950   for (i = 1; parts[i]; i++)
951     {
952       if (strcmp (parts[i], "executable") == 0)
953         matchlet->executable = 1;
954       else if (strcmp (parts[i], "match-case") == 0)
955         matchlet->match_case = 1;
956       else if (strcmp (parts[i], "non-empty") == 0)
957         matchlet->non_empty = 1;
958       else if (strcmp (parts[i], "on-disc") == 0)
959         matchlet->on_disc = 1;
960       else
961         matchlet->mimetype = g_strdup (parts[i]);
962     }
963
964   g_strfreev (parts);
965
966   return matchlet;
967 }
968
969 static gint
970 cmp_match (gconstpointer a, gconstpointer b)
971 {
972   const TreeMatch *aa = (const TreeMatch *)a;
973   const TreeMatch *bb = (const TreeMatch *)b;
974
975   return bb->priority - aa->priority;
976 }
977
978 static void
979 insert_match (TreeMatch *match)
980 {
981   tree_matches = g_list_insert_sorted (tree_matches, match, cmp_match);
982 }
983
984 static void
985 insert_matchlet (TreeMatch    *match,
986                  TreeMatchlet *matchlet,
987                  gint          depth)
988 {
989   if (depth == 0)
990     match->matches = g_list_append (match->matches, matchlet);
991   else
992     {
993       GList *last;
994       TreeMatchlet *m;
995
996       last = g_list_last (match->matches);
997       if (!last)
998         {
999           tree_matchlet_free (matchlet);
1000           g_warning ("can't insert tree matchlet at depth %d", depth);
1001           return;
1002         }
1003
1004       m = (TreeMatchlet *) last->data;
1005       while (--depth > 0)
1006         {
1007           last = g_list_last (m->matches);
1008           if (!last)
1009             {
1010               tree_matchlet_free (matchlet);
1011               g_warning ("can't insert tree matchlet at depth %d", depth);
1012               return;
1013             }
1014
1015           m = (TreeMatchlet *) last->data;
1016         }
1017       m->matches = g_list_append (m->matches, matchlet);
1018     }
1019 }
1020
1021 static void
1022 read_tree_magic_from_directory (const gchar *prefix)
1023 {
1024   gchar *filename;
1025   gchar *text;
1026   gsize len;
1027   gchar **lines;
1028   gint i;
1029   TreeMatch *match;
1030   TreeMatchlet *matchlet;
1031   gint depth;
1032
1033   filename = g_build_filename (prefix, "mime", "treemagic", NULL);
1034
1035   if (g_file_get_contents (filename, &text, &len, NULL))
1036     {
1037       if (strcmp (text, "MIME-TreeMagic") == 0)
1038         {
1039           lines = g_strsplit (text + strlen ("MIME-TreeMagic") + 2, "\n", 0);
1040           match = NULL;
1041           for (i = 0; lines[i] && lines[i][0]; i++)
1042             {
1043               if (lines[i][0] == '[')
1044                 {
1045                   match = parse_header (lines[i]);
1046                   insert_match (match);
1047                 }
1048               else if (match != NULL)
1049                 {
1050                   matchlet = parse_match_line (lines[i], &depth);
1051                   insert_matchlet (match, matchlet, depth);
1052                 }
1053               else
1054                 {
1055                   g_warning ("%s: header corrupt; skipping\n", filename);
1056                   break;
1057                 }
1058             }
1059
1060           g_strfreev (lines);
1061         }
1062       else
1063         g_warning ("%s: header not found, skipping\n", filename);
1064
1065       g_free (text);
1066     }
1067
1068   g_free (filename);
1069 }
1070
1071
1072 static void
1073 xdg_mime_reload (void *user_data)
1074 {
1075   need_reload = TRUE;
1076 }
1077
1078 static void
1079 tree_magic_shutdown (void)
1080 {
1081   g_list_free_full (tree_matches, (GDestroyNotify) tree_match_free);
1082   tree_matches = NULL;
1083 }
1084
1085 static void
1086 tree_magic_init (void)
1087 {
1088   static gboolean initialized = FALSE;
1089   const gchar *dir;
1090   const gchar * const * dirs;
1091   int i;
1092
1093   if (!initialized)
1094     {
1095       initialized = TRUE;
1096
1097       xdg_mime_register_reload_callback (xdg_mime_reload, NULL, NULL);
1098       need_reload = TRUE;
1099     }
1100
1101   if (need_reload)
1102     {
1103       need_reload = FALSE;
1104
1105       tree_magic_shutdown ();
1106
1107       dir = g_get_user_data_dir ();
1108       read_tree_magic_from_directory (dir);
1109       dirs = g_get_system_data_dirs ();
1110       for (i = 0; dirs[i]; i++)
1111         read_tree_magic_from_directory (dirs[i]);
1112     }
1113 }
1114
1115 /* a filtering enumerator */
1116
1117 typedef struct
1118 {
1119   gchar *path;
1120   gint depth;
1121   gboolean ignore_case;
1122   gchar **components;
1123   gchar **case_components;
1124   GFileEnumerator **enumerators;
1125   GFile **children;
1126 } Enumerator;
1127
1128 static gboolean
1129 component_match (Enumerator  *e,
1130                  gint         depth,
1131                  const gchar *name)
1132 {
1133   gchar *case_folded, *key;
1134   gboolean found;
1135
1136   if (strcmp (name, e->components[depth]) == 0)
1137     return TRUE;
1138
1139   if (!e->ignore_case)
1140     return FALSE;
1141
1142   case_folded = g_utf8_casefold (name, -1);
1143   key = g_utf8_collate_key (case_folded, -1);
1144
1145   found = strcmp (key, e->case_components[depth]) == 0;
1146
1147   g_free (case_folded);
1148   g_free (key);
1149
1150   return found;
1151 }
1152
1153 static GFile *
1154 next_match_recurse (Enumerator *e,
1155                     gint        depth)
1156 {
1157   GFile *file;
1158   GFileInfo *info;
1159   const gchar *name;
1160
1161   while (TRUE)
1162     {
1163       if (e->enumerators[depth] == NULL)
1164         {
1165           if (depth > 0)
1166             {
1167               file = next_match_recurse (e, depth - 1);
1168               if (file)
1169                 {
1170                   e->children[depth] = file;
1171                   e->enumerators[depth] = g_file_enumerate_children (file,
1172                                                                      G_FILE_ATTRIBUTE_STANDARD_NAME,
1173                                                                      G_FILE_QUERY_INFO_NONE,
1174                                                                      NULL,
1175                                                                      NULL);
1176                 }
1177             }
1178           if (e->enumerators[depth] == NULL)
1179             return NULL;
1180         }
1181
1182       while ((info = g_file_enumerator_next_file (e->enumerators[depth], NULL, NULL)))
1183         {
1184           name = g_file_info_get_name (info);
1185           if (component_match (e, depth, name))
1186             {
1187               file = g_file_get_child (e->children[depth], name);
1188               g_object_unref (info);
1189               return file;
1190             }
1191           g_object_unref (info);
1192         }
1193
1194       g_object_unref (e->enumerators[depth]);
1195       e->enumerators[depth] = NULL;
1196       g_object_unref (e->children[depth]);
1197       e->children[depth] = NULL;
1198     }
1199 }
1200
1201 static GFile *
1202 enumerator_next (Enumerator *e)
1203 {
1204   return next_match_recurse (e, e->depth - 1);
1205 }
1206
1207 static Enumerator *
1208 enumerator_new (GFile      *root,
1209                 const char *path,
1210                 gboolean    ignore_case)
1211 {
1212   Enumerator *e;
1213   gint i;
1214   gchar *case_folded;
1215
1216   e = g_new0 (Enumerator, 1);
1217   e->path = g_strdup (path);
1218   e->ignore_case = ignore_case;
1219
1220   e->components = g_strsplit (e->path, G_DIR_SEPARATOR_S, -1);
1221   e->depth = g_strv_length (e->components);
1222   if (e->ignore_case)
1223     {
1224       e->case_components = g_new0 (char *, e->depth + 1);
1225       for (i = 0; e->components[i]; i++)
1226         {
1227           case_folded = g_utf8_casefold (e->components[i], -1);
1228           e->case_components[i] = g_utf8_collate_key (case_folded, -1);
1229           g_free (case_folded);
1230         }
1231     }
1232
1233   e->children = g_new0 (GFile *, e->depth);
1234   e->children[0] = g_object_ref (root);
1235   e->enumerators = g_new0 (GFileEnumerator *, e->depth);
1236   e->enumerators[0] = g_file_enumerate_children (root,
1237                                                  G_FILE_ATTRIBUTE_STANDARD_NAME,
1238                                                  G_FILE_QUERY_INFO_NONE,
1239                                                  NULL,
1240                                                  NULL);
1241
1242   return e;
1243 }
1244
1245 static void
1246 enumerator_free (Enumerator *e)
1247 {
1248   gint i;
1249
1250   for (i = 0; i < e->depth; i++)
1251     {
1252       if (e->enumerators[i])
1253         g_object_unref (e->enumerators[i]);
1254       if (e->children[i])
1255         g_object_unref (e->children[i]);
1256     }
1257
1258   g_free (e->enumerators);
1259   g_free (e->children);
1260   g_strfreev (e->components);
1261   if (e->case_components)
1262     g_strfreev (e->case_components);
1263   g_free (e->path);
1264   g_free (e);
1265 }
1266
1267 static gboolean
1268 matchlet_match (TreeMatchlet *matchlet,
1269                 GFile        *root)
1270 {
1271   GFile *file;
1272   GFileInfo *info;
1273   gboolean result;
1274   const gchar *attrs;
1275   Enumerator *e;
1276   GList *l;
1277
1278   e = enumerator_new (root, matchlet->path, !matchlet->match_case);
1279
1280   do
1281     {
1282       file = enumerator_next (e);
1283       if (!file)
1284         {
1285           enumerator_free (e);
1286           return FALSE;
1287         }
1288
1289       if (matchlet->mimetype)
1290         attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1291                 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ","
1292                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
1293       else
1294         attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1295                 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE;
1296       info = g_file_query_info (file,
1297                                 attrs,
1298                                 G_FILE_QUERY_INFO_NONE,
1299                                 NULL,
1300                                 NULL);
1301       if (info)
1302         {
1303           result = TRUE;
1304
1305           if (matchlet->type != G_FILE_TYPE_UNKNOWN &&
1306               g_file_info_get_file_type (info) != matchlet->type)
1307             result = FALSE;
1308
1309           if (matchlet->executable &&
1310               !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
1311             result = FALSE;
1312         }
1313       else
1314         result = FALSE;
1315
1316       if (result && matchlet->non_empty)
1317         {
1318           GFileEnumerator *child_enum;
1319           GFileInfo *child_info;
1320
1321           child_enum = g_file_enumerate_children (file,
1322                                                   G_FILE_ATTRIBUTE_STANDARD_NAME,
1323                                                   G_FILE_QUERY_INFO_NONE,
1324                                                   NULL,
1325                                                   NULL);
1326
1327           if (child_enum)
1328             {
1329               child_info = g_file_enumerator_next_file (child_enum, NULL, NULL);
1330               if (child_info)
1331                 g_object_unref (child_info);
1332               else
1333                 result = FALSE;
1334               g_object_unref (child_enum);
1335             }
1336           else
1337             result = FALSE;
1338         }
1339
1340       if (result && matchlet->mimetype)
1341         {
1342           if (strcmp (matchlet->mimetype, g_file_info_get_content_type (info)) != 0)
1343             result = FALSE;
1344         }
1345
1346       if (info)
1347         g_object_unref (info);
1348       g_object_unref (file);
1349     }
1350   while (!result);
1351
1352   enumerator_free (e);
1353
1354   if (!matchlet->matches)
1355     return TRUE;
1356
1357   for (l = matchlet->matches; l; l = l->next)
1358     {
1359       TreeMatchlet *submatchlet;
1360
1361       submatchlet = l->data;
1362       if (matchlet_match (submatchlet, root))
1363         return TRUE;
1364     }
1365
1366   return FALSE;
1367 }
1368
1369 static void
1370 match_match (TreeMatch    *match,
1371              GFile        *root,
1372              GPtrArray    *types)
1373 {
1374   GList *l;
1375
1376   for (l = match->matches; l; l = l->next)
1377     {
1378       TreeMatchlet *matchlet = l->data;
1379       if (matchlet_match (matchlet, root))
1380         {
1381           g_ptr_array_add (types, g_strdup (match->contenttype));
1382           break;
1383         }
1384     }
1385 }
1386
1387 /**
1388  * g_content_type_guess_for_tree:
1389  * @root: the root of the tree to guess a type for
1390  *
1391  * Tries to guess the type of the tree with root @root, by
1392  * looking at the files it contains. The result is an array
1393  * of content types, with the best guess coming first.
1394  *
1395  * The types returned all have the form x-content/foo, e.g.
1396  * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
1397  * (for a camera memory card). See the
1398  * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
1399  * specification for more on x-content types.
1400  *
1401  * This function is useful in the implementation of
1402  * g_mount_guess_content_type().
1403  *
1404  * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
1405  *     array of zero or more content types. Free with g_strfreev()
1406  *
1407  * Since: 2.18
1408  */
1409 gchar **
1410 g_content_type_guess_for_tree (GFile *root)
1411 {
1412   GPtrArray *types;
1413   GList *l;
1414
1415   types = g_ptr_array_new ();
1416
1417   G_LOCK (gio_treemagic);
1418
1419   tree_magic_init ();
1420   for (l = tree_matches; l; l = l->next)
1421     {
1422       TreeMatch *match = l->data;
1423       match_match (match, root, types);
1424     }
1425
1426   G_UNLOCK (gio_treemagic);
1427
1428   g_ptr_array_add (types, NULL);
1429
1430   return (gchar **)g_ptr_array_free (types, FALSE);
1431 }