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.
32 #include <type_traits>
35 #include <QReadWriteLock>
38 #include <QVariantHash>
40 #include "base/interfaces/istringable.h"
41 #include "utils/string.h"
44 struct IsQFlags
: std::false_type
{};
47 struct IsQFlags
<QFlags
<T
>> : std::true_type
{};
49 // There are 2 ways for class `T` provide serialization support into `SettingsStorage`:
50 // 1. If the `T` state is intended for users to edit (via a text editor), then
51 // implement `IStringable` interface
52 // 2. Otherwise, use `Q_DECLARE_METATYPE(T)` and let `QMetaType` handle the serialization
53 class SettingsStorage final
: public QObject
56 Q_DISABLE_COPY_MOVE(SettingsStorage
)
62 static void initInstance();
63 static void freeInstance();
64 static SettingsStorage
*instance();
67 T
loadValue(const QString
&key
, const T
&defaultValue
= {}) const
69 if constexpr (std::is_base_of_v
<IStringable
, T
>)
71 const QString value
= loadValue(key
, defaultValue
.toString());
74 else if constexpr (std::is_enum_v
<T
>)
76 const auto value
= loadValue
<QString
>(key
);
77 return Utils::String::toEnum(value
, defaultValue
);
79 else if constexpr (IsQFlags
<T
>::value
)
81 const typename
T::Int value
= loadValue(key
, static_cast<typename
T::Int
>(defaultValue
));
84 else if constexpr (std::is_same_v
<T
, QVariant
>)
86 // fast path for loading QVariant
87 return loadValueImpl(key
, defaultValue
);
91 const QVariant value
= loadValueImpl(key
);
92 // check if retrieved value is convertible to T
93 return value
.template canConvert
<T
>() ? value
.template value
<T
>() : defaultValue
;
98 void storeValue(const QString
&key
, const T
&value
)
100 if constexpr (std::is_base_of_v
<IStringable
, 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
>::value
)
105 storeValueImpl(key
, static_cast<typename
T::Int
>(value
));
107 storeValueImpl(key
, QVariant::fromValue(value
));
110 void removeValue(const QString
&key
);
111 bool hasKey(const QString
&key
) const;
117 QVariant
loadValueImpl(const QString
&key
, const QVariant
&defaultValue
= {}) const;
118 void storeValueImpl(const QString
&key
, const QVariant
&value
);
119 void readNativeSettings();
120 bool writeNativeSettings() const;
122 static SettingsStorage
*m_instance
;
124 const QString m_nativeSettingsName
;
125 bool m_dirty
= false;
128 mutable QReadWriteLock m_lock
;