1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 06:43:22 +02:00

Improved error handling

Refactoring
This commit is contained in:
Martin Tůma 2015-12-04 22:56:34 +01:00
parent 4550e473de
commit f3eb136187
2 changed files with 14 additions and 7 deletions

View File

@ -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();
}

View File

@ -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