Implement DataBrowser which downloads resources (images, css) on its own.
authorunc0rr
Sun, 04 Sep 2011 13:46:08 +0400
changeset 5755 a079b4dea081
parent 5754 583b7a683b17
child 5756 b451fd21ff4c
Implement DataBrowser which downloads resources (images, css) on its own. There's a hack with resources base url and a bug with images not appearing when downloading done (scroll helps though).
QTfrontend/CMakeLists.txt
QTfrontend/databrowser.cpp
QTfrontend/databrowser.h
QTfrontend/pagedata.cpp
QTfrontend/pagedata.h
project_files/hedgewars.pro
--- a/QTfrontend/CMakeLists.txt	Sun Sep 04 11:48:01 2011 +0400
+++ b/QTfrontend/CMakeLists.txt	Sun Sep 04 13:46:08 2011 +0400
@@ -115,6 +115,7 @@
     drawmapwidget.cpp
     drawmapscene.cpp
     themesmodel.cpp
+    databrowser.cpp
     )
 
 #xfire integration
@@ -196,6 +197,7 @@
     drawmapwidget.h
     drawmapscene.h
     themesmodel.h
+    databrowser.h
     )
 
 set(hwfr_hdrs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/databrowser.cpp	Sun Sep 04 13:46:08 2011 +0400
@@ -0,0 +1,52 @@
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QDebug>
+
+#include "databrowser.h"
+
+const QNetworkRequest::Attribute typeAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 1);
+const QNetworkRequest::Attribute urlAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 2);
+
+DataBrowser::DataBrowser(QWidget *parent) :
+    QTextBrowser(parent)
+{
+
+    manager = new QNetworkAccessManager(this);
+}
+
+QVariant DataBrowser::loadResource(int type, const QUrl & name)
+{
+    if(type == QTextDocument::ImageResource || type == QTextDocument::StyleSheetResource)
+        if(resources.contains(name))
+        {
+            return resources.take(name);
+        }
+        else
+            if(!requestedResources.contains(name))
+            {
+                requestedResources.insert(name);
+
+                QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + name.toString()));
+                newRequest.setAttribute(typeAttribute, type);
+                newRequest.setAttribute(urlAttribute, name);
+
+                QNetworkReply *reply = manager->get(newRequest);
+                connect(reply, SIGNAL(finished()), this, SLOT(resourceDownloaded()));
+            }
+
+    return QVariant();
+}
+
+void DataBrowser::resourceDownloaded()
+{
+    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
+
+    if(reply)
+    {
+        int type = reply->request().attribute(typeAttribute).toInt();
+        QUrl url = reply->request().attribute(urlAttribute).toUrl();
+        resources.insert(url, reply->readAll());
+        document()->addResource(type, reply->request().url(), QVariant());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/databrowser.h	Sun Sep 04 13:46:08 2011 +0400
@@ -0,0 +1,30 @@
+#ifndef DATABROWSER_H
+#define DATABROWSER_H
+
+#include <QTextBrowser>
+#include <QSet>
+
+class QNetworkAccessManager;
+
+class DataBrowser : public QTextBrowser
+{
+    Q_OBJECT
+public:
+    explicit DataBrowser(QWidget *parent = 0);
+
+signals:
+
+public slots:
+
+private:
+    QNetworkAccessManager *manager;
+    QHash<QUrl, QByteArray> resources;
+    QSet<QUrl> requestedResources;
+
+    QVariant loadResource(int type, const QUrl & name);
+
+private slots:
+    void resourceDownloaded();
+};
+
+#endif // DATABROWSER_H
--- a/QTfrontend/pagedata.cpp	Sun Sep 04 11:48:01 2011 +0400
+++ b/QTfrontend/pagedata.cpp	Sun Sep 04 13:46:08 2011 +0400
@@ -23,11 +23,11 @@
 #include <QNetworkReply>
 #include <QFileInfo>
 #include <QFileDialog>
-#include <QTextBrowser>
 #include <QDebug>
 #include <QProgressBar>
 
 #include "pagedata.h"
+#include "databrowser.h"
 
 #include "quazip.h"
 
@@ -40,7 +40,7 @@
 
     BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true);
 
-    web = new QTextBrowser(this);
+    web = new DataBrowser(this);
     connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(install(const QUrl&)));
     web->setOpenLinks(false);
     pageLayout->addWidget(web, 0, 0, 1, 3);
@@ -115,7 +115,7 @@
 
 void PageDataDownload::fetchList()
 {
-    QNetworkRequest newRequest(QUrl("http://hedgewars.org/download.html"));
+    QNetworkRequest newRequest(QUrl("http://hedgewars.org/node/2833"));
 
     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
     QNetworkReply *reply = manager->get(newRequest);
--- a/QTfrontend/pagedata.h	Sun Sep 04 11:48:01 2011 +0400
+++ b/QTfrontend/pagedata.h	Sun Sep 04 13:46:08 2011 +0400
@@ -22,7 +22,7 @@
 #include <QUrl>
 #include "AbstractPage.h"
 
-class QTextBrowser;
+class DataBrowser;
 class QProgressBar;
 class QNetworkReply;
 class QVBoxLayout;
@@ -40,7 +40,7 @@
     void fetchList();
 
 private:
-    QTextBrowser *web;
+    DataBrowser *web;
     QHash<QNetworkReply*, QProgressBar *> progressBars;
     QVBoxLayout *progressBarsLayout;
 
--- a/project_files/hedgewars.pro	Sun Sep 04 11:48:01 2011 +0400
+++ b/project_files/hedgewars.pro	Sun Sep 04 13:46:08 2011 +0400
@@ -65,7 +65,8 @@
     ../QTfrontend/pagegamestats.h \
     ../QTfrontend/HWApplication.h \
     ../QTfrontend/AbstractPage.h \
-    ../QTfrontend/themesmodel.h
+    ../QTfrontend/themesmodel.h \
+    ../QTfrontend/databrowser.h
 
 SOURCES += ../QTfrontend/SDLs.cpp ../QTfrontend/SquareLabel.cpp \
 	../QTfrontend/about.cpp ../QTfrontend/ammoSchemeModel.cpp \
@@ -116,7 +117,8 @@
     ../QTfrontend/pagegamestats.cpp \
     ../QTfrontend/pageplayrecord.cpp \
     ../QTfrontend/HWApplication.cpp \
-    ../QTfrontend/themesmodel.cpp
+    ../QTfrontend/themesmodel.cpp \
+    ../QTfrontend/databrowser.cpp
 
 win32 {
 	SOURCES += ../QTfrontend/xfire.cpp