WebUI: Improve table overflow handling
[qBittorrent.git] / src / base / settingsstorage.h
blob9163d605ae1736f25978fcab5d864cf4c6ddf59e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Mike Tzou (Chocobo1)
4 * Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
5 * Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #pragma once
33 #include <type_traits>
35 #include <QObject>
36 #include <QReadWriteLock>
37 #include <QTimer>
38 #include <QVariant>
39 #include <QVariantHash>
41 #include "base/concepts/stringable.h"
42 #include "utils/string.h"
44 template <typename T>
45 concept IsQFlags = std::same_as<T, QFlags<typename T::enum_type>>;
47 // There are 2 ways for class `T` provide serialization support into `SettingsStorage`:
48 // 1. If the `T` state is intended for users to edit (via a text editor), then
49 // `T` should satisfy `Stringable` concept
50 // 2. Otherwise, use `Q_DECLARE_METATYPE(T)` and let `QMetaType` handle the serialization
51 class SettingsStorage final : public QObject
53 Q_OBJECT
54 Q_DISABLE_COPY_MOVE(SettingsStorage)
56 SettingsStorage();
57 ~SettingsStorage();
59 public:
60 static void initInstance();
61 static void freeInstance();
62 static SettingsStorage *instance();
64 template <typename T>
65 T loadValue(const QString &key, const T &defaultValue = {}) const
67 if constexpr (std::same_as<T, QVariant>)
69 // fast path for loading QVariant
70 return loadValueImpl(key, defaultValue);
72 else if constexpr (Stringable<T>)
74 const QString value = loadValue(key, defaultValue.toString());
75 return T {value};
77 else if constexpr (std::is_enum_v<T>)
79 const auto value = loadValue<QString>(key);
80 return Utils::String::toEnum(value, defaultValue);
82 else if constexpr (IsQFlags<T>)
84 const typename T::Int value = loadValue(key, static_cast<typename T::Int>(defaultValue));
85 return T {value};
87 else
89 const QVariant value = loadValueImpl(key);
90 // check if retrieved value is convertible to T
91 return value.template canConvert<T>() ? value.template value<T>() : defaultValue;
95 template <typename T>
96 void storeValue(const QString &key, const T &value)
98 if constexpr (std::same_as<T, QVariant>)
99 storeValueImpl(key, value);
100 else if constexpr (Stringable<T>)
101 storeValueImpl(key, value.toString());
102 else if constexpr (std::is_enum_v<T>)
103 storeValueImpl(key, Utils::String::fromEnum(value));
104 else if constexpr (IsQFlags<T>)
105 storeValueImpl(key, static_cast<typename T::Int>(value));
106 else
107 storeValueImpl(key, QVariant::fromValue(value));
110 void removeValue(const QString &key);
111 bool hasKey(const QString &key) const;
112 bool isEmpty() const;
114 public slots:
115 bool save();
117 private:
118 QVariant loadValueImpl(const QString &key, const QVariant &defaultValue = {}) const;
119 void storeValueImpl(const QString &key, const QVariant &value);
120 void readNativeSettings();
121 bool writeNativeSettings() const;
123 static SettingsStorage *m_instance;
125 const QString m_nativeSettingsName;
126 bool m_dirty = false;
127 QVariantHash m_data;
128 QTimer m_timer;
129 mutable QReadWriteLock m_lock;