QXmpp Version: 1.10.0
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
29class QXmppClientPrivate;
30class QXmppMessage;
31class QXmppOutgoingClient;
32class QXmppPresence;
33class QXmppIq;
34
35// managers
40
41namespace QXmpp::Private {
42struct SessionBegin;
43}
44
52
60
61class 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 {
81 };
82 Q_ENUM(Error)
83
84
85 enum State {
88 ConnectedState
89 };
90 Q_ENUM(State)
91
92
99 ResumedStream
100 };
101
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>
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
217#if QXMPP_DEPRECATED_SINCE(1, 1)
218 QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppRosterManager>() instead")
219 QXmppRosterManager &rosterManager();
220
221 QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppVCardManager>() instead")
222 QXmppVCardManager &vCardManager();
223
224 QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppVersionManager>() instead")
225 QXmppVersionManager &versionManager();
226#endif
227
228Q_SIGNALS:
229
250 void connected();
251
254
261
272 Q_SIGNAL void errorOccurred(const QXmppError &error);
273
276
281 void messageReceived(const QXmppMessage &message);
282
287 void presenceReceived(const QXmppPresence &presence);
288
295 void iqReceived(const QXmppIq &iq);
296
299 void sslErrors(const QList<QSslError> &errors);
300
303
309 Q_SIGNAL void credentialsChanged();
310
311public Q_SLOTS:
312 void connectToServer(const QXmppConfiguration &,
313 const QXmppPresence &initialPresence =
314 QXmppPresence());
315 void connectToServer(const QString &jid,
316 const QString &password);
317 void disconnectFromServer();
318 bool sendPacket(const QXmppNonza &);
319 void sendMessage(const QString &bareJid, const QString &message);
320
321private:
322 QXmppOutgoingClient *stream() const;
323 void injectIq(const QDomElement &element, const std::optional<QXmppE2eeMetadata> &e2eeMetadata);
324 bool injectMessage(QXmppMessage &&message);
325
326 void setIgnoredStreamErrors(const QVector<QXmpp::StreamError> &);
327
328private Q_SLOTS:
329 void _q_elementReceived(const QDomElement &element, bool &handled);
330 void _q_reconnect();
331 void _q_socketStateChanged(QAbstractSocket::SocketState state);
332 void _q_streamConnected(const QXmpp::Private::SessionBegin &);
333 void _q_streamDisconnected();
334
335private:
336 const std::unique_ptr<QXmppClientPrivate> d;
337
338 friend class QXmppClientExtension;
339 friend class QXmppCarbonManagerV2;
340 friend class QXmppRegistrationManager;
341 friend class TestClient;
342};
343
344#endif // QXMPPCLIENT_H
The QXmppCarbonManagerV2 class handles message carbons as described in XEP-0280: Message Carbons.
Definition: QXmppCarbonManagerV2.h:11
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:32
Main class for starting and managing connections to XMPP servers.
Definition: QXmppClient.h:62
void presenceReceived(const QXmppPresence &presence)
Q_SIGNAL void credentialsChanged()
void error(QXmppClient::Error)
int indexOfExtension() const
Returns the index of an extension.
Definition: QXmppClient.h:173
void connected()
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
std::variant< QDomElement, QXmppError > IqResult
Definition: QXmppClient.h:71
InitialExtensions
Definition: QXmppClient.h:104
@ BasicExtensions
Creates a client with the default set of extensions.
Definition: QXmppClient.h:108
@ NoExtensions
Creates a client without any extensions.
Definition: QXmppClient.h:106
State
This enumeration describes a client state.
Definition: QXmppClient.h:85
@ DisconnectedState
Disconnected from the server.
Definition: QXmppClient.h:86
@ ConnectingState
Trying to connect to the server.
Definition: QXmppClient.h:87
Q_SIGNAL void errorOccurred(const QXmppError &error)
void loggerChanged(QXmppLogger *logger)
This signal is emitted when the logger changes.
Error
Definition: QXmppClient.h:76
@ NoError
No error.
Definition: QXmppClient.h:77
@ XmppStreamError
Error due to XML stream.
Definition: QXmppClient.h:80
@ KeepAliveError
Error due to no response to a keep alive.
Definition: QXmppClient.h:79
@ SocketError
Error due to TCP socket.
Definition: QXmppClient.h:78
void sslErrors(const QList< QSslError > &errors)
void stateChanged(QXmppClient::State state)
This signal is emitted when the client state changes.
void messageReceived(const QXmppMessage &message)
StreamManagementState
Describes the use of XEP-0198: Stream Management.
Definition: QXmppClient.h:93
@ NoStreamManagement
Stream Management is not used.
Definition: QXmppClient.h:95
@ NewStream
Stream Management is used and the previous stream has not been resumed.
Definition: QXmppClient.h:97
void iqReceived(const QXmppIq &iq)
T * addNewExtension(Args... args)
Definition: QXmppClient.h:117
void disconnected()
This signal is emitted when the XMPP connection disconnects.
std::variant< QXmpp::Success, QXmppError > EmptyResult
Definition: QXmppClient.h:72
The QXmppConfiguration class holds configuration options.
Definition: QXmppConfiguration.h:37
Definition: QXmppDiscoveryIq.h:18
Definition: QXmppE2eeExtension.h:23
The QXmppIq class is the base class for all IQs.
Definition: QXmppIq.h:23
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:110
The QXmppLogger class represents a sink for logging messages.
Definition: QXmppLogger.h:29
The QXmppMessage class represents an XMPP message.
Definition: QXmppMessage.h:64
Definition: QXmppNonza.h:14
The QXmppPresence class represents an XMPP presence stanza.
Definition: QXmppPresence.h:22
The QXmppRegistrationManager class manages in-band registration and account management tasks like cha...
Definition: QXmppRegistrationManager.h:225
The QXmppRosterManager class provides access to a connected client's roster.
Definition: QXmppRosterManager.h:57
Condition
A detailed condition of the error.
Definition: QXmppStanza.h:110
The QXmppStanza class is the base class for all XMPP stanzas.
Definition: QXmppStanza.h:88
Definition: QXmppTask.h:62
The QXmppVCardManager class gets/sets XMPP vCards. It is an implementation of XEP-0054: vcard-temp.
Definition: QXmppVCardManager.h:43
The QXmppVersionManager class makes it possible to request for the software version of an entity as d...
Definition: QXmppVersionManager.h:24
Definition: QXmppError.h:17