qBittorrent
SettingsStorage Class Reference

#include <settingsstorage.h>

Inheritance diagram for SettingsStorage:
Collaboration diagram for SettingsStorage:

Public Slots

bool save ()
 

Public Member Functions

template<typename T >
loadValue (const QString &key, const T &defaultValue={}) const
 
template<typename T >
void storeValue (const QString &key, const T &value)
 
void removeValue (const QString &key)
 
bool hasKey (const QString &key) const
 

Static Public Member Functions

static void initInstance ()
 
static void freeInstance ()
 
static SettingsStorageinstance ()
 

Private Member Functions

 SettingsStorage ()
 
 ~SettingsStorage ()
 
QVariant loadValueImpl (const QString &key, const QVariant &defaultValue={}) const
 
void storeValueImpl (const QString &key, const QVariant &value)
 

Private Attributes

bool m_dirty = false
 
QVariantHash m_data
 
QTimer m_timer
 
QReadWriteLock m_lock
 

Static Private Attributes

static SettingsStoragem_instance = nullptr
 

Detailed Description

Definition at line 47 of file settingsstorage.h.

Constructor & Destructor Documentation

◆ SettingsStorage()

SettingsStorage::SettingsStorage ( )
private

Definition at line 71 of file settingsstorage.cpp.

72  : m_data {TransactionalSettings(QLatin1String("qBittorrent")).read()}
73 {
74  m_timer.setSingleShot(true);
75  m_timer.setInterval(5 * 1000);
76  connect(&m_timer, &QTimer::timeout, this, &SettingsStorage::save);
77 }
QVariantHash m_data

References m_timer, and save().

Referenced by initInstance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~SettingsStorage()

SettingsStorage::~SettingsStorage ( )
private

Definition at line 79 of file settingsstorage.cpp.

80 {
81  save();
82 }

References save().

Here is the call graph for this function:

Member Function Documentation

◆ freeInstance()

void SettingsStorage::freeInstance ( )
static

Definition at line 90 of file settingsstorage.cpp.

91 {
92  delete m_instance;
93  m_instance = nullptr;
94 }
static SettingsStorage * m_instance

References m_instance.

Referenced by Application::cleanup().

Here is the caller graph for this function:

◆ hasKey()

bool SettingsStorage::hasKey ( const QString &  key) const

Definition at line 149 of file settingsstorage.cpp.

150 {
151  const QReadLocker locker {&m_lock};
152  return m_data.contains(key);
153 }
QReadWriteLock m_lock

References m_data, and m_lock.

◆ initInstance()

void SettingsStorage::initInstance ( )
static

Definition at line 84 of file settingsstorage.cpp.

85 {
86  if (!m_instance)
88 }

References m_instance, and SettingsStorage().

Referenced by Application::Application().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ instance()

◆ loadValue()

template<typename T >
T SettingsStorage::loadValue ( const QString &  key,
const T &  defaultValue = {} 
) const
inline

Definition at line 59 of file settingsstorage.h.

59  {}) const
60  {
61  if constexpr (std::is_enum_v<T>)
62  {
63  const auto value = loadValue<QString>(key);
64  return Utils::String::toEnum(value, defaultValue);
65  }
66  else if constexpr (IsQFlags<T>::value)
67  {
68  const typename T::Int value = loadValue(key, static_cast<typename T::Int>(defaultValue));
69  return T {value};
70  }
71  else if constexpr (std::is_same_v<T, QVariant>)
72  {
73  // fast path for loading QVariant
74  return loadValueImpl(key, defaultValue);
75  }
76  else
77  {
78  const QVariant value = loadValueImpl(key);
79  // check if retrieved value is convertible to T
80  return value.template canConvert<T>() ? value.template value<T>() : defaultValue;
81  }
82  }
T loadValue(const QString &key, const T &defaultValue={}) const
QVariant loadValueImpl(const QString &key, const QVariant &defaultValue={}) const
T toEnum(const QString &serializedValue, const T &defaultValue)
Definition: string.h:77
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64

Referenced by anonymous_namespace{upgrade.cpp}::exportWebUIHttpsFiles(), AddNewTorrentDialog::isEnabled(), AddNewTorrentDialog::isTopLevel(), RSS::Session::loadLegacy(), TorrentFilesWatcher::loadLegacy(), AddNewTorrentDialog::populateSavePaths(), AddNewTorrentDialog::savePathHistoryLength(), anonymous_namespace{addnewtorrentdialog.cpp}::updatePathHistory(), and anonymous_namespace{upgrade.cpp}::upgradeTorrentContentLayout().

Here is the caller graph for this function:

◆ loadValueImpl()

QVariant SettingsStorage::loadValueImpl ( const QString &  key,
const QVariant &  defaultValue = {} 
) const
private

Definition at line 117 of file settingsstorage.cpp.

118 {
119  const QReadLocker locker(&m_lock);
120  return m_data.value(key, defaultValue);
121 }

References m_data, and m_lock.

◆ removeValue()

void SettingsStorage::removeValue ( const QString &  key)

Definition at line 135 of file settingsstorage.cpp.

136 {
137  const QWriteLocker locker(&m_lock);
138 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
139  if (m_data.remove(key))
140 #else
141  if (m_data.remove(key) > 0)
142 #endif
143  {
144  m_dirty = true;
145  m_timer.start();
146  }
147 }

References m_data, m_dirty, m_lock, and m_timer.

Referenced by TorrentFilesWatcher::loadLegacy().

Here is the caller graph for this function:

◆ save

bool SettingsStorage::save ( )
slot

Definition at line 101 of file settingsstorage.cpp.

102 {
103  const QWriteLocker locker(&m_lock); // guard for `m_dirty` too
104  if (!m_dirty) return true;
105 
106  const TransactionalSettings settings(QLatin1String("qBittorrent"));
107  if (!settings.write(m_data))
108  {
109  m_timer.start();
110  return false;
111  }
112 
113  m_dirty = false;
114  return true;
115 }

References m_data, m_dirty, m_lock, m_timer, and anonymous_namespace{addnewtorrentdialog.cpp}::settings().

Referenced by SettingsStorage(), and ~SettingsStorage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ storeValue()

template<typename T >
void SettingsStorage::storeValue ( const QString &  key,
const T &  value 
)
inline

Definition at line 85 of file settingsstorage.h.

86  {
87  if constexpr (std::is_enum_v<T>)
89  else if constexpr (IsQFlags<T>::value)
90  storeValueImpl(key, static_cast<typename T::Int>(value));
91  else
92  storeValueImpl(key, value);
93  }
void storeValueImpl(const QString &key, const QVariant &value)
QString fromEnum(const T &value)
Definition: string.h:67

References Utils::String::fromEnum(), storeValueImpl(), and anonymous_namespace{preferences.cpp}::value().

Referenced by SettingValue< T >::operator=(), setCurrentMigrationVersion(), AddNewTorrentDialog::setEnabled(), AddNewTorrentDialog::setSavePathHistoryLength(), AddNewTorrentDialog::setTopLevel(), anonymous_namespace{preferences.cpp}::setValue(), and anonymous_namespace{addnewtorrentdialog.cpp}::updatePathHistory().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ storeValueImpl()

void SettingsStorage::storeValueImpl ( const QString &  key,
const QVariant &  value 
)
private

Definition at line 123 of file settingsstorage.cpp.

124 {
125  const QWriteLocker locker(&m_lock);
126  QVariant &currentValue = m_data[key];
127  if (currentValue != value)
128  {
129  m_dirty = true;
130  currentValue = value;
131  m_timer.start();
132  }
133 }

References m_data, m_dirty, m_lock, m_timer, and anonymous_namespace{preferences.cpp}::value().

Referenced by storeValue().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_data

QVariantHash SettingsStorage::m_data
private

Definition at line 108 of file settingsstorage.h.

Referenced by hasKey(), loadValueImpl(), removeValue(), save(), and storeValueImpl().

◆ m_dirty

bool SettingsStorage::m_dirty = false
private

Definition at line 107 of file settingsstorage.h.

Referenced by removeValue(), save(), and storeValueImpl().

◆ m_instance

SettingsStorage * SettingsStorage::m_instance = nullptr
staticprivate

Definition at line 105 of file settingsstorage.h.

Referenced by freeInstance(), initInstance(), and instance().

◆ m_lock

QReadWriteLock SettingsStorage::m_lock
mutableprivate

Definition at line 110 of file settingsstorage.h.

Referenced by hasKey(), loadValueImpl(), removeValue(), save(), and storeValueImpl().

◆ m_timer

QTimer SettingsStorage::m_timer
private

Definition at line 109 of file settingsstorage.h.

Referenced by removeValue(), save(), SettingsStorage(), and storeValueImpl().


The documentation for this class was generated from the following files: