1 #include <QNetworkAccessManager>
2 #include <QNetworkRequest>
3 #include <QNetworkReply>
7 #include <QNetworkDiskCache>
8 #include <QDesktopServices>
10 #include "mapfetcher.h"
12 static int MAX_PARALLEL_DOWNLOADS = 2;
14 MapFetcher::MapFetcher(QObject *parent)
17 QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
18 diskCache->setCacheDirectory(QDesktopServices::storageLocation(
19 QDesktopServices::CacheLocation));
20 m_manager.setCache(diskCache);
22 connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
25 void MapFetcher::fetchMapImage(const QUrl &url)
27 qDebug() << "fetchMapImage()";
31 //Limit parallel downloads
32 if (downloadQueue.length() < MAX_PARALLEL_DOWNLOADS)
33 QTimer::singleShot(0, this, SLOT(startNextDownload()));
35 downloadQueue.enqueue(url);
38 void MapFetcher::startNextDownload()
40 qDebug() << "startNextDownload()";
41 if (downloadQueue.isEmpty())
44 QUrl url = downloadQueue.dequeue();
45 QNetworkRequest request(url);
46 request.setRawHeader("User-Agent", "Map Test");
47 request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
48 QNetworkRequest::PreferCache);
50 QNetworkReply *reply = m_manager.get(request);
52 currentDownloads.append(reply);
55 void MapFetcher::downloadFinished(QNetworkReply *reply)
57 qDebug() << "downloadFinished()";
59 emit error(reply->errorString());
63 QUrl url = reply->url();
64 if (!image.load(reply, 0))
67 emit mapImageReceived(url, QPixmap::fromImage(image));
70 currentDownloads.removeAll(reply);
75 MapFetcher::~MapFetcher()