QXmpp  Version:1.3.0
QXmppTransferManager.h
1 /*
2  * Copyright (C) 2008-2020 The QXmpp developers
3  *
4  * Author:
5  * Jeremy LainĂ©
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 QXMPPTRANSFERMANAGER_H
25 #define QXMPPTRANSFERMANAGER_H
26 
27 #include "QXmppClientExtension.h"
28 
29 #include <QDateTime>
30 #include <QSharedData>
31 #include <QUrl>
32 #include <QVariant>
33 
34 class QTcpSocket;
35 class QXmppByteStreamIq;
36 class QXmppIbbCloseIq;
37 class QXmppIbbDataIq;
38 class QXmppIbbOpenIq;
39 class QXmppIq;
40 class QXmppStreamInitiationIq;
41 class QXmppTransferFileInfoPrivate;
42 class QXmppTransferJobPrivate;
44 class QXmppTransferManagerPrivate;
45 
46 class QXMPP_EXPORT QXmppTransferFileInfo
47 {
48 public:
49  QXmppTransferFileInfo();
50  QXmppTransferFileInfo(const QXmppTransferFileInfo &other);
51  ~QXmppTransferFileInfo();
52 
53  QDateTime date() const;
54  void setDate(const QDateTime &date);
55 
56  QByteArray hash() const;
57  void setHash(const QByteArray &hash);
58 
59  QString name() const;
60  void setName(const QString &name);
61 
62  QString description() const;
63  void setDescription(const QString &description);
64 
65  qint64 size() const;
66  void setSize(qint64 size);
67 
68  bool isNull() const;
69  QXmppTransferFileInfo &operator=(const QXmppTransferFileInfo &other);
70  bool operator==(const QXmppTransferFileInfo &other) const;
71 
73  void parse(const QDomElement &element);
74  void toXml(QXmlStreamWriter *writer) const;
76 
77 private:
78  QSharedDataPointer<QXmppTransferFileInfoPrivate> d;
79 };
80 
85 
86 class QXMPP_EXPORT QXmppTransferJob : public QXmppLoggable
87 {
88  Q_OBJECT
89  Q_FLAGS(Method Methods)
91  Q_PROPERTY(Direction direction READ direction CONSTANT)
93  Q_PROPERTY(QUrl localFileUrl READ localFileUrl WRITE setLocalFileUrl NOTIFY localFileUrlChanged)
95  Q_PROPERTY(QString jid READ jid CONSTANT)
97  Q_PROPERTY(Method method READ method CONSTANT)
99  Q_PROPERTY(State state READ state NOTIFY stateChanged)
100 
102  Q_PROPERTY(QString fileName READ fileName CONSTANT)
104  Q_PROPERTY(qint64 fileSize READ fileSize CONSTANT)
105 
106 public:
108  enum Direction {
110  OutgoingDirection
111  };
112  Q_ENUM(Direction)
113 
114 
115  enum Error {
116  NoError = 0,
120  ProtocolError
121  };
122  Q_ENUM(Error)
123 
124 
125  enum Method {
126  NoMethod = 0,
127  InBandMethod = 1,
128  SocksMethod = 2,
129  AnyMethod = 3
130  };
131  Q_ENUM(Method)
132  Q_DECLARE_FLAGS(Methods, Method)
133 
134 
135  enum State {
136  OfferState = 0,
137  StartState = 1,
138  TransferState = 2,
139  FinishedState = 3
140  };
141  Q_ENUM(State)
142 
143  ~QXmppTransferJob() override;
144 
145  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
147  QXmppTransferJob::Direction direction() const;
149  QString jid() const;
151  QXmppTransferJob::Method method() const;
153  QXmppTransferJob::State state() const;
154 
155  QXmppTransferJob::Error error() const;
156  QString sid() const;
157  qint64 speed() const;
158 
159  // XEP-0096 : File transfer
160  QXmppTransferFileInfo fileInfo() const;
161  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
163  QUrl localFileUrl() const;
164  void setLocalFileUrl(const QUrl &localFileUrl);
165 
167  QDateTime fileDate() const;
168  QByteArray fileHash() const;
169  QString fileName() const;
170  qint64 fileSize() const;
172 
173 Q_SIGNALS:
176  void error(QXmppTransferJob::Error error);
177 
185  void finished();
186 
188  void localFileUrlChanged(const QUrl &localFileUrl);
189 
191  void progress(qint64 done, qint64 total);
192 
194  void stateChanged(QXmppTransferJob::State state);
195 
196 public Q_SLOTS:
197  void abort();
198  void accept(const QString &filePath);
199  void accept(QIODevice *output);
200 
201 private Q_SLOTS:
202  void _q_terminated();
203 
204 private:
205  QXmppTransferJob(const QString &jid, QXmppTransferJob::Direction direction, QXmppClient *client, QObject *parent);
206  void setState(QXmppTransferJob::State state);
207  void terminate(QXmppTransferJob::Error error);
208 
209  QXmppTransferJobPrivate *const d;
210  friend class QXmppTransferManager;
211  friend class QXmppTransferManagerPrivate;
212  friend class QXmppTransferIncomingJob;
213  friend class QXmppTransferOutgoingJob;
214 };
215 
234 class QXMPP_EXPORT QXmppTransferManager : public QXmppClientExtension
235 {
236  Q_OBJECT
237 
239  Q_PROPERTY(QString proxy READ proxy WRITE setProxy)
241  Q_PROPERTY(bool proxyOnly READ proxyOnly WRITE setProxyOnly)
243  Q_PROPERTY(QXmppTransferJob::Methods supportedMethods READ supportedMethods WRITE setSupportedMethods)
244 
245 public:
247  ~QXmppTransferManager() override;
248 
249  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
251  QString proxy() const;
252  void setProxy(const QString &proxyJid);
253 
254  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
257  bool proxyOnly() const;
258  void setProxyOnly(bool proxyOnly);
259 
260  // documentation needs to be here, see https://stackoverflow.com/questions/49192523/
264  QXmppTransferJob::Methods supportedMethods() const;
265  void setSupportedMethods(QXmppTransferJob::Methods methods);
266 
268  QStringList discoveryFeatures() const override;
269  bool handleStanza(const QDomElement &element) override;
271 
272 Q_SIGNALS:
277  void fileReceived(QXmppTransferJob *job);
278 
280  void jobStarted(QXmppTransferJob *job);
281 
285  void jobFinished(QXmppTransferJob *job);
286 
287 public Q_SLOTS:
288  QXmppTransferJob *sendFile(const QString &jid, const QString &filePath, const QString &description = QString());
289  QXmppTransferJob *sendFile(const QString &jid, QIODevice *device, const QXmppTransferFileInfo &fileInfo, const QString &sid = QString());
290 
291 protected:
293  void setClient(QXmppClient *client) override;
295 
296 private Q_SLOTS:
297  void _q_iqReceived(const QXmppIq &);
298  void _q_jobDestroyed(QObject *object);
299  void _q_jobError(QXmppTransferJob::Error error);
300  void _q_jobFinished();
301  void _q_jobStateChanged(QXmppTransferJob::State state);
302  void _q_socksServerConnected(QTcpSocket *socket, const QString &hostName, quint16 port);
303 
304 private:
305  QXmppTransferManagerPrivate *d;
306 
307  void byteStreamIqReceived(const QXmppByteStreamIq &);
308  void byteStreamResponseReceived(const QXmppIq &);
309  void byteStreamResultReceived(const QXmppByteStreamIq &);
310  void byteStreamSetReceived(const QXmppByteStreamIq &);
311  void ibbCloseIqReceived(const QXmppIbbCloseIq &);
312  void ibbDataIqReceived(const QXmppIbbDataIq &);
313  void ibbOpenIqReceived(const QXmppIbbOpenIq &);
314  void ibbResponseReceived(const QXmppIq &);
315  void streamInitiationIqReceived(const QXmppStreamInitiationIq &);
316  void streamInitiationResultReceived(const QXmppStreamInitiationIq &);
317  void streamInitiationSetReceived(const QXmppStreamInitiationIq &);
318  void socksServerSendOffer(QXmppTransferJob *job);
319 
320  friend class QXmppTransferManagerPrivate;
321 };
322 
323 Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppTransferJob::Methods)
324 
325 #endif
QXmppTransferJob::Method
Method
This enum is used to describe a transfer method.
Definition: QXmppTransferManager.h:125
QXmppIbbDataIq
QXmppIbbCloseIq represents an IBB data request as defined by XEP-0047: In-Band Bytestreams.
Definition: QXmppIbbIq.h:92
QXmppTransferJob::FileAccessError
An error was encountered trying to access a local file.
Definition: QXmppTransferManager.h:118
QXmppTransferJob::FileCorruptError
The file is corrupt: the file size or hash do not match.
Definition: QXmppTransferManager.h:119
QXmppClientExtension
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:47
QXmppTransferJob
The QXmppTransferJob class represents a single file transfer job.
Definition: QXmppTransferManager.h:86
QXmppTransferManager
The QXmppTransferManager class provides support for sending and receiving files.
Definition: QXmppTransferManager.h:234
QXmppTransferJob::State
State
This enum is used to describe the state of a transfer job.
Definition: QXmppTransferManager.h:135
QXmppTransferJob::Direction
Direction
This enum is used to describe the direction of a transfer job.
Definition: QXmppTransferManager.h:108
QXmppIbbCloseIq
QXmppIbbCloseIq represents an IBB close request as defined by XEP-0047: In-Band Bytestreams.
Definition: QXmppIbbIq.h:66
QXmppByteStreamIq
QXmppByteStreamIq represents a SOCKS5 bytestreams negoatiation IQ as defined by XEP-0065: SOCKS5 Byte...
Definition: QXmppByteStreamIq.h:35
QXmppIq
The QXmppIq class is the base class for all IQs.
Definition: QXmppIq.h:41
QXmppTransferJob::Error
Error
This enum is used to describe the type of error encountered by a transfer job.
Definition: QXmppTransferManager.h:115
QXmppIbbOpenIq
QXmppIbbOpenIq represents an IBB open request as defined by XEP-0047: In-Band Bytestreams.
Definition: QXmppIbbIq.h:36
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
QXmppTransferJob::AbortError
The file transfer was aborted.
Definition: QXmppTransferManager.h:117
QXmppTransferJob::IncomingDirection
The file is being received.
Definition: QXmppTransferManager.h:109