author | koda |
Sun, 16 Oct 2011 18:37:57 +0200 | |
branch | 0.9.16 |
changeset 6001 | 24b3a7ba3884 |
parent 5780 | 528dd4a14861 |
permissions | -rw-r--r-- |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
1 |
#include <QNetworkAccessManager> |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
2 |
#include <QNetworkRequest> |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
3 |
#include <QNetworkReply> |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
4 |
#include <QDebug> |
5780 | 5 |
#include <QUrl> |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
6 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
7 |
#include "databrowser.h" |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
8 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
9 |
const QNetworkRequest::Attribute typeAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 1); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
10 |
const QNetworkRequest::Attribute urlAttribute = (QNetworkRequest::Attribute)(QNetworkRequest::User + 2); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
11 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
12 |
DataBrowser::DataBrowser(QWidget *parent) : |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
13 |
QTextBrowser(parent) |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
14 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
15 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
16 |
manager = new QNetworkAccessManager(this); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
17 |
} |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
18 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
19 |
QVariant DataBrowser::loadResource(int type, const QUrl & name) |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
20 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
21 |
if(type == QTextDocument::ImageResource || type == QTextDocument::StyleSheetResource) |
5758 | 22 |
{ |
5780 | 23 |
if(resources.contains(name.toString())) |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
24 |
{ |
5780 | 25 |
return resources.take(name.toString()); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
26 |
} |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
27 |
else |
5780 | 28 |
if(!requestedResources.contains(name.toString())) |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
29 |
{ |
5772
c09c50efe8b5
Replace iterators with simple declarative foreach (not tested if it works)
unc0rr
parents:
5759
diff
changeset
|
30 |
qDebug() << "Requesting resource" << name.toString(); |
5780 | 31 |
requestedResources.insert(name.toString()); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
32 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
33 |
QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + name.toString())); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
34 |
newRequest.setAttribute(typeAttribute, type); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
35 |
newRequest.setAttribute(urlAttribute, name); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
36 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
37 |
QNetworkReply *reply = manager->get(newRequest); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
38 |
connect(reply, SIGNAL(finished()), this, SLOT(resourceDownloaded())); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
39 |
} |
5758 | 40 |
} |
5755
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 |
return QVariant(); |
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 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
45 |
void DataBrowser::resourceDownloaded() |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
46 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
47 |
QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender()); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
48 |
|
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
49 |
if(reply) |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
50 |
{ |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
51 |
int type = reply->request().attribute(typeAttribute).toInt(); |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
52 |
QUrl url = reply->request().attribute(urlAttribute).toUrl(); |
5780 | 53 |
resources.insert(url.toString(), reply->readAll()); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
54 |
document()->addResource(type, reply->request().url(), QVariant()); |
5759 | 55 |
update(); |
5755
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
56 |
} |
a079b4dea081
Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff
changeset
|
57 |
} |