Migrate away from low-level SettingsStorage class

Also add `QFlags<T>` support to `SettingsStorage`.
PR #15693.
This commit is contained in:
Chocobo1
2021-11-08 13:23:33 +08:00
committed by GitHub
parent e33c4086b9
commit 32698fe0be
16 changed files with 226 additions and 243 deletions

View File

@@ -33,16 +33,13 @@
#include <QDebug>
#include "base/logger.h"
#include "base/settingsstorage.h"
const QString KEY_ENABLED = QStringLiteral("Network/PortForwardingEnabled");
PortForwarderImpl::PortForwarderImpl(lt::session *provider, QObject *parent)
: Net::PortForwarder {parent}
, m_active {SettingsStorage::instance()->loadValue(KEY_ENABLED, true)}
, m_storeActive {"Network/PortForwardingEnabled", true}
, m_provider {provider}
{
if (m_active)
if (isEnabled())
start();
}
@@ -53,20 +50,19 @@ PortForwarderImpl::~PortForwarderImpl()
bool PortForwarderImpl::isEnabled() const
{
return m_active;
return m_storeActive;
}
void PortForwarderImpl::setEnabled(const bool enabled)
{
if (m_active != enabled)
if (m_storeActive != enabled)
{
if (enabled)
start();
else
stop();
m_active = enabled;
SettingsStorage::instance()->storeValue(KEY_ENABLED, enabled);
m_storeActive = enabled;
}
}

View File

@@ -36,6 +36,7 @@
#include <QHash>
#include "base/net/portforwarder.h"
#include "base/settingvalue.h"
class PortForwarderImpl final : public Net::PortForwarder
{
@@ -56,7 +57,7 @@ private:
void start();
void stop();
bool m_active;
CachedSettingValue<bool> m_storeActive;
lt::session *m_provider;
QHash<quint16, std::vector<lt::port_mapping_t>> m_mappedPorts;
};

View File

@@ -28,20 +28,7 @@
#include "proxyconfigurationmanager.h"
#include "base/settingsstorage.h"
#define SETTINGS_KEY(name) QStringLiteral("Network/Proxy/" name)
const QString KEY_ONLY_FOR_TORRENTS = SETTINGS_KEY("OnlyForTorrents");
const QString KEY_TYPE = SETTINGS_KEY("Type");
const QString KEY_IP = SETTINGS_KEY("IP");
const QString KEY_PORT = SETTINGS_KEY("Port");
const QString KEY_USERNAME = SETTINGS_KEY("Username");
const QString KEY_PASSWORD = SETTINGS_KEY("Password");
namespace
{
inline SettingsStorage *settings() { return SettingsStorage::instance(); }
}
#define SETTINGS_KEY(name) ("Network/Proxy/" name)
bool Net::operator==(const ProxyConfiguration &left, const ProxyConfiguration &right)
{
@@ -62,17 +49,21 @@ using namespace Net;
ProxyConfigurationManager *ProxyConfigurationManager::m_instance = nullptr;
ProxyConfigurationManager::ProxyConfigurationManager(QObject *parent)
: QObject(parent)
: QObject {parent}
, m_storeProxyOnlyForTorrents {SETTINGS_KEY("OnlyForTorrents")}
, m_storeProxyType {SETTINGS_KEY("Type")}
, m_storeProxyIP {SETTINGS_KEY("IP")}
, m_storeProxyPort {SETTINGS_KEY("Port")}
, m_storeProxyUsername {SETTINGS_KEY("Username")}
, m_storeProxyPassword {SETTINGS_KEY("Password")}
{
m_isProxyOnlyForTorrents = settings()->loadValue(KEY_ONLY_FOR_TORRENTS, false);
m_config.type = static_cast<ProxyType>(
settings()->loadValue(KEY_TYPE, static_cast<int>(ProxyType::None)));
m_config.type = m_storeProxyType.get(ProxyType::None);
if ((m_config.type < ProxyType::None) || (m_config.type > ProxyType::SOCKS4))
m_config.type = ProxyType::None;
m_config.ip = settings()->loadValue<QString>(KEY_IP, "0.0.0.0");
m_config.port = settings()->loadValue<ushort>(KEY_PORT, 8080);
m_config.username = settings()->loadValue<QString>(KEY_USERNAME);
m_config.password = settings()->loadValue<QString>(KEY_PASSWORD);
m_config.ip = m_storeProxyIP.get(QLatin1String("0.0.0.0"));
m_config.port = m_storeProxyPort.get(8080);
m_config.username = m_storeProxyUsername;
m_config.password = m_storeProxyPassword;
configureProxy();
}
@@ -100,14 +91,14 @@ ProxyConfiguration ProxyConfigurationManager::proxyConfiguration() const
void ProxyConfigurationManager::setProxyConfiguration(const ProxyConfiguration &config)
{
if (config != m_config)
if (m_config != config)
{
m_config = config;
settings()->storeValue(KEY_TYPE, static_cast<int>(config.type));
settings()->storeValue(KEY_IP, config.ip);
settings()->storeValue(KEY_PORT, config.port);
settings()->storeValue(KEY_USERNAME, config.username);
settings()->storeValue(KEY_PASSWORD, config.password);
m_storeProxyType = config.type;
m_storeProxyIP = config.ip;
m_storeProxyPort = config.port;
m_storeProxyUsername = config.username;
m_storeProxyPassword = config.password;
configureProxy();
emit proxyConfigurationChanged();
@@ -116,16 +107,12 @@ void ProxyConfigurationManager::setProxyConfiguration(const ProxyConfiguration &
bool ProxyConfigurationManager::isProxyOnlyForTorrents() const
{
return m_isProxyOnlyForTorrents || (m_config.type == ProxyType::SOCKS4);
return m_storeProxyOnlyForTorrents || (m_config.type == ProxyType::SOCKS4);
}
void ProxyConfigurationManager::setProxyOnlyForTorrents(bool onlyForTorrents)
void ProxyConfigurationManager::setProxyOnlyForTorrents(const bool onlyForTorrents)
{
if (m_isProxyOnlyForTorrents != onlyForTorrents)
{
settings()->storeValue(KEY_ONLY_FOR_TORRENTS, onlyForTorrents);
m_isProxyOnlyForTorrents = onlyForTorrents;
}
m_storeProxyOnlyForTorrents = onlyForTorrents;
}
bool ProxyConfigurationManager::isAuthenticationRequired() const
@@ -138,7 +125,7 @@ void ProxyConfigurationManager::configureProxy()
{
// Define environment variables for urllib in search engine plugins
QString proxyStrHTTP, proxyStrSOCK;
if (!m_isProxyOnlyForTorrents)
if (!isProxyOnlyForTorrents())
{
switch (m_config.type)
{

View File

@@ -30,8 +30,12 @@
#include <QObject>
#include "base/settingvalue.h"
namespace Net
{
Q_NAMESPACE
enum class ProxyType
{
None = 0,
@@ -41,6 +45,7 @@ namespace Net
SOCKS5_PW = 4,
SOCKS4 = 5
};
Q_ENUM_NS(ProxyType)
struct ProxyConfiguration
{
@@ -53,7 +58,7 @@ namespace Net
bool operator==(const ProxyConfiguration &left, const ProxyConfiguration &right);
bool operator!=(const ProxyConfiguration &left, const ProxyConfiguration &right);
class ProxyConfigurationManager : public QObject
class ProxyConfigurationManager final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(ProxyConfigurationManager)
@@ -81,6 +86,11 @@ namespace Net
static ProxyConfigurationManager *m_instance;
ProxyConfiguration m_config;
bool m_isProxyOnlyForTorrents;
SettingValue<bool> m_storeProxyOnlyForTorrents;
SettingValue<ProxyType> m_storeProxyType;
SettingValue<QString> m_storeProxyIP;
SettingValue<ushort> m_storeProxyPort;
SettingValue<QString> m_storeProxyUsername;
SettingValue<QString> m_storeProxyPassword;
};
}

View File

@@ -45,7 +45,6 @@
#include "../global.h"
#include "../logger.h"
#include "../profile.h"
#include "../settingsstorage.h"
#include "../utils/fs.h"
#include "rss_article.h"
#include "rss_autodownloadrule.h"
@@ -62,10 +61,6 @@ struct ProcessingJob
const QString ConfFolderName(QStringLiteral("rss"));
const QString RulesFileName(QStringLiteral("download_rules.json"));
const QString SettingsKey_ProcessingEnabled(QStringLiteral("RSS/AutoDownloader/EnableProcessing"));
const QString SettingsKey_SmartEpisodeFilter(QStringLiteral("RSS/AutoDownloader/SmartEpisodeFilter"));
const QString SettingsKey_DownloadRepacks(QStringLiteral("RSS/AutoDownloader/DownloadRepacks"));
namespace
{
QVector<RSS::AutoDownloadRule> rulesFromJSON(const QByteArray &jsonData)
@@ -103,7 +98,9 @@ QString computeSmartFilterRegex(const QStringList &filters)
}
AutoDownloader::AutoDownloader()
: m_processingEnabled(SettingsStorage::instance()->loadValue(SettingsKey_ProcessingEnabled, false))
: m_storeProcessingEnabled("RSS/AutoDownloader/EnableProcessing", false)
, m_storeSmartEpisodeFilter("RSS/AutoDownloader/SmartEpisodeFilter")
, m_storeDownloadRepacks("RSS/AutoDownloader/DownloadRepacks")
, m_processingTimer(new QTimer(this))
, m_ioThread(new QThread(this))
{
@@ -142,7 +139,7 @@ AutoDownloader::AutoDownloader()
m_processingTimer->setSingleShot(true);
connect(m_processingTimer, &QTimer::timeout, this, &AutoDownloader::process);
if (m_processingEnabled)
if (isProcessingEnabled())
startProcessing();
}
@@ -288,22 +285,19 @@ void AutoDownloader::importRulesFromLegacyFormat(const QByteArray &data)
QStringList AutoDownloader::smartEpisodeFilters() const
{
const auto filtersSetting = SettingsStorage::instance()->loadValue<QVariant>(SettingsKey_SmartEpisodeFilter);
if (filtersSetting.isNull())
const QVariant filter = m_storeSmartEpisodeFilter.get();
if (filter.isNull())
{
QStringList filters =
const QStringList defaultFilters =
{
"s(\\d+)e(\\d+)", // Format 1: s01e01
"(\\d+)x(\\d+)", // Format 2: 01x01
"(\\d{4}[.\\-]\\d{1,2}[.\\-]\\d{1,2})", // Format 3: 2017.01.01
"(\\d{1,2}[.\\-]\\d{1,2}[.\\-]\\d{4})" // Format 4: 01.01.2017
};
return filters;
return defaultFilters;
}
return filtersSetting.toStringList();
return filter.toStringList();
}
QRegularExpression AutoDownloader::smartEpisodeRegex() const
@@ -313,7 +307,7 @@ QRegularExpression AutoDownloader::smartEpisodeRegex() const
void AutoDownloader::setSmartEpisodeFilters(const QStringList &filters)
{
SettingsStorage::instance()->storeValue(SettingsKey_SmartEpisodeFilter, filters);
m_storeSmartEpisodeFilter = filters;
const QString regex = computeSmartFilterRegex(filters);
m_smartEpisodeRegex.setPattern(regex);
@@ -321,12 +315,12 @@ void AutoDownloader::setSmartEpisodeFilters(const QStringList &filters)
bool AutoDownloader::downloadRepacks() const
{
return SettingsStorage::instance()->loadValue(SettingsKey_DownloadRepacks, true);
return m_storeDownloadRepacks.get(true);
}
void AutoDownloader::setDownloadRepacks(const bool downloadRepacks)
void AutoDownloader::setDownloadRepacks(const bool enabled)
{
SettingsStorage::instance()->storeValue(SettingsKey_DownloadRepacks, downloadRepacks);
m_storeDownloadRepacks = enabled;
}
void AutoDownloader::process()
@@ -480,13 +474,13 @@ void AutoDownloader::storeDeferred()
bool AutoDownloader::isProcessingEnabled() const
{
return m_processingEnabled;
return m_storeProcessingEnabled;
}
void AutoDownloader::resetProcessingQueue()
{
m_processingQueue.clear();
if (!m_processingEnabled) return;
if (!isProcessingEnabled()) return;
for (Article *article : asConst(Session::instance()->rootFolder()->articles()))
{
@@ -503,11 +497,10 @@ void AutoDownloader::startProcessing()
void AutoDownloader::setProcessingEnabled(const bool enabled)
{
if (m_processingEnabled != enabled)
if (m_storeProcessingEnabled != enabled)
{
m_processingEnabled = enabled;
SettingsStorage::instance()->storeValue(SettingsKey_ProcessingEnabled, m_processingEnabled);
if (m_processingEnabled)
m_storeProcessingEnabled = enabled;
if (enabled)
{
startProcessing();
}
@@ -517,7 +510,7 @@ void AutoDownloader::setProcessingEnabled(const bool enabled)
disconnect(Session::instance()->rootFolder(), &Folder::newArticle, this, &AutoDownloader::handleNewArticle);
}
emit processingStateChanged(m_processingEnabled);
emit processingStateChanged(enabled);
}
}

View File

@@ -37,6 +37,7 @@
#include <QSharedPointer>
#include "base/exceptions.h"
#include "base/settingvalue.h"
class QThread;
class QTimer;
@@ -86,7 +87,7 @@ namespace RSS
QRegularExpression smartEpisodeRegex() const;
bool downloadRepacks() const;
void setDownloadRepacks(bool downloadRepacks);
void setDownloadRepacks(bool enabled);
bool hasRule(const QString &ruleName) const;
AutoDownloadRule ruleByName(const QString &ruleName) const;
@@ -131,7 +132,10 @@ namespace RSS
static QPointer<AutoDownloader> m_instance;
bool m_processingEnabled;
CachedSettingValue<bool> m_storeProcessingEnabled;
SettingValue<QVariant> m_storeSmartEpisodeFilter;
SettingValue<bool> m_storeDownloadRepacks;
QTimer *m_processingTimer;
QThread *m_ioThread;
AsyncFileStorage *m_fileStorage;

View File

@@ -53,19 +53,15 @@ const QString ConfFolderName(QStringLiteral("rss"));
const QString DataFolderName(QStringLiteral("rss/articles"));
const QString FeedsFileName(QStringLiteral("feeds.json"));
const QString SettingsKey_ProcessingEnabled(QStringLiteral("RSS/Session/EnableProcessing"));
const QString SettingsKey_RefreshInterval(QStringLiteral("RSS/Session/RefreshInterval"));
const QString SettingsKey_MaxArticlesPerFeed(QStringLiteral("RSS/Session/MaxArticlesPerFeed"));
using namespace RSS;
QPointer<Session> Session::m_instance = nullptr;
Session::Session()
: m_processingEnabled(SettingsStorage::instance()->loadValue(SettingsKey_ProcessingEnabled, false))
: m_storeProcessingEnabled("RSS/Session/EnableProcessing")
, m_storeRefreshInterval("RSS/Session/RefreshInterval", 30)
, m_storeMaxArticlesPerFeed("RSS/Session/MaxArticlesPerFeed", 50)
, m_workingThread(new QThread(this))
, m_refreshInterval(SettingsStorage::instance()->loadValue(SettingsKey_RefreshInterval, 30))
, m_maxArticlesPerFeed(SettingsStorage::instance()->loadValue(SettingsKey_MaxArticlesPerFeed, 50))
{
Q_ASSERT(!m_instance); // only one instance is allowed
m_instance = this;
@@ -96,9 +92,9 @@ Session::Session()
load();
connect(&m_refreshTimer, &QTimer::timeout, this, &Session::refresh);
if (m_processingEnabled)
if (isProcessingEnabled())
{
m_refreshTimer.start(m_refreshInterval * MsecsPerMin);
m_refreshTimer.start(refreshInterval() * MsecsPerMin);
refresh();
}
@@ -166,7 +162,7 @@ nonstd::expected<void, QString> Session::addFeed(const QString &url, const QStri
const auto destFolder = result.value();
addItem(new Feed(generateUID(), url, path, this), destFolder);
store();
if (m_processingEnabled)
if (isProcessingEnabled())
feedByURL(url)->refresh();
return {};
@@ -429,18 +425,17 @@ void Session::addItem(Item *item, Folder *destFolder)
bool Session::isProcessingEnabled() const
{
return m_processingEnabled;
return m_storeProcessingEnabled;
}
void Session::setProcessingEnabled(bool enabled)
void Session::setProcessingEnabled(const bool enabled)
{
if (m_processingEnabled != enabled)
if (m_storeProcessingEnabled != enabled)
{
m_processingEnabled = enabled;
SettingsStorage::instance()->storeValue(SettingsKey_ProcessingEnabled, m_processingEnabled);
if (m_processingEnabled)
m_storeProcessingEnabled = enabled;
if (enabled)
{
m_refreshTimer.start(m_refreshInterval * MsecsPerMin);
m_refreshTimer.start(refreshInterval() * MsecsPerMin);
refresh();
}
else
@@ -448,7 +443,7 @@ void Session::setProcessingEnabled(bool enabled)
m_refreshTimer.stop();
}
emit processingStateChanged(m_processingEnabled);
emit processingStateChanged(enabled);
}
}
@@ -479,16 +474,15 @@ Feed *Session::feedByURL(const QString &url) const
int Session::refreshInterval() const
{
return m_refreshInterval;
return m_storeRefreshInterval;
}
void Session::setRefreshInterval(const int refreshInterval)
{
if (m_refreshInterval != refreshInterval)
if (m_storeRefreshInterval != refreshInterval)
{
SettingsStorage::instance()->storeValue(SettingsKey_RefreshInterval, refreshInterval);
m_refreshInterval = refreshInterval;
m_refreshTimer.start(m_refreshInterval * MsecsPerMin);
m_storeRefreshInterval = refreshInterval;
m_refreshTimer.start(m_storeRefreshInterval * MsecsPerMin);
}
}
@@ -527,15 +521,14 @@ QUuid Session::generateUID() const
int Session::maxArticlesPerFeed() const
{
return m_maxArticlesPerFeed;
return m_storeMaxArticlesPerFeed;
}
void Session::setMaxArticlesPerFeed(const int n)
{
if (m_maxArticlesPerFeed != n)
if (m_storeMaxArticlesPerFeed != n)
{
m_maxArticlesPerFeed = n;
SettingsStorage::instance()->storeValue(SettingsKey_MaxArticlesPerFeed, n);
m_storeMaxArticlesPerFeed = n;
emit maxArticlesPerFeedChanged(n);
}
}

View File

@@ -74,6 +74,7 @@
#include <QTimer>
#include "base/3rdparty/expected.hpp"
#include "base/settingvalue.h"
class QThread;
@@ -154,13 +155,13 @@ namespace RSS
static QPointer<Session> m_instance;
bool m_processingEnabled;
CachedSettingValue<bool> m_storeProcessingEnabled;
CachedSettingValue<int> m_storeRefreshInterval;
CachedSettingValue<int> m_storeMaxArticlesPerFeed;
QThread *m_workingThread;
AsyncFileStorage *m_confFileStorage;
AsyncFileStorage *m_dataFileStorage;
QTimer m_refreshTimer;
int m_refreshInterval;
int m_maxArticlesPerFeed;
QHash<QString, Item *> m_itemsByPath;
QHash<QUuid, Feed *> m_feedsByUID;
QHash<QString, Feed *> m_feedsByURL;

View File

@@ -38,6 +38,12 @@
#include "utils/string.h"
template <typename T>
struct IsQFlags : std::false_type {};
template <typename T>
struct IsQFlags<QFlags<T>> : std::true_type {};
class SettingsStorage : public QObject
{
Q_OBJECT
@@ -61,6 +67,11 @@ public:
{
return loadValueImpl(key, defaultValue);
}
else if constexpr (IsQFlags<T>::value)
{
const QVariant value = loadValueImpl(key, static_cast<typename T::Int>(defaultValue));
return T {value.template value<typename T::Int>()};
}
else
{
const QVariant value = loadValueImpl(key);
@@ -74,6 +85,8 @@ public:
{
if constexpr (std::is_enum_v<T>)
storeValueImpl(key, Utils::String::fromEnum(value));
else if constexpr (IsQFlags<T>::value)
storeValueImpl(key, static_cast<typename T::Int>(value));
else
storeValueImpl(key, value);
}