author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 29 Nov 2018 20:37:40 +0100 | |
changeset 14348 | ffebbcc40c9b |
parent 11830 | 6078cf0e4944 |
permissions | -rw-r--r-- |
6948 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
6948 | 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
6948 | 17 |
*/ |
18 |
||
19 |
/** |
|
20 |
* @file |
|
21 |
* @brief DataBrowser class implementation |
|
22 |
*/ |
|
23 |
||
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
24 |
#include <QNetworkAccessManager> |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
25 |
#include <QNetworkRequest> |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
26 |
#include <QNetworkReply> |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
27 |
#include <QDebug> |
5780 | 28 |
#include <QUrl> |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
29 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
30 |
#include "databrowser.h" |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
31 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
32 |
const QNetworkRequest::Attribute typeAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 1); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
33 |
const QNetworkRequest::Attribute urlAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 2); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
34 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
35 |
DataBrowser::DataBrowser(QWidget *parent) : |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
36 |
QTextBrowser(parent) |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
37 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
38 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
39 |
manager = new QNetworkAccessManager(this); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
40 |
} |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
41 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
42 |
QVariant DataBrowser::loadResource(int type, const QUrl & name) |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
43 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
44 |
if(type == QTextDocument::ImageResource || type == QTextDocument::StyleSheetResource) |
5758 | 45 |
{ |
5780 | 46 |
if(resources.contains(name.toString())) |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
47 |
{ |
5780 | 48 |
return resources.take(name.toString()); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
49 |
} |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
50 |
else if(!requestedResources.contains(name.toString())) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
51 |
{ |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
52 |
qDebug() << "Requesting resource" << name.toString(); |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
53 |
requestedResources.insert(name.toString()); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
54 |
|
11830 | 55 |
QNetworkRequest newRequest(QUrl("https://www.hedgewars.org" + name.toString())); |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
56 |
newRequest.setAttribute(typeAttribute, type); |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
57 |
newRequest.setAttribute(urlAttribute, name); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
58 |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
59 |
QNetworkReply *reply = manager->get(newRequest); |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
60 |
connect(reply, SIGNAL(finished()), this, SLOT(resourceDownloaded())); |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6060
diff
changeset
|
61 |
} |
5758 | 62 |
} |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
63 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
64 |
return QVariant(); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
65 |
} |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
66 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
67 |
void DataBrowser::resourceDownloaded() |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
68 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
69 |
QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
70 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
71 |
if(reply) |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
72 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
73 |
int type = reply->request().attribute(typeAttribute).toInt(); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
74 |
QUrl url = reply->request().attribute(urlAttribute).toUrl(); |
5780 | 75 |
resources.insert(url.toString(), reply->readAll()); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
76 |
document()->addResource(type, reply->request().url(), QVariant()); |
5759 | 77 |
update(); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
78 |
} |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
79 |
} |