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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppPubSubIq.h"
#include "QXmppConstants_p.h"
#include "QXmppUtils.h"
#include <QDomElement>
#include <QSharedData>
/// \cond
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
static const QStringList PUBSUB_QUERIES = {
QStringLiteral("affiliations"),
QStringLiteral("default"),
QStringLiteral("items"),
QStringLiteral("publish"),
QStringLiteral("retract"),
QStringLiteral("subscribe"),
QStringLiteral("subscription"),
QStringLiteral("subscriptions"),
QStringLiteral("unsubscribe"),
};
class QXmppPubSubIqPrivate : public QSharedData
{
public:
QXmppPubSubIqPrivate();
QXmppPubSubIq::QueryType queryType;
QString queryJid;
QString queryNode;
QList<QXmppPubSubItem> items;
QString subscriptionId;
QString subscriptionType;
};
QXmppPubSubIqPrivate::QXmppPubSubIqPrivate()
: queryType(QXmppPubSubIq::ItemsQuery)
{
}
QXmppPubSubIq::QXmppPubSubIq()
: d(new QXmppPubSubIqPrivate)
{
}
QXmppPubSubIq::QXmppPubSubIq(const QXmppPubSubIq &iq) = default;
QXmppPubSubIq::~QXmppPubSubIq() = default;
QXmppPubSubIq &QXmppPubSubIq::operator=(const QXmppPubSubIq &iq) = default;
/// Returns the PubSub queryType for this IQ.
QXmppPubSubIq::QueryType QXmppPubSubIq::queryType() const
{
return d->queryType;
}
/// Sets the PubSub queryType for this IQ.
///
/// \param queryType
void QXmppPubSubIq::setQueryType(QXmppPubSubIq::QueryType queryType)
{
d->queryType = queryType;
}
/// Returns the JID being queried.
QString QXmppPubSubIq::queryJid() const
{
return d->queryJid;
}
/// Sets the JID being queried.
///
/// \param queryJid
void QXmppPubSubIq::setQueryJid(const QString &queryJid)
{
d->queryJid = queryJid;
}
/// Returns the node being queried.
QString QXmppPubSubIq::queryNode() const
{
return d->queryNode;
}
/// Sets the node being queried.
///
/// \param queryNode
void QXmppPubSubIq::setQueryNode(const QString &queryNode)
{
d->queryNode = queryNode;
}
/// Returns the subscription ID.
QString QXmppPubSubIq::subscriptionId() const
{
return d->subscriptionId;
}
/// Sets the subscription ID.
///
/// \param subscriptionId
void QXmppPubSubIq::setSubscriptionId(const QString &subscriptionId)
{
d->subscriptionId = subscriptionId;
}
/// Returns the IQ's items.
QList<QXmppPubSubItem> QXmppPubSubIq::items() const
{
return d->items;
}
/// Sets the IQ's items.
///
/// \param items
void QXmppPubSubIq::setItems(const QList<QXmppPubSubItem> &items)
{
d->items = items;
}
bool QXmppPubSubIq::isPubSubIq(const QDomElement &element)
{
return element.firstChildElement(QStringLiteral("pubsub")).namespaceURI() == ns_pubsub;
}
void QXmppPubSubIq::parseElementFromChild(const QDomElement &element)
{
const QDomElement pubSubElement = element.firstChildElement(QStringLiteral("pubsub"));
const QDomElement queryElement = pubSubElement.firstChildElement();
// determine query type
const QString tagName = queryElement.tagName();
int queryType = PUBSUB_QUERIES.indexOf(queryElement.tagName());
if (queryType > -1) {
d->queryType = QueryType(queryType);
}
d->queryJid = queryElement.attribute(QStringLiteral("jid"));
d->queryNode = queryElement.attribute(QStringLiteral("node"));
// parse contents
QDomElement childElement;
switch (d->queryType) {
case QXmppPubSubIq::ItemsQuery:
case QXmppPubSubIq::PublishQuery:
case QXmppPubSubIq::RetractQuery:
childElement = queryElement.firstChildElement(QStringLiteral("item"));
while (!childElement.isNull()) {
QXmppPubSubItem item;
item.parse(childElement);
d->items << item;
childElement = childElement.nextSiblingElement(QStringLiteral("item"));
}
break;
case QXmppPubSubIq::SubscriptionQuery:
d->subscriptionId = queryElement.attribute(QStringLiteral("subid"));
d->subscriptionType = queryElement.attribute(QStringLiteral("subscription"));
break;
default:
break;
}
}
void QXmppPubSubIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("pubsub"));
writer->writeDefaultNamespace(ns_pubsub);
// write query type
writer->writeStartElement(PUBSUB_QUERIES.at(d->queryType));
helperToXmlAddAttribute(writer, QStringLiteral("jid"), d->queryJid);
helperToXmlAddAttribute(writer, QStringLiteral("node"), d->queryNode);
// write contents
switch (d->queryType) {
case QXmppPubSubIq::ItemsQuery:
case QXmppPubSubIq::PublishQuery:
case QXmppPubSubIq::RetractQuery:
for (const auto &item : d->items) {
item.toXml(writer);
}
break;
case QXmppPubSubIq::SubscriptionQuery:
helperToXmlAddAttribute(writer, QStringLiteral("subid"), d->subscriptionId);
helperToXmlAddAttribute(writer, QStringLiteral("subscription"), d->subscriptionType);
break;
default:
break;
}
writer->writeEndElement();
writer->writeEndElement();
}
QT_WARNING_POP
/// \endcond
|