QXmpp: QXmppPubSubIq_p.h Source File

QXmpp Version: 1.14.3

Loading...
Searching...
No Matches
QXmppPubSubIq_p.h
1// SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
2// SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#ifndef QXMPPPUBSUBIQ_H
7#define QXMPPPUBSUBIQ_H
8
9#include "QXmppIq.h"
10#include "QXmppPubSubBaseItem.h"
11
12#include <optional>
13
14#include <QDomElement>
15#include <QSharedDataPointer>
16
17class QXmppDataForm;
18class QXmppPubSubSubscription;
19class QXmppPubSubAffiliation;
20class QXmppResultSetReply;
21
22namespace QXmpp::Private {
23
24class PubSubIqPrivate;
25
26class QXMPP_EXPORT PubSubIqBase : public QXmppIq
27{
28public:
30 enum QueryType : uint8_t {
31 Affiliations,
32 OwnerAffiliations,
33 Configure,
34 Create,
35 Default,
36 OwnerDefault,
37 Delete,
38 Items,
39 Options,
40 Publish,
41 Purge,
42 Retract,
43 Subscribe,
44 Subscription,
45 Subscriptions,
46 OwnerSubscriptions,
47 Unsubscribe,
48 };
49
50 PubSubIqBase();
51 PubSubIqBase(const PubSubIqBase &other);
52 ~PubSubIqBase();
53
54 PubSubIqBase &operator=(const PubSubIqBase &other);
55
56 QueryType queryType() const;
57 void setQueryType(QueryType queryType);
58
59 QString queryJid() const;
60 void setQueryJid(const QString &queryJid);
61
62 QString queryNode() const;
63 void setQueryNode(const QString &queryNode);
64
65 QString subscriptionId() const;
66 void setSubscriptionId(const QString &subscriptionId);
67
68 QVector<QXmppPubSubSubscription> subscriptions() const;
69 void setSubscriptions(const QVector<QXmppPubSubSubscription> &);
70
71 std::optional<QXmppPubSubSubscription> subscription() const;
72 void setSubscription(const std::optional<QXmppPubSubSubscription> &);
73
74 QVector<QXmppPubSubAffiliation> affiliations() const;
75 void setAffiliations(const QVector<QXmppPubSubAffiliation> &);
76
77 std::optional<uint32_t> maxItems() const;
78 void setMaxItems(std::optional<uint32_t>);
79
80 bool retractNotify() const;
81 void setRetractNotify(bool);
82
83 std::optional<QXmppDataForm> dataForm() const;
84 void setDataForm(const std::optional<QXmppDataForm> &);
85
86 std::optional<QXmppResultSetReply> itemsContinuation() const;
87 void setItemsContinuation(const std::optional<QXmppResultSetReply> &itemsContinuation);
88
90 static bool isPubSubIq(const QDomElement &element);
91
92protected:
93 static bool isPubSubIq(const QDomElement &element,
94 bool (*isItemValid)(const QDomElement &));
95
96 void parseElementFromChild(const QDomElement &) override;
97 void toXmlElementFromChild(QXmlStreamWriter *writer) const override;
98
99 virtual void parseItems(const QDomElement &queryElement) = 0;
100 virtual void serializeItems(QXmlStreamWriter *writer) const = 0;
102
103private:
104 static std::optional<QueryType> queryTypeFromDomElement(const QDomElement &element);
105 static bool queryTypeIsOwnerIq(QueryType type);
106
107 QSharedDataPointer<PubSubIqPrivate> d;
108};
109
110template<typename T = QXmppPubSubBaseItem>
111class PubSubIq : public PubSubIqBase
112{
113public:
114 QVector<T> items() const;
115 void setItems(const QVector<T> &items);
116
117 static bool isPubSubIq(const QDomElement &element);
118
119protected:
121 void parseItems(const QDomElement &queryElement) override;
122 void serializeItems(QXmlStreamWriter *writer) const override;
124
125private:
126 QVector<T> m_items;
127};
128
129template<typename T>
130QVector<T> PubSubIq<T>::items() const
131{
132 return m_items;
133}
134
135template<typename T>
136void PubSubIq<T>::setItems(const QVector<T> &items)
137{
138 m_items = items;
139}
140
141template<typename T>
142bool PubSubIq<T>::isPubSubIq(const QDomElement &element)
143{
144 return PubSubIqBase::isPubSubIq(element, [](const QDomElement &item) -> bool {
145 return T::isItem(item);
146 });
147}
148
150template<typename T>
151void PubSubIq<T>::parseItems(const QDomElement &queryElement)
152{
153 for (auto childElement = queryElement.firstChildElement(QStringLiteral("item"));
154 !childElement.isNull();
155 childElement = childElement.nextSiblingElement(QStringLiteral("item"))) {
156 T item;
157 item.parse(childElement);
158 m_items.push_back(std::move(item));
159 }
160}
161
162template<typename T>
163void PubSubIq<T>::serializeItems(QXmlStreamWriter *writer) const
164{
165 for (const auto &item : std::as_const(m_items)) {
166 item.toXml(writer);
167 }
168}
170
171} // namespace QXmpp::Private
172
173#endif // QXMPPPUBSUBIQ_H
QXmppDataForm
Definition QXmppDataForm.h:28
QXmppPubSubAffiliation
Definition QXmppPubSubAffiliation.h:18
QXmppPubSubSubscription
Definition QXmppPubSubSubscription.h:19
QXmppResultSetReply
The QXmppResultSetReply class represents a set element in a reply as defined by XEP-0059: Result Set ...
Definition QXmppResultSet.h:53