Add Events to FileDownloader

OnComplete: worked
OnProgress: not implemented
This commit is contained in:
Oleg Korshul
2019-12-26 17:40:17 +03:00
parent 6225da01dc
commit 39ddc7582f
4 changed files with 49 additions and 10 deletions

View File

@ -103,6 +103,15 @@ void CFileDownloader::CheckSuspend()
return m_pInternal->CheckSuspend();
}
void CFileDownloader::SetEvent_OnProgress(CFileDownloader_OnProgress func)
{
m_pInternal->GetInternal()->m_func_onProgress = func;
}
void CFileDownloader::SetEvent_OnComplete(CFileDownloader_OnComplete func)
{
m_pInternal->GetInternal()->m_func_onComplete = func;
}
#ifdef _MAC
bool CFileDownloader::m_bIsARCEnabled = false;

View File

@ -34,6 +34,10 @@
#include <string>
#include "../kernel_config.h"
typedef void (*CFileDownloader_OnComplete)(int error);
// <return> cancel: 1, else 0
typedef int (*CFileDownloader_OnProgress)(int percent);
class CFileDownloader_private;
class KERNEL_DECL CFileDownloader
{
@ -68,6 +72,10 @@ public:
void CheckSuspend();
//events
void SetEvent_OnProgress(CFileDownloader_OnProgress);
void SetEvent_OnComplete(CFileDownloader_OnComplete);
#ifdef _MAC
static void SetARCEnabled(const bool& enabled);
static bool GetARCEnabled();

View File

@ -33,6 +33,7 @@
#include "../../DesktopEditor/common/File.h"
#include "../../DesktopEditor/graphics/BaseThread.h"
#include "./FileDownloader.h"
class CFileDownloaderBase
{
@ -43,6 +44,9 @@ public :
m_sFileUrl = sFileUrl;
m_bComplete = false;
m_bDelete = bDelete;
m_func_onComplete = NULL;
m_func_onProgress = NULL;
}
virtual ~CFileDownloaderBase ()
{
@ -62,6 +66,8 @@ public:
bool m_bComplete; // Закачался файл или нет
bool m_bDelete; // Удалять ли файл в деструкторе
CFileDownloader_OnComplete m_func_onComplete;
CFileDownloader_OnProgress m_func_onProgress;
};
class CFileDownloader_private : public NSThreads::CBaseThread
@ -70,6 +76,12 @@ protected:
// создаем в зависимости от платформы
CFileDownloaderBase* m_pInternal;
public:
CFileDownloaderBase* GetInternal()
{
return m_pInternal;
}
public:
CFileDownloader_private(std::wstring sFileUrl, bool bDelete = true);
virtual ~CFileDownloader_private()
@ -108,18 +120,13 @@ protected :
{
m_pInternal->m_bComplete = false;
if ( true )
{
int hrResultAll = m_pInternal->DownloadFile();
int hrResultAll = m_pInternal->DownloadFile();
if (0 == hrResultAll)
m_pInternal->m_bComplete = true;
if (0 != hrResultAll)
{
m_bRunThread = FALSE;
return 0;
}
}
if (m_pInternal->m_func_onComplete)
m_pInternal->m_func_onComplete(hrResultAll);
m_pInternal->m_bComplete = true;
m_bRunThread = FALSE;
return 0;
}

View File

@ -44,6 +44,13 @@
#include "../../DesktopEditor/common/Directory.h"
#include "../include/ASCSVGWriter.h"
#include "../../Common/FileDownloader/FileDownloader.h"
void Download_OnComplete(int error)
{
int y = error;
return;
}
//#define RASTER_TEST
//#define METAFILE_TEST
@ -52,9 +59,17 @@
//#define TO_PDF
//#define TO_HTML_RENDERER
//#define ONLY_TEXT
//#define DOWNLOADER_TEST
int main(int argc, char *argv[])
{
#ifdef DOWNLOADER_TEST
CFileDownloader oDownloader(L"https://download.onlyoffice.com/assets/fb/fb_icon_325x325.jpg", false);
oDownloader.SetFilePath(L"D:\\111.jpg");
oDownloader.SetEvent_OnComplete(Download_OnComplete);
oDownloader.DownloadSync();
#endif
#ifdef RASTER_TEST
CBgraFrame oFrame;
oFrame.OpenFile(L"D:\\22.png");