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

@@ -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;