aboutsummaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2020-06-22 22:57:44 +0200
committerLinus Jahn <lnj@kaidan.im>2021-08-22 16:09:02 +0200
commitd4f5a0c418bf30c8e2d125957adff0e5ff731e54 (patch)
treedac8305ffdaa2b3e0b67d9419326a0feee08ce41 /src/base
parentdaa3385a3d0b14202cb6cdadaaca72c93614ecc6 (diff)
downloadqxmpp-d4f5a0c418bf30c8e2d125957adff0e5ff731e54.tar.gz
Implement XEP-0118: User Tune: Add PubSub item
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/QXmppTuneItem.cpp264
-rw-r--r--src/base/QXmppTuneItem.h78
4 files changed, 346 insertions, 0 deletions
diff --git a/src/base/QXmppConstants.cpp b/src/base/QXmppConstants.cpp
index a4e3b26a..5350cda3 100644
--- a/src/base/QXmppConstants.cpp
+++ b/src/base/QXmppConstants.cpp
@@ -90,6 +90,8 @@ const char* ns_stream_initiation_file_transfer = "http://jabber.org/protocol/si/
const char* ns_activity = "http://jabber.org/protocol/activity";
// XEP-0115: Entity Capabilities
const char* ns_capabilities = "http://jabber.org/protocol/caps";
+// XEP-0118: User Tune
+const char* ns_tune = "http://jabber.org/protocol/tune";
// XEP-0136: Message Archiving
const char* ns_archive = "urn:xmpp:archive";
// XEP-0138: Stream Compression
diff --git a/src/base/QXmppConstants_p.h b/src/base/QXmppConstants_p.h
index 93f5e8e2..bd1d39f7 100644
--- a/src/base/QXmppConstants_p.h
+++ b/src/base/QXmppConstants_p.h
@@ -102,6 +102,8 @@ extern const char* ns_stream_initiation_file_transfer;
extern const char* ns_activity;
// XEP-0115: Entity Capabilities
extern const char* ns_capabilities;
+// XEP-0118: User Tune
+extern const char* ns_tune;
// XEP-0136: Message Archiving
extern const char* ns_archive;
// XEP-0138: Stream Compression
diff --git a/src/base/QXmppTuneItem.cpp b/src/base/QXmppTuneItem.cpp
new file mode 100644
index 00000000..fe6efd75
--- /dev/null
+++ b/src/base/QXmppTuneItem.cpp
@@ -0,0 +1,264 @@
+/*
+ * 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 "QXmppTuneItem.h"
+
+#include "QXmppConstants_p.h"
+#include "QXmppUtils.h"
+
+#include <QDomElement>
+#include <QUrl>
+#include <QXmlStreamWriter>
+
+/// \cond
+class QXmppTuneItemPrivate : public QSharedData
+{
+public:
+ QXmppTuneItemPrivate();
+
+ QString artist;
+ quint16 length;
+ quint8 rating;
+ QString source;
+ QString title;
+ QString track;
+ QUrl uri;
+};
+
+QXmppTuneItemPrivate::QXmppTuneItemPrivate()
+ : length(0),
+ rating(0)
+{
+}
+/// \endcond
+
+///
+/// \class QXmppTuneItem
+///
+/// This class represents a PubSub item for \xep{0118, User Tune}.
+///
+/// \since QXmpp 1.5
+///
+
+///
+/// Default constructor
+///
+QXmppTuneItem::QXmppTuneItem()
+ : d(new QXmppTuneItemPrivate)
+{
+}
+
+/// Copy-constructor.
+QXmppTuneItem::QXmppTuneItem(const QXmppTuneItem &other) = default;
+
+QXmppTuneItem::~QXmppTuneItem() = default;
+
+/// Assignment operator.
+QXmppTuneItem &QXmppTuneItem::operator=(const QXmppTuneItem &other) = default;
+
+///
+/// Returns the artist of the piece or song.
+///
+QString QXmppTuneItem::artist() const
+{
+ return d->artist;
+}
+
+///
+/// Sets the artist of the piece or song.
+///
+void QXmppTuneItem::setArtist(const QString &artist)
+{
+ d->artist = artist;
+}
+
+///
+/// Returns the length of the piece in seconds (0 means unknown).
+///
+quint16 QXmppTuneItem::length() const
+{
+ return d->length;
+}
+
+///
+/// Sets the length of the piece in seconds (0 means unknown).
+///
+void QXmppTuneItem::setLength(quint16 length)
+{
+ d->length = length;
+}
+
+///
+/// Returns the user's rating of the song or piece (from 1 to 10), 0 means
+/// invalid or unknown.
+///
+quint8 QXmppTuneItem::rating() const
+{
+ return d->rating;
+}
+
+///
+/// Sets the user's rating of the song or piece (from 1 to 10), 0 means invalid
+/// or unknown.
+///
+void QXmppTuneItem::setRating(quint8 rating)
+{
+ if (rating > 10)
+ d->rating = 0;
+ else
+ d->rating = rating;
+}
+
+///
+/// Returns the album, other collection or other source (e.g. website) of the
+/// piece.
+///
+QString QXmppTuneItem::source() const
+{
+ return d->source;
+}
+
+///
+/// Sets the album, other collection or other source (e.g. website) of the
+/// piece.
+///
+void QXmppTuneItem::setSource(const QString &source)
+{
+ d->source = source;
+}
+
+///
+/// Returns the title of the piece.
+///
+QString QXmppTuneItem::title() const
+{
+ return d->title;
+}
+
+///
+/// Sets the title of the piece.
+///
+void QXmppTuneItem::setTitle(const QString &title)
+{
+ d->title = title;
+}
+
+///
+/// Returns the track number or other identifier in the collection or source.
+///
+QString QXmppTuneItem::track() const
+{
+ return d->track;
+}
+
+///
+/// Sets the track number or other identifier in the collection or source.
+///
+void QXmppTuneItem::setTrack(const QString &track)
+{
+ d->track = track;
+}
+
+///
+/// Returns an URI or URL pointing to information about the song, collection or
+/// artist.
+///
+QUrl QXmppTuneItem::uri() const
+{
+ return d->uri;
+}
+
+///
+/// Sets an URI or URL pointing to information about the song, collection or
+/// artist.
+///
+void QXmppTuneItem::setUri(const QUrl &uri)
+{
+ d->uri = uri;
+}
+
+///
+/// Returns true, if the element is a valid \xep{0118}: User Tune PubSub item.
+///
+bool QXmppTuneItem::isItem(const QDomElement &itemElement)
+{
+ auto isPayloadValid = [](const QDomElement &payload) -> bool {
+ return payload.tagName() == QStringLiteral("tune") &&
+ payload.namespaceURI() == ns_tune;
+ };
+
+ return QXmppPubSubItem::isItem(itemElement, isPayloadValid);
+}
+
+/// \cond
+void QXmppTuneItem::parsePayload(const QDomElement &tune)
+{
+ auto child = tune.firstChildElement();
+ while (!child.isNull()) {
+ if (child.tagName() == QStringLiteral("artist")) {
+ d->artist = child.text();
+ } else if (child.tagName() == QStringLiteral("length")) {
+ bool ok = false;
+ d->length = child.text().toUShort(&ok);
+
+ if (!ok) {
+ d->length = 0;
+ }
+ } else if (child.tagName() == QStringLiteral("rating")) {
+ bool ok = false;
+ d->rating = child.text().toUShort(&ok);
+
+ if (!ok || d->rating > 10) {
+ d->rating = 0;
+ }
+ } else if (child.tagName() == QStringLiteral("source")) {
+ d->source = child.text();
+ } else if (child.tagName() == QStringLiteral("title")) {
+ d->title = child.text();
+ } else if (child.tagName() == QStringLiteral("track")) {
+ d->track = child.text();
+ } else if (child.tagName() == QStringLiteral("uri")) {
+ d->uri = QUrl(child.text());
+ }
+ child = child.nextSiblingElement();
+ }
+}
+
+void QXmppTuneItem::serializePayload(QXmlStreamWriter *writer) const
+{
+ writer->writeStartElement(QStringLiteral("tune"));
+ writer->writeDefaultNamespace(ns_tune);
+
+ helperToXmlAddTextElement(writer, QStringLiteral("artist"), d->artist);
+ if (d->length != 0)
+ writer->writeTextElement(QStringLiteral("length"), QString::number(d->length));
+ if (d->rating != 0)
+ writer->writeTextElement(QStringLiteral("rating"), QString::number(d->rating));
+ helperToXmlAddTextElement(writer, QStringLiteral("source"), d->source);
+ helperToXmlAddTextElement(writer, QStringLiteral("title"), d->title);
+ helperToXmlAddTextElement(writer, QStringLiteral("track"), d->track);
+ helperToXmlAddTextElement(writer, QStringLiteral("uri"), d->uri.toString(QUrl::FullyEncoded));
+
+ writer->writeEndElement();
+}
+/// \endcond
diff --git a/src/base/QXmppTuneItem.h b/src/base/QXmppTuneItem.h
new file mode 100644
index 00000000..da7345d5
--- /dev/null
+++ b/src/base/QXmppTuneItem.h
@@ -0,0 +1,78 @@
+/*
+ * 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.
+ *
+ */
+
+#ifndef QXMPPTUNEITEM_H
+#define QXMPPTUNEITEM_H
+
+#include "QXmppPubSubItem.h"
+
+#include <QSharedDataPointer>
+
+class QXmppTuneItemPrivate;
+class QUrl;
+
+class QXMPP_EXPORT QXmppTuneItem : public QXmppPubSubItem
+{
+public:
+ QXmppTuneItem();
+ QXmppTuneItem(const QXmppTuneItem &other);
+ ~QXmppTuneItem();
+
+ QXmppTuneItem &operator=(const QXmppTuneItem &other);
+
+ QString artist() const;
+ void setArtist(const QString &artist);
+
+ quint16 length() const;
+ void setLength(quint16 length);
+
+ quint8 rating() const;
+ void setRating(quint8 rating);
+
+ QString source() const;
+ void setSource(const QString &source);
+
+ QString title() const;
+ void setTitle(const QString &title);
+
+ QString track() const;
+ void setTrack(const QString &track);
+
+ QUrl uri() const;
+ void setUri(const QUrl &uri);
+
+ static bool isItem(const QDomElement &itemElement);
+
+protected:
+ /// \cond
+ void parsePayload(const QDomElement &payloadElement) override;
+ void serializePayload(QXmlStreamWriter *writer) const override;
+ /// \endcond
+
+private:
+ QSharedDataPointer<QXmppTuneItemPrivate> d;
+};
+
+Q_DECLARE_METATYPE(QXmppTuneItem)
+
+#endif // QXMPPTUNEITEM_H