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.7"
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "efebc4ed7a86708e2dc8581033a3c5d6effe0b0b"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "2253e7ce3a699ddd110c6c9ce4c7ca7e6f7c02f5"
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);
184 static enum install_res install_file(const WCHAR *file_name)
188 res = MsiInstallProductW(file_name, NULL);
189 if(res != ERROR_SUCCESS) {
190 ERR("MsiInstallProduct failed: %u\n", res);
191 return INSTALL_FAILED;
197 static enum install_res install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
199 LPWSTR dos_file_name;
202 enum install_res ret;
204 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
205 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
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(!wine_get_dos_file_name)
232 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
234 if(wine_get_dos_file_name) { /* Wine UNIX mode */
235 dos_file_name = wine_get_dos_file_name(file_path);
237 ERR("Could not get dos file name of %s\n", debugstr_a(file_path));
238 heap_free(file_path);
239 return INSTALL_FAILED;
241 } else { /* Windows mode */
243 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
244 res = MultiByteToWideChar( CP_ACP, 0, file_path, -1, 0, 0);
245 dos_file_name = heap_alloc (res*sizeof(WCHAR));
246 MultiByteToWideChar( CP_ACP, 0, file_path, -1, dos_file_name, res);
249 heap_free(file_path);
251 ret = install_file(dos_file_name);
253 heap_free(dos_file_name);
257 static HKEY open_config_key(void)
262 static const WCHAR wine_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
264 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
265 res = RegOpenKeyW(HKEY_CURRENT_USER, wine_keyW, &hkey);
266 if(res != ERROR_SUCCESS)
269 res = RegOpenKeyA(hkey, addon->config_key, &ret);
271 return res == ERROR_SUCCESS ? ret : NULL;
274 static enum install_res install_from_registered_dir(void)
278 DWORD res, type, size = MAX_PATH;
279 enum install_res ret;
281 hkey = open_config_key();
285 package_dir = heap_alloc(size);
286 res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
287 if(res == ERROR_MORE_DATA) {
288 package_dir = heap_realloc(package_dir, size);
289 res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
292 if(res == ERROR_FILE_NOT_FOUND) {
293 heap_free(package_dir);
295 } else if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
296 heap_free(package_dir);
297 return INSTALL_FAILED;
300 TRACE("Trying %s/%s\n", debugstr_a(package_dir), debugstr_a(addon->file_name));
302 ret = install_from_unix_file(package_dir, "", addon->file_name);
304 heap_free(package_dir);
308 static enum install_res install_from_default_dir(void)
310 const char *data_dir, *package_dir;
311 char *dir_buf = NULL;
315 if((data_dir = wine_get_data_dir())) {
316 package_dir = data_dir;
317 }else if((data_dir = wine_get_build_dir())) {
318 len = strlen(data_dir);
319 dir_buf = heap_alloc(len + sizeof("/../"));
320 memcpy(dir_buf, data_dir, len);
321 strcpy(dir_buf+len, "/../");
322 package_dir = dir_buf;
327 ret = install_from_unix_file(package_dir, addon->subdir_name, addon->file_name);
330 if (ret == INSTALL_NEXT)
331 ret = install_from_unix_file(INSTALL_DATADIR "/wine/", addon->subdir_name, addon->file_name);
332 if (ret == INSTALL_NEXT && strcmp(INSTALL_DATADIR, "/usr/share"))
333 ret = install_from_unix_file("/usr/share/wine/", addon->subdir_name, addon->file_name);
337 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
338 REFIID riid, void **ppv)
340 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
348 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
353 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
358 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
359 DWORD dwReserved, IBinding *pib)
361 set_status(IDS_DOWNLOADING);
365 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
371 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
377 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
378 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
380 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
383 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
385 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
390 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
391 HRESULT hresult, LPCWSTR szError)
393 if(FAILED(hresult)) {
394 ERR("Binding failed %08x\n", hresult);
398 set_status(IDS_INSTALLING);
402 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
403 DWORD* grfBINDF, BINDINFO* pbindinfo)
410 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
411 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
417 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
418 REFIID riid, IUnknown* punk)
424 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
425 InstallCallback_QueryInterface,
426 InstallCallback_AddRef,
427 InstallCallback_Release,
428 InstallCallback_OnStartBinding,
429 InstallCallback_GetPriority,
430 InstallCallback_OnLowResource,
431 InstallCallback_OnProgress,
432 InstallCallback_OnStopBinding,
433 InstallCallback_GetBindInfo,
434 InstallCallback_OnDataAvailable,
435 InstallCallback_OnObjectAvailable
438 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
440 static void append_url_params( WCHAR *url )
442 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
443 static const WCHAR v_formatW[] = {'&','v','='};
444 DWORD size = INTERNET_MAX_URL_LENGTH * sizeof(WCHAR);
445 DWORD len = strlenW(url);
447 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
448 len += sizeof(arch_formatW)/sizeof(WCHAR);
449 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING),
450 url+len, size/sizeof(WCHAR)-len)-1;
451 memcpy(url+len, v_formatW, sizeof(v_formatW));
452 len += sizeof(v_formatW)/sizeof(WCHAR);
453 MultiByteToWideChar(CP_ACP, 0, addon->version, -1, url+len, size/sizeof(WCHAR)-len);
456 static LPWSTR get_url(void)
458 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
459 WCHAR *url, *config_key;
464 static const WCHAR httpW[] = {'h','t','t','p'};
466 url = heap_alloc(size);
467 returned_size = size;
469 hkey = open_config_key();
472 config_key = heap_strdupAtoW(addon->url_config_key);
473 res = RegQueryValueExW(hkey, config_key, NULL, &type, (LPBYTE)url, &returned_size);
474 heap_free(config_key);
476 if(res == ERROR_SUCCESS && type == REG_SZ) goto found;
479 MultiByteToWideChar( CP_ACP, 0, addon->url_default, -1, url, size / sizeof(WCHAR) );
482 if (returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) append_url_params( url );
484 TRACE("Got URL %s\n", debugstr_w(url));
488 static DWORD WINAPI download_proc(PVOID arg)
490 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
493 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
494 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
496 TRACE("using temp file %s\n", debugstr_w(tmp_file));
498 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
500 ERR("URLDownloadToFile failed: %08x\n", hres);
504 if(sha_check(tmp_file))
505 install_file(tmp_file);
506 DeleteFileW(tmp_file);
507 EndDialog(install_dialog, 0);
511 static void run_winebrowser(const WCHAR *url)
513 PROCESS_INFORMATION pi;
520 static const WCHAR winebrowserW[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
522 url_len = strlenW(url);
524 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(winebrowserW)/sizeof(WCHAR));
525 memcpy(app+len, winebrowserW, sizeof(winebrowserW));
526 len += sizeof(winebrowserW)/sizeof(WCHAR) -1;
528 args = heap_alloc((len+1+url_len)*sizeof(WCHAR));
532 memcpy(args, app, len*sizeof(WCHAR));
534 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
536 TRACE("starting %s\n", debugstr_w(args));
538 memset(&si, 0, sizeof(si));
540 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
543 CloseHandle(pi.hThread);
544 CloseHandle(pi.hProcess);
548 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
552 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
553 install_dialog = hwnd;
557 switch (((NMHDR *)lParam)->code)
561 if (wParam == ID_DWL_STATUS)
562 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
574 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
575 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
576 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
577 CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
585 BOOL install_addon(addon_t addon_type)
590 addon = addons_info+addon_type;
593 * Try to find addon .msi file in following order:
594 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
595 * - $datadir/$addon_subdir/
596 * - $INSTALL_DATADIR/wine/$addon_subdir/
597 * - /usr/share/wine/$addon_subdir/
598 * - download from URL stored in $url_config_key value of HKCU/Wine/Software/$config_key key
600 if (install_from_registered_dir() == INSTALL_NEXT
601 && install_from_default_dir() == INSTALL_NEXT
602 && (url = get_url()))
603 DialogBoxW(hInst, addon->dialog_template, 0, installer_proc);