libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
roommember.h
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
2
// SPDX-License-Identifier: LGPL-2.1-or-later
3
4
#
pragma
once
5
6
#
include
"quotient_common.h"
7
#
include
"uri.h"
8
#
include
"avatar.h"
9
10
#
include
<
QtCore
/
QObject
>
11
#
include
<
QtQmlIntegration
/
qqmlintegration
.
h
>
12
13
namespace
Quotient
{
14
class
Room;
15
class
RoomMemberEvent;
16
17
//! \brief Representation of a user state in a room
18
//!
19
//! The class is intentionally a read-only data object that is effectively a wrapper around an
20
//! `m.room.member` event for the desired user. This is designed to provide the data in a format
21
//! ready for visualizing a user (avatar or name) in the context of the room it was generated in.
22
//! This means that if a user has set a unique name or avatar for a particular room that is what
23
//! will be returned.
24
//!
25
//! \note The RoomMember class is not intended for interacting with the user's profile.
26
//! For that a Quotient::User object should be obtained from a Quotient::Connection as that
27
//! has the support functions for modifying profile information.
28
//! \warning RoomMember is a gadget class and should not be kept between syncs. It does not track
29
//! changes of the member state therefore some member changes (i.e. leaving the room) may
30
//! render a RoomMember dangling, when calling any of its methods leads to undefined
31
//! behaviour.
32
//! \sa User
33
class
QUOTIENT_API
RoomMember
{
34
Q_GADGET
35
QML_NAMED_ELEMENT
(
roomMember
)
36
QML_UNCREATABLE
(
""
)
37
38
Q_PROPERTY
(
bool
isEmpty
READ
isEmpty
CONSTANT
)
39
Q_PROPERTY
(
QString
id
READ
id
CONSTANT
)
40
Q_PROPERTY
(
Uri
uri
READ
uri
CONSTANT
)
41
Q_PROPERTY
(
bool
isLocalMember
READ
isLocalMember
CONSTANT
)
42
Q_PROPERTY
(
QString
displayName
READ
displayName
CONSTANT
)
43
Q_PROPERTY
(
QString
htmlSafeDisplayName
READ
htmlSafeDisplayName
CONSTANT
)
44
Q_PROPERTY
(
QString
fullName
READ
fullName
CONSTANT
)
45
Q_PROPERTY
(
QString
htmlSafeFullName
READ
htmlSafeFullName
CONSTANT
)
46
Q_PROPERTY
(
QString
disambiguatedName
READ
disambiguatedName
CONSTANT
)
47
Q_PROPERTY
(
QString
htmlSafeDisambiguatedName
READ
htmlSafeDisambiguatedName
CONSTANT
)
48
Q_PROPERTY
(
int
hue
READ
hue
CONSTANT
)
49
Q_PROPERTY
(
qreal
hueF
READ
hueF
CONSTANT
)
50
Q_PROPERTY
(
QColor
color
READ
color
CONSTANT
)
51
Q_PROPERTY
(
QUrl
avatarUrl
READ
avatarUrl
CONSTANT
)
52
Q_PROPERTY
(
int
powerLevel
READ
powerLevel
CONSTANT
)
53
54
public
:
55
RoomMember
() =
default
;
56
57
explicit
RoomMember
(
const
Room
*
room
,
const
RoomMemberEvent
*
member
);
58
59
bool
isEmpty
()
const
{
return
_member
==
nullptr
; }
60
61
bool
operator
==(
const
RoomMember
&
other
)
const
;
62
63
//! @brief Get unique stable user id
64
//!
65
//! The Matrix user ID is generated by the server and is never changed.
66
QString
id
()
const
;
67
68
//! @brief The matrix.to URI for the user
69
//!
70
//! Typically used when you want to visit a user (see
71
//! Quotient::UriResolverBase::visitResource()).
72
//!
73
//! @sa Quotient::UriResolverBase::visitResource()
74
Uri
uri
()
const
;
75
76
//! Whether this member is the local user
77
bool
isLocalMember
()
const
;
78
79
//! The membership state of the member
80
Membership
membershipState
()
const
;
81
82
//! \brief The raw unmodified display name for the user in the given room
83
//!
84
//! The value will be empty if no display name has been set.
85
//!
86
//! \warning This value is not sanitized or HTML escape so use appropriately.
87
//! For ready to display values use displayName() or fullName() for
88
//! plain text and htmlSafeDisplayName() or htmlSafeFullName() fo
89
//! rich text.
90
//!
91
//! \sa displayName(), htmlSafeDisplayName(), fullName(), htmlSafeFullName()
92
QString
name
()
const
;
93
94
//! \brief Get the user display name ready for display
95
//!
96
//! This function always aims to return something that can be displayed in a
97
//! UI, so if no display name is set the user's Matrix ID will be returned.
98
//!
99
//! The output is sanitized and suitable for a plain text field. For a rich
100
//! field use htmlSafeDisplayName().
101
//!
102
//! \sa htmlSafeDisplayName()
103
QString
displayName
()
const
;
104
105
//! \brief Get the user display name ready for display
106
//!
107
//! This function always aims to return something that can be displayed in a
108
//! UI, so if no display name is set the user's Matrix ID will be returned.
109
//!
110
//! The output is sanitized and html escaped ready for a rich text field. For
111
//! a plain field use displayName().
112
//!
113
//! \sa displayName()
114
QString
htmlSafeDisplayName
()
const
;
115
116
//! \brief Get user name and id in a single string
117
//!
118
//! This function always aims to return something that can be displayed in a
119
//! UI, so if no display name is set the just user's Matrix ID will be returned.
120
//! The constructed string follows the format 'name (id)' which the spec
121
//! recommends for users disambiguation in a room context and in other places.
122
//!
123
//! The output is sanitized and suitable for a plain text field. For a rich
124
//! field use htmlSafeFullName().
125
//!
126
//! \sa htmlSafeFullName()
127
QString
fullName
()
const
;
128
129
//! \brief Get user name and id in a single string
130
//!
131
//! This function always aims to return something that can be displayed in a
132
//! UI, so if no display name is set the just user's Matrix ID will be returned.
133
//! The constructed string follows the format 'name (id)' which the spec
134
//! recommends for users disambiguation in a room context and in other places.
135
//!
136
//! The output is sanitized and html escaped ready for a rich text field. For
137
//! a plain field use fullName().
138
//!
139
//! \sa fullName()
140
QString
htmlSafeFullName
()
const
;
141
142
//! \brief Get the disambiguated user name
143
//!
144
//! This function always aims to return something that can be displayed in a
145
//! UI, so if no display name is set the just user's Matrix ID will be returned.
146
//! The output is equivalent to fullName() if there is another user in the room
147
//! with the same name. Otherwise it is equivalent to displayName().
148
//!
149
//! The output is sanitized and suitable for a plain text field. For a rich
150
//! field use htmlSafeDisambiguatedName().
151
//!
152
//! \sa htmlSafeDisambiguatedName(), fullName(), displayName()
153
QString
disambiguatedName
()
const
;
154
155
//! \brief Get the disambiguated user name
156
//!
157
//! This function always aims to return something that can be displayed in a
158
//! UI, so if no display name is set the just user's Matrix ID will be returned.
159
//! The output is equivalent to htmlSafeFullName() if there is another user in the room
160
//! with the same name. Otherwise it is equivalent to htmlSafeDisplayName().
161
//!
162
//! The output is sanitized and html escaped ready for a rich text field. For
163
//! a plain field use disambiguatedName().
164
//!
165
//! \sa disambiguatedName(), htmlSafeFullName(), htmlSafeDisplayName()
166
QString
htmlSafeDisambiguatedName
()
const
;
167
168
//! \brief Check whether the name or id of the member contains a substring
169
//!
170
//! This is useful for a predicate to filter room members.
171
//! \sa MemberMatcher
172
Q_INVOKABLE
bool
matches
(
QStringView
substr
,
Qt
::
CaseSensitivity
cs
=
Qt
::
CaseSensitive
)
const
;
173
174
//! \brief Hue color component of this user based on the user's Matrix ID
175
//!
176
//! The implementation is based on XEP-0392:
177
//! https://xmpp.org/extensions/xep-0392.html
178
//! Naming and ranges are the same as QColor's hue methods:
179
//! https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
180
int
hue
()
const
;
181
182
//! \brief HueF color component of this user based on the user's Matrix ID
183
//!
184
//! The implementation is based on XEP-0392:
185
//! https://xmpp.org/extensions/xep-0392.html
186
//! Naming and ranges are the same as QColor's hue methods:
187
//! https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
188
qreal
hueF
()
const
;
189
190
//! \brief Color based on the user's Matrix ID
191
//!
192
//! See https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal
193
//! for the methodology.
194
QColor
color
()
const
;
195
196
const
Avatar
&
avatarObject
()
const
;
197
198
//! \brief The mxc URL as a string for the user avatar in the room
199
//!
200
//! This can be empty if none set.
201
QString
avatarMediaId
()
const
;
202
203
//! \brief The mxc URL for the user avatar in the room
204
//!
205
//! This can be empty if none set.
206
QUrl
avatarUrl
()
const
;
207
208
QImage
avatar
(
int
width
,
int
height
,
Avatar
::
get_callback_t
callback
)
const
;
209
210
QImage
avatar
(
int
dimension
,
Avatar
::
get_callback_t
callback
)
const
;
211
212
//! \brief The power level of the member.
213
//!
214
//! This is in the context of the current room. Will return the default power
215
//! level for the room if not specifically set.
216
int
powerLevel
()
const
;
217
218
private
:
219
const
Room
*
_room
=
nullptr
;
220
const
RoomMemberEvent
*
_member
=
nullptr
;
221
222
qreal
_hueF
= 0;
223
};
224
225
//! \brief A factory to get a functional object matching room members against a substring
226
//!
227
//! This is a convenience wrapper to use RoomMember::matches() in standard algorithms.
228
inline
auto
memberMatcher
(
auto
substr, Qt::CaseSensitivity cs =
Qt
::
CaseSensitive
)
229
{
230
#
ifdef
__cpp_lib_bind_back
231
return
std::bind_back(&RoomMember::matches, substr, cs);
232
#
else
233
return
[substr, cs](
const
RoomMember& m) {
return
m.matches(substr, cs); };
234
#
endif
235
}
236
237
struct
QUOTIENT_API
MemberSorter
{
238
bool
operator
()(
const
RoomMember
&
u1
,
const
RoomMember
&
u2
)
const
239
{
240
return
operator
()(
u1
.
displayName
(),
u2
.
displayName
());
241
}
242
bool
operator
()(
QStringView
u1name
,
QStringView
u2name
)
const
;
243
};
244
245
}
// namespace Quotient
Quotient::RoomMember
Representation of a user state in a room.
Definition
roommember.h:33
Quotient
Definition
accountregistry.h:13
Quotient::memberMatcher
auto memberMatcher(auto substr, Qt::CaseSensitivity cs=Qt::CaseSensitive)
A factory to get a functional object matching room members against a substring.
Definition
roommember.h:228
QUOTIENT_API
#define QUOTIENT_API
Definition
quotient_export.h:22
Quotient::MemberSorter
Definition
roommember.h:237
Quotient
roommember.h
Generated by
1.9.8