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"
32 #include "mapcommon.h"
34 MapFetcher::MapFetcher(QNetworkAccessManager *manager, QObject *parent)
38 QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
39 diskCache->setCacheDirectory(QDesktopServices::storageLocation(
40 QDesktopServices::CacheLocation));
41 m_manager->setCache(diskCache);
43 connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(
44 downloadFinished(QNetworkReply*)));
47 void MapFetcher::fetchMapImage(const QUrl &url)
49 //qDebug() << __PRETTY_FUNCTION__;
51 if (url.isEmpty() || !url.isValid())
54 if (loadImageFromCache(url))
57 if (m_downloadQueue.size() >= DOWNLOAD_QUEUE_SIZE)
58 m_downloadQueue.dequeue();
60 m_downloadQueue.enqueue(url);
62 if (m_currentDownloads.size() < MAX_PARALLEL_DOWNLOADS)
66 bool MapFetcher::loadImageFromCache(const QUrl &url)
68 //qDebug() << __PRETTY_FUNCTION__;
70 bool imageFound = false;
72 if (m_manager->cache()) {
73 QIODevice *cacheImage = m_manager->cache()->data(url);
78 if (image.load(cacheImage, 0)) {
80 emit mapImageReceived(url, QPixmap::fromImage(image));
90 void MapFetcher::startNextDownload()
92 //qDebug() << __PRETTY_FUNCTION__;
94 if (m_downloadQueue.isEmpty())
97 QUrl url = m_downloadQueue.dequeue();
99 QNetworkRequest request(url);
100 /// @todo Jussi: set user agent DONE
101 request.setRawHeader("User-Agent", "Situare");
102 QNetworkReply *reply = m_manager->get(request);
104 m_currentDownloads.append(reply);
107 void MapFetcher::downloadFinished(QNetworkReply *reply)
109 //qDebug() << __PRETTY_FUNCTION__;
111 if (reply->error() == QNetworkReply::NoError) {
113 QUrl url = reply->url();
115 if (!image.load(reply, 0))
118 emit mapImageReceived(url, QPixmap::fromImage(image));
121 emit error(reply->errorString());
124 m_currentDownloads.removeAll(reply);
125 reply->deleteLater();
129 /// @todo Jussi: remove destructor DONE