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"
32 #include <glib/gstdio.h>
40 #ifndef ASSOCF_INIT_BYEXENAME
41 #define ASSOCF_INIT_BYEXENAME 0x00000002
44 /* These were wrong in MingW */
45 #define REAL_ASSOCSTR_COMMAND 1
46 #define REAL_ASSOCSTR_EXECUTABLE 2
47 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
48 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
50 #ifndef AssocQueryString
51 #pragma message("AssocQueryString not available with SDK used")
54 static void g_win32_app_info_iface_init (GAppInfoIface
*iface
);
58 GObject parent_instance
;
61 gboolean id_is_exename
;
64 gboolean no_open_with
;
67 G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo
, g_win32_app_info
, G_TYPE_OBJECT
,
68 G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO
,
69 g_win32_app_info_iface_init
))
73 g_win32_app_info_finalize (GObject
*object
)
77 info
= G_WIN32_APP_INFO (object
);
80 g_free (info
->id_utf8
);
82 g_free (info
->executable
);
84 G_OBJECT_CLASS (g_win32_app_info_parent_class
)->finalize (object
);
88 g_win32_app_info_class_init (GWin32AppInfoClass
*klass
)
90 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
92 gobject_class
->finalize
= g_win32_app_info_finalize
;
96 g_win32_app_info_init (GWin32AppInfo
*local
)
101 g_desktop_app_info_new_from_id (wchar_t *id
/* takes ownership */,
102 gboolean id_is_exename
)
104 #ifdef AssocQueryString
107 wchar_t buffer
[1024];
112 info
= g_object_new (G_TYPE_WIN32_APP_INFO
, NULL
);
113 info
->id
= id
; /* Takes ownership */
114 info
->id_utf8
= g_utf16_to_utf8 (id
, -1, NULL
, NULL
, NULL
);
115 info
->id_is_exename
= id_is_exename
;
117 #ifdef AssocQueryString
120 flags
|= ASSOCF_INIT_BYEXENAME
;
123 if (AssocQueryStringW(flags
,
124 REAL_ASSOCSTR_EXECUTABLE
,
128 &buffer_size
) == S_OK
)
129 info
->executable
= g_utf16_to_utf8 (buffer
, -1, NULL
, NULL
, NULL
);
132 if (AssocQueryStringW(flags
,
133 REAL_ASSOCSTR_FRIENDLYAPPNAME
,
137 &buffer_size
) == S_OK
)
138 info
->name
= g_utf16_to_utf8 (buffer
, -1, NULL
, NULL
, NULL
);
141 if (info
->name
== NULL
)
143 /* TODO: Should look up name from executable resources */
144 if (info
->executable
)
145 info
->name
= g_path_get_basename (info
->executable
);
147 info
->name
= g_strdup (info
->id_utf8
);
150 #ifdef AssocQueryString
151 if (AssocQueryKeyW(flags
,
157 if (RegQueryValueExW (app_key
, L
"NoOpenWith", 0,
158 NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
159 info
->no_open_with
= TRUE
;
160 RegCloseKey (app_key
);
164 return G_APP_INFO (info
);
168 dup_wstring (wchar_t *str
)
171 for (len
= 0; str
[len
] != 0; len
++)
173 return (wchar_t *)g_memdup (str
, (len
+ 1) * 2);
177 g_win32_app_info_dup (GAppInfo
*appinfo
)
179 GWin32AppInfo
*info
= G_WIN32_APP_INFO (appinfo
);
180 GWin32AppInfo
*new_info
;
182 new_info
= g_object_new (G_TYPE_WIN32_APP_INFO
, NULL
);
184 new_info
->id
= dup_wstring (info
->id
);
185 new_info
->id_utf8
= g_strdup (info
->id_utf8
);
186 new_info
->id_is_exename
= info
->id_is_exename
;
187 new_info
->name
= g_strdup (info
->name
);
188 new_info
->executable
= g_strdup (info
->executable
);
189 new_info
->no_open_with
= info
->no_open_with
;
191 return G_APP_INFO (new_info
);
195 g_win32_app_info_equal (GAppInfo
*appinfo1
,
198 GWin32AppInfo
*info1
= G_WIN32_APP_INFO (appinfo1
);
199 GWin32AppInfo
*info2
= G_WIN32_APP_INFO (appinfo2
);
201 if (info1
->executable
== NULL
||
202 info2
->executable
== NULL
)
205 return strcmp (info1
->executable
, info2
->executable
) == 0;
209 g_win32_app_info_get_id (GAppInfo
*appinfo
)
211 GWin32AppInfo
*info
= G_WIN32_APP_INFO (appinfo
);
213 return info
->id_utf8
;
217 g_win32_app_info_get_name (GAppInfo
*appinfo
)
219 GWin32AppInfo
*info
= G_WIN32_APP_INFO (appinfo
);
221 if (info
->name
== NULL
)
228 g_win32_app_info_get_description (GAppInfo
*appinfo
)
230 /* Win32 has no app descriptions */
235 g_win32_app_info_get_executable (GAppInfo
*appinfo
)
237 GWin32AppInfo
*info
= G_WIN32_APP_INFO (appinfo
);
239 return info
->executable
;
243 g_win32_app_info_get_icon (GAppInfo
*appinfo
)
245 /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
247 /* TODO: How to handle icons */
252 g_win32_app_info_launch (GAppInfo
*appinfo
,
254 GAppLaunchContext
*launch_context
,
257 GWin32AppInfo
*info
= G_WIN32_APP_INFO (appinfo
);
258 #ifdef AssocQueryString
262 SHELLEXECUTEINFOW exec_info
= {0};
265 /* TODO: What might startup_id mean on win32? */
266 #ifdef AssocQueryString
268 if (info
->id_is_exename
)
269 flags
|= ASSOCF_INIT_BYEXENAME
;
271 if (AssocQueryKeyW (flags
,
272 ASSOCKEY_SHELLEXECCLASS
,
277 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
, _("Can't find application"));
282 for (l
= files
; l
!= NULL
; l
= l
->next
)
284 char *path
= g_file_get_path (l
->data
);
285 wchar_t *wfilename
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, NULL
);
289 memset (&exec_info
, 0, sizeof (exec_info
));
290 exec_info
.cbSize
= sizeof (exec_info
);
291 exec_info
.fMask
= SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_CLASSKEY
;
292 exec_info
.lpFile
= wfilename
;
293 exec_info
.nShow
= SW_SHOWNORMAL
;
294 exec_info
.hkeyClass
= class_key
;
296 if (!ShellExecuteExW(&exec_info
))
302 last_error
= GetLastError ();
303 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
|
304 FORMAT_MESSAGE_FROM_SYSTEM
,
307 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
311 message_utf8
= g_utf16_to_utf8 (message
, -1, NULL
, NULL
, NULL
);
312 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
, _("Error launching application: %s"), message_utf8
);
313 g_free (message_utf8
);
317 RegCloseKey (class_key
);
324 RegCloseKey (class_key
);
330 g_win32_app_info_supports_uris (GAppInfo
*appinfo
)
336 g_win32_app_info_supports_files (GAppInfo
*appinfo
)
342 g_win32_app_info_launch_uris (GAppInfo
*appinfo
,
344 GAppLaunchContext
*launch_context
,
347 g_set_error_literal (error
, G_IO_ERROR
,
348 G_IO_ERROR_NOT_SUPPORTED
,
349 _("URIs not supported"));
354 g_win32_app_info_should_show (GAppInfo
*appinfo
)
356 GWin32AppInfo
*info
= G_WIN32_APP_INFO (appinfo
);
358 if (info
->no_open_with
)
365 g_win32_app_info_set_as_default_for_type (GAppInfo
*appinfo
,
366 const char *content_type
,
369 g_set_error_literal (error
, G_IO_ERROR
,
370 G_IO_ERROR_NOT_SUPPORTED
,
371 _("association changes not supported on win32"));
376 g_app_info_create_from_commandline (const char *commandline
,
377 const char *application_name
,
378 GAppInfoCreateFlags flags
,
381 g_set_error_literal (error
, G_IO_ERROR
,
382 G_IO_ERROR_NOT_SUPPORTED
,
383 _("Association creation not supported on win32"));
389 g_win32_app_info_iface_init (GAppInfoIface
*iface
)
391 iface
->dup
= g_win32_app_info_dup
;
392 iface
->equal
= g_win32_app_info_equal
;
393 iface
->get_id
= g_win32_app_info_get_id
;
394 iface
->get_name
= g_win32_app_info_get_name
;
395 iface
->get_description
= g_win32_app_info_get_description
;
396 iface
->get_executable
= g_win32_app_info_get_executable
;
397 iface
->get_icon
= g_win32_app_info_get_icon
;
398 iface
->launch
= g_win32_app_info_launch
;
399 iface
->supports_uris
= g_win32_app_info_supports_uris
;
400 iface
->supports_files
= g_win32_app_info_supports_files
;
401 iface
->launch_uris
= g_win32_app_info_launch_uris
;
402 iface
->should_show
= g_win32_app_info_should_show
;
403 iface
->set_as_default_for_type
= g_win32_app_info_set_as_default_for_type
;
407 enumerate_open_with_list (HKEY dir_key
,
412 DWORD name_len
, nbytes
;
417 /* Must also look inside for a,b,c, + MRUList */
420 nbytes
= sizeof (data
) - 2;
421 while (RegEnumValueW (dir_key
,
428 &nbytes
) == ERROR_SUCCESS
)
430 data
[nbytes
/2] = '\0';
431 if (type
== REG_SZ
&&
432 /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
435 data_alloc
= (wchar_t *)g_memdup (data
, nbytes
+ 2);
436 data_alloc
[nbytes
/2] = 0;
437 *prognames
= g_list_prepend (*prognames
, data_alloc
);
441 nbytes
= sizeof (data
) - 2;
446 while (RegEnumKeyExW (dir_key
,
453 NULL
) == ERROR_SUCCESS
)
455 *prognames
= g_list_prepend (*prognames
, g_memdup (name
, (name_len
+ 1) * 2));
462 enumerate_open_with_progids (HKEY dir_key
,
467 DWORD name_len
, type
;
471 while (RegEnumValueW (dir_key
,
480 *progids
= g_list_prepend (*progids
, g_memdup (name
, (name_len
+ 1) * 2));
487 enumerate_open_with_root (HKEY dir_key
,
493 if (RegOpenKeyExW (dir_key
, L
"OpenWithList", 0,
494 KEY_READ
, ®_key
) == ERROR_SUCCESS
)
496 enumerate_open_with_list (reg_key
, prognames
);
497 RegCloseKey (reg_key
);
500 if (RegOpenKeyExW (dir_key
, L
"OpenWithProgids", 0,
501 KEY_QUERY_VALUE
, ®_key
) == ERROR_SUCCESS
)
503 enumerate_open_with_progids (reg_key
, progids
);
504 RegCloseKey (reg_key
);
509 app_info_in_list (GAppInfo
*info
,
514 if (g_app_info_equal (info
, list
->data
))
522 g_app_info_get_all_for_type (const char *content_type
)
524 GList
*progids
= NULL
;
525 GList
*prognames
= NULL
;
526 HKEY reg_key
, sys_file_assoc_key
, reg_key2
;
527 wchar_t percieved_type
[128];
528 DWORD nchars
, key_type
;
533 wc_key
= g_utf8_to_utf16 (content_type
, -1, NULL
, NULL
, NULL
);
534 if (RegOpenKeyExW (HKEY_CLASSES_ROOT
, wc_key
, 0,
535 KEY_QUERY_VALUE
, ®_key
) == ERROR_SUCCESS
)
537 enumerate_open_with_root (reg_key
, &progids
, &prognames
);
539 nchars
= sizeof (percieved_type
) / sizeof(wchar_t);
540 if (RegQueryValueExW (reg_key
, L
"PerceivedType", 0,
541 &key_type
, (LPBYTE
) percieved_type
, &nchars
) == ERROR_SUCCESS
)
543 if (key_type
== REG_SZ
&&
544 RegOpenKeyExW (HKEY_CLASSES_ROOT
, L
"SystemFileAssociations", 0,
545 KEY_QUERY_VALUE
, &sys_file_assoc_key
) == ERROR_SUCCESS
)
547 if (RegOpenKeyExW (sys_file_assoc_key
, percieved_type
, 0,
548 KEY_QUERY_VALUE
, ®_key2
) == ERROR_SUCCESS
)
550 enumerate_open_with_root (reg_key2
, &progids
, &prognames
);
551 RegCloseKey (reg_key2
);
554 RegCloseKey (sys_file_assoc_key
);
557 RegCloseKey (reg_key
);
560 if (RegOpenKeyExW (HKEY_CURRENT_USER
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
561 KEY_QUERY_VALUE
, ®_key
) == ERROR_SUCCESS
)
563 if (RegOpenKeyExW (reg_key
, wc_key
, 0,
564 KEY_QUERY_VALUE
, ®_key2
) == ERROR_SUCCESS
)
566 enumerate_open_with_root (reg_key2
, &progids
, &prognames
);
567 RegCloseKey (reg_key2
);
570 RegCloseKey (reg_key
);
574 for (l
= prognames
; l
!= NULL
; l
= l
->next
)
578 /* l->data ownership is taken */
579 info
= g_desktop_app_info_new_from_id ((wchar_t *)l
->data
, TRUE
);
580 if (app_info_in_list (info
, infos
))
581 g_object_unref (info
);
583 infos
= g_list_prepend (infos
, info
);
585 g_list_free (prognames
);
587 for (l
= progids
; l
!= NULL
; l
= l
->next
)
591 /* l->data ownership is taken */
592 info
= g_desktop_app_info_new_from_id ((wchar_t *)l
->data
, FALSE
);
593 if (app_info_in_list (info
, infos
))
594 g_object_unref (info
);
596 infos
= g_list_prepend (infos
, info
);
598 g_list_free (progids
);
601 return g_list_reverse (infos
);
605 g_app_info_get_default_for_type (const char *content_type
,
606 gboolean must_support_uris
)
609 wchar_t buffer
[1024];
612 wtype
= g_utf8_to_utf16 (content_type
, -1, NULL
, NULL
, NULL
);
614 /* Verify that we have some sort of app registered for this type */
615 #ifdef AssocQueryString
617 if (AssocQueryStringW (0,
618 REAL_ASSOCSTR_COMMAND
,
622 &buffer_size
) == S_OK
)
623 /* Takes ownership of wtype */
624 return g_desktop_app_info_new_from_id (wtype
, FALSE
);
632 g_app_info_get_default_for_uri_scheme (const char *uri_scheme
)
634 /* TODO: Implement */
639 g_app_info_get_all (void)
648 if (RegOpenKeyExW (HKEY_CLASSES_ROOT
, L
"Applications", 0,
649 KEY_READ
, ®_key
) != ERROR_SUCCESS
)
655 while (RegEnumKeyExW (reg_key
,
662 NULL
) == ERROR_SUCCESS
)
664 wchar_t *name_dup
= g_memdup (name
, (name_len
+1)*2);
665 /* name_dup ownership is taken */
666 info
= g_desktop_app_info_new_from_id (name_dup
, TRUE
);
667 infos
= g_list_prepend (infos
, info
);
673 RegCloseKey (reg_key
);
675 return g_list_reverse (infos
);