00001 #ifndef BALL_SYSTEM_SIMPLEDOWNLOADER_H
00002 #define BALL_SYSTEM_SIMPLEDOWNLOADER_H
00003
00004 #ifndef BALL_DATATYPE_STRING_H
00005 #include <BALL/DATATYPE/string.h>
00006 #endif
00007
00008 #include <QtCore/QThread>
00009 #include <QtCore/QFile>
00010 #include <QtNetwork/QFtp>
00011
00012 #include <QtNetwork/QNetworkReply>
00013
00014 class QByteArray;
00015
00016 namespace BALL
00017 {
00018 namespace SimpleDownloaderHelper
00019 {
00020 class HelperThread;
00021 }
00022
00033 class BALL_EXPORT SimpleDownloader
00034 : public QObject
00035 {
00036 Q_OBJECT
00037
00038 public:
00039
00040
00041
00042
00043
00044
00045
00046 SimpleDownloader(const String& url, unsigned int timeout = UINT_MAX);
00047
00048
00049
00050
00051
00052
00053
00054
00055 SimpleDownloader(const QUrl& url, unsigned int timeout = UINT_MAX);
00056
00064 int downloadToBuffer(std::vector<char>& buffer);
00065
00072 int downloadToFile(const String& path);
00073
00082 int uploadStringToBuffer(const String& data, std::vector<char>& response);
00083
00092 int uploadStringToFile(const String& data, const String& response);
00093
00102 int uploadFileToBuffer(const String& path, std::vector<char>& response);
00103
00112 int uploadFileToFile(const String& path, const String& response);
00113
00120 void setTimeout(unsigned int timeout);
00121
00127 void setURL(const String& url);
00128
00134 void setURL(const QUrl& url);
00135
00141 const QUrl& getURL() const;
00142
00143 private:
00144 int download_(SimpleDownloaderHelper::HelperThread& thread);
00145 int qftpDownloadHack_(QIODevice* iodev);
00146
00147 QUrl url_;
00148 unsigned int timeout_;
00149 };
00150
00151 namespace SimpleDownloaderHelper
00152 {
00153 class HelperThread : public QThread
00154 {
00155 public:
00156 HelperThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent);
00157 HelperThread(const QUrl& url, const String& path, SimpleDownloader* parent);
00158
00159 int getStatus();
00160
00161 protected:
00162 virtual QNetworkReply* getReply_(QNetworkAccessManager* man) = 0;
00163
00164 void run();
00165
00166 int err_;
00167 QUrl url_;
00168 QByteArray* result_;
00169 String path_;
00170 SimpleDownloader* parent_;
00171 };
00172
00173 class DLThread : public HelperThread
00174 {
00175 public:
00176 DLThread(const QUrl& url, QByteArray* result, SimpleDownloader* parent);
00177 DLThread(const QUrl& url, const String& path, SimpleDownloader* parent);
00178
00179 protected:
00180 virtual QNetworkReply* getReply_(QNetworkAccessManager* man);
00181 };
00182
00183 class UPThread : public HelperThread
00184 {
00185 public:
00186 UPThread(const QUrl& url, const QByteArray* data, QByteArray* result, SimpleDownloader* parent);
00187 UPThread(const QUrl& url, const QByteArray* data, const String& path, SimpleDownloader* parent);
00188 UPThread(const QUrl& url, QIODevice* file, QByteArray* result, SimpleDownloader* parent);
00189 UPThread(const QUrl& url, QIODevice* file, const String& path, SimpleDownloader* parent);
00190
00191 protected:
00192 virtual QNetworkReply* getReply_(QNetworkAccessManager* man);
00193
00194 const QByteArray* data_;
00195 QIODevice* file_;
00196 };
00197
00198 class BasicHelper : public QObject
00199 {
00200 Q_OBJECT
00201
00202 public:
00203 BasicHelper(HelperThread* caller, QNetworkReply* reply);
00204 virtual ~BasicHelper(){}
00205
00206 public slots:
00207 void error(QNetworkReply::NetworkError error);
00208 #ifndef QT_NO_OPENSSL
00209 void sslErrors(const QList<QSslError>& errors);
00210 #endif
00211 virtual void finished() = 0;
00212
00213 protected:
00214 HelperThread* caller_;
00215 QNetworkReply* reply_;
00216 };
00217
00218 class DLArrayHelper : public BasicHelper
00219 {
00220 Q_OBJECT
00221
00222 public:
00223 DLArrayHelper(HelperThread* caller, QNetworkReply* reply, QByteArray* result);
00224
00225 public slots:
00226 void finished();
00227
00228 private:
00229 QByteArray* result_;
00230 };
00231
00232 class DLHelper : public BasicHelper
00233 {
00234 Q_OBJECT
00235
00236 public:
00237 DLHelper(HelperThread* caller, QNetworkReply* reply, const String& path);
00238
00239 public slots:
00240 void finished();
00241 void receivedData();
00242
00243 private:
00244 QFile file_;
00245 };
00246
00247
00248
00249
00250
00251
00252 class QFtpHackHelper;
00253
00254 class QFtpHackThread : public QThread
00255 {
00256 Q_OBJECT
00257
00258 public:
00259 QFtpHackThread(const QUrl& url, QIODevice* iodev, SimpleDownloader* parent);
00260 ~QFtpHackThread();
00261
00262 protected:
00263 void run();
00264
00265 private:
00266 QFtp* ftp_;
00267 QFtpHackHelper* helper_;
00268 QUrl url_;
00269 QIODevice* iodev_;
00270 SimpleDownloader* parent_;
00271 };
00272
00273 class QFtpHackHelper : public QObject
00274 {
00275 Q_OBJECT
00276
00277 public:
00278 QFtpHackHelper(QFtpHackThread* th);
00279
00280 public slots:
00281 void done(bool error);
00282
00283 private:
00284 QFtpHackThread* th_;
00285 };
00286
00287 }
00288 }
00289
00290 #endif //BALL_SYSTEM_SIMPLEDOWNLOADER_H