It is 'registered', not 'registred'
[glib.git] / gio / gwin32appinfo.c
blob40e59ef0d8a05c009587254909fa488d357d652d
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include <string.h>
27 #include "gcontenttype.h"
28 #include "gwin32appinfo.h"
29 #include "gappinfo.h"
30 #include "gioerror.h"
31 #include "gfile.h"
32 #include <glib/gstdio.h>
33 #include "glibintl.h"
35 #include <windows.h>
36 #include <shlwapi.h>
39 #ifndef ASSOCF_INIT_BYEXENAME
40 #define ASSOCF_INIT_BYEXENAME 0x00000002
41 #endif
43 /* These were wrong in MingW */
44 #define REAL_ASSOCSTR_COMMAND 1
45 #define REAL_ASSOCSTR_EXECUTABLE 2
46 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
47 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
49 #ifndef AssocQueryString
50 #pragma message("AssocQueryString not available with SDK used")
51 #endif
53 static void g_win32_app_info_iface_init (GAppInfoIface *iface);
55 struct _GWin32AppInfo
57 GObject parent_instance;
58 wchar_t *id;
59 char *id_utf8;
60 gboolean id_is_exename;
61 char *executable;
62 char *name;
63 gboolean no_open_with;
66 G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo, g_win32_app_info, G_TYPE_OBJECT,
67 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
68 g_win32_app_info_iface_init))
71 static void
72 g_win32_app_info_finalize (GObject *object)
74 GWin32AppInfo *info;
76 info = G_WIN32_APP_INFO (object);
78 g_free (info->id);
79 g_free (info->id_utf8);
80 g_free (info->name);
81 g_free (info->executable);
83 G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize (object);
86 static void
87 g_win32_app_info_class_init (GWin32AppInfoClass *klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 gobject_class->finalize = g_win32_app_info_finalize;
94 static void
95 g_win32_app_info_init (GWin32AppInfo *local)
99 static GAppInfo *
100 g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
101 gboolean id_is_exename)
103 #ifdef AssocQueryString
104 ASSOCF flags;
105 #endif
106 wchar_t buffer[1024];
107 DWORD buffer_size;
108 GWin32AppInfo *info;
109 HKEY app_key;
111 info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
112 info->id = id; /* Takes ownership */
113 info->id_utf8 = g_utf16_to_utf8 (id, -1, NULL, NULL, NULL);
114 info->id_is_exename = id_is_exename;
116 #ifdef AssocQueryString
117 flags = 0;
118 if (id_is_exename)
119 flags |= ASSOCF_INIT_BYEXENAME;
121 buffer_size = 1024;
122 if (AssocQueryStringW(flags,
123 REAL_ASSOCSTR_EXECUTABLE,
125 NULL,
126 buffer,
127 &buffer_size) == S_OK)
128 info->executable = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
130 buffer_size = 1024;
131 if (AssocQueryStringW(flags,
132 REAL_ASSOCSTR_FRIENDLYAPPNAME,
134 NULL,
135 buffer,
136 &buffer_size) == S_OK)
137 info->name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
138 #endif
140 if (info->name == NULL)
142 /* TODO: Should look up name from executable resources */
143 if (info->executable)
144 info->name = g_path_get_basename (info->executable);
145 else
146 info->name = g_strdup (info->id_utf8);
149 #ifdef AssocQueryString
150 if (AssocQueryKeyW(flags,
151 ASSOCKEY_APP,
152 info->id,
153 NULL,
154 &app_key) == S_OK)
156 if (RegQueryValueExW (app_key, L"NoOpenWith", 0,
157 NULL, NULL, NULL) == ERROR_SUCCESS)
158 info->no_open_with = TRUE;
159 RegCloseKey (app_key);
161 #endif
163 return G_APP_INFO (info);
166 static wchar_t *
167 dup_wstring (wchar_t *str)
169 gsize len;
170 for (len = 0; str[len] != 0; len++)
172 return (wchar_t *)g_memdup (str, (len + 1) * 2);
175 static GAppInfo *
176 g_win32_app_info_dup (GAppInfo *appinfo)
178 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
179 GWin32AppInfo *new_info;
181 new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
183 new_info->id = dup_wstring (info->id);
184 new_info->id_utf8 = g_strdup (info->id_utf8);
185 new_info->id_is_exename = info->id_is_exename;
186 new_info->name = g_strdup (info->name);
187 new_info->executable = g_strdup (info->executable);
188 new_info->no_open_with = info->no_open_with;
190 return G_APP_INFO (new_info);
193 static gboolean
194 g_win32_app_info_equal (GAppInfo *appinfo1,
195 GAppInfo *appinfo2)
197 GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
198 GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
200 if (info1->executable == NULL ||
201 info2->executable == NULL)
202 return FALSE;
204 return strcmp (info1->executable, info2->executable) == 0;
207 static const char *
208 g_win32_app_info_get_id (GAppInfo *appinfo)
210 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
212 return info->id_utf8;
215 static const char *
216 g_win32_app_info_get_name (GAppInfo *appinfo)
218 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
220 if (info->name == NULL)
221 return _("Unnamed");
223 return info->name;
226 static const char *
227 g_win32_app_info_get_description (GAppInfo *appinfo)
229 /* Win32 has no app descriptions */
230 return NULL;
233 static const char *
234 g_win32_app_info_get_executable (GAppInfo *appinfo)
236 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
238 return info->executable;
241 static GIcon *
242 g_win32_app_info_get_icon (GAppInfo *appinfo)
244 /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
246 /* TODO: How to handle icons */
247 return NULL;
250 static gboolean
251 g_win32_app_info_launch (GAppInfo *appinfo,
252 GList *files,
253 GAppLaunchContext *launch_context,
254 GError **error)
256 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
257 #ifdef AssocQueryString
258 ASSOCF flags;
259 #endif
260 HKEY class_key;
261 SHELLEXECUTEINFOW exec_info = {0};
262 GList *l;
264 /* TODO: What might startup_id mean on win32? */
265 #ifdef AssocQueryString
266 flags = 0;
267 if (info->id_is_exename)
268 flags |= ASSOCF_INIT_BYEXENAME;
270 if (AssocQueryKeyW (flags,
271 ASSOCKEY_SHELLEXECCLASS,
272 info->id,
273 NULL,
274 &class_key) != S_OK)
276 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
277 return FALSE;
279 #endif
281 /* FIXME: Need to do something with
282 * g_app_launch_context_get_environment()... ShellExecuteExW()
283 * doesn't have any way to pass an environment though. We need to
284 * either (a) update environment, ShellExecuteExW(), revert
285 * environment; or (b) find an API to figure out what app
286 * ShellExecuteExW() would launch, and then use g_spawn_async()
287 * instead.
290 for (l = files; l != NULL; l = l->next)
292 char *path = g_file_get_path (l->data);
293 wchar_t *wfilename = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
295 g_free (path);
297 memset (&exec_info, 0, sizeof (exec_info));
298 exec_info.cbSize = sizeof (exec_info);
299 exec_info.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_CLASSKEY;
300 exec_info.lpFile = wfilename;
301 exec_info.nShow = SW_SHOWNORMAL;
302 exec_info.hkeyClass = class_key;
304 if (!ShellExecuteExW (&exec_info))
306 char *message_utf8 = g_win32_error_message (GetLastError ());
308 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Error launching application: %s"), message_utf8);
309 g_free (message_utf8);
311 g_free (wfilename);
312 RegCloseKey (class_key);
313 return FALSE;
316 g_free (wfilename);
319 RegCloseKey (class_key);
321 return TRUE;
324 static gboolean
325 g_win32_app_info_supports_uris (GAppInfo *appinfo)
327 return FALSE;
330 static gboolean
331 g_win32_app_info_supports_files (GAppInfo *appinfo)
333 return TRUE;
336 static gboolean
337 g_win32_app_info_launch_uris (GAppInfo *appinfo,
338 GList *uris,
339 GAppLaunchContext *launch_context,
340 GError **error)
342 g_set_error_literal (error, G_IO_ERROR,
343 G_IO_ERROR_NOT_SUPPORTED,
344 _("URIs not supported"));
345 return FALSE;
348 static gboolean
349 g_win32_app_info_should_show (GAppInfo *appinfo)
351 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
353 if (info->no_open_with)
354 return FALSE;
356 return TRUE;
359 static gboolean
360 g_win32_app_info_set_as_default_for_type (GAppInfo *appinfo,
361 const char *content_type,
362 GError **error)
364 g_set_error_literal (error, G_IO_ERROR,
365 G_IO_ERROR_NOT_SUPPORTED,
366 _("association changes not supported on win32"));
367 return FALSE;
370 GAppInfo *
371 g_app_info_create_from_commandline (const char *commandline,
372 const char *application_name,
373 GAppInfoCreateFlags flags,
374 GError **error)
376 g_set_error_literal (error, G_IO_ERROR,
377 G_IO_ERROR_NOT_SUPPORTED,
378 _("Association creation not supported on win32"));
379 return NULL;
383 static void
384 g_win32_app_info_iface_init (GAppInfoIface *iface)
386 iface->dup = g_win32_app_info_dup;
387 iface->equal = g_win32_app_info_equal;
388 iface->get_id = g_win32_app_info_get_id;
389 iface->get_name = g_win32_app_info_get_name;
390 iface->get_description = g_win32_app_info_get_description;
391 iface->get_executable = g_win32_app_info_get_executable;
392 iface->get_icon = g_win32_app_info_get_icon;
393 iface->launch = g_win32_app_info_launch;
394 iface->supports_uris = g_win32_app_info_supports_uris;
395 iface->supports_files = g_win32_app_info_supports_files;
396 iface->launch_uris = g_win32_app_info_launch_uris;
397 iface->should_show = g_win32_app_info_should_show;
398 iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
401 static void
402 enumerate_open_with_list (HKEY dir_key,
403 GList **prognames)
405 DWORD index;
406 wchar_t name[256];
407 DWORD name_len, nbytes;
408 wchar_t data[256];
409 wchar_t *data_alloc;
410 DWORD type;
412 /* Must also look inside for a,b,c, + MRUList */
413 index = 0;
414 name_len = 256;
415 nbytes = sizeof (data) - 2;
416 while (RegEnumValueW (dir_key,
417 index,
418 name,
419 &name_len,
421 &type,
422 (LPBYTE)data,
423 &nbytes) == ERROR_SUCCESS)
425 data[nbytes/2] = '\0';
426 if (type == REG_SZ &&
427 /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
428 name_len == 1)
430 data_alloc = (wchar_t *)g_memdup (data, nbytes + 2);
431 data_alloc[nbytes/2] = 0;
432 *prognames = g_list_prepend (*prognames, data_alloc);
434 index++;
435 name_len = 256;
436 nbytes = sizeof (data) - 2;
439 index = 0;
440 name_len = 256;
441 while (RegEnumKeyExW (dir_key,
442 index,
443 name,
444 &name_len,
445 NULL,
446 NULL,
447 NULL,
448 NULL) == ERROR_SUCCESS)
450 *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
451 index++;
452 name_len = 256;
456 static void
457 enumerate_open_with_progids (HKEY dir_key,
458 GList **progids)
460 DWORD index;
461 wchar_t name[256];
462 DWORD name_len, type;
464 index = 0;
465 name_len = 256;
466 while (RegEnumValueW (dir_key,
467 index,
468 name,
469 &name_len,
471 &type,
472 NULL,
473 0) == ERROR_SUCCESS)
475 *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
476 index++;
477 name_len = 256;
481 static void
482 enumerate_open_with_root (HKEY dir_key,
483 GList **progids,
484 GList **prognames)
486 HKEY reg_key = NULL;
488 if (RegOpenKeyExW (dir_key, L"OpenWithList", 0,
489 KEY_READ, &reg_key) == ERROR_SUCCESS)
491 enumerate_open_with_list (reg_key, prognames);
492 RegCloseKey (reg_key);
495 if (RegOpenKeyExW (dir_key, L"OpenWithProgids", 0,
496 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
498 enumerate_open_with_progids (reg_key, progids);
499 RegCloseKey (reg_key);
503 static gboolean
504 app_info_in_list (GAppInfo *info,
505 GList *list)
507 while (list != NULL)
509 if (g_app_info_equal (info, list->data))
510 return TRUE;
511 list = list->next;
513 return FALSE;
516 GList *
517 g_app_info_get_all_for_type (const char *content_type)
519 GList *progids = NULL;
520 GList *prognames = NULL;
521 HKEY reg_key, sys_file_assoc_key, reg_key2;
522 wchar_t percieved_type[128];
523 DWORD nchars, key_type;
524 wchar_t *wc_key;
525 GList *l;
526 GList *infos;
528 wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
529 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
530 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
532 enumerate_open_with_root (reg_key, &progids, &prognames);
534 nchars = sizeof (percieved_type) / sizeof(wchar_t);
535 if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
536 &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
538 if (key_type == REG_SZ &&
539 RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
540 KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
542 if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
543 KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
545 enumerate_open_with_root (reg_key2, &progids, &prognames);
546 RegCloseKey (reg_key2);
549 RegCloseKey (sys_file_assoc_key);
552 RegCloseKey (reg_key);
555 if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
556 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
558 if (RegOpenKeyExW (reg_key, wc_key, 0,
559 KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
561 enumerate_open_with_root (reg_key2, &progids, &prognames);
562 RegCloseKey (reg_key2);
565 RegCloseKey (reg_key);
568 infos = NULL;
569 for (l = prognames; l != NULL; l = l->next)
571 GAppInfo *info;
573 /* l->data ownership is taken */
574 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
575 if (app_info_in_list (info, infos))
576 g_object_unref (info);
577 else
578 infos = g_list_prepend (infos, info);
580 g_list_free (prognames);
582 for (l = progids; l != NULL; l = l->next)
584 GAppInfo *info;
586 /* l->data ownership is taken */
587 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
588 if (app_info_in_list (info, infos))
589 g_object_unref (info);
590 else
591 infos = g_list_prepend (infos, info);
593 g_list_free (progids);
595 g_free (wc_key);
596 return g_list_reverse (infos);
599 GList *
600 g_app_info_get_recommended_for_type (const char *content_type)
602 /* FIXME: this should generate a list of applications that are registered
603 * as direct handlers for the given content type, without using MIME subclassing.
604 * See g_app_info_get_recommended_for_type() in gdesktopappinfo.c for a reference
605 * UNIX implementation.
607 return g_app_info_get_all_for_type (content_type);
610 GList *
611 g_app_info_get_fallback_for_type (const char *content_type)
613 /* FIXME: this should generate a list of applications that are registered
614 * as handlers for a superclass of the given content type, but are not
615 * direct handlers for the content type itself. See g_app_info_get_fallback_for_type()
616 * in gdesktopappinfo.c for a reference UNIX implementation.
618 return g_app_info_get_all_for_type (content_type);
621 GAppInfo *
622 g_app_info_get_default_for_type (const char *content_type,
623 gboolean must_support_uris)
625 wchar_t *wtype;
626 wchar_t buffer[1024];
627 DWORD buffer_size;
629 wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
631 /* Verify that we have some sort of app registered for this type */
632 #ifdef AssocQueryString
633 buffer_size = 1024;
634 if (AssocQueryStringW (0,
635 REAL_ASSOCSTR_COMMAND,
636 wtype,
637 NULL,
638 buffer,
639 &buffer_size) == S_OK)
640 /* Takes ownership of wtype */
641 return g_desktop_app_info_new_from_id (wtype, FALSE);
642 #endif
644 g_free (wtype);
645 return NULL;
648 GAppInfo *
649 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
651 /* TODO: Implement */
652 return NULL;
655 GList *
656 g_app_info_get_all (void)
658 DWORD index;
659 wchar_t name[256];
660 DWORD name_len;
661 HKEY reg_key;
662 GList *infos;
663 GAppInfo *info;
665 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
666 KEY_READ, &reg_key) != ERROR_SUCCESS)
667 return NULL;
669 infos = NULL;
670 index = 0;
671 name_len = 256;
672 while (RegEnumKeyExW (reg_key,
673 index,
674 name,
675 &name_len,
676 NULL,
677 NULL,
678 NULL,
679 NULL) == ERROR_SUCCESS)
681 wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
682 /* name_dup ownership is taken */
683 info = g_desktop_app_info_new_from_id (name_dup, TRUE);
684 infos = g_list_prepend (infos, info);
686 index++;
687 name_len = 256;
690 RegCloseKey (reg_key);
692 return g_list_reverse (infos);
695 void
696 g_app_info_reset_type_associations (const char *content_type)
698 /* nothing to do */