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 known by GIO.
404 * Returns: %TRUE if the type is unknown.
407 g_content_type_is_unknown (const char *type)
409 g_return_val_if_fail (type != NULL, FALSE);
411 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
417 MIME_TAG_TYPE_COMMENT
422 int current_lang_level;
423 int comment_lang_level;
429 language_level (const char *lang)
431 const char * const *lang_list;
434 /* The returned list is sorted from most desirable to least
435 desirable and always contains the default locale "C". */
436 lang_list = g_get_language_names ();
438 for (i = 0; lang_list[i]; i++)
439 if (strcmp (lang_list[i], lang) == 0)
446 mime_info_start_element (GMarkupParseContext *context,
447 const gchar *element_name,
448 const gchar **attribute_names,
449 const gchar **attribute_values,
455 MimeParser *parser = user_data;
457 if (strcmp (element_name, "comment") == 0)
460 for (i = 0; attribute_names[i]; i++)
461 if (strcmp (attribute_names[i], "xml:lang") == 0)
463 lang = attribute_values[i];
467 parser->current_lang_level = language_level (lang);
468 parser->current_type = MIME_TAG_TYPE_COMMENT;
471 parser->current_type = MIME_TAG_TYPE_OTHER;
476 mime_info_end_element (GMarkupParseContext *context,
477 const gchar *element_name,
481 MimeParser *parser = user_data;
483 parser->current_type = MIME_TAG_TYPE_OTHER;
487 mime_info_text (GMarkupParseContext *context,
493 MimeParser *parser = user_data;
495 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
496 parser->current_lang_level > parser->comment_lang_level)
498 g_free (parser->comment);
499 parser->comment = g_strndup (text, text_len);
500 parser->comment_lang_level = parser->current_lang_level;
505 load_comment_for_mime_helper (const char *dir,
506 const char *basename)
508 GMarkupParseContext *context;
509 char *filename, *data;
512 MimeParser parse_data = {0};
513 GMarkupParser parser = {
514 mime_info_start_element,
515 mime_info_end_element,
519 filename = g_build_filename (dir, "mime", basename, NULL);
521 res = g_file_get_contents (filename, &data, &len, NULL);
526 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
527 res = g_markup_parse_context_parse (context, data, len, NULL);
529 g_markup_parse_context_free (context);
534 return parse_data.comment;
539 load_comment_for_mime (const char *mimetype)
541 const char * const* dirs;
546 basename = g_strdup_printf ("%s.xml", mimetype);
548 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
555 dirs = g_get_system_data_dirs ();
557 for (i = 0; dirs[i] != NULL; i++)
559 comment = load_comment_for_mime_helper (dirs[i], basename);
568 return g_strdup_printf (_("%s type"), mimetype);
572 * g_content_type_get_description:
573 * @type: a content type string.
575 * Gets the human readable description of the content type.
577 * Returns: a short description of the content type @type.
580 g_content_type_get_description (const char *type)
582 static GHashTable *type_comment_cache = NULL;
585 g_return_val_if_fail (type != NULL, NULL);
587 G_LOCK (gio_xdgmime);
588 if (type_comment_cache == NULL)
589 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
591 comment = g_hash_table_lookup (type_comment_cache, type);
592 comment = g_strdup (comment);
593 G_UNLOCK (gio_xdgmime);
598 comment = load_comment_for_mime (type);
600 G_LOCK (gio_xdgmime);
601 g_hash_table_insert (type_comment_cache,
604 G_UNLOCK (gio_xdgmime);
610 * g_content_type_get_mime_type:
611 * @type: a content type string.
613 * Gets the mime-type for the content type.
615 * Returns: the registered mime-type for the given @type.
618 g_content_type_get_mime_type (const char *type)
620 g_return_val_if_fail (type != NULL, NULL);
622 return g_strdup (type);
626 * g_content_type_get_icon:
627 * @type: a content type string.
629 * Gets the icon for a content type.
631 * Returns: #GIcon corresponding to the content type.
634 g_content_type_get_icon (const char *type)
636 g_return_val_if_fail (type != NULL, NULL);
638 /* TODO: Implement */
643 * g_content_type_can_be_executable:
644 * @type: a content type string.
646 * Checks if a content type can be executable. Note that for instance
647 * things like text files can be executables (i.e. scripts and batch files).
649 * Returns: %TRUE if the file type corresponds to a type that
650 * can be executable, %FALSE otherwise.
653 g_content_type_can_be_executable (const char *type)
655 g_return_val_if_fail (type != NULL, FALSE);
657 if (g_content_type_is_a (type, "application/x-executable") ||
658 g_content_type_is_a (type, "text/plain"))
665 looks_like_text (const guchar *data, gsize data_size)
668 for (i = 0; i < data_size; i++)
670 if g_ascii_iscntrl (data[i])
677 * g_content_type_guess:
678 * @filename: a string.
679 * @data: a stream of data.
680 * @data_size: the size of @data.
681 * @result_uncertain: a flag indicating the certainty of the
684 * Guesses the content type based on example data. If the function is
685 * uncertain, @result_uncertain will be set to %TRUE.
687 * Returns: a string indicating a guessed content type for the
691 g_content_type_guess (const char *filename,
694 gboolean *result_uncertain)
697 const char *name_mimetypes[10], *sniffed_mimetype;
700 int n_name_mimetypes;
704 n_name_mimetypes = 0;
705 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
707 if (result_uncertain)
708 *result_uncertain = FALSE;
710 G_LOCK (gio_xdgmime);
714 basename = g_path_get_basename (filename);
715 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
719 /* Got an extension match, and no conflicts. This is it. */
720 if (n_name_mimetypes == 1)
722 G_UNLOCK (gio_xdgmime);
723 return g_strdup (name_mimetypes[0]);
728 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
729 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
731 looks_like_text (data, data_size))
732 sniffed_mimetype = "text/plain";
735 if (n_name_mimetypes == 0)
737 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
739 *result_uncertain = TRUE;
741 mimetype = g_strdup (sniffed_mimetype);
746 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
748 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
749 mimetype = g_strdup (sniffed_mimetype);
752 /* There are conflicts between the name matches and we have a sniffed
753 type, use that as a tie breaker. */
755 for (i = 0; i < n_name_mimetypes; i++)
757 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
759 /* This nametype match is derived from (or the same as) the sniffed type).
760 This is probably it. */
761 mimetype = g_strdup (name_mimetypes[i]);
768 if (mimetype == NULL)
770 /* Conflicts, and sniffed type was no help or not there. guess on the first one */
771 mimetype = g_strdup (name_mimetypes[0]);
772 if (result_uncertain)
773 *result_uncertain = TRUE;
777 G_UNLOCK (gio_xdgmime);
783 foreach_mimetype (gpointer key,
787 GList **l = user_data;
789 *l = g_list_prepend (*l, (char *)key);
794 enumerate_mimetypes_subdir (const char *dir,
796 GHashTable *mimetypes)
805 while ((ent = readdir (d)) != NULL)
807 if (g_str_has_suffix (ent->d_name, ".xml"))
809 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
810 g_hash_table_insert (mimetypes, mimetype, NULL);
818 enumerate_mimetypes_dir (const char *dir,
819 GHashTable *mimetypes)
826 mimedir = g_build_filename (dir, "mime", NULL);
828 d = opendir (mimedir);
831 while ((ent = readdir (d)) != NULL)
833 if (strcmp (ent->d_name, "packages") != 0)
835 name = g_build_filename (mimedir, ent->d_name, NULL);
836 if (g_file_test (name, G_FILE_TEST_IS_DIR))
837 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
848 * g_content_types_get_registered:
850 * Gets a list of strings containing the registered content types on
853 * Returns: #GList of the registered content types.
856 g_content_types_get_registered (void)
858 const char * const* dirs;
859 GHashTable *mimetypes;
863 mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
865 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
866 dirs = g_get_system_data_dirs ();
868 for (i = 0; dirs[i] != NULL; i++)
869 enumerate_mimetypes_dir (dirs[i], mimetypes);
872 g_hash_table_foreach_steal (mimetypes, foreach_mimetype, &l);
873 g_hash_table_destroy (mimetypes);
878 #endif /* Unix version */
880 #define __G_CONTENT_TYPE_C__
881 #include "gioaliasdef.c"