Synchronize with trunk revision 59781.
[reactos.git] / dll / cpl / appwiz / addons.c
1 /*
2 * Copyright 2006-2010 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 #include <wine/port.h>
21
22 #include <stdio.h>
23
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31
32 #include <windef.h>
33 #include <winbase.h>
34 #include <msi.h>
35
36 #include "appwiz.h"
37
38 #include <wine/debug.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(appwiz);
41
42 #define GECKO_VERSION "1.9"
43
44 #ifdef __i386__
45 #define ARCH_STRING "x86"
46 #define GECKO_SHA "cadf123dd7c4cedf2b22c066b6259e8649ac2b40"
47 #else
48 #define ARCH_STRING ""
49 #define GECKO_SHA "???"
50 #endif
51
52 typedef struct {
53 const char *version;
54 const char *file_name;
55 const char *sha;
56 const char *config_key;
57 const char *dir_config_key;
58 LPCWSTR dialog_template;
59 } addon_info_t;
60
61 static const addon_info_t addons_info[] = {
62 {
63 GECKO_VERSION,
64 "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi",
65 GECKO_SHA,
66 "MSHTML",
67 "GeckoCabDir",
68 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG)
69 }
70 };
71
72 static const addon_info_t *addon;
73
74 static HWND install_dialog = NULL;
75
76 static WCHAR GeckoUrl[] = L"http://www.reactos.org/wine-gecko.php";
77
78 /* SHA definitions are copied from advapi32. They aren't available in headers. */
79
80 typedef struct {
81 ULONG Unknown[6];
82 ULONG State[5];
83 ULONG Count[2];
84 UCHAR Buffer[64];
85 } SHA_CTX, *PSHA_CTX;
86
87 void WINAPI A_SHAInit(PSHA_CTX);
88 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
89 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
90
91 static BOOL sha_check(const WCHAR *file_name)
92 {
93 const unsigned char *file_map;
94 HANDLE file, map;
95 ULONG sha[5];
96 char buf[2*sizeof(sha)+1];
97 SHA_CTX ctx;
98 DWORD size, i;
99
100 file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
101 if(file == INVALID_HANDLE_VALUE)
102 return FALSE;
103
104 size = GetFileSize(file, NULL);
105
106 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
107 CloseHandle(file);
108 if(!map)
109 return FALSE;
110
111 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
112 CloseHandle(map);
113 if(!file_map)
114 return FALSE;
115
116 A_SHAInit(&ctx);
117 A_SHAUpdate(&ctx, file_map, size);
118 A_SHAFinal(&ctx, sha);
119
120 UnmapViewOfFile(file_map);
121
122 for(i=0; i < sizeof(sha); i++)
123 sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
124
125 if(strcmp(buf, addon->sha)) {
126 WARN("Got %s, expected %s\n", buf, addon->sha);
127 return FALSE;
128 }
129
130 return TRUE;
131 }
132
133 static void set_status(DWORD id)
134 {
135 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
136 WCHAR buf[64];
137
138 LoadStringW(hApplet, id, buf, sizeof(buf)/sizeof(WCHAR));
139 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
140 }
141
142 enum install_res {
143 INSTALL_OK = 0,
144 INSTALL_FAILED,
145 INSTALL_NEXT,
146 };
147
148 static enum install_res install_file(const WCHAR *file_name)
149 {
150 ULONG res;
151
152 res = MsiInstallProductW(file_name, NULL);
153 if(res != ERROR_SUCCESS) {
154 ERR("MsiInstallProduct failed: %u\n", res);
155 return INSTALL_FAILED;
156 }
157
158 return INSTALL_OK;
159 }
160
161 static enum install_res install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
162 {
163 LPWSTR dos_file_name;
164 char *file_path;
165 int fd, len;
166 enum install_res ret;
167 UINT res;
168
169 len = strlen(dir);
170 file_path = heap_alloc(len+strlen(subdir)+strlen(file_name)+3);
171 if(!file_path)
172 return INSTALL_FAILED;
173
174 memcpy(file_path, dir, len);
175 if(len && file_path[len-1] != '/' && file_path[len-1] != '\\')
176 file_path[len++] = '/';
177 if(*subdir) {
178 strcpy(file_path+len, subdir);
179 len += strlen(subdir);
180 file_path[len++] = '/';
181 }
182 strcpy(file_path+len, file_name);
183
184 fd = _open(file_path, O_RDONLY);
185 if(fd == -1) {
186 TRACE("%s not found\n", debugstr_a(file_path));
187 heap_free(file_path);
188 return INSTALL_NEXT;
189 }
190
191 _close(fd);
192
193 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
194 res = MultiByteToWideChar( CP_ACP, 0, file_path, -1, 0, 0);
195 dos_file_name = heap_alloc (res*sizeof(WCHAR));
196 MultiByteToWideChar( CP_ACP, 0, file_path, -1, dos_file_name, res);
197
198 heap_free(file_path);
199
200 ret = install_file(dos_file_name);
201
202 heap_free(dos_file_name);
203 return ret;
204 }
205
206 static const CHAR mshtml_keyA[] =
207 {'S','o','f','t','w','a','r','e',
208 '\\','W','i','n','e',
209 '\\','M','S','H','T','M','L',0};
210
211 static enum install_res install_from_registered_dir(void)
212 {
213 char *package_dir;
214 DWORD res, type, size = MAX_PATH;
215 enum install_res ret;
216
217 package_dir = heap_alloc(size + sizeof(addon->file_name));
218
219 res = RegGetValueA(HKEY_CURRENT_USER, mshtml_keyA, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
220 if(res == ERROR_MORE_DATA) {
221 package_dir = heap_realloc(package_dir, size + sizeof(addon->file_name));
222 res = RegGetValueA(HKEY_CURRENT_USER, mshtml_keyA, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
223 }
224
225 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
226 heap_free(package_dir);
227 return INSTALL_FAILED;
228 }
229
230 TRACE("Trying %s/%s\n", debugstr_a(package_dir), debugstr_a(addon->file_name));
231
232 ret = install_from_unix_file(package_dir, "", addon->file_name);
233
234 heap_free(package_dir);
235 return ret;
236 }
237
238 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
239 REFIID riid, void **ppv)
240 {
241 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
242 *ppv = iface;
243 return S_OK;
244 }
245
246 return E_INVALIDARG;
247 }
248
249 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
250 {
251 return 2;
252 }
253
254 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
255 {
256 return 1;
257 }
258
259 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
260 DWORD dwReserved, IBinding *pib)
261 {
262 set_status(IDS_DOWNLOADING);
263 return S_OK;
264 }
265
266 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
267 LONG *pnPriority)
268 {
269 return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
273 DWORD dwReserved)
274 {
275 return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
279 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
280 {
281 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
282
283 if(ulProgressMax)
284 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
285 if(ulProgress)
286 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
287
288 return S_OK;
289 }
290
291 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
292 HRESULT hresult, LPCWSTR szError)
293 {
294 if(FAILED(hresult)) {
295 ERR("Binding failed %08x\n", hresult);
296 return S_OK;
297 }
298
299 set_status(IDS_INSTALLING);
300 return S_OK;
301 }
302
303 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
304 DWORD* grfBINDF, BINDINFO* pbindinfo)
305 {
306 /* FIXME */
307 *grfBINDF = 0;
308 return S_OK;
309 }
310
311 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
312 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
313 {
314 ERR("\n");
315 return E_NOTIMPL;
316 }
317
318 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
319 REFIID riid, IUnknown* punk)
320 {
321 ERR("\n");
322 return E_NOTIMPL;
323 }
324
325 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
326 InstallCallback_QueryInterface,
327 InstallCallback_AddRef,
328 InstallCallback_Release,
329 InstallCallback_OnStartBinding,
330 InstallCallback_GetPriority,
331 InstallCallback_OnLowResource,
332 InstallCallback_OnProgress,
333 InstallCallback_OnStopBinding,
334 InstallCallback_GetBindInfo,
335 InstallCallback_OnDataAvailable,
336 InstallCallback_OnObjectAvailable
337 };
338
339 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
340
341 static DWORD WINAPI download_proc(PVOID arg)
342 {
343 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
344 HRESULT hres;
345
346 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
347 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
348
349 TRACE("using temp file %s\n", debugstr_w(tmp_file));
350
351 hres = URLDownloadToFileW(NULL, GeckoUrl, tmp_file, 0, &InstallCallback);
352 if(FAILED(hres)) {
353 ERR("URLDownloadToFile failed: %08x\n", hres);
354 return 0;
355 }
356
357 if(sha_check(tmp_file)) {
358 install_file(tmp_file);
359 }else {
360 WCHAR message[256];
361
362 if(LoadStringW(hApplet, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR))) {
363 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
364 }
365 }
366
367 DeleteFileW(tmp_file);
368 EndDialog(install_dialog, 0);
369 return 0;
370 }
371
372 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
373 {
374 switch(msg) {
375 case WM_INITDIALOG:
376 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
377 install_dialog = hwnd;
378 return TRUE;
379
380 case WM_NOTIFY:
381 break;
382
383 case WM_COMMAND:
384 switch(wParam) {
385 case IDCANCEL:
386 EndDialog(hwnd, 0);
387 return FALSE;
388
389 case ID_DWL_INSTALL:
390 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
391 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
392 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
393 CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
394 return FALSE;
395 }
396 }
397
398 return FALSE;
399 }
400
401 BOOL install_addon(addon_t addon_type)
402 {
403
404 if(!*ARCH_STRING)
405 return FALSE;
406
407 addon = addons_info + addon_type;
408
409 /*
410 * Try to find addon .msi file in following order:
411 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
412 * - download the package
413 */
414 if (install_from_registered_dir() == INSTALL_NEXT)
415 DialogBoxW(hApplet, addon->dialog_template, 0, installer_proc);
416
417 return TRUE;
418 }