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