From ecfa45db97e40ad7034a3cce4d26b3db87eb5c10 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Mon, 6 Jul 2020 00:27:25 +0200 Subject: Add QXmppPubSubAffiliation --- src/base/QXmppPubSubAffiliation.cpp | 182 ++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 src/base/QXmppPubSubAffiliation.cpp (limited to 'src/base/QXmppPubSubAffiliation.cpp') diff --git a/src/base/QXmppPubSubAffiliation.cpp b/src/base/QXmppPubSubAffiliation.cpp new file mode 100644 index 00000000..a7c41643 --- /dev/null +++ b/src/base/QXmppPubSubAffiliation.cpp @@ -0,0 +1,182 @@ +/* + * 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. + * + */ + +#include "QXmppPubSubAffiliation.h" + +#include "QXmppConstants_p.h" +#include "QXmppUtils.h" + +#include +#include + +/// +/// \class QXmppPubSubAffiliation +/// +/// This class represents an affiliation of a user with a PubSub node as defined +/// in \xep{0060, Publish-Subscribe}. +/// +/// \sa QXmppPubSubIq +/// \sa QXmppPubSubEvent +/// +/// \since QXmpp 1.5 +/// + +static const QStringList PUBSUB_AFFILIATIONS = { + QStringLiteral("none"), + QStringLiteral("member"), + QStringLiteral("outcast"), + QStringLiteral("owner"), + QStringLiteral("publisher"), + QStringLiteral("publish-only"), +}; + +class QXmppPubSubAffiliationPrivate : public QSharedData +{ +public: + QXmppPubSubAffiliationPrivate(QXmppPubSubAffiliation::Affiliation type, + const QString &node, + const QString &jid); + + QXmppPubSubAffiliation::Affiliation type; + QString node; + QString jid; +}; + +QXmppPubSubAffiliationPrivate::QXmppPubSubAffiliationPrivate(QXmppPubSubAffiliation::Affiliation type, + const QString &node, + const QString &jid) + : type(type), + node(node), + jid(jid) +{ +} + +/// +/// Default constructor. +/// +QXmppPubSubAffiliation::QXmppPubSubAffiliation(Affiliation type, + const QString &node, + const QString &jid) + : d(new QXmppPubSubAffiliationPrivate(type, node, jid)) +{ +} + +/// Copy constructor. +QXmppPubSubAffiliation::QXmppPubSubAffiliation(const QXmppPubSubAffiliation &) = default; + +QXmppPubSubAffiliation::~QXmppPubSubAffiliation() = default; + +/// Assignment operator. +QXmppPubSubAffiliation &QXmppPubSubAffiliation::operator=(const QXmppPubSubAffiliation &) = default; + +/// +/// Returns the type of the affiliation. +/// +QXmppPubSubAffiliation::Affiliation QXmppPubSubAffiliation::type() const +{ + return d->type; +} + +/// +/// Sets the type of the affiliation. +/// +void QXmppPubSubAffiliation::setType(Affiliation type) +{ + d->type = type; +} + +/// +/// Returns the node name of the node the affiliation belongs to. +/// +QString QXmppPubSubAffiliation::node() const +{ + return d->node; +} + +/// +/// Sets the node name. +/// +void QXmppPubSubAffiliation::setNode(const QString &node) +{ + d->node = node; +} + +/// +/// Returns the JID of the user. +/// +QString QXmppPubSubAffiliation::jid() const +{ + return d->jid; +} + +/// +/// Sets the JID of the user. +/// +void QXmppPubSubAffiliation::setJid(const QString &jid) +{ + d->jid = jid; +} + +/// +/// Returns true if the DOM element is a PubSub affiliation. +/// +bool QXmppPubSubAffiliation::isAffiliation(const QDomElement &element) +{ + if (element.tagName() != QStringLiteral("affiliation") || + !PUBSUB_AFFILIATIONS.contains(element.attribute(QStringLiteral("affiliation")))) { + return false; + } + + if (element.namespaceURI() == ns_pubsub) { + return element.hasAttribute(QStringLiteral("node")); + } + if (element.namespaceURI() == ns_pubsub_owner) { + return element.hasAttribute(QStringLiteral("jid")); + } + return false; +} + +/// \cond +void QXmppPubSubAffiliation::parse(const QDomElement &element) +{ + if (const auto typeIndex = PUBSUB_AFFILIATIONS.indexOf(element.attribute(QStringLiteral("affiliation"))); + typeIndex != -1) { + d->type = Affiliation(typeIndex); + } else { + // this can only happen, when isAffiliation() returns false + d->type = None; + } + + d->node = element.attribute(QStringLiteral("node")); + d->jid = element.attribute(QStringLiteral("jid")); +} + +void QXmppPubSubAffiliation::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement(QStringLiteral("affiliation")); + writer->writeAttribute(QStringLiteral("affiliation"), PUBSUB_AFFILIATIONS.at(int(d->type))); + helperToXmlAddAttribute(writer, QStringLiteral("node"), d->node); + helperToXmlAddAttribute(writer, QStringLiteral("jid"), d->jid); + writer->writeEndElement(); +} +/// \endcond -- cgit v1.2.3