Revert "gdi32: Make AddFontToList skip adding a face into global lists if the font...
[wine] / dlls / mshtml / install.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26
27 #define COBJMACROS
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30
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"
40
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/library.h"
44
45 #include "mshtml_private.h"
46 #include "resource.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
49
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
57
58 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" GECKO_ARCH ".cab"
59
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};
64
65 static HWND install_dialog = NULL;
66 static LPWSTR url = NULL;
67
68 static void set_status(DWORD id)
69 {
70     HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
71     WCHAR buf[64];
72
73     LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
74     SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
75 }
76
77 static void set_registry(LPCSTR install_dir)
78 {
79     WCHAR mshtml_key[100];
80     LPWSTR gecko_path;
81     HKEY hkey;
82     DWORD res, len;
83
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};
86
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));
92
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;
98     }
99
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);
103
104     if (len && gecko_path[len-1] != '\\')
105         gecko_path[len++] = '\\';
106
107     memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
108
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);
115 }
116
117 static BOOL install_cab(LPCWSTR file_name)
118 {
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;
125
126     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
127
128     TRACE("(%s)\n", debugstr_w(file_name));
129
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;
136     }
137
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;
143     }
144
145     advpack = LoadLibraryW(wszAdvpack);
146     pExtractFilesA = (void *)GetProcAddress(advpack, "ExtractFiles");
147
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;
156     }
157
158     set_registry(install_dir);
159     return TRUE;
160 }
161
162 static BOOL install_from_unix_file(const char *file_name)
163 {
164     LPWSTR dos_file_name;
165     int fd;
166     BOOL ret;
167
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};
170
171     fd = open(file_name, O_RDONLY);
172     if(fd == -1) {
173         TRACE("%s not found\n", debugstr_a(file_name));
174         return FALSE;
175     }
176
177     close(fd);
178
179     if(!wine_get_dos_file_name)
180         wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
181
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;
187         }
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);
194     }
195
196     ret = install_cab(dos_file_name);
197
198     heap_free(dos_file_name);
199     return ret;
200 }
201
202 static BOOL install_from_registered_dir(void)
203 {
204     char *file_name;
205     HKEY hkey;
206     DWORD res, type, size = MAX_PATH;
207     BOOL ret;
208
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;
213
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);
219     }
220     RegCloseKey(hkey);
221     if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
222         heap_free(file_name);
223         return FALSE;
224     }
225
226     strcat(file_name, GECKO_FILE_NAME);
227
228     TRACE("Trying %s\n", debugstr_a(file_name));
229
230     ret = install_from_unix_file(file_name);
231
232     heap_free(file_name);
233     return ret;
234 }
235
236 static BOOL install_from_default_dir(void)
237 {
238     const char *data_dir, *subdir;
239     char *file_name;
240     int len, len2;
241     BOOL ret;
242
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;
249
250     len = strlen(data_dir);
251     len2 = strlen(subdir);
252
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));
257
258     ret = install_from_unix_file(file_name);
259
260     heap_free(file_name);
261
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;
267 }
268
269 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
270                                                      REFIID riid, void **ppv)
271 {
272     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
273         *ppv = iface;
274         return S_OK;
275     }
276
277     return E_INVALIDARG;
278 }
279
280 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
281 {
282     return 2;
283 }
284
285 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
286 {
287     return 1;
288 }
289
290 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
291         DWORD dwReserved, IBinding *pib)
292 {
293     set_status(IDS_DOWNLOADING);
294     return S_OK;
295 }
296
297 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
298         LONG *pnPriority)
299 {
300     return E_NOTIMPL;
301 }
302
303 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
304        DWORD dwReserved)
305 {
306     return E_NOTIMPL;
307 }
308
309 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
310         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
311 {
312     HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
313
314     if(ulProgressMax)
315         SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
316     if(ulProgress)
317         SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
318
319     return S_OK;
320 }
321
322 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
323         HRESULT hresult, LPCWSTR szError)
324 {
325     if(FAILED(hresult)) {
326         ERR("Binding failed %08x\n", hresult);
327         return S_OK;
328     }
329
330     set_status(IDS_INSTALLING);
331     return S_OK;
332 }
333
334 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
335         DWORD* grfBINDF, BINDINFO* pbindinfo)
336 {
337     /* FIXME */
338     *grfBINDF = 0;
339     return S_OK;
340 }
341
342 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
343         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
344 {
345     ERR("\n");
346     return E_NOTIMPL;
347 }
348
349 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
350         REFIID riid, IUnknown* punk)
351 {
352     ERR("\n");
353     return E_NOTIMPL;
354 }
355
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
368 };
369
370 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
371
372 static LPWSTR get_url(void)
373 {
374     HKEY hkey;
375     DWORD res, type;
376     DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
377     DWORD returned_size;
378     LPWSTR url;
379
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','='};
384
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;
389
390     url = heap_alloc(size);
391     returned_size = size;
392
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;
398     }
399
400     if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
401         DWORD len;
402
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);
410     }
411
412     TRACE("Got URL %s\n", debugstr_w(url));
413     return url;
414 }
415
416 static DWORD WINAPI download_proc(PVOID arg)
417 {
418     WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
419     HRESULT hres;
420
421     GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
422     GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
423
424     TRACE("using temp file %s\n", debugstr_w(tmp_file));
425
426     hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
427     if(FAILED(hres)) {
428         ERR("URLDownloadToFile failed: %08x\n", hres);
429         return 0;
430     }
431
432     install_cab(tmp_file);
433     DeleteFileW(tmp_file);
434     EndDialog(install_dialog, 0);
435     return 0;
436 }
437
438 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
439 {
440     switch(msg) {
441     case WM_INITDIALOG:
442         ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
443         install_dialog = hwnd;
444         return TRUE;
445
446     case WM_COMMAND:
447         switch(wParam) {
448         case IDCANCEL:
449             EndDialog(hwnd, 0);
450             return FALSE;
451
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;
458         }
459     }
460
461     return FALSE;
462 }
463
464 BOOL install_wine_gecko(BOOL silent)
465 {
466     HANDLE hsem;
467
468     if(!*GECKO_ARCH)
469         return FALSE;
470
471     SetLastError(ERROR_SUCCESS);
472     hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
473
474     if(GetLastError() == ERROR_ALREADY_EXISTS) {
475         WaitForSingleObject(hsem, INFINITE);
476     }else {
477         /*
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
484          */
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);
489     }
490
491     heap_free(url);
492     url = NULL;
493     ReleaseSemaphore(hsem, 1, NULL);
494     CloseHandle(hsem);
495
496     return TRUE;
497 }