aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2021-08-01 22:48:27 +0200
committerLinus Jahn <lnj@kaidan.im>2021-08-22 16:09:02 +0200
commitbcc19801f86a155177791d36cce20d9227d17dd4 (patch)
tree37c0ba09371c93930ceaff634a7c0564a0a689c6
parentd4f5a0c418bf30c8e2d125957adff0e5ff731e54 (diff)
downloadqxmpp-bcc19801f86a155177791d36cce20d9227d17dd4.tar.gz
Implement XEP-0118: User Tune: Add manager with tests
-rw-r--r--doc/xep.doc1
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/base/QXmppConstants.cpp1
-rw-r--r--src/base/QXmppConstants_p.h1
-rw-r--r--src/client/QXmppUserTuneManager.cpp134
-rw-r--r--src/client/QXmppUserTuneManager.h56
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/qxmppusertunemanager/tst_qxmppusertunemanager.cpp127
8 files changed, 323 insertions, 0 deletions
diff --git a/doc/xep.doc b/doc/xep.doc
index c32dd0a0..8e2ae949 100644
--- a/doc/xep.doc
+++ b/doc/xep.doc
@@ -23,6 +23,7 @@ Complete:
- \xep{0095, Stream Initiation}
- \xep{0096, SI File Transfer}
- \xep{0115, Entity Capabilities}
+- \xep{0118, User Tune} (v1.3)
- \xep{0128, Service Discovery Extensions}
- \xep{0136, Message Archiving}
- \xep{0153, vCard-Based Avatars}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 00d853ee..7715c1ff 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -98,6 +98,7 @@ set(INSTALL_HEADER_FILES
client/QXmppTransferManager.h
client/QXmppTransferManager_p.h
client/QXmppUploadRequestManager.h
+ client/QXmppUserTuneManager.h
client/QXmppVCardManager.h
client/QXmppVersionManager.h
@@ -196,6 +197,7 @@ set(SOURCE_FILES
client/QXmppTlsManager.cpp
client/QXmppTransferManager.cpp
client/QXmppUploadRequestManager.cpp
+ client/QXmppUserTuneManager.cpp
client/QXmppVCardManager.cpp
client/QXmppVersionManager.cpp
diff --git a/src/base/QXmppConstants.cpp b/src/base/QXmppConstants.cpp
index 5350cda3..26c162be 100644
--- a/src/base/QXmppConstants.cpp
+++ b/src/base/QXmppConstants.cpp
@@ -92,6 +92,7 @@ const char* ns_activity = "http://jabber.org/protocol/activity";
const char* ns_capabilities = "http://jabber.org/protocol/caps";
// XEP-0118: User Tune
const char* ns_tune = "http://jabber.org/protocol/tune";
+const char* ns_tune_notify = "http://jabber.org/protocol/tune+notify";
// 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 bd1d39f7..4407c5a0 100644
--- a/src/base/QXmppConstants_p.h
+++ b/src/base/QXmppConstants_p.h
@@ -104,6 +104,7 @@ extern const char* ns_activity;
extern const char* ns_capabilities;
// XEP-0118: User Tune
extern const char* ns_tune;
+extern const char* ns_tune_notify;
// XEP-0136: Message Archiving
extern const char* ns_archive;
// XEP-0138: Stream Compression
diff --git a/src/client/QXmppUserTuneManager.cpp b/src/client/QXmppUserTuneManager.cpp
new file mode 100644
index 00000000..73bc257e
--- /dev/null
+++ b/src/client/QXmppUserTuneManager.cpp
@@ -0,0 +1,134 @@
+/*
+ * 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 "QXmppUserTuneManager.h"
+
+#include "QXmppConstants_p.h"
+#include "QXmppFutureUtils_p.h"
+#include "QXmppPubSubEvent.h"
+#include "QXmppPubSubManager.h"
+#include "QXmppTuneItem.h"
+
+using namespace QXmpp::Private;
+
+///
+/// \class QXmppUserTuneManager
+///
+/// The QXmppUserTuneManager implements \xep{0118, User Tune}. You'll receive
+/// tune updates from all presence subscriptions. You can publish tune
+/// information on the user's account (publish()) and request tune information
+/// from specific accounts (request()).
+///
+/// The manager needs to be added to the client first and also requires the
+/// QXmppPubSubManager.
+/// \code
+/// QXmppClient client;
+/// auto *pubSubManager = client.addNewExtension<QXmppPubSubManager>();
+/// auto *tuneManager = client.addNewExtension<QXmppUserTuneManager>();
+/// \endcode
+///
+/// \since QXmpp 1.5
+///
+
+///
+/// \typedef QXmppUserTuneManager::TuneResult
+///
+/// Contains the User Tune information or an error.
+///
+
+///
+/// \typedef QXmppUserTuneManager::PublishResult
+///
+/// Contains the ID of the published item on success or a stanza error.
+///
+
+///
+/// \fn QXmppUserTuneManager::userTuneChanged()
+///
+/// Emitted whenever an \xep{0118, User Tune} items event arrives.
+///
+
+QXmppUserTuneManager::QXmppUserTuneManager()
+{
+}
+
+QStringList QXmppUserTuneManager::discoveryFeatures() const
+{
+ return {
+ ns_tune,
+ ns_tune_notify,
+ };
+}
+
+///
+/// Request User Tune information from an account.
+///
+/// \param jid The account JID to request.
+///
+QFuture<QXmppUserTuneManager::TuneResult> QXmppUserTuneManager::request(const QString &jid)
+{
+ using PubSub = QXmppPubSubManager;
+ using Error = QXmppStanza::Error;
+
+ return chain<TuneResult>(pubSub()->requestItems<QXmppTuneItem>(jid, ns_tune), this,
+ [](PubSub::ItemsResult<QXmppTuneItem> &&result) -> TuneResult {
+ if (const auto items = std::get_if<PubSub::Items<QXmppTuneItem>>(&result)) {
+ if (!items->items.isEmpty()) {
+ return items->items.constFirst();
+ }
+ return Error(Error::Cancel, Error::ItemNotFound, QStringLiteral("No tune available."));
+ } else {
+ return std::get<QXmppStanza::Error>(result);
+ }
+ });
+}
+
+///
+/// Publishes User Tune information on the user's account.
+///
+/// \param item The User Tune item to be published.
+///
+QFuture<QXmppUserTuneManager::PublishResult> QXmppUserTuneManager::publish(const QXmppTuneItem &item)
+{
+ return pubSub()->publishPepItem(ns_tune, item);
+}
+
+/// \cond
+bool QXmppUserTuneManager::handlePubSubEvent(const QDomElement &element, const QString &pubSubService, const QString &nodeName)
+{
+ if (nodeName == ns_tune && QXmppPubSubEvent<QXmppTuneItem>::isPubSubEvent(element)) {
+ QXmppPubSubEvent<QXmppTuneItem> event;
+ event.parse(element);
+
+ if (event.eventType() == QXmppPubSubEventBase::Items) {
+ if (!event.items().isEmpty()) {
+ emit userTuneChanged(pubSubService, event.items().constFirst());
+ } else {
+ emit userTuneChanged(pubSubService, {});
+ }
+ return true;
+ }
+ }
+ return false;
+}
+/// \endcond
diff --git a/src/client/QXmppUserTuneManager.h b/src/client/QXmppUserTuneManager.h
new file mode 100644
index 00000000..0ae99669
--- /dev/null
+++ b/src/client/QXmppUserTuneManager.h
@@ -0,0 +1,56 @@
+/*
+ * 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 QXMPPUSERTUNEMANAGER_H
+#define QXMPPUSERTUNEMANAGER_H
+
+#include "QXmppPubSubEventManager.h"
+
+#include <variant>
+
+class QXmppTuneItem;
+
+class QXMPP_EXPORT QXmppUserTuneManager : public QXmppPubSubEventManager
+{
+ Q_OBJECT
+
+public:
+ using TuneResult = std::variant<QXmppTuneItem, QXmppStanza::Error>;
+ using PublishResult = std::variant<QString, QXmppStanza::Error>;
+
+ QXmppUserTuneManager();
+
+ QStringList discoveryFeatures() const override;
+
+ QFuture<TuneResult> request(const QString &jid);
+ QFuture<PublishResult> publish(const QXmppTuneItem &);
+
+ Q_SIGNAL void userTuneChanged(const QString &jid, const QXmppTuneItem &);
+
+protected:
+ /// \cond
+ bool handlePubSubEvent(const QDomElement &element, const QString &pubSubService, const QString &nodeName) override;
+ /// \endcond
+};
+
+#endif // QXMPPUSERTUNEMANAGER_H
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 247ce0f2..bad03081 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -58,6 +58,7 @@ add_simple_test(qxmppstream)
add_simple_test(qxmppstreamfeatures)
add_simple_test(qxmppstunmessage)
add_simple_test(qxmpptrustmessages)
+add_simple_test(qxmppusertunemanager TestClient.h)
add_simple_test(qxmppvcardiq)
add_simple_test(qxmppvcardmanager)
add_simple_test(qxmppversioniq)
diff --git a/tests/qxmppusertunemanager/tst_qxmppusertunemanager.cpp b/tests/qxmppusertunemanager/tst_qxmppusertunemanager.cpp
new file mode 100644
index 00000000..c78e0019
--- /dev/null
+++ b/tests/qxmppusertunemanager/tst_qxmppusertunemanager.cpp
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2008-2020 The QXmpp developers
+ *
+ * Authors:
+ * Linus Jahn
+ * Germán Márquez Mejía
+ *
+ * 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 "QXmppPubSubManager.h"
+#include "QXmppTuneItem.h"
+#include "QXmppUserTuneManager.h"
+
+#include "TestClient.h"
+
+using PSManager = QXmppPubSubManager;
+
+class tst_QXmppUserTuneManager : public QObject
+{
+ Q_OBJECT
+
+private:
+ Q_SLOT void initTestCase();
+ Q_SLOT void testRequest();
+ Q_SLOT void testPublish();
+ Q_SLOT void testEvents();
+};
+
+void tst_QXmppUserTuneManager::initTestCase()
+{
+ qRegisterMetaType<QXmppTuneItem>();
+}
+
+void tst_QXmppUserTuneManager::testRequest()
+{
+ TestClient test;
+ test.addNewExtension<QXmppPubSubManager>();
+ auto *tuneManager = test.addNewExtension<QXmppUserTuneManager>();
+
+ auto future = tuneManager->request("anthony@qxmpp.org");
+ test.expect("<iq id=\"qxmpp1\" to=\"anthony@qxmpp.org\" type=\"get\"><pubsub xmlns=\"http://jabber.org/protocol/pubsub\"><items node=\"http://jabber.org/protocol/tune\"/></pubsub></iq>");
+ test.inject(QStringLiteral("<iq id=\"qxmpp1\" from=\"anthony@qxmpp.org\" type=\"result\">"
+ "<pubsub xmlns=\"http://jabber.org/protocol/pubsub\">"
+ "<items node=\"http://jabber.org/protocol/tune\">"
+ "<item id='abc3'><tune xmlns='http://jabber.org/protocol/tune'><title>I Kiste girl</title></tune></item>"
+ "</items>"
+ "</pubsub></iq>"));
+
+ QCoreApplication::processEvents();
+ auto item = expectFutureVariant<QXmppTuneItem>(future);
+ QCOMPARE(item.id(), QString("abc3"));
+ QCOMPARE(item.title(), QString("I Kiste girl"));
+}
+
+void tst_QXmppUserTuneManager::testPublish()
+{
+ TestClient test;
+ test.configuration().setJid("stpeter@jabber.org");
+ test.addNewExtension<QXmppPubSubManager>();
+ auto *tuneManager = test.addNewExtension<QXmppUserTuneManager>();
+
+ QXmppTuneItem item;
+ item.setArtist("Yes");
+ item.setLength(686);
+ item.setRating(8);
+ item.setSource("Yessongs");
+ item.setTitle("Heart of the Sunrise");
+ item.setTrack("3");
+ item.setUri({ "http://www.yesworld.com/lyrics/Fragile.html#9" });
+
+ auto future = tuneManager->publish(item);
+ test.expect("<iq id='qxmpp1' to='stpeter@jabber.org' type='set'>"
+ "<pubsub xmlns='http://jabber.org/protocol/pubsub'>"
+ "<publish node='http://jabber.org/protocol/tune'>"
+ "<item><tune xmlns='http://jabber.org/protocol/tune'>"
+ "<artist>Yes</artist><length>686</length><rating>8</rating><source>Yessongs</source><title>Heart of the Sunrise</title><track>3</track><uri>http://www.yesworld.com/lyrics/Fragile.html#9</uri></tune></item>"
+ "</publish>"
+ "</pubsub></iq>");
+ test.inject(QStringLiteral("<iq type='result' from='stpeter@jabber.org' id='qxmpp1'>"
+ "<pubsub xmlns='http://jabber.org/protocol/pubsub'>"
+ "<publish node='http://jabber.org/protocol/tune'>"
+ "<item id='abcdf'/>"
+ "</publish></pubsub></iq>"));
+
+ QCOMPARE(expectFutureVariant<QString>(future), QString("abcdf"));
+}
+
+void tst_QXmppUserTuneManager::testEvents()
+{
+ TestClient test;
+ test.configuration().setJid("stpeter@jabber.org");
+ auto *psManager = test.addNewExtension<QXmppPubSubManager>();
+ auto *tuneManager = test.addNewExtension<QXmppUserTuneManager>();
+
+ QSignalSpy spy(tuneManager, &QXmppUserTuneManager::userTuneChanged);
+
+ psManager->handleStanza(xmlToDom(QStringLiteral("<message from='stpeter@jabber.org' to='maineboy@jabber.org'>"
+ "<event xmlns='http://jabber.org/protocol/pubsub#event'>"
+ "<items node='http://jabber.org/protocol/tune'>"
+ "<item id='bffe6584-0f9c-11dc-84ba-001143d5d5db'>"
+ "<tune xmlns='http://jabber.org/protocol/tune'>"
+ "<artist>Yes</artist><length>686</length><rating>8</rating><source>Yessongs</source><title>Heart of the Sunrise</title><track>3</track><uri>http://www.yesworld.com/lyrics/Fragile.html#9</uri>"
+ "</tune></item></items>"
+ "</event></message>")));
+
+ QCOMPARE(spy.count(), 1);
+ QCOMPARE(spy.constFirst().at(0).toString(), QString("stpeter@jabber.org"));
+ QCOMPARE(spy.constFirst().at(1).value<QXmppTuneItem>().artist(), QString("Yes"));
+}
+
+QTEST_MAIN(tst_QXmppUserTuneManager)
+#include "tst_qxmppusertunemanager.moc"