author | unc0rr |
Sun, 04 Sep 2011 14:44:13 +0400 | |
changeset 5756 | b451fd21ff4c |
parent 5755 | a079b4dea081 |
child 5762 | 0d6a338bf377 |
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" |
5271 | 32 |
|
5754 | 33 |
#include "quazip.h" |
5756 | 34 |
#include "quazipfile.h" |
5754 | 35 |
|
5271 | 36 |
PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent) |
37 |
{ |
|
38 |
QGridLayout * pageLayout = new QGridLayout(this); |
|
39 |
pageLayout->setColumnStretch(0, 1); |
|
40 |
pageLayout->setColumnStretch(1, 1); |
|
41 |
pageLayout->setColumnStretch(2, 1); |
|
42 |
||
5754 | 43 |
BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true); |
5271 | 44 |
|
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
5754
diff
changeset
|
45 |
web = new DataBrowser(this); |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
46 |
connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(install(const QUrl&))); |
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 |
|
5754 | 50 |
progressBarsLayout = new QVBoxLayout(this); |
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(); |
5271 | 54 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
55 |
|
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
56 |
void PageDataDownload::install(const QUrl &url) |
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
57 |
{ |
5754 | 58 |
qWarning() << "Download Request" << url.toString(); |
59 |
QString fileName = QFileInfo(url.toString()).fileName(); |
|
60 |
||
5756 | 61 |
QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + url.toString())); |
5754 | 62 |
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
|
63 |
|
5754 | 64 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
65 |
QNetworkReply *reply = manager->get(newRequest); |
|
66 |
connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
|
67 |
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
|
68 |
|
5754 | 69 |
QProgressBar *progressBar = new QProgressBar(this); |
70 |
progressBarsLayout->addWidget(progressBar); |
|
71 |
progressBars.insert(reply, progressBar); |
|
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
72 |
} |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
73 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
74 |
|
5754 | 75 |
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
|
76 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
77 |
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
|
78 |
|
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
79 |
if(reply) |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
80 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
81 |
web->setHtml(QString::fromUtf8(reply->readAll())); |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
82 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
83 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
84 |
|
5754 | 85 |
void PageDataDownload::fileDownloaded() |
86 |
{ |
|
87 |
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
|
88 |
|
5754 | 89 |
if(reply) |
90 |
{ |
|
91 |
QByteArray fileContents = reply->readAll(); |
|
92 |
QProgressBar *progressBar = progressBars.value(reply, 0); |
|
93 |
||
94 |
if(progressBar) |
|
95 |
{ |
|
96 |
progressBars.remove(reply); |
|
97 |
progressBar->deleteLater(); |
|
98 |
} |
|
5756 | 99 |
|
100 |
extractDataPack(&fileContents); |
|
5754 | 101 |
} |
102 |
} |
|
103 |
||
104 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
105 |
{ |
|
106 |
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
|
107 |
|
5754 | 108 |
if(reply) |
109 |
{ |
|
110 |
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
|
111 |
|
5754 | 112 |
if(progressBar) |
113 |
{ |
|
114 |
progressBar->setValue(bytesRecieved); |
|
115 |
progressBar->setMaximum(bytesTotal); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
void PageDataDownload::fetchList() |
|
121 |
{ |
|
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
5754
diff
changeset
|
122 |
QNetworkRequest newRequest(QUrl("http://hedgewars.org/node/2833")); |
5754 | 123 |
|
124 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
|
125 |
QNetworkReply *reply = manager->get(newRequest); |
|
126 |
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
|
127 |
} |
|
5756 | 128 |
|
129 |
bool PageDataDownload::extractDataPack(QByteArray * buf) |
|
130 |
{ |
|
131 |
QBuffer buffer; |
|
132 |
buffer.setBuffer(buf); |
|
133 |
||
134 |
QuaZip zip; |
|
135 |
zip.setIoDevice(&buffer); |
|
136 |
if(!zip.open(QuaZip::mdUnzip)) |
|
137 |
{ |
|
138 |
qWarning("testRead(): zip.open(): %d", zip.getZipError()); |
|
139 |
return false; |
|
140 |
} |
|
141 |
||
142 |
QuaZipFile file(&zip); |
|
143 |
||
144 |
for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) |
|
145 |
{ |
|
146 |
if(!file.open(QIODevice::ReadOnly)) |
|
147 |
{ |
|
148 |
qWarning("file.open(): %d", file.getZipError()); |
|
149 |
return false; |
|
150 |
} |
|
151 |
||
152 |
||
153 |
QString fileName = file.getActualFileName(); |
|
154 |
QString filePath = QDir::tempPath() + "/" + fileName; |
|
155 |
if (fileName.endsWith("/")) |
|
156 |
{ |
|
157 |
QFileInfo fi(filePath); |
|
158 |
QDir().mkpath(fi.filePath()); |
|
159 |
} else |
|
160 |
{ |
|
161 |
qDebug() << filePath; |
|
162 |
QFile out(filePath); |
|
163 |
if(!out.open(QFile::WriteOnly)) |
|
164 |
{ |
|
165 |
qWarning() << "out.open():" << out.errorString(); |
|
166 |
return false; |
|
167 |
} |
|
168 |
||
169 |
out.write(file.readAll()); |
|
170 |
||
171 |
out.close(); |
|
172 |
||
173 |
if(file.getZipError() != UNZ_OK) { |
|
174 |
qWarning("file.getFileName(): %d", file.getZipError()); |
|
175 |
return false; |
|
176 |
} |
|
177 |
||
178 |
if(!file.atEnd()) { |
|
179 |
qWarning("read all but not EOF"); |
|
180 |
return false; |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
file.close(); |
|
185 |
||
186 |
if(file.getZipError()!=UNZ_OK) { |
|
187 |
qWarning("file.close(): %d", file.getZipError()); |
|
188 |
return false; |
|
189 |
} |
|
190 |
} |
|
191 |
||
192 |
zip.close(); |
|
193 |
||
194 |
return true; |
|
195 |
} |