refacator: allow settings to expose a proxy and emit saving
This commit is contained in:
parent
c1af4c3b71
commit
5647121dd8
6 changed files with 33 additions and 7 deletions
|
|
@ -287,8 +287,7 @@ void MainWindow::connectSlots()
|
|||
|
||||
connect(this, &MainWindow::shown, this, &MainWindow::firstShown, Qt::QueuedConnection);
|
||||
|
||||
connect(&m_configScopes, &ConfigScopes::saving, this, &MainWindow::configScopesSaving, Qt::DirectConnection);
|
||||
|
||||
connect(Settings::instance(), &Settings::serverSettingsChanged, this, &MainWindow::serverConfigSaving);
|
||||
connect(Settings::instance(), &Settings::settingsChanged, this, &MainWindow::settingsChanged);
|
||||
|
||||
connect(&m_coreProcess, &CoreProcess::starting, this, &MainWindow::coreProcessStarting, Qt::DirectConnection);
|
||||
|
|
@ -398,7 +397,7 @@ void MainWindow::settingsChanged(const QString &key)
|
|||
}
|
||||
}
|
||||
|
||||
void MainWindow::configScopesSaving()
|
||||
void MainWindow::serverConfigSaving()
|
||||
{
|
||||
m_serverConfig.commit();
|
||||
}
|
||||
|
|
@ -502,7 +501,7 @@ void MainWindow::openSettings()
|
|||
auto dialog = SettingsDialog(this, m_serverConfig, m_coreProcess);
|
||||
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
m_configScopes.save();
|
||||
Settings::save();
|
||||
|
||||
applyConfig();
|
||||
|
||||
|
|
@ -566,7 +565,7 @@ void MainWindow::coreModeToggled()
|
|||
const auto coreMode = serverMode ? Settings::CoreMode::Server : Settings::CoreMode::Client;
|
||||
Settings::setValue(Settings::Core::CoreMode, coreMode);
|
||||
|
||||
m_configScopes.save();
|
||||
Settings::save();
|
||||
updateModeControls(serverMode);
|
||||
}
|
||||
|
||||
|
|
@ -728,7 +727,7 @@ void MainWindow::saveSettings()
|
|||
Settings::setValue(Settings::Core::CoreMode, Settings::CoreMode::Server);
|
||||
}
|
||||
Settings::setValue(Settings::Client::RemoteHost, ui->lineHostname->text());
|
||||
m_configScopes.save();
|
||||
Settings::save();
|
||||
}
|
||||
|
||||
void MainWindow::setIcon()
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ private:
|
|||
void firstShown();
|
||||
|
||||
void settingsChanged(const QString &key = QString());
|
||||
void configScopesSaving();
|
||||
void serverConfigSaving();
|
||||
void coreProcessStarting();
|
||||
void coreProcessError(CoreProcess::Error error);
|
||||
void coreConnectionStateChanged(CoreProcess::ConnectionState state);
|
||||
|
|
|
|||
|
|
@ -28,6 +28,11 @@ void QSettingsProxy::loadUser()
|
|||
m_pSettings = std::make_unique<QSettings>(Settings::UserSettingFile, QSettings::IniFormat);
|
||||
}
|
||||
|
||||
void QSettingsProxy::load(const QString &fileName)
|
||||
{
|
||||
m_pSettings = std::make_unique<QSettings>(fileName, QSettings::IniFormat);
|
||||
}
|
||||
|
||||
void QSettingsProxy::loadSystem()
|
||||
{
|
||||
m_pSettings = std::make_unique<QSettings>(Settings::SystemSettingFile, QSettings::IniFormat);
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ public:
|
|||
|
||||
virtual void loadUser();
|
||||
virtual void loadSystem();
|
||||
virtual void load(const QString &fileName);
|
||||
virtual void clear()
|
||||
{
|
||||
m_pSettings->clear();
|
||||
|
|
|
|||
|
|
@ -26,17 +26,21 @@ void Settings::setSettingFile(const QString &settingsFile)
|
|||
if (instance()->m_settings)
|
||||
instance()->m_settings->deleteLater();
|
||||
instance()->m_settings = new QSettings(instance()->m_portableSettingsFile, QSettings::IniFormat);
|
||||
instance()->m_settingsProxy->load(instance()->m_portableSettingsFile);
|
||||
qInfo().noquote() << "settings file:" << instance()->m_settings->fileName();
|
||||
}
|
||||
|
||||
Settings::Settings(QObject *parent) : QObject(parent)
|
||||
{
|
||||
m_settingsProxy = std::make_shared<QSettingsProxy>();
|
||||
if (QFile(m_portableSettingsFile).exists()) {
|
||||
m_settings = new QSettings(m_portableSettingsFile, QSettings::IniFormat);
|
||||
m_settingsProxy->load(m_portableSettingsFile);
|
||||
qInfo().noquote() << "settings file:" << m_settings->fileName();
|
||||
return;
|
||||
}
|
||||
initSettings();
|
||||
m_settingsProxy->load(m_settings->fileName());
|
||||
}
|
||||
|
||||
bool Settings::isPortableSettings()
|
||||
|
|
@ -125,6 +129,18 @@ const QString Settings::logLevelText()
|
|||
return instance()->m_logLevels.at(instance()->value(Log::Level).toInt());
|
||||
}
|
||||
|
||||
QSettingsProxy &Settings::proxy()
|
||||
{
|
||||
return *instance()->m_settingsProxy;
|
||||
}
|
||||
|
||||
void Settings::save(bool emitSaving)
|
||||
{
|
||||
if (emitSaving)
|
||||
Q_EMIT instance()->serverSettingsChanged();
|
||||
instance()->m_settings->sync();
|
||||
}
|
||||
|
||||
bool Settings::isWritable()
|
||||
{
|
||||
return instance()->m_settings->isWritable();
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include <QDir>
|
||||
|
||||
#include "common/QSettingsProxy.h"
|
||||
#include "common/constants.h"
|
||||
|
||||
class Settings : public QObject
|
||||
|
|
@ -137,11 +138,14 @@ public:
|
|||
static const QString settingsFile();
|
||||
static const QString settingsPath();
|
||||
static const QString logLevelText();
|
||||
static QSettingsProxy &proxy();
|
||||
static void save(bool emitSaving = true);
|
||||
|
||||
signals:
|
||||
void scopeChanged(bool isSystemScope);
|
||||
void writableChanged(bool canWrite);
|
||||
void settingsChanged(const QString key);
|
||||
void serverSettingsChanged();
|
||||
|
||||
private:
|
||||
explicit Settings(QObject *parent = nullptr);
|
||||
|
|
@ -154,6 +158,7 @@ private:
|
|||
|
||||
QSettings *m_settings = nullptr;
|
||||
QString m_portableSettingsFile = QStringLiteral("settings/%1.conf").arg(kAppName);
|
||||
std::shared_ptr<QSettingsProxy> m_settingsProxy;
|
||||
|
||||
// clang-format off
|
||||
inline static const QStringList m_logLevels = {
|
||||
|
|
|
|||
Loading…
Reference in a new issue