1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include <sys/types.h>
27 #include "gcontenttypeprivate.h"
33 * SECTION:gcontenttype
34 * @short_description: Platform-specific content typing
36 * A content type is a platform specific string that defines the type
37 * of a file. On unix it is a mime type, on win32 it is an extension string
38 * like ".doc", ".txt" or a percieved string like "audio". Such strings
39 * can be looked up in the registry at HKEY_CLASSES_ROOT.
47 get_registry_classes_key (const char *subdir,
48 const wchar_t *key_name)
59 wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
60 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
61 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS &&
62 RegQueryValueExW (reg_key, key_name, 0,
63 &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
66 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
67 RegQueryValueExW (reg_key, key_name, 0,
68 &key_type, (LPBYTE) wc_temp, &nbytes);
69 wc_temp[nbytes/2] = '\0';
70 value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
76 RegCloseKey (reg_key);
82 g_content_type_equals (const char *type1,
85 char *progid1, *progid2;
88 g_return_val_if_fail (type1 != NULL, FALSE);
89 g_return_val_if_fail (type2 != NULL, FALSE);
91 if (g_ascii_strcasecmp (type1, type2) == 0)
95 progid1 = get_registry_classes_key (type1, NULL);
96 progid2 = get_registry_classes_key (type2, NULL);
97 if (progid1 != NULL && progid2 != NULL &&
98 strcmp (progid1, progid2) == 0)
107 g_content_type_is_a (const char *type,
108 const char *supertype)
113 g_return_val_if_fail (type != NULL, FALSE);
114 g_return_val_if_fail (supertype != NULL, FALSE);
116 if (g_content_type_equals (type, supertype))
120 value_utf8 = get_registry_classes_key (type, L"PerceivedType");
121 if (value_utf8 && strcmp (value_utf8, supertype) == 0)
129 g_content_type_is_unknown (const char *type)
131 g_return_val_if_fail (type != NULL, FALSE);
133 return strcmp ("*", type) == 0;
137 g_content_type_get_description (const char *type)
142 g_return_val_if_fail (type != NULL, NULL);
144 progid = get_registry_classes_key (type, NULL);
147 description = get_registry_classes_key (progid, NULL);
154 if (g_content_type_is_unknown (type))
155 return g_strdup (_("Unknown type"));
156 return g_strdup_printf (_("%s filetype"), type);
160 g_content_type_get_mime_type (const char *type)
164 g_return_val_if_fail (type != NULL, NULL);
166 mime = get_registry_classes_key (type, L"Content Type");
169 else if (g_content_type_is_unknown (type))
170 return g_strdup ("application/octet-stream");
171 else if (*type == '.')
172 return g_strdup_printf ("application/x-ext-%s", type+1);
173 /* TODO: Map "image" to "image/ *", etc? */
175 return g_strdup ("application/octet-stream");
179 g_content_type_get_icon (const char *type)
181 g_return_val_if_fail (type != NULL, NULL);
183 /* TODO: How do we represent icons???
184 In the registry they are the default value of
185 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
187 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
188 REG_SZ: shimgvw.dll,3
194 g_content_type_can_be_executable (const char *type)
196 g_return_val_if_fail (type != NULL, FALSE);
198 if (strcmp (type, ".exe") == 0 ||
199 strcmp (type, ".com") == 0 ||
200 strcmp (type, ".bat") == 0)
206 looks_like_text (const guchar *data,
211 for (i = 0; i < data_size; i++)
214 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c))
221 g_content_type_guess (const char *filename,
224 gboolean *result_uncertain)
234 basename = g_path_get_basename (filename);
235 dot = strrchr (basename, '.');
237 type = g_strdup (dot);
244 if (data && looks_like_text (data, data_size))
245 return g_strdup (".txt");
247 return g_strdup ("*");
251 g_content_types_get_registered (void)
254 wchar_t keyname[256];
262 while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
269 NULL) == ERROR_SUCCESS)
271 key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
274 if (*key_utf8 == '.')
275 types = g_list_prepend (types, key_utf8);
283 return g_list_reverse (types);
286 #else /* !G_OS_WIN32 - Unix specific version */
290 #define XDG_PREFIX _gio_xdg
291 #include "xdgmime/xdgmime.h"
293 /* We lock this mutex whenever we modify global state in this module. */
294 G_LOCK_DEFINE_STATIC (gio_xdgmime);
297 _g_unix_content_type_get_sniff_len (void)
301 G_LOCK (gio_xdgmime);
302 size = xdg_mime_get_max_buffer_extents ();
303 G_UNLOCK (gio_xdgmime);
309 _g_unix_content_type_unalias (const char *type)
313 G_LOCK (gio_xdgmime);
314 res = g_strdup (xdg_mime_unalias_mime_type (type));
315 G_UNLOCK (gio_xdgmime);
321 _g_unix_content_type_get_parents (const char *type)
324 const char **parents;
328 array = g_ptr_array_new ();
330 G_LOCK (gio_xdgmime);
332 umime = xdg_mime_unalias_mime_type (type);
333 g_ptr_array_add (array, g_strdup (umime));
335 parents = xdg_mime_get_mime_parents (umime);
336 for (i = 0; parents && parents[i] != NULL; i++)
337 g_ptr_array_add (array, g_strdup (parents[i]));
339 G_UNLOCK (gio_xdgmime);
341 g_ptr_array_add (array, NULL);
343 return (char **)g_ptr_array_free (array, FALSE);
347 * g_content_type_equals:
348 * @type1: a content type string.
349 * @type2: a content type string.
351 * Compares two content types for equality.
353 * Returns: %TRUE if the two strings are identical or equivalent,
357 g_content_type_equals (const char *type1,
362 g_return_val_if_fail (type1 != NULL, FALSE);
363 g_return_val_if_fail (type2 != NULL, FALSE);
365 G_LOCK (gio_xdgmime);
366 res = xdg_mime_mime_type_equal (type1, type2);
367 G_UNLOCK (gio_xdgmime);
373 * g_content_type_is_a:
374 * @type: a content type string.
375 * @supertype: a string.
377 * Determines if @type is a subset of @supertype.
379 * Returns: %TRUE if @type is a kind of @supertype,
383 g_content_type_is_a (const char *type,
384 const char *supertype)
388 g_return_val_if_fail (type != NULL, FALSE);
389 g_return_val_if_fail (supertype != NULL, FALSE);
391 G_LOCK (gio_xdgmime);
392 res = xdg_mime_mime_type_subclass (type, supertype);
393 G_UNLOCK (gio_xdgmime);
399 * g_content_type_is_unknown:
400 * @type: a content type string.
402 * Checks if the content type is the generic "unknown" type.
403 * On unix this is the "application/octet-stream" mimetype,
404 * while on win32 it is "*".
406 * Returns: %TRUE if the type is the unknown type.
409 g_content_type_is_unknown (const char *type)
411 g_return_val_if_fail (type != NULL, FALSE);
413 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
419 MIME_TAG_TYPE_COMMENT
424 int current_lang_level;
425 int comment_lang_level;
431 language_level (const char *lang)
433 const char * const *lang_list;
436 /* The returned list is sorted from most desirable to least
437 desirable and always contains the default locale "C". */
438 lang_list = g_get_language_names ();
440 for (i = 0; lang_list[i]; i++)
441 if (strcmp (lang_list[i], lang) == 0)
448 mime_info_start_element (GMarkupParseContext *context,
449 const gchar *element_name,
450 const gchar **attribute_names,
451 const gchar **attribute_values,
457 MimeParser *parser = user_data;
459 if (strcmp (element_name, "comment") == 0)
462 for (i = 0; attribute_names[i]; i++)
463 if (strcmp (attribute_names[i], "xml:lang") == 0)
465 lang = attribute_values[i];
469 parser->current_lang_level = language_level (lang);
470 parser->current_type = MIME_TAG_TYPE_COMMENT;
473 parser->current_type = MIME_TAG_TYPE_OTHER;
478 mime_info_end_element (GMarkupParseContext *context,
479 const gchar *element_name,
483 MimeParser *parser = user_data;
485 parser->current_type = MIME_TAG_TYPE_OTHER;
489 mime_info_text (GMarkupParseContext *context,
495 MimeParser *parser = user_data;
497 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
498 parser->current_lang_level > parser->comment_lang_level)
500 g_free (parser->comment);
501 parser->comment = g_strndup (text, text_len);
502 parser->comment_lang_level = parser->current_lang_level;
507 load_comment_for_mime_helper (const char *dir,
508 const char *basename)
510 GMarkupParseContext *context;
511 char *filename, *data;
514 MimeParser parse_data = {0};
515 GMarkupParser parser = {
516 mime_info_start_element,
517 mime_info_end_element,
521 filename = g_build_filename (dir, "mime", basename, NULL);
523 res = g_file_get_contents (filename, &data, &len, NULL);
528 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
529 res = g_markup_parse_context_parse (context, data, len, NULL);
531 g_markup_parse_context_free (context);
536 return parse_data.comment;
541 load_comment_for_mime (const char *mimetype)
543 const char * const* dirs;
548 basename = g_strdup_printf ("%s.xml", mimetype);
550 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
557 dirs = g_get_system_data_dirs ();
559 for (i = 0; dirs[i] != NULL; i++)
561 comment = load_comment_for_mime_helper (dirs[i], basename);
570 return g_strdup_printf (_("%s type"), mimetype);
574 * g_content_type_get_description:
575 * @type: a content type string.
577 * Gets the human readable description of the content type.
579 * Returns: a short description of the content type @type.
582 g_content_type_get_description (const char *type)
584 static GHashTable *type_comment_cache = NULL;
587 g_return_val_if_fail (type != NULL, NULL);
589 G_LOCK (gio_xdgmime);
590 if (type_comment_cache == NULL)
591 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
593 comment = g_hash_table_lookup (type_comment_cache, type);
594 comment = g_strdup (comment);
595 G_UNLOCK (gio_xdgmime);
600 comment = load_comment_for_mime (type);
602 G_LOCK (gio_xdgmime);
603 g_hash_table_insert (type_comment_cache,
606 G_UNLOCK (gio_xdgmime);
612 * g_content_type_get_mime_type:
613 * @type: a content type string.
615 * Gets the mime-type for the content type. If one is registered
617 * Returns: the registered mime-type for the given @type, or NULL if unknown.
620 g_content_type_get_mime_type (const char *type)
622 g_return_val_if_fail (type != NULL, NULL);
624 return g_strdup (type);
628 * g_content_type_get_icon:
629 * @type: a content type string.
631 * Gets the icon for a content type.
633 * Returns: #GIcon corresponding to the content type.
636 g_content_type_get_icon (const char *type)
638 g_return_val_if_fail (type != NULL, NULL);
640 /* TODO: Implement */
645 * g_content_type_can_be_executable:
646 * @type: a content type string.
648 * Checks if a content type can be executable. Note that for instance
649 * things like text files can be executables (i.e. scripts and batch files).
651 * Returns: %TRUE if the file type corresponds to a type that
652 * can be executable, %FALSE otherwise.
655 g_content_type_can_be_executable (const char *type)
657 g_return_val_if_fail (type != NULL, FALSE);
659 if (g_content_type_is_a (type, "application/x-executable") ||
660 g_content_type_is_a (type, "text/plain"))
667 looks_like_text (const guchar *data, gsize data_size)
670 for (i = 0; i < data_size; i++)
672 if g_ascii_iscntrl (data[i])
679 * g_content_type_guess:
680 * @filename: a string.
681 * @data: a stream of data.
682 * @data_size: the size of @data.
683 * @result_uncertain: a flag indicating the certainty of the
686 * Guesses the content type based on example data. If the function is
687 * uncertain, @result_uncertain will be set to %TRUE.
689 * Returns: a string indicating a guessed content type for the
693 g_content_type_guess (const char *filename,
696 gboolean *result_uncertain)
699 const char *name_mimetypes[10], *sniffed_mimetype;
702 int n_name_mimetypes;
706 n_name_mimetypes = 0;
707 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
709 if (result_uncertain)
710 *result_uncertain = FALSE;
712 G_LOCK (gio_xdgmime);
716 basename = g_path_get_basename (filename);
717 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
721 /* Got an extension match, and no conflicts. This is it. */
722 if (n_name_mimetypes == 1)
724 G_UNLOCK (gio_xdgmime);
725 return g_strdup (name_mimetypes[0]);
730 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
731 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
733 looks_like_text (data, data_size))
734 sniffed_mimetype = "text/plain";
737 if (n_name_mimetypes == 0)
739 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
741 *result_uncertain = TRUE;
743 mimetype = g_strdup (sniffed_mimetype);
748 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
750 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
751 mimetype = g_strdup (sniffed_mimetype);
754 /* There are conflicts between the name matches and we have a sniffed
755 type, use that as a tie breaker. */
757 for (i = 0; i < n_name_mimetypes; i++)
759 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
761 /* This nametype match is derived from (or the same as) the sniffed type).
762 This is probably it. */
763 mimetype = g_strdup (name_mimetypes[i]);
770 if (mimetype == NULL)
772 /* Conflicts, and sniffed type was no help or not there. guess on the first one */
773 mimetype = g_strdup (name_mimetypes[0]);
774 if (result_uncertain)
775 *result_uncertain = TRUE;
779 G_UNLOCK (gio_xdgmime);
785 enumerate_mimetypes_subdir (const char *dir,
787 GHashTable *mimetypes)
796 while ((ent = readdir (d)) != NULL)
798 if (g_str_has_suffix (ent->d_name, ".xml"))
800 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
801 g_hash_table_insert (mimetypes, mimetype, NULL);
809 enumerate_mimetypes_dir (const char *dir,
810 GHashTable *mimetypes)
817 mimedir = g_build_filename (dir, "mime", NULL);
819 d = opendir (mimedir);
822 while ((ent = readdir (d)) != NULL)
824 if (strcmp (ent->d_name, "packages") != 0)
826 name = g_build_filename (mimedir, ent->d_name, NULL);
827 if (g_file_test (name, G_FILE_TEST_IS_DIR))
828 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
839 * g_content_types_get_registered:
841 * Gets a list of strings containing all the registered content types
842 * known to the system.
844 * Returns: #GList of the registered content types.
847 g_content_types_get_registered (void)
849 const char * const* dirs;
850 GHashTable *mimetypes;
856 mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
858 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
859 dirs = g_get_system_data_dirs ();
861 for (i = 0; dirs[i] != NULL; i++)
862 enumerate_mimetypes_dir (dirs[i], mimetypes);
865 g_hash_table_iter_init (&iter, mimetypes);
866 while (g_hash_table_iter_next (&iter, &key, NULL))
867 l = g_list_prepend (l, key);
869 g_hash_table_destroy (mimetypes);
874 #endif /* Unix version */
876 #define __G_CONTENT_TYPE_C__
877 #include "gioaliasdef.c"