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