[RAPPS] Remove unused function definitions
[reactos.git] / base / applications / rapps / loaddlg.cpp
index 155241b..5b31ed0 100644 (file)
 
 #include <rosctrls.h>
 #include <windowsx.h>
+#undef SubclassWindow
 
 #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"
+#define CERT_ISSUER_INFO "US\r\nLet's Encrypt\r\nLet's Encrypt Authority X3"
+#define CERT_SUBJECT_INFO "rapps.reactos.org"
 #endif
 
 enum DownloadStatus
@@ -95,169 +96,130 @@ struct DownloadParam
 };
 
 
-class CDownloadDialog :
-    public CComObjectRootEx<CComMultiThreadModelNoCS>,
-    public IBindStatusCallback
+class CDownloaderProgress
+    : public CWindowImpl<CDownloaderProgress, CWindow, CControlWinTraits>
 {
-    HWND m_hDialog;
-    PBOOL m_pbCancelled;
-    BOOL m_UrlHasBeenCopied;
+    ATL::CStringW m_szProgressText;
 
 public:
-    ~CDownloadDialog()
+    CDownloaderProgress()
     {
-        //DestroyWindow(m_hDialog);
     }
 
-    HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
+    VOID SetMarquee(BOOL Enable)
     {
-        m_hDialog = Dlg;
-        m_pbCancelled = pbCancelled;
-        m_UrlHasBeenCopied = FALSE;
-        return S_OK;
-    }
+        if (Enable)
+            ModifyStyle(0, PBS_MARQUEE, 0);
+        else
+            ModifyStyle(PBS_MARQUEE, 0, 0);
 
-    virtual HRESULT STDMETHODCALLTYPE OnStartBinding(
-        DWORD dwReserved,
-        IBinding *pib)
-    {
-        return S_OK;
+        SendMessage(PBM_SETMARQUEE, Enable, 0);
     }
 
-    virtual HRESULT STDMETHODCALLTYPE GetPriority(
-        LONG *pnPriority)
+    VOID SetProgress(ULONG ulProgress, ULONG ulProgressMax)
     {
-        return S_OK;
-    }
+        WCHAR szProgress[100];
 
-    virtual HRESULT STDMETHODCALLTYPE OnLowResource(
-        DWORD reserved)
-    {
-        return S_OK;
-    }
+        /* format the bits and bytes into pretty and accessible units... */
+        StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
 
-    virtual HRESULT STDMETHODCALLTYPE OnProgress(
-        ULONG ulProgress,
-        ULONG ulProgressMax,
-        ULONG ulStatusCode,
-        LPCWSTR szStatusText)
-    {
-        HWND Item;
-        LONG r;
+        /* use our subclassed progress bar text subroutine */
+        ATL::CStringW ProgressText;
 
-        Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_PROGRESS);
-        if (Item)
+        if (ulProgressMax)
         {
-            WCHAR szProgress[100];
+            /* total size is known */
+            WCHAR szProgressMax[100];
+            UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
 
-            /* format the bits and bytes into pretty and accessible units... */
-            StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
+            /* send the current progress to the progress bar */
+            SendMessage(PBM_SETPOS, uiPercentage, 0);
 
-            /* use our subclassed progress bar text subroutine */
-            ATL::CStringW m_ProgressText;
+            /* format total download size */
+            StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
 
-            if (ulProgressMax)
-            {
-                /* total size is known */
-                WCHAR szProgressMax[100];
-                UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
+            /* generate the text on progress bar */
+            ProgressText.Format(L"%u%% \x2014 %ls / %ls",
+                                uiPercentage,
+                                szProgress,
+                                szProgressMax);
+        }
+        else
+        {
+            /* send the current progress to the progress bar */
+            SendMessage(PBM_SETPOS, 0, 0);
 
-                /* send the current progress to the progress bar */
-                SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
+            /* total size is not known, display only current size */
+            ProgressText.Format(L"%ls...", szProgress);
+        }
 
-                /* format total download size */
-                StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
+        /* and finally display it */
+        SendMessage(WM_SETTEXT, 0, (LPARAM) ProgressText.GetString());
+    }
 
-                /* generate the text on progress bar */
-                m_ProgressText.Format(L"%u%% \x2014 %ls / %ls",
-                                      uiPercentage,
-                                      szProgress,
-                                      szProgressMax);
-            }
-            else
-            {
-                /* send the current progress to the progress bar */
-                SendMessageW(Item, PBM_SETPOS, 0, 0);
+    LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+    {
+        PAINTSTRUCT  ps;
+        HDC hDC = BeginPaint(&ps), hdcMem;
+        HBITMAP hbmMem;
+        HANDLE hOld;
+        RECT myRect;
+        UINT win_width, win_height;
 
-                /* total size is not known, display only current size */
-                m_ProgressText.Format(L"%ls...",
-                                      szProgress);
-            }
-            /* and finally display it */
-            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetString());
-        }
+        GetClientRect(&myRect);
 
-        Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
-        if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
-        {
-            DWORD len = wcslen(szStatusText) + 1;
-            ATL::CStringW buf;
+        /* grab the progress bar rect size */
+        win_width = myRect.right - myRect.left;
+        win_height = myRect.bottom - myRect.top;
 
-            /* beautify our url for display purposes */
-            if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
-            {
-                /* just use the original */
-                buf.ReleaseBuffer();
-                buf = szStatusText;
-            }
-            else
-            {
-                buf.ReleaseBuffer();
-            }
+        /* create an off-screen DC for double-buffering */
+        hdcMem = CreateCompatibleDC(hDC);
+        hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
 
-            /* paste it into our dialog and don't do it again in this instance */
-            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetString());
-            m_UrlHasBeenCopied = TRUE;
-        }
+        hOld = SelectObject(hdcMem, hbmMem);
 
-        SetLastError(ERROR_SUCCESS);
-        r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
-        if (r || GetLastError() != ERROR_SUCCESS)
-        {
-            *m_pbCancelled = TRUE;
-            return E_ABORT;
-        }
+        /* call the original draw code and redirect it to our memory buffer */
+        DefWindowProc(uMsg, (WPARAM) hdcMem, lParam);
 
-        return S_OK;
-    }
+        /* draw our nifty progress text over it */
+        SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
+        DrawShadowText(hdcMem, m_szProgressText.GetString(), m_szProgressText.GetLength(),
+                       &myRect,
+                       DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
+                       GetSysColor(COLOR_CAPTIONTEXT),
+                       GetSysColor(COLOR_3DSHADOW),
+                       1, 1);
 
-    virtual HRESULT STDMETHODCALLTYPE OnStopBinding(
-        HRESULT hresult,
-        LPCWSTR szError)
-    {
-        return S_OK;
-    }
+        /* transfer the off-screen DC to the screen */
+        BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
 
-    virtual HRESULT STDMETHODCALLTYPE GetBindInfo(
-        DWORD *grfBINDF,
-        BINDINFO *pbindinfo)
-    {
-        return S_OK;
-    }
+        /* free the off-screen DC */
+        SelectObject(hdcMem, hOld);
+        DeleteObject(hbmMem);
+        DeleteDC(hdcMem);
 
-    virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(
-        DWORD grfBSCF,
-        DWORD dwSize,
-        FORMATETC *pformatetc,
-        STGMEDIUM *pstgmed)
-    {
-        return S_OK;
+        EndPaint(&ps);
+        return 0;
     }
 
-    virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(
-        REFIID riid,
-        IUnknown *punk)
+    LRESULT OnSetText(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
     {
-        return S_OK;
+        if (lParam)
+        {
+            m_szProgressText = (PCWSTR) lParam;
+        }
+        return 0;
     }
 
-    BEGIN_COM_MAP(CDownloadDialog)
-        COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback, IBindStatusCallback)
-    END_COM_MAP()
+    BEGIN_MSG_MAP(CDownloaderProgress)
+        MESSAGE_HANDLER(WM_ERASEBKGND, OnPaint)
+        MESSAGE_HANDLER(WM_PAINT, OnPaint)
+        MESSAGE_HANDLER(WM_SETTEXT, OnSetText)
+    END_MSG_MAP()
 };
 
 class CDowloadingAppsListView
-    : public CUiWindow<CListView>
+    : public CListView
 {
 public:
     HWND Create(HWND hwndParent)
@@ -284,9 +246,8 @@ public:
 
     VOID SetDownloadStatus(INT ItemIndex, DownloadStatus Status)
     {
-        HWND hListView = GetWindow();
         ATL::CStringW szBuffer = LoadStatusString(Status);
-        ListView_SetItemText(hListView, ItemIndex, 1, const_cast<LPWSTR>(szBuffer.GetString()));
+        SetItemText(ItemIndex, 1, szBuffer.GetString());
     }
 
     BOOL AddItem(INT ItemIndex, LPWSTR lpText)
@@ -324,77 +285,86 @@ public:
     }
 };
 
-extern "C"
-HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
-{
-    return ShellObjectCreatorInit<CDownloadDialog>(Dlg, pbCancelled, riid, ppv);
-}
-
 #ifdef USE_CERT_PINNING
-static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
+typedef CHeapPtr<char, CLocalAllocator> CLocalPtr;
+
+static BOOL CertGetSubjectAndIssuer(HINTERNET hFile, CLocalPtr& subjectInfo, CLocalPtr& issuerInfo)
 {
-    HINTERNET hConnect;
-    HINTERNET hRequest;
     DWORD certInfoLength;
-    BOOL Ret = FALSE;
-    INTERNET_CERTIFICATE_INFOW certInfo;
+    INTERNET_CERTIFICATE_INFOA certInfo;
+    DWORD size, flags;
 
-    hConnect = InternetConnectW(hInternet, lpszHostName, INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
-    if (hConnect)
+    size = sizeof(flags);
+    if (!InternetQueryOptionA(hFile, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size))
     {
-        hRequest = HttpOpenRequestW(hConnect, L"HEAD", NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
-        if (hRequest != NULL)
-        {
-            Ret = HttpSendRequestW(hRequest, L"", 0, NULL, 0);
-            if (Ret)
-            {
-                certInfoLength = sizeof(certInfo);
-                Ret = InternetQueryOptionW(hRequest,
-                                           INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
-                                           &certInfo,
-                                           &certInfoLength);
-                if (Ret)
-                {
-                    if (certInfo.lpszEncryptionAlgName)
-                        LocalFree(certInfo.lpszEncryptionAlgName);
-                    if (certInfo.lpszIssuerInfo)
-                    {
-                        if (strcmp((LPSTR) certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
-                            Ret = FALSE;
-                        LocalFree(certInfo.lpszIssuerInfo);
-                    }
-                    if (certInfo.lpszProtocolName)
-                        LocalFree(certInfo.lpszProtocolName);
-                    if (certInfo.lpszSignatureAlgName)
-                        LocalFree(certInfo.lpszSignatureAlgName);
-                    if (certInfo.lpszSubjectInfo)
-                    {
-                        if (strcmp((LPSTR) certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
-                            Ret = FALSE;
-                        LocalFree(certInfo.lpszSubjectInfo);
-                    }
-                }
-            }
-            InternetCloseHandle(hRequest);
-        }
-        InternetCloseHandle(hConnect);
+        return FALSE;
+    }
+
+    if (!flags & SECURITY_FLAG_SECURE)
+    {
+        return FALSE;
     }
-    return Ret;
+
+    /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
+    certInfoLength = sizeof(certInfo);
+    if (!InternetQueryOptionA(hFile,
+                              INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
+                              &certInfo,
+                              &certInfoLength))
+    {
+        return FALSE;
+    }
+
+    subjectInfo.Attach(certInfo.lpszSubjectInfo);
+    issuerInfo.Attach(certInfo.lpszIssuerInfo);
+
+    if (certInfo.lpszProtocolName)
+        LocalFree(certInfo.lpszProtocolName);
+    if (certInfo.lpszSignatureAlgName)
+        LocalFree(certInfo.lpszSignatureAlgName);
+    if (certInfo.lpszEncryptionAlgName)
+        LocalFree(certInfo.lpszEncryptionAlgName);
+
+    return certInfo.lpszSubjectInfo && certInfo.lpszIssuerInfo;
 }
 #endif
 
 inline VOID MessageBox_LoadString(HWND hMainWnd, INT StringID)
 {
-    ATL::CString szMsgText;
+    ATL::CStringW szMsgText;
     if (szMsgText.LoadStringW(StringID))
     {
         MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
     }
 }
 
+// Download dialog (loaddlg.cpp)
+class CDownloadManager
+{
+    static ATL::CSimpleArray<DownloadInfo> AppsToInstallList;
+    static CDowloadingAppsListView DownloadsListView;
+    static CDownloaderProgress ProgressBar;
+    static BOOL bCancelled;
+    static VOID UpdateProgress(HWND hDlg, ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText);
+public:
+    static VOID Add(DownloadInfo info);
+    static VOID Download(const DownloadInfo& DLInfo, BOOL bIsModal = FALSE);
+    static INT_PTR CALLBACK DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
+    static DWORD WINAPI ThreadFunc(LPVOID Context);
+    static VOID LaunchDownloadDialog(BOOL);
+};
+
+
 // CDownloadManager
 ATL::CSimpleArray<DownloadInfo>         CDownloadManager::AppsToInstallList;
 CDowloadingAppsListView                 CDownloadManager::DownloadsListView;
+CDownloaderProgress                     CDownloadManager::ProgressBar;
+BOOL                                    CDownloadManager::bCancelled;
+
+VOID CDownloadManager::Add(DownloadInfo info)
+{
+    AppsToInstallList.Add(info);
+}
 
 VOID CDownloadManager::Download(const DownloadInfo &DLInfo, BOOL bIsModal)
 {
@@ -414,8 +384,10 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
         HICON hIconSm, hIconBg;
         ATL::CStringW szTempCaption;
 
-        hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
-        hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
+        bCancelled = FALSE;
+
+        hIconBg = (HICON) GetClassLongPtrW(hMainWnd, GCLP_HICON);
+        hIconSm = (HICON) GetClassLongPtrW(hMainWnd, GCLP_HICONSM);
 
         if (hIconBg && hIconSm)
         {
@@ -428,11 +400,10 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
         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);
+            // and subclass it so that it learns to print a status text
+            ProgressBar.SubclassWindow(Item);
+            ProgressBar.SendMessage(PBM_SETRANGE, 0, MAKELPARAM(0, 100));
+            ProgressBar.SendMessage(PBM_SETPOS, 0, 0);
         }
 
         // Add a ListView
@@ -444,7 +415,7 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
         DownloadsListView.LoadList(AppsToInstallList);
 
         // Get a dlg string for later use
-        GetWindowTextW(Dlg, szCaption, MAX_PATH);
+        GetWindowTextW(Dlg, szCaption, _countof(szCaption));
 
         // Hide a placeholder from displaying
         szTempCaption = szCaption;
@@ -471,7 +442,7 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
     case WM_COMMAND:
         if (wParam == IDCANCEL)
         {
-            SetWindowLongW(Dlg, GWLP_USERDATA, 1);
+            bCancelled = TRUE;
             PostMessageW(Dlg, WM_CLOSE, 0, 0);
         }
         return FALSE;
@@ -486,104 +457,46 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
     }
 }
 
-LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
-                                                        UINT uMsg,
-                                                        WPARAM wParam,
-                                                        LPARAM lParam,
-                                                        UINT_PTR uIdSubclass,
-                                                        DWORD_PTR dwRefData)
+BOOL UrlHasBeenCopied;
+
+VOID CDownloadManager::UpdateProgress(
+    HWND hDlg,
+    ULONG ulProgress,
+    ULONG ulProgressMax,
+    ULONG ulStatusCode,
+    LPCWSTR szStatusText)
 {
-    static ATL::CStringW szProgressText;
+    HWND Item;
 
-    switch (uMsg)
-    {
-    case WM_SETTEXT:
-    {
-        if (lParam)
-        {
-            szProgressText = (PCWSTR) lParam;
-        }
-        return TRUE;
-    }
+    ProgressBar.SetProgress(ulProgress, ulProgressMax);
 
-    case WM_ERASEBKGND:
-    case WM_PAINT:
+    Item = GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS);
+    if (Item && szStatusText && wcslen(szStatusText) > 0 && UrlHasBeenCopied == FALSE)
     {
-        PAINTSTRUCT  ps;
-        HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
-        HBITMAP hbmMem;
-        HANDLE hOld;
-        RECT myRect;
-        UINT win_width, win_height;
-
-        GetClientRect(hWnd, &myRect);
-
-        /* grab the progress bar rect size */
-        win_width = myRect.right - myRect.left;
-        win_height = myRect.bottom - myRect.top;
-
-        /* create an off-screen DC for double-buffering */
-        hdcMem = CreateCompatibleDC(hDC);
-        hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
-
-        hOld = SelectObject(hdcMem, hbmMem);
-
-        /* call the original draw code and redirect it to our memory buffer */
-        DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
-
-        /* draw our nifty progress text over it */
-        SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
-        DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
-                       &myRect,
-                       DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
-                       GetSysColor(COLOR_CAPTIONTEXT),
-                       GetSysColor(COLOR_3DSHADOW),
-                       1, 1);
-
-        /* transfer the off-screen DC to the screen */
-        BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
-
-        /* free the off-screen DC */
-        SelectObject(hdcMem, hOld);
-        DeleteObject(hbmMem);
-        DeleteDC(hdcMem);
+        SIZE_T len = wcslen(szStatusText) + 1;
+        ATL::CStringW buf;
+        DWORD dummyLen;
 
-        EndPaint(hWnd, &ps);
-        return 0;
-    }
-
-    /* Raymond Chen says that we should safely unsubclass all the things!
-    (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
+        /* beautify our url for display purposes */
+        if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &dummyLen, ICU_DECODE | ICU_NO_ENCODE))
+        {
+            /* just use the original */
+            buf.ReleaseBuffer();
+            buf = szStatusText;
+        }
+        else
+        {
+            buf.ReleaseBuffer();
+        }
 
-    case WM_NCDESTROY:
-    {
-        szProgressText.Empty();
-        RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
-    }
-    /* Fall-through */
-    default:
-        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
+        /* paste it into our dialog and don't do it again in this instance */
+        SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetString());
+        UrlHasBeenCopied = TRUE;
     }
 }
 
-VOID CDownloadManager::SetProgressMarquee(HWND Item, BOOL Enable)
-{
-    if (!Item)
-        return;
-
-    DWORD style = GetWindowLongPtr(Item, GWL_STYLE);
-    if (!style)
-        return;
-
-    if (!SetWindowLongPtr(Item, GWL_STYLE, (Enable ? style | PBS_MARQUEE : style & ~PBS_MARQUEE)))
-        return;
-
-    SendMessageW(Item, PBM_SETMARQUEE, Enable, 0);
-}
-
 DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
 {
-    CComPtr<IBindStatusCallback> dl;
     ATL::CStringW Path;
     PWSTR p, q;
 
@@ -595,7 +508,6 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
     ULONG dwCurrentBytesRead = 0;
     ULONG dwStatusLen = sizeof(dwStatus);
 
-    BOOL bCancelled = FALSE;
     BOOL bTempfile = FALSE;
     BOOL bCab = FALSE;
 
@@ -612,6 +524,8 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
     LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
     ATL::CStringW szNewCaption;
 
+    const DWORD dwUrlConnectFlags = INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION;
+
     if (InfoArray.GetSize() <= 0)
     {
         MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
@@ -624,9 +538,22 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
         Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
         if (Item)
         {
-            SetProgressMarquee(Item, FALSE);
-            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) L"");
-            SendMessageW(Item, PBM_SETPOS, 0, 0);
+            ProgressBar.SetMarquee(FALSE);
+            ProgressBar.SendMessage(WM_SETTEXT, 0, (LPARAM) L"");
+            ProgressBar.SendMessage(PBM_SETPOS, 0, 0);
+        }
+
+        // is this URL an update package for RAPPS? if so store it in a different place
+        if (InfoArray[iAppId].szUrl == APPLICATION_DATABASE_URL)
+        {
+            bCab = TRUE;
+            if (!GetStorageDirectory(Path))
+                goto end;
+        }
+        else
+        {
+            bCab = FALSE;
+            Path = SettingsInfo.szDownloadDir;
         }
 
         // Change caption to show the currently downloaded app
@@ -657,18 +584,6 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
         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 (InfoArray[iAppId].szUrl == 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)
         {
@@ -693,49 +608,27 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
         DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_DOWNLOADING);
 
         // download it
+        UrlHasBeenCopied = FALSE;
         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)
         {
         case 0: // preconfig
+        default:
             hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
             break;
-        case 1: // direct (no proxy) 
+        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;
 
-        hFile = InternetOpenUrlW(hOpen, InfoArray[iAppId].szUrl.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));
@@ -753,31 +646,87 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
         dwContentLen = 0;
 
         if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
-            HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
+        {
+            hFile = InternetOpenUrlW(hOpen, InfoArray[iAppId].szUrl.GetString(), NULL, 0,
+                                     dwUrlConnectFlags,
+                                     0);
+            if (!hFile)
+            {
+                MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
+                goto end;
+            }
+
+            // query connection
+            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;
+            }
+
+            // query content length
+            HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatusLen, NULL);
+        }
 
         if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
+        {
+            // force passive mode on FTP
+            hFile = InternetOpenUrlW(hOpen, InfoArray[iAppId].szUrl.GetString(), NULL, 0,
+                                     dwUrlConnectFlags | INTERNET_FLAG_PASSIVE,
+                                     0);
+            if (!hFile)
+            {
+                MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
+                goto end;
+            }
+
             dwContentLen = FtpGetFileSize(hFile, &dwStatus);
+        }
 
         if (!dwContentLen)
         {
             // content-length is not known, enable marquee mode
-            SetProgressMarquee(Item, TRUE);
+            ProgressBar.SetMarquee(TRUE);
         }
 
+        free(urlComponents.lpszScheme);
+        free(urlComponents.lpszHostName);
+
 #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(InfoArray[iAppId].szUrl, APPLICATION_DATABASE_URL) == 0) &&
-            (!CertIsValid(hOpen, urlComponents.lpszHostName)))
+            (wcscmp(InfoArray[iAppId].szUrl, APPLICATION_DATABASE_URL) == 0))
         {
-            MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
-            goto end;
+            CLocalPtr subjectName, issuerName;
+            CStringW szMsgText;
+            bool bAskQuestion = false;
+            if (!CertGetSubjectAndIssuer(hFile, subjectName, issuerName))
+            {
+                szMsgText.LoadStringW(IDS_UNABLE_TO_QUERY_CERT);
+                bAskQuestion = true;
+            }
+            else
+            {
+                if (strcmp(subjectName, CERT_SUBJECT_INFO) ||
+                    strcmp(issuerName, CERT_ISSUER_INFO))
+                {
+                    szMsgText.Format(IDS_MISMATCH_CERT_INFO, (char*)subjectName, (const char*)issuerName);
+                    bAskQuestion = true;
+                }
+            }
+
+            if (bAskQuestion)
+            {
+                if (MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_YESNO | MB_ICONERROR) != IDYES)
+                {
+                    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)
@@ -799,7 +748,7 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
             }
 
             dwCurrentBytesRead += dwBytesRead;
-            dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
+            UpdateProgress(hDlg, dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
         } while (dwBytesRead && !bCancelled);
 
         CloseHandle(hOut);
@@ -811,10 +760,10 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
         if (!dwContentLen)
         {
             // set progress bar to 100%
-            SetProgressMarquee(Item, FALSE);
+            ProgressBar.SetMarquee(FALSE);
 
             dwContentLen = dwCurrentBytesRead;
-            dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
+            UpdateProgress(hDlg, dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
         }
 
         /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
@@ -895,7 +844,28 @@ end:
     return 0;
 }
 
-BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<CAvailableApplicationInfo>& AppsList, BOOL bIsModal)
+//TODO: Reuse the dialog
+VOID CDownloadManager::LaunchDownloadDialog(BOOL bIsModal)
+{
+    if (bIsModal)
+    {
+        DialogBoxW(hInst,
+                   MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
+                   hMainWnd,
+                   DownloadDlgProc);
+    }
+    else
+    {
+        CreateDialogW(hInst,
+                      MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
+                      hMainWnd,
+                      DownloadDlgProc);
+    }
+}
+// CDownloadManager
+
+
+BOOL DownloadListOfApplications(const ATL::CSimpleArray<CAvailableApplicationInfo>& AppsList, BOOL bIsModal)
 {
     if (AppsList.GetSize() == 0)
         return FALSE;
@@ -903,48 +873,29 @@ BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<CAvail
     // Initialize shared variables
     for (INT i = 0; i < AppsList.GetSize(); ++i)
     {
-        AppsToInstallList.Add(AppsList[i]); // implicit conversion to DownloadInfo
+        CDownloadManager::Add(AppsList[i]); // implicit conversion to DownloadInfo
     }
 
     // Create a dialog and issue a download process
-    LaunchDownloadDialog(bIsModal);
+    CDownloadManager::LaunchDownloadDialog(bIsModal);
 
     return TRUE;
 }
 
-BOOL CDownloadManager::DownloadApplication(CAvailableApplicationInfo* pAppInfo, BOOL bIsModal)
+BOOL DownloadApplication(CAvailableApplicationInfo* pAppInfo, BOOL bIsModal)
 {
     if (!pAppInfo)
         return FALSE;
 
-    Download(*pAppInfo, bIsModal);
+    CDownloadManager::Download(*pAppInfo, bIsModal);
     return TRUE;
 }
 
-VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
+VOID DownloadApplicationsDB(LPCWSTR lpUrl)
 {
     static DownloadInfo DatabaseDLInfo;
     DatabaseDLInfo.szUrl = lpUrl;
     DatabaseDLInfo.szName.LoadStringW(IDS_DL_DIALOG_DB_DISP);
-    Download(DatabaseDLInfo, TRUE);
+    CDownloadManager::Download(DatabaseDLInfo, TRUE);
 }
 
-//TODO: Reuse the dialog
-VOID CDownloadManager::LaunchDownloadDialog(BOOL bIsModal)
-{
-    if (bIsModal)
-    {
-        DialogBoxW(hInst,
-                   MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
-                   hMainWnd,
-                   DownloadDlgProc);
-    }
-    else
-    {
-        CreateDialogW(hInst,
-                      MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
-                      hMainWnd,
-                      DownloadDlgProc);
-    }
-}
-// CDownloadManager