/** ****************************************************************************** * * @file OPMaps.cpp * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. * @brief * @see The GNU Public License (GPL) Version 3 * @defgroup OPMapWidget * @{ * *****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License * for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "opmaps.h" namespace core { OPMaps* OPMaps::m_pInstance=0; OPMaps* OPMaps::Instance() { if(!m_pInstance) m_pInstance=new OPMaps; return m_pInstance; } OPMaps::OPMaps():RetryLoadTile(2),useMemoryCache(true) { accessmode=AccessMode::ServerAndCache; Language=LanguageType::PortuguesePortugal; LanguageStr=LanguageType().toShortString(Language); Cache::Instance(); } OPMaps::~OPMaps() { TileDBcacheQueue.wait(); } QByteArray OPMaps::GetImageFrom(const MapType::Types &type,const Point &pos,const int &zoom) { #ifdef DEBUG_TIMINGS QTime time; time.restart(); #endif #ifdef DEBUG_GMAPS qDebug()<<"Entered GetImageFrom"; #endif //DEBUG_GMAPS QByteArray ret; if(useMemoryCache) { #ifdef DEBUG_GMAPS qDebug()<<"Try Tile from memory:Size="<ImageCache.GetImageFromCache(type,pos,zoom); if(!ret.isEmpty()) { errorvars.lock(); ++diag.tilesFromDB; errorvars.unlock(); #ifdef DEBUG_GMAPS qDebug()<<"Tile found in Database"; #endif //DEBUG_GMAPS if(useMemoryCache) { #ifdef DEBUG_GMAPS qDebug()<<"Add Tile to memory"; #endif //DEBUG_GMAPS AddTileToMemoryCache(RawTile(type,pos,zoom),ret); } return ret; } } if(accessmode!=AccessMode::CacheOnly) { QEventLoop q; QNetworkReply *reply; QNetworkRequest qheader; QNetworkAccessManager network; QTimer tT; tT.setSingleShot(true); connect(&network, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit())); connect(&tT, SIGNAL(timeout()), &q, SLOT(quit())); network.setProxy(Proxy); #ifdef DEBUG_GMAPS qDebug()<<"Try Tile from the Internet"; #endif //DEBUG_GMAPS #ifdef DEBUG_TIMINGS qDebug()<<"opmaps before make image url"<error()!=QNetworkReply::NoError)) { errorvars.lock(); ++diag.networkerrors; errorvars.unlock(); reply->deleteLater(); return ret; } ret=reply->readAll(); reply->deleteLater();//TODO can't this be global?? if(ret.isEmpty()) { #ifdef DEBUG_GMAPS qDebug()<<"Invalid Tile"; #endif //DEBUG_GMAPS errorvars.lock(); ++diag.emptytiles; errorvars.unlock(); return ret; } #ifdef DEBUG_GMAPS qDebug()<<"Received Tile from the Internet"; #endif //DEBUG_GMAPS errorvars.lock(); ++diag.tilesFromNet; errorvars.unlock(); if (useMemoryCache) { #ifdef DEBUG_GMAPS qDebug()<<"Add Tile to memory cache"; #endif //DEBUG_GMAPS AddTileToMemoryCache(RawTile(type,pos,zoom),ret); } if(accessmode!=AccessMode::ServerOnly) { #ifdef DEBUG_GMAPS qDebug()<<"Add tile to DataBase"; #endif //DEBUG_GMAPS CacheItemQueue * item=new CacheItemQueue(type,pos,ret,zoom); TileDBcacheQueue.EnqueueCacheTask(item); } } } #ifdef DEBUG_GMAPS qDebug()<<"Entered GetImageFrom"; #endif //DEBUG_GMAPS return ret; } bool OPMaps::ExportToGMDB(const QString &file) { return Cache::Instance()->ImageCache.ExportMapDataToDB(Cache::Instance()->ImageCache.GtileCache()+QDir::separator()+"Data.qmdb",file); } bool OPMaps::ImportFromGMDB(const QString &file) { return Cache::Instance()->ImageCache.ExportMapDataToDB(file,Cache::Instance()->ImageCache.GtileCache()+QDir::separator()+"Data.qmdb"); } diagnostics OPMaps::GetDiagnostics() { diagnostics i; errorvars.lock(); i=diag; errorvars.unlock(); return i; } }