CMake
This commit is contained in:
227
include/siadrive_api/siaapi.h
Normal file
227
include/siadrive_api/siaapi.h
Normal file
@@ -0,0 +1,227 @@
|
||||
#ifndef _SIAAPI_H
|
||||
#define _SIAAPI_H
|
||||
|
||||
#include <siacommon.h>
|
||||
#include <siacurl.h>
|
||||
#include <autothread.h>
|
||||
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
class CSiaDriveConfig;
|
||||
class SIADRIVE_EXPORTABLE CSiaBase
|
||||
{
|
||||
public:
|
||||
explicit CSiaBase(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) :
|
||||
_siaCurl(siaCurl),
|
||||
_siaDriveConfig(siaDriveConfig)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual ~CSiaBase() = 0
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
const CSiaCurl& _siaCurl;
|
||||
CSiaDriveConfig* _siaDriveConfig;
|
||||
|
||||
protected:
|
||||
inline const CSiaCurl& GetSiaCurl() const
|
||||
{
|
||||
return _siaCurl;
|
||||
}
|
||||
|
||||
inline CSiaDriveConfig& GetSiaDriveConfig() const
|
||||
{
|
||||
return *_siaDriveConfig;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class SIADRIVE_EXPORTABLE CSiaApi
|
||||
{
|
||||
public:
|
||||
enum class _SiaApiError
|
||||
{
|
||||
Success,
|
||||
NotImplemented,
|
||||
RequestError,
|
||||
WalletExists,
|
||||
WalletLocked,
|
||||
WalletUnlocked,
|
||||
WalletNotCreated
|
||||
};
|
||||
|
||||
enum class _SiaSeedLanguage
|
||||
{
|
||||
English,
|
||||
German,
|
||||
Japanese
|
||||
};
|
||||
|
||||
class _CSiaFileTree;
|
||||
class SIADRIVE_EXPORTABLE _CSiaFile :
|
||||
public virtual CSiaBase
|
||||
{
|
||||
friend CSiaApi;
|
||||
friend _CSiaFileTree;
|
||||
|
||||
private:
|
||||
explicit _CSiaFile(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig, const json& fileJson);
|
||||
|
||||
public:
|
||||
virtual ~_CSiaFile();
|
||||
|
||||
// Properties
|
||||
Property(SString, SiaPath, public, private)
|
||||
Property(std::uint64_t, FileSize, public, private)
|
||||
Property(bool, Available, public, private)
|
||||
Property(bool, Renewing, public, private)
|
||||
Property(std::uint32_t, Redundancy, public, private)
|
||||
Property(std::uint32_t, UploadProgress, public, private)
|
||||
Property(std::uint32_t, Expiration, public, private)
|
||||
};
|
||||
|
||||
class SIADRIVE_EXPORTABLE _CSiaFileTree :
|
||||
public virtual CSiaBase
|
||||
{
|
||||
friend CSiaApi;
|
||||
public:
|
||||
explicit _CSiaFileTree(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
||||
|
||||
public:
|
||||
virtual ~_CSiaFileTree();
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<_CSiaFile>> _fileList;
|
||||
|
||||
public:
|
||||
void BuildTree(const json& result);
|
||||
|
||||
std::vector<std::shared_ptr<_CSiaFile>> GetFileList() const;
|
||||
|
||||
std::vector<std::shared_ptr<_CSiaFile>> Query(SString query) const;
|
||||
|
||||
std::shared_ptr<_CSiaFile> GetFile(const SString& siaPath) const;
|
||||
|
||||
std::vector<SString> QueryDirectories(SString query) const;
|
||||
|
||||
bool FileExists(const SString& siaPath) const;
|
||||
};
|
||||
|
||||
class SIADRIVE_EXPORTABLE _CSiaWallet :
|
||||
public virtual CSiaBase
|
||||
{
|
||||
friend CSiaApi;
|
||||
private:
|
||||
explicit _CSiaWallet(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
||||
|
||||
public:
|
||||
virtual ~_CSiaWallet();
|
||||
|
||||
// Properties
|
||||
Property(bool, Created, public, private)
|
||||
Property(bool, Locked, public, private)
|
||||
|
||||
public:
|
||||
_SiaApiError Create(const _SiaSeedLanguage& seedLanguage, SString& seed);
|
||||
bool Refresh();
|
||||
_SiaApiError Restore(const SString& seed);
|
||||
_SiaApiError Lock();
|
||||
_SiaApiError Unlock(const SString& password);
|
||||
_SiaApiError GetConfirmedBalance(SiaCurrency& balance) const;
|
||||
_SiaApiError GetUnonfirmedBalance(SiaCurrency& balance) const;
|
||||
_SiaApiError GetAddress(SString& address) const;
|
||||
};
|
||||
|
||||
class SIADRIVE_EXPORTABLE _CSiaRenter :
|
||||
public virtual CSiaBase,
|
||||
public virtual CAutoThread
|
||||
{
|
||||
friend CSiaApi;
|
||||
|
||||
private:
|
||||
explicit _CSiaRenter(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
||||
|
||||
public:
|
||||
virtual ~_CSiaRenter();
|
||||
|
||||
Property(SiaCurrency, Funds, public, private)
|
||||
Property(std::uint64_t, Hosts, public, private)
|
||||
Property(SiaCurrency, Unspent, public, private)
|
||||
Property(std::uint64_t, TotalUsedBytes, public, private)
|
||||
Property(std::uint32_t, TotalUploadProgress, public, private)
|
||||
|
||||
protected:
|
||||
virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) override;
|
||||
|
||||
public:
|
||||
_SiaApiError FileExists(const SString& siaPath, bool& exists) const;
|
||||
_SiaApiError DownloadFile(const SString& siaPath, const SString& location) const;
|
||||
_SiaApiError GetFileTree(std::shared_ptr<_CSiaFileTree>& siaFileTree) const;
|
||||
};
|
||||
|
||||
class SIADRIVE_EXPORTABLE _CSiaConsensus :
|
||||
public virtual CSiaBase,
|
||||
public virtual CAutoThread
|
||||
{
|
||||
friend CSiaApi;
|
||||
|
||||
private:
|
||||
explicit _CSiaConsensus(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
||||
|
||||
public:
|
||||
virtual ~_CSiaConsensus();
|
||||
|
||||
// Properties
|
||||
Property(std::uint64_t, Height, public, private)
|
||||
Property(bool, Synced, public, private)
|
||||
Property(SString, CurrentBlock, public, private)
|
||||
|
||||
protected:
|
||||
virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) override;
|
||||
};
|
||||
|
||||
public:
|
||||
explicit CSiaApi(const SiaHostConfig& hostConfig, CSiaDriveConfig* siaDriveConfig);
|
||||
|
||||
public:
|
||||
~CSiaApi();
|
||||
|
||||
private:
|
||||
SiaHostConfig _hostConfig;
|
||||
CSiaCurl _siaCurl;
|
||||
CSiaDriveConfig* _siaDriveConfig;
|
||||
std::shared_ptr<_CSiaWallet> _wallet;
|
||||
std::shared_ptr<_CSiaRenter> _renter;
|
||||
std::shared_ptr<_CSiaConsensus> _consensus;
|
||||
|
||||
public:
|
||||
static SString FormatToSiaPath(SString path);
|
||||
|
||||
public:
|
||||
std::shared_ptr<_CSiaWallet> GetWallet() const;
|
||||
std::shared_ptr<_CSiaRenter> GetRenter() const;
|
||||
std::shared_ptr<_CSiaConsensus> GetConsensus() const;
|
||||
SString GetServerVersion() const;
|
||||
SiaHostConfig GetHostConfig() const;
|
||||
};
|
||||
|
||||
typedef CSiaApi::_SiaApiError SiaApiError;
|
||||
typedef CSiaApi::_SiaSeedLanguage SiaSeedLanguage;
|
||||
typedef CSiaApi::_CSiaWallet CSiaWallet;
|
||||
typedef CSiaApi::_CSiaRenter CSiaRenter;
|
||||
typedef CSiaApi::_CSiaConsensus CSiaConsensus;
|
||||
typedef std::shared_ptr<CSiaWallet> CSiaWalletPtr;
|
||||
typedef std::shared_ptr<CSiaRenter> CSiaRenterPtr;
|
||||
typedef std::shared_ptr<CSiaConsensus> CSiaConsensusPtr;
|
||||
typedef CSiaApi::_CSiaFile CSiaFile;
|
||||
typedef std::shared_ptr<CSiaFile> CSiaFilePtr;
|
||||
typedef std::vector<CSiaFilePtr> CSiaFileCollection;
|
||||
typedef CSiaApi::_CSiaFileTree CSiaFileTree;
|
||||
typedef std::shared_ptr<CSiaFileTree> CSiaFileTreePtr;
|
||||
|
||||
NS_END(2)
|
||||
#endif //_SIAAPI_H
|
68
src/siadrive_api/siaapi.cpp
Normal file
68
src/siadrive_api/siaapi.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <siaapi.h>
|
||||
#include <regex>
|
||||
#include <siadriveconfig.h>
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaApi::CSiaApi(const SiaHostConfig& hostConfig, CSiaDriveConfig* siaDriveConfig) :
|
||||
_hostConfig(hostConfig),
|
||||
_siaCurl(hostConfig),
|
||||
_siaDriveConfig(siaDriveConfig),
|
||||
_wallet(new CSiaWallet(_siaCurl, siaDriveConfig)),
|
||||
_renter(new CSiaRenter(_siaCurl, siaDriveConfig)),
|
||||
_consensus(new CSiaConsensus(_siaCurl, siaDriveConfig))
|
||||
{
|
||||
}
|
||||
|
||||
CSiaApi::~CSiaApi()
|
||||
{
|
||||
//TODO Make this an option to lock on exit
|
||||
//_wallet->Lock();
|
||||
}
|
||||
|
||||
SString CSiaApi::FormatToSiaPath(SString path)
|
||||
{
|
||||
if (path.Length())
|
||||
{
|
||||
std::replace(path.begin(), path.end(), '\\', '/');
|
||||
std::wregex r(L"/+");
|
||||
path = std::regex_replace(path.str(), r, L"/");
|
||||
|
||||
while (path[0] == '/')
|
||||
{
|
||||
path = path.SubString(1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
path = L"/";
|
||||
}
|
||||
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
SString CSiaApi::GetServerVersion() const
|
||||
{
|
||||
return _siaCurl.GetServerVersion();
|
||||
}
|
||||
|
||||
CSiaWalletPtr CSiaApi::GetWallet() const
|
||||
{
|
||||
return _wallet;
|
||||
}
|
||||
|
||||
CSiaRenterPtr CSiaApi::GetRenter() const
|
||||
{
|
||||
return _renter;
|
||||
}
|
||||
|
||||
CSiaConsensusPtr CSiaApi::GetConsensus() const
|
||||
{
|
||||
return _consensus;
|
||||
}
|
||||
|
||||
SiaHostConfig CSiaApi::GetHostConfig() const
|
||||
{
|
||||
return _hostConfig;
|
||||
}
|
@@ -1,5 +1,9 @@
|
||||
#include <siadriveconfig.h>
|
||||
#include <fstream>
|
||||
#ifdef _WIN32
|
||||
#include <Shlobj.h>
|
||||
#endif
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaDriveConfig::CSiaDriveConfig() :
|
||||
@@ -10,8 +14,10 @@ CSiaDriveConfig::CSiaDriveConfig() :
|
||||
CSiaDriveConfig::CSiaDriveConfig(const SString& filePath) :
|
||||
_FilePath(filePath)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
#ifdef _DEBUG
|
||||
::DeleteFile(filePath.c_str());
|
||||
::DeleteFile(filePath.str().c_str());
|
||||
#endif
|
||||
#endif
|
||||
Load();
|
||||
}
|
||||
@@ -23,6 +29,7 @@ CSiaDriveConfig::~CSiaDriveConfig()
|
||||
|
||||
void CSiaDriveConfig::LoadDefaults()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SetUI_Main_TabIndex(0);
|
||||
SetRenter_UploadDbFilePath("./config/renter_upload.db3");
|
||||
|
||||
@@ -44,21 +51,21 @@ void CSiaDriveConfig::LoadDefaults()
|
||||
::PathCombine(&cacheFolder[0], &sdFolder[0], L"Cache");
|
||||
::CreateDirectory(cacheFolder.str().c_str(), nullptr);
|
||||
SetCacheFolder(cacheFolder);
|
||||
#else
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void CSiaDriveConfig::Load( )
|
||||
{
|
||||
CFile f;
|
||||
if (f.Open(GetFilePath().c_str(), CFile::modeRead))
|
||||
std::ifstream myfile(GetFilePath().str().c_str());
|
||||
if (myfile.is_open())
|
||||
{
|
||||
SString s;
|
||||
s.Resize(static_cast<std::size_t>(f.GetLength()));
|
||||
|
||||
f.Read(&s[0], static_cast<UINT>(s.Length()));
|
||||
|
||||
f.Close();
|
||||
|
||||
_configDocument = json::parse(s.begin(), s.end());
|
||||
std::stringstream ss;
|
||||
ss << myfile.rdbuf();
|
||||
std::string jsonTxt = ss.str();
|
||||
_configDocument = json::parse(jsonTxt.begin(), jsonTxt.end());
|
||||
myfile.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -69,6 +76,7 @@ void CSiaDriveConfig::Load( )
|
||||
|
||||
void CSiaDriveConfig::Save() const
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SString folder = GetFilePath();
|
||||
::PathRemoveFileSpec(&folder[0]);
|
||||
|
||||
@@ -76,6 +84,8 @@ void CSiaDriveConfig::Save() const
|
||||
{
|
||||
::CreateDirectory(folder.str().c_str(), nullptr);
|
||||
}
|
||||
#else
|
||||
|
||||
#endif
|
||||
std::ofstream(SString::ToUtf8(GetFilePath()).c_str()) << std::setw(2) << _configDocument << std::endl;
|
||||
}
|
Reference in New Issue
Block a user