QXmpp  Version: 1.4.0
QXmppClient.h
1 /*
2  * Copyright (C) 2008-2021 The QXmpp developers
3  *
4  * Author:
5  * Manjeet Dahiya
6  *
7  * Source:
8  * https://github.com/qxmpp-project/qxmpp
9  *
10  * This file is a part of QXmpp library.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  */
23 
24 #ifndef QXMPPCLIENT_H
25 #define QXMPPCLIENT_H
26 
27 #include "QXmppConfiguration.h"
28 #include "QXmppLogger.h"
29 #include "QXmppPresence.h"
30 
31 #include <QAbstractSocket>
32 #include <QObject>
33 #include <QSslError>
34 
36 class QXmppClientPrivate;
37 class QXmppPresence;
38 class QXmppMessage;
39 class QXmppIq;
40 class QXmppStream;
41 class QXmppInternalClientExtension;
42 
43 // managers
44 class QXmppDiscoveryIq;
45 class QXmppRosterManager;
46 class QXmppVCardManager;
48 
56 
64 
93 class QXMPP_EXPORT QXmppClient : public QXmppLoggable
94 {
95  Q_OBJECT
96 
98  Q_PROPERTY(QXmppLogger *logger READ logger WRITE setLogger NOTIFY loggerChanged)
100  Q_PROPERTY(State state READ state NOTIFY stateChanged)
101 
102 public:
105  enum Error {
110  };
111  Q_ENUM(Error)
112 
113 
114  enum State {
117  ConnectedState
118  };
119  Q_ENUM(State)
120 
121 
128  ResumedStream
129  };
130 
131  QXmppClient(QObject *parent = nullptr);
132  ~QXmppClient() override;
133 
134  bool addExtension(QXmppClientExtension *extension);
135  bool insertExtension(int index, QXmppClientExtension *extension);
136  bool removeExtension(QXmppClientExtension *extension);
137 
138  QList<QXmppClientExtension *> extensions();
139 
153  template<typename T>
155  {
156  const QList<QXmppClientExtension *> list = extensions();
157  for (auto ext : list) {
158  T *extension = qobject_cast<T *>(ext);
159  if (extension)
160  return extension;
161  }
162  return nullptr;
163  }
164 
180  template<typename T>
182  {
183  auto list = extensions();
184  for (int i = 0; i < list.size(); ++i) {
185  if (qobject_cast<T *>(list.at(i)) != nullptr)
186  return i;
187  }
188  return -1;
189  }
190 
191  bool isAuthenticated() const;
192  bool isConnected() const;
193 
194  bool isActive() const;
195  void setActive(bool active);
196 
197  StreamManagementState streamManagementState() const;
198 
199  QXmppPresence clientPresence() const;
200  void setClientPresence(const QXmppPresence &presence);
201 
202  QXmppConfiguration &configuration();
203 
204  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
206  QXmppLogger *logger() const;
207  void setLogger(QXmppLogger *logger);
208 
209  QAbstractSocket::SocketError socketError();
210  QString socketErrorString() const;
211 
212  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
214  State state() const;
215  QXmppStanza::Error::Condition xmppStreamError();
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 
228 Q_SIGNALS:
229 
250  void connected();
251 
253  void disconnected();
254 
261 
263  void loggerChanged(QXmppLogger *logger);
264 
269  void messageReceived(const QXmppMessage &message);
270 
275  void presenceReceived(const QXmppPresence &presence);
276 
283  void iqReceived(const QXmppIq &iq);
284 
287  void sslErrors(const QList<QSslError> &errors);
288 
291 
292 public Q_SLOTS:
293  void connectToServer(const QXmppConfiguration &,
294  const QXmppPresence &initialPresence =
295  QXmppPresence());
296  void connectToServer(const QString &jid,
297  const QString &password);
298  void disconnectFromServer();
299  bool sendPacket(const QXmppStanza &);
300  void sendMessage(const QString &bareJid, const QString &message);
301 
302 private Q_SLOTS:
303  void _q_elementReceived(const QDomElement &element, bool &handled);
304  void _q_reconnect();
305  void _q_socketStateChanged(QAbstractSocket::SocketState state);
306  void _q_streamConnected();
307  void _q_streamDisconnected();
308  void _q_streamError(QXmppClient::Error error);
309 
310 private:
311  QXmppClientPrivate *const d;
312 
313  friend class QXmppInternalClientExtension;
314 };
315 
316 #endif // QXMPPCLIENT_H
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:47
The QXmppClient class is the main class for using QXmpp.
Definition: QXmppClient.h:94
void presenceReceived(const QXmppPresence &presence)
void error(QXmppClient::Error)
void connected()
State
This enumeration describes a client state.
Definition: QXmppClient.h:114
@ DisconnectedState
Disconnected from the server.
Definition: QXmppClient.h:115
@ ConnectingState
Trying to connect to the server.
Definition: QXmppClient.h:116
T * findExtension()
Returns the extension which can be cast into type T*, or 0 if there is no such extension.
Definition: QXmppClient.h:154
void loggerChanged(QXmppLogger *logger)
This signal is emitted when the logger changes.
Error
Definition: QXmppClient.h:105
@ NoError
No error.
Definition: QXmppClient.h:106
@ XmppStreamError
Error due to XML stream.
Definition: QXmppClient.h:109
@ KeepAliveError
Error due to no response to a keep alive.
Definition: QXmppClient.h:108
@ SocketError
Error due to TCP socket.
Definition: QXmppClient.h:107
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:122
@ NoStreamManagement
Stream Management is not used.
Definition: QXmppClient.h:124
@ NewStream
Stream Management is used and the previous stream has not been resumed.
Definition: QXmppClient.h:126
void iqReceived(const QXmppIq &iq)
void disconnected()
This signal is emitted when the XMPP connection disconnects.
int indexOfExtension()
Returns the index of an extension.
Definition: QXmppClient.h:181
The QXmppConfiguration class holds configuration options.
Definition: QXmppConfiguration.h:48
QXmppDiscoveryIq represents a discovery IQ request or result containing a list of features and other ...
Definition: QXmppDiscoveryIq.h:44
The QXmppIq class is the base class for all IQs.
Definition: QXmppIq.h:42
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:124
The QXmppLogger class represents a sink for logging messages.
Definition: QXmppLogger.h:46
The QXmppMessage class represents an XMPP message.
Definition: QXmppMessage.h:46
The QXmppPresence class represents an XMPP presence stanza.
Definition: QXmppPresence.h:36
The QXmppRosterManager class provides access to a connected client's roster.
Definition: QXmppRosterManager.h:69
Condition
A detailed condition of the error.
Definition: QXmppStanza.h:121
The QXmppStanza class is the base class for all XMPP stanzas.
Definition: QXmppStanza.h:100
The QXmppStream class is the base class for all XMPP streams.
Definition: QXmppStream.h:42
The QXmppVCardManager class gets/sets XMPP vCards. It is an implementation of XEP-0054: vcard-temp.
Definition: QXmppVCardManager.h:57
The QXmppVersionManager class makes it possible to request for the software version of an entity as d...
Definition: QXmppVersionManager.h:43