qBittorrent
anonymous_namespace{settingsstorage.cpp}::TransactionalSettings Class Reference

Public Member Functions

 TransactionalSettings (const QString &name)
 
QVariantHash read () const
 
bool write (const QVariantHash &data) const
 

Private Member Functions

QString deserialize (const QString &name, QVariantHash &data) const
 
QString serialize (const QString &name, const QVariantHash &data) const
 

Private Attributes

const QString m_name
 

Detailed Description

Definition at line 46 of file settingsstorage.cpp.

Constructor & Destructor Documentation

◆ TransactionalSettings()

anonymous_namespace{settingsstorage.cpp}::TransactionalSettings::TransactionalSettings ( const QString &  name)
inlineexplicit

Definition at line 49 of file settingsstorage.cpp.

Member Function Documentation

◆ deserialize()

QString TransactionalSettings::deserialize ( const QString &  name,
QVariantHash &  data 
) const
private

Definition at line 207 of file settingsstorage.cpp.

208 {
210 
211  if (settings->allKeys().isEmpty())
212  return {};
213 
214  // Copy everything into memory. This means even keys inserted in the file manually
215  // or that we don't touch directly in this code (eg disabled by ifdef). This ensures
216  // that they will be copied over when save our settings to disk.
217  for (const QString &key : asConst(settings->allKeys()))
218  {
219  const QVariant value = settings->value(key);
220  if (value.isValid())
221  data[key] = value;
222  }
223 
224  return settings->fileName();
225 }
SettingsPtr applicationSettings(const QString &name) const
Definition: profile.cpp:109
static const Profile * instance()
Definition: profile.cpp:67
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64
std::unique_ptr< QSettings > SettingsPtr
Definition: profile.h:44

References Profile::applicationSettings(), asConst(), Profile::instance(), anonymous_namespace{addnewtorrentdialog.cpp}::settings(), and anonymous_namespace{preferences.cpp}::value().

Here is the call graph for this function:

◆ read()

QVariantHash TransactionalSettings::read ( ) const

Definition at line 155 of file settingsstorage.cpp.

156 {
157  QVariantHash res;
158 
159  const QString newPath = deserialize(m_name + QLatin1String("_new"), res);
160  if (!newPath.isEmpty())
161  { // "_new" file is NOT empty
162  // This means that the PC closed either due to power outage
163  // or because the disk was full. In any case the settings weren't transferred
164  // in their final position. So assume that qbittorrent_new.ini/qbittorrent_new.conf
165  // contains the most recent settings.
166  Logger::instance()->addMessage(QObject::tr("Detected unclean program exit. Using fallback file to restore settings: %1")
167  .arg(Utils::Fs::toNativePath(newPath))
168  , Log::WARNING);
169 
170  QString finalPath = newPath;
171  int index = finalPath.lastIndexOf("_new", -1, Qt::CaseInsensitive);
172  finalPath.remove(index, 4);
173 
174  Utils::Fs::forceRemove(finalPath);
175  QFile::rename(newPath, finalPath);
176  }
177  else
178  {
179  deserialize(m_name, res);
180  }
181 
182  return res;
183 }
void addMessage(const QString &message, const Log::MsgType &type=Log::NORMAL)
Definition: logger.cpp:73
static Logger * instance()
Definition: logger.cpp:56
QString deserialize(const QString &name, QVariantHash &data) const
@ WARNING
Definition: logger.h:47
bool forceRemove(const QString &filePath)
Definition: fs.cpp:173
QString toNativePath(const QString &path)
Definition: fs.cpp:64

References Logger::addMessage(), Utils::Fs::forceRemove(), Logger::instance(), Utils::Fs::toNativePath(), and Log::WARNING.

Here is the call graph for this function:

◆ serialize()

QString TransactionalSettings::serialize ( const QString &  name,
const QVariantHash &  data 
) const
private

Definition at line 227 of file settingsstorage.cpp.

228 {
230  for (auto i = data.begin(); i != data.end(); ++i)
231  settings->setValue(i.key(), i.value());
232 
233  settings->sync(); // Important to get error status
234 
235  switch (settings->status())
236  {
237  case QSettings::NoError:
238  return settings->fileName();
239  case QSettings::AccessError:
240  Logger::instance()->addMessage(QObject::tr("An access error occurred while trying to write the configuration file."), Log::CRITICAL);
241  break;
242  case QSettings::FormatError:
243  Logger::instance()->addMessage(QObject::tr("A format error occurred while trying to write the configuration file."), Log::CRITICAL);
244  break;
245  default:
246  Logger::instance()->addMessage(QObject::tr("An unknown error occurred while trying to write the configuration file."), Log::CRITICAL);
247  break;
248  }
249  return {};
250 }
@ CRITICAL
Definition: logger.h:48

References Logger::addMessage(), Profile::applicationSettings(), Log::CRITICAL, Logger::instance(), Profile::instance(), and anonymous_namespace{addnewtorrentdialog.cpp}::settings().

Here is the call graph for this function:

◆ write()

bool TransactionalSettings::write ( const QVariantHash &  data) const

Definition at line 185 of file settingsstorage.cpp.

186 {
187  // QSettings deletes the file before writing it out. This can result in problems
188  // if the disk is full or a power outage occurs. Those events might occur
189  // between deleting the file and recreating it. This is a safety measure.
190  // Write everything to qBittorrent_new.ini/qBittorrent_new.conf and if it succeeds
191  // replace qBittorrent.ini/qBittorrent.conf with it.
192  const QString newPath = serialize(m_name + QLatin1String("_new"), data);
193  if (newPath.isEmpty())
194  {
195  Utils::Fs::forceRemove(newPath);
196  return false;
197  }
198 
199  QString finalPath = newPath;
200  int index = finalPath.lastIndexOf("_new", -1, Qt::CaseInsensitive);
201  finalPath.remove(index, 4);
202 
203  Utils::Fs::forceRemove(finalPath);
204  return QFile::rename(newPath, finalPath);
205 }
QString serialize(const QString &name, const QVariantHash &data) const

References Utils::Fs::forceRemove(), and serialize().

Here is the call graph for this function:

Member Data Documentation

◆ m_name

const QString anonymous_namespace{settingsstorage.cpp}::TransactionalSettings::m_name
private

Definition at line 65 of file settingsstorage.cpp.


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