mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-11-24 11:45:53 +01:00
Also clear the error URLs when clearing the tile cache.
Code cleanup.
This commit is contained in:
parent
fe1f6c80b4
commit
3d06fe8831
@ -29,10 +29,10 @@
|
|||||||
#define TIMEOUT 30 /* s */
|
#define TIMEOUT 30 /* s */
|
||||||
|
|
||||||
|
|
||||||
class ReplyTimeout : public QObject
|
class Downloader::ReplyTimeout : public QObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void set(QNetworkReply *reply, int timeout)
|
static void setTimeout(QNetworkReply *reply, int timeout)
|
||||||
{
|
{
|
||||||
Q_ASSERT(reply);
|
Q_ASSERT(reply);
|
||||||
new ReplyTimeout(reply, timeout);
|
new ReplyTimeout(reply, timeout);
|
||||||
@ -57,13 +57,29 @@ private:
|
|||||||
QBasicTimer _timer;
|
QBasicTimer _timer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Downloader::Redirect
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Redirect() : _level(0) {}
|
||||||
|
Redirect(const QUrl &origin, int level) :
|
||||||
|
_origin(origin), _level(level) {}
|
||||||
|
|
||||||
|
const QUrl &origin() const {return _origin;}
|
||||||
|
int level() const {return _level;}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QUrl _origin;
|
||||||
|
int _level;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Downloader::Downloader(QObject *parent) : QObject(parent)
|
Downloader::Downloader(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
connect(&_manager, SIGNAL(finished(QNetworkReply*)),
|
connect(&_manager, SIGNAL(finished(QNetworkReply*)),
|
||||||
SLOT(downloadFinished(QNetworkReply*)));
|
SLOT(downloadFinished(QNetworkReply*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Downloader::doDownload(const Download &dl, const Redirect &redirect)
|
bool Downloader::doDownload(const Download &dl, const Redirect *redirect)
|
||||||
{
|
{
|
||||||
QUrl url(dl.url());
|
QUrl url(dl.url());
|
||||||
|
|
||||||
@ -74,16 +90,16 @@ bool Downloader::doDownload(const Download &dl, const Redirect &redirect)
|
|||||||
|
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
request.setAttribute(ATTR_FILE, QVariant(dl.file()));
|
request.setAttribute(ATTR_FILE, QVariant(dl.file()));
|
||||||
if (!redirect.isNull()) {
|
if (redirect) {
|
||||||
request.setAttribute(ATTR_ORIGIN, QVariant(redirect.origin()));
|
request.setAttribute(ATTR_ORIGIN, QVariant(redirect->origin()));
|
||||||
request.setAttribute(ATTR_LEVEL, QVariant(redirect.level()));
|
request.setAttribute(ATTR_LEVEL, QVariant(redirect->level()));
|
||||||
}
|
}
|
||||||
request.setRawHeader("User-Agent", USER_AGENT);
|
request.setRawHeader("User-Agent", USER_AGENT);
|
||||||
|
|
||||||
QNetworkReply *reply = _manager.get(request);
|
QNetworkReply *reply = _manager.get(request);
|
||||||
if (reply) {
|
if (reply) {
|
||||||
_currentDownloads.insert(url, reply);
|
_currentDownloads.insert(url);
|
||||||
ReplyTimeout::set(reply, TIMEOUT);
|
ReplyTimeout::setTimeout(reply, TIMEOUT);
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -143,14 +159,14 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
|||||||
} else {
|
} else {
|
||||||
Redirect redirect(origin.isEmpty() ? url : origin, level + 1);
|
Redirect redirect(origin.isEmpty() ? url : origin, level + 1);
|
||||||
Download dl(location, filename);
|
Download dl(location, filename);
|
||||||
doDownload(dl, redirect);
|
doDownload(dl, &redirect);
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
if (!saveToDisk(filename, reply))
|
if (!saveToDisk(filename, reply))
|
||||||
_errorDownloads.insert(url);
|
_errorDownloads.insert(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
_currentDownloads.remove(url);
|
Q_ASSERT(_currentDownloads.remove(url));
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
if (_currentDownloads.isEmpty())
|
if (_currentDownloads.isEmpty())
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QMap>
|
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
|
|
||||||
class QNetworkReply;
|
class QNetworkReply;
|
||||||
@ -31,6 +30,7 @@ public:
|
|||||||
Downloader(QObject *parent = 0);
|
Downloader(QObject *parent = 0);
|
||||||
|
|
||||||
bool get(const QList<Download> &list);
|
bool get(const QList<Download> &list);
|
||||||
|
void clearErrors() {_errorDownloads.clear();}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void finished();
|
void finished();
|
||||||
@ -39,28 +39,14 @@ private slots:
|
|||||||
void downloadFinished(QNetworkReply *reply);
|
void downloadFinished(QNetworkReply *reply);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Redirect
|
class Redirect;
|
||||||
{
|
class ReplyTimeout;
|
||||||
public:
|
|
||||||
Redirect() : _level(0) {}
|
|
||||||
Redirect(const QUrl &origin, int level) :
|
|
||||||
_origin(origin), _level(level) {}
|
|
||||||
|
|
||||||
const QUrl &origin() const {return _origin;}
|
bool doDownload(const Download &dl, const Redirect *redirect = 0);
|
||||||
int level() const {return _level;}
|
|
||||||
|
|
||||||
bool isNull() const {return (_level == 0);}
|
|
||||||
|
|
||||||
private:
|
|
||||||
QUrl _origin;
|
|
||||||
int _level;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool doDownload(const Download &dl, const Redirect &redirect = Redirect());
|
|
||||||
bool saveToDisk(const QString &filename, QIODevice *data);
|
bool saveToDisk(const QString &filename, QIODevice *data);
|
||||||
|
|
||||||
QNetworkAccessManager _manager;
|
QNetworkAccessManager _manager;
|
||||||
QMap<QUrl, QNetworkReply *> _currentDownloads;
|
QSet<QUrl> _currentDownloads;
|
||||||
QSet<QUrl> _errorDownloads;
|
QSet<QUrl> _errorDownloads;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -77,6 +77,8 @@ void TileLoader::clearCache()
|
|||||||
|
|
||||||
for (int i = 0; i < list.count(); i++)
|
for (int i = 0; i < list.count(); i++)
|
||||||
dir.remove(list.at(i));
|
dir.remove(list.at(i));
|
||||||
|
|
||||||
|
_downloader->clearErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TileLoader::tileUrl(const Tile &tile) const
|
QString TileLoader::tileUrl(const Tile &tile) const
|
||||||
|
Loading…
Reference in New Issue
Block a user