1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 *
22 * Author: Alexander Larsson <alexl@redhat.com>
23 */
24
25 #include "config.h"
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include "gcontenttype.h"
31 #include "gthemedicon.h"
32 #include "gicon.h"
33 #include "glibintl.h"
34
35 #include <windows.h>
36
37 static char *
38 get_registry_classes_key (const char *subdir,
39 const wchar_t *key_name)
40 {
41 wchar_t *wc_key;
42 HKEY reg_key = NULL;
43 DWORD key_type;
44 DWORD nbytes;
45 char *value_utf8;
46
47 value_utf8 = NULL;
48
49 nbytes = 0;
50 wc_key = g_utf8_to_utf16 (subdir, -1, NULL, NULL, NULL);
51 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
52 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS &&
53 RegQueryValueExW (reg_key, key_name, 0,
54 &key_type, NULL, &nbytes) == ERROR_SUCCESS &&
55 (key_type == REG_SZ || key_type == REG_EXPAND_SZ))
56 {
57 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
58 RegQueryValueExW (reg_key, key_name, 0,
59 &key_type, (LPBYTE) wc_temp, &nbytes);
60 wc_temp[nbytes/2] = '\0';
61 if (key_type == REG_EXPAND_SZ)
62 {
63 wchar_t dummy[1];
64 DWORD len = ExpandEnvironmentStringsW (wc_temp, dummy, 1);
65 if (len > 0)
66 {
67 wchar_t *wc_temp_expanded = g_new (wchar_t, len);
68 if (ExpandEnvironmentStringsW (wc_temp, wc_temp_expanded, len) == len)
69 value_utf8 = g_utf16_to_utf8 (wc_temp_expanded, -1, NULL, NULL, NULL);
70 g_free (wc_temp_expanded);
71 }
72 }
73 else
74 {
75 value_utf8 = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
76 }
77 g_free (wc_temp);
78
79 }
80 g_free (wc_key);
81
82 if (reg_key != NULL)
83 RegCloseKey (reg_key);
84
85 return value_utf8;
86 }
87
88 /*< private >*/
89 void
90 g_content_type_set_mime_dirs (const gchar * const *dirs)
91 {
92 /* noop on Windows */
93 }
94
95 /*< private >*/
96 const gchar * const *
97 g_content_type_get_mime_dirs (void)
98 {
99 const gchar * const *mime_dirs = { NULL };
100 return mime_dirs;
101 }
102
103 gboolean
104 g_content_type_equals (const gchar *type1,
105 const gchar *type2)
106 {
107 char *progid1, *progid2;
108 gboolean res;
109
110 g_return_val_if_fail (type1 != NULL, FALSE);
111 g_return_val_if_fail (type2 != NULL, FALSE);
112
113 if (g_ascii_strcasecmp (type1, type2) == 0)
114 return TRUE;
115
116 res = FALSE;
117 progid1 = get_registry_classes_key (type1, NULL);
118 progid2 = get_registry_classes_key (type2, NULL);
119 if (progid1 != NULL && progid2 != NULL &&
120 strcmp (progid1, progid2) == 0)
121 res = TRUE;
122 g_free (progid1);
123 g_free (progid2);
124
125 return res;
126 }
127
128 gboolean
129 g_content_type_is_a (const gchar *type,
130 const gchar *supertype)
131 {
132 gboolean res;
133 char *perceived_type;
134 char *perceived_supertype;
135
136 g_return_val_if_fail (type != NULL, FALSE);
137 g_return_val_if_fail (supertype != NULL, FALSE);
138
139 if (g_content_type_equals (type, supertype))
140 return TRUE;
141
142 perceived_type = get_registry_classes_key (type, L"PerceivedType");
143 perceived_supertype = get_registry_classes_key (supertype, L"PerceivedType");
144
145 res = perceived_type && perceived_supertype &&
146 strcmp (perceived_type, perceived_supertype) == 0;
147
148 g_free (perceived_type);
149 g_free (perceived_supertype);
150
151 return res;
152 }
153
154 gboolean
155 g_content_type_is_mime_type (const gchar *type,
156 const gchar *mime_type)
157 {
158 gchar *content_type;
159 gboolean ret;
160
161 g_return_val_if_fail (type != NULL, FALSE);
162 g_return_val_if_fail (mime_type != NULL, FALSE);
163
164 content_type = g_content_type_from_mime_type (mime_type);
165 ret = g_content_type_is_a (type, content_type);
166 g_free (content_type);
167
168 return ret;
169 }
170
171 gboolean
172 g_content_type_is_unknown (const gchar *type)
173 {
174 g_return_val_if_fail (type != NULL, FALSE);
175
176 return strcmp ("*", type) == 0;
177 }
178
179 gchar *
180 g_content_type_get_description (const gchar *type)
181 {
182 char *progid;
183 char *description;
184
185 g_return_val_if_fail (type != NULL, NULL);
186
187 progid = get_registry_classes_key (type, NULL);
188 if (progid)
189 {
190 description = get_registry_classes_key (progid, NULL);
191 g_free (progid);
192
193 if (description)
194 return description;
195 }
196
197 if (g_content_type_is_unknown (type))
198 return g_strdup (_("Unknown type"));
199
200 return g_strdup_printf (_("%s filetype"), type);
201 }
202
203 gchar *
204 g_content_type_get_mime_type (const gchar *type)
205 {
206 char *mime;
207
208 g_return_val_if_fail (type != NULL, NULL);
209
210 mime = get_registry_classes_key (type, L"Content Type");
211 if (mime)
212 return mime;
213 else if (g_content_type_is_unknown (type))
214 return g_strdup ("application/octet-stream");
215 else if (*type == '.')
216 return g_strdup_printf ("application/x-ext-%s", type+1);
217 else if (strcmp ("inode/directory", type) == 0)
218 return g_strdup (type);
219 /* TODO: Map "image" to "image/ *", etc? */
220
221 return g_strdup ("application/octet-stream");
222 }
223
224 G_LOCK_DEFINE_STATIC (_type_icons);
225 static GHashTable *_type_icons = NULL;
226
227 GIcon *
228 g_content_type_get_icon (const gchar *type)
229 {
230 GIcon *themed_icon;
231 char *name = NULL;
232
233 g_return_val_if_fail (type != NULL, NULL);
234
235 /* In the Registry icons are the default value of
236 HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
237 <type>: <value>
238 REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
239 REG_SZ: shimgvw.dll,3
240 */
241 G_LOCK (_type_icons);
242 if (!_type_icons)
243 _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
244 name = g_hash_table_lookup (_type_icons, type);
245 if (!name && type[0] == '.')
246 {
247 /* double lookup by extension */
248 gchar *key = get_registry_classes_key (type, NULL);
249 if (!key)
250 key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
251 else
252 {
253 gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
254 g_free (key);
255 key = key2;
256 }
257 name = get_registry_classes_key (key, NULL);
258 if (name && strcmp (name, "%1") == 0)
259 {
260 g_free (name);
261 name = NULL;
262 }
263 if (name)
264 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
265 g_free (key);
266 }
267
268 if (!name)
269 {
270 /* if no icon found, fall back to standard generic names */
271 if (strcmp (type, "inode/directory") == 0)
272 name = "folder";
273 else if (g_content_type_can_be_executable (type))
274 name = "system-run";
275 else
276 name = "text-x-generic";
277 g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
278 }
279 themed_icon = g_themed_icon_new (name);
280 G_UNLOCK (_type_icons);
281
282 return G_ICON (themed_icon);
283 }
284
285 GIcon *
286 g_content_type_get_symbolic_icon (const gchar *type)
287 {
288 return g_content_type_get_icon (type);
289 }
290
291 gchar *
292 g_content_type_get_generic_icon_name (const gchar *type)
293 {
294 return NULL;
295 }
296
297 gboolean
298 g_content_type_can_be_executable (const gchar *type)
299 {
300 g_return_val_if_fail (type != NULL, FALSE);
301
302 if (strcmp (type, ".exe") == 0 ||
303 strcmp (type, ".com") == 0 ||
304 strcmp (type, ".bat") == 0)
305 return TRUE;
306
307 /* TODO: Also look at PATHEXT, which lists the extensions for
308 * "scripts" in addition to those for true binary executables.
309 *
310 * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
311 * right now, for instance). And in a sense, all associated file
312 * types are "executable" on Windows... You can just type foo.jpg as
313 * a command name in cmd.exe, and it will run the application
314 * associated with .jpg. Hard to say what this API actually means
315 * with "executable".
316 */
317
318 return FALSE;
319 }
320
321 static gboolean
322 looks_like_text (const guchar *data,
323 gsize data_size)
324 {
325 gsize i;
326 guchar c;
327 for (i = 0; i < data_size; i++)
328 {
329 c = data[i];
330 if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
331 return FALSE;
332 }
333 return TRUE;
334 }
335
336 gchar *
337 g_content_type_from_mime_type (const gchar *mime_type)
338 {
339 char *key, *content_type;
340
341 g_return_val_if_fail (mime_type != NULL, NULL);
342
343 /* This is a hack to allow directories to have icons in filechooser */
344 if (strcmp ("inode/directory", mime_type) == 0)
345 return g_strdup (mime_type);
346
347 key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
348 content_type = get_registry_classes_key (key, L"Extension");
349 g_free (key);
350
351
352 return content_type ? g_steal_pointer (&content_type) : g_strdup ("*");
353 }
354
355 gchar *
356 g_content_type_guess (const gchar *filename,
357 const guchar *data,
358 gsize data_size,
359 gboolean *result_uncertain)
360 {
361 char *basename;
362 char *type;
363 char *dot;
364 size_t i;
365
366 type = NULL;
367
368 if (result_uncertain)
369 *result_uncertain = FALSE;
370
371 /* our test suite and potentially other code used -1 in the past, which is
372 * not documented and not allowed; guard against that */
373 g_return_val_if_fail (data_size != (gsize) -1, g_strdup ("*"));
374
375 if (filename)
376 {
377 i = strlen (filename);
378 if (i > 0 && filename[i - 1] == G_DIR_SEPARATOR)
379 {
380 type = g_strdup ("inode/directory");
381 if (result_uncertain)
382 *result_uncertain = TRUE;
383 }
384 else
385 {
386 basename = g_path_get_basename (filename);
387 dot = strrchr (basename, '.');
388 if (dot)
389 type = g_strdup (dot);
390 g_free (basename);
391 }
392 }
393
394 if (type)
395 return type;
396
397 if (data && looks_like_text (data, data_size))
398 return g_strdup (".txt");
399
400 return g_strdup ("*");
401 }
402
403 GList *
404 g_content_types_get_registered (void)
405 {
406 DWORD index;
407 wchar_t keyname[256];
408 DWORD key_len;
409 char *key_utf8;
410 GList *types;
411
412 types = NULL;
413 index = 0;
414 key_len = 256;
415 while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
416 index,
417 keyname,
418 &key_len,
419 NULL,
420 NULL,
421 NULL,
422 NULL) == ERROR_SUCCESS)
423 {
424 key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
425 if (key_utf8)
426 {
427 if (*key_utf8 == '.')
428 types = g_list_prepend (types, key_utf8);
429 else
430 g_free (key_utf8);
431 }
432 index++;
433 key_len = 256;
434 }
435
436 return g_list_reverse (types);
437 }
438
439 gchar **
440 g_content_type_guess_for_tree (GFile *root)
441 {
442 /* FIXME: implement */
443 return NULL;
444 }