QTfrontend/ui/widget/databrowser.cpp
author Wuzzy <Wuzzy2@mail.ru>
Sun, 15 Jul 2018 14:21:06 +0200
changeset 13491 4ef83bcb850b
parent 11835 6078cf0e4944
permissions -rw-r--r--
Clean up cMaxHHs and cMaxTeams a bit, get rid of hardcoded numbers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6948
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     1
/*
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     2
 * Hedgewars, a free turn based strategy game
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10108
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
6948
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     4
 *
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     8
 *
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    12
 * GNU General Public License for more details.
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    13
 *
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    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
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    17
 */
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    18
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    19
/**
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    20
 * @file
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    21
 * @brief DataBrowser class implementation
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    22
 */
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6616
diff changeset
    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
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    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
9af54df78b8a Fix a warning
unc0rr
parents: 5755
diff changeset
    45
    {
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    46
        if(resources.contains(name.toString()))
5755
a079b4dea081 Implement DataBrowser which downloads resources (images, css) on its own.
unc0rr
parents:
diff changeset
    47
        {
5780
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    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
11835
6078cf0e4944 http → https
nemo
parents: 11046
diff changeset
    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
9af54df78b8a Fix a warning
unc0rr
parents: 5755
diff changeset
    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
528dd4a14861 Support Qt version which have no qHash(QUrl)
unc0rr
parents: 5772
diff changeset
    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
082a068c078d Call update() so loaded images appear
unc0rr
parents: 5758
diff changeset
    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
}