From 6685217ac78c7588aaea67059a0844d51713dcd5 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Mon, 5 Sep 2022 20:21:59 +0200 Subject: Implement XEP-0264: Jingle Content Thumbnails XEP-0264: Jingle Content Thumbnails version 0.4. https://xmpp.org/extensions/xep-0264.html --- src/base/QXmppThumbnail.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/base/QXmppThumbnail.cpp (limited to 'src/base/QXmppThumbnail.cpp') 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 +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppThumbnail.h" + +#include "QXmppConstants_p.h" + +#include +#include +#include +#include +#include + +class QXmppThumbnailPrivate : public QSharedData +{ +public: + QString uri; + QMimeType mediaType; + std::optional width; + std::optional 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 QXmppThumbnail::width() const +{ + return d->width; +} + +/// Sets the width of the thumbnail image. +void QXmppThumbnail::setWidth(std::optional newWidth) +{ + d->width = newWidth; +} + +/// Returns the height of the thumbnail image. +std::optional QXmppThumbnail::height() const +{ + return d->height; +} + +/// Sets the height of the thumbnail image. +void QXmppThumbnail::setHeight(std::optional 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 -- cgit v1.2.3