bf1418fbfaa94420d4ecf1ef882d8d770956a3c4
[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 "mapfetcher.h"
30
31 class QNetworkReply;
32 class QUrl;
33
34 /**
35 * @brief MapFetcher handles requests to get map tiles.
36 *
37 * @author Jussi Laitinen jussi.laitinen@ixonos.com
38 */
39 class MapFetcher : public QObject
40 {
41     Q_OBJECT
42
43 public:
44     /**
45     * @brief Constructor for MapFetcher.
46     *
47     * @param manager Network access manager
48     * @param parent parent object
49     */
50     MapFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
51
52 /*******************************************************************************
53  * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
54  ******************************************************************************/
55 public slots:
56     /**
57     * @brief Enqueue fetching of map image
58     *
59     * Image fetching is triggered after events have been processed
60     * so the UI stays responsive.
61     *
62     * @param url URL of the image to be fetched
63     */
64     void enqueueFetchMapImage(QUrl url);
65
66 private:
67     /**
68     * @brief Loads image from cache if it's available and emits imageReveived
69     * signal with url and image. If image is in cache returns true, false
70     * otherwise.
71     *
72     * @param url
73     * @return bool true if image was loaded from cache, false otherwise
74     */
75     bool loadImageFromCache(const QUrl &url);
76
77 private slots:
78
79     /**
80     * @brief This slot is called when network manager has finished
81     * the download. Loads image and emits imageReceived signal with
82     * url and image. If there was a error in reply emits error-signal.
83     *
84     * @param reply
85     */
86     void downloadFinished(QNetworkReply *reply);
87
88     /**
89     * @brief Fetch next map image.
90     *
91     * Next queued request is taken from the queue and fetching is started
92     */
93     void fetchMapImage();
94
95     /**
96     * @brief This slot is called when next download is started. Takes url
97     * from queue, sends request and puts request to download queue.
98     */
99     void startNextDownload();
100
101 /*******************************************************************************
102  * SIGNALS
103  ******************************************************************************/
104 signals:    
105     /**
106     * @brief Signal which is emitted when a map tile
107     * is received from the server and loaded to pixmap.
108     *
109     * @param url URL to image
110     * @param image image pixmap
111     */
112     void mapImageReceived(const QUrl &url, const QPixmap &image);
113
114     /**
115     * @brief Signal which is emitted when there is error
116     * in network reply.
117     *
118     * @param message error message
119     */
120     void error(const QString &message);
121
122 /*******************************************************************************
123  * DATA MEMBERS
124  ******************************************************************************/
125 private:
126     static const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
127     static const int DOWNLOAD_QUEUE_SIZE = 50; ///< Max downloads waiting in queue
128
129     QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
130     QQueue<QUrl> m_downloadQueue;             ///< Queue of pending requests
131     QList<QUrl> m_fetchMapImagesList; ///< "Queue" for map image fetching requests
132     bool m_isFetchMapImagesTimerRunning; ///< is the singleshot timer already running
133     QNetworkAccessManager *m_manager;       ///< Network access manager
134 };
135
136 #endif