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
;
34 typedef struct _IBindStatusCallbackImpl
36 const IBindStatusCallbackVtbl
*vtbl
;
40 } IBindStatusCallbackImpl
;
44 dlQueryInterface(IBindStatusCallback
* This
, REFIID riid
, void** ppvObject
)
46 if (!ppvObject
) return E_POINTER
;
48 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IBindStatusCallback
))
50 IBindStatusCallback_AddRef(This
);
60 dlAddRef(IBindStatusCallback
* iface
)
62 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
63 return InterlockedIncrement(&This
->ref
);
68 dlRelease(IBindStatusCallback
* iface
)
70 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
71 DWORD ref
= InterlockedDecrement(&This
->ref
);
75 DestroyWindow(This
->hDialog
);
76 HeapFree(GetProcessHeap(), 0, This
);
84 dlOnStartBinding(IBindStatusCallback
* iface
, DWORD dwReserved
, IBinding
* pib
)
91 dlGetPriority(IBindStatusCallback
* iface
, LONG
* pnPriority
)
98 dlOnLowResource( IBindStatusCallback
* iface
, DWORD reserved
)
105 dlOnProgress(IBindStatusCallback
* iface
,
109 LPCWSTR szStatusText
)
111 IBindStatusCallbackImpl
*This
= (IBindStatusCallbackImpl
*) iface
;
116 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_PROGRESS
);
117 if (Item
&& ulProgressMax
)
119 SendMessageW(Item
, PBM_SETPOS
, ((ULONGLONG
)ulProgress
* 100) / ulProgressMax
, 0);
122 Item
= GetDlgItem(This
->hDialog
, IDC_DOWNLOAD_STATUS
);
123 if (Item
&& szStatusText
)
125 SendMessageW(Item
, WM_GETTEXT
, sizeof(OldText
) / sizeof(OldText
[0]), (LPARAM
) OldText
);
126 if (sizeof(OldText
) / sizeof(OldText
[0]) - 1 <= wcslen(OldText
) || 0 != wcscmp(OldText
, szStatusText
))
128 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) szStatusText
);
133 r
= GetWindowLongPtrW(This
->hDialog
, GWLP_USERDATA
);
134 if (0 != r
|| 0 != GetLastError())
136 *This
->pbCancelled
= TRUE
;
145 dlOnStopBinding(IBindStatusCallback
* iface
, HRESULT hresult
, LPCWSTR szError
)
152 dlGetBindInfo(IBindStatusCallback
* iface
, DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
159 dlOnDataAvailable(IBindStatusCallback
* iface
, DWORD grfBSCF
,
160 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
167 dlOnObjectAvailable(IBindStatusCallback
* iface
, REFIID riid
, IUnknown
* punk
)
172 static const IBindStatusCallbackVtbl dlVtbl
=
187 static IBindStatusCallback
*
188 CreateDl(HWND Dlg
, BOOL
*pbCancelled
)
190 IBindStatusCallbackImpl
*This
;
192 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IBindStatusCallbackImpl
));
196 This
->vtbl
= &dlVtbl
;
199 This
->pbCancelled
= pbCancelled
;
201 return (IBindStatusCallback
*) This
;
206 ThreadFunc(LPVOID Context
)
208 IBindStatusCallback
*dl
= NULL
;
209 WCHAR path
[MAX_PATH
];
211 HWND Dlg
= (HWND
) Context
;
212 DWORD dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
213 DWORD dwCurrentBytesRead
= 0;
214 DWORD dwStatusLen
= sizeof(dwStatus
);
215 BOOL bCancelled
= FALSE
;
216 BOOL bTempfile
= FALSE
;
218 HINTERNET hOpen
= NULL
;
219 HINTERNET hFile
= NULL
;
220 HANDLE hOut
= INVALID_HANDLE_VALUE
;
221 unsigned char lpBuffer
[4096];
222 const LPWSTR lpszAgent
= L
"RApps/1.0";
223 URL_COMPONENTS urlComponents
;
226 /* built the path for the download */
227 p
= wcsrchr(AppInfo
->szUrlDownload
, L
'/');
232 if (wcscmp(AppInfo
->szUrlDownload
, APPLICATION_DATABASE_URL
) == 0)
235 if (!GetStorageDirectory(path
, sizeof(path
) / sizeof(path
[0])))
240 if (FAILED(StringCbCopyW(path
, sizeof(path
), SettingsInfo
.szDownloadDir
)))
245 if (GetFileAttributesW(path
) == INVALID_FILE_ATTRIBUTES
)
247 if (!CreateDirectoryW(path
, NULL
))
251 if (FAILED(StringCbCatW(path
, sizeof(path
), L
"\\")))
253 if (FAILED(StringCbCatW(path
, sizeof(path
), p
+ 1)))
258 dl
= CreateDl(Context
, &bCancelled
);
263 switch(SettingsInfo
.Proxy
)
265 case 0: /* preconfig */
266 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
268 case 1: /* direct (no proxy) */
269 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
271 case 2: /* use proxy */
272 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
274 default: /* preconfig */
275 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
282 hFile
= InternetOpenUrlW(hOpen
, AppInfo
->szUrlDownload
, NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
|INTERNET_FLAG_KEEP_CONNECTION
, 0);
286 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
289 if(dwStatus
!= HTTP_STATUS_OK
)
291 WCHAR szMsgText
[MAX_STR_LEN
];
293 if (!LoadStringW(hInst
, IDS_UNABLE_TO_DOWNLOAD
, szMsgText
, sizeof(szMsgText
) / sizeof(WCHAR
)))
296 MessageBoxW(hMainWnd
, szMsgText
, NULL
, MB_OK
| MB_ICONERROR
);
300 dwStatusLen
= sizeof(dwStatus
);
302 memset(&urlComponents
, 0, sizeof(urlComponents
));
303 urlComponents
.dwStructSize
= sizeof(urlComponents
);
305 if(FAILED(StringCbLengthW(AppInfo
->szUrlDownload
, sizeof(AppInfo
->szUrlDownload
), &urlLength
)))
308 urlComponents
.dwSchemeLength
= urlLength
*sizeof(WCHAR
);
309 urlComponents
.lpszScheme
= malloc(urlComponents
.dwSchemeLength
);
311 if(!InternetCrackUrlW(AppInfo
->szUrlDownload
, urlLength
+1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
314 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
315 HttpQueryInfo(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
317 if(urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
318 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
320 free(urlComponents
.lpszScheme
);
322 hOut
= CreateFileW(path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
324 if (hOut
== INVALID_HANDLE_VALUE
)
329 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
)) goto end
;
330 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
)) goto end
;
331 dwCurrentBytesRead
+= dwBytesRead
;
332 IBindStatusCallback_OnProgress(dl
, dwCurrentBytesRead
, dwContentLen
, 0, AppInfo
->szUrlDownload
);
337 hOut
= INVALID_HANDLE_VALUE
;
342 ShowWindow(Dlg
, SW_HIDE
);
346 ShellExecuteW( NULL
, L
"open", path
, NULL
, NULL
, SW_SHOWNORMAL
);
349 if (hOut
!= INVALID_HANDLE_VALUE
)
352 InternetCloseHandle(hFile
);
353 InternetCloseHandle(hOpen
);
356 IBindStatusCallback_Release(dl
);
360 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
371 DownloadDlgProc(HWND Dlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
381 HICON hIconSm
= NULL
, hIconBg
= NULL
;
383 hIconBg
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICON
);
384 hIconSm
= (HICON
)GetClassLongPtr(hMainWnd
, GCLP_HICONSM
);
386 if (hIconBg
&& hIconSm
)
388 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
389 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
392 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 0);
393 Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
396 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
397 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
400 Thread
= CreateThread(NULL
, 0, ThreadFunc
, Dlg
, 0, &ThreadId
);
401 if (!Thread
) return FALSE
;
407 if (wParam
== IDCANCEL
)
409 SetWindowLongPtrW(Dlg
, GWLP_USERDATA
, 1);
410 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
424 DownloadApplication(INT Index
)
426 if (!IS_AVAILABLE_ENUM(SelectedEnumType
))
429 AppInfo
= (PAPPLICATION_INFO
) ListViewGetlParam(Index
);
430 if (!AppInfo
) return FALSE
;
432 WriteLogMessage(EVENTLOG_SUCCESS
, MSG_SUCCESS_INSTALL
, AppInfo
->szName
);
435 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
443 DownloadApplicationsDB(LPWSTR lpUrl
)
445 APPLICATION_INFO IntInfo
;
447 ZeroMemory(&IntInfo
, sizeof(APPLICATION_INFO
));
448 if (FAILED(StringCbCopyW(IntInfo
.szUrlDownload
,
449 sizeof(IntInfo
.szUrlDownload
),
458 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),