appwiz.cpl: Replace install_wine_gecko with configurable install_addon.
[wine.git] / dlls / appwiz.cpl / addons.c
blob1cff5c4480fe2ab8c2d20bb76fa37c3d5a805a05
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "cpl.h"
36 #include "winreg.h"
37 #include "ole2.h"
38 #include "commctrl.h"
39 #include "advpub.h"
40 #include "wininet.h"
41 #include "shellapi.h"
42 #include "urlmon.h"
43 #include "msi.h"
45 #include "appwiz.h"
46 #include "res.h"
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"
56 #ifdef __i386__
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"
62 #else
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
65 #endif
67 typedef struct {
68 const char *version;
69 const char *file_name;
70 const char *subdir_name;
71 const char *sha;
72 const char *config_key;
73 const char *url_config_key;
74 const char *dir_config_key;
75 } addon_info_t;
77 static const addon_info_t addons_info[] = {
79 GECKO_VERSION,
80 "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi",
81 "gecko",
82 GECKO_SHA,
83 "MSHTML", "GeckoUrl", "GeckoCabDir"
87 static const addon_info_t *addon;
89 static HWND install_dialog = NULL;
90 static LPWSTR url = NULL;
92 /* SHA definitions are copied from advapi32. They aren't available in headers. */
94 typedef struct {
95 ULONG Unknown[6];
96 ULONG State[5];
97 ULONG Count[2];
98 UCHAR Buffer[64];
99 } SHA_CTX, *PSHA_CTX;
101 void WINAPI A_SHAInit(PSHA_CTX);
102 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
103 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
105 static BOOL sha_check(const WCHAR *file_name)
107 const unsigned char *file_map;
108 HANDLE file, map;
109 ULONG sha[5];
110 char buf[2*sizeof(sha)+1];
111 SHA_CTX ctx;
112 DWORD size, i;
114 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
115 if(file == INVALID_HANDLE_VALUE)
116 return FALSE;
118 size = GetFileSize(file, NULL);
120 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
121 CloseHandle(file);
122 if(!map)
123 return FALSE;
125 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
126 CloseHandle(map);
127 if(!file_map)
128 return FALSE;
130 A_SHAInit(&ctx);
131 A_SHAUpdate(&ctx, file_map, size);
132 A_SHAFinal(&ctx, sha);
134 UnmapViewOfFile(file_map);
136 for(i=0; i < sizeof(sha); i++)
137 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
139 if(strcmp(buf, addon->sha)) {
140 WCHAR message[256];
142 WARN("Got %s, expected %s\n", buf, addon->sha);
144 if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
145 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
147 return FALSE;
150 return TRUE;
153 static void set_status(DWORD id)
155 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
156 WCHAR buf[64];
158 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
159 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
162 static BOOL install_file(const WCHAR *file_name)
164 ULONG res;
166 res = MsiInstallProductW(file_name, NULL);
167 if(res != ERROR_SUCCESS) {
168 ERR("MsiInstallProduct failed: %u\n", res);
169 return FALSE;
172 return TRUE;
175 static BOOL install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
177 LPWSTR dos_file_name;
178 char *file_path;
179 int fd, len;
180 BOOL ret;
182 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
183 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
185 len = strlen(dir);
186 file_path = heap_alloc(len+strlen(subdir)+strlen(file_name)+3);
187 if(!file_path)
188 return FALSE;
190 memcpy(file_path, dir, len);
191 if(len && file_path[len-1] != '/' && file_path[len-1] != '\\')
192 file_path[len++] = '/';
193 if(*subdir) {
194 strcpy(file_path+len, subdir);
195 len += strlen(subdir);
196 file_path[len++] = '/';
198 strcpy(file_path+len, file_name);
200 fd = open(file_path, O_RDONLY);
201 if(fd == -1) {
202 TRACE("%s not found\n", debugstr_a(file_path));
203 heap_free(file_path);
204 return FALSE;
207 close(fd);
209 if(!wine_get_dos_file_name)
210 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
212 if(wine_get_dos_file_name) { /* Wine UNIX mode */
213 dos_file_name = wine_get_dos_file_name(file_path);
214 if(!dos_file_name) {
215 ERR("Could not get dos file name of %s\n", debugstr_a(file_path));
216 heap_free(file_path);
217 return FALSE;
219 } else { /* Windows mode */
220 UINT res;
221 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
222 res = MultiByteToWideChar( CP_ACP, 0, file_path, -1, 0, 0);
223 dos_file_name = heap_alloc (res*sizeof(WCHAR));
224 MultiByteToWideChar( CP_ACP, 0, file_path, -1, dos_file_name, res);
227 heap_free(file_path);
229 ret = install_file(dos_file_name);
231 heap_free(dos_file_name);
232 return ret;
235 static HKEY open_config_key(void)
237 HKEY hkey, ret;
238 DWORD res;
240 static const WCHAR wine_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
242 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
243 res = RegOpenKeyW(HKEY_CURRENT_USER, wine_keyW, &hkey);
244 if(res != ERROR_SUCCESS)
245 return NULL;
247 res = RegOpenKeyA(hkey, addon->config_key, &ret);
248 RegCloseKey(hkey);
249 return res == ERROR_SUCCESS ? ret : NULL;
252 static BOOL install_from_registered_dir(void)
254 char *package_dir;
255 HKEY hkey;
256 DWORD res, type, size = MAX_PATH;
257 BOOL ret;
259 hkey = open_config_key();
260 if(!hkey)
261 return FALSE;
263 package_dir = heap_alloc(size);
264 res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
265 if(res == ERROR_MORE_DATA) {
266 package_dir = heap_realloc(package_dir, size);
267 res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
269 RegCloseKey(hkey);
270 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
271 heap_free(package_dir);
272 return FALSE;
275 TRACE("Trying %s/%s\n", debugstr_a(package_dir), debugstr_a(addon->file_name));
277 ret = install_from_unix_file(package_dir, "", addon->file_name);
279 heap_free(package_dir);
280 return ret;
283 static BOOL install_from_default_dir(void)
285 const char *data_dir, *package_dir;
286 char *dir_buf = NULL;
287 int len;
288 BOOL ret;
290 if((data_dir = wine_get_data_dir())) {
291 package_dir = data_dir;
292 }else if((data_dir = wine_get_build_dir())) {
293 len = strlen(data_dir);
294 dir_buf = heap_alloc(len + sizeof("/../"));
295 memcpy(dir_buf, data_dir, len);
296 strcpy(dir_buf+len, "/../");
297 package_dir = dir_buf;
298 }else {
299 return FALSE;
302 ret = install_from_unix_file(package_dir, addon->subdir_name, addon->file_name);
303 heap_free(dir_buf);
305 if (!ret)
306 ret = install_from_unix_file(INSTALL_DATADIR "/wine/", addon->subdir_name, addon->file_name);
307 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
308 ret = install_from_unix_file("/usr/share/wine/", addon->subdir_name, addon->file_name);
309 return ret;
312 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
313 REFIID riid, void **ppv)
315 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
316 *ppv = iface;
317 return S_OK;
320 return E_INVALIDARG;
323 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
325 return 2;
328 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
330 return 1;
333 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
334 DWORD dwReserved, IBinding *pib)
336 set_status(IDS_DOWNLOADING);
337 return S_OK;
340 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
341 LONG *pnPriority)
343 return E_NOTIMPL;
346 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
347 DWORD dwReserved)
349 return E_NOTIMPL;
352 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
353 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
355 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
357 if(ulProgressMax)
358 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
359 if(ulProgress)
360 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
362 return S_OK;
365 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
366 HRESULT hresult, LPCWSTR szError)
368 if(FAILED(hresult)) {
369 ERR("Binding failed %08x\n", hresult);
370 return S_OK;
373 set_status(IDS_INSTALLING);
374 return S_OK;
377 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
378 DWORD* grfBINDF, BINDINFO* pbindinfo)
380 /* FIXME */
381 *grfBINDF = 0;
382 return S_OK;
385 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
386 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
388 ERR("\n");
389 return E_NOTIMPL;
392 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
393 REFIID riid, IUnknown* punk)
395 ERR("\n");
396 return E_NOTIMPL;
399 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
400 InstallCallback_QueryInterface,
401 InstallCallback_AddRef,
402 InstallCallback_Release,
403 InstallCallback_OnStartBinding,
404 InstallCallback_GetPriority,
405 InstallCallback_OnLowResource,
406 InstallCallback_OnProgress,
407 InstallCallback_OnStopBinding,
408 InstallCallback_GetBindInfo,
409 InstallCallback_OnDataAvailable,
410 InstallCallback_OnObjectAvailable
413 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
415 static LPWSTR get_url(void)
417 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
418 WCHAR *url, *config_key;
419 HKEY hkey;
420 DWORD res, type;
421 DWORD returned_size;
423 static const WCHAR httpW[] = {'h','t','t','p'};
424 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
425 static const WCHAR v_formatW[] = {'&','v','='};
427 hkey = open_config_key();
428 if(!hkey)
429 return NULL;
431 url = heap_alloc(size);
432 returned_size = size;
434 config_key = heap_strdupAtoW(addon->url_config_key);
435 res = RegQueryValueExW(hkey, config_key, NULL, &type, (LPBYTE)url, &returned_size);
436 heap_free(config_key);
437 RegCloseKey(hkey);
438 if(res != ERROR_SUCCESS || type != REG_SZ) {
439 heap_free(url);
440 return NULL;
443 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
444 DWORD len;
446 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), url+len, size/sizeof(WCHAR)-len)-1;
450 memcpy(url+len, v_formatW, sizeof(v_formatW));
451 len += sizeof(v_formatW)/sizeof(WCHAR);
452 MultiByteToWideChar(CP_ACP, 0, addon->version, -1, url+len, size/sizeof(WCHAR)-len);
455 TRACE("Got URL %s\n", debugstr_w(url));
456 return url;
459 static DWORD WINAPI download_proc(PVOID arg)
461 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
462 HRESULT hres;
464 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
465 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
467 TRACE("using temp file %s\n", debugstr_w(tmp_file));
469 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
470 if(FAILED(hres)) {
471 ERR("URLDownloadToFile failed: %08x\n", hres);
472 return 0;
475 if(sha_check(tmp_file))
476 install_file(tmp_file);
477 DeleteFileW(tmp_file);
478 EndDialog(install_dialog, 0);
479 return 0;
482 static void run_winebrowser(const WCHAR *url)
484 PROCESS_INFORMATION pi;
485 STARTUPINFOW si;
486 WCHAR app[MAX_PATH];
487 LONG len, url_len;
488 WCHAR *args;
489 BOOL ret;
491 static const WCHAR winebrowserW[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
493 url_len = strlenW(url);
495 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(winebrowserW)/sizeof(WCHAR));
496 memcpy(app+len, winebrowserW, sizeof(winebrowserW));
497 len += sizeof(winebrowserW)/sizeof(WCHAR) -1;
499 args = heap_alloc((len+1+url_len)*sizeof(WCHAR));
500 if(!args)
501 return;
503 memcpy(args, app, len*sizeof(WCHAR));
504 args[len++] = ' ';
505 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
507 TRACE("starting %s\n", debugstr_w(args));
509 memset(&si, 0, sizeof(si));
510 si.cb = sizeof(si);
511 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
512 heap_free(args);
513 if (ret) {
514 CloseHandle(pi.hThread);
515 CloseHandle(pi.hProcess);
519 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
521 switch(msg) {
522 case WM_INITDIALOG:
523 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
524 install_dialog = hwnd;
525 return TRUE;
527 case WM_NOTIFY:
528 switch (((NMHDR *)lParam)->code)
530 case NM_CLICK:
531 case NM_RETURN:
532 if (wParam == ID_DWL_STATUS)
533 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
534 break;
536 break;
538 case WM_COMMAND:
539 switch(wParam) {
540 case IDCANCEL:
541 EndDialog(hwnd, 0);
542 return FALSE;
544 case ID_DWL_INSTALL:
545 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
546 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
547 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
548 CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
549 return FALSE;
553 return FALSE;
556 BOOL install_addon(addon_t addon_type)
558 if(!*ARCH_STRING)
559 return FALSE;
561 addon = addons_info+addon_type;
564 * Try to find addon .msi file in following order:
565 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
566 * - $datadir/$addon_subdir/
567 * - $INSTALL_DATADIR/wine/$addon_subdir/
568 * - /usr/share/wine/$addon_subdir/
569 * - download from URL stored in $url_config_key value of HKCU/Wine/Software/$config_key key
571 if(!install_from_registered_dir()
572 && !install_from_default_dir()
573 && (url = get_url()))
574 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
576 heap_free(url);
577 url = NULL;
578 return TRUE;