Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / settingsstorage.h
blobf9263a27fe09bfc1dc9a23da93ac59a448668799
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>
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.
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.
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.
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.
30 #pragma once
32 #include <type_traits>
34 #include <QObject>
35 #include <QReadWriteLock>
36 #include <QTimer>
37 #include <QVariantHash>
39 #include "path.h"
40 #include "utils/string.h"
42 template <typename T>
43 struct IsQFlags : std::false_type {};
45 template <typename T>
46 struct IsQFlags<QFlags<T>> : std::true_type {};
48 class SettingsStorage : public QObject
50 Q_OBJECT
51 SettingsStorage();
52 ~SettingsStorage();
54 public:
55 static void initInstance();
56 static void freeInstance();
57 static SettingsStorage *instance();
59 template <typename T>
60 T loadValue(const QString &key, const T &defaultValue = {}) const
62 if constexpr (std::is_enum_v<T>)
64 const auto value = loadValue<QString>(key);
65 return Utils::String::toEnum(value, defaultValue);
67 else if constexpr (IsQFlags<T>::value)
69 const typename T::Int value = loadValue(key, static_cast<typename T::Int>(defaultValue));
70 return T {value};
72 else if constexpr (std::is_same_v<T, Path>)
74 const auto value = loadValue<QString>(key, defaultValue.toString());
75 return Path(value);
77 else if constexpr (std::is_same_v<T, QVariant>)
79 // fast path for loading QVariant
80 return loadValueImpl(key, defaultValue);
82 else
84 const QVariant value = loadValueImpl(key);
85 // check if retrieved value is convertible to T
86 return value.template canConvert<T>() ? value.template value<T>() : defaultValue;
90 template <typename T>
91 void storeValue(const QString &key, const T &value)
93 if constexpr (std::is_enum_v<T>)
94 storeValueImpl(key, Utils::String::fromEnum(value));
95 else if constexpr (IsQFlags<T>::value)
96 storeValueImpl(key, static_cast<typename T::Int>(value));
97 else if constexpr (std::is_same_v<T, Path>)
98 storeValueImpl(key, value.toString());
99 else
100 storeValueImpl(key, QVariant::fromValue(value));
103 void removeValue(const QString &key);
104 bool hasKey(const QString &key) const;
106 public slots:
107 bool save();
109 private:
110 QVariant loadValueImpl(const QString &key, const QVariant &defaultValue = {}) const;
111 void storeValueImpl(const QString &key, const QVariant &value);
113 static SettingsStorage *m_instance;
115 bool m_dirty = false;
116 QVariantHash m_data;
117 QTimer m_timer;
118 mutable QReadWriteLock m_lock;