acf1ddf9275919755a80754f5c0bfa0f3c6a998c
[reactos.git] / reactos / base / applications / rapps / loaddlg.cpp
1 /* PROJECT: ReactOS Applications Manager
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/applications/rapps/loaddlg.cpp
4 * PURPOSE: Displaying a download dialog
5 * COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
6 * Copyright 2004 Mike McCormack (for CodeWeavers)
7 * Copyright 2005 Ge van Geldorp (gvg@reactos.org)
8 * Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
9 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
10 * Copyright 2017 Alexander Shaposhnikov (chaez.san@gmail.com)
11 */
12
13 /*
14 * Based on Wine dlls/shdocvw/shdocvw_main.c
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 */
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
47 typedef enum
48 {
49 DLWaiting = IDS_STATUS_WAITING,
50 DLDownloading = IDS_STATUS_DOWNLOADING,
51 DLWaitingToInstall = IDS_STATUS_DOWNLOADED,
52 DLInstalling = IDS_STATUS_INSTALLING,
53 DLInstalled = IDS_STATUS_INSTALLED,
54 DLFinished = IDS_STATUS_FINISHED
55 } DOWNLOAD_STATUS;
56
57 ATL::CStringW LoadStatusString(DOWNLOAD_STATUS StatusParam)
58 {
59 ATL::CStringW szString;
60 szString.LoadStringW(StatusParam);
61 return szString;
62 }
63
64 class CDownloadDialog :
65 public CComObjectRootEx<CComMultiThreadModelNoCS>,
66 public IBindStatusCallback
67 {
68 HWND m_hDialog;
69 PBOOL m_pbCancelled;
70 BOOL m_UrlHasBeenCopied;
71
72 public:
73 ~CDownloadDialog()
74 {
75 //DestroyWindow(m_hDialog);
76 }
77
78 HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
79 {
80 m_hDialog = Dlg;
81 m_pbCancelled = pbCancelled;
82 m_UrlHasBeenCopied = FALSE;
83 return S_OK;
84 }
85
86 virtual HRESULT STDMETHODCALLTYPE OnStartBinding(
87 DWORD dwReserved,
88 IBinding *pib)
89 {
90 return S_OK;
91 }
92
93 virtual HRESULT STDMETHODCALLTYPE GetPriority(
94 LONG *pnPriority)
95 {
96 return S_OK;
97 }
98
99 virtual HRESULT STDMETHODCALLTYPE OnLowResource(
100 DWORD reserved)
101 {
102 return S_OK;
103 }
104
105 virtual HRESULT STDMETHODCALLTYPE OnProgress(
106 ULONG ulProgress,
107 ULONG ulProgressMax,
108 ULONG ulStatusCode,
109 LPCWSTR szStatusText)
110 {
111 HWND Item;
112 LONG r;
113
114 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_PROGRESS);
115 if (Item && ulProgressMax)
116 {
117 WCHAR szProgress[100];
118 WCHAR szProgressMax[100];
119 UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
120
121 /* send the current progress to the progress bar */
122 SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
123
124 /* format the bits and bytes into pretty and accessible units... */
125 StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
126 StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
127
128 /* ...and post all of it to our subclassed progress bar text subroutine */
129 ATL::CStringW m_ProgressText;
130 m_ProgressText.Format(L"%u%% \x2014 %ls / %ls",
131 uiPercentage,
132 szProgress,
133 szProgressMax);
134 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetString());
135 }
136
137 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
138 if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
139 {
140 DWORD len = wcslen(szStatusText) + 1;
141 ATL::CStringW buf;
142
143 /* beautify our url for display purposes */
144 if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
145 {
146 /* just use the original */
147 buf.ReleaseBuffer();
148 buf = szStatusText;
149 }
150 else
151 {
152 buf.ReleaseBuffer();
153 }
154
155 /* paste it into our dialog and don't do it again in this instance */
156 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetString());
157 m_UrlHasBeenCopied = TRUE;
158 }
159
160 SetLastError(0);
161 r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
162 if (0 != r || 0 != GetLastError())
163 {
164 *m_pbCancelled = TRUE;
165 return E_ABORT;
166 }
167
168 return S_OK;
169 }
170
171 virtual HRESULT STDMETHODCALLTYPE OnStopBinding(
172 HRESULT hresult,
173 LPCWSTR szError)
174 {
175 return S_OK;
176 }
177
178 virtual HRESULT STDMETHODCALLTYPE GetBindInfo(
179 DWORD *grfBINDF,
180 BINDINFO *pbindinfo)
181 {
182 return S_OK;
183 }
184
185 virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(
186 DWORD grfBSCF,
187 DWORD dwSize,
188 FORMATETC *pformatetc,
189 STGMEDIUM *pstgmed)
190 {
191 return S_OK;
192 }
193
194 virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(
195 REFIID riid,
196 IUnknown *punk)
197 {
198 return S_OK;
199 }
200
201 BEGIN_COM_MAP(CDownloadDialog)
202 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback, IBindStatusCallback)
203 END_COM_MAP()
204 };
205
206 class CDowloadingAppsListView
207 : public CUiWindow<CListView>
208 {
209 public:
210 HWND Create(HWND hwndParent)
211 {
212 RECT r = {10, 150, 320, 350};
213 const DWORD style = WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL
214 | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | LVS_NOCOLUMNHEADER;
215
216 HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE);
217
218 AddColumn(0, 150, LVCFMT_LEFT);
219 AddColumn(1, 120, LVCFMT_LEFT);
220
221 return hwnd;
222 }
223
224 VOID LoadList(ATL::CSimpleArray<PAPPLICATION_INFO> arrInfo)
225 {
226 for (INT i = 0; i < arrInfo.GetSize(); ++i)
227 {
228 PAPPLICATION_INFO AppInfo = arrInfo[i];
229 AddRow(i, AppInfo->szName.GetString(), DOWNLOAD_STATUS::DLWaiting);
230 }
231 }
232
233 VOID SetDownloadStatus(INT ItemIndex, DOWNLOAD_STATUS Status)
234 {
235 HWND hListView = GetWindow();
236 ATL::CStringW szBuffer = LoadStatusString(Status);
237 ListView_SetItemText(hListView, ItemIndex, 1, const_cast<LPWSTR>(szBuffer.GetString()));
238 }
239
240 BOOL AddItem(INT ItemIndex, LPWSTR lpText)
241 {
242 LVITEMW Item;
243
244 ZeroMemory(&Item, sizeof(Item));
245
246 Item.mask = LVIF_TEXT | LVIF_STATE;
247 Item.pszText = lpText;
248 Item.iItem = ItemIndex;
249
250 return InsertItem(&Item);
251 }
252
253 VOID AddRow(INT RowIndex, LPCWSTR szAppName, const DOWNLOAD_STATUS Status)
254 {
255 ATL::CStringW szStatus = LoadStatusString(Status);
256 AddItem(RowIndex,
257 const_cast<LPWSTR>(szAppName));
258 SetDownloadStatus(RowIndex, Status);
259 }
260
261 BOOL AddColumn(INT Index, INT Width, INT Format)
262 {
263 LVCOLUMNW Column;
264 ZeroMemory(&Column, sizeof(Column));
265
266 Column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
267 Column.iSubItem = Index;
268 Column.cx = Width;
269 Column.fmt = Format;
270
271 return (InsertColumn(Index, &Column) == -1) ? FALSE : TRUE;
272 }
273 };
274
275 extern "C"
276 HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
277 {
278 return ShellObjectCreatorInit<CDownloadDialog>(Dlg, pbCancelled, riid, ppv);
279 }
280
281 #ifdef USE_CERT_PINNING
282 static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
283 {
284 HINTERNET hConnect;
285 HINTERNET hRequest;
286 DWORD certInfoLength;
287 BOOL Ret = FALSE;
288 INTERNET_CERTIFICATE_INFOW certInfo;
289
290 hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
291 if (hConnect)
292 {
293 hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
294 if (hRequest != NULL)
295 {
296 Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0);
297 if (Ret)
298 {
299 certInfoLength = sizeof(certInfo);
300 Ret = InternetQueryOptionW(hRequest,
301 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
302 &certInfo,
303 &certInfoLength);
304 if (Ret)
305 {
306 if (certInfo.lpszEncryptionAlgName)
307 LocalFree(certInfo.lpszEncryptionAlgName);
308 if (certInfo.lpszIssuerInfo)
309 {
310 if (strcmp((LPSTR) certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
311 Ret = FALSE;
312 LocalFree(certInfo.lpszIssuerInfo);
313 }
314 if (certInfo.lpszProtocolName)
315 LocalFree(certInfo.lpszProtocolName);
316 if (certInfo.lpszSignatureAlgName)
317 LocalFree(certInfo.lpszSignatureAlgName);
318 if (certInfo.lpszSubjectInfo)
319 {
320 if (strcmp((LPSTR) certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
321 Ret = FALSE;
322 LocalFree(certInfo.lpszSubjectInfo);
323 }
324 }
325 }
326 InternetCloseHandle(hRequest);
327 }
328 InternetCloseHandle(hConnect);
329 }
330 return Ret;
331 }
332 #endif
333
334 inline VOID
335 MessageBox_LoadString(HWND hMainWnd, INT StringID)
336 {
337 ATL::CString szMsgText;
338 if (szMsgText.LoadStringW(hInst, StringID))
339 {
340 MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
341 }
342 }
343
344 // ÑDownloadManager
345 PAPPLICATION_INFO ÑDownloadManager::AppInfo;
346 ATL::CSimpleArray<PAPPLICATION_INFO> ÑDownloadManager::AppsToInstallList;
347 CDowloadingAppsListView ÑDownloadManager::DownloadsListView;
348 INT ÑDownloadManager::iCurrentApp;
349
350 #define DL_START_NEW WM_APP + 1
351
352 INT_PTR CALLBACK ÑDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
353 {
354 HANDLE Thread;
355 DWORD ThreadId;
356 HWND Item;
357 static WCHAR szCaption[MAX_PATH];
358
359 switch (uMsg)
360 {
361 case WM_INITDIALOG:
362 {
363 HICON hIconSm, hIconBg;
364
365 hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
366 hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
367
368 if (hIconBg && hIconSm)
369 {
370 SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
371 SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
372 }
373
374 SetWindowLongW(Dlg, GWLP_USERDATA, 0);
375 Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
376 if (Item)
377 {
378 // initialize the default values for our nifty progress bar
379 // and subclass it so that it learns to print a status text
380 SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
381 SendMessageW(Item, PBM_SETPOS, 0, 0);
382
383 SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
384 }
385 // Get a dlg string for later use
386 GetWindowTextW(Dlg, szCaption, MAX_PATH);
387
388 // Add a ListView
389 HWND hListView = DownloadsListView.Create(Dlg);
390 if (!hListView)
391 {
392 return FALSE;
393 }
394 DownloadsListView.LoadList(AppsToInstallList);
395 iCurrentApp = -1;
396
397 ShowWindow(Dlg, SW_SHOW);
398
399 //Start new download
400 SendMessageW(Dlg, DL_START_NEW, 0, 0);
401
402 return TRUE;
403 }
404 case WM_COMMAND:
405 if (wParam == IDCANCEL)
406 {
407 SetWindowLongW(Dlg, GWLP_USERDATA, 1);
408 PostMessageW(Dlg, WM_CLOSE, 0, 0);
409 }
410 return FALSE;
411
412 case DL_START_NEW:
413
414 ++iCurrentApp;
415 // If some downloads left we issue it
416 if (iCurrentApp < AppsToInstallList.GetSize())
417 {
418 AppInfo = AppsToInstallList[iCurrentApp];
419 if (!AppInfo)
420 {
421 return FALSE;
422 }
423
424 ATL::CStringW szNewCaption;
425
426 // Reset progress bar
427 Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
428 if (Item)
429 {
430 SendMessageW(Item, PBM_SETPOS, 0, 0);
431 }
432
433 // Change caption to show the currently downloaded app
434 szNewCaption.Format(szCaption, AppInfo->szName.GetString());
435 SetWindowTextW(Dlg, szNewCaption.GetString());
436
437 // Add a neat placeholder until the download URL is retrieved
438 SetDlgItemTextW(Dlg, IDC_DOWNLOAD_STATUS, L"\x2022 \x2022 \x2022");
439 Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
440
441 if (!Thread)
442 {
443 return FALSE;
444 }
445
446 CloseHandle(Thread);
447 return TRUE;
448 }
449 // No downloads left, closing (fallthrough)
450
451 case WM_CLOSE:
452 EndDialog(Dlg, 0);
453 DestroyWindow(Dlg);
454 return TRUE;
455
456 default:
457 return FALSE;
458 }
459 }
460
461 LRESULT CALLBACK ÑDownloadManager::DownloadProgressProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
462 {
463 static ATL::CStringW szProgressText;
464
465 switch (uMsg)
466 {
467 case WM_SETTEXT:
468 {
469 if (lParam)
470 {
471 szProgressText = (PCWSTR) lParam;
472 }
473 return TRUE;
474 }
475
476 case WM_ERASEBKGND:
477 case WM_PAINT:
478 {
479 PAINTSTRUCT ps;
480 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
481 HBITMAP hbmMem;
482 HANDLE hOld;
483 RECT myRect;
484 UINT win_width, win_height;
485
486 GetClientRect(hWnd, &myRect);
487
488 /* grab the progress bar rect size */
489 win_width = myRect.right - myRect.left;
490 win_height = myRect.bottom - myRect.top;
491
492 /* create an off-screen DC for double-buffering */
493 hdcMem = CreateCompatibleDC(hDC);
494 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
495
496 hOld = SelectObject(hdcMem, hbmMem);
497
498 /* call the original draw code and redirect it to our memory buffer */
499 DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
500
501 /* draw our nifty progress text over it */
502 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
503 DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
504 &myRect,
505 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
506 GetSysColor(COLOR_CAPTIONTEXT),
507 GetSysColor(COLOR_3DSHADOW),
508 1, 1);
509
510 /* transfer the off-screen DC to the screen */
511 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
512
513 /* free the off-screen DC */
514 SelectObject(hdcMem, hOld);
515 DeleteObject(hbmMem);
516 DeleteDC(hdcMem);
517
518 EndPaint(hWnd, &ps);
519 return 0;
520 }
521
522 /* Raymond Chen says that we should safely unsubclass all the things!
523 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
524
525 case WM_NCDESTROY:
526 {
527 szProgressText.Empty();
528 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
529 }
530 /* Fall-through */
531 default:
532 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
533 }
534 }
535
536 DWORD WINAPI ÑDownloadManager::ThreadFunc(LPVOID Context)
537 {
538 CComPtr<IBindStatusCallback> dl;
539 ATL::CStringW Path;
540 PWSTR p, q;
541 HWND Dlg = (HWND) Context;
542
543 ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
544 ULONG dwCurrentBytesRead = 0;
545 ULONG dwStatusLen = sizeof(dwStatus);
546
547 BOOL bCancelled = FALSE;
548 BOOL bTempfile = FALSE;
549 BOOL bCab = FALSE;
550
551 HINTERNET hOpen = NULL;
552 HINTERNET hFile = NULL;
553 HANDLE hOut = INVALID_HANDLE_VALUE;
554
555 unsigned char lpBuffer[4096];
556 LPCWSTR lpszAgent = L"RApps/1.0";
557 URL_COMPONENTS urlComponents;
558 size_t urlLength, filenameLength;
559
560 const INT iAppId = iCurrentApp;
561 const PAPPLICATION_INFO pCurrentInfo = AppInfo;
562 if (!AppInfo)
563 {
564 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
565 goto end;
566 }
567
568 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading);
569
570 // build the path for the download
571 p = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'/');
572 q = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'?');
573
574 // do we have a final slash separator?
575 if (!p)
576 goto end;
577
578 // prepare the tentative length of the filename, maybe we've to remove part of it later on
579 filenameLength = wcslen(p) * sizeof(WCHAR);
580
581 /* do we have query arguments in the target URL after the filename? account for them
582 (e.g. https://example.org/myfile.exe?no_adware_plz) */
583 if (q && q > p && (q - p) > 0)
584 filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
585
586 // is this URL an update package for RAPPS? if so store it in a different place
587 if (pCurrentInfo->szUrlDownload == APPLICATION_DATABASE_URL)
588 {
589 bCab = TRUE;
590 if (!GetStorageDirectory(Path))
591 goto end;
592 }
593 else
594 {
595 Path = SettingsInfo.szDownloadDir;
596 }
597
598 // is the path valid? can we access it?
599 if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
600 {
601 if (!CreateDirectoryW(Path.GetString(), NULL))
602 goto end;
603 }
604
605 // append a \ to the provided file system path, and the filename portion from the URL after that
606 Path += L"\\";
607 Path += (LPWSTR) (p + 1);
608
609 if (!bCab && pCurrentInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
610 {
611 // only open it in case of total correctness
612 if (VerifyInteg(pCurrentInfo->szSHA1, Path))
613 goto run;
614 }
615
616 // download it
617 bTempfile = TRUE;
618 CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
619
620 if (dl == NULL)
621 goto end;
622
623 /* FIXME: this should just be using the system-wide proxy settings */
624 switch (SettingsInfo.Proxy)
625 {
626 case 0: // preconfig
627 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
628 break;
629 case 1: // direct (no proxy)
630 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
631 break;
632 case 2: // use proxy
633 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
634 break;
635 default: // preconfig
636 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
637 break;
638 }
639
640 if (!hOpen)
641 goto end;
642
643 hFile = InternetOpenUrlW(hOpen, pCurrentInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
644 if (!hFile)
645 {
646 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
647 goto end;
648 }
649
650 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
651 goto end;
652
653 if (dwStatus != HTTP_STATUS_OK)
654 {
655 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
656 goto end;
657 }
658
659 dwStatusLen = sizeof(dwStatus);
660
661 memset(&urlComponents, 0, sizeof(urlComponents));
662 urlComponents.dwStructSize = sizeof(urlComponents);
663
664 urlLength = pCurrentInfo->szUrlDownload.GetLength();
665 urlComponents.dwSchemeLength = urlLength + 1;
666 urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
667 urlComponents.dwHostNameLength = urlLength + 1;
668 urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
669
670 if (!InternetCrackUrlW(pCurrentInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
671 goto end;
672
673 if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
674 HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
675
676 if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
677 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
678
679 #ifdef USE_CERT_PINNING
680 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
681 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
682 (wcscmp(pCurrentInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
683 (!CertIsValid(hOpen, urlComponents.lpszHostName)))
684 {
685 MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
686 goto end;
687 }
688 #endif
689
690 free(urlComponents.lpszScheme);
691 free(urlComponents.lpszHostName);
692
693 hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
694
695 if (hOut == INVALID_HANDLE_VALUE)
696 goto end;
697
698 do
699 {
700 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
701 {
702 MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
703 goto end;
704 }
705
706 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
707 {
708 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
709 goto end;
710 }
711
712 dwCurrentBytesRead += dwBytesRead;
713 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, pCurrentInfo->szUrlDownload);
714 } while (dwBytesRead && !bCancelled);
715
716 CloseHandle(hOut);
717 hOut = INVALID_HANDLE_VALUE;
718
719 if (bCancelled)
720 goto end;
721
722 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
723 verify its integrity by using the native advapi32.A_SHA1 functions */
724 if (!bCab && pCurrentInfo->szSHA1[0] != 0)
725 {
726 ATL::CStringW szMsgText;
727
728 // change a few strings in the download dialog to reflect the verification process
729 if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
730 goto end;
731
732 SetWindowTextW(Dlg, szMsgText.GetString());
733 SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
734
735 // this may take a while, depending on the file size
736 if (!VerifyInteg(pCurrentInfo->szSHA1, Path.GetString()))
737 {
738 if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
739 goto end;
740
741 MessageBoxW(Dlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
742 goto end;
743 }
744 }
745
746
747 run:
748 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall);
749
750 // run it
751 if (!bCab)
752 {
753 SHELLEXECUTEINFOW shExInfo = {0};
754 shExInfo.cbSize = sizeof(shExInfo);
755 shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
756 shExInfo.lpVerb = L"open";
757 shExInfo.lpFile = Path.GetString();
758 shExInfo.lpParameters = L"";
759 shExInfo.nShow = SW_SHOW;
760
761 if (ShellExecuteExW(&shExInfo))
762 {
763 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLInstalling);
764 //TODO: issue an install operation separately so that the apps could be downloaded in the background
765 WaitForSingleObject(shExInfo.hProcess, INFINITE);
766 CloseHandle(shExInfo.hProcess);
767 }
768 else
769 {
770 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
771 }
772 }
773
774 end:
775 if (hOut != INVALID_HANDLE_VALUE)
776 CloseHandle(hOut);
777
778 InternetCloseHandle(hFile);
779 InternetCloseHandle(hOpen);
780
781 if (bTempfile)
782 {
783 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
784 DeleteFileW(Path.GetString());
785 }
786 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished);
787 SendMessageW(Dlg, DL_START_NEW, 0, 0);
788 return 0;
789 }
790
791 BOOL ÑDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<PAPPLICATION_INFO>& AppsList)
792 {
793 if (AppsList.GetSize() == 0)
794 {
795 return FALSE;
796 }
797
798 // Initialize shared variables
799 AppsToInstallList = AppsList;
800
801 // Create a dialog and issue a download process
802 LaunchDownloadDialog();
803
804 return TRUE;
805 }
806
807 BOOL ÑDownloadManager::DownloadApplication(PAPPLICATION_INFO pAppInfo)
808 {
809 if (!pAppInfo)
810 {
811 return FALSE;
812 }
813
814 AppsToInstallList.RemoveAll();
815 AppsToInstallList.Add(pAppInfo);
816 LaunchDownloadDialog();
817
818 return TRUE;
819 }
820
821 VOID ÑDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
822 {
823 APPLICATION_INFO IntInfo;
824 IntInfo.szUrlDownload = lpUrl;
825
826 DownloadApplication(&IntInfo);
827 }
828
829 //TODO: Reuse the dialog
830 VOID ÑDownloadManager::LaunchDownloadDialog()
831 {
832 CreateDialogW(hInst,
833 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
834 hMainWnd,
835 DownloadDlgProc);
836 }
837 // CDownloadManager