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 PageDataDownload::PageDataDownload(QWidget* parent) : AbstractPage(parent) |
|
38 { |
|
39 QGridLayout * pageLayout = new QGridLayout(this); |
|
40 pageLayout->setColumnStretch(0, 1); |
|
41 pageLayout->setColumnStretch(1, 1); |
|
42 pageLayout->setColumnStretch(2, 1); |
|
43 |
|
44 |
|
45 web = new DataBrowser(this); |
|
46 connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(request(const QUrl&))); |
|
47 web->setOpenLinks(false); |
|
48 pageLayout->addWidget(web, 0, 0, 1, 3); |
|
49 |
|
50 progressBarsLayout = new QVBoxLayout(); |
|
51 pageLayout->addLayout(progressBarsLayout, 1, 0, 1, 3); |
|
52 |
|
53 fetchList(); |
|
54 |
|
55 |
|
56 BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true); |
|
57 connect(BtnBack, SIGNAL(clicked()), this, SIGNAL(goBack())); |
|
58 } |
|
59 |
|
60 void PageDataDownload::request(const QUrl &url) |
|
61 { |
|
62 QUrl finalUrl; |
|
63 if(url.host().isEmpty()) |
|
64 finalUrl = QUrl("http://www.hedgewars.org" + url.path()); |
|
65 else |
|
66 finalUrl = url; |
|
67 |
|
68 if(url.path().endsWith(".zip")) |
|
69 { |
|
70 qWarning() << "Download Request" << url.toString(); |
|
71 QString fileName = QFileInfo(url.toString()).fileName(); |
|
72 |
|
73 QNetworkRequest newRequest(finalUrl); |
|
74 newRequest.setAttribute(QNetworkRequest::User, fileName); |
|
75 |
|
76 QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
|
77 QNetworkReply *reply = manager->get(newRequest); |
|
78 connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
|
79 connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); |
|
80 |
|
81 QProgressBar *progressBar = new QProgressBar(this); |
|
82 progressBarsLayout->addWidget(progressBar); |
|
83 progressBars.insert(reply, progressBar); |
|
84 } else |
|
85 { |
|
86 qWarning() << "Page Request" << url.toString(); |
|
87 |
|
88 QNetworkRequest newRequest(finalUrl); |
|
89 |
|
90 QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
|
91 QNetworkReply *reply = manager->get(newRequest); |
|
92 connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
|
93 } |
|
94 } |
|
95 |
|
96 |
|
97 void PageDataDownload::pageDownloaded() |
|
98 { |
|
99 QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
|
100 |
|
101 if(reply) |
|
102 { |
|
103 QString html = QString::fromUtf8(reply->readAll()); |
|
104 int begin = html.indexOf("<!-- BEGIN -->"); |
|
105 int end = html.indexOf("<!-- END -->"); |
|
106 if(begin != -1 && begin < end) |
|
107 { |
|
108 html.truncate(end); |
|
109 html.remove(0, begin); |
|
110 } |
|
111 web->setHtml(html); |
|
112 } |
|
113 } |
|
114 |
|
115 void PageDataDownload::fileDownloaded() |
|
116 { |
|
117 QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
|
118 |
|
119 if(reply) |
|
120 { |
|
121 QByteArray fileContents = reply->readAll(); |
|
122 QProgressBar *progressBar = progressBars.value(reply, 0); |
|
123 |
|
124 if(progressBar) |
|
125 { |
|
126 progressBars.remove(reply); |
|
127 progressBar->deleteLater(); |
|
128 } |
|
129 |
|
130 extractDataPack(&fileContents); |
|
131 } |
|
132 } |
|
133 |
|
134 void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
135 { |
|
136 QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
|
137 |
|
138 if(reply) |
|
139 { |
|
140 QProgressBar *progressBar = progressBars.value(reply, 0); |
|
141 |
|
142 if(progressBar) |
|
143 { |
|
144 progressBar->setValue(bytesRecieved); |
|
145 progressBar->setMaximum(bytesTotal); |
|
146 } |
|
147 } |
|
148 } |
|
149 |
|
150 void PageDataDownload::fetchList() |
|
151 { |
|
152 request(QUrl("http://hedgewars.org/content.html")); |
|
153 } |
|
154 |
|
155 bool PageDataDownload::extractDataPack(QByteArray * buf) |
|
156 { |
|
157 QBuffer buffer; |
|
158 buffer.setBuffer(buf); |
|
159 |
|
160 QuaZip zip; |
|
161 zip.setIoDevice(&buffer); |
|
162 if(!zip.open(QuaZip::mdUnzip)) |
|
163 { |
|
164 qWarning("testRead(): zip.open(): %d", zip.getZipError()); |
|
165 return false; |
|
166 } |
|
167 |
|
168 QuaZipFile file(&zip); |
|
169 |
|
170 QDir extractDir(*cfgdir); |
|
171 extractDir.cd("Data"); |
|
172 |
|
173 for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) |
|
174 { |
|
175 if(!file.open(QIODevice::ReadOnly)) |
|
176 { |
|
177 qWarning("file.open(): %d", file.getZipError()); |
|
178 return false; |
|
179 } |
|
180 |
|
181 |
|
182 QString fileName = file.getActualFileName(); |
|
183 QString filePath = extractDir.filePath(fileName); |
|
184 if (fileName.endsWith("/")) |
|
185 { |
|
186 QFileInfo fi(filePath); |
|
187 QDir().mkpath(fi.filePath()); |
|
188 } else |
|
189 { |
|
190 qDebug() << "Extracting" << filePath; |
|
191 QFile out(filePath); |
|
192 if(!out.open(QFile::WriteOnly)) |
|
193 { |
|
194 qWarning() << "out.open():" << out.errorString(); |
|
195 return false; |
|
196 } |
|
197 |
|
198 out.write(file.readAll()); |
|
199 |
|
200 out.close(); |
|
201 |
|
202 if(file.getZipError() != UNZ_OK) { |
|
203 qWarning("file.getFileName(): %d", file.getZipError()); |
|
204 return false; |
|
205 } |
|
206 |
|
207 if(!file.atEnd()) { |
|
208 qWarning("read all but not EOF"); |
|
209 return false; |
|
210 } |
|
211 } |
|
212 |
|
213 file.close(); |
|
214 |
|
215 if(file.getZipError()!=UNZ_OK) { |
|
216 qWarning("file.close(): %d", file.getZipError()); |
|
217 return false; |
|
218 } |
|
219 } |
|
220 |
|
221 zip.close(); |
|
222 |
|
223 return true; |
|
224 } |
|