2 * Copyright 2006-2010 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
31 #define NONAMELESSUNION
49 #include "wine/debug.h"
50 #include "wine/library.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
54 #define GECKO_VERSION "2.47"
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "f9a937e9a46d47fda701d257e60601f22e7a4510"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "8efa810b1ac83d59e0171d4347d21730560926da"
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
67 #define MONO_VERSION "4.7.0"
68 #define MONO_SHA "ff05e1d2a93c3a07672cefc7a8f3a087d75828ac"
72 const char *file_name
;
73 const char *subdir_name
;
75 const char *url_default
;
76 const char *config_key
;
77 const char *url_config_key
;
78 const char *dir_config_key
;
79 LPCWSTR dialog_template
;
82 static const addon_info_t addons_info
[] = {
85 "wine_gecko-" GECKO_VERSION
"-" ARCH_STRING
".msi",
88 "http://source.winehq.org/winegecko.php",
89 "MSHTML", "GeckoUrl", "GeckoCabDir",
90 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG
)
94 "wine-mono-" MONO_VERSION
".msi",
97 "http://source.winehq.org/winemono.php",
98 "Dotnet", "MonoUrl", "MonoCabDir",
99 MAKEINTRESOURCEW(ID_DWL_MONO_DIALOG
)
103 static const addon_info_t
*addon
;
105 static HWND install_dialog
= NULL
;
106 static LPWSTR url
= NULL
;
107 static IBinding
*dwl_binding
;
108 static WCHAR
*msi_file
;
110 static WCHAR
* (CDECL
*p_wine_get_dos_file_name
)(const char*);
111 static const WCHAR kernel32_dllW
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
114 /* SHA definitions are copied from advapi32. They aren't available in headers. */
121 } SHA_CTX
, *PSHA_CTX
;
123 void WINAPI
A_SHAInit(PSHA_CTX
);
124 void WINAPI
A_SHAUpdate(PSHA_CTX
,const unsigned char*,UINT
);
125 void WINAPI
A_SHAFinal(PSHA_CTX
,PULONG
);
127 static BOOL
sha_check(const WCHAR
*file_name
)
129 const unsigned char *file_map
;
132 char buf
[2*sizeof(sha
)+1];
136 file
= CreateFileW(file_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
137 if(file
== INVALID_HANDLE_VALUE
) {
138 WARN("Could not open file: %u\n", GetLastError());
142 size
= GetFileSize(file
, NULL
);
144 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
149 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
155 A_SHAUpdate(&ctx
, file_map
, size
);
156 A_SHAFinal(&ctx
, sha
);
158 UnmapViewOfFile(file_map
);
160 for(i
=0; i
< sizeof(sha
); i
++)
161 sprintf(buf
+ i
*2, "%02x", *((unsigned char*)sha
+i
));
163 if(strcmp(buf
, addon
->sha
)) {
164 WARN("Got %s, expected %s\n", buf
, addon
->sha
);
171 static void set_status(DWORD id
)
173 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
176 LoadStringW(hInst
, id
, buf
, sizeof(buf
)/sizeof(WCHAR
));
177 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
186 static enum install_res
install_file(const WCHAR
*file_name
)
190 res
= MsiInstallProductW(file_name
, NULL
);
191 if(res
!= ERROR_SUCCESS
) {
192 ERR("MsiInstallProduct failed: %u\n", res
);
193 return INSTALL_FAILED
;
199 static enum install_res
install_from_unix_file(const char *dir
, const char *subdir
, const char *file_name
)
201 LPWSTR dos_file_name
;
204 enum install_res ret
;
207 file_path
= heap_alloc(len
+strlen(subdir
)+strlen(file_name
)+3);
209 return INSTALL_FAILED
;
211 memcpy(file_path
, dir
, len
);
212 if(len
&& file_path
[len
-1] != '/' && file_path
[len
-1] != '\\')
213 file_path
[len
++] = '/';
215 strcpy(file_path
+len
, subdir
);
216 len
+= strlen(subdir
);
217 file_path
[len
++] = '/';
219 strcpy(file_path
+len
, file_name
);
221 fd
= open(file_path
, O_RDONLY
);
223 TRACE("%s not found\n", debugstr_a(file_path
));
224 heap_free(file_path
);
230 if(p_wine_get_dos_file_name
) { /* Wine UNIX mode */
231 dos_file_name
= p_wine_get_dos_file_name(file_path
);
233 ERR("Could not get dos file name of %s\n", debugstr_a(file_path
));
234 heap_free(file_path
);
235 return INSTALL_FAILED
;
237 } else { /* Windows mode */
239 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
240 res
= MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, 0, 0);
241 dos_file_name
= heap_alloc (res
*sizeof(WCHAR
));
242 MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, dos_file_name
, res
);
245 heap_free(file_path
);
247 ret
= install_file(dos_file_name
);
249 heap_free(dos_file_name
);
253 static HKEY
open_config_key(void)
258 static const WCHAR wine_keyW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
260 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
261 res
= RegOpenKeyW(HKEY_CURRENT_USER
, wine_keyW
, &hkey
);
262 if(res
!= ERROR_SUCCESS
)
265 res
= RegOpenKeyA(hkey
, addon
->config_key
, &ret
);
267 return res
== ERROR_SUCCESS
? ret
: NULL
;
270 static enum install_res
install_from_registered_dir(void)
274 DWORD res
, type
, size
= MAX_PATH
;
275 enum install_res ret
;
277 hkey
= open_config_key();
281 package_dir
= heap_alloc(size
);
282 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
283 if(res
== ERROR_MORE_DATA
) {
284 package_dir
= heap_realloc(package_dir
, size
);
285 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
288 if(res
== ERROR_FILE_NOT_FOUND
) {
289 heap_free(package_dir
);
291 } else if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
292 heap_free(package_dir
);
293 return INSTALL_FAILED
;
296 TRACE("Trying %s/%s\n", debugstr_a(package_dir
), debugstr_a(addon
->file_name
));
298 ret
= install_from_unix_file(package_dir
, "", addon
->file_name
);
300 heap_free(package_dir
);
304 static enum install_res
install_from_default_dir(void)
306 const char *data_dir
, *package_dir
;
307 char *dir_buf
= NULL
;
309 enum install_res ret
;
311 if((data_dir
= wine_get_data_dir())) {
312 package_dir
= data_dir
;
313 }else if((data_dir
= wine_get_build_dir())) {
314 len
= strlen(data_dir
);
315 dir_buf
= heap_alloc(len
+ sizeof("/../"));
316 memcpy(dir_buf
, data_dir
, len
);
317 strcpy(dir_buf
+len
, "/../");
318 package_dir
= dir_buf
;
323 ret
= install_from_unix_file(package_dir
, addon
->subdir_name
, addon
->file_name
);
326 if (ret
== INSTALL_NEXT
)
327 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/", addon
->subdir_name
, addon
->file_name
);
328 if (ret
== INSTALL_NEXT
&& strcmp(INSTALL_DATADIR
, "/usr/share"))
329 ret
= install_from_unix_file("/usr/share/wine/", addon
->subdir_name
, addon
->file_name
);
333 static WCHAR
*get_cache_file_name(BOOL ensure_exists
)
335 const char *home_dir
= NULL
, *xdg_cache_dir
;
336 size_t len
, size
= strlen(addon
->file_name
) + 7; /* strlen("/wine/"), '\0' */
337 char *cache_file_name
;
340 /* non-Wine (eg. Windows) cache is currently not supported */
341 if(!p_wine_get_dos_file_name
)
344 xdg_cache_dir
= getenv("XDG_CACHE_HOME");
345 if(xdg_cache_dir
&& *xdg_cache_dir
) {
346 size
+= strlen(xdg_cache_dir
);
348 home_dir
= getenv("HOME");
352 size
+= strlen(home_dir
) + 8; /* strlen("/.cache/") */
355 cache_file_name
= heap_alloc(size
);
359 if(xdg_cache_dir
&& *xdg_cache_dir
) {
360 len
= strlen(xdg_cache_dir
);
361 if(len
> 1 && xdg_cache_dir
[len
-1] == '/')
363 memcpy(cache_file_name
, xdg_cache_dir
, len
);
364 cache_file_name
[len
] = 0;
366 len
= strlen(home_dir
);
367 memcpy(cache_file_name
, home_dir
, len
);
368 strcpy(cache_file_name
+len
, "/.cache");
372 if(ensure_exists
&& mkdir(cache_file_name
, 0777) && errno
!= EEXIST
) {
373 WARN("%s does not exist and could not be created: %s\n", cache_file_name
, strerror(errno
));
374 heap_free(cache_file_name
);
378 strcpy(cache_file_name
+len
, "/wine");
381 if(ensure_exists
&& mkdir(cache_file_name
, 0777) && errno
!= EEXIST
) {
382 WARN("%s does not exist and could not be created: %s\n", cache_file_name
, strerror(errno
));
386 cache_file_name
[len
++] = '/';
387 strcpy(cache_file_name
+len
, addon
->file_name
);
388 ret
= p_wine_get_dos_file_name(cache_file_name
);
390 TRACE("%s -> %s\n", cache_file_name
, debugstr_w(ret
));
392 heap_free(cache_file_name
);
396 static enum install_res
install_from_cache(void)
398 WCHAR
*cache_file_name
;
399 enum install_res res
;
401 cache_file_name
= get_cache_file_name(FALSE
);
405 if(!sha_check(cache_file_name
)) {
406 WARN("could not validate checksum\n");
407 DeleteFileW(cache_file_name
);
408 heap_free(cache_file_name
);
412 res
= install_file(cache_file_name
);
413 heap_free(cache_file_name
);
417 static IInternetBindInfo InstallCallbackBindInfo
;
419 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
420 REFIID riid
, void **ppv
)
422 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
427 if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
428 TRACE("IID_IInternetBindInfo\n");
429 *ppv
= &InstallCallbackBindInfo
;
436 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
441 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
446 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
447 DWORD dwReserved
, IBinding
*pib
)
449 set_status(IDS_DOWNLOADING
);
451 IBinding_AddRef(pib
);
457 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
463 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
469 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
470 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
472 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
475 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
477 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
482 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
483 HRESULT hresult
, LPCWSTR szError
)
486 IBinding_Release(dwl_binding
);
490 if(FAILED(hresult
)) {
491 if(hresult
== E_ABORT
)
492 TRACE("Binding aborted\n");
494 ERR("Binding failed %08x\n", hresult
);
499 ERR("No MSI file\n");
503 set_status(IDS_INSTALLING
);
504 EnableWindow(GetDlgItem(install_dialog
, IDCANCEL
), 0);
506 if(sha_check(msi_file
)) {
507 WCHAR
*cache_file_name
;
509 install_file(msi_file
);
511 cache_file_name
= get_cache_file_name(TRUE
);
512 if(cache_file_name
) {
513 MoveFileW(msi_file
, cache_file_name
);
514 heap_free(cache_file_name
);
519 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, sizeof(message
)/sizeof(WCHAR
)))
520 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
523 DeleteFileW(msi_file
);
527 EndDialog(install_dialog
, 0);
531 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
532 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
536 *grfBINDF
= BINDF_ASYNCHRONOUS
;
540 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
541 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
544 msi_file
= heap_strdupW(pstgmed
->u
.lpszFileName
);
545 TRACE("got file name %s\n", debugstr_w(msi_file
));
551 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
552 REFIID riid
, IUnknown
* punk
)
558 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
559 InstallCallback_QueryInterface
,
560 InstallCallback_AddRef
,
561 InstallCallback_Release
,
562 InstallCallback_OnStartBinding
,
563 InstallCallback_GetPriority
,
564 InstallCallback_OnLowResource
,
565 InstallCallback_OnProgress
,
566 InstallCallback_OnStopBinding
,
567 InstallCallback_GetBindInfo
,
568 InstallCallback_OnDataAvailable
,
569 InstallCallback_OnObjectAvailable
572 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
574 static HRESULT WINAPI
InstallCallbackBindInfo_QueryInterface(IInternetBindInfo
*iface
, REFIID riid
, void **ppv
)
576 return IBindStatusCallback_QueryInterface(&InstallCallback
, riid
, ppv
);
579 static ULONG WINAPI
InstallCallbackBindInfo_AddRef(IInternetBindInfo
*iface
)
584 static ULONG WINAPI
InstallCallbackBindInfo_Release(IInternetBindInfo
*iface
)
589 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindInfo(IInternetBindInfo
*iface
, DWORD
*bindf
, BINDINFO
*bindinfo
)
595 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindString(IInternetBindInfo
*iface
, ULONG string_type
,
596 WCHAR
**strs
, ULONG cnt
, ULONG
*fetched
)
598 static const WCHAR wine_addon_downloaderW
[] =
599 {'W','i','n','e',' ','A','d','d','o','n',' ','D','o','w','n','l','o','a','d','e','r',0};
601 switch(string_type
) {
602 case BINDSTRING_USER_AGENT
:
603 TRACE("BINDSTRING_USER_AGENT\n");
605 *strs
= CoTaskMemAlloc(sizeof(wine_addon_downloaderW
));
607 return E_OUTOFMEMORY
;
609 memcpy(*strs
, wine_addon_downloaderW
, sizeof(wine_addon_downloaderW
));
617 static const IInternetBindInfoVtbl InstallCallbackBindInfoVtbl
= {
618 InstallCallbackBindInfo_QueryInterface
,
619 InstallCallbackBindInfo_AddRef
,
620 InstallCallbackBindInfo_Release
,
621 InstallCallbackBindInfo_GetBindInfo
,
622 InstallCallbackBindInfo_GetBindString
625 static IInternetBindInfo InstallCallbackBindInfo
= { &InstallCallbackBindInfoVtbl
};
627 static void append_url_params( WCHAR
*url
)
629 static const WCHAR arch_formatW
[] = {'?','a','r','c','h','='};
630 static const WCHAR v_formatW
[] = {'&','v','='};
631 static const WCHAR winevW
[] = {'&','w','i','n','e','v','='};
632 DWORD size
= INTERNET_MAX_URL_LENGTH
* sizeof(WCHAR
);
633 DWORD len
= strlenW(url
);
635 memcpy(url
+len
, arch_formatW
, sizeof(arch_formatW
));
636 len
+= sizeof(arch_formatW
)/sizeof(WCHAR
);
637 len
+= MultiByteToWideChar(CP_ACP
, 0, ARCH_STRING
, sizeof(ARCH_STRING
),
638 url
+len
, size
/sizeof(WCHAR
)-len
)-1;
639 memcpy(url
+len
, v_formatW
, sizeof(v_formatW
));
640 len
+= sizeof(v_formatW
)/sizeof(WCHAR
);
641 len
+= MultiByteToWideChar(CP_ACP
, 0, addon
->version
, -1, url
+len
, size
/sizeof(WCHAR
)-len
)-1;
642 memcpy(url
+len
, winevW
, sizeof(winevW
));
643 len
+= sizeof(winevW
)/sizeof(WCHAR
);
644 MultiByteToWideChar(CP_ACP
, 0, PACKAGE_VERSION
, -1, url
+len
, size
/sizeof(WCHAR
)-len
);
647 static LPWSTR
get_url(void)
649 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
650 WCHAR
*url
, *config_key
;
655 static const WCHAR httpW
[] = {'h','t','t','p'};
657 url
= heap_alloc(size
);
658 returned_size
= size
;
660 hkey
= open_config_key();
663 config_key
= heap_strdupAtoW(addon
->url_config_key
);
664 res
= RegQueryValueExW(hkey
, config_key
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
665 heap_free(config_key
);
667 if(res
== ERROR_SUCCESS
&& type
== REG_SZ
) goto found
;
670 MultiByteToWideChar( CP_ACP
, 0, addon
->url_default
, -1, url
, size
/ sizeof(WCHAR
) );
673 if (returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) append_url_params( url
);
675 TRACE("Got URL %s\n", debugstr_w(url
));
679 static BOOL
start_download(void)
686 hres
= CreateURLMoniker(NULL
, url
, &mon
);
690 hres
= CreateAsyncBindCtx(0, &InstallCallback
, NULL
, &bind_ctx
);
691 if(SUCCEEDED(hres
)) {
692 hres
= IMoniker_BindToStorage(mon
, bind_ctx
, NULL
, &IID_IUnknown
, (void**)&tmp
);
693 IBindCtx_Release(bind_ctx
);
695 IMoniker_Release(mon
);
700 IUnknown_Release(tmp
);
704 static void run_winebrowser(const WCHAR
*url
)
706 PROCESS_INFORMATION pi
;
713 static const WCHAR winebrowserW
[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
715 url_len
= strlenW(url
);
717 len
= GetSystemDirectoryW(app
, MAX_PATH
-sizeof(winebrowserW
)/sizeof(WCHAR
));
718 memcpy(app
+len
, winebrowserW
, sizeof(winebrowserW
));
719 len
+= sizeof(winebrowserW
)/sizeof(WCHAR
) -1;
721 args
= heap_alloc((len
+1+url_len
)*sizeof(WCHAR
));
725 memcpy(args
, app
, len
*sizeof(WCHAR
));
727 memcpy(args
+len
, url
, (url_len
+1) * sizeof(WCHAR
));
729 TRACE("starting %s\n", debugstr_w(args
));
731 memset(&si
, 0, sizeof(si
));
733 ret
= CreateProcessW(app
, args
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
);
736 CloseHandle(pi
.hThread
);
737 CloseHandle(pi
.hProcess
);
741 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
745 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
746 install_dialog
= hwnd
;
750 switch (((NMHDR
*)lParam
)->code
)
754 if (wParam
== ID_DWL_STATUS
)
755 run_winebrowser(((NMLINK
*)lParam
)->item
.szUrl
);
764 IBinding_Abort(dwl_binding
);
769 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
770 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
771 if(!start_download())
772 EndDialog(install_dialog
, 0);
779 BOOL
install_addon(addon_t addon_type
)
784 addon
= addons_info
+addon_type
;
786 p_wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleW(kernel32_dllW
), "wine_get_dos_file_name");
789 * Try to find addon .msi file in following order:
790 * - directory stored in $dir_config_key value of HKCU/Software/Wine/$config_key key
791 * - $datadir/$addon_subdir/
792 * - $INSTALL_DATADIR/wine/$addon_subdir/
793 * - /usr/share/wine/$addon_subdir/
794 * - download from URL stored in $url_config_key value of HKCU/Software/Wine/$config_key key
796 if (install_from_registered_dir() == INSTALL_NEXT
797 && install_from_default_dir() == INSTALL_NEXT
798 && install_from_cache() == INSTALL_NEXT
799 && (url
= get_url()))
800 DialogBoxW(hInst
, addon
->dialog_template
, 0, installer_proc
);