author | nemo |
Sun, 11 Sep 2011 10:22:09 -0400 | |
changeset 5855 | 74c621e12baa |
parent 5769 | 09aca6e1317a |
child 5999 | dad168522e3b |
child 6009 | 14f6fc9869f2 |
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); |
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 | 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 | 51 |
progressBarsLayout = new QVBoxLayout(); |
5754 | 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 |
|
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 | 59 |
QUrl finalUrl; |
60 |
if(url.host().isEmpty()) |
|
61 |
finalUrl = QUrl("http://www.hedgewars.org" + url.path()); |
|
62 |
else |
|
63 |
finalUrl = url; |
|
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 | 69 |
|
5768 | 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 | 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 | 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 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
98 |
if(reply) |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
99 |
{ |
5766 | 100 |
QString html = QString::fromUtf8(reply->readAll()); |
5768 | 101 |
int begin = html.indexOf("<!-- BEGIN -->"); |
102 |
int end = html.indexOf("<!-- END -->"); |
|
103 |
if(begin != -1 && begin < end) |
|
104 |
{ |
|
105 |
html.truncate(end); |
|
106 |
html.remove(0, begin); |
|
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 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
110 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
111 |
|
5754 | 112 |
void PageDataDownload::fileDownloaded() |
113 |
{ |
|
114 |
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
|
115 |
|
5754 | 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 |
} |
|
5756 | 126 |
|
127 |
extractDataPack(&fileContents); |
|
5754 | 128 |
} |
129 |
} |
|
130 |
||
131 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
132 |
{ |
|
133 |
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
|
134 |
|
5754 | 135 |
if(reply) |
136 |
{ |
|
137 |
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
|
138 |
|
5754 | 139 |
if(progressBar) |
140 |
{ |
|
141 |
progressBar->setValue(bytesRecieved); |
|
142 |
progressBar->setMaximum(bytesTotal); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
void PageDataDownload::fetchList() |
|
148 |
{ |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
149 |
request(QUrl("http://hedgewars.org/content.html")); |
5754 | 150 |
} |
5756 | 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 |
||
5762 | 167 |
QDir extractDir(*cfgdir); |
168 |
extractDir.cd("Data"); |
|
169 |
||
5756 | 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(); |
|
5762 | 180 |
QString filePath = extractDir.filePath(fileName); |
5756 | 181 |
if (fileName.endsWith("/")) |
182 |
{ |
|
183 |
QFileInfo fi(filePath); |
|
184 |
QDir().mkpath(fi.filePath()); |
|
185 |
} else |
|
186 |
{ |
|
5762 | 187 |
qDebug() << "Extracting" << filePath; |
5756 | 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 |
} |