msi: Name all or none of the formal arguments in function prototypes.
[wine] / dlls / mshtml / install.c
1 /*
2  * Copyright 2006 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
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "ole2.h"
32 #include "commctrl.h"
33 #include "advpub.h"
34 #include "wininet.h"
35 #include "shellapi.h"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 #include "mshtml_private.h"
41 #include "resource.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44
45 static HWND install_dialog = NULL;
46 static LPWSTR tmp_file_name = NULL;
47 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
48 static LPWSTR url = NULL;
49
50 static void clean_up(void)
51 {
52     if(tmp_file != INVALID_HANDLE_VALUE)
53         CloseHandle(tmp_file);
54
55     if(tmp_file_name) {
56         DeleteFileW(tmp_file_name);
57         mshtml_free(tmp_file_name);
58         tmp_file_name = NULL;
59     }
60
61     if(tmp_file != INVALID_HANDLE_VALUE) {
62         CloseHandle(tmp_file);
63         tmp_file = INVALID_HANDLE_VALUE;
64     }
65
66     if(install_dialog)
67         EndDialog(install_dialog, 0);
68 }
69
70 static void set_status(DWORD id)
71 {
72     HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
73     WCHAR buf[64];
74
75     LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
76     SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
77 }
78
79 static void set_registry(LPCSTR install_dir)
80 {
81     WCHAR mshtml_key[100];
82     LPWSTR gecko_path;
83     HKEY hkey;
84     DWORD res, len, size;
85
86     static const WCHAR wszMshtmlKey[] = {
87         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
88         '\\','M','S','H','T','M','L','\\'};
89     static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
90     static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
91
92     memcpy(mshtml_key, wszMshtmlKey, sizeof(wszMshtmlKey));
93     MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
94             mshtml_key+sizeof(wszMshtmlKey)/sizeof(WCHAR),
95             (sizeof(mshtml_key)-sizeof(wszMshtmlKey))/sizeof(WCHAR));
96
97     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
98     res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
99     if(res != ERROR_SUCCESS) {
100         ERR("Faild to create MSHTML key: %d\n", res);
101         return;
102     }
103
104     len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
105     gecko_path = mshtml_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
106     MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, (len+1)*sizeof(WCHAR));
107
108     if (len && gecko_path[len-1] != '\\')
109         gecko_path[len++] = '\\';
110
111     memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
112
113     size = len*sizeof(WCHAR)+sizeof(wszWineGecko);
114     res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
115                        len*sizeof(WCHAR)+sizeof(wszWineGecko));
116     mshtml_free(gecko_path);
117     RegCloseKey(hkey);
118     if(res != ERROR_SUCCESS)
119         ERR("Failed to set GeckoPath value: %08x\n", res);
120 }
121
122 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
123                                                      REFIID riid, void **ppv)
124 {
125     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
126         *ppv = iface;
127         return S_OK;
128     }
129
130     return E_INVALIDARG;
131 }
132
133 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
134 {
135     return 2;
136 }
137
138 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
139 {
140     return 1;
141 }
142
143 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
144         DWORD dwReserved, IBinding *pib)
145 {
146     WCHAR tmp_dir[MAX_PATH];
147
148     set_status(IDS_DOWNLOADING);
149
150     GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
151
152     tmp_file_name = mshtml_alloc(MAX_PATH*sizeof(WCHAR));
153     GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
154
155     TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
156
157     tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL, 
158                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
159
160     if(tmp_file == INVALID_HANDLE_VALUE) {
161         ERR("Could not create file: %d\n", GetLastError());
162         clean_up();
163         return E_FAIL;
164     }
165
166     return S_OK;
167 }
168
169 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
170         LONG *pnPriority)
171 {
172     return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
176        DWORD dwReserved)
177 {
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
182         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
183 {
184     HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
185
186     if(ulProgressMax)
187         SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
188     if(ulProgress)
189         SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
190
191     return S_OK;
192 }
193
194 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
195         HRESULT hresult, LPCWSTR szError)
196 {
197     LPSTR file_name;
198     DWORD len;
199     HMODULE advpack;
200     char install_dir[MAX_PATH];
201     typeof(ExtractFilesA) *pExtractFilesA;
202     BOOL res;
203     HRESULT hres;
204
205     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
206
207     if(FAILED(hresult)) {
208         ERR("Binding failed %08x\n", hresult);
209         clean_up();
210         return S_OK;
211     }
212
213     CloseHandle(tmp_file);
214     tmp_file = INVALID_HANDLE_VALUE;
215
216     set_status(IDS_INSTALLING);
217
218     GetWindowsDirectoryA(install_dir, sizeof(install_dir));
219     strcat(install_dir, "\\gecko\\");
220     res = CreateDirectoryA(install_dir, NULL);
221     if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
222         ERR("Could not create directory: %08u\n", GetLastError());
223         return S_OK;
224     }
225
226     strcat(install_dir, GECKO_VERSION);
227     res = CreateDirectoryA(install_dir, NULL);
228     if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
229         ERR("Could not create directory: %08u\n", GetLastError());
230         return S_OK;
231     }
232
233     advpack = LoadLibraryW(wszAdvpack);
234     pExtractFilesA = (typeof(ExtractFilesA)*)GetProcAddress(advpack, "ExtractFiles");
235
236     len = WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, NULL, 0, NULL, NULL);
237     file_name = mshtml_alloc(len);
238     WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, file_name, -1, NULL, NULL);
239
240     /* FIXME: Use unicode version (not yet implemented) */
241     hres = pExtractFilesA(file_name, install_dir, 0, NULL, NULL, 0);
242     FreeLibrary(advpack);
243     mshtml_free(file_name);
244     if(FAILED(hres)) {
245         ERR("Could not extract package: %08x\n", hres);
246         clean_up();
247     }
248
249     set_registry(install_dir);
250     clean_up();
251
252     return S_OK;
253 }
254
255 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
256         DWORD* grfBINDF, BINDINFO* pbindinfo)
257 {
258     /* FIXME */
259     *grfBINDF = 0;
260     return S_OK;
261 }
262
263 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
264         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
265 {
266     IStream *str = pstgmed->u.pstm;
267     BYTE buf[1024];
268     DWORD size;
269     HRESULT hres;
270
271     do {
272         size = 0;
273         hres = IStream_Read(str, buf, sizeof(buf), &size);
274         if(size)
275             WriteFile(tmp_file, buf, size, NULL, NULL);
276     }while(hres == S_OK);
277
278     return S_OK;
279 }
280
281 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
282         REFIID riid, IUnknown* punk)
283 {
284     ERR("\n");
285     return E_NOTIMPL;
286 }
287
288 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
289     InstallCallback_QueryInterface,
290     InstallCallback_AddRef,
291     InstallCallback_Release,
292     InstallCallback_OnStartBinding,
293     InstallCallback_GetPriority,
294     InstallCallback_OnLowResource,
295     InstallCallback_OnProgress,
296     InstallCallback_OnStopBinding,
297     InstallCallback_GetBindInfo,
298     InstallCallback_OnDataAvailable,
299     InstallCallback_OnObjectAvailable
300 };
301
302 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
303
304 static LPWSTR get_url(void)
305 {
306     HKEY hkey;
307     DWORD res, type;
308     DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
309     LPWSTR url;
310
311     static const WCHAR wszMshtmlKey[] = {
312         'S','o','f','t','w','a','r','e','\\','W','i','n','e',
313         '\\','M','S','H','T','M','L',0};
314     static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
315     static const WCHAR httpW[] = {'h','t','t','p'};
316     static const WCHAR v_formatW[] = {'?','v','=',0};
317
318     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
319     res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
320     if(res != ERROR_SUCCESS)
321         return NULL;
322
323     url = mshtml_alloc(size);
324
325     res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &size);
326     RegCloseKey(hkey);
327     if(res != ERROR_SUCCESS || type != REG_SZ) {
328         mshtml_free(url);
329         return NULL;
330     }
331
332     if(size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
333         strcatW(url, v_formatW);
334         MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+strlenW(url), -1);
335     }
336
337     TRACE("Got URL %s\n", debugstr_w(url));
338     return url;
339 }
340
341 static DWORD WINAPI download_proc(PVOID arg)
342 {
343     IMoniker *mon;
344     IBindCtx *bctx;
345     IStream *str = NULL;
346     HRESULT hres;
347
348     CreateURLMoniker(NULL, url, &mon);
349     mshtml_free(url);
350     url = NULL;
351
352     CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
353
354     hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
355     IBindCtx_Release(bctx);
356     if(FAILED(hres)) {
357         ERR("BindToStorage failed: %08x\n", hres);
358         return 0;
359     }
360
361     if(str)
362         IStream_Release(str);
363
364     return 0;
365 }
366
367 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
368 {
369     switch(msg) {
370     case WM_INITDIALOG:
371         install_dialog = hwnd;
372         return TRUE;
373
374     case WM_COMMAND:
375         switch(wParam) {
376         case IDCANCEL:
377             EndDialog(hwnd, 0);
378             return FALSE;
379
380         case ID_DWL_INSTALL:
381             EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
382             EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
383             CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
384             return FALSE;
385         }
386     }
387
388     return FALSE;
389 }
390
391 BOOL install_wine_gecko(void)
392 {
393     HANDLE hsem;
394
395     SetLastError(ERROR_SUCCESS);
396     hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
397
398     if(GetLastError() == ERROR_ALREADY_EXISTS) {
399         WaitForSingleObject(hsem, INFINITE);
400     }else {
401         if((url = get_url()))
402             DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
403     }
404
405     ReleaseSemaphore(hsem, 1, NULL);
406     CloseHandle(hsem);
407
408     return TRUE;
409 }