Refactored to match server v2.0 changes.
[situare] / src / situareservice / situareservice.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       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
24 #ifndef SITUARESERVICE_H
25 #define SITUARESERVICE_H
26
27 #include <QObject>
28
29 #include "../user/user.h"
30 #include "imagefetcher.h"
31
32 class NetworkAccessManager;
33 class QNetworkReply;
34 class QNetworkRequest;
35 class GeoCoordinate;
36 class QUrl;
37
38 /**
39 * @brief SituareService class for communicating with Situare server
40 *
41 * @author Henri Lampela
42 * @author Sami Rämö - sami.ramo (at) ixonos.com
43 */
44 class SituareService : public QObject
45 {
46     Q_OBJECT
47
48 private:
49     /**
50     * @brief Request name enum
51     */
52     enum RequestName { RequestUnknown,
53                        RequestGetLocations,
54                        RequestUpdateLocation,
55                        RequestReverseGeo};
56
57 public:
58     /**
59     * @brief Default constructor
60     *
61     * @param parent instance of parent
62     */
63     SituareService(QObject *parent = 0);
64
65     /**
66     * @brief Destructor
67     *
68     */
69     ~SituareService();
70
71 /*******************************************************************************
72  * MEMBER FUNCTIONS AND SLOTS
73  ******************************************************************************/
74
75     /**
76     * @brief Retrieves location user and friends information from Situare server
77     *
78     */
79     void fetchLocations();
80
81     /**
82     * @brief Translates coordinates to street address via Situare server
83     *
84     * @param coordinates coordinates to be translated
85     */
86     void reverseGeo(const GeoCoordinate &coordinates);
87
88     /**
89     * @brief Updates location to the Situare server
90     *
91     * @param coordinates current cordinates
92     * @param status message
93     * @param publish publish location on Facebook wall
94     */
95     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
96
97 public slots:
98
99     /**
100     * @brief Public slot, to clear user data
101     *
102     */
103     void clearUserData();
104
105     /**
106     * @brief Public slot, which indicates when http request has been completed
107     *
108     * @param reply storage for http reply
109     */
110     void requestFinished(QNetworkReply *reply);
111
112     /**
113       * @brief Update session data
114       *
115       * @param session New session data
116       */
117     void updateSession(const QString &session);
118
119 private:
120
121     /**
122     * @brief Requests ImageFetcher if user/friend has a profile image
123     *        uses members: m_user and m_friendsList
124     *
125     * @param imageUrlList list of image urls
126     */
127     void addProfileImages(const QList<QUrl> &imageUrlList);
128
129     /**
130       * @brief Append access_token and other session data to the reques url
131       *
132       * @param[in,out] requestUrl Request URL with other request parameters and ending to &
133       */
134     void appendAccessToken(QString &requestUrl);
135
136     /**
137       * @brief Convert coordinate value in degrees (double) to string with enough precision
138       *
139       * @param degrees Coordinate value in degrees
140       * @returns Coordinate value as string
141       */
142     QString degreesToString(double degrees);
143
144     /**
145     * @brief Returns reuquest script's name.
146     *
147     * @param url url to check
148     * @return RequestName
149     */
150     SituareService::RequestName getRequestName(const QUrl &url) const;
151
152     /**
153     * @brief Parses reply from JSON string
154     *
155     * Calls different parse methods or emits error signal if response contains error status.
156     */
157     void parseReply(const QByteArray &jsonReply, RequestName requestName);
158
159     /**
160     * @brief Parses reverse geo data
161     *
162     * @param reverseGeoData reverse geo data QVariant tree
163     */
164     void parseReverseGeoData(const QVariant &reverseGeoData);
165
166     /**
167     * @brief Parses user and friend data from JSON string
168     *
169     * @param userData user data QVariant tree
170     */
171     void parseUserData(const QVariant &userData);
172
173     /**
174       * @brief Build and send request
175       *
176       * Appends script pathname and parameters to the server base URL. Access token is appended and
177       * the request sent if the access token is available, otherwise the request is queued.
178       *
179       * @param script Requested script pathname
180       * @param parameters Hash containing parameter key/value pairs.
181       */
182     void buildRequest(const QString &script, const QHash<QString,QString> &parameters);
183
184     /**
185       * @brief Send request
186       *
187       * @param requestUrl Request URL containing also all parameters
188       */
189     void sendRequest(const QString &requestUrl);
190
191 private slots:
192     /**
193     * @brief Slot for received images
194     *
195     * @param url Image url
196     * @param image Received image
197     */
198     void imageReceived(const QUrl &url, const QPixmap &image);
199
200 /*******************************************************************************
201  * SIGNALS
202  ******************************************************************************/
203
204 signals:
205
206     /**
207     * @brief Signals error
208     *
209     * @param context error context
210     * @param error error code
211     */
212     void error(const int context, const int error);
213
214     /**
215     * @brief Signal for image fetching
216     *
217     * @param url Image url
218     */
219     void fetchImage(const QUrl &url);
220
221     /**
222     * @brief Signals when user's/friend's image is downloaded
223     *
224     * @param user Instance of user/friend
225     */
226     void imageReady(User *user);
227
228     /**
229     * @brief Signals when address data is retrieved
230     *
231     * @param address Street address
232     */
233     void reverseGeoReady(const QString &address);
234
235     /**
236     * @brief Signals when updateLocation request finished successfully
237     */
238     void updateWasSuccessful();
239
240     /**
241     * @brief Signals when user data is retrieved
242     *
243     * @param user instance of user
244     * @param friendList list of friends
245     */
246     void userDataChanged(User *user, QList<User *> &friendList);
247
248 /*******************************************************************************
249  * DATA MEMBERS
250  ******************************************************************************/
251
252 private:
253     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
254
255     QList<QNetworkReply *> m_currentRequests;       ///< List of current http requests
256     QList<User *> m_friendsList;                    ///< List of friends(User)
257
258     QString m_session;                          ///< Session data
259
260     NetworkAccessManager *m_networkManager;     ///< Pointer to QNetworkAccessManager
261
262     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
263     User *m_user;                               ///< Pointer to User
264 };
265
266 #endif // SITUARESERVICE_H