2 * Copyright 2006-2010 Jacek Caban for CodeWeavers
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.
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.
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
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
48 #include "wine/library.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl);
52 #define GECKO_VERSION "1.1.0"
55 #define ARCH_STRING "x86"
56 #elif defined(__x86_64__)
57 #define ARCH_STRING "x86_64"
59 #define ARCH_STRING ""
62 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".cab"
64 static const WCHAR mshtml_keyW[] =
65 {'S','o','f','t','w','a','r','e',
67 '\\','M','S','H','T','M','L',0};
69 static HWND install_dialog = NULL;
70 static LPWSTR url = NULL;
72 static inline char *heap_strdupWtoA(LPCWSTR str)
77 DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
78 ret = heap_alloc(size);
79 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
85 static void set_status(DWORD id)
87 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
90 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
91 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
94 static void set_registry(const WCHAR *install_dir)
96 WCHAR mshtml_key[100];
101 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
102 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
104 memcpy(mshtml_key, mshtml_keyW, sizeof(mshtml_keyW));
105 mshtml_key[sizeof(mshtml_keyW)/sizeof(WCHAR)-1] = '\\';
106 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, sizeof(GECKO_VERSION),
107 mshtml_key+sizeof(mshtml_keyW)/sizeof(WCHAR),
108 (sizeof(mshtml_key)-sizeof(mshtml_keyW))/sizeof(WCHAR));
110 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
111 res = RegCreateKeyW(HKEY_CURRENT_USER, mshtml_key, &hkey);
112 if(res != ERROR_SUCCESS) {
113 ERR("Faild to create MSHTML key: %d\n", res);
117 len = strlenW(install_dir);
118 gecko_path = heap_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
119 memcpy(gecko_path, install_dir, len*sizeof(WCHAR));
121 if (len && gecko_path[len-1] != '\\')
122 gecko_path[len++] = '\\';
124 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
126 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
127 len*sizeof(WCHAR)+sizeof(wszWineGecko));
128 heap_free(gecko_path);
130 if(res != ERROR_SUCCESS)
131 ERR("Failed to set GeckoPath value: %08x\n", res);
134 static BOOL install_cab(LPCWSTR file_name)
136 char *install_dir_a, *file_name_a;
137 WCHAR install_dir[MAX_PATH];
141 static const WCHAR gecko_subdirW[] = {'\\','g','e','c','k','o','\\',0};
143 TRACE("(%s)\n", debugstr_w(file_name));
145 GetSystemDirectoryW(install_dir, sizeof(install_dir)/sizeof(WCHAR));
146 strcatW(install_dir, gecko_subdirW);
147 res = CreateDirectoryW(install_dir, NULL);
148 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
149 ERR("Could not create directory: %08u\n", GetLastError());
153 len = strlenW(install_dir);
154 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, install_dir+len, sizeof(install_dir)/sizeof(WCHAR)-len);
155 res = CreateDirectoryW(install_dir, NULL);
156 if(!res && GetLastError() != ERROR_ALREADY_EXISTS) {
157 ERR("Could not create directory: %08u\n", GetLastError());
162 /* FIXME: Use ExtractFilesW once it's implemented */
163 file_name_a = heap_strdupWtoA(file_name);
164 install_dir_a = heap_strdupWtoA(install_dir);
165 if(file_name_a && install_dir_a)
166 hres = ExtractFilesA(file_name_a, install_dir_a, 0, NULL, NULL, 0);
168 hres = E_OUTOFMEMORY;
169 heap_free(file_name_a);
170 heap_free(install_dir_a);
172 ERR("Could not extract package: %08x\n", hres);
176 set_registry(install_dir);
180 static BOOL install_from_unix_file(const char *file_name)
182 LPWSTR dos_file_name;
186 static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
187 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
189 fd = open(file_name, O_RDONLY);
191 TRACE("%s not found\n", debugstr_a(file_name));
197 if(!wine_get_dos_file_name)
198 wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
200 if(wine_get_dos_file_name) { /* Wine UNIX mode */
201 dos_file_name = wine_get_dos_file_name(file_name);
203 ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
206 } else { /* Windows mode */
208 WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
209 res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
210 dos_file_name = heap_alloc (res*sizeof(WCHAR));
211 MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
214 ret = install_cab(dos_file_name);
216 heap_free(dos_file_name);
220 static BOOL install_from_registered_dir(void)
224 DWORD res, type, size = MAX_PATH;
227 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
228 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
229 if(res != ERROR_SUCCESS)
232 file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
233 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
234 if(res == ERROR_MORE_DATA) {
235 file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
236 res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
239 if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
240 heap_free(file_name);
244 strcat(file_name, GECKO_FILE_NAME);
246 TRACE("Trying %s\n", debugstr_a(file_name));
248 ret = install_from_unix_file(file_name);
250 heap_free(file_name);
254 static BOOL install_from_default_dir(void)
256 const char *data_dir, *subdir;
261 if((data_dir = wine_get_data_dir()))
263 else if((data_dir = wine_get_build_dir()))
264 subdir = "/../gecko/";
268 len = strlen(data_dir);
269 len2 = strlen(subdir);
271 file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
272 memcpy(file_name, data_dir, len);
273 memcpy(file_name+len, subdir, len2);
274 memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
276 ret = install_from_unix_file(file_name);
278 heap_free(file_name);
281 ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
282 if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
283 ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
287 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
288 REFIID riid, void **ppv)
290 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
298 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
303 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
308 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
309 DWORD dwReserved, IBinding *pib)
311 set_status(IDS_DOWNLOADING);
315 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
321 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
327 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
328 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
330 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
333 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
335 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
340 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
341 HRESULT hresult, LPCWSTR szError)
343 if(FAILED(hresult)) {
344 ERR("Binding failed %08x\n", hresult);
348 set_status(IDS_INSTALLING);
352 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
353 DWORD* grfBINDF, BINDINFO* pbindinfo)
360 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
361 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
367 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
368 REFIID riid, IUnknown* punk)
374 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
375 InstallCallback_QueryInterface,
376 InstallCallback_AddRef,
377 InstallCallback_Release,
378 InstallCallback_OnStartBinding,
379 InstallCallback_GetPriority,
380 InstallCallback_OnLowResource,
381 InstallCallback_OnProgress,
382 InstallCallback_OnStopBinding,
383 InstallCallback_GetBindInfo,
384 InstallCallback_OnDataAvailable,
385 InstallCallback_OnObjectAvailable
388 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
390 static LPWSTR get_url(void)
394 DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
398 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
399 static const WCHAR httpW[] = {'h','t','t','p'};
400 static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
401 static const WCHAR v_formatW[] = {'&','v','='};
403 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
404 res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
405 if(res != ERROR_SUCCESS)
408 url = heap_alloc(size);
409 returned_size = size;
411 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
413 if(res != ERROR_SUCCESS || type != REG_SZ) {
418 if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
422 memcpy(url+len, arch_formatW, sizeof(arch_formatW));
423 len += sizeof(arch_formatW)/sizeof(WCHAR);
424 len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
425 memcpy(url+len, v_formatW, sizeof(v_formatW));
426 len += sizeof(v_formatW)/sizeof(WCHAR);
427 MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
430 TRACE("Got URL %s\n", debugstr_w(url));
434 static DWORD WINAPI download_proc(PVOID arg)
436 WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
439 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
440 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
442 TRACE("using temp file %s\n", debugstr_w(tmp_file));
444 hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
446 ERR("URLDownloadToFile failed: %08x\n", hres);
450 install_cab(tmp_file);
451 DeleteFileW(tmp_file);
452 EndDialog(install_dialog, 0);
456 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
460 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
461 install_dialog = hwnd;
471 ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
472 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
473 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
474 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
482 BOOL install_wine_gecko(void)
488 * Try to find Gecko .cab file in following order:
489 * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
491 * - $INSTALL_DATADIR/wine/gecko/
492 * - /usr/share/wine/gecko/
493 * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
495 if(!install_from_registered_dir()
496 && !install_from_default_dir()
497 && (url = get_url()))
498 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);