e103bc5e7df4da9b77dd1e11cdbab24db7059222
[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 TRACE("Trying %s/%s\n", debugstr_a(package_dir), debugstr_a(addon->file_name));
218
219 ret = install_from_unix_file(package_dir, "", addon->file_name);
220
221 heap_free(package_dir);
222 return ret;
223 }
224
225 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
226 REFIID riid, void **ppv)
227 {
228 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
229 *ppv = iface;
230 return S_OK;
231 }
232
233 return E_INVALIDARG;
234 }
235
236 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
237 {
238 return 2;
239 }
240
241 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
242 {
243 return 1;
244 }
245
246 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
247 DWORD dwReserved, IBinding *pib)
248 {
249 set_status(IDS_DOWNLOADING);
250 return S_OK;
251 }
252
253 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
254 LONG *pnPriority)
255 {
256 return E_NOTIMPL;
257 }
258
259 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
260 DWORD dwReserved)
261 {
262 return E_NOTIMPL;
263 }
264
265 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
266 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
267 {
268 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
269
270 if(ulProgressMax)
271 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
272 if(ulProgress)
273 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
274
275 return S_OK;
276 }
277
278 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
279 HRESULT hresult, LPCWSTR szError)
280 {
281 if(FAILED(hresult)) {
282 ERR("Binding failed %08x\n", hresult);
283 return S_OK;
284 }
285
286 set_status(IDS_INSTALLING);
287 return S_OK;
288 }
289
290 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
291 DWORD* grfBINDF, BINDINFO* pbindinfo)
292 {
293 /* FIXME */
294 *grfBINDF = 0;
295 return S_OK;
296 }
297
298 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
299 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
300 {
301 ERR("\n");
302 return E_NOTIMPL;
303 }
304
305 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
306 REFIID riid, IUnknown* punk)
307 {
308 ERR("\n");
309 return E_NOTIMPL;
310 }
311
312 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
313 InstallCallback_QueryInterface,
314 InstallCallback_AddRef,
315 InstallCallback_Release,
316 InstallCallback_OnStartBinding,
317 InstallCallback_GetPriority,
318 InstallCallback_OnLowResource,
319 InstallCallback_OnProgress,
320 InstallCallback_OnStopBinding,
321 InstallCallback_GetBindInfo,
322 InstallCallback_OnDataAvailable,
323 InstallCallback_OnObjectAvailable
324 };
325
326 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
327
328 static DWORD WINAPI download_proc(PVOID arg)
329 {
330 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
331 HRESULT hres;
332
333 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
334 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
335
336 TRACE("using temp file %s\n", debugstr_w(tmp_file));
337
338 hres = URLDownloadToFileW(NULL, GeckoUrl, tmp_file, 0, &InstallCallback);
339 if(FAILED(hres)) {
340 ERR("URLDownloadToFile failed: %08x\n", hres);
341 return 0;
342 }
343
344 if(sha_check(tmp_file)) {
345 install_file(tmp_file);
346 }else {
347 WCHAR message[256];
348
349 if(LoadStringW(hApplet, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR))) {
350 MessageBoxW(NULL, message, NULL, MB_ICONERROR);
351 }
352 }
353
354 DeleteFileW(tmp_file);
355 EndDialog(install_dialog, 0);
356 return 0;
357 }
358
359 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
360 {
361 switch(msg) {
362 case WM_INITDIALOG:
363 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
364 install_dialog = hwnd;
365 return TRUE;
366
367 case WM_NOTIFY:
368 break;
369
370 case WM_COMMAND:
371 switch(wParam) {
372 case IDCANCEL:
373 EndDialog(hwnd, 0);
374 return FALSE;
375
376 case ID_DWL_INSTALL:
377 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
378 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
379 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
380 CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
381 return FALSE;
382 }
383 }
384
385 return FALSE;
386 }
387
388 BOOL install_addon(addon_t addon_type)
389 {
390
391 if(!*ARCH_STRING)
392 return FALSE;
393
394 addon = addons_info + addon_type;
395
396 /*
397 * Try to find addon .msi file in following order:
398 * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
399 * - download the package
400 */
401 if (install_from_registered_dir() == INSTALL_NEXT)
402 DialogBoxW(hApplet, addon->dialog_template, 0, installer_proc);
403
404 return TRUE;
405 }