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,
212 for (i = 0; i < data_size; i++)
215 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c))
222 g_content_type_guess (const char *filename,
225 gboolean *result_uncertain)
235 basename = g_path_get_basename (filename);
236 dot = strrchr (basename, '.');
238 type = g_strdup (dot);
245 if (data && looks_like_text (data, data_size))
246 return g_strdup (".txt");
248 return g_strdup ("*");
252 g_content_types_get_registered (void)
255 wchar_t keyname[256];
263 while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
270 NULL) == ERROR_SUCCESS)
272 key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
275 if (*key_utf8 == '.')
276 types = g_list_prepend (types, key_utf8);
284 return g_list_reverse (types);
287 #else /* !G_OS_WIN32 - Unix specific version */
289 #define XDG_PREFIX _gio_xdg
290 #include "xdgmime/xdgmime.h"
292 /* We lock this mutex whenever we modify global state in this module. */
293 G_LOCK_DEFINE_STATIC (gio_xdgmime);
296 _g_unix_content_type_get_sniff_len (void)
300 G_LOCK (gio_xdgmime);
301 size = xdg_mime_get_max_buffer_extents ();
302 G_UNLOCK (gio_xdgmime);
308 _g_unix_content_type_unalias (const char *type)
312 G_LOCK (gio_xdgmime);
313 res = g_strdup (xdg_mime_unalias_mime_type (type));
314 G_UNLOCK (gio_xdgmime);
320 _g_unix_content_type_get_parents (const char *type)
323 const char **parents;
327 array = g_ptr_array_new ();
329 G_LOCK (gio_xdgmime);
331 umime = xdg_mime_unalias_mime_type (type);
332 g_ptr_array_add (array, g_strdup (umime));
334 parents = xdg_mime_get_mime_parents (umime);
335 for (i = 0; parents && parents[i] != NULL; i++)
336 g_ptr_array_add (array, g_strdup (parents[i]));
338 G_UNLOCK (gio_xdgmime);
340 g_ptr_array_add (array, NULL);
342 return (char **)g_ptr_array_free (array, FALSE);
346 * g_content_type_equals:
347 * @type1: a content type string.
348 * @type2: a content type string.
350 * Compares two content types for equality.
352 * Returns: %TRUE if the two strings are identical or equivalent,
356 g_content_type_equals (const char *type1,
361 g_return_val_if_fail (type1 != NULL, FALSE);
362 g_return_val_if_fail (type2 != NULL, FALSE);
364 G_LOCK (gio_xdgmime);
365 res = xdg_mime_mime_type_equal (type1, type2);
366 G_UNLOCK (gio_xdgmime);
372 * g_content_type_is_a:
373 * @type: a content type string.
374 * @supertype: a string.
376 * Determines if @type is a subset of @supertype.
378 * Returns: %TRUE if @type is a kind of @supertype,
382 g_content_type_is_a (const char *type,
383 const char *supertype)
387 g_return_val_if_fail (type != NULL, FALSE);
388 g_return_val_if_fail (supertype != NULL, FALSE);
390 G_LOCK (gio_xdgmime);
391 res = xdg_mime_mime_type_subclass (type, supertype);
392 G_UNLOCK (gio_xdgmime);
398 * g_content_type_is_unknown:
399 * @type: a content type string.
401 * Checks if the content type is known by GIO.
403 * Returns: %TRUE if the type is unknown.
406 g_content_type_is_unknown (const char *type)
408 g_return_val_if_fail (type != NULL, FALSE);
410 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
416 MIME_TAG_TYPE_COMMENT
421 int current_lang_level;
422 int comment_lang_level;
428 language_level (const char *lang)
430 const char * const *lang_list;
433 /* The returned list is sorted from most desirable to least
434 desirable and always contains the default locale "C". */
435 lang_list = g_get_language_names ();
437 for (i = 0; lang_list[i]; i++)
438 if (strcmp (lang_list[i], lang) == 0)
445 mime_info_start_element (GMarkupParseContext *context,
446 const gchar *element_name,
447 const gchar **attribute_names,
448 const gchar **attribute_values,
454 MimeParser *parser = user_data;
456 if (strcmp (element_name, "comment") == 0)
459 for (i = 0; attribute_names[i]; i++)
460 if (strcmp (attribute_names[i], "xml:lang") == 0)
462 lang = attribute_values[i];
466 parser->current_lang_level = language_level (lang);
467 parser->current_type = MIME_TAG_TYPE_COMMENT;
470 parser->current_type = MIME_TAG_TYPE_OTHER;
475 mime_info_end_element (GMarkupParseContext *context,
476 const gchar *element_name,
480 MimeParser *parser = user_data;
482 parser->current_type = MIME_TAG_TYPE_OTHER;
486 mime_info_text (GMarkupParseContext *context,
492 MimeParser *parser = user_data;
494 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
495 parser->current_lang_level > parser->comment_lang_level)
497 g_free (parser->comment);
498 parser->comment = g_strndup (text, text_len);
499 parser->comment_lang_level = parser->current_lang_level;
504 load_comment_for_mime_helper (const char *dir,
505 const char *basename)
507 GMarkupParseContext *context;
508 char *filename, *data;
511 MimeParser parse_data = {0};
512 GMarkupParser parser = {
513 mime_info_start_element,
514 mime_info_end_element,
518 filename = g_build_filename (dir, "mime", basename, NULL);
520 res = g_file_get_contents (filename, &data, &len, NULL);
525 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
526 res = g_markup_parse_context_parse (context, data, len, NULL);
528 g_markup_parse_context_free (context);
533 return parse_data.comment;
538 load_comment_for_mime (const char *mimetype)
540 const char * const* dirs;
545 basename = g_strdup_printf ("%s.xml", mimetype);
547 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
554 dirs = g_get_system_data_dirs ();
556 for (i = 0; dirs[i] != NULL; i++)
558 comment = load_comment_for_mime_helper (dirs[i], basename);
567 return g_strdup_printf (_("%s type"), mimetype);
571 * g_content_type_get_description:
572 * @type: a content type string.
574 * Gets the human readable description of the content type.
576 * Returns: a short description of the content type @type.
579 g_content_type_get_description (const char *type)
581 static GHashTable *type_comment_cache = NULL;
584 g_return_val_if_fail (type != NULL, NULL);
586 G_LOCK (gio_xdgmime);
587 if (type_comment_cache == NULL)
588 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
590 comment = g_hash_table_lookup (type_comment_cache, type);
591 comment = g_strdup (comment);
592 G_UNLOCK (gio_xdgmime);
597 comment = load_comment_for_mime (type);
599 G_LOCK (gio_xdgmime);
600 g_hash_table_insert (type_comment_cache,
603 G_UNLOCK (gio_xdgmime);
609 * g_content_type_get_mime_type:
610 * @type: a content type string.
612 * Gets the mime-type for the content type.
614 * Returns: the registered mime-type for the given @type.
617 g_content_type_get_mime_type (const char *type)
619 g_return_val_if_fail (type != NULL, NULL);
621 return g_strdup (type);
625 * g_content_type_get_icon:
626 * @type: a content type string.
628 * Gets the icon for a content type.
630 * Returns: #GIcon corresponding to the content type.
633 g_content_type_get_icon (const char *type)
635 g_return_val_if_fail (type != NULL, NULL);
637 /* TODO: Implement */
642 * g_content_type_can_be_executable:
643 * @type: a content type string.
645 * Checks if a content type can be executable. Note that for instance
646 * things like text files can be executables (i.e. scripts and batch files).
648 * Returns: %TRUE if the file type corresponds to a type that
649 * can be executable, %FALSE otherwise.
652 g_content_type_can_be_executable (const char *type)
654 g_return_val_if_fail (type != NULL, FALSE);
656 if (g_content_type_is_a (type, "application/x-executable") ||
657 g_content_type_is_a (type, "text/plain"))
664 looks_like_text (const guchar *data, gsize data_size)
667 for (i = 0; i < data_size; i++)
669 if g_ascii_iscntrl (data[i])
676 * g_content_type_guess:
677 * @filename: a string.
678 * @data: a stream of data.
679 * @data_size: the size of @data.
680 * @result_uncertain: a flag indicating the certainty of the
683 * Guesses the content type based on example data. If the function is
684 * uncertain, @result_uncertain will be set to %TRUE.
686 * Returns: a string indicating a guessed content type for the
690 g_content_type_guess (const char *filename,
693 gboolean *result_uncertain)
696 const char *name_mimetypes[10], *sniffed_mimetype;
699 int n_name_mimetypes;
703 n_name_mimetypes = 0;
704 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
706 if (result_uncertain)
707 *result_uncertain = FALSE;
709 G_LOCK (gio_xdgmime);
713 basename = g_path_get_basename (filename);
714 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
718 /* Got an extension match, and no conflicts. This is it. */
719 if (n_name_mimetypes == 1)
721 G_UNLOCK (gio_xdgmime);
722 return g_strdup (name_mimetypes[0]);
727 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
728 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
730 looks_like_text (data, data_size))
731 sniffed_mimetype = "text/plain";
734 if (n_name_mimetypes == 0)
736 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
738 *result_uncertain = TRUE;
740 mimetype = g_strdup (sniffed_mimetype);
745 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
747 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
748 mimetype = g_strdup (sniffed_mimetype);
751 /* There are conflicts between the name matches and we have a sniffed
752 type, use that as a tie breaker. */
754 for (i = 0; i < n_name_mimetypes; i++)
756 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
758 /* This nametype match is derived from (or the same as) the sniffed type).
759 This is probably it. */
760 mimetype = g_strdup (name_mimetypes[i]);
767 if (mimetype == NULL)
769 /* Conflicts, and sniffed type was no help or not there. guess on the first one */
770 mimetype = g_strdup (name_mimetypes[0]);
771 if (result_uncertain)
772 *result_uncertain = TRUE;
776 G_UNLOCK (gio_xdgmime);
782 foreach_mimetype (gpointer key,
786 GList **l = user_data;
788 *l = g_list_prepend (*l, (char *)key);
793 enumerate_mimetypes_subdir (const char *dir,
795 GHashTable *mimetypes)
804 while ((ent = readdir (d)) != NULL)
806 if (g_str_has_suffix (ent->d_name, ".xml"))
808 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
809 g_hash_table_insert (mimetypes, mimetype, NULL);
817 enumerate_mimetypes_dir (const char *dir,
818 GHashTable *mimetypes)
825 mimedir = g_build_filename (dir, "mime", NULL);
827 d = opendir (mimedir);
830 while ((ent = readdir (d)) != NULL)
832 if (strcmp (ent->d_name, "packages") != 0)
834 name = g_build_filename (mimedir, ent->d_name, NULL);
835 if (g_file_test (name, G_FILE_TEST_IS_DIR))
836 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
847 * g_content_types_get_registered:
849 * Gets a list of strings containing the registered content types on
852 * Returns: #GList of the registered content types.
855 g_content_types_get_registered (void)
857 const char * const* dirs;
858 GHashTable *mimetypes;
862 mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
864 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
865 dirs = g_get_system_data_dirs ();
867 for (i = 0; dirs[i] != NULL; i++)
868 enumerate_mimetypes_dir (dirs[i], mimetypes);
871 g_hash_table_foreach_steal (mimetypes, foreach_mimetype, &l);
872 g_hash_table_destroy (mimetypes);
877 #endif /* Unix version */
879 #define __G_CONTENT_TYPE_C__
880 #include "gioaliasdef.c"