|
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> |
|
21 #include <QNetworkAccessManager> |
|
22 #include <QNetworkRequest> |
|
23 #include <QNetworkReply> |
|
24 #include <QFileInfo> |
|
25 #include <QFileDialog> |
|
26 #include <QDebug> |
|
27 #include <QProgressBar> |
|
28 #include <QBuffer> |
|
29 |
|
30 #include "pagedata.h" |
|
31 #include "databrowser.h" |
|
32 #include "hwconsts.h" |
|
33 |
|
34 #include "quazip.h" |
|
35 #include "quazipfile.h" |
|
36 |
|
37 QLayout * PageDataDownload::bodyLayoutDefinition() |
|
38 { |
|
39 QGridLayout * pageLayout = new QGridLayout(); |
|
40 pageLayout->setColumnStretch(0, 1); |
|
41 pageLayout->setColumnStretch(1, 1); |
|
42 pageLayout->setColumnStretch(2, 1); |
|
43 |
|
44 web = new DataBrowser(this); |
|
45 pageLayout->addWidget(web, 0, 0, 1, 3); |
|
46 |
|
47 progressBarsLayout = new QVBoxLayout(); |
|
48 pageLayout->addLayout(progressBarsLayout, 1, 0, 1, 3); |
|
49 return pageLayout; |
|
50 } |
|
51 |
|
52 void PageDataDownload::connectSignals() |
|
53 { |
|
54 connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(request(const QUrl&))); |
|
55 } |
|
56 |
|
57 PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent) |
|
58 { |
|
59 initPage(); |
|
60 |
|
61 web->setOpenLinks(false); |
|
62 fetchList(); |
|
63 } |
|
64 |
|
65 void PageDataDownload::request(const QUrl &url) |
|
66 { |
|
67 QUrl finalUrl; |
|
68 if(url.host().isEmpty()) |
|
69 finalUrl = QUrl("http://www.hedgewars.org" + url.path()); |
|
70 else |
|
71 finalUrl = url; |
|
72 |
|
73 if(url.path().endsWith(".zip")) |
|
74 { |
|
75 qWarning() << "Download Request" << url.toString(); |
|
76 QString fileName = QFileInfo(url.toString()).fileName(); |
|
77 |
|
78 QNetworkRequest newRequest(finalUrl); |
|
79 newRequest.setAttribute(QNetworkRequest::User, fileName); |
|
80 |
|
81 QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
|
82 QNetworkReply *reply = manager->get(newRequest); |
|
83 connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
|
84 connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); |
|
85 |
|
86 QProgressBar *progressBar = new QProgressBar(this); |
|
87 progressBarsLayout->addWidget(progressBar); |
|
88 progressBars.insert(reply, progressBar); |
|
89 } else |
|
90 { |
|
91 qWarning() << "Page Request" << url.toString(); |
|
92 |
|
93 QNetworkRequest newRequest(finalUrl); |
|
94 |
|
95 QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
|
96 QNetworkReply *reply = manager->get(newRequest); |
|
97 connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
|
98 } |
|
99 } |
|
100 |
|
101 |
|
102 void PageDataDownload::pageDownloaded() |
|
103 { |
|
104 QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
|
105 |
|
106 if(reply) |
|
107 { |
|
108 QString html = QString::fromUtf8(reply->readAll()); |
|
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 } |
|
116 web->setHtml(html); |
|
117 } |
|
118 } |
|
119 |
|
120 void PageDataDownload::fileDownloaded() |
|
121 { |
|
122 QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
|
123 |
|
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 } |
|
134 |
|
135 extractDataPack(&fileContents); |
|
136 } |
|
137 } |
|
138 |
|
139 void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
140 { |
|
141 QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
|
142 |
|
143 if(reply) |
|
144 { |
|
145 QProgressBar *progressBar = progressBars.value(reply, 0); |
|
146 |
|
147 if(progressBar) |
|
148 { |
|
149 progressBar->setValue(bytesRecieved); |
|
150 progressBar->setMaximum(bytesTotal); |
|
151 } |
|
152 } |
|
153 } |
|
154 |
|
155 void PageDataDownload::fetchList() |
|
156 { |
|
157 request(QUrl("http://hedgewars.org/content.html")); |
|
158 } |
|
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 |
|
175 QDir extractDir(*cfgdir); |
|
176 extractDir.cd("Data"); |
|
177 |
|
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(); |
|
188 QString filePath = extractDir.filePath(fileName); |
|
189 if (fileName.endsWith("/")) |
|
190 { |
|
191 QFileInfo fi(filePath); |
|
192 QDir().mkpath(fi.filePath()); |
|
193 } else |
|
194 { |
|
195 qDebug() << "Extracting" << filePath; |
|
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 } |