Extract downloaded content into temp dir
authorunc0rr
Sun, 04 Sep 2011 14:44:13 +0400
changeset 5756 b451fd21ff4c
parent 5755 a079b4dea081
child 5757 5bf89dbf3547
Extract downloaded content into temp dir
QTfrontend/main.cpp
QTfrontend/pagedata.cpp
QTfrontend/pagedata.h
--- a/QTfrontend/main.cpp	Sun Sep 04 13:46:08 2011 +0400
+++ b/QTfrontend/main.cpp	Sun Sep 04 14:44:13 2011 +0400
@@ -470,8 +470,8 @@
         app.installTranslator(&Translator);
     }
 
+#ifdef _WIN32
     // Win32 registry setup (used for xfire detection etc. - don't set it if we're running in "portable" mode with a custom config dir)
-#ifdef _WIN32
     if(!custom_config)
     {
         QSettings registry_hklm("HKEY_LOCAL_MACHINE", QSettings::NativeFormat);
--- a/QTfrontend/pagedata.cpp	Sun Sep 04 13:46:08 2011 +0400
+++ b/QTfrontend/pagedata.cpp	Sun Sep 04 14:44:13 2011 +0400
@@ -25,11 +25,13 @@
 #include <QFileDialog>
 #include <QDebug>
 #include <QProgressBar>
+#include <QBuffer>
 
 #include "pagedata.h"
 #include "databrowser.h"
 
 #include "quazip.h"
+#include "quazipfile.h"
 
 PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent)
 {
@@ -56,7 +58,7 @@
     qWarning() << "Download Request" << url.toString();
     QString fileName = QFileInfo(url.toString()).fileName();
 
-    QNetworkRequest newRequest(url);
+    QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + url.toString()));
     newRequest.setAttribute(QNetworkRequest::User, fileName);
 
     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
@@ -94,6 +96,8 @@
             progressBars.remove(reply);
             progressBar->deleteLater();
         }
+
+        extractDataPack(&fileContents);
     }
 }
 
@@ -121,3 +125,71 @@
     QNetworkReply *reply = manager->get(newRequest);
     connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
 }
+
+bool PageDataDownload::extractDataPack(QByteArray * buf)
+{
+    QBuffer buffer;
+    buffer.setBuffer(buf);
+
+    QuaZip zip;
+    zip.setIoDevice(&buffer);
+    if(!zip.open(QuaZip::mdUnzip))
+    {
+      qWarning("testRead(): zip.open(): %d", zip.getZipError());
+      return false;
+    }
+
+    QuaZipFile file(&zip);
+
+    for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
+    {
+        if(!file.open(QIODevice::ReadOnly))
+        {
+            qWarning("file.open(): %d", file.getZipError());
+            return false;
+        }
+
+
+        QString fileName = file.getActualFileName();
+        QString filePath = QDir::tempPath() + "/" + fileName;
+        if (fileName.endsWith("/"))
+        {
+            QFileInfo fi(filePath);
+            QDir().mkpath(fi.filePath());
+        } else
+        {
+            qDebug() << filePath;
+            QFile out(filePath);
+            if(!out.open(QFile::WriteOnly))
+            {
+                qWarning() << "out.open():" << out.errorString();
+                return false;
+            }
+
+            out.write(file.readAll());
+
+            out.close();
+
+            if(file.getZipError() != UNZ_OK) {
+                qWarning("file.getFileName(): %d", file.getZipError());
+                return false;
+            }
+
+            if(!file.atEnd()) {
+                qWarning("read all but not EOF");
+                return false;
+            }
+        }
+
+        file.close();
+
+        if(file.getZipError()!=UNZ_OK) {
+            qWarning("file.close(): %d", file.getZipError());
+            return false;
+        }
+    }
+
+    zip.close();
+
+    return true;
+}
--- a/QTfrontend/pagedata.h	Sun Sep 04 13:46:08 2011 +0400
+++ b/QTfrontend/pagedata.h	Sun Sep 04 14:44:13 2011 +0400
@@ -44,6 +44,8 @@
     QHash<QNetworkReply*, QProgressBar *> progressBars;
     QVBoxLayout *progressBarsLayout;
 
+    bool extractDataPack(QByteArray * buf);
+
 private slots:
     void install(const QUrl &url);