QXmpp: QXmppPubSubEvent.h Source File

QXmpp Version: 1.14.3

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;
18class QXmppPubSubBaseItem;
19
20 class 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 = {});
36 QXmppPubSubEventBase(const QXmppPubSubEventBase &other);
37 QXmppPubSubEventBase(QXmppPubSubEventBase &&);
38 virtual ~QXmppPubSubEventBase();
39
40 QXmppPubSubEventBase &operator=(const QXmppPubSubEventBase &other);
41 QXmppPubSubEventBase &operator=(QXmppPubSubEventBase &&);
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>
77 class QXmppPubSubEvent : public QXmppPubSubEventBase
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>
99QVector<T> QXmppPubSubEvent<T>::items() const
100{
101 return m_items;
102}
103
107template<typename T>
108 void QXmppPubSubEvent<T>::setItems(const QVector<T> &items)
109{
110 m_items = items;
111}
112
117template<typename T>
118 bool 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 : std::as_const(m_items)) {
143 item.toXml(writer);
144 }
145}
147
148Q_DECLARE_METATYPE(QXmppPubSubEventBase::EventType)
149
150#endif // QXMPPPUBSUBEVENT_H
QXmppDataForm
Definition QXmppDataForm.h:28
QXmppMessage::parseExtension
virtual bool parseExtension(const QDomElement &element, QXmpp::SceMode)
Definition QXmppMessage.cpp:1684
QXmppMessage::QXmppMessage
QXmppMessage(const QString &from=QString(), const QString &to=QString(), const QString &body=QString(), const QString &thread=QString())
Definition QXmppMessage.cpp:232
QXmppMessage::serializeExtensions
virtual void serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode, const QString &baseNamespace={}) const
Definition QXmppMessage.cpp:1943
QXmppPubSubBaseItem
Definition QXmppPubSubBaseItem.h:19
QXmppPubSubEventBase
Definition QXmppPubSubEvent.h:21
QXmppPubSubEventBase::setSubscription
void setSubscription(const std::optional< QXmppPubSubSubscription > &subscription)
Definition QXmppPubSubEvent.cpp:203
QXmppPubSubEventBase::operator=
QXmppPubSubEventBase & operator=(const QXmppPubSubEventBase &other)
Default assignment operator.
QXmppPubSubEventBase::setEventType
void setEventType(EventType)
Definition QXmppPubSubEvent.cpp:117
QXmppPubSubEventBase::subscription
std::optional< QXmppPubSubSubscription > subscription() const
Definition QXmppPubSubEvent.cpp:195
QXmppPubSubEventBase::setConfigurationForm
void setConfigurationForm(const std::optional< QXmppDataForm > &configurationForm)
Definition QXmppPubSubEvent.cpp:219
QXmppPubSubEventBase::node
QString node() const
Definition QXmppPubSubEvent.cpp:131
QXmppPubSubEventBase::setRedirectUri
void setRedirectUri(const QString &)
Definition QXmppPubSubEvent.cpp:187
QXmppPubSubEventBase::EventType
EventType
Definition QXmppPubSubEvent.h:26
QXmppPubSubEventBase::operator=
QXmppPubSubEventBase & operator=(QXmppPubSubEventBase &&)
Default move-assignment operator.
QXmppPubSubEventBase::configurationForm
std::optional< QXmppDataForm > configurationForm() const
Definition QXmppPubSubEvent.cpp:211
QXmppPubSubEventBase::QXmppPubSubEventBase
QXmppPubSubEventBase(const QXmppPubSubEventBase &other)
Default copy-constructor.
QXmppPubSubEventBase::redirectUri
QString redirectUri() const
Definition QXmppPubSubEvent.cpp:176
QXmppPubSubEventBase::setNode
void setNode(const QString &node)
Definition QXmppPubSubEvent.cpp:145
QXmppPubSubEventBase::QXmppPubSubEventBase
QXmppPubSubEventBase(EventType=Items, const QString &node={})
Definition QXmppPubSubEvent.cpp:90
QXmppPubSubEventBase::QXmppPubSubEventBase
QXmppPubSubEventBase(QXmppPubSubEventBase &&)
Default move-constructor.
QXmppPubSubEventBase::retractIds
QStringList retractIds() const
Definition QXmppPubSubEvent.cpp:155
QXmppPubSubEventBase::eventType
EventType eventType() const
Definition QXmppPubSubEvent.cpp:109
QXmppPubSubEventBase::setRetractIds
void setRetractIds(const QStringList &)
Definition QXmppPubSubEvent.cpp:165
QXmppPubSubEvent
The QXmppPubSubEvent class represents a PubSub event notification as defined by XEP-0060: Publish-Sub...
Definition QXmppPubSubEvent.h:78
QXmppPubSubEvent::isPubSubEvent
static bool isPubSubEvent(const QDomElement &element)
Definition QXmppPubSubEvent.h:118
QXmppPubSubEvent::items
QVector< T > items() const
Definition QXmppPubSubEvent.h:99
QXmppPubSubEvent::setItems
void setItems(const QVector< T > &items)
Definition QXmppPubSubEvent.h:108
QXmpp::SceMode
SceMode
Definition QXmppGlobal.h:148