2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
7 Situare is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 Situare is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Situare; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 #include <QNetworkAccessManager>
23 #include <QNetworkRequest>
24 #include <QNetworkReply>
28 #include <QNetworkDiskCache>
29 #include <QDesktopServices>
31 #include "mapfetcher.h"
33 static int MAX_PARALLEL_DOWNLOADS = 2;
35 MapFetcher::MapFetcher(QObject *parent, QNetworkAccessManager *manager)
36 : QObject(parent), m_manager(manager)
38 QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
40 diskCache->setCacheDirectory(QDesktopServices::storageLocation(
41 QDesktopServices::CacheLocation));
43 m_manager->setCache(diskCache);
45 connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
48 void MapFetcher::fetchMapImage(const QUrl &url)
50 qDebug() << __PRETTY_FUNCTION__;
52 if (url.isEmpty() || !url.isValid())
55 if (downloadQueue.length() < MAX_PARALLEL_DOWNLOADS)
56 QTimer::singleShot(0, this, SLOT(startNextDownload()));
58 downloadQueue.enqueue(url);
61 void MapFetcher::startNextDownload()
63 qDebug() << __PRETTY_FUNCTION__;
65 if (downloadQueue.isEmpty())
68 QUrl url = downloadQueue.dequeue();
69 QNetworkRequest request(url);
70 request.setRawHeader("User-Agent", "Map Test");
71 request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
72 QNetworkRequest::PreferCache);
74 QNetworkReply *reply = m_manager->get(request);
75 currentDownloads.append(reply);
78 void MapFetcher::downloadFinished(QNetworkReply *reply)
80 qDebug() << __PRETTY_FUNCTION__;
82 if (reply->error() == QNetworkReply::NoError) {
84 QUrl url = reply->url();
86 if (!image.load(reply, 0))
89 emit mapImageReceived(url, QPixmap::fromImage(image));
92 emit error(reply->errorString());
95 currentDownloads.removeAll(reply);
100 MapFetcher::~MapFetcher()