QXmpp Version: 1.5.6
Loading...
Searching...
No Matches
QXmppPubSubEvent.h
1// SPDX-FileCopyrightText: 2020 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef QXMPPPUBSUBEVENT_H
6#define QXMPPPUBSUBEVENT_H
7
8#include "QXmppMessage.h"
9#include "QXmppPubSubSubscription.h"
10
11#include <functional>
12
13#include <QDomElement>
14#include <QSharedData>
15
16class QXmppDataForm;
17class QXmppPubSubEventPrivate;
19
20class QXMPP_EXPORT QXmppPubSubEventBase : public QXmppMessage
21{
22public:
26 enum EventType : uint8_t {
27 Configuration,
28 Delete,
29 Items,
30 Retract,
31 Purge,
32 Subscription,
33 };
34
35 QXmppPubSubEventBase(EventType = Items, const QString &node = {});
38 virtual ~QXmppPubSubEventBase();
39
42
43 EventType eventType() const;
44 void setEventType(EventType);
45
46 QString node() const;
47 void setNode(const QString &node);
48
49 QStringList retractIds() const;
50 void setRetractIds(const QStringList &);
51
52 QString redirectUri() const;
53 void setRedirectUri(const QString &);
54
55 std::optional<QXmppPubSubSubscription> subscription() const;
56 void setSubscription(const std::optional<QXmppPubSubSubscription> &subscription);
57
58 std::optional<QXmppDataForm> configurationForm() const;
59 void setConfigurationForm(const std::optional<QXmppDataForm> &configurationForm);
60
61protected:
63 static bool isPubSubEvent(const QDomElement &element, std::function<bool(const QDomElement &)> isItemValid);
64
65 bool parseExtension(const QDomElement &element, QXmpp::SceMode) override;
66 void serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode, const QString &baseNamespace) const override;
67
68 virtual void parseItems(const QDomElement &) = 0;
69 virtual void serializeItems(QXmlStreamWriter *writer) const = 0;
71
72private:
73 QSharedDataPointer<QXmppPubSubEventPrivate> d;
74};
75
76template<typename T = QXmppPubSubBaseItem>
78{
79public:
80 QVector<T> items() const;
81 void setItems(const QVector<T> &items);
82
83 static bool isPubSubEvent(const QDomElement &element);
84
85protected:
87 void parseItems(const QDomElement &) override;
88 void serializeItems(QXmlStreamWriter *writer) const override;
90
91private:
92 QVector<T> m_items;
93};
94
98template<typename T>
100{
101 return m_items;
102}
103
107template<typename T>
108void QXmppPubSubEvent<T>::setItems(const QVector<T> &items)
109{
110 m_items = items;
111}
112
117template<typename T>
118bool QXmppPubSubEvent<T>::isPubSubEvent(const QDomElement &element)
119{
120 return QXmppPubSubEventBase::isPubSubEvent(element, [](const QDomElement &element) {
121 return T::isItem(element);
122 });
123}
124
126template<typename T>
127void QXmppPubSubEvent<T>::parseItems(const QDomElement &parent)
128{
129 QDomElement child = parent.firstChildElement(QStringLiteral("item"));
130 while (!child.isNull()) {
131 T item;
132 item.parse(child);
133 m_items << item;
134
135 child = child.nextSiblingElement(QStringLiteral("item"));
136 }
137}
138
139template<typename T>
140void QXmppPubSubEvent<T>::serializeItems(QXmlStreamWriter *writer) const
141{
142 for (const auto &item : qAsConst(m_items)) {
143 item.toXml(writer);
144 }
145}
147
148Q_DECLARE_METATYPE(QXmppPubSubEventBase::EventType)
149
150#endif // QXMPPPUBSUBEVENT_H
Definition QXmppDataForm.h:27
The QXmppMessage class represents an XMPP message.
Definition QXmppMessage.h:35
virtual bool parseExtension(const QDomElement &element, QXmpp::SceMode)
Definition QXmppMessage.cpp:1375
virtual void serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode, const QString &baseNamespace={}) const
Definition QXmppMessage.cpp:1607
virtual void parse(const QDomElement &)=0
virtual void toXml(QXmlStreamWriter *writer) const =0
Definition QXmppPubSubBaseItem.h:19
Definition QXmppPubSubEvent.h:21
QXmppPubSubEventBase & operator=(const QXmppPubSubEventBase &other)
Default assignment operator.
EventType
Definition QXmppPubSubEvent.h:26
QXmppPubSubEventBase & operator=(QXmppPubSubEventBase &&)
Default move-assignment operator.
QXmppPubSubEventBase(const QXmppPubSubEventBase &other)
Default copy-constructor.
QXmppPubSubEventBase(QXmppPubSubEventBase &&)
Default move-constructor.
The QXmppPubSubEvent class represents a PubSub event notification as defined by XEP-0060: Publish-Sub...
Definition QXmppPubSubEvent.h:78
static bool isPubSubEvent(const QDomElement &element)
Definition QXmppPubSubEvent.h:118
QVector< T > items() const
Definition QXmppPubSubEvent.h:99
void setItems(const QVector< T > &items)
Definition QXmppPubSubEvent.h:108
SceMode
Definition QXmppGlobal.h:136