Refacroring

This commit is contained in:
Oleg Korshul
2023-07-07 20:10:08 +03:00
parent 5ae7d4f5cb
commit 3ca59f7775
6 changed files with 429 additions and 30 deletions

View File

@ -1,12 +1,14 @@
HEADERS += \
$$PWD/include/FileTransporter.h \
$$PWD/include/manager.h \
$$PWD/src/FileTransporter_private.h \
$$PWD/src/transport_external.h \
$$PWD/src/Session.h
SOURCES += \
$$PWD/src/FileTransporter.cpp \
$$PWD/src/Session.cpp
$$PWD/src/Session.cpp \
$$PWD/src/manager.cpp
core_windows {
SOURCES += $$PWD/src/FileTransporter_win.cpp

View File

@ -0,0 +1,86 @@
/*
* (c) Copyright Ascensio System SIA 2010-2019
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#ifndef APPLICATION_DOWNLOAD_MANAGER_H
#define APPLICATION_DOWNLOAD_MANAGER_H
#include <string>
#include <functional>
#include "./FileTransporter.h"
namespace ASC
{
enum DownloadStatus
{
Error = 0,
Success = 1
};
class IDownloadTask
{
public:
IDownloadTask();
virtual ~IDownloadTask();
public:
virtual std::wstring GetPath() = 0;
virtual DownloadStatus GetStatus() = 0;
};
class CDownloadManager_private;
class CDownloadManager
{
public:
CDownloadManager();
~CDownloadManager();
public:
NSNetwork::NSFileTransport::CSession* GetSession();
void SetMaxConcurrentDownloadCount(const int& count);
void AddTask(const std::wstring& url,
const std::wstring& directory,
const std::wstring& filename,
void* observer,
std::function<void(IDownloadTask*)> handler);
void OnDestroyObserver(void* observer);
void Clear();
static bool DownloadExternal(const std::wstring& url, const std::wstring& path);
private:
CDownloadManager_private* m_internal;
};
}
#endif // APPLICATION_DOWNLOAD_MANAGER_H

View File

@ -40,7 +40,11 @@ namespace NSNetwork
namespace NSFileTransport
{
#ifdef _MAC
#ifdef _IOS
bool m_bIsARCEnabled = true;
#else
bool m_bIsARCEnabled = false;
#endif
void SetARCEnabled(const bool& enabled)
{

View File

@ -132,7 +132,7 @@ namespace NSNetwork
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
close(fp);
fclose(fp);
}
m_bComplete = (CURLE_OK == res);

View File

@ -146,41 +146,76 @@ namespace NSNetwork
#endif
NSString* stringURL = StringWToNSString(m_sDownloadFileUrl);
NSString *escapedURL = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:escapedURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSString *filePath = StringWToNSString ( m_sDownloadFilePath );
[urlData writeToFile:filePath atomically:YES];
NSString* escapedURL = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
#if defined(_IOS)
return 0;
#else
#ifndef _ASC_USE_ARC_
int nResult = 1;
if (m_pSession)
{
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[escapedURL]]];
__block NSData* result = nil;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[m_pSession->m_session dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse* response, NSError *error) {
if (error == nil)
result = data;
dispatch_semaphore_signal(sem);
}] resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
if (result)
{
NSString* filePath = StringWToNSString(m_sDownloadFilePath);
[urlData writeToFile:filePath atomically:YES];
nResult = 0;
}
nResult = 1;
return nResult;
}
else
{
NSURL* url = [NSURL URLWithString:escapedURL];
NSData* urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSString* filePath = StringWToNSString(m_sDownloadFilePath);
[urlData writeToFile:filePath atomically:YES];
#if defined(_IOS)
return 0;
#else
#ifndef _ASC_USE_ARC_
if (!GetARCEnabled())
{
[stringURL release];
//[url release];
[urlData release];
}
#endif
#endif
return 0;
}
#if defined(_IOS)
return 1;
#else
#ifndef _ASC_USE_ARC_
if (!GetARCEnabled())
{
[stringURL release];
//[url release];
[urlData release];
}
#endif
#endif
return 0;
#endif
#endif
return 1;
}
#if defined(_IOS)
return 1;
#else
#ifndef _ASC_USE_ARC_
if (!GetARCEnabled())
{
[stringURL release];
//[url release];
}
#endif
#endif
return 1;
}
virtual int UploadData() override

View File

@ -0,0 +1,272 @@
/*
* (c) Copyright Ascensio System SIA 2010-2019
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#include <list>
#include "./../include/manager.h"
#include "./../../../core/DesktopEditor/graphics/TemporaryCS.h"
#include "./../../../core/DesktopEditor/graphics/BaseThread.h"
namespace ASC
{
class CDownloadTask : public IDownloadTask, public NSThreads::CBaseThread
{
public:
std::wstring Directory;
std::wstring FileName;
std::wstring Url;
DownloadStatus Status;
int ID;
void* Observer;
std::function<void(CDownloadTask*)> Handler;
bool IsDeleted;
CDownloadManager_private* m_manager;
public:
CDownloadTask(CDownloadManager_private* manager) : IDownloadTask()
{
m_manager = manager;
IsDeleted = false;
Observer = nullptr;
Handler = nullptr;
}
virtual ~CDownloadTask()
{
}
virtual DWORD ThreadProc();
public:
std::wstring GetPath()
{
return Directory + L"/" + FileName;
}
DownloadStatus GetStatus()
{
return Status;
}
};
class CDownloadManager_private
{
public:
NSCriticalSection::CRITICAL_SECTION m_oCS;
NSNetwork::NSFileTransport::CSession m_oSession;
std::list<CDownloadTask*> m_arTasks;
std::list<CDownloadTask*> m_arWaitTasks;
int m_nMaxConcurrentDownloadCount;
int m_nTaskID;
int m_nPriority;
public:
CDownloadManager_private()
{
m_oCS.InitializeCriticalSection();
m_nMaxConcurrentDownloadCount = 5;
m_nTaskID = 0;
m_nPriority = 0;
}
~CDownloadManager_private()
{
Clear();
}
public:
IDownloadTask* Create(const std::wstring& url,
const std::wstring& directory,
const std::wstring& filename,
void* observer,
std::function<void(IDownloadTask*)> handler)
{
CTemporaryCS oCS(&m_oCS);
CDownloadTask* pTask = new CDownloadTask(this);
pTask->Url = url;
pTask->Directory = directory;
pTask->FileName = filename;
pTask->Observer = observer;
pTask->Handler = handler;
pTask->ID = m_nTaskID++;
if (m_nMaxConcurrentDownloadCount < (int)m_arTasks.size())
{
m_arTasks.push_back(pTask);
pTask->DestroyOnFinish();
pTask->Start(m_nPriority);
}
else
{
m_arWaitTasks.push_back(pTask);
}
return pTask;
}
void OnDownloadTask(CDownloadTask* task)
{
CTemporaryCS oCS(&m_oCS);
for (std::list<CDownloadTask*>::iterator iter = m_arTasks.begin(); iter != m_arTasks.end(); iter++)
{
if (task == (*iter))
{
m_arTasks.erase(iter);
break;
}
}
if (task->Handler)
task->Handler(task);
}
void OnDestroyObserver(void* observer)
{
CTemporaryCS oCS(&m_oCS);
for (std::list<CDownloadTask*>::iterator iter = m_arTasks.begin(); iter != m_arTasks.end(); iter++)
{
(*iter)->Handler = nullptr;
}
for (std::list<CDownloadTask*>::iterator iter = m_arWaitTasks.begin(); iter != m_arWaitTasks.end();)
{
CDownloadTask* pTask = *iter;
if (pTask->Observer == observer)
{
iter = m_arTasks.erase(iter);
delete pTask;
}
else
iter++;
}
}
void Clear()
{
CTemporaryCS oCS(&m_oCS);
for (std::list<CDownloadTask*>::iterator iter = m_arTasks.begin(); iter != m_arTasks.end(); iter++)
{
(*iter)->Handler = nullptr;
}
for (std::list<CDownloadTask*>::iterator iter = m_arWaitTasks.begin(); iter != m_arWaitTasks.end();)
{
CDownloadTask* pTask = *iter;
delete pTask;
}
m_arWaitTasks.clear();
}
};
DWORD CDownloadTask::ThreadProc()
{
NSNetwork::NSFileTransport::CFileDownloader oDownloader(Url, false);
oDownloader.SetSession(&m_manager->m_oSession);
oDownloader.SetFilePath(GetPath());
oDownloader.Start( 0 );
while ( oDownloader.IsRunned() )
{
NSThreads::Sleep( 10 );
}
Status = oDownloader.IsFileDownloaded() ? ASC::Success : ASC::Error;
m_manager->OnDownloadTask(this);
return 0;
}
// INTERFACE
IDownloadTask::IDownloadTask() {}
IDownloadTask::~IDownloadTask() {}
CDownloadManager::CDownloadManager()
{
m_internal = new CDownloadManager_private();
}
CDownloadManager::~CDownloadManager()
{
Clear();
delete m_internal;
}
NSNetwork::NSFileTransport::CSession* CDownloadManager::GetSession()
{
return &m_internal->m_oSession;
}
void CDownloadManager::SetMaxConcurrentDownloadCount(const int& count)
{
m_internal->m_nMaxConcurrentDownloadCount = count;
}
void CDownloadManager::AddTask(const std::wstring& url,
const std::wstring& directory,
const std::wstring& filename,
void* observer,
std::function<void(IDownloadTask*)> handler)
{
m_internal->Create(url, directory, filename, observer, handler);
}
void CDownloadManager::OnDestroyObserver(void* observer)
{
m_internal->OnDestroyObserver(observer);
}
void CDownloadManager::Clear()
{
m_internal->Clear();
}
bool DownloadExternal(const std::wstring& url, const std::wstring& path)
{
NSNetwork::NSFileTransport::CFileDownloader oDownloader(url, false);
NSNetwork::NSFileTransport::CSession oSession;
oDownloader.SetSession(&oSession);
oDownloader.SetFilePath(path);
oDownloader.Start( 0 );
while ( oDownloader.IsRunned() )
{
NSThreads::Sleep( 10 );
}
return oDownloader.IsFileDownloaded();
}
}