Updated error handling, added error contexts. Fixed fullscreen button
[situare] / src / map / mapfetcher.h
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6        Sami Rämö - sami.ramo@ixonos.com
7
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.
11
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.
16
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,
20    USA.
21 */
22
23 #ifndef MAPFETCHER_H
24 #define MAPFETCHER_H
25
26 #include <QtCore>
27 #include <QNetworkAccessManager>
28
29 #include "maptilerequest.h"
30 #include "network/networkaccessmanager.h"
31
32 class QNetworkReply;
33 class QUrl;
34
35 /**
36 * @brief MapFetcher handles requests to get map tiles.
37 *
38 * @author Jussi Laitinen jussi.laitinen@ixonos.com
39 * @author Sami Rämö sami.ramo@ixonos.com
40 */
41 class MapFetcher : public QObject
42 {
43     Q_OBJECT
44
45 public:
46     /**
47     * @brief Constructor for MapFetcher.
48     *
49     * @param manager Network access manager
50     * @param parent parent object
51     */
52     MapFetcher(NetworkAccessManager *manager, QObject *parent = 0);
53
54 /*******************************************************************************
55  * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
56  ******************************************************************************/
57 public:
58     /**
59     * @brief Set size of the download queue
60     *
61     * @param size New size
62     */
63     void setDownloadQueueSize(int size);
64
65 public slots:
66     /**
67     * @brief Enqueue fetching of map image
68     *
69     * Image fetching is triggered after events have been processed
70     * so the UI stays responsive.
71     *
72     * @param zoomLevel Zoom level
73     * @param x Tile x index
74     * @param y Tile y index
75     */
76     void enqueueFetchMapImage(int zoomLevel, int x, int y);
77
78 private:
79     /**
80     * @brief Build URL for donwloading single map tile from OpenStreetMap tile server
81     *
82     * @param zoomLevel Zoom level
83     * @param tileNumbers Tile x & y numbers
84     * @return URL for the required tile
85     */
86     QUrl buildURL(int zoomLevel, QPoint tileNumbers);
87
88     /**
89     * @brief Limit pending requests list size to m_pendingRequestsSize
90     *
91     */
92     void limitPendingRequestsListSize();
93
94     /**
95     * @brief Loads image from cache
96     *
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
99     * checked.
100     *
101     * @param url URL of the requested image
102     * @return True if requested zoom level image was found and was not expired. Otherwise false.
103     */
104     bool loadImageFromCache(const QUrl &url);
105
106     /**
107     * @brief Find first item based on criteria if the request is already checked from the cache
108     *
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.
112     *
113     * @param cacheChecked Search criteria
114     * @return Index of the first matching request, or -1 if not found.
115     */
116     int newestRequestIndex(bool cacheChecked);
117
118     /**
119     * @brief Parse given URL to zoom, x and y values. Parsed values are
120     * placed in variables given as parameters.
121     *
122     * @param url url to parse
123     * @param [out] zoom zoom variable
124     * @param [out] x x variable
125     * @param [out] y y variable
126     */
127     void parseURL(const QUrl &url, int *zoom, int *x, int *y);
128
129     /**
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
135     */
136     bool translateIndexesToUpperLevel(int &zoomLevel, int &x, int &y);
137
138 private slots:
139
140     /**
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.
144     *
145     * @param reply
146     */
147     void downloadFinished(QNetworkReply *reply);
148
149     /**
150     * @brief Check next request if it is found from cache
151     *
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.
156     */
157     void checkNextRequestFromCache();
158
159     /**
160     * @brief This slot is called when next download is started. Takes url
161     * from queue, sends request and puts request to download queue.
162     */
163     void startNextDownload();
164
165 /*******************************************************************************
166  * SIGNALS
167  ******************************************************************************/
168 signals:    
169     /**
170     * @brief Signal which is emitted when a map tile
171     * is received from the server and loaded to pixmap.
172     *
173     * @param zoomLevel Zoom level
174     * @param x Tile x index
175     * @param y Tile y index
176     * @param image image pixmap
177     */
178     void mapImageReceived(int zoomLevel, int x, int y, const QPixmap &image);
179
180     /**
181     * @brief Signals error
182     *
183     * @param context error context
184     * @param error error code
185     */
186     void error(const int context, const int error);
187
188 /*******************************************************************************
189  * DATA MEMBERS
190  ******************************************************************************/
191 private:
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
197 };
198
199 #endif