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
BALL::SimpleDownloader::uploadStringToFile
int uploadStringToFile(const String &data, const String &response)
BALL_EXPORT
#define BALL_EXPORT
Definition: COMMON/global.h:50
BALL::SimpleDownloaderHelper::HelperThread::path_
String path_
Definition: simpleDownloader.h:167
BALL::SimpleDownloaderHelper::UPThread::UPThread
UPThread(const QUrl &url, const QByteArray *data, const String &path, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::HelperThread::parent_
SimpleDownloader * parent_
Definition: simpleDownloader.h:168
BALL::SimpleDownloaderHelper::BasicHelper::sslErrors
void sslErrors(const QList< QSslError > &errors)
BALL::SimpleDownloader::getURL
const QUrl & getURL() const
BALL::SimpleDownloaderHelper::DLHelper
Definition: simpleDownloader.h:231
BALL::SimpleDownloader::downloadToFile
int downloadToFile(const String &path)
BALL::SimpleDownloaderHelper::HelperThread::url_
QUrl url_
Definition: simpleDownloader.h:165
BALL::SimpleDownloader::setTimeout
void setTimeout(unsigned int timeout)
BALL::SimpleDownloaderHelper::DLArrayHelper::finished
void finished()
BALL::SimpleDownloaderHelper::BasicHelper::caller_
HelperThread * caller_
Definition: simpleDownloader.h:212
BALL::SimpleDownloaderHelper::HelperThread::HelperThread
HelperThread(const QUrl &url, QByteArray *result, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::HelperThread
Definition: simpleDownloader.h:152
BALL::SimpleDownloaderHelper::BasicHelper::reply_
QNetworkReply * reply_
Definition: simpleDownloader.h:213
BALL::SimpleDownloaderHelper::DLArrayHelper
Definition: simpleDownloader.h:217
BALL::SimpleDownloader::SimpleDownloader
SimpleDownloader(const QUrl &url, unsigned int timeout=UINT_MAX)
BALL::SimpleDownloaderHelper::HelperThread::HelperThread
HelperThread(const QUrl &url, const String &path, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::UPThread
Definition: simpleDownloader.h:182
BALL::String
Definition: string.h:57
BALL::SimpleDownloader
Definition: simpleDownloader.h:34
BALL::SimpleDownloaderHelper::DLThread::DLThread
DLThread(const QUrl &url, QByteArray *result, SimpleDownloader *parent)
BALL::SimpleDownloader::setURL
void setURL(const QUrl &url)
BALL
Definition: constants.h:13
BALL::SimpleDownloaderHelper::HelperThread::result_
QByteArray * result_
Definition: simpleDownloader.h:166
BALL::SimpleDownloaderHelper::DLHelper::finished
void finished()
BALL::SimpleDownloader::uploadStringToBuffer
int uploadStringToBuffer(const String &data, std::vector< char > &response)
BALL::SimpleDownloader::uploadFileToFile
int uploadFileToFile(const String &path, const String &response)
BALL::SimpleDownloader::uploadFileToBuffer
int uploadFileToBuffer(const String &path, std::vector< char > &response)
BALL::SimpleDownloaderHelper::UPThread::UPThread
UPThread(const QUrl &url, QIODevice *file, QByteArray *result, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::HelperThread::run
void run()
BALL::SimpleDownloader::setURL
void setURL(const String &url)
BALL::SimpleDownloaderHelper::UPThread::UPThread
UPThread(const QUrl &url, const QByteArray *data, QByteArray *result, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::DLHelper::receivedData
void receivedData()
BALL::SimpleDownloaderHelper::UPThread::file_
QIODevice * file_
Definition: simpleDownloader.h:193
string.h
BALL::SimpleDownloaderHelper::DLThread::getReply_
virtual QNetworkReply * getReply_(QNetworkAccessManager *man)
BALL::SimpleDownloaderHelper::DLArrayHelper::DLArrayHelper
DLArrayHelper(HelperThread *caller, QNetworkReply *reply, QByteArray *result)
BALL::SimpleDownloaderHelper::BasicHelper::error
void error(QNetworkReply::NetworkError error)
BALL::SimpleDownloaderHelper::UPThread::UPThread
UPThread(const QUrl &url, QIODevice *file, const String &path, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::BasicHelper::finished
virtual void finished()=0
BALL::SimpleDownloaderHelper::UPThread::data_
const QByteArray * data_
Definition: simpleDownloader.h:192
BALL::SimpleDownloaderHelper::DLHelper::DLHelper
DLHelper(HelperThread *caller, QNetworkReply *reply, const String &path)
QThread
BALL::SimpleDownloaderHelper::BasicHelper::~BasicHelper
virtual ~BasicHelper()
Definition: simpleDownloader.h:202
BALL::SimpleDownloaderHelper::UPThread::getReply_
virtual QNetworkReply * getReply_(QNetworkAccessManager *man)
BALL::SimpleDownloaderHelper::DLThread::DLThread
DLThread(const QUrl &url, const String &path, SimpleDownloader *parent)
BALL::SimpleDownloaderHelper::HelperThread::getStatus
int getStatus()
BALL::SimpleDownloaderHelper::BasicHelper
Definition: simpleDownloader.h:197
BALL::SimpleDownloaderHelper::HelperThread::getReply_
virtual QNetworkReply * getReply_(QNetworkAccessManager *man)=0
BALL::SimpleDownloader::SimpleDownloader
SimpleDownloader(const String &url, unsigned int timeout=UINT_MAX)
BALL::SimpleDownloaderHelper::DLThread
Definition: simpleDownloader.h:172
QObject
BALL::SimpleDownloaderHelper::HelperThread::err_
int err_
Definition: simpleDownloader.h:164
BALL::SimpleDownloader::downloadToBuffer
int downloadToBuffer(std::vector< char > &buffer)
BALL::SimpleDownloaderHelper::BasicHelper::BasicHelper
BasicHelper(HelperThread *caller, QNetworkReply *reply)