wined3d: Allow FBO blits again between surfaces with fixups if they have the same...
[wine/multimedia.git] / dlls / mshtml / install.c
blob826161561039c99ccfe8fe40e1896fd8a356c8d2
1 /*
2 * Copyright 2006-2007 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 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
27 #define COBJMACROS
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "ole2.h"
36 #include "commctrl.h"
37 #include "advpub.h"
38 #include "wininet.h"
39 #include "shellapi.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/library.h"
45 #include "mshtml_private.h"
46 #include "resource.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
50 #ifdef __i386__
51 #define GECKO_ARCH "x86"
52 #elif defined(__x86_64__)
53 #define GECKO_ARCH "x86_64"
54 #else
55 #define GECKO_ARCH ""
56 #endif
58 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" GECKO_ARCH ".cab"
60 static const WCHAR mshtml_keyW[] =
61 {'S','o','f','t','w','a','r','e',
62 '\\','W','i','n','e',
63 '\\','M','S','H','T','M','L',0};
65 static HWND install_dialog = NULL;
66 static LPWSTR url = NULL;
68 static void set_status(DWORD id)
70 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
71 WCHAR buf[64];
73 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
74 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
77 static void set_registry(LPCSTR install_dir)
79 WCHAR mshtml_key[100];
80 LPWSTR gecko_path;
81 HKEY hkey;
82 DWORD res, len;
84 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
85 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
87 memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
88 mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
89 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
90 mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
91 (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
93 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
94 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
95 if(res != ERROR_SUCCESS) {
96 ERR("Faild to create MSHTML key: %d\n", res);
97 return;
100 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
101 gecko_path = heap_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
102 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, len+1);
104 if (len && gecko_path[len-1] != '\\')
105 gecko_path[len++] = '\\';
107 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
109 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
110 len*sizeof(WCHAR)+sizeof(wszWineGecko));
111 heap_free(gecko_path);
112 RegCloseKey(hkey);
113 if(res != ERROR_SUCCESS)
114 ERR("Failed to set GeckoPath value: %08x\n", res);
117 static BOOL install_cab(LPCWSTR file_name)
119 HMODULE advpack;
120 char install_dir[MAX_PATH];
121 HRESULT (WINAPI *pExtractFilesA)(LPCSTR,LPCSTR,DWORD,LPCSTR,LPVOID,DWORD);
122 LPSTR file_name_a;
123 DWORD res;
124 HRESULT hres;
126 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
128 TRACE("(%s)\n", debugstr_w(file_name));
130 GetSystemDirectoryA(install_dir, sizeof(install_dir));
131 strcat(install_dir, "\\gecko\\");
132 res = CreateDirectoryA(install_dir, NULL);
133 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
134 ERR("Could not create directory: %08u\n", GetLastError());
135 return FALSE;
138 strcat(install_dir, GECKO_VERSION);
139 res = CreateDirectoryA(install_dir, NULL);
140 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
141 ERR("Could not create directory: %08u\n", GetLastError());
142 return FALSE;
145 advpack = LoadLibraryW(wszAdvpack);
146 pExtractFilesA = (void *)GetProcAddress(advpack, "ExtractFiles");
148 /* FIXME: Use unicode version (not yet implemented) */
149 file_name_a = heap_strdupWtoA(file_name);
150 hres = pExtractFilesA(file_name_a, install_dir, 0, NULL, NULL, 0);
151 FreeLibrary(advpack);
152 heap_free(file_name_a);
153 if(FAILED(hres)) {
154 ERR("Could not extract package: %08x\n", hres);
155 return FALSE;
158 set_registry(install_dir);
159 return TRUE;
162 static BOOL install_from_unix_file(const char *file_name)
164 LPWSTR dos_file_name;
165 int fd;
166 BOOL ret;
168 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
169 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
171 fd = open(file_name, O_RDONLY);
172 if(fd == -1) {
173 TRACE("%s not found\n", debugstr_a(file_name));
174 return FALSE;
177 close(fd);
179 if(!wine_get_dos_file_name)
180 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
182 if(wine_get_dos_file_name) { /* Wine UNIX mode */
183 dos_file_name = wine_get_dos_file_name(file_name);
184 if(!dos_file_name) {
185 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
186 return FALSE;
188 } else { /* Windows mode */
189 UINT res;
190 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
191 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
192 dos_file_name = heap_alloc (res*sizeof(WCHAR));
193 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
196 ret = install_cab(dos_file_name);
198 heap_free(dos_file_name);
199 return ret;
202 static BOOL install_from_registered_dir(void)
204 char *file_name;
205 HKEY hkey;
206 DWORD res, type, size = MAX_PATH;
207 BOOL ret;
209 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
210 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
211 if(res != ERROR_SUCCESS)
212 return FALSE;
214 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
215 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
216 if(res == ERROR_MORE_DATA) {
217 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
218 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
220 RegCloseKey(hkey);
221 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
222 heap_free(file_name);
223 return FALSE;
226 strcat(file_name, GECKO_FILE_NAME);
228 TRACE("Trying %s\n", debugstr_a(file_name));
230 ret = install_from_unix_file(file_name);
232 heap_free(file_name);
233 return ret;
236 static BOOL install_from_default_dir(void)
238 const char *data_dir, *subdir;
239 char *file_name;
240 int len, len2;
241 BOOL ret;
243 if((data_dir = wine_get_data_dir()))
244 subdir = "/gecko/";
245 else if((data_dir = wine_get_build_dir()))
246 subdir = "/../gecko/";
247 else
248 return FALSE;
250 len = strlen(data_dir);
251 len2 = strlen(subdir);
253 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
254 memcpy(file_name, data_dir, len);
255 memcpy(file_name+len, subdir, len2);
256 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
258 ret = install_from_unix_file(file_name);
260 heap_free(file_name);
262 if (!ret)
263 ret = install_from_unix_file( GECKO_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
264 if (!ret && strcmp( GECKO_DATADIR, "/usr/share" ))
265 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
266 return ret;
269 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
270 REFIID riid, void **ppv)
272 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
273 *ppv = iface;
274 return S_OK;
277 return E_INVALIDARG;
280 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
282 return 2;
285 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
287 return 1;
290 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
291 DWORD dwReserved, IBinding *pib)
293 set_status(IDS_DOWNLOADING);
294 return S_OK;
297 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
298 LONG *pnPriority)
300 return E_NOTIMPL;
303 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
304 DWORD dwReserved)
306 return E_NOTIMPL;
309 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
310 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
312 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
314 if(ulProgressMax)
315 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
316 if(ulProgress)
317 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
319 return S_OK;
322 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
323 HRESULT hresult, LPCWSTR szError)
325 if(FAILED(hresult)) {
326 ERR("Binding failed %08x\n", hresult);
327 return S_OK;
330 set_status(IDS_INSTALLING);
331 return S_OK;
334 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
335 DWORD* grfBINDF, BINDINFO* pbindinfo)
337 /* FIXME */
338 *grfBINDF = 0;
339 return S_OK;
342 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
343 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
345 ERR("\n");
346 return E_NOTIMPL;
349 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
350 REFIID riid, IUnknown* punk)
352 ERR("\n");
353 return E_NOTIMPL;
356 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
357 InstallCallback_QueryInterface,
358 InstallCallback_AddRef,
359 InstallCallback_Release,
360 InstallCallback_OnStartBinding,
361 InstallCallback_GetPriority,
362 InstallCallback_OnLowResource,
363 InstallCallback_OnProgress,
364 InstallCallback_OnStopBinding,
365 InstallCallback_GetBindInfo,
366 InstallCallback_OnDataAvailable,
367 InstallCallback_OnObjectAvailable
370 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
372 static LPWSTR get_url(void)
374 HKEY hkey;
375 DWORD res, type;
376 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
377 DWORD returned_size;
378 LPWSTR url;
380 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
381 static const WCHAR httpW[] = {'h','t','t','p'};
382 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
383 static const WCHAR v_formatW[] = {'&','v','='};
385 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
386 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
387 if(res != ERROR_SUCCESS)
388 return NULL;
390 url = heap_alloc(size);
391 returned_size = size;
393 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
394 RegCloseKey(hkey);
395 if(res != ERROR_SUCCESS || type != REG_SZ) {
396 heap_free(url);
397 return NULL;
400 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
401 DWORD len;
403 len = strlenW(url);
404 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
405 len += sizeof(arch_formatW)/sizeof(WCHAR);
406 len += MultiByteToWideChar(CP_ACP, 0, GECKO_ARCH, sizeof(GECKO_ARCH), url+len, size/sizeof(WCHAR)-len)-1;
407 memcpy(url+len, v_formatW, sizeof(v_formatW));
408 len += sizeof(v_formatW)/sizeof(WCHAR);
409 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
412 TRACE("Got URL %s\n", debugstr_w(url));
413 return url;
416 static DWORD WINAPI download_proc(PVOID arg)
418 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
419 HRESULT hres;
421 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
422 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
424 TRACE("using temp file %s\n", debugstr_w(tmp_file));
426 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
427 if(FAILED(hres)) {
428 ERR("URLDownloadToFile failed: %08x\n", hres);
429 return 0;
432 install_cab(tmp_file);
433 DeleteFileW(tmp_file);
434 EndDialog(install_dialog, 0);
435 return 0;
438 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
440 switch(msg) {
441 case WM_INITDIALOG:
442 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
443 install_dialog = hwnd;
444 return TRUE;
446 case WM_COMMAND:
447 switch(wParam) {
448 case IDCANCEL:
449 EndDialog(hwnd, 0);
450 return FALSE;
452 case ID_DWL_INSTALL:
453 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
454 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
455 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
456 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
457 return FALSE;
461 return FALSE;
464 BOOL install_wine_gecko(BOOL silent)
466 HANDLE hsem;
468 if(!*GECKO_ARCH)
469 return FALSE;
471 SetLastError(ERROR_SUCCESS);
472 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
474 if(GetLastError() == ERROR_ALREADY_EXISTS) {
475 WaitForSingleObject(hsem, INFINITE);
476 }else {
478 * Try to find Gecko .cab file in following order:
479 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
480 * - $datadir/gecko/
481 * - $GECKO_DATADIR/wine/gecko/
482 * - /usr/share/wine/gecko/
483 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
485 if(!install_from_registered_dir()
486 && !install_from_default_dir()
487 && !silent && (url = get_url()))
488 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
491 heap_free(url);
492 url = NULL;
493 ReleaseSemaphore(hsem, 1, NULL);
494 CloseHandle(hsem);
496 return TRUE;