aboutsummaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2022-09-05 20:21:59 +0200
committerLinus Jahn <lnj@kaidan.im>2022-09-06 00:46:56 +0200
commit6685217ac78c7588aaea67059a0844d51713dcd5 (patch)
tree71e997556b805724de6d736dc1db2d43c0d9a4dc /src/base
parent5f813f8a4bbe1e075daad7b3933829044befffb4 (diff)
downloadqxmpp-6685217ac78c7588aaea67059a0844d51713dcd5.tar.gz
Implement XEP-0264: Jingle Content Thumbnails
XEP-0264: Jingle Content Thumbnails version 0.4. https://xmpp.org/extensions/xep-0264.html
Diffstat (limited to 'src/base')
-rw-r--r--src/base/QXmppConstants.cpp2
-rw-r--r--src/base/QXmppConstants_p.h2
-rw-r--r--src/base/QXmppThumbnail.cpp144
-rw-r--r--src/base/QXmppThumbnail.h51
4 files changed, 199 insertions, 0 deletions
diff --git a/src/base/QXmppConstants.cpp b/src/base/QXmppConstants.cpp
index e4c49d25..30eb8d22 100644
--- a/src/base/QXmppConstants.cpp
+++ b/src/base/QXmppConstants.cpp
@@ -125,6 +125,8 @@ const char *ns_attention = "urn:xmpp:attention:0";
const char *ns_bob = "urn:xmpp:bob";
// XEP-0249: Direct MUC Invitations
const char *ns_conference = "jabber:x:conference";
+// XEP-0264: Jingle Content Thumbnails
+const char *ns_thumbs = "urn:xmpp:thumbs:1";
// XEP-0280: Message Carbons
const char *ns_carbons = "urn:xmpp:carbons:2";
// XEP-0297: Stanza Forwarding
diff --git a/src/base/QXmppConstants_p.h b/src/base/QXmppConstants_p.h
index 891ef2aa..32c1cdbd 100644
--- a/src/base/QXmppConstants_p.h
+++ b/src/base/QXmppConstants_p.h
@@ -137,6 +137,8 @@ extern const char *ns_attention;
extern const char *ns_bob;
// XEP-0249: Direct MUC Invitations
extern const char *ns_conference;
+// XEP-0264: Jingle Content Thumbnails
+extern const char *ns_thumbs;
// XEP-0280: Message Carbons
extern const char *ns_carbons;
// XEP-0297: Stanza Forwarding
diff --git a/src/base/QXmppThumbnail.cpp b/src/base/QXmppThumbnail.cpp
new file mode 100644
index 00000000..f5ccc89c
--- /dev/null
+++ b/src/base/QXmppThumbnail.cpp
@@ -0,0 +1,144 @@
+// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#include "QXmppThumbnail.h"
+
+#include "QXmppConstants_p.h"
+
+#include <QDomElement>
+#include <QMimeDatabase>
+#include <QMimeType>
+#include <QUrl>
+#include <QXmlStreamWriter>
+
+class QXmppThumbnailPrivate : public QSharedData
+{
+public:
+ QString uri;
+ QMimeType mediaType;
+ std::optional<uint32_t> width;
+ std::optional<uint32_t> height;
+};
+
+///
+/// \class QXmppThumbnail
+///
+/// Thumbnail from \xep{0264, Jingle Content Thumbnails}.
+///
+/// \since QXmpp 1.5
+///
+
+/// Default constructor
+QXmppThumbnail::QXmppThumbnail()
+ : d(new QXmppThumbnailPrivate)
+{
+}
+/// Default copy-constructor
+QXmppThumbnail::QXmppThumbnail(const QXmppThumbnail &) = default;
+/// Default move-constructor
+QXmppThumbnail::QXmppThumbnail(QXmppThumbnail &&) noexcept = default;
+QXmppThumbnail::~QXmppThumbnail() = default;
+/// Default assignment operator
+QXmppThumbnail &QXmppThumbnail::operator=(const QXmppThumbnail &) = default;
+/// Default move-assignment operator
+QXmppThumbnail &QXmppThumbnail::operator=(QXmppThumbnail &&) noexcept = default;
+
+/// Returns the URI with the location for the data (usually a \xep{0231, Bits of Binary} content ID)
+const QString &QXmppThumbnail::uri() const
+{
+ return d->uri;
+}
+
+/// Sets the URI with the location for the data (usually a \xep{0231, Bits of Binary} content ID)
+void QXmppThumbnail::setUri(const QString &newUri)
+{
+ d->uri = newUri;
+}
+
+/// Returns the MIME type of the thumbnail data.
+const QMimeType &QXmppThumbnail::mediaType() const
+{
+ return d->mediaType;
+}
+
+/// Sets the MIME type of the thumbnail data.
+void QXmppThumbnail::setMediaType(const QMimeType &newMediaType)
+{
+ d->mediaType = newMediaType;
+}
+
+/// Returns the width of the thumbnail image.
+std::optional<uint32_t> QXmppThumbnail::width() const
+{
+ return d->width;
+}
+
+/// Sets the width of the thumbnail image.
+void QXmppThumbnail::setWidth(std::optional<uint32_t> newWidth)
+{
+ d->width = newWidth;
+}
+
+/// Returns the height of the thumbnail image.
+std::optional<uint32_t> QXmppThumbnail::height() const
+{
+ return d->height;
+}
+
+/// Sets the height of the thumbnail image.
+void QXmppThumbnail::setHeight(std::optional<uint32_t> newHeight)
+{
+ d->height = newHeight;
+}
+
+/// \cond
+bool QXmppThumbnail::parse(const QDomElement &el)
+{
+ if (el.tagName() == "thumbnail" && el.namespaceURI() == ns_thumbs) {
+ if (!el.hasAttribute("uri")) {
+ return false;
+ }
+
+ d->uri = el.attribute("uri");
+ if (el.hasAttribute("media-type")) {
+ d->mediaType = QMimeDatabase().mimeTypeForName(el.attribute(QStringLiteral("media-type")));
+ }
+
+ bool success = false;
+ if (auto string = el.attribute("width"); !string.isEmpty()) {
+ if (auto parsedInt = string.toUInt(&success); success) {
+ d->width = parsedInt;
+ } else {
+ return false;
+ }
+ }
+ if (auto string = el.attribute("height"); !string.isEmpty()) {
+ if (auto parsedInt = string.toUInt(&success); success) {
+ d->height = parsedInt;
+ } else {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+}
+
+void QXmppThumbnail::toXml(QXmlStreamWriter *writer) const
+{
+ writer->writeStartElement(QStringLiteral("thumbnail"));
+ writer->writeDefaultNamespace(ns_thumbs);
+ writer->writeAttribute(QStringLiteral("uri"), d->uri);
+ if (d->mediaType.isValid()) {
+ writer->writeAttribute("media-type", d->mediaType.name());
+ }
+ if (d->width) {
+ writer->writeAttribute("width", QString::number(*d->width));
+ }
+ if (d->height) {
+ writer->writeAttribute("height", QString::number(*d->height));
+ }
+ writer->writeEndElement();
+}
+/// \endcond
diff --git a/src/base/QXmppThumbnail.h b/src/base/QXmppThumbnail.h
new file mode 100644
index 00000000..50853642
--- /dev/null
+++ b/src/base/QXmppThumbnail.h
@@ -0,0 +1,51 @@
+// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#ifndef QXMPPTHUMBNAIL_H
+#define QXMPPTHUMBNAIL_H
+
+#include "QXmppGlobal.h"
+
+#include <optional>
+
+#include <QSharedDataPointer>
+
+class QDomElement;
+class QMimeType;
+class QXmlStreamWriter;
+class QXmppThumbnailPrivate;
+
+class QXMPP_EXPORT QXmppThumbnail
+{
+public:
+ QXmppThumbnail();
+ QXmppThumbnail(const QXmppThumbnail &);
+ QXmppThumbnail(QXmppThumbnail &&) noexcept;
+ ~QXmppThumbnail();
+
+ QXmppThumbnail &operator=(const QXmppThumbnail &);
+ QXmppThumbnail &operator=(QXmppThumbnail &&) noexcept;
+
+ const QString &uri() const;
+ void setUri(const QString &newUri);
+
+ const QMimeType &mediaType() const;
+ void setMediaType(const QMimeType &);
+
+ std::optional<uint32_t> width() const;
+ void setWidth(std::optional<uint32_t>);
+
+ std::optional<uint32_t> height() const;
+ void setHeight(std::optional<uint32_t>);
+
+ /// \cond
+ bool parse(const QDomElement &);
+ void toXml(QXmlStreamWriter *writer) const;
+ /// \endcond
+
+private:
+ QSharedDataPointer<QXmppThumbnailPrivate> d;
+};
+
+#endif // QXMPPTHUMBNAIL_H