MapView sends always the center point from the users view
[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 Constructor.
51     *
52     * @param parent QObject
53     */
54     NetworkAccessManager(QObject *parent = 0);
55
56     /**
57     * @brief Returns connection state.
58     *
59     * @return true if connected, false otherwise
60     */
61     bool isConnected();
62
63     /**
64     * @brief Makes get request and returns reply.
65     *
66     * @param request QNetworkRequest
67     * @param onlineRequestsOnly true if does only requests when online, false otherwise
68     * @return QNetworkReply
69     */
70     QNetworkReply *get(const QNetworkRequest &request, bool onlineRequestsOnly = false);
71
72     /**
73     * @brief Makes post request and returns reply.
74     *
75     * @param request QNetworkRequest
76     * @param data QByteArray
77     * @param onlineRequestsOnly bool
78     * @return QNetworkReply
79     */
80     QNetworkReply *post(const QNetworkRequest &request, QByteArray &data,
81                          bool onlineRequestsOnly = false);
82
83     /**
84     * @brief Sets cache.
85     *
86     * @param cache QAbstractNetworkCache instance
87     */
88     void setCache(QAbstractNetworkCache *cache);
89
90     /**
91     * @brief Returns cache.
92     *
93     * @return QAbstractNetworkCache
94     */
95     QAbstractNetworkCache *cache() const;
96
97 private slots:
98     /**
99     * @brief Slot for network connected state.
100     */
101     void connected();
102
103     /**
104     * @brief Slot for network disconnected state.
105     */
106     void disconnected();
107
108     /**
109     * @brief Slot for finished download.
110     *
111     * @param reply reply from network
112     */
113     void downloadFinished(QNetworkReply *reply);
114
115 /*******************************************************************************
116  * SIGNALS
117  ******************************************************************************/
118 signals:
119     /**
120     * Signal for finished download.
121     *
122     * @param reply reply from network
123     */
124     void finished(QNetworkReply *reply);
125
126 /*******************************************************************************
127  * DATA MEMBERS
128  ******************************************************************************/
129 private:
130     bool m_connected;                                       ///< Connection flag
131     NetworkHandler *m_networkHandler;                       ///< Instance of NetworkHandler
132     QNetworkAccessManager *m_networkAccessManagerPrivate;   ///< Instance of QNetworkAccessManager
133     QList<QNetworkRequest> m_requestQueue;                  ///< Queue for requests
134     QHash<QString, QNetworkReply*> m_offlineReplyQueue;     ///< Queue for offline replies
135     QHash<QString, QNetworkReply*> m_temporaryReplyQueue;   ///< Queue for temporary replies
136 };
137
138 #endif // NETWORKACCESSMANAGER_H