From 110be45f72c507135576e7fd2d6faf4f16d047ad Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Sun, 5 Jul 2020 01:29:52 +0200 Subject: Add QXmppPubSubEvent The pubsub events are inheriting from QXmppMessage and are template classes with the item type as template parameter. Supports nearly everything from XEP-0060. --- src/base/QXmppPubSubEvent.h | 166 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/base/QXmppPubSubEvent.h (limited to 'src/base/QXmppPubSubEvent.h') diff --git a/src/base/QXmppPubSubEvent.h b/src/base/QXmppPubSubEvent.h new file mode 100644 index 00000000..06058a1e --- /dev/null +++ b/src/base/QXmppPubSubEvent.h @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * Linus Jahn + * + * Source: + * https://github.com/qxmpp-project/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#ifndef QXMPPPUBSUBEVENT_H +#define QXMPPPUBSUBEVENT_H + +#include "QXmppMessage.h" +#include "QXmppPubSubSubscription.h" + +#include + +#include +#include + +class QXmppDataForm; +class QXmppPubSubEventPrivate; +class QXmppPubSubItem; + +class QXMPP_EXPORT QXmppPubSubEventBase : public QXmppMessage +{ +public: + /// + /// Enumeration of different event types + /// + enum EventType : uint8_t { + Configuration, + Delete, + Items, + Purge, + Subscription, + }; + + QXmppPubSubEventBase(EventType = Items, const QString &node = {}); + QXmppPubSubEventBase(const QXmppPubSubEventBase &other); + virtual ~QXmppPubSubEventBase(); + + QXmppPubSubEventBase &operator=(const QXmppPubSubEventBase &other); + + EventType eventType() const; + void setEventType(EventType); + + QString node() const; + void setNode(const QString &node); + + QStringList retractIds() const; + void setRetractIds(const QStringList &); + + QString redirectUri() const; + void setRedirectUri(const QString &); + + std::optional subscription() const; + void setSubscription(const std::optional &subscription); + + std::optional configurationForm() const; + void setConfigurationForm(const std::optional &configurationForm); + +protected: + /// \cond + static bool isPubSubEvent(const QDomElement &element, std::function isItemValid); + + bool parseExtension(const QDomElement &element) override; + void serializeExtensions(QXmlStreamWriter *writer) const override; + + virtual void parseItems(const QDomElement &) = 0; + virtual void serializeItems(QXmlStreamWriter *writer) const = 0; + /// \endcond + +private: + QSharedDataPointer d; +}; + +template +class QXmppPubSubEvent : public QXmppPubSubEventBase +{ +public: + QVector items() const; + void setItems(const QVector &items); + + static bool isPubSubEvent(const QDomElement &element); + +protected: + /// \cond + void parseItems(const QDomElement &) override; + void serializeItems(QXmlStreamWriter *writer) const override; + /// \endcond + +private: + QVector m_items; +}; + +/// +/// Returns the PubSub items of the event. +/// +template +QVector QXmppPubSubEvent::items() const +{ + return m_items; +} + +/// +/// Sets the PubSub items of the event. +/// +template +void QXmppPubSubEvent::setItems(const QVector &items) +{ + m_items = items; +} + +/// +/// Returns whether the element is a valid QXmppPubSubEvent and contains only +/// valid items of type T. +/// +template +bool QXmppPubSubEvent::isPubSubEvent(const QDomElement &element) +{ + return QXmppPubSubEventBase::isPubSubEvent(element, [](const QDomElement &element) { + return T::isItem(element); + }); +} + +/// \cond +template +void QXmppPubSubEvent::parseItems(const QDomElement &parent) +{ + QDomElement child = parent.firstChildElement(QStringLiteral("item")); + while (!child.isNull()) { + T item; + item.parse(child); + m_items << item; + + child = child.nextSiblingElement(QStringLiteral("item")); + } +} + +template +void QXmppPubSubEvent::serializeItems(QXmlStreamWriter *writer) const +{ + for (const auto &item : qAsConst(m_items)) { + item.toXml(writer); + } +} +/// \endcond + +Q_DECLARE_METATYPE(QXmppPubSubEventBase::EventType) + +#endif // QXMPPPUBSUBEVENT_H -- cgit v1.2.3