Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / appwiz.cpl / addons.c
blob82a6797abf73492ca979d3c72ab3813e41e70fdf
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.7"
56 #ifdef __i386__
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"
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 enum install_res {
179 INSTALL_OK = 0,
180 INSTALL_FAILED,
181 INSTALL_NEXT,
184 static enum install_res install_file(const WCHAR *file_name)
186 ULONG res;
188 res = MsiInstallProductW(file_name, NULL);
189 if(res != ERROR_SUCCESS) {
190 ERR("MsiInstallProduct failed: %u\n", res);
191 return INSTALL_FAILED;
194 return INSTALL_OK;
197 static enum install_res install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
199 LPWSTR dos_file_name;
200 char *file_path;
201 int fd, len;
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};
207 len = strlen(dir);
208 file_path = heap_alloc(len+strlen(subdir)+strlen(file_name)+3);
209 if(!file_path)
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++] = '/';
215 if(*subdir) {
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);
223 if(fd == -1) {
224 TRACE("%s not found\n", debugstr_a(file_path));
225 heap_free(file_path);
226 return INSTALL_NEXT;
229 close(fd);
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);
236 if(!dos_file_name) {
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 */
242 UINT res;
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);
254 return ret;
257 static HKEY open_config_key(void)
259 HKEY hkey, ret;
260 DWORD res;
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)
267 return NULL;
269 res = RegOpenKeyA(hkey, addon->config_key, &ret);
270 RegCloseKey(hkey);
271 return res == ERROR_SUCCESS ? ret : NULL;
274 static enum install_res install_from_registered_dir(void)
276 char *package_dir;
277 HKEY hkey;
278 DWORD res, type, size = MAX_PATH;
279 enum install_res ret;
281 hkey = open_config_key();
282 if(!hkey)
283 return INSTALL_NEXT;
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);
291 RegCloseKey(hkey);
292 if(res == ERROR_FILE_NOT_FOUND) {
293 heap_free(package_dir);
294 return INSTALL_NEXT;
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);
305 return ret;
308 static enum install_res install_from_default_dir(void)
310 const char *data_dir, *package_dir;
311 char *dir_buf = NULL;
312 int len;
313 BOOL ret;
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;
323 }else {
324 return INSTALL_NEXT;
327 ret = install_from_unix_file(package_dir, addon->subdir_name, addon->file_name);
328 heap_free(dir_buf);
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);
334 return ret;
337 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
338 REFIID riid, void **ppv)
340 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
341 *ppv = iface;
342 return S_OK;
345 return E_INVALIDARG;
348 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
350 return 2;
353 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
355 return 1;
358 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
359 DWORD dwReserved, IBinding *pib)
361 set_status(IDS_DOWNLOADING);
362 return S_OK;
365 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
366 LONG *pnPriority)
368 return E_NOTIMPL;
371 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
372 DWORD dwReserved)
374 return E_NOTIMPL;
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);
382 if(ulProgressMax)
383 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
384 if(ulProgress)
385 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
387 return S_OK;
390 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
391 HRESULT hresult, LPCWSTR szError)
393 if(FAILED(hresult)) {
394 ERR("Binding failed %08x\n", hresult);
395 return S_OK;
398 set_status(IDS_INSTALLING);
399 return S_OK;
402 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
403 DWORD* grfBINDF, BINDINFO* pbindinfo)
405 /* FIXME */
406 *grfBINDF = 0;
407 return S_OK;
410 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
411 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
413 ERR("\n");
414 return E_NOTIMPL;
417 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
418 REFIID riid, IUnknown* punk)
420 ERR("\n");
421 return E_NOTIMPL;
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;
460 HKEY hkey;
461 DWORD res, type;
462 DWORD returned_size;
464 static const WCHAR httpW[] = {'h','t','t','p'};
466 url = heap_alloc(size);
467 returned_size = size;
469 hkey = open_config_key();
470 if (hkey)
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);
475 RegCloseKey(hkey);
476 if(res == ERROR_SUCCESS && type == REG_SZ) goto found;
479 MultiByteToWideChar( CP_ACP, 0, addon->url_default, -1, url, size / sizeof(WCHAR) );
481 found:
482 if (returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) append_url_params( url );
484 TRACE("Got URL %s\n", debugstr_w(url));
485 return url;
488 static DWORD WINAPI download_proc(PVOID arg)
490 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
491 HRESULT hres;
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);
499 if(FAILED(hres)) {
500 ERR("URLDownloadToFile failed: %08x\n", hres);
501 return 0;
504 if(sha_check(tmp_file))
505 install_file(tmp_file);
506 DeleteFileW(tmp_file);
507 EndDialog(install_dialog, 0);
508 return 0;
511 static void run_winebrowser(const WCHAR *url)
513 PROCESS_INFORMATION pi;
514 STARTUPINFOW si;
515 WCHAR app[MAX_PATH];
516 LONG len, url_len;
517 WCHAR *args;
518 BOOL ret;
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));
529 if(!args)
530 return;
532 memcpy(args, app, len*sizeof(WCHAR));
533 args[len++] = ' ';
534 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
536 TRACE("starting %s\n", debugstr_w(args));
538 memset(&si, 0, sizeof(si));
539 si.cb = sizeof(si);
540 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
541 heap_free(args);
542 if (ret) {
543 CloseHandle(pi.hThread);
544 CloseHandle(pi.hProcess);
548 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
550 switch(msg) {
551 case WM_INITDIALOG:
552 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
553 install_dialog = hwnd;
554 return TRUE;
556 case WM_NOTIFY:
557 switch (((NMHDR *)lParam)->code)
559 case NM_CLICK:
560 case NM_RETURN:
561 if (wParam == ID_DWL_STATUS)
562 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
563 break;
565 break;
567 case WM_COMMAND:
568 switch(wParam) {
569 case IDCANCEL:
570 EndDialog(hwnd, 0);
571 return FALSE;
573 case ID_DWL_INSTALL:
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));
578 return FALSE;
582 return FALSE;
585 BOOL install_addon(addon_t addon_type)
587 if(!*ARCH_STRING)
588 return FALSE;
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);
605 heap_free(url);
606 url = NULL;
607 return TRUE;