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
&&
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 value_utf8
= g_utf16_to_utf8 (wc_temp
, -1, NULL
, NULL
, NULL
);
80 RegCloseKey (reg_key
);
86 g_content_type_equals (const char *type1
,
89 char *progid1
, *progid2
;
92 g_return_val_if_fail (type1
!= NULL
, FALSE
);
93 g_return_val_if_fail (type2
!= NULL
, FALSE
);
95 if (g_ascii_strcasecmp (type1
, type2
) == 0)
99 progid1
= get_registry_classes_key (type1
, NULL
);
100 progid2
= get_registry_classes_key (type2
, NULL
);
101 if (progid1
!= NULL
&& progid2
!= NULL
&&
102 strcmp (progid1
, progid2
) == 0)
111 g_content_type_is_a (const char *type
,
112 const char *supertype
)
117 g_return_val_if_fail (type
!= NULL
, FALSE
);
118 g_return_val_if_fail (supertype
!= NULL
, FALSE
);
120 if (g_content_type_equals (type
, supertype
))
124 value_utf8
= get_registry_classes_key (type
, L
"PerceivedType");
125 if (value_utf8
&& strcmp (value_utf8
, supertype
) == 0)
133 g_content_type_is_unknown (const char *type
)
135 g_return_val_if_fail (type
!= NULL
, FALSE
);
137 return strcmp ("*", type
) == 0;
141 g_content_type_get_description (const char *type
)
146 g_return_val_if_fail (type
!= NULL
, NULL
);
148 progid
= get_registry_classes_key (type
, NULL
);
151 description
= get_registry_classes_key (progid
, NULL
);
158 if (g_content_type_is_unknown (type
))
159 return g_strdup (_("Unknown type"));
160 return g_strdup_printf (_("%s filetype"), type
);
164 g_content_type_get_mime_type (const char *type
)
168 g_return_val_if_fail (type
!= NULL
, NULL
);
170 mime
= get_registry_classes_key (type
, L
"Content Type");
173 else if (g_content_type_is_unknown (type
))
174 return g_strdup ("application/octet-stream");
175 else if (*type
== '.')
176 return g_strdup_printf ("application/x-ext-%s", type
+1);
177 /* TODO: Map "image" to "image/ *", etc? */
179 return g_strdup ("application/octet-stream");
183 g_content_type_get_icon (const char *type
)
185 g_return_val_if_fail (type
!= NULL
, NULL
);
187 /* TODO: How do we represent icons???
188 In the registry they are the default value of
189 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
191 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
192 REG_SZ: shimgvw.dll,3
198 g_content_type_can_be_executable (const char *type
)
200 g_return_val_if_fail (type
!= NULL
, FALSE
);
202 if (strcmp (type
, ".exe") == 0 ||
203 strcmp (type
, ".com") == 0 ||
204 strcmp (type
, ".bat") == 0)
210 looks_like_text (const guchar
*data
,
215 for (i
= 0; i
< data_size
; i
++)
218 if (g_ascii_iscntrl (c
) && !g_ascii_isspace (c
))
225 g_content_type_from_mime_type (const char *mime_type
)
227 char *key
, *content_type
;
229 g_return_val_if_fail (mime_type
!= NULL
, NULL
);
231 key
= g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type
, NULL
);
232 content_type
= get_registry_classes_key (key
, L
"Extension");
239 g_content_type_guess (const char *filename
,
242 gboolean
*result_uncertain
)
252 basename
= g_path_get_basename (filename
);
253 dot
= strrchr (basename
, '.');
255 type
= g_strdup (dot
);
262 if (data
&& looks_like_text (data
, data_size
))
263 return g_strdup (".txt");
265 return g_strdup ("*");
269 g_content_types_get_registered (void)
272 wchar_t keyname
[256];
280 while (RegEnumKeyExW(HKEY_CLASSES_ROOT
,
287 NULL
) == ERROR_SUCCESS
)
289 key_utf8
= g_utf16_to_utf8 (keyname
, -1, NULL
, NULL
, NULL
);
292 if (*key_utf8
== '.')
293 types
= g_list_prepend (types
, key_utf8
);
301 return g_list_reverse (types
);
304 #else /* !G_OS_WIN32 - Unix specific version */
308 #define XDG_PREFIX _gio_xdg
309 #include "xdgmime/xdgmime.h"
311 /* We lock this mutex whenever we modify global state in this module. */
312 G_LOCK_DEFINE_STATIC (gio_xdgmime
);
315 _g_unix_content_type_get_sniff_len (void)
319 G_LOCK (gio_xdgmime
);
320 size
= xdg_mime_get_max_buffer_extents ();
321 G_UNLOCK (gio_xdgmime
);
327 _g_unix_content_type_unalias (const char *type
)
331 G_LOCK (gio_xdgmime
);
332 res
= g_strdup (xdg_mime_unalias_mime_type (type
));
333 G_UNLOCK (gio_xdgmime
);
339 _g_unix_content_type_get_parents (const char *type
)
346 array
= g_ptr_array_new ();
348 G_LOCK (gio_xdgmime
);
350 umime
= xdg_mime_unalias_mime_type (type
);
352 g_ptr_array_add (array
, g_strdup (umime
));
354 parents
= xdg_mime_list_mime_parents (umime
);
355 for (i
= 0; parents
&& parents
[i
] != NULL
; i
++)
356 g_ptr_array_add (array
, g_strdup (parents
[i
]));
360 G_UNLOCK (gio_xdgmime
);
362 g_ptr_array_add (array
, NULL
);
364 return (char **)g_ptr_array_free (array
, FALSE
);
368 * g_content_type_equals:
369 * @type1: a content type string.
370 * @type2: a content type string.
372 * Compares two content types for equality.
374 * Returns: %TRUE if the two strings are identical or equivalent,
378 g_content_type_equals (const char *type1
,
383 g_return_val_if_fail (type1
!= NULL
, FALSE
);
384 g_return_val_if_fail (type2
!= NULL
, FALSE
);
386 G_LOCK (gio_xdgmime
);
387 res
= xdg_mime_mime_type_equal (type1
, type2
);
388 G_UNLOCK (gio_xdgmime
);
394 * g_content_type_is_a:
395 * @type: a content type string.
396 * @supertype: a string.
398 * Determines if @type is a subset of @supertype.
400 * Returns: %TRUE if @type is a kind of @supertype,
404 g_content_type_is_a (const char *type
,
405 const char *supertype
)
409 g_return_val_if_fail (type
!= NULL
, FALSE
);
410 g_return_val_if_fail (supertype
!= NULL
, FALSE
);
412 G_LOCK (gio_xdgmime
);
413 res
= xdg_mime_mime_type_subclass (type
, supertype
);
414 G_UNLOCK (gio_xdgmime
);
420 * g_content_type_is_unknown:
421 * @type: a content type string.
423 * Checks if the content type is the generic "unknown" type.
424 * On unix this is the "application/octet-stream" mimetype,
425 * while on win32 it is "*".
427 * Returns: %TRUE if the type is the unknown type.
430 g_content_type_is_unknown (const char *type
)
432 g_return_val_if_fail (type
!= NULL
, FALSE
);
434 return strcmp (XDG_MIME_TYPE_UNKNOWN
, type
) == 0;
440 MIME_TAG_TYPE_COMMENT
445 int current_lang_level
;
446 int comment_lang_level
;
452 language_level (const char *lang
)
454 const char * const *lang_list
;
457 /* The returned list is sorted from most desirable to least
458 desirable and always contains the default locale "C". */
459 lang_list
= g_get_language_names ();
461 for (i
= 0; lang_list
[i
]; i
++)
462 if (strcmp (lang_list
[i
], lang
) == 0)
469 mime_info_start_element (GMarkupParseContext
*context
,
470 const gchar
*element_name
,
471 const gchar
**attribute_names
,
472 const gchar
**attribute_values
,
478 MimeParser
*parser
= user_data
;
480 if (strcmp (element_name
, "comment") == 0)
483 for (i
= 0; attribute_names
[i
]; i
++)
484 if (strcmp (attribute_names
[i
], "xml:lang") == 0)
486 lang
= attribute_values
[i
];
490 parser
->current_lang_level
= language_level (lang
);
491 parser
->current_type
= MIME_TAG_TYPE_COMMENT
;
494 parser
->current_type
= MIME_TAG_TYPE_OTHER
;
499 mime_info_end_element (GMarkupParseContext
*context
,
500 const gchar
*element_name
,
504 MimeParser
*parser
= user_data
;
506 parser
->current_type
= MIME_TAG_TYPE_OTHER
;
510 mime_info_text (GMarkupParseContext
*context
,
516 MimeParser
*parser
= user_data
;
518 if (parser
->current_type
== MIME_TAG_TYPE_COMMENT
&&
519 parser
->current_lang_level
> parser
->comment_lang_level
)
521 g_free (parser
->comment
);
522 parser
->comment
= g_strndup (text
, text_len
);
523 parser
->comment_lang_level
= parser
->current_lang_level
;
528 load_comment_for_mime_helper (const char *dir
,
529 const char *basename
)
531 GMarkupParseContext
*context
;
532 char *filename
, *data
;
535 MimeParser parse_data
= {0};
536 GMarkupParser parser
= {
537 mime_info_start_element
,
538 mime_info_end_element
,
542 filename
= g_build_filename (dir
, "mime", basename
, NULL
);
544 res
= g_file_get_contents (filename
, &data
, &len
, NULL
);
549 context
= g_markup_parse_context_new (&parser
, 0, &parse_data
, NULL
);
550 res
= g_markup_parse_context_parse (context
, data
, len
, NULL
);
552 g_markup_parse_context_free (context
);
557 return parse_data
.comment
;
562 load_comment_for_mime (const char *mimetype
)
564 const char * const* dirs
;
569 basename
= g_strdup_printf ("%s.xml", mimetype
);
571 comment
= load_comment_for_mime_helper (g_get_user_data_dir (), basename
);
578 dirs
= g_get_system_data_dirs ();
580 for (i
= 0; dirs
[i
] != NULL
; i
++)
582 comment
= load_comment_for_mime_helper (dirs
[i
], basename
);
591 return g_strdup_printf (_("%s type"), mimetype
);
595 * g_content_type_get_description:
596 * @type: a content type string.
598 * Gets the human readable description of the content type.
600 * Returns: a short description of the content type @type.
603 g_content_type_get_description (const char *type
)
605 static GHashTable
*type_comment_cache
= NULL
;
608 g_return_val_if_fail (type
!= NULL
, NULL
);
610 G_LOCK (gio_xdgmime
);
611 type
= xdg_mime_unalias_mime_type (type
);
613 if (type_comment_cache
== NULL
)
614 type_comment_cache
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
616 comment
= g_hash_table_lookup (type_comment_cache
, type
);
617 comment
= g_strdup (comment
);
618 G_UNLOCK (gio_xdgmime
);
623 comment
= load_comment_for_mime (type
);
625 G_LOCK (gio_xdgmime
);
626 g_hash_table_insert (type_comment_cache
,
629 G_UNLOCK (gio_xdgmime
);
635 * g_content_type_get_mime_type:
636 * @type: a content type string.
638 * Gets the mime-type for the content type. If one is registered
640 * Returns: the registered mime-type for the given @type, or NULL if unknown.
643 g_content_type_get_mime_type (const char *type
)
645 g_return_val_if_fail (type
!= NULL
, NULL
);
647 return g_strdup (type
);
651 * g_content_type_get_icon:
652 * @type: a content type string.
654 * Gets the icon for a content type.
656 * Returns: #GIcon corresponding to the content type.
659 g_content_type_get_icon (const char *type
)
661 char *mimetype_icon
, *generic_mimetype_icon
, *q
;
662 char *xdg_mimetype_icon
, *legacy_mimetype_icon
;
668 g_return_val_if_fail (type
!= NULL
, NULL
);
670 G_LOCK (gio_xdgmime
);
671 xdg_mimetype_icon
= g_strdup (xdg_mime_get_icon (type
));
672 G_UNLOCK (gio_xdgmime
);
674 mimetype_icon
= g_strdup (type
);
676 while ((q
= strchr (mimetype_icon
, '/')) != NULL
)
679 p
= strchr (type
, '/');
681 p
= type
+ strlen (type
);
683 /* Not all icons have migrated to the new icon theme spec, look for old names too */
684 legacy_mimetype_icon
= g_strconcat ("gnome-mime-", mimetype_icon
, NULL
);
686 generic_mimetype_icon
= g_malloc (p
- type
+ strlen ("-x-generic") + 1);
687 memcpy (generic_mimetype_icon
, type
, p
- type
);
688 memcpy (generic_mimetype_icon
+ (p
- type
), "-x-generic", strlen ("-x-generic"));
689 generic_mimetype_icon
[(p
- type
) + strlen ("-x-generic")] = 0;
691 if (xdg_mimetype_icon
)
692 icon_names
[n
++] = xdg_mimetype_icon
;
694 icon_names
[n
++] = mimetype_icon
;
695 icon_names
[n
++] = legacy_mimetype_icon
;
696 icon_names
[n
++] = generic_mimetype_icon
;
698 themed_icon
= g_themed_icon_new_from_names (icon_names
, n
);
700 g_free (xdg_mimetype_icon
);
701 g_free (mimetype_icon
);
702 g_free (legacy_mimetype_icon
);
703 g_free (generic_mimetype_icon
);
709 * g_content_type_can_be_executable:
710 * @type: a content type string.
712 * Checks if a content type can be executable. Note that for instance
713 * things like text files can be executables (i.e. scripts and batch files).
715 * Returns: %TRUE if the file type corresponds to a type that
716 * can be executable, %FALSE otherwise.
719 g_content_type_can_be_executable (const char *type
)
721 g_return_val_if_fail (type
!= NULL
, FALSE
);
723 if (g_content_type_is_a (type
, "application/x-executable") ||
724 g_content_type_is_a (type
, "text/plain"))
731 looks_like_text (const guchar
*data
, gsize data_size
)
736 for (i
= 0; i
< data_size
; i
++)
740 if (g_ascii_iscntrl (c
) &&
741 !g_ascii_isspace (c
))
748 * g_content_type_from_mime_type:
749 * @mime_type: a mime type string.
751 * Tries to find a content type based on the mime type name.
753 * Returns: Newly allocated string with content type or NULL when does not know.
758 g_content_type_from_mime_type (const char *mime_type
)
760 g_return_val_if_fail (mime_type
!= NULL
, NULL
);
762 /* mime type and content type are same on unixes */
763 return g_strdup (mime_type
);
767 * g_content_type_guess:
768 * @filename: a string.
769 * @data: a stream of data.
770 * @data_size: the size of @data.
771 * @result_uncertain: a flag indicating the certainty of the
774 * Guesses the content type based on example data. If the function is
775 * uncertain, @result_uncertain will be set to %TRUE.
777 * Returns: a string indicating a guessed content type for the
781 g_content_type_guess (const char *filename
,
784 gboolean
*result_uncertain
)
787 const char *name_mimetypes
[10], *sniffed_mimetype
;
790 int n_name_mimetypes
;
794 n_name_mimetypes
= 0;
795 sniffed_mimetype
= XDG_MIME_TYPE_UNKNOWN
;
797 if (result_uncertain
)
798 *result_uncertain
= FALSE
;
800 G_LOCK (gio_xdgmime
);
804 basename
= g_path_get_basename (filename
);
805 n_name_mimetypes
= xdg_mime_get_mime_types_from_file_name (basename
, name_mimetypes
, 10);
809 /* Got an extension match, and no conflicts. This is it. */
810 if (n_name_mimetypes
== 1)
812 G_UNLOCK (gio_xdgmime
);
813 return g_strdup (name_mimetypes
[0]);
818 sniffed_mimetype
= xdg_mime_get_mime_type_for_data (data
, data_size
, &sniffed_prio
);
819 if (sniffed_mimetype
== XDG_MIME_TYPE_UNKNOWN
&&
821 looks_like_text (data
, data_size
))
822 sniffed_mimetype
= "text/plain";
825 if (n_name_mimetypes
== 0)
827 if (sniffed_mimetype
== XDG_MIME_TYPE_UNKNOWN
&&
829 *result_uncertain
= TRUE
;
831 mimetype
= g_strdup (sniffed_mimetype
);
836 if (sniffed_mimetype
!= XDG_MIME_TYPE_UNKNOWN
)
838 if (sniffed_prio
>= 80) /* High priority sniffing match, use that */
839 mimetype
= g_strdup (sniffed_mimetype
);
842 /* There are conflicts between the name matches and we have a sniffed
843 type, use that as a tie breaker. */
845 for (i
= 0; i
< n_name_mimetypes
; i
++)
847 if ( xdg_mime_mime_type_subclass (name_mimetypes
[i
], sniffed_mimetype
))
849 /* This nametype match is derived from (or the same as) the sniffed type).
850 This is probably it. */
851 mimetype
= g_strdup (name_mimetypes
[i
]);
858 if (mimetype
== NULL
)
860 /* Conflicts, and sniffed type was no help or not there. Guess on the first one */
861 mimetype
= g_strdup (name_mimetypes
[0]);
862 if (result_uncertain
)
863 *result_uncertain
= TRUE
;
867 G_UNLOCK (gio_xdgmime
);
873 enumerate_mimetypes_subdir (const char *dir
,
875 GHashTable
*mimetypes
)
884 while ((ent
= readdir (d
)) != NULL
)
886 if (g_str_has_suffix (ent
->d_name
, ".xml"))
888 mimetype
= g_strdup_printf ("%s/%.*s", prefix
, (int) strlen (ent
->d_name
) - 4, ent
->d_name
);
889 g_hash_table_insert (mimetypes
, mimetype
, NULL
);
897 enumerate_mimetypes_dir (const char *dir
,
898 GHashTable
*mimetypes
)
905 mimedir
= g_build_filename (dir
, "mime", NULL
);
907 d
= opendir (mimedir
);
910 while ((ent
= readdir (d
)) != NULL
)
912 if (strcmp (ent
->d_name
, "packages") != 0)
914 name
= g_build_filename (mimedir
, ent
->d_name
, NULL
);
915 if (g_file_test (name
, G_FILE_TEST_IS_DIR
))
916 enumerate_mimetypes_subdir (name
, ent
->d_name
, mimetypes
);
927 * g_content_types_get_registered:
929 * Gets a list of strings containing all the registered content types
930 * known to the system. The list and its data should be freed using
931 * @g_list_foreach(list, g_free, NULL) and @g_list_free(list)
932 * Returns: #GList of the registered content types.
935 g_content_types_get_registered (void)
937 const char * const* dirs
;
938 GHashTable
*mimetypes
;
944 mimetypes
= g_hash_table_new (g_str_hash
, g_str_equal
);
946 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes
);
947 dirs
= g_get_system_data_dirs ();
949 for (i
= 0; dirs
[i
] != NULL
; i
++)
950 enumerate_mimetypes_dir (dirs
[i
], mimetypes
);
953 g_hash_table_iter_init (&iter
, mimetypes
);
954 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
955 l
= g_list_prepend (l
, key
);
957 g_hash_table_destroy (mimetypes
);
962 #endif /* Unix version */
964 #define __G_CONTENT_TYPE_C__
965 #include "gioaliasdef.c"