1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/downloader.cpp

96 lines
2.1 KiB
C++
Raw Normal View History

2015-11-23 02:33:01 +01:00
#include <QFile>
#include <QFileInfo>
#include "config.h"
2015-11-23 02:33:01 +01:00
#include "downloader.h"
2015-11-24 10:05:28 +01:00
#if defined(Q_OS_LINUX)
#define PLATFORM_STR "Linux"
2015-11-24 10:05:28 +01:00
#elif defined(Q_OS_WIN32)
#define PLATFORM_STR "Windows"
2015-11-24 10:05:28 +01:00
#elif defined(Q_OS_MAC)
#define PLATFORM_STR "OS X"
#else
#define PLATFORM_STR "Unknown"
#endif
2016-04-01 23:14:57 +02:00
#define USER_AGENT \
APP_NAME "/" APP_VERSION " (" PLATFORM_STR "; Qt " QT_VERSION_STR ")"
2015-11-23 02:33:01 +01:00
Downloader::Downloader()
{
2015-12-04 22:56:34 +01:00
connect(&_manager, SIGNAL(finished(QNetworkReply*)),
2015-11-23 02:33:01 +01:00
SLOT(downloadFinished(QNetworkReply*)));
}
bool Downloader::doDownload(const Download &dl)
2015-11-23 02:33:01 +01:00
{
QUrl url(dl.url());
2015-12-04 22:56:34 +01:00
if (_errorDownloads.contains(url))
return false;
2015-12-04 22:56:34 +01:00
2015-11-23 02:33:01 +01:00
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::User, QVariant(dl.file()));
request.setRawHeader("User-Agent", USER_AGENT);
2015-12-04 22:56:34 +01:00
QNetworkReply *reply = _manager.get(request);
2015-11-23 02:33:01 +01:00
2015-12-04 22:56:34 +01:00
_currentDownloads.append(reply);
return true;
2015-11-23 02:33:01 +01:00
}
bool Downloader::saveToDisk(const QString &filename, QIODevice *data)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
fprintf(stderr, "Error writing map tile: %s: %s\n",
2015-11-23 02:33:01 +01:00
qPrintable(filename), qPrintable(file.errorString()));
return false;
}
file.write(data->readAll());
file.close();
return true;
}
void Downloader::downloadFinished(QNetworkReply *reply)
{
QUrl url = reply->url();
if (reply->error()) {
2015-12-04 22:56:34 +01:00
_errorDownloads.insert(url);
fprintf(stderr, "Error downloading map tile: %s: %s\n",
2015-11-23 02:33:01 +01:00
url.toEncoded().constData(), qPrintable(reply->errorString()));
} else {
QUrl redirect = reply->attribute(
QNetworkRequest::RedirectionTargetAttribute).toUrl();
2015-11-23 02:33:01 +01:00
QString filename = reply->request().attribute(QNetworkRequest::User)
.toString();
if (!redirect.isEmpty()) {
Download dl(redirect, filename);
doDownload(dl);
} else
2016-08-02 23:05:52 +02:00
if (!saveToDisk(filename, reply))
_errorDownloads.insert(url);
2015-11-23 02:33:01 +01:00
}
2015-12-04 22:56:34 +01:00
_currentDownloads.removeAll(reply);
2015-11-23 02:33:01 +01:00
reply->deleteLater();
2015-12-04 22:56:34 +01:00
if (_currentDownloads.isEmpty())
2015-11-23 02:33:01 +01:00
emit finished();
}
bool Downloader::get(const QList<Download> &list)
2015-11-23 02:33:01 +01:00
{
bool finishEmitted = false;
2015-11-23 02:33:01 +01:00
for (int i = 0; i < list.count(); i++)
finishEmitted |= doDownload(list.at(i));
return finishEmitted;
2015-11-23 02:33:01 +01:00
}