QTfrontend/ui/page/pagedata.cpp
branchphysfslayer
changeset 8055 04dd8b7fb605
parent 7824 3df73ec7fcf2
child 8059 07661fb20586
equal deleted inserted replaced
8052:845b5ae03841 8055:04dd8b7fb605
    29 
    29 
    30 #include "pagedata.h"
    30 #include "pagedata.h"
    31 #include "databrowser.h"
    31 #include "databrowser.h"
    32 #include "hwconsts.h"
    32 #include "hwconsts.h"
    33 #include "DataManager.h"
    33 #include "DataManager.h"
    34 
    34 #include "FileEngine.h"
    35 #include "quazip.h"
       
    36 #include "quazipfile.h"
       
    37 
    35 
    38 QLayout * PageDataDownload::bodyLayoutDefinition()
    36 QLayout * PageDataDownload::bodyLayoutDefinition()
    39 {
    37 {
    40     QGridLayout * pageLayout = new QGridLayout();
    38     QGridLayout * pageLayout = new QGridLayout();
    41     pageLayout->setColumnStretch(0, 1);
    39     pageLayout->setColumnStretch(0, 1);
    72     if(url.host().isEmpty())
    70     if(url.host().isEmpty())
    73         finalUrl = QUrl("http://www.hedgewars.org" + url.path());
    71         finalUrl = QUrl("http://www.hedgewars.org" + url.path());
    74     else
    72     else
    75         finalUrl = url;
    73         finalUrl = url;
    76 
    74 
    77     if(url.path().endsWith(".zip"))
    75     if(url.path().endsWith(".hwp"))
    78     {
    76     {
    79         qWarning() << "Download Request" << url.toString();
    77         qWarning() << "Download Request" << url.toString();
    80         QString fileName = QFileInfo(url.toString()).fileName();
    78         QString fileName = QFileInfo(url.toString()).fileName();
    81 
    79 
    82         QNetworkRequest newRequest(finalUrl);
    80         QNetworkRequest newRequest(finalUrl);
   126 {
   124 {
   127     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
   125     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
   128 
   126 
   129     if(reply)
   127     if(reply)
   130     {
   128     {
   131         QByteArray fileContents = reply->readAll();
       
   132         QProgressBar *progressBar = progressBars.value(reply, 0);
   129         QProgressBar *progressBar = progressBars.value(reply, 0);
   133 
   130 
   134         if(progressBar)
   131         if(progressBar)
   135         {
   132         {
   136             progressBars.remove(reply);
   133             progressBars.remove(reply);
   137             progressBar->deleteLater();
   134             progressBar->deleteLater();
   138         }
   135         }
   139 
   136 
   140         extractDataPack(&fileContents);
   137         QDir extractDir(*cfgdir);
       
   138         extractDir.cd("Data");
       
   139 
       
   140         QString fileName = extractDir.filePath(QFileInfo(reply->url().path()).fileName());
       
   141 
       
   142         QFile out(fileName);
       
   143         if(!out.open(QFile::WriteOnly))
       
   144         {
       
   145             qWarning() << "out.open():" << out.errorString();
       
   146             return ;
       
   147         }
       
   148 
       
   149         out.write(reply->readAll());
       
   150 
       
   151         out.close();
       
   152 
       
   153         // now mount it
       
   154         FileEngineHandler::mount(fileName);
   141     }
   155     }
   142 }
   156 }
   143 
   157 
   144 void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
   158 void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
   145 {
   159 {
   160 void PageDataDownload::fetchList()
   174 void PageDataDownload::fetchList()
   161 {
   175 {
   162     request(QUrl("http://hedgewars.org/content.html"));
   176     request(QUrl("http://hedgewars.org/content.html"));
   163 }
   177 }
   164 
   178 
   165 bool PageDataDownload::extractDataPack(QByteArray * buf)
       
   166 {
       
   167     QBuffer buffer;
       
   168     buffer.setBuffer(buf);
       
   169 
       
   170     QuaZip zip;
       
   171     zip.setIoDevice(&buffer);
       
   172     if(!zip.open(QuaZip::mdUnzip))
       
   173     {
       
   174         qWarning("testRead(): zip.open(): %d", zip.getZipError());
       
   175         return false;
       
   176     }
       
   177 
       
   178     QuaZipFile file(&zip);
       
   179 
       
   180     QDir extractDir(*cfgdir);
       
   181     extractDir.cd("Data");
       
   182 
       
   183     for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
       
   184     {
       
   185         if(!file.open(QIODevice::ReadOnly))
       
   186         {
       
   187             qWarning("file.open(): %d", file.getZipError());
       
   188             return false;
       
   189         }
       
   190 
       
   191 
       
   192         QString fileName = file.getActualFileName();
       
   193         QString filePath = extractDir.filePath(fileName);
       
   194         if (fileName.endsWith("/"))
       
   195         {
       
   196             QFileInfo fi(filePath);
       
   197             QDir().mkpath(fi.filePath());
       
   198         }
       
   199         else
       
   200         {
       
   201             qDebug() << "Extracting" << filePath;
       
   202             QFile out(filePath);
       
   203             if(!out.open(QFile::WriteOnly))
       
   204             {
       
   205                 qWarning() << "out.open():" << out.errorString();
       
   206                 return false;
       
   207             }
       
   208 
       
   209             out.write(file.readAll());
       
   210 
       
   211             out.close();
       
   212 
       
   213             if(file.getZipError() != UNZ_OK)
       
   214             {
       
   215                 qWarning("file.getFileName(): %d", file.getZipError());
       
   216                 return false;
       
   217             }
       
   218 
       
   219             if(!file.atEnd())
       
   220             {
       
   221                 qWarning("read all but not EOF");
       
   222                 return false;
       
   223             }
       
   224 
       
   225             m_contentDownloaded = true;
       
   226         }
       
   227 
       
   228         file.close();
       
   229 
       
   230         if(file.getZipError()!=UNZ_OK)
       
   231         {
       
   232             qWarning("file.close(): %d", file.getZipError());
       
   233             return false;
       
   234         }
       
   235     }
       
   236 
       
   237     zip.close();
       
   238 
       
   239     return true;
       
   240 }
       
   241 
       
   242 
   179 
   243 void PageDataDownload::onPageLeave()
   180 void PageDataDownload::onPageLeave()
   244 {
   181 {
   245     if (m_contentDownloaded)
   182     if (m_contentDownloaded)
   246     {
   183     {