author | koda |
Sun, 09 Jun 2013 12:22:53 +0200 | |
branch | webgl |
changeset 9160 | fc46e75f6b72 |
parent 9080 | 9b42757d7e71 |
child 9829 | 1804dd141015 |
permissions | -rw-r--r-- |
5271 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
9080 | 3 |
* Copyright (c) 2004-2013 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" |
8055
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
34 |
#include "FileEngine.h" |
5754 | 35 |
|
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
|
36 |
QLayout * PageDataDownload::bodyLayoutDefinition() |
5271 | 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 |
QGridLayout * pageLayout = new QGridLayout(); |
5271 | 39 |
pageLayout->setColumnStretch(0, 1); |
40 |
pageLayout->setColumnStretch(1, 1); |
|
41 |
pageLayout->setColumnStretch(2, 1); |
|
42 |
||
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
5754
diff
changeset
|
43 |
web = new DataBrowser(this); |
5271 | 44 |
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
|
45 |
|
5769 | 46 |
progressBarsLayout = new QVBoxLayout(); |
5754 | 47 |
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
|
48 |
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
|
49 |
} |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
50 |
|
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
|
51 |
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
|
52 |
{ |
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 |
connect(web, SIGNAL(anchorClicked(QUrl)), this, SLOT(request(const QUrl&))); |
6932 | 54 |
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
|
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(); |
8876
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
63 |
web->setHtml(QString( |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
64 |
"<center><h2>Hedgewars Downloadable Content</h2><br><br>" |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
65 |
"<i>%1</i></center>") |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
66 |
.arg(tr("Loading, please wait."))); |
6932 | 67 |
m_contentDownloaded = false; |
5271 | 68 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
69 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
70 |
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
|
71 |
{ |
5768 | 72 |
QUrl finalUrl; |
73 |
if(url.host().isEmpty()) |
|
74 |
finalUrl = QUrl("http://www.hedgewars.org" + url.path()); |
|
75 |
else |
|
76 |
finalUrl = url; |
|
77 |
||
8059 | 78 |
if(url.path().endsWith(".hwp") || url.path().endsWith(".zip")) |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
79 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
80 |
qWarning() << "Download Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
81 |
QString fileName = QFileInfo(url.toString()).fileName(); |
5754 | 82 |
|
5768 | 83 |
QNetworkRequest newRequest(finalUrl); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
84 |
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
|
85 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
86 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
87 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
88 |
connect(reply, SIGNAL(finished()), this, SLOT(fileDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
89 |
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
|
90 |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
91 |
QProgressBar *progressBar = new QProgressBar(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
92 |
progressBarsLayout->addWidget(progressBar); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
93 |
progressBars.insert(reply, progressBar); |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
94 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6384
diff
changeset
|
95 |
else |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
96 |
{ |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
97 |
qWarning() << "Page Request" << url.toString(); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
98 |
|
5768 | 99 |
QNetworkRequest newRequest(finalUrl); |
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
100 |
|
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
101 |
QNetworkAccessManager *manager = new QNetworkAccessManager(this); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
102 |
QNetworkReply *reply = manager->get(newRequest); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
103 |
connect(reply, SIGNAL(finished()), this, SLOT(pageDownloaded())); |
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
104 |
} |
5567
44c9a577b082
Tiny bit of progress on download page - hooked it up to "Info" button for now.
nemo
parents:
5271
diff
changeset
|
105 |
} |
5746
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 |
|
5754 | 108 |
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
|
109 |
{ |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
110 |
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
|
111 |
|
8876
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
112 |
if (reply && (reply->error() == QNetworkReply::NoError)) { |
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); |
8876
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
122 |
} else |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
123 |
web->setHtml(QString( |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
124 |
"<center><h2>Hedgewars Downloadable Content</h2><br><br>" |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
125 |
"<p><i><h4>%1</i></h4></p></center>") |
bd8df772dbe6
reintroduce the notification message in case you access dlc page without internet connection
koda
parents:
8059
diff
changeset
|
126 |
.arg(tr("This page requires an internet connection."))); |
5746
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
127 |
} |
fbc52bb92cad
Use QTextBrowser instead of WebKit for downloadable contents page (currently only shows the page itself)
unc0rr
parents:
5582
diff
changeset
|
128 |
|
5754 | 129 |
void PageDataDownload::fileDownloaded() |
130 |
{ |
|
131 |
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
|
132 |
|
5754 | 133 |
if(reply) |
134 |
{ |
|
135 |
QProgressBar *progressBar = progressBars.value(reply, 0); |
|
136 |
||
137 |
if(progressBar) |
|
138 |
{ |
|
139 |
progressBars.remove(reply); |
|
140 |
progressBar->deleteLater(); |
|
141 |
} |
|
5756 | 142 |
|
8055
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
143 |
QDir extractDir(*cfgdir); |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
144 |
extractDir.cd("Data"); |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
145 |
|
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
146 |
QString fileName = extractDir.filePath(QFileInfo(reply->url().path()).fileName()); |
8059 | 147 |
if(fileName.endsWith(".zip")) |
148 |
fileName = fileName.left(fileName.length() - 4) + ".hwp"; |
|
8055
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
149 |
|
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
150 |
QFile out(fileName); |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
151 |
if(!out.open(QFile::WriteOnly)) |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
152 |
{ |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
153 |
qWarning() << "out.open():" << out.errorString(); |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
154 |
return ; |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
155 |
} |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
156 |
|
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
157 |
out.write(reply->readAll()); |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
158 |
|
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
159 |
out.close(); |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
160 |
|
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
161 |
// now mount it |
04dd8b7fb605
- Download .hwp files into Data folder and mount them immediately (yes, could cause problems in case of overlapped contents)
unc0rr
parents:
7824
diff
changeset
|
162 |
FileEngineHandler::mount(fileName); |
5754 | 163 |
} |
164 |
} |
|
165 |
||
166 |
void PageDataDownload::downloadProgress(qint64 bytesRecieved, qint64 bytesTotal) |
|
167 |
{ |
|
168 |
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
|
169 |
|
5754 | 170 |
if(reply) |
171 |
{ |
|
172 |
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
|
173 |
|
5754 | 174 |
if(progressBar) |
175 |
{ |
|
176 |
progressBar->setValue(bytesRecieved); |
|
177 |
progressBar->setMaximum(bytesTotal); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
void PageDataDownload::fetchList() |
|
183 |
{ |
|
5765
e87f6756773e
Allow surfing, download and extract only .zip files
unc0rr
parents:
5764
diff
changeset
|
184 |
request(QUrl("http://hedgewars.org/content.html")); |
5754 | 185 |
} |
5756 | 186 |
|
6932 | 187 |
|
188 |
void PageDataDownload::onPageLeave() |
|
189 |
{ |
|
190 |
if (m_contentDownloaded) |
|
191 |
{ |
|
192 |
m_contentDownloaded = false; |
|
7824 | 193 |
//DataManager::instance().reload(); |
6932 | 194 |
} |
195 |
} |