2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * FILE: base/applications/rapps/loaddlg.cpp
5 * PURPOSE: Displaying a download dialog
6 * COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
7 * Copyright 2004 Mike McCormack (for CodeWeavers)
8 * Copyright 2005 Ge van Geldorp (gvg@reactos.org)
9 * Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
10 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
11 * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
15 * Based on Wine dlls/shdocvw/shdocvw_main.c
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <shlobj_undoc.h>
34 #include <shlguid_undoc.h>
40 #include <shellutils.h>
49 #ifdef USE_CERT_PINNING
50 #define CERT_ISSUER_INFO "US\r\nLet's Encrypt\r\nLet's Encrypt Authority X3"
51 #define CERT_SUBJECT_INFO "svn.reactos.org"
56 DLSTATUS_WAITING
= IDS_STATUS_WAITING
,
57 DLSTATUS_DOWNLOADING
= IDS_STATUS_DOWNLOADING
,
58 DLSTATUS_WAITING_INSTALL
= IDS_STATUS_DOWNLOADED
,
59 DLSTATUS_INSTALLING
= IDS_STATUS_INSTALLING
,
60 DLSTATUS_INSTALLED
= IDS_STATUS_INSTALLED
,
61 DLSTATUS_FINISHED
= IDS_STATUS_FINISHED
64 ATL::CStringW
LoadStatusString(DownloadStatus StatusParam
)
66 ATL::CStringW szString
;
67 szString
.LoadStringW(StatusParam
);
74 DownloadInfo(const CAvailableApplicationInfo
& AppInfo
)
75 :szUrl(AppInfo
.m_szUrlDownload
), szName(AppInfo
.m_szName
), szSHA1(AppInfo
.m_szSHA1
)
86 DownloadParam() : Dialog(NULL
), AppInfo(), szCaption(NULL
) {}
87 DownloadParam(HWND dlg
, const ATL::CSimpleArray
<DownloadInfo
> &info
, LPCWSTR caption
)
88 : Dialog(dlg
), AppInfo(info
), szCaption(caption
)
93 ATL::CSimpleArray
<DownloadInfo
> AppInfo
;
98 class CDownloadDialog
:
99 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
100 public IBindStatusCallback
104 BOOL m_UrlHasBeenCopied
;
109 //DestroyWindow(m_hDialog);
112 HRESULT
Initialize(HWND Dlg
, BOOL
*pbCancelled
)
115 m_pbCancelled
= pbCancelled
;
116 m_UrlHasBeenCopied
= FALSE
;
120 virtual HRESULT STDMETHODCALLTYPE
OnStartBinding(
127 virtual HRESULT STDMETHODCALLTYPE
GetPriority(
133 virtual HRESULT STDMETHODCALLTYPE
OnLowResource(
139 virtual HRESULT STDMETHODCALLTYPE
OnProgress(
143 LPCWSTR szStatusText
)
148 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_PROGRESS
);
151 WCHAR szProgress
[100];
153 /* format the bits and bytes into pretty and accessible units... */
154 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
156 /* use our subclassed progress bar text subroutine */
157 ATL::CStringW m_ProgressText
;
161 /* total size is known */
162 WCHAR szProgressMax
[100];
163 UINT uiPercentage
= ((ULONGLONG
) ulProgress
* 100) / ulProgressMax
;
165 /* send the current progress to the progress bar */
166 SendMessageW(Item
, PBM_SETPOS
, uiPercentage
, 0);
168 /* format total download size */
169 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
171 /* generate the text on progress bar */
172 m_ProgressText
.Format(L
"%u%% \x2014 %ls / %ls",
179 /* send the current progress to the progress bar */
180 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
182 /* total size is not known, display only current size */
183 m_ProgressText
.Format(L
"%ls...",
186 /* and finally display it */
187 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) m_ProgressText
.GetString());
190 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_STATUS
);
191 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && m_UrlHasBeenCopied
== FALSE
)
193 DWORD len
= wcslen(szStatusText
) + 1;
196 /* beautify our url for display purposes */
197 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &len
, ICU_DECODE
| ICU_NO_ENCODE
))
199 /* just use the original */
208 /* paste it into our dialog and don't do it again in this instance */
209 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
210 m_UrlHasBeenCopied
= TRUE
;
213 SetLastError(ERROR_SUCCESS
);
214 r
= GetWindowLongPtrW(m_hDialog
, GWLP_USERDATA
);
215 if (r
|| GetLastError() != ERROR_SUCCESS
)
217 *m_pbCancelled
= TRUE
;
224 virtual HRESULT STDMETHODCALLTYPE
OnStopBinding(
231 virtual HRESULT STDMETHODCALLTYPE
GetBindInfo(
238 virtual HRESULT STDMETHODCALLTYPE
OnDataAvailable(
241 FORMATETC
*pformatetc
,
247 virtual HRESULT STDMETHODCALLTYPE
OnObjectAvailable(
254 BEGIN_COM_MAP(CDownloadDialog
)
255 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback
, IBindStatusCallback
)
259 class CDowloadingAppsListView
260 : public CUiWindow
<CListView
>
263 HWND
Create(HWND hwndParent
)
265 RECT r
= {10, 150, 320, 350};
266 const DWORD style
= WS_CHILD
| WS_VISIBLE
| LVS_REPORT
| LVS_SINGLESEL
267 | LVS_SHOWSELALWAYS
| LVS_NOSORTHEADER
| LVS_NOCOLUMNHEADER
;
269 HWND hwnd
= CListView::Create(hwndParent
, r
, NULL
, style
, WS_EX_CLIENTEDGE
);
271 AddColumn(0, 150, LVCFMT_LEFT
);
272 AddColumn(1, 120, LVCFMT_LEFT
);
277 VOID
LoadList(ATL::CSimpleArray
<DownloadInfo
> arrInfo
)
279 for (INT i
= 0; i
< arrInfo
.GetSize(); ++i
)
281 AddRow(i
, arrInfo
[i
].szName
.GetString(), DLSTATUS_WAITING
);
285 VOID
SetDownloadStatus(INT ItemIndex
, DownloadStatus Status
)
287 HWND hListView
= GetWindow();
288 ATL::CStringW szBuffer
= LoadStatusString(Status
);
289 ListView_SetItemText(hListView
, ItemIndex
, 1, const_cast<LPWSTR
>(szBuffer
.GetString()));
292 BOOL
AddItem(INT ItemIndex
, LPWSTR lpText
)
296 ZeroMemory(&Item
, sizeof(Item
));
298 Item
.mask
= LVIF_TEXT
| LVIF_STATE
;
299 Item
.pszText
= lpText
;
300 Item
.iItem
= ItemIndex
;
302 return InsertItem(&Item
);
305 VOID
AddRow(INT RowIndex
, LPCWSTR szAppName
, const DownloadStatus Status
)
307 ATL::CStringW szStatus
= LoadStatusString(Status
);
309 const_cast<LPWSTR
>(szAppName
));
310 SetDownloadStatus(RowIndex
, Status
);
313 BOOL
AddColumn(INT Index
, INT Width
, INT Format
)
316 ZeroMemory(&Column
, sizeof(Column
));
318 Column
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_SUBITEM
;
319 Column
.iSubItem
= Index
;
323 return (InsertColumn(Index
, &Column
) == -1) ? FALSE
: TRUE
;
328 HRESULT WINAPI
CDownloadDialog_Constructor(HWND Dlg
, BOOL
*pbCancelled
, REFIID riid
, LPVOID
*ppv
)
330 return ShellObjectCreatorInit
<CDownloadDialog
>(Dlg
, pbCancelled
, riid
, ppv
);
333 #ifdef USE_CERT_PINNING
334 static BOOL
CertIsValid(HINTERNET hFile
, LPWSTR lpszHostName
)
336 DWORD certInfoLength
;
337 INTERNET_CERTIFICATE_INFOA certInfo
;
340 /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
341 certInfoLength
= sizeof(certInfo
);
342 if (!InternetQueryOptionA(hFile
,
343 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
350 if (certInfo
.lpszSubjectInfo
)
352 if (strcmp(certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) == 0)
354 LocalFree(certInfo
.lpszSubjectInfo
);
356 if (certInfo
.lpszIssuerInfo
)
358 if (strcmp(certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) == 0)
360 LocalFree(certInfo
.lpszIssuerInfo
);
362 if (certInfo
.lpszProtocolName
)
363 LocalFree(certInfo
.lpszProtocolName
);
364 if (certInfo
.lpszSignatureAlgName
)
365 LocalFree(certInfo
.lpszSignatureAlgName
);
366 if (certInfo
.lpszEncryptionAlgName
)
367 LocalFree(certInfo
.lpszEncryptionAlgName
);
369 return ValidFlags
== 3;
373 inline VOID
MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
375 ATL::CString szMsgText
;
376 if (szMsgText
.LoadStringW(StringID
))
378 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
383 ATL::CSimpleArray
<DownloadInfo
> CDownloadManager::AppsToInstallList
;
384 CDowloadingAppsListView
CDownloadManager::DownloadsListView
;
386 VOID
CDownloadManager::Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
)
388 AppsToInstallList
.RemoveAll();
389 AppsToInstallList
.Add(DLInfo
);
390 LaunchDownloadDialog(bIsModal
);
393 INT_PTR CALLBACK
CDownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
395 static WCHAR szCaption
[MAX_PATH
];
401 HICON hIconSm
, hIconBg
;
402 ATL::CStringW szTempCaption
;
404 hIconBg
= (HICON
) GetClassLongW(hMainWnd
, GCLP_HICON
);
405 hIconSm
= (HICON
) GetClassLongW(hMainWnd
, GCLP_HICONSM
);
407 if (hIconBg
&& hIconSm
)
409 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
410 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
413 SetWindowLongW(Dlg
, GWLP_USERDATA
, 0);
414 HWND Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
417 // initialize the default values for our nifty progress bar
418 // and subclass it so that it learns to print a status text
419 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
420 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
422 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
426 HWND hListView
= DownloadsListView
.Create(Dlg
);
431 DownloadsListView
.LoadList(AppsToInstallList
);
433 // Get a dlg string for later use
434 GetWindowTextW(Dlg
, szCaption
, _countof(szCaption
));
436 // Hide a placeholder from displaying
437 szTempCaption
= szCaption
;
438 szTempCaption
.Replace(L
"%ls", L
"");
439 SetWindowText(Dlg
, szTempCaption
.GetString());
441 ShowWindow(Dlg
, SW_SHOW
);
443 // Start download process
444 DownloadParam
*param
= new DownloadParam(Dlg
, AppsToInstallList
, szCaption
);
446 HANDLE Thread
= CreateThread(NULL
, 0, ThreadFunc
, (LPVOID
) param
, 0, &ThreadId
);
454 AppsToInstallList
.RemoveAll();
459 if (wParam
== IDCANCEL
)
461 SetWindowLongW(Dlg
, GWLP_USERDATA
, 1);
462 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
468 //DestroyWindow(Dlg);
476 LRESULT CALLBACK
CDownloadManager::DownloadProgressProc(HWND hWnd
,
480 UINT_PTR uIdSubclass
,
483 static ATL::CStringW szProgressText
;
491 szProgressText
= (PCWSTR
) lParam
;
500 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
504 UINT win_width
, win_height
;
506 GetClientRect(hWnd
, &myRect
);
508 /* grab the progress bar rect size */
509 win_width
= myRect
.right
- myRect
.left
;
510 win_height
= myRect
.bottom
- myRect
.top
;
512 /* create an off-screen DC for double-buffering */
513 hdcMem
= CreateCompatibleDC(hDC
);
514 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
516 hOld
= SelectObject(hdcMem
, hbmMem
);
518 /* call the original draw code and redirect it to our memory buffer */
519 DefSubclassProc(hWnd
, uMsg
, (WPARAM
) hdcMem
, lParam
);
521 /* draw our nifty progress text over it */
522 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
523 DrawShadowText(hdcMem
, szProgressText
.GetString(), szProgressText
.GetLength(),
525 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
526 GetSysColor(COLOR_CAPTIONTEXT
),
527 GetSysColor(COLOR_3DSHADOW
),
530 /* transfer the off-screen DC to the screen */
531 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
533 /* free the off-screen DC */
534 SelectObject(hdcMem
, hOld
);
535 DeleteObject(hbmMem
);
542 /* Raymond Chen says that we should safely unsubclass all the things!
543 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
547 szProgressText
.Empty();
548 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
552 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
556 VOID
CDownloadManager::SetProgressMarquee(HWND Item
, BOOL Enable
)
561 DWORD style
= GetWindowLongPtr(Item
, GWL_STYLE
);
565 if (!SetWindowLongPtr(Item
, GWL_STYLE
, (Enable
? style
| PBS_MARQUEE
: style
& ~PBS_MARQUEE
)))
568 SendMessageW(Item
, PBM_SETMARQUEE
, Enable
, 0);
571 DWORD WINAPI
CDownloadManager::ThreadFunc(LPVOID param
)
573 CComPtr
<IBindStatusCallback
> dl
;
577 HWND hDlg
= static_cast<DownloadParam
*>(param
)->Dialog
;
581 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
582 ULONG dwCurrentBytesRead
= 0;
583 ULONG dwStatusLen
= sizeof(dwStatus
);
585 BOOL bCancelled
= FALSE
;
586 BOOL bTempfile
= FALSE
;
589 HINTERNET hOpen
= NULL
;
590 HINTERNET hFile
= NULL
;
591 HANDLE hOut
= INVALID_HANDLE_VALUE
;
593 unsigned char lpBuffer
[4096];
594 LPCWSTR lpszAgent
= L
"RApps/1.0";
595 URL_COMPONENTS urlComponents
;
596 size_t urlLength
, filenameLength
;
598 const ATL::CSimpleArray
<DownloadInfo
> &InfoArray
= static_cast<DownloadParam
*>(param
)->AppInfo
;
599 LPCWSTR szCaption
= static_cast<DownloadParam
*>(param
)->szCaption
;
600 ATL::CStringW szNewCaption
;
602 if (InfoArray
.GetSize() <= 0)
604 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
608 for (iAppId
= 0; iAppId
< InfoArray
.GetSize(); ++iAppId
)
610 // Reset progress bar
611 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_PROGRESS
);
614 SetProgressMarquee(Item
, FALSE
);
615 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) L
"");
616 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
619 // Change caption to show the currently downloaded app
622 szNewCaption
.Format(szCaption
, InfoArray
[iAppId
].szName
.GetString());
626 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP
);
629 SetWindowTextW(hDlg
, szNewCaption
.GetString());
631 // build the path for the download
632 p
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'/');
633 q
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'?');
635 // do we have a final slash separator?
639 // prepare the tentative length of the filename, maybe we've to remove part of it later on
640 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
642 /* do we have query arguments in the target URL after the filename? account for them
643 (e.g. https://example.org/myfile.exe?no_adware_plz) */
644 if (q
&& q
> p
&& (q
- p
) > 0)
645 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
647 // is this URL an update package for RAPPS? if so store it in a different place
648 if (InfoArray
[iAppId
].szUrl
== APPLICATION_DATABASE_URL
)
651 if (!GetStorageDirectory(Path
))
656 Path
= SettingsInfo
.szDownloadDir
;
659 // is the path valid? can we access it?
660 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
662 if (!CreateDirectoryW(Path
.GetString(), NULL
))
666 // append a \ to the provided file system path, and the filename portion from the URL after that
668 Path
+= (LPWSTR
) (p
+ 1);
670 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
672 // only open it in case of total correctness
673 if (VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
677 // Add the download URL
678 SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, InfoArray
[iAppId
].szUrl
.GetString());
680 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_DOWNLOADING
);
684 CDownloadDialog_Constructor(hDlg
, &bCancelled
, IID_PPV_ARG(IBindStatusCallback
, &dl
));
689 /* FIXME: this should just be using the system-wide proxy settings */
690 switch (SettingsInfo
.Proxy
)
693 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
695 case 1: // direct (no proxy)
696 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
699 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
701 default: // preconfig
702 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
709 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
713 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
717 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
720 if (dwStatus
!= HTTP_STATUS_OK
)
722 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
726 dwStatusLen
= sizeof(dwStatus
);
728 memset(&urlComponents
, 0, sizeof(urlComponents
));
729 urlComponents
.dwStructSize
= sizeof(urlComponents
);
731 urlLength
= InfoArray
[iAppId
].szUrl
.GetLength();
732 urlComponents
.dwSchemeLength
= urlLength
+ 1;
733 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
734 urlComponents
.dwHostNameLength
= urlLength
+ 1;
735 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
737 if (!InternetCrackUrlW(InfoArray
[iAppId
].szUrl
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
742 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
743 HttpQueryInfoW(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
745 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
746 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
750 // content-length is not known, enable marquee mode
751 SetProgressMarquee(Item
, TRUE
);
754 #ifdef USE_CERT_PINNING
755 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
756 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
757 (wcscmp(InfoArray
[iAppId
].szUrl
, APPLICATION_DATABASE_URL
) == 0) &&
758 (!CertIsValid(hFile
, urlComponents
.lpszHostName
)))
760 MessageBox_LoadString(hMainWnd
, IDS_CERT_DOES_NOT_MATCH
);
765 free(urlComponents
.lpszScheme
);
766 free(urlComponents
.lpszHostName
);
768 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
770 if (hOut
== INVALID_HANDLE_VALUE
)
773 dwCurrentBytesRead
= 0;
776 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
778 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
782 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
784 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
788 dwCurrentBytesRead
+= dwBytesRead
;
789 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
790 } while (dwBytesRead
&& !bCancelled
);
793 hOut
= INVALID_HANDLE_VALUE
;
800 // set progress bar to 100%
801 SetProgressMarquee(Item
, FALSE
);
803 dwContentLen
= dwCurrentBytesRead
;
804 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
807 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
808 verify its integrity by using the native advapi32.A_SHA1 functions */
809 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] != 0)
811 ATL::CStringW szMsgText
;
813 // change a few strings in the download dialog to reflect the verification process
814 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_TITLE
))
817 SetWindowTextW(hDlg
, szMsgText
.GetString());
818 SendMessageW(GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
820 // this may take a while, depending on the file size
821 if (!VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
.GetString()))
823 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_FAIL
))
826 MessageBoxW(hDlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
832 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_WAITING_INSTALL
);
837 SHELLEXECUTEINFOW shExInfo
= {0};
838 shExInfo
.cbSize
= sizeof(shExInfo
);
839 shExInfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
840 shExInfo
.lpVerb
= L
"open";
841 shExInfo
.lpFile
= Path
.GetString();
842 shExInfo
.lpParameters
= L
"";
843 shExInfo
.nShow
= SW_SHOW
;
845 if (ShellExecuteExW(&shExInfo
))
847 //reflect installation progress in the titlebar
848 //TODO: make a separate string with a placeholder to include app name?
849 ATL::CStringW szMsgText
= LoadStatusString(DLSTATUS_INSTALLING
);
850 SetWindowTextW(hDlg
, szMsgText
.GetString());
852 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_INSTALLING
);
854 //TODO: issue an install operation separately so that the apps could be downloaded in the background
855 WaitForSingleObject(shExInfo
.hProcess
, INFINITE
);
856 CloseHandle(shExInfo
.hProcess
);
860 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_INSTALL
);
865 if (hOut
!= INVALID_HANDLE_VALUE
)
868 InternetCloseHandle(hFile
);
869 InternetCloseHandle(hOpen
);
873 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
874 DeleteFileW(Path
.GetString());
877 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_FINISHED
);
880 delete static_cast<DownloadParam
*>(param
);
881 SendMessageW(hDlg
, WM_CLOSE
, 0, 0);
885 BOOL
CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray
<CAvailableApplicationInfo
>& AppsList
, BOOL bIsModal
)
887 if (AppsList
.GetSize() == 0)
890 // Initialize shared variables
891 for (INT i
= 0; i
< AppsList
.GetSize(); ++i
)
893 AppsToInstallList
.Add(AppsList
[i
]); // implicit conversion to DownloadInfo
896 // Create a dialog and issue a download process
897 LaunchDownloadDialog(bIsModal
);
902 BOOL
CDownloadManager::DownloadApplication(CAvailableApplicationInfo
* pAppInfo
, BOOL bIsModal
)
907 Download(*pAppInfo
, bIsModal
);
911 VOID
CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl
)
913 static DownloadInfo DatabaseDLInfo
;
914 DatabaseDLInfo
.szUrl
= lpUrl
;
915 DatabaseDLInfo
.szName
.LoadStringW(IDS_DL_DIALOG_DB_DISP
);
916 Download(DatabaseDLInfo
, TRUE
);
919 //TODO: Reuse the dialog
920 VOID
CDownloadManager::LaunchDownloadDialog(BOOL bIsModal
)
925 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
932 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),