Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
305 KeyOz 1
/*
2
*
3
* This file is part of QMapControl,
4
* an open-source cross-platform map widget
5
*
6
* Copyright (C) 2007 - 2008 Kai Winter
7
*
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
* GNU Lesser General Public License for more details.
17
*
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
20
*
21
* Contact e-mail: kaiwinter@gmx.de
22
* Program URL   : http://qmapcontrol.sourceforge.net/
23
*
24
*/
25
 
26
#include "mapnetwork.h"
27
#include <QWaitCondition>
28
namespace qmapcontrol
29
{
30
    MapNetwork::MapNetwork(ImageManager* parent)
31
        :parent(parent), http(new QHttp(this)), loaded(0)
32
    {
33
        connect(http, SIGNAL(requestFinished(int, bool)),
34
                this, SLOT(requestFinished(int, bool)));
35
    }
36
 
37
    MapNetwork::~MapNetwork()
38
    {
39
        http->clearPendingRequests();
40
        delete http;
41
    }
42
 
43
 
44
    void MapNetwork::loadImage(const QString& host, const QString& url)
45
    {
46
        // qDebug() << "getting: " << QString(host).append(url);
47
        // http->setHost(host);
48
        // int getId = http->get(url);
49
 
50
        http->setHost(host);
51
        QHttpRequestHeader header("GET", url);
52
        header.setValue("User-Agent", "Mozilla");
53
        header.setValue("Host", host);
54
        int getId = http->request(header);
55
 
56
        if (vectorMutex.tryLock())
57
        {
58
            loadingMap[getId] = url;
59
            vectorMutex.unlock();
60
        }
61
    }
62
 
63
    void MapNetwork::requestFinished(int id, bool error)
64
    {
65
        // sleep(1);
66
        // qDebug() << "MapNetwork::requestFinished" << http->state() << ", id: " << id;
67
        if (error)
68
        {
69
            qDebug() << "network error: " << http->errorString();
70
            //restart query
71
 
72
        }
73
        else if (vectorMutex.tryLock())
74
        {
75
            // check if id is in map?
76
            if (loadingMap.contains(id))
77
            {
78
 
79
                QString url = loadingMap[id];
80
                loadingMap.remove(id);
81
                vectorMutex.unlock();
82
                // qDebug() << "request finished for id: " << id << ", belongs to: " << notifier.url << endl;
83
                QByteArray ax;
84
 
85
                if (http->bytesAvailable()>0)
86
                {
87
                    QPixmap pm;
88
                    ax = http->readAll();
89
 
90
                    if (pm.loadFromData(ax))
91
                    {
92
                        loaded += pm.size().width()*pm.size().height()*pm.depth()/8/1024;
93
                        // qDebug() << "Network loaded: " << (loaded);
94
                        parent->receivedImage(pm, url);
95
                    }
96
                    else
97
                    {
98
                        qDebug() << "NETWORK_PIXMAP_ERROR: " << ax;
99
                    }
100
                }
101
 
102
            }
103
            else
104
                vectorMutex.unlock();
105
 
106
        }
107
        if (loadingMap.size() == 0)
108
        {
109
            // qDebug () << "all loaded";
110
            parent->loadingQueueEmpty();
111
        }
112
    }
113
 
114
    void MapNetwork::abortLoading()
115
    {
116
        http->clearPendingRequests();
117
        // http->abort();
118
        if (vectorMutex.tryLock())
119
        {
120
            loadingMap.clear();
121
            vectorMutex.unlock();
122
        }
123
    }
124
 
125
    bool MapNetwork::imageIsLoading(QString url)
126
    {
127
        return loadingMap.values().contains(url);
128
    }
129
 
130
    void MapNetwork::setProxy(QString host, int port)
131
    {
132
#ifndef Q_WS_QWS
133
        // do not set proxy on qt/extended
134
        http->setProxy(host, port);
135
#endif
136
    }
137
}