diff options
| author | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-09-10 09:21:10 +0000 |
|---|---|---|
| committer | Jeremy Lainé <jeremy.laine@m4x.org> | 2010-09-10 09:21:10 +0000 |
| commit | 9ae1e22d0b6edb457bd63f71f31ef4e64a987ef5 (patch) | |
| tree | d3756c0d25d96048e66b2e91f5be01847bc77372 /src | |
| parent | e8a1de4fdbdba709eb4f76799fe5f78756ec5321 (diff) | |
| download | qxmpp-9ae1e22d0b6edb457bd63f71f31ef4e64a987ef5.tar.gz | |
add preliminary QXmppPubSubIq class
Diffstat (limited to 'src')
| -rw-r--r-- | src/QXmppPubSubIq.cpp | 253 | ||||
| -rw-r--r-- | src/QXmppPubSubIq.h | 105 | ||||
| -rw-r--r-- | src/src.pro | 2 |
3 files changed, 360 insertions, 0 deletions
diff --git a/src/QXmppPubSubIq.cpp b/src/QXmppPubSubIq.cpp new file mode 100644 index 00000000..08ee76a0 --- /dev/null +++ b/src/QXmppPubSubIq.cpp @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/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 <QDomElement> + +#include "QXmppConstants.h" +#include "QXmppPubSubIq.h" +#include "QXmppUtils.h" + +static const char *ns_pubsub = "http://jabber.org/protocol/pubsub"; + +static const char *pubsub_queries[] = { + "affiliations", + "default", + "items", + "publish", + "retract", + "subscribe", + "subscription", + "subscriptions", + "unsubscribe", +}; + +/// Returns the ID of the PubSub item. +/// + +QString QXmppPubSubItem::id() const +{ + return m_id; +} + +/// Sets the ID of the PubSub item. +/// +/// \param id + +void QXmppPubSubItem::setId(const QString &id) +{ + m_id = id; +} + +/// Returns the contents of the PubSub item. +/// + +QXmppElement QXmppPubSubItem::contents() const +{ + return m_contents; +} + +/// Sets the contents of the PubSub item. +/// +/// \param contents + +void QXmppPubSubItem::setContents(const QXmppElement &contents) +{ + m_contents = contents; +} + +void QXmppPubSubItem::parse(const QDomElement &element) +{ + m_id = element.attribute("id"); + m_contents = QXmppElement(element.firstChildElement()); +} + +void QXmppPubSubItem::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement("item"); + helperToXmlAddAttribute(writer, "id", m_id); + m_contents.toXml(writer); + writer->writeEndElement(); +} + +/// Returns the PubSub queryType for this IQ. +/// + +QXmppPubSubIq::QueryType QXmppPubSubIq::queryType() const +{ + return m_queryType; +} + +/// Sets the PubSub queryType for this IQ. +/// +/// \param queryType + +void QXmppPubSubIq::setQueryType(QXmppPubSubIq::QueryType queryType) +{ + m_queryType = queryType; +} + +/// Returns the JID being queried. +/// + +QString QXmppPubSubIq::queryJid() const +{ + return m_queryJid; +} + +/// Sets the JID being queried. +/// +/// \param queryJid + +void QXmppPubSubIq::setQueryJid(const QString &queryJid) +{ + m_queryJid = queryJid; +} + +/// Returns the node being queried. +/// + +QString QXmppPubSubIq::queryNode() const +{ + return m_queryNode; +} + +/// Sets the node being queried. +/// +/// \param queryNode + +void QXmppPubSubIq::setQueryNode(const QString &queryNode) +{ + m_queryNode = queryNode; +} + +/// Returns the subscription ID. +/// + +QString QXmppPubSubIq::subscriptionId() const +{ + return m_subscriptionId; +} + +/// Sets the subscription ID. +/// +/// \param subscriptionId + +void QXmppPubSubIq::setSubscriptionId(const QString &subscriptionId) +{ + m_subscriptionId = subscriptionId; +} + +/// Returns the IQ's items. +/// + +QList<QXmppPubSubItem> QXmppPubSubIq::items() const +{ + return m_items; +} + +/// Sets the IQ's items. +/// +/// \param items + +void QXmppPubSubIq::setItems(const QList<QXmppPubSubItem> &items) +{ + m_items = items; +} + +bool QXmppPubSubIq::isPubSubIq(const QDomElement &element) +{ + const QDomElement pubSubElement = element.firstChildElement("pubsub"); + return pubSubElement.namespaceURI() == ns_pubsub; +} + +void QXmppPubSubIq::parseElementFromChild(const QDomElement &element) +{ + const QDomElement pubSubElement = element.firstChildElement("pubsub"); + + const QDomElement queryElement = pubSubElement.firstChildElement(); + + // determine query type + const QString tagName = queryElement.tagName(); + for (int i = ItemsQuery; i <= SubscriptionsQuery; i++) + { + if (tagName == pubsub_queries[i]) + { + m_queryType = static_cast<QueryType>(i); + break; + } + } + m_queryJid = queryElement.attribute("jid"); + m_queryNode = queryElement.attribute("node"); + + // parse contents + QDomElement childElement; + switch (m_queryType) + { + case QXmppPubSubIq::ItemsQuery: + case QXmppPubSubIq::PublishQuery: + childElement = queryElement.firstChildElement("item"); + while (!childElement.isNull()) + { + QXmppPubSubItem item; + item.parse(childElement); + m_items << item; + childElement = childElement.nextSiblingElement("item"); + } + break; + case QXmppPubSubIq::SubscriptionQuery: + m_subscriptionId = queryElement.attribute("subid"); + m_subscriptionType = queryElement.attribute("subscription"); + break; + default: + break; + } +} + +void QXmppPubSubIq::toXmlElementFromChild(QXmlStreamWriter *writer) const +{ + writer->writeStartElement("pubsub"); + helperToXmlAddAttribute(writer, "xmlns", ns_pubsub); + + // write query type + writer->writeStartElement(pubsub_queries[m_queryType]); + helperToXmlAddAttribute(writer, "jid", m_queryJid); + helperToXmlAddAttribute(writer, "node", m_queryNode); + + // write contents + switch (m_queryType) + { + case QXmppPubSubIq::ItemsQuery: + case QXmppPubSubIq::PublishQuery: + foreach (const QXmppPubSubItem &item, m_items) + item.toXml(writer); + break; + case QXmppPubSubIq::SubscriptionQuery: + helperToXmlAddAttribute(writer, "subid", m_subscriptionId); + helperToXmlAddAttribute(writer, "subscription", m_subscriptionType); + break; + default: + break; + } + writer->writeEndElement(); + writer->writeEndElement(); +} diff --git a/src/QXmppPubSubIq.h b/src/QXmppPubSubIq.h new file mode 100644 index 00000000..e42bf711 --- /dev/null +++ b/src/QXmppPubSubIq.h @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2008-2010 The QXmpp developers + * + * Author: + * Jeremy Lainé + * + * Source: + * http://code.google.com/p/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 QXMPPPUBSUBIQ_H +#define QXMPPPUBSUBIQ_H + +#include "QXmppIq.h" + +/// \brief The QXmppPubSubItem class represents a publish-subscribe item +/// as defined by XEP-0060: Publish-Subscribe. +/// + +class QXmppPubSubItem +{ +public: + QString id() const; + void setId(const QString &id); + + QXmppElement contents() const; + void setContents(const QXmppElement &contents); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + +private: + QString m_id; + QXmppElement m_contents; +}; + +/// \brief The QXmppPubSubIq class represents an IQ used for the +/// publish-subscribe mechanisms defined by XEP-0060: Publish-Subscribe. +/// +/// \ingroup Stanzas + +class QXmppPubSubIq : public QXmppIq +{ +public: + enum QueryType + { + AffiliationsQuery, + DefaultQuery, + ItemsQuery, + PublishQuery, + RetractQuery, + SubscribeQuery, + SubscriptionQuery, + SubscriptionsQuery, + UnsubscribeQuery, + }; + + QXmppPubSubIq::QueryType queryType() const; + void setQueryType(QXmppPubSubIq::QueryType queryType); + + QString queryJid() const; + void setQueryJid(const QString &jid); + + QString queryNode() const; + void setQueryNode(const QString &node); + + QList<QXmppPubSubItem> items() const; + void setItems(const QList<QXmppPubSubItem> &items); + + QString subscriptionId() const; + void setSubscriptionId(const QString &id); + + static bool isPubSubIq(const QDomElement &element); + +protected: + /// \cond + void parseElementFromChild(const QDomElement&); + void toXmlElementFromChild(QXmlStreamWriter *writer) const; + /// \endcond + +private: + QXmppPubSubIq::QueryType m_queryType; + QString m_queryJid; + QString m_queryNode; + QList<QXmppPubSubItem> m_items; + QString m_subscriptionId; + QString m_subscriptionType; +}; + +#endif diff --git a/src/src.pro b/src/src.pro index f293e5ea..375e61f2 100644 --- a/src/src.pro +++ b/src/src.pro @@ -59,6 +59,7 @@ INSTALL_HEADERS = QXmppUtils.h \ QXmppPacket.h \ QXmppPingIq.h \ QXmppPresence.h \ + QXmppPubSubIq.h \ QXmppRoster.h \ QXmppRosterIq.h \ QXmppRosterManager.h \ @@ -119,6 +120,7 @@ SOURCES += QXmppUtils.cpp \ QXmppPacket.cpp \ QXmppPingIq.cpp \ QXmppPresence.cpp \ + QXmppPubSubIq.cpp \ QXmppRosterIq.cpp \ QXmppRosterManager.cpp \ QXmppSaslAuth.cpp \ |
