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>
28 #include "gcontenttypeprivate.h"
34 * SECTION:gcontenttype
35 * @short_description: platform specific content typing
37 * A content type is a platform specific string that defines the type
38 * of a file. On unix it is a mime type, on win32 it is an extension string
39 * like ".doc", ".txt" or a percieved string like "audio". Such strings
40 * can be looked up in the registry at HKEY_CLASSES_ROOT.
48 get_registry_classes_key (const char *subdir,
49 const wchar_t *key_name)
60 wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
61 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
62 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS &&
63 RegQueryValueExW (reg_key, key_name, 0,
64 &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
67 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
68 RegQueryValueExW (reg_key, key_name, 0,
69 &key_type, (LPBYTE) wc_temp, &nbytes);
70 wc_temp[nbytes/2] = '\0';
71 value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
77 RegCloseKey (reg_key);
83 g_content_type_equals (const char *type1,
86 char *progid1, *progid2;
89 g_return_val_if_fail (type1 != NULL, FALSE);
90 g_return_val_if_fail (type2 != NULL, FALSE);
92 if (g_ascii_strcasecmp (type1, type2) == 0)
96 progid1 = get_registry_classes_key (type1, NULL);
97 progid2 = get_registry_classes_key (type2, NULL);
98 if (progid1 != NULL && progid2 != NULL &&
99 strcmp (progid1, progid2) == 0)
108 g_content_type_is_a (const char *type,
109 const char *supertype)
114 g_return_val_if_fail (type != NULL, FALSE);
115 g_return_val_if_fail (supertype != NULL, FALSE);
117 if (g_content_type_equals (type, supertype))
121 value_utf8 = get_registry_classes_key (type, L"PerceivedType");
122 if (value_utf8 && strcmp (value_utf8, supertype) == 0)
130 g_content_type_is_unknown (const char *type)
132 g_return_val_if_fail (type != NULL, FALSE);
134 return strcmp ("*", type) == 0;
138 g_content_type_get_description (const char *type)
143 g_return_val_if_fail (type != NULL, NULL);
145 progid = get_registry_classes_key (type, NULL);
148 description = get_registry_classes_key (progid, NULL);
155 if (g_content_type_is_unknown (type))
156 return g_strdup (_("Unknown type"));
157 return g_strdup_printf (_("%s filetype"), type);
161 g_content_type_get_mime_type (const char *type)
165 g_return_val_if_fail (type != NULL, NULL);
167 mime = get_registry_classes_key (type, L"Content Type");
170 else if (g_content_type_is_unknown (type))
171 return g_strdup ("application/octet-stream");
172 else if (*type == '.')
173 return g_strdup_printf ("application/x-ext-%s", type+1);
174 /* TODO: Map "image" to "image/ *", etc? */
176 return g_strdup ("application/octet-stream");
180 g_content_type_get_icon (const char *type)
182 g_return_val_if_fail (type != NULL, NULL);
184 /* TODO: How do we represent icons???
185 In the registry they are the default value of
186 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
188 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
189 REG_SZ: shimgvw.dll,3
195 g_content_type_can_be_executable (const char *type)
197 g_return_val_if_fail (type != NULL, FALSE);
199 if (strcmp (type, ".exe") == 0 ||
200 strcmp (type, ".com") == 0 ||
201 strcmp (type, ".bat") == 0)
207 looks_like_text (const guchar *data, gsize data_size)
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 */
288 #define XDG_PREFIX _gio_xdg
289 #include "xdgmime/xdgmime.h"
291 /* We lock this mutex whenever we modify global state in this module. */
292 G_LOCK_DEFINE_STATIC (gio_xdgmime);
295 _g_unix_content_type_get_sniff_len (void)
299 G_LOCK (gio_xdgmime);
300 size = xdg_mime_get_max_buffer_extents ();
301 G_UNLOCK (gio_xdgmime);
307 _g_unix_content_type_unalias (const char *type)
311 G_LOCK (gio_xdgmime);
312 res = g_strdup (xdg_mime_unalias_mime_type (type));
313 G_UNLOCK (gio_xdgmime);
319 _g_unix_content_type_get_parents (const char *type)
322 const char **parents;
326 array = g_ptr_array_new ();
328 G_LOCK (gio_xdgmime);
330 umime = xdg_mime_unalias_mime_type (type);
331 g_ptr_array_add (array, g_strdup (umime));
333 parents = xdg_mime_get_mime_parents (umime);
334 for (i = 0; parents && parents[i] != NULL; i++)
335 g_ptr_array_add (array, g_strdup (parents[i]));
337 G_UNLOCK (gio_xdgmime);
339 g_ptr_array_add (array, NULL);
341 return (char **)g_ptr_array_free (array, FALSE);
345 * g_content_type_equals:
346 * @type1: a content type string.
347 * @type2: a content type string.
349 * Compares two content types for equality.
351 * Returns: %TRUE if the two strings are identical or equivalent,
355 g_content_type_equals (const char *type1,
360 g_return_val_if_fail (type1 != NULL, FALSE);
361 g_return_val_if_fail (type2 != NULL, FALSE);
363 G_LOCK (gio_xdgmime);
364 res = xdg_mime_mime_type_equal (type1, type2);
365 G_UNLOCK (gio_xdgmime);
371 * g_content_type_is_a:
372 * @type: a content type string.
373 * @supertype: a string.
375 * Determines if @type is a subset of @supertype.
377 * Returns: %TRUE if @type is a kind of @supertype,
381 g_content_type_is_a (const char *type,
382 const char *supertype)
386 g_return_val_if_fail (type != NULL, FALSE);
387 g_return_val_if_fail (supertype != NULL, FALSE);
389 G_LOCK (gio_xdgmime);
390 res = xdg_mime_mime_type_subclass (type, supertype);
391 G_UNLOCK (gio_xdgmime);
397 * g_content_type_is_unknown:
398 * @type: a content type string.
400 * Checks if the content type is known by GIO.
402 * Returns: %TRUE if the type is unknown.
405 g_content_type_is_unknown (const char *type)
407 g_return_val_if_fail (type != NULL, FALSE);
409 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
415 MIME_TAG_TYPE_COMMENT
420 int current_lang_level;
421 int comment_lang_level;
427 language_level (const char *lang)
429 const char * const *lang_list;
432 /* The returned list is sorted from most desirable to least
433 desirable and always contains the default locale "C". */
434 lang_list = g_get_language_names ();
436 for (i = 0; lang_list[i]; i++)
437 if (strcmp (lang_list[i], lang) == 0)
444 mime_info_start_element (GMarkupParseContext *context,
445 const gchar *element_name,
446 const gchar **attribute_names,
447 const gchar **attribute_values,
453 MimeParser *parser = user_data;
455 if (strcmp (element_name, "comment") == 0)
458 for (i = 0; attribute_names[i]; i++)
459 if (strcmp (attribute_names[i], "xml:lang") == 0)
461 lang = attribute_values[i];
465 parser->current_lang_level = language_level (lang);
466 parser->current_type = MIME_TAG_TYPE_COMMENT;
469 parser->current_type = MIME_TAG_TYPE_OTHER;
474 mime_info_end_element (GMarkupParseContext *context,
475 const gchar *element_name,
479 MimeParser *parser = user_data;
481 parser->current_type = MIME_TAG_TYPE_OTHER;
485 mime_info_text (GMarkupParseContext *context,
491 MimeParser *parser = user_data;
493 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
494 parser->current_lang_level > parser->comment_lang_level)
496 g_free (parser->comment);
497 parser->comment = g_strndup (text, text_len);
498 parser->comment_lang_level = parser->current_lang_level;
503 load_comment_for_mime_helper (const char *dir, const char *basename)
505 GMarkupParseContext *context;
506 char *filename, *data;
509 MimeParser parse_data = {0};
510 GMarkupParser parser = {
511 mime_info_start_element,
512 mime_info_end_element,
516 filename = g_build_filename (dir, "mime", basename, NULL);
518 res = g_file_get_contents (filename, &data, &len, NULL);
523 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
524 res = g_markup_parse_context_parse (context, data, len, NULL);
526 g_markup_parse_context_free (context);
531 return parse_data.comment;
536 load_comment_for_mime (const char *mimetype)
538 const char * const* dirs;
543 basename = g_strdup_printf ("%s.xml", mimetype);
545 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
552 dirs = g_get_system_data_dirs ();
554 for (i = 0; dirs[i] != NULL; i++)
556 comment = load_comment_for_mime_helper (dirs[i], basename);
565 return g_strdup_printf (_("%s type"), mimetype);
569 * g_content_type_get_description:
570 * @type: a content type string.
572 * Gets the human readable description of the content type.
574 * Returns: a short description of the content type @type.
577 g_content_type_get_description (const char *type)
579 static GHashTable *type_comment_cache = NULL;
582 g_return_val_if_fail (type != NULL, NULL);
584 G_LOCK (gio_xdgmime);
585 if (type_comment_cache == NULL)
586 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
588 comment = g_hash_table_lookup (type_comment_cache, type);
589 comment = g_strdup (comment);
590 G_UNLOCK (gio_xdgmime);
595 comment = load_comment_for_mime (type);
597 G_LOCK (gio_xdgmime);
598 g_hash_table_insert (type_comment_cache,
601 G_UNLOCK (gio_xdgmime);
607 * g_content_type_get_mime_type:
608 * @type: a content type string.
610 * Gets the mime-type for the content type.
612 * Returns: the registered mime-type for the given @type.
615 g_content_type_get_mime_type (const char *type)
617 g_return_val_if_fail (type != NULL, NULL);
619 return g_strdup (type);
623 * g_content_type_get_icon:
624 * @type: a content type string.
626 * Gets the icon for a content type.
628 * Returns: #GIcon corresponding to the content type.
631 g_content_type_get_icon (const char *type)
633 g_return_val_if_fail (type != NULL, NULL);
635 /* TODO: Implement */
640 * g_content_type_can_be_executable:
641 * @type: a content type string.
643 * Checks if a content type can be executable. Note that for instance
644 * things like text files can be executables (i.e. scripts and batch files).
646 * Returns: %TRUE if the file type corresponds to a type that
647 * can be executable, %FALSE otherwise.
650 g_content_type_can_be_executable (const char *type)
652 g_return_val_if_fail (type != NULL, FALSE);
654 if (g_content_type_is_a (type, "application/x-executable") ||
655 g_content_type_is_a (type, "text/plain"))
662 looks_like_text (const guchar *data, gsize data_size)
665 for (i = 0; i < data_size; i++)
667 if g_ascii_iscntrl (data[i])
674 * g_content_type_guess:
675 * @filename: a string.
676 * @data: a stream of data.
677 * @data_size: the size of @data.
678 * @result_uncertain: a flag indicating the certainty of the
681 * Guesses the content type based on example data. If the function is
682 * uncertain, @result_uncertain will be set to %TRUE.
684 * Returns: a string indicating a guessed content type for the
688 g_content_type_guess (const char *filename,
691 gboolean *result_uncertain)
694 const char *name_mimetypes[10], *sniffed_mimetype;
697 int n_name_mimetypes;
701 n_name_mimetypes = 0;
702 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
704 if (result_uncertain)
705 *result_uncertain = FALSE;
707 G_LOCK (gio_xdgmime);
711 basename = g_path_get_basename (filename);
712 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
716 /* Got an extension match, and no conflicts. This is it. */
717 if (n_name_mimetypes == 1)
719 G_UNLOCK (gio_xdgmime);
720 return g_strdup (name_mimetypes[0]);
725 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
726 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
728 looks_like_text (data, data_size))
729 sniffed_mimetype = "text/plain";
732 if (n_name_mimetypes == 0)
734 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
736 *result_uncertain = TRUE;
738 mimetype = g_strdup (sniffed_mimetype);
743 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
745 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
746 mimetype = g_strdup (sniffed_mimetype);
749 /* There are conflicts between the name matches and we have a sniffed
750 type, use that as a tie breaker. */
752 for (i = 0; i < n_name_mimetypes; i++)
754 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
756 /* This nametype match is derived from (or the same as) the sniffed type).
757 This is probably it. */
758 mimetype = g_strdup (name_mimetypes[i]);
765 if (mimetype == NULL)
767 /* Conflicts, and sniffed type was no help or not there. guess on the first one */
768 mimetype = g_strdup (name_mimetypes[0]);
769 if (result_uncertain)
770 *result_uncertain = TRUE;
774 G_UNLOCK (gio_xdgmime);
780 foreach_mimetype (gpointer key,
784 GList **l = user_data;
786 *l = g_list_prepend (*l, (char *)key);
791 enumerate_mimetypes_subdir (const char *dir, const char *prefix, GHashTable *mimetypes)
800 while ((ent = readdir (d)) != NULL)
802 if (g_str_has_suffix (ent->d_name, ".xml"))
804 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
805 g_hash_table_insert (mimetypes, mimetype, NULL);
813 enumerate_mimetypes_dir (const char *dir, GHashTable *mimetypes)
820 mimedir = g_build_filename (dir, "mime", NULL);
822 d = opendir (mimedir);
825 while ((ent = readdir (d)) != NULL)
827 if (strcmp (ent->d_name, "packages") != 0)
829 name = g_build_filename (mimedir, ent->d_name, NULL);
830 if (g_file_test (name, G_FILE_TEST_IS_DIR))
831 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
842 * g_content_types_get_registered:
844 * Gets a list of strings containing the registered content types on
847 * Returns: #GList of the registered content types.
850 g_content_types_get_registered (void)
852 const char * const* dirs;
853 GHashTable *mimetypes;
857 mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
859 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
860 dirs = g_get_system_data_dirs ();
862 for (i = 0; dirs[i] != NULL; i++)
863 enumerate_mimetypes_dir (dirs[i], mimetypes);
866 g_hash_table_foreach_steal (mimetypes, foreach_mimetype, &l);
867 g_hash_table_destroy (mimetypes);
872 #endif /* Unix version */
874 #define __G_CONTENT_TYPE_C__
875 #include "gioaliasdef.c"