mshtml: Use stored nsdoc in IHTMLTxtRange::put_text.
[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 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION ".cab"
51
52 static const WCHAR mshtml_keyW[] =
53     {'S','o','f','t','w','a','r','e',
54      '\\','W','i','n','e',
55      '\\','M','S','H','T','M','L',0};
56
57 static HWND install_dialog = NULL;
58 static LPWSTR tmp_file_name = NULL;
59 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
60 static LPWSTR url = NULL;
61
62 static void clean_up(void)
63 {
64     if(tmp_file != INVALID_HANDLE_VALUE)
65         CloseHandle(tmp_file);
66
67     if(tmp_file_name) {
68         DeleteFileW(tmp_file_name);
69         heap_free(tmp_file_name);
70         tmp_file_name = NULL;
71     }
72
73     if(tmp_file != INVALID_HANDLE_VALUE) {
74         CloseHandle(tmp_file);
75         tmp_file = INVALID_HANDLE_VALUE;
76     }
77
78     if(install_dialog)
79         EndDialog(install_dialog, 0);
80 }
81
82 static void set_status(DWORD id)
83 {
84     HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
85     WCHAR buf[64];
86
87     LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
88     SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
89 }
90
91 static void set_registry(LPCSTR install_dir)
92 {
93     WCHAR mshtml_key[100];
94     LPWSTR gecko_path;
95     HKEY hkey;
96     DWORD res, len;
97
98     static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
99     static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
100
101     memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
102     mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
103     MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
104             mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
105             (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
106
107     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
108     res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
109     if(res != ERROR_SUCCESS) {
110         ERR("Faild to create MSHTML key: %d\n", res);
111         return;
112     }
113
114     len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
115     gecko_path = heap_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
116     MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, len+1);
117
118     if (len && gecko_path[len-1] != '\\')
119         gecko_path[len++] = '\\';
120
121     memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
122
123     res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
124                        len*sizeof(WCHAR)+sizeof(wszWineGecko));
125     heap_free(gecko_path);
126     RegCloseKey(hkey);
127     if(res != ERROR_SUCCESS)
128         ERR("Failed to set GeckoPath value: %08x\n", res);
129 }
130
131 static BOOL install_cab(LPCWSTR file_name)
132 {
133     HMODULE advpack;
134     char install_dir[MAX_PATH];
135     HRESULT (WINAPI *pExtractFilesA)(LPCSTR,LPCSTR,DWORD,LPCSTR,LPVOID,DWORD);
136     LPSTR file_name_a;
137     DWORD res;
138     HRESULT hres;
139
140     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
141
142     TRACE("(%s)\n", debugstr_w(file_name));
143
144     GetWindowsDirectoryA(install_dir, sizeof(install_dir));
145     strcat(install_dir, "\\gecko\\");
146     res = CreateDirectoryA(install_dir, NULL);
147     if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
148         ERR("Could not create directory: %08u\n", GetLastError());
149         return FALSE;
150     }
151
152     strcat(install_dir, GECKO_VERSION);
153     res = CreateDirectoryA(install_dir, NULL);
154     if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
155         ERR("Could not create directory: %08u\n", GetLastError());
156         return FALSE;
157     }
158
159     advpack = LoadLibraryW(wszAdvpack);
160     pExtractFilesA = (void *)GetProcAddress(advpack, "ExtractFiles");
161
162     /* FIXME: Use unicode version (not yet implemented) */
163     file_name_a = heap_strdupWtoA(file_name);
164     hres = pExtractFilesA(file_name_a, install_dir, 0, NULL, NULL, 0);
165     FreeLibrary(advpack);
166     heap_free(file_name_a);
167     if(FAILED(hres)) {
168         ERR("Could not extract package: %08x\n", hres);
169         clean_up();
170         return FALSE;
171     }
172
173     set_registry(install_dir);
174     clean_up();
175
176     return TRUE;
177 }
178
179 static BOOL install_from_unix_file(const char *file_name)
180 {
181     LPWSTR dos_file_name;
182     int fd;
183     BOOL ret;
184
185     static WCHAR *(*wine_get_dos_file_name)(const char*);
186     static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
187
188     fd = open(file_name, O_RDONLY);
189     if(fd == -1) {
190         TRACE("%s not found\n", debugstr_a(file_name));
191         return FALSE;
192     }
193
194     close(fd);
195
196     if(!wine_get_dos_file_name) {
197         wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
198         if(!wine_get_dos_file_name) {
199             ERR("Could not get wine_get_dos_file_name function.\n");
200             return FALSE;
201         }
202     }
203
204     dos_file_name = wine_get_dos_file_name(file_name);
205     if(!dos_file_name) {
206         ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
207         return FALSE;
208     }
209
210     ret = install_cab(dos_file_name);
211
212     heap_free(dos_file_name);
213     return ret;
214 }
215
216 static BOOL install_from_registered_dir(void)
217 {
218     char *file_name;
219     HKEY hkey;
220     DWORD res, type, size = MAX_PATH;
221     BOOL ret;
222
223     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
224     res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
225     if(res != ERROR_SUCCESS)
226         return FALSE;
227
228     file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
229     res = RegQueryValueExA(hkey, "GeckoCabDir", NULL, &type, (PBYTE)file_name, &size);
230     if(res == ERROR_MORE_DATA) {
231         file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
232         res = RegQueryValueExA(hkey, "GeckoCabDir", NULL, &type, (PBYTE)file_name, &size);
233     }
234     RegCloseKey(hkey);
235     if(res != ERROR_SUCCESS || type != REG_SZ) {
236         heap_free(file_name);
237         return FALSE;
238     }
239
240     strcat(file_name, GECKO_FILE_NAME);
241
242     TRACE("Trying %s\n", debugstr_a(file_name));
243
244     ret = install_from_unix_file(file_name);
245
246     heap_free(file_name);
247     return ret;
248 }
249
250 static BOOL install_from_default_dir(void)
251 {
252     const char *data_dir, *subdir;
253     char *file_name;
254     int len, len2;
255     BOOL ret;
256
257     if((data_dir = wine_get_data_dir()))
258         subdir = "/gecko/";
259     else if((data_dir = wine_get_build_dir()))
260         subdir = "/../gecko/";
261     else
262         return FALSE;
263
264     len = strlen(data_dir);
265     len2 = strlen(subdir);
266
267     file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
268     memcpy(file_name, data_dir, len);
269     memcpy(file_name+len, subdir, len2);
270     memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
271
272     ret = install_from_unix_file(file_name);
273
274     heap_free(file_name);
275     return ret;
276 }
277
278 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
279                                                      REFIID riid, void **ppv)
280 {
281     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
282         *ppv = iface;
283         return S_OK;
284     }
285
286     return E_INVALIDARG;
287 }
288
289 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
290 {
291     return 2;
292 }
293
294 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
295 {
296     return 1;
297 }
298
299 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
300         DWORD dwReserved, IBinding *pib)
301 {
302     WCHAR tmp_dir[MAX_PATH];
303
304     set_status(IDS_DOWNLOADING);
305
306     GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
307
308     tmp_file_name = heap_alloc(MAX_PATH*sizeof(WCHAR));
309     GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
310
311     TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
312
313     tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL, 
314                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
315
316     if(tmp_file == INVALID_HANDLE_VALUE) {
317         ERR("Could not create file: %d\n", GetLastError());
318         clean_up();
319         return E_FAIL;
320     }
321
322     return S_OK;
323 }
324
325 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
326         LONG *pnPriority)
327 {
328     return E_NOTIMPL;
329 }
330
331 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
332        DWORD dwReserved)
333 {
334     return E_NOTIMPL;
335 }
336
337 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
338         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
339 {
340     HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
341
342     if(ulProgressMax)
343         SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
344     if(ulProgress)
345         SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
346
347     return S_OK;
348 }
349
350 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
351         HRESULT hresult, LPCWSTR szError)
352 {
353     if(FAILED(hresult)) {
354         ERR("Binding failed %08x\n", hresult);
355         clean_up();
356         return S_OK;
357     }
358
359     CloseHandle(tmp_file);
360     tmp_file = INVALID_HANDLE_VALUE;
361
362     set_status(IDS_INSTALLING);
363
364     install_cab(tmp_file_name);
365
366     return S_OK;
367 }
368
369 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
370         DWORD* grfBINDF, BINDINFO* pbindinfo)
371 {
372     /* FIXME */
373     *grfBINDF = 0;
374     return S_OK;
375 }
376
377 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
378         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
379 {
380     IStream *str = pstgmed->u.pstm;
381     BYTE buf[1024];
382     DWORD size;
383     HRESULT hres;
384
385     do {
386         size = 0;
387         hres = IStream_Read(str, buf, sizeof(buf), &size);
388         if(size)
389             WriteFile(tmp_file, buf, size, NULL, NULL);
390     }while(hres == S_OK);
391
392     return S_OK;
393 }
394
395 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
396         REFIID riid, IUnknown* punk)
397 {
398     ERR("\n");
399     return E_NOTIMPL;
400 }
401
402 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
403     InstallCallback_QueryInterface,
404     InstallCallback_AddRef,
405     InstallCallback_Release,
406     InstallCallback_OnStartBinding,
407     InstallCallback_GetPriority,
408     InstallCallback_OnLowResource,
409     InstallCallback_OnProgress,
410     InstallCallback_OnStopBinding,
411     InstallCallback_GetBindInfo,
412     InstallCallback_OnDataAvailable,
413     InstallCallback_OnObjectAvailable
414 };
415
416 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
417
418 static LPWSTR get_url(void)
419 {
420     HKEY hkey;
421     DWORD res, type;
422     DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
423     LPWSTR url;
424
425     static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
426     static const WCHAR httpW[] = {'h','t','t','p'};
427     static const WCHAR v_formatW[] = {'?','v','=',0};
428
429     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
430     res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
431     if(res != ERROR_SUCCESS)
432         return NULL;
433
434     url = heap_alloc(size);
435
436     res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &size);
437     RegCloseKey(hkey);
438     if(res != ERROR_SUCCESS || type != REG_SZ) {
439         heap_free(url);
440         return NULL;
441     }
442
443     if(size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
444         strcatW(url, v_formatW);
445         MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+strlenW(url), -1);
446     }
447
448     TRACE("Got URL %s\n", debugstr_w(url));
449     return url;
450 }
451
452 static DWORD WINAPI download_proc(PVOID arg)
453 {
454     IMoniker *mon;
455     IBindCtx *bctx;
456     IStream *str = NULL;
457     HRESULT hres;
458
459     CreateURLMoniker(NULL, url, &mon);
460     heap_free(url);
461     url = NULL;
462
463     CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
464
465     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
466     IBindCtx_Release(bctx);
467     if(FAILED(hres)) {
468         ERR("BindToStorage failed: %08x\n", hres);
469         return 0;
470     }
471
472     if(str)
473         IStream_Release(str);
474
475     return 0;
476 }
477
478 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
479 {
480     switch(msg) {
481     case WM_INITDIALOG:
482         ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
483         install_dialog = hwnd;
484         return TRUE;
485
486     case WM_COMMAND:
487         switch(wParam) {
488         case IDCANCEL:
489             EndDialog(hwnd, 0);
490             return FALSE;
491
492         case ID_DWL_INSTALL:
493             ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
494             EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
495             EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
496             CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
497             return FALSE;
498         }
499     }
500
501     return FALSE;
502 }
503
504 BOOL install_wine_gecko(BOOL silent)
505 {
506     HANDLE hsem;
507
508     SetLastError(ERROR_SUCCESS);
509     hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
510
511     if(GetLastError() == ERROR_ALREADY_EXISTS) {
512         WaitForSingleObject(hsem, INFINITE);
513     }else {
514         /*
515          * Try to find Gecko .cab file in following order:
516          * - directory stored in GeckoCabDir value of HKCU/Software/MSHTML key
517          * - $datadir/gecko
518          * - download from URL stored in GeckoUrl value of HKCU/Software/MSHTML key
519          */
520         if(!install_from_registered_dir()
521            && !install_from_default_dir()
522            && !silent && (url = get_url()))
523             DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
524     }
525
526     ReleaseSemaphore(hsem, 1, NULL);
527     CloseHandle(hsem);
528
529     return TRUE;
530 }