diff --git a/src/map/downloader.cpp b/src/map/downloader.cpp index 8a628c54..c7543d71 100644 --- a/src/map/downloader.cpp +++ b/src/map/downloader.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "config.h" #include "downloader.h" @@ -25,12 +26,41 @@ #define ATTR_LEVEL (QNetworkRequest::Attribute)(QNetworkRequest::User + 2) #define MAX_REDIRECT_LEVEL 5 +#define TIMEOUT 30 /* s */ +class ReplyTimeout : public QObject +{ +public: + static void set(QNetworkReply *reply, int timeout) + { + Q_ASSERT(reply); + new ReplyTimeout(reply, timeout); + } + +private: + ReplyTimeout(QNetworkReply *reply, int timeout) : QObject(reply) + { + _timer.start(timeout * 1000, this); + } + + void timerEvent(QTimerEvent *ev) + { + if (!_timer.isActive() || ev->timerId() != _timer.timerId()) + return; + QNetworkReply *reply = static_cast(parent()); + if (reply->isRunning()) + reply->close(); + _timer.stop(); + } + + QBasicTimer _timer; +}; + Downloader::Downloader(QObject *parent) : QObject(parent) { connect(&_manager, SIGNAL(finished(QNetworkReply*)), - SLOT(downloadFinished(QNetworkReply*))); + SLOT(downloadFinished(QNetworkReply*))); } bool Downloader::doDownload(const Download &dl, const Redirect &redirect) @@ -49,9 +79,13 @@ bool Downloader::doDownload(const Download &dl, const Redirect &redirect) request.setAttribute(ATTR_LEVEL, QVariant(redirect.level())); } request.setRawHeader("User-Agent", USER_AGENT); - QNetworkReply *reply = _manager.get(request); - _currentDownloads.insert(url, reply); + QNetworkReply *reply = _manager.get(request); + if (reply) { + _currentDownloads.insert(url, reply); + ReplyTimeout::set(reply, TIMEOUT); + } else + return false; return true; } diff --git a/src/map/downloader.h b/src/map/downloader.h index b92646b4..5027bcbf 100644 --- a/src/map/downloader.h +++ b/src/map/downloader.h @@ -7,7 +7,6 @@ #include #include - class QNetworkReply; class Download diff --git a/src/map/tileloader.h b/src/map/tileloader.h index 9bbb7346..0598e7c6 100644 --- a/src/map/tileloader.h +++ b/src/map/tileloader.h @@ -3,7 +3,8 @@ #include #include "tile.h" -#include "downloader.h" + +class Downloader; class TileLoader { diff --git a/src/map/wmtsmap.cpp b/src/map/wmtsmap.cpp index d819f17f..f69fb2d5 100644 --- a/src/map/wmtsmap.cpp +++ b/src/map/wmtsmap.cpp @@ -3,6 +3,7 @@ #include "common/wgs84.h" #include "config.h" #include "transform.h" +#include "downloader.h" #include "wmts.h" #include "wmtsmap.h"