00001 #ifndef BALL_SYSTEM_SIMPLEDOWNADER_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 00011 #include <QtNetwork/QNetworkReply> 00012 00013 class QByteArray; 00014 00015 namespace BALL 00016 { 00017 namespace SimpleDownloaderHelper 00018 { 00019 class DLThread; 00020 } 00021 00029 class BALL_EXPORT SimpleDownloader 00030 { 00031 public: 00032 /* 00033 * Default Constructor. 00034 * 00035 * @param url The URL to download. 00036 * @param timeout The maximum number of milliseconds the download is allowed to take. 00037 * default: infinite 00038 */ 00039 SimpleDownloader(const String& url, unsigned int timeout = UINT_MAX); 00040 00048 int downloadToBuffer(std::vector<char>& buffer); 00049 00056 int downloadToFile(const String& path); 00057 00064 void setTimeout(unsigned int timeout); 00065 00071 void setURL(const String& url); 00072 00078 const String& getURL() const; 00079 00080 private: 00081 int download_(SimpleDownloaderHelper::DLThread& thread); 00082 00083 String url_; 00084 unsigned int timeout_; 00085 }; 00086 00087 namespace SimpleDownloaderHelper 00088 { 00089 class DLThread : public QThread 00090 { 00091 public: 00092 DLThread(const String& url, QByteArray* result); 00093 DLThread(const String& url, const String& path); 00094 00095 int getStatus(); 00096 00097 protected: 00098 void run(); 00099 00100 int err_; 00101 String url_; 00102 QByteArray* result_; 00103 String path_; 00104 }; 00105 00106 class BasicHelper : public QObject 00107 { 00108 Q_OBJECT 00109 00110 public: 00111 BasicHelper(DLThread* caller, QNetworkReply* reply); 00112 virtual ~BasicHelper(){} 00113 00114 public slots: 00115 void error(QNetworkReply::NetworkError error); 00116 #ifndef QT_NO_OPENSSL 00117 void sslErrors(const QList<QSslError>& errors); 00118 #endif 00119 virtual void finished() = 0; 00120 00121 protected: 00122 DLThread* caller_; 00123 QNetworkReply* reply_; 00124 }; 00125 00126 class DLArrayHelper : public BasicHelper 00127 { 00128 Q_OBJECT 00129 00130 public: 00131 DLArrayHelper(DLThread* caller, QNetworkReply* reply, QByteArray* result); 00132 00133 public slots: 00134 void finished(); 00135 00136 private: 00137 QByteArray* result_; 00138 }; 00139 00140 class DLHelper : public BasicHelper 00141 { 00142 Q_OBJECT 00143 00144 public: 00145 DLHelper(DLThread* caller, QNetworkReply* reply, const String& path); 00146 00147 public slots: 00148 void finished(); 00149 void receivedData(); 00150 00151 private: 00152 QFile file_; 00153 }; 00154 } 00155 } 00156 00157 #endif //BALL_SYSTEM_SIMPLEDOWNLOADER_H