ntdll: Get the unix tid on DragonFly BSD.
[wine/multimedia.git] / dlls / appwiz.cpl / addons.c
blobfd484877ef2e71dfc18cb84e7b2df7a4c69ea3e6
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 *url_default;
76 const char *config_key;
77 const char *url_config_key;
78 const char *dir_config_key;
79 LPCWSTR dialog_template;
80 } addon_info_t;
82 static const addon_info_t addons_info[] = {
84 GECKO_VERSION,
85 "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi",
86 "gecko",
87 GECKO_SHA,
88 "http://source.winehq.org/winegecko.php",
89 "MSHTML", "GeckoUrl", "GeckoCabDir",
90 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG)
93 MONO_VERSION,
94 "wine-mono-" MONO_VERSION ".msi",
95 "mono",
96 MONO_SHA,
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. */
110 typedef struct {
111 ULONG Unknown[6];
112 ULONG State[5];
113 ULONG Count[2];
114 UCHAR Buffer[64];
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;
124 HANDLE file, map;
125 ULONG sha[5];
126 char buf[2*sizeof(sha)+1];
127 SHA_CTX ctx;
128 DWORD size, i;
130 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
131 if(file == INVALID_HANDLE_VALUE)
132 return FALSE;
134 size = GetFileSize(file, NULL);
136 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
137 CloseHandle(file);
138 if(!map)
139 return FALSE;
141 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
142 CloseHandle(map);
143 if(!file_map)
144 return FALSE;
146 A_SHAInit(&ctx);
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)) {
156 WCHAR message[256];
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);
163 return FALSE;
166 return TRUE;
169 static void set_status(DWORD id)
171 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
172 WCHAR buf[64];
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)
180 ULONG res;
182 res = MsiInstallProductW(file_name, NULL);
183 if(res != ERROR_SUCCESS) {
184 ERR("MsiInstallProduct failed: %u\n", res);
185 return FALSE;
188 return TRUE;
191 static BOOL install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
193 LPWSTR dos_file_name;
194 char *file_path;
195 int fd, len;
196 BOOL ret;
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};
201 len = strlen(dir);
202 file_path = heap_alloc(len+strlen(subdir)+strlen(file_name)+3);
203 if(!file_path)
204 return FALSE;
206 memcpy(file_path, dir, len);
207 if(len && file_path[len-1] != '/' && file_path[len-1] != '\\')
208 file_path[len++] = '/';
209 if(*subdir) {
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);
217 if(fd == -1) {
218 TRACE("%s not found\n", debugstr_a(file_path));
219 heap_free(file_path);
220 return FALSE;
223 close(fd);
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);
230 if(!dos_file_name) {
231 ERR("Could not get dos file name of %s\n", debugstr_a(file_path));
232 heap_free(file_path);
233 return FALSE;
235 } else { /* Windows mode */
236 UINT res;
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);
248 return ret;
251 static HKEY open_config_key(void)
253 HKEY hkey, ret;
254 DWORD res;
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)
261 return NULL;
263 res = RegOpenKeyA(hkey, addon->config_key, &ret);
264 RegCloseKey(hkey);
265 return res == ERROR_SUCCESS ? ret : NULL;
268 static BOOL install_from_registered_dir(void)
270 char *package_dir;
271 HKEY hkey;
272 DWORD res, type, size = MAX_PATH;
273 BOOL ret;
275 hkey = open_config_key();
276 if(!hkey)
277 return FALSE;
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);
285 RegCloseKey(hkey);
286 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
287 heap_free(package_dir);
288 return FALSE;
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);
296 return ret;
299 static BOOL install_from_default_dir(void)
301 const char *data_dir, *package_dir;
302 char *dir_buf = NULL;
303 int len;
304 BOOL ret;
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;
314 }else {
315 return FALSE;
318 ret = install_from_unix_file(package_dir, addon->subdir_name, addon->file_name);
319 heap_free(dir_buf);
321 if (!ret)
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);
325 return ret;
328 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
329 REFIID riid, void **ppv)
331 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
332 *ppv = iface;
333 return S_OK;
336 return E_INVALIDARG;
339 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
341 return 2;
344 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
346 return 1;
349 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
350 DWORD dwReserved, IBinding *pib)
352 set_status(IDS_DOWNLOADING);
353 return S_OK;
356 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
357 LONG *pnPriority)
359 return E_NOTIMPL;
362 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
363 DWORD dwReserved)
365 return E_NOTIMPL;
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);
373 if(ulProgressMax)
374 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
375 if(ulProgress)
376 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
378 return S_OK;
381 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
382 HRESULT hresult, LPCWSTR szError)
384 if(FAILED(hresult)) {
385 ERR("Binding failed %08x\n", hresult);
386 return S_OK;
389 set_status(IDS_INSTALLING);
390 return S_OK;
393 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
394 DWORD* grfBINDF, BINDINFO* pbindinfo)
396 /* FIXME */
397 *grfBINDF = 0;
398 return S_OK;
401 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
402 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
404 ERR("\n");
405 return E_NOTIMPL;
408 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
409 REFIID riid, IUnknown* punk)
411 ERR("\n");
412 return E_NOTIMPL;
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;
451 HKEY hkey;
452 DWORD res, type;
453 DWORD returned_size;
455 static const WCHAR httpW[] = {'h','t','t','p'};
457 url = heap_alloc(size);
458 returned_size = size;
460 hkey = open_config_key();
461 if (hkey)
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);
466 RegCloseKey(hkey);
467 if(res == ERROR_SUCCESS && type == REG_SZ) goto found;
470 MultiByteToWideChar( CP_ACP, 0, addon->url_default, -1, url, size / sizeof(WCHAR) );
472 found:
473 if (returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) append_url_params( url );
475 TRACE("Got URL %s\n", debugstr_w(url));
476 return url;
479 static DWORD WINAPI download_proc(PVOID arg)
481 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
482 HRESULT hres;
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);
490 if(FAILED(hres)) {
491 ERR("URLDownloadToFile failed: %08x\n", hres);
492 return 0;
495 if(sha_check(tmp_file))
496 install_file(tmp_file);
497 DeleteFileW(tmp_file);
498 EndDialog(install_dialog, 0);
499 return 0;
502 static void run_winebrowser(const WCHAR *url)
504 PROCESS_INFORMATION pi;
505 STARTUPINFOW si;
506 WCHAR app[MAX_PATH];
507 LONG len, url_len;
508 WCHAR *args;
509 BOOL ret;
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));
520 if(!args)
521 return;
523 memcpy(args, app, len*sizeof(WCHAR));
524 args[len++] = ' ';
525 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
527 TRACE("starting %s\n", debugstr_w(args));
529 memset(&si, 0, sizeof(si));
530 si.cb = sizeof(si);
531 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
532 heap_free(args);
533 if (ret) {
534 CloseHandle(pi.hThread);
535 CloseHandle(pi.hProcess);
539 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
541 switch(msg) {
542 case WM_INITDIALOG:
543 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
544 install_dialog = hwnd;
545 return TRUE;
547 case WM_NOTIFY:
548 switch (((NMHDR *)lParam)->code)
550 case NM_CLICK:
551 case NM_RETURN:
552 if (wParam == ID_DWL_STATUS)
553 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
554 break;
556 break;
558 case WM_COMMAND:
559 switch(wParam) {
560 case IDCANCEL:
561 EndDialog(hwnd, 0);
562 return FALSE;
564 case ID_DWL_INSTALL:
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));
569 return FALSE;
573 return FALSE;
576 BOOL install_addon(addon_t addon_type)
578 if(!*ARCH_STRING)
579 return FALSE;
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);
596 heap_free(url);
597 url = NULL;
598 return TRUE;