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