services: Move service cleanup code to separate function.
[wine/multimedia.git] / dlls / appwiz.cpl / addons.c
blob9a410b820b7cd3e6cb8a42b0d70e6f2cc30d7c48
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.4"
56 #ifdef __i386__
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "c30aa99621e98336eb4b7e2074118b8af8ea2ad5"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "bf0aaf56a8cf9abd75be02b56b05e5c4e9a4df93"
62 #else
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
65 #endif
67 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi"
69 static const WCHAR mshtml_keyW[] =
70 {'S','o','f','t','w','a','r','e',
71 '\\','W','i','n','e',
72 '\\','M','S','H','T','M','L',0};
74 static HWND install_dialog = NULL;
75 static LPWSTR url = NULL;
77 /* SHA definitions are copied from advapi32. They aren't available in headers. */
79 typedef struct {
80 ULONG Unknown[6];
81 ULONG State[5];
82 ULONG Count[2];
83 UCHAR Buffer[64];
84 } SHA_CTX, *PSHA_CTX;
86 void WINAPI A_SHAInit(PSHA_CTX);
87 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
88 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
90 static BOOL sha_check(const WCHAR *file_name)
92 const unsigned char *file_map;
93 HANDLE file, map;
94 ULONG sha[5];
95 char buf[2*sizeof(sha)+1];
96 SHA_CTX ctx;
97 DWORD size, i;
99 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
100 if(file == INVALID_HANDLE_VALUE)
101 return FALSE;
103 size = GetFileSize(file, NULL);
105 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
106 CloseHandle(file);
107 if(!map)
108 return FALSE;
110 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
111 CloseHandle(map);
112 if(!file_map)
113 return FALSE;
115 A_SHAInit(&ctx);
116 A_SHAUpdate(&ctx, file_map, size);
117 A_SHAFinal(&ctx, sha);
119 UnmapViewOfFile(file_map);
121 for(i=0; i < sizeof(sha); i++)
122 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
124 if(strcmp(buf, GECKO_SHA)) {
125 WCHAR message[256];
127 WARN("Got %s, expected %s\n", buf, GECKO_SHA);
129 if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
130 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
132 return FALSE;
135 return TRUE;
138 static void set_status(DWORD id)
140 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
141 WCHAR buf[64];
143 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
144 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
147 static BOOL install_file(const WCHAR *file_name)
149 ULONG res;
151 res = MsiInstallProductW(file_name, NULL);
152 if(res != ERROR_SUCCESS) {
153 ERR("MsiInstallProduct failed: %u\n", res);
154 return FALSE;
157 return TRUE;
160 static BOOL install_from_unix_file(const char *file_name)
162 LPWSTR dos_file_name;
163 int fd;
164 BOOL ret;
166 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
167 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
169 fd = open(file_name, O_RDONLY);
170 if(fd == -1) {
171 TRACE("%s not found\n", debugstr_a(file_name));
172 return FALSE;
175 close(fd);
177 if(!wine_get_dos_file_name)
178 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
180 if(wine_get_dos_file_name) { /* Wine UNIX mode */
181 dos_file_name = wine_get_dos_file_name(file_name);
182 if(!dos_file_name) {
183 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
184 return FALSE;
186 } else { /* Windows mode */
187 UINT res;
188 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
189 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
190 dos_file_name = heap_alloc (res*sizeof(WCHAR));
191 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
194 ret = install_file(dos_file_name);
196 heap_free(dos_file_name);
197 return ret;
200 static BOOL install_from_registered_dir(void)
202 char *file_name;
203 HKEY hkey;
204 DWORD res, type, size = MAX_PATH;
205 BOOL ret;
207 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
208 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
209 if(res != ERROR_SUCCESS)
210 return FALSE;
212 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
213 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
214 if(res == ERROR_MORE_DATA) {
215 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
216 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
218 RegCloseKey(hkey);
219 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
220 heap_free(file_name);
221 return FALSE;
224 strcat(file_name, GECKO_FILE_NAME);
226 TRACE("Trying %s\n", debugstr_a(file_name));
228 ret = install_from_unix_file(file_name);
230 heap_free(file_name);
231 return ret;
234 static BOOL install_from_default_dir(void)
236 const char *data_dir, *subdir;
237 char *file_name;
238 int len, len2;
239 BOOL ret;
241 if((data_dir = wine_get_data_dir()))
242 subdir = "/gecko/";
243 else if((data_dir = wine_get_build_dir()))
244 subdir = "/../gecko/";
245 else
246 return FALSE;
248 len = strlen(data_dir);
249 len2 = strlen(subdir);
251 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
252 memcpy(file_name, data_dir, len);
253 memcpy(file_name+len, subdir, len2);
254 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
256 ret = install_from_unix_file(file_name);
258 heap_free(file_name);
260 if (!ret)
261 ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
262 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
263 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
264 return ret;
267 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
268 REFIID riid, void **ppv)
270 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
271 *ppv = iface;
272 return S_OK;
275 return E_INVALIDARG;
278 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
280 return 2;
283 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
285 return 1;
288 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
289 DWORD dwReserved, IBinding *pib)
291 set_status(IDS_DOWNLOADING);
292 return S_OK;
295 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
296 LONG *pnPriority)
298 return E_NOTIMPL;
301 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
302 DWORD dwReserved)
304 return E_NOTIMPL;
307 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
308 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
310 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
312 if(ulProgressMax)
313 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
314 if(ulProgress)
315 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
317 return S_OK;
320 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
321 HRESULT hresult, LPCWSTR szError)
323 if(FAILED(hresult)) {
324 ERR("Binding failed %08x\n", hresult);
325 return S_OK;
328 set_status(IDS_INSTALLING);
329 return S_OK;
332 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
333 DWORD* grfBINDF, BINDINFO* pbindinfo)
335 /* FIXME */
336 *grfBINDF = 0;
337 return S_OK;
340 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
341 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
343 ERR("\n");
344 return E_NOTIMPL;
347 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
348 REFIID riid, IUnknown* punk)
350 ERR("\n");
351 return E_NOTIMPL;
354 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
355 InstallCallback_QueryInterface,
356 InstallCallback_AddRef,
357 InstallCallback_Release,
358 InstallCallback_OnStartBinding,
359 InstallCallback_GetPriority,
360 InstallCallback_OnLowResource,
361 InstallCallback_OnProgress,
362 InstallCallback_OnStopBinding,
363 InstallCallback_GetBindInfo,
364 InstallCallback_OnDataAvailable,
365 InstallCallback_OnObjectAvailable
368 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
370 static LPWSTR get_url(void)
372 HKEY hkey;
373 DWORD res, type;
374 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
375 DWORD returned_size;
376 LPWSTR url;
378 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
379 static const WCHAR httpW[] = {'h','t','t','p'};
380 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
381 static const WCHAR v_formatW[] = {'&','v','='};
383 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
384 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
385 if(res != ERROR_SUCCESS)
386 return NULL;
388 url = heap_alloc(size);
389 returned_size = size;
391 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
392 RegCloseKey(hkey);
393 if(res != ERROR_SUCCESS || type != REG_SZ) {
394 heap_free(url);
395 return NULL;
398 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
399 DWORD len;
401 len = strlenW(url);
402 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
403 len += sizeof(arch_formatW)/sizeof(WCHAR);
404 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
405 memcpy(url+len, v_formatW, sizeof(v_formatW));
406 len += sizeof(v_formatW)/sizeof(WCHAR);
407 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
410 TRACE("Got URL %s\n", debugstr_w(url));
411 return url;
414 static DWORD WINAPI download_proc(PVOID arg)
416 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
417 HRESULT hres;
419 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
420 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
422 TRACE("using temp file %s\n", debugstr_w(tmp_file));
424 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
425 if(FAILED(hres)) {
426 ERR("URLDownloadToFile failed: %08x\n", hres);
427 return 0;
430 if(sha_check(tmp_file))
431 install_file(tmp_file);
432 DeleteFileW(tmp_file);
433 EndDialog(install_dialog, 0);
434 return 0;
437 static void run_winebrowser(const WCHAR *url)
439 PROCESS_INFORMATION pi;
440 STARTUPINFOW si;
441 WCHAR app[MAX_PATH];
442 LONG len, url_len;
443 WCHAR *args;
444 BOOL ret;
446 static const WCHAR winebrowserW[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
448 url_len = strlenW(url);
450 len = GetSystemDirectoryW(app, MAX_PATH-sizeof(winebrowserW)/sizeof(WCHAR));
451 memcpy(app+len, winebrowserW, sizeof(winebrowserW));
452 len += sizeof(winebrowserW)/sizeof(WCHAR) -1;
454 args = heap_alloc((len+1+url_len)*sizeof(WCHAR));
455 if(!args)
456 return;
458 memcpy(args, app, len*sizeof(WCHAR));
459 args[len++] = ' ';
460 memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
462 TRACE("starting %s\n", debugstr_w(args));
464 memset(&si, 0, sizeof(si));
465 si.cb = sizeof(si);
466 ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
467 heap_free(args);
468 if (ret) {
469 CloseHandle(pi.hThread);
470 CloseHandle(pi.hProcess);
474 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
476 switch(msg) {
477 case WM_INITDIALOG:
478 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
479 install_dialog = hwnd;
480 return TRUE;
482 case WM_NOTIFY:
483 switch (((NMHDR *)lParam)->code)
485 case NM_CLICK:
486 case NM_RETURN:
487 if (wParam == ID_DWL_STATUS)
488 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
489 break;
491 break;
493 case WM_COMMAND:
494 switch(wParam) {
495 case IDCANCEL:
496 EndDialog(hwnd, 0);
497 return FALSE;
499 case ID_DWL_INSTALL:
500 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
501 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
502 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
503 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
504 return FALSE;
508 return FALSE;
511 BOOL install_wine_gecko(void)
513 if(!*ARCH_STRING)
514 return FALSE;
517 * Try to find Gecko .cab file in following order:
518 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
519 * - $datadir/gecko/
520 * - $INSTALL_DATADIR/wine/gecko/
521 * - /usr/share/wine/gecko/
522 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
524 if(!install_from_registered_dir()
525 && !install_from_default_dir()
526 && (url = get_url()))
527 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
529 heap_free(url);
530 url = NULL;
531 return TRUE;