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 <ulink url="http://www.wikipedia.org/wiki/Internet_media_type">mime type</ulink> like "text/plain" or "image/png".
46 * On Win32 it is an extension string like ".doc", ".txt" or a perceived
47 * string like "audio". Such strings can be looked up in the registry at
53 #define XDG_PREFIX _gio_xdg
54 #include "xdgmime/xdgmime.h"
56 /* We lock this mutex whenever we modify global state in this module. */
57 G_LOCK_DEFINE_STATIC (gio_xdgmime);
60 _g_unix_content_type_get_sniff_len (void)
65 size = xdg_mime_get_max_buffer_extents ();
66 G_UNLOCK (gio_xdgmime);
72 _g_unix_content_type_unalias (const gchar *type)
77 res = g_strdup (xdg_mime_unalias_mime_type (type));
78 G_UNLOCK (gio_xdgmime);
84 _g_unix_content_type_get_parents (const gchar *type)
91 array = g_ptr_array_new ();
95 umime = xdg_mime_unalias_mime_type (type);
97 g_ptr_array_add (array, g_strdup (umime));
99 parents = xdg_mime_list_mime_parents (umime);
100 for (i = 0; parents && parents[i] != NULL; i++)
101 g_ptr_array_add (array, g_strdup (parents[i]));
105 G_UNLOCK (gio_xdgmime);
107 g_ptr_array_add (array, NULL);
109 return (gchar **)g_ptr_array_free (array, FALSE);
113 * g_content_type_equals:
114 * @type1: a content type string
115 * @type2: a content type string
117 * Compares two content types for equality.
119 * Returns: %TRUE if the two strings are identical or equivalent,
123 g_content_type_equals (const gchar *type1,
128 g_return_val_if_fail (type1 != NULL, FALSE);
129 g_return_val_if_fail (type2 != NULL, FALSE);
131 G_LOCK (gio_xdgmime);
132 res = xdg_mime_mime_type_equal (type1, type2);
133 G_UNLOCK (gio_xdgmime);
139 * g_content_type_is_a:
140 * @type: a content type string
141 * @supertype: a content type string
143 * Determines if @type is a subset of @supertype.
145 * Returns: %TRUE if @type is a kind of @supertype,
149 g_content_type_is_a (const gchar *type,
150 const gchar *supertype)
154 g_return_val_if_fail (type != NULL, FALSE);
155 g_return_val_if_fail (supertype != NULL, FALSE);
157 G_LOCK (gio_xdgmime);
158 res = xdg_mime_mime_type_subclass (type, supertype);
159 G_UNLOCK (gio_xdgmime);
165 * g_content_type_is_unknown:
166 * @type: a content type string
168 * Checks if the content type is the generic "unknown" type.
169 * On UNIX this is the "application/octet-stream" mimetype,
170 * while on win32 it is "*".
172 * Returns: %TRUE if the type is the unknown type.
175 g_content_type_is_unknown (const gchar *type)
177 g_return_val_if_fail (type != NULL, FALSE);
179 return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
185 MIME_TAG_TYPE_COMMENT
190 int current_lang_level;
191 int comment_lang_level;
197 language_level (const char *lang)
199 const char * const *lang_list;
202 /* The returned list is sorted from most desirable to least
203 desirable and always contains the default locale "C". */
204 lang_list = g_get_language_names ();
206 for (i = 0; lang_list[i]; i++)
207 if (strcmp (lang_list[i], lang) == 0)
214 mime_info_start_element (GMarkupParseContext *context,
215 const gchar *element_name,
216 const gchar **attribute_names,
217 const gchar **attribute_values,
223 MimeParser *parser = user_data;
225 if (strcmp (element_name, "comment") == 0)
228 for (i = 0; attribute_names[i]; i++)
229 if (strcmp (attribute_names[i], "xml:lang") == 0)
231 lang = attribute_values[i];
235 parser->current_lang_level = language_level (lang);
236 parser->current_type = MIME_TAG_TYPE_COMMENT;
239 parser->current_type = MIME_TAG_TYPE_OTHER;
243 mime_info_end_element (GMarkupParseContext *context,
244 const gchar *element_name,
248 MimeParser *parser = user_data;
250 parser->current_type = MIME_TAG_TYPE_OTHER;
254 mime_info_text (GMarkupParseContext *context,
260 MimeParser *parser = user_data;
262 if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
263 parser->current_lang_level > parser->comment_lang_level)
265 g_free (parser->comment);
266 parser->comment = g_strndup (text, text_len);
267 parser->comment_lang_level = parser->current_lang_level;
272 load_comment_for_mime_helper (const char *dir,
273 const char *basename)
275 GMarkupParseContext *context;
276 char *filename, *data;
279 MimeParser parse_data = {0};
280 GMarkupParser parser = {
281 mime_info_start_element,
282 mime_info_end_element,
286 filename = g_build_filename (dir, "mime", basename, NULL);
288 res = g_file_get_contents (filename, &data, &len, NULL);
293 context = g_markup_parse_context_new (&parser, 0, &parse_data, NULL);
294 res = g_markup_parse_context_parse (context, data, len, NULL);
296 g_markup_parse_context_free (context);
301 return parse_data.comment;
306 load_comment_for_mime (const char *mimetype)
308 const char * const* dirs;
313 basename = g_strdup_printf ("%s.xml", mimetype);
315 comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
322 dirs = g_get_system_data_dirs ();
324 for (i = 0; dirs[i] != NULL; i++)
326 comment = load_comment_for_mime_helper (dirs[i], basename);
335 return g_strdup_printf (_("%s type"), mimetype);
339 * g_content_type_get_description:
340 * @type: a content type string
342 * Gets the human readable description of the content type.
344 * Returns: a short description of the content type @type. Free the
345 * returned string with g_free()
348 g_content_type_get_description (const gchar *type)
350 static GHashTable *type_comment_cache = NULL;
353 g_return_val_if_fail (type != NULL, NULL);
355 G_LOCK (gio_xdgmime);
356 type = xdg_mime_unalias_mime_type (type);
358 if (type_comment_cache == NULL)
359 type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
361 comment = g_hash_table_lookup (type_comment_cache, type);
362 comment = g_strdup (comment);
363 G_UNLOCK (gio_xdgmime);
368 comment = load_comment_for_mime (type);
370 G_LOCK (gio_xdgmime);
371 g_hash_table_insert (type_comment_cache,
374 G_UNLOCK (gio_xdgmime);
380 * g_content_type_get_mime_type:
381 * @type: a content type string
383 * Gets the mime type for the content type, if one is registered.
385 * Returns: (allow-none): the registered mime type for the given @type,
386 * or %NULL if unknown.
389 g_content_type_get_mime_type (const char *type)
391 g_return_val_if_fail (type != NULL, NULL);
393 return g_strdup (type);
398 g_content_type_get_icon_internal (const gchar *type,
402 char *generic_mimetype_icon = NULL;
404 char *xdg_mimetype_icon = NULL;
405 char *xdg_mimetype_generic_icon;
409 const char *file_template;
410 const char *xdg_icon;
412 g_return_val_if_fail (type != NULL, NULL);
416 file_template = "%s-symbolic";
420 file_template = "%s";
423 G_LOCK (gio_xdgmime);
424 xdg_icon = xdg_mime_get_icon (type);
425 G_UNLOCK (gio_xdgmime);
426 if (xdg_icon != NULL)
427 xdg_mimetype_icon = g_strdup_printf (file_template, xdg_icon);
429 if (xdg_mimetype_icon)
430 icon_names[n++] = xdg_mimetype_icon;
432 mimetype_icon = g_strdup_printf (file_template, type);
433 while ((q = strchr (mimetype_icon, '/')) != NULL)
436 icon_names[n++] = mimetype_icon;
438 xdg_mimetype_generic_icon = g_content_type_get_generic_icon_name (type);
439 if (xdg_mimetype_generic_icon)
440 generic_mimetype_icon = g_strdup_printf (file_template, xdg_mimetype_generic_icon);
441 if (generic_mimetype_icon)
442 icon_names[n++] = generic_mimetype_icon;
444 themed_icon = g_themed_icon_new_from_names (icon_names, n);
446 g_free (xdg_mimetype_icon);
447 g_free (xdg_mimetype_generic_icon);
448 g_free (mimetype_icon);
449 g_free (generic_mimetype_icon);
455 * g_content_type_get_icon:
456 * @type: a content type string
458 * Gets the icon for a content type.
460 * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
461 * object with g_object_unref()
464 g_content_type_get_icon (const gchar *type)
466 return g_content_type_get_icon_internal (type, FALSE);
470 * g_content_type_get_symbolic_icon:
471 * @type: a content type string
473 * Gets the symbolic icon for a content type.
475 * Returns: (transfer full): symbolic #GIcon corresponding to the content type.
476 * Free the returned object with g_object_unref()
481 g_content_type_get_symbolic_icon (const gchar *type)
483 return g_content_type_get_icon_internal (type, TRUE);
487 * g_content_type_get_generic_icon_name:
488 * @type: a content type string
490 * Gets the generic icon name for a content type.
492 * See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
493 * specification for more on the generic icon name.
495 * Returns: (allow-none): the registered generic icon name for the given @type,
496 * or %NULL if unknown. Free with g_free()
501 g_content_type_get_generic_icon_name (const gchar *type)
503 const gchar *xdg_icon_name;
506 G_LOCK (gio_xdgmime);
507 xdg_icon_name = xdg_mime_get_generic_icon (type);
508 G_UNLOCK (gio_xdgmime);
513 const char *suffix = "-x-generic";
515 p = strchr (type, '/');
517 p = type + strlen (type);
519 icon_name = g_malloc (p - type + strlen (suffix) + 1);
520 memcpy (icon_name, type, p - type);
521 memcpy (icon_name + (p - type), suffix, strlen (suffix));
522 icon_name[(p - type) + strlen (suffix)] = 0;
526 icon_name = g_strdup (xdg_icon_name);
533 * g_content_type_can_be_executable:
534 * @type: a content type string
536 * Checks if a content type can be executable. Note that for instance
537 * things like text files can be executables (i.e. scripts and batch files).
539 * Returns: %TRUE if the file type corresponds to a type that
540 * can be executable, %FALSE otherwise.
543 g_content_type_can_be_executable (const gchar *type)
545 g_return_val_if_fail (type != NULL, FALSE);
547 if (g_content_type_is_a (type, "application/x-executable") ||
548 g_content_type_is_a (type, "text/plain"))
555 looks_like_text (const guchar *data, gsize data_size)
560 for (i = 0; i < data_size; i++)
564 if (g_ascii_iscntrl (c) &&
565 !g_ascii_isspace (c) &&
573 * g_content_type_from_mime_type:
574 * @mime_type: a mime type string
576 * Tries to find a content type based on the mime type name.
578 * Returns: (allow-none): Newly allocated string with content type
579 * or %NULL. Free with g_free()
584 g_content_type_from_mime_type (const gchar *mime_type)
588 g_return_val_if_fail (mime_type != NULL, NULL);
590 G_LOCK (gio_xdgmime);
591 /* mime type and content type are same on unixes */
592 umime = g_strdup (xdg_mime_unalias_mime_type (mime_type));
593 G_UNLOCK (gio_xdgmime);
599 * g_content_type_guess:
600 * @filename: (allow-none): a string, or %NULL
601 * @data: (allow-none) (array length=data_size): a stream of data, or %NULL
602 * @data_size: the size of @data
603 * @result_uncertain: (allow-none) (out): return location for the certainty
604 * of the result, or %NULL
606 * Guesses the content type based on example data. If the function is
607 * uncertain, @result_uncertain will be set to %TRUE. Either @filename
608 * or @data may be %NULL, in which case the guess will be based solely
609 * on the other argument.
611 * Returns: a string indicating a guessed content type for the
612 * given data. Free with g_free()
615 g_content_type_guess (const gchar *filename,
618 gboolean *result_uncertain)
621 const char *name_mimetypes[10], *sniffed_mimetype;
624 int n_name_mimetypes;
628 n_name_mimetypes = 0;
629 sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
631 if (result_uncertain)
632 *result_uncertain = FALSE;
634 /* our test suite and potentially other code used -1 in the past, which is
635 * not documented and not allowed; guard against that */
636 g_return_val_if_fail (data_size != (gsize) -1, g_strdup (XDG_MIME_TYPE_UNKNOWN));
638 G_LOCK (gio_xdgmime);
642 i = strlen (filename);
643 if (filename[i - 1] == '/')
645 name_mimetypes[0] = "inode/directory";
646 name_mimetypes[1] = NULL;
647 n_name_mimetypes = 1;
648 if (result_uncertain)
649 *result_uncertain = TRUE;
653 basename = g_path_get_basename (filename);
654 n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
659 /* Got an extension match, and no conflicts. This is it. */
660 if (n_name_mimetypes == 1)
662 gchar *s = g_strdup (name_mimetypes[0]);
663 G_UNLOCK (gio_xdgmime);
669 sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
670 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
672 looks_like_text (data, data_size))
673 sniffed_mimetype = "text/plain";
675 /* For security reasons we don't ever want to sniff desktop files
676 * where we know the filename and it doesn't have a .desktop extension.
677 * This is because desktop files allow executing any application and
678 * we don't want to make it possible to hide them looking like something
681 if (filename != NULL &&
682 strcmp (sniffed_mimetype, "application/x-desktop") == 0)
683 sniffed_mimetype = "text/plain";
686 if (n_name_mimetypes == 0)
688 if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
690 *result_uncertain = TRUE;
692 mimetype = g_strdup (sniffed_mimetype);
697 if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
699 if (sniffed_prio >= 80) /* High priority sniffing match, use that */
700 mimetype = g_strdup (sniffed_mimetype);
703 /* There are conflicts between the name matches and we
704 * have a sniffed type, use that as a tie breaker.
706 for (i = 0; i < n_name_mimetypes; i++)
708 if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
710 /* This nametype match is derived from (or the same as)
711 * the sniffed type). This is probably it.
713 mimetype = g_strdup (name_mimetypes[i]);
720 if (mimetype == NULL)
722 /* Conflicts, and sniffed type was no help or not there.
723 * Guess on the first one
725 mimetype = g_strdup (name_mimetypes[0]);
726 if (result_uncertain)
727 *result_uncertain = TRUE;
731 G_UNLOCK (gio_xdgmime);
737 enumerate_mimetypes_subdir (const char *dir,
739 GHashTable *mimetypes)
748 while ((ent = readdir (d)) != NULL)
750 if (g_str_has_suffix (ent->d_name, ".xml"))
752 mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
753 g_hash_table_replace (mimetypes, mimetype, NULL);
761 enumerate_mimetypes_dir (const char *dir,
762 GHashTable *mimetypes)
769 mimedir = g_build_filename (dir, "mime", NULL);
771 d = opendir (mimedir);
774 while ((ent = readdir (d)) != NULL)
776 if (strcmp (ent->d_name, "packages") != 0)
778 name = g_build_filename (mimedir, ent->d_name, NULL);
779 if (g_file_test (name, G_FILE_TEST_IS_DIR))
780 enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
791 * g_content_types_get_registered:
793 * Gets a list of strings containing all the registered content types
794 * known to the system. The list and its data should be freed using
796 * g_list_free_full (list, g_free);
799 * Returns: (element-type utf8) (transfer full): #GList of the registered content types
802 g_content_types_get_registered (void)
804 const char * const* dirs;
805 GHashTable *mimetypes;
811 mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
813 enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
814 dirs = g_get_system_data_dirs ();
816 for (i = 0; dirs[i] != NULL; i++)
817 enumerate_mimetypes_dir (dirs[i], mimetypes);
820 g_hash_table_iter_init (&iter, mimetypes);
821 while (g_hash_table_iter_next (&iter, &key, NULL))
823 l = g_list_prepend (l, key);
824 g_hash_table_iter_steal (&iter);
827 g_hash_table_destroy (mimetypes);
833 /* tree magic data */
834 static GList *tree_matches = NULL;
835 static gboolean need_reload = FALSE;
837 G_LOCK_DEFINE_STATIC (gio_treemagic);
843 guint match_case : 1;
844 guint executable : 1;
860 tree_matchlet_free (TreeMatchlet *matchlet)
862 g_list_free_full (matchlet->matches, (GDestroyNotify) tree_matchlet_free);
863 g_free (matchlet->path);
864 g_free (matchlet->mimetype);
865 g_slice_free (TreeMatchlet, matchlet);
869 tree_match_free (TreeMatch *match)
871 g_list_free_full (match->matches, (GDestroyNotify) tree_matchlet_free);
872 g_free (match->contenttype);
873 g_slice_free (TreeMatch, match);
877 parse_header (gchar *line)
885 if (line[0] != '[' || line[len - 1] != ']')
889 s = strchr (line, ':');
891 match = g_slice_new0 (TreeMatch);
892 match->priority = atoi (line + 1);
893 match->contenttype = g_strdup (s + 1);
898 static TreeMatchlet *
899 parse_match_line (gchar *line,
903 TreeMatchlet *matchlet;
907 matchlet = g_slice_new0 (TreeMatchlet);
916 *depth = atoi (line);
917 s = strchr (line, '>');
923 matchlet->path = g_strdup (s);
925 parts = g_strsplit (s, ",", 0);
926 if (strcmp (parts[0], "=file") == 0)
927 matchlet->type = G_FILE_TYPE_REGULAR;
928 else if (strcmp (parts[0], "=directory") == 0)
929 matchlet->type = G_FILE_TYPE_DIRECTORY;
930 else if (strcmp (parts[0], "=link") == 0)
931 matchlet->type = G_FILE_TYPE_SYMBOLIC_LINK;
933 matchlet->type = G_FILE_TYPE_UNKNOWN;
934 for (i = 1; parts[i]; i++)
936 if (strcmp (parts[i], "executable") == 0)
937 matchlet->executable = 1;
938 else if (strcmp (parts[i], "match-case") == 0)
939 matchlet->match_case = 1;
940 else if (strcmp (parts[i], "non-empty") == 0)
941 matchlet->non_empty = 1;
942 else if (strcmp (parts[i], "on-disc") == 0)
943 matchlet->on_disc = 1;
945 matchlet->mimetype = g_strdup (parts[i]);
954 cmp_match (gconstpointer a, gconstpointer b)
956 const TreeMatch *aa = (const TreeMatch *)a;
957 const TreeMatch *bb = (const TreeMatch *)b;
959 return bb->priority - aa->priority;
963 insert_match (TreeMatch *match)
965 tree_matches = g_list_insert_sorted (tree_matches, match, cmp_match);
969 insert_matchlet (TreeMatch *match,
970 TreeMatchlet *matchlet,
974 match->matches = g_list_append (match->matches, matchlet);
980 last = g_list_last (match->matches);
983 tree_matchlet_free (matchlet);
984 g_warning ("can't insert tree matchlet at depth %d", depth);
988 m = (TreeMatchlet *) last->data;
991 last = g_list_last (m->matches);
994 tree_matchlet_free (matchlet);
995 g_warning ("can't insert tree matchlet at depth %d", depth);
999 m = (TreeMatchlet *) last->data;
1001 m->matches = g_list_append (m->matches, matchlet);
1006 read_tree_magic_from_directory (const gchar *prefix)
1014 TreeMatchlet *matchlet;
1017 filename = g_build_filename (prefix, "mime", "treemagic", NULL);
1019 if (g_file_get_contents (filename, &text, &len, NULL))
1021 if (strcmp (text, "MIME-TreeMagic") == 0)
1023 lines = g_strsplit (text + strlen ("MIME-TreeMagic") + 2, "\n", 0);
1025 for (i = 0; lines[i] && lines[i][0]; i++)
1027 if (lines[i][0] == '[')
1029 match = parse_header (lines[i]);
1030 insert_match (match);
1034 matchlet = parse_match_line (lines[i], &depth);
1035 insert_matchlet (match, matchlet, depth);
1042 g_warning ("%s: header not found, skipping\n", filename);
1052 xdg_mime_reload (void *user_data)
1058 tree_magic_shutdown (void)
1060 g_list_free_full (tree_matches, (GDestroyNotify) tree_match_free);
1061 tree_matches = NULL;
1065 tree_magic_init (void)
1067 static gboolean initialized = FALSE;
1069 const gchar * const * dirs;
1076 xdg_mime_register_reload_callback (xdg_mime_reload, NULL, NULL);
1082 need_reload = FALSE;
1084 tree_magic_shutdown ();
1086 dir = g_get_user_data_dir ();
1087 read_tree_magic_from_directory (dir);
1088 dirs = g_get_system_data_dirs ();
1089 for (i = 0; dirs[i]; i++)
1090 read_tree_magic_from_directory (dirs[i]);
1094 /* a filtering enumerator */
1100 gboolean ignore_case;
1102 gchar **case_components;
1103 GFileEnumerator **enumerators;
1108 component_match (Enumerator *e,
1112 gchar *case_folded, *key;
1115 if (strcmp (name, e->components[depth]) == 0)
1118 if (!e->ignore_case)
1121 case_folded = g_utf8_casefold (name, -1);
1122 key = g_utf8_collate_key (case_folded, -1);
1124 found = strcmp (key, e->case_components[depth]) == 0;
1126 g_free (case_folded);
1133 next_match_recurse (Enumerator *e,
1142 if (e->enumerators[depth] == NULL)
1146 file = next_match_recurse (e, depth - 1);
1149 e->children[depth] = file;
1150 e->enumerators[depth] = g_file_enumerate_children (file,
1151 G_FILE_ATTRIBUTE_STANDARD_NAME,
1152 G_FILE_QUERY_INFO_NONE,
1157 if (e->enumerators[depth] == NULL)
1161 while ((info = g_file_enumerator_next_file (e->enumerators[depth], NULL, NULL)))
1163 name = g_file_info_get_name (info);
1164 if (component_match (e, depth, name))
1166 file = g_file_get_child (e->children[depth], name);
1167 g_object_unref (info);
1170 g_object_unref (info);
1173 g_object_unref (e->enumerators[depth]);
1174 e->enumerators[depth] = NULL;
1175 g_object_unref (e->children[depth]);
1176 e->children[depth] = NULL;
1181 enumerator_next (Enumerator *e)
1183 return next_match_recurse (e, e->depth - 1);
1187 enumerator_new (GFile *root,
1189 gboolean ignore_case)
1195 e = g_new0 (Enumerator, 1);
1196 e->path = g_strdup (path);
1197 e->ignore_case = ignore_case;
1199 e->components = g_strsplit (e->path, G_DIR_SEPARATOR_S, -1);
1200 e->depth = g_strv_length (e->components);
1203 e->case_components = g_new0 (char *, e->depth + 1);
1204 for (i = 0; e->components[i]; i++)
1206 case_folded = g_utf8_casefold (e->components[i], -1);
1207 e->case_components[i] = g_utf8_collate_key (case_folded, -1);
1208 g_free (case_folded);
1212 e->children = g_new0 (GFile *, e->depth);
1213 e->children[0] = g_object_ref (root);
1214 e->enumerators = g_new0 (GFileEnumerator *, e->depth);
1215 e->enumerators[0] = g_file_enumerate_children (root,
1216 G_FILE_ATTRIBUTE_STANDARD_NAME,
1217 G_FILE_QUERY_INFO_NONE,
1225 enumerator_free (Enumerator *e)
1229 for (i = 0; i < e->depth; i++)
1231 if (e->enumerators[i])
1232 g_object_unref (e->enumerators[i]);
1234 g_object_unref (e->children[i]);
1237 g_free (e->enumerators);
1238 g_free (e->children);
1239 g_strfreev (e->components);
1240 if (e->case_components)
1241 g_strfreev (e->case_components);
1247 matchlet_match (TreeMatchlet *matchlet,
1257 e = enumerator_new (root, matchlet->path, !matchlet->match_case);
1261 file = enumerator_next (e);
1264 enumerator_free (e);
1268 if (matchlet->mimetype)
1269 attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1270 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ","
1271 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
1273 attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
1274 G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE;
1275 info = g_file_query_info (file,
1277 G_FILE_QUERY_INFO_NONE,
1284 if (matchlet->type != G_FILE_TYPE_UNKNOWN &&
1285 g_file_info_get_file_type (info) != matchlet->type)
1288 if (matchlet->executable &&
1289 !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
1295 if (result && matchlet->non_empty)
1297 GFileEnumerator *child_enum;
1298 GFileInfo *child_info;
1300 child_enum = g_file_enumerate_children (file,
1301 G_FILE_ATTRIBUTE_STANDARD_NAME,
1302 G_FILE_QUERY_INFO_NONE,
1308 child_info = g_file_enumerator_next_file (child_enum, NULL, NULL);
1310 g_object_unref (child_info);
1313 g_object_unref (child_enum);
1319 if (result && matchlet->mimetype)
1321 if (strcmp (matchlet->mimetype, g_file_info_get_content_type (info)) != 0)
1325 g_object_unref (info);
1326 g_object_unref (file);
1330 enumerator_free (e);
1332 if (!matchlet->matches)
1335 for (l = matchlet->matches; l; l = l->next)
1337 TreeMatchlet *submatchlet;
1339 submatchlet = l->data;
1340 if (matchlet_match (submatchlet, root))
1348 match_match (TreeMatch *match,
1354 for (l = match->matches; l; l = l->next)
1356 TreeMatchlet *matchlet = l->data;
1357 if (matchlet_match (matchlet, root))
1359 g_ptr_array_add (types, g_strdup (match->contenttype));
1366 * g_content_type_guess_for_tree:
1367 * @root: the root of the tree to guess a type for
1369 * Tries to guess the type of the tree with root @root, by
1370 * looking at the files it contains. The result is an array
1371 * of content types, with the best guess coming first.
1373 * The types returned all have the form x-content/foo, e.g.
1374 * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
1375 * (for a camera memory card). See the <ulink url="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec">shared-mime-info</ulink>
1376 * specification for more on x-content types.
1378 * This function is useful in the implementation of
1379 * g_mount_guess_content_type().
1381 * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
1382 * array of zero or more content types. Free with g_strfreev()
1387 g_content_type_guess_for_tree (GFile *root)
1392 types = g_ptr_array_new ();
1394 G_LOCK (gio_treemagic);
1397 for (l = tree_matches; l; l = l->next)
1399 TreeMatch *match = l->data;
1400 match_match (match, root, types);
1403 G_UNLOCK (gio_treemagic);
1405 g_ptr_array_add (types, NULL);
1407 return (gchar **)g_ptr_array_free (types, FALSE);