QXmppClient.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Manjeet Dahiya
  6. *
  7. * Source:
  8. * http://code.google.com/p/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. #ifndef QXMPPCLIENT_H
  24. #define QXMPPCLIENT_H
  25. #include <QObject>
  26. #include <QAbstractSocket>
  27. #include "QXmppConfiguration.h"
  28. #include "QXmppLogger.h"
  29. #include "QXmppPresence.h"
  30. class QXmppClientExtension;
  31. class QXmppClientPrivate;
  32. class QXmppPresence;
  33. class QXmppMessage;
  34. class QXmppIq;
  35. class QXmppStream;
  36. // managers
  37. class QXmppDiscoveryIq;
  38. class QXmppRosterManager;
  39. class QXmppVCardManager;
  40. class QXmppVersionManager;
  41. /// \defgroup Core
  42. /// \defgroup Managers
  43. /// \brief The QXmppClient class is the main class for using QXmpp.
  44. ///
  45. /// It provides the user all the required functionality to connect to the
  46. /// server and perform operations afterwards.
  47. ///
  48. /// This class will provide the handle/reference to QXmppRosterManager
  49. /// (roster management), QXmppVCardManager (vCard manager), and
  50. /// QXmppVersionManager (software version information).
  51. ///
  52. /// By default, the client will automatically try reconnecting to the server.
  53. /// You can change this a behaviour using
  54. /// QXmppConfiguration::setAutoReconnectionEnabled().
  55. ///
  56. /// Not all the managers or extensions have been enabled by default. One can
  57. /// enable/disable the managers using the funtions addExtension() and
  58. /// removeExtension(). findExtension() can be used to find reference/pointer to
  59. /// particular instansiated and enabled manager.
  60. ///
  61. /// List of managers enabled by default:
  62. /// - QXmppRosterManager
  63. /// - QXmppVCardManager
  64. /// - QXmppVersionManager
  65. /// - QXmppDiscoveryManager
  66. /// - QXmppEntityTimeManager
  67. ///
  68. /// \ingroup Core
  69. class QXMPP_EXPORT QXmppClient : public QXmppLoggable
  70. {
  71. Q_OBJECT
  72. Q_ENUMS(Error State)
  73. Q_PROPERTY(QXmppLogger* logger READ logger WRITE setLogger NOTIFY loggerChanged)
  74. Q_PROPERTY(State state READ state NOTIFY stateChanged)
  75. public:
  76. /// An enumeration for type of error.
  77. /// Error could come due a TCP socket or XML stream or due to various stanzas.
  78. enum Error
  79. {
  80. NoError, ///< No error.
  81. SocketError, ///< Error due to TCP socket.
  82. KeepAliveError, ///< Error due to no response to a keep alive.
  83. XmppStreamError, ///< Error due to XML stream.
  84. };
  85. /// This enumeration describes a client state.
  86. enum State
  87. {
  88. DisconnectedState, ///< Disconnected from the server.
  89. ConnectingState, ///< Trying to connect to the server.
  90. ConnectedState, ///< Connected to the server.
  91. };
  92. QXmppClient(QObject *parent = 0);
  93. ~QXmppClient();
  94. bool addExtension(QXmppClientExtension* extension);
  95. bool removeExtension(QXmppClientExtension* extension);
  96. QList<QXmppClientExtension*> extensions();
  97. /// \brief Returns the extension which can be cast into type T*, or 0
  98. /// if there is no such extension.
  99. ///
  100. /// Usage example:
  101. /// \code
  102. /// QXmppDiscoveryManager* ext = client->findExtension<QXmppDiscoveryManager>();
  103. /// if(ext)
  104. /// {
  105. /// //extension found, do stuff...
  106. /// }
  107. /// \endcode
  108. ///
  109. template<typename T>
  110. T* findExtension()
  111. {
  112. QList<QXmppClientExtension*> list = extensions();
  113. for (int i = 0; i < list.size(); ++i)
  114. {
  115. T* extension = qobject_cast<T*>(list.at(i));
  116. if(extension)
  117. return extension;
  118. }
  119. return 0;
  120. }
  121. void connectToServer(const QXmppConfiguration&,
  122. const QXmppPresence& initialPresence =
  123. QXmppPresence());
  124. bool isAuthenticated() const;
  125. bool isConnected() const;
  126. QXmppPresence clientPresence() const;
  127. void setClientPresence(const QXmppPresence &presence);
  128. QXmppConfiguration &configuration();
  129. QXmppLogger *logger() const;
  130. void setLogger(QXmppLogger *logger);
  131. QAbstractSocket::SocketError socketError();
  132. State state() const;
  133. QXmppStanza::Error::Condition xmppStreamError();
  134. QXmppRosterManager& rosterManager();
  135. QXmppVCardManager& vCardManager();
  136. QXmppVersionManager& versionManager();
  137. signals:
  138. /// This signal is emitted when the client connects successfully to the XMPP
  139. /// server i.e. when a successful XMPP connection is established.
  140. /// XMPP Connection involves following sequential steps:
  141. /// - TCP socket connection
  142. /// - Client sends start stream
  143. /// - Server sends start stream
  144. /// - TLS negotiation (encryption)
  145. /// - Authentication
  146. /// - Resource binding
  147. /// - Session establishment
  148. ///
  149. /// After all these steps a successful XMPP connection is established and
  150. /// connected() signal is emitted.
  151. ///
  152. /// After the connected() signal is emitted QXmpp will send the roster request
  153. /// to the server. On receiving the roster, QXmpp will emit
  154. /// QXmppRosterManager::rosterReceived(). After this signal, QXmppRosterManager object gets
  155. /// populated and you can use rosterManager() to get the handle of QXmppRosterManager object.
  156. ///
  157. void connected();
  158. /// This signal is emitted when the XMPP connection disconnects.
  159. ///
  160. void disconnected();
  161. /// This signal is emitted when the XMPP connection encounters any error.
  162. /// The QXmppClient::Error parameter specifies the type of error occurred.
  163. /// It could be due to TCP socket or the xml stream or the stanza.
  164. /// Depending upon the type of error occurred use the respective get function to
  165. /// know the error.
  166. void error(QXmppClient::Error);
  167. /// This signal is emitted when the logger changes.
  168. void loggerChanged(QXmppLogger *logger);
  169. /// Notifies that an XMPP message stanza is received. The QXmppMessage
  170. /// parameter contains the details of the message sent to this client.
  171. /// In other words whenever someone sends you a message this signal is
  172. /// emitted.
  173. void messageReceived(const QXmppMessage &message);
  174. /// Notifies that an XMPP presence stanza is received. The QXmppPresence
  175. /// parameter contains the details of the presence sent to this client.
  176. /// This signal is emitted when someone login/logout or when someone's status
  177. /// changes Busy, Idle, Invisible etc.
  178. void presenceReceived(const QXmppPresence &presence);
  179. /// Notifies that an XMPP iq stanza is received. The QXmppIq
  180. /// parameter contains the details of the iq sent to this client.
  181. /// IQ stanzas provide a structured request-response mechanism. Roster
  182. /// management, setting-getting vCards etc is done using iq stanzas.
  183. void iqReceived(const QXmppIq &iq);
  184. /// This signal is emitted when the client state changes.
  185. void stateChanged(QXmppClient::State state);
  186. public slots:
  187. void connectToServer(const QString &jid,
  188. const QString &password);
  189. void disconnectFromServer();
  190. bool sendPacket(const QXmppStanza&);
  191. void sendMessage(const QString& bareJid, const QString& message);
  192. private slots:
  193. void _q_elementReceived(const QDomElement &element, bool &handled);
  194. void _q_reconnect();
  195. void _q_socketStateChanged(QAbstractSocket::SocketState state);
  196. void _q_streamConnected();
  197. void _q_streamDisconnected();
  198. void _q_streamError(QXmppClient::Error error);
  199. private:
  200. QXmppClientPrivate * const d;
  201. };
  202. #endif // QXMPPCLIENT_H