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
32 #define NONAMELESSSTRUCT
50 #include "wine/debug.h"
51 #include "wine/unicode.h"
52 #include "wine/library.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
56 #define GECKO_VERSION "2.21"
59 #define ARCH_STRING "x86"
60 #define GECKO_SHA "a514fc4d53783a586c7880a676c415695fe934a3"
61 #elif defined(__x86_64__)
62 #define ARCH_STRING "x86_64"
63 #define GECKO_SHA "c6f249ff2c6eb7dfe423ef246aba54e1a3b26934"
65 #define ARCH_STRING ""
66 #define GECKO_SHA "???"
69 #define MONO_VERSION "0.0.8"
70 #define MONO_SHA "dd349e72249ce5ff981be0e9dae33ac4a46a9f60"
74 const char *file_name
;
75 const char *subdir_name
;
77 const char *url_default
;
78 const char *config_key
;
79 const char *url_config_key
;
80 const char *dir_config_key
;
81 LPCWSTR dialog_template
;
84 static const addon_info_t addons_info
[] = {
87 "wine_gecko-" GECKO_VERSION
"-" ARCH_STRING
".msi",
90 "http://source.winehq.org/winegecko.php",
91 "MSHTML", "GeckoUrl", "GeckoCabDir",
92 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG
)
96 "wine-mono-" MONO_VERSION
".msi",
99 "http://source.winehq.org/winemono.php",
100 "Dotnet", "MonoUrl", "MonoCabDir",
101 MAKEINTRESOURCEW(ID_DWL_MONO_DIALOG
)
105 static const addon_info_t
*addon
;
107 static HWND install_dialog
= NULL
;
108 static LPWSTR url
= NULL
;
109 static IBinding
*dwl_binding
;
111 static WCHAR
* (CDECL
*p_wine_get_dos_file_name
)(const char*);
112 static const WCHAR kernel32_dllW
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
115 /* SHA definitions are copied from advapi32. They aren't available in headers. */
122 } SHA_CTX
, *PSHA_CTX
;
124 void WINAPI
A_SHAInit(PSHA_CTX
);
125 void WINAPI
A_SHAUpdate(PSHA_CTX
,const unsigned char*,UINT
);
126 void WINAPI
A_SHAFinal(PSHA_CTX
,PULONG
);
128 static BOOL
sha_check(const WCHAR
*file_name
)
130 const unsigned char *file_map
;
133 char buf
[2*sizeof(sha
)+1];
137 file
= CreateFileW(file_name
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
138 if(file
== INVALID_HANDLE_VALUE
)
141 size
= GetFileSize(file
, NULL
);
143 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
148 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
154 A_SHAUpdate(&ctx
, file_map
, size
);
155 A_SHAFinal(&ctx
, sha
);
157 UnmapViewOfFile(file_map
);
159 for(i
=0; i
< sizeof(sha
); i
++)
160 sprintf(buf
+ i
*2, "%02x", *((unsigned char*)sha
+i
));
162 if(strcmp(buf
, addon
->sha
)) {
163 WARN("Got %s, expected %s\n", buf
, addon
->sha
);
170 static void set_status(DWORD id
)
172 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
175 LoadStringW(hInst
, id
, buf
, sizeof(buf
)/sizeof(WCHAR
));
176 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
185 static enum install_res
install_file(const WCHAR
*file_name
)
189 EnableWindow(GetDlgItem(install_dialog
, IDCANCEL
), 0);
191 res
= MsiInstallProductW(file_name
, NULL
);
192 if(res
!= ERROR_SUCCESS
) {
193 ERR("MsiInstallProduct failed: %u\n", res
);
194 return INSTALL_FAILED
;
200 static enum install_res
install_from_unix_file(const char *dir
, const char *subdir
, const char *file_name
)
202 LPWSTR dos_file_name
;
205 enum install_res ret
;
208 file_path
= heap_alloc(len
+strlen(subdir
)+strlen(file_name
)+3);
210 return INSTALL_FAILED
;
212 memcpy(file_path
, dir
, len
);
213 if(len
&& file_path
[len
-1] != '/' && file_path
[len
-1] != '\\')
214 file_path
[len
++] = '/';
216 strcpy(file_path
+len
, subdir
);
217 len
+= strlen(subdir
);
218 file_path
[len
++] = '/';
220 strcpy(file_path
+len
, file_name
);
222 fd
= open(file_path
, O_RDONLY
);
224 TRACE("%s not found\n", debugstr_a(file_path
));
225 heap_free(file_path
);
231 if(p_wine_get_dos_file_name
) { /* Wine UNIX mode */
232 dos_file_name
= p_wine_get_dos_file_name(file_path
);
234 ERR("Could not get dos file name of %s\n", debugstr_a(file_path
));
235 heap_free(file_path
);
236 return INSTALL_FAILED
;
238 } else { /* Windows mode */
240 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
241 res
= MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, 0, 0);
242 dos_file_name
= heap_alloc (res
*sizeof(WCHAR
));
243 MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, dos_file_name
, res
);
246 heap_free(file_path
);
248 ret
= install_file(dos_file_name
);
250 heap_free(dos_file_name
);
254 static HKEY
open_config_key(void)
259 static const WCHAR wine_keyW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
261 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
262 res
= RegOpenKeyW(HKEY_CURRENT_USER
, wine_keyW
, &hkey
);
263 if(res
!= ERROR_SUCCESS
)
266 res
= RegOpenKeyA(hkey
, addon
->config_key
, &ret
);
268 return res
== ERROR_SUCCESS
? ret
: NULL
;
271 static enum install_res
install_from_registered_dir(void)
275 DWORD res
, type
, size
= MAX_PATH
;
276 enum install_res ret
;
278 hkey
= open_config_key();
282 package_dir
= heap_alloc(size
);
283 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
284 if(res
== ERROR_MORE_DATA
) {
285 package_dir
= heap_realloc(package_dir
, size
);
286 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
289 if(res
== ERROR_FILE_NOT_FOUND
) {
290 heap_free(package_dir
);
292 } else if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
293 heap_free(package_dir
);
294 return INSTALL_FAILED
;
297 TRACE("Trying %s/%s\n", debugstr_a(package_dir
), debugstr_a(addon
->file_name
));
299 ret
= install_from_unix_file(package_dir
, "", addon
->file_name
);
301 heap_free(package_dir
);
305 static enum install_res
install_from_default_dir(void)
307 const char *data_dir
, *package_dir
;
308 char *dir_buf
= NULL
;
312 if((data_dir
= wine_get_data_dir())) {
313 package_dir
= data_dir
;
314 }else if((data_dir
= wine_get_build_dir())) {
315 len
= strlen(data_dir
);
316 dir_buf
= heap_alloc(len
+ sizeof("/../"));
317 memcpy(dir_buf
, data_dir
, len
);
318 strcpy(dir_buf
+len
, "/../");
319 package_dir
= dir_buf
;
324 ret
= install_from_unix_file(package_dir
, addon
->subdir_name
, addon
->file_name
);
327 if (ret
== INSTALL_NEXT
)
328 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/", addon
->subdir_name
, addon
->file_name
);
329 if (ret
== INSTALL_NEXT
&& strcmp(INSTALL_DATADIR
, "/usr/share"))
330 ret
= install_from_unix_file("/usr/share/wine/", addon
->subdir_name
, addon
->file_name
);
334 static WCHAR
*get_cache_file_name(BOOL ensure_exists
)
336 const char *home_dir
= NULL
, *xdg_cache_dir
;
337 size_t len
, size
= strlen(addon
->file_name
) + 7; /* strlen("/wine/"), '\0' */
338 char *cache_file_name
;
341 /* non-Wine (eg. Windows) cache is currently not supported */
342 if(!p_wine_get_dos_file_name
)
345 xdg_cache_dir
= getenv("XDG_CACHE_HOME");
346 if(xdg_cache_dir
&& *xdg_cache_dir
) {
347 size
+= strlen(xdg_cache_dir
);
349 home_dir
= getenv("HOME");
353 size
+= strlen(home_dir
) + 8; /* strlen("/.cache/") */
356 cache_file_name
= heap_alloc(size
);
360 if(xdg_cache_dir
&& *xdg_cache_dir
) {
361 len
= strlen(xdg_cache_dir
);
362 if(len
> 1 && xdg_cache_dir
[len
-1] == '/')
364 memcpy(cache_file_name
, xdg_cache_dir
, len
);
365 cache_file_name
[len
] = 0;
367 len
= strlen(home_dir
);
368 memcpy(cache_file_name
, home_dir
, len
);
369 strcpy(cache_file_name
+len
, "/.cache");
373 if(ensure_exists
&& mkdir(cache_file_name
, 0777) && errno
!= EEXIST
) {
374 WARN("%s does not exist and could not be created: %s\n", cache_file_name
, strerror(errno
));
375 heap_free(cache_file_name
);
379 strcpy(cache_file_name
+len
, "/wine");
382 if(ensure_exists
&& mkdir(cache_file_name
, 0777) && errno
!= EEXIST
) {
383 WARN("%s does not exist and could not be created: %s\n", cache_file_name
, strerror(errno
));
387 cache_file_name
[len
++] = '/';
388 strcpy(cache_file_name
+len
, addon
->file_name
);
389 ret
= p_wine_get_dos_file_name(cache_file_name
);
391 TRACE("%s -> %s\n", cache_file_name
, debugstr_w(ret
));
393 heap_free(cache_file_name
);
397 static enum install_res
install_from_cache(void)
399 WCHAR
*cache_file_name
;
400 enum install_res res
;
402 cache_file_name
= get_cache_file_name(FALSE
);
406 if(!sha_check(cache_file_name
)) {
407 WARN("could not validate check sum\n");
408 DeleteFileW(cache_file_name
);
409 heap_free(cache_file_name
);
413 res
= install_file(cache_file_name
);
414 heap_free(cache_file_name
);
418 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
419 REFIID riid
, void **ppv
)
421 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
429 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
434 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
439 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
440 DWORD dwReserved
, IBinding
*pib
)
442 set_status(IDS_DOWNLOADING
);
444 IBinding_AddRef(pib
);
450 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
456 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
462 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
463 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
465 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
468 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
470 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
475 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
476 HRESULT hresult
, LPCWSTR szError
)
479 IBinding_Release(dwl_binding
);
483 if(FAILED(hresult
)) {
484 if(hresult
== E_ABORT
)
485 TRACE("Binding aborted\n");
487 ERR("Binding failed %08x\n", hresult
);
491 set_status(IDS_INSTALLING
);
495 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
496 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
503 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
504 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
510 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
511 REFIID riid
, IUnknown
* punk
)
517 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
518 InstallCallback_QueryInterface
,
519 InstallCallback_AddRef
,
520 InstallCallback_Release
,
521 InstallCallback_OnStartBinding
,
522 InstallCallback_GetPriority
,
523 InstallCallback_OnLowResource
,
524 InstallCallback_OnProgress
,
525 InstallCallback_OnStopBinding
,
526 InstallCallback_GetBindInfo
,
527 InstallCallback_OnDataAvailable
,
528 InstallCallback_OnObjectAvailable
531 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
533 static void append_url_params( WCHAR
*url
)
535 static const WCHAR arch_formatW
[] = {'?','a','r','c','h','='};
536 static const WCHAR v_formatW
[] = {'&','v','='};
537 DWORD size
= INTERNET_MAX_URL_LENGTH
* sizeof(WCHAR
);
538 DWORD len
= strlenW(url
);
540 memcpy(url
+len
, arch_formatW
, sizeof(arch_formatW
));
541 len
+= sizeof(arch_formatW
)/sizeof(WCHAR
);
542 len
+= MultiByteToWideChar(CP_ACP
, 0, ARCH_STRING
, sizeof(ARCH_STRING
),
543 url
+len
, size
/sizeof(WCHAR
)-len
)-1;
544 memcpy(url
+len
, v_formatW
, sizeof(v_formatW
));
545 len
+= sizeof(v_formatW
)/sizeof(WCHAR
);
546 MultiByteToWideChar(CP_ACP
, 0, addon
->version
, -1, url
+len
, size
/sizeof(WCHAR
)-len
);
549 static LPWSTR
get_url(void)
551 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
552 WCHAR
*url
, *config_key
;
557 static const WCHAR httpW
[] = {'h','t','t','p'};
559 url
= heap_alloc(size
);
560 returned_size
= size
;
562 hkey
= open_config_key();
565 config_key
= heap_strdupAtoW(addon
->url_config_key
);
566 res
= RegQueryValueExW(hkey
, config_key
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
567 heap_free(config_key
);
569 if(res
== ERROR_SUCCESS
&& type
== REG_SZ
) goto found
;
572 MultiByteToWideChar( CP_ACP
, 0, addon
->url_default
, -1, url
, size
/ sizeof(WCHAR
) );
575 if (returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) append_url_params( url
);
577 TRACE("Got URL %s\n", debugstr_w(url
));
581 static DWORD WINAPI
download_proc(PVOID arg
)
583 WCHAR tmp_dir
[MAX_PATH
], tmp_file
[MAX_PATH
];
586 GetTempPathW(sizeof(tmp_dir
)/sizeof(WCHAR
), tmp_dir
);
587 GetTempFileNameW(tmp_dir
, NULL
, 0, tmp_file
);
589 TRACE("using temp file %s\n", debugstr_w(tmp_file
));
591 hres
= URLDownloadToFileW(NULL
, url
, tmp_file
, 0, &InstallCallback
);
593 ERR("URLDownloadToFile failed: %08x\n", hres
);
597 if(sha_check(tmp_file
)) {
598 WCHAR
*cache_file_name
;
600 install_file(tmp_file
);
602 cache_file_name
= get_cache_file_name(TRUE
);
603 if(cache_file_name
) {
604 MoveFileW(tmp_file
, cache_file_name
);
605 heap_free(cache_file_name
);
610 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, sizeof(message
)/sizeof(WCHAR
)))
611 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
614 DeleteFileW(tmp_file
);
615 EndDialog(install_dialog
, 0);
619 static void run_winebrowser(const WCHAR
*url
)
621 PROCESS_INFORMATION pi
;
628 static const WCHAR winebrowserW
[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
630 url_len
= strlenW(url
);
632 len
= GetSystemDirectoryW(app
, MAX_PATH
-sizeof(winebrowserW
)/sizeof(WCHAR
));
633 memcpy(app
+len
, winebrowserW
, sizeof(winebrowserW
));
634 len
+= sizeof(winebrowserW
)/sizeof(WCHAR
) -1;
636 args
= heap_alloc((len
+1+url_len
)*sizeof(WCHAR
));
640 memcpy(args
, app
, len
*sizeof(WCHAR
));
642 memcpy(args
+len
, url
, (url_len
+1) * sizeof(WCHAR
));
644 TRACE("starting %s\n", debugstr_w(args
));
646 memset(&si
, 0, sizeof(si
));
648 ret
= CreateProcessW(app
, args
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
);
651 CloseHandle(pi
.hThread
);
652 CloseHandle(pi
.hProcess
);
656 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
660 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
661 install_dialog
= hwnd
;
665 switch (((NMHDR
*)lParam
)->code
)
669 if (wParam
== ID_DWL_STATUS
)
670 run_winebrowser(((NMLINK
*)lParam
)->item
.szUrl
);
679 IBinding_Abort(dwl_binding
);
684 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
685 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
686 CloseHandle( CreateThread(NULL
, 0, download_proc
, NULL
, 0, NULL
));
694 BOOL
install_addon(addon_t addon_type
)
699 addon
= addons_info
+addon_type
;
701 p_wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleW(kernel32_dllW
), "wine_get_dos_file_name");
704 * Try to find addon .msi file in following order:
705 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
706 * - $datadir/$addon_subdir/
707 * - $INSTALL_DATADIR/wine/$addon_subdir/
708 * - /usr/share/wine/$addon_subdir/
709 * - download from URL stored in $url_config_key value of HKCU/Wine/Software/$config_key key
711 if (install_from_registered_dir() == INSTALL_NEXT
712 && install_from_default_dir() == INSTALL_NEXT
713 && install_from_cache() == INSTALL_NEXT
714 && (url
= get_url()))
715 DialogBoxW(hInst
, addon
->dialog_template
, 0, installer_proc
);