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