Added download progress notification
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Tue, 5 Oct 2010 08:21:52 +0000 (10:21 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Tue, 5 Oct 2010 08:21:52 +0000 (10:21 +0200)
src/plugins/xdxf/HttpDownloader.cpp
src/plugins/xdxf/HttpDownloader.h
src/plugins/xdxf/XdxfDialog.cpp
src/plugins/xdxf/XdxfDictDownloadProgressDialog.cpp
src/plugins/xdxf/XdxfDictDownloadProgressDialog.h
src/plugins/xdxf/XdxfDictDownloader.cpp
src/plugins/xdxf/XdxfDictDownloader.h

index e693c92..54f8273 100644 (file)
@@ -46,12 +46,16 @@ void HttpDownloader::download(QUrl url, QString file) {
     // Following line is crucial becouse sourceforge wont redirect correctly
     //    if no user-agent is supplied
     request.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)");
-    manager->get(request);
+    currentReply = manager->get(request);
+    connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)),
+               this, SIGNAL(progress(qint64,qint64)));
 
 }
 
 void HttpDownloader::downloadFinished(QNetworkReply *reply) {
 
+    disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)),
+               this, SIGNAL(progress(qint64,qint64)));
     if(reply->error() != QNetworkReply::NoError) {
         Q_EMIT error(reply->errorString());
         return;
@@ -65,7 +69,9 @@ void HttpDownloader::downloadFinished(QNetworkReply *reply) {
        QNetworkRequest req;
        req.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)");
        req.setUrl(r);
-       manager->get(req);
+       currentReply = manager->get(req);
+       connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)),
+                  this, SIGNAL(progress(qint64,qint64)));
     }
     else {
         QFile resultFile(destFile);
index ec6ac27..76f27d1 100644 (file)
@@ -49,11 +49,13 @@ private Q_SLOTS:
 Q_SIGNALS:
     void finished();
     void error(QString);
+    void progress(qint64,qint64);
 
 private:
     QHttp *http;
     QNetworkAccessManager *manager;
     QString destFile;
+    QNetworkReply* currentReply;
 
 
 };
index 4a918e2..f89b92b 100644 (file)
@@ -245,7 +245,6 @@ void XdxfDialog::selectFile() {
 }
 
 void XdxfDialog::downloadFile() {
-   qDebug()<<"a";
    XdxfPlugin::dictDownloader.download(this);
 }
 
index 2e95783..9d730bf 100644 (file)
@@ -57,6 +57,10 @@ XdxfDictDownloadProgressDialog::XdxfDictDownloadProgressDialog(QWidget*parent):
     #endif
 }
 
+void XdxfDictDownloadProgressDialog::show() {
+    downloadProgressBar->setMaximum(0);
+    QDialog::show();
+}
 
 void XdxfDictDownloadProgressDialog::setText(QString text) {
     setWindowTitle(text);
@@ -65,6 +69,13 @@ void XdxfDictDownloadProgressDialog::setText(QString text) {
     #endif
 }
 
+void XdxfDictDownloadProgressDialog::updateProgress(float progress) {
+    if(downloadProgressBar->maximum() == 0) {
+        downloadProgressBar->setMaximum(100);
+    }
+    downloadProgressBar->setValue(progress*100);
+}
+
 void XdxfDictDownloadProgressDialog::reject() {
     #ifndef Q_WS_MAEMO_5
         Q_EMIT cancelDownloading();
index bce23c0..9238901 100644 (file)
@@ -46,10 +46,15 @@ public Q_SLOTS:
     //! Set text which will be displayed to user as info about current download
     void setText(QString);
 
+    void updateProgress(float progress);
+
+    void show();
+
 Q_SIGNALS:
     //! signal emitted when user cancels downloading of a dictionary
     void cancelDownloading();
 
+
 private:
     QLabel* downloadLabel;
     QProgressBar* downloadProgressBar;
index 38066f4..908f1ef 100644 (file)
@@ -50,6 +50,9 @@ XdxfDictDownloader::XdxfDictDownloader(QObject *parent) :
     connect(&http, SIGNAL(finished()), this, SLOT(processFinished()));
     connect(&http, SIGNAL(error(QString)),
             this, SLOT(downloadingError(QString)));
+    connect(&http, SIGNAL(progress(qint64,qint64)),
+            this, SLOT(updateDownloadProgress(qint64,qint64)));
+
 }
 
 void XdxfDictDownloader::download(QWidget *parent) {
@@ -62,6 +65,9 @@ void XdxfDictDownloader::download(QWidget *parent) {
 
     connect(progressDialog, SIGNAL(cancelDownloading()),
             this, SLOT(breakDownloading()));
+    connect(this, SIGNAL(downloadProgress(float)),
+            progressDialog, SLOT(updateProgress(float)));
+
     progressDialog->setText(tr("Downloading dictionaries list"));
     progressDialog->show();
 }
@@ -70,6 +76,10 @@ QString XdxfDictDownloader::downloadedFile() {
     return _downloadedFile;
 }
 
+void XdxfDictDownloader::updateDownloadProgress(qint64 downloaded,
+                                                qint64 total)   {
+    Q_EMIT downloadProgress(float(downloaded) / float(total));
+}
 
 void XdxfDictDownloader::downloadingError(QString error) {
     breakDownloading();
index 2f7dd98..6d14407 100644 (file)
@@ -64,6 +64,8 @@ Q_SIGNALS:
     //! emitted to inform user about errors and warnings
     void notify(Notify::NotifyType, QString);
 
+    void downloadProgress(float);
+
 private Q_SLOTS:
     //! obtained list of dictionaries from website
     void dictListReceived(QNetworkReply*);
@@ -76,6 +78,8 @@ private Q_SLOTS:
 
     void downloadingError(QString);
 
+    void updateDownloadProgress(qint64, qint64);
+
 private:
     //! dict is downloaded and unpacked
     void downloadComplete();