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