d3drm: Implement IDirect3DRMMesh_AddGroup.
[wine] / dlls / appwiz.cpl / 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
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <stdio.h>
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 "winuser.h"
35 #include "cpl.h"
36 #include "winreg.h"
37 #include "ole2.h"
38 #include "commctrl.h"
39 #include "advpub.h"
40 #include "wininet.h"
41 #include "shellapi.h"
42 #include "urlmon.h"
43 #include "msi.h"
44
45 #include "appwiz.h"
46 #include "res.h"
47
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "wine/library.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl);
53
54 #define GECKO_VERSION "1.5"
55
56 #ifdef __i386__
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "07b2bc74d03c885bb39124a7641715314cd3ae71"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "80a3b36c30bb79a11889879392fdc1fcda9ca165"
62 #else
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
65 #endif
66
67 typedef struct {
68     const char *version;
69     const char *file_name;
70     const char *subdir_name;
71     const char *sha;
72     const char *config_key;
73     const char *url_config_key;
74     const char *dir_config_key;
75     LPCWSTR dialog_template;
76 } addon_info_t;
77
78 static const addon_info_t addons_info[] = {
79     {
80         GECKO_VERSION,
81         "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi",
82         "gecko",
83         GECKO_SHA,
84         "MSHTML", "GeckoUrl", "GeckoCabDir",
85         MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG)
86     }
87 };
88
89 static const addon_info_t *addon;
90
91 static HWND install_dialog = NULL;
92 static LPWSTR url = NULL;
93
94 /* SHA definitions are copied from advapi32. They aren't available in headers. */
95
96 typedef struct {
97    ULONG Unknown[6];
98    ULONG State[5];
99    ULONG Count[2];
100    UCHAR Buffer[64];
101 } SHA_CTX, *PSHA_CTX;
102
103 void WINAPI A_SHAInit(PSHA_CTX);
104 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
105 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
106
107 static BOOL sha_check(const WCHAR *file_name)
108 {
109     const unsigned char *file_map;
110     HANDLE file, map;
111     ULONG sha[5];
112     char buf[2*sizeof(sha)+1];
113     SHA_CTX ctx;
114     DWORD size, i;
115
116     file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
117     if(file == INVALID_HANDLE_VALUE)
118         return FALSE;
119
120     size = GetFileSize(file, NULL);
121
122     map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
123     CloseHandle(file);
124     if(!map)
125         return FALSE;
126
127     file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
128     CloseHandle(map);
129     if(!file_map)
130         return FALSE;
131
132     A_SHAInit(&ctx);
133     A_SHAUpdate(&ctx, file_map, size);
134     A_SHAFinal(&ctx, sha);
135
136     UnmapViewOfFile(file_map);
137
138     for(i=0; i < sizeof(sha); i++)
139         sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
140
141     if(strcmp(buf, addon->sha)) {
142         WCHAR message[256];
143
144         WARN("Got %s, expected %s\n", buf, addon->sha);
145
146         if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
147             MessageBoxW(NULL, message, NULL, MB_ICONERROR);
148
149         return FALSE;
150     }
151
152     return TRUE;
153 }
154
155 static void set_status(DWORD id)
156 {
157     HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
158     WCHAR buf[64];
159
160     LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
161     SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
162 }
163
164 static BOOL install_file(const WCHAR *file_name)
165 {
166     ULONG res;
167
168     res = MsiInstallProductW(file_name, NULL);
169     if(res != ERROR_SUCCESS) {
170         ERR("MsiInstallProduct failed: %u\n", res);
171         return FALSE;
172     }
173
174     return TRUE;
175 }
176
177 static BOOL install_from_unix_file(const char *dir, const char *subdir, const char *file_name)
178 {
179     LPWSTR dos_file_name;
180     char *file_path;
181     int fd, len;
182     BOOL ret;
183
184     static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
185     static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
186
187     len = strlen(dir);
188     file_path = heap_alloc(len+strlen(subdir)+strlen(file_name)+3);
189     if(!file_path)
190         return FALSE;
191
192     memcpy(file_path, dir, len);
193     if(len && file_path[len-1] != '/' && file_path[len-1] != '\\')
194         file_path[len++] = '/';
195     if(*subdir) {
196         strcpy(file_path+len, subdir);
197         len += strlen(subdir);
198         file_path[len++] = '/';
199     }
200     strcpy(file_path+len, file_name);
201
202     fd = open(file_path, O_RDONLY);
203     if(fd == -1) {
204         TRACE("%s not found\n", debugstr_a(file_path));
205         heap_free(file_path);
206         return FALSE;
207     }
208
209     close(fd);
210
211     if(!wine_get_dos_file_name)
212         wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
213
214     if(wine_get_dos_file_name) { /* Wine UNIX mode */
215         dos_file_name = wine_get_dos_file_name(file_path);
216         if(!dos_file_name) {
217             ERR("Could not get dos file name of %s\n", debugstr_a(file_path));
218             heap_free(file_path);
219             return FALSE;
220         }
221     } else { /* Windows mode */
222         UINT res;
223         WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
224         res = MultiByteToWideChar( CP_ACP, 0, file_path, -1, 0, 0);
225         dos_file_name = heap_alloc (res*sizeof(WCHAR));
226         MultiByteToWideChar( CP_ACP, 0, file_path, -1, dos_file_name, res);
227     }
228
229     heap_free(file_path);
230
231     ret = install_file(dos_file_name);
232
233     heap_free(dos_file_name);
234     return ret;
235 }
236
237 static HKEY open_config_key(void)
238 {
239     HKEY hkey, ret;
240     DWORD res;
241
242     static const WCHAR wine_keyW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e',0};
243
244     /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
245     res = RegOpenKeyW(HKEY_CURRENT_USER, wine_keyW, &hkey);
246     if(res != ERROR_SUCCESS)
247         return NULL;
248
249     res = RegOpenKeyA(hkey, addon->config_key, &ret);
250     RegCloseKey(hkey);
251     return res == ERROR_SUCCESS ? ret : NULL;
252 }
253
254 static BOOL install_from_registered_dir(void)
255 {
256     char *package_dir;
257     HKEY hkey;
258     DWORD res, type, size = MAX_PATH;
259     BOOL ret;
260
261     hkey = open_config_key();
262     if(!hkey)
263         return FALSE;
264
265     package_dir = heap_alloc(size);
266     res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
267     if(res == ERROR_MORE_DATA) {
268         package_dir = heap_realloc(package_dir, size);
269         res = RegGetValueA(hkey, NULL, addon->dir_config_key, RRF_RT_ANY, &type, (PBYTE)package_dir, &size);
270     }
271     RegCloseKey(hkey);
272     if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
273         heap_free(package_dir);
274         return FALSE;
275     }
276
277     TRACE("Trying %s/%s\n", debugstr_a(package_dir), debugstr_a(addon->file_name));
278
279     ret = install_from_unix_file(package_dir, "", addon->file_name);
280
281     heap_free(package_dir);
282     return ret;
283 }
284
285 static BOOL install_from_default_dir(void)
286 {
287     const char *data_dir, *package_dir;
288     char *dir_buf = NULL;
289     int len;
290     BOOL ret;
291
292     if((data_dir = wine_get_data_dir())) {
293         package_dir = data_dir;
294     }else if((data_dir = wine_get_build_dir())) {
295         len = strlen(data_dir);
296         dir_buf = heap_alloc(len + sizeof("/../"));
297         memcpy(dir_buf, data_dir, len);
298         strcpy(dir_buf+len, "/../");
299         package_dir = dir_buf;
300     }else {
301         return FALSE;
302     }
303
304     ret = install_from_unix_file(package_dir, addon->subdir_name, addon->file_name);
305     heap_free(dir_buf);
306
307     if (!ret)
308         ret = install_from_unix_file(INSTALL_DATADIR "/wine/", addon->subdir_name, addon->file_name);
309     if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
310         ret = install_from_unix_file("/usr/share/wine/", addon->subdir_name, addon->file_name);
311     return ret;
312 }
313
314 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
315         REFIID riid, void **ppv)
316 {
317     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
318         *ppv = iface;
319         return S_OK;
320     }
321
322     return E_INVALIDARG;
323 }
324
325 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
326 {
327     return 2;
328 }
329
330 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
331 {
332     return 1;
333 }
334
335 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
336         DWORD dwReserved, IBinding *pib)
337 {
338     set_status(IDS_DOWNLOADING);
339     return S_OK;
340 }
341
342 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
343         LONG *pnPriority)
344 {
345     return E_NOTIMPL;
346 }
347
348 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
349        DWORD dwReserved)
350 {
351     return E_NOTIMPL;
352 }
353
354 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
355         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
356 {
357     HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
358
359     if(ulProgressMax)
360         SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
361     if(ulProgress)
362         SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
363
364     return S_OK;
365 }
366
367 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
368         HRESULT hresult, LPCWSTR szError)
369 {
370     if(FAILED(hresult)) {
371         ERR("Binding failed %08x\n", hresult);
372         return S_OK;
373     }
374
375     set_status(IDS_INSTALLING);
376     return S_OK;
377 }
378
379 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
380         DWORD* grfBINDF, BINDINFO* pbindinfo)
381 {
382     /* FIXME */
383     *grfBINDF = 0;
384     return S_OK;
385 }
386
387 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
388         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
389 {
390     ERR("\n");
391     return E_NOTIMPL;
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     DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
420     WCHAR *url, *config_key;
421     HKEY hkey;
422     DWORD res, type;
423     DWORD returned_size;
424
425     static const WCHAR httpW[] = {'h','t','t','p'};
426     static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
427     static const WCHAR v_formatW[] = {'&','v','='};
428
429     hkey = open_config_key();
430     if(!hkey)
431         return NULL;
432
433     url = heap_alloc(size);
434     returned_size = size;
435
436     config_key = heap_strdupAtoW(addon->url_config_key);
437     res = RegQueryValueExW(hkey, config_key, NULL, &type, (LPBYTE)url, &returned_size);
438     heap_free(config_key);
439     RegCloseKey(hkey);
440     if(res != ERROR_SUCCESS || type != REG_SZ) {
441         heap_free(url);
442         return NULL;
443     }
444
445     if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
446         DWORD len;
447
448         len = strlenW(url);
449         memcpy(url+len, arch_formatW, sizeof(arch_formatW));
450         len += sizeof(arch_formatW)/sizeof(WCHAR);
451         len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
452         memcpy(url+len, v_formatW, sizeof(v_formatW));
453         len += sizeof(v_formatW)/sizeof(WCHAR);
454         MultiByteToWideChar(CP_ACP, 0, addon->version, -1, url+len, size/sizeof(WCHAR)-len);
455     }
456
457     TRACE("Got URL %s\n", debugstr_w(url));
458     return url;
459 }
460
461 static DWORD WINAPI download_proc(PVOID arg)
462 {
463     WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
464     HRESULT hres;
465
466     GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
467     GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
468
469     TRACE("using temp file %s\n", debugstr_w(tmp_file));
470
471     hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
472     if(FAILED(hres)) {
473         ERR("URLDownloadToFile failed: %08x\n", hres);
474         return 0;
475     }
476
477     if(sha_check(tmp_file))
478         install_file(tmp_file);
479     DeleteFileW(tmp_file);
480     EndDialog(install_dialog, 0);
481     return 0;
482 }
483
484 static void run_winebrowser(const WCHAR *url)
485 {
486     PROCESS_INFORMATION pi;
487     STARTUPINFOW si;
488     WCHAR app[MAX_PATH];
489     LONG len, url_len;
490     WCHAR *args;
491     BOOL ret;
492
493     static const WCHAR winebrowserW[] = {'\\','w','i','n','e','b','r','o','w','s','e','r','.','e','x','e',0};
494
495     url_len = strlenW(url);
496
497     len = GetSystemDirectoryW(app, MAX_PATH-sizeof(winebrowserW)/sizeof(WCHAR));
498     memcpy(app+len, winebrowserW, sizeof(winebrowserW));
499     len += sizeof(winebrowserW)/sizeof(WCHAR) -1;
500
501     args = heap_alloc((len+1+url_len)*sizeof(WCHAR));
502     if(!args)
503         return;
504
505     memcpy(args, app, len*sizeof(WCHAR));
506     args[len++] = ' ';
507     memcpy(args+len, url, (url_len+1) * sizeof(WCHAR));
508
509     TRACE("starting %s\n", debugstr_w(args));
510
511     memset(&si, 0, sizeof(si));
512     si.cb = sizeof(si);
513     ret = CreateProcessW(app, args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
514     heap_free(args);
515     if (ret) {
516         CloseHandle(pi.hThread);
517         CloseHandle(pi.hProcess);
518     }
519 }
520
521 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
522 {
523     switch(msg) {
524     case WM_INITDIALOG:
525         ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
526         install_dialog = hwnd;
527         return TRUE;
528
529     case WM_NOTIFY:
530         switch (((NMHDR *)lParam)->code)
531         {
532         case NM_CLICK:
533         case NM_RETURN:
534             if (wParam == ID_DWL_STATUS)
535                 run_winebrowser(((NMLINK*)lParam)->item.szUrl);
536             break;
537         }
538         break;
539
540     case WM_COMMAND:
541         switch(wParam) {
542         case IDCANCEL:
543             EndDialog(hwnd, 0);
544             return FALSE;
545
546         case ID_DWL_INSTALL:
547             ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
548             EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
549             EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
550             CloseHandle( CreateThread(NULL, 0, download_proc, NULL, 0, NULL));
551             return FALSE;
552         }
553     }
554
555     return FALSE;
556 }
557
558 BOOL install_addon(addon_t addon_type)
559 {
560     if(!*ARCH_STRING)
561         return FALSE;
562
563     addon = addons_info+addon_type;
564
565     /*
566      * Try to find addon .msi file in following order:
567      * - directory stored in $dir_config_key value of HKCU/Wine/Software/$config_key key
568      * - $datadir/$addon_subdir/
569      * - $INSTALL_DATADIR/wine/$addon_subdir/
570      * - /usr/share/wine/$addon_subdir/
571      * - download from URL stored in $url_config_key value of HKCU/Wine/Software/$config_key key
572      */
573     if(!install_from_registered_dir()
574        && !install_from_default_dir()
575        && (url = get_url()))
576         DialogBoxW(hInst, addon->dialog_template, 0, installer_proc);
577
578     heap_free(url);
579     url = NULL;
580     return TRUE;
581 }