fcc7fa2feba5aca35b83faf401852dfea6401ea4
[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   return FALSE;
207 }
208
209 static gboolean
210 looks_like_text (const guchar *data, 
211                  gsize         data_size)
212 {
213   gsize i;
214   guchar c;
215   for (i = 0; i < data_size; i++)
216     {
217       c = data[i];
218       if (g_ascii_iscntrl (c) && !g_ascii_isspace (c))
219         return FALSE;
220     }
221   return TRUE;
222 }
223
224 char *
225 g_content_type_from_mime_type (const char *mime_type)
226 {
227   char *key, *content_type;
228
229   g_return_val_if_fail (mime_type != NULL, NULL);
230
231   key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
232   content_type = get_registry_classes_key (key, L"Extension");
233   g_free (key);
234
235   return content_type;
236 }
237
238 char *
239 g_content_type_guess (const char   *filename,
240                       const guchar *data,
241                       gsize         data_size,
242                       gboolean     *result_uncertain)
243 {
244   char *basename;
245   char *type;
246   char *dot;
247
248   type = NULL;
249
250   if (filename)
251     {
252       basename = g_path_get_basename (filename);
253       dot = strrchr (basename, '.');
254       if (dot)
255         type = g_strdup (dot);
256       g_free (basename);
257     }
258
259   if (type)
260     return type;
261
262   if (data && looks_like_text (data, data_size))
263     return g_strdup (".txt");
264
265   return g_strdup ("*");
266 }
267
268 GList *
269 g_content_types_get_registered (void)
270 {
271   DWORD index;
272   wchar_t keyname[256];
273   DWORD key_len;
274   char *key_utf8;
275   GList *types;
276
277   types = NULL;
278   index = 0;
279   key_len = 256;
280   while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
281                        index,
282                        keyname,
283                        &key_len,
284                        NULL,
285                        NULL,
286                        NULL,
287                        NULL) == ERROR_SUCCESS)
288     {
289       key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
290       if (key_utf8)
291         {
292           if (*key_utf8 == '.')
293             types = g_list_prepend (types, key_utf8);
294           else
295             g_free (key_utf8);
296         }
297       index++;
298       key_len = 256;
299     }
300   
301   return g_list_reverse (types);
302 }
303
304 #else /* !G_OS_WIN32 - Unix specific version */
305
306 #include <dirent.h>
307
308 #define XDG_PREFIX _gio_xdg
309 #include "xdgmime/xdgmime.h"
310
311 /* We lock this mutex whenever we modify global state in this module.  */
312 G_LOCK_DEFINE_STATIC (gio_xdgmime);
313
314 gsize
315 _g_unix_content_type_get_sniff_len (void)
316 {
317   gsize size;
318
319   G_LOCK (gio_xdgmime);
320   size = xdg_mime_get_max_buffer_extents ();
321   G_UNLOCK (gio_xdgmime);
322
323   return size;
324 }
325
326 char *
327 _g_unix_content_type_unalias (const char *type)
328 {
329   char *res;
330   
331   G_LOCK (gio_xdgmime);
332   res = g_strdup (xdg_mime_unalias_mime_type (type));
333   G_UNLOCK (gio_xdgmime);
334   
335   return res;
336 }
337
338 char **
339 _g_unix_content_type_get_parents (const char *type)
340 {
341   const char *umime;
342   char **parents;
343   GPtrArray *array;
344   int i;
345
346   array = g_ptr_array_new ();
347   
348   G_LOCK (gio_xdgmime);
349   
350   umime = xdg_mime_unalias_mime_type (type);
351   
352   g_ptr_array_add (array, g_strdup (umime));
353   
354   parents = xdg_mime_list_mime_parents (umime);
355   for (i = 0; parents && parents[i] != NULL; i++)
356     g_ptr_array_add (array, g_strdup (parents[i]));
357   
358   free (parents);
359   
360   G_UNLOCK (gio_xdgmime);
361   
362   g_ptr_array_add (array, NULL);
363   
364   return (char **)g_ptr_array_free (array, FALSE);
365 }
366
367 /**
368  * g_content_type_equals:
369  * @type1: a content type string.
370  * @type2: a content type string.
371  *
372  * Compares two content types for equality.
373  *
374  * Returns: %TRUE if the two strings are identical or equivalent,
375  * %FALSE otherwise.
376  **/  
377 gboolean
378 g_content_type_equals (const char *type1,
379                        const char *type2)
380 {
381   gboolean res;
382   
383   g_return_val_if_fail (type1 != NULL, FALSE);
384   g_return_val_if_fail (type2 != NULL, FALSE);
385   
386   G_LOCK (gio_xdgmime);
387   res = xdg_mime_mime_type_equal (type1, type2);
388   G_UNLOCK (gio_xdgmime);
389         
390   return res;
391 }
392
393 /**
394  * g_content_type_is_a:
395  * @type: a content type string. 
396  * @supertype: a string.
397  *
398  * Determines if @type is a subset of @supertype.  
399  *
400  * Returns: %TRUE if @type is a kind of @supertype,
401  * %FALSE otherwise. 
402  **/  
403 gboolean
404 g_content_type_is_a (const char *type,
405                      const char *supertype)
406 {
407   gboolean res;
408     
409   g_return_val_if_fail (type != NULL, FALSE);
410   g_return_val_if_fail (supertype != NULL, FALSE);
411   
412   G_LOCK (gio_xdgmime);
413   res = xdg_mime_mime_type_subclass (type, supertype);
414   G_UNLOCK (gio_xdgmime);
415         
416   return res;
417 }
418
419 /**
420  * g_content_type_is_unknown:
421  * @type: a content type string. 
422  * 
423  * Checks if the content type is the generic "unknown" type.
424  * On unix this is the "application/octet-stream" mimetype,
425  * while on win32 it is "*".
426  * 
427  * Returns: %TRUE if the type is the unknown type.
428  **/  
429 gboolean
430 g_content_type_is_unknown (const char *type)
431 {
432   g_return_val_if_fail (type != NULL, FALSE);
433
434   return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
435 }
436
437
438 typedef enum {
439   MIME_TAG_TYPE_OTHER,
440   MIME_TAG_TYPE_COMMENT
441 } MimeTagType;
442
443 typedef struct {
444   int current_type;
445   int current_lang_level;
446   int comment_lang_level;
447   char *comment;
448 } MimeParser;
449
450
451 static int
452 language_level (const char *lang)
453 {
454   const char * const *lang_list;
455   int i;
456   
457   /* The returned list is sorted from most desirable to least
458      desirable and always contains the default locale "C". */
459   lang_list = g_get_language_names ();
460   
461   for (i = 0; lang_list[i]; i++)
462     if (strcmp (lang_list[i], lang) == 0)
463       return 1000-i;
464   
465   return 0;
466 }
467
468 static void
469 mime_info_start_element (GMarkupParseContext  *context,
470                          const gchar          *element_name,
471                          const gchar         **attribute_names,
472                          const gchar         **attribute_values,
473                          gpointer              user_data,
474                          GError              **error)
475 {
476   int i;
477   const char *lang;
478   MimeParser *parser = user_data;
479   
480   if (strcmp (element_name, "comment") == 0)
481     {
482       lang = "C";
483       for (i = 0; attribute_names[i]; i++)
484         if (strcmp (attribute_names[i], "xml:lang") == 0)
485           {
486             lang = attribute_values[i];
487             break;
488           }
489       
490       parser->current_lang_level = language_level (lang);
491       parser->current_type = MIME_TAG_TYPE_COMMENT;
492     }
493   else
494     parser->current_type = MIME_TAG_TYPE_OTHER;
495   
496 }
497
498 static void
499 mime_info_end_element (GMarkupParseContext  *context,
500                        const gchar          *element_name,
501                        gpointer              user_data,
502                        GError              **error)
503 {
504   MimeParser *parser = user_data;
505   
506   parser->current_type = MIME_TAG_TYPE_OTHER;
507 }
508
509 static void
510 mime_info_text (GMarkupParseContext  *context,
511                 const gchar          *text,
512                 gsize                 text_len,  
513                 gpointer              user_data,
514                 GError              **error)
515 {
516   MimeParser *parser = user_data;
517
518   if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
519       parser->current_lang_level > parser->comment_lang_level)
520     {
521       g_free (parser->comment);
522       parser->comment = g_strndup (text, text_len);
523       parser->comment_lang_level = parser->current_lang_level;
524     }
525 }
526
527 static char *
528 load_comment_for_mime_helper (const char *dir, 
529                               const char *basename)
530 {
531   GMarkupParseContext *context;
532   char *filename, *data;
533   gsize len;
534   gboolean res;
535   MimeParser parse_data = {0};
536   GMarkupParser parser = {
537     mime_info_start_element,
538     mime_info_end_element,
539     mime_info_text
540   };
541
542   filename = g_build_filename (dir, "mime", basename, NULL);
543   
544   res = g_file_get_contents (filename,  &data,  &len,  NULL);
545   g_free (filename);
546   if (!res)
547     return NULL;
548   
549   context = g_markup_parse_context_new   (&parser, 0, &parse_data, NULL);
550   res = g_markup_parse_context_parse (context, data, len, NULL);
551   g_free (data);
552   g_markup_parse_context_free (context);
553   
554   if (!res)
555     return NULL;
556
557   return parse_data.comment;
558 }
559
560
561 static char *
562 load_comment_for_mime (const char *mimetype)
563 {
564   const char * const* dirs;
565   char *basename;
566   char *comment;
567   int i;
568
569   basename = g_strdup_printf ("%s.xml", mimetype);
570
571   comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
572   if (comment)
573     {
574       g_free (basename);
575       return comment;
576     }
577   
578   dirs = g_get_system_data_dirs ();
579
580   for (i = 0; dirs[i] != NULL; i++)
581     {
582       comment = load_comment_for_mime_helper (dirs[i], basename);
583       if (comment)
584         {
585           g_free (basename);
586           return comment;
587         }
588     }
589   g_free (basename);
590   
591   return g_strdup_printf (_("%s type"), mimetype);
592 }
593
594 /**
595  * g_content_type_get_description:
596  * @type: a content type string. 
597  * 
598  * Gets the human readable description of the content type.
599  * 
600  * Returns: a short description of the content type @type. 
601  **/  
602 char *
603 g_content_type_get_description (const char *type)
604 {
605   static GHashTable *type_comment_cache = NULL;
606   char *comment;
607
608   g_return_val_if_fail (type != NULL, NULL);
609   
610   G_LOCK (gio_xdgmime);
611   type = xdg_mime_unalias_mime_type (type);
612
613   if (type_comment_cache == NULL)
614     type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
615
616   comment = g_hash_table_lookup (type_comment_cache, type);
617   comment = g_strdup (comment);
618   G_UNLOCK (gio_xdgmime);
619   
620   if (comment != NULL)
621     return comment;
622
623   comment = load_comment_for_mime (type);
624   
625   G_LOCK (gio_xdgmime);
626   g_hash_table_insert (type_comment_cache,
627                        g_strdup (type),
628                        g_strdup (comment));
629   G_UNLOCK (gio_xdgmime);
630
631   return comment;
632 }
633
634 /**
635  * g_content_type_get_mime_type:
636  * @type: a content type string. 
637  * 
638  * Gets the mime-type for the content type. If one is registered
639  * 
640  * Returns: the registered mime-type for the given @type, or NULL if unknown.
641  **/  
642 char *
643 g_content_type_get_mime_type (const char *type)
644 {
645   g_return_val_if_fail (type != NULL, NULL);
646
647   return g_strdup (type);
648 }
649
650 /**
651  * g_content_type_get_icon:
652  * @type: a content type string.
653  * 
654  * Gets the icon for a content type.
655  * 
656  * Returns: #GIcon corresponding to the content type.
657  **/  
658 GIcon *
659 g_content_type_get_icon (const char *type)
660 {
661   char *mimetype_icon, *generic_mimetype_icon, *q;
662   char *xdg_mimetype_icon, *legacy_mimetype_icon;
663   char *icon_names[4];
664   int n;
665   const char *p;
666   GIcon *themed_icon;
667   
668   g_return_val_if_fail (type != NULL, NULL);
669   
670   G_LOCK (gio_xdgmime);
671   xdg_mimetype_icon = g_strdup (xdg_mime_get_icon (type));
672   G_UNLOCK (gio_xdgmime);
673
674   mimetype_icon = g_strdup (type);
675   
676   while ((q = strchr (mimetype_icon, '/')) != NULL)
677     *q = '-';
678   
679   p = strchr (type, '/');
680   if (p == NULL)
681     p = type + strlen (type);
682
683   /* Not all icons have migrated to the new icon theme spec, look for old names too */
684   legacy_mimetype_icon = g_strconcat ("gnome-mime-", mimetype_icon, NULL);
685   
686   generic_mimetype_icon = g_malloc (p - type + strlen ("-x-generic") + 1);
687   memcpy (generic_mimetype_icon, type, p - type);
688   memcpy (generic_mimetype_icon + (p - type), "-x-generic", strlen ("-x-generic"));
689   generic_mimetype_icon[(p - type) + strlen ("-x-generic")] = 0;
690   
691   if (xdg_mimetype_icon)
692     icon_names[n++] = xdg_mimetype_icon;
693
694   icon_names[n++] = mimetype_icon;
695   icon_names[n++] = legacy_mimetype_icon;
696   icon_names[n++] = generic_mimetype_icon;
697   
698   themed_icon = g_themed_icon_new_from_names (icon_names, n);
699   
700   g_free (xdg_mimetype_icon);
701   g_free (mimetype_icon);
702   g_free (legacy_mimetype_icon);
703   g_free (generic_mimetype_icon);
704   
705   return themed_icon;
706 }
707
708 /**
709  * g_content_type_can_be_executable:
710  * @type: a content type string.
711  * 
712  * Checks if a content type can be executable. Note that for instance
713  * things like text files can be executables (i.e. scripts and batch files).
714  * 
715  * Returns: %TRUE if the file type corresponds to a type that
716  * can be executable, %FALSE otherwise. 
717  **/  
718 gboolean
719 g_content_type_can_be_executable (const char *type)
720 {
721   g_return_val_if_fail (type != NULL, FALSE);
722
723   if (g_content_type_is_a (type, "application/x-executable")  ||
724       g_content_type_is_a (type, "text/plain"))
725     return TRUE;
726
727   return FALSE;
728 }
729
730 static gboolean
731 looks_like_text (const guchar *data, gsize data_size)
732 {
733   gsize i;
734   char c;
735   
736   for (i = 0; i < data_size; i++)
737     {
738       c = data[i];
739       
740       if (g_ascii_iscntrl (c) &&
741           !g_ascii_isspace (c))
742         return FALSE;
743     }
744   return TRUE;
745 }
746
747 /**
748  * g_content_type_from_mime_type:
749  * @mime_type: a mime type string.
750  *
751  * Tries to find a content type based on the mime type name.
752  *
753  * Returns: Newly allocated string with content type or NULL when does not know.
754  *
755  * Since: 2.18
756  **/
757 char *
758 g_content_type_from_mime_type (const char *mime_type)
759 {
760   g_return_val_if_fail (mime_type != NULL, NULL);
761
762   /* mime type and content type are same on unixes */
763   return g_strdup (mime_type);
764 }
765
766 /**
767  * g_content_type_guess:
768  * @filename: a string.
769  * @data: a stream of data.
770  * @data_size: the size of @data.
771  * @result_uncertain: a flag indicating the certainty of the 
772  * result.
773  * 
774  * Guesses the content type based on example data. If the function is 
775  * uncertain, @result_uncertain will be set to %TRUE.
776  * 
777  * Returns: a string indicating a guessed content type for the 
778  * given data. 
779  **/  
780 char *
781 g_content_type_guess (const char   *filename,
782                       const guchar *data,
783                       gsize         data_size,
784                       gboolean     *result_uncertain)
785 {
786   char *basename;
787   const char *name_mimetypes[10], *sniffed_mimetype;
788   char *mimetype;
789   int i;
790   int n_name_mimetypes;
791   int sniffed_prio;
792
793   sniffed_prio = 0;
794   n_name_mimetypes = 0;
795   sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
796
797   if (result_uncertain)
798     *result_uncertain = FALSE;
799   
800   G_LOCK (gio_xdgmime);
801   
802   if (filename)
803     {
804       basename = g_path_get_basename (filename);
805       n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
806       g_free (basename);
807     }
808
809   /* Got an extension match, and no conflicts. This is it. */
810   if (n_name_mimetypes == 1)
811     {
812       G_UNLOCK (gio_xdgmime);
813       return g_strdup (name_mimetypes[0]);
814     }
815   
816   if (data)
817     {
818       sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
819       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
820           data &&
821           looks_like_text (data, data_size))
822         sniffed_mimetype = "text/plain";
823     }
824   
825   if (n_name_mimetypes == 0)
826     {
827       if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
828           result_uncertain)
829         *result_uncertain = TRUE;
830       
831       mimetype = g_strdup (sniffed_mimetype);
832     }
833   else
834     {
835       mimetype = NULL;
836       if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
837         {
838           if (sniffed_prio >= 80) /* High priority sniffing match, use that */
839             mimetype = g_strdup (sniffed_mimetype);
840           else
841             {
842               /* There are conflicts between the name matches and we have a sniffed
843                  type, use that as a tie breaker. */
844               
845               for (i = 0; i < n_name_mimetypes; i++)
846                 {
847                   if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
848                     {
849                       /* This nametype match is derived from (or the same as) the sniffed type).
850                          This is probably it. */
851                       mimetype = g_strdup (name_mimetypes[i]);
852                       break;
853                     }
854                 }
855             }
856         }
857       
858       if (mimetype == NULL)
859         {
860           /* Conflicts, and sniffed type was no help or not there. Guess on the first one */
861           mimetype = g_strdup (name_mimetypes[0]);
862           if (result_uncertain)
863             *result_uncertain = TRUE;
864         }
865     }
866   
867   G_UNLOCK (gio_xdgmime);
868
869   return mimetype;
870 }
871
872 static void
873 enumerate_mimetypes_subdir (const char *dir, 
874                             const char *prefix, 
875                             GHashTable *mimetypes)
876 {
877   DIR *d;
878   struct dirent *ent;
879   char *mimetype;
880
881   d = opendir (dir);
882   if (d)
883     {
884       while ((ent = readdir (d)) != NULL)
885         {
886           if (g_str_has_suffix (ent->d_name, ".xml"))
887             {
888               mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
889               g_hash_table_insert (mimetypes, mimetype, NULL);
890             }
891         }
892       closedir (d);
893     }
894 }
895
896 static void
897 enumerate_mimetypes_dir (const char *dir, 
898                          GHashTable *mimetypes)
899 {
900   DIR *d;
901   struct dirent *ent;
902   char *mimedir;
903   char *name;
904
905   mimedir = g_build_filename (dir, "mime", NULL);
906   
907   d = opendir (mimedir);
908   if (d)
909     {
910       while ((ent = readdir (d)) != NULL)
911         {
912           if (strcmp (ent->d_name, "packages") != 0)
913             {
914               name = g_build_filename (mimedir, ent->d_name, NULL);
915               if (g_file_test (name, G_FILE_TEST_IS_DIR))
916                 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
917               g_free (name);
918             }
919         }
920       closedir (d);
921     }
922   
923   g_free (mimedir);
924 }
925
926 /**
927  * g_content_types_get_registered:
928  * 
929  * Gets a list of strings containing all the registered content types
930  * known to the system. The list and its data should be freed using 
931  * @g_list_foreach(list, g_free, NULL) and @g_list_free(list)
932  * Returns: #GList of the registered content types.
933  **/  
934 GList *
935 g_content_types_get_registered (void)
936 {
937   const char * const* dirs;
938   GHashTable *mimetypes;
939   GHashTableIter iter;
940   gpointer key;
941   int i;
942   GList *l;
943
944   mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
945
946   enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
947   dirs = g_get_system_data_dirs ();
948
949   for (i = 0; dirs[i] != NULL; i++)
950     enumerate_mimetypes_dir (dirs[i], mimetypes);
951
952   l = NULL;
953   g_hash_table_iter_init (&iter, mimetypes);
954   while (g_hash_table_iter_next (&iter, &key, NULL))
955     l = g_list_prepend (l, key);
956
957   g_hash_table_destroy (mimetypes);
958
959   return l;
960 }
961
962 #endif /* Unix version */
963
964 #define __G_CONTENT_TYPE_C__
965 #include "gioaliasdef.c"