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