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

@@ -63,12 +63,9 @@ namespace
{
#define SETTINGS_KEY(name) "AddNewTorrentDialog/" name
const QString KEY_ENABLED = QStringLiteral(SETTINGS_KEY("Enabled"));
const QString KEY_DEFAULTCATEGORY = QStringLiteral(SETTINGS_KEY("DefaultCategory"));
const QString KEY_TREEHEADERSTATE = QStringLiteral(SETTINGS_KEY("TreeHeaderState"));
const QString KEY_TOPLEVEL = QStringLiteral(SETTINGS_KEY("TopLevel"));
const QString KEY_SAVEPATHHISTORY = QStringLiteral(SETTINGS_KEY("SavePathHistory"));
const QString KEY_SAVEPATHHISTORYLENGTH = QStringLiteral(SETTINGS_KEY("SavePathHistoryLength"));
const QString KEY_REMEMBERLASTSAVEPATH = QStringLiteral(SETTINGS_KEY("RememberLastSavePath"));
// just a shortcut
inline SettingsStorage *settings()
@@ -83,13 +80,12 @@ const int AddNewTorrentDialog::maxPathHistoryLength;
AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
: QDialog(parent)
, m_ui(new Ui::AddNewTorrentDialog)
, m_contentModel(nullptr)
, m_contentDelegate(nullptr)
, m_hasMetadata(false)
, m_oldIndex(0)
, m_torrentParams(inParams)
, m_storeDialogSize(SETTINGS_KEY("DialogSize"))
, m_storeSplitterState(SETTINGS_KEY("SplitterState"))
, m_storeDefaultCategory(SETTINGS_KEY("DefaultCategory"))
, m_storeRememberLastSavePath(SETTINGS_KEY("RememberLastSavePath"))
, m_storeTreeHeaderState(SETTINGS_KEY("TreeHeaderState"))
{
// TODO: set dialog file properties using m_torrentParams.filePriorities
m_ui->setupUi(this);
@@ -114,8 +110,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);
m_ui->checkBoxRememberLastSavePath->setChecked(rememberLastSavePath);
m_ui->checkBoxRememberLastSavePath->setChecked(m_storeRememberLastSavePath);
m_ui->contentLayoutComboBox->setCurrentIndex(
static_cast<int>(m_torrentParams.contentLayout.value_or(session->torrentContentLayout())));
@@ -129,7 +124,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
// Load categories
QStringList categories = session->categories().keys();
std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
auto defaultCategory = settings()->loadValue<QString>(KEY_DEFAULTCATEGORY);
const QString defaultCategory = m_storeDefaultCategory;
if (!m_torrentParams.category.isEmpty())
m_ui->categoryComboBox->addItem(m_torrentParams.category);
@@ -164,22 +159,22 @@ AddNewTorrentDialog::~AddNewTorrentDialog()
bool AddNewTorrentDialog::isEnabled()
{
return SettingsStorage::instance()->loadValue(KEY_ENABLED, true);
return settings()->loadValue(KEY_ENABLED, true);
}
void AddNewTorrentDialog::setEnabled(bool value)
void AddNewTorrentDialog::setEnabled(const bool value)
{
SettingsStorage::instance()->storeValue(KEY_ENABLED, value);
settings()->storeValue(KEY_ENABLED, value);
}
bool AddNewTorrentDialog::isTopLevel()
{
return SettingsStorage::instance()->loadValue(KEY_TOPLEVEL, true);
return settings()->loadValue(KEY_TOPLEVEL, true);
}
void AddNewTorrentDialog::setTopLevel(bool value)
void AddNewTorrentDialog::setTopLevel(const bool value)
{
SettingsStorage::instance()->storeValue(KEY_TOPLEVEL, value);
settings()->storeValue(KEY_TOPLEVEL, value);
}
int AddNewTorrentDialog::savePathHistoryLength()
@@ -189,7 +184,7 @@ int AddNewTorrentDialog::savePathHistoryLength()
return qBound(minPathHistoryLength, value, maxPathHistoryLength);
}
void AddNewTorrentDialog::setSavePathHistoryLength(int value)
void AddNewTorrentDialog::setSavePathHistoryLength(const int value)
{
const int clampedValue = qBound(minPathHistoryLength, value, maxPathHistoryLength);
const int oldValue = savePathHistoryLength();
@@ -204,8 +199,7 @@ void AddNewTorrentDialog::setSavePathHistoryLength(int value)
void AddNewTorrentDialog::loadState()
{
Utils::Gui::resize(this, m_storeDialogSize);
m_ui->splitter->restoreState(m_storeSplitterState);
m_headerState = settings()->loadValue<QByteArray>(KEY_TREEHEADERSTATE);
m_ui->splitter->restoreState(m_storeSplitterState);;
}
void AddNewTorrentDialog::saveState()
@@ -213,7 +207,7 @@ void AddNewTorrentDialog::saveState()
m_storeDialogSize = size();
m_storeSplitterState = m_ui->splitter->saveState();
if (m_contentModel)
settings()->storeValue(KEY_TREEHEADERSTATE, m_ui->contentTreeView->header()->saveState());
m_storeTreeHeaderState = m_ui->contentTreeView->header()->saveState();
}
void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
@@ -489,12 +483,11 @@ void AddNewTorrentDialog::populateSavePathComboBox()
for (const QString &savePath : savePathHistory)
m_ui->savePath->addItem(savePath);
const bool rememberLastSavePath {settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false)};
const QString defSavePath {BitTorrent::Session::instance()->defaultSavePath()};
if (!m_torrentParams.savePath.isEmpty())
setSavePath(m_torrentParams.savePath);
else if (!rememberLastSavePath)
else if (!m_storeRememberLastSavePath)
setSavePath(defSavePath);
// else last used save path will be selected since it is the first in the list
}
@@ -607,9 +600,9 @@ void AddNewTorrentDialog::accept()
// Category
m_torrentParams.category = m_ui->categoryComboBox->currentText();
if (m_ui->defaultCategoryCheckbox->isChecked())
settings()->storeValue(KEY_DEFAULTCATEGORY, m_torrentParams.category);
m_storeDefaultCategory = m_torrentParams.category;
settings()->storeValue(KEY_REMEMBERLASTSAVEPATH, m_ui->checkBoxRememberLastSavePath->isChecked());
m_storeRememberLastSavePath = m_ui->checkBoxRememberLastSavePath->isChecked();
// Save file priorities
if (m_contentModel)
@@ -722,8 +715,8 @@ void AddNewTorrentDialog::setupTreeview()
// List files in torrent
m_contentModel->model()->setupModelData(m_torrentInfo);
if (!m_headerState.isEmpty())
m_ui->contentTreeView->header()->restoreState(m_headerState);
if (!m_storeTreeHeaderState.get().isEmpty())
m_ui->contentTreeView->header()->restoreState(m_storeTreeHeaderState);
// Hide useless columns after loading the header state
m_ui->contentTreeView->hideColumn(PROGRESS);

View File

@@ -108,16 +108,18 @@ private:
void showEvent(QShowEvent *event) override;
Ui::AddNewTorrentDialog *m_ui;
TorrentContentFilterModel *m_contentModel;
PropListDelegate *m_contentDelegate;
bool m_hasMetadata;
TorrentContentFilterModel *m_contentModel = nullptr;
PropListDelegate *m_contentDelegate = nullptr;
bool m_hasMetadata = false;
BitTorrent::MagnetUri m_magnetURI;
BitTorrent::TorrentInfo m_torrentInfo;
QByteArray m_headerState;
int m_oldIndex;
int m_oldIndex = 0;
std::unique_ptr<TorrentFileGuard> m_torrentGuard;
BitTorrent::AddTorrentParams m_torrentParams;
SettingValue<QSize> m_storeDialogSize;
SettingValue<QByteArray> m_storeSplitterState;
SettingValue<QString> m_storeDefaultCategory;
SettingValue<bool> m_storeRememberLastSavePath;
CachedSettingValue<QByteArray> m_storeTreeHeaderState;
};

View File

@@ -56,12 +56,10 @@
#include "base/bittorrent/session.h"
#include "base/bittorrent/sessionstatus.h"
#include "base/global.h"
#include "base/logger.h"
#include "base/net/downloadmanager.h"
#include "base/preferences.h"
#include "base/rss/rss_folder.h"
#include "base/rss/rss_session.h"
#include "base/settingsstorage.h"
#include "base/utils/foreignapps.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
@@ -105,34 +103,14 @@ using namespace std::chrono_literals;
namespace
{
#define SETTINGS_KEY(name) "GUI/" name
// ExecutionLog properties keys
#define EXECUTIONLOG_SETTINGS_KEY(name) QStringLiteral(SETTINGS_KEY("Log/") name)
const QString KEY_EXECUTIONLOG_ENABLED = EXECUTIONLOG_SETTINGS_KEY("Enabled");
const QString KEY_EXECUTIONLOG_TYPES = EXECUTIONLOG_SETTINGS_KEY("Types");
// Notifications properties keys
#define NOTIFICATIONS_SETTINGS_KEY(name) QStringLiteral(SETTINGS_KEY("Notifications/") name)
const QString KEY_NOTIFICATIONS_ENABLED = NOTIFICATIONS_SETTINGS_KEY("Enabled");
const QString KEY_NOTIFICATIONS_TORRENTADDED = NOTIFICATIONS_SETTINGS_KEY("TorrentAdded");
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB)
const QString KEY_NOTIFICATION_TIMEOUT = NOTIFICATIONS_SETTINGS_KEY("Timeout");
#endif
// Misc
const QString KEY_DOWNLOAD_TRACKER_FAVICON = QStringLiteral(SETTINGS_KEY("DownloadTrackerFavicon"));
#define EXECUTIONLOG_SETTINGS_KEY(name) (SETTINGS_KEY("Log/") name)
#define NOTIFICATIONS_SETTINGS_KEY(name) (SETTINGS_KEY("Notifications/") name)
const std::chrono::seconds PREVENT_SUSPEND_INTERVAL {60};
#if !defined(Q_OS_MACOS)
const int TIME_TRAY_BALLOON = 5000;
#endif
// just a shortcut
inline SettingsStorage *settings()
{
return SettingsStorage::instance();
}
bool isTorrentLink(const QString &str)
{
return str.startsWith(QLatin1String("magnet:"), Qt::CaseInsensitive)
@@ -145,10 +123,14 @@ namespace
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_ui(new Ui::MainWindow)
, m_posInitialized(false)
, m_forceExit(false)
, m_unlockDlgShowing(false)
, m_hasPython(false)
, m_storeExecutionLogEnabled(EXECUTIONLOG_SETTINGS_KEY("Enabled"))
, m_storeDownloadTrackerFavicon(SETTINGS_KEY("DownloadTrackerFavicon"))
, m_storeNotificationEnabled(NOTIFICATIONS_SETTINGS_KEY("Enabled"))
, m_storeNotificationTorrentAdded(NOTIFICATIONS_SETTINGS_KEY("TorrentAdded"))
, m_storeExecutionLogTypes(EXECUTIONLOG_SETTINGS_KEY("Types"), Log::MsgType::ALL)
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB)
, m_storeNotificationTimeOut(NOTIFICATIONS_SETTINGS_KEY("Timeout"))
#endif
{
m_ui->setupUi(this);
@@ -358,11 +340,11 @@ MainWindow::MainWindow(QWidget *parent)
m_ui->actionSearchWidget->setChecked(pref->isSearchEnabled());
m_ui->actionExecutionLogs->setChecked(isExecutionLogEnabled());
Log::MsgTypes flags(executionLogMsgTypes());
m_ui->actionNormalMessages->setChecked(flags & Log::NORMAL);
m_ui->actionInformationMessages->setChecked(flags & Log::INFO);
m_ui->actionWarningMessages->setChecked(flags & Log::WARNING);
m_ui->actionCriticalMessages->setChecked(flags & Log::CRITICAL);
const Log::MsgTypes flags = executionLogMsgTypes();
m_ui->actionNormalMessages->setChecked(flags.testFlag(Log::NORMAL));
m_ui->actionInformationMessages->setChecked(flags.testFlag(Log::INFO));
m_ui->actionWarningMessages->setChecked(flags.testFlag(Log::WARNING));
m_ui->actionCriticalMessages->setChecked(flags.testFlag(Log::CRITICAL));
displayRSSTab(m_ui->actionRSSReader->isChecked());
on_actionExecutionLogs_triggered(m_ui->actionExecutionLogs->isChecked());
@@ -485,68 +467,66 @@ MainWindow::~MainWindow()
bool MainWindow::isExecutionLogEnabled() const
{
return settings()->loadValue(KEY_EXECUTIONLOG_ENABLED, false);
return m_storeExecutionLogEnabled;
}
void MainWindow::setExecutionLogEnabled(bool value)
void MainWindow::setExecutionLogEnabled(const bool value)
{
settings()->storeValue(KEY_EXECUTIONLOG_ENABLED, value);
m_storeExecutionLogEnabled = value;
}
int MainWindow::executionLogMsgTypes() const
Log::MsgTypes 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);
return m_storeExecutionLogTypes;
}
void MainWindow::setExecutionLogMsgTypes(const int value)
void MainWindow::setExecutionLogMsgTypes(const Log::MsgTypes value)
{
m_executionLog->setMessageTypes(static_cast<Log::MsgTypes>(value));
settings()->storeValue(KEY_EXECUTIONLOG_TYPES, value);
m_executionLog->setMessageTypes(value);
m_storeExecutionLogTypes = value;
}
bool MainWindow::isNotificationsEnabled() const
{
return settings()->loadValue(KEY_NOTIFICATIONS_ENABLED, true);
return m_storeNotificationEnabled.get(true);
}
void MainWindow::setNotificationsEnabled(bool value)
{
settings()->storeValue(KEY_NOTIFICATIONS_ENABLED, value);
m_storeNotificationEnabled = value;
}
bool MainWindow::isTorrentAddedNotificationsEnabled() const
{
return settings()->loadValue(KEY_NOTIFICATIONS_TORRENTADDED, false);
return m_storeNotificationTorrentAdded;
}
void MainWindow::setTorrentAddedNotificationsEnabled(bool value)
void MainWindow::setTorrentAddedNotificationsEnabled(const bool value)
{
settings()->storeValue(KEY_NOTIFICATIONS_TORRENTADDED, value);
m_storeNotificationTorrentAdded = value;
}
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB)
int MainWindow::getNotificationTimeout() const
{
return settings()->loadValue(KEY_NOTIFICATION_TIMEOUT, -1);
return m_storeNotificationTimeOut.get(-1);
}
void MainWindow::setNotificationTimeout(const int value)
{
settings()->storeValue(KEY_NOTIFICATION_TIMEOUT, value);
m_storeNotificationTimeOut = value;
}
#endif
bool MainWindow::isDownloadTrackerFavicon() const
{
return settings()->loadValue(KEY_DOWNLOAD_TRACKER_FAVICON, false);
return m_storeDownloadTrackerFavicon;
}
void MainWindow::setDownloadTrackerFavicon(bool value)
void MainWindow::setDownloadTrackerFavicon(const bool value)
{
m_transferListFiltersWidget->setDownloadTrackerFavicon(value);
settings()->storeValue(KEY_DOWNLOAD_TRACKER_FAVICON, value);
m_storeDownloadTrackerFavicon = value;
}
void MainWindow::addToolbarContextMenu()
@@ -2005,7 +1985,7 @@ void MainWindow::on_actionExecutionLogs_triggered(bool checked)
if (checked)
{
Q_ASSERT(!m_executionLog);
m_executionLog = new ExecutionLogWidget(static_cast<Log::MsgType>(executionLogMsgTypes()), m_tabs);
m_executionLog = new ExecutionLogWidget(executionLogMsgTypes(), m_tabs);
#ifdef Q_OS_MACOS
m_tabs->addTab(m_executionLog, tr("Execution Log"));
#else
@@ -2025,43 +2005,39 @@ void MainWindow::on_actionExecutionLogs_triggered(bool checked)
setExecutionLogEnabled(checked);
}
void MainWindow::on_actionNormalMessages_triggered(bool checked)
void MainWindow::on_actionNormalMessages_triggered(const bool checked)
{
if (!m_executionLog)
return;
Log::MsgTypes flags(executionLogMsgTypes());
checked ? (flags |= Log::NORMAL) : (flags &= ~Log::NORMAL);
const Log::MsgTypes flags = executionLogMsgTypes().setFlag(Log::NORMAL, checked);
setExecutionLogMsgTypes(flags);
}
void MainWindow::on_actionInformationMessages_triggered(bool checked)
void MainWindow::on_actionInformationMessages_triggered(const bool checked)
{
if (!m_executionLog)
return;
Log::MsgTypes flags(executionLogMsgTypes());
checked ? (flags |= Log::INFO) : (flags &= ~Log::INFO);
const Log::MsgTypes flags = executionLogMsgTypes().setFlag(Log::INFO, checked);
setExecutionLogMsgTypes(flags);
}
void MainWindow::on_actionWarningMessages_triggered(bool checked)
void MainWindow::on_actionWarningMessages_triggered(const bool checked)
{
if (!m_executionLog)
return;
Log::MsgTypes flags(executionLogMsgTypes());
checked ? (flags |= Log::WARNING) : (flags &= ~Log::WARNING);
const Log::MsgTypes flags = executionLogMsgTypes().setFlag(Log::WARNING, checked);
setExecutionLogMsgTypes(flags);
}
void MainWindow::on_actionCriticalMessages_triggered(bool checked)
void MainWindow::on_actionCriticalMessages_triggered(const bool checked)
{
if (!m_executionLog)
return;
Log::MsgTypes flags(executionLogMsgTypes());
checked ? (flags |= Log::CRITICAL) : (flags &= ~Log::CRITICAL);
const Log::MsgTypes flags = executionLogMsgTypes().setFlag(Log::CRITICAL, checked);
setExecutionLogMsgTypes(flags);
}

View File

@@ -36,6 +36,8 @@
#endif
#include "base/bittorrent/torrent.h"
#include "base/logger.h"
#include "base/settingvalue.h"
class QCloseEvent;
class QFileSystemWatcher;
@@ -85,8 +87,8 @@ public:
// ExecutionLog properties
bool isExecutionLogEnabled() const;
void setExecutionLogEnabled(bool value);
int executionLogMsgTypes() const;
void setExecutionLogMsgTypes(int value);
Log::MsgTypes executionLogMsgTypes() const;
void setExecutionLogMsgTypes(Log::MsgTypes value);
// Notifications properties
bool isNotificationsEnabled() const;
@@ -217,7 +219,7 @@ private:
QFileSystemWatcher *m_executableWatcher;
// GUI related
bool m_posInitialized;
bool m_posInitialized = false;
QPointer<QTabWidget> m_tabs;
QPointer<StatusBar> m_statusBar;
QPointer<OptionsDialog> m_options;
@@ -234,9 +236,9 @@ private:
TransferListFiltersWidget *m_transferListFiltersWidget;
PropertiesWidget *m_propertiesWidget;
bool m_displaySpeedInTitle;
bool m_forceExit;
bool m_forceExit = false;
bool m_uiLocked;
bool m_unlockDlgShowing;
bool m_unlockDlgShowing = false;
LineEdit *m_searchFilter;
QAction *m_searchFilterAction;
// Widgets
@@ -249,9 +251,19 @@ private:
// Power Management
PowerManagement *m_pwr;
QTimer *m_preventTimer;
bool m_hasPython;
bool m_hasPython = false;
QMenu *m_toolbarMenu;
SettingValue<bool> m_storeExecutionLogEnabled;
SettingValue<bool> m_storeDownloadTrackerFavicon;
SettingValue<bool> m_storeNotificationEnabled;
SettingValue<bool> m_storeNotificationTorrentAdded;
CachedSettingValue<Log::MsgTypes> m_storeExecutionLogTypes;
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) && defined(QT_DBUS_LIB)
SettingValue<int> m_storeNotificationTimeOut;
#endif
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
void checkProgramUpdate(bool invokedByUser);
void handleUpdateCheckFinished(ProgramUpdater *updater, bool invokedByUser);

View File

@@ -33,7 +33,6 @@
#include "base/bittorrent/session.h"
#include "base/global.h"
#include "base/settingsstorage.h"
#include "base/utils/fs.h"
#include "ui_watchedfolderoptionsdialog.h"
#include "utils.h"