QXmpp Version: 1.10.0
QXmppPep_p.h
1// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#include <QXmppPubSubEvent.h>
6#include <QXmppPubSubManager.h>
7
8namespace QXmpp::Private::Pep {
9
10template<typename T>
11using GetResult = std::variant<T, QXmppError>;
12using PublishResult = std::variant<QString, QXmppError>;
13
14template<typename ItemT>
15inline QXmppTask<GetResult<ItemT>> request(QXmppPubSubManager *pubSub, const QString &jid, const QString &nodeName, QObject *parent)
16{
17 using PubSub = QXmppPubSubManager;
18
19 auto process = [](PubSub::ItemsResult<ItemT> &&result) -> GetResult<ItemT> {
20 if (const auto itemsResult = std::get_if<PubSub::Items<ItemT>>(&result)) {
21 if (!itemsResult->items.isEmpty()) {
22 return itemsResult->items.takeFirst();
23 }
24 return QXmppError { QStringLiteral("User has no published items."), {} };
25 } else {
26 return std::get<QXmppError>(std::move(result));
27 }
28 };
29 return chain<GetResult<ItemT>>(pubSub->requestItems<ItemT>(jid, nodeName), parent, process);
30}
31
32// NodeName is a template parameter, so the right qstring comparison overload is used
33// (if we used 'const QString &' as type, a 'const char *' string would be converted)
34template<typename ItemT, typename NodeName, typename Manager, typename ReceivedSignal>
35inline bool handlePubSubEvent(const QDomElement &element, const QString &pubSubService, const QString &eventNode, NodeName nodeName, Manager *manager, ReceivedSignal itemReceived)
36{
37 if (eventNode == nodeName && QXmppPubSubEvent<ItemT>::isPubSubEvent(element)) {
39 event.parse(element);
40
41 if (event.eventType() == QXmppPubSubEventBase::Items) {
42 if (!event.items().isEmpty()) {
43 (manager->*itemReceived)(pubSubService, event.items().constFirst());
44 } else {
45 (manager->*itemReceived)(pubSubService, {});
46 }
47 return true;
48 } else if (event.eventType() == QXmppPubSubEventBase::Retract) {
49 (manager->*itemReceived)(pubSubService, {});
50 return true;
51 }
52 }
53 return false;
54}
55
56} // namespace QXmpp::Private::Pep
virtual void parse(const QDomElement &)=0
Definition: QXmppOmemoManager.h:69
EventType eventType() const
Definition: QXmppPubSubEvent.cpp:104
The QXmppPubSubEvent class represents a PubSub event notification as defined by XEP-0060: Publish-Sub...
Definition: QXmppPubSubEvent.h:78
QVector< T > items() const
Definition: QXmppPubSubEvent.h:99
The QXmppPubSubManager aims to provide publish-subscribe functionality as specified in XEP-0060: Publ...
Definition: QXmppPubSubManager.h:21
QXmppTask< ItemsResult< T > > requestItems(const QString &jid, const QString &nodeName)
Definition: QXmppPubSubManager.h:210
Definition: QXmppTask.h:62
Definition: QXmppError.h:17