BALL  1.5.0
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 
11 #include <QtNetwork/QNetworkReply>
12 
13 class QByteArray;
14 
15 namespace BALL
16 {
17  namespace SimpleDownloaderHelper
18  {
19  class HelperThread;
20  }
21 
33  : public QObject
34  {
35  Q_OBJECT
36 
37  public:
38  /*
39  * Default Constructor.
40  *
41  * @param url The URL to download.
42  * @param timeout The maximum number of milliseconds the download is allowed to take.
43  * default: infinite
44  */
45  SimpleDownloader(const String& url, unsigned int timeout = UINT_MAX);
46 
47  /*
48  * Default Constructor.
49  *
50  * @param url The URL to download.
51  * @param timeout The maximum number of milliseconds the download is allowed to take.
52  * default: infinite
53  */
54  SimpleDownloader(const QUrl& url, unsigned int timeout = UINT_MAX);
55 
63  int downloadToBuffer(std::vector<char>& buffer);
64 
71  int downloadToFile(const String& path);
72 
81  int uploadStringToBuffer(const String& data, std::vector<char>& response);
82 
91  int uploadStringToFile(const String& data, const String& response);
92 
101  int uploadFileToBuffer(const String& path, std::vector<char>& response);
102 
111  int uploadFileToFile(const String& path, const String& response);
112 
119  void setTimeout(unsigned int timeout);
120 
126  void setURL(const String& url);
127 
133  void setURL(const QUrl& url);
134 
140  const QUrl& getURL() const;
141 
142  private:
143  int download_(SimpleDownloaderHelper::HelperThread& thread);
144 
145  QUrl url_;
146  unsigned int timeout_;
147  };
148 
149  namespace SimpleDownloaderHelper
150  {
151  class HelperThread : public QThread
152  {
153  public:
154  HelperThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent);
155  HelperThread(const QUrl& url, const String& path, SimpleDownloader* parent);
156 
157  int getStatus();
158 
159  protected:
160  virtual QNetworkReply* getReply_(QNetworkAccessManager* man) = 0;
161 
162  void run();
163 
164  int err_;
165  QUrl url_;
166  QByteArray* result_;
169  };
170 
171  class DLThread : public HelperThread
172  {
173  public:
174  DLThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent);
175  DLThread(const QUrl& url, const String& path, SimpleDownloader* parent);
176 
177  protected:
178  virtual QNetworkReply* getReply_(QNetworkAccessManager* man);
179  };
180 
181  class UPThread : public HelperThread
182  {
183  public:
184  UPThread(const QUrl& url, const QByteArray* data, QByteArray* result, SimpleDownloader* parent);
185  UPThread(const QUrl& url, const QByteArray* data, const String& path, SimpleDownloader* parent);
186  UPThread(const QUrl& url, QIODevice* file, QByteArray* result, SimpleDownloader* parent);
187  UPThread(const QUrl& url, QIODevice* file, const String& path, SimpleDownloader* parent);
188 
189  protected:
190  virtual QNetworkReply* getReply_(QNetworkAccessManager* man);
191 
192  const QByteArray* data_;
193  QIODevice* file_;
194  };
195 
196  class BasicHelper : public QObject
197  {
198  Q_OBJECT
199 
200  public:
201  BasicHelper(HelperThread* caller, QNetworkReply* reply);
202  virtual ~BasicHelper(){}
203 
204  public Q_SLOTS:
205  void error(QNetworkReply::NetworkError error);
206 #ifndef QT_NO_SSL
207  void sslErrors(const QList<QSslError>& errors);
208 #endif
209  virtual void finished() = 0;
210 
211  protected:
213  QNetworkReply* reply_;
214  };
215 
216  class DLArrayHelper : public BasicHelper
217  {
218  Q_OBJECT
219 
220  public:
221  DLArrayHelper(HelperThread* caller, QNetworkReply* reply, QByteArray* result);
222 
223  public Q_SLOTS:
224  void finished();
225 
226  private:
227  QByteArray* result_;
228  };
229 
230  class DLHelper : public BasicHelper
231  {
232  Q_OBJECT
233 
234  public:
235  DLHelper(HelperThread* caller, QNetworkReply* reply, const String& path);
236 
237  public Q_SLOTS:
238  void finished();
239  void receivedData();
240 
241  private:
242  QFile file_;
243  };
244 
245  }
246 }
247 
248 #endif //BALL_SYSTEM_SIMPLEDOWNLOADER_H
#define BALL_EXPORT
Definition: COMMON/global.h:50
Definition: constants.h:12