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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include <sys/types.h>
28 #include "gcontenttype.h"
29 #include "gthemedicon.h"
36 get_registry_classes_key (const char *subdir,
37 const wchar_t *key_name)
48 wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
49 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
50 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS &&
51 RegQueryValueExW (reg_key, key_name, 0,
52 &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
53 (key_type == REG_SZ || key_type == REG_EXPAND_SZ))
55 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
56 RegQueryValueExW (reg_key, key_name, 0,
57 &key_type, (LPBYTE) wc_temp, &nbytes);
58 wc_temp[nbytes/2] = '\0';
59 if (key_type == REG_EXPAND_SZ)
62 int len = ExpandEnvironmentStringsW (wc_temp, dummy, 1);
65 wchar_t *wc_temp_expanded = g_new (wchar_t, len);
66 if (ExpandEnvironmentStringsW (wc_temp, wc_temp_expanded, len) == len)
67 value_utf8 = g_utf16_to_utf8 (wc_temp_expanded, -1, NULL, NULL, NULL);
68 g_free (wc_temp_expanded);
73 value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
81 RegCloseKey (reg_key);
87 g_content_type_equals (const gchar *type1,
90 char *progid1, *progid2;
93 g_return_val_if_fail (type1 != NULL, FALSE);
94 g_return_val_if_fail (type2 != NULL, FALSE);
96 if (g_ascii_strcasecmp (type1, type2) == 0)
100 progid1 = get_registry_classes_key (type1, NULL);
101 progid2 = get_registry_classes_key (type2, NULL);
102 if (progid1 != NULL && progid2 != NULL &&
103 strcmp (progid1, progid2) == 0)
112 g_content_type_is_a (const gchar *type,
113 const gchar *supertype)
118 g_return_val_if_fail (type != NULL, FALSE);
119 g_return_val_if_fail (supertype != NULL, FALSE);
121 if (g_content_type_equals (type, supertype))
125 value_utf8 = get_registry_classes_key (type, L"PerceivedType");
126 if (value_utf8 && strcmp (value_utf8, supertype) == 0)
134 g_content_type_is_unknown (const gchar *type)
136 g_return_val_if_fail (type != NULL, FALSE);
138 return strcmp ("*", type) == 0;
142 g_content_type_get_description (const gchar *type)
147 g_return_val_if_fail (type != NULL, NULL);
149 progid = get_registry_classes_key (type, NULL);
152 description = get_registry_classes_key (progid, NULL);
159 if (g_content_type_is_unknown (type))
160 return g_strdup (_("Unknown type"));
161 return g_strdup_printf (_("%s filetype"), type);
165 g_content_type_get_mime_type (const gchar *type)
169 g_return_val_if_fail (type != NULL, NULL);
171 mime = get_registry_classes_key (type, L"Content Type");
174 else if (g_content_type_is_unknown (type))
175 return g_strdup ("application/octet-stream");
176 else if (*type == '.')
177 return g_strdup_printf ("application/x-ext-%s", type+1);
178 /* TODO: Map "image" to "image/ *", etc? */
180 return g_strdup ("application/octet-stream");
183 G_LOCK_DEFINE_STATIC (_type_icons);
184 static GHashTable *_type_icons = NULL;
187 g_content_type_get_icon (const gchar *type)
192 g_return_val_if_fail (type != NULL, NULL);
194 /* In the Registry icons are the default value of
195 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
197 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
198 REG_SZ: shimgvw.dll,3
200 G_LOCK (_type_icons);
202 _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
203 name = g_hash_table_lookup (_type_icons, type);
204 if (!name && type[0] == '.')
206 /* double lookup by extension */
207 gchar *key = get_registry_classes_key (type, NULL);
209 key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
212 gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
216 name = get_registry_classes_key (key, NULL);
217 if (name && strcmp (name, "%1") == 0)
223 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
227 /* icon-name similar to how it was with gtk-2-12 */
230 themed_icon = g_themed_icon_new (name);
234 /* if not found an icon fall back to gtk-builtins */
235 name = strcmp (type, "inode/directory") == 0 ? "gtk-directory" :
236 g_content_type_can_be_executable (type) ? "gtk-execute" : "gtk-file";
237 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
238 themed_icon = g_themed_icon_new_with_default_fallbacks (name);
240 G_UNLOCK (_type_icons);
242 return G_ICON (themed_icon);
246 g_content_type_get_symbolic_icon (const gchar *type)
248 return g_content_type_get_icon (type);
252 g_content_type_get_generic_icon_name (const gchar *type)
258 g_content_type_can_be_executable (const gchar *type)
260 g_return_val_if_fail (type != NULL, FALSE);
262 if (strcmp (type, ".exe") == 0 ||
263 strcmp (type, ".com") == 0 ||
264 strcmp (type, ".bat") == 0)
267 /* TODO: Also look at PATHEXT, which lists the extensions for
268 * "scripts" in addition to those for true binary executables.
270 * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
271 * right now, for instance). And in a sense, all associated file
272 * types are "executable" on Windows... You can just type foo.jpg as
273 * a command name in cmd.exe, and it will run the application
274 * associated with .jpg. Hard to say what this API actually means
282 looks_like_text (const guchar *data,
287 for (i = 0; i < data_size; i++)
290 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
297 g_content_type_from_mime_type (const gchar *mime_type)
299 char *key, *content_type;
301 g_return_val_if_fail (mime_type != NULL, NULL);
303 key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
304 content_type = get_registry_classes_key (key, L"Extension");
311 g_content_type_guess (const gchar *filename,
314 gboolean *result_uncertain)
322 if (result_uncertain)
323 *result_uncertain = FALSE;
325 /* our test suite and potentially other code used -1 in the past, which is
326 * not documented and not allowed; guard against that */
327 g_return_val_if_fail (data_size != (gsize) -1, g_strdup ("*"));
331 basename = g_path_get_basename (filename);
332 dot = strrchr (basename, '.');
334 type = g_strdup (dot);
341 if (data && looks_like_text (data, data_size))
342 return g_strdup (".txt");
344 return g_strdup ("*");
348 g_content_types_get_registered (void)
351 wchar_t keyname[256];
359 while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
366 NULL) == ERROR_SUCCESS)
368 key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
371 if (*key_utf8 == '.')
372 types = g_list_prepend (types, key_utf8);
380 return g_list_reverse (types);
384 g_content_type_guess_for_tree (GFile *root)
386 /* FIXME: implement */