QXmpp: QXmppPep_p.h Source File

QXmpp Version: 1.14.2

Loading...
Searching...
No Matches
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 (hasValue(result)) {
21 if (!getValue(result).items.isEmpty()) {
22 return getValue(result).items.takeFirst();
23 }
24 return QXmppError { QStringLiteral("User has no published items."), {} };
25 } else {
26 return getError(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)) {
38 QXmppPubSubEvent<ItemT> event;
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
QXmppPubSubEventBase::eventType
EventType eventType() const
Definition QXmppPubSubEvent.cpp:109
QXmppPubSubEvent::isPubSubEvent
static bool isPubSubEvent(const QDomElement &element)
Definition QXmppPubSubEvent.h:118
QXmppPubSubEvent::items
QVector< T > items() const
Definition QXmppPubSubEvent.h:99
QXmppPubSubManager::requestItems
QXmppTask< ItemsResult< T > > requestItems(const QString &jid, const QString &nodeName)
Definition QXmppPubSubManager.h:210
QXmpp::getValue
const T & getValue(const Result< T > &r)
Definition QXmppGlobal.h:232
QXmpp::hasValue
bool hasValue(const Result< T > &r)
Definition QXmppGlobal.h:216
QXmpp::getError
const QXmppError & getError(const Result< T > &r)
Definition QXmppError.h:68