QXmppTransferManager.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Author:
  5. * Jeremy Lainé
  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 QXMPPTRANSFERMANAGER_H
  24. #define QXMPPTRANSFERMANAGER_H
  25. #include <QDateTime>
  26. #include <QSharedData>
  27. #include <QUrl>
  28. #include <QVariant>
  29. #include "QXmppClientExtension.h"
  30. class QTcpSocket;
  31. class QXmppByteStreamIq;
  32. class QXmppIbbCloseIq;
  33. class QXmppIbbDataIq;
  34. class QXmppIbbOpenIq;
  35. class QXmppIq;
  36. class QXmppStreamInitiationIq;
  37. class QXmppTransferFileInfoPrivate;
  38. class QXmppTransferJobPrivate;
  39. class QXmppTransferManager;
  40. class QXmppTransferManagerPrivate;
  41. class QXMPP_EXPORT QXmppTransferFileInfo
  42. {
  43. public:
  44. QXmppTransferFileInfo();
  45. QXmppTransferFileInfo(const QXmppTransferFileInfo &other);
  46. ~QXmppTransferFileInfo();
  47. QDateTime date() const;
  48. void setDate(const QDateTime &date);
  49. QByteArray hash() const;
  50. void setHash(const QByteArray &hash);
  51. QString name() const;
  52. void setName(const QString &name);
  53. QString description() const;
  54. void setDescription(const QString &description);
  55. qint64 size() const;
  56. void setSize(qint64 size);
  57. bool isNull() const;
  58. QXmppTransferFileInfo& operator=(const QXmppTransferFileInfo &other);
  59. bool operator==(const QXmppTransferFileInfo &other) const;
  60. /// \cond
  61. void parse(const QDomElement &element);
  62. void toXml(QXmlStreamWriter *writer) const;
  63. /// \endcond
  64. private:
  65. QSharedDataPointer<QXmppTransferFileInfoPrivate> d;
  66. };
  67. /// \brief The QXmppTransferJob class represents a single file transfer job.
  68. ///
  69. /// \sa QXmppTransferManager
  70. ///
  71. class QXMPP_EXPORT QXmppTransferJob : public QXmppLoggable
  72. {
  73. Q_OBJECT
  74. Q_ENUMS(Direction Error State)
  75. Q_FLAGS(Method Methods)
  76. Q_PROPERTY(Direction direction READ direction CONSTANT)
  77. Q_PROPERTY(QUrl localFileUrl READ localFileUrl WRITE setLocalFileUrl NOTIFY localFileUrlChanged)
  78. Q_PROPERTY(QString jid READ jid CONSTANT)
  79. Q_PROPERTY(Method method READ method CONSTANT)
  80. Q_PROPERTY(State state READ state NOTIFY stateChanged)
  81. Q_PROPERTY(QString fileName READ fileName CONSTANT)
  82. Q_PROPERTY(qint64 fileSize READ fileSize CONSTANT)
  83. public:
  84. /// This enum is used to describe the direction of a transfer job.
  85. enum Direction
  86. {
  87. IncomingDirection, ///< The file is being received.
  88. OutgoingDirection, ///< The file is being sent.
  89. };
  90. /// This enum is used to describe the type of error encountered by a transfer job.
  91. enum Error
  92. {
  93. NoError = 0, ///< No error occurred.
  94. AbortError, ///< The file transfer was aborted.
  95. FileAccessError, ///< An error was encountered trying to access a local file.
  96. FileCorruptError, ///< The file is corrupt: the file size or hash do not match.
  97. ProtocolError, ///< An error was encountered in the file transfer protocol.
  98. };
  99. /// This enum is used to describe a transfer method.
  100. enum Method
  101. {
  102. NoMethod = 0, ///< No transfer method.
  103. InBandMethod = 1, ///< XEP-0047: In-Band Bytestreams
  104. SocksMethod = 2, ///< XEP-0065: SOCKS5 Bytestreams
  105. AnyMethod = 3, ///< Any supported transfer method.
  106. };
  107. Q_DECLARE_FLAGS(Methods, Method)
  108. /// This enum is used to describe the state of a transfer job.
  109. enum State
  110. {
  111. OfferState = 0, ///< The transfer is being offered to the remote party.
  112. StartState = 1, ///< The transfer is being connected.
  113. TransferState = 2, ///< The transfer is ongoing.
  114. FinishedState = 3, ///< The transfer is finished.
  115. };
  116. ~QXmppTransferJob();
  117. QXmppTransferJob::Direction direction() const;
  118. QXmppTransferJob::Error error() const;
  119. QString jid() const;
  120. QXmppTransferJob::Method method() const;
  121. QString sid() const;
  122. qint64 speed() const;
  123. QXmppTransferJob::State state() const;
  124. // XEP-0096 : File transfer
  125. QXmppTransferFileInfo fileInfo() const;
  126. QUrl localFileUrl() const;
  127. void setLocalFileUrl(const QUrl &localFileUrl);
  128. /// \cond
  129. QDateTime fileDate() const;
  130. QByteArray fileHash() const;
  131. QString fileName() const;
  132. qint64 fileSize() const;
  133. /// \endcond
  134. signals:
  135. /// This signal is emitted when an error is encountered while
  136. /// processing the transfer job.
  137. void error(QXmppTransferJob::Error error);
  138. /// This signal is emitted when the transfer job is finished.
  139. ///
  140. /// You can determine if the job completed successfully by testing whether
  141. /// error() returns QXmppTransferJob::NoError.
  142. ///
  143. /// Note: Do not delete the job in the slot connected to this signal,
  144. /// instead use deleteLater().
  145. void finished();
  146. /// This signal is emitted when the local file URL changes.
  147. void localFileUrlChanged(const QUrl &localFileUrl);
  148. /// This signal is emitted to indicate the progress of this transfer job.
  149. void progress(qint64 done, qint64 total);
  150. /// This signal is emitted when the transfer job changes state.
  151. void stateChanged(QXmppTransferJob::State state);
  152. public slots:
  153. void abort();
  154. void accept(const QString &filePath);
  155. void accept(QIODevice *output);
  156. private slots:
  157. void _q_terminated();
  158. private:
  159. QXmppTransferJob(const QString &jid, QXmppTransferJob::Direction direction, QXmppClient *client, QObject *parent);
  160. void setState(QXmppTransferJob::State state);
  161. void terminate(QXmppTransferJob::Error error);
  162. QXmppTransferJobPrivate *const d;
  163. friend class QXmppTransferManager;
  164. friend class QXmppTransferManagerPrivate;
  165. friend class QXmppTransferIncomingJob;
  166. friend class QXmppTransferOutgoingJob;
  167. };
  168. /// \brief The QXmppTransferManager class provides support for sending and
  169. /// receiving files.
  170. ///
  171. /// Stream initiation is performed as described in XEP-0095: Stream Initiation
  172. /// and XEP-0096: SI File Transfer. The actual file transfer is then performed
  173. /// using either XEP-0065: SOCKS5 Bytestreams or XEP-0047: In-Band Bytestreams.
  174. ///
  175. /// To make use of this manager, you need to instantiate it and load it into
  176. /// the QXmppClient instance as follows:
  177. ///
  178. /// \code
  179. /// QXmppTransferManager *manager = new QXmppTransferManager;
  180. /// client->addExtension(manager);
  181. /// \endcode
  182. ///
  183. /// \ingroup Managers
  184. class QXMPP_EXPORT QXmppTransferManager : public QXmppClientExtension
  185. {
  186. Q_OBJECT
  187. Q_PROPERTY(QString proxy READ proxy WRITE setProxy)
  188. Q_PROPERTY(bool proxyOnly READ proxyOnly WRITE setProxyOnly)
  189. Q_PROPERTY(QXmppTransferJob::Methods supportedMethods READ supportedMethods WRITE setSupportedMethods)
  190. public:
  191. QXmppTransferManager();
  192. ~QXmppTransferManager();
  193. QString proxy() const;
  194. void setProxy(const QString &proxyJid);
  195. bool proxyOnly() const;
  196. void setProxyOnly(bool proxyOnly);
  197. QXmppTransferJob::Methods supportedMethods() const;
  198. void setSupportedMethods(QXmppTransferJob::Methods methods);
  199. /// \cond
  200. QStringList discoveryFeatures() const;
  201. bool handleStanza(const QDomElement &element);
  202. /// \endcond
  203. signals:
  204. /// This signal is emitted when a new file transfer offer is received.
  205. ///
  206. /// To accept the transfer job, call the job's QXmppTransferJob::accept() method.
  207. /// To refuse the transfer job, call the job's QXmppTransferJob::abort() method.
  208. void fileReceived(QXmppTransferJob *job);
  209. /// This signal is emitted whenever a transfer job is started.
  210. void jobStarted(QXmppTransferJob *job);
  211. /// This signal is emitted whenever a transfer job is finished.
  212. ///
  213. /// \sa QXmppTransferJob::finished()
  214. void jobFinished(QXmppTransferJob *job);
  215. public slots:
  216. QXmppTransferJob *sendFile(const QString &jid, const QString &filePath, const QString &description = QString());
  217. QXmppTransferJob *sendFile(const QString &jid, QIODevice *device, const QXmppTransferFileInfo &fileInfo, const QString &sid = QString());
  218. protected:
  219. /// \cond
  220. void setClient(QXmppClient* client);
  221. /// \endcond
  222. private slots:
  223. void _q_iqReceived(const QXmppIq&);
  224. void _q_jobDestroyed(QObject *object);
  225. void _q_jobError(QXmppTransferJob::Error error);
  226. void _q_jobFinished();
  227. void _q_jobStateChanged(QXmppTransferJob::State state);
  228. void _q_socksServerConnected(QTcpSocket *socket, const QString &hostName, quint16 port);
  229. private:
  230. QXmppTransferManagerPrivate *d;
  231. void byteStreamIqReceived(const QXmppByteStreamIq&);
  232. void byteStreamResponseReceived(const QXmppIq&);
  233. void byteStreamResultReceived(const QXmppByteStreamIq&);
  234. void byteStreamSetReceived(const QXmppByteStreamIq&);
  235. void ibbCloseIqReceived(const QXmppIbbCloseIq&);
  236. void ibbDataIqReceived(const QXmppIbbDataIq&);
  237. void ibbOpenIqReceived(const QXmppIbbOpenIq&);
  238. void ibbResponseReceived(const QXmppIq&);
  239. void streamInitiationIqReceived(const QXmppStreamInitiationIq&);
  240. void streamInitiationResultReceived(const QXmppStreamInitiationIq&);
  241. void streamInitiationSetReceived(const QXmppStreamInitiationIq&);
  242. void socksServerSendOffer(QXmppTransferJob *job);
  243. friend class QXmppTransferManagerPrivate;
  244. };
  245. Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppTransferJob::Methods)
  246. #endif