diff options
| author | Jonah Brüchert <jbb@kaidan.im> | 2022-09-28 13:38:25 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2022-09-28 15:56:05 +0200 |
| commit | 1916eca1a982b22c10c1c737fcbcb86bee0ec5cc (patch) | |
| tree | 7ebda6bc36a8e0df2b0130cb844902f840d37a47 /src/base | |
| parent | 61564526f1cfd53c31353aeb6df26ba0f5da9834 (diff) | |
| download | qxmpp-1916eca1a982b22c10c1c737fcbcb86bee0ec5cc.tar.gz | |
Allow adding multiple oob urls and a description
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/QXmppMessage.cpp | 46 | ||||
| -rw-r--r-- | src/base/QXmppMessage.h | 4 | ||||
| -rw-r--r-- | src/base/QXmppOutOfBandUrl.cpp | 88 | ||||
| -rw-r--r-- | src/base/QXmppOutOfBandUrl.h | 40 |
4 files changed, 169 insertions, 9 deletions
diff --git a/src/base/QXmppMessage.cpp b/src/base/QXmppMessage.cpp index f45789f0..2e53a223 100644 --- a/src/base/QXmppMessage.cpp +++ b/src/base/QXmppMessage.cpp @@ -16,6 +16,7 @@ #include "QXmppOmemoElement_p.h" #include "QXmppOmemoEnvelope_p.h" #endif +#include "QXmppOutOfBandUrl.h" #include "QXmppTrustMessageElement.h" #include "QXmppUtils.h" @@ -78,7 +79,7 @@ public: QXmppMessage::Type type; // XEP-0066: Out of Band Data - QString outOfBandUrl; + QVector<QXmppOutOfBandUrl> outOfBandUrls; // XEP-0071: XHTML-IM QString xhtml; @@ -333,17 +334,45 @@ void QXmppMessage::setParentThread(const QString &parent) /// QString QXmppMessage::outOfBandUrl() const { - return d->outOfBandUrl; + if (d->outOfBandUrls.empty()) { + return {}; + } + + return d->outOfBandUrls.front().url(); } /// /// Sets the attached URL for \xep{0066}: Out of Band Data /// +/// This overrides all other urls that may be contained in the list of out of band urls. +/// /// \since QXmpp 1.0 /// void QXmppMessage::setOutOfBandUrl(const QString &url) { - d->outOfBandUrl = url; + QXmppOutOfBandUrl data; + data.setUrl(url); + d->outOfBandUrls = { std::move(data) }; +} + +/// +/// Returns possibly attached URLs from \xep{0066}: Out of Band Data +/// +/// \since QXmpp 1.5 +/// +QVector<QXmppOutOfBandUrl> QXmppMessage::outOfBandUrls() const +{ + return d->outOfBandUrls; +} + +/// +/// Sets the attached URLs for \xep{0066}: Out of Band Data +/// +/// \since QXmpp 1.5 +/// +void QXmppMessage::setOutOfBandUrls(const QVector<QXmppOutOfBandUrl> &urls) +{ + d->outOfBandUrls = urls; } /// @@ -1408,7 +1437,9 @@ bool QXmppMessage::parseExtension(const QDomElement &element, QXmpp::SceMode sce } // XEP-0066: Out of Band Data if (element.namespaceURI() == ns_oob) { - d->outOfBandUrl = element.firstChildElement(QStringLiteral("url")).text(); + QXmppOutOfBandUrl data; + data.parse(element); + d->outOfBandUrls.push_back(std::move(data)); return true; } } @@ -1637,11 +1668,8 @@ void QXmppMessage::serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode } // XEP-0066: Out of Band Data - if (!d->outOfBandUrl.isEmpty()) { - writer->writeStartElement(QStringLiteral("x")); - writer->writeDefaultNamespace(ns_oob); - writer->writeTextElement(QStringLiteral("url"), d->outOfBandUrl); - writer->writeEndElement(); + for (const auto &url : d->outOfBandUrls) { + url.toXml(writer); } // XEP-0071: XHTML-IM diff --git a/src/base/QXmppMessage.h b/src/base/QXmppMessage.h index a4d4fcbe..51547f0a 100644 --- a/src/base/QXmppMessage.h +++ b/src/base/QXmppMessage.h @@ -23,6 +23,7 @@ class QXmppMixInvitation; class QXmppOmemoElement; #endif class QXmppTrustMessageElement; +class QXmppOutOfBandUrl; /// /// \brief The QXmppMessage class represents an XMPP message. @@ -127,6 +128,9 @@ public: QString outOfBandUrl() const; void setOutOfBandUrl(const QString &); + QVector<QXmppOutOfBandUrl> outOfBandUrls() const; + void setOutOfBandUrls(const QVector<QXmppOutOfBandUrl> &urls); + // XEP-0071: XHTML-IM QString xhtml() const; void setXhtml(const QString &xhtml); diff --git a/src/base/QXmppOutOfBandUrl.cpp b/src/base/QXmppOutOfBandUrl.cpp new file mode 100644 index 00000000..d986ae11 --- /dev/null +++ b/src/base/QXmppOutOfBandUrl.cpp @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppOutOfBandUrl.h" + +#include "QXmppConstants_p.h" + +#include <QDomElement> +#include <QXmlStreamWriter> + +class QXmppOutOfBandUrlPrivate : public QSharedData +{ +public: + QString url; + std::optional<QString> description; +}; + +/// +/// \class QXmppOutOfBandUrl +/// +/// A URL and a description of its contents, from \xep{0066}: Out of Band Data +/// +/// \since QXmpp 1.5 +/// + +QXmppOutOfBandUrl::QXmppOutOfBandUrl() + : d(new QXmppOutOfBandUrlPrivate()) +{ +} + +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppOutOfBandUrl); + +/// +/// Returns the attached URL +/// +const QString &QXmppOutOfBandUrl::url() const +{ + return d->url; +} + +/// +/// Sets the attached URL +/// +void QXmppOutOfBandUrl::setUrl(const QString &url) +{ + d->url = url; +} + +/// +/// Returns the description of the URL +/// +const std::optional<QString> &QXmppOutOfBandUrl::description() const +{ + return d->description; +} + +/// +/// Set the description of the URL +/// +void QXmppOutOfBandUrl::setDescription(const std::optional<QString> &description) +{ + d->description = description; +} + +/// \cond +bool QXmppOutOfBandUrl::parse(const QDomElement &el) +{ + d->url = el.firstChildElement(QStringLiteral("url")).text(); + auto childEl = el.firstChildElement(QStringLiteral("desc")); + if (!childEl.isNull()) { + d->description = childEl.text(); + } + + return true; +} + +void QXmppOutOfBandUrl::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement(QStringLiteral("x")); + writer->writeDefaultNamespace(ns_oob); + writer->writeTextElement(QStringLiteral("url"), d->url); + if (d->description) { + writer->writeTextElement(QStringLiteral("desc"), *d->description); + } + writer->writeEndElement(); +} +/// \endcond diff --git a/src/base/QXmppOutOfBandUrl.h b/src/base/QXmppOutOfBandUrl.h new file mode 100644 index 00000000..6df2abc9 --- /dev/null +++ b/src/base/QXmppOutOfBandUrl.h @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPOUTOFBANDURL_H +#define QXMPPOUTOFBANDURL_H + +#include "QXmppGlobal.h" + +#include <optional> + +#include <QSharedDataPointer> + +class QXmppOutOfBandUrlPrivate; +class QDomElement; +class QXmlStreamWriter; + +class QXMPP_EXPORT QXmppOutOfBandUrl +{ +public: + QXmppOutOfBandUrl(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppOutOfBandUrl) + + const QString &url() const; + void setUrl(const QString &url); + + const std::optional<QString> &description() const; + void setDescription(const std::optional<QString> &description); + + /// \cond + bool parse(const QDomElement &el); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + +private: + QSharedDataPointer<QXmppOutOfBandUrlPrivate> d; +}; + +#endif // QXMPPOUTOFBANDURL_H |
