Better documentation for g_value_dup_object().
[glib.git] / gio / gcontenttype.c
blob3856a2dfa7a157e36b6e42422c01dccd7366a256
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
22 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "config.h"
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include "gcontenttypeprivate.h"
31 #include "gthemedicon.h"
32 #include "gicon.h"
33 #include "gfile.h"
34 #include "gfileenumerator.h"
35 #include "gfileinfo.h"
36 #include "glibintl.h"
39 /**
40 * SECTION:gcontenttype
41 * @short_description: Platform-specific content typing
42 * @include: gio/gio.h
44 * A content type is a platform specific string that defines the type
45 * of a file. On unix it is a mime type, on win32 it is an extension string
46 * like ".doc", ".txt" or a percieved string like "audio". Such strings
47 * can be looked up in the registry at HKEY_CLASSES_ROOT.
48 **/
50 #ifdef G_OS_WIN32
52 #include <windows.h>
54 static char *
55 get_registry_classes_key (const char *subdir,
56 const wchar_t *key_name)
58 wchar_t *wc_key;
59 HKEY reg_key = NULL;
60 DWORD key_type;
61 DWORD nbytes;
62 char *value_utf8;
64 value_utf8 = NULL;
66 nbytes = 0;
67 wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
68 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
69 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS &&
70 RegQueryValueExW (reg_key, key_name, 0,
71 &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
72 (key_type == REG_SZ || key_type == REG_EXPAND_SZ))
74 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
75 RegQueryValueExW (reg_key, key_name, 0,
76 &key_type, (LPBYTE) wc_temp, &nbytes);
77 wc_temp[nbytes/2] = '\0';
78 if (key_type == REG_EXPAND_SZ)
80 wchar_t dummy[1];
81 int len = ExpandEnvironmentStringsW (wc_temp, dummy, 1);
82 if (len > 0)
84 wchar_t *wc_temp_expanded = g_new (wchar_t, len);
85 if (ExpandEnvironmentStringsW (wc_temp, wc_temp_expanded, len) == len)
86 value_utf8 = g_utf16_to_utf8 (wc_temp_expanded, -1, NULL, NULL, NULL);
87 g_free (wc_temp_expanded);
90 else
92 value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
94 g_free (wc_temp);
97 g_free (wc_key);
99 if (reg_key != NULL)
100 RegCloseKey (reg_key);
102 return value_utf8;
105 gboolean
106 g_content_type_equals (const gchar *type1,
107 const gchar *type2)
109 char *progid1, *progid2;
110 gboolean res;
112 g_return_val_if_fail (type1 != NULL, FALSE);
113 g_return_val_if_fail (type2 != NULL, FALSE);
115 if (g_ascii_strcasecmp (type1, type2) == 0)
116 return TRUE;
118 res = FALSE;
119 progid1 = get_registry_classes_key (type1, NULL);
120 progid2 = get_registry_classes_key (type2, NULL);
121 if (progid1 != NULL && progid2 != NULL &&
122 strcmp (progid1, progid2) == 0)
123 res = TRUE;
124 g_free (progid1);
125 g_free (progid2);
127 return res;
130 gboolean
131 g_content_type_is_a (const gchar *type,
132 const gchar *supertype)
134 gboolean res;
135 char *value_utf8;
137 g_return_val_if_fail (type != NULL, FALSE);
138 g_return_val_if_fail (supertype != NULL, FALSE);
140 if (g_content_type_equals (type, supertype))
141 return TRUE;
143 res = FALSE;
144 value_utf8 = get_registry_classes_key (type, L"PerceivedType");
145 if (value_utf8 && strcmp (value_utf8, supertype) == 0)
146 res = TRUE;
147 g_free (value_utf8);
149 return res;
152 gboolean
153 g_content_type_is_unknown (const gchar *type)
155 g_return_val_if_fail (type != NULL, FALSE);
157 return strcmp ("*", type) == 0;
160 gchar *
161 g_content_type_get_description (const gchar *type)
163 char *progid;
164 char *description;
166 g_return_val_if_fail (type != NULL, NULL);
168 progid = get_registry_classes_key (type, NULL);
169 if (progid)
171 description = get_registry_classes_key (progid, NULL);
172 g_free (progid);
174 if (description)
175 return description;
178 if (g_content_type_is_unknown (type))
179 return g_strdup (_("Unknown type"));
180 return g_strdup_printf (_("%s filetype"), type);
183 gchar *
184 g_content_type_get_mime_type (const gchar *type)
186 char *mime;
188 g_return_val_if_fail (type != NULL, NULL);
190 mime = get_registry_classes_key (type, L"Content Type");
191 if (mime)
192 return mime;
193 else if (g_content_type_is_unknown (type))
194 return g_strdup ("application/octet-stream");
195 else if (*type == '.')
196 return g_strdup_printf ("application/x-ext-%s", type+1);
197 /* TODO: Map "image" to "image/ *", etc? */
199 return g_strdup ("application/octet-stream");
202 G_LOCK_DEFINE_STATIC (_type_icons);
203 static GHashTable *_type_icons = NULL;
205 GIcon *
206 g_content_type_get_icon (const gchar *type)
208 GIcon *themed_icon;
209 char *name = NULL;
211 g_return_val_if_fail (type != NULL, NULL);
213 /* In the Registry icons are the default value of
214 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
215 <type>: <value>
216 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
217 REG_SZ: shimgvw.dll,3
219 G_LOCK (_type_icons);
220 if (!_type_icons)
221 _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
222 name = g_hash_table_lookup (_type_icons, type);
223 if (!name && type[0] == '.')
225 /* double lookup by extension */
226 gchar *key = get_registry_classes_key (type, NULL);
227 if (!key)
228 key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
229 else
231 gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
232 g_free (key);
233 key = key2;
235 name = get_registry_classes_key (key, NULL);
236 if (name && strcmp (name, "%1") == 0)
238 g_free (name);
239 name = NULL;
241 if (name)
242 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
243 g_free (key);
246 /* icon-name similar to how it was with gtk-2-12 */
247 if (name)
249 themed_icon = g_themed_icon_new (name);
251 else
253 /* if not found an icon fall back to gtk-builtins */
254 name = strcmp (type, "inode/directory") == 0 ? "gtk-directory" :
255 g_content_type_can_be_executable (type) ? "gtk-execute" : "gtk-file";
256 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
257 themed_icon = g_themed_icon_new_with_default_fallbacks (name);
259 G_UNLOCK (_type_icons);
261 return G_ICON (themed_icon);
264 gboolean
265 g_content_type_can_be_executable (const gchar *type)
267 g_return_val_if_fail (type != NULL, FALSE);
269 if (strcmp (type, ".exe") == 0 ||
270 strcmp (type, ".com") == 0 ||
271 strcmp (type, ".bat") == 0)
272 return TRUE;
274 /* TODO: Also look at PATHEXT, which lists the extensions for
275 * "scripts" in addition to those for true binary executables.
277 * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
278 * right now, for instance). And in a sense, all associated file
279 * types are "executable" on Windows... You can just type foo.jpg as
280 * a command name in cmd.exe, and it will run the application
281 * associated with .jpg. Hard to say what this API actually means
282 * with "executable".
285 return FALSE;
288 static gboolean
289 looks_like_text (const guchar *data,
290 gsize data_size)
292 gsize i;
293 guchar c;
294 for (i = 0; i < data_size; i++)
296 c = data[i];
297 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
298 return FALSE;
300 return TRUE;
303 gchar *
304 g_content_type_from_mime_type (const gchar *mime_type)
306 char *key, *content_type;
308 g_return_val_if_fail (mime_type != NULL, NULL);
310 key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
311 content_type = get_registry_classes_key (key, L"Extension");
312 g_free (key);
314 return content_type;
317 gchar *
318 g_content_type_guess (const gchar *filename,
319 const guchar *data,
320 gsize data_size,
321 gboolean *result_uncertain)
323 char *basename;
324 char *type;
325 char *dot;
327 type = NULL;
329 if (result_uncertain)
330 *result_uncertain = FALSE;
332 if (filename)
334 basename = g_path_get_basename (filename);
335 dot = strrchr (basename, '.');
336 if (dot)
337 type = g_strdup (dot);
338 g_free (basename);
341 if (type)
342 return type;
344 if (data && looks_like_text (data, data_size))
345 return g_strdup (".txt");
347 return g_strdup ("*");
350 GList *
351 g_content_types_get_registered (void)
353 DWORD index;
354 wchar_t keyname[256];
355 DWORD key_len;
356 char *key_utf8;
357 GList *types;
359 types = NULL;
360 index = 0;
361 key_len = 256;
362 while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
363 index,
364 keyname,
365 &key_len,
366 NULL,
367 NULL,
368 NULL,
369 NULL) == ERROR_SUCCESS)
371 key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
372 if (key_utf8)
374 if (*key_utf8 == '.')
375 types = g_list_prepend (types, key_utf8);
376 else
377 g_free (key_utf8);
379 index++;
380 key_len = 256;
383 return g_list_reverse (types);
386 gchar **
387 g_content_type_guess_for_tree (GFile *root)
389 /* FIXME: implement */
390 return NULL;
393 #else /* !G_OS_WIN32 - Unix specific version */
395 #include <dirent.h>
397 #define XDG_PREFIX _gio_xdg
398 #include "xdgmime/xdgmime.h"
400 /* We lock this mutex whenever we modify global state in this module. */
401 G_LOCK_DEFINE_STATIC (gio_xdgmime);
403 gsize
404 _g_unix_content_type_get_sniff_len (void)
406 gsize size;
408 G_LOCK (gio_xdgmime);
409 size = xdg_mime_get_max_buffer_extents ();
410 G_UNLOCK (gio_xdgmime);
412 return size;
415 gchar *
416 _g_unix_content_type_unalias (const gchar *type)
418 gchar *res;
420 G_LOCK (gio_xdgmime);
421 res = g_strdup (xdg_mime_unalias_mime_type (type));
422 G_UNLOCK (gio_xdgmime);
424 return res;
427 gchar **
428 _g_unix_content_type_get_parents (const gchar *type)
430 const gchar *umime;
431 gchar **parents;
432 GPtrArray *array;
433 int i;
435 array = g_ptr_array_new ();
437 G_LOCK (gio_xdgmime);
439 umime = xdg_mime_unalias_mime_type (type);
441 g_ptr_array_add (array, g_strdup (umime));
443 parents = xdg_mime_list_mime_parents (umime);
444 for (i = 0; parents && parents[i] != NULL; i++)
445 g_ptr_array_add (array, g_strdup (parents[i]));
447 free (parents);
449 G_UNLOCK (gio_xdgmime);
451 g_ptr_array_add (array, NULL);
453 return (gchar **)g_ptr_array_free (array, FALSE);
457 * g_content_type_equals:
458 * @type1: a content type string
459 * @type2: a content type string
461 * Compares two content types for equality.
463 * Returns: %TRUE if the two strings are identical or equivalent,
464 * %FALSE otherwise.
466 gboolean
467 g_content_type_equals (const gchar *type1,
468 const gchar *type2)
470 gboolean res;
472 g_return_val_if_fail (type1 != NULL, FALSE);
473 g_return_val_if_fail (type2 != NULL, FALSE);
475 G_LOCK (gio_xdgmime);
476 res = xdg_mime_mime_type_equal (type1, type2);
477 G_UNLOCK (gio_xdgmime);
479 return res;
483 * g_content_type_is_a:
484 * @type: a content type string
485 * @supertype: a content type string
487 * Determines if @type is a subset of @supertype.
489 * Returns: %TRUE if @type is a kind of @supertype,
490 * %FALSE otherwise.
492 gboolean
493 g_content_type_is_a (const gchar *type,
494 const gchar *supertype)
496 gboolean res;
498 g_return_val_if_fail (type != NULL, FALSE);
499 g_return_val_if_fail (supertype != NULL, FALSE);
501 G_LOCK (gio_xdgmime);
502 res = xdg_mime_mime_type_subclass (type, supertype);
503 G_UNLOCK (gio_xdgmime);
505 return res;
509 * g_content_type_is_unknown:
510 * @type: a content type string
512 * Checks if the content type is the generic "unknown" type.
513 * On UNIX this is the "application/octet-stream" mimetype,
514 * while on win32 it is "*".
516 * Returns: %TRUE if the type is the unknown type.
518 gboolean
519 g_content_type_is_unknown (const gchar *type)
521 g_return_val_if_fail (type != NULL, FALSE);
523 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
527 typedef enum {
528 MIME_TAG_TYPE_OTHER,
529 MIME_TAG_TYPE_COMMENT
530 } MimeTagType;
532 typedef struct {
533 int current_type;
534 int current_lang_level;
535 int comment_lang_level;
536 char *comment;
537 } MimeParser;
540 static int
541 language_level (const char *lang)
543 const char * const *lang_list;
544 int i;
546 /* The returned list is sorted from most desirable to least
547 desirable and always contains the default locale "C". */
548 lang_list = g_get_language_names ();
550 for (i = 0; lang_list[i]; i++)
551 if (strcmp (lang_list[i], lang) == 0)
552 return 1000-i;
554 return 0;
557 static void
558 mime_info_start_element (GMarkupParseContext *context,
559 const gchar *element_name,
560 const gchar **attribute_names,
561 const gchar **attribute_values,
562 gpointer user_data,
563 GError **error)
565 int i;
566 const char *lang;
567 MimeParser *parser = user_data;
569 if (strcmp (element_name, "comment") == 0)
571 lang = "C";
572 for (i = 0; attribute_names[i]; i++)
573 if (strcmp (attribute_names[i], "xml:lang") == 0)
575 lang = attribute_values[i];
576 break;
579 parser->current_lang_level = language_level (lang);
580 parser->current_type = MIME_TAG_TYPE_COMMENT;
582 else
583 parser->current_type = MIME_TAG_TYPE_OTHER;
586 static void
587 mime_info_end_element (GMarkupParseContext *context,
588 const gchar *element_name,
589 gpointer user_data,
590 GError **error)
592 MimeParser *parser = user_data;
594 parser->current_type = MIME_TAG_TYPE_OTHER;
597 static void
598 mime_info_text (GMarkupParseContext *context,
599 const gchar *text,
600 gsize text_len,
601 gpointer user_data,
602 GError **error)
604 MimeParser *parser = user_data;
606 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
607 parser->current_lang_level > parser->comment_lang_level)
609 g_free (parser->comment);
610 parser->comment = g_strndup (text, text_len);
611 parser->comment_lang_level = parser->current_lang_level;
615 static char *
616 load_comment_for_mime_helper (const char *dir,
617 const char *basename)
619 GMarkupParseContext *context;
620 char *filename, *data;
621 gsize len;
622 gboolean res;
623 MimeParser parse_data = {0};
624 GMarkupParser parser = {
625 mime_info_start_element,
626 mime_info_end_element,
627 mime_info_text
630 filename = g_build_filename (dir, "mime", basename, NULL);
632 res = g_file_get_contents (filename, &data, &len, NULL);
633 g_free (filename);
634 if (!res)
635 return NULL;
637 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
638 res = g_markup_parse_context_parse (context, data, len, NULL);
639 g_free (data);
640 g_markup_parse_context_free (context);
642 if (!res)
643 return NULL;
645 return parse_data.comment;
649 static char *
650 load_comment_for_mime (const char *mimetype)
652 const char * const* dirs;
653 char *basename;
654 char *comment;
655 int i;
657 basename = g_strdup_printf ("%s.xml", mimetype);
659 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
660 if (comment)
662 g_free (basename);
663 return comment;
666 dirs = g_get_system_data_dirs ();
668 for (i = 0; dirs[i] != NULL; i++)
670 comment = load_comment_for_mime_helper (dirs[i], basename);
671 if (comment)
673 g_free (basename);
674 return comment;
677 g_free (basename);
679 return g_strdup_printf (_("%s type"), mimetype);
683 * g_content_type_get_description:
684 * @type: a content type string
686 * Gets the human readable description of the content type.
688 * Returns: a short description of the content type @type. Free the
689 * returned string with g_free()
691 gchar *
692 g_content_type_get_description (const gchar *type)
694 static GHashTable *type_comment_cache = NULL;
695 gchar *comment;
697 g_return_val_if_fail (type != NULL, NULL);
699 G_LOCK (gio_xdgmime);
700 type = xdg_mime_unalias_mime_type (type);
702 if (type_comment_cache == NULL)
703 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
705 comment = g_hash_table_lookup (type_comment_cache, type);
706 comment = g_strdup (comment);
707 G_UNLOCK (gio_xdgmime);
709 if (comment != NULL)
710 return comment;
712 comment = load_comment_for_mime (type);
714 G_LOCK (gio_xdgmime);
715 g_hash_table_insert (type_comment_cache,
716 g_strdup (type),
717 g_strdup (comment));
718 G_UNLOCK (gio_xdgmime);
720 return comment;
724 * g_content_type_get_mime_type:
725 * @type: a content type string
727 * Gets the mime type for the content type, if one is registered.
729 * Returns: (allow-none): the registered mime type for the given @type,
730 * or %NULL if unknown.
732 char *
733 g_content_type_get_mime_type (const char *type)
735 g_return_val_if_fail (type != NULL, NULL);
737 return g_strdup (type);
741 * g_content_type_get_icon:
742 * @type: a content type string
744 * Gets the icon for a content type.
746 * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
747 * object with g_object_unref()
749 GIcon *
750 g_content_type_get_icon (const gchar *type)
752 char *mimetype_icon, *generic_mimetype_icon, *q;
753 char *xdg_mimetype_icon, *legacy_mimetype_icon;
754 char *xdg_mimetype_generic_icon;
755 char *icon_names[5];
756 int n = 0;
757 const char *p;
758 GIcon *themed_icon;
760 g_return_val_if_fail (type != NULL, NULL);
762 G_LOCK (gio_xdgmime);
763 xdg_mimetype_icon = g_strdup (xdg_mime_get_icon (type));
764 xdg_mimetype_generic_icon = g_strdup (xdg_mime_get_generic_icon (type));
765 G_UNLOCK (gio_xdgmime);
767 mimetype_icon = g_strdup (type);
769 while ((q = strchr (mimetype_icon, '/')) != NULL)
770 *q = '-';
772 p = strchr (type, '/');
773 if (p == NULL)
774 p = type + strlen (type);
776 /* Not all icons have migrated to the new icon theme spec, look for old names too */
777 legacy_mimetype_icon = g_strconcat ("gnome-mime-", mimetype_icon, NULL);
779 generic_mimetype_icon = g_malloc (p - type + strlen ("-x-generic") + 1);
780 memcpy (generic_mimetype_icon, type, p - type);
781 memcpy (generic_mimetype_icon + (p - type), "-x-generic", strlen ("-x-generic"));
782 generic_mimetype_icon[(p - type) + strlen ("-x-generic")] = 0;
784 if (xdg_mimetype_icon)
785 icon_names[n++] = xdg_mimetype_icon;
787 icon_names[n++] = mimetype_icon;
788 icon_names[n++] = legacy_mimetype_icon;
790 if (xdg_mimetype_generic_icon)
791 icon_names[n++] = xdg_mimetype_generic_icon;
793 icon_names[n++] = generic_mimetype_icon;
795 themed_icon = g_themed_icon_new_from_names (icon_names, n);
797 g_free (xdg_mimetype_icon);
798 g_free (xdg_mimetype_generic_icon);
799 g_free (mimetype_icon);
800 g_free (legacy_mimetype_icon);
801 g_free (generic_mimetype_icon);
803 return themed_icon;
807 * g_content_type_can_be_executable:
808 * @type: a content type string
810 * Checks if a content type can be executable. Note that for instance
811 * things like text files can be executables (i.e. scripts and batch files).
813 * Returns: %TRUE if the file type corresponds to a type that
814 * can be executable, %FALSE otherwise.
816 gboolean
817 g_content_type_can_be_executable (const gchar *type)
819 g_return_val_if_fail (type != NULL, FALSE);
821 if (g_content_type_is_a (type, "application/x-executable") ||
822 g_content_type_is_a (type, "text/plain"))
823 return TRUE;
825 return FALSE;
828 static gboolean
829 looks_like_text (const guchar *data, gsize data_size)
831 gsize i;
832 char c;
834 for (i = 0; i < data_size; i++)
836 c = data[i];
838 if (g_ascii_iscntrl (c) &&
839 !g_ascii_isspace (c) &&
840 c != '\b')
841 return FALSE;
843 return TRUE;
847 * g_content_type_from_mime_type:
848 * @mime_type: a mime type string
850 * Tries to find a content type based on the mime type name.
852 * Returns: (allow-none): Newly allocated string with content type
853 * or %NULL. Free with g_free()
855 * Since: 2.18
857 gchar *
858 g_content_type_from_mime_type (const gchar *mime_type)
860 char *umime;
862 g_return_val_if_fail (mime_type != NULL, NULL);
864 G_LOCK (gio_xdgmime);
865 /* mime type and content type are same on unixes */
866 umime = g_strdup (xdg_mime_unalias_mime_type (mime_type));
867 G_UNLOCK (gio_xdgmime);
869 return umime;
873 * g_content_type_guess:
874 * @filename: (allow-none): a string, or %NULL
875 * @data: (allow-none) (array length=data_size): a stream of data, or %NULL
876 * @data_size: the size of @data
877 * @result_uncertain: (allow-none) (out): return location for the certainty
878 * of the result, or %NULL
880 * Guesses the content type based on example data. If the function is
881 * uncertain, @result_uncertain will be set to %TRUE. Either @filename
882 * or @data may be %NULL, in which case the guess will be based solely
883 * on the other argument.
885 * Returns: a string indicating a guessed content type for the
886 * given data. Free with g_free()
888 gchar *
889 g_content_type_guess (const gchar *filename,
890 const guchar *data,
891 gsize data_size,
892 gboolean *result_uncertain)
894 char *basename;
895 const char *name_mimetypes[10], *sniffed_mimetype;
896 char *mimetype;
897 int i;
898 int n_name_mimetypes;
899 int sniffed_prio;
901 sniffed_prio = 0;
902 n_name_mimetypes = 0;
903 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
905 if (result_uncertain)
906 *result_uncertain = FALSE;
908 G_LOCK (gio_xdgmime);
910 if (filename)
912 i = strlen (filename);
913 if (filename[i - 1] == '/')
915 name_mimetypes[0] = "inode/directory";
916 name_mimetypes[1] = NULL;
917 n_name_mimetypes = 1;
918 if (result_uncertain)
919 *result_uncertain = TRUE;
921 else
923 basename = g_path_get_basename (filename);
924 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
925 g_free (basename);
929 /* Got an extension match, and no conflicts. This is it. */
930 if (n_name_mimetypes == 1)
932 G_UNLOCK (gio_xdgmime);
933 return g_strdup (name_mimetypes[0]);
936 if (data)
938 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
939 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
940 data &&
941 looks_like_text (data, data_size))
942 sniffed_mimetype = "text/plain";
944 /* For security reasons we don't ever want to sniff desktop files
945 * where we know the filename and it doesn't have a .desktop extension.
946 * This is because desktop files allow executing any application and
947 * we don't want to make it possible to hide them looking like something
948 * else.
950 if (filename != NULL &&
951 strcmp (sniffed_mimetype, "application/x-desktop") == 0)
952 sniffed_mimetype = "text/plain";
955 if (n_name_mimetypes == 0)
957 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
958 result_uncertain)
959 *result_uncertain = TRUE;
961 mimetype = g_strdup (sniffed_mimetype);
963 else
965 mimetype = NULL;
966 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
968 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
969 mimetype = g_strdup (sniffed_mimetype);
970 else
972 /* There are conflicts between the name matches and we
973 * have a sniffed type, use that as a tie breaker.
975 for (i = 0; i < n_name_mimetypes; i++)
977 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
979 /* This nametype match is derived from (or the same as)
980 * the sniffed type). This is probably it.
982 mimetype = g_strdup (name_mimetypes[i]);
983 break;
989 if (mimetype == NULL)
991 /* Conflicts, and sniffed type was no help or not there.
992 * Guess on the first one
994 mimetype = g_strdup (name_mimetypes[0]);
995 if (result_uncertain)
996 *result_uncertain = TRUE;
1000 G_UNLOCK (gio_xdgmime);
1002 return mimetype;
1005 static void
1006 enumerate_mimetypes_subdir (const char *dir,
1007 const char *prefix,
1008 GHashTable *mimetypes)
1010 DIR *d;
1011 struct dirent *ent;
1012 char *mimetype;
1014 d = opendir (dir);
1015 if (d)
1017 while ((ent = readdir (d)) != NULL)
1019 if (g_str_has_suffix (ent->d_name, ".xml"))
1021 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
1022 g_hash_table_replace (mimetypes, mimetype, NULL);
1025 closedir (d);
1029 static void
1030 enumerate_mimetypes_dir (const char *dir,
1031 GHashTable *mimetypes)
1033 DIR *d;
1034 struct dirent *ent;
1035 char *mimedir;
1036 char *name;
1038 mimedir = g_build_filename (dir, "mime", NULL);
1040 d = opendir (mimedir);
1041 if (d)
1043 while ((ent = readdir (d)) != NULL)
1045 if (strcmp (ent->d_name, "packages") != 0)
1047 name = g_build_filename (mimedir, ent->d_name, NULL);
1048 if (g_file_test (name, G_FILE_TEST_IS_DIR))
1049 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
1050 g_free (name);
1053 closedir (d);
1056 g_free (mimedir);
1060 * g_content_types_get_registered:
1062 * Gets a list of strings containing all the registered content types
1063 * known to the system. The list and its data should be freed using
1064 * <programlisting>
1065 * g_list_foreach (list, g_free, NULL);
1066 * g_list_free (list);
1067 * </programlisting>
1069 * Returns: (element-type utf8) (transfer full): #GList of the registered content types
1071 GList *
1072 g_content_types_get_registered (void)
1074 const char * const* dirs;
1075 GHashTable *mimetypes;
1076 GHashTableIter iter;
1077 gpointer key;
1078 int i;
1079 GList *l;
1081 mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1083 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
1084 dirs = g_get_system_data_dirs ();
1086 for (i = 0; dirs[i] != NULL; i++)
1087 enumerate_mimetypes_dir (dirs[i], mimetypes);
1089 l = NULL;
1090 g_hash_table_iter_init (&iter, mimetypes);
1091 while (g_hash_table_iter_next (&iter, &key, NULL))
1093 l = g_list_prepend (l, key);
1094 g_hash_table_iter_steal (&iter);
1097 g_hash_table_destroy (mimetypes);
1099 return l;
1103 /* tree magic data */
1104 static GList *tree_matches = NULL;
1105 static gboolean need_reload = FALSE;
1107 G_LOCK_DEFINE_STATIC (gio_treemagic);
1109 typedef struct
1111 gchar *path;
1112 GFileType type;
1113 guint match_case : 1;
1114 guint executable : 1;
1115 guint non_empty : 1;
1116 guint on_disc : 1;
1117 gchar *mimetype;
1118 GList *matches;
1119 } TreeMatchlet;
1121 typedef struct
1123 gchar *contenttype;
1124 gint priority;
1125 GList *matches;
1126 } TreeMatch;
1129 static void
1130 tree_matchlet_free (TreeMatchlet *matchlet)
1132 g_list_foreach (matchlet->matches, (GFunc)tree_matchlet_free, NULL);
1133 g_list_free (matchlet->matches);
1134 g_free (matchlet->path);
1135 g_free (matchlet->mimetype);
1136 g_slice_free (TreeMatchlet, matchlet);
1139 static void
1140 tree_match_free (TreeMatch *match)
1142 g_list_foreach (match->matches, (GFunc)tree_matchlet_free, NULL);
1143 g_list_free (match->matches);
1144 g_free (match->contenttype);
1145 g_slice_free (TreeMatch, match);
1148 static TreeMatch *
1149 parse_header (gchar *line)
1151 gint len;
1152 gchar *s;
1153 TreeMatch *match;
1155 len = strlen (line);
1157 if (line[0] != '[' || line[len - 1] != ']')
1158 return NULL;
1160 line[len - 1] = 0;
1161 s = strchr (line, ':');
1163 match = g_slice_new0 (TreeMatch);
1164 match->priority = atoi (line + 1);
1165 match->contenttype = g_strdup (s + 1);
1167 return match;
1170 static TreeMatchlet *
1171 parse_match_line (gchar *line,
1172 gint *depth)
1174 gchar *s, *p;
1175 TreeMatchlet *matchlet;
1176 gchar **parts;
1177 gint i;
1179 matchlet = g_slice_new0 (TreeMatchlet);
1181 if (line[0] == '>')
1183 *depth = 0;
1184 s = line;
1186 else
1188 *depth = atoi (line);
1189 s = strchr (line, '>');
1191 s += 2;
1192 p = strchr (s, '"');
1193 *p = 0;
1195 matchlet->path = g_strdup (s);
1196 s = p + 1;
1197 parts = g_strsplit (s, ",", 0);
1198 if (strcmp (parts[0], "=file") == 0)
1199 matchlet->type = G_FILE_TYPE_REGULAR;
1200 else if (strcmp (parts[0], "=directory") == 0)
1201 matchlet->type = G_FILE_TYPE_DIRECTORY;
1202 else if (strcmp (parts[0], "=link") == 0)
1203 matchlet->type = G_FILE_TYPE_SYMBOLIC_LINK;
1204 else
1205 matchlet->type = G_FILE_TYPE_UNKNOWN;
1206 for (i = 1; parts[i]; i++)
1208 if (strcmp (parts[i], "executable") == 0)
1209 matchlet->executable = 1;
1210 else if (strcmp (parts[i], "match-case") == 0)
1211 matchlet->match_case = 1;
1212 else if (strcmp (parts[i], "non-empty") == 0)
1213 matchlet->non_empty = 1;
1214 else if (strcmp (parts[i], "on-disc") == 0)
1215 matchlet->on_disc = 1;
1216 else
1217 matchlet->mimetype = g_strdup (parts[i]);
1220 g_strfreev (parts);
1222 return matchlet;
1225 static gint
1226 cmp_match (gconstpointer a, gconstpointer b)
1228 const TreeMatch *aa = (const TreeMatch *)a;
1229 const TreeMatch *bb = (const TreeMatch *)b;
1231 return bb->priority - aa->priority;
1234 static void
1235 insert_match (TreeMatch *match)
1237 tree_matches = g_list_insert_sorted (tree_matches, match, cmp_match);
1240 static void
1241 insert_matchlet (TreeMatch *match,
1242 TreeMatchlet *matchlet,
1243 gint depth)
1245 if (depth == 0)
1246 match->matches = g_list_append (match->matches, matchlet);
1247 else
1249 GList *last;
1250 TreeMatchlet *m;
1252 last = g_list_last (match->matches);
1253 if (!last)
1255 tree_matchlet_free (matchlet);
1256 g_warning ("can't insert tree matchlet at depth %d", depth);
1257 return;
1260 m = (TreeMatchlet *) last->data;
1261 while (--depth > 0)
1263 last = g_list_last (m->matches);
1264 if (!last)
1266 tree_matchlet_free (matchlet);
1267 g_warning ("can't insert tree matchlet at depth %d", depth);
1268 return;
1271 m = (TreeMatchlet *) last->data;
1273 m->matches = g_list_append (m->matches, matchlet);
1277 static void
1278 read_tree_magic_from_directory (const gchar *prefix)
1280 gchar *filename;
1281 gchar *text;
1282 gsize len;
1283 gchar **lines;
1284 gint i;
1285 TreeMatch *match;
1286 TreeMatchlet *matchlet;
1287 gint depth;
1289 filename = g_build_filename (prefix, "mime", "treemagic", NULL);
1291 if (g_file_get_contents (filename, &text, &len, NULL))
1293 if (strcmp (text, "MIME-TreeMagic") == 0)
1295 lines = g_strsplit (text + strlen ("MIME-TreeMagic") + 2, "\n", 0);
1296 match = NULL;
1297 for (i = 0; lines[i] && lines[i][0]; i++)
1299 if (lines[i][0] == '[')
1301 match = parse_header (lines[i]);
1302 insert_match (match);
1304 else
1306 matchlet = parse_match_line (lines[i], &depth);
1307 insert_matchlet (match, matchlet, depth);
1311 g_strfreev (lines);
1313 else
1314 g_warning ("%s: header not found, skipping\n", filename);
1316 g_free (text);
1319 g_free (filename);
1323 static void
1324 xdg_mime_reload (void *user_data)
1326 need_reload = TRUE;
1329 static void
1330 tree_magic_shutdown (void)
1332 g_list_foreach (tree_matches, (GFunc)tree_match_free, NULL);
1333 g_list_free (tree_matches);
1334 tree_matches = NULL;
1337 static void
1338 tree_magic_init (void)
1340 static gboolean initialized = FALSE;
1341 const gchar *dir;
1342 const gchar * const * dirs;
1343 int i;
1345 if (!initialized)
1347 initialized = TRUE;
1349 xdg_mime_register_reload_callback (xdg_mime_reload, NULL, NULL);
1350 need_reload = TRUE;
1353 if (need_reload)
1355 need_reload = FALSE;
1357 tree_magic_shutdown ();
1359 dir = g_get_user_data_dir ();
1360 read_tree_magic_from_directory (dir);
1361 dirs = g_get_system_data_dirs ();
1362 for (i = 0; dirs[i]; i++)
1363 read_tree_magic_from_directory (dirs[i]);
1367 /* a filtering enumerator */
1369 typedef struct
1371 gchar *path;
1372 gint depth;
1373 gboolean ignore_case;
1374 gchar **components;
1375 gchar **case_components;
1376 GFileEnumerator **enumerators;
1377 GFile **children;
1378 } Enumerator;
1380 static gboolean
1381 component_match (Enumerator *e,
1382 gint depth,
1383 const gchar *name)
1385 gchar *case_folded, *key;
1386 gboolean found;
1388 if (strcmp (name, e->components[depth]) == 0)
1389 return TRUE;
1391 if (!e->ignore_case)
1392 return FALSE;
1394 case_folded = g_utf8_casefold (name, -1);
1395 key = g_utf8_collate_key (case_folded, -1);
1397 found = strcmp (key, e->case_components[depth]) == 0;
1399 g_free (case_folded);
1400 g_free (key);
1402 return found;
1405 static GFile *
1406 next_match_recurse (Enumerator *e,
1407 gint depth)
1409 GFile *file;
1410 GFileInfo *info;
1411 const gchar *name;
1413 while (TRUE)
1415 if (e->enumerators[depth] == NULL)
1417 if (depth > 0)
1419 file = next_match_recurse (e, depth - 1);
1420 if (file)
1422 e->children[depth] = file;
1423 e->enumerators[depth] = g_file_enumerate_children (file,
1424 G_FILE_ATTRIBUTE_STANDARD_NAME,
1425 G_FILE_QUERY_INFO_NONE,
1426 NULL,
1427 NULL);
1430 if (e->enumerators[depth] == NULL)
1431 return NULL;
1434 while ((info = g_file_enumerator_next_file (e->enumerators[depth], NULL, NULL)))
1436 name = g_file_info_get_name (info);
1437 if (component_match (e, depth, name))
1439 file = g_file_get_child (e->children[depth], name);
1440 g_object_unref (info);
1441 return file;
1443 g_object_unref (info);
1446 g_object_unref (e->enumerators[depth]);
1447 e->enumerators[depth] = NULL;
1448 g_object_unref (e->children[depth]);
1449 e->children[depth] = NULL;
1453 static GFile *
1454 enumerator_next (Enumerator *e)
1456 return next_match_recurse (e, e->depth - 1);
1459 static Enumerator *
1460 enumerator_new (GFile *root,
1461 const char *path,
1462 gboolean ignore_case)
1464 Enumerator *e;
1465 gint i;
1466 gchar *case_folded;
1468 e = g_new0 (Enumerator, 1);
1469 e->path = g_strdup (path);
1470 e->ignore_case = ignore_case;
1472 e->components = g_strsplit (e->path, G_DIR_SEPARATOR_S, -1);
1473 e->depth = g_strv_length (e->components);
1474 if (e->ignore_case)
1476 e->case_components = g_new0 (char *, e->depth + 1);
1477 for (i = 0; e->components[i]; i++)
1479 case_folded = g_utf8_casefold (e->components[i], -1);
1480 e->case_components[i] = g_utf8_collate_key (case_folded, -1);
1481 g_free (case_folded);
1485 e->children = g_new0 (GFile *, e->depth);
1486 e->children[0] = g_object_ref (root);
1487 e->enumerators = g_new0 (GFileEnumerator *, e->depth);
1488 e->enumerators[0] = g_file_enumerate_children (root,
1489 G_FILE_ATTRIBUTE_STANDARD_NAME,
1490 G_FILE_QUERY_INFO_NONE,
1491 NULL,
1492 NULL);
1494 return e;
1497 static void
1498 enumerator_free (Enumerator *e)
1500 gint i;
1502 for (i = 0; i < e->depth; i++)
1504 if (e->enumerators[i])
1505 g_object_unref (e->enumerators[i]);
1506 if (e->children[i])
1507 g_object_unref (e->children[i]);
1510 g_free (e->enumerators);
1511 g_free (e->children);
1512 g_strfreev (e->components);
1513 if (e->case_components)
1514 g_strfreev (e->case_components);
1515 g_free (e->path);
1516 g_free (e);
1519 static gboolean
1520 matchlet_match (TreeMatchlet *matchlet,
1521 GFile *root)
1523 GFile *file;
1524 GFileInfo *info;
1525 gboolean result;
1526 const gchar *attrs;
1527 Enumerator *e;
1528 GList *l;
1530 e = enumerator_new (root, matchlet->path, !matchlet->match_case);
1534 file = enumerator_next (e);
1535 if (!file)
1537 enumerator_free (e);
1538 return FALSE;
1541 if (matchlet->mimetype)
1542 attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1543 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ","
1544 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
1545 else
1546 attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1547 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE;
1548 info = g_file_query_info (file,
1549 attrs,
1550 G_FILE_QUERY_INFO_NONE,
1551 NULL,
1552 NULL);
1553 if (info)
1555 result = TRUE;
1557 if (matchlet->type != G_FILE_TYPE_UNKNOWN &&
1558 g_file_info_get_file_type (info) != matchlet->type)
1559 result = FALSE;
1561 if (matchlet->executable &&
1562 !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
1563 result = FALSE;
1565 else
1566 result = FALSE;
1568 if (result && matchlet->non_empty)
1570 GFileEnumerator *child_enum;
1571 GFileInfo *child_info;
1573 child_enum = g_file_enumerate_children (file,
1574 G_FILE_ATTRIBUTE_STANDARD_NAME,
1575 G_FILE_QUERY_INFO_NONE,
1576 NULL,
1577 NULL);
1579 if (child_enum)
1581 child_info = g_file_enumerator_next_file (child_enum, NULL, NULL);
1582 if (child_info)
1583 g_object_unref (child_info);
1584 else
1585 result = FALSE;
1586 g_object_unref (child_enum);
1588 else
1589 result = FALSE;
1592 if (result && matchlet->mimetype)
1594 if (strcmp (matchlet->mimetype, g_file_info_get_content_type (info)) != 0)
1595 result = FALSE;
1598 g_object_unref (info);
1599 g_object_unref (file);
1601 while (!result);
1603 enumerator_free (e);
1605 if (!matchlet->matches)
1606 return TRUE;
1608 for (l = matchlet->matches; l; l = l->next)
1610 TreeMatchlet *submatchlet;
1612 submatchlet = l->data;
1613 if (matchlet_match (submatchlet, root))
1614 return TRUE;
1617 return FALSE;
1620 static void
1621 match_match (TreeMatch *match,
1622 GFile *root,
1623 GPtrArray *types)
1625 GList *l;
1627 for (l = match->matches; l; l = l->next)
1629 TreeMatchlet *matchlet = l->data;
1630 if (matchlet_match (matchlet, root))
1632 g_ptr_array_add (types, g_strdup (match->contenttype));
1633 break;
1639 * g_content_type_guess_for_tree:
1640 * @root: the root of the tree to guess a type for
1642 * Tries to guess the type of the tree with root @root, by
1643 * looking at the files it contains. The result is an array
1644 * of content types, with the best guess coming first.
1646 * The types returned all have the form x-content/foo, e.g.
1647 * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
1648 * (for a camera memory card). See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
1649 * specification for more on x-content types.
1651 * This function is useful in the implementation of
1652 * g_mount_guess_content_type().
1654 * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
1655 * array of zero or more content types, or %NULL. Free with g_strfreev()
1657 * Since: 2.18
1659 gchar **
1660 g_content_type_guess_for_tree (GFile *root)
1662 GPtrArray *types;
1663 GList *l;
1665 types = g_ptr_array_new ();
1667 G_LOCK (gio_treemagic);
1669 tree_magic_init ();
1670 for (l = tree_matches; l; l = l->next)
1672 TreeMatch *match = l->data;
1673 match_match (match, root, types);
1676 G_UNLOCK (gio_treemagic);
1678 g_ptr_array_add (types, NULL);
1680 return (gchar **)g_ptr_array_free (types, FALSE);
1683 #endif /* Unix version */