Make GSettingsSchemaKey public
[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 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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
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, &reg_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           int 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 gboolean
89 g_content_type_equals (const gchar *type1,
90                        const gchar *type2)
91 {
92   char *progid1, *progid2;
93   gboolean res;
94   
95   g_return_val_if_fail (type1 != NULL, FALSE);
96   g_return_val_if_fail (type2 != NULL, FALSE);
97
98   if (g_ascii_strcasecmp (type1, type2) == 0)
99     return TRUE;
100
101   res = FALSE;
102   progid1 = get_registry_classes_key (type1, NULL);
103   progid2 = get_registry_classes_key (type2, NULL);
104   if (progid1 != NULL && progid2 != NULL &&
105       strcmp (progid1, progid2) == 0)
106     res = TRUE;
107   g_free (progid1);
108   g_free (progid2);
109   
110   return res;
111 }
112
113 gboolean
114 g_content_type_is_a (const gchar *type,
115                      const gchar *supertype)
116 {
117   gboolean res;
118   char *value_utf8;
119
120   g_return_val_if_fail (type != NULL, FALSE);
121   g_return_val_if_fail (supertype != NULL, FALSE);
122
123   if (g_content_type_equals (type, supertype))
124     return TRUE;
125
126   res = FALSE;
127   value_utf8 = get_registry_classes_key (type, L"PerceivedType");
128   if (value_utf8 && strcmp (value_utf8, supertype) == 0)
129     res = TRUE;
130   g_free (value_utf8);
131   
132   return res;
133 }
134
135 gboolean
136 g_content_type_is_unknown (const gchar *type)
137 {
138   g_return_val_if_fail (type != NULL, FALSE);
139
140   return strcmp ("*", type) == 0;
141 }
142
143 gchar *
144 g_content_type_get_description (const gchar *type)
145 {
146   char *progid;
147   char *description;
148
149   g_return_val_if_fail (type != NULL, NULL);
150
151   progid = get_registry_classes_key (type, NULL);
152   if (progid)
153     {
154       description = get_registry_classes_key (progid, NULL);
155       g_free (progid);
156
157       if (description)
158         return description;
159     }
160
161   if (g_content_type_is_unknown (type))
162     return g_strdup (_("Unknown type"));
163   return g_strdup_printf (_("%s filetype"), type);
164 }
165
166 gchar *
167 g_content_type_get_mime_type (const gchar *type)
168 {
169   char *mime;
170
171   g_return_val_if_fail (type != NULL, NULL);
172
173   mime = get_registry_classes_key (type, L"Content Type");
174   if (mime)
175     return mime;
176   else if (g_content_type_is_unknown (type))
177     return g_strdup ("application/octet-stream");
178   else if (*type == '.')
179     return g_strdup_printf ("application/x-ext-%s", type+1);
180   /* TODO: Map "image" to "image/ *", etc? */
181
182   return g_strdup ("application/octet-stream");
183 }
184
185 G_LOCK_DEFINE_STATIC (_type_icons);
186 static GHashTable *_type_icons = NULL;
187
188 GIcon *
189 g_content_type_get_icon (const gchar *type)
190 {
191   GIcon *themed_icon;
192   char *name = NULL;
193
194   g_return_val_if_fail (type != NULL, NULL);
195
196   /* In the Registry icons are the default value of
197      HKEY_CLASSES_ROOT\<progid>\DefaultIcon with typical values like:
198      <type>: <value>
199      REG_EXPAND_SZ: %SystemRoot%\System32\Wscript.exe,3
200      REG_SZ: shimgvw.dll,3
201   */
202   G_LOCK (_type_icons);
203   if (!_type_icons)
204     _type_icons = g_hash_table_new (g_str_hash, g_str_equal);
205   name = g_hash_table_lookup (_type_icons, type);
206   if (!name && type[0] == '.')
207     {
208       /* double lookup by extension */
209       gchar *key = get_registry_classes_key (type, NULL);
210       if (!key)
211         key = g_strconcat (type+1, "file\\DefaultIcon", NULL);
212       else
213         {
214           gchar *key2 = g_strconcat (key, "\\DefaultIcon", NULL);
215           g_free (key);
216           key = key2;
217         }
218       name = get_registry_classes_key (key, NULL);
219       if (name && strcmp (name, "%1") == 0)
220         {
221           g_free (name);
222           name = NULL;
223         }
224       if (name)
225         g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
226       g_free (key);
227     }
228
229   /* icon-name similar to how it was with gtk-2-12 */
230   if (name)
231     {
232       themed_icon = g_themed_icon_new (name);
233     }
234   else
235     {
236       /* if not found an icon fall back to gtk-builtins */
237       name = strcmp (type, "inode/directory") == 0 ? "gtk-directory" : 
238                            g_content_type_can_be_executable (type) ? "gtk-execute" : "gtk-file";
239       g_hash_table_insert (_type_icons, g_strdup (type), g_strdup (name));
240       themed_icon = g_themed_icon_new_with_default_fallbacks (name);
241     }
242   G_UNLOCK (_type_icons);
243
244   return G_ICON (themed_icon);
245 }
246
247 GIcon *
248 g_content_type_get_symbolic_icon (const gchar *type)
249 {
250   return g_content_type_get_icon (type);
251 }
252
253 gchar *
254 g_content_type_get_generic_icon_name (const gchar *type)
255 {
256   return NULL;
257 }
258
259 gboolean
260 g_content_type_can_be_executable (const gchar *type)
261 {
262   g_return_val_if_fail (type != NULL, FALSE);
263
264   if (strcmp (type, ".exe") == 0 ||
265       strcmp (type, ".com") == 0 ||
266       strcmp (type, ".bat") == 0)
267     return TRUE;
268
269   /* TODO: Also look at PATHEXT, which lists the extensions for
270    * "scripts" in addition to those for true binary executables.
271    *
272    * (PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH for me
273    * right now, for instance). And in a sense, all associated file
274    * types are "executable" on Windows... You can just type foo.jpg as
275    * a command name in cmd.exe, and it will run the application
276    * associated with .jpg. Hard to say what this API actually means
277    * with "executable".
278    */
279
280   return FALSE;
281 }
282
283 static gboolean
284 looks_like_text (const guchar *data, 
285                  gsize         data_size)
286 {
287   gsize i;
288   guchar c;
289   for (i = 0; i < data_size; i++)
290     {
291       c = data[i];
292       if (g_ascii_iscntrl (c) && !g_ascii_isspace (c) && c != '\b')
293         return FALSE;
294     }
295   return TRUE;
296 }
297
298 gchar *
299 g_content_type_from_mime_type (const gchar *mime_type)
300 {
301   char *key, *content_type;
302
303   g_return_val_if_fail (mime_type != NULL, NULL);
304
305   key = g_strconcat ("MIME\\DataBase\\Content Type\\", mime_type, NULL);
306   content_type = get_registry_classes_key (key, L"Extension");
307   g_free (key);
308
309   return content_type;
310 }
311
312 gchar *
313 g_content_type_guess (const gchar  *filename,
314                       const guchar *data,
315                       gsize         data_size,
316                       gboolean     *result_uncertain)
317 {
318   char *basename;
319   char *type;
320   char *dot;
321
322   type = NULL;
323
324   if (result_uncertain)
325     *result_uncertain = FALSE;
326
327   /* our test suite and potentially other code used -1 in the past, which is
328    * not documented and not allowed; guard against that */
329   g_return_val_if_fail (data_size != (gsize) -1, g_strdup ("*"));
330
331   if (filename)
332     {
333       basename = g_path_get_basename (filename);
334       dot = strrchr (basename, '.');
335       if (dot)
336         type = g_strdup (dot);
337       g_free (basename);
338     }
339
340   if (type)
341     return type;
342
343   if (data && looks_like_text (data, data_size))
344     return g_strdup (".txt");
345
346   return g_strdup ("*");
347 }
348
349 GList *
350 g_content_types_get_registered (void)
351 {
352   DWORD index;
353   wchar_t keyname[256];
354   DWORD key_len;
355   char *key_utf8;
356   GList *types;
357
358   types = NULL;
359   index = 0;
360   key_len = 256;
361   while (RegEnumKeyExW(HKEY_CLASSES_ROOT,
362                        index,
363                        keyname,
364                        &key_len,
365                        NULL,
366                        NULL,
367                        NULL,
368                        NULL) == ERROR_SUCCESS)
369     {
370       key_utf8 = g_utf16_to_utf8 (keyname, -1, NULL, NULL, NULL);
371       if (key_utf8)
372         {
373           if (*key_utf8 == '.')
374             types = g_list_prepend (types, key_utf8);
375           else
376             g_free (key_utf8);
377         }
378       index++;
379       key_len = 256;
380     }
381   
382   return g_list_reverse (types);
383 }
384
385 gchar **
386 g_content_type_guess_for_tree (GFile *root)
387 {
388   /* FIXME: implement */
389   return NULL;
390 }