MultiLevelJsonBase.cpp   MultiLevelJsonBase.cpp 
skipping to change at line 43 skipping to change at line 43
#include <QThread> #include <QThread>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <stdexcept> #include <stdexcept>
#include <stdio.h> #include <stdio.h>
// #include <QNetworkDiskCache> // #include <QNetworkDiskCache>
// Init statics // Init statics
QNetworkAccessManager* MultiLevelJsonBase::networkAccessManager = NULL; QNetworkAccessManager* MultiLevelJsonBase::networkAccessManager = Q_NULLPTR ;
QNetworkAccessManager& MultiLevelJsonBase::getNetworkAccessManager() QNetworkAccessManager& MultiLevelJsonBase::getNetworkAccessManager()
{ {
if (networkAccessManager==NULL) if (networkAccessManager==Q_NULLPTR)
{ {
networkAccessManager = new QNetworkAccessManager(&StelApp::g etInstance()); networkAccessManager = new QNetworkAccessManager(&StelApp::g etInstance());
// Cache on JSON files doesn't work, and I don't know why. // Cache on JSON files doesn't work, and I don't know why.
// There may be a problem in Qt for text objects caching (compression is ac tivated in cache) // There may be a problem in Qt for text objects caching (compression is ac tivated in cache)
// QNetworkDiskCache* cache = new QNetworkDiskCache(networkAcce ssManager); // QNetworkDiskCache* cache = new QNetworkDiskCache(networkAcce ssManager);
// QString cachePath = StelApp::getInstance().getCacheDir(); // QString cachePath = StelApp::getInstance().getCacheDir();
// cache->setCacheDirectory(cachePath+"/JSONCache"); // cache->setCacheDirectory(cachePath+"/JSONCache");
// networkAccessManager->setCache(cache); // networkAccessManager->setCache(cache);
connect(networkAccessManager, SIGNAL(finished(QNetworkReply* )), &StelApp::getInstance(), SLOT(reportFileDownloadFinished(QNetworkReply* ))); connect(networkAccessManager, SIGNAL(finished(QNetworkReply* )), &StelApp::getInstance(), SLOT(reportFileDownloadFinished(QNetworkReply* )));
} }
skipping to change at line 95 skipping to change at line 95
catch (std::runtime_error e) catch (std::runtime_error e)
{ {
qWarning() << "WARNING : Can't parse loaded JSON description : " << e.what(); qWarning() << "WARNING : Can't parse loaded JSON description : " << e.what();
tile->errorOccured = true; tile->errorOccured = true;
} }
} }
MultiLevelJsonBase::MultiLevelJsonBase(MultiLevelJsonBase* parent) : StelSk yLayer(parent) MultiLevelJsonBase::MultiLevelJsonBase(MultiLevelJsonBase* parent) : StelSk yLayer(parent)
, errorOccured(false) , errorOccured(false)
, downloading(false) , downloading(false)
, httpReply(NULL) , httpReply(Q_NULLPTR)
, deletionDelay(2.) , deletionDelay(2.)
, loadThread(NULL) , loadThread(Q_NULLPTR)
, timeWhenDeletionScheduled(-1.) // Avoid tiles to be deleted just a fter constructed , timeWhenDeletionScheduled(-1.) // Avoid tiles to be deleted just a fter constructed
, loadingState(false) , loadingState(false)
, lastPercent(0) , lastPercent(0)
{ {
if (parent!=NULL) if (parent!=Q_NULLPTR)
{ {
deletionDelay = parent->deletionDelay; deletionDelay = parent->deletionDelay;
} }
} }
void MultiLevelJsonBase::initFromUrl(const QString& url) void MultiLevelJsonBase::initFromUrl(const QString& url)
{ {
const MultiLevelJsonBase* parent = qobject_cast<MultiLevelJsonBase*> (QObject::parent()); const MultiLevelJsonBase* parent = qobject_cast<MultiLevelJsonBase*> (QObject::parent());
contructorUrl = url; contructorUrl = url;
if (!url.startsWith("http://") && (parent==NULL || !parent->getBaseU rl().startsWith("http://"))) if (!url.startsWith("http://") && (parent==Q_NULLPTR || !parent->get BaseUrl().startsWith("http://")))
{ {
// Assume a local file // Assume a local file
QString fileName = StelFileMgr::findFile(url); QString fileName = StelFileMgr::findFile(url);
if (fileName.isEmpty()) if (fileName.isEmpty())
{ {
if (parent==NULL) if (parent==Q_NULLPTR)
{ {
qWarning() << "NULL parent"; qWarning() << "NULL parent";
errorOccured = true; errorOccured = true;
return; return;
} }
fileName = StelFileMgr::findFile(parent->getBaseUrl( )+url); fileName = StelFileMgr::findFile(parent->getBaseUrl( )+url);
if (fileName.isEmpty()) if (fileName.isEmpty())
{ {
qWarning() << "WARNING : Can't find JSON des cription: " << url; qWarning() << "WARNING : Can't find JSON des cription: " << url;
errorOccured = true; errorOccured = true;
skipping to change at line 168 skipping to change at line 168
QUrl qurl; QUrl qurl;
if (url.startsWith("http://")) if (url.startsWith("http://"))
{ {
qurl.setUrl(url); qurl.setUrl(url);
} }
else else
{ {
Q_ASSERT(parent->getBaseUrl().startsWith("http://")) ; Q_ASSERT(parent->getBaseUrl().startsWith("http://")) ;
qurl.setUrl(parent->getBaseUrl()+url); qurl.setUrl(parent->getBaseUrl()+url);
} }
Q_ASSERT(httpReply==NULL); Q_ASSERT(httpReply==Q_NULLPTR);
QNetworkRequest req(qurl); QNetworkRequest req(qurl);
req.setRawHeader("User-Agent", StelUtils::getUserAgentString ().toLatin1()); req.setRawHeader("User-Agent", StelUtils::getUserAgentString ().toLatin1());
httpReply = getNetworkAccessManager().get(req); httpReply = getNetworkAccessManager().get(req);
//qDebug() << "Started downloading " << httpReply->request() .url().path(); //qDebug() << "Started downloading " << httpReply->request() .url().path();
Q_ASSERT(httpReply->error()==QNetworkReply::NoError); Q_ASSERT(httpReply->error()==QNetworkReply::NoError);
//qDebug() << httpReply->attribute(QNetworkRequest::SourceIs FromCacheAttribute).toBool(); //qDebug() << httpReply->attribute(QNetworkRequest::SourceIs FromCacheAttribute).toBool();
connect(httpReply, SIGNAL(finished()), this, SLOT(downloadFi nished())); connect(httpReply, SIGNAL(finished()), this, SLOT(downloadFi nished()));
//connect(httpReply, SIGNAL(error(QNetworkReply::NetworkErro r)), this, SLOT(downloadError(QNetworkReply::NetworkError))); //connect(httpReply, SIGNAL(error(QNetworkReply::NetworkErro r)), this, SLOT(downloadError(QNetworkReply::NetworkError)));
//connect(httpReply, SIGNAL(destroyed()), this, SLOT(replyDe stroyed())); //connect(httpReply, SIGNAL(destroyed()), this, SLOT(replyDe stroyed()));
downloading = true; downloading = true;
QString turl = qurl.toString(); QString turl = qurl.toString();
baseUrl = turl.left(turl.lastIndexOf('/')+1); baseUrl = turl.left(turl.lastIndexOf('/')+1);
} }
} }
// Constructor from a map used for JSON files with more than 1 level // Constructor from a map used for JSON files with more than 1 level
void MultiLevelJsonBase::initFromQVariantMap(const QVariantMap& map) void MultiLevelJsonBase::initFromQVariantMap(const QVariantMap& map)
{ {
const MultiLevelJsonBase* parent = qobject_cast<MultiLevelJsonBase*> (QObject::parent()); const MultiLevelJsonBase* parent = qobject_cast<MultiLevelJsonBase*> (QObject::parent());
if (parent!=NULL) if (parent!=Q_NULLPTR)
{ {
baseUrl = parent->getBaseUrl(); baseUrl = parent->getBaseUrl();
contructorUrl = parent->contructorUrl + "/?"; contructorUrl = parent->contructorUrl + "/?";
} }
try try
{ {
loadFromQVariantMap(map); loadFromQVariantMap(map);
} }
catch (std::runtime_error e) catch (std::runtime_error e)
{ {
skipping to change at line 218 skipping to change at line 218
{ {
if (httpReply) if (httpReply)
{ {
//qDebug() << "Abort: " << httpReply->request().url().path() ; //qDebug() << "Abort: " << httpReply->request().url().path() ;
//httpReply->abort(); //httpReply->abort();
// TODO: This line should not be commented, but I have to ke ep it because of a Qt bug. // TODO: This line should not be commented, but I have to ke ep it because of a Qt bug.
// It should be fixed with Qt 4.5.1 // It should be fixed with Qt 4.5.1
// It causes a nasty memory leak, but prevents an even more nasty // It causes a nasty memory leak, but prevents an even more nasty
//httpReply->deleteLater(); //httpReply->deleteLater();
httpReply = NULL; httpReply = Q_NULLPTR;
} }
if (loadThread && loadThread->isRunning()) if (loadThread && loadThread->isRunning())
{ {
//qDebug() << "--> Abort thread " << contructorUrl; //qDebug() << "--> Abort thread " << contructorUrl;
disconnect(loadThread, SIGNAL(finished()), this, SLOT(jsonLo adFinished())); disconnect(loadThread, SIGNAL(finished()), this, SLOT(jsonLo adFinished()));
// The thread is currently running, it needs to be properly stopped // The thread is currently running, it needs to be properly stopped
if (loadThread->wait(1)==false) if (loadThread->wait(1)==false)
{ {
loadThread->terminate(); loadThread->terminate();
//loadThread->wait(2000); //loadThread->wait(2000);
skipping to change at line 260 skipping to change at line 260
timeWhenDeletionScheduled=-1.; timeWhenDeletionScheduled=-1.;
foreach (MultiLevelJsonBase* tile, subTiles) foreach (MultiLevelJsonBase* tile, subTiles)
{ {
tile->cancelDeletion(); tile->cancelDeletion();
} }
} }
// Load the tile information from a JSON file // Load the tile information from a JSON file
QVariantMap MultiLevelJsonBase::loadFromJSON(QIODevice& input, bool qZcompr essed, bool gzCompressed) QVariantMap MultiLevelJsonBase::loadFromJSON(QIODevice& input, bool qZcompr essed, bool gzCompressed)
{ {
StelJsonParser parser;
QVariantMap map; QVariantMap map;
if (qZcompressed && input.size()>0) if (qZcompressed && input.size()>0)
{ {
QByteArray ar = qUncompress(input.readAll()); QByteArray ar = qUncompress(input.readAll());
input.close(); input.close();
QBuffer buf(&ar); QBuffer buf(&ar);
buf.open(QIODevice::ReadOnly); buf.open(QIODevice::ReadOnly);
map = parser.parse(&buf).toMap(); map = StelJsonParser::parse(&buf).toMap();
buf.close(); buf.close();
} }
else if (gzCompressed) else if (gzCompressed)
{ {
QByteArray ar = StelUtils::uncompress(input.readAll()); QByteArray ar = StelUtils::uncompress(input.readAll());
input.close(); input.close();
map = parser.parse(ar).toMap(); map = StelJsonParser::parse(ar).toMap();
} }
else else
{ {
map = parser.parse(&input).toMap(); map = StelJsonParser::parse(&input).toMap();
} }
if (map.isEmpty()) if (map.isEmpty())
throw std::runtime_error("empty JSON file, cannot load"); throw std::runtime_error("empty JSON file, cannot load");
return map; return map;
} }
// Called when the download for the JSON file terminated // Called when the download for the JSON file terminated
void MultiLevelJsonBase::downloadFinished() void MultiLevelJsonBase::downloadFinished()
{ {
//qDebug() << "Finished downloading " << httpReply->request().url(). path(); //qDebug() << "Finished downloading " << httpReply->request().url(). path();
Q_ASSERT(downloading); Q_ASSERT(downloading);
if (httpReply->error()!=QNetworkReply::NoError) if (httpReply->error()!=QNetworkReply::NoError)
{ {
if (httpReply->error()!=QNetworkReply::OperationCanceledErro r) if (httpReply->error()!=QNetworkReply::OperationCanceledErro r)
qWarning() << "WARNING : Problem while downloading J SON description for " << httpReply->request().url().path() << ": "<< httpRe ply->errorString(); qWarning() << "WARNING : Problem while downloading J SON description for " << httpReply->request().url().path() << ": "<< httpRe ply->errorString();
errorOccured = true; errorOccured = true;
httpReply->deleteLater(); httpReply->deleteLater();
httpReply=NULL; httpReply=Q_NULLPTR;
downloading=false; downloading=false;
timeWhenDeletionScheduled = StelApp::getInstance().getTotalR unTime(); timeWhenDeletionScheduled = StelApp::getInstance().getTotalR unTime();
return; return;
} }
QByteArray content = httpReply->readAll(); QByteArray content = httpReply->readAll();
if (content.isEmpty()) if (content.isEmpty())
{ {
qWarning() << "WARNING : empty JSON description for " << htt pReply->request().url().path(); qWarning() << "WARNING : empty JSON description for " << htt pReply->request().url().path();
errorOccured = true; errorOccured = true;
httpReply->deleteLater(); httpReply->deleteLater();
httpReply=NULL; httpReply=Q_NULLPTR;
downloading=false; downloading=false;
return; return;
} }
const bool qZcompressed = httpReply->request().url().path().endsWith (".qZ"); const bool qZcompressed = httpReply->request().url().path().endsWith (".qZ");
const bool gzCompressed = httpReply->request().url().path().endsWith (".gz"); const bool gzCompressed = httpReply->request().url().path().endsWith (".gz");
httpReply->deleteLater(); httpReply->deleteLater();
httpReply=NULL; httpReply=Q_NULLPTR;
Q_ASSERT(loadThread==NULL); Q_ASSERT(loadThread==Q_NULLPTR);
loadThread = new JsonLoadThread(this, content, qZcompressed, gzCompr essed); loadThread = new JsonLoadThread(this, content, qZcompressed, gzCompr essed);
connect(loadThread, SIGNAL(finished()), this, SLOT(jsonLoadFinished( ))); connect(loadThread, SIGNAL(finished()), this, SLOT(jsonLoadFinished( )));
loadThread->start(QThread::LowestPriority); loadThread->start(QThread::LowestPriority);
} }
// Called when the element is fully loaded from the JSON file // Called when the element is fully loaded from the JSON file
void MultiLevelJsonBase::jsonLoadFinished() void MultiLevelJsonBase::jsonLoadFinished()
{ {
loadThread->wait(); loadThread->wait();
delete loadThread; delete loadThread;
loadThread = NULL; loadThread = Q_NULLPTR;
downloading = false; downloading = false;
if (errorOccured) if (errorOccured)
return; return;
try try
{ {
loadFromQVariantMap(temporaryResultMap); loadFromQVariantMap(temporaryResultMap);
} }
catch (std::runtime_error e) catch (std::runtime_error e)
{ {
qWarning() << "WARNING: invalid variant map: " << e.what(); qWarning() << "WARNING: invalid variant map: " << e.what();
 End of changes. 19 change blocks. 
19 lines changed or deleted 18 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/