CMake
This commit is contained in:
34
include/siadrive_api/siadriveconfig.h
Normal file
34
include/siadrive_api/siadriveconfig.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <siacommon.h>
|
||||
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
class SIADRIVE_EXPORTABLE CSiaDriveConfig
|
||||
{
|
||||
public:
|
||||
CSiaDriveConfig();
|
||||
|
||||
CSiaDriveConfig(const SString& filePath);
|
||||
|
||||
public:
|
||||
~CSiaDriveConfig();
|
||||
|
||||
Property(SString, FilePath, public, private)
|
||||
JProperty(std::uint8_t, UI_Main_TabIndex, public, private, _configDocument)
|
||||
JProperty(std::string, Renter_UploadDbFilePath, public, private, _configDocument)
|
||||
JProperty(std::string, TempFolder, public, private, _configDocument)
|
||||
JProperty(std::string, CacheFolder, public, public, _configDocument)
|
||||
|
||||
private:
|
||||
json _configDocument;
|
||||
|
||||
private:
|
||||
void LoadDefaults();
|
||||
|
||||
void Load( );
|
||||
|
||||
void Save() const;
|
||||
};
|
||||
|
||||
NS_END(2)
|
81
src/siadrive_api/siadriveconfig.cpp
Normal file
81
src/siadrive_api/siadriveconfig.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <siadriveconfig.h>
|
||||
#include <fstream>
|
||||
using namespace Sia::Api;
|
||||
|
||||
CSiaDriveConfig::CSiaDriveConfig() :
|
||||
CSiaDriveConfig(DEFAULT_CONFIG_FILE_PATH)
|
||||
{
|
||||
}
|
||||
|
||||
CSiaDriveConfig::CSiaDriveConfig(const SString& filePath) :
|
||||
_FilePath(filePath)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
::DeleteFile(filePath.c_str());
|
||||
#endif
|
||||
Load();
|
||||
}
|
||||
|
||||
CSiaDriveConfig::~CSiaDriveConfig()
|
||||
{
|
||||
Save();
|
||||
}
|
||||
|
||||
void CSiaDriveConfig::LoadDefaults()
|
||||
{
|
||||
SetUI_Main_TabIndex(0);
|
||||
SetRenter_UploadDbFilePath("./config/renter_upload.db3");
|
||||
|
||||
SString tempFolder;
|
||||
tempFolder.Resize(MAX_PATH + 1);
|
||||
::GetTempPath(MAX_PATH, &tempFolder[0]);
|
||||
SetTempFolder(tempFolder);
|
||||
|
||||
PWSTR localAppData = nullptr;
|
||||
::SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &localAppData);
|
||||
|
||||
SString sdFolder;
|
||||
sdFolder.Resize(MAX_PATH + 1);
|
||||
::PathCombine(&sdFolder[0], localAppData, L"SiaDrive");
|
||||
::CreateDirectory(sdFolder.str().c_str(), nullptr);
|
||||
|
||||
SString cacheFolder;
|
||||
cacheFolder.Resize(MAX_PATH + 1);
|
||||
::PathCombine(&cacheFolder[0], &sdFolder[0], L"Cache");
|
||||
::CreateDirectory(cacheFolder.str().c_str(), nullptr);
|
||||
SetCacheFolder(cacheFolder);
|
||||
}
|
||||
|
||||
void CSiaDriveConfig::Load( )
|
||||
{
|
||||
CFile f;
|
||||
if (f.Open(GetFilePath().c_str(), CFile::modeRead))
|
||||
{
|
||||
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());
|
||||
}
|
||||
else
|
||||
{
|
||||
LoadDefaults();
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
void CSiaDriveConfig::Save() const
|
||||
{
|
||||
SString folder = GetFilePath();
|
||||
::PathRemoveFileSpec(&folder[0]);
|
||||
|
||||
if (!::PathIsDirectory(folder.str().c_str()))
|
||||
{
|
||||
::CreateDirectory(folder.str().c_str(), nullptr);
|
||||
}
|
||||
|
||||
std::ofstream(SString::ToUtf8(GetFilePath()).c_str()) << std::setw(2) << _configDocument << std::endl;
|
||||
}
|
Reference in New Issue
Block a user