ef7d503e481e2697f84fa30c27d75c5a92d78ad7
[situare] / src / situareservice / imagefetcher.h
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@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 IMAGEFETCHER_H
23 #define IMAGEFETCHER_H
24
25 #include <QtCore>
26
27 class NetworkAccessManager;
28 class QNetworkReply;
29 class QUrl;
30
31 /**
32 * @brief ImageFetcher handles requests to get images.
33 *
34 * @author Henri Lampela
35 */
36 class ImageFetcher : public QObject
37 {
38     Q_OBJECT
39
40 public:
41
42     /**
43     * @brief Default constructor
44     *
45     * @param manager QNetworkAccessManager
46     * @param parent Parent object
47     */
48     ImageFetcher(NetworkAccessManager *manager, QObject *parent = 0);
49
50 /*******************************************************************************
51  * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
52  ******************************************************************************/
53 public slots:
54
55     /**
56     * @brief Fecth image from given url
57     *
58     * @param url Image url
59     */
60     void fetchImage(const QUrl &url);
61
62 private slots:
63
64     /**
65     * @brief This slot is called when network manager has finished
66     * the download. Loads image and emits imageReceived signal with
67     * url and image. If there was a error in reply emits error-signal.
68     *
69     * @param reply
70     */
71     void downloadFinished(QNetworkReply *reply);
72
73     /**
74     * @brief This slot is called when next download is started. Takes url
75     * from queue, sends request and puts request to download queue.
76     */
77     void startNextDownload();
78
79 /*******************************************************************************
80  * SIGNALS
81  ******************************************************************************/
82 signals:
83     /**
84     * @brief Signal which is emitted when image
85     * is received from the server and loaded to pixmap.
86     *
87     * @param url URL to image
88     * @param image image QPixmap
89     */
90     void imageReceived(const QUrl &url, const QPixmap &image);
91
92     /**
93     * @brief Signal which is emitted when there is error
94     * in network reply.
95     *
96     * @param error error code
97     */
98     void error(const int error);
99
100 /*******************************************************************************
101  * DATA MEMBERS
102  ******************************************************************************/
103 private:
104     QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
105     QQueue<QUrl> m_downloadQueue;             ///< Queue of pending requests
106     NetworkAccessManager *m_manager;       ///< Network access manager
107 };
108
109 #endif // IMAGEFETCHER_H