Code refactoring.
[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
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.
10
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.
15
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,
19    USA.
20 */
21
22 #ifndef MAPFETCHER_H
23 #define MAPFETCHER_H
24
25 #include <QtCore>
26 #include <QNetworkAccessManager>
27
28 #include "mapfetcher.h"
29
30 class QNetworkReply;
31 class QUrl;
32
33 /**
34 * @brief MapFetcher handles requests to get map tiles.
35 *
36 * @author Jussi Laitinen jussi.laitinen@ixonos.com
37 */
38 class MapFetcher : public QObject
39 {
40     Q_OBJECT
41
42 public:
43     /**
44       * @todo remove fn:s from comment blocks
45       *
46       * DONE
47       */
48     /**
49     * @brief Constructor for MapFetcher.
50     *
51     * @param parent parent object
52     */
53     MapFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
54
55     ~MapFetcher();
56
57     /**
58     * @brief Fetch image from given URL.
59     *
60     * @param url URL to image
61     */
62     void fetchMapImage(const QUrl &url);
63
64 signals:    
65     /**
66     * @brief Signal which is emitted when a map tile
67     * is received from the server and loaded to pixmap.
68     *
69     * @param url URL to image
70     * @param image image pixmap
71     */
72     void mapImageReceived(const QUrl &url, const QPixmap &image);
73
74     /**
75     * @brief Signal which is emitted when there is error
76     * in network reply.
77     *
78     * @param message error message
79     */
80     void error(const QString &message);
81
82 private slots:
83
84     /**
85     * @brief This slot is called when network manager has finished
86     * the download. Loads image and emits imageReceived signal with
87     * url and image. If there was a error in reply emits error-signal.
88     *
89     * @param reply
90     */
91     void downloadFinished(QNetworkReply *reply);
92
93     /**
94     * @brief This slot is called when next download is started. Takes url
95     * from queue, sends request and puts request to download queue.
96     */
97     void startNextDownload();
98
99 private:
100
101     /**
102     * @brief Loads image from cache if it's available and emits imageReveived
103     * signal with url and image. If image is in cache returns true, false
104     * otherwise.
105     *
106     * @param url
107     * @return bool true if image was loaded from cache, false otherwise
108     */
109     bool loadImageFromCache(const QUrl &url);
110 /**
111   * @todo add comments for data members
112   *
113   * DONE
114   */
115     QNetworkAccessManager *m_manager;       ///< Network access manager
116     QList<QNetworkReply*> currentDownloads; ///< List of current downloads
117     QQueue<QUrl> downloadQueue;             ///< Queue of pending requests
118 };
119
120 #endif