* Copyright 2017 Alexander Shaposhnikov (chaez.san@gmail.com)
*/
-/*
- * Based on Wine dlls/shdocvw/shdocvw_main.c
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "rapps.h"
+ /*
+ * Based on Wine dlls/shdocvw/shdocvw_main.c
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include "defines.h"
#include <shlobj_undoc.h>
#include <shlguid_undoc.h>
#include <atlbase.h>
#include <atlcom.h>
+#include <atlwin.h>
#include <wininet.h>
#include <shellutils.h>
+
+#include <rosctrls.h>
#include <windowsx.h>
-static PAPPLICATION_INFO AppInfo;
+#include "rosui.h"
+#include "dialogs.h"
+#include "misc.h"
+
+#ifdef USE_CERT_PINNING
+#define CERT_ISSUER_INFO "BE\r\nGlobalSign nv-sa\r\nGlobalSign Domain Validation CA - SHA256 - G2"
+#define CERT_SUBJECT_INFO "Domain Control Validated\r\n*.reactos.org"
+#endif
+
+typedef enum
+{
+ DLWaiting = IDS_STATUS_WAITING,
+ DLDownloading = IDS_STATUS_DOWNLOADING,
+ DLWaitingToInstall = IDS_STATUS_DOWNLOADED,
+ DLInstalling = IDS_STATUS_INSTALLING,
+ DLInstalled = IDS_STATUS_INSTALLED,
+ DLFinished = IDS_STATUS_FINISHED
+} DOWNLOAD_STATUS;
+
+ATL::CStringW LoadStatusString(DOWNLOAD_STATUS StatusParam)
+{
+ ATL::CStringW szString;
+ szString.LoadStringW(StatusParam);
+ return szString;
+}
class CDownloadDialog :
public CComObjectRootEx<CComMultiThreadModelNoCS>,
public:
~CDownloadDialog()
{
- DestroyWindow(m_hDialog);
+ //DestroyWindow(m_hDialog);
}
HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
END_COM_MAP()
};
+class CDowloadingAppsListView
+ : public CUiWindow<CListView>
+{
+public:
+ HWND Create(HWND hwndParent)
+ {
+ RECT r = {10, 150, 320, 350};
+ const DWORD style = WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL
+ | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | LVS_NOCOLUMNHEADER;
+
+ HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE);
+
+ AddColumn(0, 150, LVCFMT_LEFT);
+ AddColumn(1, 120, LVCFMT_LEFT);
+
+ return hwnd;
+ }
+
+ VOID LoadList(ATL::CSimpleArray<PAPPLICATION_INFO> arrInfo)
+ {
+ for (INT i = 0; i < arrInfo.GetSize(); ++i)
+ {
+ PAPPLICATION_INFO AppInfo = arrInfo[i];
+ AddRow(i, AppInfo->szName.GetString(), DOWNLOAD_STATUS::DLWaiting);
+ }
+ }
+
+ VOID SetDownloadStatus(INT ItemIndex, DOWNLOAD_STATUS Status)
+ {
+ HWND hListView = GetWindow();
+ ATL::CStringW szBuffer = LoadStatusString(Status);
+ ListView_SetItemText(hListView, ItemIndex, 1, const_cast<LPWSTR>(szBuffer.GetString()));
+ }
+
+ BOOL AddItem(INT ItemIndex, LPWSTR lpText)
+ {
+ LVITEMW Item;
+
+ ZeroMemory(&Item, sizeof(Item));
+
+ Item.mask = LVIF_TEXT | LVIF_STATE;
+ Item.pszText = lpText;
+ Item.iItem = ItemIndex;
+
+ return InsertItem(&Item);
+ }
+
+ VOID AddRow(INT RowIndex, LPCWSTR szAppName, const DOWNLOAD_STATUS Status)
+ {
+ ATL::CStringW szStatus = LoadStatusString(Status);
+ AddItem(RowIndex,
+ const_cast<LPWSTR>(szAppName));
+ SetDownloadStatus(RowIndex, Status);
+ }
+
+ BOOL AddColumn(INT Index, INT Width, INT Format)
+ {
+ LVCOLUMNW Column;
+ ZeroMemory(&Column, sizeof(Column));
+
+ Column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
+ Column.iSubItem = Index;
+ Column.cx = Width;
+ Column.fmt = Format;
+
+ return (InsertColumn(Index, &Column) == -1) ? FALSE : TRUE;
+ }
+};
+
extern "C"
HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
{
}
#endif
-inline VOID
-MessageBox_LoadString(HWND hMainWnd, INT StringID)
+inline VOID MessageBox_LoadString(HWND hMainWnd, INT StringID)
{
ATL::CString szMsgText;
if (szMsgText.LoadStringW(hInst, StringID))
+ {
MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
+ }
}
-static
-DWORD WINAPI
-ThreadFunc(LPVOID Context)
+struct DownloadParam
{
- CComPtr<IBindStatusCallback> dl;
- ATL::CStringW Path;
- PWSTR p, q;
- HWND Dlg = (HWND) Context;
- ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
- ULONG dwCurrentBytesRead = 0;
- ULONG dwStatusLen = sizeof(dwStatus);
- BOOL bCancelled = FALSE;
- BOOL bTempfile = FALSE;
- BOOL bCab = FALSE;
- HINTERNET hOpen = NULL;
- HINTERNET hFile = NULL;
- HANDLE hOut = INVALID_HANDLE_VALUE;
- unsigned char lpBuffer[4096];
- LPCWSTR lpszAgent = L"RApps/1.0";
- URL_COMPONENTS urlComponents;
- size_t urlLength, filenameLength;
-
- /* build the path for the download */
- p = wcsrchr(AppInfo->szUrlDownload, L'/');
- q = wcsrchr(AppInfo->szUrlDownload, L'?');
-
- /* do we have a final slash separator? */
- if (!p)
- goto end;
-
- /* prepare the tentative length of the filename, maybe we've to remove part of it later on */
- filenameLength = wcslen(p) * sizeof(WCHAR);
-
- /* do we have query arguments in the target URL after the filename? account for them
- (e.g. https://example.org/myfile.exe?no_adware_plz) */
- if (q && q > p && (q - p) > 0)
- filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
-
- /* is this URL an update package for RAPPS? if so store it in a different place */
- if (AppInfo->szUrlDownload == APPLICATION_DATABASE_URL)
- {
- bCab = TRUE;
- if (!GetStorageDirectory(Path))
- goto end;
- }
- else
- {
- Path = SettingsInfo.szDownloadDir;
- }
-
- /* is the path valid? can we access it? */
- if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
- {
- if (!CreateDirectoryW(Path.GetString(), NULL))
- goto end;
- }
-
- /* append a \ to the provided file system path, and the filename portion from the URL after that */
- Path.Format(L"\\%ls", (p + 1));
-
- if (!bCab && AppInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
+ DownloadParam() : Dialog(NULL), AppInfo(), szCaption(NULL) {}
+ DownloadParam(HWND dlg, const ATL::CSimpleArray<PAPPLICATION_INFO> &info, LPCWSTR caption)
+ : Dialog(dlg), AppInfo(info), szCaption(caption)
{
- /* only open it in case of total correctness */
- if (VerifyInteg(AppInfo->szSHA1, Path))
- goto run;
}
- /* download it */
- bTempfile = TRUE;
- CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
-
- if (dl == NULL)
- goto end;
-
- /* FIXME: this should just be using the system-wide proxy settings */
- switch (SettingsInfo.Proxy)
- {
- case 0: /* preconfig */
- hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
- break;
- case 1: /* direct (no proxy) */
- hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
- break;
- case 2: /* use proxy */
- hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
- break;
- default: /* preconfig */
- hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
- break;
- }
-
- if (!hOpen)
- goto end;
+ HWND Dialog;
+ ATL::CSimpleArray<PAPPLICATION_INFO> AppInfo;
+ LPCWSTR szCaption;
+};
- hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
- if (!hFile)
- {
- MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
- goto end;
- }
+// CDownloadManager
+ATL::CSimpleArray<PAPPLICATION_INFO> CDownloadManager::AppsToInstallList;
+CDowloadingAppsListView CDownloadManager::DownloadsListView;
+INT CDownloadManager::iCurrentApp;
- if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
- goto end;
+INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+ static WCHAR szCaption[MAX_PATH];
- if (dwStatus != HTTP_STATUS_OK)
+ switch (uMsg)
{
- MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
- goto end;
- }
-
- dwStatusLen = sizeof(dwStatus);
-
- memset(&urlComponents, 0, sizeof(urlComponents));
- urlComponents.dwStructSize = sizeof(urlComponents);
-
- urlLength = AppInfo->szUrlDownload.GetLength();
- urlComponents.dwSchemeLength = urlLength + 1;
- urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
- urlComponents.dwHostNameLength = urlLength + 1;
- urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
-
- if (!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
- goto end;
-
- if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
- HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
-
- if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
- dwContentLen = FtpGetFileSize(hFile, &dwStatus);
-
-#ifdef USE_CERT_PINNING
- /* are we using HTTPS to download the RAPPS update package? check if the certificate is original */
- if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
- (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
- (!CertIsValid(hOpen, urlComponents.lpszHostName)))
+ case WM_INITDIALOG:
{
- MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
- goto end;
- }
-#endif
-
- free(urlComponents.lpszScheme);
- free(urlComponents.lpszHostName);
+ HICON hIconSm, hIconBg;
- hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+ hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
+ hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
- if (hOut == INVALID_HANDLE_VALUE)
- goto end;
-
- do
- {
- if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
+ if (hIconBg && hIconSm)
{
- MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
- goto end;
+ SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
+ SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
}
- if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
+ SetWindowLongW(Dlg, GWLP_USERDATA, 0);
+ HWND Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
+ if (Item)
{
- MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
- goto end;
- }
-
- dwCurrentBytesRead += dwBytesRead;
- dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, AppInfo->szUrlDownload);
- } while (dwBytesRead && !bCancelled);
+ // initialize the default values for our nifty progress bar
+ // and subclass it so that it learns to print a status text
+ SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
+ SendMessageW(Item, PBM_SETPOS, 0, 0);
- CloseHandle(hOut);
- hOut = INVALID_HANDLE_VALUE;
+ SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
+ }
- if (bCancelled)
- goto end;
+ // Add a ListView
+ HWND hListView = DownloadsListView.Create(Dlg);
+ if (!hListView)
+ {
+ return FALSE;
+ }
+ DownloadsListView.LoadList(AppsToInstallList);
- /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
- verify its integrity by using the native advapi32.A_SHA1 functions */
- if (!bCab && AppInfo->szSHA1[0] != 0)
- {
- ATL::CStringW szMsgText;
+ ShowWindow(Dlg, SW_SHOW);
- /* change a few strings in the download dialog to reflect the verification process */
- if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
- goto end;
+ // Get a dlg string for later use
+ GetWindowTextW(Dlg, szCaption, MAX_PATH);
- SetWindowText(Dlg, szMsgText.GetString());
- SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
+ // Start download process
+ DownloadParam *param = new DownloadParam(Dlg, AppsToInstallList, szCaption);
+ DWORD ThreadId;
+ HANDLE Thread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) param, 0, &ThreadId);
- /* this may take a while, depending on the file size */
- if (!VerifyInteg(AppInfo->szSHA1, Path.GetString()))
+ if (!Thread)
{
- if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
- goto end;
-
- MessageBoxW(Dlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
- goto end;
+ return FALSE;
}
- }
-
- ShowWindow(Dlg, SW_HIDE);
-run:
- /* run it */
- if (!bCab)
- ShellExecuteW(NULL, L"open", Path.GetString(), NULL, NULL, SW_SHOWNORMAL);
+ CloseHandle(Thread);
+ return TRUE;
+ }
-end:
- if (hOut != INVALID_HANDLE_VALUE)
- CloseHandle(hOut);
+ case WM_COMMAND:
+ if (wParam == IDCANCEL)
+ {
+ SetWindowLongW(Dlg, GWLP_USERDATA, 1);
+ PostMessageW(Dlg, WM_CLOSE, 0, 0);
+ }
+ return FALSE;
- InternetCloseHandle(hFile);
- InternetCloseHandle(hOpen);
+ case WM_CLOSE:
+ EndDialog(Dlg, 0);
+ DestroyWindow(Dlg);
+ return TRUE;
- if (bTempfile)
- {
- if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
- DeleteFileW(Path.GetString());
+ default:
+ return FALSE;
}
-
- EndDialog(Dlg, 0);
-
- return 0;
}
-
-LRESULT CALLBACK
-DownloadProgressProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
+LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
+ UINT uMsg,
+ WPARAM wParam,
+ LPARAM lParam,
+ UINT_PTR uIdSubclass,
+ DWORD_PTR dwRefData)
{
static ATL::CStringW szProgressText;
}
/* Raymond Chen says that we should safely unsubclass all the things!
- (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
+ (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
+
case WM_NCDESTROY:
{
szProgressText.Empty();
}
}
-static
-INT_PTR CALLBACK
-DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
+DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
{
- HANDLE Thread;
- DWORD ThreadId;
- HWND Item;
+ CComPtr<IBindStatusCallback> dl;
+ ATL::CStringW Path;
+ PWSTR p, q;
- switch (uMsg)
+ HWND hDlg = static_cast<DownloadParam*>(param)->Dialog;
+
+ ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
+ ULONG dwCurrentBytesRead = 0;
+ ULONG dwStatusLen = sizeof(dwStatus);
+
+ BOOL bCancelled = FALSE;
+ BOOL bTempfile = FALSE;
+ BOOL bCab = FALSE;
+
+ HINTERNET hOpen = NULL;
+ HINTERNET hFile = NULL;
+ HANDLE hOut = INVALID_HANDLE_VALUE;
+
+ unsigned char lpBuffer[4096];
+ LPCWSTR lpszAgent = L"RApps/1.0";
+ URL_COMPONENTS urlComponents;
+ size_t urlLength, filenameLength;
+
+ const INT iAppId = iCurrentApp;
+ const ATL::CSimpleArray<PAPPLICATION_INFO> InfoArray = static_cast<DownloadParam*>(param)->AppInfo;
+ LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
+
+ delete param;
+ if (InfoArray.GetSize() <= 0)
{
- case WM_INITDIALOG:
+ MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
+ goto end;
+ }
+
+ for (INT iAppId = 0; iAppId < InfoArray.GetSize(); ++iAppId)
{
- HICON hIconSm = NULL, hIconBg = NULL;
+ PAPPLICATION_INFO pCurrentInfo = AppsToInstallList[iAppId];
+ if (!pCurrentInfo)
+ {
+ MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
+ continue;
+ }
- hIconBg = (HICON) GetClassLongPtr(hMainWnd, GCLP_HICON);
- hIconSm = (HICON) GetClassLongPtr(hMainWnd, GCLP_HICONSM);
- if (hIconBg && hIconSm)
+ // build the path for the download
+ p = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'/');
+ q = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'?');
+
+ // do we have a final slash separator?
+ if (!p)
+ goto end;
+
+ // prepare the tentative length of the filename, maybe we've to remove part of it later on
+ filenameLength = wcslen(p) * sizeof(WCHAR);
+
+ /* do we have query arguments in the target URL after the filename? account for them
+ (e.g. https://example.org/myfile.exe?no_adware_plz) */
+ if (q && q > p && (q - p) > 0)
+ filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
+
+ // is this URL an update package for RAPPS? if so store it in a different place
+ if (pCurrentInfo->szUrlDownload == APPLICATION_DATABASE_URL)
{
- SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
- SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
+ bCab = TRUE;
+ if (!GetStorageDirectory(Path))
+ goto end;
+ }
+ else
+ {
+ Path = SettingsInfo.szDownloadDir;
}
- SetWindowLongPtrW(Dlg, GWLP_USERDATA, 0);
- Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
+ // is the path valid? can we access it?
+ if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
+ {
+ if (!CreateDirectoryW(Path.GetString(), NULL))
+ goto end;
+ }
+
+ // append a \ to the provided file system path, and the filename portion from the URL after that
+ Path += L"\\";
+ Path += (LPWSTR) (p + 1);
+
+ if (!bCab && pCurrentInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
+ {
+ // only open it in case of total correctness
+ if (VerifyInteg(pCurrentInfo->szSHA1, Path))
+ goto run;
+ }
+
+ // Reset progress bar
+ HWND Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
if (Item)
{
- /* initialize the default values for our nifty progress bar
- and subclass it so that it learns to print a status text */
- SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
SendMessageW(Item, PBM_SETPOS, 0, 0);
+ }
- SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
+ // Change caption to show the currently downloaded app
+ if (!bCab)
+ {
+ ATL::CStringW szNewCaption = "";
+ szNewCaption.Format(szCaption, pCurrentInfo->szName.GetString());
+ SetWindowTextW(hDlg, szNewCaption.GetString());
+ }
+ else
+ {
+ //TODO: add this string to .rc
+ SetWindowTextW(hDlg, L"Downloading Database...");
}
- /* add a neat placeholder until the download URL is retrieved */
- Item = GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS);
- SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) L"\x2022 \x2022 \x2022");
+ // Add the download URL
+ SetDlgItemTextW(hDlg, IDC_DOWNLOAD_STATUS, pCurrentInfo->szUrlDownload.GetString());
- Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
- if (!Thread)
- return FALSE;
- CloseHandle(Thread);
- return TRUE;
- }
- case WM_COMMAND:
- if (wParam == IDCANCEL)
+ DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading);
+
+ // download it
+ bTempfile = TRUE;
+ CDownloadDialog_Constructor(hDlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
+
+ if (dl == NULL)
+ goto end;
+
+ /* FIXME: this should just be using the system-wide proxy settings */
+ switch (SettingsInfo.Proxy)
{
- SetWindowLongPtrW(Dlg, GWLP_USERDATA, 1);
- PostMessageW(Dlg, WM_CLOSE, 0, 0);
+ case 0: // preconfig
+ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
+ break;
+ case 1: // direct (no proxy)
+ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+ break;
+ case 2: // use proxy
+ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
+ break;
+ default: // preconfig
+ hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
+ break;
}
- return FALSE;
- case WM_CLOSE:
- DestroyWindow(Dlg);
- return TRUE;
+ if (!hOpen)
+ goto end;
- default:
- return FALSE;
+ hFile = InternetOpenUrlW(hOpen, pCurrentInfo->szUrlDownload.GetString(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
+
+ if (!hFile)
+ {
+ MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
+ goto end;
+ }
+
+ if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
+ goto end;
+
+ if (dwStatus != HTTP_STATUS_OK)
+ {
+ MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
+ goto end;
+ }
+
+ dwStatusLen = sizeof(dwStatus);
+
+ memset(&urlComponents, 0, sizeof(urlComponents));
+ urlComponents.dwStructSize = sizeof(urlComponents);
+
+ urlLength = pCurrentInfo->szUrlDownload.GetLength();
+ urlComponents.dwSchemeLength = urlLength + 1;
+ urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
+ urlComponents.dwHostNameLength = urlLength + 1;
+ urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
+
+ if (!InternetCrackUrlW(pCurrentInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
+ goto end;
+
+ if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
+ HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
+
+ if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
+ dwContentLen = FtpGetFileSize(hFile, &dwStatus);
+
+#ifdef USE_CERT_PINNING
+ // are we using HTTPS to download the RAPPS update package? check if the certificate is original
+ if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
+ (wcscmp(pCurrentInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
+ (!CertIsValid(hOpen, urlComponents.lpszHostName)))
+ {
+ MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
+ goto end;
+ }
+#endif
+
+ free(urlComponents.lpszScheme);
+ free(urlComponents.lpszHostName);
+
+ hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+
+ if (hOut == INVALID_HANDLE_VALUE)
+ goto end;
+
+ do
+ {
+ if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
+ {
+ MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
+ goto end;
+ }
+
+ if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
+ {
+ MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
+ goto end;
+ }
+
+ dwCurrentBytesRead += dwBytesRead;
+ dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, pCurrentInfo->szUrlDownload);
+ } while (dwBytesRead && !bCancelled);
+
+ CloseHandle(hOut);
+ hOut = INVALID_HANDLE_VALUE;
+
+ if (bCancelled)
+ goto end;
+
+ /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
+ verify its integrity by using the native advapi32.A_SHA1 functions */
+ if (!bCab && pCurrentInfo->szSHA1[0] != 0)
+ {
+ ATL::CStringW szMsgText;
+
+ // change a few strings in the download dialog to reflect the verification process
+ if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
+ goto end;
+
+ SetWindowTextW(hDlg, szMsgText.GetString());
+ SendMessageW(GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
+
+ // this may take a while, depending on the file size
+ if (!VerifyInteg(pCurrentInfo->szSHA1, Path.GetString()))
+ {
+ if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
+ goto end;
+
+ MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
+ goto end;
+ }
+ }
+
+run:
+ DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall);
+
+ // run it
+ if (!bCab)
+ {
+ SHELLEXECUTEINFOW shExInfo = {0};
+ shExInfo.cbSize = sizeof(shExInfo);
+ shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
+ shExInfo.lpVerb = L"open";
+ shExInfo.lpFile = Path.GetString();
+ shExInfo.lpParameters = L"";
+ shExInfo.nShow = SW_SHOW;
+
+ if (ShellExecuteExW(&shExInfo))
+ {
+ DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLInstalling);
+ //TODO: issue an install operation separately so that the apps could be downloaded in the background
+ WaitForSingleObject(shExInfo.hProcess, INFINITE);
+ CloseHandle(shExInfo.hProcess);
+ }
+ else
+ {
+ MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
+ }
+ }
+
+end:
+ if (hOut != INVALID_HANDLE_VALUE)
+ CloseHandle(hOut);
+
+ InternetCloseHandle(hFile);
+ InternetCloseHandle(hOpen);
+
+ if (bTempfile)
+ {
+ if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
+ DeleteFileW(Path.GetString());
+ }
+
+ DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished);
}
+
+ SendMessageW(hDlg, WM_CLOSE, 0, 0);
+ return 0;
}
-BOOL
-DownloadApplication(INT Index)
+BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<PAPPLICATION_INFO>& AppsList, BOOL modal)
{
- if (!IS_AVAILABLE_ENUM(SelectedEnumType))
+ if (AppsList.GetSize() == 0)
+ {
return FALSE;
+ }
- AppInfo = (PAPPLICATION_INFO) ListViewGetlParam(Index);
- if (!AppInfo)
- return FALSE;
+ // Initialize shared variables
+ AppsToInstallList = AppsList;
- WriteLogMessage(EVENTLOG_SUCCESS, MSG_SUCCESS_INSTALL, AppInfo->szName.GetString());
+ // Create a dialog and issue a download process
+ LaunchDownloadDialog(modal);
+
+ return TRUE;
+}
+
+BOOL CDownloadManager::DownloadApplication(PAPPLICATION_INFO pAppInfo, BOOL modal)
+{
+ if (!pAppInfo)
+ {
+ return FALSE;
+ }
- CreateDialogW(hInst,
- MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
- hMainWnd,
- DownloadDlgProc);
+ AppsToInstallList.RemoveAll();
+ AppsToInstallList.Add(pAppInfo);
+ LaunchDownloadDialog(modal);
return TRUE;
}
-VOID
-DownloadApplicationsDB(LPCWSTR lpUrl)
+VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
{
- APPLICATION_INFO IntInfo;
+ static APPLICATION_INFO IntInfo;
IntInfo.szUrlDownload = lpUrl;
+ //TODO: add this string to .rc
+ IntInfo.szName = L"RAPPS DB";
+ DownloadApplication(&IntInfo, TRUE);
+}
- AppInfo = &IntInfo;
+//TODO: Reuse the dialog
+VOID CDownloadManager::LaunchDownloadDialog(BOOL modal)
+{
+ if (modal)
+ {
+ DialogBoxW(hInst,
+ MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
+ hMainWnd,
+ DownloadDlgProc);
+ }
+ else
+ {
+ CreateDialogW(hInst,
+ MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
+ hMainWnd,
+ DownloadDlgProc);
+ }
- CreateDialogW(hInst,
- MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
- hMainWnd,
- DownloadDlgProc);
}
+// CDownloadManager