2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Jussi Laitinen - jussi.laitinen@ixonos.com
6 Sami Rämö - sami.ramo@ixonos.com
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 #include <QNetworkAccessManager>
24 #include <QNetworkRequest>
25 #include <QNetworkReply>
29 #include <QNetworkDiskCache>
30 #include <QDesktopServices>
32 #include "mapfetcher.h"
33 #include "mapcommon.h"
34 #include "engine/networkaccessmanager.h"
36 const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
37 const int NOT_FOUND = -1; ///< Return value if matching request is not found from the list
39 MapFetcher::MapFetcher(NetworkAccessManager *manager, QObject *parent)
41 , m_pendingRequestsSize(0)
42 , m_fetchMapImagesTimerRunning(false)
45 qDebug() << __PRETTY_FUNCTION__;
47 QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
48 diskCache->setCacheDirectory(QDesktopServices::storageLocation(
49 QDesktopServices::CacheLocation));
50 m_manager->setCache(diskCache);
52 connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(
53 downloadFinished(QNetworkReply*)));
56 QUrl MapFetcher::buildURL(int zoomLevel, QPoint tileNumbers)
58 qDebug() << __PRETTY_FUNCTION__;
61 * @brief Map server string for building actual URL
67 * NOTE: If the URL is changed, then the parseURL method must be changed to match
68 * the new URL structure
72 const QString MAP_SERVER_URL = QString("http://tile.openstreetmap.org/mapnik/%1/%2/%3.png");
74 return QString(MAP_SERVER_URL)
75 .arg(zoomLevel).arg(tileNumbers.x()).arg(tileNumbers.y());
78 void MapFetcher::checkNextRequestFromCache()
80 qDebug() << __PRETTY_FUNCTION__;
82 int i = newestRequestIndex(false);
85 QUrl url = m_pendingRequests[i].url;
86 if (!url.isEmpty() && url.isValid()) {
87 if (loadImageFromCache(url)) {
88 // was found, remove from the list
89 m_pendingRequests.removeAt(i);
92 // didn't found from cache so mark cache checked and leave to queue
93 m_pendingRequests[i].cacheChecked = true;
95 if (m_currentDownloads.size() < MAX_PARALLEL_DOWNLOADS)
101 // schedule checking of the next request if the list is not empty
102 if (newestRequestIndex(false) != NOT_FOUND)
103 QTimer::singleShot(0, this, SLOT(checkNextRequestFromCache()));
105 m_fetchMapImagesTimerRunning = false;
108 void MapFetcher::downloadFinished(QNetworkReply *reply)
110 qDebug() << __PRETTY_FUNCTION__;
112 if (m_currentDownloads.contains(reply)) {
114 if (reply->error() == QNetworkReply::NoError) {
116 QUrl url = reply->url();
118 if (!image.load(reply, 0))
124 parseURL(url, &zoomLevel, &x, &y);
126 emit mapImageReceived(zoomLevel, x, y, QPixmap::fromImage(image));
129 emit error(reply->errorString());
132 m_currentDownloads.removeAll(reply);
133 reply->deleteLater();
138 void MapFetcher::enqueueFetchMapImage(int zoomLevel, int x, int y)
140 QUrl url = buildURL(zoomLevel, QPoint(x, y));
142 // check if new request is already in the list and move it to the begin of the list...
144 for (int i = 0; i < m_pendingRequests.size(); i++) {
145 if (m_pendingRequests[i].url == url) {
146 m_pendingRequests.move(i, 0);
151 // ...or add new request to the begining of the list
153 MapTileRequest request(url);
154 m_pendingRequests.prepend(request);
157 limitPendingRequestsListSize();
159 if (!m_fetchMapImagesTimerRunning) {
160 m_fetchMapImagesTimerRunning = true;
161 QTimer::singleShot(0, this, SLOT(checkNextRequestFromCache()));
165 void MapFetcher::limitPendingRequestsListSize()
167 qDebug() << __PRETTY_FUNCTION__;
169 while (m_pendingRequests.size() > m_pendingRequestsSize) {
170 m_pendingRequests.removeLast();
174 bool MapFetcher::loadImageFromCache(const QUrl &url)
176 qDebug() << __PRETTY_FUNCTION__;
178 bool imageFound = false;
180 QAbstractNetworkCache *cache = m_manager->cache();
187 parseURL(url, &zoomLevel, &x, &y);
188 int originalZoomLevel = zoomLevel;
190 // try to fetch requested and upper level images until found or all levels tried
192 QIODevice *cacheImage = cache->data(buildURL(zoomLevel, QPoint(x, y)));
195 if (pixmap.loadFromData(cacheImage->readAll())) {
197 emit mapImageReceived(zoomLevel, x, y, pixmap);
202 } while (!imageFound && translateIndexesToUpperLevel(zoomLevel, x, y));
204 // check expiration if image was found from requested level
205 if (imageFound && (originalZoomLevel == zoomLevel)) {
206 // check if image is expired
207 QNetworkCacheMetaData metaData = cache->metaData(url);
208 if ((metaData.expirationDate().isValid()) && (url.isValid())) {
210 if (metaData.expirationDate() < QDateTime::currentDateTime()) {
217 // if image was found, but from upper level, return false
218 if (imageFound && (originalZoomLevel != zoomLevel))
225 bool MapFetcher::translateIndexesToUpperLevel(int &zoomLevel, int &x, int &y)
227 qDebug() << __PRETTY_FUNCTION__;
229 if (zoomLevel > MIN_MAP_ZOOM_LEVEL) {
240 int MapFetcher::newestRequestIndex(bool cacheChecked)
242 qDebug() << __PRETTY_FUNCTION__;
244 for (int i = 0; i < m_pendingRequests.size(); i++) {
245 if (m_pendingRequests[i].cacheChecked == cacheChecked) {
253 void MapFetcher::parseURL(const QUrl &url, int *zoom, int *x, int *y)
255 qDebug() << __PRETTY_FUNCTION__;
257 QString path = url.path();
258 QStringList pathParts = path.split("/", QString::SkipEmptyParts);
260 int size = pathParts.size();
262 // Example URL: "http://tile.openstreetmap.org/mapnik/14/9354/4263.png"
263 const int MIN_PATH_SPLITTED_PARTS = 4;
264 const int ZOOM_INDEX = size - 3;
265 const int X_INDEX = size - 2;
266 const int Y_INDEX = size - 1;
267 const int FILE_EXTENSION_LENGTH = 4;
269 if (size >= MIN_PATH_SPLITTED_PARTS) {
270 *zoom = (pathParts.at(ZOOM_INDEX)).toInt();
271 *x = (pathParts.at(X_INDEX)).toInt();
272 QString yString = pathParts.at(Y_INDEX);
273 yString.chop(FILE_EXTENSION_LENGTH);
274 *y = yString.toInt();
278 void MapFetcher::setDownloadQueueSize(int size)
280 qDebug() << __PRETTY_FUNCTION__ << "size:" << size;
282 m_pendingRequestsSize = size;
283 limitPendingRequestsListSize();
286 void MapFetcher::startNextDownload()
288 qDebug() << __PRETTY_FUNCTION__;
290 int i = newestRequestIndex(true);
292 if (i != NOT_FOUND) {
293 QUrl url = m_pendingRequests.takeAt(i).url;
295 QNetworkRequest request(url);
296 request.setRawHeader("User-Agent", "Situare");
297 QNetworkReply *reply = m_manager->get(request);
299 m_currentDownloads.append(reply);