2 * Copyright 2006 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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 #include "mshtml_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
43 static HWND install_dialog = NULL;
44 static LPWSTR tmp_file_name = NULL;
45 static HANDLE tmp_file = INVALID_HANDLE_VALUE;
46 static LPWSTR url = NULL;
48 static void clean_up(void)
50 if(tmp_file != INVALID_HANDLE_VALUE)
51 CloseHandle(tmp_file);
54 DeleteFileW(tmp_file_name);
55 mshtml_free(tmp_file_name);
59 if(tmp_file != INVALID_HANDLE_VALUE) {
60 CloseHandle(tmp_file);
61 tmp_file = INVALID_HANDLE_VALUE;
65 EndDialog(install_dialog, 0);
68 static void set_status(DWORD id)
70 HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
73 LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
74 SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
77 static void set_registry(LPCSTR install_dir)
83 static const WCHAR wszMshtmlKey[] = {
84 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
85 '\\','M','S','H','T','M','L',0};
86 static const WCHAR wszGeckoPath[] = {'G','e','c','k','o','P','a','t','h',0};
87 static const WCHAR wszWineGecko[] = {'w','i','n','e','_','g','e','c','k','o',0};
89 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
90 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
91 if(res != ERROR_SUCCESS) {
92 ERR("Faild to open MSHTML key: %ld\n", res);
96 len = MultiByteToWideChar(CP_ACP, 0, install_dir, -1, NULL, 0)-1;
97 gecko_path = mshtml_alloc((len+1)*sizeof(WCHAR)+sizeof(wszWineGecko));
98 MultiByteToWideChar(CP_ACP, 0, install_dir, -1, gecko_path, (len+1)*sizeof(WCHAR));
100 if (len && gecko_path[len-1] != '\\')
101 gecko_path[len++] = '\\';
103 memcpy(gecko_path+len, wszWineGecko, sizeof(wszWineGecko));
105 size = len*sizeof(WCHAR)+sizeof(wszWineGecko);
106 res = RegSetValueExW(hkey, wszGeckoPath, 0, REG_SZ, (LPVOID)gecko_path,
107 len*sizeof(WCHAR)+sizeof(wszWineGecko));
108 mshtml_free(gecko_path);
110 if(res != ERROR_SUCCESS)
111 ERR("Failed to set GeckoPath value: %08lx\n", res);
114 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
115 REFIID riid, void **ppv)
117 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
125 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
130 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
135 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
136 DWORD dwReserved, IBinding *pib)
138 WCHAR tmp_dir[MAX_PATH];
140 set_status(IDS_DOWNLOADING);
142 GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
144 tmp_file_name = mshtml_alloc(MAX_PATH*sizeof(WCHAR));
145 GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);
147 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));
149 tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL,
150 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
152 if(tmp_file == INVALID_HANDLE_VALUE) {
153 ERR("Could not create file: %ld\n", GetLastError());
161 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
167 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
173 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
174 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
176 HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
179 SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
181 SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
186 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
187 HRESULT hresult, LPCWSTR szError)
192 char program_files[MAX_PATH];
193 typeof(ExtractFilesA) *pExtractFilesA;
196 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
198 if(FAILED(hresult)) {
199 ERR("Binding failed %08lx\n", hresult);
204 CloseHandle(tmp_file);
205 tmp_file = INVALID_HANDLE_VALUE;
207 set_status(IDS_INSTALLING);
209 advpack = LoadLibraryW(wszAdvpack);
210 pExtractFilesA = (typeof(ExtractFilesA)*)GetProcAddress(advpack, "ExtractFiles");
212 len = WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, NULL, 0, NULL, NULL);
213 file_name = mshtml_alloc(len);
214 WideCharToMultiByte(CP_ACP, 0, tmp_file_name, -1, file_name, -1, NULL, NULL);
216 GetEnvironmentVariableA("ProgramFiles", program_files, sizeof(program_files));
218 /* FIXME: Use unicode version (not yet implemented) */
219 hres = pExtractFilesA(file_name, program_files, 0, NULL, NULL, 0);
220 FreeLibrary(advpack);
221 mshtml_free(file_name);
223 ERR("Could not extract package: %08lx\n", hres);
227 set_registry(program_files);
233 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
234 DWORD* grfBINDF, BINDINFO* pbindinfo)
241 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
242 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
244 IStream *str = pstgmed->u.pstm;
251 hres = IStream_Read(str, buf, sizeof(buf), &size);
253 WriteFile(tmp_file, buf, size, NULL, NULL);
254 }while(hres == S_OK);
259 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
260 REFIID riid, IUnknown* punk)
266 static IBindStatusCallbackVtbl InstallCallbackVtbl = {
267 InstallCallback_QueryInterface,
268 InstallCallback_AddRef,
269 InstallCallback_Release,
270 InstallCallback_OnStartBinding,
271 InstallCallback_GetPriority,
272 InstallCallback_OnLowResource,
273 InstallCallback_OnProgress,
274 InstallCallback_OnStopBinding,
275 InstallCallback_GetBindInfo,
276 InstallCallback_OnDataAvailable,
277 InstallCallback_OnObjectAvailable
280 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
282 static LPWSTR get_url(void)
286 DWORD size = 512*sizeof(WCHAR);
289 static const WCHAR wszMshtmlKey[] = {
290 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
291 '\\','M','S','H','T','M','L',0};
292 static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
294 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
295 res = RegOpenKeyW(HKEY_CURRENT_USER, wszMshtmlKey, &hkey);
296 if(res != ERROR_SUCCESS)
299 url = mshtml_alloc(size);
301 res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &size);
303 if(res != ERROR_SUCCESS || type != REG_SZ) {
311 static DWORD WINAPI download_proc(PVOID arg)
318 CreateURLMoniker(NULL, url, &mon);
322 CreateAsyncBindCtx(0, &InstallCallback, 0, &bctx);
324 hres = IMoniker_BindToStorage(mon, bctx, NULL, &IID_IStream, (void**)&str);
325 IBindCtx_Release(bctx);
327 ERR("BindToStorage failed: %08lx\n", hres);
332 IStream_Release(str);
337 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
341 install_dialog = hwnd;
351 EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
352 EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
353 CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
361 void install_wine_gecko(void)
365 SetLastError(ERROR_SUCCESS);
366 hsem = CreateSemaphoreA( NULL, 0, 1, "mshtml_install_semaphore");
368 if(GetLastError() == ERROR_ALREADY_EXISTS) {
369 WaitForSingleObject(hsem, INFINITE);
371 if((url = get_url()))
372 DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
375 ReleaseSemaphore(hsem, 1, NULL);