- Changed the download process - now ThreadFunc handles the download loop
- Passing values to ThreadFunc as a parameter rather than static members
- Declared .cab download AppInfo static so it won't fade away during the download
- Fixed .cab extraction failing while no rapps folder present
- Visual improvements to the .cab dialog

svn path=/branches/GSoC_2017/rapps/; revision=75659
This commit is contained in:
Alexander Shaposhnikov 2017-08-24 18:46:39 +00:00
parent bf41db72f6
commit c41df30066
7 changed files with 305 additions and 286 deletions

View file

@ -227,37 +227,32 @@ VOID CAvailableApps::FreeCachedEntries()
} }
} }
BOOL CAvailableApps::DeleteCurrentAppsDB() VOID CAvailableApps::DeleteCurrentAppsDB()
{ {
HANDLE hFind = INVALID_HANDLE_VALUE; HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW FindFileData; WIN32_FIND_DATAW FindFileData;
BOOL result = TRUE;
if (m_szPath.IsEmpty()) if (m_szPath.IsEmpty())
return FALSE; return;
result = result && DeleteFileW(m_szCabPath.GetString()); DeleteFileW(m_szCabPath.GetString());
hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData); hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) if (hFind != INVALID_HANDLE_VALUE)
return result; {
ATL::CStringW szTmp; ATL::CStringW szTmp;
do do
{ {
szTmp = m_szPath + FindFileData.cFileName; szTmp = m_szPath + FindFileData.cFileName;
result = result && DeleteFileW(szTmp.GetString()); DeleteFileW(szTmp.GetString());
} while (FindNextFileW(hFind, &FindFileData) != 0); } while (FindNextFileW(hFind, &FindFileData) != 0);
FindClose(hFind); FindClose(hFind);
}
return result;
} }
BOOL CAvailableApps::UpdateAppsDB() BOOL CAvailableApps::UpdateAppsDB()
{ {
if (!DeleteCurrentAppsDB()) DeleteCurrentAppsDB();
return FALSE;
CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL); CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL);
@ -287,12 +282,10 @@ BOOL CAvailableApps::EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnu
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
{ {
if (GetFileAttributesW(m_szCabPath) == INVALID_FILE_ATTRIBUTES) if(!UpdateAppsDB()) {
{ return FALSE;
CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL);
} }
ExtractFilesFromCab(m_szCabPath, m_szAppsPath);
hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData); hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)

View file

@ -1461,7 +1461,7 @@ private:
nSelectedApps = 0; nSelectedApps = 0;
if (EnumType < 0) EnumType = SelectedEnumType; if (EnumType < 0) EnumType = SelectedEnumType;
if (IS_INSTALLED_ENUM(SelectedEnumType)) if (IS_INSTALLED_ENUM(EnumType))
{ {
FreeInstalledAppList(); FreeInstalledAppList();
} }
@ -1482,7 +1482,10 @@ private:
if (isAvailableEnum(EnumType)) if (isAvailableEnum(EnumType))
{ {
/* Enum available applications */ /* Enum available applications */
m_AvailableApps.EnumAvailableApplications(EnumType, s_EnumAvailableAppProc); if (!m_AvailableApps.EnumAvailableApplications(EnumType, s_EnumAvailableAppProc))
{
return;
}
} }
SelectedEnumType = EnumType; SelectedEnumType = EnumType;

View file

@ -115,7 +115,7 @@ class CAvailableApps
public: public:
CAvailableApps(); CAvailableApps();
VOID FreeCachedEntries(); VOID FreeCachedEntries();
BOOL DeleteCurrentAppsDB(); VOID DeleteCurrentAppsDB();
BOOL UpdateAppsDB(); BOOL UpdateAppsDB();
BOOL EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc); BOOL EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc);
const PAPPLICATION_INFO FindInfo(const ATL::CStringW& szAppName); const PAPPLICATION_INFO FindInfo(const ATL::CStringW& szAppName);

View file

@ -10,7 +10,6 @@ class CDowloadingAppsListView;
class CDownloadManager class CDownloadManager
{ {
static PAPPLICATION_INFO AppInfo;
static ATL::CSimpleArray<PAPPLICATION_INFO> AppsToInstallList; static ATL::CSimpleArray<PAPPLICATION_INFO> AppsToInstallList;
static CDowloadingAppsListView DownloadsListView; static CDowloadingAppsListView DownloadsListView;
static INT iCurrentApp; static INT iCurrentApp;

View file

@ -346,19 +346,26 @@ inline VOID MessageBox_LoadString(HWND hMainWnd, INT StringID)
} }
} }
struct DownloadParam
{
DownloadParam() : Dialog(NULL), AppInfo(), szCaption(NULL) {}
DownloadParam(HWND dlg, const ATL::CSimpleArray<PAPPLICATION_INFO> &info, LPCWSTR caption)
: Dialog(dlg), AppInfo(info), szCaption(caption)
{
}
HWND Dialog;
ATL::CSimpleArray<PAPPLICATION_INFO> AppInfo;
LPCWSTR szCaption;
};
// CDownloadManager // CDownloadManager
PAPPLICATION_INFO CDownloadManager::AppInfo;
ATL::CSimpleArray<PAPPLICATION_INFO> CDownloadManager::AppsToInstallList; ATL::CSimpleArray<PAPPLICATION_INFO> CDownloadManager::AppsToInstallList;
CDowloadingAppsListView CDownloadManager::DownloadsListView; CDowloadingAppsListView CDownloadManager::DownloadsListView;
INT CDownloadManager::iCurrentApp; INT CDownloadManager::iCurrentApp;
#define DL_START_NEW WM_APP + 1
INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam) INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ {
HANDLE Thread;
DWORD ThreadId;
HWND Item;
static WCHAR szCaption[MAX_PATH]; static WCHAR szCaption[MAX_PATH];
switch (uMsg) switch (uMsg)
@ -377,7 +384,7 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
} }
SetWindowLongW(Dlg, GWLP_USERDATA, 0); SetWindowLongW(Dlg, GWLP_USERDATA, 0);
Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS); HWND Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
if (Item) if (Item)
{ {
// initialize the default values for our nifty progress bar // initialize the default values for our nifty progress bar
@ -387,8 +394,6 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
SetWindowSubclass(Item, DownloadProgressProc, 0, 0); SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
} }
// Get a dlg string for later use
GetWindowTextW(Dlg, szCaption, MAX_PATH);
// Add a ListView // Add a ListView
HWND hListView = DownloadsListView.Create(Dlg); HWND hListView = DownloadsListView.Create(Dlg);
@ -397,51 +402,16 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
return FALSE; return FALSE;
} }
DownloadsListView.LoadList(AppsToInstallList); DownloadsListView.LoadList(AppsToInstallList);
iCurrentApp = -1;
ShowWindow(Dlg, SW_SHOW); ShowWindow(Dlg, SW_SHOW);
// Start new download // Get a dlg string for later use
SendMessageW(Dlg, DL_START_NEW, 0, 0); GetWindowTextW(Dlg, szCaption, MAX_PATH);
return TRUE; // Start download process
} DownloadParam *param = new DownloadParam(Dlg, AppsToInstallList, szCaption);
case WM_COMMAND: DWORD ThreadId;
if (wParam == IDCANCEL) HANDLE Thread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) param, 0, &ThreadId);
{
SetWindowLongW(Dlg, GWLP_USERDATA, 1);
PostMessageW(Dlg, WM_CLOSE, 0, 0);
}
return FALSE;
case DL_START_NEW:
++iCurrentApp;
// If some downloads left we issue it
if (iCurrentApp < AppsToInstallList.GetSize())
{
AppInfo = AppsToInstallList[iCurrentApp];
if (!AppInfo)
{
return FALSE;
}
ATL::CStringW szNewCaption;
// Reset progress bar
Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
if (Item)
{
SendMessageW(Item, PBM_SETPOS, 0, 0);
}
// Change caption to show the currently downloaded app
szNewCaption.Format(szCaption, AppInfo->szName.GetString());
SetWindowTextW(Dlg, szNewCaption.GetString());
// Add a neat placeholder until the download URL is retrieved
SetDlgItemTextW(Dlg, IDC_DOWNLOAD_STATUS, L"\x2022 \x2022 \x2022");
Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
if (!Thread) if (!Thread)
{ {
@ -451,7 +421,14 @@ INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM w
CloseHandle(Thread); CloseHandle(Thread);
return TRUE; return TRUE;
} }
// No downloads left, closing (fallthrough)
case WM_COMMAND:
if (wParam == IDCANCEL)
{
SetWindowLongW(Dlg, GWLP_USERDATA, 1);
PostMessageW(Dlg, WM_CLOSE, 0, 0);
}
return FALSE;
case WM_CLOSE: case WM_CLOSE:
EndDialog(Dlg, 0); EndDialog(Dlg, 0);
@ -463,7 +440,12 @@ 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) LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam,
UINT_PTR uIdSubclass,
DWORD_PTR dwRefData)
{ {
static ATL::CStringW szProgressText; static ATL::CStringW szProgressText;
@ -538,12 +520,13 @@ LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd, UINT uMsg, WP
} }
} }
DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context) DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
{ {
CComPtr<IBindStatusCallback> dl; CComPtr<IBindStatusCallback> dl;
ATL::CStringW Path; ATL::CStringW Path;
PWSTR p, q; PWSTR p, q;
HWND Dlg = (HWND) Context;
HWND hDlg = static_cast<DownloadParam*>(param)->Dialog;
ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus; ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
ULONG dwCurrentBytesRead = 0; ULONG dwCurrentBytesRead = 0;
@ -563,14 +546,25 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context)
size_t urlLength, filenameLength; size_t urlLength, filenameLength;
const INT iAppId = iCurrentApp; const INT iAppId = iCurrentApp;
const PAPPLICATION_INFO pCurrentInfo = AppInfo; const ATL::CSimpleArray<PAPPLICATION_INFO> InfoArray = static_cast<DownloadParam*>(param)->AppInfo;
if (!AppInfo) LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
delete param;
if (InfoArray.GetSize() <= 0)
{ {
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD); MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
goto end; goto end;
} }
DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading); for (INT iAppId = 0; iAppId < InfoArray.GetSize(); ++iAppId)
{
PAPPLICATION_INFO pCurrentInfo = AppsToInstallList[iAppId];
if (!pCurrentInfo)
{
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
continue;
}
// build the path for the download // build the path for the download
p = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'/'); p = wcsrchr(pCurrentInfo->szUrlDownload.GetString(), L'/');
@ -618,9 +612,34 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context)
goto run; goto run;
} }
// Reset progress bar
HWND Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
if (Item)
{
SendMessageW(Item, PBM_SETPOS, 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 the download URL
SetDlgItemTextW(hDlg, IDC_DOWNLOAD_STATUS, pCurrentInfo->szUrlDownload.GetString());
DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLDownloading);
// download it // download it
bTempfile = TRUE; bTempfile = TRUE;
CDownloadDialog_Constructor(Dlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl)); CDownloadDialog_Constructor(hDlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
if (dl == NULL) if (dl == NULL)
goto end; goto end;
@ -645,7 +664,8 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context)
if (!hOpen) if (!hOpen)
goto end; goto end;
hFile = InternetOpenUrlW(hOpen, pCurrentInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0); hFile = InternetOpenUrlW(hOpen, pCurrentInfo->szUrlDownload.GetString(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
if (!hFile) if (!hFile)
{ {
MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2); MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
@ -734,8 +754,8 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context)
if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE)) if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
goto end; goto end;
SetWindowTextW(Dlg, szMsgText.GetString()); SetWindowTextW(hDlg, szMsgText.GetString());
SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString()); SendMessageW(GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
// this may take a while, depending on the file size // this may take a while, depending on the file size
if (!VerifyInteg(pCurrentInfo->szSHA1, Path.GetString())) if (!VerifyInteg(pCurrentInfo->szSHA1, Path.GetString()))
@ -743,12 +763,11 @@ DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID Context)
if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL)) if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
goto end; goto end;
MessageBoxW(Dlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR); MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
goto end; goto end;
} }
} }
run: run:
DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall); DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLWaitingToInstall);
@ -788,8 +807,11 @@ end:
if (bCancelled || (SettingsInfo.bDelInstaller && !bCab)) if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
DeleteFileW(Path.GetString()); DeleteFileW(Path.GetString());
} }
DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished); DownloadsListView.SetDownloadStatus(iAppId, DOWNLOAD_STATUS::DLFinished);
SendMessageW(Dlg, DL_START_NEW, 0, 0); }
SendMessageW(hDlg, WM_CLOSE, 0, 0);
return 0; return 0;
} }
@ -825,10 +847,11 @@ BOOL CDownloadManager::DownloadApplication(PAPPLICATION_INFO pAppInfo, BOOL moda
VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl) VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
{ {
APPLICATION_INFO IntInfo; static APPLICATION_INFO IntInfo;
IntInfo.szUrlDownload = lpUrl; IntInfo.szUrlDownload = lpUrl;
//TODO: add this string to .rc
DownloadApplication(&IntInfo); IntInfo.szName = L"RAPPS DB";
DownloadApplication(&IntInfo, TRUE);
} }
//TODO: Reuse the dialog //TODO: Reuse the dialog

View file

@ -260,6 +260,8 @@ BOOL ExtractFilesFromCab(LPCWSTR lpCabName, LPCWSTR lpOutputPath)
if (Result == S_OK) if (Result == S_OK)
{ {
Dest.Operation = EXTRACT_EXTRACTFILES; Dest.Operation = EXTRACT_EXTRACTFILES;
CreateDirectoryW(lpOutputPath, NULL);
Result = pfnExtract(&Dest, szCabName); Result = pfnExtract(&Dest, szCabName);
if (Result == S_OK) if (Result == S_OK)
{ {

View file

@ -156,7 +156,6 @@ INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
} }
InitLogs(); InitLogs();
InitCommonControls(); InitCommonControls();
//skip window creation if there were some keys //skip window creation if there were some keys