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