QXmpp: QXmppClient.h Source File

QXmpp Version: 1.14.4

Loading...
Searching...
No Matches
QXmppClient.h
1// SPDX-FileCopyrightText: 2009 Manjeet Dahiya <manjeetdahiya@gmail.com>
2// SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
3// SPDX-FileCopyrightText: 2023 Melvin Keskin <melvo@olomono.de>
4//
5// SPDX-License-Identifier: LGPL-2.1-or-later
6
7#ifndef QXMPPCLIENT_H
8#define QXMPPCLIENT_H
9
10#include "QXmppConfiguration.h"
11#include "QXmppLogger.h"
12#include "QXmppPresence.h"
13#include "QXmppSendResult.h"
14#include "QXmppSendStanzaParams.h"
15#include "QXmppStreamError.h"
16
17#include <memory>
18#include <variant>
19
20#include <QAbstractSocket>
21#include <QObject>
22#include <QSslError>
23
24template<typename T>
25class QXmppTask;
26
27class QXmppE2eeExtension;
28class QXmppClientExtension;
29class QXmppClientPrivate;
30class QXmppMessage;
31class QXmppOutgoingClient;
32class QXmppPresence;
33class QXmppIq;
34
35// managers
36class QXmppDiscoveryIq;
37class QXmppRosterManager;
38class QXmppVCardManager;
39class QXmppVersionManager;
40
41namespace QXmpp::Private {
42struct SessionBegin;
43}
44
52
60
61 class QXMPP_EXPORT QXmppClient : public QXmppLoggable
62{
63 Q_OBJECT
64
66 Q_PROPERTY(QXmppLogger *logger READ logger WRITE setLogger NOTIFY loggerChanged)
68 Q_PROPERTY(State state READ state NOTIFY stateChanged)
69
70public:
71 using IqResult = std::variant<QDomElement, QXmppError>;
72 using EmptyResult = std::variant<QXmpp::Success, QXmppError>;
73
76 enum Error {
77 NoError,
78 SocketError,
79 KeepAliveError,
80 XmppStreamError,
81 };
82 Q_ENUM(Error)
83
84
85 enum State {
86 DisconnectedState,
87 ConnectingState,
88 ConnectedState
89 };
90 Q_ENUM(State)
91
92
93 enum StreamManagementState {
95 NoStreamManagement,
97 NewStream,
99 ResumedStream
100 };
101
104 enum InitialExtensions {
106 NoExtensions,
108 BasicExtensions,
109 };
110
111 QXmppClient(InitialExtensions, QObject *parent = nullptr);
112 QXmppClient(QObject *parent = nullptr);
113 ~QXmppClient() override;
114
115 bool addExtension(QXmppClientExtension *extension);
116 template<typename T, typename... Args>
117 T *addNewExtension(Args... args)
118 {
119 // it's impossible that addExtension() returns false: ext is a new object
120 auto *ext = new T(args...);
121 addExtension(ext);
122 return ext;
123 }
124 bool insertExtension(int index, QXmppClientExtension *extension);
125 bool removeExtension(QXmppClientExtension *extension);
126 QXmppE2eeExtension *encryptionExtension() const;
127 void setEncryptionExtension(QXmppE2eeExtension *);
128
129 QList<QXmppClientExtension *> extensions() const;
130
144 template<typename T>
145 T *findExtension() const
146 {
147 const QList<QXmppClientExtension *> list = extensions();
148 for (auto ext : list) {
149 T *extension = qobject_cast<T *>(ext);
150 if (extension) {
151 return extension;
152 }
153 }
154 return nullptr;
155 }
156
172 template<typename T>
173 int indexOfExtension() const
174 {
175 auto list = extensions();
176 for (int i = 0; i < list.size(); ++i) {
177 if (qobject_cast<T *>(list.at(i)) != nullptr) {
178 return i;
179 }
180 }
181 return -1;
182 }
183
184 bool isAuthenticated() const;
185 bool isConnected() const;
186
187 bool isActive() const;
188 void setActive(bool active);
189
190 StreamManagementState streamManagementState() const;
191
192 QXmppPresence clientPresence() const;
193 void setClientPresence(const QXmppPresence &presence);
194
195 QXmppConfiguration &configuration();
196
197 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
199 QXmppLogger *logger() const;
200 void setLogger(QXmppLogger *logger);
201
202 QAbstractSocket::SocketError socketError();
203 QString socketErrorString() const;
204
205 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
207 State state() const;
208 QXmppStanza::Error::Condition xmppStreamError();
209
210 QXmppTask<QXmpp::SendResult> sendSensitive(QXmppStanza &&, const std::optional<QXmppSendStanzaParams> & = {});
211 QXmppTask<QXmpp::SendResult> send(QXmppStanza &&, const std::optional<QXmppSendStanzaParams> & = {});
212 QXmppTask<QXmpp::SendResult> reply(QXmppStanza &&stanza, const std::optional<QXmppE2eeMetadata> &e2eeMetadata, const std::optional<QXmppSendStanzaParams> & = {});
213 QXmppTask<IqResult> sendIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> & = {});
214 QXmppTask<IqResult> sendSensitiveIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> & = {});
215 QXmppTask<EmptyResult> sendGenericIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> & = {});
216
237 Q_SIGNAL void connected();
238
240 Q_SIGNAL void disconnected();
241
247 Q_SIGNAL void error(QXmppClient::Error);
248
259 Q_SIGNAL void errorOccurred(const QXmppError &error);
260
262 Q_SIGNAL void loggerChanged(QXmppLogger *logger);
263
268 Q_SIGNAL void messageReceived(const QXmppMessage &message);
269
274 Q_SIGNAL void presenceReceived(const QXmppPresence &presence);
275
282 Q_SIGNAL void iqReceived(const QXmppIq &iq);
283
286 Q_SIGNAL void sslErrors(const QList<QSslError> &errors);
287
289 Q_SIGNAL void stateChanged(QXmppClient::State state);
290
296 Q_SIGNAL void credentialsChanged();
297
298 Q_SLOT void connectToServer(const QXmppConfiguration &, const QXmppPresence &initialPresence = {});
299 Q_SLOT void connectToServer(const QString &jid, const QString &password);
300 Q_SLOT void disconnectFromServer();
301
302#if QXMPP_DEPRECATED_SINCE(1, 1)
303 [[deprecated("Use findExtension<QXmppRosterManager>() instead")]]
304 QXmppRosterManager &rosterManager();
305
306 [[deprecated("Use findExtension<QXmppVCardManager>() instead")]]
307 QXmppVCardManager &vCardManager();
308
309 [[deprecated("Use findExtension<QXmppVersionManager>() instead")]]
310 QXmppVersionManager &versionManager();
311#endif
312#if QXMPP_DEPRECATED_SINCE(1, 12)
313 [[deprecated("Use async send()")]]
314 Q_SLOT bool sendPacket(const QXmppNonza &);
315 [[deprecated("Use async send()")]]
316 Q_SLOT void sendMessage(const QString &bareJid, const QString &message);
317#endif
318
320 bool sendLegacy(const QXmppNonza &s)
321 {
322 QT_WARNING_PUSH
323 QT_WARNING_DISABLE_DEPRECATED
324 return sendPacket(s);
325 QT_WARNING_POP
326 }
327 QString sendLegacyId(const QXmppStanza &s) { return sendLegacy(s) ? s.id() : QString(); }
329
330private:
331 QXmppOutgoingClient *stream() const;
332 void injectIq(const QDomElement &element, const std::optional<QXmppE2eeMetadata> &e2eeMetadata);
333 bool injectMessage(QXmppMessage &&message);
334
335 void setIgnoredStreamErrors(const QVector<QXmpp::StreamError> &);
336
337 void _q_elementReceived(const QDomElement &element, bool &handled);
338 void _q_reconnect();
339 void onInternalSocketStateChanged();
340 void _q_streamConnected(const QXmpp::Private::SessionBegin &);
341 void _q_streamDisconnected();
342
343 const std::unique_ptr<QXmppClientPrivate> d;
344
345 friend class QXmppClientExtension;
346 friend class QXmppCarbonManagerV2;
347 friend class QXmppRegistrationManager;
348 friend class TestClient;
349};
350
351#endif // QXMPPCLIENT_H
QXmppClientExtension
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition QXmppClientExtension.h:32
QXmppClient
Main class for starting and managing connections to XMPP servers.
Definition QXmppClient.h:62
QXmppClient::connected
Q_SIGNAL void connected()
QXmppClient::credentialsChanged
Q_SIGNAL void credentialsChanged()
QXmppClient::loggerChanged
Q_SIGNAL void loggerChanged(QXmppLogger *logger)
This signal is emitted when the logger changes.
QXmppClient::indexOfExtension
int indexOfExtension() const
Returns the index of an extension.
Definition QXmppClient.h:173
QXmppClient::messageReceived
Q_SIGNAL void messageReceived(const QXmppMessage &message)
QXmppClient::findExtension
T * findExtension() const
Returns the extension which can be cast into type T*, or 0 if there is no such extension.
Definition QXmppClient.h:145
QXmppClient::error
Q_SIGNAL void error(QXmppClient::Error)
QXmppClient::InitialExtensions
InitialExtensions
Definition QXmppClient.h:104
QXmppClient::BasicExtensions
@ BasicExtensions
Creates a client with the default set of extensions.
Definition QXmppClient.h:108
QXmppClient::NoExtensions
@ NoExtensions
Creates a client without any extensions.
Definition QXmppClient.h:106
QXmppClient::disconnected
Q_SIGNAL void disconnected()
This signal is emitted when the XMPP connection disconnects.
QXmppClient::State
State
This enumeration describes a client state.
Definition QXmppClient.h:85
QXmppClient::DisconnectedState
@ DisconnectedState
Disconnected from the server.
Definition QXmppClient.h:86
QXmppClient::ConnectingState
@ ConnectingState
Trying to connect to the server.
Definition QXmppClient.h:87
QXmppClient::ConnectedState
@ ConnectedState
Connected to the server.
Definition QXmppClient.h:88
QXmppClient::addExtension
bool addExtension(QXmppClientExtension *extension)
Registers a new extension with the client.
Definition QXmppClient.cpp:378
QXmppClient::errorOccurred
Q_SIGNAL void errorOccurred(const QXmppError &error)
QXmppClient::Error
Error
Definition QXmppClient.h:76
QXmppClient::NoError
@ NoError
No error.
Definition QXmppClient.h:77
QXmppClient::XmppStreamError
@ XmppStreamError
Error due to XML stream.
Definition QXmppClient.h:80
QXmppClient::KeepAliveError
@ KeepAliveError
Error due to no response to a keep alive.
Definition QXmppClient.h:79
QXmppClient::SocketError
@ SocketError
Error due to TCP socket.
Definition QXmppClient.h:78
QXmppClient::state
State state
The client's current state.
Definition QXmppClient.h:68
QXmppClient::extensions
QList< QXmppClientExtension * > extensions() const
Returns a list containing all the client's extensions.
Definition QXmppClient.cpp:435
QXmppClient::stateChanged
Q_SIGNAL void stateChanged(QXmppClient::State state)
This signal is emitted when the client state changes.
QXmppClient::setLogger
void setLogger(QXmppLogger *logger)
Sets the QXmppLogger associated with the current QXmppClient.
Definition QXmppClient.cpp:947
QXmppClient::presenceReceived
Q_SIGNAL void presenceReceived(const QXmppPresence &presence)
QXmppClient::StreamManagementState
StreamManagementState
Describes the use of XEP-0198: Stream Management.
Definition QXmppClient.h:93
QXmppClient::ResumedStream
@ ResumedStream
Stream Management is used and the previous stream has been resumed.
Definition QXmppClient.h:99
QXmppClient::NoStreamManagement
@ NoStreamManagement
Stream Management is not used.
Definition QXmppClient.h:95
QXmppClient::NewStream
@ NewStream
Stream Management is used and the previous stream has not been resumed.
Definition QXmppClient.h:97
QXmppClient::connectToServer
Q_SLOT void connectToServer(const QXmppConfiguration &, const QXmppPresence &initialPresence={})
Definition QXmppClient.cpp:456
QXmppClient::addNewExtension
T * addNewExtension(Args... args)
Definition QXmppClient.h:117
QXmppClient::EmptyResult
std::variant< QXmpp::Success, QXmppError > EmptyResult
Definition QXmppClient.h:72
QXmppClient::QXmppClient
QXmppClient(InitialExtensions, QObject *parent=nullptr)
Definition QXmppClient.cpp:300
QXmppClient::sslErrors
Q_SIGNAL void sslErrors(const QList< QSslError > &errors)
QXmppClient::iqReceived
Q_SIGNAL void iqReceived(const QXmppIq &iq)
QXmppClient::logger
QXmppLogger * logger
The QXmppLogger associated with the current QXmppClient.
Definition QXmppClient.h:66
QXmppClient::IqResult
std::variant< QDomElement, QXmppError > IqResult
Definition QXmppClient.h:71
QXmppConfiguration
The QXmppConfiguration class holds configuration options.
Definition QXmppConfiguration.h:37
QXmppDiscoveryIq
Definition QXmppDiscoveryIq.h:200
QXmppE2eeExtension
Definition QXmppE2eeExtension.h:23
QXmppIq
The QXmppIq class is the base class for all IQs.
Definition QXmppIq.h:23
QXmppLoggable::QXmppLoggable
QXmppLoggable(QObject *parent=nullptr)
Definition QXmppLogger.cpp:57
QXmppLogger
The QXmppLogger class represents a sink for logging messages.
Definition QXmppLogger.h:29
QXmppMessage
The QXmppMessage class represents an XMPP message.
Definition QXmppMessage.h:64
QXmppNonza
Definition QXmppNonza.h:14
QXmppPresence
The QXmppPresence class represents an XMPP presence stanza.
Definition QXmppPresence.h:22
QXmppRosterManager
The QXmppRosterManager class provides access to a connected client's roster.
Definition QXmppRosterManager.h:84
QXmppStanza::Error::Condition
Condition
A detailed condition of the error.
Definition QXmppStanza.h:128
QXmppStanza
The QXmppStanza class is the base class for all XMPP stanzas.
Definition QXmppStanza.h:106
QXmppStanza::id
QString id() const
Definition QXmppStanza.cpp:750
QXmppTask
Definition QXmppTask.h:46
QXmppVCardManager
The QXmppVCardManager class gets/sets XMPP vCards. It is an implementation of XEP-0054: vcard-temp.
Definition QXmppVCardManager.h:43
QXmppVersionManager
The QXmppVersionManager class makes it possible to request for the software version of an entity as d...
Definition QXmppVersionManager.h:24
QXmpp
Definition Algorithms.h:14
QXmppError
Definition QXmppError.h:17