1f8a1716bdfc4e65eec6fe45499e1c9a65160e72
[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 public:
49
50     /**
51     * @brief Default constructor
52     *
53     * @param parent instance of parent
54     */
55     SituareService(QObject *parent = 0);
56
57     /**
58     * @brief Destructor
59     *
60     */
61     ~SituareService();
62
63 /*******************************************************************************
64  * MEMBER FUNCTIONS AND SLOTS
65  ******************************************************************************/
66
67     /**
68     * @brief Retrieves location user and friends information from Situare server
69     *
70     */
71     void fetchLocations();
72
73     /**
74     * @brief Translates coordinates to street address via Situare server
75     *
76     * @param coordinates coordinates to be translated
77     */
78     void reverseGeo(const GeoCoordinate &coordinates);
79
80     /**
81     * @brief Updates location to the Situare server
82     *
83     * @param coordinates current cordinates
84     * @param status message
85     * @param publish publish location on Facebook wall
86     */
87     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
88
89 public slots:
90
91     /**
92     * @brief Public slot, to clear user data
93     *
94     */
95     void clearUserData();
96
97     /**
98     * @brief Public slot, which indicates when http request has been completed
99     *
100     * @param reply storage for http reply
101     */
102     void requestFinished(QNetworkReply *reply);
103
104     /**
105       * @brief Update session data
106       *
107       * @param session New session data
108       */
109     void updateSession(const QString &session);
110
111 private:
112
113     /**
114     * @brief Requests ImageFetcher if user/friend has a profile image
115     *        uses members: m_user and m_friendsList
116     *
117     * @param imageUrlList list of image urls
118     */
119     void addProfileImages(const QList<QUrl> &imageUrlList);
120
121     /**
122       * @brief Append access_token and other session data to the reques url
123       *
124       * @param[in,out] requestUrl Request URL with other request parameters and ending to &
125       */
126     void appendAccessToken(QString &requestUrl);
127
128     /**
129       * @brief Convert coordinate value in degrees (double) to string with enough precision
130       *
131       * @param degrees Coordinate value in degrees
132       * @returns Coordinate value as string
133       */
134     QString degreesToString(double degrees);
135
136     /**
137     * @brief Parses user and friend data from JSON string
138     *
139     * @param jsonReply JSON string
140     */
141     void parseUserData(const QByteArray &jsonReply);
142
143     /**
144       * @brief Build and send request
145       *
146       * Appends script pathname and parameters to the server base URL. Access token is appended and
147       * the request sent if the access token is available, otherwise the request is queued.
148       *
149       * @param script Requested script pathname
150       * @param parameters Hash containing parameter key/value pairs.
151       */
152     void buildRequest(const QString &script, const QHash<QString,QString> &parameters);
153
154     /**
155       * @brief Send request
156       *
157       * @param requestUrl Request URL containing also all parameters
158       */
159     void sendRequest(const QString &requestUrl);
160
161 private slots:
162     /**
163     * @brief Slot for received images
164     *
165     * @param url Image url
166     * @param image Received image
167     */
168     void imageReceived(const QUrl &url, const QPixmap &image);
169
170 /*******************************************************************************
171  * SIGNALS
172  ******************************************************************************/
173
174 signals:
175
176     /**
177     * @brief Signals error
178     *
179     * @param context error context
180     * @param error error code
181     */
182     void error(const int context, const int error);
183
184     /**
185     * @brief Signal for image fetching
186     *
187     * @param url Image url
188     */
189     void fetchImage(const QUrl &url);
190
191     /**
192     * @brief Signals when user's/friend's image is downloaded
193     *
194     * @param user Instance of user/friend
195     */
196     void imageReady(User *user);
197
198     /**
199     * @brief Signals when address data is retrieved
200     *
201     * @param address Street address
202     */
203     void reverseGeoReady(const QString &address);
204
205     /**
206     * @brief Signals when updateLocation request finished successfully
207     *
208     */
209     void updateWasSuccessful();
210
211     /**
212     * @brief Signals when user data is retrieved
213     *
214     * @param user instance of user
215     * @param friendList list of friends
216     */
217     void userDataChanged(User *user, QList<User *> &friendList);
218
219 /*******************************************************************************
220  * DATA MEMBERS
221  ******************************************************************************/
222
223 private:
224     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
225
226     QList<QNetworkReply *> m_currentRequests;       ///< List of current http requests
227     QList<User *> m_friendsList;                    ///< List of friends(User)
228
229     QString m_session;                          ///< Session data
230
231     NetworkAccessManager *m_networkManager;     ///< Pointer to QNetworkAccessManager
232
233     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
234     User *m_user;                               ///< Pointer to User
235 };
236
237 #endif // SITUARESERVICE_H