QTfrontend/pagedata.cpp
author koda
Fri, 14 Oct 2011 19:00:31 +0200
branch0.9.16
changeset 5999 dad168522e3b
parent 5769 09aca6e1317a
permissions -rw-r--r--
display a message when network is down or server is not reachable, nemo also made the page refresh at display time
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     1
/*
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     3
 * Copyright (c) 2006-2011 Andrey Korotaev <unC0Rr@gmail.com>
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     4
 *
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     8
 *
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    12
 * GNU General Public License for more details.
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    13
 *
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    17
 */
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    18
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    19
#include <QGridLayout>
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    20
#include <QPushButton>
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    21
#include <QNetworkAccessManager>
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    22
#include <QNetworkRequest>
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    23
#include <QNetworkReply>
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    24
#include <QFileInfo>
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    25
#include <QFileDialog>
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    26
#include <QDebug>
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    27
#include <QProgressBar>
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
    28
#include <QBuffer>
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    29
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    30
#include "pagedata.h"
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents: 5754
diff changeset
    31
#include "databrowser.h"
5762
0d6a338bf377 Extract to cfgdir
unc0rr
parents: 5756
diff changeset
    32
#include "hwconsts.h"
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    33
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    34
#include "quazip.h"
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
    35
#include "quazipfile.h"
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    36
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    37
PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent)
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    38
{
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    39
    QGridLayout * pageLayout = new QGridLayout(this);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    40
    pageLayout->setColumnStretch(0, 1);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    41
    pageLayout->setColumnStretch(1, 1);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    42
    pageLayout->setColumnStretch(2, 1);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    43
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    44
    BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true);
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    45
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents: 5754
diff changeset
    46
    web = new DataBrowser(this);
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    47
    connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(request(const QUrl&)));
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    48
    web->setOpenLinks(false);
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    49
    pageLayout->addWidget(web, 0, 0, 1, 3);
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    50
5769
09aca6e1317a Prevent runtime warning
unc0rr
parents: 5768
diff changeset
    51
    progressBarsLayout = new QVBoxLayout();
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    52
    pageLayout->addLayout(progressBarsLayout, 1, 0, 1, 3);
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    53
5999
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
    54
    web->setHtml("<center><h2>Hedgewars Downloadable Content</h2><br><br><i>Loading, please wait</i></center>");
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    55
}
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    56
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    57
void PageDataDownload::request(const QUrl &url)
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    58
{
5768
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    59
    QUrl finalUrl;
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    60
    if(url.host().isEmpty())
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    61
        finalUrl = QUrl("http://www.hedgewars.org" + url.path());
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    62
    else
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    63
        finalUrl = url;
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    64
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    65
    if(url.path().endsWith(".zip"))
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    66
    {
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    67
        qWarning() << "Download Request" << url.toString();
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    68
        QString fileName = QFileInfo(url.toString()).fileName();
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    69
5768
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    70
        QNetworkRequest newRequest(finalUrl);
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    71
        newRequest.setAttribute(QNetworkRequest::User, fileName);
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    72
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    73
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    74
        QNetworkReply *reply = manager->get(newRequest);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    75
        connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded()));
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    76
        connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    77
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    78
        QProgressBar *progressBar = new QProgressBar(this);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    79
        progressBarsLayout->addWidget(progressBar);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    80
        progressBars.insert(reply, progressBar);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    81
    } else
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    82
    {
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    83
        qWarning() << "Page Request" << url.toString();
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    84
5768
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
    85
        QNetworkRequest newRequest(finalUrl);
5765
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    86
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    87
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    88
        QNetworkReply *reply = manager->get(newRequest);
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    89
        connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
e87f6756773e Allow surfing, download and extract only .zip files
unc0rr
parents: 5764
diff changeset
    90
    }
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    91
}
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    92
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    93
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    94
void PageDataDownload::pageDownloaded()
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    95
{
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    96
    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    97
5999
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
    98
    if(reply && (reply->error() == QNetworkReply::NoError))
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    99
    {
5766
nemo
parents: 5765
diff changeset
   100
        QString html = QString::fromUtf8(reply->readAll());
5768
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   101
        int begin = html.indexOf("<!-- BEGIN -->");
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   102
        int end = html.indexOf("<!-- END -->");
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   103
        if(begin != -1 && begin < end)
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   104
        {
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   105
            html.truncate(end);
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   106
            html.remove(0, begin);
852ba8f5685c Only prepend hedgewars.org if the url is relative
unc0rr
parents: 5766
diff changeset
   107
        }
5764
d785d7e23113 truncate page on comment tags, as suggested by unc0rr
nemo
parents: 5762
diff changeset
   108
        web->setHtml(html);
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
   109
    }
5999
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
   110
    else
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
   111
    {
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
   112
        web->setHtml("<center><h2>Hedgewars Downloadable Content</h2><br><br><p><i><h4>This page requires an internet connection.</i></h4></center>");
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
   113
    }
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
   114
}
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
   115
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   116
void PageDataDownload::fileDownloaded()
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   117
{
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   118
    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
   119
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   120
    if(reply)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   121
    {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   122
        QByteArray fileContents = reply->readAll();
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   123
        QProgressBar *progressBar = progressBars.value(reply, 0);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   124
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   125
        if(progressBar)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   126
        {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   127
            progressBars.remove(reply);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   128
            progressBar->deleteLater();
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   129
        }
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   130
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   131
        extractDataPack(&fileContents);
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   132
    }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   133
}
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   134
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   135
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   136
{
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   137
    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
   138
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   139
    if(reply)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   140
    {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   141
        QProgressBar *progressBar = progressBars.value(reply, 0);
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
   142
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   143
        if(progressBar)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   144
        {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   145
            progressBar->setValue(bytesRecieved);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   146
            progressBar->setMaximum(bytesTotal);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   147
        }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   148
    }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   149
}
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   150
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   151
void PageDataDownload::fetchList()
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   152
{
5999
dad168522e3b display a message when network is down or server is not reachable, nemo also made the page refresh at display time
koda
parents: 5769
diff changeset
   153
    request(QUrl("http://www.hedgewars.org/content.html"));
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   154
}
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   155
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   156
bool PageDataDownload::extractDataPack(QByteArray * buf)
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   157
{
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   158
    QBuffer buffer;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   159
    buffer.setBuffer(buf);
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   160
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   161
    QuaZip zip;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   162
    zip.setIoDevice(&buffer);
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   163
    if(!zip.open(QuaZip::mdUnzip))
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   164
    {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   165
      qWarning("testRead(): zip.open(): %d", zip.getZipError());
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   166
      return false;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   167
    }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   168
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   169
    QuaZipFile file(&zip);
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   170
5762
0d6a338bf377 Extract to cfgdir
unc0rr
parents: 5756
diff changeset
   171
    QDir extractDir(*cfgdir);
0d6a338bf377 Extract to cfgdir
unc0rr
parents: 5756
diff changeset
   172
    extractDir.cd("Data");
0d6a338bf377 Extract to cfgdir
unc0rr
parents: 5756
diff changeset
   173
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   174
    for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile())
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   175
    {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   176
        if(!file.open(QIODevice::ReadOnly))
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   177
        {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   178
            qWarning("file.open(): %d", file.getZipError());
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   179
            return false;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   180
        }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   181
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   182
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   183
        QString fileName = file.getActualFileName();
5762
0d6a338bf377 Extract to cfgdir
unc0rr
parents: 5756
diff changeset
   184
        QString filePath = extractDir.filePath(fileName);
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   185
        if (fileName.endsWith("/"))
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   186
        {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   187
            QFileInfo fi(filePath);
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   188
            QDir().mkpath(fi.filePath());
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   189
        } else
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   190
        {
5762
0d6a338bf377 Extract to cfgdir
unc0rr
parents: 5756
diff changeset
   191
            qDebug() << "Extracting" << filePath;
5756
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   192
            QFile out(filePath);
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   193
            if(!out.open(QFile::WriteOnly))
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   194
            {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   195
                qWarning() << "out.open():" << out.errorString();
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   196
                return false;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   197
            }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   198
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   199
            out.write(file.readAll());
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   200
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   201
            out.close();
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   202
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   203
            if(file.getZipError() != UNZ_OK) {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   204
                qWarning("file.getFileName(): %d", file.getZipError());
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   205
                return false;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   206
            }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   207
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   208
            if(!file.atEnd()) {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   209
                qWarning("read all but not EOF");
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   210
                return false;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   211
            }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   212
        }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   213
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   214
        file.close();
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   215
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   216
        if(file.getZipError()!=UNZ_OK) {
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   217
            qWarning("file.close(): %d", file.getZipError());
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   218
            return false;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   219
        }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   220
    }
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   221
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   222
    zip.close();
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   223
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   224
    return true;
b451fd21ff4c Extract downloaded content into temp dir
unc0rr
parents: 5755
diff changeset
   225
}