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