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>
30 #include "gcontenttypeprivate.h"
31 #include "gthemedicon.h"
34 #include "gfileenumerator.h"
35 #include "gfileinfo.h"
40 * SECTION:gcontenttype
41 * @short_description: Platform-specific content typing
44 * A content type is a platform specific string that defines the type
45 * of a file. On unix it is a mime type, on win32 it is an extension string
46 * like ".doc", ".txt" or a percieved string like "audio". Such strings
47 * can be looked up in the registry at HKEY_CLASSES_ROOT.
55 get_registry_classes_key (const char *subdir
,
56 const wchar_t *key_name
)
67 wc_key
= g_utf8_to_utf16 (subdir
, -1, NULL
, NULL
, NULL
);
68 if (RegOpenKeyExW (HKEY_CLASSES_ROOT
, wc_key
, 0,
69 KEY_QUERY_VALUE
, ®_key
) == ERROR_SUCCESS
&&
70 RegQueryValueExW (reg_key
, key_name
, 0,
71 &key_type
, NULL
, &nbytes
) == ERROR_SUCCESS
&&
72 (key_type
== REG_SZ
|| key_type
== REG_EXPAND_SZ
))
74 wchar_t *wc_temp
= g_new (wchar_t, (nbytes
+1)/2 + 1);
75 RegQueryValueExW (reg_key
, key_name
, 0,
76 &key_type
, (LPBYTE
) wc_temp
, &nbytes
);
77 wc_temp
[nbytes
/2] = '\0';
78 if (key_type
== REG_EXPAND_SZ
)
81 int len
= ExpandEnvironmentStringsW (wc_temp
, dummy
, 1);
84 wchar_t *wc_temp_expanded
= g_new (wchar_t, len
);
85 if (ExpandEnvironmentStringsW (wc_temp
, wc_temp_expanded
, len
) == len
)
86 value_utf8
= g_utf16_to_utf8 (wc_temp_expanded
, -1, NULL
, NULL
, NULL
);
87 g_free (wc_temp_expanded
);
92 value_utf8
= g_utf16_to_utf8 (wc_temp
, -1, NULL
, NULL
, NULL
);
100 RegCloseKey (reg_key
);
106 g_content_type_equals (const gchar
*type1
,
109 char *progid1
, *progid2
;
112 g_return_val_if_fail (type1
!= NULL
, FALSE
);
113 g_return_val_if_fail (type2
!= NULL
, FALSE
);
115 if (g_ascii_strcasecmp (type1
, type2
) == 0)
119 progid1
= get_registry_classes_key (type1
, NULL
);
120 progid2
= get_registry_classes_key (type2
, NULL
);
121 if (progid1
!= NULL
&& progid2
!= NULL
&&
122 strcmp (progid1
, progid2
) == 0)
131 g_content_type_is_a (const gchar
*type
,
132 const gchar
*supertype
)
137 g_return_val_if_fail (type
!= NULL
, FALSE
);
138 g_return_val_if_fail (supertype
!= NULL
, FALSE
);
140 if (g_content_type_equals (type
, supertype
))
144 value_utf8
= get_registry_classes_key (type
, L
"PerceivedType");
145 if (value_utf8
&& strcmp (value_utf8
, supertype
) == 0)
153 g_content_type_is_unknown (const gchar
*type
)
155 g_return_val_if_fail (type
!= NULL
, FALSE
);
157 return strcmp ("*", type
) == 0;
161 g_content_type_get_description (const gchar
*type
)
166 g_return_val_if_fail (type
!= NULL
, NULL
);
168 progid
= get_registry_classes_key (type
, NULL
);
171 description
= get_registry_classes_key (progid
, NULL
);
178 if (g_content_type_is_unknown (type
))
179 return g_strdup (_("Unknown type"));
180 return g_strdup_printf (_("%s filetype"), type
);
184 g_content_type_get_mime_type (const gchar
*type
)
188 g_return_val_if_fail (type
!= NULL
, NULL
);
190 mime
= get_registry_classes_key (type
, L
"Content Type");
193 else if (g_content_type_is_unknown (type
))
194 return g_strdup ("application/octet-stream");
195 else if (*type
== '.')
196 return g_strdup_printf ("application/x-ext-%s", type
+1);
197 /* TODO: Map "image" to "image/ *", etc? */
199 return g_strdup ("application/octet-stream");
202 G_LOCK_DEFINE_STATIC (_type_icons
);
203 static GHashTable
*_type_icons
= NULL
;
206 g_content_type_get_icon (const gchar
*type
)
211 g_return_val_if_fail (type
!= NULL
, NULL
);
213 /* In the Registry icons are the default value of
214 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
216 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
217 REG_SZ: shimgvw.dll,3
219 G_LOCK (_type_icons
);
221 _type_icons
= g_hash_table_new (g_str_hash
, g_str_equal
);
222 name
= g_hash_table_lookup (_type_icons
, type
);
223 if (!name
&& type
[0] == '.')
225 /* double lookup by extension */
226 gchar
*key
= get_registry_classes_key (type
, NULL
);
228 key
= g_strconcat (type
+1, "file\\DefaultIcon", NULL
);
231 gchar
*key2
= g_strconcat (key
, "\\DefaultIcon", NULL
);
235 name
= get_registry_classes_key (key
, NULL
);
236 if (name
&& strcmp (name
, "%1") == 0)
242 g_hash_table_insert (_type_icons
, g_strdup (type
), g_strdup (name
));
246 /* icon-name similar to how it was with gtk-2-12 */
249 themed_icon
= g_themed_icon_new (name
);
253 /* if not found an icon fall back to gtk-builtins */
254 name
= strcmp (type
, "inode/directory") == 0 ? "gtk-directory" :
255 g_content_type_can_be_executable (type
) ? "gtk-execute" : "gtk-file";
256 g_hash_table_insert (_type_icons
, g_strdup (type
), g_strdup (name
));
257 themed_icon
= g_themed_icon_new_with_default_fallbacks (name
);
259 G_UNLOCK (_type_icons
);
261 return G_ICON (themed_icon
);
265 g_content_type_can_be_executable (const gchar
*type
)
267 g_return_val_if_fail (type
!= NULL
, FALSE
);
269 if (strcmp (type
, ".exe") == 0 ||
270 strcmp (type
, ".com") == 0 ||
271 strcmp (type
, ".bat") == 0)
274 /* TODO: Also look at PATHEXT, which lists the extensions for
275 * "scripts" in addition to those for true binary executables.
277 * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
278 * right now, for instance). And in a sense, all associated file
279 * types are "executable" on Windows... You can just type foo.jpg as
280 * a command name in cmd.exe, and it will run the application
281 * associated with .jpg. Hard to say what this API actually means
289 looks_like_text (const guchar
*data
,
294 for (i
= 0; i
< data_size
; i
++)
297 if (g_ascii_iscntrl (c
) && !g_ascii_isspace (c
) && c
!= '\b')
304 g_content_type_from_mime_type (const gchar
*mime_type
)
306 char *key
, *content_type
;
308 g_return_val_if_fail (mime_type
!= NULL
, NULL
);
310 key
= g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type
, NULL
);
311 content_type
= get_registry_classes_key (key
, L
"Extension");
318 g_content_type_guess (const gchar
*filename
,
321 gboolean
*result_uncertain
)
329 if (result_uncertain
)
330 *result_uncertain
= FALSE
;
334 basename
= g_path_get_basename (filename
);
335 dot
= strrchr (basename
, '.');
337 type
= g_strdup (dot
);
344 if (data
&& looks_like_text (data
, data_size
))
345 return g_strdup (".txt");
347 return g_strdup ("*");
351 g_content_types_get_registered (void)
354 wchar_t keyname
[256];
362 while (RegEnumKeyExW(HKEY_CLASSES_ROOT
,
369 NULL
) == ERROR_SUCCESS
)
371 key_utf8
= g_utf16_to_utf8 (keyname
, -1, NULL
, NULL
, NULL
);
374 if (*key_utf8
== '.')
375 types
= g_list_prepend (types
, key_utf8
);
383 return g_list_reverse (types
);
387 g_content_type_guess_for_tree (GFile
*root
)
389 /* FIXME: implement */
393 #else /* !G_OS_WIN32 - Unix specific version */
397 #define XDG_PREFIX _gio_xdg
398 #include "xdgmime/xdgmime.h"
400 /* We lock this mutex whenever we modify global state in this module. */
401 G_LOCK_DEFINE_STATIC (gio_xdgmime
);
404 _g_unix_content_type_get_sniff_len (void)
408 G_LOCK (gio_xdgmime
);
409 size
= xdg_mime_get_max_buffer_extents ();
410 G_UNLOCK (gio_xdgmime
);
416 _g_unix_content_type_unalias (const gchar
*type
)
420 G_LOCK (gio_xdgmime
);
421 res
= g_strdup (xdg_mime_unalias_mime_type (type
));
422 G_UNLOCK (gio_xdgmime
);
428 _g_unix_content_type_get_parents (const gchar
*type
)
435 array
= g_ptr_array_new ();
437 G_LOCK (gio_xdgmime
);
439 umime
= xdg_mime_unalias_mime_type (type
);
441 g_ptr_array_add (array
, g_strdup (umime
));
443 parents
= xdg_mime_list_mime_parents (umime
);
444 for (i
= 0; parents
&& parents
[i
] != NULL
; i
++)
445 g_ptr_array_add (array
, g_strdup (parents
[i
]));
449 G_UNLOCK (gio_xdgmime
);
451 g_ptr_array_add (array
, NULL
);
453 return (gchar
**)g_ptr_array_free (array
, FALSE
);
457 * g_content_type_equals:
458 * @type1: a content type string
459 * @type2: a content type string
461 * Compares two content types for equality.
463 * Returns: %TRUE if the two strings are identical or equivalent,
467 g_content_type_equals (const gchar
*type1
,
472 g_return_val_if_fail (type1
!= NULL
, FALSE
);
473 g_return_val_if_fail (type2
!= NULL
, FALSE
);
475 G_LOCK (gio_xdgmime
);
476 res
= xdg_mime_mime_type_equal (type1
, type2
);
477 G_UNLOCK (gio_xdgmime
);
483 * g_content_type_is_a:
484 * @type: a content type string
485 * @supertype: a content type string
487 * Determines if @type is a subset of @supertype.
489 * Returns: %TRUE if @type is a kind of @supertype,
493 g_content_type_is_a (const gchar
*type
,
494 const gchar
*supertype
)
498 g_return_val_if_fail (type
!= NULL
, FALSE
);
499 g_return_val_if_fail (supertype
!= NULL
, FALSE
);
501 G_LOCK (gio_xdgmime
);
502 res
= xdg_mime_mime_type_subclass (type
, supertype
);
503 G_UNLOCK (gio_xdgmime
);
509 * g_content_type_is_unknown:
510 * @type: a content type string
512 * Checks if the content type is the generic "unknown" type.
513 * On UNIX this is the "application/octet-stream" mimetype,
514 * while on win32 it is "*".
516 * Returns: %TRUE if the type is the unknown type.
519 g_content_type_is_unknown (const gchar
*type
)
521 g_return_val_if_fail (type
!= NULL
, FALSE
);
523 return strcmp (XDG_MIME_TYPE_UNKNOWN
, type
) == 0;
529 MIME_TAG_TYPE_COMMENT
534 int current_lang_level
;
535 int comment_lang_level
;
541 language_level (const char *lang
)
543 const char * const *lang_list
;
546 /* The returned list is sorted from most desirable to least
547 desirable and always contains the default locale "C". */
548 lang_list
= g_get_language_names ();
550 for (i
= 0; lang_list
[i
]; i
++)
551 if (strcmp (lang_list
[i
], lang
) == 0)
558 mime_info_start_element (GMarkupParseContext
*context
,
559 const gchar
*element_name
,
560 const gchar
**attribute_names
,
561 const gchar
**attribute_values
,
567 MimeParser
*parser
= user_data
;
569 if (strcmp (element_name
, "comment") == 0)
572 for (i
= 0; attribute_names
[i
]; i
++)
573 if (strcmp (attribute_names
[i
], "xml:lang") == 0)
575 lang
= attribute_values
[i
];
579 parser
->current_lang_level
= language_level (lang
);
580 parser
->current_type
= MIME_TAG_TYPE_COMMENT
;
583 parser
->current_type
= MIME_TAG_TYPE_OTHER
;
587 mime_info_end_element (GMarkupParseContext
*context
,
588 const gchar
*element_name
,
592 MimeParser
*parser
= user_data
;
594 parser
->current_type
= MIME_TAG_TYPE_OTHER
;
598 mime_info_text (GMarkupParseContext
*context
,
604 MimeParser
*parser
= user_data
;
606 if (parser
->current_type
== MIME_TAG_TYPE_COMMENT
&&
607 parser
->current_lang_level
> parser
->comment_lang_level
)
609 g_free (parser
->comment
);
610 parser
->comment
= g_strndup (text
, text_len
);
611 parser
->comment_lang_level
= parser
->current_lang_level
;
616 load_comment_for_mime_helper (const char *dir
,
617 const char *basename
)
619 GMarkupParseContext
*context
;
620 char *filename
, *data
;
623 MimeParser parse_data
= {0};
624 GMarkupParser parser
= {
625 mime_info_start_element
,
626 mime_info_end_element
,
630 filename
= g_build_filename (dir
, "mime", basename
, NULL
);
632 res
= g_file_get_contents (filename
, &data
, &len
, NULL
);
637 context
= g_markup_parse_context_new (&parser
, 0, &parse_data
, NULL
);
638 res
= g_markup_parse_context_parse (context
, data
, len
, NULL
);
640 g_markup_parse_context_free (context
);
645 return parse_data
.comment
;
650 load_comment_for_mime (const char *mimetype
)
652 const char * const* dirs
;
657 basename
= g_strdup_printf ("%s.xml", mimetype
);
659 comment
= load_comment_for_mime_helper (g_get_user_data_dir (), basename
);
666 dirs
= g_get_system_data_dirs ();
668 for (i
= 0; dirs
[i
] != NULL
; i
++)
670 comment
= load_comment_for_mime_helper (dirs
[i
], basename
);
679 return g_strdup_printf (_("%s type"), mimetype
);
683 * g_content_type_get_description:
684 * @type: a content type string
686 * Gets the human readable description of the content type.
688 * Returns: a short description of the content type @type. Free the
689 * returned string with g_free()
692 g_content_type_get_description (const gchar
*type
)
694 static GHashTable
*type_comment_cache
= NULL
;
697 g_return_val_if_fail (type
!= NULL
, NULL
);
699 G_LOCK (gio_xdgmime
);
700 type
= xdg_mime_unalias_mime_type (type
);
702 if (type_comment_cache
== NULL
)
703 type_comment_cache
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
705 comment
= g_hash_table_lookup (type_comment_cache
, type
);
706 comment
= g_strdup (comment
);
707 G_UNLOCK (gio_xdgmime
);
712 comment
= load_comment_for_mime (type
);
714 G_LOCK (gio_xdgmime
);
715 g_hash_table_insert (type_comment_cache
,
718 G_UNLOCK (gio_xdgmime
);
724 * g_content_type_get_mime_type:
725 * @type: a content type string
727 * Gets the mime type for the content type, if one is registered.
729 * Returns: (allow-none): the registered mime type for the given @type,
730 * or %NULL if unknown.
733 g_content_type_get_mime_type (const char *type
)
735 g_return_val_if_fail (type
!= NULL
, NULL
);
737 return g_strdup (type
);
741 * g_content_type_get_icon:
742 * @type: a content type string
744 * Gets the icon for a content type.
746 * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
747 * object with g_object_unref()
750 g_content_type_get_icon (const gchar
*type
)
752 char *mimetype_icon
, *generic_mimetype_icon
, *q
;
753 char *xdg_mimetype_icon
, *legacy_mimetype_icon
;
754 char *xdg_mimetype_generic_icon
;
760 g_return_val_if_fail (type
!= NULL
, NULL
);
762 G_LOCK (gio_xdgmime
);
763 xdg_mimetype_icon
= g_strdup (xdg_mime_get_icon (type
));
764 xdg_mimetype_generic_icon
= g_strdup (xdg_mime_get_generic_icon (type
));
765 G_UNLOCK (gio_xdgmime
);
767 mimetype_icon
= g_strdup (type
);
769 while ((q
= strchr (mimetype_icon
, '/')) != NULL
)
772 p
= strchr (type
, '/');
774 p
= type
+ strlen (type
);
776 /* Not all icons have migrated to the new icon theme spec, look for old names too */
777 legacy_mimetype_icon
= g_strconcat ("gnome-mime-", mimetype_icon
, NULL
);
779 generic_mimetype_icon
= g_malloc (p
- type
+ strlen ("-x-generic") + 1);
780 memcpy (generic_mimetype_icon
, type
, p
- type
);
781 memcpy (generic_mimetype_icon
+ (p
- type
), "-x-generic", strlen ("-x-generic"));
782 generic_mimetype_icon
[(p
- type
) + strlen ("-x-generic")] = 0;
784 if (xdg_mimetype_icon
)
785 icon_names
[n
++] = xdg_mimetype_icon
;
787 icon_names
[n
++] = mimetype_icon
;
788 icon_names
[n
++] = legacy_mimetype_icon
;
790 if (xdg_mimetype_generic_icon
)
791 icon_names
[n
++] = xdg_mimetype_generic_icon
;
793 icon_names
[n
++] = generic_mimetype_icon
;
795 themed_icon
= g_themed_icon_new_from_names (icon_names
, n
);
797 g_free (xdg_mimetype_icon
);
798 g_free (xdg_mimetype_generic_icon
);
799 g_free (mimetype_icon
);
800 g_free (legacy_mimetype_icon
);
801 g_free (generic_mimetype_icon
);
807 * g_content_type_can_be_executable:
808 * @type: a content type string
810 * Checks if a content type can be executable. Note that for instance
811 * things like text files can be executables (i.e. scripts and batch files).
813 * Returns: %TRUE if the file type corresponds to a type that
814 * can be executable, %FALSE otherwise.
817 g_content_type_can_be_executable (const gchar
*type
)
819 g_return_val_if_fail (type
!= NULL
, FALSE
);
821 if (g_content_type_is_a (type
, "application/x-executable") ||
822 g_content_type_is_a (type
, "text/plain"))
829 looks_like_text (const guchar
*data
, gsize data_size
)
834 for (i
= 0; i
< data_size
; i
++)
838 if (g_ascii_iscntrl (c
) &&
839 !g_ascii_isspace (c
) &&
847 * g_content_type_from_mime_type:
848 * @mime_type: a mime type string
850 * Tries to find a content type based on the mime type name.
852 * Returns: (allow-none): Newly allocated string with content type
853 * or %NULL. Free with g_free()
858 g_content_type_from_mime_type (const gchar
*mime_type
)
862 g_return_val_if_fail (mime_type
!= NULL
, NULL
);
864 G_LOCK (gio_xdgmime
);
865 /* mime type and content type are same on unixes */
866 umime
= g_strdup (xdg_mime_unalias_mime_type (mime_type
));
867 G_UNLOCK (gio_xdgmime
);
873 * g_content_type_guess:
874 * @filename: (allow-none): a string, or %NULL
875 * @data: (allow-none) (array length=data_size): a stream of data, or %NULL
876 * @data_size: the size of @data
877 * @result_uncertain: (allow-none) (out): return location for the certainty
878 * of the result, or %NULL
880 * Guesses the content type based on example data. If the function is
881 * uncertain, @result_uncertain will be set to %TRUE. Either @filename
882 * or @data may be %NULL, in which case the guess will be based solely
883 * on the other argument.
885 * Returns: a string indicating a guessed content type for the
886 * given data. Free with g_free()
889 g_content_type_guess (const gchar
*filename
,
892 gboolean
*result_uncertain
)
895 const char *name_mimetypes
[10], *sniffed_mimetype
;
898 int n_name_mimetypes
;
902 n_name_mimetypes
= 0;
903 sniffed_mimetype
= XDG_MIME_TYPE_UNKNOWN
;
905 if (result_uncertain
)
906 *result_uncertain
= FALSE
;
908 G_LOCK (gio_xdgmime
);
912 i
= strlen (filename
);
913 if (filename
[i
- 1] == '/')
915 name_mimetypes
[0] = "inode/directory";
916 name_mimetypes
[1] = NULL
;
917 n_name_mimetypes
= 1;
918 if (result_uncertain
)
919 *result_uncertain
= TRUE
;
923 basename
= g_path_get_basename (filename
);
924 n_name_mimetypes
= xdg_mime_get_mime_types_from_file_name (basename
, name_mimetypes
, 10);
929 /* Got an extension match, and no conflicts. This is it. */
930 if (n_name_mimetypes
== 1)
932 G_UNLOCK (gio_xdgmime
);
933 return g_strdup (name_mimetypes
[0]);
938 sniffed_mimetype
= xdg_mime_get_mime_type_for_data (data
, data_size
, &sniffed_prio
);
939 if (sniffed_mimetype
== XDG_MIME_TYPE_UNKNOWN
&&
941 looks_like_text (data
, data_size
))
942 sniffed_mimetype
= "text/plain";
944 /* For security reasons we don't ever want to sniff desktop files
945 * where we know the filename and it doesn't have a .desktop extension.
946 * This is because desktop files allow executing any application and
947 * we don't want to make it possible to hide them looking like something
950 if (filename
!= NULL
&&
951 strcmp (sniffed_mimetype
, "application/x-desktop") == 0)
952 sniffed_mimetype
= "text/plain";
955 if (n_name_mimetypes
== 0)
957 if (sniffed_mimetype
== XDG_MIME_TYPE_UNKNOWN
&&
959 *result_uncertain
= TRUE
;
961 mimetype
= g_strdup (sniffed_mimetype
);
966 if (sniffed_mimetype
!= XDG_MIME_TYPE_UNKNOWN
)
968 if (sniffed_prio
>= 80) /* High priority sniffing match, use that */
969 mimetype
= g_strdup (sniffed_mimetype
);
972 /* There are conflicts between the name matches and we
973 * have a sniffed type, use that as a tie breaker.
975 for (i
= 0; i
< n_name_mimetypes
; i
++)
977 if ( xdg_mime_mime_type_subclass (name_mimetypes
[i
], sniffed_mimetype
))
979 /* This nametype match is derived from (or the same as)
980 * the sniffed type). This is probably it.
982 mimetype
= g_strdup (name_mimetypes
[i
]);
989 if (mimetype
== NULL
)
991 /* Conflicts, and sniffed type was no help or not there.
992 * Guess on the first one
994 mimetype
= g_strdup (name_mimetypes
[0]);
995 if (result_uncertain
)
996 *result_uncertain
= TRUE
;
1000 G_UNLOCK (gio_xdgmime
);
1006 enumerate_mimetypes_subdir (const char *dir
,
1008 GHashTable
*mimetypes
)
1017 while ((ent
= readdir (d
)) != NULL
)
1019 if (g_str_has_suffix (ent
->d_name
, ".xml"))
1021 mimetype
= g_strdup_printf ("%s/%.*s", prefix
, (int) strlen (ent
->d_name
) - 4, ent
->d_name
);
1022 g_hash_table_replace (mimetypes
, mimetype
, NULL
);
1030 enumerate_mimetypes_dir (const char *dir
,
1031 GHashTable
*mimetypes
)
1038 mimedir
= g_build_filename (dir
, "mime", NULL
);
1040 d
= opendir (mimedir
);
1043 while ((ent
= readdir (d
)) != NULL
)
1045 if (strcmp (ent
->d_name
, "packages") != 0)
1047 name
= g_build_filename (mimedir
, ent
->d_name
, NULL
);
1048 if (g_file_test (name
, G_FILE_TEST_IS_DIR
))
1049 enumerate_mimetypes_subdir (name
, ent
->d_name
, mimetypes
);
1060 * g_content_types_get_registered:
1062 * Gets a list of strings containing all the registered content types
1063 * known to the system. The list and its data should be freed using
1065 * g_list_foreach (list, g_free, NULL);
1066 * g_list_free (list);
1069 * Returns: (element-type utf8) (transfer full): #GList of the registered content types
1072 g_content_types_get_registered (void)
1074 const char * const* dirs
;
1075 GHashTable
*mimetypes
;
1076 GHashTableIter iter
;
1081 mimetypes
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
1083 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes
);
1084 dirs
= g_get_system_data_dirs ();
1086 for (i
= 0; dirs
[i
] != NULL
; i
++)
1087 enumerate_mimetypes_dir (dirs
[i
], mimetypes
);
1090 g_hash_table_iter_init (&iter
, mimetypes
);
1091 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
1093 l
= g_list_prepend (l
, key
);
1094 g_hash_table_iter_steal (&iter
);
1097 g_hash_table_destroy (mimetypes
);
1103 /* tree magic data */
1104 static GList
*tree_matches
= NULL
;
1105 static gboolean need_reload
= FALSE
;
1107 G_LOCK_DEFINE_STATIC (gio_treemagic
);
1113 guint match_case
: 1;
1114 guint executable
: 1;
1115 guint non_empty
: 1;
1130 tree_matchlet_free (TreeMatchlet
*matchlet
)
1132 g_list_foreach (matchlet
->matches
, (GFunc
)tree_matchlet_free
, NULL
);
1133 g_list_free (matchlet
->matches
);
1134 g_free (matchlet
->path
);
1135 g_free (matchlet
->mimetype
);
1136 g_slice_free (TreeMatchlet
, matchlet
);
1140 tree_match_free (TreeMatch
*match
)
1142 g_list_foreach (match
->matches
, (GFunc
)tree_matchlet_free
, NULL
);
1143 g_list_free (match
->matches
);
1144 g_free (match
->contenttype
);
1145 g_slice_free (TreeMatch
, match
);
1149 parse_header (gchar
*line
)
1155 len
= strlen (line
);
1157 if (line
[0] != '[' || line
[len
- 1] != ']')
1161 s
= strchr (line
, ':');
1163 match
= g_slice_new0 (TreeMatch
);
1164 match
->priority
= atoi (line
+ 1);
1165 match
->contenttype
= g_strdup (s
+ 1);
1170 static TreeMatchlet
*
1171 parse_match_line (gchar
*line
,
1175 TreeMatchlet
*matchlet
;
1179 matchlet
= g_slice_new0 (TreeMatchlet
);
1188 *depth
= atoi (line
);
1189 s
= strchr (line
, '>');
1192 p
= strchr (s
, '"');
1195 matchlet
->path
= g_strdup (s
);
1197 parts
= g_strsplit (s
, ",", 0);
1198 if (strcmp (parts
[0], "=file") == 0)
1199 matchlet
->type
= G_FILE_TYPE_REGULAR
;
1200 else if (strcmp (parts
[0], "=directory") == 0)
1201 matchlet
->type
= G_FILE_TYPE_DIRECTORY
;
1202 else if (strcmp (parts
[0], "=link") == 0)
1203 matchlet
->type
= G_FILE_TYPE_SYMBOLIC_LINK
;
1205 matchlet
->type
= G_FILE_TYPE_UNKNOWN
;
1206 for (i
= 1; parts
[i
]; i
++)
1208 if (strcmp (parts
[i
], "executable") == 0)
1209 matchlet
->executable
= 1;
1210 else if (strcmp (parts
[i
], "match-case") == 0)
1211 matchlet
->match_case
= 1;
1212 else if (strcmp (parts
[i
], "non-empty") == 0)
1213 matchlet
->non_empty
= 1;
1214 else if (strcmp (parts
[i
], "on-disc") == 0)
1215 matchlet
->on_disc
= 1;
1217 matchlet
->mimetype
= g_strdup (parts
[i
]);
1226 cmp_match (gconstpointer a
, gconstpointer b
)
1228 const TreeMatch
*aa
= (const TreeMatch
*)a
;
1229 const TreeMatch
*bb
= (const TreeMatch
*)b
;
1231 return bb
->priority
- aa
->priority
;
1235 insert_match (TreeMatch
*match
)
1237 tree_matches
= g_list_insert_sorted (tree_matches
, match
, cmp_match
);
1241 insert_matchlet (TreeMatch
*match
,
1242 TreeMatchlet
*matchlet
,
1246 match
->matches
= g_list_append (match
->matches
, matchlet
);
1252 last
= g_list_last (match
->matches
);
1255 tree_matchlet_free (matchlet
);
1256 g_warning ("can't insert tree matchlet at depth %d", depth
);
1260 m
= (TreeMatchlet
*) last
->data
;
1263 last
= g_list_last (m
->matches
);
1266 tree_matchlet_free (matchlet
);
1267 g_warning ("can't insert tree matchlet at depth %d", depth
);
1271 m
= (TreeMatchlet
*) last
->data
;
1273 m
->matches
= g_list_append (m
->matches
, matchlet
);
1278 read_tree_magic_from_directory (const gchar
*prefix
)
1286 TreeMatchlet
*matchlet
;
1289 filename
= g_build_filename (prefix
, "mime", "treemagic", NULL
);
1291 if (g_file_get_contents (filename
, &text
, &len
, NULL
))
1293 if (strcmp (text
, "MIME-TreeMagic") == 0)
1295 lines
= g_strsplit (text
+ strlen ("MIME-TreeMagic") + 2, "\n", 0);
1297 for (i
= 0; lines
[i
] && lines
[i
][0]; i
++)
1299 if (lines
[i
][0] == '[')
1301 match
= parse_header (lines
[i
]);
1302 insert_match (match
);
1306 matchlet
= parse_match_line (lines
[i
], &depth
);
1307 insert_matchlet (match
, matchlet
, depth
);
1314 g_warning ("%s: header not found, skipping\n", filename
);
1324 xdg_mime_reload (void *user_data
)
1330 tree_magic_shutdown (void)
1332 g_list_foreach (tree_matches
, (GFunc
)tree_match_free
, NULL
);
1333 g_list_free (tree_matches
);
1334 tree_matches
= NULL
;
1338 tree_magic_init (void)
1340 static gboolean initialized
= FALSE
;
1342 const gchar
* const * dirs
;
1349 xdg_mime_register_reload_callback (xdg_mime_reload
, NULL
, NULL
);
1355 need_reload
= FALSE
;
1357 tree_magic_shutdown ();
1359 dir
= g_get_user_data_dir ();
1360 read_tree_magic_from_directory (dir
);
1361 dirs
= g_get_system_data_dirs ();
1362 for (i
= 0; dirs
[i
]; i
++)
1363 read_tree_magic_from_directory (dirs
[i
]);
1367 /* a filtering enumerator */
1373 gboolean ignore_case
;
1375 gchar
**case_components
;
1376 GFileEnumerator
**enumerators
;
1381 component_match (Enumerator
*e
,
1385 gchar
*case_folded
, *key
;
1388 if (strcmp (name
, e
->components
[depth
]) == 0)
1391 if (!e
->ignore_case
)
1394 case_folded
= g_utf8_casefold (name
, -1);
1395 key
= g_utf8_collate_key (case_folded
, -1);
1397 found
= strcmp (key
, e
->case_components
[depth
]) == 0;
1399 g_free (case_folded
);
1406 next_match_recurse (Enumerator
*e
,
1415 if (e
->enumerators
[depth
] == NULL
)
1419 file
= next_match_recurse (e
, depth
- 1);
1422 e
->children
[depth
] = file
;
1423 e
->enumerators
[depth
] = g_file_enumerate_children (file
,
1424 G_FILE_ATTRIBUTE_STANDARD_NAME
,
1425 G_FILE_QUERY_INFO_NONE
,
1430 if (e
->enumerators
[depth
] == NULL
)
1434 while ((info
= g_file_enumerator_next_file (e
->enumerators
[depth
], NULL
, NULL
)))
1436 name
= g_file_info_get_name (info
);
1437 if (component_match (e
, depth
, name
))
1439 file
= g_file_get_child (e
->children
[depth
], name
);
1440 g_object_unref (info
);
1443 g_object_unref (info
);
1446 g_object_unref (e
->enumerators
[depth
]);
1447 e
->enumerators
[depth
] = NULL
;
1448 g_object_unref (e
->children
[depth
]);
1449 e
->children
[depth
] = NULL
;
1454 enumerator_next (Enumerator
*e
)
1456 return next_match_recurse (e
, e
->depth
- 1);
1460 enumerator_new (GFile
*root
,
1462 gboolean ignore_case
)
1468 e
= g_new0 (Enumerator
, 1);
1469 e
->path
= g_strdup (path
);
1470 e
->ignore_case
= ignore_case
;
1472 e
->components
= g_strsplit (e
->path
, G_DIR_SEPARATOR_S
, -1);
1473 e
->depth
= g_strv_length (e
->components
);
1476 e
->case_components
= g_new0 (char *, e
->depth
+ 1);
1477 for (i
= 0; e
->components
[i
]; i
++)
1479 case_folded
= g_utf8_casefold (e
->components
[i
], -1);
1480 e
->case_components
[i
] = g_utf8_collate_key (case_folded
, -1);
1481 g_free (case_folded
);
1485 e
->children
= g_new0 (GFile
*, e
->depth
);
1486 e
->children
[0] = g_object_ref (root
);
1487 e
->enumerators
= g_new0 (GFileEnumerator
*, e
->depth
);
1488 e
->enumerators
[0] = g_file_enumerate_children (root
,
1489 G_FILE_ATTRIBUTE_STANDARD_NAME
,
1490 G_FILE_QUERY_INFO_NONE
,
1498 enumerator_free (Enumerator
*e
)
1502 for (i
= 0; i
< e
->depth
; i
++)
1504 if (e
->enumerators
[i
])
1505 g_object_unref (e
->enumerators
[i
]);
1507 g_object_unref (e
->children
[i
]);
1510 g_free (e
->enumerators
);
1511 g_free (e
->children
);
1512 g_strfreev (e
->components
);
1513 if (e
->case_components
)
1514 g_strfreev (e
->case_components
);
1520 matchlet_match (TreeMatchlet
*matchlet
,
1530 e
= enumerator_new (root
, matchlet
->path
, !matchlet
->match_case
);
1534 file
= enumerator_next (e
);
1537 enumerator_free (e
);
1541 if (matchlet
->mimetype
)
1542 attrs
= G_FILE_ATTRIBUTE_STANDARD_TYPE
","
1543 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE
","
1544 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
;
1546 attrs
= G_FILE_ATTRIBUTE_STANDARD_TYPE
","
1547 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE
;
1548 info
= g_file_query_info (file
,
1550 G_FILE_QUERY_INFO_NONE
,
1557 if (matchlet
->type
!= G_FILE_TYPE_UNKNOWN
&&
1558 g_file_info_get_file_type (info
) != matchlet
->type
)
1561 if (matchlet
->executable
&&
1562 !g_file_info_get_attribute_boolean (info
, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE
))
1568 if (result
&& matchlet
->non_empty
)
1570 GFileEnumerator
*child_enum
;
1571 GFileInfo
*child_info
;
1573 child_enum
= g_file_enumerate_children (file
,
1574 G_FILE_ATTRIBUTE_STANDARD_NAME
,
1575 G_FILE_QUERY_INFO_NONE
,
1581 child_info
= g_file_enumerator_next_file (child_enum
, NULL
, NULL
);
1583 g_object_unref (child_info
);
1586 g_object_unref (child_enum
);
1592 if (result
&& matchlet
->mimetype
)
1594 if (strcmp (matchlet
->mimetype
, g_file_info_get_content_type (info
)) != 0)
1598 g_object_unref (info
);
1599 g_object_unref (file
);
1603 enumerator_free (e
);
1605 if (!matchlet
->matches
)
1608 for (l
= matchlet
->matches
; l
; l
= l
->next
)
1610 TreeMatchlet
*submatchlet
;
1612 submatchlet
= l
->data
;
1613 if (matchlet_match (submatchlet
, root
))
1621 match_match (TreeMatch
*match
,
1627 for (l
= match
->matches
; l
; l
= l
->next
)
1629 TreeMatchlet
*matchlet
= l
->data
;
1630 if (matchlet_match (matchlet
, root
))
1632 g_ptr_array_add (types
, g_strdup (match
->contenttype
));
1639 * g_content_type_guess_for_tree:
1640 * @root: the root of the tree to guess a type for
1642 * Tries to guess the type of the tree with root @root, by
1643 * looking at the files it contains. The result is an array
1644 * of content types, with the best guess coming first.
1646 * The types returned all have the form x-content/foo, e.g.
1647 * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
1648 * (for a camera memory card). See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
1649 * specification for more on x-content types.
1651 * This function is useful in the implementation of
1652 * g_mount_guess_content_type().
1654 * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
1655 * array of zero or more content types, or %NULL. Free with g_strfreev()
1660 g_content_type_guess_for_tree (GFile
*root
)
1665 types
= g_ptr_array_new ();
1667 G_LOCK (gio_treemagic
);
1670 for (l
= tree_matches
; l
; l
= l
->next
)
1672 TreeMatch
*match
= l
->data
;
1673 match_match (match
, root
, types
);
1676 G_UNLOCK (gio_treemagic
);
1678 g_ptr_array_add (types
, NULL
);
1680 return (gchar
**)g_ptr_array_free (types
, FALSE
);
1683 #endif /* Unix version */