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