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>
50 #ifdef USE_CERT_PINNING
51 #define CERT_ISSUER_INFO "US\r\nLet's Encrypt\r\nLet's Encrypt Authority X3"
52 #define CERT_SUBJECT_INFO "rapps.reactos.org"
57 DLSTATUS_WAITING
= IDS_STATUS_WAITING
,
58 DLSTATUS_DOWNLOADING
= IDS_STATUS_DOWNLOADING
,
59 DLSTATUS_WAITING_INSTALL
= IDS_STATUS_DOWNLOADED
,
60 DLSTATUS_INSTALLING
= IDS_STATUS_INSTALLING
,
61 DLSTATUS_INSTALLED
= IDS_STATUS_INSTALLED
,
62 DLSTATUS_FINISHED
= IDS_STATUS_FINISHED
65 ATL::CStringW
LoadStatusString(DownloadStatus StatusParam
)
67 ATL::CStringW szString
;
68 szString
.LoadStringW(StatusParam
);
75 DownloadInfo(const CAvailableApplicationInfo
& AppInfo
)
76 :szUrl(AppInfo
.m_szUrlDownload
), szName(AppInfo
.m_szName
), szSHA1(AppInfo
.m_szSHA1
)
87 DownloadParam() : Dialog(NULL
), AppInfo(), szCaption(NULL
) {}
88 DownloadParam(HWND dlg
, const ATL::CSimpleArray
<DownloadInfo
> &info
, LPCWSTR caption
)
89 : Dialog(dlg
), AppInfo(info
), szCaption(caption
)
94 ATL::CSimpleArray
<DownloadInfo
> AppInfo
;
99 class CDownloaderProgress
100 : public CWindowImpl
<CDownloaderProgress
, CWindow
, CControlWinTraits
>
102 ATL::CStringW m_szProgressText
;
105 CDownloaderProgress()
109 VOID
SetMarquee(BOOL Enable
)
112 ModifyStyle(0, PBS_MARQUEE
, 0);
114 ModifyStyle(PBS_MARQUEE
, 0, 0);
116 SendMessage(PBM_SETMARQUEE
, Enable
, 0);
119 VOID
SetProgress(ULONG ulProgress
, ULONG ulProgressMax
)
121 WCHAR szProgress
[100];
123 /* format the bits and bytes into pretty and accessible units... */
124 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
126 /* use our subclassed progress bar text subroutine */
127 ATL::CStringW ProgressText
;
131 /* total size is known */
132 WCHAR szProgressMax
[100];
133 UINT uiPercentage
= ((ULONGLONG
) ulProgress
* 100) / ulProgressMax
;
135 /* send the current progress to the progress bar */
136 SendMessage(PBM_SETPOS
, uiPercentage
, 0);
138 /* format total download size */
139 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
141 /* generate the text on progress bar */
142 ProgressText
.Format(L
"%u%% \x2014 %ls / %ls",
149 /* send the current progress to the progress bar */
150 SendMessage(PBM_SETPOS
, 0, 0);
152 /* total size is not known, display only current size */
153 ProgressText
.Format(L
"%ls...", szProgress
);
156 /* and finally display it */
157 SendMessage(WM_SETTEXT
, 0, (LPARAM
) ProgressText
.GetString());
160 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
163 HDC hDC
= BeginPaint(&ps
), hdcMem
;
167 UINT win_width
, win_height
;
169 GetClientRect(&myRect
);
171 /* grab the progress bar rect size */
172 win_width
= myRect
.right
- myRect
.left
;
173 win_height
= myRect
.bottom
- myRect
.top
;
175 /* create an off-screen DC for double-buffering */
176 hdcMem
= CreateCompatibleDC(hDC
);
177 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
179 hOld
= SelectObject(hdcMem
, hbmMem
);
181 /* call the original draw code and redirect it to our memory buffer */
182 DefWindowProc(uMsg
, (WPARAM
) hdcMem
, lParam
);
184 /* draw our nifty progress text over it */
185 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
186 DrawShadowText(hdcMem
, m_szProgressText
.GetString(), m_szProgressText
.GetLength(),
188 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
189 GetSysColor(COLOR_CAPTIONTEXT
),
190 GetSysColor(COLOR_3DSHADOW
),
193 /* transfer the off-screen DC to the screen */
194 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
196 /* free the off-screen DC */
197 SelectObject(hdcMem
, hOld
);
198 DeleteObject(hbmMem
);
205 LRESULT
OnSetText(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
&bHandled
)
209 m_szProgressText
= (PCWSTR
) lParam
;
214 BEGIN_MSG_MAP(CDownloaderProgress
)
215 MESSAGE_HANDLER(WM_ERASEBKGND
, OnPaint
)
216 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
217 MESSAGE_HANDLER(WM_SETTEXT
, OnSetText
)
221 class CDowloadingAppsListView
225 HWND
Create(HWND hwndParent
)
227 RECT r
= {10, 150, 320, 350};
228 const DWORD style
= WS_CHILD
| WS_VISIBLE
| LVS_REPORT
| LVS_SINGLESEL
229 | LVS_SHOWSELALWAYS
| LVS_NOSORTHEADER
| LVS_NOCOLUMNHEADER
;
231 HWND hwnd
= CListView::Create(hwndParent
, r
, NULL
, style
, WS_EX_CLIENTEDGE
);
233 AddColumn(0, 150, LVCFMT_LEFT
);
234 AddColumn(1, 120, LVCFMT_LEFT
);
239 VOID
LoadList(ATL::CSimpleArray
<DownloadInfo
> arrInfo
)
241 for (INT i
= 0; i
< arrInfo
.GetSize(); ++i
)
243 AddRow(i
, arrInfo
[i
].szName
.GetString(), DLSTATUS_WAITING
);
247 VOID
SetDownloadStatus(INT ItemIndex
, DownloadStatus Status
)
249 ATL::CStringW szBuffer
= LoadStatusString(Status
);
250 SetItemText(ItemIndex
, 1, szBuffer
.GetString());
253 BOOL
AddItem(INT ItemIndex
, LPWSTR lpText
)
257 ZeroMemory(&Item
, sizeof(Item
));
259 Item
.mask
= LVIF_TEXT
| LVIF_STATE
;
260 Item
.pszText
= lpText
;
261 Item
.iItem
= ItemIndex
;
263 return InsertItem(&Item
);
266 VOID
AddRow(INT RowIndex
, LPCWSTR szAppName
, const DownloadStatus Status
)
268 ATL::CStringW szStatus
= LoadStatusString(Status
);
270 const_cast<LPWSTR
>(szAppName
));
271 SetDownloadStatus(RowIndex
, Status
);
274 BOOL
AddColumn(INT Index
, INT Width
, INT Format
)
277 ZeroMemory(&Column
, sizeof(Column
));
279 Column
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_SUBITEM
;
280 Column
.iSubItem
= Index
;
284 return (InsertColumn(Index
, &Column
) == -1) ? FALSE
: TRUE
;
288 #ifdef USE_CERT_PINNING
289 typedef CHeapPtr
<char, CLocalAllocator
> CLocalPtr
;
291 static BOOL
CertGetSubjectAndIssuer(HINTERNET hFile
, CLocalPtr
& subjectInfo
, CLocalPtr
& issuerInfo
)
293 DWORD certInfoLength
;
294 INTERNET_CERTIFICATE_INFOA certInfo
;
297 size
= sizeof(flags
);
298 if (!InternetQueryOptionA(hFile
, INTERNET_OPTION_SECURITY_FLAGS
, &flags
, &size
))
303 if (!flags
& SECURITY_FLAG_SECURE
)
308 /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
309 certInfoLength
= sizeof(certInfo
);
310 if (!InternetQueryOptionA(hFile
,
311 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
318 subjectInfo
.Attach(certInfo
.lpszSubjectInfo
);
319 issuerInfo
.Attach(certInfo
.lpszIssuerInfo
);
321 if (certInfo
.lpszProtocolName
)
322 LocalFree(certInfo
.lpszProtocolName
);
323 if (certInfo
.lpszSignatureAlgName
)
324 LocalFree(certInfo
.lpszSignatureAlgName
);
325 if (certInfo
.lpszEncryptionAlgName
)
326 LocalFree(certInfo
.lpszEncryptionAlgName
);
328 return certInfo
.lpszSubjectInfo
&& certInfo
.lpszIssuerInfo
;
332 inline VOID
MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
334 ATL::CStringW szMsgText
;
335 if (szMsgText
.LoadStringW(StringID
))
337 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
341 // Download dialog (loaddlg.cpp)
342 class CDownloadManager
344 static ATL::CSimpleArray
<DownloadInfo
> AppsToInstallList
;
345 static CDowloadingAppsListView DownloadsListView
;
346 static CDownloaderProgress ProgressBar
;
347 static BOOL bCancelled
;
348 static VOID
UpdateProgress(HWND hDlg
, ULONG ulProgress
, ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
);
350 static VOID
Add(DownloadInfo info
);
351 static VOID
Download(const DownloadInfo
& DLInfo
, BOOL bIsModal
= FALSE
);
352 static INT_PTR CALLBACK
DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
353 static DWORD WINAPI
ThreadFunc(LPVOID Context
);
354 static VOID
LaunchDownloadDialog(BOOL
);
359 ATL::CSimpleArray
<DownloadInfo
> CDownloadManager::AppsToInstallList
;
360 CDowloadingAppsListView
CDownloadManager::DownloadsListView
;
361 CDownloaderProgress
CDownloadManager::ProgressBar
;
362 BOOL
CDownloadManager::bCancelled
;
364 VOID
CDownloadManager::Add(DownloadInfo info
)
366 AppsToInstallList
.Add(info
);
369 VOID
CDownloadManager::Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
)
371 AppsToInstallList
.RemoveAll();
372 AppsToInstallList
.Add(DLInfo
);
373 LaunchDownloadDialog(bIsModal
);
376 INT_PTR CALLBACK
CDownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
378 static WCHAR szCaption
[MAX_PATH
];
384 HICON hIconSm
, hIconBg
;
385 ATL::CStringW szTempCaption
;
389 hIconBg
= (HICON
) GetClassLongPtrW(hMainWnd
, GCLP_HICON
);
390 hIconSm
= (HICON
) GetClassLongPtrW(hMainWnd
, GCLP_HICONSM
);
392 if (hIconBg
&& hIconSm
)
394 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
395 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
398 SetWindowLongW(Dlg
, GWLP_USERDATA
, 0);
399 HWND Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
402 // initialize the default values for our nifty progress bar
403 // and subclass it so that it learns to print a status text
404 ProgressBar
.SubclassWindow(Item
);
405 ProgressBar
.SendMessage(PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
406 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
410 HWND hListView
= DownloadsListView
.Create(Dlg
);
415 DownloadsListView
.LoadList(AppsToInstallList
);
417 // Get a dlg string for later use
418 GetWindowTextW(Dlg
, szCaption
, _countof(szCaption
));
420 // Hide a placeholder from displaying
421 szTempCaption
= szCaption
;
422 szTempCaption
.Replace(L
"%ls", L
"");
423 SetWindowText(Dlg
, szTempCaption
.GetString());
425 ShowWindow(Dlg
, SW_SHOW
);
427 // Start download process
428 DownloadParam
*param
= new DownloadParam(Dlg
, AppsToInstallList
, szCaption
);
430 HANDLE Thread
= CreateThread(NULL
, 0, ThreadFunc
, (LPVOID
) param
, 0, &ThreadId
);
438 AppsToInstallList
.RemoveAll();
443 if (wParam
== IDCANCEL
)
446 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
452 //DestroyWindow(Dlg);
460 BOOL UrlHasBeenCopied
;
462 VOID
CDownloadManager::UpdateProgress(
467 LPCWSTR szStatusText
)
471 ProgressBar
.SetProgress(ulProgress
, ulProgressMax
);
473 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
);
474 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && UrlHasBeenCopied
== FALSE
)
476 SIZE_T len
= wcslen(szStatusText
) + 1;
480 /* beautify our url for display purposes */
481 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &dummyLen
, ICU_DECODE
| ICU_NO_ENCODE
))
483 /* just use the original */
492 /* paste it into our dialog and don't do it again in this instance */
493 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
494 UrlHasBeenCopied
= TRUE
;
498 DWORD WINAPI
CDownloadManager::ThreadFunc(LPVOID param
)
503 HWND hDlg
= static_cast<DownloadParam
*>(param
)->Dialog
;
507 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
508 ULONG dwCurrentBytesRead
= 0;
509 ULONG dwStatusLen
= sizeof(dwStatus
);
511 BOOL bTempfile
= FALSE
;
514 HINTERNET hOpen
= NULL
;
515 HINTERNET hFile
= NULL
;
516 HANDLE hOut
= INVALID_HANDLE_VALUE
;
518 unsigned char lpBuffer
[4096];
519 LPCWSTR lpszAgent
= L
"RApps/1.0";
520 URL_COMPONENTS urlComponents
;
521 size_t urlLength
, filenameLength
;
523 const ATL::CSimpleArray
<DownloadInfo
> &InfoArray
= static_cast<DownloadParam
*>(param
)->AppInfo
;
524 LPCWSTR szCaption
= static_cast<DownloadParam
*>(param
)->szCaption
;
525 ATL::CStringW szNewCaption
;
527 const DWORD dwUrlConnectFlags
= INTERNET_FLAG_DONT_CACHE
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
;
529 if (InfoArray
.GetSize() <= 0)
531 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
535 for (iAppId
= 0; iAppId
< InfoArray
.GetSize(); ++iAppId
)
537 // Reset progress bar
538 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_PROGRESS
);
541 ProgressBar
.SetMarquee(FALSE
);
542 ProgressBar
.SendMessage(WM_SETTEXT
, 0, (LPARAM
) L
"");
543 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
546 // is this URL an update package for RAPPS? if so store it in a different place
547 if (InfoArray
[iAppId
].szUrl
== APPLICATION_DATABASE_URL
)
550 if (!GetStorageDirectory(Path
))
556 Path
= SettingsInfo
.szDownloadDir
;
559 // Change caption to show the currently downloaded app
562 szNewCaption
.Format(szCaption
, InfoArray
[iAppId
].szName
.GetString());
566 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP
);
569 SetWindowTextW(hDlg
, szNewCaption
.GetString());
571 // build the path for the download
572 p
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'/');
573 q
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'?');
575 // do we have a final slash separator?
579 // prepare the tentative length of the filename, maybe we've to remove part of it later on
580 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
582 /* do we have query arguments in the target URL after the filename? account for them
583 (e.g. https://example.org/myfile.exe?no_adware_plz) */
584 if (q
&& q
> p
&& (q
- p
) > 0)
585 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
587 // is the path valid? can we access it?
588 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
590 if (!CreateDirectoryW(Path
.GetString(), NULL
))
594 // append a \ to the provided file system path, and the filename portion from the URL after that
596 Path
+= (LPWSTR
) (p
+ 1);
598 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
600 // only open it in case of total correctness
601 if (VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
605 // Add the download URL
606 SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, InfoArray
[iAppId
].szUrl
.GetString());
608 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_DOWNLOADING
);
611 UrlHasBeenCopied
= FALSE
;
614 /* FIXME: this should just be using the system-wide proxy settings */
615 switch (SettingsInfo
.Proxy
)
619 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
621 case 1: // direct (no proxy)
622 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
625 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
632 dwStatusLen
= sizeof(dwStatus
);
634 memset(&urlComponents
, 0, sizeof(urlComponents
));
635 urlComponents
.dwStructSize
= sizeof(urlComponents
);
637 urlLength
= InfoArray
[iAppId
].szUrl
.GetLength();
638 urlComponents
.dwSchemeLength
= urlLength
+ 1;
639 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
640 urlComponents
.dwHostNameLength
= urlLength
+ 1;
641 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
643 if (!InternetCrackUrlW(InfoArray
[iAppId
].szUrl
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
648 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
650 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0,
655 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
660 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
663 if (dwStatus
!= HTTP_STATUS_OK
)
665 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
669 // query content length
670 HttpQueryInfoW(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatusLen
, NULL
);
673 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
675 // force passive mode on FTP
676 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0,
677 dwUrlConnectFlags
| INTERNET_FLAG_PASSIVE
,
681 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
685 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
690 // content-length is not known, enable marquee mode
691 ProgressBar
.SetMarquee(TRUE
);
694 free(urlComponents
.lpszScheme
);
695 free(urlComponents
.lpszHostName
);
697 #ifdef USE_CERT_PINNING
698 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
699 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
700 (wcscmp(InfoArray
[iAppId
].szUrl
, APPLICATION_DATABASE_URL
) == 0))
702 CLocalPtr subjectName
, issuerName
;
704 bool bAskQuestion
= false;
705 if (!CertGetSubjectAndIssuer(hFile
, subjectName
, issuerName
))
707 szMsgText
.LoadStringW(IDS_UNABLE_TO_QUERY_CERT
);
712 if (strcmp(subjectName
, CERT_SUBJECT_INFO
) ||
713 strcmp(issuerName
, CERT_ISSUER_INFO
))
715 szMsgText
.Format(IDS_MISMATCH_CERT_INFO
, (char*)subjectName
, (const char*)issuerName
);
722 if (MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_YESNO
| MB_ICONERROR
) != IDYES
)
730 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
732 if (hOut
== INVALID_HANDLE_VALUE
)
735 dwCurrentBytesRead
= 0;
738 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
740 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
744 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
746 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
750 dwCurrentBytesRead
+= dwBytesRead
;
751 UpdateProgress(hDlg
, dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
752 } while (dwBytesRead
&& !bCancelled
);
755 hOut
= INVALID_HANDLE_VALUE
;
762 // set progress bar to 100%
763 ProgressBar
.SetMarquee(FALSE
);
765 dwContentLen
= dwCurrentBytesRead
;
766 UpdateProgress(hDlg
, dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
769 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
770 verify its integrity by using the native advapi32.A_SHA1 functions */
771 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] != 0)
773 ATL::CStringW szMsgText
;
775 // change a few strings in the download dialog to reflect the verification process
776 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_TITLE
))
779 SetWindowTextW(hDlg
, szMsgText
.GetString());
780 SendMessageW(GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
782 // this may take a while, depending on the file size
783 if (!VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
.GetString()))
785 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_FAIL
))
788 MessageBoxW(hDlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
794 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_WAITING_INSTALL
);
799 SHELLEXECUTEINFOW shExInfo
= {0};
800 shExInfo
.cbSize
= sizeof(shExInfo
);
801 shExInfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
802 shExInfo
.lpVerb
= L
"open";
803 shExInfo
.lpFile
= Path
.GetString();
804 shExInfo
.lpParameters
= L
"";
805 shExInfo
.nShow
= SW_SHOW
;
807 if (ShellExecuteExW(&shExInfo
))
809 //reflect installation progress in the titlebar
810 //TODO: make a separate string with a placeholder to include app name?
811 ATL::CStringW szMsgText
= LoadStatusString(DLSTATUS_INSTALLING
);
812 SetWindowTextW(hDlg
, szMsgText
.GetString());
814 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_INSTALLING
);
816 //TODO: issue an install operation separately so that the apps could be downloaded in the background
817 WaitForSingleObject(shExInfo
.hProcess
, INFINITE
);
818 CloseHandle(shExInfo
.hProcess
);
822 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_INSTALL
);
827 if (hOut
!= INVALID_HANDLE_VALUE
)
830 InternetCloseHandle(hFile
);
831 InternetCloseHandle(hOpen
);
835 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
836 DeleteFileW(Path
.GetString());
839 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_FINISHED
);
842 delete static_cast<DownloadParam
*>(param
);
843 SendMessageW(hDlg
, WM_CLOSE
, 0, 0);
847 //TODO: Reuse the dialog
848 VOID
CDownloadManager::LaunchDownloadDialog(BOOL bIsModal
)
853 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
860 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
868 BOOL
DownloadListOfApplications(const ATL::CSimpleArray
<CAvailableApplicationInfo
>& AppsList
, BOOL bIsModal
)
870 if (AppsList
.GetSize() == 0)
873 // Initialize shared variables
874 for (INT i
= 0; i
< AppsList
.GetSize(); ++i
)
876 CDownloadManager::Add(AppsList
[i
]); // implicit conversion to DownloadInfo
879 // Create a dialog and issue a download process
880 CDownloadManager::LaunchDownloadDialog(bIsModal
);
885 BOOL
DownloadApplication(CAvailableApplicationInfo
* pAppInfo
, BOOL bIsModal
)
890 CDownloadManager::Download(*pAppInfo
, bIsModal
);
894 VOID
DownloadApplicationsDB(LPCWSTR lpUrl
)
896 static DownloadInfo DatabaseDLInfo
;
897 DatabaseDLInfo
.szUrl
= lpUrl
;
898 DatabaseDLInfo
.szName
.LoadStringW(IDS_DL_DIALOG_DB_DISP
);
899 CDownloadManager::Download(DatabaseDLInfo
, TRUE
);