QTfrontend/pagedata.cpp
author unc0rr
Sun, 04 Sep 2011 13:46:08 +0400
changeset 5755 a079b4dea081
parent 5754 583b7a683b17
child 5756 b451fd21ff4c
permissions -rw-r--r--
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).
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>
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    28
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    29
#include "pagedata.h"
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents: 5754
diff changeset
    30
#include "databrowser.h"
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    31
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    32
#include "quazip.h"
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    33
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    34
PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent)
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    35
{
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    36
    QGridLayout * pageLayout = new QGridLayout(this);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    37
    pageLayout->setColumnStretch(0, 1);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    38
    pageLayout->setColumnStretch(1, 1);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    39
    pageLayout->setColumnStretch(2, 1);
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    40
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    41
    BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true);
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    42
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents: 5754
diff changeset
    43
    web = new DataBrowser(this);
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    44
    connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(install(const QUrl&)));
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    45
    web->setOpenLinks(false);
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    46
    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
    47
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    48
    progressBarsLayout = new QVBoxLayout(this);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    49
    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
    50
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    51
    fetchList();
5271
48d18e329298 ack. forgot to add my stub files
nemo
parents:
diff changeset
    52
}
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    53
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    54
void PageDataDownload::install(const QUrl &url)
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    55
{
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    56
    qWarning() << "Download Request" << url.toString();
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    57
    QString fileName = QFileInfo(url.toString()).fileName();
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    58
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    59
    QNetworkRequest newRequest(url);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    60
    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
    61
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    62
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    63
    QNetworkReply *reply = manager->get(newRequest);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    64
    connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded()));
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    65
    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
    66
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    67
    QProgressBar *progressBar = new QProgressBar(this);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    68
    progressBarsLayout->addWidget(progressBar);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    69
    progressBars.insert(reply, progressBar);
5567
44c9a577b082 Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents: 5271
diff changeset
    70
}
5746
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    71
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    72
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    73
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
    74
{
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    75
    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
    76
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    77
    if(reply)
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    78
    {
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    79
        web->setHtml(QString::fromUtf8(reply->readAll()));
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    80
    }
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    81
}
fbc52bb92cad Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents: 5582
diff changeset
    82
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    83
void PageDataDownload::fileDownloaded()
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    84
{
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    85
    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
    86
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    87
    if(reply)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    88
    {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    89
        QByteArray fileContents = reply->readAll();
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    90
        QProgressBar *progressBar = progressBars.value(reply, 0);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    91
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    92
        if(progressBar)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    93
        {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    94
            progressBars.remove(reply);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    95
            progressBar->deleteLater();
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    96
        }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    97
    }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    98
}
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
    99
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   100
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   101
{
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   102
    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
   103
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   104
    if(reply)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   105
    {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   106
        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
   107
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   108
        if(progressBar)
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   109
        {
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   110
            progressBar->setValue(bytesRecieved);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   111
            progressBar->setMaximum(bytesTotal);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   112
        }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   113
    }
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   114
}
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   115
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   116
void PageDataDownload::fetchList()
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   117
{
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents: 5754
diff changeset
   118
    QNetworkRequest newRequest(QUrl("http://hedgewars.org/node/2833"));
5754
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   119
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   120
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   121
    QNetworkReply *reply = manager->get(newRequest);
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   122
    connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded()));
583b7a683b17 - Implement progressbars to show download progress
unc0rr
parents: 5746
diff changeset
   123
}