1 /* GIO - GLib Input, Output and Streaming Library
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>
27 #include "gcontenttypeprivate.h"
28 #include "gwin32appinfo.h"
30 #include <glib/gstdio.h>
38 #ifndef ASSOCF_INIT_BYEXENAME
39 #define ASSOCF_INIT_BYEXENAME 0x00000002
42 /* These were wrong in MingW */
43 #define REAL_ASSOCSTR_COMMAND 1
44 #define REAL_ASSOCSTR_EXECUTABLE 2
45 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
46 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
49 static void g_win32_app_info_iface_init (GAppInfoIface *iface);
53 GObject parent_instance;
56 gboolean id_is_exename;
59 gboolean no_open_with;
62 G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo, g_win32_app_info, G_TYPE_OBJECT,
63 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
64 g_win32_app_info_iface_init))
68 g_win32_app_info_finalize (GObject *object)
72 info = G_WIN32_APP_INFO (object);
75 g_free (info->id_utf8);
77 g_free (info->executable);
79 if (G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize)
80 (*G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize) (object);
84 g_win32_app_info_class_init (GWin32AppInfoClass *klass)
86 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88 gobject_class->finalize = g_win32_app_info_finalize;
92 g_win32_app_info_init (GWin32AppInfo *local)
97 g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
98 gboolean id_is_exename)
101 wchar_t buffer[1024];
106 info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
107 info->id = id; /* Takes ownership */
108 info->id_utf8 = g_utf16_to_utf8 (id, -1, NULL, NULL, NULL);
109 info->id_is_exename = id_is_exename;
113 flags |= ASSOCF_INIT_BYEXENAME;
116 if (AssocQueryStringW(flags,
117 REAL_ASSOCSTR_EXECUTABLE,
121 &buffer_size) == S_OK)
122 info->executable = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
125 if (AssocQueryStringW(flags,
126 REAL_ASSOCSTR_FRIENDLYAPPNAME,
130 &buffer_size) == S_OK)
131 info->name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
133 if (info->name == NULL)
135 /* TODO: Should look up name from executable resources */
136 if (info->executable)
137 info->name = g_path_get_basename (info->executable);
139 info->name = g_strdup (info->id_utf8);
142 if (AssocQueryKeyW(flags,
148 if (RegQueryValueExW (app_key, L"NoOpenWith", 0,
149 NULL, NULL, NULL) == ERROR_SUCCESS)
150 info->no_open_with = TRUE;
151 RegCloseKey (app_key);
154 return G_APP_INFO (info);
158 dup_wstring (wchar_t *str)
161 for (len = 0; str[len] != 0; len++)
163 return (wchar_t *)g_memdup (str, (len + 1) * 2);
167 g_win32_app_info_dup (GAppInfo *appinfo)
169 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
170 GWin32AppInfo *new_info;
172 new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
174 new_info->id = dup_wstring (info->id);
175 new_info->id_utf8 = g_strdup (info->id_utf8);
176 new_info->id_is_exename = info->id_is_exename;
177 new_info->name = g_strdup (info->name);
178 new_info->executable = g_strdup (info->executable);
179 new_info->no_open_with = info->no_open_with;
181 return G_APP_INFO (new_info);
185 g_win32_app_info_equal (GAppInfo *appinfo1,
188 GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
189 GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
191 if (info1->executable == NULL ||
192 info2->executable == NULL)
195 return strcmp (info1->executable, info2->executable) == 0;
199 g_win32_app_info_get_id (GAppInfo *appinfo)
201 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
203 return info->id_utf8;
207 g_win32_app_info_get_name (GAppInfo *appinfo)
209 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
211 if (info->name == NULL)
218 g_win32_app_info_get_description (GAppInfo *appinfo)
220 /* Win32 has no app descriptions */
225 g_win32_app_info_get_executable (GAppInfo *appinfo)
227 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
229 return info->executable;
233 g_win32_app_info_get_icon (GAppInfo *appinfo)
235 /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
237 /* TODO: How to handle icons */
242 g_win32_app_info_launch (GAppInfo *appinfo,
244 GAppLaunchContext *launch_context,
247 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
250 SHELLEXECUTEINFOW exec_info = {0};
253 /* TODO: What might startup_id mean on win32? */
256 if (info->id_is_exename)
257 flags |= ASSOCF_INIT_BYEXENAME;
259 if (AssocQueryKeyW (flags,
260 ASSOCKEY_SHELLEXECCLASS,
265 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
269 for (l = file; l != NULL; l = l->next)
271 char *path = g_file_get_path (l->data);
272 wchar_t *wfilename = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
276 memset (&exec_info, 0, sizeof (exec_info));
277 exec_info.cbSize = sizeof (exec_info);
278 exec_info.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_CLASSKEY;
279 exec_info.lpFile = wfilename;
280 exec_info.nShow = SW_SHOWNORMAL;
281 exec_info.hkeyClass = class_key;
283 if (!ShellExecuteExW(&exec_info))
289 last_error = GetLastError ();
290 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
291 FORMAT_MESSAGE_FROM_SYSTEM,
294 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
298 message_utf8 = g_utf16_to_utf8 (message, -1, NULL, NULL, NULL);
299 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Error launching application: %s"), message_utf8);
300 g_free (message_utf8);
304 RegCloseKey (class_key);
311 RegCloseKey (class_key);
317 g_win32_app_info_supports_uris (GAppInfo *appinfo)
323 g_win32_app_info_launch_uris (GAppInfo *appinfo,
325 GAppLaunchContext *launch_context,
328 g_set_error (error, G_IO_ERROR,
329 G_IO_ERROR_NOT_SUPPORTED,
330 _("URIs not supported"));
335 g_win32_app_info_should_show (GAppInfo *appinfo,
336 const char *win32_env)
338 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
340 if (info->no_open_with)
347 g_win32_app_info_set_as_default_for_type (GAppInfo *appinfo,
348 const char *content_type,
351 g_set_error (error, G_IO_ERROR,
352 G_IO_ERROR_NOT_SUPPORTED,
353 _("association changes not supported on win32"));
358 g_app_info_create_from_commandline (const char *commandline,
359 const char *application_name,
360 GAppInfoCreateFlags flags,
363 g_set_error (error, G_IO_ERROR,
364 G_IO_ERROR_NOT_SUPPORTED,
365 _("Association creation not supported on win32"));
371 g_win32_app_info_iface_init (GAppInfoIface *iface)
373 iface->dup = g_win32_app_info_dup;
374 iface->equal = g_win32_app_info_equal;
375 iface->get_id = g_win32_app_info_get_id;
376 iface->get_name = g_win32_app_info_get_name;
377 iface->get_description = g_win32_app_info_get_description;
378 iface->get_executable = g_win32_app_info_get_executable;
379 iface->get_icon = g_win32_app_info_get_icon;
380 iface->launch = g_win32_app_info_launch;
381 iface->supports_uris = g_win32_app_info_supports_uris;
382 iface->launch_uris = g_win32_app_info_launch_uris;
383 iface->should_show = g_win32_app_info_should_show;
384 iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
388 enumerate_open_with_list (HKEY dir_key,
393 DWORD name_len, nbytes;
398 /* Must also look inside for a,b,c, + MRUList */
401 nbytes = sizeof (data) - 2;
402 while (RegEnumValueW (dir_key,
409 &nbytes) == ERROR_SUCCESS)
411 data[nbytes/2] = '\0';
412 if (type == REG_SZ &&
413 /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
416 data_alloc = (wchar_t *)g_memdup (data, nbytes + 2);
417 data_alloc[nbytes/2] = 0;
418 *prognames = g_list_prepend (*prognames, data_alloc);
422 nbytes = sizeof (data) - 2;
427 while (RegEnumKeyExW (dir_key,
434 NULL) == ERROR_SUCCESS)
436 *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
443 enumerate_open_with_progids (HKEY dir_key,
448 DWORD name_len, type;
452 while (RegEnumValueW (dir_key,
461 *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
468 enumerate_open_with_root (HKEY dir_key,
474 if (RegOpenKeyExW (dir_key, L"OpenWithList", 0,
475 KEY_READ, ®_key) == ERROR_SUCCESS)
477 enumerate_open_with_list (reg_key, prognames);
478 RegCloseKey (reg_key);
481 if (RegOpenKeyExW (dir_key, L"OpenWithProgids", 0,
482 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
484 enumerate_open_with_progids (reg_key, progids);
485 RegCloseKey (reg_key);
490 app_info_in_list (GAppInfo *info,
495 if (g_app_info_equal (info, list->data))
503 g_app_info_get_all_for_type (const char *content_type)
505 GList *progids = NULL;
506 GList *prognames = NULL;
507 HKEY reg_key, sys_file_assoc_key, reg_key2;
508 wchar_t percieved_type[128];
509 DWORD nchars, key_type;
514 wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
515 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
516 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
518 enumerate_open_with_root (reg_key, &progids, &prognames);
520 nchars = sizeof (percieved_type) / sizeof(wchar_t);
521 if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
522 &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
524 if (key_type == REG_SZ &&
525 RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
526 KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
528 if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
529 KEY_QUERY_VALUE, ®_key2) == ERROR_SUCCESS)
531 enumerate_open_with_root (reg_key2, &progids, &prognames);
532 RegCloseKey (reg_key2);
535 RegCloseKey (sys_file_assoc_key);
538 RegCloseKey (reg_key);
541 if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
542 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
544 if (RegOpenKeyExW (reg_key, wc_key, 0,
545 KEY_QUERY_VALUE, ®_key2) == ERROR_SUCCESS)
547 enumerate_open_with_root (reg_key2, &progids, &prognames);
548 RegCloseKey (reg_key2);
551 RegCloseKey (reg_key);
555 for (l = prognames; l != NULL; l = l->next)
559 /* l->data ownership is taken */
560 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
561 if (app_info_in_list (info, infos))
562 g_object_unref (info);
564 infos = g_list_prepend (infos, info);
566 g_list_free (prognames);
568 for (l = progids; l != NULL; l = l->next)
572 /* l->data ownership is taken */
573 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
574 if (app_info_in_list (info, infos))
575 g_object_unref (info);
577 infos = g_list_prepend (infos, info);
579 g_list_free (progids);
582 return g_list_reverse (infos);
586 g_app_info_get_default_for_type (const char *content_type,
587 gboolean must_support_uris)
590 wchar_t buffer[1024];
593 wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
595 /* Verify that we have some sort of app registered for this type */
597 if (AssocQueryStringW (0,
598 REAL_ASSOCSTR_COMMAND,
602 &buffer_size) == S_OK)
603 /* Takes ownership of wtype */
604 return g_desktop_app_info_new_from_id (wtype, FALSE);
611 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
613 /* TODO: Implement */
618 g_app_info_get_all (void)
627 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
628 KEY_READ, ®_key) != ERROR_SUCCESS)
634 while (RegEnumKeyExW (reg_key,
641 NULL) == ERROR_SUCCESS)
643 wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
644 /* name_dup ownership is taken */
645 info = g_desktop_app_info_new_from_id (name_dup, TRUE);
646 infos = g_list_prepend (infos, info);
652 RegCloseKey (reg_key);
654 return g_list_reverse (infos);