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>
37 #include <QVariantHash>
40 #include "utils/string.h"
43 struct IsQFlags
: std::false_type
{};
46 struct IsQFlags
<QFlags
<T
>> : std::true_type
{};
48 class SettingsStorage
: public QObject
55 static void initInstance();
56 static void freeInstance();
57 static SettingsStorage
*instance();
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
));
72 else if constexpr (std::is_same_v
<T
, Path
>)
74 const auto value
= loadValue
<QString
>(key
, defaultValue
.toString());
77 else if constexpr (std::is_same_v
<T
, QVariant
>)
79 // fast path for loading QVariant
80 return loadValueImpl(key
, defaultValue
);
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
;
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());
100 storeValueImpl(key
, QVariant::fromValue(value
));
103 void removeValue(const QString
&key
);
104 bool hasKey(const QString
&key
) const;
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;
118 mutable QReadWriteLock m_lock
;