libqutim  0.3.1.0
filetransfer.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** qutIM - instant messenger
4 **
5 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
6 **
7 *****************************************************************************
8 **
9 ** $QUTIM_BEGIN_LICENSE$
10 ** This program is free software: you can redistribute it and/or modify
11 ** it under the terms of the GNU General Public License as published by
12 ** the Free Software Foundation, either version 3 of the License, or
13 ** (at your option) any later version.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 ** See the GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program. If not, see http://www.gnu.org/licenses/.
22 ** $QUTIM_END_LICENSE$
23 **
24 ****************************************************************************/
25 
26 #ifndef FILETRANSFER_H
27 #define FILETRANSFER_H
28 
29 #include "libqutim_global.h"
30 #include <QHostAddress>
31 #include <QStringList>
32 #include <QSharedData>
33 
34 class QDir;
35 class QUrl;
36 class QIcon;
37 
38 namespace qutim_sdk_0_3
39 {
40 
41 class ChatUnit;
42 class LocalizedString;
43 class FileTransferFactory;
44 class FileTransferManager;
45 class FileTransferManagerPrivate;
46 class FileTransferJobPrivate;
47 class FileTransferInfoPrivate;
48 class FileTransferObserverPrivate;
49 class FileTransferFactoryPrivate;
50 class FileTransferManagerPrivate;
51 
53 {
54 public:
56  FileTransferInfo(const FileTransferInfo &other);
58  FileTransferInfo &operator =(const FileTransferInfo &other);
59 
60  // Relative file name
61  QString fileName() const;
62  void setFileName(const QString &fileName);
63  qint64 fileSize() const;
64  void setFileSize(qint64 fileSize);
65 private:
66  QSharedDataPointer<FileTransferInfoPrivate> d_ptr;
67 };
68 
69 class LIBQUTIM_EXPORT FileTransferJob : public QObject
70 {
71  Q_OBJECT
72  Q_DECLARE_PRIVATE(FileTransferJob)
73  Q_ENUMS(Direction ErrorType State)
74  Q_PROPERTY(QString title READ title NOTIFY titleChanged)
75  Q_PROPERTY(QString fileName READ fileName NOTIFY fileNameChanged)
76  Q_PROPERTY(int filesCount READ filesCount)
77  Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
78  Q_PROPERTY(qint64 totalSize READ totalSize NOTIFY totalSizeChanged)
79  Q_PROPERTY(qint64 fileSize READ fileSize NOTIFY fileSizeChanged)
80  Q_PROPERTY(qint64 progress READ progress NOTIFY progressChanged)
81  Q_PROPERTY(qutim_sdk_0_3::FileTransferJob::State state READ state NOTIFY stateChanged)
82  Q_PROPERTY(qutim_sdk_0_3::ChatUnit *chatUnit READ chatUnit)
83 public:
84  enum Direction { Outgoing, Incoming };
85  enum ErrorType { NetworkError, Canceled, NotSupported, IOError, NoError };
86  enum State { Initiation, Started, Finished, Error };
87 
88  FileTransferJob(ChatUnit *unit, Direction direction, FileTransferFactory *factory);
89  virtual ~FileTransferJob();
90 
91  // Send some file or dir
92  void send(const QUrl &url, const QString &title = QString());
93  void send(const QDir &baseDir, const QStringList &files, const QString &title);
94 
95  Direction direction() const;
96  QString title() const;
97  QString fileName() const;
98  // For outgoing only
99  QDir baseDir() const;
100  FileTransferInfo info(int index) const;
101  int filesCount() const;
102  int currentIndex() const;
103  qint64 fileSize() const;
104  qint64 progress() const;
105  qint64 totalSize() const;
106  State state() const;
107  LocalizedString stateString();
108  ErrorType error() const;
109  LocalizedString errorString();
110  ChatUnit *chatUnit() const;
111  bool isAccepted();
112 public slots:
113  void stop();
114  // Accept incoming job
115  void accept();
116 protected:
117  virtual void doSend() = 0;
118  virtual void doStop() = 0;
119  virtual void doReceive() = 0;
120  // For incoming only
121  void init(int filesCount, qint64 totalSize, const QString &title);
122  // Device for local files to read/write
123  QIODevice *setCurrentIndex(int index);
124  void setFileProgress(qint64 fileProgress);
125  void setError(ErrorType error);
126  void setErrorString(const LocalizedString &error);
127  void setState(State state);
128  void setStateString(const LocalizedString &state);
129  void setFileInfo(int index, const FileTransferInfo &info);
130  virtual void virtual_hook(int id, void *data);
131 signals:
132 #if !defined(Q_MOC_RUN) && !defined(DOXYGEN_SHOULD_SKIP_THIS) && !defined(IN_IDE_PARSER)
133 private: // don't tell moc, doxygen or kdevelop, but those signals are in fact private
134 #endif
135  void titleChanged(const QString &);
136  void fileNameChanged(const QString &);
137  void fileSizeChanged(qint64);
138  void progressChanged(qint64);
139  void totalSizeChanged(qint64);
140  void currentIndexChanged(int);
142  void errorStringChanged(const qutim_sdk_0_3::LocalizedString &);
143  void stateChanged(qutim_sdk_0_3::FileTransferJob::State);
144  void stateStringChanged(const qutim_sdk_0_3::LocalizedString &);
145  void finished();
146  void accepted();
147 private:
148  friend class FileTransferManager;
149  QScopedPointer<FileTransferJobPrivate> d_ptr;
150 };
151 
152 class LIBQUTIM_EXPORT FileTransferObserver : public QObject
153 {
154  Q_OBJECT
155  Q_DECLARE_PRIVATE(FileTransferObserver)
156  Q_PROPERTY(bool ability READ checkAbility NOTIFY abilityChanged)
157  Q_PROPERTY(ChatUnit *chatUnit READ chatUnit)
158 public:
161  bool checkAbility() const;
162  ChatUnit *chatUnit() const;
163 signals:
164  void abilityChanged(bool);
165 protected:
166  Q_PRIVATE_SLOT(d_func(), void _q_clearObserverData(QObject *unit));
167  QScopedPointer<FileTransferObserverPrivate> d_ptr;
168 };
169 
170 class LIBQUTIM_EXPORT FileTransferFactory : public QObject
171 {
172  Q_OBJECT
173  Q_DECLARE_PRIVATE(FileTransferFactory)
174  Q_ENUMS(Capability)
175 public:
176  enum Capability {
177  CanSendMultiple = 0x01
178  };
179  Q_DECLARE_FLAGS(Capabilities, Capability)
180  FileTransferFactory(const LocalizedString &name, Capabilities capabilities);
182  Capabilities capabilities() const;
183  LocalizedString name() const;
184  LocalizedString description() const;
185  QIcon icon() const;
186  virtual bool checkAbility(ChatUnit *unit) = 0;
187  virtual bool startObserve(ChatUnit *unit) = 0;
188  virtual bool stopObserve(ChatUnit *unit) = 0;
189  virtual FileTransferJob *create(ChatUnit *unit) = 0;
190 protected:
191  void changeAvailability(ChatUnit *unit, bool canSend);
192  void setDescription(const LocalizedString &description);
193  void setIcon(const QIcon &icon);
194  virtual void virtual_hook(int id, void *data);
195  QScopedPointer<FileTransferFactoryPrivate> d_ptr;
196 };
197 
198 class LIBQUTIM_EXPORT FileTransferManager : public QObject
199 {
200  Q_OBJECT
201  Q_DECLARE_PRIVATE(FileTransferManager)
202  Q_CLASSINFO("Service", "FileTransferManager")
203 public:
206 
207  static bool checkAbility(ChatUnit *unit);
208  // If the factory is null, the file/directory will be sent through
209  // the most appropriate factory
210  static FileTransferJob *send(ChatUnit *unit, const QUrl &url,
211  const QString &title = QString(),
212  FileTransferFactory *factory = 0);
213  static QIODevice *openFile(FileTransferJob *job);
214  static QList<FileTransferFactory*> factories();
215  // This method must be called by the file transfer settings layer
216  // whenever the user has changed an order of the file transfer
217  // factories.
218  // TODO: come up with a more appropriate name
219  static void updateFactories(const QStringList &factoryClassNames);
220 protected:
221  virtual QIODevice *doOpenFile(FileTransferJob *job) = 0;
222  virtual void handleJob(FileTransferJob *job, FileTransferJob *oldJob) = 0;
223  virtual void virtual_hook(int id, void *data);
224  QScopedPointer<FileTransferManagerPrivate> d_ptr;
225 };
226 
227 }
228 
229 #endif // FILETRANSFER_H
230 

Generated by Doxygen