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
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "wine/library.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
54 #define GECKO_VERSION "1.5"
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "07b2bc74d03c885bb39124a7641715314cd3ae71"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "80a3b36c30bb79a11889879392fdc1fcda9ca165"
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
67 #define MONO_VERSION "0.0.4"
68 #define MONO_SHA "7d827f7d28a88ae0da95a136573783124ffce4b1"
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
;
108 /* SHA definitions are copied from advapi32. They aren't available in headers. */
115 } SHA_CTX
, *PSHA_CTX
;
117 void WINAPI
A_SHAInit(PSHA_CTX
);
118 void WINAPI
A_SHAUpdate(PSHA_CTX
,const unsigned char*,UINT
);
119 void WINAPI
A_SHAFinal(PSHA_CTX
,PULONG
);
121 static BOOL
sha_check(const WCHAR
*file_name
)
123 const unsigned char *file_map
;
126 char buf
[2*sizeof(sha
)+1];
130 file
= CreateFileW(file_name
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
131 if(file
== INVALID_HANDLE_VALUE
)
134 size
= GetFileSize(file
, NULL
);
136 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
141 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
147 A_SHAUpdate(&ctx
, file_map
, size
);
148 A_SHAFinal(&ctx
, sha
);
150 UnmapViewOfFile(file_map
);
152 for(i
=0; i
< sizeof(sha
); i
++)
153 sprintf(buf
+ i
*2, "%02x", *((unsigned char*)sha
+i
));
155 if(strcmp(buf
, addon
->sha
)) {
158 WARN("Got %s, expected %s\n", buf
, addon
->sha
);
160 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, sizeof(message
)/sizeof(WCHAR
)))
161 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
169 static void set_status(DWORD id
)
171 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
174 LoadStringW(hInst
, id
, buf
, sizeof(buf
)/sizeof(WCHAR
));
175 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
178 static BOOL
install_file(const WCHAR
*file_name
)
182 res
= MsiInstallProductW(file_name
, NULL
);
183 if(res
!= ERROR_SUCCESS
) {
184 ERR("MsiInstallProduct failed: %u\n", res
);
191 static BOOL
install_from_unix_file(const char *dir
, const char *subdir
, const char *file_name
)
193 LPWSTR dos_file_name
;
198 static WCHAR
* (CDECL
*wine_get_dos_file_name
)(const char*);
199 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
202 file_path
= heap_alloc(len
+strlen(subdir
)+strlen(file_name
)+3);
206 memcpy(file_path
, dir
, len
);
207 if(len
&& file_path
[len
-1] != '/' && file_path
[len
-1] != '\\')
208 file_path
[len
++] = '/';
210 strcpy(file_path
+len
, subdir
);
211 len
+= strlen(subdir
);
212 file_path
[len
++] = '/';
214 strcpy(file_path
+len
, file_name
);
216 fd
= open(file_path
, O_RDONLY
);
218 TRACE("%s not found\n", debugstr_a(file_path
));
219 heap_free(file_path
);
225 if(!wine_get_dos_file_name
)
226 wine_get_dos_file_name
= (void*)GetProcAddress(GetModuleHandleW(kernel32W
), "wine_get_dos_file_name");
228 if(wine_get_dos_file_name
) { /* Wine UNIX mode */
229 dos_file_name
= wine_get_dos_file_name(file_path
);
231 ERR("Could not get dos file name of %s\n", debugstr_a(file_path
));
232 heap_free(file_path
);
235 } else { /* Windows mode */
237 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
238 res
= MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, 0, 0);
239 dos_file_name
= heap_alloc (res
*sizeof(WCHAR
));
240 MultiByteToWideChar( CP_ACP
, 0, file_path
, -1, dos_file_name
, res
);
243 heap_free(file_path
);
245 ret
= install_file(dos_file_name
);
247 heap_free(dos_file_name
);
251 static HKEY
open_config_key(void)
256 static const WCHAR wine_keyW
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
258 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
259 res
= RegOpenKeyW(HKEY_CURRENT_USER
, wine_keyW
, &hkey
);
260 if(res
!= ERROR_SUCCESS
)
263 res
= RegOpenKeyA(hkey
, addon
->config_key
, &ret
);
265 return res
== ERROR_SUCCESS
? ret
: NULL
;
268 static BOOL
install_from_registered_dir(void)
272 DWORD res
, type
, size
= MAX_PATH
;
275 hkey
= open_config_key();
279 package_dir
= heap_alloc(size
);
280 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
281 if(res
== ERROR_MORE_DATA
) {
282 package_dir
= heap_realloc(package_dir
, size
);
283 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
286 if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
287 heap_free(package_dir
);
291 TRACE("Trying %s/%s\n", debugstr_a(package_dir
), debugstr_a(addon
->file_name
));
293 ret
= install_from_unix_file(package_dir
, "", addon
->file_name
);
295 heap_free(package_dir
);
299 static BOOL
install_from_default_dir(void)
301 const char *data_dir
, *package_dir
;
302 char *dir_buf
= NULL
;
306 if((data_dir
= wine_get_data_dir())) {
307 package_dir
= data_dir
;
308 }else if((data_dir
= wine_get_build_dir())) {
309 len
= strlen(data_dir
);
310 dir_buf
= heap_alloc(len
+ sizeof("/../"));
311 memcpy(dir_buf
, data_dir
, len
);
312 strcpy(dir_buf
+len
, "/../");
313 package_dir
= dir_buf
;
318 ret
= install_from_unix_file(package_dir
, addon
->subdir_name
, addon
->file_name
);
322 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/", addon
->subdir_name
, addon
->file_name
);
323 if (!ret
&& strcmp(INSTALL_DATADIR
, "/usr/share"))
324 ret
= install_from_unix_file("/usr/share/wine/", addon
->subdir_name
, addon
->file_name
);
328 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
329 REFIID riid
, void **ppv
)
331 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
339 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
344 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
349 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
350 DWORD dwReserved
, IBinding
*pib
)
352 set_status(IDS_DOWNLOADING
);
356 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
362 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
368 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
369 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
371 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
374 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
376 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
381 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
382 HRESULT hresult
, LPCWSTR szError
)
384 if(FAILED(hresult
)) {
385 ERR("Binding failed %08x\n", hresult
);
389 set_status(IDS_INSTALLING
);
393 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
394 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
401 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
402 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
408 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
409 REFIID riid
, IUnknown
* punk
)
415 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
416 InstallCallback_QueryInterface
,
417 InstallCallback_AddRef
,
418 InstallCallback_Release
,
419 InstallCallback_OnStartBinding
,
420 InstallCallback_GetPriority
,
421 InstallCallback_OnLowResource
,
422 InstallCallback_OnProgress
,
423 InstallCallback_OnStopBinding
,
424 InstallCallback_GetBindInfo
,
425 InstallCallback_OnDataAvailable
,
426 InstallCallback_OnObjectAvailable
429 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
431 static void append_url_params( WCHAR
*url
)
433 static const WCHAR arch_formatW
[] = {'?','a','r','c','h','='};
434 static const WCHAR v_formatW
[] = {'&','v','='};
435 DWORD size
= INTERNET_MAX_URL_LENGTH
* sizeof(WCHAR
);
436 DWORD len
= strlenW(url
);
438 memcpy(url
+len
, arch_formatW
, sizeof(arch_formatW
));
439 len
+= sizeof(arch_formatW
)/sizeof(WCHAR
);
440 len
+= MultiByteToWideChar(CP_ACP
, 0, ARCH_STRING
, sizeof(ARCH_STRING
),
441 url
+len
, size
/sizeof(WCHAR
)-len
)-1;
442 memcpy(url
+len
, v_formatW
, sizeof(v_formatW
));
443 len
+= sizeof(v_formatW
)/sizeof(WCHAR
);
444 MultiByteToWideChar(CP_ACP
, 0, addon
->version
, -1, url
+len
, size
/sizeof(WCHAR
)-len
);
447 static LPWSTR
get_url(void)
449 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
450 WCHAR
*url
, *config_key
;
455 static const WCHAR httpW
[] = {'h','t','t','p'};
457 url
= heap_alloc(size
);
458 returned_size
= size
;
460 hkey
= open_config_key();
463 config_key
= heap_strdupAtoW(addon
->url_config_key
);
464 res
= RegQueryValueExW(hkey
, config_key
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
465 heap_free(config_key
);
467 if(res
== ERROR_SUCCESS
&& type
== REG_SZ
) goto found
;
470 MultiByteToWideChar( CP_ACP
, 0, addon
->url_default
, -1, url
, size
/ sizeof(WCHAR
) );
473 if (returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) append_url_params( url
);
475 TRACE("Got URL %s\n", debugstr_w(url
));
479 static DWORD WINAPI
download_proc(PVOID arg
)
481 WCHAR tmp_dir
[MAX_PATH
], tmp_file
[MAX_PATH
];
484 GetTempPathW(sizeof(tmp_dir
)/sizeof(WCHAR
), tmp_dir
);
485 GetTempFileNameW(tmp_dir
, NULL
, 0, tmp_file
);
487 TRACE("using temp file %s\n", debugstr_w(tmp_file
));
489 hres
= URLDownloadToFileW(NULL
, url
, tmp_file
, 0, &InstallCallback
);
491 ERR("URLDownloadToFile failed: %08x\n", hres
);
495 if(sha_check(tmp_file
))
496 install_file(tmp_file
);
497 DeleteFileW(tmp_file
);
498 EndDialog(install_dialog
, 0);
502 static void run_winebrowser(const WCHAR
*url
)
504 PROCESS_INFORMATION pi
;
511 static const WCHAR winebrowserW
[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
513 url_len
= strlenW(url
);
515 len
= GetSystemDirectoryW(app
, MAX_PATH
-sizeof(winebrowserW
)/sizeof(WCHAR
));
516 memcpy(app
+len
, winebrowserW
, sizeof(winebrowserW
));
517 len
+= sizeof(winebrowserW
)/sizeof(WCHAR
) -1;
519 args
= heap_alloc((len
+1+url_len
)*sizeof(WCHAR
));
523 memcpy(args
, app
, len
*sizeof(WCHAR
));
525 memcpy(args
+len
, url
, (url_len
+1) * sizeof(WCHAR
));
527 TRACE("starting %s\n", debugstr_w(args
));
529 memset(&si
, 0, sizeof(si
));
531 ret
= CreateProcessW(app
, args
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
534 CloseHandle(pi
.hThread
);
535 CloseHandle(pi
.hProcess
);
539 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
543 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
544 install_dialog
= hwnd
;
548 switch (((NMHDR
*)lParam
)->code
)
552 if (wParam
== ID_DWL_STATUS
)
553 run_winebrowser(((NMLINK
*)lParam
)->item
.szUrl
);
565 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
566 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
567 EnableWindow(GetDlgItem(hwnd
, IDCANCEL
), 0); /* FIXME */
568 CloseHandle( CreateThread(NULL
, 0, download_proc
, NULL
, 0, NULL
));
576 BOOL
install_addon(addon_t addon_type
)
581 addon
= addons_info
+addon_type
;
584 * Try to find addon .msi file in following order:
585 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
586 * - $datadir/$addon_subdir/
587 * - $INSTALL_DATADIR/wine/$addon_subdir/
588 * - /usr/share/wine/$addon_subdir/
589 * - download from URL stored in $url_config_key value of HKCU/Wine/Software/$config_key key
591 if(!install_from_registered_dir()
592 && !install_from_default_dir()
593 && (url
= get_url()))
594 DialogBoxW(hInst
, addon
->dialog_template
, 0, installer_proc
);