QXmpp Version: 1.5.6
Loading...
Searching...
No Matches
QXmppClient.h
1// SPDX-FileCopyrightText: 2009 Manjeet Dahiya <manjeetdahiya@gmail.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef QXMPPCLIENT_H
6#define QXMPPCLIENT_H
7
8#include "QXmppConfiguration.h"
9#include "QXmppLogger.h"
10#include "QXmppPresence.h"
11#include "QXmppSendResult.h"
12#include "QXmppSendStanzaParams.h"
13
14#include <memory>
15#include <variant>
16
17#include <QAbstractSocket>
18#include <QObject>
19#include <QSslError>
20
21template<typename T>
22class QXmppTask;
23
26class QXmppClientPrivate;
27class QXmppPresence;
28class QXmppMessage;
29class QXmppIq;
30class QXmppStream;
31class QXmppInternalClientExtension;
32
33// managers
38
46
54
83class QXMPP_EXPORT QXmppClient : public QXmppLoggable
84{
85 Q_OBJECT
86
88 Q_PROPERTY(QXmppLogger *logger READ logger WRITE setLogger NOTIFY loggerChanged)
90 Q_PROPERTY(State state READ state NOTIFY stateChanged)
91
92public:
93 using IqResult = std::variant<QDomElement, QXmppError>;
94 using EmptyResult = std::variant<QXmpp::Success, QXmppError>;
95
104 Q_ENUM(Error)
105
106
112 Q_ENUM(State)
113
114
123
124 QXmppClient(QObject *parent = nullptr);
125 ~QXmppClient() override;
126
127 bool addExtension(QXmppClientExtension *extension);
128 template<typename T, typename... Args>
129 T *addNewExtension(Args... args)
130 {
131 // it's impossible that addExtension() returns false: ext is a new object
132 auto *ext = new T(args...);
133 addExtension(ext);
134 return ext;
135 }
136 bool insertExtension(int index, QXmppClientExtension *extension);
137 bool removeExtension(QXmppClientExtension *extension);
138 QXmppE2eeExtension *encryptionExtension() const;
139 void setEncryptionExtension(QXmppE2eeExtension *);
140
141 QList<QXmppClientExtension *> extensions();
142
156 template<typename T>
158 {
159 const QList<QXmppClientExtension *> list = extensions();
160 for (auto ext : list) {
161 T *extension = qobject_cast<T *>(ext);
162 if (extension) {
163 return extension;
164 }
165 }
166 return nullptr;
167 }
168
184 template<typename T>
186 {
187 auto list = extensions();
188 for (int i = 0; i < list.size(); ++i) {
189 if (qobject_cast<T *>(list.at(i)) != nullptr) {
190 return i;
191 }
192 }
193 return -1;
194 }
195
196 bool isAuthenticated() const;
197 bool isConnected() const;
198
199 bool isActive() const;
200 void setActive(bool active);
201
202 StreamManagementState streamManagementState() const;
203
204 QXmppPresence clientPresence() const;
205 void setClientPresence(const QXmppPresence &presence);
206
207 QXmppConfiguration &configuration();
208
209 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
211 QXmppLogger *logger() const;
212 void setLogger(QXmppLogger *logger);
213
214 QAbstractSocket::SocketError socketError();
215 QString socketErrorString() const;
216
217 // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
219 State state() const;
220 QXmppStanza::Error::Condition xmppStreamError();
221
222 QXmppTask<QXmpp::SendResult> sendSensitive(QXmppStanza &&, const std::optional<QXmppSendStanzaParams> & = {});
223 QXmppTask<QXmpp::SendResult> send(QXmppStanza &&, const std::optional<QXmppSendStanzaParams> & = {});
224 QXmppTask<QXmpp::SendResult> reply(QXmppStanza &&stanza, const std::optional<QXmppE2eeMetadata> &e2eeMetadata, const std::optional<QXmppSendStanzaParams> & = {});
225 QXmppTask<IqResult> sendIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> & = {});
226 QXmppTask<IqResult> sendSensitiveIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> & = {});
227 QXmppTask<EmptyResult> sendGenericIq(QXmppIq &&, const std::optional<QXmppSendStanzaParams> & = {});
228
229#if QXMPP_DEPRECATED_SINCE(1, 1)
230 QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppRosterManager>() instead")
231 QXmppRosterManager &rosterManager();
232
233 QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppVCardManager>() instead")
234 QXmppVCardManager &vCardManager();
235
236 QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppVersionManager>() instead")
237 QXmppVersionManager &versionManager();
238#endif
239
240Q_SIGNALS:
241
262 void connected();
263
266
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
304public Q_SLOTS:
305 void connectToServer(const QXmppConfiguration &,
306 const QXmppPresence &initialPresence =
307 QXmppPresence());
308 void connectToServer(const QString &jid,
309 const QString &password);
310 void disconnectFromServer();
311 bool sendPacket(const QXmppNonza &);
312 void sendMessage(const QString &bareJid, const QString &message);
313
314private:
315 void injectIq(const QDomElement &element, const std::optional<QXmppE2eeMetadata> &e2eeMetadata);
316 bool injectMessage(QXmppMessage &&message);
317
318private Q_SLOTS:
319 void _q_elementReceived(const QDomElement &element, bool &handled);
320 void _q_reconnect();
321 void _q_socketStateChanged(QAbstractSocket::SocketState state);
322 void _q_streamConnected();
323 void _q_streamDisconnected();
324 void _q_streamError(QXmppClient::Error error);
325
326private:
327 const std::unique_ptr<QXmppClientPrivate> d;
328
329 friend class QXmppClientExtension;
330 friend class QXmppInternalClientExtension;
331 friend class TestClient;
332};
333
334#endif // QXMPPCLIENT_H
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition QXmppClientExtension.h:33
The QXmppClient class is the main class for using QXmpp.
Definition QXmppClient.h:84
void presenceReceived(const QXmppPresence &presence)
void error(QXmppClient::Error)
void connected()
std::variant< QDomElement, QXmppError > IqResult
Definition QXmppClient.h:93
State
This enumeration describes a client state.
Definition QXmppClient.h:107
@ DisconnectedState
Disconnected from the server.
Definition QXmppClient.h:108
@ ConnectingState
Trying to connect to the server.
Definition QXmppClient.h:109
void loggerChanged(QXmppLogger *logger)
This signal is emitted when the logger changes.
Error
Definition QXmppClient.h:98
@ NoError
No error.
Definition QXmppClient.h:99
@ XmppStreamError
Error due to XML stream.
Definition QXmppClient.h:102
@ KeepAliveError
Error due to no response to a keep alive.
Definition QXmppClient.h:101
@ SocketError
Error due to TCP socket.
Definition QXmppClient.h:100
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:115
@ NoStreamManagement
Stream Management is not used.
Definition QXmppClient.h:117
@ NewStream
Stream Management is used and the previous stream has not been resumed.
Definition QXmppClient.h:119
void iqReceived(const QXmppIq &iq)
T * addNewExtension(Args... args)
Definition QXmppClient.h:129
T * findExtension()
Returns the extension which can be cast into type T*, or 0 if there is no such extension.
Definition QXmppClient.h:157
void disconnected()
This signal is emitted when the XMPP connection disconnects.
std::variant< QXmpp::Success, QXmppError > EmptyResult
Definition QXmppClient.h:94
int indexOfExtension()
Returns the index of an extension.
Definition QXmppClient.h:185
The QXmppConfiguration class holds configuration options.
Definition QXmppConfiguration.h:29
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:105
The QXmppLogger class represents a sink for logging messages.
Definition QXmppLogger.h:27
The QXmppMessage class represents an XMPP message.
Definition QXmppMessage.h:35
Definition QXmppNonza.h:14
The QXmppPresence class represents an XMPP presence stanza.
Definition QXmppPresence.h:21
The QXmppRosterManager class provides access to a connected client's roster.
Definition QXmppRosterManager.h:56
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
The QXmppStream class is the base class for all XMPP streams.
Definition QXmppStream.h:36
Definition QXmppTask.h:62
The QXmppVCardManager class gets/sets XMPP vCards. It is an implementation of XEP-0054: vcard-temp.
Definition QXmppVCardManager.h:38
The QXmppVersionManager class makes it possible to request for the software version of an entity as d...
Definition QXmppVersionManager.h:24
Definition QXmppError.h:18