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 "BE\r\nGlobalSign nv-sa\r\nGlobalSign Domain Validation CA - SHA256 - G2"
51 #define CERT_SUBJECT_INFO "Domain Control Validated\r\n*.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
);
149 if (Item
&& ulProgressMax
)
151 WCHAR szProgress
[100];
152 WCHAR szProgressMax
[100];
153 UINT uiPercentage
= ((ULONGLONG
) ulProgress
* 100) / ulProgressMax
;
155 /* send the current progress to the progress bar */
156 SendMessageW(Item
, PBM_SETPOS
, uiPercentage
, 0);
158 /* format the bits and bytes into pretty and accessible units... */
159 StrFormatByteSizeW(ulProgress
, szProgress
, _countof(szProgress
));
160 StrFormatByteSizeW(ulProgressMax
, szProgressMax
, _countof(szProgressMax
));
162 /* ...and post all of it to our subclassed progress bar text subroutine */
163 ATL::CStringW m_ProgressText
;
164 m_ProgressText
.Format(L
"%u%% \x2014 %ls / %ls",
168 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) m_ProgressText
.GetString());
171 Item
= GetDlgItem(m_hDialog
, IDC_DOWNLOAD_STATUS
);
172 if (Item
&& szStatusText
&& wcslen(szStatusText
) > 0 && m_UrlHasBeenCopied
== FALSE
)
174 DWORD len
= wcslen(szStatusText
) + 1;
177 /* beautify our url for display purposes */
178 if (!InternetCanonicalizeUrlW(szStatusText
, buf
.GetBuffer(len
), &len
, ICU_DECODE
| ICU_NO_ENCODE
))
180 /* just use the original */
189 /* paste it into our dialog and don't do it again in this instance */
190 SendMessageW(Item
, WM_SETTEXT
, 0, (LPARAM
) buf
.GetString());
191 m_UrlHasBeenCopied
= TRUE
;
194 SetLastError(ERROR_SUCCESS
);
195 r
= GetWindowLongPtrW(m_hDialog
, GWLP_USERDATA
);
196 if (r
|| GetLastError() != ERROR_SUCCESS
)
198 *m_pbCancelled
= TRUE
;
205 virtual HRESULT STDMETHODCALLTYPE
OnStopBinding(
212 virtual HRESULT STDMETHODCALLTYPE
GetBindInfo(
219 virtual HRESULT STDMETHODCALLTYPE
OnDataAvailable(
222 FORMATETC
*pformatetc
,
228 virtual HRESULT STDMETHODCALLTYPE
OnObjectAvailable(
235 BEGIN_COM_MAP(CDownloadDialog
)
236 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback
, IBindStatusCallback
)
240 class CDowloadingAppsListView
241 : public CUiWindow
<CListView
>
244 HWND
Create(HWND hwndParent
)
246 RECT r
= {10, 150, 320, 350};
247 const DWORD style
= WS_CHILD
| WS_VISIBLE
| LVS_REPORT
| LVS_SINGLESEL
248 | LVS_SHOWSELALWAYS
| LVS_NOSORTHEADER
| LVS_NOCOLUMNHEADER
;
250 HWND hwnd
= CListView::Create(hwndParent
, r
, NULL
, style
, WS_EX_CLIENTEDGE
);
252 AddColumn(0, 150, LVCFMT_LEFT
);
253 AddColumn(1, 120, LVCFMT_LEFT
);
258 VOID
LoadList(ATL::CSimpleArray
<DownloadInfo
> arrInfo
)
260 for (INT i
= 0; i
< arrInfo
.GetSize(); ++i
)
262 AddRow(i
, arrInfo
[i
].szName
.GetString(), DLSTATUS_WAITING
);
266 VOID
SetDownloadStatus(INT ItemIndex
, DownloadStatus Status
)
268 HWND hListView
= GetWindow();
269 ATL::CStringW szBuffer
= LoadStatusString(Status
);
270 ListView_SetItemText(hListView
, ItemIndex
, 1, const_cast<LPWSTR
>(szBuffer
.GetString()));
273 BOOL
AddItem(INT ItemIndex
, LPWSTR lpText
)
277 ZeroMemory(&Item
, sizeof(Item
));
279 Item
.mask
= LVIF_TEXT
| LVIF_STATE
;
280 Item
.pszText
= lpText
;
281 Item
.iItem
= ItemIndex
;
283 return InsertItem(&Item
);
286 VOID
AddRow(INT RowIndex
, LPCWSTR szAppName
, const DownloadStatus Status
)
288 ATL::CStringW szStatus
= LoadStatusString(Status
);
290 const_cast<LPWSTR
>(szAppName
));
291 SetDownloadStatus(RowIndex
, Status
);
294 BOOL
AddColumn(INT Index
, INT Width
, INT Format
)
297 ZeroMemory(&Column
, sizeof(Column
));
299 Column
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_SUBITEM
;
300 Column
.iSubItem
= Index
;
304 return (InsertColumn(Index
, &Column
) == -1) ? FALSE
: TRUE
;
309 HRESULT WINAPI
CDownloadDialog_Constructor(HWND Dlg
, BOOL
*pbCancelled
, REFIID riid
, LPVOID
*ppv
)
311 return ShellObjectCreatorInit
<CDownloadDialog
>(Dlg
, pbCancelled
, riid
, ppv
);
314 #ifdef USE_CERT_PINNING
315 static BOOL
CertIsValid(HINTERNET hInternet
, LPWSTR lpszHostName
)
319 DWORD certInfoLength
;
321 INTERNET_CERTIFICATE_INFOW certInfo
;
323 hConnect
= InternetConnectW(hInternet
, lpszHostName
, INTERNET_DEFAULT_HTTPS_PORT
, NULL
, NULL
, INTERNET_SERVICE_HTTP
, INTERNET_FLAG_SECURE
, 0);
326 hRequest
= HttpOpenRequestW(hConnect
, L
"HEAD", NULL
, NULL
, NULL
, NULL
, INTERNET_FLAG_SECURE
, 0);
327 if (hRequest
!= NULL
)
329 Ret
= HttpSendRequestW(hRequest
, L
"", 0, NULL
, 0);
332 certInfoLength
= sizeof(certInfo
);
333 Ret
= InternetQueryOptionW(hRequest
,
334 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
,
339 if (certInfo
.lpszEncryptionAlgName
)
340 LocalFree(certInfo
.lpszEncryptionAlgName
);
341 if (certInfo
.lpszIssuerInfo
)
343 if (strcmp((LPSTR
) certInfo
.lpszIssuerInfo
, CERT_ISSUER_INFO
) != 0)
345 LocalFree(certInfo
.lpszIssuerInfo
);
347 if (certInfo
.lpszProtocolName
)
348 LocalFree(certInfo
.lpszProtocolName
);
349 if (certInfo
.lpszSignatureAlgName
)
350 LocalFree(certInfo
.lpszSignatureAlgName
);
351 if (certInfo
.lpszSubjectInfo
)
353 if (strcmp((LPSTR
) certInfo
.lpszSubjectInfo
, CERT_SUBJECT_INFO
) != 0)
355 LocalFree(certInfo
.lpszSubjectInfo
);
359 InternetCloseHandle(hRequest
);
361 InternetCloseHandle(hConnect
);
367 inline VOID
MessageBox_LoadString(HWND hMainWnd
, INT StringID
)
369 ATL::CString szMsgText
;
370 if (szMsgText
.LoadStringW(StringID
))
372 MessageBoxW(hMainWnd
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
377 ATL::CSimpleArray
<DownloadInfo
> CDownloadManager::AppsToInstallList
;
378 CDowloadingAppsListView
CDownloadManager::DownloadsListView
;
380 VOID
CDownloadManager::Download(const DownloadInfo
&DLInfo
, BOOL bIsModal
)
382 AppsToInstallList
.RemoveAll();
383 AppsToInstallList
.Add(DLInfo
);
384 LaunchDownloadDialog(bIsModal
);
387 INT_PTR CALLBACK
CDownloadManager::DownloadDlgProc(HWND Dlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
389 static WCHAR szCaption
[MAX_PATH
];
395 HICON hIconSm
, hIconBg
;
396 ATL::CStringW szTempCaption
;
398 hIconBg
= (HICON
) GetClassLongW(hMainWnd
, GCLP_HICON
);
399 hIconSm
= (HICON
) GetClassLongW(hMainWnd
, GCLP_HICONSM
);
401 if (hIconBg
&& hIconSm
)
403 SendMessageW(Dlg
, WM_SETICON
, ICON_BIG
, (LPARAM
) hIconBg
);
404 SendMessageW(Dlg
, WM_SETICON
, ICON_SMALL
, (LPARAM
) hIconSm
);
407 SetWindowLongW(Dlg
, GWLP_USERDATA
, 0);
408 HWND Item
= GetDlgItem(Dlg
, IDC_DOWNLOAD_PROGRESS
);
411 // initialize the default values for our nifty progress bar
412 // and subclass it so that it learns to print a status text
413 SendMessageW(Item
, PBM_SETRANGE
, 0, MAKELPARAM(0, 100));
414 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
416 SetWindowSubclass(Item
, DownloadProgressProc
, 0, 0);
420 HWND hListView
= DownloadsListView
.Create(Dlg
);
425 DownloadsListView
.LoadList(AppsToInstallList
);
427 // Get a dlg string for later use
428 GetWindowTextW(Dlg
, szCaption
, MAX_PATH
);
430 // Hide a placeholder from displaying
431 szTempCaption
= szCaption
;
432 szTempCaption
.Replace(L
"%ls", L
"");
433 SetWindowText(Dlg
, szTempCaption
.GetString());
435 ShowWindow(Dlg
, SW_SHOW
);
437 // Start download process
438 DownloadParam
*param
= new DownloadParam(Dlg
, AppsToInstallList
, szCaption
);
440 HANDLE Thread
= CreateThread(NULL
, 0, ThreadFunc
, (LPVOID
) param
, 0, &ThreadId
);
448 AppsToInstallList
.RemoveAll();
453 if (wParam
== IDCANCEL
)
455 SetWindowLongW(Dlg
, GWLP_USERDATA
, 1);
456 PostMessageW(Dlg
, WM_CLOSE
, 0, 0);
462 //DestroyWindow(Dlg);
470 LRESULT CALLBACK
CDownloadManager::DownloadProgressProc(HWND hWnd
,
474 UINT_PTR uIdSubclass
,
477 static ATL::CStringW szProgressText
;
485 szProgressText
= (PCWSTR
) lParam
;
494 HDC hDC
= BeginPaint(hWnd
, &ps
), hdcMem
;
498 UINT win_width
, win_height
;
500 GetClientRect(hWnd
, &myRect
);
502 /* grab the progress bar rect size */
503 win_width
= myRect
.right
- myRect
.left
;
504 win_height
= myRect
.bottom
- myRect
.top
;
506 /* create an off-screen DC for double-buffering */
507 hdcMem
= CreateCompatibleDC(hDC
);
508 hbmMem
= CreateCompatibleBitmap(hDC
, win_width
, win_height
);
510 hOld
= SelectObject(hdcMem
, hbmMem
);
512 /* call the original draw code and redirect it to our memory buffer */
513 DefSubclassProc(hWnd
, uMsg
, (WPARAM
) hdcMem
, lParam
);
515 /* draw our nifty progress text over it */
516 SelectFont(hdcMem
, GetStockFont(DEFAULT_GUI_FONT
));
517 DrawShadowText(hdcMem
, szProgressText
.GetString(), szProgressText
.GetLength(),
519 DT_CENTER
| DT_VCENTER
| DT_NOPREFIX
| DT_SINGLELINE
,
520 GetSysColor(COLOR_CAPTIONTEXT
),
521 GetSysColor(COLOR_3DSHADOW
),
524 /* transfer the off-screen DC to the screen */
525 BitBlt(hDC
, 0, 0, win_width
, win_height
, hdcMem
, 0, 0, SRCCOPY
);
527 /* free the off-screen DC */
528 SelectObject(hdcMem
, hOld
);
529 DeleteObject(hbmMem
);
536 /* Raymond Chen says that we should safely unsubclass all the things!
537 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
541 szProgressText
.Empty();
542 RemoveWindowSubclass(hWnd
, DownloadProgressProc
, uIdSubclass
);
546 return DefSubclassProc(hWnd
, uMsg
, wParam
, lParam
);
550 DWORD WINAPI
CDownloadManager::ThreadFunc(LPVOID param
)
552 CComPtr
<IBindStatusCallback
> dl
;
556 HWND hDlg
= static_cast<DownloadParam
*>(param
)->Dialog
;
560 ULONG dwContentLen
, dwBytesWritten
, dwBytesRead
, dwStatus
;
561 ULONG dwCurrentBytesRead
= 0;
562 ULONG dwStatusLen
= sizeof(dwStatus
);
564 BOOL bCancelled
= FALSE
;
565 BOOL bTempfile
= FALSE
;
568 HINTERNET hOpen
= NULL
;
569 HINTERNET hFile
= NULL
;
570 HANDLE hOut
= INVALID_HANDLE_VALUE
;
572 unsigned char lpBuffer
[4096];
573 LPCWSTR lpszAgent
= L
"RApps/1.0";
574 URL_COMPONENTS urlComponents
;
575 size_t urlLength
, filenameLength
;
577 const ATL::CSimpleArray
<DownloadInfo
> &InfoArray
= static_cast<DownloadParam
*>(param
)->AppInfo
;
578 LPCWSTR szCaption
= static_cast<DownloadParam
*>(param
)->szCaption
;
579 ATL::CStringW szNewCaption
;
581 if (InfoArray
.GetSize() <= 0)
583 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
587 for (iAppId
= 0; iAppId
< InfoArray
.GetSize(); ++iAppId
)
589 // Reset progress bar
590 Item
= GetDlgItem(hDlg
, IDC_DOWNLOAD_PROGRESS
);
593 SendMessageW(Item
, PBM_SETPOS
, 0, 0);
596 // Change caption to show the currently downloaded app
599 szNewCaption
.Format(szCaption
, InfoArray
[iAppId
].szName
.GetString());
603 szNewCaption
.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP
);
606 SetWindowTextW(hDlg
, szNewCaption
.GetString());
608 // build the path for the download
609 p
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'/');
610 q
= wcsrchr(InfoArray
[iAppId
].szUrl
.GetString(), L
'?');
612 // do we have a final slash separator?
616 // prepare the tentative length of the filename, maybe we've to remove part of it later on
617 filenameLength
= wcslen(p
) * sizeof(WCHAR
);
619 /* do we have query arguments in the target URL after the filename? account for them
620 (e.g. https://example.org/myfile.exe?no_adware_plz) */
621 if (q
&& q
> p
&& (q
- p
) > 0)
622 filenameLength
-= wcslen(q
- 1) * sizeof(WCHAR
);
624 // is this URL an update package for RAPPS? if so store it in a different place
625 if (InfoArray
[iAppId
].szUrl
== APPLICATION_DATABASE_URL
)
628 if (!GetStorageDirectory(Path
))
633 Path
= SettingsInfo
.szDownloadDir
;
636 // is the path valid? can we access it?
637 if (GetFileAttributesW(Path
.GetString()) == INVALID_FILE_ATTRIBUTES
)
639 if (!CreateDirectoryW(Path
.GetString(), NULL
))
643 // append a \ to the provided file system path, and the filename portion from the URL after that
645 Path
+= (LPWSTR
) (p
+ 1);
647 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] && GetFileAttributesW(Path
.GetString()) != INVALID_FILE_ATTRIBUTES
)
649 // only open it in case of total correctness
650 if (VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
))
654 // Add the download URL
655 SetDlgItemTextW(hDlg
, IDC_DOWNLOAD_STATUS
, InfoArray
[iAppId
].szUrl
.GetString());
657 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_DOWNLOADING
);
661 CDownloadDialog_Constructor(hDlg
, &bCancelled
, IID_PPV_ARG(IBindStatusCallback
, &dl
));
666 /* FIXME: this should just be using the system-wide proxy settings */
667 switch (SettingsInfo
.Proxy
)
670 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
672 case 1: // direct (no proxy)
673 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_DIRECT
, NULL
, NULL
, 0);
676 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PROXY
, SettingsInfo
.szProxyServer
, SettingsInfo
.szNoProxyFor
, 0);
678 default: // preconfig
679 hOpen
= InternetOpenW(lpszAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, 0);
686 hFile
= InternetOpenUrlW(hOpen
, InfoArray
[iAppId
].szUrl
.GetString(), NULL
, 0, INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_KEEP_CONNECTION
, 0);
690 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD2
);
694 if (!HttpQueryInfoW(hFile
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, &dwStatus
, &dwStatusLen
, NULL
))
697 if (dwStatus
!= HTTP_STATUS_OK
)
699 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_DOWNLOAD
);
703 dwStatusLen
= sizeof(dwStatus
);
705 memset(&urlComponents
, 0, sizeof(urlComponents
));
706 urlComponents
.dwStructSize
= sizeof(urlComponents
);
708 urlLength
= InfoArray
[iAppId
].szUrl
.GetLength();
709 urlComponents
.dwSchemeLength
= urlLength
+ 1;
710 urlComponents
.lpszScheme
= (LPWSTR
) malloc(urlComponents
.dwSchemeLength
* sizeof(WCHAR
));
711 urlComponents
.dwHostNameLength
= urlLength
+ 1;
712 urlComponents
.lpszHostName
= (LPWSTR
) malloc(urlComponents
.dwHostNameLength
* sizeof(WCHAR
));
714 if (!InternetCrackUrlW(InfoArray
[iAppId
].szUrl
, urlLength
+ 1, ICU_DECODE
| ICU_ESCAPE
, &urlComponents
))
717 if (urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
|| urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
)
718 HttpQueryInfoW(hFile
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, &dwContentLen
, &dwStatus
, 0);
720 if (urlComponents
.nScheme
== INTERNET_SCHEME_FTP
)
721 dwContentLen
= FtpGetFileSize(hFile
, &dwStatus
);
723 #ifdef USE_CERT_PINNING
724 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
725 if ((urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) &&
726 (wcscmp(InfoArray
[iAppId
].szUrl
, APPLICATION_DATABASE_URL
) == 0) &&
727 (!CertIsValid(hOpen
, urlComponents
.lpszHostName
)))
729 MessageBox_LoadString(hMainWnd
, IDS_CERT_DOES_NOT_MATCH
);
734 free(urlComponents
.lpszScheme
);
735 free(urlComponents
.lpszHostName
);
737 hOut
= CreateFileW(Path
.GetString(), GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, CREATE_ALWAYS
, 0, NULL
);
739 if (hOut
== INVALID_HANDLE_VALUE
)
742 dwCurrentBytesRead
= 0;
745 if (!InternetReadFile(hFile
, lpBuffer
, _countof(lpBuffer
), &dwBytesRead
))
747 MessageBox_LoadString(hMainWnd
, IDS_INTERRUPTED_DOWNLOAD
);
751 if (!WriteFile(hOut
, &lpBuffer
[0], dwBytesRead
, &dwBytesWritten
, NULL
))
753 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_WRITE
);
757 dwCurrentBytesRead
+= dwBytesRead
;
758 dl
->OnProgress(dwCurrentBytesRead
, dwContentLen
, 0, InfoArray
[iAppId
].szUrl
.GetString());
759 } while (dwBytesRead
&& !bCancelled
);
762 hOut
= INVALID_HANDLE_VALUE
;
767 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
768 verify its integrity by using the native advapi32.A_SHA1 functions */
769 if (!bCab
&& InfoArray
[iAppId
].szSHA1
[0] != 0)
771 ATL::CStringW szMsgText
;
773 // change a few strings in the download dialog to reflect the verification process
774 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_TITLE
))
777 SetWindowTextW(hDlg
, szMsgText
.GetString());
778 SendMessageW(GetDlgItem(hDlg
, IDC_DOWNLOAD_STATUS
), WM_SETTEXT
, 0, (LPARAM
) Path
.GetString());
780 // this may take a while, depending on the file size
781 if (!VerifyInteg(InfoArray
[iAppId
].szSHA1
.GetString(), Path
.GetString()))
783 if (!szMsgText
.LoadStringW(IDS_INTEG_CHECK_FAIL
))
786 MessageBoxW(hDlg
, szMsgText
.GetString(), NULL
, MB_OK
| MB_ICONERROR
);
792 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_WAITING_INSTALL
);
797 SHELLEXECUTEINFOW shExInfo
= {0};
798 shExInfo
.cbSize
= sizeof(shExInfo
);
799 shExInfo
.fMask
= SEE_MASK_NOCLOSEPROCESS
;
800 shExInfo
.lpVerb
= L
"open";
801 shExInfo
.lpFile
= Path
.GetString();
802 shExInfo
.lpParameters
= L
"";
803 shExInfo
.nShow
= SW_SHOW
;
805 if (ShellExecuteExW(&shExInfo
))
807 //reflect installation progress in the titlebar
808 //TODO: make a separate string with a placeholder to include app name?
809 ATL::CStringW szMsgText
= LoadStatusString(DLSTATUS_INSTALLING
);
810 SetWindowTextW(hDlg
, szMsgText
.GetString());
812 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_INSTALLING
);
814 //TODO: issue an install operation separately so that the apps could be downloaded in the background
815 WaitForSingleObject(shExInfo
.hProcess
, INFINITE
);
816 CloseHandle(shExInfo
.hProcess
);
820 MessageBox_LoadString(hMainWnd
, IDS_UNABLE_TO_INSTALL
);
825 if (hOut
!= INVALID_HANDLE_VALUE
)
828 InternetCloseHandle(hFile
);
829 InternetCloseHandle(hOpen
);
833 if (bCancelled
|| (SettingsInfo
.bDelInstaller
&& !bCab
))
834 DeleteFileW(Path
.GetString());
837 DownloadsListView
.SetDownloadStatus(iAppId
, DLSTATUS_FINISHED
);
840 delete static_cast<DownloadParam
*>(param
);
841 SendMessageW(hDlg
, WM_CLOSE
, 0, 0);
845 BOOL
CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray
<CAvailableApplicationInfo
>& AppsList
, BOOL bIsModal
)
847 if (AppsList
.GetSize() == 0)
850 // Initialize shared variables
851 for (INT i
= 0; i
< AppsList
.GetSize(); ++i
)
853 AppsToInstallList
.Add(AppsList
[i
]); // implicit conversion to DownloadInfo
856 // Create a dialog and issue a download process
857 LaunchDownloadDialog(bIsModal
);
862 BOOL
CDownloadManager::DownloadApplication(CAvailableApplicationInfo
* pAppInfo
, BOOL bIsModal
)
867 Download(*pAppInfo
, bIsModal
);
871 VOID
CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl
)
873 static DownloadInfo DatabaseDLInfo
;
874 DatabaseDLInfo
.szUrl
= lpUrl
;
875 DatabaseDLInfo
.szName
.LoadStringW(IDS_DL_DIALOG_DB_DISP
);
876 Download(DatabaseDLInfo
, TRUE
);
879 //TODO: Reuse the dialog
880 VOID
CDownloadManager::LaunchDownloadDialog(BOOL bIsModal
)
885 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),
892 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG
),