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,
27 #include <QNetworkAccessManager>
29 #include "maptilerequest.h"
30 #include "network/networkaccessmanager.h"
36 * @brief MapFetcher handles requests to get map tiles.
38 * @author Jussi Laitinen jussi.laitinen@ixonos.com
39 * @author Sami Rämö sami.ramo@ixonos.com
41 class MapFetcher : public QObject
47 * @brief Constructor for MapFetcher.
49 * @param manager Network access manager
50 * @param parent parent object
52 MapFetcher(NetworkAccessManager *manager, QObject *parent = 0);
54 /*******************************************************************************
55 * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
56 ******************************************************************************/
59 * @brief Set size of the download queue
61 * @param size New size
63 void setDownloadQueueSize(int size);
67 * @brief Enqueue fetching of map image
69 * Image fetching is triggered after events have been processed
70 * so the UI stays responsive.
72 * @param zoomLevel Zoom level
73 * @param x Tile x index
74 * @param y Tile y index
76 void enqueueFetchMapImage(int zoomLevel, int x, int y);
80 * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
82 * @param zoomLevel Zoom level
83 * @param tileNumbers Tile x & y numbers
84 * @return URL for the required tile
86 QUrl buildURL(int zoomLevel, QPoint tileNumbers);
89 * @brief Limit pending requests list size to m_pendingRequestsSize
92 void limitPendingRequestsListSize();
95 * @brief Loads image from cache
97 * Tries to load requested, or upper zoom level image from the cache. Emits imageReveived signal
98 * if any image was found. If image found was from requested zoom level then expidation date is
101 * @param url URL of the requested image
102 * @return True if requested zoom level image was found and was not expired. Otherwise false.
104 bool loadImageFromCache(const QUrl &url);
107 * @brief Find first item based on criteria if the request is already checked from the cache
109 * If cacheChecked is true, then returns index of the first request which is already
110 * checked from the cache. If cacheChecked is false then returns first item which
111 * isn't checked from the cache. Returns -1 if the item is not found.
113 * @param cacheChecked Search criteria
114 * @return Index of the first matching request, or -1 if not found.
116 int newestRequestIndex(bool cacheChecked);
119 * @brief Parse given URL to zoom, x and y values. Parsed values are
120 * placed in variables given as parameters.
122 * @param url url to parse
123 * @param [out] zoom zoom variable
124 * @param [out] x x variable
125 * @param [out] y y variable
127 void parseURL(const QUrl &url, int *zoom, int *x, int *y);
130 * @brief Translate indexes to matching upper level map tile indexes
131 * @param[in,out] zoomLevel Zoom level
132 * @param[in,out] x x index
133 * @param[in,out] y y index
134 * @return true if translation succeeded, otherwise false
136 bool translateIndexesToUpperLevel(int &zoomLevel, int &x, int &y);
141 * @brief This slot is called when network manager has finished
142 * the download. Loads image and emits imageReceived signal with
143 * url and image. If there was a error in reply emits error-signal.
147 void downloadFinished(QNetworkReply *reply);
150 * @brief Check next request if it is found from cache
152 * Next queued request, which is not already checked against cache, is taken
153 * from the queue and checked against cache. If not found from cache, then
154 * cache checked flag is set. New download is started if there aren't too
155 * many simultaneus downloads already running.
157 void checkNextRequestFromCache();
160 * @brief This slot is called when next download is started. Takes url
161 * from queue, sends request and puts request to download queue.
163 void startNextDownload();
165 /*******************************************************************************
167 ******************************************************************************/
170 * @brief Signal which is emitted when a map tile
171 * is received from the server and loaded to pixmap.
173 * @param zoomLevel Zoom level
174 * @param x Tile x index
175 * @param y Tile y index
176 * @param image image pixmap
178 void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
181 * @brief Signals error
183 * @param context error context
184 * @param error error code
186 void error(const int context, const int error);
188 /*******************************************************************************
190 ******************************************************************************/
192 QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
193 int m_pendingRequestsSize; ///< Max number of pending requests
194 bool m_fetchMapImagesTimerRunning; ///< is the singleshot timer already running
195 NetworkAccessManager *m_manager; ///< Network access manager
196 QList<MapTileRequest> m_pendingRequests; ///< List of map image fetching requests