QXmpp Version: 1.15.0
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
8#include "StringLiterals.h"
9
10namespace QXmpp::Private::Pep {
11
12template<typename T>
13using GetResult = std::variant<T, QXmppError>;
14using PublishResult = std::variant<QString, QXmppError>;
15
16template<typename ItemT>
17inline QXmppTask<GetResult<ItemT>> request(QXmppPubSubManager *pubSub, const QString &jid, const QString &nodeName, QObject *parent)
18{
19 auto result = co_await pubSub->requestItems<ItemT>(jid, nodeName);
20
21 if (hasValue(result)) {
22 if (!getValue(result).items.isEmpty()) {
23 co_return getValue(result).items.constFirst();
24 }
25 co_return QXmppError { u"User has no published items."_s, {} };
26 } else {
27 co_return getError(std::move(result));
28 }
29}
30
31// NodeName is a template parameter, so the right qstring comparison overload is used
32// (if we used 'const QString &' as type, a 'const char *' string would be converted)
33template<typename ItemT, typename NodeName, typename Manager, typename ReceivedSignal>
34inline bool handlePubSubEvent(const QDomElement &element, const QString &pubSubService, const QString &eventNode, NodeName nodeName, Manager *manager, ReceivedSignal itemReceived)
35{
36 if (eventNode == nodeName && QXmppPubSubEvent<ItemT>::isPubSubEvent(element)) {
37 QXmppPubSubEvent<ItemT> event;
38 event.parse(element);
39
40 if (event.eventType() == QXmppPubSubEventBase::Items) {
41 if (!event.items().isEmpty()) {
42 (manager->*itemReceived)(pubSubService, event.items().constFirst());
43 } else {
44 (manager->*itemReceived)(pubSubService, {});
45 }
46 return true;
47 } else if (event.eventType() == QXmppPubSubEventBase::Retract) {
48 (manager->*itemReceived)(pubSubService, {});
49 return true;
50 }
51 }
52 return false;
53}
54
55} // namespace QXmpp::Private::Pep
EventType eventType() const
Definition QXmppPubSubEvent.cpp:109
static bool isPubSubEvent(const QDomElement &element)
Definition QXmppPubSubEvent.h:118
QVector< T > items() const
Definition QXmppPubSubEvent.h:99
QXmppTask< ItemsResult< T > > requestItems(const QString &jid, const QString &nodeName)
Definition QXmppPubSubManager.h:209
const T & getValue(const Result< T > &r)
Definition QXmppGlobal.h:232
bool hasValue(const Result< T > &r)
Definition QXmppGlobal.h:216
const QXmppError & getError(const Result< T > &r)
Definition QXmppError.h:68