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
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 #include "mshtml_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
45 static HWND install_dialog
= NULL
;
46 static LPWSTR tmp_file_name
= NULL
;
47 static HANDLE tmp_file
= INVALID_HANDLE_VALUE
;
48 static LPWSTR url
= NULL
;
50 static void clean_up(void)
52 if(tmp_file
!= INVALID_HANDLE_VALUE
)
53 CloseHandle(tmp_file
);
56 DeleteFileW(tmp_file_name
);
57 mshtml_free(tmp_file_name
);
61 if(tmp_file
!= INVALID_HANDLE_VALUE
) {
62 CloseHandle(tmp_file
);
63 tmp_file
= INVALID_HANDLE_VALUE
;
67 EndDialog(install_dialog
, 0);
70 static void set_status(DWORD id
)
72 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
75 LoadStringW(hInst
, id
, buf
, sizeof(buf
)/sizeof(WCHAR
));
76 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
79 static void set_registry(LPCSTR install_dir
)
81 WCHAR mshtml_key
[100];
86 static const WCHAR wszMshtmlKey
[] = {
87 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
88 '\\','M','S','H','T','M','L','\\'};
89 static const WCHAR wszGeckoPath
[] = {'G','e','c','k','o','P','a','t','h',0};
90 static const WCHAR wszWineGecko
[] = {'w','i','n','e','_','g','e','c','k','o',0};
92 memcpy(mshtml_key
, wszMshtmlKey
, sizeof(wszMshtmlKey
));
93 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, sizeof(GECKO_VERSION
),
94 mshtml_key
+sizeof(wszMshtmlKey
)/sizeof(WCHAR
),
95 (sizeof(mshtml_key
)-sizeof(wszMshtmlKey
))/sizeof(WCHAR
));
97 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML\<version> */
98 res
= RegCreateKeyW(HKEY_CURRENT_USER
, mshtml_key
, &hkey
);
99 if(res
!= ERROR_SUCCESS
) {
100 ERR("Faild to create MSHTML key: %d\n", res
);
104 len
= MultiByteToWideChar(CP_ACP
, 0, install_dir
, -1, NULL
, 0)-1;
105 gecko_path
= mshtml_alloc((len
+1)*sizeof(WCHAR
)+sizeof(wszWineGecko
));
106 MultiByteToWideChar(CP_ACP
, 0, install_dir
, -1, gecko_path
, (len
+1)*sizeof(WCHAR
));
108 if (len
&& gecko_path
[len
-1] != '\\')
109 gecko_path
[len
++] = '\\';
111 memcpy(gecko_path
+len
, wszWineGecko
, sizeof(wszWineGecko
));
113 size
= len
*sizeof(WCHAR
)+sizeof(wszWineGecko
);
114 res
= RegSetValueExW(hkey
, wszGeckoPath
, 0, REG_SZ
, (LPVOID
)gecko_path
,
115 len
*sizeof(WCHAR
)+sizeof(wszWineGecko
));
116 mshtml_free(gecko_path
);
118 if(res
!= ERROR_SUCCESS
)
119 ERR("Failed to set GeckoPath value: %08x\n", res
);
122 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
123 REFIID riid
, void **ppv
)
125 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
133 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
138 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
143 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
144 DWORD dwReserved
, IBinding
*pib
)
146 WCHAR tmp_dir
[MAX_PATH
];
148 set_status(IDS_DOWNLOADING
);
150 GetTempPathW(sizeof(tmp_dir
)/sizeof(WCHAR
), tmp_dir
);
152 tmp_file_name
= mshtml_alloc(MAX_PATH
*sizeof(WCHAR
));
153 GetTempFileNameW(tmp_dir
, NULL
, 0, tmp_file_name
);
155 TRACE("creating temp file %s\n", debugstr_w(tmp_file_name
));
157 tmp_file
= CreateFileW(tmp_file_name
, GENERIC_WRITE
, 0, NULL
,
158 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
160 if(tmp_file
== INVALID_HANDLE_VALUE
) {
161 ERR("Could not create file: %d\n", GetLastError());
169 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
175 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
181 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
182 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
184 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
187 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
189 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
194 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
195 HRESULT hresult
, LPCWSTR szError
)
200 char install_dir
[MAX_PATH
];
201 typeof(ExtractFilesA
) *pExtractFilesA
;
205 static const WCHAR wszAdvpack
[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
207 if(FAILED(hresult
)) {
208 ERR("Binding failed %08x\n", hresult
);
213 CloseHandle(tmp_file
);
214 tmp_file
= INVALID_HANDLE_VALUE
;
216 set_status(IDS_INSTALLING
);
218 GetWindowsDirectoryA(install_dir
, sizeof(install_dir
));
219 strcat(install_dir
, "\\gecko\\");
220 res
= CreateDirectoryA(install_dir
, NULL
);
221 if(!res
&& GetLastError() != ERROR_ALREADY_EXISTS
) {
222 ERR("Could not create directory: %08u\n", GetLastError());
226 strcat(install_dir
, GECKO_VERSION
);
227 res
= CreateDirectoryA(install_dir
, NULL
);
228 if(!res
&& GetLastError() != ERROR_ALREADY_EXISTS
) {
229 ERR("Could not create directory: %08u\n", GetLastError());
233 advpack
= LoadLibraryW(wszAdvpack
);
234 pExtractFilesA
= (typeof(ExtractFilesA
)*)GetProcAddress(advpack
, "ExtractFiles");
236 len
= WideCharToMultiByte(CP_ACP
, 0, tmp_file_name
, -1, NULL
, 0, NULL
, NULL
);
237 file_name
= mshtml_alloc(len
);
238 WideCharToMultiByte(CP_ACP
, 0, tmp_file_name
, -1, file_name
, -1, NULL
, NULL
);
240 /* FIXME: Use unicode version (not yet implemented) */
241 hres
= pExtractFilesA(file_name
, install_dir
, 0, NULL
, NULL
, 0);
242 FreeLibrary(advpack
);
243 mshtml_free(file_name
);
245 ERR("Could not extract package: %08x\n", hres
);
249 set_registry(install_dir
);
255 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
256 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
263 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
264 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
266 IStream
*str
= pstgmed
->u
.pstm
;
273 hres
= IStream_Read(str
, buf
, sizeof(buf
), &size
);
275 WriteFile(tmp_file
, buf
, size
, NULL
, NULL
);
276 }while(hres
== S_OK
);
281 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
282 REFIID riid
, IUnknown
* punk
)
288 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
289 InstallCallback_QueryInterface
,
290 InstallCallback_AddRef
,
291 InstallCallback_Release
,
292 InstallCallback_OnStartBinding
,
293 InstallCallback_GetPriority
,
294 InstallCallback_OnLowResource
,
295 InstallCallback_OnProgress
,
296 InstallCallback_OnStopBinding
,
297 InstallCallback_GetBindInfo
,
298 InstallCallback_OnDataAvailable
,
299 InstallCallback_OnObjectAvailable
302 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
304 static LPWSTR
get_url(void)
308 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
311 static const WCHAR wszMshtmlKey
[] = {
312 'S','o','f','t','w','a','r','e','\\','W','i','n','e',
313 '\\','M','S','H','T','M','L',0};
314 static const WCHAR wszGeckoUrl
[] = {'G','e','c','k','o','U','r','l',0};
315 static const WCHAR httpW
[] = {'h','t','t','p'};
316 static const WCHAR v_formatW
[] = {'?','v','=',0};
318 /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
319 res
= RegOpenKeyW(HKEY_CURRENT_USER
, wszMshtmlKey
, &hkey
);
320 if(res
!= ERROR_SUCCESS
)
323 url
= mshtml_alloc(size
);
325 res
= RegQueryValueExW(hkey
, wszGeckoUrl
, NULL
, &type
, (LPBYTE
)url
, &size
);
327 if(res
!= ERROR_SUCCESS
|| type
!= REG_SZ
) {
332 if(size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) {
333 strcatW(url
, v_formatW
);
334 MultiByteToWideChar(CP_ACP
, 0, GECKO_VERSION
, -1, url
+strlenW(url
), -1);
337 TRACE("Got URL %s\n", debugstr_w(url
));
341 static DWORD WINAPI
download_proc(PVOID arg
)
348 CreateURLMoniker(NULL
, url
, &mon
);
352 CreateAsyncBindCtx(0, &InstallCallback
, 0, &bctx
);
354 hres
= IMoniker_BindToStorage(mon
, bctx
, NULL
, &IID_IStream
, (void**)&str
);
355 IBindCtx_Release(bctx
);
357 ERR("BindToStorage failed: %08x\n", hres
);
362 IStream_Release(str
);
367 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
371 install_dialog
= hwnd
;
381 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
382 EnableWindow(GetDlgItem(hwnd
, IDCANCEL
), 0); /* FIXME */
383 CreateThread(NULL
, 0, download_proc
, NULL
, 0, NULL
);
391 BOOL
install_wine_gecko(void)
395 SetLastError(ERROR_SUCCESS
);
396 hsem
= CreateSemaphoreA( NULL
, 0, 1, "mshtml_install_semaphore");
398 if(GetLastError() == ERROR_ALREADY_EXISTS
) {
399 WaitForSingleObject(hsem
, INFINITE
);
401 if((url
= get_url()))
402 DialogBoxW(hInst
, MAKEINTRESOURCEW(ID_DWL_DIALOG
), 0, installer_proc
);
405 ReleaseSemaphore(hsem
, 1, NULL
);