author | Medo <smaxein@googlemail.com> |
Thu, 31 May 2012 18:54:40 +0200 | |
changeset 7160 | c42949cfdd92 |
parent 7015 | 08642c7da4a3 |
child 7823 | 46f115569045 |
permissions | -rw-r--r-- |
5271 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
6952 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
5271 | 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" |
6932 | 33 |
#include "DataManager.h" |
5271 | 34 |
|
5754 | 35 |
#include "quazip.h" |
5756 | 36 |
#include "quazipfile.h" |
5754 | 37 |
|
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
|
38 |
QLayout * PageDataDownload::bodyLayoutDefinition() |
5271 | 39 |
{ |
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
|
40 |
QGridLayout * pageLayout = new QGridLayout(); |
5271 | 41 |
pageLayout->setColumnStretch(0, 1); |
42 |
pageLayout->setColumnStretch(1, 1); |
|
43 |
pageLayout->setColumnStretch(2, 1); |
|
44 |
||
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
5754
diff
changeset
|
45 |
web = new DataBrowser(this); |
5271 | 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 |
|
5769 | 48 |
progressBarsLayout = new QVBoxLayout(); |
5754 | 49 |
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
|
50 |
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
|
51 |
} |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
52 |
|
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
|
53 |
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
|
54 |
{ |
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 |
connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(request(const QUrl&))); |
6932 | 56 |
connect(this, SIGNAL(goBack()), this, SLOT(onPageLeave())); |
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 |
} |
6009 | 58 |
|
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
|
59 |
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
|
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 |
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
|
62 |
|
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
|
63 |
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
|
64 |
// fetchList(); |
6932 | 65 |
|
66 |
m_contentDownloaded = false; |
|
5271 | 67 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
68 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
69 |
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
|
70 |
{ |
5768 | 71 |
QUrl finalUrl; |
72 |
if(url.host().isEmpty()) |
|
73 |
finalUrl = QUrl("http://www.hedgewars.org" + url.path()); |
|
74 |
else |
|
75 |
finalUrl = url; |
|
76 |
||
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
77 |
if(url.path().endsWith(".zip")) |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
78 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
79 |
qWarning() << "Download Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
80 |
QString fileName = QFileInfo(url.toString()).fileName(); |
5754 | 81 |
|
5768 | 82 |
QNetworkRequest newRequest(finalUrl); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
83 |
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
|
84 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
85 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
86 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
87 |
connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
88 |
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
|
89 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
90 |
QProgressBar *progressBar = new QProgressBar(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
91 |
progressBarsLayout->addWidget(progressBar); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
92 |
progressBars.insert(reply, progressBar); |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
93 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
94 |
else |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
95 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
96 |
qWarning() << "Page Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
97 |
|
5768 | 98 |
QNetworkRequest newRequest(finalUrl); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
99 |
|
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
100 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
101 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
102 |
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
103 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
104 |
} |
5746
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 |
|
5754 | 107 |
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
|
108 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
109 |
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
|
110 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
111 |
if(reply) |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
112 |
{ |
5766 | 113 |
QString html = QString::fromUtf8(reply->readAll()); |
5768 | 114 |
int begin = html.indexOf("<!-- BEGIN -->"); |
115 |
int end = html.indexOf("<!-- END -->"); |
|
116 |
if(begin != -1 && begin < end) |
|
117 |
{ |
|
118 |
html.truncate(end); |
|
119 |
html.remove(0, begin); |
|
120 |
} |
|
5764
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
121 |
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
|
122 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
123 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
124 |
|
5754 | 125 |
void PageDataDownload::fileDownloaded() |
126 |
{ |
|
127 |
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
|
128 |
|
5754 | 129 |
if(reply) |
130 |
{ |
|
131 |
QByteArray fileContents = reply->readAll(); |
|
132 |
QProgressBar *progressBar = progressBars.value(reply, 0); |
|
133 |
||
134 |
if(progressBar) |
|
135 |
{ |
|
136 |
progressBars.remove(reply); |
|
137 |
progressBar->deleteLater(); |
|
138 |
} |
|
5756 | 139 |
|
140 |
extractDataPack(&fileContents); |
|
5754 | 141 |
} |
142 |
} |
|
143 |
||
144 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
145 |
{ |
|
146 |
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
|
147 |
|
5754 | 148 |
if(reply) |
149 |
{ |
|
150 |
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
|
151 |
|
5754 | 152 |
if(progressBar) |
153 |
{ |
|
154 |
progressBar->setValue(bytesRecieved); |
|
155 |
progressBar->setMaximum(bytesTotal); |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
void PageDataDownload::fetchList() |
|
161 |
{ |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
162 |
request(QUrl("http://hedgewars.org/content.html")); |
5754 | 163 |
} |
5756 | 164 |
|
165 |
bool PageDataDownload::extractDataPack(QByteArray * buf) |
|
166 |
{ |
|
167 |
QBuffer buffer; |
|
168 |
buffer.setBuffer(buf); |
|
169 |
||
170 |
QuaZip zip; |
|
171 |
zip.setIoDevice(&buffer); |
|
172 |
if(!zip.open(QuaZip::mdUnzip)) |
|
173 |
{ |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
174 |
qWarning("testRead(): zip.open(): %d", zip.getZipError()); |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
175 |
return false; |
5756 | 176 |
} |
177 |
||
178 |
QuaZipFile file(&zip); |
|
179 |
||
5762 | 180 |
QDir extractDir(*cfgdir); |
181 |
extractDir.cd("Data"); |
|
182 |
||
5756 | 183 |
for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) |
184 |
{ |
|
185 |
if(!file.open(QIODevice::ReadOnly)) |
|
186 |
{ |
|
187 |
qWarning("file.open(): %d", file.getZipError()); |
|
188 |
return false; |
|
189 |
} |
|
190 |
||
191 |
||
192 |
QString fileName = file.getActualFileName(); |
|
5762 | 193 |
QString filePath = extractDir.filePath(fileName); |
5756 | 194 |
if (fileName.endsWith("/")) |
195 |
{ |
|
196 |
QFileInfo fi(filePath); |
|
197 |
QDir().mkpath(fi.filePath()); |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
198 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
199 |
else |
5756 | 200 |
{ |
5762 | 201 |
qDebug() << "Extracting" << filePath; |
5756 | 202 |
QFile out(filePath); |
203 |
if(!out.open(QFile::WriteOnly)) |
|
204 |
{ |
|
205 |
qWarning() << "out.open():" << out.errorString(); |
|
206 |
return false; |
|
207 |
} |
|
208 |
||
209 |
out.write(file.readAll()); |
|
210 |
||
211 |
out.close(); |
|
212 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
213 |
if(file.getZipError() != UNZ_OK) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
214 |
{ |
5756 | 215 |
qWarning("file.getFileName(): %d", file.getZipError()); |
216 |
return false; |
|
217 |
} |
|
218 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
219 |
if(!file.atEnd()) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
220 |
{ |
5756 | 221 |
qWarning("read all but not EOF"); |
222 |
return false; |
|
223 |
} |
|
6932 | 224 |
|
7015 | 225 |
if (this->isVisible()) |
226 |
m_contentDownloaded = true; |
|
227 |
else |
|
228 |
DataManager::instance().reload(); |
|
5756 | 229 |
} |
230 |
||
231 |
file.close(); |
|
232 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
233 |
if(file.getZipError()!=UNZ_OK) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
234 |
{ |
5756 | 235 |
qWarning("file.close(): %d", file.getZipError()); |
236 |
return false; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
zip.close(); |
|
241 |
||
242 |
return true; |
|
243 |
} |
|
6932 | 244 |
|
245 |
||
246 |
void PageDataDownload::onPageLeave() |
|
247 |
{ |
|
248 |
if (m_contentDownloaded) |
|
249 |
{ |
|
250 |
m_contentDownloaded = false; |
|
251 |
DataManager::instance().reload(); |
|
252 |
} |
|
253 |
} |