author | nemo |
Sun, 04 Sep 2011 12:19:31 -0400 | |
changeset 5764 | d785d7e23113 |
parent 5762 | 0d6a338bf377 |
child 5765 | e87f6756773e |
permissions | -rw-r--r-- |
5271 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2006-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include <QGridLayout> |
|
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 | 26 |
#include <QDebug> |
27 |
#include <QProgressBar> |
|
5756 | 28 |
#include <QBuffer> |
5271 | 29 |
|
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 | 32 |
#include "hwconsts.h" |
5271 | 33 |
|
5754 | 34 |
#include "quazip.h" |
5756 | 35 |
#include "quazipfile.h" |
5754 | 36 |
|
5271 | 37 |
PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent) |
38 |
{ |
|
39 |
QGridLayout * pageLayout = new QGridLayout(this); |
|
40 |
pageLayout->setColumnStretch(0, 1); |
|
41 |
pageLayout->setColumnStretch(1, 1); |
|
42 |
pageLayout->setColumnStretch(2, 1); |
|
43 |
||
5754 | 44 |
BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true); |
5271 | 45 |
|
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
5754
diff
changeset
|
46 |
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
|
47 |
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
|
48 |
web->setOpenLinks(false); |
5271 | 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 |
|
5754 | 51 |
progressBarsLayout = new QVBoxLayout(this); |
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 |
|
5754 | 54 |
fetchList(); |
5271 | 55 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
56 |
|
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
57 |
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
|
58 |
{ |
5754 | 59 |
qWarning() << "Download Request" << url.toString(); |
60 |
QString fileName = QFileInfo(url.toString()).fileName(); |
|
61 |
||
5756 | 62 |
QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + url.toString())); |
5754 | 63 |
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
|
64 |
|
5754 | 65 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
66 |
QNetworkReply *reply = manager->get(newRequest); |
|
67 |
connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
|
68 |
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
|
69 |
|
5754 | 70 |
QProgressBar *progressBar = new QProgressBar(this); |
71 |
progressBarsLayout->addWidget(progressBar); |
|
72 |
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
|
73 |
} |
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 |
|
5754 | 76 |
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
|
77 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
78 |
QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
5764
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
79 |
QString html = QString::fromUtf8(reply->readAll()); |
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
80 |
html.remove(0,html.indexOf("<!-- BEGIN -->")); |
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
81 |
html.truncate(html.indexOf("<!-- END -->")); |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
82 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
83 |
if(reply) |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
84 |
{ |
5764
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
85 |
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
|
86 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
87 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
88 |
|
5754 | 89 |
void PageDataDownload::fileDownloaded() |
90 |
{ |
|
91 |
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
|
92 |
|
5754 | 93 |
if(reply) |
94 |
{ |
|
95 |
QByteArray fileContents = reply->readAll(); |
|
96 |
QProgressBar *progressBar = progressBars.value(reply, 0); |
|
97 |
||
98 |
if(progressBar) |
|
99 |
{ |
|
100 |
progressBars.remove(reply); |
|
101 |
progressBar->deleteLater(); |
|
102 |
} |
|
5756 | 103 |
|
104 |
extractDataPack(&fileContents); |
|
5754 | 105 |
} |
106 |
} |
|
107 |
||
108 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
109 |
{ |
|
110 |
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
|
111 |
|
5754 | 112 |
if(reply) |
113 |
{ |
|
114 |
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
|
115 |
|
5754 | 116 |
if(progressBar) |
117 |
{ |
|
118 |
progressBar->setValue(bytesRecieved); |
|
119 |
progressBar->setMaximum(bytesTotal); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
void PageDataDownload::fetchList() |
|
125 |
{ |
|
5764
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
126 |
//QNetworkRequest newRequest(QUrl("http://hedgewars.org/node/2833")); |
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
127 |
QNetworkRequest newRequest(QUrl("http://hedgewars.org/content.html")); |
5754 | 128 |
|
129 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
|
130 |
QNetworkReply *reply = manager->get(newRequest); |
|
131 |
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
|
132 |
} |
|
5756 | 133 |
|
134 |
bool PageDataDownload::extractDataPack(QByteArray * buf) |
|
135 |
{ |
|
136 |
QBuffer buffer; |
|
137 |
buffer.setBuffer(buf); |
|
138 |
||
139 |
QuaZip zip; |
|
140 |
zip.setIoDevice(&buffer); |
|
141 |
if(!zip.open(QuaZip::mdUnzip)) |
|
142 |
{ |
|
143 |
qWarning("testRead(): zip.open(): %d", zip.getZipError()); |
|
144 |
return false; |
|
145 |
} |
|
146 |
||
147 |
QuaZipFile file(&zip); |
|
148 |
||
5762 | 149 |
QDir extractDir(*cfgdir); |
150 |
extractDir.cd("Data"); |
|
151 |
||
5756 | 152 |
for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) |
153 |
{ |
|
154 |
if(!file.open(QIODevice::ReadOnly)) |
|
155 |
{ |
|
156 |
qWarning("file.open(): %d", file.getZipError()); |
|
157 |
return false; |
|
158 |
} |
|
159 |
||
160 |
||
161 |
QString fileName = file.getActualFileName(); |
|
5762 | 162 |
QString filePath = extractDir.filePath(fileName); |
5756 | 163 |
if (fileName.endsWith("/")) |
164 |
{ |
|
165 |
QFileInfo fi(filePath); |
|
166 |
QDir().mkpath(fi.filePath()); |
|
167 |
} else |
|
168 |
{ |
|
5762 | 169 |
qDebug() << "Extracting" << filePath; |
5756 | 170 |
QFile out(filePath); |
171 |
if(!out.open(QFile::WriteOnly)) |
|
172 |
{ |
|
173 |
qWarning() << "out.open():" << out.errorString(); |
|
174 |
return false; |
|
175 |
} |
|
176 |
||
177 |
out.write(file.readAll()); |
|
178 |
||
179 |
out.close(); |
|
180 |
||
181 |
if(file.getZipError() != UNZ_OK) { |
|
182 |
qWarning("file.getFileName(): %d", file.getZipError()); |
|
183 |
return false; |
|
184 |
} |
|
185 |
||
186 |
if(!file.atEnd()) { |
|
187 |
qWarning("read all but not EOF"); |
|
188 |
return false; |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
file.close(); |
|
193 |
||
194 |
if(file.getZipError()!=UNZ_OK) { |
|
195 |
qWarning("file.close(): %d", file.getZipError()); |
|
196 |
return false; |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
zip.close(); |
|
201 |
||
202 |
return true; |
|
203 |
} |