1 /* PROJECT: ReactOS Applications Manager
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/applications/rapps/loaddlg.c
4 * PURPOSE: Displaying a download dialog
5 * COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
6 * Copyright 2004 Mike McCormack (for CodeWeavers)
7 * Copyright 2005 Ge van Geldorp (gvg@reactos.org)
8 * Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
11 * Based on Wine dlls/shdocvw/shdocvw_main.c
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 static PAPPLICATION_INFO AppInfo
;
33 static HICON hIcon
= NULL
;
35 typedef struct _IBindStatusCallbackImpl
37 const IBindStatusCallbackVtbl
*vtbl
;
41 } IBindStatusCallbackImpl
;
45 dlQueryInterface(IBindStatusCallback
* This
, REFIID riid
, void** ppvObject
)
47 if (!ppvObject
) return E_POINTER
;
49 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IBindStatusCallback
))
51 IBindStatusCallback_AddRef(This
);
61 dlAddRef(IBindStatusCallback
* iface
)
63 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
64 return InterlockedIncrement(&This
->ref
);
69 dlRelease(IBindStatusCallback
* iface
)
71 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
72 DWORD ref
= InterlockedDecrement(&This
->ref
);
76 DestroyWindow(This
->hDialog
);
77 HeapFree(GetProcessHeap(), 0, This
);
85 dlOnStartBinding(IBindStatusCallback
* iface
, DWORD dwReserved
, IBinding
* pib
)
92 dlGetPriority(IBindStatusCallback
* iface
, LONG
* pnPriority
)
99 dlOnLowResource( IBindStatusCallback
* iface
, DWORD reserved
)
106 dlOnProgress(IBindStatusCallback
* iface
,
110 LPCWSTR szStatusText
)
112 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
117 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_PROGRESS
);
118 if (Item
&& ulProgressMax
)
120 SendMessageW(Item
, PBM_SETPOS
, ((ULONGLONG
)ulProgress
* 100) / ulProgressMax
, 0);
123 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_STATUS
);
124 if (Item
&& szStatusText
)
126 SendMessageW(Item
, WM_GETTEXT
, sizeof(OldText
) / sizeof(OldText
[0]), (LPARAM
) OldText
);
127 if (sizeof(OldText
) / sizeof(OldText
[0]) - 1 <= wcslen(OldText
) || 0 != wcscmp(OldText
, szStatusText
))
129 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) szStatusText
);
134 r
= GetWindowLongPtrW(This
->hDialog
, GWLP_USERDATA
);
135 if (0 != r
|| 0 != GetLastError())
137 *This
->pbCancelled
= TRUE
;
146 dlOnStopBinding(IBindStatusCallback
* iface
, HRESULT hresult
, LPCWSTR szError
)
153 dlGetBindInfo(IBindStatusCallback
* iface
, DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
160 dlOnDataAvailable(IBindStatusCallback
* iface
, DWORD grfBSCF
,
161 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
168 dlOnObjectAvailable(IBindStatusCallback
* iface
, REFIID riid
, IUnknown
* punk
)
173 static const IBindStatusCallbackVtbl dlVtbl
=
188 static IBindStatusCallback
*
189 CreateDl(HWND Dlg
, BOOL
*pbCancelled
)
191 IBindStatusCallbackImpl
*This
;
193 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IBindStatusCallbackImpl
));
197 This
->vtbl
= &dlVtbl
;
200 This
->pbCancelled
= pbCancelled
;
202 return (IBindStatusCallback
*) This
;
207 ThreadFunc(LPVOID Context
)
209 IBindStatusCallback
*dl
= NULL
;
210 WCHAR path
[MAX_PATH
];
212 HWND Dlg
= (HWND
) Context
;
213 DWORD dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
214 DWORD dwCurrentBytesRead
= 0;
215 DWORD dwStatusLen
= sizeof(dwStatus
);
216 BOOL bCancelled
= FALSE
;
217 BOOL bTempfile
= FALSE
;
219 HINTERNET hOpen
= NULL
;
220 HINTERNET hFile
= NULL
;
221 HANDLE hOut
= INVALID_HANDLE_VALUE
;
222 unsigned char lpBuffer
[4096];
223 const LPWSTR lpszAgent
= L
"RApps/1.0";
224 URL_COMPONENTS urlComponents
;
227 /* built the path for the download */
228 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
233 if (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0)
236 if (!GetStorageDirectory(path
, sizeof(path
) / sizeof(path
[0])))
241 if (FAILED(StringCbCopyW(path
, sizeof(path
), SettingsInfo
.szDownloadDir
)))
246 if (GetFileAttributesW(path
) == INVALID_FILE_ATTRIBUTES
)
248 if (!CreateDirectoryW(path
, NULL
))
252 if (FAILED(StringCbCatW(path
, sizeof(path
), L
"\\")))
254 if (FAILED(StringCbCatW(path
, sizeof(path
), p
+ 1)))
259 dl
= CreateDl(Context
, &bCancelled
);
264 switch(SettingsInfo
.Proxy
)
266 case 0: /* preconfig */
267 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
269 case 1: /* direct (no proxy) */
270 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
272 case 2: /* use proxy */
273 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
275 default: /* preconfig */
276 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
283 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
|INTERNET_FLAG_KEEP_CONNECTION
, 0);
287 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
290 if(dwStatus
!= HTTP_STATUS_OK
)
292 WCHAR szMsgText
[MAX_STR_LEN
];
294 if (!LoadStringW(hInst
, IDS_UNABLE_TO_DOWNLOAD
, szMsgText
, sizeof(szMsgText
) / sizeof(WCHAR
)))
297 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
301 dwStatusLen
= sizeof(dwStatus
);
303 memset(&urlComponents
, 0, sizeof(urlComponents
));
304 urlComponents
.dwStructSize
= sizeof(urlComponents
);
306 if(FAILED(StringCbLengthW(AppInfo
->szUrlDownload
, sizeof(AppInfo
->szUrlDownload
), &urlLength
)))
309 urlComponents
.dwSchemeLength
= urlLength
*sizeof(WCHAR
);
310 urlComponents
.lpszScheme
= malloc(urlComponents
.dwSchemeLength
);
312 if(!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
315 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
316 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
318 if(urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
319 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
321 free(urlComponents
.lpszScheme
);
323 hOut
= CreateFileW(path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
325 if (hOut
== INVALID_HANDLE_VALUE
)
330 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
)) goto end
;
331 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
)) goto end
;
332 dwCurrentBytesRead
+= dwBytesRead
;
333 IBindStatusCallback_OnProgress(dl
, dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
338 hOut
= INVALID_HANDLE_VALUE
;
343 ShowWindow(Dlg
, SW_HIDE
);
347 ShellExecuteW( NULL
, L
"open", path
, NULL
, NULL
, SW_SHOWNORMAL
);
350 if (hOut
!= INVALID_HANDLE_VALUE
)
353 InternetCloseHandle(hFile
);
354 InternetCloseHandle(hOpen
);
357 IBindStatusCallback_Release(dl
);
361 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
372 DownloadDlgProc(HWND Dlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
382 hIcon
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_MAIN
));
385 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIcon
);
386 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIcon
);
389 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
390 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
393 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
394 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
397 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
398 if (!Thread
) return FALSE
;
403 if (wParam
== IDCANCEL
)
405 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
406 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
411 if (hIcon
) DestroyIcon(hIcon
);
421 DownloadApplication(INT Index
)
423 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
426 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
427 if (!AppInfo
) return FALSE
;
429 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
);
432 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
440 DownloadApplicationsDB(LPWSTR lpUrl
)
442 APPLICATION_INFO IntInfo
;
444 ZeroMemory(&IntInfo
, sizeof(APPLICATION_INFO
));
445 if (FAILED(StringCbCopyW(IntInfo
.szUrlDownload
,
446 sizeof(IntInfo
.szUrlDownload
),
455 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),