Blame gio/gcontenttype-win32.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 "gcontenttype.h"
Packit ae235b
#include "gthemedicon.h"
Packit ae235b
#include "gicon.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include <windows.h>
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_registry_classes_key (const char    *subdir,
Packit ae235b
                          const wchar_t *key_name)
Packit ae235b
{
Packit ae235b
  wchar_t *wc_key;
Packit ae235b
  HKEY reg_key = NULL;
Packit ae235b
  DWORD key_type;
Packit ae235b
  DWORD nbytes;
Packit ae235b
  char *value_utf8;
Packit ae235b
Packit ae235b
  value_utf8 = NULL;
Packit ae235b
  
Packit ae235b
  nbytes = 0;
Packit ae235b
  wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
Packit ae235b
  if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
Packit ae235b
                     KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS &&
Packit ae235b
      RegQueryValueExW (reg_key, key_name, 0,
Packit ae235b
                        &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
Packit ae235b
      (key_type == REG_SZ || key_type == REG_EXPAND_SZ))
Packit ae235b
    {
Packit ae235b
      wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
Packit ae235b
      RegQueryValueExW (reg_key, key_name, 0,
Packit ae235b
                        &key_type, (LPBYTE) wc_temp, &nbytes);
Packit ae235b
      wc_temp[nbytes/2] = '\0';
Packit ae235b
      if (key_type == REG_EXPAND_SZ)
Packit ae235b
        {
Packit ae235b
          wchar_t dummy[1];
Packit ae235b
          int len = ExpandEnvironmentStringsW (wc_temp, dummy, 1);
Packit ae235b
          if (len > 0)
Packit ae235b
            {
Packit ae235b
              wchar_t *wc_temp_expanded = g_new (wchar_t, len);
Packit ae235b
              if (ExpandEnvironmentStringsW (wc_temp, wc_temp_expanded, len) == len)
Packit ae235b
                value_utf8 = g_utf16_to_utf8 (wc_temp_expanded, -1, NULL, NULL, NULL);
Packit ae235b
              g_free (wc_temp_expanded);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
Packit ae235b
        }
Packit ae235b
      g_free (wc_temp);
Packit ae235b
      
Packit ae235b
    }
Packit ae235b
  g_free (wc_key);
Packit ae235b
  
Packit ae235b
  if (reg_key != NULL)
Packit ae235b
    RegCloseKey (reg_key);
Packit ae235b
Packit ae235b
  return value_utf8;
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_content_type_equals (const gchar *type1,
Packit ae235b
                       const gchar *type2)
Packit ae235b
{
Packit ae235b
  char *progid1, *progid2;
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
  if (g_ascii_strcasecmp (type1, type2) == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  res = FALSE;
Packit ae235b
  progid1 = get_registry_classes_key (type1, NULL);
Packit ae235b
  progid2 = get_registry_classes_key (type2, NULL);
Packit ae235b
  if (progid1 != NULL && progid2 != NULL &&
Packit ae235b
      strcmp (progid1, progid2) == 0)
Packit ae235b
    res = TRUE;
Packit ae235b
  g_free (progid1);
Packit ae235b
  g_free (progid2);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
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
  char *value_utf8;
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
  if (g_content_type_equals (type, supertype))
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  res = FALSE;
Packit ae235b
  value_utf8 = get_registry_classes_key (type, L"PerceivedType");
Packit ae235b
  if (value_utf8 && strcmp (value_utf8, supertype) == 0)
Packit ae235b
    res = TRUE;
Packit ae235b
  g_free (value_utf8);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
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
  gchar *content_type;
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (mime_type != NULL, FALSE);
Packit ae235b
Packit ae235b
  content_type = g_content_type_from_mime_type (mime_type);
Packit ae235b
  ret = g_content_type_is_a (type, content_type);
Packit ae235b
  g_free (content_type);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
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 ("*", type) == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_content_type_get_description (const gchar *type)
Packit ae235b
{
Packit ae235b
  char *progid;
Packit ae235b
  char *description;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, NULL);
Packit ae235b
Packit ae235b
  progid = get_registry_classes_key (type, NULL);
Packit ae235b
  if (progid)
Packit ae235b
    {
Packit ae235b
      description = get_registry_classes_key (progid, NULL);
Packit ae235b
      g_free (progid);
Packit ae235b
Packit ae235b
      if (description)
Packit ae235b
        return description;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_content_type_is_unknown (type))
Packit ae235b
    return g_strdup (_("Unknown type"));
Packit ae235b
Packit ae235b
  return g_strdup_printf (_("%s filetype"), type);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_content_type_get_mime_type (const gchar *type)
Packit ae235b
{
Packit ae235b
  char *mime;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, NULL);
Packit ae235b
Packit ae235b
  mime = get_registry_classes_key (type, L"Content Type");
Packit ae235b
  if (mime)
Packit ae235b
    return mime;
Packit ae235b
  else if (g_content_type_is_unknown (type))
Packit ae235b
    return g_strdup ("application/octet-stream");
Packit ae235b
  else if (*type == '.')
Packit ae235b
    return g_strdup_printf ("application/x-ext-%s", type+1);
Packit ae235b
  else if (strcmp ("inode/directory", type) == 0)
Packit ae235b
    return g_strdup (type);
Packit ae235b
  /* TODO: Map "image" to "image/ *", etc? */
Packit ae235b
Packit ae235b
  return g_strdup ("application/octet-stream");
Packit ae235b
}
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (_type_icons);
Packit ae235b
static GHashTable *_type_icons = NULL;
Packit ae235b
Packit ae235b
GIcon *
Packit ae235b
g_content_type_get_icon (const gchar *type)
Packit ae235b
{
Packit ae235b
  GIcon *themed_icon;
Packit ae235b
  char *name = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (type != NULL, NULL);
Packit ae235b
Packit ae235b
  /* In the Registry icons are the default value of
Packit ae235b
     HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
Packit ae235b
     <type>: <value>
Packit ae235b
     REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
Packit ae235b
     REG_SZ: shimgvw.dll,3
Packit ae235b
  */
Packit ae235b
  G_LOCK (_type_icons);
Packit ae235b
  if (!_type_icons)
Packit ae235b
    _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
Packit ae235b
  name = g_hash_table_lookup (_type_icons, type);
Packit ae235b
  if (!name && type[0] == '.')
Packit ae235b
    {
Packit ae235b
      /* double lookup by extension */
Packit ae235b
      gchar *key = get_registry_classes_key (type, NULL);
Packit ae235b
      if (!key)
Packit ae235b
        key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
Packit ae235b
          g_free (key);
Packit ae235b
          key = key2;
Packit ae235b
        }
Packit ae235b
      name = get_registry_classes_key (key, NULL);
Packit ae235b
      if (name && strcmp (name, "%1") == 0)
Packit ae235b
        {
Packit ae235b
          g_free (name);
Packit ae235b
          name = NULL;
Packit ae235b
        }
Packit ae235b
      if (name)
Packit ae235b
        g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
Packit ae235b
      g_free (key);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!name)
Packit ae235b
    {
Packit ae235b
      /* if no icon found, fall back to standard generic names */
Packit ae235b
      if (strcmp (type, "inode/directory") == 0)
Packit ae235b
        name = "folder";
Packit ae235b
      else if (g_content_type_can_be_executable (type))
Packit ae235b
        name = "system-run";
Packit ae235b
      else
Packit ae235b
        name = "text-x-generic";
Packit ae235b
      g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
Packit ae235b
    }
Packit ae235b
  themed_icon = g_themed_icon_new (name);
Packit ae235b
  G_UNLOCK (_type_icons);
Packit ae235b
Packit ae235b
  return G_ICON (themed_icon);
Packit ae235b
}
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 (type);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_content_type_get_generic_icon_name (const gchar *type)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
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 (strcmp (type, ".exe") == 0 ||
Packit ae235b
      strcmp (type, ".com") == 0 ||
Packit ae235b
      strcmp (type, ".bat") == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  /* TODO: Also look at PATHEXT, which lists the extensions for
Packit ae235b
   * "scripts" in addition to those for true binary executables.
Packit ae235b
   *
Packit ae235b
   * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
Packit ae235b
   * right now, for instance). And in a sense, all associated file
Packit ae235b
   * types are "executable" on Windows... You can just type foo.jpg as
Packit ae235b
   * a command name in cmd.exe, and it will run the application
Packit ae235b
   * associated with .jpg. Hard to say what this API actually means
Packit ae235b
   * with "executable".
Packit ae235b
   */
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
looks_like_text (const guchar *data, 
Packit ae235b
                 gsize         data_size)
Packit ae235b
{
Packit ae235b
  gsize i;
Packit ae235b
  guchar c;
Packit ae235b
  for (i = 0; i < data_size; i++)
Packit ae235b
    {
Packit ae235b
      c = data[i];
Packit ae235b
      if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
Packit ae235b
        return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_content_type_from_mime_type (const gchar *mime_type)
Packit ae235b
{
Packit ae235b
  char *key, *content_type;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (mime_type != NULL, NULL);
Packit ae235b
Packit ae235b
  /* This is a hack to allow directories to have icons in filechooser */
Packit ae235b
  if (strcmp ("inode/directory", mime_type) == 0)
Packit ae235b
    return g_strdup (mime_type);
Packit ae235b
Packit ae235b
  key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
Packit ae235b
  content_type = get_registry_classes_key (key, L"Extension");
Packit ae235b
  g_free (key);
Packit ae235b
Packit ae235b
  return content_type;
Packit ae235b
}
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
  char *type;
Packit ae235b
  char *dot;
Packit ae235b
Packit ae235b
  type = NULL;
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 ("*"));
Packit ae235b
Packit ae235b
  if (filename)
Packit ae235b
    {
Packit ae235b
      basename = g_path_get_basename (filename);
Packit ae235b
      dot = strrchr (basename, '.');
Packit ae235b
      if (dot)
Packit ae235b
        type = g_strdup (dot);
Packit ae235b
      g_free (basename);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (type)
Packit ae235b
    return type;
Packit ae235b
Packit ae235b
  if (data && looks_like_text (data, data_size))
Packit ae235b
    return g_strdup (".txt");
Packit ae235b
Packit ae235b
  return g_strdup ("*");
Packit ae235b
}
Packit ae235b
Packit ae235b
GList *
Packit ae235b
g_content_types_get_registered (void)
Packit ae235b
{
Packit ae235b
  DWORD index;
Packit ae235b
  wchar_t keyname[256];
Packit ae235b
  DWORD key_len;
Packit ae235b
  char *key_utf8;
Packit ae235b
  GList *types;
Packit ae235b
Packit ae235b
  types = NULL;
Packit ae235b
  index = 0;
Packit ae235b
  key_len = 256;
Packit ae235b
  while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
Packit ae235b
                       index,
Packit ae235b
                       keyname,
Packit ae235b
                       &key_len,
Packit ae235b
                       NULL,
Packit ae235b
                       NULL,
Packit ae235b
                       NULL,
Packit ae235b
                       NULL) == ERROR_SUCCESS)
Packit ae235b
    {
Packit ae235b
      key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
Packit ae235b
      if (key_utf8)
Packit ae235b
        {
Packit ae235b
          if (*key_utf8 == '.')
Packit ae235b
            types = g_list_prepend (types, key_utf8);
Packit ae235b
          else
Packit ae235b
            g_free (key_utf8);
Packit ae235b
        }
Packit ae235b
      index++;
Packit ae235b
      key_len = 256;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return g_list_reverse (types);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar **
Packit ae235b
g_content_type_guess_for_tree (GFile *root)
Packit ae235b
{
Packit ae235b
  /* FIXME: implement */
Packit ae235b
  return NULL;
Packit ae235b
}