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
14
class
QVariant;
15
16
namespace
Quotient
{
17
18
class
QUOTIENT_API
Settings
:
public
QSettings
{
19
Q_OBJECT
20
public
:
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
*/
29
static
void
setLegacyNames
(
const
QString
&
organizationName
,
const
QString
&
applicationName
= {});
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. */
35
Q_INVOKABLE
void
setValue
(
const
QString
&
key
,
const
QVariant
&
value
);
36
37
/// Remove the value from both the primary and legacy locations
38
Q_INVOKABLE
void
remove
(
const
QString
&
key
);
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
*/
51
Q_INVOKABLE
QVariant
value
(
const
QString
&
key
,
const
QVariant
&
defaultValue
= {})
const
;
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
72
Q_INVOKABLE
QStringList
childGroups
()
const
;
73
Q_INVOKABLE
QStringList
childGroups
(
bool
decodeSlashes
)
const
;
74
75
//! Escape forward- and backslashes in keys because QSettings doesn't (see #842)
76
static
QString
toEncoded
(
QString
key
);
77
78
//! Unescape `\` and `/` in keys stored with escapedForSettings()
79
static
QString
fromEncoded
(
QString
key
);
80
81
private
:
82
static
QString
legacyOrganizationName
;
83
static
QString
legacyApplicationName
;
84
85
protected
:
86
QSettings
legacySettings
{
legacyOrganizationName
,
legacyApplicationName
};
87
};
88
89
class
QUOTIENT_API
SettingsGroup
:
public
Settings
{
90
Q_OBJECT
91
public
:
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.
113
class
QUOTIENT_API
AccountSettings
:
public
SettingsGroup
{
114
Q_OBJECT
115
Q_PROPERTY
(
QString
userId
READ
userId
CONSTANT
)
116
QUO_DECLARE_SETTING
(
QString
,
deviceId
,
setDeviceId
)
117
QUO_DECLARE_SETTING
(
QString
,
deviceName
,
setDeviceName
)
118
QUO_DECLARE_SETTING
(
bool
,
keepLoggedIn
,
setKeepLoggedIn
)
119
Q_PROPERTY
(
QByteArray
encryptionAccountPickle
READ
encryptionAccountPickle
120
WRITE
setEncryptionAccountPickle
)
121
public
:
122
explicit
AccountSettings
(
const
QString
&
accountId
,
QObject
*
parent
=
nullptr
);
123
124
QString
userId
()
const
;
125
126
QUrl
homeserver
()
const
;
127
void
setHomeserver
(
const
QUrl
&
url
);
128
129
QByteArray
encryptionAccountPickle
();
130
void
setEncryptionAccountPickle
(
const
QByteArray
&
encryptionAccountPickle
);
131
Q_INVOKABLE
void
clearEncryptionAccountPickle
();
132
};
133
134
class
QUOTIENT_API
AccountSettingsGroup
:
public
SettingsGroup
{
135
Q_OBJECT
136
public
:
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
150
Q_INVOKABLE
QStringList
accountNames
()
const
;
151
};
152
153
}
// namespace Quotient
Quotient::AccountSettingsGroup
Definition
settings.h:134
Quotient::AccountSettings
A group of settings for one Matrix account.
Definition
settings.h:113
Quotient::SettingsGroup
Definition
settings.h:89
Quotient::Settings
Definition
settings.h:18
Quotient
Definition
accountregistry.h:13
QUOTIENT_API
#define QUOTIENT_API
Definition
quotient_export.h:22
QUO_DECLARE_SETTING
#define QUO_DECLARE_SETTING(type, propname, setter)
Definition
settings.h:95
Quotient
settings.h
Generated by
1.9.8