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