Add TODO comment.
[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 <string.h>
28 #include <stdio.h>
29 #include "gcontenttypeprivate.h"
30 #include "gthemedicon.h"
31 #include "glibintl.h"
32
33 #include "gioalias.h"
34
35 /**
36  * SECTION:gcontenttype
37  * @short_description: Platform-specific content typing
38  * @include: gio/gio.h
39  *
40  * A content type is a platform specific string that defines the type
41  * of a file. On unix it is a mime type, on win32 it is an extension string
42  * like ".doc", ".txt" or a percieved string like "audio". Such strings
43  * can be looked up in the registry at HKEY_CLASSES_ROOT.
44  **/
45
46 #ifdef G_OS_WIN32
47
48 #include <windows.h>
49
50 static char *
51 get_registry_classes_key (const char    *subdir,
52                           const wchar_t *key_name)
53 {
54   wchar_t *wc_key;
55   HKEY reg_key = NULL;
56   DWORD key_type;
57   DWORD nbytes;
58   char *value_utf8;
59
60   value_utf8 = NULL;
61   
62   nbytes = 0;
63   wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
64   if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
65                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS &&
66       RegQueryValueExW (reg_key, key_name, 0,
67                         &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
68       key_type == REG_SZ)
69     {
70       wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
71       RegQueryValueExW (reg_key, key_name, 0,
72                         &key_type, (LPBYTE) wc_temp, &nbytes);
73       wc_temp[nbytes/2] = '\0';
74       value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
75       g_free (wc_temp);
76     }
77   g_free (wc_key);
78   
79   if (reg_key != NULL)
80     RegCloseKey (reg_key);
81
82   return value_utf8;
83 }
84
85 gboolean
86 g_content_type_equals (const char *type1,
87                        const char *type2)
88 {
89   char *progid1, *progid2;
90   gboolean res;
91   
92   g_return_val_if_fail (type1 != NULL, FALSE);
93   g_return_val_if_fail (type2 != NULL, FALSE);
94
95   if (g_ascii_strcasecmp (type1, type2) == 0)
96     return TRUE;
97
98   res = FALSE;
99   progid1 = get_registry_classes_key (type1, NULL);
100   progid2 = get_registry_classes_key (type2, NULL);
101   if (progid1 != NULL && progid2 != NULL &&
102       strcmp (progid1, progid2) == 0)
103     res = TRUE;
104   g_free (progid1);
105   g_free (progid2);
106   
107   return res;
108 }
109
110 gboolean
111 g_content_type_is_a (const char *type,
112                      const char *supertype)
113 {
114   gboolean res;
115   char *value_utf8;
116
117   g_return_val_if_fail (type != NULL, FALSE);
118   g_return_val_if_fail (supertype != NULL, FALSE);
119
120   if (g_content_type_equals (type, supertype))
121     return TRUE;
122
123   res = FALSE;
124   value_utf8 = get_registry_classes_key (type, L"PerceivedType");
125   if (value_utf8 && strcmp (value_utf8, supertype) == 0)
126     res = TRUE;
127   g_free (value_utf8);
128   
129   return res;
130 }
131
132 gboolean
133 g_content_type_is_unknown (const char *type)
134 {
135   g_return_val_if_fail (type != NULL, FALSE);
136
137   return strcmp ("*", type) == 0;
138 }
139
140 char *
141 g_content_type_get_description (const char *type)
142 {
143   char *progid;
144   char *description;
145
146   g_return_val_if_fail (type != NULL, NULL);
147
148   progid = get_registry_classes_key (type, NULL);
149   if (progid)
150     {
151       description = get_registry_classes_key (progid, NULL);
152       g_free (progid);
153
154       if (description)
155         return description;
156     }
157
158   if (g_content_type_is_unknown (type))
159     return g_strdup (_("Unknown type"));
160   return g_strdup_printf (_("%s filetype"), type);
161 }
162
163 char *
164 g_content_type_get_mime_type (const char *type)
165 {
166   char *mime;
167
168   g_return_val_if_fail (type != NULL, NULL);
169
170   mime = get_registry_classes_key (type, L"Content Type");
171   if (mime)
172     return mime;
173   else if (g_content_type_is_unknown (type))
174     return g_strdup ("application/octet-stream");
175   else if (*type == '.')
176     return g_strdup_printf ("application/x-ext-%s", type+1);
177   /* TODO: Map "image" to "image/ *", etc? */
178
179   return g_strdup ("application/octet-stream");
180 }
181
182 GIcon *
183 g_content_type_get_icon (const char *type)
184 {
185   g_return_val_if_fail (type != NULL, NULL);
186
187   /* TODO: How do we represent icons???
188      In the registry they are the default value of
189      HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
190      <type>: <value>
191      REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
192      REG_SZ: shimgvw.dll,3
193   */
194   return NULL;
195 }
196
197 gboolean
198 g_content_type_can_be_executable (const char *type)
199 {
200   g_return_val_if_fail (type != NULL, FALSE);
201
202   if (strcmp (type, ".exe") == 0 ||
203       strcmp (type, ".com") == 0 ||
204       strcmp (type, ".bat") == 0)
205     return TRUE;
206
207   /* TODO: Also look at PATHEXT, which lists the extensions for
208    * "scripts" in addition to those for true binary executables.
209    *
210    * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
211    * right now, for instance). And in a sense, all associated file
212    * types are "executable" on Windows... You can just type foo.jpg as
213    * a command name in cmd.exe, and it will run the application
214    * associated with .jpg. Hard to say what this API actually means
215    * with "executable".
216    */
217
218   return FALSE;
219 }
220
221 static gboolean
222 looks_like_text (const guchar *data, 
223                  gsize         data_size)
224 {
225   gsize i;
226   guchar c;
227   for (i = 0; i < data_size; i++)
228     {
229       c = data[i];
230       if (g_ascii_iscntrl (c) && !g_ascii_isspace (c))
231         return FALSE;
232     }
233   return TRUE;
234 }
235
236 char *
237 g_content_type_from_mime_type (const char *mime_type)
238 {
239   char *key, *content_type;
240
241   g_return_val_if_fail (mime_type != NULL, NULL);
242
243   key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
244   content_type = get_registry_classes_key (key, L"Extension");
245   g_free (key);
246
247   return content_type;
248 }
249
250 char *
251 g_content_type_guess (const char   *filename,
252                       const guchar *data,
253                       gsize         data_size,
254                       gboolean     *result_uncertain)
255 {
256   char *basename;
257   char *type;
258   char *dot;
259
260   type = NULL;
261
262   if (filename)
263     {
264       basename = g_path_get_basename (filename);
265       dot = strrchr (basename, '.');
266       if (dot)
267         type = g_strdup (dot);
268       g_free (basename);
269     }
270
271   if (type)
272     return type;
273
274   if (data && looks_like_text (data, data_size))
275     return g_strdup (".txt");
276
277   return g_strdup ("*");
278 }
279
280 GList *
281 g_content_types_get_registered (void)
282 {
283   DWORD index;
284   wchar_t keyname[256];
285   DWORD key_len;
286   char *key_utf8;
287   GList *types;
288
289   types = NULL;
290   index = 0;
291   key_len = 256;
292   while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
293                        index,
294                        keyname,
295                        &key_len,
296                        NULL,
297                        NULL,
298                        NULL,
299                        NULL) == ERROR_SUCCESS)
300     {
301       key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
302       if (key_utf8)
303         {
304           if (*key_utf8 == '.')
305             types = g_list_prepend (types, key_utf8);
306           else
307             g_free (key_utf8);
308         }
309       index++;
310       key_len = 256;
311     }
312   
313   return g_list_reverse (types);
314 }
315
316 #else /* !G_OS_WIN32 - Unix specific version */
317
318 #include <dirent.h>
319
320 #define XDG_PREFIX _gio_xdg
321 #include "xdgmime/xdgmime.h"
322
323 /* We lock this mutex whenever we modify global state in this module.  */
324 G_LOCK_DEFINE_STATIC (gio_xdgmime);
325
326 gsize
327 _g_unix_content_type_get_sniff_len (void)
328 {
329   gsize size;
330
331   G_LOCK (gio_xdgmime);
332   size = xdg_mime_get_max_buffer_extents ();
333   G_UNLOCK (gio_xdgmime);
334
335   return size;
336 }
337
338 char *
339 _g_unix_content_type_unalias (const char *type)
340 {
341   char *res;
342   
343   G_LOCK (gio_xdgmime);
344   res = g_strdup (xdg_mime_unalias_mime_type (type));
345   G_UNLOCK (gio_xdgmime);
346   
347   return res;
348 }
349
350 char **
351 _g_unix_content_type_get_parents (const char *type)
352 {
353   const char *umime;
354   char **parents;
355   GPtrArray *array;
356   int i;
357
358   array = g_ptr_array_new ();
359   
360   G_LOCK (gio_xdgmime);
361   
362   umime = xdg_mime_unalias_mime_type (type);
363   
364   g_ptr_array_add (array, g_strdup (umime));
365   
366   parents = xdg_mime_list_mime_parents (umime);
367   for (i = 0; parents && parents[i] != NULL; i++)
368     g_ptr_array_add (array, g_strdup (parents[i]));
369   
370   free (parents);
371   
372   G_UNLOCK (gio_xdgmime);
373   
374   g_ptr_array_add (array, NULL);
375   
376   return (char **)g_ptr_array_free (array, FALSE);
377 }
378
379 /**
380  * g_content_type_equals:
381  * @type1: a content type string.
382  * @type2: a content type string.
383  *
384  * Compares two content types for equality.
385  *
386  * Returns: %TRUE if the two strings are identical or equivalent,
387  * %FALSE otherwise.
388  **/  
389 gboolean
390 g_content_type_equals (const char *type1,
391                        const char *type2)
392 {
393   gboolean res;
394   
395   g_return_val_if_fail (type1 != NULL, FALSE);
396   g_return_val_if_fail (type2 != NULL, FALSE);
397   
398   G_LOCK (gio_xdgmime);
399   res = xdg_mime_mime_type_equal (type1, type2);
400   G_UNLOCK (gio_xdgmime);
401         
402   return res;
403 }
404
405 /**
406  * g_content_type_is_a:
407  * @type: a content type string. 
408  * @supertype: a string.
409  *
410  * Determines if @type is a subset of @supertype.  
411  *
412  * Returns: %TRUE if @type is a kind of @supertype,
413  * %FALSE otherwise. 
414  **/  
415 gboolean
416 g_content_type_is_a (const char *type,
417                      const char *supertype)
418 {
419   gboolean res;
420     
421   g_return_val_if_fail (type != NULL, FALSE);
422   g_return_val_if_fail (supertype != NULL, FALSE);
423   
424   G_LOCK (gio_xdgmime);
425   res = xdg_mime_mime_type_subclass (type, supertype);
426   G_UNLOCK (gio_xdgmime);
427         
428   return res;
429 }
430
431 /**
432  * g_content_type_is_unknown:
433  * @type: a content type string. 
434  * 
435  * Checks if the content type is the generic "unknown" type.
436  * On unix this is the "application/octet-stream" mimetype,
437  * while on win32 it is "*".
438  * 
439  * Returns: %TRUE if the type is the unknown type.
440  **/  
441 gboolean
442 g_content_type_is_unknown (const char *type)
443 {
444   g_return_val_if_fail (type != NULL, FALSE);
445
446   return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
447 }
448
449
450 typedef enum {
451   MIME_TAG_TYPE_OTHER,
452   MIME_TAG_TYPE_COMMENT
453 } MimeTagType;
454
455 typedef struct {
456   int current_type;
457   int current_lang_level;
458   int comment_lang_level;
459   char *comment;
460 } MimeParser;
461
462
463 static int
464 language_level (const char *lang)
465 {
466   const char * const *lang_list;
467   int i;
468   
469   /* The returned list is sorted from most desirable to least
470      desirable and always contains the default locale "C". */
471   lang_list = g_get_language_names ();
472   
473   for (i = 0; lang_list[i]; i++)
474     if (strcmp (lang_list[i], lang) == 0)
475       return 1000-i;
476   
477   return 0;
478 }
479
480 static void
481 mime_info_start_element (GMarkupParseContext  *context,
482                          const gchar          *element_name,
483                          const gchar         **attribute_names,
484                          const gchar         **attribute_values,
485                          gpointer              user_data,
486                          GError              **error)
487 {
488   int i;
489   const char *lang;
490   MimeParser *parser = user_data;
491   
492   if (strcmp (element_name, "comment") == 0)
493     {
494       lang = "C";
495       for (i = 0; attribute_names[i]; i++)
496         if (strcmp (attribute_names[i], "xml:lang") == 0)
497           {
498             lang = attribute_values[i];
499             break;
500           }
501       
502       parser->current_lang_level = language_level (lang);
503       parser->current_type = MIME_TAG_TYPE_COMMENT;
504     }
505   else
506     parser->current_type = MIME_TAG_TYPE_OTHER;
507   
508 }
509
510 static void
511 mime_info_end_element (GMarkupParseContext  *context,
512                        const gchar          *element_name,
513                        gpointer              user_data,
514                        GError              **error)
515 {
516   MimeParser *parser = user_data;
517   
518   parser->current_type = MIME_TAG_TYPE_OTHER;
519 }
520
521 static void
522 mime_info_text (GMarkupParseContext  *context,
523                 const gchar          *text,
524                 gsize                 text_len,  
525                 gpointer              user_data,
526                 GError              **error)
527 {
528   MimeParser *parser = user_data;
529
530   if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
531       parser->current_lang_level > parser->comment_lang_level)
532     {
533       g_free (parser->comment);
534       parser->comment = g_strndup (text, text_len);
535       parser->comment_lang_level = parser->current_lang_level;
536     }
537 }
538
539 static char *
540 load_comment_for_mime_helper (const char *dir, 
541                               const char *basename)
542 {
543   GMarkupParseContext *context;
544   char *filename, *data;
545   gsize len;
546   gboolean res;
547   MimeParser parse_data = {0};
548   GMarkupParser parser = {
549     mime_info_start_element,
550     mime_info_end_element,
551     mime_info_text
552   };
553
554   filename = g_build_filename (dir, "mime", basename, NULL);
555   
556   res = g_file_get_contents (filename,  &data,  &len,  NULL);
557   g_free (filename);
558   if (!res)
559     return NULL;
560   
561   context = g_markup_parse_context_new   (&parser, 0, &parse_data, NULL);
562   res = g_markup_parse_context_parse (context, data, len, NULL);
563   g_free (data);
564   g_markup_parse_context_free (context);
565   
566   if (!res)
567     return NULL;
568
569   return parse_data.comment;
570 }
571
572
573 static char *
574 load_comment_for_mime (const char *mimetype)
575 {
576   const char * const* dirs;
577   char *basename;
578   char *comment;
579   int i;
580
581   basename = g_strdup_printf ("%s.xml", mimetype);
582
583   comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
584   if (comment)
585     {
586       g_free (basename);
587       return comment;
588     }
589   
590   dirs = g_get_system_data_dirs ();
591
592   for (i = 0; dirs[i] != NULL; i++)
593     {
594       comment = load_comment_for_mime_helper (dirs[i], basename);
595       if (comment)
596         {
597           g_free (basename);
598           return comment;
599         }
600     }
601   g_free (basename);
602   
603   return g_strdup_printf (_("%s type"), mimetype);
604 }
605
606 /**
607  * g_content_type_get_description:
608  * @type: a content type string. 
609  * 
610  * Gets the human readable description of the content type.
611  * 
612  * Returns: a short description of the content type @type. 
613  **/  
614 char *
615 g_content_type_get_description (const char *type)
616 {
617   static GHashTable *type_comment_cache = NULL;
618   char *comment;
619
620   g_return_val_if_fail (type != NULL, NULL);
621   
622   G_LOCK (gio_xdgmime);
623   type = xdg_mime_unalias_mime_type (type);
624
625   if (type_comment_cache == NULL)
626     type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
627
628   comment = g_hash_table_lookup (type_comment_cache, type);
629   comment = g_strdup (comment);
630   G_UNLOCK (gio_xdgmime);
631   
632   if (comment != NULL)
633     return comment;
634
635   comment = load_comment_for_mime (type);
636   
637   G_LOCK (gio_xdgmime);
638   g_hash_table_insert (type_comment_cache,
639                        g_strdup (type),
640                        g_strdup (comment));
641   G_UNLOCK (gio_xdgmime);
642
643   return comment;
644 }
645
646 /**
647  * g_content_type_get_mime_type:
648  * @type: a content type string. 
649  * 
650  * Gets the mime-type for the content type. If one is registered
651  * 
652  * Returns: the registered mime-type for the given @type, or NULL if unknown.
653  **/  
654 char *
655 g_content_type_get_mime_type (const char *type)
656 {
657   g_return_val_if_fail (type != NULL, NULL);
658
659   return g_strdup (type);
660 }
661
662 /**
663  * g_content_type_get_icon:
664  * @type: a content type string.
665  * 
666  * Gets the icon for a content type.
667  * 
668  * Returns: #GIcon corresponding to the content type.
669  **/  
670 GIcon *
671 g_content_type_get_icon (const char *type)
672 {
673   char *mimetype_icon, *generic_mimetype_icon, *q;
674   char *xdg_mimetype_icon, *legacy_mimetype_icon;
675   char *icon_names[4];
676   int n;
677   const char *p;
678   GIcon *themed_icon;
679   
680   g_return_val_if_fail (type != NULL, NULL);
681   
682   G_LOCK (gio_xdgmime);
683   xdg_mimetype_icon = g_strdup (xdg_mime_get_icon (type));
684   G_UNLOCK (gio_xdgmime);
685
686   mimetype_icon = g_strdup (type);
687   
688   while ((q = strchr (mimetype_icon, '/')) != NULL)
689     *q = '-';
690   
691   p = strchr (type, '/');
692   if (p == NULL)
693     p = type + strlen (type);
694
695   /* Not all icons have migrated to the new icon theme spec, look for old names too */
696   legacy_mimetype_icon = g_strconcat ("gnome-mime-", mimetype_icon, NULL);
697   
698   generic_mimetype_icon = g_malloc (p - type + strlen ("-x-generic") + 1);
699   memcpy (generic_mimetype_icon, type, p - type);
700   memcpy (generic_mimetype_icon + (p - type), "-x-generic", strlen ("-x-generic"));
701   generic_mimetype_icon[(p - type) + strlen ("-x-generic")] = 0;
702   
703   if (xdg_mimetype_icon)
704     icon_names[n++] = xdg_mimetype_icon;
705
706   icon_names[n++] = mimetype_icon;
707   icon_names[n++] = legacy_mimetype_icon;
708   icon_names[n++] = generic_mimetype_icon;
709   
710   themed_icon = g_themed_icon_new_from_names (icon_names, n);
711   
712   g_free (xdg_mimetype_icon);
713   g_free (mimetype_icon);
714   g_free (legacy_mimetype_icon);
715   g_free (generic_mimetype_icon);
716   
717   return themed_icon;
718 }
719
720 /**
721  * g_content_type_can_be_executable:
722  * @type: a content type string.
723  * 
724  * Checks if a content type can be executable. Note that for instance
725  * things like text files can be executables (i.e. scripts and batch files).
726  * 
727  * Returns: %TRUE if the file type corresponds to a type that
728  * can be executable, %FALSE otherwise. 
729  **/  
730 gboolean
731 g_content_type_can_be_executable (const char *type)
732 {
733   g_return_val_if_fail (type != NULL, FALSE);
734
735   if (g_content_type_is_a (type, "application/x-executable")  ||
736       g_content_type_is_a (type, "text/plain"))
737     return TRUE;
738
739   return FALSE;
740 }
741
742 static gboolean
743 looks_like_text (const guchar *data, gsize data_size)
744 {
745   gsize i;
746   char c;
747   
748   for (i = 0; i < data_size; i++)
749     {
750       c = data[i];
751       
752       if (g_ascii_iscntrl (c) &&
753           !g_ascii_isspace (c))
754         return FALSE;
755     }
756   return TRUE;
757 }
758
759 /**
760  * g_content_type_from_mime_type:
761  * @mime_type: a mime type string.
762  *
763  * Tries to find a content type based on the mime type name.
764  *
765  * Returns: Newly allocated string with content type or NULL when does not know.
766  *
767  * Since: 2.18
768  **/
769 char *
770 g_content_type_from_mime_type (const char *mime_type)
771 {
772   g_return_val_if_fail (mime_type != NULL, NULL);
773
774   /* mime type and content type are same on unixes */
775   return g_strdup (mime_type);
776 }
777
778 /**
779  * g_content_type_guess:
780  * @filename: a string.
781  * @data: a stream of data.
782  * @data_size: the size of @data.
783  * @result_uncertain: a flag indicating the certainty of the 
784  * result.
785  * 
786  * Guesses the content type based on example data. If the function is 
787  * uncertain, @result_uncertain will be set to %TRUE.
788  * 
789  * Returns: a string indicating a guessed content type for the 
790  * given data. 
791  **/  
792 char *
793 g_content_type_guess (const char   *filename,
794                       const guchar *data,
795                       gsize         data_size,
796                       gboolean     *result_uncertain)
797 {
798   char *basename;
799   const char *name_mimetypes[10], *sniffed_mimetype;
800   char *mimetype;
801   int i;
802   int n_name_mimetypes;
803   int sniffed_prio;
804
805   sniffed_prio = 0;
806   n_name_mimetypes = 0;
807   sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
808
809   if (result_uncertain)
810     *result_uncertain = FALSE;
811   
812   G_LOCK (gio_xdgmime);
813   
814   if (filename)
815     {
816       basename = g_path_get_basename (filename);
817       n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
818       g_free (basename);
819     }
820
821   /* Got an extension match, and no conflicts. This is it. */
822   if (n_name_mimetypes == 1)
823     {
824       G_UNLOCK (gio_xdgmime);
825       return g_strdup (name_mimetypes[0]);
826     }
827   
828   if (data)
829     {
830       sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
831       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
832           data &&
833           looks_like_text (data, data_size))
834         sniffed_mimetype = "text/plain";
835     }
836   
837   if (n_name_mimetypes == 0)
838     {
839       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
840           result_uncertain)
841         *result_uncertain = TRUE;
842       
843       mimetype = g_strdup (sniffed_mimetype);
844     }
845   else
846     {
847       mimetype = NULL;
848       if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
849         {
850           if (sniffed_prio >= 80) /* High priority sniffing match, use that */
851             mimetype = g_strdup (sniffed_mimetype);
852           else
853             {
854               /* There are conflicts between the name matches and we have a sniffed
855                  type, use that as a tie breaker. */
856               
857               for (i = 0; i < n_name_mimetypes; i++)
858                 {
859                   if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
860                     {
861                       /* This nametype match is derived from (or the same as) the sniffed type).
862                          This is probably it. */
863                       mimetype = g_strdup (name_mimetypes[i]);
864                       break;
865                     }
866                 }
867             }
868         }
869       
870       if (mimetype == NULL)
871         {
872           /* Conflicts, and sniffed type was no help or not there. Guess on the first one */
873           mimetype = g_strdup (name_mimetypes[0]);
874           if (result_uncertain)
875             *result_uncertain = TRUE;
876         }
877     }
878   
879   G_UNLOCK (gio_xdgmime);
880
881   return mimetype;
882 }
883
884 static void
885 enumerate_mimetypes_subdir (const char *dir, 
886                             const char *prefix, 
887                             GHashTable *mimetypes)
888 {
889   DIR *d;
890   struct dirent *ent;
891   char *mimetype;
892
893   d = opendir (dir);
894   if (d)
895     {
896       while ((ent = readdir (d)) != NULL)
897         {
898           if (g_str_has_suffix (ent->d_name, ".xml"))
899             {
900               mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
901               g_hash_table_insert (mimetypes, mimetype, NULL);
902             }
903         }
904       closedir (d);
905     }
906 }
907
908 static void
909 enumerate_mimetypes_dir (const char *dir, 
910                          GHashTable *mimetypes)
911 {
912   DIR *d;
913   struct dirent *ent;
914   char *mimedir;
915   char *name;
916
917   mimedir = g_build_filename (dir, "mime", NULL);
918   
919   d = opendir (mimedir);
920   if (d)
921     {
922       while ((ent = readdir (d)) != NULL)
923         {
924           if (strcmp (ent->d_name, "packages") != 0)
925             {
926               name = g_build_filename (mimedir, ent->d_name, NULL);
927               if (g_file_test (name, G_FILE_TEST_IS_DIR))
928                 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
929               g_free (name);
930             }
931         }
932       closedir (d);
933     }
934   
935   g_free (mimedir);
936 }
937
938 /**
939  * g_content_types_get_registered:
940  * 
941  * Gets a list of strings containing all the registered content types
942  * known to the system. The list and its data should be freed using 
943  * @g_list_foreach(list, g_free, NULL) and @g_list_free(list)
944  * Returns: #GList of the registered content types.
945  **/  
946 GList *
947 g_content_types_get_registered (void)
948 {
949   const char * const* dirs;
950   GHashTable *mimetypes;
951   GHashTableIter iter;
952   gpointer key;
953   int i;
954   GList *l;
955
956   mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
957
958   enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
959   dirs = g_get_system_data_dirs ();
960
961   for (i = 0; dirs[i] != NULL; i++)
962     enumerate_mimetypes_dir (dirs[i], mimetypes);
963
964   l = NULL;
965   g_hash_table_iter_init (&iter, mimetypes);
966   while (g_hash_table_iter_next (&iter, &key, NULL))
967     l = g_list_prepend (l, key);
968
969   g_hash_table_destroy (mimetypes);
970
971   return l;
972 }
973
974 #endif /* Unix version */
975
976 #define __G_CONTENT_TYPE_C__
977 #include "gioaliasdef.c"