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
class
QVariant;
13
14
namespace
Quotient
{
15
16
class
QUOTIENT_API
Settings
:
public
QSettings
{
17
Q_OBJECT
18
public
:
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
*/
27
static
void
setLegacyNames
(
const
QString
&
organizationName
,
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. */
34
Q_INVOKABLE
void
setValue
(
const
QString
&
key
,
const
QVariant
&
value
);
35
36
/// Remove the value from both the primary and legacy locations
37
Q_INVOKABLE
void
remove
(
const
QString
&
key
);
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
*/
50
Q_INVOKABLE
QVariant
value
(
const
QString
&
key
,
const
QVariant
&
defaultValue
= {})
const
;
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
73
Q_INVOKABLE
QStringList
childGroups
()
const
;
74
75
//! Escape forward- and backslashes in keys because QSettings doesn't (see #842)
76
static
QString
escapedForSettings
(
QString
key
);
77
78
//! Unescape `\` and `/` in keys stored with escapedForSettings()
79
static
QString
unescapedFromSettings
(
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
public
:
91
explicit
SettingsGroup
(
QString
path
,
QObject
*
parent
=
nullptr
)
92
:
Settings
(
parent
)
93
,
groupPath
(
std
::
move
(
path
))
94
{}
95
96
Q_INVOKABLE
bool
contains
(
const
QString
&
key
)
const
;
97
Q_INVOKABLE
QVariant
value
(
const
QString
&
key
,
const
QVariant
&
defaultValue
= {})
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`
109
Q_INVOKABLE
QString
group
()
const
;
110
Q_INVOKABLE
QStringList
childGroups
()
const
;
111
Q_INVOKABLE
void
setValue
(
const
QString
&
key
,
const
QVariant
&
value
);
112
113
Q_INVOKABLE
void
remove
(
const
QString
&
key
);
114
115
private
:
116
QString
fullPath
(
QAnyStringView
key
)
const
{
return
groupPath
% u'/' %
key
.
toString
(); }
117
118
QString
groupPath
;
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.
139
class
QUOTIENT_API
AccountSettings
:
public
SettingsGroup
{
140
Q_OBJECT
141
Q_PROPERTY
(
QString
userId
READ
userId
CONSTANT
)
142
QUO_DECLARE_SETTING
(
QString
,
deviceId
,
setDeviceId
)
143
QUO_DECLARE_SETTING
(
QString
,
deviceName
,
setDeviceName
)
144
QUO_DECLARE_SETTING
(
bool
,
keepLoggedIn
,
setKeepLoggedIn
)
145
Q_PROPERTY
(
QByteArray
encryptionAccountPickle
READ
encryptionAccountPickle
146
WRITE
setEncryptionAccountPickle
)
147
public
:
148
explicit
AccountSettings
(
const
QString
&
accountId
,
QObject
*
parent
=
nullptr
)
149
:
SettingsGroup
(
"Accounts/"_L1
+
escapedForSettings
(
accountId
),
parent
)
150
{}
151
152
QString
userId
()
const
;
153
154
QUrl
homeserver
()
const
;
155
void
setHomeserver
(
const
QUrl
&
url
);
156
157
QByteArray
encryptionAccountPickle
();
158
void
setEncryptionAccountPickle
(
const
QByteArray
&
encryptionAccountPickle
);
159
Q_INVOKABLE
void
clearEncryptionAccountPickle
();
160
};
161
}
// namespace Quotient
Quotient::AccountSettings
A group of settings for one Matrix account.
Definition
settings.h:139
Quotient::SettingsGroup
Definition
settings.h:89
Quotient::Settings
Definition
settings.h:16
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:121
Quotient
settings.h
Generated by
1.9.8