Blame gio/gcontenttype.c

Packit ae235b
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Packit ae235b
Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include "gcontenttypeprivate.h"
Packit ae235b
#include "gthemedicon.h"
Packit ae235b
#include "gicon.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gfileenumerator.h"
Packit ae235b
#include "gfileinfo.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gcontenttype
Packit ae235b
 * @short_description: Platform-specific content typing
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * A content type is a platform specific string that defines the type
Packit ae235b
 * of a file. On UNIX it is a
Packit ae235b
 * [mime type](http://www.wikipedia.org/wiki/Internet_media_type)
Packit ae235b
 * like "text/plain" or "image/png".
Packit ae235b
 * On Win32 it is an extension string like ".doc", ".txt" or a perceived
Packit ae235b
 * string like "audio". Such strings can be looked up in the registry at
Packit ae235b
 * HKEY_CLASSES_ROOT.
Packit ae235b
 * On OSX it is a [Uniform Type Identifier](https://en.wikipedia.org/wiki/Uniform_Type_Identifier)
Packit ae235b
 * such as "com.apple.application".
Packit ae235b
 **/
Packit ae235b
Packit ae235b
#include <dirent.h>
Packit ae235b
Packit ae235b
#define XDG_PREFIX _gio_xdg
Packit ae235b
#include "xdgmime/xdgmime.h"
Packit ae235b
Packit ae235b
/* We lock this mutex whenever we modify global state in this module.  */
Packit ae235b
G_LOCK_DEFINE_STATIC (gio_xdgmime);
Packit ae235b
Packit ae235b
gsize
Packit ae235b
_g_unix_content_type_get_sniff_len (void)
Packit ae235b
{
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  size = xdg_mime_get_max_buffer_extents ();
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return size;
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
_g_unix_content_type_unalias (const gchar *type)
Packit ae235b
{
Packit ae235b
  gchar *res;
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  res = g_strdup (xdg_mime_unalias_mime_type (type));
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar **
Packit ae235b
_g_unix_content_type_get_parents (const gchar *type)
Packit ae235b
{
Packit ae235b
  const gchar *umime;
Packit ae235b
  gchar **parents;
Packit ae235b
  GPtrArray *array;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  array = g_ptr_array_new ();
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  umime = xdg_mime_unalias_mime_type (type);
Packit ae235b
Packit ae235b
  g_ptr_array_add (array, g_strdup (umime));
Packit ae235b
Packit ae235b
  parents = xdg_mime_list_mime_parents (umime);
Packit ae235b
  for (i = 0; parents && parents[i] != NULL; i++)
Packit ae235b
    g_ptr_array_add (array, g_strdup (parents[i]));
Packit ae235b
Packit ae235b
  free (parents);
Packit ae235b
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  g_ptr_array_add (array, NULL);
Packit ae235b
Packit ae235b
  return (gchar **)g_ptr_array_free (array, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_equals:
Packit ae235b
 * @type1: a content type string
Packit ae235b
 * @type2: a content type string
Packit ae235b
 *
Packit ae235b
 * Compares two content types for equality.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the two strings are identical or equivalent,
Packit ae235b
 *     %FALSE otherwise.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_content_type_equals (const gchar *type1,
Packit ae235b
                       const gchar *type2)
Packit ae235b
{
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type1 != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (type2 != NULL, FALSE);
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  res = xdg_mime_mime_type_equal (type1, type2);
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_is_a:
Packit ae235b
 * @type: a content type string
Packit ae235b
 * @supertype: a content type string
Packit ae235b
 *
Packit ae235b
 * Determines if @type is a subset of @supertype.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @type is a kind of @supertype,
Packit ae235b
 *     %FALSE otherwise.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_content_type_is_a (const gchar *type,
Packit ae235b
                     const gchar *supertype)
Packit ae235b
{
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (supertype != NULL, FALSE);
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  res = xdg_mime_mime_type_subclass (type, supertype);
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_is_mime_type:
Packit ae235b
 * @type: a content type string
Packit ae235b
 * @mime_type: a mime type string
Packit ae235b
 *
Packit ae235b
 * Determines if @type is a subset of @mime_type.
Packit ae235b
 * Convenience wrapper around g_content_type_is_a().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @type is a kind of @mime_type,
Packit ae235b
 *     %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.52
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_content_type_is_mime_type (const gchar *type,
Packit ae235b
                             const gchar *mime_type)
Packit ae235b
{
Packit ae235b
  return g_content_type_is_a (type, mime_type);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_is_unknown:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Checks if the content type is the generic "unknown" type.
Packit ae235b
 * On UNIX this is the "application/octet-stream" mimetype,
Packit ae235b
 * while on win32 it is "*" and on OSX it is a dynamic type
Packit ae235b
 * or octet-stream.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the type is the unknown type.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_content_type_is_unknown (const gchar *type)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (type != NULL, FALSE);
Packit ae235b
Packit ae235b
  return strcmp (XDG_MIME_TYPE_UNKNOWN, type) == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
typedef enum {
Packit ae235b
  MIME_TAG_TYPE_OTHER,
Packit ae235b
  MIME_TAG_TYPE_COMMENT
Packit ae235b
} MimeTagType;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  int current_type;
Packit ae235b
  int current_lang_level;
Packit ae235b
  int comment_lang_level;
Packit ae235b
  char *comment;
Packit ae235b
} MimeParser;
Packit ae235b
Packit ae235b
Packit ae235b
static int
Packit ae235b
language_level (const char *lang)
Packit ae235b
{
Packit ae235b
  const char * const *lang_list;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  /* The returned list is sorted from most desirable to least
Packit ae235b
     desirable and always contains the default locale "C". */
Packit ae235b
  lang_list = g_get_language_names ();
Packit ae235b
Packit ae235b
  for (i = 0; lang_list[i]; i++)
Packit ae235b
    if (strcmp (lang_list[i], lang) == 0)
Packit ae235b
      return 1000-i;
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mime_info_start_element (GMarkupParseContext  *context,
Packit ae235b
                         const gchar          *element_name,
Packit ae235b
                         const gchar         **attribute_names,
Packit ae235b
                         const gchar         **attribute_values,
Packit ae235b
                         gpointer              user_data,
Packit ae235b
                         GError              **error)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  const char *lang;
Packit ae235b
  MimeParser *parser = user_data;
Packit ae235b
Packit ae235b
  if (strcmp (element_name, "comment") == 0)
Packit ae235b
    {
Packit ae235b
      lang = "C";
Packit ae235b
      for (i = 0; attribute_names[i]; i++)
Packit ae235b
        if (strcmp (attribute_names[i], "xml:lang") == 0)
Packit ae235b
          {
Packit ae235b
            lang = attribute_values[i];
Packit ae235b
            break;
Packit ae235b
          }
Packit ae235b
Packit ae235b
      parser->current_lang_level = language_level (lang);
Packit ae235b
      parser->current_type = MIME_TAG_TYPE_COMMENT;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    parser->current_type = MIME_TAG_TYPE_OTHER;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mime_info_end_element (GMarkupParseContext  *context,
Packit ae235b
                       const gchar          *element_name,
Packit ae235b
                       gpointer              user_data,
Packit ae235b
                       GError              **error)
Packit ae235b
{
Packit ae235b
  MimeParser *parser = user_data;
Packit ae235b
Packit ae235b
  parser->current_type = MIME_TAG_TYPE_OTHER;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mime_info_text (GMarkupParseContext  *context,
Packit ae235b
                const gchar          *text,
Packit ae235b
                gsize                 text_len,
Packit ae235b
                gpointer              user_data,
Packit ae235b
                GError              **error)
Packit ae235b
{
Packit ae235b
  MimeParser *parser = user_data;
Packit ae235b
Packit ae235b
  if (parser->current_type == MIME_TAG_TYPE_COMMENT &&
Packit ae235b
      parser->current_lang_level > parser->comment_lang_level)
Packit ae235b
    {
Packit ae235b
      g_free (parser->comment);
Packit ae235b
      parser->comment = g_strndup (text, text_len);
Packit ae235b
      parser->comment_lang_level = parser->current_lang_level;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
load_comment_for_mime_helper (const char *dir,
Packit ae235b
                              const char *basename)
Packit ae235b
{
Packit ae235b
  GMarkupParseContext *context;
Packit ae235b
  char *filename, *data;
Packit ae235b
  gsize len;
Packit ae235b
  gboolean res;
Packit ae235b
  MimeParser parse_data = {0};
Packit ae235b
  GMarkupParser parser = {
Packit ae235b
    mime_info_start_element,
Packit ae235b
    mime_info_end_element,
Packit ae235b
    mime_info_text
Packit ae235b
  };
Packit ae235b
Packit ae235b
  filename = g_build_filename (dir, "mime", basename, NULL);
Packit ae235b
Packit ae235b
  res = g_file_get_contents (filename,  &data,  &len,  NULL);
Packit ae235b
  g_free (filename);
Packit ae235b
  if (!res)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  context = g_markup_parse_context_new   (&parser, 0, &parse_data, NULL);
Packit ae235b
  res = g_markup_parse_context_parse (context, data, len, NULL);
Packit ae235b
  g_free (data);
Packit ae235b
  g_markup_parse_context_free (context);
Packit ae235b
Packit ae235b
  if (!res)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return parse_data.comment;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static char *
Packit ae235b
load_comment_for_mime (const char *mimetype)
Packit ae235b
{
Packit ae235b
  const char * const* dirs;
Packit ae235b
  char *basename;
Packit ae235b
  char *comment;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  basename = g_strdup_printf ("%s.xml", mimetype);
Packit ae235b
Packit ae235b
  comment = load_comment_for_mime_helper (g_get_user_data_dir (), basename);
Packit ae235b
  if (comment)
Packit ae235b
    {
Packit ae235b
      g_free (basename);
Packit ae235b
      return comment;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  dirs = g_get_system_data_dirs ();
Packit ae235b
Packit ae235b
  for (i = 0; dirs[i] != NULL; i++)
Packit ae235b
    {
Packit ae235b
      comment = load_comment_for_mime_helper (dirs[i], basename);
Packit ae235b
      if (comment)
Packit ae235b
        {
Packit ae235b
          g_free (basename);
Packit ae235b
          return comment;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  g_free (basename);
Packit ae235b
Packit ae235b
  return g_strdup_printf (_("%s type"), mimetype);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_get_description:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Gets the human readable description of the content type.
Packit ae235b
 *
Packit ae235b
 * Returns: a short description of the content type @type. Free the
Packit ae235b
 *     returned string with g_free()
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_content_type_get_description (const gchar *type)
Packit ae235b
{
Packit ae235b
  static GHashTable *type_comment_cache = NULL;
Packit ae235b
  gchar *comment;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, NULL);
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  type = xdg_mime_unalias_mime_type (type);
Packit ae235b
Packit ae235b
  if (type_comment_cache == NULL)
Packit ae235b
    type_comment_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
Packit ae235b
Packit ae235b
  comment = g_hash_table_lookup (type_comment_cache, type);
Packit ae235b
  comment = g_strdup (comment);
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  if (comment != NULL)
Packit ae235b
    return comment;
Packit ae235b
Packit ae235b
  comment = load_comment_for_mime (type);
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  g_hash_table_insert (type_comment_cache,
Packit ae235b
                       g_strdup (type),
Packit ae235b
                       g_strdup (comment));
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return comment;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_get_mime_type:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Gets the mime type for the content type, if one is registered.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): the registered mime type for the given @type,
Packit ae235b
 *     or %NULL if unknown.
Packit ae235b
 */
Packit ae235b
char *
Packit ae235b
g_content_type_get_mime_type (const char *type)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (type != NULL, NULL);
Packit ae235b
Packit ae235b
  return g_strdup (type);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_content_type_get_icon_internal (const gchar *type,
Packit ae235b
                                  gboolean     symbolic)
Packit ae235b
{
Packit ae235b
  char *mimetype_icon;
Packit ae235b
  char *generic_mimetype_icon = NULL;
Packit ae235b
  char *q;
Packit ae235b
  char *icon_names[6];
Packit ae235b
  int n = 0;
Packit ae235b
  GIcon *themed_icon;
Packit ae235b
  const char  *xdg_icon;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, NULL);
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  xdg_icon = xdg_mime_get_icon (type);
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  if (xdg_icon)
Packit ae235b
    icon_names[n++] = g_strdup (xdg_icon);
Packit ae235b
Packit ae235b
  mimetype_icon = g_strdup (type);
Packit ae235b
  while ((q = strchr (mimetype_icon, '/')) != NULL)
Packit ae235b
    *q = '-';
Packit ae235b
Packit ae235b
  icon_names[n++] = mimetype_icon;
Packit ae235b
Packit ae235b
  generic_mimetype_icon = g_content_type_get_generic_icon_name (type);
Packit ae235b
  if (generic_mimetype_icon)
Packit ae235b
    icon_names[n++] = generic_mimetype_icon;
Packit ae235b
Packit ae235b
  if (symbolic)
Packit ae235b
    {
Packit ae235b
      for (i = 0; i < n; i++)
Packit ae235b
        {
Packit ae235b
          icon_names[n + i] = icon_names[i];
Packit ae235b
          icon_names[i] = g_strconcat (icon_names[i], "-symbolic", NULL);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      n += n;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  themed_icon = g_themed_icon_new_from_names (icon_names, n);
Packit ae235b
Packit ae235b
  for (i = 0; i < n; i++)
Packit ae235b
    g_free (icon_names[i]);
Packit ae235b
Packit ae235b
  return themed_icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_get_icon:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Gets the icon for a content type.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): #GIcon corresponding to the content type. Free the returned
Packit ae235b
 *     object with g_object_unref()
Packit ae235b
 */
Packit ae235b
GIcon *
Packit ae235b
g_content_type_get_icon (const gchar *type)
Packit ae235b
{
Packit ae235b
  return g_content_type_get_icon_internal (type, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_get_symbolic_icon:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Gets the symbolic icon for a content type.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): symbolic #GIcon corresponding to the content type.
Packit ae235b
 *     Free the returned object with g_object_unref()
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
GIcon *
Packit ae235b
g_content_type_get_symbolic_icon (const gchar *type)
Packit ae235b
{
Packit ae235b
  return g_content_type_get_icon_internal (type, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_get_generic_icon_name:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Gets the generic icon name for a content type.
Packit ae235b
 *
Packit ae235b
 * See the
Packit ae235b
 * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
Packit ae235b
 * specification for more on the generic icon name.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): the registered generic icon name for the given @type,
Packit ae235b
 *     or %NULL if unknown. Free with g_free()
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_content_type_get_generic_icon_name (const gchar *type)
Packit ae235b
{
Packit ae235b
  const gchar *xdg_icon_name;
Packit ae235b
  gchar *icon_name;
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  xdg_icon_name = xdg_mime_get_generic_icon (type);
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  if (!xdg_icon_name)
Packit ae235b
    {
Packit ae235b
      const char *p;
Packit ae235b
      const char *suffix = "-x-generic";
Packit ae235b
Packit ae235b
      p = strchr (type, '/');
Packit ae235b
      if (p == NULL)
Packit ae235b
        p = type + strlen (type);
Packit ae235b
Packit ae235b
      icon_name = g_malloc (p - type + strlen (suffix) + 1);
Packit ae235b
      memcpy (icon_name, type, p - type);
Packit ae235b
      memcpy (icon_name + (p - type), suffix, strlen (suffix));
Packit ae235b
      icon_name[(p - type) + strlen (suffix)] = 0;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      icon_name = g_strdup (xdg_icon_name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return icon_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_can_be_executable:
Packit ae235b
 * @type: a content type string
Packit ae235b
 *
Packit ae235b
 * Checks if a content type can be executable. Note that for instance
Packit ae235b
 * things like text files can be executables (i.e. scripts and batch files).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the file type corresponds to a type that
Packit ae235b
 *     can be executable, %FALSE otherwise.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_content_type_can_be_executable (const gchar *type)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (type != NULL, FALSE);
Packit ae235b
Packit ae235b
  if (g_content_type_is_a (type, "application/x-executable")  ||
Packit ae235b
      g_content_type_is_a (type, "text/plain"))
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
looks_like_text (const guchar *data, gsize data_size)
Packit ae235b
{
Packit ae235b
  gsize i;
Packit ae235b
  char c;
Packit ae235b
Packit ae235b
  for (i = 0; i < data_size; i++)
Packit ae235b
    {
Packit ae235b
      c = data[i];
Packit ae235b
Packit ae235b
      if (g_ascii_iscntrl (c) &&
Packit ae235b
          !g_ascii_isspace (c) &&
Packit ae235b
          c != '\b')
Packit ae235b
        return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_from_mime_type:
Packit ae235b
 * @mime_type: a mime type string
Packit ae235b
 *
Packit ae235b
 * Tries to find a content type based on the mime type name.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable): Newly allocated string with content type or
Packit ae235b
 *     %NULL. Free with g_free()
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_content_type_from_mime_type (const gchar *mime_type)
Packit ae235b
{
Packit ae235b
  char *umime;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (mime_type != NULL, NULL);
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
  /* mime type and content type are same on unixes */
Packit ae235b
  umime = g_strdup (xdg_mime_unalias_mime_type (mime_type));
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return umime;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_guess:
Packit ae235b
 * @filename: (nullable): a string, or %NULL
Packit ae235b
 * @data: (nullable) (array length=data_size): a stream of data, or %NULL
Packit ae235b
 * @data_size: the size of @data
Packit ae235b
 * @result_uncertain: (out) (optional): return location for the certainty
Packit ae235b
 *     of the result, or %NULL
Packit ae235b
 *
Packit ae235b
 * Guesses the content type based on example data. If the function is
Packit ae235b
 * uncertain, @result_uncertain will be set to %TRUE. Either @filename
Packit ae235b
 * or @data may be %NULL, in which case the guess will be based solely
Packit ae235b
 * on the other argument.
Packit ae235b
 *
Packit ae235b
 * Returns: a string indicating a guessed content type for the
Packit ae235b
 *     given data. Free with g_free()
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_content_type_guess (const gchar  *filename,
Packit ae235b
                      const guchar *data,
Packit ae235b
                      gsize         data_size,
Packit ae235b
                      gboolean     *result_uncertain)
Packit ae235b
{
Packit ae235b
  char *basename;
Packit ae235b
  const char *name_mimetypes[10], *sniffed_mimetype;
Packit ae235b
  char *mimetype;
Packit ae235b
  int i;
Packit ae235b
  int n_name_mimetypes;
Packit ae235b
  int sniffed_prio;
Packit ae235b
Packit ae235b
  sniffed_prio = 0;
Packit ae235b
  n_name_mimetypes = 0;
Packit ae235b
  sniffed_mimetype = XDG_MIME_TYPE_UNKNOWN;
Packit ae235b
Packit ae235b
  if (result_uncertain)
Packit ae235b
    *result_uncertain = FALSE;
Packit ae235b
Packit ae235b
  /* our test suite and potentially other code used -1 in the past, which is
Packit ae235b
   * not documented and not allowed; guard against that */
Packit ae235b
  g_return_val_if_fail (data_size != (gsize) -1, g_strdup (XDG_MIME_TYPE_UNKNOWN));
Packit ae235b
Packit ae235b
  G_LOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  if (filename)
Packit ae235b
    {
Packit ae235b
      i = strlen (filename);
Packit ae235b
      if (filename[i - 1] == '/')
Packit ae235b
        {
Packit ae235b
          name_mimetypes[0] = "inode/directory";
Packit ae235b
          name_mimetypes[1] = NULL;
Packit ae235b
          n_name_mimetypes = 1;
Packit ae235b
          if (result_uncertain)
Packit ae235b
            *result_uncertain = TRUE;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          basename = g_path_get_basename (filename);
Packit ae235b
          n_name_mimetypes = xdg_mime_get_mime_types_from_file_name (basename, name_mimetypes, 10);
Packit ae235b
          g_free (basename);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Got an extension match, and no conflicts. This is it. */
Packit ae235b
  if (n_name_mimetypes == 1)
Packit ae235b
    {
Packit ae235b
      gchar *s = g_strdup (name_mimetypes[0]);
Packit ae235b
      G_UNLOCK (gio_xdgmime);
Packit ae235b
      return s;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (data)
Packit ae235b
    {
Packit ae235b
      sniffed_mimetype = xdg_mime_get_mime_type_for_data (data, data_size, &sniffed_prio);
Packit ae235b
      if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
Packit ae235b
          data &&
Packit ae235b
          looks_like_text (data, data_size))
Packit ae235b
        sniffed_mimetype = "text/plain";
Packit ae235b
Packit ae235b
      /* For security reasons we don't ever want to sniff desktop files
Packit ae235b
       * where we know the filename and it doesn't have a .desktop extension.
Packit ae235b
       * This is because desktop files allow executing any application and
Packit ae235b
       * we don't want to make it possible to hide them looking like something
Packit ae235b
       * else.
Packit ae235b
       */
Packit ae235b
      if (filename != NULL &&
Packit ae235b
          strcmp (sniffed_mimetype, "application/x-desktop") == 0)
Packit ae235b
        sniffed_mimetype = "text/plain";
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (n_name_mimetypes == 0)
Packit ae235b
    {
Packit ae235b
      if (sniffed_mimetype == XDG_MIME_TYPE_UNKNOWN &&
Packit ae235b
          result_uncertain)
Packit ae235b
        *result_uncertain = TRUE;
Packit ae235b
Packit ae235b
      mimetype = g_strdup (sniffed_mimetype);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      mimetype = NULL;
Packit ae235b
      if (sniffed_mimetype != XDG_MIME_TYPE_UNKNOWN)
Packit ae235b
        {
Packit ae235b
          if (sniffed_prio >= 80) /* High priority sniffing match, use that */
Packit ae235b
            mimetype = g_strdup (sniffed_mimetype);
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              /* There are conflicts between the name matches and we
Packit ae235b
               * have a sniffed type, use that as a tie breaker.
Packit ae235b
               */
Packit ae235b
              for (i = 0; i < n_name_mimetypes; i++)
Packit ae235b
                {
Packit ae235b
                  if ( xdg_mime_mime_type_subclass (name_mimetypes[i], sniffed_mimetype))
Packit ae235b
                    {
Packit ae235b
                      /* This nametype match is derived from (or the same as)
Packit ae235b
                       * the sniffed type). This is probably it.
Packit ae235b
                       */
Packit ae235b
                      mimetype = g_strdup (name_mimetypes[i]);
Packit ae235b
                      break;
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (mimetype == NULL)
Packit ae235b
        {
Packit ae235b
          /* Conflicts, and sniffed type was no help or not there.
Packit ae235b
           * Guess on the first one
Packit ae235b
           */
Packit ae235b
          mimetype = g_strdup (name_mimetypes[0]);
Packit ae235b
          if (result_uncertain)
Packit ae235b
            *result_uncertain = TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_UNLOCK (gio_xdgmime);
Packit ae235b
Packit ae235b
  return mimetype;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enumerate_mimetypes_subdir (const char *dir,
Packit ae235b
                            const char *prefix,
Packit ae235b
                            GHashTable *mimetypes)
Packit ae235b
{
Packit ae235b
  DIR *d;
Packit ae235b
  struct dirent *ent;
Packit ae235b
  char *mimetype;
Packit ae235b
Packit ae235b
  d = opendir (dir);
Packit ae235b
  if (d)
Packit ae235b
    {
Packit ae235b
      while ((ent = readdir (d)) != NULL)
Packit ae235b
        {
Packit ae235b
          if (g_str_has_suffix (ent->d_name, ".xml"))
Packit ae235b
            {
Packit ae235b
              mimetype = g_strdup_printf ("%s/%.*s", prefix, (int) strlen (ent->d_name) - 4, ent->d_name);
Packit ae235b
              g_hash_table_replace (mimetypes, mimetype, NULL);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      closedir (d);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enumerate_mimetypes_dir (const char *dir,
Packit ae235b
                         GHashTable *mimetypes)
Packit ae235b
{
Packit ae235b
  DIR *d;
Packit ae235b
  struct dirent *ent;
Packit ae235b
  char *mimedir;
Packit ae235b
  char *name;
Packit ae235b
Packit ae235b
  mimedir = g_build_filename (dir, "mime", NULL);
Packit ae235b
Packit ae235b
  d = opendir (mimedir);
Packit ae235b
  if (d)
Packit ae235b
    {
Packit ae235b
      while ((ent = readdir (d)) != NULL)
Packit ae235b
        {
Packit ae235b
          if (strcmp (ent->d_name, "packages") != 0)
Packit ae235b
            {
Packit ae235b
              name = g_build_filename (mimedir, ent->d_name, NULL);
Packit ae235b
              if (g_file_test (name, G_FILE_TEST_IS_DIR))
Packit ae235b
                enumerate_mimetypes_subdir (name, ent->d_name, mimetypes);
Packit ae235b
              g_free (name);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      closedir (d);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (mimedir);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_types_get_registered:
Packit ae235b
 *
Packit ae235b
 * Gets a list of strings containing all the registered content types
Packit ae235b
 * known to the system. The list and its data should be freed using
Packit ae235b
 * g_list_free_full (list, g_free).
Packit ae235b
 *
Packit ae235b
 * Returns: (element-type utf8) (transfer full): list of the registered
Packit ae235b
 *     content types
Packit ae235b
 */
Packit ae235b
GList *
Packit ae235b
g_content_types_get_registered (void)
Packit ae235b
{
Packit ae235b
  const char * const* dirs;
Packit ae235b
  GHashTable *mimetypes;
Packit ae235b
  GHashTableIter iter;
Packit ae235b
  gpointer key;
Packit ae235b
  int i;
Packit ae235b
  GList *l;
Packit ae235b
Packit ae235b
  mimetypes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
Packit ae235b
Packit ae235b
  enumerate_mimetypes_dir (g_get_user_data_dir (), mimetypes);
Packit ae235b
  dirs = g_get_system_data_dirs ();
Packit ae235b
Packit ae235b
  for (i = 0; dirs[i] != NULL; i++)
Packit ae235b
    enumerate_mimetypes_dir (dirs[i], mimetypes);
Packit ae235b
Packit ae235b
  l = NULL;
Packit ae235b
  g_hash_table_iter_init (&iter, mimetypes);
Packit ae235b
  while (g_hash_table_iter_next (&iter, &key, NULL))
Packit ae235b
    {
Packit ae235b
      l = g_list_prepend (l, key);
Packit ae235b
      g_hash_table_iter_steal (&iter);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_hash_table_destroy (mimetypes);
Packit ae235b
Packit ae235b
  return l;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* tree magic data */
Packit ae235b
static GList *tree_matches = NULL;
Packit ae235b
static gboolean need_reload = FALSE;
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (gio_treemagic);
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gchar *path;
Packit ae235b
  GFileType type;
Packit ae235b
  guint match_case : 1;
Packit ae235b
  guint executable : 1;
Packit ae235b
  guint non_empty  : 1;
Packit ae235b
  guint on_disc    : 1;
Packit ae235b
  gchar *mimetype;
Packit ae235b
  GList *matches;
Packit ae235b
} TreeMatchlet;
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gchar *contenttype;
Packit ae235b
  gint priority;
Packit ae235b
  GList *matches;
Packit ae235b
} TreeMatch;
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
tree_matchlet_free (TreeMatchlet *matchlet)
Packit ae235b
{
Packit ae235b
  g_list_free_full (matchlet->matches, (GDestroyNotify) tree_matchlet_free);
Packit ae235b
  g_free (matchlet->path);
Packit ae235b
  g_free (matchlet->mimetype);
Packit ae235b
  g_slice_free (TreeMatchlet, matchlet);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
tree_match_free (TreeMatch *match)
Packit ae235b
{
Packit ae235b
  g_list_free_full (match->matches, (GDestroyNotify) tree_matchlet_free);
Packit ae235b
  g_free (match->contenttype);
Packit ae235b
  g_slice_free (TreeMatch, match);
Packit ae235b
}
Packit ae235b
Packit ae235b
static TreeMatch *
Packit ae235b
parse_header (gchar *line)
Packit ae235b
{
Packit ae235b
  gint len;
Packit ae235b
  gchar *s;
Packit ae235b
  TreeMatch *match;
Packit ae235b
Packit ae235b
  len = strlen (line);
Packit ae235b
Packit ae235b
  if (line[0] != '[' || line[len - 1] != ']')
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  line[len - 1] = 0;
Packit ae235b
  s = strchr (line, ':');
Packit ae235b
Packit ae235b
  match = g_slice_new0 (TreeMatch);
Packit ae235b
  match->priority = atoi (line + 1);
Packit ae235b
  match->contenttype = g_strdup (s + 1);
Packit ae235b
Packit ae235b
  return match;
Packit ae235b
}
Packit ae235b
Packit ae235b
static TreeMatchlet *
Packit ae235b
parse_match_line (gchar *line,
Packit ae235b
                  gint  *depth)
Packit ae235b
{
Packit ae235b
  gchar *s, *p;
Packit ae235b
  TreeMatchlet *matchlet;
Packit ae235b
  gchar **parts;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  matchlet = g_slice_new0 (TreeMatchlet);
Packit ae235b
Packit ae235b
  if (line[0] == '>')
Packit ae235b
    {
Packit ae235b
      *depth = 0;
Packit ae235b
      s = line;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      *depth = atoi (line);
Packit ae235b
      s = strchr (line, '>');
Packit ae235b
    }
Packit ae235b
  s += 2;
Packit ae235b
  p = strchr (s, '"');
Packit ae235b
  *p = 0;
Packit ae235b
Packit ae235b
  matchlet->path = g_strdup (s);
Packit ae235b
  s = p + 1;
Packit ae235b
  parts = g_strsplit (s, ",", 0);
Packit ae235b
  if (strcmp (parts[0], "=file") == 0)
Packit ae235b
    matchlet->type = G_FILE_TYPE_REGULAR;
Packit ae235b
  else if (strcmp (parts[0], "=directory") == 0)
Packit ae235b
    matchlet->type = G_FILE_TYPE_DIRECTORY;
Packit ae235b
  else if (strcmp (parts[0], "=link") == 0)
Packit ae235b
    matchlet->type = G_FILE_TYPE_SYMBOLIC_LINK;
Packit ae235b
  else
Packit ae235b
    matchlet->type = G_FILE_TYPE_UNKNOWN;
Packit ae235b
  for (i = 1; parts[i]; i++)
Packit ae235b
    {
Packit ae235b
      if (strcmp (parts[i], "executable") == 0)
Packit ae235b
        matchlet->executable = 1;
Packit ae235b
      else if (strcmp (parts[i], "match-case") == 0)
Packit ae235b
        matchlet->match_case = 1;
Packit ae235b
      else if (strcmp (parts[i], "non-empty") == 0)
Packit ae235b
        matchlet->non_empty = 1;
Packit ae235b
      else if (strcmp (parts[i], "on-disc") == 0)
Packit ae235b
        matchlet->on_disc = 1;
Packit ae235b
      else
Packit ae235b
        matchlet->mimetype = g_strdup (parts[i]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_strfreev (parts);
Packit ae235b
Packit ae235b
  return matchlet;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
cmp_match (gconstpointer a, gconstpointer b)
Packit ae235b
{
Packit ae235b
  const TreeMatch *aa = (const TreeMatch *)a;
Packit ae235b
  const TreeMatch *bb = (const TreeMatch *)b;
Packit ae235b
Packit ae235b
  return bb->priority - aa->priority;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
insert_match (TreeMatch *match)
Packit ae235b
{
Packit ae235b
  tree_matches = g_list_insert_sorted (tree_matches, match, cmp_match);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
insert_matchlet (TreeMatch    *match,
Packit ae235b
                 TreeMatchlet *matchlet,
Packit ae235b
                 gint          depth)
Packit ae235b
{
Packit ae235b
  if (depth == 0)
Packit ae235b
    match->matches = g_list_append (match->matches, matchlet);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GList *last;
Packit ae235b
      TreeMatchlet *m;
Packit ae235b
Packit ae235b
      last = g_list_last (match->matches);
Packit ae235b
      if (!last)
Packit ae235b
        {
Packit ae235b
          tree_matchlet_free (matchlet);
Packit ae235b
          g_warning ("can't insert tree matchlet at depth %d", depth);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      m = (TreeMatchlet *) last->data;
Packit ae235b
      while (--depth > 0)
Packit ae235b
        {
Packit ae235b
          last = g_list_last (m->matches);
Packit ae235b
          if (!last)
Packit ae235b
            {
Packit ae235b
              tree_matchlet_free (matchlet);
Packit ae235b
              g_warning ("can't insert tree matchlet at depth %d", depth);
Packit ae235b
              return;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          m = (TreeMatchlet *) last->data;
Packit ae235b
        }
Packit ae235b
      m->matches = g_list_append (m->matches, matchlet);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
read_tree_magic_from_directory (const gchar *prefix)
Packit ae235b
{
Packit ae235b
  gchar *filename;
Packit ae235b
  gchar *text;
Packit ae235b
  gsize len;
Packit ae235b
  gchar **lines;
Packit ae235b
  gint i;
Packit ae235b
  TreeMatch *match;
Packit ae235b
  TreeMatchlet *matchlet;
Packit ae235b
  gint depth;
Packit ae235b
Packit ae235b
  filename = g_build_filename (prefix, "mime", "treemagic", NULL);
Packit ae235b
Packit ae235b
  if (g_file_get_contents (filename, &text, &len, NULL))
Packit ae235b
    {
Packit ae235b
      if (strcmp (text, "MIME-TreeMagic") == 0)
Packit ae235b
        {
Packit ae235b
          lines = g_strsplit (text + strlen ("MIME-TreeMagic") + 2, "\n", 0);
Packit ae235b
          match = NULL;
Packit ae235b
          for (i = 0; lines[i] && lines[i][0]; i++)
Packit ae235b
            {
Packit ae235b
              if (lines[i][0] == '[')
Packit ae235b
                {
Packit ae235b
                  match = parse_header (lines[i]);
Packit ae235b
                  insert_match (match);
Packit ae235b
                }
Packit ae235b
              else if (match != NULL)
Packit ae235b
                {
Packit ae235b
                  matchlet = parse_match_line (lines[i], &depth);
Packit ae235b
                  insert_matchlet (match, matchlet, depth);
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  g_warning ("%s: header corrupt; skipping\n", filename);
Packit ae235b
                  break;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_strfreev (lines);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_warning ("%s: header not found, skipping\n", filename);
Packit ae235b
Packit ae235b
      g_free (text);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
xdg_mime_reload (void *user_data)
Packit ae235b
{
Packit ae235b
  need_reload = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
tree_magic_shutdown (void)
Packit ae235b
{
Packit ae235b
  g_list_free_full (tree_matches, (GDestroyNotify) tree_match_free);
Packit ae235b
  tree_matches = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
tree_magic_init (void)
Packit ae235b
{
Packit ae235b
  static gboolean initialized = FALSE;
Packit ae235b
  const gchar *dir;
Packit ae235b
  const gchar * const * dirs;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  if (!initialized)
Packit ae235b
    {
Packit ae235b
      initialized = TRUE;
Packit ae235b
Packit ae235b
      xdg_mime_register_reload_callback (xdg_mime_reload, NULL, NULL);
Packit ae235b
      need_reload = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (need_reload)
Packit ae235b
    {
Packit ae235b
      need_reload = FALSE;
Packit ae235b
Packit ae235b
      tree_magic_shutdown ();
Packit ae235b
Packit ae235b
      dir = g_get_user_data_dir ();
Packit ae235b
      read_tree_magic_from_directory (dir);
Packit ae235b
      dirs = g_get_system_data_dirs ();
Packit ae235b
      for (i = 0; dirs[i]; i++)
Packit ae235b
        read_tree_magic_from_directory (dirs[i]);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* a filtering enumerator */
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  gchar *path;
Packit ae235b
  gint depth;
Packit ae235b
  gboolean ignore_case;
Packit ae235b
  gchar **components;
Packit ae235b
  gchar **case_components;
Packit ae235b
  GFileEnumerator **enumerators;
Packit ae235b
  GFile **children;
Packit ae235b
} Enumerator;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
component_match (Enumerator  *e,
Packit ae235b
                 gint         depth,
Packit ae235b
                 const gchar *name)
Packit ae235b
{
Packit ae235b
  gchar *case_folded, *key;
Packit ae235b
  gboolean found;
Packit ae235b
Packit ae235b
  if (strcmp (name, e->components[depth]) == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (!e->ignore_case)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  case_folded = g_utf8_casefold (name, -1);
Packit ae235b
  key = g_utf8_collate_key (case_folded, -1);
Packit ae235b
Packit ae235b
  found = strcmp (key, e->case_components[depth]) == 0;
Packit ae235b
Packit ae235b
  g_free (case_folded);
Packit ae235b
  g_free (key);
Packit ae235b
Packit ae235b
  return found;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
next_match_recurse (Enumerator *e,
Packit ae235b
                    gint        depth)
Packit ae235b
{
Packit ae235b
  GFile *file;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  const gchar *name;
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      if (e->enumerators[depth] == NULL)
Packit ae235b
        {
Packit ae235b
          if (depth > 0)
Packit ae235b
            {
Packit ae235b
              file = next_match_recurse (e, depth - 1);
Packit ae235b
              if (file)
Packit ae235b
                {
Packit ae235b
                  e->children[depth] = file;
Packit ae235b
                  e->enumerators[depth] = g_file_enumerate_children (file,
Packit ae235b
                                                                     G_FILE_ATTRIBUTE_STANDARD_NAME,
Packit ae235b
                                                                     G_FILE_QUERY_INFO_NONE,
Packit ae235b
                                                                     NULL,
Packit ae235b
                                                                     NULL);
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          if (e->enumerators[depth] == NULL)
Packit ae235b
            return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      while ((info = g_file_enumerator_next_file (e->enumerators[depth], NULL, NULL)))
Packit ae235b
        {
Packit ae235b
          name = g_file_info_get_name (info);
Packit ae235b
          if (component_match (e, depth, name))
Packit ae235b
            {
Packit ae235b
              file = g_file_get_child (e->children[depth], name);
Packit ae235b
              g_object_unref (info);
Packit ae235b
              return file;
Packit ae235b
            }
Packit ae235b
          g_object_unref (info);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_object_unref (e->enumerators[depth]);
Packit ae235b
      e->enumerators[depth] = NULL;
Packit ae235b
      g_object_unref (e->children[depth]);
Packit ae235b
      e->children[depth] = NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
enumerator_next (Enumerator *e)
Packit ae235b
{
Packit ae235b
  return next_match_recurse (e, e->depth - 1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static Enumerator *
Packit ae235b
enumerator_new (GFile      *root,
Packit ae235b
                const char *path,
Packit ae235b
                gboolean    ignore_case)
Packit ae235b
{
Packit ae235b
  Enumerator *e;
Packit ae235b
  gint i;
Packit ae235b
  gchar *case_folded;
Packit ae235b
Packit ae235b
  e = g_new0 (Enumerator, 1);
Packit ae235b
  e->path = g_strdup (path);
Packit ae235b
  e->ignore_case = ignore_case;
Packit ae235b
Packit ae235b
  e->components = g_strsplit (e->path, G_DIR_SEPARATOR_S, -1);
Packit ae235b
  e->depth = g_strv_length (e->components);
Packit ae235b
  if (e->ignore_case)
Packit ae235b
    {
Packit ae235b
      e->case_components = g_new0 (char *, e->depth + 1);
Packit ae235b
      for (i = 0; e->components[i]; i++)
Packit ae235b
        {
Packit ae235b
          case_folded = g_utf8_casefold (e->components[i], -1);
Packit ae235b
          e->case_components[i] = g_utf8_collate_key (case_folded, -1);
Packit ae235b
          g_free (case_folded);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  e->children = g_new0 (GFile *, e->depth);
Packit ae235b
  e->children[0] = g_object_ref (root);
Packit ae235b
  e->enumerators = g_new0 (GFileEnumerator *, e->depth);
Packit ae235b
  e->enumerators[0] = g_file_enumerate_children (root,
Packit ae235b
                                                 G_FILE_ATTRIBUTE_STANDARD_NAME,
Packit ae235b
                                                 G_FILE_QUERY_INFO_NONE,
Packit ae235b
                                                 NULL,
Packit ae235b
                                                 NULL);
Packit ae235b
Packit ae235b
  return e;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enumerator_free (Enumerator *e)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < e->depth; i++)
Packit ae235b
    {
Packit ae235b
      if (e->enumerators[i])
Packit ae235b
        g_object_unref (e->enumerators[i]);
Packit ae235b
      if (e->children[i])
Packit ae235b
        g_object_unref (e->children[i]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (e->enumerators);
Packit ae235b
  g_free (e->children);
Packit ae235b
  g_strfreev (e->components);
Packit ae235b
  if (e->case_components)
Packit ae235b
    g_strfreev (e->case_components);
Packit ae235b
  g_free (e->path);
Packit ae235b
  g_free (e);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
matchlet_match (TreeMatchlet *matchlet,
Packit ae235b
                GFile        *root)
Packit ae235b
{
Packit ae235b
  GFile *file;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  gboolean result;
Packit ae235b
  const gchar *attrs;
Packit ae235b
  Enumerator *e;
Packit ae235b
  GList *l;
Packit ae235b
Packit ae235b
  e = enumerator_new (root, matchlet->path, !matchlet->match_case);
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      file = enumerator_next (e);
Packit ae235b
      if (!file)
Packit ae235b
        {
Packit ae235b
          enumerator_free (e);
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (matchlet->mimetype)
Packit ae235b
        attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
Packit ae235b
                G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ","
Packit ae235b
                G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
Packit ae235b
      else
Packit ae235b
        attrs = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
Packit ae235b
                G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE;
Packit ae235b
      info = g_file_query_info (file,
Packit ae235b
                                attrs,
Packit ae235b
                                G_FILE_QUERY_INFO_NONE,
Packit ae235b
                                NULL,
Packit ae235b
                                NULL);
Packit ae235b
      if (info)
Packit ae235b
        {
Packit ae235b
          result = TRUE;
Packit ae235b
Packit ae235b
          if (matchlet->type != G_FILE_TYPE_UNKNOWN &&
Packit ae235b
              g_file_info_get_file_type (info) != matchlet->type)
Packit ae235b
            result = FALSE;
Packit ae235b
Packit ae235b
          if (matchlet->executable &&
Packit ae235b
              !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
Packit ae235b
            result = FALSE;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        result = FALSE;
Packit ae235b
Packit ae235b
      if (result && matchlet->non_empty)
Packit ae235b
        {
Packit ae235b
          GFileEnumerator *child_enum;
Packit ae235b
          GFileInfo *child_info;
Packit ae235b
Packit ae235b
          child_enum = g_file_enumerate_children (file,
Packit ae235b
                                                  G_FILE_ATTRIBUTE_STANDARD_NAME,
Packit ae235b
                                                  G_FILE_QUERY_INFO_NONE,
Packit ae235b
                                                  NULL,
Packit ae235b
                                                  NULL);
Packit ae235b
Packit ae235b
          if (child_enum)
Packit ae235b
            {
Packit ae235b
              child_info = g_file_enumerator_next_file (child_enum, NULL, NULL);
Packit ae235b
              if (child_info)
Packit ae235b
                g_object_unref (child_info);
Packit ae235b
              else
Packit ae235b
                result = FALSE;
Packit ae235b
              g_object_unref (child_enum);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            result = FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (result && matchlet->mimetype)
Packit ae235b
        {
Packit ae235b
          if (strcmp (matchlet->mimetype, g_file_info_get_content_type (info)) != 0)
Packit ae235b
            result = FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (info)
Packit ae235b
        g_object_unref (info);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
  while (!result);
Packit ae235b
Packit ae235b
  enumerator_free (e);
Packit ae235b
Packit ae235b
  if (!matchlet->matches)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  for (l = matchlet->matches; l; l = l->next)
Packit ae235b
    {
Packit ae235b
      TreeMatchlet *submatchlet;
Packit ae235b
Packit ae235b
      submatchlet = l->data;
Packit ae235b
      if (matchlet_match (submatchlet, root))
Packit ae235b
        return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
match_match (TreeMatch    *match,
Packit ae235b
             GFile        *root,
Packit ae235b
             GPtrArray    *types)
Packit ae235b
{
Packit ae235b
  GList *l;
Packit ae235b
Packit ae235b
  for (l = match->matches; l; l = l->next)
Packit ae235b
    {
Packit ae235b
      TreeMatchlet *matchlet = l->data;
Packit ae235b
      if (matchlet_match (matchlet, root))
Packit ae235b
        {
Packit ae235b
          g_ptr_array_add (types, g_strdup (match->contenttype));
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_content_type_guess_for_tree:
Packit ae235b
 * @root: the root of the tree to guess a type for
Packit ae235b
 *
Packit ae235b
 * Tries to guess the type of the tree with root @root, by
Packit ae235b
 * looking at the files it contains. The result is an array
Packit ae235b
 * of content types, with the best guess coming first.
Packit ae235b
 *
Packit ae235b
 * The types returned all have the form x-content/foo, e.g.
Packit ae235b
 * x-content/audio-cdda (for audio CDs) or x-content/image-dcf
Packit ae235b
 * (for a camera memory card). See the
Packit ae235b
 * [shared-mime-info](http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec)
Packit ae235b
 * specification for more on x-content types.
Packit ae235b
 *
Packit ae235b
 * This function is useful in the implementation of
Packit ae235b
 * g_mount_guess_content_type().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (array zero-terminated=1): an %NULL-terminated
Packit ae235b
 *     array of zero or more content types. Free with g_strfreev()
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_content_type_guess_for_tree (GFile *root)
Packit ae235b
{
Packit ae235b
  GPtrArray *types;
Packit ae235b
  GList *l;
Packit ae235b
Packit ae235b
  types = g_ptr_array_new ();
Packit ae235b
Packit ae235b
  G_LOCK (gio_treemagic);
Packit ae235b
Packit ae235b
  tree_magic_init ();
Packit ae235b
  for (l = tree_matches; l; l = l->next)
Packit ae235b
    {
Packit ae235b
      TreeMatch *match = l->data;
Packit ae235b
      match_match (match, root, types);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_UNLOCK (gio_treemagic);
Packit ae235b
Packit ae235b
  g_ptr_array_add (types, NULL);
Packit ae235b
Packit ae235b
  return (gchar **)g_ptr_array_free (types, FALSE);
Packit ae235b
}