DownloadDialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 #endif
00027
00028 #include "DownloadDialog.h"
00029
00030 using namespace Gui::Dialog;
00031
00032
00033
00034 DownloadDialog::DownloadDialog( QUrl download_url, QString s, QString p )
00035 {
00036 this->setAttribute(Qt::WA_DeleteOnClose);
00037 stopped = false;
00038 purpose = s;
00039 url = download_url;
00040 path = p;
00041 statusLabel = new QLabel( QLatin1String(""), this );
00042 progressDialog = new QProgressDialog(this);
00043 http = new QHttp(this);
00044 buffer = new QBuffer(&ba, this);
00045 progressDialog->setLabel(statusLabel);
00046 QFileInfo fi( url.toString() );
00047
00048 if ( true != url.isValid() ||
00049 true == url.host().isEmpty() )
00050 {
00051 return;
00052 }
00053 else
00054 {
00055 statusLabel->setText( fi.fileName() );
00056 }
00057
00058 buffer->open(QBuffer::ReadWrite);
00059
00060 if ( url.port() == -1 )
00061 {
00062 http->setHost( url.host(), 80 );
00063 }
00064 else
00065 {
00066 http->setHost( url.host(), url.port() );
00067 }
00068
00069 http_request_id = http->get(url.path(), buffer);
00070
00071 QObject::connect( http,
00072 SIGNAL( requestFinished(int, bool) ),
00073 this,
00074 SLOT( request_finished(int, bool) ) );
00075 QObject::connect( http,
00076 SIGNAL( dataReadProgress(int, int)),
00077 this,
00078 SLOT( update_progress(int, int) ) );
00079 QObject::connect( http,
00080 SIGNAL( responseHeaderReceived(const QHttpResponseHeader &) ),
00081 this,
00082 SLOT( read_response_header(const QHttpResponseHeader &) ) );
00083 QObject::connect( progressDialog,
00084 SIGNAL( canceled() ),
00085 this,
00086 SLOT( cancel_download() ) );
00087
00088 }
00089
00090 DownloadDialog::~DownloadDialog()
00091 {
00092 }
00093
00094 void DownloadDialog::request_finished( int request_id , bool request_error )
00095 {
00096 if ( true == stopped ){
00097 buffer->close();
00098 progressDialog->hide();
00099 return;
00100 }
00101
00102 if ( http_request_id != request_id ){
00103 return;
00104 }
00105
00106 if ( true == request_error ) {
00107 stopped = true;
00108 buffer->close();
00109 progressDialog->hide();
00110 download_finished( this, false, purpose, path, http->errorString() );
00111 }else{
00112 stopped = true;
00113 buffer->close();
00114 progressDialog->hide();
00115 download_finished( this, true, purpose, path, QLatin1String("") );
00116 }
00117 }
00118
00119 void DownloadDialog::read_response_header( const QHttpResponseHeader & response_header )
00120 {
00121 if ( true == stopped ) {
00122 return;
00123 }
00124
00125 if ( response_header.statusCode() != 200 ) {
00126 stopped = true;
00127 progressDialog->hide();
00128 http->abort();
00129 download_finished( this, false, purpose, path, response_header.reasonPhrase() );
00130 }
00131 }
00132
00133 QByteArray DownloadDialog::return_data()
00134 {
00135 return ba;
00136 }
00137
00138 void DownloadDialog::update_progress( int read_bytes, int total_bytes )
00139 {
00140 if ( true == stopped ){
00141 return;
00142 }
00143
00144 progressDialog->setMaximum(total_bytes);
00145 progressDialog->setValue(read_bytes);
00146 }
00147
00148 void DownloadDialog::cancel_download()
00149 {
00150 statusLabel->setText(tr("Canceled."));
00151 stopped = true;
00152 http->abort();
00153 buffer->close();
00154 close();
00155 }
00156
00157 void DownloadDialog::closeEvent( QCloseEvent * e )
00158 {
00159 if ( stopped == false ){
00160 stopped = true;
00161 http->abort();
00162 buffer->close();
00163 }
00164
00165 e->accept();
00166 }
00167
00168 #include "moc_DownloadDialog.cpp"