1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// SPDX-FileCopyrightText: 2020 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppPubSubAffiliation.h"
#include "QXmppConstants_p.h"
#include "QXmppUtils.h"
#include <QDomElement>
#include <QXmlStreamWriter>
///
/// \class QXmppPubSubAffiliation
///
/// This class represents an affiliation of a user with a PubSub node as defined
/// in \xep{0060, Publish-Subscribe}.
///
/// \sa QXmppPubSubIq
/// \sa QXmppPubSubEvent
///
/// \since QXmpp 1.5
///
static const QStringList PUBSUB_AFFILIATIONS = {
QStringLiteral("none"),
QStringLiteral("member"),
QStringLiteral("outcast"),
QStringLiteral("owner"),
QStringLiteral("publisher"),
QStringLiteral("publish-only"),
};
class QXmppPubSubAffiliationPrivate : public QSharedData
{
public:
QXmppPubSubAffiliationPrivate(QXmppPubSubAffiliation::Affiliation type,
const QString &node,
const QString &jid);
QXmppPubSubAffiliation::Affiliation type;
QString node;
QString jid;
};
QXmppPubSubAffiliationPrivate::QXmppPubSubAffiliationPrivate(QXmppPubSubAffiliation::Affiliation type,
const QString &node,
const QString &jid)
: type(type),
node(node),
jid(jid)
{
}
///
/// Default constructor.
///
QXmppPubSubAffiliation::QXmppPubSubAffiliation(Affiliation type,
const QString &node,
const QString &jid)
: d(new QXmppPubSubAffiliationPrivate(type, node, jid))
{
}
/// Copy constructor.
QXmppPubSubAffiliation::QXmppPubSubAffiliation(const QXmppPubSubAffiliation &) = default;
/// Move-constructor.
QXmppPubSubAffiliation::QXmppPubSubAffiliation(QXmppPubSubAffiliation &&) = default;
QXmppPubSubAffiliation::~QXmppPubSubAffiliation() = default;
/// Assignment operator.
QXmppPubSubAffiliation &QXmppPubSubAffiliation::operator=(const QXmppPubSubAffiliation &) = default;
/// Move-assignment operator.
QXmppPubSubAffiliation &QXmppPubSubAffiliation::operator=(QXmppPubSubAffiliation &&) = default;
///
/// Returns the type of the affiliation.
///
QXmppPubSubAffiliation::Affiliation QXmppPubSubAffiliation::type() const
{
return d->type;
}
///
/// Sets the type of the affiliation.
///
void QXmppPubSubAffiliation::setType(Affiliation type)
{
d->type = type;
}
///
/// Returns the node name of the node the affiliation belongs to.
///
QString QXmppPubSubAffiliation::node() const
{
return d->node;
}
///
/// Sets the node name.
///
void QXmppPubSubAffiliation::setNode(const QString &node)
{
d->node = node;
}
///
/// Returns the JID of the user.
///
QString QXmppPubSubAffiliation::jid() const
{
return d->jid;
}
///
/// Sets the JID of the user.
///
void QXmppPubSubAffiliation::setJid(const QString &jid)
{
d->jid = jid;
}
///
/// Returns true if the DOM element is a PubSub affiliation.
///
bool QXmppPubSubAffiliation::isAffiliation(const QDomElement &element)
{
if (element.tagName() != QStringLiteral("affiliation") ||
!PUBSUB_AFFILIATIONS.contains(element.attribute(QStringLiteral("affiliation")))) {
return false;
}
if (element.namespaceURI() == ns_pubsub) {
return element.hasAttribute(QStringLiteral("node"));
}
if (element.namespaceURI() == ns_pubsub_owner) {
return element.hasAttribute(QStringLiteral("jid"));
}
return false;
}
/// \cond
void QXmppPubSubAffiliation::parse(const QDomElement &element)
{
if (const auto typeIndex = PUBSUB_AFFILIATIONS.indexOf(element.attribute(QStringLiteral("affiliation")));
typeIndex != -1) {
d->type = Affiliation(typeIndex);
} else {
// this can only happen, when isAffiliation() returns false
d->type = None;
}
d->node = element.attribute(QStringLiteral("node"));
d->jid = element.attribute(QStringLiteral("jid"));
}
void QXmppPubSubAffiliation::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("affiliation"));
writer->writeAttribute(QStringLiteral("affiliation"), PUBSUB_AFFILIATIONS.at(int(d->type)));
helperToXmlAddAttribute(writer, QStringLiteral("node"), d->node);
helperToXmlAddAttribute(writer, QStringLiteral("jid"), d->jid);
writer->writeEndElement();
}
/// \endcond
|