unicode: Simplify width table generation
[glib.git] / gio / gwin32appinfo.c
blob7762a2b26ea5f61dec5d4a08a74d2ae3a0ac7d33
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include <string.h>
25 #include "gcontenttype.h"
26 #include "gwin32appinfo.h"
27 #include "gappinfo.h"
28 #include "gioerror.h"
29 #include "gfile.h"
30 #include <glib/gstdio.h>
31 #include "glibintl.h"
33 #include <windows.h>
34 #include <shlwapi.h>
37 #ifndef ASSOCF_INIT_BYEXENAME
38 #define ASSOCF_INIT_BYEXENAME 0x00000002
39 #endif
41 /* These were wrong in MingW */
42 #define REAL_ASSOCSTR_COMMAND 1
43 #define REAL_ASSOCSTR_EXECUTABLE 2
44 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
45 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
47 #ifndef AssocQueryString
48 #pragma message("AssocQueryString not available with SDK used")
49 #endif
51 static void g_win32_app_info_iface_init (GAppInfoIface *iface);
53 struct _GWin32AppInfo
55 GObject parent_instance;
56 wchar_t *id;
57 char *id_utf8;
58 gboolean id_is_exename;
59 char *executable;
60 char *name;
61 gboolean no_open_with;
64 G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo, g_win32_app_info, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
66 g_win32_app_info_iface_init))
69 static void
70 g_win32_app_info_finalize (GObject *object)
72 GWin32AppInfo *info;
74 info = G_WIN32_APP_INFO (object);
76 g_free (info->id);
77 g_free (info->id_utf8);
78 g_free (info->name);
79 g_free (info->executable);
81 G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize (object);
84 static void
85 g_win32_app_info_class_init (GWin32AppInfoClass *klass)
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 gobject_class->finalize = g_win32_app_info_finalize;
92 static void
93 g_win32_app_info_init (GWin32AppInfo *local)
97 static GAppInfo *
98 g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
99 gboolean id_is_exename)
101 #ifdef AssocQueryString
102 ASSOCF flags;
103 #endif
104 wchar_t buffer[1024];
105 DWORD buffer_size;
106 GWin32AppInfo *info;
107 HKEY app_key;
109 info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
110 info->id = id; /* Takes ownership */
111 info->id_utf8 = g_utf16_to_utf8 (id, -1, NULL, NULL, NULL);
112 info->id_is_exename = id_is_exename;
114 #ifdef AssocQueryString
115 flags = 0;
116 if (id_is_exename)
117 flags |= ASSOCF_INIT_BYEXENAME;
119 buffer_size = 1024;
120 if (AssocQueryStringW(flags,
121 REAL_ASSOCSTR_EXECUTABLE,
123 NULL,
124 buffer,
125 &buffer_size) == S_OK)
126 info->executable = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
128 buffer_size = 1024;
129 if (AssocQueryStringW(flags,
130 REAL_ASSOCSTR_FRIENDLYAPPNAME,
132 NULL,
133 buffer,
134 &buffer_size) == S_OK)
135 info->name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
136 #endif
138 if (info->name == NULL)
140 /* TODO: Should look up name from executable resources */
141 if (info->executable)
142 info->name = g_path_get_basename (info->executable);
143 else
144 info->name = g_strdup (info->id_utf8);
147 #ifdef AssocQueryString
148 if (AssocQueryKeyW(flags,
149 ASSOCKEY_APP,
150 info->id,
151 NULL,
152 &app_key) == S_OK)
154 if (RegQueryValueExW (app_key, L"NoOpenWith", 0,
155 NULL, NULL, NULL) == ERROR_SUCCESS)
156 info->no_open_with = TRUE;
157 RegCloseKey (app_key);
159 #endif
161 return G_APP_INFO (info);
164 static wchar_t *
165 dup_wstring (wchar_t *str)
167 gsize len;
168 for (len = 0; str[len] != 0; len++)
170 return (wchar_t *)g_memdup (str, (len + 1) * 2);
173 static GAppInfo *
174 g_win32_app_info_dup (GAppInfo *appinfo)
176 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
177 GWin32AppInfo *new_info;
179 new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
181 new_info->id = dup_wstring (info->id);
182 new_info->id_utf8 = g_strdup (info->id_utf8);
183 new_info->id_is_exename = info->id_is_exename;
184 new_info->name = g_strdup (info->name);
185 new_info->executable = g_strdup (info->executable);
186 new_info->no_open_with = info->no_open_with;
188 return G_APP_INFO (new_info);
191 static gboolean
192 g_win32_app_info_equal (GAppInfo *appinfo1,
193 GAppInfo *appinfo2)
195 GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
196 GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
198 if (info1->executable == NULL ||
199 info2->executable == NULL)
200 return FALSE;
202 return strcmp (info1->executable, info2->executable) == 0;
205 static const char *
206 g_win32_app_info_get_id (GAppInfo *appinfo)
208 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
210 return info->id_utf8;
213 static const char *
214 g_win32_app_info_get_name (GAppInfo *appinfo)
216 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
218 if (info->name == NULL)
219 return _("Unnamed");
221 return info->name;
224 static const char *
225 g_win32_app_info_get_description (GAppInfo *appinfo)
227 /* Win32 has no app descriptions */
228 return NULL;
231 static const char *
232 g_win32_app_info_get_executable (GAppInfo *appinfo)
234 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
236 return info->executable;
239 static GIcon *
240 g_win32_app_info_get_icon (GAppInfo *appinfo)
242 /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
244 /* TODO: How to handle icons */
245 return NULL;
248 static gboolean
249 g_win32_app_info_launch (GAppInfo *appinfo,
250 GList *files,
251 GAppLaunchContext *launch_context,
252 GError **error)
254 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
255 #ifdef AssocQueryString
256 ASSOCF flags;
257 #endif
258 HKEY class_key;
259 SHELLEXECUTEINFOW exec_info = {0};
260 GList *l;
262 /* TODO: What might startup_id mean on win32? */
263 #ifdef AssocQueryString
264 flags = 0;
265 if (info->id_is_exename)
266 flags |= ASSOCF_INIT_BYEXENAME;
268 if (AssocQueryKeyW (flags,
269 ASSOCKEY_SHELLEXECCLASS,
270 info->id,
271 NULL,
272 &class_key) != S_OK)
274 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
275 return FALSE;
277 #endif
279 /* FIXME: Need to do something with
280 * g_app_launch_context_get_environment()... ShellExecuteExW()
281 * doesn't have any way to pass an environment though. We need to
282 * either (a) update environment, ShellExecuteExW(), revert
283 * environment; or (b) find an API to figure out what app
284 * ShellExecuteExW() would launch, and then use g_spawn_async()
285 * instead.
288 for (l = files; l != NULL; l = l->next)
290 char *path = g_file_get_path (l->data);
291 wchar_t *wfilename = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
293 g_free (path);
295 memset (&exec_info, 0, sizeof (exec_info));
296 exec_info.cbSize = sizeof (exec_info);
297 exec_info.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_CLASSKEY;
298 exec_info.lpFile = wfilename;
299 exec_info.nShow = SW_SHOWNORMAL;
300 exec_info.hkeyClass = class_key;
302 if (!ShellExecuteExW (&exec_info))
304 char *message_utf8 = g_win32_error_message (GetLastError ());
306 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Error launching application: %s"), message_utf8);
307 g_free (message_utf8);
309 g_free (wfilename);
310 RegCloseKey (class_key);
311 return FALSE;
314 g_free (wfilename);
317 RegCloseKey (class_key);
319 return TRUE;
322 static gboolean
323 g_win32_app_info_supports_uris (GAppInfo *appinfo)
325 return FALSE;
328 static gboolean
329 g_win32_app_info_supports_files (GAppInfo *appinfo)
331 return TRUE;
334 static gboolean
335 g_win32_app_info_launch_uris (GAppInfo *appinfo,
336 GList *uris,
337 GAppLaunchContext *launch_context,
338 GError **error)
340 g_set_error_literal (error, G_IO_ERROR,
341 G_IO_ERROR_NOT_SUPPORTED,
342 _("URIs not supported"));
343 return FALSE;
346 static gboolean
347 g_win32_app_info_should_show (GAppInfo *appinfo)
349 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
351 if (info->no_open_with)
352 return FALSE;
354 return TRUE;
357 static gboolean
358 g_win32_app_info_set_as_default_for_type (GAppInfo *appinfo,
359 const char *content_type,
360 GError **error)
362 g_set_error_literal (error, G_IO_ERROR,
363 G_IO_ERROR_NOT_SUPPORTED,
364 _("association changes not supported on win32"));
365 return FALSE;
368 GAppInfo *
369 g_app_info_create_from_commandline (const char *commandline,
370 const char *application_name,
371 GAppInfoCreateFlags flags,
372 GError **error)
374 g_set_error_literal (error, G_IO_ERROR,
375 G_IO_ERROR_NOT_SUPPORTED,
376 _("Association creation not supported on win32"));
377 return NULL;
381 static void
382 g_win32_app_info_iface_init (GAppInfoIface *iface)
384 iface->dup = g_win32_app_info_dup;
385 iface->equal = g_win32_app_info_equal;
386 iface->get_id = g_win32_app_info_get_id;
387 iface->get_name = g_win32_app_info_get_name;
388 iface->get_description = g_win32_app_info_get_description;
389 iface->get_executable = g_win32_app_info_get_executable;
390 iface->get_icon = g_win32_app_info_get_icon;
391 iface->launch = g_win32_app_info_launch;
392 iface->supports_uris = g_win32_app_info_supports_uris;
393 iface->supports_files = g_win32_app_info_supports_files;
394 iface->launch_uris = g_win32_app_info_launch_uris;
395 iface->should_show = g_win32_app_info_should_show;
396 iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
399 static void
400 enumerate_open_with_list (HKEY dir_key,
401 GList **prognames)
403 DWORD index;
404 wchar_t name[256];
405 DWORD name_len, nbytes;
406 wchar_t data[256];
407 wchar_t *data_alloc;
408 DWORD type;
410 /* Must also look inside for a,b,c, + MRUList */
411 index = 0;
412 name_len = 256;
413 nbytes = sizeof (data) - 2;
414 while (RegEnumValueW (dir_key,
415 index,
416 name,
417 &name_len,
419 &type,
420 (LPBYTE)data,
421 &nbytes) == ERROR_SUCCESS)
423 data[nbytes/2] = '\0';
424 if (type == REG_SZ &&
425 /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
426 name_len == 1)
428 data_alloc = (wchar_t *)g_memdup (data, nbytes + 2);
429 data_alloc[nbytes/2] = 0;
430 *prognames = g_list_prepend (*prognames, data_alloc);
432 index++;
433 name_len = 256;
434 nbytes = sizeof (data) - 2;
437 index = 0;
438 name_len = 256;
439 while (RegEnumKeyExW (dir_key,
440 index,
441 name,
442 &name_len,
443 NULL,
444 NULL,
445 NULL,
446 NULL) == ERROR_SUCCESS)
448 *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
449 index++;
450 name_len = 256;
454 static void
455 enumerate_open_with_progids (HKEY dir_key,
456 GList **progids)
458 DWORD index;
459 wchar_t name[256];
460 DWORD name_len, type;
462 index = 0;
463 name_len = 256;
464 while (RegEnumValueW (dir_key,
465 index,
466 name,
467 &name_len,
469 &type,
470 NULL,
471 0) == ERROR_SUCCESS)
473 *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
474 index++;
475 name_len = 256;
479 static void
480 enumerate_open_with_root (HKEY dir_key,
481 GList **progids,
482 GList **prognames)
484 HKEY reg_key = NULL;
486 if (RegOpenKeyExW (dir_key, L"OpenWithList", 0,
487 KEY_READ, &reg_key) == ERROR_SUCCESS)
489 enumerate_open_with_list (reg_key, prognames);
490 RegCloseKey (reg_key);
493 if (RegOpenKeyExW (dir_key, L"OpenWithProgids", 0,
494 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
496 enumerate_open_with_progids (reg_key, progids);
497 RegCloseKey (reg_key);
501 static gboolean
502 app_info_in_list (GAppInfo *info,
503 GList *list)
505 while (list != NULL)
507 if (g_app_info_equal (info, list->data))
508 return TRUE;
509 list = list->next;
511 return FALSE;
514 GList *
515 g_app_info_get_all_for_type (const char *content_type)
517 GList *progids = NULL;
518 GList *prognames = NULL;
519 HKEY reg_key, sys_file_assoc_key, reg_key2;
520 wchar_t percieved_type[128];
521 DWORD nchars, key_type;
522 wchar_t *wc_key;
523 GList *l;
524 GList *infos;
526 wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
527 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
528 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
530 enumerate_open_with_root (reg_key, &progids, &prognames);
532 nchars = sizeof (percieved_type) / sizeof(wchar_t);
533 if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
534 &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
536 if (key_type == REG_SZ &&
537 RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
538 KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
540 if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
541 KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
543 enumerate_open_with_root (reg_key2, &progids, &prognames);
544 RegCloseKey (reg_key2);
547 RegCloseKey (sys_file_assoc_key);
550 RegCloseKey (reg_key);
553 if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
554 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
556 if (RegOpenKeyExW (reg_key, wc_key, 0,
557 KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
559 enumerate_open_with_root (reg_key2, &progids, &prognames);
560 RegCloseKey (reg_key2);
563 RegCloseKey (reg_key);
566 infos = NULL;
567 for (l = prognames; l != NULL; l = l->next)
569 GAppInfo *info;
571 /* l->data ownership is taken */
572 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
573 if (app_info_in_list (info, infos))
574 g_object_unref (info);
575 else
576 infos = g_list_prepend (infos, info);
578 g_list_free (prognames);
580 for (l = progids; l != NULL; l = l->next)
582 GAppInfo *info;
584 /* l->data ownership is taken */
585 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
586 if (app_info_in_list (info, infos))
587 g_object_unref (info);
588 else
589 infos = g_list_prepend (infos, info);
591 g_list_free (progids);
593 g_free (wc_key);
594 return g_list_reverse (infos);
597 GList *
598 g_app_info_get_recommended_for_type (const char *content_type)
600 /* FIXME: this should generate a list of applications that are registered
601 * as direct handlers for the given content type, without using MIME subclassing.
602 * See g_app_info_get_recommended_for_type() in gdesktopappinfo.c for a reference
603 * UNIX implementation.
605 return g_app_info_get_all_for_type (content_type);
608 GList *
609 g_app_info_get_fallback_for_type (const char *content_type)
611 /* FIXME: this should generate a list of applications that are registered
612 * as handlers for a superclass of the given content type, but are not
613 * direct handlers for the content type itself. See g_app_info_get_fallback_for_type()
614 * in gdesktopappinfo.c for a reference UNIX implementation.
616 return g_app_info_get_all_for_type (content_type);
619 GAppInfo *
620 g_app_info_get_default_for_type (const char *content_type,
621 gboolean must_support_uris)
623 wchar_t *wtype;
624 wchar_t buffer[1024];
625 DWORD buffer_size;
627 wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
629 /* Verify that we have some sort of app registered for this type */
630 #ifdef AssocQueryString
631 buffer_size = 1024;
632 if (AssocQueryStringW (0,
633 REAL_ASSOCSTR_COMMAND,
634 wtype,
635 NULL,
636 buffer,
637 &buffer_size) == S_OK)
638 /* Takes ownership of wtype */
639 return g_desktop_app_info_new_from_id (wtype, FALSE);
640 #endif
642 g_free (wtype);
643 return NULL;
646 GAppInfo *
647 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
649 /* TODO: Implement */
650 return NULL;
653 GList *
654 g_app_info_get_all (void)
656 DWORD index;
657 wchar_t name[256];
658 DWORD name_len;
659 HKEY reg_key;
660 GList *infos;
661 GAppInfo *info;
663 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
664 KEY_READ, &reg_key) != ERROR_SUCCESS)
665 return NULL;
667 infos = NULL;
668 index = 0;
669 name_len = 256;
670 while (RegEnumKeyExW (reg_key,
671 index,
672 name,
673 &name_len,
674 NULL,
675 NULL,
676 NULL,
677 NULL) == ERROR_SUCCESS)
679 wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
680 /* name_dup ownership is taken */
681 info = g_desktop_app_info_new_from_id (name_dup, TRUE);
682 infos = g_list_prepend (infos, info);
684 index++;
685 name_len = 256;
688 RegCloseKey (reg_key);
690 return g_list_reverse (infos);
693 void
694 g_app_info_reset_type_associations (const char *content_type)
696 /* nothing to do */