BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
simpleDownloader.h
Go to the documentation of this file.
1 #ifndef BALL_SYSTEM_SIMPLEDOWNLOADER_H
2 #define BALL_SYSTEM_SIMPLEDOWNLOADER_H
3 
4 #ifndef BALL_DATATYPE_STRING_H
5  #include <BALL/DATATYPE/string.h>
6 #endif
7 
8 #include <QtCore/QThread>
9 #include <QtCore/QFile>
10 #include <QtNetwork/QFtp>
11 
12 #include <QtNetwork/QNetworkReply>
13 
14 class QByteArray;
15 
16 namespace BALL
17 {
18  namespace SimpleDownloaderHelper
19  {
20  class HelperThread;
21  }
22 
34  : public QObject
35  {
36  Q_OBJECT
37 
38  public:
39  /*
40  * Default Constructor.
41  *
42  * @param url The URL to download.
43  * @param timeout The maximum number of milliseconds the download is allowed to take.
44  * default: infinite
45  */
46  SimpleDownloader(const String& url, unsigned int timeout = UINT_MAX);
47 
48  /*
49  * Default Constructor.
50  *
51  * @param url The URL to download.
52  * @param timeout The maximum number of milliseconds the download is allowed to take.
53  * default: infinite
54  */
55  SimpleDownloader(const QUrl& url, unsigned int timeout = UINT_MAX);
56 
64  int downloadToBuffer(std::vector<char>& buffer);
65 
72  int downloadToFile(const String& path);
73 
82  int uploadStringToBuffer(const String& data, std::vector<char>& response);
83 
92  int uploadStringToFile(const String& data, const String& response);
93 
102  int uploadFileToBuffer(const String& path, std::vector<char>& response);
103 
112  int uploadFileToFile(const String& path, const String& response);
113 
120  void setTimeout(unsigned int timeout);
121 
127  void setURL(const String& url);
128 
134  void setURL(const QUrl& url);
135 
141  const QUrl& getURL() const;
142 
143  private:
144  int download_(SimpleDownloaderHelper::HelperThread& thread);
145  int qftpDownloadHack_(QIODevice* iodev);
146 
147  QUrl url_;
148  unsigned int timeout_;
149  };
150 
151  namespace SimpleDownloaderHelper
152  {
153  class HelperThread : public QThread
154  {
155  public:
156  HelperThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent);
157  HelperThread(const QUrl& url, const String& path, SimpleDownloader* parent);
158 
159  int getStatus();
160 
161  protected:
162  virtual QNetworkReply* getReply_(QNetworkAccessManager* man) = 0;
163 
164  void run();
165 
166  int err_;
167  QUrl url_;
168  QByteArray* result_;
171  };
172 
173  class DLThread : public HelperThread
174  {
175  public:
176  DLThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent);
177  DLThread(const QUrl& url, const String& path, SimpleDownloader* parent);
178 
179  protected:
180  virtual QNetworkReply* getReply_(QNetworkAccessManager* man);
181  };
182 
183  class UPThread : public HelperThread
184  {
185  public:
186  UPThread(const QUrl& url, const QByteArray* data, QByteArray* result, SimpleDownloader* parent);
187  UPThread(const QUrl& url, const QByteArray* data, const String& path, SimpleDownloader* parent);
188  UPThread(const QUrl& url, QIODevice* file, QByteArray* result, SimpleDownloader* parent);
189  UPThread(const QUrl& url, QIODevice* file, const String& path, SimpleDownloader* parent);
190 
191  protected:
192  virtual QNetworkReply* getReply_(QNetworkAccessManager* man);
193 
194  const QByteArray* data_;
195  QIODevice* file_;
196  };
197 
198  class BasicHelper : public QObject
199  {
200  Q_OBJECT
201 
202  public:
203  BasicHelper(HelperThread* caller, QNetworkReply* reply);
204  virtual ~BasicHelper(){}
205 
206  public slots:
207  void error(QNetworkReply::NetworkError error);
208 #ifndef QT_NO_OPENSSL
209  void sslErrors(const QList<QSslError>& errors);
210 #endif
211  virtual void finished() = 0;
212 
213  protected:
215  QNetworkReply* reply_;
216  };
217 
218  class DLArrayHelper : public BasicHelper
219  {
220  Q_OBJECT
221 
222  public:
223  DLArrayHelper(HelperThread* caller, QNetworkReply* reply, QByteArray* result);
224 
225  public slots:
226  void finished();
227 
228  private:
229  QByteArray* result_;
230  };
231 
232  class DLHelper : public BasicHelper
233  {
234  Q_OBJECT
235 
236  public:
237  DLHelper(HelperThread* caller, QNetworkReply* reply, const String& path);
238 
239  public slots:
240  void finished();
241  void receivedData();
242 
243  private:
244  QFile file_;
245  };
246 
247 
248  // We need this due to a bug in QNetworkAccessManager that basically
249  // screws up the FTP download code. This should be fixed upstream with
250  // the advent of Qt5
251 
252  class QFtpHackHelper;
253 
254  class QFtpHackThread : public QThread
255  {
256  Q_OBJECT
257 
258  public:
259  QFtpHackThread(const QUrl& url, QIODevice* iodev, SimpleDownloader* parent);
260  ~QFtpHackThread();
261 
262  protected:
263  void run();
264 
265  private:
266  QFtp* ftp_;
268  QUrl url_;
269  QIODevice* iodev_;
271  };
272 
273  class QFtpHackHelper : public QObject
274  {
275  Q_OBJECT
276 
277  public:
279 
280  public slots:
281  void done(bool error);
282 
283  private:
285  };
286 
287  }
288 }
289 
290 #endif //BALL_SYSTEM_SIMPLEDOWNLOADER_H