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 CDownloadDialog
:
222 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
223 public IBindStatusCallback
227 BOOL m_UrlHasBeenCopied
;
228 CDownloaderProgress
* m_progress
;
233 //DestroyWindow(m_hDialog);
236 HRESULT
Initialize(HWND Dlg
, BOOL
*pbCancelled
, CDownloaderProgress
* pProgress
)
239 m_pbCancelled
= pbCancelled
;
240 m_UrlHasBeenCopied
= FALSE
;
241 m_progress
= pProgress
;
245 virtual HRESULT STDMETHODCALLTYPE
OnStartBinding(
252 virtual HRESULT STDMETHODCALLTYPE
GetPriority(
258 virtual HRESULT STDMETHODCALLTYPE
OnLowResource(
264 virtual HRESULT STDMETHODCALLTYPE
OnProgress(
268 LPCWSTR szStatusText
)
273 m_progress
->SetProgress(ulProgress
, ulProgressMax
);
275 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_STATUS
);
276 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && m_UrlHasBeenCopied
== FALSE
)
278 SIZE_T len
= wcslen(szStatusText
) + 1;
282 /* beautify our url for display purposes */
283 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &dummyLen
, ICU_DECODE
| ICU_NO_ENCODE
))
285 /* just use the original */
294 /* paste it into our dialog and don't do it again in this instance */
295 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
296 m_UrlHasBeenCopied
= TRUE
;
299 SetLastError(ERROR_SUCCESS
);
300 r
= GetWindowLongPtrW(m_hDialog
, GWLP_USERDATA
);
301 if (r
|| GetLastError() != ERROR_SUCCESS
)
303 *m_pbCancelled
= TRUE
;
310 virtual HRESULT STDMETHODCALLTYPE
OnStopBinding(
317 virtual HRESULT STDMETHODCALLTYPE
GetBindInfo(
324 virtual HRESULT STDMETHODCALLTYPE
OnDataAvailable(
327 FORMATETC
*pformatetc
,
333 virtual HRESULT STDMETHODCALLTYPE
OnObjectAvailable(
340 BEGIN_COM_MAP(CDownloadDialog
)
341 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback
, IBindStatusCallback
)
345 class CDowloadingAppsListView
349 HWND
Create(HWND hwndParent
)
351 RECT r
= {10, 150, 320, 350};
352 const DWORD style
= WS_CHILD
| WS_VISIBLE
| LVS_REPORT
| LVS_SINGLESEL
353 | LVS_SHOWSELALWAYS
| LVS_NOSORTHEADER
| LVS_NOCOLUMNHEADER
;
355 HWND hwnd
= CListView::Create(hwndParent
, r
, NULL
, style
, WS_EX_CLIENTEDGE
);
357 AddColumn(0, 150, LVCFMT_LEFT
);
358 AddColumn(1, 120, LVCFMT_LEFT
);
363 VOID
LoadList(ATL::CSimpleArray
<DownloadInfo
> arrInfo
)
365 for (INT i
= 0; i
< arrInfo
.GetSize(); ++i
)
367 AddRow(i
, arrInfo
[i
].szName
.GetString(), DLSTATUS_WAITING
);
371 VOID
SetDownloadStatus(INT ItemIndex
, DownloadStatus Status
)
373 ATL::CStringW szBuffer
= LoadStatusString(Status
);
374 SetItemText(ItemIndex
, 1, szBuffer
.GetString());
377 BOOL
AddItem(INT ItemIndex
, LPWSTR lpText
)
381 ZeroMemory(&Item
, sizeof(Item
));
383 Item
.mask
= LVIF_TEXT
| LVIF_STATE
;
384 Item
.pszText
= lpText
;
385 Item
.iItem
= ItemIndex
;
387 return InsertItem(&Item
);
390 VOID
AddRow(INT RowIndex
, LPCWSTR szAppName
, const DownloadStatus Status
)
392 ATL::CStringW szStatus
= LoadStatusString(Status
);
394 const_cast<LPWSTR
>(szAppName
));
395 SetDownloadStatus(RowIndex
, Status
);
398 BOOL
AddColumn(INT Index
, INT Width
, INT Format
)
401 ZeroMemory(&Column
, sizeof(Column
));
403 Column
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_SUBITEM
;
404 Column
.iSubItem
= Index
;
408 return (InsertColumn(Index
, &Column
) == -1) ? FALSE
: TRUE
;
413 HRESULT WINAPI
CDownloadDialog_Constructor(HWND Dlg
, BOOL
*pbCancelled
, CDownloaderProgress
* pProgress
, REFIID riid
, LPVOID
*ppv
)
415 return ShellObjectCreatorInit
<CDownloadDialog
>(Dlg
, pbCancelled
, pProgress
, riid
, ppv
);
418 #ifdef USE_CERT_PINNING
419 typedef CHeapPtr
<char, CLocalAllocator
> CLocalPtr
;
421 static BOOL
CertGetSubjectAndIssuer(HINTERNET hFile
, CLocalPtr
& subjectInfo
, CLocalPtr
& issuerInfo
)
423 DWORD certInfoLength
;
424 INTERNET_CERTIFICATE_INFOA certInfo
;
427 size
= sizeof(flags
);
428 if (!InternetQueryOptionA(hFile
, INTERNET_OPTION_SECURITY_FLAGS
, &flags
, &size
))
433 if (!flags
& SECURITY_FLAG_SECURE
)
438 /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
439 certInfoLength
= sizeof(certInfo
);
440 if (!InternetQueryOptionA(hFile
,
441 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
448 subjectInfo
.Attach(certInfo
.lpszSubjectInfo
);
449 issuerInfo
.Attach(certInfo
.lpszIssuerInfo
);
451 if (certInfo
.lpszProtocolName
)
452 LocalFree(certInfo
.lpszProtocolName
);
453 if (certInfo
.lpszSignatureAlgName
)
454 LocalFree(certInfo
.lpszSignatureAlgName
);
455 if (certInfo
.lpszEncryptionAlgName
)
456 LocalFree(certInfo
.lpszEncryptionAlgName
);
458 return certInfo
.lpszSubjectInfo
&& certInfo
.lpszIssuerInfo
;
462 inline VOID
MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
464 ATL::CStringW szMsgText
;
465 if (szMsgText
.LoadStringW(StringID
))
467 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
472 // Download dialog (loaddlg.cpp)
473 class CDownloadManager
475 static ATL::CSimpleArray
<DownloadInfo
> AppsToInstallList
;
476 static CDowloadingAppsListView DownloadsListView
;
477 static CDownloaderProgress ProgressBar
;
480 static VOID
Add(DownloadInfo info
);
481 static VOID
Download(const DownloadInfo
& DLInfo
, BOOL bIsModal
= FALSE
);
482 static INT_PTR CALLBACK
DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
483 static DWORD WINAPI
ThreadFunc(LPVOID Context
);
484 static BOOL
DownloadListOfApplications(const ATL::CSimpleArray
<CAvailableApplicationInfo
>& AppsList
, BOOL bIsModal
= FALSE
);
485 static BOOL
DownloadApplication(CAvailableApplicationInfo
* pAppInfo
, BOOL bIsModal
= FALSE
);
486 static VOID
DownloadApplicationsDB(LPCWSTR lpUrl
);
487 static VOID
LaunchDownloadDialog(BOOL
);
492 ATL::CSimpleArray
<DownloadInfo
> CDownloadManager::AppsToInstallList
;
493 CDowloadingAppsListView
CDownloadManager::DownloadsListView
;
494 CDownloaderProgress
CDownloadManager::ProgressBar
;
497 VOID
CDownloadManager::Add(DownloadInfo info
)
499 AppsToInstallList
.Add(info
);
502 VOID
CDownloadManager::Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
)
504 AppsToInstallList
.RemoveAll();
505 AppsToInstallList
.Add(DLInfo
);
506 LaunchDownloadDialog(bIsModal
);
509 INT_PTR CALLBACK
CDownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
511 static WCHAR szCaption
[MAX_PATH
];
517 HICON hIconSm
, hIconBg
;
518 ATL::CStringW szTempCaption
;
520 hIconBg
= (HICON
) GetClassLongPtrW(hMainWnd
, GCLP_HICON
);
521 hIconSm
= (HICON
) GetClassLongPtrW(hMainWnd
, GCLP_HICONSM
);
523 if (hIconBg
&& hIconSm
)
525 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
526 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
529 SetWindowLongW(Dlg
, GWLP_USERDATA
, 0);
530 HWND Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
533 // initialize the default values for our nifty progress bar
534 // and subclass it so that it learns to print a status text
535 ProgressBar
.SubclassWindow(Item
);
536 ProgressBar
.SendMessage(PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
537 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
541 HWND hListView
= DownloadsListView
.Create(Dlg
);
546 DownloadsListView
.LoadList(AppsToInstallList
);
548 // Get a dlg string for later use
549 GetWindowTextW(Dlg
, szCaption
, _countof(szCaption
));
551 // Hide a placeholder from displaying
552 szTempCaption
= szCaption
;
553 szTempCaption
.Replace(L
"%ls", L
"");
554 SetWindowText(Dlg
, szTempCaption
.GetString());
556 ShowWindow(Dlg
, SW_SHOW
);
558 // Start download process
559 DownloadParam
*param
= new DownloadParam(Dlg
, AppsToInstallList
, szCaption
);
561 HANDLE Thread
= CreateThread(NULL
, 0, ThreadFunc
, (LPVOID
) param
, 0, &ThreadId
);
569 AppsToInstallList
.RemoveAll();
574 if (wParam
== IDCANCEL
)
576 SetWindowLongW(Dlg
, GWLP_USERDATA
, 1);
577 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
583 //DestroyWindow(Dlg);
591 DWORD WINAPI
CDownloadManager::ThreadFunc(LPVOID param
)
593 CComPtr
<IBindStatusCallback
> dl
;
597 HWND hDlg
= static_cast<DownloadParam
*>(param
)->Dialog
;
601 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
602 ULONG dwCurrentBytesRead
= 0;
603 ULONG dwStatusLen
= sizeof(dwStatus
);
605 BOOL bCancelled
= FALSE
;
606 BOOL bTempfile
= FALSE
;
609 HINTERNET hOpen
= NULL
;
610 HINTERNET hFile
= NULL
;
611 HANDLE hOut
= INVALID_HANDLE_VALUE
;
613 unsigned char lpBuffer
[4096];
614 LPCWSTR lpszAgent
= L
"RApps/1.0";
615 URL_COMPONENTS urlComponents
;
616 size_t urlLength
, filenameLength
;
618 const ATL::CSimpleArray
<DownloadInfo
> &InfoArray
= static_cast<DownloadParam
*>(param
)->AppInfo
;
619 LPCWSTR szCaption
= static_cast<DownloadParam
*>(param
)->szCaption
;
620 ATL::CStringW szNewCaption
;
622 const DWORD dwUrlConnectFlags
= INTERNET_FLAG_DONT_CACHE
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
;
624 if (InfoArray
.GetSize() <= 0)
626 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
630 for (iAppId
= 0; iAppId
< InfoArray
.GetSize(); ++iAppId
)
632 // Reset progress bar
633 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_PROGRESS
);
636 ProgressBar
.SetMarquee(FALSE
);
637 ProgressBar
.SendMessage(WM_SETTEXT
, 0, (LPARAM
) L
"");
638 ProgressBar
.SendMessage(PBM_SETPOS
, 0, 0);
641 // is this URL an update package for RAPPS? if so store it in a different place
642 if (InfoArray
[iAppId
].szUrl
== APPLICATION_DATABASE_URL
)
645 if (!GetStorageDirectory(Path
))
651 Path
= SettingsInfo
.szDownloadDir
;
654 // Change caption to show the currently downloaded app
657 szNewCaption
.Format(szCaption
, InfoArray
[iAppId
].szName
.GetString());
661 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP
);
664 SetWindowTextW(hDlg
, szNewCaption
.GetString());
666 // build the path for the download
667 p
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'/');
668 q
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'?');
670 // do we have a final slash separator?
674 // prepare the tentative length of the filename, maybe we've to remove part of it later on
675 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
677 /* do we have query arguments in the target URL after the filename? account for them
678 (e.g. https://example.org/myfile.exe?no_adware_plz) */
679 if (q
&& q
> p
&& (q
- p
) > 0)
680 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
682 // is the path valid? can we access it?
683 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
685 if (!CreateDirectoryW(Path
.GetString(), NULL
))
689 // append a \ to the provided file system path, and the filename portion from the URL after that
691 Path
+= (LPWSTR
) (p
+ 1);
693 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
695 // only open it in case of total correctness
696 if (VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
700 // Add the download URL
701 SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, InfoArray
[iAppId
].szUrl
.GetString());
703 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_DOWNLOADING
);
707 CDownloadDialog_Constructor(hDlg
, &bCancelled
, &ProgressBar
, IID_PPV_ARG(IBindStatusCallback
, &dl
));
712 /* FIXME: this should just be using the system-wide proxy settings */
713 switch (SettingsInfo
.Proxy
)
717 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
719 case 1: // direct (no proxy)
720 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
723 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
730 dwStatusLen
= sizeof(dwStatus
);
732 memset(&urlComponents
, 0, sizeof(urlComponents
));
733 urlComponents
.dwStructSize
= sizeof(urlComponents
);
735 urlLength
= InfoArray
[iAppId
].szUrl
.GetLength();
736 urlComponents
.dwSchemeLength
= urlLength
+ 1;
737 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
738 urlComponents
.dwHostNameLength
= urlLength
+ 1;
739 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
741 if (!InternetCrackUrlW(InfoArray
[iAppId
].szUrl
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
746 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
748 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0,
753 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
758 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
761 if (dwStatus
!= HTTP_STATUS_OK
)
763 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
767 // query content length
768 HttpQueryInfoW(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatusLen
, NULL
);
771 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
773 // force passive mode on FTP
774 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0,
775 dwUrlConnectFlags
| INTERNET_FLAG_PASSIVE
,
779 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
783 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
788 // content-length is not known, enable marquee mode
789 ProgressBar
.SetMarquee(TRUE
);
792 free(urlComponents
.lpszScheme
);
793 free(urlComponents
.lpszHostName
);
795 #ifdef USE_CERT_PINNING
796 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
797 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
798 (wcscmp(InfoArray
[iAppId
].szUrl
, APPLICATION_DATABASE_URL
) == 0))
800 CLocalPtr subjectName
, issuerName
;
802 bool bAskQuestion
= false;
803 if (!CertGetSubjectAndIssuer(hFile
, subjectName
, issuerName
))
805 szMsgText
.LoadStringW(IDS_UNABLE_TO_QUERY_CERT
);
810 if (strcmp(subjectName
, CERT_SUBJECT_INFO
) ||
811 strcmp(issuerName
, CERT_ISSUER_INFO
))
813 szMsgText
.Format(IDS_MISMATCH_CERT_INFO
, (char*)subjectName
, (const char*)issuerName
);
820 if (MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_YESNO
| MB_ICONERROR
) != IDYES
)
828 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
830 if (hOut
== INVALID_HANDLE_VALUE
)
833 dwCurrentBytesRead
= 0;
836 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
838 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
842 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
844 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
848 dwCurrentBytesRead
+= dwBytesRead
;
849 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
850 } while (dwBytesRead
&& !bCancelled
);
853 hOut
= INVALID_HANDLE_VALUE
;
860 // set progress bar to 100%
861 ProgressBar
.SetMarquee(FALSE
);
863 dwContentLen
= dwCurrentBytesRead
;
864 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
867 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
868 verify its integrity by using the native advapi32.A_SHA1 functions */
869 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] != 0)
871 ATL::CStringW szMsgText
;
873 // change a few strings in the download dialog to reflect the verification process
874 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_TITLE
))
877 SetWindowTextW(hDlg
, szMsgText
.GetString());
878 SendMessageW(GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
880 // this may take a while, depending on the file size
881 if (!VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
.GetString()))
883 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_FAIL
))
886 MessageBoxW(hDlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
892 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_WAITING_INSTALL
);
897 SHELLEXECUTEINFOW shExInfo
= {0};
898 shExInfo
.cbSize
= sizeof(shExInfo
);
899 shExInfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
900 shExInfo
.lpVerb
= L
"open";
901 shExInfo
.lpFile
= Path
.GetString();
902 shExInfo
.lpParameters
= L
"";
903 shExInfo
.nShow
= SW_SHOW
;
905 if (ShellExecuteExW(&shExInfo
))
907 //reflect installation progress in the titlebar
908 //TODO: make a separate string with a placeholder to include app name?
909 ATL::CStringW szMsgText
= LoadStatusString(DLSTATUS_INSTALLING
);
910 SetWindowTextW(hDlg
, szMsgText
.GetString());
912 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_INSTALLING
);
914 //TODO: issue an install operation separately so that the apps could be downloaded in the background
915 WaitForSingleObject(shExInfo
.hProcess
, INFINITE
);
916 CloseHandle(shExInfo
.hProcess
);
920 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_INSTALL
);
925 if (hOut
!= INVALID_HANDLE_VALUE
)
928 InternetCloseHandle(hFile
);
929 InternetCloseHandle(hOpen
);
933 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
934 DeleteFileW(Path
.GetString());
937 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_FINISHED
);
940 delete static_cast<DownloadParam
*>(param
);
941 SendMessageW(hDlg
, WM_CLOSE
, 0, 0);
945 //TODO: Reuse the dialog
946 VOID
CDownloadManager::LaunchDownloadDialog(BOOL bIsModal
)
951 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
958 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
966 BOOL
DownloadListOfApplications(const ATL::CSimpleArray
<CAvailableApplicationInfo
>& AppsList
, BOOL bIsModal
)
968 if (AppsList
.GetSize() == 0)
971 // Initialize shared variables
972 for (INT i
= 0; i
< AppsList
.GetSize(); ++i
)
974 CDownloadManager::Add(AppsList
[i
]); // implicit conversion to DownloadInfo
977 // Create a dialog and issue a download process
978 CDownloadManager::LaunchDownloadDialog(bIsModal
);
983 BOOL
DownloadApplication(CAvailableApplicationInfo
* pAppInfo
, BOOL bIsModal
)
988 CDownloadManager::Download(*pAppInfo
, bIsModal
);
992 VOID
DownloadApplicationsDB(LPCWSTR lpUrl
)
994 static DownloadInfo DatabaseDLInfo
;
995 DatabaseDLInfo
.szUrl
= lpUrl
;
996 DatabaseDLInfo
.szName
.LoadStringW(IDS_DL_DIALOG_DB_DISP
);
997 CDownloadManager::Download(DatabaseDLInfo
, TRUE
);