diff --git a/src/app/application.cpp b/src/app/application.cpp index 1b0ebd683..9453f8ba3 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -214,7 +214,7 @@ const QBtCommandLineParameters &Application::commandLineArgs() const bool Application::isFileLoggerEnabled() const { - return settings()->loadValue(KEY_FILELOGGER_ENABLED, true).toBool(); + return settings()->loadValue(KEY_FILELOGGER_ENABLED, true); } void Application::setFileLoggerEnabled(const bool value) @@ -228,8 +228,8 @@ void Application::setFileLoggerEnabled(const bool value) QString Application::fileLoggerPath() const { - return settings()->loadValue(KEY_FILELOGGER_PATH, - {specialFolderLocation(SpecialFolder::Data) + LOG_FOLDER}).toString(); + return settings()->loadValue(KEY_FILELOGGER_PATH + , QString {specialFolderLocation(SpecialFolder::Data) + LOG_FOLDER}); } void Application::setFileLoggerPath(const QString &path) @@ -241,7 +241,7 @@ void Application::setFileLoggerPath(const QString &path) bool Application::isFileLoggerBackup() const { - return settings()->loadValue(KEY_FILELOGGER_BACKUP, true).toBool(); + return settings()->loadValue(KEY_FILELOGGER_BACKUP, true); } void Application::setFileLoggerBackup(const bool value) @@ -253,7 +253,7 @@ void Application::setFileLoggerBackup(const bool value) bool Application::isFileLoggerDeleteOld() const { - return settings()->loadValue(KEY_FILELOGGER_DELETEOLD, true).toBool(); + return settings()->loadValue(KEY_FILELOGGER_DELETEOLD, true); } void Application::setFileLoggerDeleteOld(const bool value) @@ -265,7 +265,7 @@ void Application::setFileLoggerDeleteOld(const bool value) int Application::fileLoggerMaxSize() const { - const int val = settings()->loadValue(KEY_FILELOGGER_MAXSIZEBYTES, DEFAULT_FILELOG_SIZE).toInt(); + const int val = settings()->loadValue(KEY_FILELOGGER_MAXSIZEBYTES, DEFAULT_FILELOG_SIZE); return std::min(std::max(val, MIN_FILELOG_SIZE), MAX_FILELOG_SIZE); } @@ -279,7 +279,7 @@ void Application::setFileLoggerMaxSize(const int bytes) int Application::fileLoggerAge() const { - const int val = settings()->loadValue(KEY_FILELOGGER_AGE, 1).toInt(); + const int val = settings()->loadValue(KEY_FILELOGGER_AGE, 1); return std::min(std::max(val, 1), 365); } @@ -290,7 +290,7 @@ void Application::setFileLoggerAge(const int value) int Application::fileLoggerAgeType() const { - const int val = settings()->loadValue(KEY_FILELOGGER_AGETYPE, 1).toInt(); + const int val = settings()->loadValue(KEY_FILELOGGER_AGETYPE, 1); return ((val < 0) || (val > 2)) ? 1 : val; } diff --git a/src/app/upgrade.cpp b/src/app/upgrade.cpp index 7d53cd417..c2673d45c 100644 --- a/src/app/upgrade.cpp +++ b/src/app/upgrade.cpp @@ -46,8 +46,8 @@ namespace const auto migrate = [](const QString &oldKey, const QString &newKey, const QString &savePath) { SettingsStorage *settingsStorage {SettingsStorage::instance()}; - const QByteArray oldData {settingsStorage->loadValue(oldKey).toByteArray()}; - const QString newData {settingsStorage->loadValue(newKey).toString()}; + const auto oldData {settingsStorage->loadValue(oldKey)}; + const auto newData {settingsStorage->loadValue(newKey)}; const QString errorMsgFormat {QObject::tr("Migrate preferences failed: WebUI https, file: \"%1\", error: \"%2\"")}; if (!newData.isEmpty() || oldData.isEmpty()) @@ -89,8 +89,8 @@ namespace const QString newKey {QLatin1String {"BitTorrent/Session/TorrentContentLayout"}}; SettingsStorage *settingsStorage {SettingsStorage::instance()}; - const QVariant oldData {settingsStorage->loadValue(oldKey)}; - const QString newData {settingsStorage->loadValue(newKey).toString()}; + const auto oldData {settingsStorage->loadValue(oldKey)}; + const auto newData {settingsStorage->loadValue(newKey)}; if (!newData.isEmpty() || !oldData.isValid()) return; @@ -128,7 +128,7 @@ void handleChangedDefaults(const DefaultPreferencesMode mode) SettingsStorage *settingsStorage {SettingsStorage::instance()}; for (auto it = changedDefaults.cbegin(); it != changedDefaults.cend(); ++it) { - if (settingsStorage->loadValue(it->name).isNull()) + if (settingsStorage->loadValue(it->name).isNull()) settingsStorage->storeValue(it->name, (mode == DefaultPreferencesMode::Legacy ? it->legacy : it->current)); } } diff --git a/src/base/bittorrent/portforwarderimpl.cpp b/src/base/bittorrent/portforwarderimpl.cpp index 4ac399d0e..f81ba8f9b 100644 --- a/src/base/bittorrent/portforwarderimpl.cpp +++ b/src/base/bittorrent/portforwarderimpl.cpp @@ -39,7 +39,7 @@ 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).toBool()} + , m_active {SettingsStorage::instance()->loadValue(KEY_ENABLED, true)} , m_provider {provider} { if (m_active) diff --git a/src/base/net/proxyconfigurationmanager.cpp b/src/base/net/proxyconfigurationmanager.cpp index 0a4961594..1380f2c6c 100644 --- a/src/base/net/proxyconfigurationmanager.cpp +++ b/src/base/net/proxyconfigurationmanager.cpp @@ -64,15 +64,15 @@ ProxyConfigurationManager *ProxyConfigurationManager::m_instance = nullptr; ProxyConfigurationManager::ProxyConfigurationManager(QObject *parent) : QObject(parent) { - m_isProxyOnlyForTorrents = settings()->loadValue(KEY_ONLY_FOR_TORRENTS, false).toBool(); + m_isProxyOnlyForTorrents = settings()->loadValue(KEY_ONLY_FOR_TORRENTS, false); m_config.type = static_cast( - settings()->loadValue(KEY_TYPE, static_cast(ProxyType::None)).toInt()); + settings()->loadValue(KEY_TYPE, static_cast(ProxyType::None))); if ((m_config.type < ProxyType::None) || (m_config.type > ProxyType::SOCKS4)) m_config.type = ProxyType::None; - m_config.ip = settings()->loadValue(KEY_IP, "0.0.0.0").toString(); - m_config.port = static_cast(settings()->loadValue(KEY_PORT, 8080).toUInt()); - m_config.username = settings()->loadValue(KEY_USERNAME).toString(); - m_config.password = settings()->loadValue(KEY_PASSWORD).toString(); + m_config.ip = settings()->loadValue(KEY_IP, "0.0.0.0"); + m_config.port = settings()->loadValue(KEY_PORT, 8080); + m_config.username = settings()->loadValue(KEY_USERNAME); + m_config.password = settings()->loadValue(KEY_PASSWORD); configureProxy(); } diff --git a/src/base/rss/rss_autodownloader.cpp b/src/base/rss/rss_autodownloader.cpp index 889a8183f..cdd857024 100644 --- a/src/base/rss/rss_autodownloader.cpp +++ b/src/base/rss/rss_autodownloader.cpp @@ -105,7 +105,7 @@ QString computeSmartFilterRegex(const QStringList &filters) } AutoDownloader::AutoDownloader() - : m_processingEnabled(SettingsStorage::instance()->loadValue(SettingsKey_ProcessingEnabled, false).toBool()) + : m_processingEnabled(SettingsStorage::instance()->loadValue(SettingsKey_ProcessingEnabled, false)) , m_processingTimer(new QTimer(this)) , m_ioThread(new QThread(this)) { @@ -290,7 +290,7 @@ void AutoDownloader::importRulesFromLegacyFormat(const QByteArray &data) QStringList AutoDownloader::smartEpisodeFilters() const { - const QVariant filtersSetting = SettingsStorage::instance()->loadValue(SettingsKey_SmartEpisodeFilter); + const auto filtersSetting = SettingsStorage::instance()->loadValue(SettingsKey_SmartEpisodeFilter); if (filtersSetting.isNull()) { @@ -323,7 +323,7 @@ void AutoDownloader::setSmartEpisodeFilters(const QStringList &filters) bool AutoDownloader::downloadRepacks() const { - return SettingsStorage::instance()->loadValue(SettingsKey_DownloadRepacks, true).toBool(); + return SettingsStorage::instance()->loadValue(SettingsKey_DownloadRepacks, true); } void AutoDownloader::setDownloadRepacks(const bool downloadRepacks) diff --git a/src/base/rss/rss_session.cpp b/src/base/rss/rss_session.cpp index 04e39e26a..d77997324 100644 --- a/src/base/rss/rss_session.cpp +++ b/src/base/rss/rss_session.cpp @@ -63,10 +63,10 @@ using namespace RSS; QPointer Session::m_instance = nullptr; Session::Session() - : m_processingEnabled(SettingsStorage::instance()->loadValue(SettingsKey_ProcessingEnabled, false).toBool()) + : m_processingEnabled(SettingsStorage::instance()->loadValue(SettingsKey_ProcessingEnabled, false)) , m_workingThread(new QThread(this)) - , m_refreshInterval(SettingsStorage::instance()->loadValue(SettingsKey_RefreshInterval, 30).toInt()) - , m_maxArticlesPerFeed(SettingsStorage::instance()->loadValue(SettingsKey_MaxArticlesPerFeed, 50).toInt()) + , 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; @@ -362,8 +362,8 @@ void Session::loadFolder(const QJsonObject &jsonObj, Folder *folder) void Session::loadLegacy() { - const QStringList legacyFeedPaths = SettingsStorage::instance()->loadValue("Rss/streamList").toStringList(); - const QStringList feedAliases = SettingsStorage::instance()->loadValue("Rss/streamAlias").toStringList(); + const auto legacyFeedPaths = SettingsStorage::instance()->loadValue("Rss/streamList"); + const auto feedAliases = SettingsStorage::instance()->loadValue("Rss/streamAlias"); if (legacyFeedPaths.size() != feedAliases.size()) { Logger::instance()->addMessage("Corrupted RSS list, not loading it.", Log::WARNING); diff --git a/src/base/settingsstorage.cpp b/src/base/settingsstorage.cpp index 790300ae7..d9ac0a337 100644 --- a/src/base/settingsstorage.cpp +++ b/src/base/settingsstorage.cpp @@ -153,8 +153,7 @@ namespace SettingsStorage *SettingsStorage::m_instance = nullptr; SettingsStorage::SettingsStorage() - : m_data{TransactionalSettings(QLatin1String("qBittorrent")).read()} - , m_dirty(false) + : m_data {TransactionalSettings(QLatin1String("qBittorrent")).read()} { m_timer.setSingleShot(true); m_timer.setInterval(5 * 1000); @@ -200,14 +199,14 @@ bool SettingsStorage::save() return true; } -QVariant SettingsStorage::loadValue(const QString &key, const QVariant &defaultValue) const +QVariant SettingsStorage::loadValueImpl(const QString &key, const QVariant &defaultValue) const { const QString realKey = mapKey(key); const QReadLocker locker(&m_lock); return m_data.value(realKey, defaultValue); } -void SettingsStorage::storeValue(const QString &key, const QVariant &value) +void SettingsStorage::storeValueImpl(const QString &key, const QVariant &value) { const QString realKey = mapKey(key); const QWriteLocker locker(&m_lock); diff --git a/src/base/settingsstorage.h b/src/base/settingsstorage.h index de36e038f..52f6188f1 100644 --- a/src/base/settingsstorage.h +++ b/src/base/settingsstorage.h @@ -29,11 +29,15 @@ #pragma once +#include + #include #include #include #include +#include "utils/string.h" + class SettingsStorage : public QObject { Q_OBJECT @@ -45,18 +49,48 @@ public: static void freeInstance(); static SettingsStorage *instance(); - QVariant loadValue(const QString &key, const QVariant &defaultValue = {}) const; - void storeValue(const QString &key, const QVariant &value); + template + T loadValue(const QString &key, const T &defaultValue = {}) const + { + if constexpr (std::is_enum_v) + { + const auto value = loadValueImpl(key).toString(); + return Utils::String::toEnum(value, defaultValue); + } + else if constexpr (std::is_same_v) + { + return loadValueImpl(key, defaultValue); + } + else + { + const QVariant value = loadValueImpl(key); + // check if retrieved value is convertible to T + return value.template canConvert() ? value.template value() : defaultValue; + } + } + + template + void storeValue(const QString &key, const T &value) + { + if constexpr (std::is_enum_v) + storeValueImpl(key, Utils::String::fromEnum(value)); + else + storeValueImpl(key, value); + } + void removeValue(const QString &key); public slots: bool save(); private: + QVariant loadValueImpl(const QString &key, const QVariant &defaultValue = {}) const; + void storeValueImpl(const QString &key, const QVariant &value); + static SettingsStorage *m_instance; + bool m_dirty = false; QVariantHash m_data; - bool m_dirty; QTimer m_timer; mutable QReadWriteLock m_lock; }; diff --git a/src/base/settingvalue.h b/src/base/settingvalue.h index 02e701257..55b1fabcf 100644 --- a/src/base/settingvalue.h +++ b/src/base/settingvalue.h @@ -28,12 +28,9 @@ #pragma once -#include - #include #include "settingsstorage.h" -#include "utils/string.h" // This is a thin/handy wrapper over `SettingsStorage`. Use it when store/load value // rarely occurs, otherwise use `CachedSettingValue`. @@ -48,17 +45,7 @@ public: T get(const T &defaultValue = {}) const { - if constexpr (std::is_enum_v) - { - const auto value = SettingsStorage::instance()->loadValue(m_keyName, {}).toString(); - return Utils::String::toEnum(value, defaultValue); - } - else - { - const QVariant value = SettingsStorage::instance()->loadValue(m_keyName); - // check if retrieved value is convertible to T - return value.template canConvert() ? value.template value() : defaultValue; - } + return SettingsStorage::instance()->loadValue(m_keyName, defaultValue); } operator T() const @@ -68,10 +55,7 @@ public: SettingValue &operator=(const T &value) { - if constexpr (std::is_enum_v) - SettingsStorage::instance()->storeValue(m_keyName, Utils::String::fromEnum(value)); - else - SettingsStorage::instance()->storeValue(m_keyName, value); + SettingsStorage::instance()->storeValue(m_keyName, value); return *this; } diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index ea8176684..9b8c50052 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -120,7 +120,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP populateSavePathComboBox(); connect(m_ui->savePath, &FileSystemPathEdit::selectedPathChanged, this, &AddNewTorrentDialog::onSavePathChanged); - const bool rememberLastSavePath = settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false).toBool(); + const bool rememberLastSavePath = settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false); m_ui->checkBoxRememberLastSavePath->setChecked(rememberLastSavePath); m_ui->contentLayoutComboBox->setCurrentIndex( @@ -135,7 +135,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP // Load categories QStringList categories = session->categories().keys(); std::sort(categories.begin(), categories.end(), Utils::String::naturalLessThan); - QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString(); + auto defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY); if (!m_torrentParams.category.isEmpty()) m_ui->categoryComboBox->addItem(m_torrentParams.category); @@ -170,7 +170,7 @@ AddNewTorrentDialog::~AddNewTorrentDialog() bool AddNewTorrentDialog::isEnabled() { - return SettingsStorage::instance()->loadValue(KEY_ENABLED, true).toBool(); + return SettingsStorage::instance()->loadValue(KEY_ENABLED, true); } void AddNewTorrentDialog::setEnabled(bool value) @@ -180,7 +180,7 @@ void AddNewTorrentDialog::setEnabled(bool value) bool AddNewTorrentDialog::isTopLevel() { - return SettingsStorage::instance()->loadValue(KEY_TOPLEVEL, true).toBool(); + return SettingsStorage::instance()->loadValue(KEY_TOPLEVEL, true); } void AddNewTorrentDialog::setTopLevel(bool value) @@ -191,7 +191,7 @@ void AddNewTorrentDialog::setTopLevel(bool value) int AddNewTorrentDialog::savePathHistoryLength() { const int defaultHistoryLength = 8; - const int value = settings()->loadValue(KEY_SAVEPATHHISTORYLENGTH, defaultHistoryLength).toInt(); + const int value = settings()->loadValue(KEY_SAVEPATHHISTORYLENGTH, defaultHistoryLength); return qBound(minPathHistoryLength, value, maxPathHistoryLength); } @@ -204,14 +204,14 @@ void AddNewTorrentDialog::setSavePathHistoryLength(int value) settings()->storeValue(KEY_SAVEPATHHISTORYLENGTH, clampedValue); settings()->storeValue(KEY_SAVEPATHHISTORY - , QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, clampedValue))); + , QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).mid(0, clampedValue))); } void AddNewTorrentDialog::loadState() { Utils::Gui::resize(this, m_storeDialogSize); m_ui->splitter->restoreState(m_storeSplitterState); - m_headerState = settings()->loadValue(KEY_TREEHEADERSTATE).toByteArray(); + m_headerState = settings()->loadValue(KEY_TREEHEADERSTATE); } void AddNewTorrentDialog::saveState() @@ -371,7 +371,7 @@ void AddNewTorrentDialog::showEvent(QShowEvent *event) void AddNewTorrentDialog::saveSavePathHistory() const { // Get current history - QStringList history = settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList(); + auto history = settings()->loadValue(KEY_SAVEPATHHISTORY); QVector historyDirs; for (const QString &path : asConst(history)) historyDirs << QDir {path}; @@ -489,11 +489,11 @@ void AddNewTorrentDialog::populateSavePathComboBox() m_ui->savePath->clear(); // Load save path history - const QStringList savePathHistory {settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList()}; + const auto savePathHistory {settings()->loadValue(KEY_SAVEPATHHISTORY)}; for (const QString &savePath : savePathHistory) m_ui->savePath->addItem(savePath); - const bool rememberLastSavePath {settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false).toBool()}; + const bool rememberLastSavePath {settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false)}; const QString defSavePath {BitTorrent::Session::instance()->defaultSavePath()}; if (!m_torrentParams.savePath.isEmpty()) diff --git a/src/gui/cookiesdialog.cpp b/src/gui/cookiesdialog.cpp index 828b272ae..d1281131e 100644 --- a/src/gui/cookiesdialog.cpp +++ b/src/gui/cookiesdialog.cpp @@ -61,9 +61,9 @@ CookiesDialog::CookiesDialog(QWidget *parent) m_cookiesModel->index(0, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); - Utils::Gui::resize(this, SettingsStorage::instance()->loadValue(KEY_SIZE).toSize()); + Utils::Gui::resize(this, SettingsStorage::instance()->loadValue(KEY_SIZE)); m_ui->treeView->header()->restoreState( - SettingsStorage::instance()->loadValue(KEY_COOKIESVIEWSTATE).toByteArray()); + SettingsStorage::instance()->loadValue(KEY_COOKIESVIEWSTATE)); } CookiesDialog::~CookiesDialog() diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index d7ab5332d..51c07ad13 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -491,7 +491,7 @@ MainWindow::~MainWindow() bool MainWindow::isExecutionLogEnabled() const { - return settings()->loadValue(KEY_EXECUTIONLOG_ENABLED, false).toBool(); + return settings()->loadValue(KEY_EXECUTIONLOG_ENABLED, false); } void MainWindow::setExecutionLogEnabled(bool value) @@ -503,7 +503,7 @@ int MainWindow::executionLogMsgTypes() const { // as default value we need all the bits set // -1 is considered the portable way to achieve that - return settings()->loadValue(KEY_EXECUTIONLOG_TYPES, -1).toInt(); + return settings()->loadValue(KEY_EXECUTIONLOG_TYPES, -1); } void MainWindow::setExecutionLogMsgTypes(const int value) @@ -514,7 +514,7 @@ void MainWindow::setExecutionLogMsgTypes(const int value) bool MainWindow::isNotificationsEnabled() const { - return settings()->loadValue(KEY_NOTIFICATIONS_ENABLED, true).toBool(); + return settings()->loadValue(KEY_NOTIFICATIONS_ENABLED, true); } void MainWindow::setNotificationsEnabled(bool value) @@ -524,7 +524,7 @@ void MainWindow::setNotificationsEnabled(bool value) bool MainWindow::isTorrentAddedNotificationsEnabled() const { - return settings()->loadValue(KEY_NOTIFICATIONS_TORRENTADDED, false).toBool(); + return settings()->loadValue(KEY_NOTIFICATIONS_TORRENTADDED, false); } void MainWindow::setTorrentAddedNotificationsEnabled(bool value) @@ -534,7 +534,7 @@ void MainWindow::setTorrentAddedNotificationsEnabled(bool value) bool MainWindow::isDownloadTrackerFavicon() const { - return settings()->loadValue(KEY_DOWNLOAD_TRACKER_FAVICON, false).toBool(); + return settings()->loadValue(KEY_DOWNLOAD_TRACKER_FAVICON, false); } void MainWindow::setDownloadTrackerFavicon(bool value)