aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2022-03-03 16:32:01 +0100
committerLinus Jahn <lnj@kaidan.im>2022-03-03 18:09:24 +0100
commit0f12fdbecb361e80d87fa1f00b98a4fc8ab55996 (patch)
treeda2aac4072ba92755777c01428683848641a220c /src/client
parent274da5d7bc049c748b0eb04f24275efe15ab7720 (diff)
downloadqxmpp-0f12fdbecb361e80d87fa1f00b98a4fc8ab55996.tar.gz
Add (private) helpers for PEP managers
The code for PEP managers is often very similiar, this should make it a bit easier.
Diffstat (limited to 'src/client')
-rw-r--r--src/client/QXmppPep_p.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/client/QXmppPep_p.h b/src/client/QXmppPep_p.h
new file mode 100644
index 00000000..f525fdef
--- /dev/null
+++ b/src/client/QXmppPep_p.h
@@ -0,0 +1,54 @@
+// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+#include <QXmppPubSubEvent.h>
+#include <QXmppPubSubManager.h>
+
+namespace QXmpp::Private::Pep {
+
+template<typename T>
+using GetResult = std::variant<T, QXmppStanza::Error>;
+using PublishResult = std::variant<QString, QXmppStanza::Error>;
+
+template<typename ItemT>
+inline QFuture<GetResult<ItemT>> request(QXmppPubSubManager *pubSub, const QString &jid, const QString &nodeName, QObject *parent)
+{
+ using PubSub = QXmppPubSubManager;
+ using Error = QXmppStanza::Error;
+
+ auto process = [](PubSub::ItemsResult<ItemT> &&result) -> GetResult<ItemT> {
+ if (const auto itemsResult = std::get_if<PubSub::Items<ItemT>>(&result)) {
+ if (!itemsResult->items.isEmpty()) {
+ return itemsResult->items.takeFirst();
+ }
+ return Error(Error::Cancel, Error::ItemNotFound, QStringLiteral("User has no published items."));
+ } else {
+ return std::get<Error>(result);
+ }
+ };
+ return chain<GetResult<ItemT>>(pubSub->requestItems<ItemT>(jid, nodeName), parent, process);
+}
+
+// NodeName is a template parameter, so the right qstring comparison overload is used
+// (if we used 'const QString &' as type, a 'const char *' string would be converted)
+template<typename ItemT, typename NodeName, typename Manager, typename ReceivedSignal>
+inline bool handlePubSubEvent(const QDomElement &element, const QString &pubSubService, const QString &eventNode, NodeName nodeName, Manager *manager, ReceivedSignal itemReceived)
+{
+ if (eventNode == nodeName && QXmppPubSubEvent<ItemT>::isPubSubEvent(element)) {
+ QXmppPubSubEvent<ItemT> event;
+ event.parse(element);
+
+ if (event.eventType() == QXmppPubSubEventBase::Items) {
+ if (!event.items().isEmpty()) {
+ (manager->*itemReceived)(pubSubService, event.items().constFirst());
+ } else {
+ (manager->*itemReceived)(pubSubService, {});
+ }
+ return true;
+ }
+ }
+ return false;
+}
+
+}