+/*
+ Situare - A location system for Facebook
+ Copyright (C) 2010 Ixonos Plc. Authors:
+
+ Jussi Laitinen - jussi.laitinen@ixonos.com
+
+ Situare is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ version 2 as published by the Free Software Foundation.
+
+ Situare is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Situare; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ USA.
+*/
+
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QDebug>
#include <QPixmap>
+#include <QNetworkDiskCache>
+#include <QDesktopServices>
#include "mapfetcher.h"
-MapFetcher::MapFetcher(QObject *parent)
- : QObject(parent)
+static int MAX_PARALLEL_DOWNLOADS = 2;
+
+MapFetcher::MapFetcher(QObject *parent, QNetworkAccessManager *manager)
+ : QObject(parent), m_manager(manager)
{
- connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
+ QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
+
+ diskCache->setCacheDirectory(QDesktopServices::storageLocation(
+ QDesktopServices::CacheLocation));
+
+ m_manager->setCache(diskCache);
+
+ connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
}
void MapFetcher::fetchMapImage(const QUrl &url)
{
- if (url.isEmpty())
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (url.isEmpty() || !url.isValid())
+ return;
+
+ if (downloadQueue.length() < MAX_PARALLEL_DOWNLOADS)
+ QTimer::singleShot(0, this, SLOT(startNextDownload()));
+
+ downloadQueue.enqueue(url);
+}
+
+void MapFetcher::startNextDownload()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (downloadQueue.isEmpty())
return;
+ QUrl url = downloadQueue.dequeue();
QNetworkRequest request(url);
request.setRawHeader("User-Agent", "Map Test");
- qDebug() << request.url();
- m_manager.get(request);
+ request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
+ QNetworkRequest::PreferCache);
+
+ QNetworkReply *reply = m_manager->get(request);
+ currentDownloads.append(reply);
}
void MapFetcher::downloadFinished(QNetworkReply *reply)
{
- qDebug() << "downloadFinished()";
- if (reply->error()) {
- qDebug() << reply->errorString();
- emit error(reply->errorString());
- }
- else {
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (reply->error() == QNetworkReply::NoError) {
QImage image;
QUrl url = reply->url();
+
if (!image.load(reply, 0))
image = QImage();
- emit mapImageReceived(image);
+ emit mapImageReceived(url, QPixmap::fromImage(image));
+ }
+ else {
+ emit error(reply->errorString());
}
+ currentDownloads.removeAll(reply);
reply->deleteLater();
+ startNextDownload();
}
MapFetcher::~MapFetcher()