[RAPPS] Reflect installation status it download dialog header.
[reactos.git] / base / applications / rapps / loaddlg.cpp
1 /*
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)
12 */
13
14 /*
15 * Based on Wine dlls/shdocvw/shdocvw_main.c
16 *
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.
21 *
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.
26 *
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
30 */
31 #include "rapps.h"
32
33 #include <shlobj_undoc.h>
34 #include <shlguid_undoc.h>
35
36 #include <atlbase.h>
37 #include <atlcom.h>
38 #include <atlwin.h>
39 #include <wininet.h>
40 #include <shellutils.h>
41
42 #include <rosctrls.h>
43 #include <windowsx.h>
44
45 #include "rosui.h"
46 #include "dialogs.h"
47 #include "misc.h"
48
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"
52 #endif
53
54 enum DownloadStatus
55 {
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
62 };
63
64 ATL::CStringW LoadStatusString(DownloadStatus StatusParam)
65 {
66 ATL::CStringW szString;
67 szString.LoadStringW(StatusParam);
68 return szString;
69 }
70
71 struct DownloadInfo
72 {
73 DownloadInfo() {}
74 DownloadInfo(const CAvailableApplicationInfo& AppInfo)
75 :szUrl(AppInfo.m_szUrlDownload), szName(AppInfo.m_szName), szSHA1(AppInfo.m_szSHA1)
76 {
77 }
78
79 ATL::CStringW szUrl;
80 ATL::CStringW szName;
81 ATL::CStringW szSHA1;
82 };
83
84 struct DownloadParam
85 {
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)
89 {
90 }
91
92 HWND Dialog;
93 ATL::CSimpleArray<DownloadInfo> AppInfo;
94 LPCWSTR szCaption;
95 };
96
97
98 class CDownloadDialog :
99 public CComObjectRootEx<CComMultiThreadModelNoCS>,
100 public IBindStatusCallback
101 {
102 HWND m_hDialog;
103 PBOOL m_pbCancelled;
104 BOOL m_UrlHasBeenCopied;
105
106 public:
107 ~CDownloadDialog()
108 {
109 //DestroyWindow(m_hDialog);
110 }
111
112 HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
113 {
114 m_hDialog = Dlg;
115 m_pbCancelled = pbCancelled;
116 m_UrlHasBeenCopied = FALSE;
117 return S_OK;
118 }
119
120 virtual HRESULT STDMETHODCALLTYPE OnStartBinding(
121 DWORD dwReserved,
122 IBinding *pib)
123 {
124 return S_OK;
125 }
126
127 virtual HRESULT STDMETHODCALLTYPE GetPriority(
128 LONG *pnPriority)
129 {
130 return S_OK;
131 }
132
133 virtual HRESULT STDMETHODCALLTYPE OnLowResource(
134 DWORD reserved)
135 {
136 return S_OK;
137 }
138
139 virtual HRESULT STDMETHODCALLTYPE OnProgress(
140 ULONG ulProgress,
141 ULONG ulProgressMax,
142 ULONG ulStatusCode,
143 LPCWSTR szStatusText)
144 {
145 HWND Item;
146 LONG r;
147
148 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_PROGRESS);
149 if (Item && ulProgressMax)
150 {
151 WCHAR szProgress[100];
152 WCHAR szProgressMax[100];
153 UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
154
155 /* send the current progress to the progress bar */
156 SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
157
158 /* format the bits and bytes into pretty and accessible units... */
159 StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
160 StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
161
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",
165 uiPercentage,
166 szProgress,
167 szProgressMax);
168 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetString());
169 }
170
171 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
172 if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
173 {
174 DWORD len = wcslen(szStatusText) + 1;
175 ATL::CStringW buf;
176
177 /* beautify our url for display purposes */
178 if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
179 {
180 /* just use the original */
181 buf.ReleaseBuffer();
182 buf = szStatusText;
183 }
184 else
185 {
186 buf.ReleaseBuffer();
187 }
188
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;
192 }
193
194 SetLastError(ERROR_SUCCESS);
195 r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
196 if (r || GetLastError() != ERROR_SUCCESS)
197 {
198 *m_pbCancelled = TRUE;
199 return E_ABORT;
200 }
201
202 return S_OK;
203 }
204
205 virtual HRESULT STDMETHODCALLTYPE OnStopBinding(
206 HRESULT hresult,
207 LPCWSTR szError)
208 {
209 return S_OK;
210 }
211
212 virtual HRESULT STDMETHODCALLTYPE GetBindInfo(
213 DWORD *grfBINDF,
214 BINDINFO *pbindinfo)
215 {
216 return S_OK;
217 }
218
219 virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(
220 DWORD grfBSCF,
221 DWORD dwSize,
222 FORMATETC *pformatetc,
223 STGMEDIUM *pstgmed)
224 {
225 return S_OK;
226 }
227
228 virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(
229 REFIID riid,
230 IUnknown *punk)
231 {
232 return S_OK;
233 }
234
235 BEGIN_COM_MAP(CDownloadDialog)
236 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback, IBindStatusCallback)
237 END_COM_MAP()
238 };
239
240 class CDowloadingAppsListView
241 : public CUiWindow<CListView>
242 {
243 public:
244 HWND Create(HWND hwndParent)
245 {
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;
249
250 HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE);
251
252 AddColumn(0, 150, LVCFMT_LEFT);
253 AddColumn(1, 120, LVCFMT_LEFT);
254
255 return hwnd;
256 }
257
258 VOID LoadList(ATL::CSimpleArray<DownloadInfo> arrInfo)
259 {
260 for (INT i = 0; i < arrInfo.GetSize(); ++i)
261 {
262 AddRow(i, arrInfo[i].szName.GetString(), DLSTATUS_WAITING);
263 }
264 }
265
266 VOID SetDownloadStatus(INT ItemIndex, DownloadStatus Status)
267 {
268 HWND hListView = GetWindow();
269 ATL::CStringW szBuffer = LoadStatusString(Status);
270 ListView_SetItemText(hListView, ItemIndex, 1, const_cast<LPWSTR>(szBuffer.GetString()));
271 }
272
273 BOOL AddItem(INT ItemIndex, LPWSTR lpText)
274 {
275 LVITEMW Item;
276
277 ZeroMemory(&Item, sizeof(Item));
278
279 Item.mask = LVIF_TEXT | LVIF_STATE;
280 Item.pszText = lpText;
281 Item.iItem = ItemIndex;
282
283 return InsertItem(&Item);
284 }
285
286 VOID AddRow(INT RowIndex, LPCWSTR szAppName, const DownloadStatus Status)
287 {
288 ATL::CStringW szStatus = LoadStatusString(Status);
289 AddItem(RowIndex,
290 const_cast<LPWSTR>(szAppName));
291 SetDownloadStatus(RowIndex, Status);
292 }
293
294 BOOL AddColumn(INT Index, INT Width, INT Format)
295 {
296 LVCOLUMNW Column;
297 ZeroMemory(&Column, sizeof(Column));
298
299 Column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
300 Column.iSubItem = Index;
301 Column.cx = Width;
302 Column.fmt = Format;
303
304 return (InsertColumn(Index, &Column) == -1) ? FALSE : TRUE;
305 }
306 };
307
308 extern "C"
309 HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
310 {
311 return ShellObjectCreatorInit<CDownloadDialog>(Dlg, pbCancelled, riid, ppv);
312 }
313
314 #ifdef USE_CERT_PINNING
315 static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
316 {
317 HINTERNET hConnect;
318 HINTERNET hRequest;
319 DWORD certInfoLength;
320 BOOL Ret = FALSE;
321 INTERNET_CERTIFICATE_INFOW certInfo;
322
323 hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
324 if (hConnect)
325 {
326 hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
327 if (hRequest != NULL)
328 {
329 Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0);
330 if (Ret)
331 {
332 certInfoLength = sizeof(certInfo);
333 Ret = InternetQueryOptionW(hRequest,
334 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
335 &certInfo,
336 &certInfoLength);
337 if (Ret)
338 {
339 if (certInfo.lpszEncryptionAlgName)
340 LocalFree(certInfo.lpszEncryptionAlgName);
341 if (certInfo.lpszIssuerInfo)
342 {
343 if (strcmp((LPSTR) certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
344 Ret = FALSE;
345 LocalFree(certInfo.lpszIssuerInfo);
346 }
347 if (certInfo.lpszProtocolName)
348 LocalFree(certInfo.lpszProtocolName);
349 if (certInfo.lpszSignatureAlgName)
350 LocalFree(certInfo.lpszSignatureAlgName);
351 if (certInfo.lpszSubjectInfo)
352 {
353 if (strcmp((LPSTR) certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
354 Ret = FALSE;
355 LocalFree(certInfo.lpszSubjectInfo);
356 }
357 }
358 }
359 InternetCloseHandle(hRequest);
360 }
361 InternetCloseHandle(hConnect);
362 }
363 return Ret;
364 }
365 #endif
366
367 inline VOID MessageBox_LoadString(HWND hMainWnd, INT StringID)
368 {
369 ATL::CString szMsgText;
370 if (szMsgText.LoadStringW(StringID))
371 {
372 MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
373 }
374 }
375
376 // CDownloadManager
377 ATL::CSimpleArray<DownloadInfo> CDownloadManager::AppsToInstallList;
378 CDowloadingAppsListView CDownloadManager::DownloadsListView;
379
380 VOID CDownloadManager::Download(const DownloadInfo &DLInfo, BOOL bIsModal)
381 {
382 AppsToInstallList.RemoveAll();
383 AppsToInstallList.Add(DLInfo);
384 LaunchDownloadDialog(bIsModal);
385 }
386
387 INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
388 {
389 static WCHAR szCaption[MAX_PATH];
390
391 switch (uMsg)
392 {
393 case WM_INITDIALOG:
394 {
395 HICON hIconSm, hIconBg;
396 ATL::CStringW szTempCaption;
397
398 hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
399 hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
400
401 if (hIconBg && hIconSm)
402 {
403 SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
404 SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
405 }
406
407 SetWindowLongW(Dlg, GWLP_USERDATA, 0);
408 HWND Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
409 if (Item)
410 {
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);
415
416 SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
417 }
418
419 // Add a ListView
420 HWND hListView = DownloadsListView.Create(Dlg);
421 if (!hListView)
422 {
423 return FALSE;
424 }
425 DownloadsListView.LoadList(AppsToInstallList);
426
427 // Get a dlg string for later use
428 GetWindowTextW(Dlg, szCaption, MAX_PATH);
429
430 // Hide a placeholder from displaying
431 szTempCaption = szCaption;
432 szTempCaption.Replace(L"%ls", L"");
433 SetWindowText(Dlg, szTempCaption.GetString());
434
435 ShowWindow(Dlg, SW_SHOW);
436
437 // Start download process
438 DownloadParam *param = new DownloadParam(Dlg, AppsToInstallList, szCaption);
439 DWORD ThreadId;
440 HANDLE Thread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) param, 0, &ThreadId);
441
442 if (!Thread)
443 {
444 return FALSE;
445 }
446
447 CloseHandle(Thread);
448 AppsToInstallList.RemoveAll();
449 return TRUE;
450 }
451
452 case WM_COMMAND:
453 if (wParam == IDCANCEL)
454 {
455 SetWindowLongW(Dlg, GWLP_USERDATA, 1);
456 PostMessageW(Dlg, WM_CLOSE, 0, 0);
457 }
458 return FALSE;
459
460 case WM_CLOSE:
461 EndDialog(Dlg, 0);
462 //DestroyWindow(Dlg);
463 return TRUE;
464
465 default:
466 return FALSE;
467 }
468 }
469
470 LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
471 UINT uMsg,
472 WPARAM wParam,
473 LPARAM lParam,
474 UINT_PTR uIdSubclass,
475 DWORD_PTR dwRefData)
476 {
477 static ATL::CStringW szProgressText;
478
479 switch (uMsg)
480 {
481 case WM_SETTEXT:
482 {
483 if (lParam)
484 {
485 szProgressText = (PCWSTR) lParam;
486 }
487 return TRUE;
488 }
489
490 case WM_ERASEBKGND:
491 case WM_PAINT:
492 {
493 PAINTSTRUCT ps;
494 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
495 HBITMAP hbmMem;
496 HANDLE hOld;
497 RECT myRect;
498 UINT win_width, win_height;
499
500 GetClientRect(hWnd, &myRect);
501
502 /* grab the progress bar rect size */
503 win_width = myRect.right - myRect.left;
504 win_height = myRect.bottom - myRect.top;
505
506 /* create an off-screen DC for double-buffering */
507 hdcMem = CreateCompatibleDC(hDC);
508 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
509
510 hOld = SelectObject(hdcMem, hbmMem);
511
512 /* call the original draw code and redirect it to our memory buffer */
513 DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
514
515 /* draw our nifty progress text over it */
516 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
517 DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
518 &myRect,
519 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
520 GetSysColor(COLOR_CAPTIONTEXT),
521 GetSysColor(COLOR_3DSHADOW),
522 1, 1);
523
524 /* transfer the off-screen DC to the screen */
525 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
526
527 /* free the off-screen DC */
528 SelectObject(hdcMem, hOld);
529 DeleteObject(hbmMem);
530 DeleteDC(hdcMem);
531
532 EndPaint(hWnd, &ps);
533 return 0;
534 }
535
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) */
538
539 case WM_NCDESTROY:
540 {
541 szProgressText.Empty();
542 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
543 }
544 /* Fall-through */
545 default:
546 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
547 }
548 }
549
550 DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
551 {
552 CComPtr<IBindStatusCallback> dl;
553 ATL::CStringW Path;
554 PWSTR p, q;
555
556 HWND hDlg = static_cast<DownloadParam*>(param)->Dialog;
557 HWND Item;
558 INT iAppId;
559
560 ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
561 ULONG dwCurrentBytesRead = 0;
562 ULONG dwStatusLen = sizeof(dwStatus);
563
564 BOOL bCancelled = FALSE;
565 BOOL bTempfile = FALSE;
566 BOOL bCab = FALSE;
567
568 HINTERNET hOpen = NULL;
569 HINTERNET hFile = NULL;
570 HANDLE hOut = INVALID_HANDLE_VALUE;
571
572 unsigned char lpBuffer[4096];
573 LPCWSTR lpszAgent = L"RApps/1.0";
574 URL_COMPONENTS urlComponents;
575 size_t urlLength, filenameLength;
576
577 const ATL::CSimpleArray<DownloadInfo> &InfoArray = static_cast<DownloadParam*>(param)->AppInfo;
578 LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
579 ATL::CStringW szNewCaption;
580
581 if (InfoArray.GetSize() <= 0)
582 {
583 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
584 goto end;
585 }
586
587 for (iAppId = 0; iAppId < InfoArray.GetSize(); ++iAppId)
588 {
589 // Reset progress bar
590 Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
591 if (Item)
592 {
593 SendMessageW(Item, PBM_SETPOS, 0, 0);
594 }
595
596 // Change caption to show the currently downloaded app
597 if (!bCab)
598 {
599 szNewCaption.Format(szCaption, InfoArray[iAppId].szName.GetString());
600 }
601 else
602 {
603 szNewCaption.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP);
604 }
605
606 SetWindowTextW(hDlg, szNewCaption.GetString());
607
608 // build the path for the download
609 p = wcsrchr(InfoArray[iAppId].szUrl.GetString(), L'/');
610 q = wcsrchr(InfoArray[iAppId].szUrl.GetString(), L'?');
611
612 // do we have a final slash separator?
613 if (!p)
614 goto end;
615
616 // prepare the tentative length of the filename, maybe we've to remove part of it later on
617 filenameLength = wcslen(p) * sizeof(WCHAR);
618
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);
623
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)
626 {
627 bCab = TRUE;
628 if (!GetStorageDirectory(Path))
629 goto end;
630 }
631 else
632 {
633 Path = SettingsInfo.szDownloadDir;
634 }
635
636 // is the path valid? can we access it?
637 if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
638 {
639 if (!CreateDirectoryW(Path.GetString(), NULL))
640 goto end;
641 }
642
643 // append a \ to the provided file system path, and the filename portion from the URL after that
644 Path += L"\\";
645 Path += (LPWSTR) (p + 1);
646
647 if (!bCab && InfoArray[iAppId].szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
648 {
649 // only open it in case of total correctness
650 if (VerifyInteg(InfoArray[iAppId].szSHA1.GetString(), Path))
651 goto run;
652 }
653
654 // Add the download URL
655 SetDlgItemTextW(hDlg, IDC_DOWNLOAD_STATUS, InfoArray[iAppId].szUrl.GetString());
656
657 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_DOWNLOADING);
658
659 // download it
660 bTempfile = TRUE;
661 CDownloadDialog_Constructor(hDlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
662
663 if (dl == NULL)
664 goto end;
665
666 /* FIXME: this should just be using the system-wide proxy settings */
667 switch (SettingsInfo.Proxy)
668 {
669 case 0: // preconfig
670 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
671 break;
672 case 1: // direct (no proxy)
673 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
674 break;
675 case 2: // use proxy
676 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
677 break;
678 default: // preconfig
679 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
680 break;
681 }
682
683 if (!hOpen)
684 goto end;
685
686 hFile = InternetOpenUrlW(hOpen, InfoArray[iAppId].szUrl.GetString(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
687
688 if (!hFile)
689 {
690 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
691 goto end;
692 }
693
694 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
695 goto end;
696
697 if (dwStatus != HTTP_STATUS_OK)
698 {
699 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
700 goto end;
701 }
702
703 dwStatusLen = sizeof(dwStatus);
704
705 memset(&urlComponents, 0, sizeof(urlComponents));
706 urlComponents.dwStructSize = sizeof(urlComponents);
707
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));
713
714 if (!InternetCrackUrlW(InfoArray[iAppId].szUrl, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
715 goto end;
716
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);
719
720 if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
721 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
722
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)))
728 {
729 MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
730 goto end;
731 }
732 #endif
733
734 free(urlComponents.lpszScheme);
735 free(urlComponents.lpszHostName);
736
737 hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
738
739 if (hOut == INVALID_HANDLE_VALUE)
740 goto end;
741
742 dwCurrentBytesRead = 0;
743 do
744 {
745 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
746 {
747 MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
748 goto end;
749 }
750
751 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
752 {
753 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
754 goto end;
755 }
756
757 dwCurrentBytesRead += dwBytesRead;
758 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
759 } while (dwBytesRead && !bCancelled);
760
761 CloseHandle(hOut);
762 hOut = INVALID_HANDLE_VALUE;
763
764 if (bCancelled)
765 goto end;
766
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)
770 {
771 ATL::CStringW szMsgText;
772
773 // change a few strings in the download dialog to reflect the verification process
774 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_TITLE))
775 goto end;
776
777 SetWindowTextW(hDlg, szMsgText.GetString());
778 SendMessageW(GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
779
780 // this may take a while, depending on the file size
781 if (!VerifyInteg(InfoArray[iAppId].szSHA1.GetString(), Path.GetString()))
782 {
783 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_FAIL))
784 goto end;
785
786 MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
787 goto end;
788 }
789 }
790
791 run:
792 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_WAITING_INSTALL);
793
794 // run it
795 if (!bCab)
796 {
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;
804
805 if (ShellExecuteExW(&shExInfo))
806 {
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());
811
812 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_INSTALLING);
813
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);
817 }
818 else
819 {
820 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
821 }
822 }
823
824 end:
825 if (hOut != INVALID_HANDLE_VALUE)
826 CloseHandle(hOut);
827
828 InternetCloseHandle(hFile);
829 InternetCloseHandle(hOpen);
830
831 if (bTempfile)
832 {
833 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
834 DeleteFileW(Path.GetString());
835 }
836
837 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_FINISHED);
838 }
839
840 delete static_cast<DownloadParam*>(param);
841 SendMessageW(hDlg, WM_CLOSE, 0, 0);
842 return 0;
843 }
844
845 BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<CAvailableApplicationInfo>& AppsList, BOOL bIsModal)
846 {
847 if (AppsList.GetSize() == 0)
848 return FALSE;
849
850 // Initialize shared variables
851 for (INT i = 0; i < AppsList.GetSize(); ++i)
852 {
853 AppsToInstallList.Add(AppsList[i]); // implicit conversion to DownloadInfo
854 }
855
856 // Create a dialog and issue a download process
857 LaunchDownloadDialog(bIsModal);
858
859 return TRUE;
860 }
861
862 BOOL CDownloadManager::DownloadApplication(CAvailableApplicationInfo* pAppInfo, BOOL bIsModal)
863 {
864 if (!pAppInfo)
865 return FALSE;
866
867 Download(*pAppInfo, bIsModal);
868 return TRUE;
869 }
870
871 VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
872 {
873 static DownloadInfo DatabaseDLInfo;
874 DatabaseDLInfo.szUrl = lpUrl;
875 DatabaseDLInfo.szName.LoadStringW(IDS_DL_DIALOG_DB_DISP);
876 Download(DatabaseDLInfo, TRUE);
877 }
878
879 //TODO: Reuse the dialog
880 VOID CDownloadManager::LaunchDownloadDialog(BOOL bIsModal)
881 {
882 if (bIsModal)
883 {
884 DialogBoxW(hInst,
885 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
886 hMainWnd,
887 DownloadDlgProc);
888 }
889 else
890 {
891 CreateDialogW(hInst,
892 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
893 hMainWnd,
894 DownloadDlgProc);
895 }
896 }
897 // CDownloadManager