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, G_IO_ERROR_NOT_SUPPORTED, _("URIs not supported"));
333 g_win32_app_info_should_show (GAppInfo *appinfo,
334 const char *win32_env)
336 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
338 if (info->no_open_with)
345 g_win32_app_info_set_as_default_for_type (GAppInfo *appinfo,
346 const char *content_type,
349 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("association changes not supported on win32"));
354 g_app_info_create_from_commandline (const char *commandline,
355 const char *application_name,
356 GAppInfoCreateFlags flags,
359 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Association creation not supported on win32"));
365 g_win32_app_info_iface_init (GAppInfoIface *iface)
367 iface->dup = g_win32_app_info_dup;
368 iface->equal = g_win32_app_info_equal;
369 iface->get_id = g_win32_app_info_get_id;
370 iface->get_name = g_win32_app_info_get_name;
371 iface->get_description = g_win32_app_info_get_description;
372 iface->get_executable = g_win32_app_info_get_executable;
373 iface->get_icon = g_win32_app_info_get_icon;
374 iface->launch = g_win32_app_info_launch;
375 iface->supports_uris = g_win32_app_info_supports_uris;
376 iface->launch_uris = g_win32_app_info_launch_uris;
377 iface->should_show = g_win32_app_info_should_show;
378 iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
382 enumerate_open_with_list (HKEY dir_key,
387 DWORD name_len, nbytes;
392 /* Must also look inside for a,b,c, + MRUList */
395 nbytes = sizeof (data) - 2;
396 while (RegEnumValueW(dir_key,
403 &nbytes) == ERROR_SUCCESS)
405 data[nbytes/2] = '\0';
406 if (type == REG_SZ &&
407 /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
410 data_alloc = (wchar_t *)g_memdup (data, nbytes + 2);
411 data_alloc[nbytes/2] = 0;
412 *prognames = g_list_prepend (*prognames, data_alloc);
416 nbytes = sizeof (data) - 2;
421 while (RegEnumKeyExW(dir_key,
428 NULL) == ERROR_SUCCESS)
430 *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
437 enumerate_open_with_progids (HKEY dir_key,
442 DWORD name_len, type;
446 while (RegEnumValueW(dir_key,
455 *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
462 enumerate_open_with_root (HKEY dir_key,
468 if (RegOpenKeyExW (dir_key, L"OpenWithList", 0,
469 KEY_READ, ®_key) == ERROR_SUCCESS)
471 enumerate_open_with_list (reg_key, prognames);
472 RegCloseKey (reg_key);
475 if (RegOpenKeyExW (dir_key, L"OpenWithProgids", 0,
476 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
478 enumerate_open_with_progids (reg_key, progids);
479 RegCloseKey (reg_key);
484 app_info_in_list (GAppInfo *info, GList *l)
488 if (g_app_info_equal (info, l->data))
496 g_app_info_get_all_for_type (const char *content_type)
498 GList *progids = NULL;
499 GList *prognames = NULL;
500 HKEY reg_key, sys_file_assoc_key, reg_key2;
501 wchar_t percieved_type[128];
502 DWORD nchars, key_type;
507 wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
508 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
509 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
511 enumerate_open_with_root (reg_key, &progids, &prognames);
513 nchars = sizeof (percieved_type) / sizeof(wchar_t);
514 if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
515 &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
517 if (key_type == REG_SZ &&
518 RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
519 KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
521 if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
522 KEY_QUERY_VALUE, ®_key2) == ERROR_SUCCESS)
524 enumerate_open_with_root (reg_key2, &progids, &prognames);
525 RegCloseKey (reg_key2);
528 RegCloseKey (sys_file_assoc_key);
531 RegCloseKey (reg_key);
534 if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
535 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
537 if (RegOpenKeyExW (reg_key, wc_key, 0,
538 KEY_QUERY_VALUE, ®_key2) == ERROR_SUCCESS)
540 enumerate_open_with_root (reg_key2, &progids, &prognames);
541 RegCloseKey (reg_key2);
544 RegCloseKey (reg_key);
548 for (l = prognames; l != NULL; l = l->next)
552 /* l->data ownership is taken */
553 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
554 if (app_info_in_list (info, infos))
555 g_object_unref (info);
557 infos = g_list_prepend (infos, info);
559 g_list_free (prognames);
561 for (l = progids; l != NULL; l = l->next)
565 /* l->data ownership is taken */
566 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
567 if (app_info_in_list (info, infos))
568 g_object_unref (info);
570 infos = g_list_prepend (infos, info);
572 g_list_free (progids);
575 return g_list_reverse (infos);
579 g_app_info_get_default_for_type (const char *content_type,
580 gboolean must_support_uris)
583 wchar_t buffer[1024];
586 wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
588 /* Verify that we have some sort of app registered for this type */
590 if (AssocQueryStringW(0,
591 REAL_ASSOCSTR_COMMAND,
595 &buffer_size) == S_OK)
596 /* Takes ownership of wtype */
597 return g_desktop_app_info_new_from_id (wtype, FALSE);
604 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
606 /* TODO: Implement */
611 g_app_info_get_all (void)
620 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
621 KEY_READ, ®_key) != ERROR_SUCCESS)
627 while (RegEnumKeyExW(reg_key,
634 NULL) == ERROR_SUCCESS)
636 wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
637 /* name_dup ownership is taken */
638 info = g_desktop_app_info_new_from_id (name_dup, TRUE);
639 infos = g_list_prepend (infos, info);
645 RegCloseKey (reg_key);
647 return g_list_reverse (infos);