mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-11-27 21:24:47 +01:00
Improved error handling
Refactoring
This commit is contained in:
parent
4550e473de
commit
f3eb136187
@ -18,19 +18,23 @@
|
|||||||
|
|
||||||
Downloader::Downloader()
|
Downloader::Downloader()
|
||||||
{
|
{
|
||||||
connect(&manager, SIGNAL(finished(QNetworkReply*)),
|
connect(&_manager, SIGNAL(finished(QNetworkReply*)),
|
||||||
SLOT(downloadFinished(QNetworkReply*)));
|
SLOT(downloadFinished(QNetworkReply*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Downloader::doDownload(const Download &dl)
|
void Downloader::doDownload(const Download &dl)
|
||||||
{
|
{
|
||||||
QUrl url(dl.url());
|
QUrl url(dl.url());
|
||||||
|
|
||||||
|
if (_errorDownloads.contains(url))
|
||||||
|
return;
|
||||||
|
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
request.setAttribute(QNetworkRequest::User, QVariant(dl.file()));
|
request.setAttribute(QNetworkRequest::User, QVariant(dl.file()));
|
||||||
request.setRawHeader("User-Agent", USER_AGENT);
|
request.setRawHeader("User-Agent", USER_AGENT);
|
||||||
QNetworkReply *reply = manager.get(request);
|
QNetworkReply *reply = _manager.get(request);
|
||||||
|
|
||||||
currentDownloads.append(reply);
|
_currentDownloads.append(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Downloader::saveToDisk(const QString &filename, QIODevice *data)
|
bool Downloader::saveToDisk(const QString &filename, QIODevice *data)
|
||||||
@ -53,6 +57,7 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
|||||||
{
|
{
|
||||||
QUrl url = reply->url();
|
QUrl url = reply->url();
|
||||||
if (reply->error()) {
|
if (reply->error()) {
|
||||||
|
_errorDownloads.insert(url);
|
||||||
fprintf(stderr, "Error downloading map tile: %s: %s\n",
|
fprintf(stderr, "Error downloading map tile: %s: %s\n",
|
||||||
url.toEncoded().constData(), qPrintable(reply->errorString()));
|
url.toEncoded().constData(), qPrintable(reply->errorString()));
|
||||||
} else {
|
} else {
|
||||||
@ -61,10 +66,10 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
|||||||
saveToDisk(filename, reply);
|
saveToDisk(filename, reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentDownloads.removeAll(reply);
|
_currentDownloads.removeAll(reply);
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
if (currentDownloads.isEmpty())
|
if (_currentDownloads.isEmpty())
|
||||||
emit finished();
|
emit finished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
#include <QSet>
|
||||||
|
|
||||||
|
|
||||||
class Download
|
class Download
|
||||||
@ -45,8 +46,9 @@ private:
|
|||||||
void doDownload(const Download &dl);
|
void doDownload(const Download &dl);
|
||||||
bool saveToDisk(const QString &filename, QIODevice *data);
|
bool saveToDisk(const QString &filename, QIODevice *data);
|
||||||
|
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager _manager;
|
||||||
QList<QNetworkReply *> currentDownloads;
|
QList<QNetworkReply *> _currentDownloads;
|
||||||
|
QSet<QUrl> _errorDownloads;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DOWNLOADER_H
|
#endif // DOWNLOADER_H
|
||||||
|
Loading…
Reference in New Issue
Block a user