[RAPPS] Replaced my @gmail.com email with @reactos,org one & Removed my copyright...
[reactos.git] / reactos / 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 "defines.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 typedef enum
55 {
56 DLWaiting = IDS_STATUS_WAITING,
57 DLDownloading = IDS_STATUS_DOWNLOADING,
58 DLWaitingToInstall = IDS_STATUS_DOWNLOADED,
59 DLInstalling = IDS_STATUS_INSTALLING,
60 DLInstalled = IDS_STATUS_INSTALLED,
61 DLFinished = IDS_STATUS_FINISHED
62 } DOWNLOAD_STATUS;
63
64 ATL::CStringW LoadStatusString(DOWNLOAD_STATUS 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(), DOWNLOAD_STATUS::DLWaiting);
263 }
264 }
265
266 VOID SetDownloadStatus(INT ItemIndex, DOWNLOAD_STATUS 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 DOWNLOAD_STATUS 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 INT CDownloadManager::iCurrentApp;
380
381 VOID CDownloadManager::Download(const DownloadInfo &DLInfo, BOOL bIsModal)
382 {
383 AppsToInstallList.RemoveAll();
384 AppsToInstallList.Add(DLInfo);
385 LaunchDownloadDialog(bIsModal);
386 }
387
388 INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
389 {
390 static WCHAR szCaption[MAX_PATH];
391
392 switch (uMsg)
393 {
394 case WM_INITDIALOG:
395 {
396 HICON hIconSm, hIconBg;
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 ShowWindow(Dlg, SW_SHOW);
428
429 // Get a dlg string for later use
430 GetWindowTextW(Dlg, szCaption, MAX_PATH);
431
432 // Start download process
433 DownloadParam *param = new DownloadParam(Dlg, AppsToInstallList, szCaption);
434 DWORD ThreadId;
435 HANDLE Thread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) param, 0, &ThreadId);
436
437 if (!Thread)
438 {
439 return FALSE;
440 }
441
442 CloseHandle(Thread);
443 AppsToInstallList.RemoveAll();
444 return TRUE;
445 }
446
447 case WM_COMMAND:
448 if (wParam == IDCANCEL)
449 {
450 SetWindowLongW(Dlg, GWLP_USERDATA, 1);
451 PostMessageW(Dlg, WM_CLOSE, 0, 0);
452 }
453 return FALSE;
454
455 case WM_CLOSE:
456 EndDialog(Dlg, 0);
457 //DestroyWindow(Dlg);
458 return TRUE;
459
460 default:
461 return FALSE;
462 }
463 }
464
465 LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
466 UINT uMsg,
467 WPARAM wParam,
468 LPARAM lParam,
469 UINT_PTR uIdSubclass,
470 DWORD_PTR dwRefData)
471 {
472 static ATL::CStringW szProgressText;
473
474 switch (uMsg)
475 {
476 case WM_SETTEXT:
477 {
478 if (lParam)
479 {
480 szProgressText = (PCWSTR) lParam;
481 }
482 return TRUE;
483 }
484
485 case WM_ERASEBKGND:
486 case WM_PAINT:
487 {
488 PAINTSTRUCT ps;
489 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
490 HBITMAP hbmMem;
491 HANDLE hOld;
492 RECT myRect;
493 UINT win_width, win_height;
494
495 GetClientRect(hWnd, &myRect);
496
497 /* grab the progress bar rect size */
498 win_width = myRect.right - myRect.left;
499 win_height = myRect.bottom - myRect.top;
500
501 /* create an off-screen DC for double-buffering */
502 hdcMem = CreateCompatibleDC(hDC);
503 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
504
505 hOld = SelectObject(hdcMem, hbmMem);
506
507 /* call the original draw code and redirect it to our memory buffer */
508 DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
509
510 /* draw our nifty progress text over it */
511 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
512 DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
513 &myRect,
514 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
515 GetSysColor(COLOR_CAPTIONTEXT),
516 GetSysColor(COLOR_3DSHADOW),
517 1, 1);
518
519 /* transfer the off-screen DC to the screen */
520 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
521
522 /* free the off-screen DC */
523 SelectObject(hdcMem, hOld);
524 DeleteObject(hbmMem);
525 DeleteDC(hdcMem);
526
527 EndPaint(hWnd, &ps);
528 return 0;
529 }
530
531 /* Raymond Chen says that we should safely unsubclass all the things!
532 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
533
534 case WM_NCDESTROY:
535 {
536 szProgressText.Empty();
537 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
538 }
539 /* Fall-through */
540 default:
541 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
542 }
543 }
544
545 DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
546 {
547 CComPtr<IBindStatusCallback> dl;
548 ATL::CStringW Path;
549 PWSTR p, q;
550
551 HWND hDlg = static_cast<DownloadParam*>(param)->Dialog;
552
553 ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
554 ULONG dwCurrentBytesRead = 0;
555 ULONG dwStatusLen = sizeof(dwStatus);
556
557 BOOL bCancelled = FALSE;
558 BOOL bTempfile = FALSE;
559 BOOL bCab = FALSE;
560
561 HINTERNET hOpen = NULL;
562 HINTERNET hFile = NULL;
563 HANDLE hOut = INVALID_HANDLE_VALUE;
564
565 unsigned char lpBuffer[4096];
566 LPCWSTR lpszAgent = L"RApps/1.0";
567 URL_COMPONENTS urlComponents;
568 size_t urlLength, filenameLength;
569
570 const INT iAppId = iCurrentApp;
571 const ATL::CSimpleArray<DownloadInfo> &InfoArray = static_cast<DownloadParam*>(param)->AppInfo;
572 LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
573 ATL::CStringW szNewCaption;
574
575
576 if (InfoArray.GetSize() <= 0)
577 {
578 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
579 goto end;
580 }
581
582 for (INT iAppId = 0; iAppId < InfoArray.GetSize(); ++iAppId)
583 {
584 const DownloadInfo &CurrentInfo = InfoArray[iAppId];
585
586 // build the path for the download
587 p = wcsrchr(CurrentInfo.szUrl.GetString(), L'/');
588 q = wcsrchr(CurrentInfo.szUrl.GetString(), L'?');
589
590 // do we have a final slash separator?
591 if (!p)
592 goto end;
593
594 // prepare the tentative length of the filename, maybe we've to remove part of it later on
595 filenameLength = wcslen(p) * sizeof(WCHAR);
596
597 /* do we have query arguments in the target URL after the filename? account for them
598 (e.g. https://example.org/myfile.exe?no_adware_plz) */
599 if (q && q > p && (q - p) > 0)
600 filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
601
602 // is this URL an update package for RAPPS? if so store it in a different place
603 if (CurrentInfo.szUrl == APPLICATION_DATABASE_URL)
604 {
605 bCab = TRUE;
606 if (!GetStorageDirectory(Path))
607 goto end;
608 }
609 else
610 {
611 Path = SettingsInfo.szDownloadDir;
612 }
613
614 // is the path valid? can we access it?
615 if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
616 {
617 if (!CreateDirectoryW(Path.GetString(), NULL))
618 goto end;
619 }
620
621 // append a \ to the provided file system path, and the filename portion from the URL after that
622 Path += L"\\";
623 Path += (LPWSTR) (p + 1);
624
625 if (!bCab && CurrentInfo.szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
626 {
627 // only open it in case of total correctness
628 if (VerifyInteg(CurrentInfo.szSHA1.GetString(), Path))
629 goto run;
630 }
631
632 // Reset progress bar
633 HWND Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
634 if (Item)
635 {
636 SendMessageW(Item, PBM_SETPOS, 0, 0);
637 }
638
639 // Change caption to show the currently downloaded app
640 if (!bCab)
641 {
642 szNewCaption.Format(szCaption, CurrentInfo.szName.GetString());
643 }
644 else
645 {
646 szNewCaption.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP);
647 }
648
649 SetWindowTextW(hDlg, szNewCaption.GetString());
650
651 // Add the download URL
652 SetDlgItemTextW(hDlg, IDC_DOWNLOAD_STATUS, CurrentInfo.szUrl.GetString());
653
654 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading);
655
656 // download it
657 bTempfile = TRUE;
658 CDownloadDialog_Constructor(hDlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
659
660 if (dl == NULL)
661 goto end;
662
663 /* FIXME: this should just be using the system-wide proxy settings */
664 switch (SettingsInfo.Proxy)
665 {
666 case 0: // preconfig
667 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
668 break;
669 case 1: // direct (no proxy)
670 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
671 break;
672 case 2: // use proxy
673 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
674 break;
675 default: // preconfig
676 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
677 break;
678 }
679
680 if (!hOpen)
681 goto end;
682
683 hFile = InternetOpenUrlW(hOpen, CurrentInfo.szUrl.GetString(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
684
685 if (!hFile)
686 {
687 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
688 goto end;
689 }
690
691 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
692 goto end;
693
694 if (dwStatus != HTTP_STATUS_OK)
695 {
696 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
697 goto end;
698 }
699
700 dwStatusLen = sizeof(dwStatus);
701
702 memset(&urlComponents, 0, sizeof(urlComponents));
703 urlComponents.dwStructSize = sizeof(urlComponents);
704
705 urlLength = CurrentInfo.szUrl.GetLength();
706 urlComponents.dwSchemeLength = urlLength + 1;
707 urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
708 urlComponents.dwHostNameLength = urlLength + 1;
709 urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
710
711 if (!InternetCrackUrlW(CurrentInfo.szUrl, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
712 goto end;
713
714 if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
715 HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
716
717 if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
718 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
719
720 #ifdef USE_CERT_PINNING
721 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
722 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
723 (wcscmp(CurrentInfo.szUrl, APPLICATION_DATABASE_URL) == 0) &&
724 (!CertIsValid(hOpen, urlComponents.lpszHostName)))
725 {
726 MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
727 goto end;
728 }
729 #endif
730
731 free(urlComponents.lpszScheme);
732 free(urlComponents.lpszHostName);
733
734 hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
735
736 if (hOut == INVALID_HANDLE_VALUE)
737 goto end;
738
739 dwCurrentBytesRead = 0;
740 do
741 {
742 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
743 {
744 MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
745 goto end;
746 }
747
748 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
749 {
750 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
751 goto end;
752 }
753
754 dwCurrentBytesRead += dwBytesRead;
755 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, CurrentInfo.szUrl.GetString());
756 } while (dwBytesRead && !bCancelled);
757
758 CloseHandle(hOut);
759 hOut = INVALID_HANDLE_VALUE;
760
761 if (bCancelled)
762 goto end;
763
764 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
765 verify its integrity by using the native advapi32.A_SHA1 functions */
766 if (!bCab && CurrentInfo.szSHA1[0] != 0)
767 {
768 ATL::CStringW szMsgText;
769
770 // change a few strings in the download dialog to reflect the verification process
771 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_TITLE))
772 goto end;
773
774 SetWindowTextW(hDlg, szMsgText.GetString());
775 SendMessageW(GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
776
777 // this may take a while, depending on the file size
778 if (!VerifyInteg(CurrentInfo.szSHA1.GetString(), Path.GetString()))
779 {
780 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_FAIL))
781 goto end;
782
783 MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
784 goto end;
785 }
786 }
787
788 run:
789 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall);
790
791 // run it
792 if (!bCab)
793 {
794 SHELLEXECUTEINFOW shExInfo = {0};
795 shExInfo.cbSize = sizeof(shExInfo);
796 shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
797 shExInfo.lpVerb = L"open";
798 shExInfo.lpFile = Path.GetString();
799 shExInfo.lpParameters = L"";
800 shExInfo.nShow = SW_SHOW;
801
802 if (ShellExecuteExW(&shExInfo))
803 {
804 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLInstalling);
805 //TODO: issue an install operation separately so that the apps could be downloaded in the background
806 WaitForSingleObject(shExInfo.hProcess, INFINITE);
807 CloseHandle(shExInfo.hProcess);
808 }
809 else
810 {
811 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
812 }
813 }
814
815 end:
816 if (hOut != INVALID_HANDLE_VALUE)
817 CloseHandle(hOut);
818
819 InternetCloseHandle(hFile);
820 InternetCloseHandle(hOpen);
821
822 if (bTempfile)
823 {
824 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
825 DeleteFileW(Path.GetString());
826 }
827
828 DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished);
829 }
830
831 delete param;
832 SendMessageW(hDlg, WM_CLOSE, 0, 0);
833 return 0;
834 }
835
836 BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<CAvailableApplicationInfo*>& AppsList, BOOL bIsModal)
837 {
838 if (AppsList.GetSize() == 0)
839 {
840 return FALSE;
841 }
842
843 // Initialize shared variables
844 for (INT i = 0; i < AppsList.GetSize(); ++i)
845 {
846 if (AppsList[i])
847 {
848 AppsToInstallList.Add(*(AppsList[i]));
849 }
850 }
851
852 // Create a dialog and issue a download process
853 LaunchDownloadDialog(bIsModal);
854
855 return TRUE;
856 }
857
858 BOOL CDownloadManager::DownloadApplication(CAvailableApplicationInfo* pAppInfo, BOOL bIsModal)
859 {
860 if (!pAppInfo)
861 return FALSE;
862
863 Download(*pAppInfo, bIsModal);
864 return TRUE;
865 }
866
867 VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
868 {
869 static DownloadInfo DatabaseDLInfo;
870 DatabaseDLInfo.szUrl = lpUrl;
871 DatabaseDLInfo.szName.LoadStringW(IDS_DL_DIALOG_DB_DISP);
872 Download(DatabaseDLInfo, TRUE);
873 }
874
875 //TODO: Reuse the dialog
876 VOID CDownloadManager::LaunchDownloadDialog(BOOL bIsModal)
877 {
878 if (bIsModal)
879 {
880 DialogBoxW(hInst,
881 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
882 hMainWnd,
883 DownloadDlgProc);
884 }
885 else
886 {
887 CreateDialogW(hInst,
888 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
889 hMainWnd,
890 DownloadDlgProc);
891 }
892 }
893 // CDownloadManager