QXmpp  Version:1.3.0
QXmppClient.h
1 /*
2  * Copyright (C) 2008-2020 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 
34 class QSslError;
35 
37 class QXmppClientPrivate;
38 class QXmppPresence;
39 class QXmppMessage;
40 class QXmppIq;
41 class QXmppStream;
42 class QXmppInternalClientExtension;
43 
44 // managers
45 class QXmppDiscoveryIq;
46 class QXmppRosterManager;
47 class QXmppVCardManager;
49 
57 
65 
93 
94 class QXMPP_EXPORT QXmppClient : public QXmppLoggable
95 {
96  Q_OBJECT
97 
99  Q_PROPERTY(QXmppLogger *logger READ logger WRITE setLogger NOTIFY loggerChanged)
101  Q_PROPERTY(State state READ state NOTIFY stateChanged)
102 
103 public:
106  enum Error {
111  };
112  Q_ENUM(Error)
113 
114 
115  enum State {
118  ConnectedState
119  };
120  Q_ENUM(State)
121 
122  QXmppClient(QObject *parent = nullptr);
123  ~QXmppClient() override;
124 
125  bool addExtension(QXmppClientExtension *extension);
126  bool insertExtension(int index, QXmppClientExtension *extension);
127  bool removeExtension(QXmppClientExtension *extension);
128 
129  QList<QXmppClientExtension *> extensions();
130 
143  template<typename T>
145  {
146  const QList<QXmppClientExtension *> list = extensions();
147  for (auto ext : list) {
148  T *extension = qobject_cast<T *>(ext);
149  if (extension)
150  return extension;
151  }
152  return nullptr;
153  }
154 
169  template<typename T>
171  {
172  auto list = extensions();
173  for (int i = 0; i < list.size(); ++i) {
174  if (qobject_cast<T *>(list.at(i)) != nullptr)
175  return i;
176  }
177  return -1;
178  }
179 
180  bool isAuthenticated() const;
181  bool isConnected() const;
182 
183  bool isActive() const;
184  void setActive(bool active);
185 
186  QXmppPresence clientPresence() const;
187  void setClientPresence(const QXmppPresence &presence);
188 
189  QXmppConfiguration &configuration();
190 
191  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
193  QXmppLogger *logger() const;
194  void setLogger(QXmppLogger *logger);
195 
196  QAbstractSocket::SocketError socketError();
197  QString socketErrorString() const;
198 
199  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
201  State state() const;
202  QXmppStanza::Error::Condition xmppStreamError();
203 
204 #if QXMPP_DEPRECATED_SINCE(1, 1)
205  QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppRosterManager>() instead")
206  QXmppRosterManager &rosterManager();
207 
208  QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppVCardManager>() instead")
209  QXmppVCardManager &vCardManager();
210 
211  QT_DEPRECATED_X("Use QXmppClient::findExtension<QXmppVersionManager>() instead")
212  QXmppVersionManager &versionManager();
213 #endif
214 
215 Q_SIGNALS:
216 
237  void connected();
238 
240  void disconnected();
241 
247  void error(QXmppClient::Error);
248 
250  void loggerChanged(QXmppLogger *logger);
251 
256  void messageReceived(const QXmppMessage &message);
257 
262  void presenceReceived(const QXmppPresence &presence);
263 
268  void iqReceived(const QXmppIq &iq);
269 
272  void sslErrors(const QList<QSslError> &errors);
273 
275  void stateChanged(QXmppClient::State state);
276 
277 public Q_SLOTS:
278  void connectToServer(const QXmppConfiguration &,
279  const QXmppPresence &initialPresence =
280  QXmppPresence());
281  void connectToServer(const QString &jid,
282  const QString &password);
283  void disconnectFromServer();
284  bool sendPacket(const QXmppStanza &);
285  void sendMessage(const QString &bareJid, const QString &message);
286 
287 private Q_SLOTS:
288  void _q_elementReceived(const QDomElement &element, bool &handled);
289  void _q_reconnect();
290  void _q_socketStateChanged(QAbstractSocket::SocketState state);
291  void _q_streamConnected();
292  void _q_streamDisconnected();
293  void _q_streamError(QXmppClient::Error error);
294 
295 private:
296  QXmppClientPrivate *const d;
297 
298  friend class QXmppInternalClientExtension;
299 };
300 
301 #endif // QXMPPCLIENT_H
QXmppPresence
The QXmppPresence class represents an XMPP presence stanza.
Definition: QXmppPresence.h:35
QXmppClient::KeepAliveError
Error due to no response to a keep alive.
Definition: QXmppClient.h:109
QXmppClient::indexOfExtension
int indexOfExtension()
Returns the index of an extension.
Definition: QXmppClient.h:170
QXmppRosterManager
The QXmppRosterManager class provides access to a connected client's roster.
Definition: QXmppRosterManager.h:67
QXmppVCardManager
The QXmppVCardManager class gets/sets XMPP vCards. It is an implementation of XEP-0054: vcard-temp.
Definition: QXmppVCardManager.h:56
QXmppClientExtension
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:47
QXmppStanza
The QXmppStanza class is the base class for all XMPP stanzas.
Definition: QXmppStanza.h:99
QXmppClient::SocketError
Error due to TCP socket.
Definition: QXmppClient.h:108
QXmppClient::findExtension
T * findExtension()
Returns the extension which can be cast into type T*, or 0 if there is no such extension.
Definition: QXmppClient.h:144
QXmppClient::Error
Error
Definition: QXmppClient.h:106
QXmppClient::NoError
No error.
Definition: QXmppClient.h:107
QXmppClient::State
State
This enumeration describes a client state.
Definition: QXmppClient.h:115
QXmppLogger
The QXmppLogger class represents a sink for logging messages.
Definition: QXmppLogger.h:45
QXmppConfiguration
The QXmppConfiguration class holds configuration options.
Definition: QXmppConfiguration.h:47
QXmppClient::DisconnectedState
Disconnected from the server.
Definition: QXmppClient.h:116
QXmppStream
The QXmppStream class is the base class for all XMPP streams.
Definition: QXmppStream.h:41
QXmppStanza::Error::Condition
Condition
A detailed condition of the error.
Definition: QXmppStanza.h:121
QXmppClient::ConnectingState
Trying to connect to the server.
Definition: QXmppClient.h:117
QXmppClient::XmppStreamError
Error due to XML stream.
Definition: QXmppClient.h:110
QXmppIq
The QXmppIq class is the base class for all IQs.
Definition: QXmppIq.h:41
QXmppClient
The QXmppClient class is the main class for using QXmpp.
Definition: QXmppClient.h:94
QXmppLoggable
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:123
QXmppVersionManager
The QXmppVersionManager class makes it possible to request for the software version of an entity as d...
Definition: QXmppVersionManager.h:42
QXmppDiscoveryIq
QXmppDiscoveryIq represents a discovery IQ request or result containing a list of features and other ...
Definition: QXmppDiscoveryIq.h:43
QXmppMessage
The QXmppMessage class represents an XMPP message.
Definition: QXmppMessage.h:42