wined3d: Make the device parameter to wined3d_device_get_vertex_declaration() const.
[wine/multimedia.git] / dlls / appwiz.cpl / addons.c
blob95e4fe89fca254658055f2ee46a8c31340180897
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.3"
56 #ifdef __i386__
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "acc6a5bc15ebb3574e00f8ef4f23912239658b41"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "5bcf29c48677dffa7a9112d481f7f5474cd255d4"
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 static inline char *heap_strdupWtoA(LPCWSTR str)
79 char *ret = NULL;
81 if(str) {
82 DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
83 ret = heap_alloc(size);
84 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
87 return ret;
90 /* SHA definitions are copied from advapi32. They aren't available in headers. */
92 typedef struct {
93 ULONG Unknown[6];
94 ULONG State[5];
95 ULONG Count[2];
96 UCHAR Buffer[64];
97 } SHA_CTX, *PSHA_CTX;
99 void WINAPI A_SHAInit(PSHA_CTX);
100 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
101 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
103 static BOOL sha_check(const WCHAR *file_name)
105 const unsigned char *file_map;
106 HANDLE file, map;
107 ULONG sha[5];
108 char buf[2*sizeof(sha)+1];
109 SHA_CTX ctx;
110 DWORD size, i;
112 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
113 if(file == INVALID_HANDLE_VALUE)
114 return FALSE;
116 size = GetFileSize(file, NULL);
118 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
119 CloseHandle(file);
120 if(!map)
121 return FALSE;
123 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
124 CloseHandle(map);
125 if(!file_map)
126 return FALSE;
128 A_SHAInit(&ctx);
129 A_SHAUpdate(&ctx, file_map, size);
130 A_SHAFinal(&ctx, sha);
132 UnmapViewOfFile(file_map);
134 for(i=0; i < sizeof(sha); i++)
135 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
137 if(strcmp(buf, GECKO_SHA)) {
138 WCHAR message[256];
140 WARN("Got %s, expected %s\n", buf, GECKO_SHA);
142 if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
143 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
145 return FALSE;
148 return TRUE;
151 static void set_status(DWORD id)
153 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
154 WCHAR buf[64];
156 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
157 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
160 static BOOL install_file(const WCHAR *file_name)
162 ULONG res;
164 res = MsiInstallProductW(file_name, NULL);
165 if(res != ERROR_SUCCESS) {
166 ERR("MsiInstallProduct failed: %u\n", res);
167 return FALSE;
170 return TRUE;
173 static BOOL install_from_unix_file(const char *file_name)
175 LPWSTR dos_file_name;
176 int fd;
177 BOOL ret;
179 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
180 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
182 fd = open(file_name, O_RDONLY);
183 if(fd == -1) {
184 TRACE("%s not found\n", debugstr_a(file_name));
185 return FALSE;
188 close(fd);
190 if(!wine_get_dos_file_name)
191 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
193 if(wine_get_dos_file_name) { /* Wine UNIX mode */
194 dos_file_name = wine_get_dos_file_name(file_name);
195 if(!dos_file_name) {
196 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
197 return FALSE;
199 } else { /* Windows mode */
200 UINT res;
201 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
202 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
203 dos_file_name = heap_alloc (res*sizeof(WCHAR));
204 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
207 ret = install_file(dos_file_name);
209 heap_free(dos_file_name);
210 return ret;
213 static BOOL install_from_registered_dir(void)
215 char *file_name;
216 HKEY hkey;
217 DWORD res, type, size = MAX_PATH;
218 BOOL ret;
220 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
221 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
222 if(res != ERROR_SUCCESS)
223 return FALSE;
225 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
226 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
227 if(res == ERROR_MORE_DATA) {
228 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
229 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
231 RegCloseKey(hkey);
232 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
233 heap_free(file_name);
234 return FALSE;
237 strcat(file_name, GECKO_FILE_NAME);
239 TRACE("Trying %s\n", debugstr_a(file_name));
241 ret = install_from_unix_file(file_name);
243 heap_free(file_name);
244 return ret;
247 static BOOL install_from_default_dir(void)
249 const char *data_dir, *subdir;
250 char *file_name;
251 int len, len2;
252 BOOL ret;
254 if((data_dir = wine_get_data_dir()))
255 subdir = "/gecko/";
256 else if((data_dir = wine_get_build_dir()))
257 subdir = "/../gecko/";
258 else
259 return FALSE;
261 len = strlen(data_dir);
262 len2 = strlen(subdir);
264 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
265 memcpy(file_name, data_dir, len);
266 memcpy(file_name+len, subdir, len2);
267 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
269 ret = install_from_unix_file(file_name);
271 heap_free(file_name);
273 if (!ret)
274 ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
275 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
276 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
277 return ret;
280 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
281 REFIID riid, void **ppv)
283 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
284 *ppv = iface;
285 return S_OK;
288 return E_INVALIDARG;
291 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
293 return 2;
296 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
298 return 1;
301 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
302 DWORD dwReserved, IBinding *pib)
304 set_status(IDS_DOWNLOADING);
305 return S_OK;
308 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
309 LONG *pnPriority)
311 return E_NOTIMPL;
314 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
315 DWORD dwReserved)
317 return E_NOTIMPL;
320 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
321 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
323 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
325 if(ulProgressMax)
326 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
327 if(ulProgress)
328 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
330 return S_OK;
333 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
334 HRESULT hresult, LPCWSTR szError)
336 if(FAILED(hresult)) {
337 ERR("Binding failed %08x\n", hresult);
338 return S_OK;
341 set_status(IDS_INSTALLING);
342 return S_OK;
345 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
346 DWORD* grfBINDF, BINDINFO* pbindinfo)
348 /* FIXME */
349 *grfBINDF = 0;
350 return S_OK;
353 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
354 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
356 ERR("\n");
357 return E_NOTIMPL;
360 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
361 REFIID riid, IUnknown* punk)
363 ERR("\n");
364 return E_NOTIMPL;
367 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
368 InstallCallback_QueryInterface,
369 InstallCallback_AddRef,
370 InstallCallback_Release,
371 InstallCallback_OnStartBinding,
372 InstallCallback_GetPriority,
373 InstallCallback_OnLowResource,
374 InstallCallback_OnProgress,
375 InstallCallback_OnStopBinding,
376 InstallCallback_GetBindInfo,
377 InstallCallback_OnDataAvailable,
378 InstallCallback_OnObjectAvailable
381 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
383 static LPWSTR get_url(void)
385 HKEY hkey;
386 DWORD res, type;
387 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
388 DWORD returned_size;
389 LPWSTR url;
391 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
392 static const WCHAR httpW[] = {'h','t','t','p'};
393 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
394 static const WCHAR v_formatW[] = {'&','v','='};
396 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
397 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
398 if(res != ERROR_SUCCESS)
399 return NULL;
401 url = heap_alloc(size);
402 returned_size = size;
404 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
405 RegCloseKey(hkey);
406 if(res != ERROR_SUCCESS || type != REG_SZ) {
407 heap_free(url);
408 return NULL;
411 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
412 DWORD len;
414 len = strlenW(url);
415 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
416 len += sizeof(arch_formatW)/sizeof(WCHAR);
417 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
418 memcpy(url+len, v_formatW, sizeof(v_formatW));
419 len += sizeof(v_formatW)/sizeof(WCHAR);
420 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
423 TRACE("Got URL %s\n", debugstr_w(url));
424 return url;
427 static DWORD WINAPI download_proc(PVOID arg)
429 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
430 HRESULT hres;
432 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
433 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
435 TRACE("using temp file %s\n", debugstr_w(tmp_file));
437 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
438 if(FAILED(hres)) {
439 ERR("URLDownloadToFile failed: %08x\n", hres);
440 return 0;
443 if(sha_check(tmp_file))
444 install_file(tmp_file);
445 DeleteFileW(tmp_file);
446 EndDialog(install_dialog, 0);
447 return 0;
450 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
452 switch(msg) {
453 case WM_INITDIALOG:
454 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
455 install_dialog = hwnd;
456 return TRUE;
458 case WM_COMMAND:
459 switch(wParam) {
460 case IDCANCEL:
461 EndDialog(hwnd, 0);
462 return FALSE;
464 case ID_DWL_INSTALL:
465 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
466 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
467 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
468 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
469 return FALSE;
473 return FALSE;
476 BOOL install_wine_gecko(void)
478 if(!*ARCH_STRING)
479 return FALSE;
482 * Try to find Gecko .cab file in following order:
483 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
484 * - $datadir/gecko/
485 * - $INSTALL_DATADIR/wine/gecko/
486 * - /usr/share/wine/gecko/
487 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
489 if(!install_from_registered_dir()
490 && !install_from_default_dir()
491 && (url = get_url()))
492 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
494 heap_free(url);
495 url = NULL;
496 return TRUE;