author | Xeli |
Sun, 18 Mar 2012 18:19:20 +0100 | |
changeset 6802 | 8701007d58c0 |
parent 6700 | e04da46ee43c |
child 6932 | 005393616925 |
permissions | -rw-r--r-- |
5271 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2006-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" |
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); |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
89 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
90 |
else |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
91 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
92 |
qWarning() << "Page Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
93 |
|
5768 | 94 |
QNetworkRequest newRequest(finalUrl); |
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 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
97 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
98 |
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
99 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
100 |
} |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
101 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
102 |
|
5754 | 103 |
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
|
104 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
105 |
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
|
106 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
107 |
if(reply) |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
108 |
{ |
5766 | 109 |
QString html = QString::fromUtf8(reply->readAll()); |
5768 | 110 |
int begin = html.indexOf("<!-- BEGIN -->"); |
111 |
int end = html.indexOf("<!-- END -->"); |
|
112 |
if(begin != -1 && begin < end) |
|
113 |
{ |
|
114 |
html.truncate(end); |
|
115 |
html.remove(0, begin); |
|
116 |
} |
|
5764
d785d7e23113
truncate page on comment tags, as suggested by unc0rr
nemo
parents:
5762
diff
changeset
|
117 |
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
|
118 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
119 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
120 |
|
5754 | 121 |
void PageDataDownload::fileDownloaded() |
122 |
{ |
|
123 |
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
|
124 |
|
5754 | 125 |
if(reply) |
126 |
{ |
|
127 |
QByteArray fileContents = reply->readAll(); |
|
128 |
QProgressBar *progressBar = progressBars.value(reply, 0); |
|
129 |
||
130 |
if(progressBar) |
|
131 |
{ |
|
132 |
progressBars.remove(reply); |
|
133 |
progressBar->deleteLater(); |
|
134 |
} |
|
5756 | 135 |
|
136 |
extractDataPack(&fileContents); |
|
5754 | 137 |
} |
138 |
} |
|
139 |
||
140 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
141 |
{ |
|
142 |
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
|
143 |
|
5754 | 144 |
if(reply) |
145 |
{ |
|
146 |
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
|
147 |
|
5754 | 148 |
if(progressBar) |
149 |
{ |
|
150 |
progressBar->setValue(bytesRecieved); |
|
151 |
progressBar->setMaximum(bytesTotal); |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
void PageDataDownload::fetchList() |
|
157 |
{ |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
158 |
request(QUrl("http://hedgewars.org/content.html")); |
5754 | 159 |
} |
5756 | 160 |
|
161 |
bool PageDataDownload::extractDataPack(QByteArray * buf) |
|
162 |
{ |
|
163 |
QBuffer buffer; |
|
164 |
buffer.setBuffer(buf); |
|
165 |
||
166 |
QuaZip zip; |
|
167 |
zip.setIoDevice(&buffer); |
|
168 |
if(!zip.open(QuaZip::mdUnzip)) |
|
169 |
{ |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
170 |
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
|
171 |
return false; |
5756 | 172 |
} |
173 |
||
174 |
QuaZipFile file(&zip); |
|
175 |
||
5762 | 176 |
QDir extractDir(*cfgdir); |
177 |
extractDir.cd("Data"); |
|
178 |
||
5756 | 179 |
for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) |
180 |
{ |
|
181 |
if(!file.open(QIODevice::ReadOnly)) |
|
182 |
{ |
|
183 |
qWarning("file.open(): %d", file.getZipError()); |
|
184 |
return false; |
|
185 |
} |
|
186 |
||
187 |
||
188 |
QString fileName = file.getActualFileName(); |
|
5762 | 189 |
QString filePath = extractDir.filePath(fileName); |
5756 | 190 |
if (fileName.endsWith("/")) |
191 |
{ |
|
192 |
QFileInfo fi(filePath); |
|
193 |
QDir().mkpath(fi.filePath()); |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
194 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
195 |
else |
5756 | 196 |
{ |
5762 | 197 |
qDebug() << "Extracting" << filePath; |
5756 | 198 |
QFile out(filePath); |
199 |
if(!out.open(QFile::WriteOnly)) |
|
200 |
{ |
|
201 |
qWarning() << "out.open():" << out.errorString(); |
|
202 |
return false; |
|
203 |
} |
|
204 |
||
205 |
out.write(file.readAll()); |
|
206 |
||
207 |
out.close(); |
|
208 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
209 |
if(file.getZipError() != UNZ_OK) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
210 |
{ |
5756 | 211 |
qWarning("file.getFileName(): %d", file.getZipError()); |
212 |
return false; |
|
213 |
} |
|
214 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
215 |
if(!file.atEnd()) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
216 |
{ |
5756 | 217 |
qWarning("read all but not EOF"); |
218 |
return false; |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
file.close(); |
|
223 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
224 |
if(file.getZipError()!=UNZ_OK) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
225 |
{ |
5756 | 226 |
qWarning("file.close(): %d", file.getZipError()); |
227 |
return false; |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
zip.close(); |
|
232 |
||
233 |
return true; |
|
234 |
} |