Fixed unit test of MapView
[situare] / src / network / networkaccessmanager.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 NETWORKACCESSMANAGER_H
23 #define NETWORKACCESSMANAGER_H
24
25 #include <QList>
26 #include <QHash>
27 #include <QNetworkReply>
28 #include <QNetworkRequest>
29 #include <QAbstractNetworkCache>
30
31 class NetworkHandler;
32 class QNetworkAccessManager;
33
34 /**
35 * @brief NetworkAccessManager class.
36 *
37 * This class handles network requests and receives network replies.
38 * NetworkAccessManager queues requests when disconnected from network
39 * and makes requests when connected to network.
40 */
41 class NetworkAccessManager : public QObject
42 {
43     Q_OBJECT
44
45 /*******************************************************************************
46  * MEMBER FUNCTIONS AND SLOTS
47  ******************************************************************************/
48 public:
49     /**
50     * @brief Returns instance of NetworkAccessManager.
51     *
52     * Creates instance if not created.
53     */
54     static NetworkAccessManager *instance();
55
56     /**
57     * @brief Makes request and return reply.
58     *
59     * @param request QNetworkRequest
60     * @return QNetworkReply
61     */
62     QNetworkReply *get(const QNetworkRequest &request);
63
64     /**
65     * @brief Sets cache.
66     *
67     * @param cache QAbstractNetworkCache instance
68     */
69     void setCache(QAbstractNetworkCache *cache);
70
71     /**
72     * @brief Returns cache.
73     *
74     * @return QAbstractNetworkCache
75     */
76     QAbstractNetworkCache *cache() const;
77
78 protected:
79     /**
80     * @brief Constructor.
81     *
82     * Instance of this class can only be created by using instance method.
83     */
84     NetworkAccessManager();
85
86 private slots:
87     /**
88     * @brief Slot for network connected state.
89     */
90     void connected();
91
92     /**
93     * @brief Slot for finished download.
94     *
95     * @param reply reply from network
96     */
97     void downloadFinished(QNetworkReply *reply);
98
99 /*******************************************************************************
100  * SIGNALS
101  ******************************************************************************/
102 signals:
103     /**
104     * Signal for finished download.
105     *
106     * @param reply reply from network
107     */
108     void finished(QNetworkReply *reply);
109
110 /*******************************************************************************
111  * DATA MEMBERS
112  ******************************************************************************/
113 private:
114     static NetworkAccessManager *m_instance;                ///< Instance of NetworkAccessManager
115     NetworkHandler *m_networkHandler;                       ///< Instance of NetworkHandler
116     QNetworkAccessManager *m_networkAccessManagerPrivate;   ///< Instance of QNetworkAccessManager
117     QList<QNetworkRequest> m_requestQueue;                  ///< Queue for requests
118     QHash<QString, QNetworkReply*> m_offlineReplyQueue;     ///< Queue for offline replies
119     QHash<QString, QNetworkReply*> m_temporaryReplyQueue;   ///< Queue for temporary replies
120 };
121
122 #endif // NETWORKACCESSMANAGER_H