/* * Copyright (C) 2008-2020 The QXmpp developers * * Author: * Jeremy Lainé * * 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. * */ #include "QXmppPubSubIq.h" #include "QXmppConstants_p.h" #include "QXmppUtils.h" #include #include static const QStringList PUBSUB_QUERIES = { QSL("affiliations"), QSL("default"), QSL("items"), QSL("publish"), QSL("retract"), QSL("subscribe"), QSL("subscription"), QSL("subscriptions"), QSL("unsubscribe"), }; class QXmppPubSubIqPrivate : public QSharedData { public: QXmppPubSubIqPrivate(); QXmppPubSubIq::QueryType queryType; QString queryJid; QString queryNode; QList items; QString subscriptionId; QString subscriptionType; }; QXmppPubSubIqPrivate::QXmppPubSubIqPrivate() : queryType(QXmppPubSubIq::ItemsQuery) { } QXmppPubSubIq::QXmppPubSubIq() : d(new QXmppPubSubIqPrivate) { } QXmppPubSubIq::QXmppPubSubIq(const QXmppPubSubIq &iq) = default; QXmppPubSubIq::~QXmppPubSubIq() = default; QXmppPubSubIq &QXmppPubSubIq::operator=(const QXmppPubSubIq &iq) = default; /// Returns the PubSub queryType for this IQ. QXmppPubSubIq::QueryType QXmppPubSubIq::queryType() const { return d->queryType; } /// Sets the PubSub queryType for this IQ. /// /// \param queryType void QXmppPubSubIq::setQueryType(QXmppPubSubIq::QueryType queryType) { d->queryType = queryType; } /// Returns the JID being queried. QString QXmppPubSubIq::queryJid() const { return d->queryJid; } /// Sets the JID being queried. /// /// \param queryJid void QXmppPubSubIq::setQueryJid(const QString &queryJid) { d->queryJid = queryJid; } /// Returns the node being queried. QString QXmppPubSubIq::queryNode() const { return d->queryNode; } /// Sets the node being queried. /// /// \param queryNode void QXmppPubSubIq::setQueryNode(const QString &queryNode) { d->queryNode = queryNode; } /// Returns the subscription ID. QString QXmppPubSubIq::subscriptionId() const { return d->subscriptionId; } /// Sets the subscription ID. /// /// \param subscriptionId void QXmppPubSubIq::setSubscriptionId(const QString &subscriptionId) { d->subscriptionId = subscriptionId; } /// Returns the IQ's items. QList QXmppPubSubIq::items() const { return d->items; } /// Sets the IQ's items. /// /// \param items void QXmppPubSubIq::setItems(const QList &items) { d->items = items; } /// \cond bool QXmppPubSubIq::isPubSubIq(const QDomElement &element) { return element.firstChildElement(QSL("pubsub")).namespaceURI() == ns_pubsub; } void QXmppPubSubIq::parseElementFromChild(const QDomElement &element) { const QDomElement pubSubElement = element.firstChildElement(QSL("pubsub")); const QDomElement queryElement = pubSubElement.firstChildElement(); // determine query type const QString tagName = queryElement.tagName(); int queryType = PUBSUB_QUERIES.indexOf(queryElement.tagName()); if (queryType > -1) d->queryType = QueryType(queryType); d->queryJid = queryElement.attribute(QSL("jid")); d->queryNode = queryElement.attribute(QSL("node")); // parse contents QDomElement childElement; switch (d->queryType) { case QXmppPubSubIq::ItemsQuery: case QXmppPubSubIq::PublishQuery: case QXmppPubSubIq::RetractQuery: childElement = queryElement.firstChildElement(QSL("item")); while (!childElement.isNull()) { QXmppPubSubItem item; item.parse(childElement); d->items << item; childElement = childElement.nextSiblingElement(QSL("item")); } break; case QXmppPubSubIq::SubscriptionQuery: d->subscriptionId = queryElement.attribute(QSL("subid")); d->subscriptionType = queryElement.attribute(QSL("subscription")); break; default: break; } } void QXmppPubSubIq::toXmlElementFromChild(QXmlStreamWriter *writer) const { writer->writeStartElement(QSL("pubsub")); writer->writeDefaultNamespace(ns_pubsub); // write query type writer->writeStartElement(PUBSUB_QUERIES.at(d->queryType)); helperToXmlAddAttribute(writer, QSL("jid"), d->queryJid); helperToXmlAddAttribute(writer, QSL("node"), d->queryNode); // write contents switch (d->queryType) { case QXmppPubSubIq::ItemsQuery: case QXmppPubSubIq::PublishQuery: case QXmppPubSubIq::RetractQuery: for (const auto &item : d->items) item.toXml(writer); break; case QXmppPubSubIq::SubscriptionQuery: helperToXmlAddAttribute(writer, QSL("subid"), d->subscriptionId); helperToXmlAddAttribute(writer, QSL("subscription"), d->subscriptionType); break; default: break; } writer->writeEndElement(); writer->writeEndElement(); } /// \endcond