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>
36 #ifndef ASSOCF_INIT_BYEXENAME
37 #define ASSOCF_INIT_BYEXENAME 0x00000002
40 /* These were wrong in MingW */
41 #define REAL_ASSOCSTR_COMMAND 1
42 #define REAL_ASSOCSTR_EXECUTABLE 2
43 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
44 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
47 static void g_win32_app_info_iface_init (GAppInfoIface *iface);
51 GObject parent_instance;
54 gboolean id_is_exename;
57 gboolean no_open_with;
60 G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo, g_win32_app_info, G_TYPE_OBJECT,
61 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
62 g_win32_app_info_iface_init))
66 g_win32_app_info_finalize (GObject *object)
70 info = G_WIN32_APP_INFO (object);
73 g_free (info->id_utf8);
75 g_free (info->executable);
77 if (G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize)
78 (*G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize) (object);
82 g_win32_app_info_class_init (GWin32AppInfoClass *klass)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86 gobject_class->finalize = g_win32_app_info_finalize;
90 g_win32_app_info_init (GWin32AppInfo *local)
95 g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
96 gboolean id_is_exename)
104 info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
105 info->id = id; /* Takes ownership */
106 info->id_utf8 = g_utf16_to_utf8 (id, -1, NULL, NULL, NULL);
107 info->id_is_exename = id_is_exename;
111 flags |= ASSOCF_INIT_BYEXENAME;
114 if (AssocQueryStringW(flags,
115 REAL_ASSOCSTR_EXECUTABLE,
119 &buffer_size) == S_OK)
120 info->executable = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
123 if (AssocQueryStringW(flags,
124 REAL_ASSOCSTR_FRIENDLYAPPNAME,
128 &buffer_size) == S_OK)
129 info->name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
131 if (info->name == NULL)
133 /* TODO: Should look up name from executable resources */
134 if (info->executable)
135 info->name = g_path_get_basename (info->executable);
137 info->name = g_strdup (info->id_utf8);
140 if (AssocQueryKeyW(flags,
146 if (RegQueryValueExW (app_key, L"NoOpenWith", 0,
147 NULL, NULL, NULL) == ERROR_SUCCESS)
148 info->no_open_with = TRUE;
149 RegCloseKey (app_key);
152 return G_APP_INFO (info);
156 dup_wstring (wchar_t *str)
159 for (len = 0; str[len] != 0; len++)
161 return (wchar_t *)g_memdup (str, (len + 1) * 2);
165 g_win32_app_info_dup (GAppInfo *appinfo)
167 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
168 GWin32AppInfo *new_info;
170 new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
172 new_info->id = dup_wstring (info->id);
173 new_info->id_utf8 = g_strdup (info->id_utf8);
174 new_info->id_is_exename = info->id_is_exename;
175 new_info->name = g_strdup (info->name);
176 new_info->executable = g_strdup (info->executable);
177 new_info->no_open_with = info->no_open_with;
179 return G_APP_INFO (new_info);
183 g_win32_app_info_equal (GAppInfo *appinfo1,
186 GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
187 GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
189 if (info1->executable == NULL ||
190 info2->executable == NULL)
193 return strcmp (info1->executable, info2->executable) == 0;
197 g_win32_app_info_get_id (GAppInfo *appinfo)
199 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
201 return info->id_utf8;
205 g_win32_app_info_get_name (GAppInfo *appinfo)
207 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
209 if (info->name == NULL)
216 g_win32_app_info_get_description (GAppInfo *appinfo)
218 /* Win32 has no app descriptions */
223 g_win32_app_info_get_executable (GAppInfo *appinfo)
225 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
227 return info->executable;
231 g_win32_app_info_get_icon (GAppInfo *appinfo)
233 /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
235 /* TODO: How to handle icons */
240 g_win32_app_info_launch (GAppInfo *appinfo,
242 GAppLaunchContext *launch_context,
245 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
248 SHELLEXECUTEINFOW exec_info = {0};
251 /* TODO: What might startup_id mean on win32? */
254 if (info->id_is_exename)
255 flags |= ASSOCF_INIT_BYEXENAME;
257 if (AssocQueryKeyW(flags,
258 ASSOCKEY_SHELLEXECCLASS,
263 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
267 for (l = file; l != NULL; l = l->next)
269 char *path = g_file_get_path (l->data);
270 wchar_t *wfilename = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
274 memset (&exec_info, 0, sizeof (exec_info));
275 exec_info.cbSize = sizeof (exec_info);
276 exec_info.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_CLASSKEY;
277 exec_info.lpFile = wfilename;
278 exec_info.nShow = SW_SHOWNORMAL;
279 exec_info.hkeyClass = class_key;
281 if (!ShellExecuteExW(&exec_info))
287 last_error = GetLastError ();
288 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
289 FORMAT_MESSAGE_FROM_SYSTEM,
292 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
296 message_utf8 = g_utf16_to_utf8 (message, -1, NULL, NULL, NULL);
297 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Error launching application: %s"), message_utf8);
298 g_free (message_utf8);
302 RegCloseKey (class_key);
309 RegCloseKey (class_key);
315 g_win32_app_info_supports_uris (GAppInfo *appinfo)
321 g_win32_app_info_launch_uris (GAppInfo *appinfo,
323 GAppLaunchContext *launch_context,
326 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("URIs not supported"));
331 g_win32_app_info_should_show (GAppInfo *appinfo,
332 const char *win32_env)
334 GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
336 if (info->no_open_with)
343 g_win32_app_info_set_as_default_for_type (GAppInfo *appinfo,
344 const char *content_type,
347 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("association changes not supported on win32"));
352 g_app_info_create_from_commandline (const char *commandline,
353 const char *application_name,
354 GAppInfoCreateFlags flags,
357 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Association creation not supported on win32"));
363 g_win32_app_info_iface_init (GAppInfoIface *iface)
365 iface->dup = g_win32_app_info_dup;
366 iface->equal = g_win32_app_info_equal;
367 iface->get_id = g_win32_app_info_get_id;
368 iface->get_name = g_win32_app_info_get_name;
369 iface->get_description = g_win32_app_info_get_description;
370 iface->get_executable = g_win32_app_info_get_executable;
371 iface->get_icon = g_win32_app_info_get_icon;
372 iface->launch = g_win32_app_info_launch;
373 iface->supports_uris = g_win32_app_info_supports_uris;
374 iface->launch_uris = g_win32_app_info_launch_uris;
375 iface->should_show = g_win32_app_info_should_show;
376 iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
380 enumerate_open_with_list (HKEY dir_key,
385 DWORD name_len, nbytes;
390 /* Must also look inside for a,b,c, + MRUList */
393 nbytes = sizeof (data) - 2;
394 while (RegEnumValueW(dir_key,
401 &nbytes) == ERROR_SUCCESS)
403 data[nbytes/2] = '\0';
404 if (type == REG_SZ &&
405 /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
408 data_alloc = (wchar_t *)g_memdup (data, nbytes + 2);
409 data_alloc[nbytes/2] = 0;
410 *prognames = g_list_prepend (*prognames, data_alloc);
414 nbytes = sizeof (data) - 2;
419 while (RegEnumKeyExW(dir_key,
426 NULL) == ERROR_SUCCESS)
428 *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
435 enumerate_open_with_progids (HKEY dir_key,
440 DWORD name_len, type;
444 while (RegEnumValueW(dir_key,
453 *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
460 enumerate_open_with_root (HKEY dir_key,
466 if (RegOpenKeyExW (dir_key, L"OpenWithList", 0,
467 KEY_READ, ®_key) == ERROR_SUCCESS)
469 enumerate_open_with_list (reg_key, prognames);
470 RegCloseKey (reg_key);
473 if (RegOpenKeyExW (dir_key, L"OpenWithProgids", 0,
474 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
476 enumerate_open_with_progids (reg_key, progids);
477 RegCloseKey (reg_key);
482 app_info_in_list (GAppInfo *info, GList *l)
486 if (g_app_info_equal (info, l->data))
494 * g_app_info_get_all_for_type:
497 * Returns a #GList of #GAppInfo for a given @content_type.
501 g_app_info_get_all_for_type (const char *content_type)
503 GList *progids = NULL;
504 GList *prognames = NULL;
505 HKEY reg_key, sys_file_assoc_key, reg_key2;
506 wchar_t percieved_type[128];
507 DWORD nchars, key_type;
512 wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
513 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
514 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
516 enumerate_open_with_root (reg_key, &progids, &prognames);
518 nchars = sizeof (percieved_type) / sizeof(wchar_t);
519 if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
520 &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
522 if (key_type == REG_SZ &&
523 RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
524 KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
526 if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
527 KEY_QUERY_VALUE, ®_key2) == ERROR_SUCCESS)
529 enumerate_open_with_root (reg_key2, &progids, &prognames);
530 RegCloseKey (reg_key2);
533 RegCloseKey (sys_file_assoc_key);
536 RegCloseKey (reg_key);
539 if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
540 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS)
542 if (RegOpenKeyExW (reg_key, wc_key, 0,
543 KEY_QUERY_VALUE, ®_key2) == ERROR_SUCCESS)
545 enumerate_open_with_root (reg_key2, &progids, &prognames);
546 RegCloseKey (reg_key2);
549 RegCloseKey (reg_key);
553 for (l = prognames; l != NULL; l = l->next)
557 /* l->data ownership is taken */
558 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
559 if (app_info_in_list (info, infos))
560 g_object_unref (info);
562 infos = g_list_prepend (infos, info);
564 g_list_free (prognames);
566 for (l = progids; l != NULL; l = l->next)
570 /* l->data ownership is taken */
571 info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
572 if (app_info_in_list (info, infos))
573 g_object_unref (info);
575 infos = g_list_prepend (infos, info);
577 g_list_free (progids);
580 return g_list_reverse (infos);
584 * g_app_info_get_default_for_type:
586 * @must_support_uris:
588 * Returns the default #GAppInfo for the given @content_type. If
589 * @must_support_uris is true, the #GAppInfo is expected to support
594 g_app_info_get_default_for_type (const char *content_type,
595 gboolean must_support_uris)
598 wchar_t buffer[1024];
601 wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
603 /* Verify that we have some sort of app registered for this type */
605 if (AssocQueryStringW(0,
606 REAL_ASSOCSTR_COMMAND,
610 &buffer_size) == S_OK)
611 /* Takes ownership of wtype */
612 return g_desktop_app_info_new_from_id (wtype, FALSE);
619 * g_app_info_get_default_for_uri_scheme:
624 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
626 /* TODO: Implement */
631 * g_app_info_get_all:
635 g_app_info_get_all (void)
644 if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
645 KEY_READ, ®_key) != ERROR_SUCCESS)
651 while (RegEnumKeyExW(reg_key,
658 NULL) == ERROR_SUCCESS)
660 wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
661 /* name_dup ownership is taken */
662 info = g_desktop_app_info_new_from_id (name_dup, TRUE);
663 infos = g_list_prepend (infos, info);
669 RegCloseKey (reg_key);
671 return g_list_reverse (infos);