QXmpp  Version:1.0.0
QXmppTransferManager.h
1 /*
2  * Copyright (C) 2008-2019 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 <QDateTime>
28 #include <QSharedData>
29 #include <QUrl>
30 #include <QVariant>
31 
32 #include "QXmppClientExtension.h"
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_ENUMS(Direction Error State)
90  Q_FLAGS(Method Methods)
91  Q_PROPERTY(Direction direction READ direction CONSTANT)
92  Q_PROPERTY(QUrl localFileUrl READ localFileUrl WRITE setLocalFileUrl NOTIFY localFileUrlChanged)
93  Q_PROPERTY(QString jid READ jid CONSTANT)
94  Q_PROPERTY(Method method READ method CONSTANT)
95  Q_PROPERTY(State state READ state NOTIFY stateChanged)
96 
97  Q_PROPERTY(QString fileName READ fileName CONSTANT)
98  Q_PROPERTY(qint64 fileSize READ fileSize CONSTANT)
99 
100 public:
103  {
105  OutgoingDirection
106  };
107 
109  enum Error
110  {
111  NoError = 0,
115  ProtocolError
116  };
117 
119  enum Method
120  {
121  NoMethod = 0,
122  InBandMethod = 1,
123  SocksMethod = 2,
124  AnyMethod = 3
125  };
126  Q_DECLARE_FLAGS(Methods, Method)
127 
128 
129  enum State
130  {
131  OfferState = 0,
132  StartState = 1,
133  TransferState = 2,
134  FinishedState = 3
135  };
136 
137  ~QXmppTransferJob();
138 
139  QXmppTransferJob::Direction direction() const;
140  QXmppTransferJob::Error error() const;
141  QString jid() const;
142  QXmppTransferJob::Method method() const;
143  QString sid() const;
144  qint64 speed() const;
145  QXmppTransferJob::State state() const;
146 
147  // XEP-0096 : File transfer
148  QXmppTransferFileInfo fileInfo() const;
149  QUrl localFileUrl() const;
150  void setLocalFileUrl(const QUrl &localFileUrl);
151 
153  QDateTime fileDate() const;
154  QByteArray fileHash() const;
155  QString fileName() const;
156  qint64 fileSize() const;
158 
159 signals:
162  void error(QXmppTransferJob::Error error);
163 
171  void finished();
172 
174  void localFileUrlChanged(const QUrl &localFileUrl);
175 
177  void progress(qint64 done, qint64 total);
178 
180  void stateChanged(QXmppTransferJob::State state);
181 
182 public slots:
183  void abort();
184  void accept(const QString &filePath);
185  void accept(QIODevice *output);
186 
187 private slots:
188  void _q_terminated();
189 
190 private:
191  QXmppTransferJob(const QString &jid, QXmppTransferJob::Direction direction, QXmppClient *client, QObject *parent);
192  void setState(QXmppTransferJob::State state);
193  void terminate(QXmppTransferJob::Error error);
194 
195  QXmppTransferJobPrivate *const d;
196  friend class QXmppTransferManager;
197  friend class QXmppTransferManagerPrivate;
198  friend class QXmppTransferIncomingJob;
199  friend class QXmppTransferOutgoingJob;
200 };
201 
218 
219 class QXMPP_EXPORT QXmppTransferManager : public QXmppClientExtension
220 {
221  Q_OBJECT
222  Q_PROPERTY(QString proxy READ proxy WRITE setProxy)
223  Q_PROPERTY(bool proxyOnly READ proxyOnly WRITE setProxyOnly)
224  Q_PROPERTY(QXmppTransferJob::Methods supportedMethods READ supportedMethods WRITE setSupportedMethods)
225 
226 public:
228  ~QXmppTransferManager();
229 
230  QString proxy() const;
231  void setProxy(const QString &proxyJid);
232 
233  bool proxyOnly() const;
234  void setProxyOnly(bool proxyOnly);
235 
236  QXmppTransferJob::Methods supportedMethods() const;
237  void setSupportedMethods(QXmppTransferJob::Methods methods);
238 
240  QStringList discoveryFeatures() const;
241  bool handleStanza(const QDomElement &element);
243 
244 signals:
249  void fileReceived(QXmppTransferJob *job);
250 
252  void jobStarted(QXmppTransferJob *job);
253 
257  void jobFinished(QXmppTransferJob *job);
258 
259 public slots:
260  QXmppTransferJob *sendFile(const QString &jid, const QString &filePath, const QString &description = QString());
261  QXmppTransferJob *sendFile(const QString &jid, QIODevice *device, const QXmppTransferFileInfo &fileInfo, const QString &sid = QString());
262 
263 protected:
265  void setClient(QXmppClient* client);
267 
268 private slots:
269  void _q_iqReceived(const QXmppIq&);
270  void _q_jobDestroyed(QObject *object);
271  void _q_jobError(QXmppTransferJob::Error error);
272  void _q_jobFinished();
273  void _q_jobStateChanged(QXmppTransferJob::State state);
274  void _q_socksServerConnected(QTcpSocket *socket, const QString &hostName, quint16 port);
275 
276 private:
277  QXmppTransferManagerPrivate *d;
278 
279  void byteStreamIqReceived(const QXmppByteStreamIq&);
280  void byteStreamResponseReceived(const QXmppIq&);
281  void byteStreamResultReceived(const QXmppByteStreamIq&);
282  void byteStreamSetReceived(const QXmppByteStreamIq&);
283  void ibbCloseIqReceived(const QXmppIbbCloseIq&);
284  void ibbDataIqReceived(const QXmppIbbDataIq&);
285  void ibbOpenIqReceived(const QXmppIbbOpenIq&);
286  void ibbResponseReceived(const QXmppIq&);
287  void streamInitiationIqReceived(const QXmppStreamInitiationIq&);
288  void streamInitiationResultReceived(const QXmppStreamInitiationIq&);
289  void streamInitiationSetReceived(const QXmppStreamInitiationIq&);
290  void socksServerSendOffer(QXmppTransferJob *job);
291 
292  friend class QXmppTransferManagerPrivate;
293 };
294 
295 Q_DECLARE_OPERATORS_FOR_FLAGS(QXmppTransferJob::Methods)
296 
297 #endif
Error
This enum is used to describe the type of error encountered by a transfer job.
Definition: QXmppTransferManager.h:109
The file transfer was aborted.
Definition: QXmppTransferManager.h:112
The QXmppTransferJob class represents a single file transfer job.
Definition: QXmppTransferManager.h:86
The QXmppLoggable class represents a source of logging messages.
Definition: QXmppLogger.h:111
An error was encountered trying to access a local file.
Definition: QXmppTransferManager.h:113
The QXmppTransferManager class provides support for sending and receiving files.
Definition: QXmppTransferManager.h:219
State
This enum is used to describe the state of a transfer job.
Definition: QXmppTransferManager.h:129
The QXmppIq class is the base class for all IQs.
Definition: QXmppIq.h:42
The file is corrupt: the file size or hash do not match.
Definition: QXmppTransferManager.h:114
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:47
The file is being received.
Definition: QXmppTransferManager.h:104
Method
This enum is used to describe a transfer method.
Definition: QXmppTransferManager.h:119
The QXmppClient class is the main class for using QXmpp.
Definition: QXmppClient.h:80
Direction
This enum is used to describe the direction of a transfer job.
Definition: QXmppTransferManager.h:102