- Implement progressbars to show download progress
authorunc0rr
Sun, 04 Sep 2011 11:48:01 +0400
changeset 5754 583b7a683b17
parent 5752 ea95ee97c805
child 5755 a079b4dea081
- Implement progressbars to show download progress - Link to quazip library (not used yet)
QTfrontend/CMakeLists.txt
QTfrontend/pagedata.cpp
QTfrontend/pagedata.h
project_files/hedgewars.pro
--- a/QTfrontend/CMakeLists.txt	Sun Sep 04 10:58:42 2011 +0400
+++ b/QTfrontend/CMakeLists.txt	Sun Sep 04 11:48:01 2011 +0400
@@ -4,7 +4,6 @@
 set(QT_USE_QTCORE TRUE)
 set(QT_USE_QTGUI TRUE)
 set(QT_USE_QTNETWORK TRUE)
-set(QT_USE_QTWEBKIT TRUE)
 set(QT_USE_QTSVG FALSE)
 set(QT_USE_QTXML FALSE)
 set(QT_USE_QTOPENGL FALSE)
@@ -20,6 +19,7 @@
 include_directories(.)
 include_directories(${SDL_INCLUDE_DIR})
 include_directories(${SDLMIXER_INCLUDE_DIR})
+include_directories(${CMAKE_SOURCE_DIR}/misc/quazip)
 if(UNIX)
     # HACK: in freebsd cannot find iconv.h included via SDL.h
     include_directories("/usr/local/include")
--- a/QTfrontend/pagedata.cpp	Sun Sep 04 10:58:42 2011 +0400
+++ b/QTfrontend/pagedata.cpp	Sun Sep 04 11:48:01 2011 +0400
@@ -24,10 +24,13 @@
 #include <QFileInfo>
 #include <QFileDialog>
 #include <QTextBrowser>
-
+#include <QDebug>
+#include <QProgressBar>
 
 #include "pagedata.h"
 
+#include "quazip.h"
+
 PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent)
 {
     QGridLayout * pageLayout = new QGridLayout(this);
@@ -35,40 +38,39 @@
     pageLayout->setColumnStretch(1, 1);
     pageLayout->setColumnStretch(2, 1);
 
-    BtnBack = addButton(":/res/Exit.png", pageLayout, 1, 0, true);
+    BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true);
 
     web = new QTextBrowser(this);
     connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(install(const QUrl&)));
     web->setOpenLinks(false);
-    //web->setSource();
-    //web->load(QUrl("http://m8y.org/hw/downloads/"));
-    //web->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
     pageLayout->addWidget(web, 0, 0, 1, 3);
 
-
-    QNetworkRequest newRequest(QUrl("http://m8y.org/hw/downloads/index.xhtml"));
-    //newRequest.setAttribute(QNetworkRequest::User, fileName);
+    progressBarsLayout = new QVBoxLayout(this);
+    pageLayout->addLayout(progressBarsLayout, 1, 0, 1, 3);
 
-    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
-    QNetworkReply *reply = manager->get(newRequest);
-    connect(reply, SIGNAL(finished()), this, SLOT(downloadIssueFinished()));
+    fetchList();
 }
 
 void PageDataDownload::install(const QUrl &url)
 {
-qWarning("Download Request");
-QString fileName = QFileInfo(url.toString()).fileName();
+    qWarning() << "Download Request" << url.toString();
+    QString fileName = QFileInfo(url.toString()).fileName();
+
+    QNetworkRequest newRequest(url);
+    newRequest.setAttribute(QNetworkRequest::User, fileName);
 
-QNetworkRequest newRequest(url);
-newRequest.setAttribute(QNetworkRequest::User, fileName);
+    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
+    QNetworkReply *reply = manager->get(newRequest);
+    connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded()));
+    connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));
 
-QNetworkAccessManager *manager = new QNetworkAccessManager(this);
-QNetworkReply *reply = manager->get(newRequest);
-//connect( reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)) );
+    QProgressBar *progressBar = new QProgressBar(this);
+    progressBarsLayout->addWidget(progressBar);
+    progressBars.insert(reply, progressBar);
 }
 
 
-void PageDataDownload::downloadIssueFinished()
+void PageDataDownload::pageDownloaded()
 {
     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
 
@@ -78,6 +80,44 @@
     }
 }
 
+void PageDataDownload::fileDownloaded()
+{
+    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
 
+    if(reply)
+    {
+        QByteArray fileContents = reply->readAll();
+        QProgressBar *progressBar = progressBars.value(reply, 0);
+
+        if(progressBar)
+        {
+            progressBars.remove(reply);
+            progressBar->deleteLater();
+        }
+    }
+}
+
+void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
+{
+    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
 
+    if(reply)
+    {
+        QProgressBar *progressBar = progressBars.value(reply, 0);
 
+        if(progressBar)
+        {
+            progressBar->setValue(bytesRecieved);
+            progressBar->setMaximum(bytesTotal);
+        }
+    }
+}
+
+void PageDataDownload::fetchList()
+{
+    QNetworkRequest newRequest(QUrl("http://hedgewars.org/download.html"));
+
+    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
+    QNetworkReply *reply = manager->get(newRequest);
+    connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
+}
--- a/QTfrontend/pagedata.h	Sun Sep 04 10:58:42 2011 +0400
+++ b/QTfrontend/pagedata.h	Sun Sep 04 11:48:01 2011 +0400
@@ -23,6 +23,9 @@
 #include "AbstractPage.h"
 
 class QTextBrowser;
+class QProgressBar;
+class QNetworkReply;
+class QVBoxLayout;
 
 class PageDataDownload : public AbstractPage
 {
@@ -32,12 +35,21 @@
     PageDataDownload(QWidget* parent = 0);
 
     QPushButton *BtnBack;
+
+public slots:
+    void fetchList();
+
+private:
     QTextBrowser *web;
+    QHash<QNetworkReply*, QProgressBar *> progressBars;
+    QVBoxLayout *progressBarsLayout;
 
 private slots:
     void install(const QUrl &url);
 
-    void downloadIssueFinished();
+    void pageDownloaded();
+    void fileDownloaded();
+    void downloadProgress(qint64, qint64);
 };
 
 #endif
--- a/project_files/hedgewars.pro	Sun Sep 04 10:58:42 2011 +0400
+++ b/project_files/hedgewars.pro	Sun Sep 04 11:48:01 2011 +0400
@@ -4,6 +4,7 @@
 INCLUDEPATH += ../QTfrontend/
 INCLUDEPATH += /usr/local/include/SDL
 INCLUDEPATH += /usr/include/SDL
+INCLUDEPATH += ../misc/quazip/
 
 DESTDIR = .
 
@@ -148,6 +149,8 @@
 
 RESOURCES += ../QTfrontend/hedgewars.qrc
 
+LIBS += -L../misc/quazip -lquazip
+
 !macx {
         LIBS += -lSDL -lSDL_mixer
 } else {