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()
|
||||
{
|
||||
connect(&manager, SIGNAL(finished(QNetworkReply*)),
|
||||
connect(&_manager, SIGNAL(finished(QNetworkReply*)),
|
||||
SLOT(downloadFinished(QNetworkReply*)));
|
||||
}
|
||||
|
||||
void Downloader::doDownload(const Download &dl)
|
||||
{
|
||||
QUrl url(dl.url());
|
||||
|
||||
if (_errorDownloads.contains(url))
|
||||
return;
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setAttribute(QNetworkRequest::User, QVariant(dl.file()));
|
||||
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)
|
||||
@ -53,6 +57,7 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
||||
{
|
||||
QUrl url = reply->url();
|
||||
if (reply->error()) {
|
||||
_errorDownloads.insert(url);
|
||||
fprintf(stderr, "Error downloading map tile: %s: %s\n",
|
||||
url.toEncoded().constData(), qPrintable(reply->errorString()));
|
||||
} else {
|
||||
@ -61,10 +66,10 @@ void Downloader::downloadFinished(QNetworkReply *reply)
|
||||
saveToDisk(filename, reply);
|
||||
}
|
||||
|
||||
currentDownloads.removeAll(reply);
|
||||
_currentDownloads.removeAll(reply);
|
||||
reply->deleteLater();
|
||||
|
||||
if (currentDownloads.isEmpty())
|
||||
if (_currentDownloads.isEmpty())
|
||||
emit finished();
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <QNetworkReply>
|
||||
#include <QUrl>
|
||||
#include <QList>
|
||||
#include <QSet>
|
||||
|
||||
|
||||
class Download
|
||||
@ -45,8 +46,9 @@ private:
|
||||
void doDownload(const Download &dl);
|
||||
bool saveToDisk(const QString &filename, QIODevice *data);
|
||||
|
||||
QNetworkAccessManager manager;
|
||||
QList<QNetworkReply *> currentDownloads;
|
||||
QNetworkAccessManager _manager;
|
||||
QList<QNetworkReply *> _currentDownloads;
|
||||
QSet<QUrl> _errorDownloads;
|
||||
};
|
||||
|
||||
#endif // DOWNLOADER_H
|
||||
|
Loading…
Reference in New Issue
Block a user