You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
231 lines
6.7 KiB
231 lines
6.7 KiB
/*
|
|
*
|
|
* This file is part of QMapControl,
|
|
* an open-source cross-platform map widget
|
|
*
|
|
* Copyright (C) 2007 - 2008 Kai Winter
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* Contact e-mail: kaiwinter@gmx.de
|
|
* Program URL : http://qmapcontrol.sourceforge.net/
|
|
*
|
|
*/
|
|
|
|
#include "mapnetwork.h"
|
|
#include <QtNetwork/QNetworkRequest>
|
|
#include <QtCore/QUrl>
|
|
#include <QtCore/QMapIterator>
|
|
#include <QtCore/QWaitCondition>
|
|
|
|
#include <QtCore/QMutexLocker>
|
|
|
|
MapNetwork::MapNetwork(ImageManager* parent){
|
|
loaded = 0;
|
|
m_pImageManager = parent;
|
|
m_bNetworkActive = false;
|
|
m_bCacheEnabled = false;
|
|
m_pHttpAccessManager = curlRequestsManager::getInstance();
|
|
}
|
|
|
|
MapNetwork::~MapNetwork(){
|
|
//curlRequest *tRequest;
|
|
//foreach(tRequest, m_lRequestsList){
|
|
|
|
//if (tReply->isRunning()){
|
|
// tReply->abort();
|
|
//}
|
|
//tReply->deleteLater();
|
|
// delete tRequest;
|
|
// tRequest = NULL;
|
|
//};
|
|
|
|
//m_pHttpAccessManager->deleteLater();
|
|
delete m_pHttpAccessManager;
|
|
m_pHttpAccessManager = NULL;
|
|
}
|
|
|
|
void MapNetwork::loadImage(const QString& host, const QString& url){
|
|
QString hostName = host;
|
|
QString portNumber = QString("80");
|
|
QRegExp r(".:.");
|
|
if(r.indexIn(host) >= 0){
|
|
QStringList s = host.split(":");
|
|
hostName = s.at(0);
|
|
portNumber = s.at(1);
|
|
};
|
|
|
|
QString finalUrl = QString("http://%1:%2%3").arg(hostName).arg(portNumber).arg(url);
|
|
char HostPart[64];
|
|
char PathPart[256];
|
|
sprintf(HostPart, "http://%s:%s", hostName.toStdString().c_str(), portNumber.toStdString().c_str());
|
|
sprintf(PathPart, "%s", url.toStdString().c_str());
|
|
|
|
if (m_bCacheEnabled){
|
|
QIODevice *QIO = m_pDiskCache->data(QUrl(finalUrl));
|
|
if (QIO != NULL){
|
|
qint64 size;
|
|
size = QIO->bytesAvailable();
|
|
if (size){
|
|
QPixmap pm;
|
|
QByteArray ax;
|
|
|
|
QIO->open(QIODevice::ReadOnly);
|
|
ax = QIO->readAll();
|
|
QIO->close();
|
|
|
|
if (pm.loadFromData(ax) && pm.size().width() > 1 && pm.size().height() > 1){
|
|
loaded += pm.size().width()*pm.size().height()*pm.depth() / 8 / 1024;
|
|
//qDebug() << "Network loaded: " << me->loaded << " width:" << pm.size().width() << " height:" << pm.size().height();
|
|
m_pImageManager->receivedImage(pm, QString(PathPart));
|
|
}
|
|
else {
|
|
m_pImageManager->fetchFailed(QString(PathPart));
|
|
};
|
|
if (loadQueueSize() == 0){
|
|
//qDebug () << "all loaded";
|
|
m_pImageManager->loadingQueueEmpty();
|
|
}
|
|
return;
|
|
};
|
|
};
|
|
};
|
|
|
|
curlRequest *tRequest = new curlRequest(HostPart, PathPart);
|
|
|
|
if (tRequest != NULL){
|
|
tRequest->setHandler(this, &MapNetwork::requestFinished, tRequest);
|
|
if (m_pHttpAccessManager->get(tRequest)){
|
|
QMutexLocker lock(&vectorMutex);
|
|
//m_lRequestsList.append(tRequest);
|
|
m_mLoadingMap.insert(url,finalUrl);
|
|
} else {
|
|
qDebug() << "Failed request: " << finalUrl;
|
|
};
|
|
};
|
|
};
|
|
|
|
//void MapNetwork::requestFinished(QNetworkReply *reply){
|
|
void *MapNetwork::requestFinished( void *ptr1, void *ptr2 ){
|
|
if (!ptr1) return NULL;
|
|
if (!ptr2) return NULL;
|
|
MapNetwork *me;
|
|
curlRequest *rr;
|
|
me = (MapNetwork*)ptr1;
|
|
rr = (curlRequest *)ptr2;
|
|
//qDebug() << "MapNetwork::requestFinished" << rr->getPath();
|
|
QString id = rr->getPath();
|
|
// check if id is in map?
|
|
bool idInMap = false;
|
|
QString url;
|
|
if( rr->isFinished() ){
|
|
QMutexLocker lock(&me->vectorMutex);
|
|
idInMap = me->m_mLoadingMap.contains(id);
|
|
if (idInMap){
|
|
url = me->m_mLoadingMap[id];
|
|
me->m_mLoadingMap.remove(id);
|
|
};
|
|
};
|
|
|
|
if( idInMap ){
|
|
if (rr->getSize()){
|
|
//qDebug() << "Network loaded: " << rr->getSize() << " bytes";
|
|
QPixmap pm;
|
|
QByteArray ax;
|
|
ax = QByteArray::fromRawData((const char *)rr->getBuffer(), (int)rr->getSize());
|
|
if (pm.loadFromData(ax) && pm.size().width() > 1 && pm.size().height() > 1){
|
|
me->loaded += pm.size().width()*pm.size().height()*pm.depth() / 8 / 1024;
|
|
//qDebug() << "Network loaded: " << me->loaded << " width:" << pm.size().width() << " height:" << pm.size().height();
|
|
me->m_pImageManager->receivedImage(pm, QString(rr->getPath()));
|
|
if( me->m_bCacheEnabled){
|
|
QNetworkCacheMetaData QMeta;
|
|
QMeta.setSaveToDisk(true);
|
|
QMeta.setUrl(QUrl(rr->getURL()));
|
|
//QIODevice *QIO = me->m_pDiskCache->data(QUrl(rr->getURL()));
|
|
QIODevice *QIO = me->m_pDiskCache->prepare(QMeta);
|
|
QIO->open(QIODevice::WriteOnly);
|
|
QIO->write((const char *)rr->getBuffer(), (int)rr->getSize());
|
|
QIO->close();
|
|
};
|
|
} else {
|
|
me->m_pImageManager->fetchFailed(QString(rr->getPath()));
|
|
};
|
|
} else {
|
|
me->m_pImageManager->fetchFailed(QString(rr->getPath()));
|
|
};
|
|
};
|
|
|
|
if (me->loadQueueSize() == 0){
|
|
//qDebug () << "all loaded";
|
|
me->m_pImageManager->loadingQueueEmpty();
|
|
}
|
|
|
|
//QMutexLocker lock( &me->vectorMutex );
|
|
//me->m_lRequestsList.removeAll(rr);
|
|
return NULL;
|
|
};
|
|
|
|
int MapNetwork::loadQueueSize() const
|
|
{
|
|
QMutexLocker lock(&vectorMutex);
|
|
return m_mLoadingMap.size();
|
|
}
|
|
|
|
void MapNetwork::setDiskCache(QNetworkDiskCache *qCache){
|
|
m_bCacheEnabled = (qCache != 0);
|
|
if (m_bCacheEnabled){//m_pHttpAccessManager){
|
|
m_pDiskCache = qCache; //m_pHttpAccessManager->setCache(qCache);
|
|
};
|
|
};
|
|
|
|
void MapNetwork::abortLoading(){
|
|
qDebug() << "MapNetwork::abortLoading";
|
|
// be sure that replyList is copied in case it's modified in another thread
|
|
//QListIterator<curlRequest *> iter(m_lRequestsList);
|
|
//while(iter.hasNext())
|
|
//{
|
|
// curlRequest *tRequest = iter.next();
|
|
// if (tRequest){
|
|
//if(reply->isRunning())
|
|
//{
|
|
// reply->abort();
|
|
//}
|
|
//reply->deleteLater();
|
|
// delete tRequest;
|
|
// tRequest = 0;
|
|
// };
|
|
//};
|
|
QMutexLocker lock(&vectorMutex);
|
|
//m_lRequestsList.clear();
|
|
m_mLoadingMap.clear();
|
|
};
|
|
|
|
bool MapNetwork::imageIsLoading(QString url)
|
|
{
|
|
QMutexLocker lock(&vectorMutex);
|
|
return m_mLoadingMap.contains(url);
|
|
}
|
|
|
|
void MapNetwork::setProxy(const QString host, const int port, const QString username, const QString password)
|
|
{
|
|
// do not set proxy on qt/extended
|
|
#ifndef Q_WS_QWS
|
|
if (m_pHttpAccessManager){
|
|
QNetworkProxy proxy = QNetworkProxy( QNetworkProxy::HttpProxy, host, port );
|
|
proxy.setUser( username );
|
|
proxy.setPassword( password );
|
|
//m_pHttpAccessManager->setProxy(proxy);
|
|
}
|
|
#endif
|
|
}
|