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
12class QVariant;
13
14namespace Quotient {
15
18public:
19 /// Add a legacy organisation/application name to migrate settings from
20 /*!
21 * Use this function before creating any Settings objects in order
22 * to set a legacy location where configuration has previously been stored.
23 * This will provide an additional fallback in case of renaming
24 * the organisation/application. Values in legacy locations are _removed_
25 * when setValue() or remove() is called.
26 */
28 const QString& applicationName = {});
29
30 explicit Settings(QObject* parent = nullptr);
31
32 /// Set the value for a given key
33 /*! If the key exists in the legacy location, it is removed. */
35
36 /// Remove the value from both the primary and legacy locations
38
39 /// Obtain a value for a given key
40 /*!
41 * If the key doesn't exist in the primary settings location, the legacy
42 * location is checked. If neither location has the key,
43 * \p defaultValue is returned.
44 *
45 * This function returns a QVariant; use get<>() to get the unwrapped
46 * value if you know the type upfront.
47 *
48 * \sa setLegacyNames, get
49 */
51
52 /// Obtain a value for a given key, coerced to the given type
53 /*!
54 * On top of value(), this function unwraps the QVariant and returns
55 * its contents assuming the type passed as the template parameter.
56 * If the type is different from the one stored inside the QVariant,
57 * \p defaultValue is returned. In presence of legacy settings,
58 * only the first found value is checked; if its type does not match,
59 * further checks through legacy settings are not performed and
60 * \p defaultValue is returned.
61 */
62 template <typename T>
63 T get(const QString& key, const T& defaultValue = {}) const
64 {
65 const auto qv = value(key);
66 return qv.isValid() && qv.canConvert<T>() ? qv.value<T>() : defaultValue;
67 }
68
69 Q_INVOKABLE bool contains(const QString& key) const;
70 //! \brief Obtain the list of child groups from the current or, if missing, legacy settings
71 //! \note Group names under `Accounts` group will be automatically unescaped
72 //! \sa AccountSettings
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
90public:
91 explicit SettingsGroup(QString path, QObject* parent = nullptr)
94 {}
95
96 Q_INVOKABLE bool contains(const QString& key) const;
98
99 template <typename T>
100 T get(const QString& key, const T& defaultValue = {}) const
101 {
102 const auto qv = value(key);
103 return qv.isValid() && qv.canConvert<T>() ? qv.value<T>() : defaultValue;
104 }
105
106 //! \brief Get the path for this settings group
107 //! \note Unlike Settings::childGroups(), this function will not unescape group names under
108 //! `Accounts`
112
114
115private:
116 QString fullPath(QAnyStringView key) const { return groupPath % u'/' % key.toString(); }
117
119};
120
121#define QUO_DECLARE_SETTING(type, propname, setter)
122 Q_PROPERTY(type propname READ propname WRITE setter) public
123 :
124 type propname() const;
125 void setter(type newValue);
126 private
127 :
128
129#define QUO_DEFINE_SETTING(classname, type, propname, qsettingname, defaultValue, setter)
130 type classname::propname() const { return get<type>(qsettingname##_L1, defaultValue); }
131 void classname::setter(type newValue) { setValue(qsettingname##_L1, std::move(newValue)); }
132
133//! \brief A group of settings for one Matrix account
134//!
135//! This class provides typesafe accessors to common account settings such as user and device id.
136//! User id (aka MXID) is stored as a group name. Although QSettings does not protect forward- and
137//! backslashes inside group names, AccountSettings covers for that, percent-encoding the user id
138//! before passing it to QSettings.
147public:
148 explicit AccountSettings(const QString& accountId, QObject* parent = nullptr)
150 {}
151
153
155 void setHomeserver(const QUrl& url);
156
160};
161} // namespace Quotient
A group of settings for one Matrix account.
Definition settings.h:139
#define QUOTIENT_API
#define QUO_DECLARE_SETTING(type, propname, setter)
Definition settings.h:121