QXmpp Version: 1.10.0
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;
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 std::optional<QXmppDataForm> dataForm() const;
81 void setDataForm(const std::optional<QXmppDataForm> &);
82
83 std::optional<QXmppResultSetReply> itemsContinuation() const;
84 void setItemsContinuation(const std::optional<QXmppResultSetReply> &itemsContinuation);
85
87 static bool isPubSubIq(const QDomElement &element);
88
89protected:
90 static bool isPubSubIq(const QDomElement &element,
91 bool (*isItemValid)(const QDomElement &));
92
93 void parseElementFromChild(const QDomElement &) override;
94 void toXmlElementFromChild(QXmlStreamWriter *writer) const override;
95
96 virtual void parseItems(const QDomElement &queryElement) = 0;
97 virtual void serializeItems(QXmlStreamWriter *writer) const = 0;
99
100private:
101 static std::optional<QueryType> queryTypeFromDomElement(const QDomElement &element);
102 static bool queryTypeIsOwnerIq(QueryType type);
103
104 QSharedDataPointer<PubSubIqPrivate> d;
105};
106
107template<typename T = QXmppPubSubBaseItem>
108class PubSubIq : public PubSubIqBase
109{
110public:
111 QVector<T> items() const;
112 void setItems(const QVector<T> &items);
113
114 static bool isPubSubIq(const QDomElement &element);
115
116protected:
118 void parseItems(const QDomElement &queryElement) override;
119 void serializeItems(QXmlStreamWriter *writer) const override;
121
122private:
123 QVector<T> m_items;
124};
125
126template<typename T>
127QVector<T> PubSubIq<T>::items() const
128{
129 return m_items;
130}
131
132template<typename T>
133void PubSubIq<T>::setItems(const QVector<T> &items)
134{
135 m_items = items;
136}
137
138template<typename T>
139bool PubSubIq<T>::isPubSubIq(const QDomElement &element)
140{
141 return PubSubIqBase::isPubSubIq(element, [](const QDomElement &item) -> bool {
142 return T::isItem(item);
143 });
144}
145
147template<typename T>
148void PubSubIq<T>::parseItems(const QDomElement &queryElement)
149{
150 for (auto childElement = queryElement.firstChildElement(QStringLiteral("item"));
151 !childElement.isNull();
152 childElement = childElement.nextSiblingElement(QStringLiteral("item"))) {
153 T item;
154 item.parse(childElement);
155 m_items.push_back(std::move(item));
156 }
157}
158
159template<typename T>
160void PubSubIq<T>::serializeItems(QXmlStreamWriter *writer) const
161{
162 for (const auto &item : std::as_const(m_items)) {
163 item.toXml(writer);
164 }
165}
167
168} // namespace QXmpp::Private
169
170#endif // QXMPPPUBSUBIQ_H
Definition: QXmppDataForm.h:27
The QXmppIq class is the base class for all IQs.
Definition: QXmppIq.h:23
Definition: QXmppPubSubAffiliation.h:18
Definition: QXmppPubSubSubscription.h:19
The QXmppResultSetReply class represents a set element in a reply as defined by XEP-0059: Result Set ...
Definition: QXmppResultSet.h:50