QTfrontend/pagedata.cpp
changeset 5801 531f64292489
parent 5769 09aca6e1317a
child 5999 dad168522e3b
child 6009 14f6fc9869f2
equal deleted inserted replaced
5753:f5dcef0bb8b9 5801:531f64292489
    21 #include <QNetworkAccessManager>
    21 #include <QNetworkAccessManager>
    22 #include <QNetworkRequest>
    22 #include <QNetworkRequest>
    23 #include <QNetworkReply>
    23 #include <QNetworkReply>
    24 #include <QFileInfo>
    24 #include <QFileInfo>
    25 #include <QFileDialog>
    25 #include <QFileDialog>
    26 #include <QTextBrowser>
    26 #include <QDebug>
    27 
    27 #include <QProgressBar>
       
    28 #include <QBuffer>
    28 
    29 
    29 #include "pagedata.h"
    30 #include "pagedata.h"
       
    31 #include "databrowser.h"
       
    32 #include "hwconsts.h"
       
    33 
       
    34 #include "quazip.h"
       
    35 #include "quazipfile.h"
    30 
    36 
    31 PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent)
    37 PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent)
    32 {
    38 {
    33     QGridLayout * pageLayout = new QGridLayout(this);
    39     QGridLayout * pageLayout = new QGridLayout(this);
    34     pageLayout->setColumnStretch(0, 1);
    40     pageLayout->setColumnStretch(0, 1);
    35     pageLayout->setColumnStretch(1, 1);
    41     pageLayout->setColumnStretch(1, 1);
    36     pageLayout->setColumnStretch(2, 1);
    42     pageLayout->setColumnStretch(2, 1);
    37 
    43 
    38     BtnBack = addButton(":/res/Exit.png", pageLayout, 1, 0, true);
    44     BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true);
    39 
    45 
    40     web = new QTextBrowser(this);
    46     web = new DataBrowser(this);
    41     connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(install(const QUrl&)));
    47     connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(request(const QUrl&)));
    42     web->setOpenLinks(false);
    48     web->setOpenLinks(false);
    43     //web->setSource();
       
    44     //web->load(QUrl("http://m8y.org/hw/downloads/"));
       
    45     //web->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
       
    46     pageLayout->addWidget(web, 0, 0, 1, 3);
    49     pageLayout->addWidget(web, 0, 0, 1, 3);
    47 
    50 
    48 
    51     progressBarsLayout = new QVBoxLayout();
    49     QNetworkRequest newRequest(QUrl("http://m8y.org/hw/downloads/index.xhtml"));
    52     pageLayout->addLayout(progressBarsLayout, 1, 0, 1, 3);
    50     //newRequest.setAttribute(QNetworkRequest::User, fileName);
    53 
    51 
    54     fetchList();
    52     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    55 }
    53     QNetworkReply *reply = manager->get(newRequest);
    56 
    54     connect(reply, SIGNAL(finished()), this, SLOT(downloadIssueFinished()));
    57 void PageDataDownload::request(const QUrl &url)
    55 }
    58 {
    56 
    59     QUrl finalUrl;
    57 void PageDataDownload::install(const QUrl &url)
    60     if(url.host().isEmpty())
    58 {
    61         finalUrl = QUrl("http://www.hedgewars.org" + url.path());
    59 qWarning("Download Request");
    62     else
    60 QString fileName = QFileInfo(url.toString()).fileName();
    63         finalUrl = url;
    61 
    64 
    62 QNetworkRequest newRequest(url);
    65     if(url.path().endsWith(".zip"))
    63 newRequest.setAttribute(QNetworkRequest::User, fileName);
    66     {
    64 
    67         qWarning() << "Download Request" << url.toString();
    65 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    68         QString fileName = QFileInfo(url.toString()).fileName();
    66 QNetworkReply *reply = manager->get(newRequest);
    69 
    67 //connect( reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)) );
    70         QNetworkRequest newRequest(finalUrl);
    68 }
    71         newRequest.setAttribute(QNetworkRequest::User, fileName);
    69 
    72 
    70 
    73         QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    71 void PageDataDownload::downloadIssueFinished()
    74         QNetworkReply *reply = manager->get(newRequest);
       
    75         connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded()));
       
    76         connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));
       
    77 
       
    78         QProgressBar *progressBar = new QProgressBar(this);
       
    79         progressBarsLayout->addWidget(progressBar);
       
    80         progressBars.insert(reply, progressBar);
       
    81     } else
       
    82     {
       
    83         qWarning() << "Page Request" << url.toString();
       
    84 
       
    85         QNetworkRequest newRequest(finalUrl);
       
    86 
       
    87         QNetworkAccessManager *manager = new QNetworkAccessManager(this);
       
    88         QNetworkReply *reply = manager->get(newRequest);
       
    89         connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
       
    90     }
       
    91 }
       
    92 
       
    93 
       
    94 void PageDataDownload::pageDownloaded()
    72 {
    95 {
    73     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
    96     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
    74 
    97 
    75     if(reply)
    98     if(reply)
    76     {
    99     {
    77         web->setHtml(QString::fromUtf8(reply->readAll()));
   100         QString html = QString::fromUtf8(reply->readAll());
    78     }
   101         int begin = html.indexOf("<!-- BEGIN -->");
    79 }
   102         int end = html.indexOf("<!-- END -->");
    80 
   103         if(begin != -1 && begin < end)
    81 
   104         {
    82 
   105             html.truncate(end);
    83 
   106             html.remove(0, begin);
       
   107         }
       
   108         web->setHtml(html);
       
   109     }
       
   110 }
       
   111 
       
   112 void PageDataDownload::fileDownloaded()
       
   113 {
       
   114     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
       
   115 
       
   116     if(reply)
       
   117     {
       
   118         QByteArray fileContents = reply->readAll();
       
   119         QProgressBar *progressBar = progressBars.value(reply, 0);
       
   120 
       
   121         if(progressBar)
       
   122         {
       
   123             progressBars.remove(reply);
       
   124             progressBar->deleteLater();
       
   125         }
       
   126 
       
   127         extractDataPack(&fileContents);
       
   128     }
       
   129 }
       
   130 
       
   131 void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
       
   132 {
       
   133     QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
       
   134 
       
   135     if(reply)
       
   136     {
       
   137         QProgressBar *progressBar = progressBars.value(reply, 0);
       
   138 
       
   139         if(progressBar)
       
   140         {
       
   141             progressBar->setValue(bytesRecieved);
       
   142             progressBar->setMaximum(bytesTotal);
       
   143         }
       
   144     }
       
   145 }
       
   146 
       
   147 void PageDataDownload::fetchList()
       
   148 {
       
   149     request(QUrl("http://hedgewars.org/content.html"));
       
   150 }
       
   151 
       
   152 bool PageDataDownload::extractDataPack(QByteArray * buf)
       
   153 {
       
   154     QBuffer buffer;
       
   155     buffer.setBuffer(buf);
       
   156 
       
   157     QuaZip zip;
       
   158     zip.setIoDevice(&buffer);
       
   159     if(!zip.open(QuaZip::mdUnzip))
       
   160     {
       
   161       qWarning("testRead(): zip.open(): %d", zip.getZipError());
       
   162       return false;
       
   163     }
       
   164 
       
   165     QuaZipFile file(&zip);
       
   166 
       
   167     QDir extractDir(*cfgdir);
       
   168     extractDir.cd("Data");
       
   169 
       
   170     for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
       
   171     {
       
   172         if(!file.open(QIODevice::ReadOnly))
       
   173         {
       
   174             qWarning("file.open(): %d", file.getZipError());
       
   175             return false;
       
   176         }
       
   177 
       
   178 
       
   179         QString fileName = file.getActualFileName();
       
   180         QString filePath = extractDir.filePath(fileName);
       
   181         if (fileName.endsWith("/"))
       
   182         {
       
   183             QFileInfo fi(filePath);
       
   184             QDir().mkpath(fi.filePath());
       
   185         } else
       
   186         {
       
   187             qDebug() << "Extracting" << filePath;
       
   188             QFile out(filePath);
       
   189             if(!out.open(QFile::WriteOnly))
       
   190             {
       
   191                 qWarning() << "out.open():" << out.errorString();
       
   192                 return false;
       
   193             }
       
   194 
       
   195             out.write(file.readAll());
       
   196 
       
   197             out.close();
       
   198 
       
   199             if(file.getZipError() != UNZ_OK) {
       
   200                 qWarning("file.getFileName(): %d", file.getZipError());
       
   201                 return false;
       
   202             }
       
   203 
       
   204             if(!file.atEnd()) {
       
   205                 qWarning("read all but not EOF");
       
   206                 return false;
       
   207             }
       
   208         }
       
   209 
       
   210         file.close();
       
   211 
       
   212         if(file.getZipError()!=UNZ_OK) {
       
   213             qWarning("file.close(): %d", file.getZipError());
       
   214             return false;
       
   215         }
       
   216     }
       
   217 
       
   218     zip.close();
       
   219 
       
   220     return true;
       
   221 }