mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-11-28 05:27:15 +03:00
Add a thin layer around SettingsStorage class
This new layer would be handy for saving GUI widget states as they don't need the value cached and they store/load rarely.
This commit is contained in:
parent
60d65d8137
commit
0b0597be0c
12 changed files with 122 additions and 92 deletions
|
@ -482,7 +482,7 @@ Session::Session(QObject *parent)
|
|||
m_storedCategories = map_cast(m_categories);
|
||||
}
|
||||
|
||||
m_tags = List::toSet(m_storedTags.value());
|
||||
m_tags = List::toSet(m_storedTags.get());
|
||||
|
||||
enqueueRefresh();
|
||||
updateSeedingLimitTimer();
|
||||
|
@ -1130,7 +1130,7 @@ void Session::initializeNativeSession()
|
|||
void Session::processBannedIPs(lt::ip_filter &filter)
|
||||
{
|
||||
// First, import current filter
|
||||
for (const QString &ip : asConst(m_bannedIPs.value()))
|
||||
for (const QString &ip : asConst(m_bannedIPs.get()))
|
||||
{
|
||||
lt::error_code ec;
|
||||
const lt::address addr = lt::make_address(ip.toLatin1().constData(), ec);
|
||||
|
@ -3142,7 +3142,7 @@ void Session::setPeerTurnoverInterval(const int val)
|
|||
|
||||
int Session::asyncIOThreads() const
|
||||
{
|
||||
return qBound(1, m_asyncIOThreads.value(), 1024);
|
||||
return qBound(1, m_asyncIOThreads.get(), 1024);
|
||||
}
|
||||
|
||||
void Session::setAsyncIOThreads(const int num)
|
||||
|
@ -3156,7 +3156,7 @@ void Session::setAsyncIOThreads(const int num)
|
|||
|
||||
int Session::hashingThreads() const
|
||||
{
|
||||
return qBound(1, m_hashingThreads.value(), 1024);
|
||||
return qBound(1, m_hashingThreads.get(), 1024);
|
||||
}
|
||||
|
||||
void Session::setHashingThreads(const int num)
|
||||
|
@ -3184,7 +3184,7 @@ void Session::setFilePoolSize(const int size)
|
|||
|
||||
int Session::checkingMemUsage() const
|
||||
{
|
||||
return qMax(1, m_checkingMemUsage.value());
|
||||
return qMax(1, m_checkingMemUsage.get());
|
||||
}
|
||||
|
||||
void Session::setCheckingMemUsage(int size)
|
||||
|
@ -3201,11 +3201,11 @@ void Session::setCheckingMemUsage(int size)
|
|||
int Session::diskCacheSize() const
|
||||
{
|
||||
#ifdef QBT_APP_64BIT
|
||||
return qMin(m_diskCacheSize.value(), 33554431); // 32768GiB
|
||||
return qMin(m_diskCacheSize.get(), 33554431); // 32768GiB
|
||||
#else
|
||||
// When build as 32bit binary, set the maximum at less than 2GB to prevent crashes
|
||||
// allocate 1536MiB and leave 512MiB to the rest of program data in RAM
|
||||
return qMin(m_diskCacheSize.value(), 1536);
|
||||
return qMin(m_diskCacheSize.get(), 1536);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -3737,7 +3737,7 @@ bool Session::isListening() const
|
|||
|
||||
MaxRatioAction Session::maxRatioAction() const
|
||||
{
|
||||
return static_cast<MaxRatioAction>(m_maxRatioAction.value());
|
||||
return static_cast<MaxRatioAction>(m_maxRatioAction.get());
|
||||
}
|
||||
|
||||
void Session::setMaxRatioAction(const MaxRatioAction act)
|
||||
|
|
|
@ -30,80 +30,110 @@
|
|||
|
||||
#include <type_traits>
|
||||
|
||||
#include <QMetaEnum>
|
||||
#include <QString>
|
||||
|
||||
#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`.
|
||||
template <typename T>
|
||||
class SettingValue
|
||||
{
|
||||
public:
|
||||
explicit SettingValue(const char *keyName)
|
||||
: m_keyName {QLatin1String {keyName}}
|
||||
{
|
||||
}
|
||||
|
||||
T get(const T &defaultValue = {}) const
|
||||
{
|
||||
return load(defaultValue);
|
||||
}
|
||||
|
||||
operator T() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
SettingValue<T> &operator=(const T &value)
|
||||
{
|
||||
store(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
// regular load/store pair
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
U load(const U &defaultValue) const
|
||||
{
|
||||
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue)
|
||||
.template value<U>();
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
void store(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName, value);
|
||||
}
|
||||
|
||||
// load/store pair for enum type, saves literal value of the enum constant
|
||||
// TODO: merge the load/store functions with `if constexpr` in C++17
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
U load(const U defaultValue) const
|
||||
{
|
||||
return Utils::String::toEnum(load(QString {}), defaultValue);
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
void store(const U value)
|
||||
{
|
||||
store(Utils::String::fromEnum(value));
|
||||
}
|
||||
|
||||
const QString m_keyName;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class CachedSettingValue
|
||||
{
|
||||
public:
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue = T())
|
||||
: m_keyName(QLatin1String(keyName))
|
||||
, m_value(loadValue(defaultValue))
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue = {})
|
||||
: m_setting {keyName}
|
||||
, m_cache {m_setting.get(defaultValue)}
|
||||
{
|
||||
}
|
||||
|
||||
// The signature of the ProxyFunc should be equivalent to the following:
|
||||
// T proxyFunc(const T &a);
|
||||
template <typename ProxyFunc>
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue
|
||||
, ProxyFunc &&proxyFunc)
|
||||
: m_keyName(QLatin1String(keyName))
|
||||
, m_value(proxyFunc(loadValue(defaultValue)))
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue, ProxyFunc &&proxyFunc)
|
||||
: m_setting {keyName}
|
||||
, m_cache {proxyFunc(m_setting.get(defaultValue))}
|
||||
{
|
||||
}
|
||||
|
||||
T value() const
|
||||
T get() const
|
||||
{
|
||||
return m_value;
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
operator T() const
|
||||
{
|
||||
return value();
|
||||
return get();
|
||||
}
|
||||
|
||||
CachedSettingValue<T> &operator=(const T &newValue)
|
||||
CachedSettingValue<T> &operator=(const T &value)
|
||||
{
|
||||
if (m_value == newValue)
|
||||
if (m_cache == value)
|
||||
return *this;
|
||||
|
||||
m_value = newValue;
|
||||
storeValue(m_value);
|
||||
m_setting = value;
|
||||
m_cache = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
// regular load/save pair
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
U loadValue(const U &defaultValue)
|
||||
{
|
||||
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue).template value<T>();
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
void storeValue(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName, value);
|
||||
}
|
||||
|
||||
// load/save pair for an enum
|
||||
// saves literal value of the enum constant, obtained from QMetaEnum
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
U loadValue(const U &defaultValue)
|
||||
{
|
||||
return Utils::String::toEnum(SettingsStorage::instance()->loadValue(m_keyName).toString(), defaultValue);
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
void storeValue(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName, Utils::String::fromEnum(value));
|
||||
}
|
||||
|
||||
const QString m_keyName;
|
||||
T m_value;
|
||||
SettingValue<T> m_setting;
|
||||
T m_cache;
|
||||
};
|
||||
|
|
|
@ -73,7 +73,7 @@ void TorrentFileGuard::markAsAddedToSession()
|
|||
|
||||
TorrentFileGuard::AutoDeleteMode TorrentFileGuard::autoDeleteMode()
|
||||
{
|
||||
return autoDeleteModeSetting();
|
||||
return autoDeleteModeSetting().get(AutoDeleteMode::Never);
|
||||
}
|
||||
|
||||
void TorrentFileGuard::setAutoDeleteMode(TorrentFileGuard::AutoDeleteMode mode)
|
||||
|
@ -81,8 +81,8 @@ void TorrentFileGuard::setAutoDeleteMode(TorrentFileGuard::AutoDeleteMode mode)
|
|||
autoDeleteModeSetting() = mode;
|
||||
}
|
||||
|
||||
CachedSettingValue<TorrentFileGuard::AutoDeleteMode> &TorrentFileGuard::autoDeleteModeSetting()
|
||||
SettingValue<TorrentFileGuard::AutoDeleteMode> &TorrentFileGuard::autoDeleteModeSetting()
|
||||
{
|
||||
static CachedSettingValue<AutoDeleteMode> setting("Core/AutoDeleteAddedTorrentFile", AutoDeleteMode::Never);
|
||||
static SettingValue<AutoDeleteMode> setting {"Core/AutoDeleteAddedTorrentFile"};
|
||||
return setting;
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
template <typename T> class CachedSettingValue;
|
||||
template <typename T> class SettingValue;
|
||||
|
||||
/// Utility class to defer file deletion
|
||||
class FileGuard
|
||||
|
@ -75,7 +75,7 @@ public:
|
|||
|
||||
private:
|
||||
TorrentFileGuard(const QString &path, AutoDeleteMode mode);
|
||||
static CachedSettingValue<AutoDeleteMode> &autoDeleteModeSetting();
|
||||
static SettingValue<AutoDeleteMode> &autoDeleteModeSetting();
|
||||
|
||||
Q_ENUM(AutoDeleteMode)
|
||||
AutoDeleteMode m_mode;
|
||||
|
|
|
@ -118,6 +118,6 @@ private:
|
|||
std::unique_ptr<TorrentFileGuard> m_torrentGuard;
|
||||
BitTorrent::AddTorrentParams m_torrentParams;
|
||||
|
||||
CachedSettingValue<QSize> m_storeDialogSize;
|
||||
CachedSettingValue<QByteArray> m_storeSplitterState;
|
||||
SettingValue<QSize> m_storeDialogSize;
|
||||
SettingValue<QByteArray> m_storeSplitterState;
|
||||
};
|
||||
|
|
|
@ -159,7 +159,7 @@ void PreviewSelectDialog::loadWindowState()
|
|||
Utils::Gui::resize(this, m_storeDialogSize);
|
||||
|
||||
// Restore TreeView Header state
|
||||
if (!m_storeTreeHeaderState.value().isEmpty())
|
||||
if (!m_storeTreeHeaderState.get().isEmpty())
|
||||
{
|
||||
m_headerStateInitialized = m_ui->previewList->header()->restoreState(m_storeTreeHeaderState);
|
||||
}
|
||||
|
|
|
@ -82,6 +82,6 @@ private:
|
|||
bool m_headerStateInitialized = false;
|
||||
|
||||
// Settings
|
||||
CachedSettingValue<QSize> m_storeDialogSize;
|
||||
CachedSettingValue<QByteArray> m_storeTreeHeaderState;
|
||||
SettingValue<QSize> m_storeDialogSize;
|
||||
SettingValue<QByteArray> m_storeTreeHeaderState;
|
||||
};
|
||||
|
|
|
@ -358,7 +358,7 @@ void SearchJobWidget::fillFilterComboBoxes()
|
|||
m_ui->filterMode->addItem(tr("Torrent names only"), static_cast<int>(NameFilteringMode::OnlyNames));
|
||||
m_ui->filterMode->addItem(tr("Everywhere"), static_cast<int>(NameFilteringMode::Everywhere));
|
||||
|
||||
QVariant selectedMode = static_cast<int>(nameFilteringModeSetting().value());
|
||||
QVariant selectedMode = static_cast<int>(nameFilteringModeSetting().get(NameFilteringMode::OnlyNames));
|
||||
int index = m_ui->filterMode->findData(selectedMode);
|
||||
m_ui->filterMode->setCurrentIndex((index == -1) ? 0 : index);
|
||||
}
|
||||
|
@ -545,9 +545,9 @@ void SearchJobWidget::appendSearchResults(const QVector<SearchResult> &results)
|
|||
updateResultsCount();
|
||||
}
|
||||
|
||||
CachedSettingValue<SearchJobWidget::NameFilteringMode> &SearchJobWidget::nameFilteringModeSetting()
|
||||
SettingValue<SearchJobWidget::NameFilteringMode> &SearchJobWidget::nameFilteringModeSetting()
|
||||
{
|
||||
static CachedSettingValue<NameFilteringMode> setting("Search/FilteringMode", NameFilteringMode::OnlyNames);
|
||||
static SettingValue<NameFilteringMode> setting {"Search/FilteringMode"};
|
||||
return setting;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ class SearchHandler;
|
|||
class SearchSortModel;
|
||||
struct SearchResult;
|
||||
|
||||
template <typename T> class CachedSettingValue;
|
||||
template <typename T> class SettingValue;
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
|
@ -117,7 +117,7 @@ private:
|
|||
void copyField(int column) const;
|
||||
|
||||
static QString statusText(Status st);
|
||||
static CachedSettingValue<NameFilteringMode> &nameFilteringModeSetting();
|
||||
static SettingValue<NameFilteringMode> &nameFilteringModeSetting();
|
||||
|
||||
Ui::SearchJobWidget *m_ui;
|
||||
SearchHandler *m_searchHandler;
|
||||
|
|
|
@ -54,16 +54,16 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defau
|
|||
, m_storeStartSeeding(SETTINGS_KEY("StartSeeding"))
|
||||
, m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio"))
|
||||
#if (LIBTORRENT_VERSION_NUM >= 20000)
|
||||
, m_storeTorrentFormat(SETTINGS_KEY("TorrentFormat"), 1)
|
||||
, m_storeTorrentFormat(SETTINGS_KEY("TorrentFormat"))
|
||||
#else
|
||||
, m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"), true)
|
||||
, m_paddedFileSizeLimit(SETTINGS_KEY("PaddedFileSizeLimit"), -1)
|
||||
, m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"))
|
||||
, m_paddedFileSizeLimit(SETTINGS_KEY("PaddedFileSizeLimit"))
|
||||
#endif
|
||||
, m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath())
|
||||
, m_storeLastAddPath(SETTINGS_KEY("LastAddPath"))
|
||||
, m_storeTrackerList(SETTINGS_KEY("TrackerList"))
|
||||
, m_storeWebSeedList(SETTINGS_KEY("WebSeedList"))
|
||||
, m_storeComments(SETTINGS_KEY("Comments"))
|
||||
, m_storeLastSavePath(SETTINGS_KEY("LastSavePath"), QDir::homePath())
|
||||
, m_storeLastSavePath(SETTINGS_KEY("LastSavePath"))
|
||||
, m_storeSource(SETTINGS_KEY("Source"))
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
@ -184,7 +184,7 @@ void TorrentCreatorDialog::onCreateButtonClicked()
|
|||
input = fi.canonicalFilePath();
|
||||
|
||||
// get save path
|
||||
const QString savePath = QString(m_storeLastSavePath) + QLatin1Char('/') + fi.fileName() + QLatin1String(".torrent");
|
||||
const QString savePath = m_storeLastSavePath.get(QDir::homePath()) + QLatin1Char('/') + fi.fileName() + QLatin1String(".torrent");
|
||||
QString destination = QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), savePath, tr("Torrent Files (*.torrent)"));
|
||||
if (destination.isEmpty())
|
||||
return;
|
||||
|
@ -324,7 +324,7 @@ void TorrentCreatorDialog::saveSettings()
|
|||
|
||||
void TorrentCreatorDialog::loadSettings()
|
||||
{
|
||||
m_ui->textInputPath->setText(m_storeLastAddPath);
|
||||
m_ui->textInputPath->setText(m_storeLastAddPath.get(QDir::homePath()));
|
||||
|
||||
m_ui->comboPieceSize->setCurrentIndex(m_storePieceSize);
|
||||
m_ui->checkPrivate->setChecked(m_storePrivateTorrent);
|
||||
|
@ -332,10 +332,10 @@ void TorrentCreatorDialog::loadSettings()
|
|||
m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio);
|
||||
m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked());
|
||||
#if (LIBTORRENT_VERSION_NUM >= 20000)
|
||||
m_ui->comboTorrentFormat->setCurrentIndex(m_storeTorrentFormat);
|
||||
m_ui->comboTorrentFormat->setCurrentIndex(m_storeTorrentFormat.get(1));
|
||||
#else
|
||||
m_ui->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment);
|
||||
m_ui->spinPaddedFileSizeLimit->setValue(m_paddedFileSizeLimit);
|
||||
m_ui->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment.get(true));
|
||||
m_ui->spinPaddedFileSizeLimit->setValue(m_paddedFileSizeLimit.get(-1));
|
||||
#endif
|
||||
|
||||
m_ui->trackersList->setPlainText(m_storeTrackerList);
|
||||
|
|
|
@ -78,21 +78,21 @@ private:
|
|||
BitTorrent::TorrentCreatorThread *m_creatorThread;
|
||||
|
||||
// settings
|
||||
CachedSettingValue<QSize> m_storeDialogSize;
|
||||
CachedSettingValue<int> m_storePieceSize;
|
||||
CachedSettingValue<bool> m_storePrivateTorrent;
|
||||
CachedSettingValue<bool> m_storeStartSeeding;
|
||||
CachedSettingValue<bool> m_storeIgnoreRatio;
|
||||
SettingValue<QSize> m_storeDialogSize;
|
||||
SettingValue<int> m_storePieceSize;
|
||||
SettingValue<bool> m_storePrivateTorrent;
|
||||
SettingValue<bool> m_storeStartSeeding;
|
||||
SettingValue<bool> m_storeIgnoreRatio;
|
||||
#if (LIBTORRENT_VERSION_NUM >= 20000)
|
||||
CachedSettingValue<int> m_storeTorrentFormat;
|
||||
SettingValue<int> m_storeTorrentFormat;
|
||||
#else
|
||||
CachedSettingValue<bool> m_storeOptimizeAlignment;
|
||||
CachedSettingValue<int> m_paddedFileSizeLimit;
|
||||
SettingValue<bool> m_storeOptimizeAlignment;
|
||||
SettingValue<int> m_paddedFileSizeLimit;
|
||||
#endif
|
||||
CachedSettingValue<QString> m_storeLastAddPath;
|
||||
CachedSettingValue<QString> m_storeTrackerList;
|
||||
CachedSettingValue<QString> m_storeWebSeedList;
|
||||
CachedSettingValue<QString> m_storeComments;
|
||||
CachedSettingValue<QString> m_storeLastSavePath;
|
||||
CachedSettingValue<QString> m_storeSource;
|
||||
SettingValue<QString> m_storeLastAddPath;
|
||||
SettingValue<QString> m_storeTrackerList;
|
||||
SettingValue<QString> m_storeWebSeedList;
|
||||
SettingValue<QString> m_storeComments;
|
||||
SettingValue<QString> m_storeLastSavePath;
|
||||
SettingValue<QString> m_storeSource;
|
||||
};
|
||||
|
|
|
@ -60,5 +60,5 @@ private:
|
|||
void loadSettings();
|
||||
|
||||
Ui::TrackerEntriesDialog *m_ui;
|
||||
CachedSettingValue<QSize> m_storeDialogSize;
|
||||
SettingValue<QSize> m_storeDialogSize;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue