gdi32: Pass a rop_mask_bits object to the pattern_rects primitive.
[wine/multimedia.git] / dlls / appwiz.cpl / addons.c
blob4a5acba06093e1bcc10def185b70b0e17849b1e8
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 #define MONO_VERSION "0.0.4"
68 #define MONO_SHA "7d827f7d28a88ae0da95a136573783124ffce4b1"
70 typedef struct {
71 const char *version;
72 const char *file_name;
73 const char *subdir_name;
74 const char *sha;
75 const char *config_key;
76 const char *url_config_key;
77 const char *dir_config_key;
78 LPCWSTR dialog_template;
79 } addon_info_t;
81 static const addon_info_t addons_info[] = {
83 GECKO_VERSION,
84 "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi",
85 "gecko",
86 GECKO_SHA,
87 "MSHTML", "GeckoUrl", "GeckoCabDir",
88 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG)
91 MONO_VERSION,
92 "wine-mono-" MONO_VERSION ".msi",
93 "mono",
94 MONO_SHA,
95 "Dotnet", "MonoUrl", "MonoCabDir",
96 MAKEINTRESOURCEW(ID_DWL_MONO_DIALOG)
100 static const addon_info_t *addon;
102 static HWND install_dialog = NULL;
103 static LPWSTR url = NULL;
105 /* SHA definitions are copied from advapi32. They aren't available in headers. */
107 typedef struct {
108 ULONG Unknown[6];
109 ULONG State[5];
110 ULONG Count[2];
111 UCHAR Buffer[64];
112 } SHA_CTX, *PSHA_CTX;
114 void WINAPI A_SHAInit(PSHA_CTX);
115 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
116 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
118 static BOOL sha_check(const WCHAR *file_name)
120 const unsigned char *file_map;
121 HANDLE file, map;
122 ULONG sha[5];
123 char buf[2*sizeof(sha)+1];
124 SHA_CTX ctx;
125 DWORD size, i;
127 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
128 if(file == INVALID_HANDLE_VALUE)
129 return FALSE;
131 size = GetFileSize(file, NULL);
133 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
134 CloseHandle(file);
135 if(!map)
136 return FALSE;
138 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
139 CloseHandle(map);
140 if(!file_map)
141 return FALSE;
143 A_SHAInit(&ctx);
144 A_SHAUpdate(&ctx, file_map, size);
145 A_SHAFinal(&ctx, sha);
147 UnmapViewOfFile(file_map);
149 for(i=0; i < sizeof(sha); i++)
150 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
152 if(strcmp(buf, addon->sha)) {
153 WCHAR message[256];
155 WARN("Got %s, expected %s\n", buf, addon->sha);
157 if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
158 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
160 return FALSE;
163 return TRUE;
166 static void set_status(DWORD id)
168 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
169 WCHAR buf[64];
171 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
172 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
175 static BOOL install_file(const WCHAR *file_name)
177 ULONG res;
179 res = MsiInstallProductW(file_name, NULL);
180 if(res != ERROR_SUCCESS) {
181 ERR("MsiInstallProduct failed: %u\n", res);
182 return FALSE;
185 return TRUE;
188 static BOOL install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
190 LPWSTR dos_file_name;
191 char *file_path;
192 int fd, len;
193 BOOL ret;
195 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
196 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
198 len = strlen(dir);
199 file_path = heap_alloc(len+strlen(subdir)+strlen(file_name)+3);
200 if(!file_path)
201 return FALSE;
203 memcpy(file_path, dir, len);
204 if(len && file_path[len-1] != '/' && file_path[len-1] != '\\')
205 file_path[len++] = '/';
206 if(*subdir) {
207 strcpy(file_path+len, subdir);
208 len += strlen(subdir);
209 file_path[len++] = '/';
211 strcpy(file_path+len, file_name);
213 fd = open(file_path, O_RDONLY);
214 if(fd == -1) {
215 TRACE("%s not found\n", debugstr_a(file_path));
216 heap_free(file_path);
217 return FALSE;
220 close(fd);
222 if(!wine_get_dos_file_name)
223 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
225 if(wine_get_dos_file_name) { /* Wine UNIX mode */
226 dos_file_name = wine_get_dos_file_name(file_path);
227 if(!dos_file_name) {
228 ERR("Could not get dos file name of %s\n", debugstr_a(file_path));
229 heap_free(file_path);
230 return FALSE;
232 } else { /* Windows mode */
233 UINT res;
234 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
235 res = MultiByteToWideChar( CP_ACP, 0, file_path, -1, 0, 0);
236 dos_file_name = heap_alloc (res*sizeof(WCHAR));
237 MultiByteToWideChar( CP_ACP, 0, file_path, -1, dos_file_name, res);
240 heap_free(file_path);
242 ret = install_file(dos_file_name);
244 heap_free(dos_file_name);
245 return ret;
248 static HKEY open_config_key(void)
250 HKEY hkey, ret;
251 DWORD res;
253 static const WCHAR wine_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
255 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
256 res = RegOpenKeyW(HKEY_CURRENT_USER, wine_keyW, &hkey);
257 if(res != ERROR_SUCCESS)
258 return NULL;
260 res = RegOpenKeyA(hkey, addon->config_key, &ret);
261 RegCloseKey(hkey);
262 return res == ERROR_SUCCESS ? ret : NULL;
265 static BOOL install_from_registered_dir(void)
267 char *package_dir;
268 HKEY hkey;
269 DWORD res, type, size = MAX_PATH;
270 BOOL ret;
272 hkey = open_config_key();
273 if(!hkey)
274 return FALSE;
276 package_dir = heap_alloc(size);
277 res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
278 if(res == ERROR_MORE_DATA) {
279 package_dir = heap_realloc(package_dir, size);
280 res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
282 RegCloseKey(hkey);
283 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
284 heap_free(package_dir);
285 return FALSE;
288 TRACE("Trying %s/%s\n", debugstr_a(package_dir), debugstr_a(addon->file_name));
290 ret = install_from_unix_file(package_dir, "", addon->file_name);
292 heap_free(package_dir);
293 return ret;
296 static BOOL install_from_default_dir(void)
298 const char *data_dir, *package_dir;
299 char *dir_buf = NULL;
300 int len;
301 BOOL ret;
303 if((data_dir = wine_get_data_dir())) {
304 package_dir = data_dir;
305 }else if((data_dir = wine_get_build_dir())) {
306 len = strlen(data_dir);
307 dir_buf = heap_alloc(len + sizeof("/../"));
308 memcpy(dir_buf, data_dir, len);
309 strcpy(dir_buf+len, "/../");
310 package_dir = dir_buf;
311 }else {
312 return FALSE;
315 ret = install_from_unix_file(package_dir, addon->subdir_name, addon->file_name);
316 heap_free(dir_buf);
318 if (!ret)
319 ret = install_from_unix_file(INSTALL_DATADIR "/wine/", addon->subdir_name, addon->file_name);
320 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
321 ret = install_from_unix_file("/usr/share/wine/", addon->subdir_name, addon->file_name);
322 return ret;
325 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
326 REFIID riid, void **ppv)
328 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
329 *ppv = iface;
330 return S_OK;
333 return E_INVALIDARG;
336 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
338 return 2;
341 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
343 return 1;
346 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
347 DWORD dwReserved, IBinding *pib)
349 set_status(IDS_DOWNLOADING);
350 return S_OK;
353 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
354 LONG *pnPriority)
356 return E_NOTIMPL;
359 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
360 DWORD dwReserved)
362 return E_NOTIMPL;
365 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
366 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
368 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
370 if(ulProgressMax)
371 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
372 if(ulProgress)
373 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
375 return S_OK;
378 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
379 HRESULT hresult, LPCWSTR szError)
381 if(FAILED(hresult)) {
382 ERR("Binding failed %08x\n", hresult);
383 return S_OK;
386 set_status(IDS_INSTALLING);
387 return S_OK;
390 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
391 DWORD* grfBINDF, BINDINFO* pbindinfo)
393 /* FIXME */
394 *grfBINDF = 0;
395 return S_OK;
398 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
399 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
401 ERR("\n");
402 return E_NOTIMPL;
405 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
406 REFIID riid, IUnknown* punk)
408 ERR("\n");
409 return E_NOTIMPL;
412 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
413 InstallCallback_QueryInterface,
414 InstallCallback_AddRef,
415 InstallCallback_Release,
416 InstallCallback_OnStartBinding,
417 InstallCallback_GetPriority,
418 InstallCallback_OnLowResource,
419 InstallCallback_OnProgress,
420 InstallCallback_OnStopBinding,
421 InstallCallback_GetBindInfo,
422 InstallCallback_OnDataAvailable,
423 InstallCallback_OnObjectAvailable
426 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
428 static LPWSTR get_url(void)
430 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
431 WCHAR *url, *config_key;
432 HKEY hkey;
433 DWORD res, type;
434 DWORD returned_size;
436 static const WCHAR httpW[] = {'h','t','t','p'};
437 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
438 static const WCHAR v_formatW[] = {'&','v','='};
440 hkey = open_config_key();
441 if(!hkey)
442 return NULL;
444 url = heap_alloc(size);
445 returned_size = size;
447 config_key = heap_strdupAtoW(addon->url_config_key);
448 res = RegQueryValueExW(hkey, config_key, NULL, &type, (LPBYTE)url, &returned_size);
449 heap_free(config_key);
450 RegCloseKey(hkey);
451 if(res != ERROR_SUCCESS || type != REG_SZ) {
452 heap_free(url);
453 return NULL;
456 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
457 DWORD len;
459 len = strlenW(url);
460 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
461 len += sizeof(arch_formatW)/sizeof(WCHAR);
462 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
463 memcpy(url+len, v_formatW, sizeof(v_formatW));
464 len += sizeof(v_formatW)/sizeof(WCHAR);
465 MultiByteToWideChar(CP_ACP, 0, addon->version, -1, url+len, size/sizeof(WCHAR)-len);
468 TRACE("Got URL %s\n", debugstr_w(url));
469 return url;
472 static DWORD WINAPI download_proc(PVOID arg)
474 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
475 HRESULT hres;
477 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
478 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
480 TRACE("using temp file %s\n", debugstr_w(tmp_file));
482 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
483 if(FAILED(hres)) {
484 ERR("URLDownloadToFile failed: %08x\n", hres);
485 return 0;
488 if(sha_check(tmp_file))
489 install_file(tmp_file);
490 DeleteFileW(tmp_file);
491 EndDialog(install_dialog, 0);
492 return 0;
495 static void run_winebrowser(const WCHAR *url)
497 PROCESS_INFORMATION pi;
498 STARTUPINFOW si;
499 WCHAR app[MAX_PATH];
500 LONG len, url_len;
501 WCHAR *args;
502 BOOL ret;
504 static const WCHAR winebrowserW[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
506 url_len = strlenW(url);
508 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(winebrowserW)/sizeof(WCHAR));
509 memcpy(app+len, winebrowserW, sizeof(winebrowserW));
510 len += sizeof(winebrowserW)/sizeof(WCHAR) -1;
512 args = heap_alloc((len+1+url_len)*sizeof(WCHAR));
513 if(!args)
514 return;
516 memcpy(args, app, len*sizeof(WCHAR));
517 args[len++] = ' ';
518 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
520 TRACE("starting %s\n", debugstr_w(args));
522 memset(&si, 0, sizeof(si));
523 si.cb = sizeof(si);
524 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
525 heap_free(args);
526 if (ret) {
527 CloseHandle(pi.hThread);
528 CloseHandle(pi.hProcess);
532 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
534 switch(msg) {
535 case WM_INITDIALOG:
536 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
537 install_dialog = hwnd;
538 return TRUE;
540 case WM_NOTIFY:
541 switch (((NMHDR *)lParam)->code)
543 case NM_CLICK:
544 case NM_RETURN:
545 if (wParam == ID_DWL_STATUS)
546 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
547 break;
549 break;
551 case WM_COMMAND:
552 switch(wParam) {
553 case IDCANCEL:
554 EndDialog(hwnd, 0);
555 return FALSE;
557 case ID_DWL_INSTALL:
558 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
559 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
560 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
561 CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
562 return FALSE;
566 return FALSE;
569 BOOL install_addon(addon_t addon_type)
571 if(!*ARCH_STRING)
572 return FALSE;
574 addon = addons_info+addon_type;
577 * Try to find addon .msi file in following order:
578 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
579 * - $datadir/$addon_subdir/
580 * - $INSTALL_DATADIR/wine/$addon_subdir/
581 * - /usr/share/wine/$addon_subdir/
582 * - download from URL stored in $url_config_key value of HKCU/Wine/Software/$config_key key
584 if(!install_from_registered_dir()
585 && !install_from_default_dir()
586 && (url = get_url()))
587 DialogBoxW(hInst, addon->dialog_template, 0, installer_proc);
589 heap_free(url);
590 url = NULL;
591 return TRUE;