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