1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
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>
26 #include <sys/types.h>
29 #include "gcontenttypeprivate.h"
30 #include "gthemedicon.h"
36 * SECTION:gcontenttype
37 * @short_description: Platform-specific content typing
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.
51 get_registry_classes_key (const char *subdir,
52 const wchar_t *key_name)
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, ®_key) == ERROR_SUCCESS &&
66 RegQueryValueExW (reg_key, key_name, 0,
67 &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
68 (key_type == REG_SZ || key_type == REG_EXPAND_SZ))
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 if (key_type == REG_EXPAND_SZ)
77 int len = ExpandEnvironmentStringsW (wc_temp, dummy, 1);
80 wchar_t *wc_temp_expanded = g_new (wchar_t, len);
81 if (ExpandEnvironmentStringsW (wc_temp, wc_temp_expanded, len) == len)
82 value_utf8 = g_utf16_to_utf8 (wc_temp_expanded, -1, NULL, NULL, NULL);
83 g_free (wc_temp_expanded);
88 value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
96 RegCloseKey (reg_key);
102 g_content_type_equals (const char *type1,
105 char *progid1, *progid2;
108 g_return_val_if_fail (type1 != NULL, FALSE);
109 g_return_val_if_fail (type2 != NULL, FALSE);
111 if (g_ascii_strcasecmp (type1, type2) == 0)
115 progid1 = get_registry_classes_key (type1, NULL);
116 progid2 = get_registry_classes_key (type2, NULL);
117 if (progid1 != NULL && progid2 != NULL &&
118 strcmp (progid1, progid2) == 0)
127 g_content_type_is_a (const char *type,
128 const char *supertype)
133 g_return_val_if_fail (type != NULL, FALSE);
134 g_return_val_if_fail (supertype != NULL, FALSE);
136 if (g_content_type_equals (type, supertype))
140 value_utf8 = get_registry_classes_key (type, L"PerceivedType");
141 if (value_utf8 && strcmp (value_utf8, supertype) == 0)
149 g_content_type_is_unknown (const char *type)
151 g_return_val_if_fail (type != NULL, FALSE);
153 return strcmp ("*", type) == 0;
157 g_content_type_get_description (const char *type)
162 g_return_val_if_fail (type != NULL, NULL);
164 progid = get_registry_classes_key (type, NULL);
167 description = get_registry_classes_key (progid, NULL);
174 if (g_content_type_is_unknown (type))
175 return g_strdup (_("Unknown type"));
176 return g_strdup_printf (_("%s filetype"), type);
180 g_content_type_get_mime_type (const char *type)
184 g_return_val_if_fail (type != NULL, NULL);
186 mime = get_registry_classes_key (type, L"Content Type");
189 else if (g_content_type_is_unknown (type))
190 return g_strdup ("application/octet-stream");
191 else if (*type == '.')
192 return g_strdup_printf ("application/x-ext-%s", type+1);
193 /* TODO: Map "image" to "image/ *", etc? */
195 return g_strdup ("application/octet-stream");
198 G_LOCK_DEFINE_STATIC (_type_icons);
199 static GHashTable *_type_icons = NULL;
202 g_content_type_get_icon (const char *type)
207 g_return_val_if_fail (type != NULL, NULL);
209 /* In the Registry icons are the default value of
210 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
212 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
213 REG_SZ: shimgvw.dll,3
215 G_LOCK (_type_icons);
217 _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
218 name = g_hash_table_lookup (_type_icons, type);
219 if (!name && type[0] == '.')
221 /* double lookup by extension */
222 gchar *key = get_registry_classes_key (type, NULL);
224 key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
227 gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
231 name = get_registry_classes_key (key, NULL);
232 if (name && strcmp (name, "%1") == 0)
238 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
242 /* icon-name similar to how it was with gtk-2-12 */
245 themed_icon = g_themed_icon_new (name);
249 /* if not found an icon fall back to gtk-builtins */
250 name = strcmp (type, "inode/directory") == 0 ? "gtk-directory" :
251 g_content_type_can_be_executable (type) ? "gtk-execute" : "gtk-file";
252 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
253 themed_icon = g_themed_icon_new_with_default_fallbacks (name);
255 G_UNLOCK (_type_icons);
257 return G_ICON (themed_icon);
261 g_content_type_can_be_executable (const char *type)
263 g_return_val_if_fail (type != NULL, FALSE);
265 if (strcmp (type, ".exe") == 0 ||
266 strcmp (type, ".com") == 0 ||
267 strcmp (type, ".bat") == 0)
270 /* TODO: Also look at PATHEXT, which lists the extensions for
271 * "scripts" in addition to those for true binary executables.
273 * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
274 * right now, for instance). And in a sense, all associated file
275 * types are "executable" on Windows... You can just type foo.jpg as
276 * a command name in cmd.exe, and it will run the application
277 * associated with .jpg. Hard to say what this API actually means
285 looks_like_text (const guchar *data,
290 for (i = 0; i < data_size; i++)
293 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c))
300 g_content_type_from_mime_type (const char *mime_type)
302 char *key, *content_type;
304 g_return_val_if_fail (mime_type != NULL, NULL);
306 key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
307 content_type = get_registry_classes_key (key, L"Extension");
314 g_content_type_guess (const char *filename,
317 gboolean *result_uncertain)
327 basename = g_path_get_basename (filename);
328 dot = strrchr (basename, '.');
330 type = g_strdup (dot);
337 if (data && looks_like_text (data, data_size))
338 return g_strdup (".txt");
340 return g_strdup ("*");
344 g_content_types_get_registered (void)
347 wchar_t keyname[256];
355 while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
362 NULL) == ERROR_SUCCESS)
364 key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
367 if (*key_utf8 == '.')
368 types = g_list_prepend (types, key_utf8);
376 return g_list_reverse (types);
379 #else /* !G_OS_WIN32 - Unix specific version */
383 #define XDG_PREFIX _gio_xdg
384 #include "xdgmime/xdgmime.h"
386 /* We lock this mutex whenever we modify global state in this module. */
387 G_LOCK_DEFINE_STATIC (gio_xdgmime);
390 _g_unix_content_type_get_sniff_len (void)
394 G_LOCK (gio_xdgmime);
395 size = xdg_mime_get_max_buffer_extents ();
396 G_UNLOCK (gio_xdgmime);
402 _g_unix_content_type_unalias (const char *type)
406 G_LOCK (gio_xdgmime);
407 res = g_strdup (xdg_mime_unalias_mime_type (type));
408 G_UNLOCK (gio_xdgmime);
414 _g_unix_content_type_get_parents (const char *type)
421 array = g_ptr_array_new ();
423 G_LOCK (gio_xdgmime);
425 umime = xdg_mime_unalias_mime_type (type);
427 g_ptr_array_add (array, g_strdup (umime));
429 parents = xdg_mime_list_mime_parents (umime);
430 for (i = 0; parents && parents[i] != NULL; i++)
431 g_ptr_array_add (array, g_strdup (parents[i]));
435 G_UNLOCK (gio_xdgmime);
437 g_ptr_array_add (array, NULL);
439 return (char **)g_ptr_array_free (array, FALSE);
443 * g_content_type_equals:
444 * @type1: a content type string.
445 * @type2: a content type string.
447 * Compares two content types for equality.
449 * Returns: %TRUE if the two strings are identical or equivalent,
453 g_content_type_equals (const char *type1,
458 g_return_val_if_fail (type1 != NULL, FALSE);
459 g_return_val_if_fail (type2 != NULL, FALSE);
461 G_LOCK (gio_xdgmime);
462 res = xdg_mime_mime_type_equal (type1, type2);
463 G_UNLOCK (gio_xdgmime);
469 * g_content_type_is_a:
470 * @type: a content type string.
471 * @supertype: a string.
473 * Determines if @type is a subset of @supertype.
475 * Returns: %TRUE if @type is a kind of @supertype,
479 g_content_type_is_a (const char *type,
480 const char *supertype)
484 g_return_val_if_fail (type != NULL, FALSE);
485 g_return_val_if_fail (supertype != NULL, FALSE);
487 G_LOCK (gio_xdgmime);
488 res = xdg_mime_mime_type_subclass (type, supertype);
489 G_UNLOCK (gio_xdgmime);
495 * g_content_type_is_unknown:
496 * @type: a content type string.
498 * Checks if the content type is the generic "unknown" type.
499 * On unix this is the "application/octet-stream" mimetype,
500 * while on win32 it is "*".
502 * Returns: %TRUE if the type is the unknown type.
505 g_content_type_is_unknown (const char *type)
507 g_return_val_if_fail (type != NULL, FALSE);
509 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
515 MIME_TAG_TYPE_COMMENT
520 int current_lang_level;
521 int comment_lang_level;
527 language_level (const char *lang)
529 const char * const *lang_list;
532 /* The returned list is sorted from most desirable to least
533 desirable and always contains the default locale "C". */
534 lang_list = g_get_language_names ();
536 for (i = 0; lang_list[i]; i++)
537 if (strcmp (lang_list[i], lang) == 0)
544 mime_info_start_element (GMarkupParseContext *context,
545 const gchar *element_name,
546 const gchar **attribute_names,
547 const gchar **attribute_values,
553 MimeParser *parser = user_data;
555 if (strcmp (element_name, "comment") == 0)
558 for (i = 0; attribute_names[i]; i++)
559 if (strcmp (attribute_names[i], "xml:lang") == 0)
561 lang = attribute_values[i];
565 parser->current_lang_level = language_level (lang);
566 parser->current_type = MIME_TAG_TYPE_COMMENT;
569 parser->current_type = MIME_TAG_TYPE_OTHER;
574 mime_info_end_element (GMarkupParseContext *context,
575 const gchar *element_name,
579 MimeParser *parser = user_data;
581 parser->current_type = MIME_TAG_TYPE_OTHER;
585 mime_info_text (GMarkupParseContext *context,
591 MimeParser *parser = user_data;
593 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
594 parser->current_lang_level > parser->comment_lang_level)
596 g_free (parser->comment);
597 parser->comment = g_strndup (text, text_len);
598 parser->comment_lang_level = parser->current_lang_level;
603 load_comment_for_mime_helper (const char *dir,
604 const char *basename)
606 GMarkupParseContext *context;
607 char *filename, *data;
610 MimeParser parse_data = {0};
611 GMarkupParser parser = {
612 mime_info_start_element,
613 mime_info_end_element,
617 filename = g_build_filename (dir, "mime", basename, NULL);
619 res = g_file_get_contents (filename, &data, &len, NULL);
624 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
625 res = g_markup_parse_context_parse (context, data, len, NULL);
627 g_markup_parse_context_free (context);
632 return parse_data.comment;
637 load_comment_for_mime (const char *mimetype)
639 const char * const* dirs;
644 basename = g_strdup_printf ("%s.xml", mimetype);
646 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
653 dirs = g_get_system_data_dirs ();
655 for (i = 0; dirs[i] != NULL; i++)
657 comment = load_comment_for_mime_helper (dirs[i], basename);
666 return g_strdup_printf (_("%s type"), mimetype);
670 * g_content_type_get_description:
671 * @type: a content type string.
673 * Gets the human readable description of the content type.
675 * Returns: a short description of the content type @type.
678 g_content_type_get_description (const char *type)
680 static GHashTable *type_comment_cache = NULL;
683 g_return_val_if_fail (type != NULL, NULL);
685 G_LOCK (gio_xdgmime);
686 type = xdg_mime_unalias_mime_type (type);
688 if (type_comment_cache == NULL)
689 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
691 comment = g_hash_table_lookup (type_comment_cache, type);
692 comment = g_strdup (comment);
693 G_UNLOCK (gio_xdgmime);
698 comment = load_comment_for_mime (type);
700 G_LOCK (gio_xdgmime);
701 g_hash_table_insert (type_comment_cache,
704 G_UNLOCK (gio_xdgmime);
710 * g_content_type_get_mime_type:
711 * @type: a content type string.
713 * Gets the mime-type for the content type. If one is registered
715 * Returns: the registered mime-type for the given @type, or NULL if unknown.
718 g_content_type_get_mime_type (const char *type)
720 g_return_val_if_fail (type != NULL, NULL);
722 return g_strdup (type);
726 * g_content_type_get_icon:
727 * @type: a content type string.
729 * Gets the icon for a content type.
731 * Returns: #GIcon corresponding to the content type.
734 g_content_type_get_icon (const char *type)
736 char *mimetype_icon, *generic_mimetype_icon, *q;
737 char *xdg_mimetype_icon, *legacy_mimetype_icon;
743 g_return_val_if_fail (type != NULL, NULL);
745 G_LOCK (gio_xdgmime);
746 xdg_mimetype_icon = g_strdup (xdg_mime_get_icon (type));
747 G_UNLOCK (gio_xdgmime);
749 mimetype_icon = g_strdup (type);
751 while ((q = strchr (mimetype_icon, '/')) != NULL)
754 p = strchr (type, '/');
756 p = type + strlen (type);
758 /* Not all icons have migrated to the new icon theme spec, look for old names too */
759 legacy_mimetype_icon = g_strconcat ("gnome-mime-", mimetype_icon, NULL);
761 generic_mimetype_icon = g_malloc (p - type + strlen ("-x-generic") + 1);
762 memcpy (generic_mimetype_icon, type, p - type);
763 memcpy (generic_mimetype_icon + (p - type), "-x-generic", strlen ("-x-generic"));
764 generic_mimetype_icon[(p - type) + strlen ("-x-generic")] = 0;
766 if (xdg_mimetype_icon)
767 icon_names[n++] = xdg_mimetype_icon;
769 icon_names[n++] = mimetype_icon;
770 icon_names[n++] = legacy_mimetype_icon;
771 icon_names[n++] = generic_mimetype_icon;
773 themed_icon = g_themed_icon_new_from_names (icon_names, n);
775 g_free (xdg_mimetype_icon);
776 g_free (mimetype_icon);
777 g_free (legacy_mimetype_icon);
778 g_free (generic_mimetype_icon);
784 * g_content_type_can_be_executable:
785 * @type: a content type string.
787 * Checks if a content type can be executable. Note that for instance
788 * things like text files can be executables (i.e. scripts and batch files).
790 * Returns: %TRUE if the file type corresponds to a type that
791 * can be executable, %FALSE otherwise.
794 g_content_type_can_be_executable (const char *type)
796 g_return_val_if_fail (type != NULL, FALSE);
798 if (g_content_type_is_a (type, "application/x-executable") ||
799 g_content_type_is_a (type, "text/plain"))
806 looks_like_text (const guchar *data, gsize data_size)
811 for (i = 0; i < data_size; i++)
815 if (g_ascii_iscntrl (c) &&
816 !g_ascii_isspace (c))
823 * g_content_type_from_mime_type:
824 * @mime_type: a mime type string.
826 * Tries to find a content type based on the mime type name.
828 * Returns: Newly allocated string with content type or NULL when does not know.
833 g_content_type_from_mime_type (const char *mime_type)
835 g_return_val_if_fail (mime_type != NULL, NULL);
837 /* mime type and content type are same on unixes */
838 return g_strdup (mime_type);
842 * g_content_type_guess:
843 * @filename: a string.
844 * @data: a stream of data.
845 * @data_size: the size of @data.
846 * @result_uncertain: a flag indicating the certainty of the
849 * Guesses the content type based on example data. If the function is
850 * uncertain, @result_uncertain will be set to %TRUE.
852 * Returns: a string indicating a guessed content type for the
856 g_content_type_guess (const char *filename,
859 gboolean *result_uncertain)
862 const char *name_mimetypes[10], *sniffed_mimetype;
865 int n_name_mimetypes;
869 n_name_mimetypes = 0;
870 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
872 if (result_uncertain)
873 *result_uncertain = FALSE;
875 G_LOCK (gio_xdgmime);
879 basename = g_path_get_basename (filename);
880 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
884 /* Got an extension match, and no conflicts. This is it. */
885 if (n_name_mimetypes == 1)
887 G_UNLOCK (gio_xdgmime);
888 return g_strdup (name_mimetypes[0]);
893 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
894 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
896 looks_like_text (data, data_size))
897 sniffed_mimetype = "text/plain";
900 if (n_name_mimetypes == 0)
902 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
904 *result_uncertain = TRUE;
906 mimetype = g_strdup (sniffed_mimetype);
911 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
913 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
914 mimetype = g_strdup (sniffed_mimetype);
917 /* There are conflicts between the name matches and we have a sniffed
918 type, use that as a tie breaker. */
920 for (i = 0; i < n_name_mimetypes; i++)
922 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
924 /* This nametype match is derived from (or the same as) the sniffed type).
925 This is probably it. */
926 mimetype = g_strdup (name_mimetypes[i]);
933 if (mimetype == NULL)
935 /* Conflicts, and sniffed type was no help or not there. Guess on the first one */
936 mimetype = g_strdup (name_mimetypes[0]);
937 if (result_uncertain)
938 *result_uncertain = TRUE;
942 G_UNLOCK (gio_xdgmime);
948 enumerate_mimetypes_subdir (const char *dir,
950 GHashTable *mimetypes)
959 while ((ent = readdir (d)) != NULL)
961 if (g_str_has_suffix (ent->d_name, ".xml"))
963 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
964 g_hash_table_insert (mimetypes, mimetype, NULL);
972 enumerate_mimetypes_dir (const char *dir,
973 GHashTable *mimetypes)
980 mimedir = g_build_filename (dir, "mime", NULL);
982 d = opendir (mimedir);
985 while ((ent = readdir (d)) != NULL)
987 if (strcmp (ent->d_name, "packages") != 0)
989 name = g_build_filename (mimedir, ent->d_name, NULL);
990 if (g_file_test (name, G_FILE_TEST_IS_DIR))
991 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
1002 * g_content_types_get_registered:
1004 * Gets a list of strings containing all the registered content types
1005 * known to the system. The list and its data should be freed using
1006 * @g_list_foreach(list, g_free, NULL) and @g_list_free(list)
1007 * Returns: #GList of the registered content types.
1010 g_content_types_get_registered (void)
1012 const char * const* dirs;
1013 GHashTable *mimetypes;
1014 GHashTableIter iter;
1019 mimetypes = g_hash_table_new (g_str_hash, g_str_equal);
1021 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
1022 dirs = g_get_system_data_dirs ();
1024 for (i = 0; dirs[i] != NULL; i++)
1025 enumerate_mimetypes_dir (dirs[i], mimetypes);
1028 g_hash_table_iter_init (&iter, mimetypes);
1029 while (g_hash_table_iter_next (&iter, &key, NULL))
1030 l = g_list_prepend (l, key);
1032 g_hash_table_destroy (mimetypes);
1037 #endif /* Unix version */
1039 #define __G_CONTENT_TYPE_C__
1040 #include "gioaliasdef.c"