libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
settings.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Kitsune Ral <kitsune-ral@users.sf.net>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include "util.h"
7
8#include <QtCore/QSettings>
9#include <QtCore/QUrl>
10#include <QtCore/QStringBuilder>
11
12#include <ranges>
13
14class QVariant;
15
16namespace Quotient {
17
20public:
21 /// Add a legacy organisation/application name to migrate settings from
22 /*!
23 * Use this function before creating any Settings objects in order
24 * to set a legacy location where configuration has previously been stored.
25 * This will provide an additional fallback in case of renaming
26 * the organisation/application. Values in legacy locations are _removed_
27 * when setValue() or remove() is called.
28 */
30
31 explicit Settings(QObject* parent = nullptr);
32
33 /// Set the value for a given key
34 /*! If the key exists in the legacy location, it is removed. */
36
37 /// Remove the value from both the primary and legacy locations
39
40 /// Obtain a value for a given key
41 /*!
42 * If the key doesn't exist in the primary settings location, the legacy
43 * location is checked. If neither location has the key,
44 * \p defaultValue is returned.
45 *
46 * This function returns a QVariant; use get<>() to get the unwrapped
47 * value if you know the type upfront.
48 *
49 * \sa setLegacyNames, get
50 */
52
53 /// Obtain a value for a given key, coerced to the given type
54 /*!
55 * On top of value(), this function unwraps the QVariant and returns
56 * its contents assuming the type passed as the template parameter.
57 * If the type is different from the one stored inside the QVariant,
58 * \p defaultValue is returned. In presence of legacy settings,
59 * only the first found value is checked; if its type does not match,
60 * further checks through legacy settings are not performed and
61 * \p defaultValue is returned.
62 */
63 template <typename T>
64 T get(const QString& key, const T& defaultValue = {}) const
65 {
66 const auto qv = value(key);
67 return qv.isValid() && qv.canConvert<T>() ? qv.value<T>() : defaultValue;
68 }
69
70 Q_INVOKABLE bool contains(const QString& key) const;
71
74
75 //! Escape forward- and backslashes in keys because QSettings doesn't (see #842)
77
78 //! Unescape `\` and `/` in keys stored with escapedForSettings()
80
81private:
84
85protected:
87};
88
91public:
92 explicit SettingsGroup(const QString& path, QObject* parent = nullptr);
93};
94
95#define QUO_DECLARE_SETTING(type, propname, setter)
96 Q_PROPERTY(type propname READ propname WRITE setter) public
97 :
98 type propname() const;
99 void setter(type newValue);
100 private
101 :
102
103#define QUO_DEFINE_SETTING(classname, type, propname, qsettingname, defaultValue, setter)
104 type classname::propname() const { return get<type>(qsettingname##_L1, defaultValue); }
105 void classname::setter(type newValue) { setValue(qsettingname##_L1, std::move(newValue)); }
106
107//! \brief A group of settings for one Matrix account
108//!
109//! This class provides typesafe accessors to common account settings such as user and device id.
110//! User id (aka MXID) is stored as a group name. Although QSettings does not protect forward- and
111//! backslashes inside group names, AccountSettings covers for that, percent-encoding the user id
112//! before passing it to QSettings.
121public:
122 explicit AccountSettings(const QString& accountId, QObject* parent = nullptr);
123
125
127 void setHomeserver(const QUrl& url);
128
132};
133
136public:
137 static auto name() { return u"Accounts"_s; }
138
139 explicit AccountSettingsGroup(QObject* parent = nullptr);
140
141 auto asRange() const
142 {
143 return std::views::transform(accountNames(),
144 [](const QString& mxid) { return AccountSettings(mxid); });
145 }
146
147 //! \brief Obtain the list of child groups from the current or, if missing, legacy settings
148 //! \note Slashes in account names will be automatically unescaped
149 //! \sa AccountSettings
151};
152
153} // namespace Quotient
A group of settings for one Matrix account.
Definition settings.h:113
#define QUOTIENT_API
#define QUO_DECLARE_SETTING(type, propname, setter)
Definition settings.h:95