author | sheepluva |
Sun, 25 Sep 2011 16:09:55 +0200 | |
changeset 6022 | 8ed6e810051a |
parent 6009 | 14f6fc9869f2 |
child 6042 | 8b5345758f62 |
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 |
||
44 |
||
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
5754
diff
changeset
|
45 |
web = new DataBrowser(this); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
46 |
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
|
47 |
web->setOpenLinks(false); |
5271 | 48 |
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
|
49 |
|
5769 | 50 |
progressBarsLayout = new QVBoxLayout(); |
5754 | 51 |
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
|
52 |
|
5754 | 53 |
fetchList(); |
6009 | 54 |
|
55 |
||
56 |
BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true); |
|
57 |
connect(BtnBack, SIGNAL(clicked()), this, SIGNAL(goBack())); |
|
5271 | 58 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
59 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
60 |
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
|
61 |
{ |
5768 | 62 |
QUrl finalUrl; |
63 |
if(url.host().isEmpty()) |
|
64 |
finalUrl = QUrl("http://www.hedgewars.org" + url.path()); |
|
65 |
else |
|
66 |
finalUrl = url; |
|
67 |
||
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
68 |
if(url.path().endsWith(".zip")) |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
69 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
70 |
qWarning() << "Download Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
71 |
QString fileName = QFileInfo(url.toString()).fileName(); |
5754 | 72 |
|
5768 | 73 |
QNetworkRequest newRequest(finalUrl); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
74 |
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
|
75 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
76 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
77 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
78 |
connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
79 |
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
|
80 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
81 |
QProgressBar *progressBar = new QProgressBar(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
82 |
progressBarsLayout->addWidget(progressBar); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
83 |
progressBars.insert(reply, progressBar); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
84 |
} else |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
85 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
86 |
qWarning() << "Page Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
87 |
|
5768 | 88 |
QNetworkRequest newRequest(finalUrl); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
89 |
|
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
90 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
91 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
92 |
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
93 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
94 |
} |
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 |
|
5754 | 97 |
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
|
98 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
99 |
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
|
100 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
101 |
if(reply) |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
102 |
{ |
5766 | 103 |
QString html = QString::fromUtf8(reply->readAll()); |
5768 | 104 |
int begin = html.indexOf("<!-- BEGIN -->"); |
105 |
int end = html.indexOf("<!-- END -->"); |
|
106 |
if(begin != -1 && begin < end) |
|
107 |
{ |
|
108 |
html.truncate(end); |
|
109 |
html.remove(0, begin); |
|
110 |
} |
|
5764
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
111 |
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
|
112 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
113 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
114 |
|
5754 | 115 |
void PageDataDownload::fileDownloaded() |
116 |
{ |
|
117 |
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
|
118 |
|
5754 | 119 |
if(reply) |
120 |
{ |
|
121 |
QByteArray fileContents = reply->readAll(); |
|
122 |
QProgressBar *progressBar = progressBars.value(reply, 0); |
|
123 |
||
124 |
if(progressBar) |
|
125 |
{ |
|
126 |
progressBars.remove(reply); |
|
127 |
progressBar->deleteLater(); |
|
128 |
} |
|
5756 | 129 |
|
130 |
extractDataPack(&fileContents); |
|
5754 | 131 |
} |
132 |
} |
|
133 |
||
134 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
135 |
{ |
|
136 |
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
|
137 |
|
5754 | 138 |
if(reply) |
139 |
{ |
|
140 |
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
|
141 |
|
5754 | 142 |
if(progressBar) |
143 |
{ |
|
144 |
progressBar->setValue(bytesRecieved); |
|
145 |
progressBar->setMaximum(bytesTotal); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
void PageDataDownload::fetchList() |
|
151 |
{ |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
152 |
request(QUrl("http://hedgewars.org/content.html")); |
5754 | 153 |
} |
5756 | 154 |
|
155 |
bool PageDataDownload::extractDataPack(QByteArray * buf) |
|
156 |
{ |
|
157 |
QBuffer buffer; |
|
158 |
buffer.setBuffer(buf); |
|
159 |
||
160 |
QuaZip zip; |
|
161 |
zip.setIoDevice(&buffer); |
|
162 |
if(!zip.open(QuaZip::mdUnzip)) |
|
163 |
{ |
|
164 |
qWarning("testRead(): zip.open(): %d", zip.getZipError()); |
|
165 |
return false; |
|
166 |
} |
|
167 |
||
168 |
QuaZipFile file(&zip); |
|
169 |
||
5762 | 170 |
QDir extractDir(*cfgdir); |
171 |
extractDir.cd("Data"); |
|
172 |
||
5756 | 173 |
for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) |
174 |
{ |
|
175 |
if(!file.open(QIODevice::ReadOnly)) |
|
176 |
{ |
|
177 |
qWarning("file.open(): %d", file.getZipError()); |
|
178 |
return false; |
|
179 |
} |
|
180 |
||
181 |
||
182 |
QString fileName = file.getActualFileName(); |
|
5762 | 183 |
QString filePath = extractDir.filePath(fileName); |
5756 | 184 |
if (fileName.endsWith("/")) |
185 |
{ |
|
186 |
QFileInfo fi(filePath); |
|
187 |
QDir().mkpath(fi.filePath()); |
|
188 |
} else |
|
189 |
{ |
|
5762 | 190 |
qDebug() << "Extracting" << filePath; |
5756 | 191 |
QFile out(filePath); |
192 |
if(!out.open(QFile::WriteOnly)) |
|
193 |
{ |
|
194 |
qWarning() << "out.open():" << out.errorString(); |
|
195 |
return false; |
|
196 |
} |
|
197 |
||
198 |
out.write(file.readAll()); |
|
199 |
||
200 |
out.close(); |
|
201 |
||
202 |
if(file.getZipError() != UNZ_OK) { |
|
203 |
qWarning("file.getFileName(): %d", file.getZipError()); |
|
204 |
return false; |
|
205 |
} |
|
206 |
||
207 |
if(!file.atEnd()) { |
|
208 |
qWarning("read all but not EOF"); |
|
209 |
return false; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
file.close(); |
|
214 |
||
215 |
if(file.getZipError()!=UNZ_OK) { |
|
216 |
qWarning("file.close(): %d", file.getZipError()); |
|
217 |
return false; |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
zip.close(); |
|
222 |
||
223 |
return true; |
|
224 |
} |