QXmpp  Version: 1.15.1
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 
10 namespace QXmpp::Private::Pep {
11 
12 template<typename T>
13 using GetResult = std::variant<T, QXmppError>;
14 using PublishResult = std::variant<QString, QXmppError>;
15 
16 template<typename ItemT>
17 inline 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)
33 template<typename ItemT, typename NodeName, typename Manager, typename ReceivedSignal>
34 inline 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)) {
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
const T & getValue(const Result< T > &r)
Definition: QXmppGlobal.h:232
Definition: QXmppPep_p.h:10
Definition: QXmppError.h:17
Definition: QXmppTask.h:67
virtual void parse(const QDomElement &)=0
QVector< T > items() const
Definition: QXmppPubSubEvent.h:99
QXmppTask< ItemsResult< T > > requestItems(const QString &jid, const QString &nodeName)
Definition: QXmppPubSubManager.h:209
const QXmppError & getError(const Result< T > &r)
Definition: QXmppError.h:68
EventType eventType() const
Definition: QXmppPubSubEvent.cpp:109
Definition: QXmppOmemoManager.h:68
bool hasValue(const Result< T > &r)
Definition: QXmppGlobal.h:216
The QXmppPubSubEvent class represents a PubSub event notification as defined by XEP-0060: Publish-Sub...
Definition: QXmppPubSubEvent.h:77
The QXmppPubSubManager aims to provide publish-subscribe functionality as specified in XEP-0060: Publ...
Definition: QXmppPubSubManager.h:20