qBittorrent
settingsstorage.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
4  * Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL". If you
25  * modify file(s), you may extend this exception to your version of the file(s),
26  * but you are not obligated to do so. If you do not wish to do so, delete this
27  * exception statement from your version.
28  */
29 
30 #pragma once
31 
32 #include <type_traits>
33 
34 #include <QObject>
35 #include <QReadWriteLock>
36 #include <QTimer>
37 #include <QVariantHash>
38 
39 #include "utils/string.h"
40 
41 template <typename T>
42 struct IsQFlags : std::false_type {};
43 
44 template <typename T>
45 struct IsQFlags<QFlags<T>> : std::true_type {};
46 
47 class SettingsStorage : public QObject
48 {
49  Q_OBJECT
52 
53 public:
54  static void initInstance();
55  static void freeInstance();
56  static SettingsStorage *instance();
57 
58  template <typename T>
59  T loadValue(const QString &key, const T &defaultValue = {}) 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  }
83 
84  template <typename T>
85  void storeValue(const QString &key, const T &value)
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  }
94 
95  void removeValue(const QString &key);
96  bool hasKey(const QString &key) const;
97 
98 public slots:
99  bool save();
100 
101 private:
102  QVariant loadValueImpl(const QString &key, const QVariant &defaultValue = {}) const;
103  void storeValueImpl(const QString &key, const QVariant &value);
104 
106 
107  bool m_dirty = false;
108  QVariantHash m_data;
109  QTimer m_timer;
110  mutable QReadWriteLock m_lock;
111 };
QReadWriteLock m_lock
void storeValue(const QString &key, const T &value)
T loadValue(const QString &key, const T &defaultValue={}) const
static SettingsStorage * instance()
void storeValueImpl(const QString &key, const QVariant &value)
void removeValue(const QString &key)
static SettingsStorage * m_instance
static void freeInstance()
static void initInstance()
bool hasKey(const QString &key) const
QVariantHash m_data
QVariant loadValueImpl(const QString &key, const QVariant &defaultValue={}) const
QString fromEnum(const T &value)
Definition: string.h:67
T toEnum(const QString &serializedValue, const T &defaultValue)
Definition: string.h:77
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64