QTfrontend/databrowser.cpp
author nemo
Sun, 04 Sep 2011 12:34:43 -0400
changeset 5766 ca9c7551bc8a
parent 5759 082a068c078d
child 5772 c09c50efe8b5
permissions -rw-r--r--
oops
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9af54df78b8a Fix a warning
unc0rr
parents: 5755
diff changeset
    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
            {
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    29
                requestedResources.insert(name);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    30
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    31
                QNetworkRequest newRequest(QUrl("http://www.hedgewars.org" + name.toString()));
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    32
                newRequest.setAttribute(typeAttribute, type);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    33
                newRequest.setAttribute(urlAttribute, name);
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
                QNetworkReply *reply = manager->get(newRequest);
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    36
                connect(reply, SIGNAL(finished()), this, SLOT(resourceDownloaded()));
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    37
            }
5758
9af54df78b8a Fix a warning
unc0rr
parents: 5755
diff changeset
    38
    }
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    39
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    40
    return QVariant();
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
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    43
void DataBrowser::resourceDownloaded()
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
    QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
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
    if(reply)
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
        int type = reply->request().attribute(typeAttribute).toInt();
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    50
        QUrl url = reply->request().attribute(urlAttribute).toUrl();
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    51
        resources.insert(url, reply->readAll());
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    52
        document()->addResource(type, reply->request().url(), QVariant());
5759
082a068c078d Call update() so loaded images appear
unc0rr
parents: 5758
diff changeset
    53
        update();
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    54
    }
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    55
}