添加设置UI

This commit is contained in:
RigoLigoRLC
2022-01-27 21:41:33 +08:00
parent 02530de7da
commit aa22cd7ff2
22 changed files with 943 additions and 43 deletions
+99
View File
@@ -0,0 +1,99 @@
#include "spkconfig.h"
SpkConfig::SpkConfig(QObject *parent, QString configPath) :
QObject(parent),
mSettings(configPath, QSettings::IniFormat, this)
{
}
SpkConfig::~SpkConfig()
{
// TODO
}
bool SpkConfig::BindField(QString key, QString *value, QString defaultValue, std::function<bool(void)> callback)
{
if(mStringBindMap.contains(key))
return false;
*value = mSettings.value(key, defaultValue).toString();
mStringBindMap[key] = QPair<QString*, std::function<bool(void)>>(value, callback);
return true;
}
bool SpkConfig::BindField(QString key, int *value, int defaultValue, std::function<bool(void)> callback)
{
if(mIntBindMap.contains(key))
return false;
*value = mSettings.value(key, defaultValue).toInt(); // Read inital value
mIntBindMap[key] = QPair<int*, std::function<bool(void)>>(value, callback);
return true;
}
bool SpkConfig::SetField(QString key, QString value)
{
auto it = mStringBindMap.find(key);
if(it == mStringBindMap.end())
return false;
else
{
if(it->second) // Has a valid callback?
{
QString originalValue = *it->first; // Backup first in case of a failure
*it->first = value; // Set the target
if(!it->second()) // Failure, restore and give up
{
*it->first = originalValue;
return false;
}
else // Success, set the mSettings and continue
{
mSettings.setValue(key, value);
return true;
}
}
// No valid callback, normal operations
*it->first = value;
mSettings.setValue(key, value);
return true;
}
}
bool SpkConfig::SetField(QString key, int value)
{
auto it = mIntBindMap.find(key);
if(it == mIntBindMap.end())
return false;
else
{
if(it->second) // Has a valid callback?
{
int originalValue = *it->first; // Backup first in case of a failure
*it->first = value; // Set the target
if(!it->second()) // Failure, restore and give up
{
*it->first = originalValue;
return false;
}
else // Success, set the mSettings and continue
{
mSettings.setValue(key, value);
return true;
}
}
// No valid callback, normal operations
*it->first = value;
mSettings.setValue(key, value);
return true;
}
}
QVariant SpkConfig::ReadField(QString key, QVariant defaultValue)
{
return mSettings.value(key, defaultValue);
}
+23 -8
View File
@@ -8,20 +8,25 @@
SpkDownloadMgr::SpkDownloadMgr(QObject *parent)
{
mDestFolder = CFG->value("dirs/download", "%1/.local/spark-store/downloads")
.toString().arg(QDir::homePath());
CFG->BindField("dirs/download", &mDestFolder,
QString("*/.local/spark-store/downloads"));
mDestFolder.replace('*', QDir::homePath());
QDir dest(mDestFolder);
if(!dest.exists())
QDir().mkdir(mDestFolder);
// Distribution servers
QString srvPaths = CFG->value("download/servers", "https://d1.store.deepinos.org.cn/;;"
"https://d2.store.deepinos.org.cn/;;"
"https://d3.store.deepinos.org.cn/;;"
"https://d4.store.deepinos.org.cn/;;"
"https://d5.store.deepinos.org.cn/").toString();
mServers = srvPaths.split(";;");
CFG->BindField("download/servers", &mBulkServerPaths,
"https://d1.store.deepinos.org.cn/;;"
"https://d2.store.deepinos.org.cn/;;"
"https://d3.store.deepinos.org.cn/;;"
"https://d4.store.deepinos.org.cn/;;"
"https://d5.store.deepinos.org.cn/",
std::bind(&SpkDownloadMgr::ServerAddressesChangedCallback, this));
mServers = mBulkServerPaths.split(";;");
mCurrentDownloadId = -1;
mActiveWorkerCount = 0;
@@ -333,3 +338,13 @@ void SpkDownloadMgr::TryScheduleFailureRetries(int i)
LinkReplyWithMe(mScheduledWorkers[i].Reply);
}
}
bool SpkDownloadMgr::ServerAddressesChangedCallback()
{
if(mCurrentDownloadId != -1)
return false;
// URL format verification *is done in the GUI*, we jsut have to split it here
mServers = mBulkServerPaths.split(";;");
return true;
}
+4 -3
View File
@@ -12,14 +12,15 @@ SpkResource* SpkResource::Instance = nullptr;
// clazy:excludeall=container-anti-pattern
SpkResource::SpkResource(QObject *parent) : QObject(parent),
mMaximumConcurrent(CFG->value("resource/concurrent", 5).toInt()),
mCacheDirectory(CFG->value("dirs/cache", "%1/.cache/spark-store/res/")
mMaximumConcurrent(CFG->ReadField("resource/concurrent", 5).toInt()),
mCacheDirectory(CFG->ReadField("dirs/cache", "*/.cache/spark-store/res/")
.toString()
.arg(QDir::homePath()))
.replace('*', QDir::homePath()))
{
Q_ASSERT(!Instance);
qRegisterMetaType<ResourceResult>();
Instance = this;
mRequestSemaphore = new QSemaphore(mMaximumConcurrent);
QString path = mCacheDirectory.section('/', 1, -2, QString::SectionIncludeLeadingSep);
+6 -7
View File
@@ -26,12 +26,11 @@ SpkStore::SpkStore(bool aCli, QString &aLogPath)
// Finish all essential initialization after this.
mConfigPath = QDir::homePath() + "/.config/spark-store/config"; //TODO: flexible config via CLI
if(QFileInfo(mConfigPath).exists())
mCfg = new QSettings(QDir::homePath() + "/.config/spark-store/config", QSettings::IniFormat,
this);
if(QFileInfo::exists(mConfigPath))
mCfg = new SpkConfig(this, QDir::homePath() + "/.config/spark-store/config");
else
{
mCfg = new QSettings(":/info/default_config", QSettings::IniFormat, this);
mCfg = new SpkConfig(this, ":/info/default_config");
#if 0
bool cfgDirOk;
if(!qgetenv("SPARK_NO_INSTALL_CONFIG").toInt())
@@ -59,9 +58,8 @@ SpkStore::SpkStore(bool aCli, QString &aLogPath)
mDistroName = SpkUtils::GetDistroName();
// Initialize URL
mApiRequestUrl = mCfg->value("url/api", "https://store.deepinos.org/api/").toString();
mResourceRequestUrl = mCfg->value("url/res", "http://img.store.deepinos.org.cn/").toString();
mCfg->BindField("url/api", &mApiRequestUrl, "https://store.deepinos.org/api/");
mCfg->BindField("url/res", &mResourceRequestUrl, "http://img.store.deepinos.org.cn/");
mUserAgentStr = QString("Spark-Store/%1 Distro/%2")
.arg(GitVer::DescribeTags())
@@ -74,6 +72,7 @@ SpkStore::SpkStore(bool aCli, QString &aLogPath)
// UI Initialization
mResMgr = new SpkResource(this); // Resource manager must be created before the windows
SpkUi::Initialize();
SpkUi::SpkUiMetaObject.SetAccentColor(QColor(200,100,0));
mMainWindow = new SpkMainWindow;
SpkUi::Popup = new SpkUi::SpkPopup(mMainWindow);
+6
View File
@@ -3,6 +3,7 @@
#include <QJsonObject>
#include <QJsonDocument>
#include <QDir>
#include <QWidget>
#include "spkutils.h"
void SpkUtils::VerifySingleRequest(QPointer<QNetworkReply> aReply)
@@ -110,3 +111,8 @@ bool SpkUtils::EnsureDirExists(QString path)
return true;
}
void SpkUtils::FillWidget(QWidget *widget, QVariant val)
{
}