Merge branch 'master' into situare_interact
[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       Jussi Laitinen - jussi.laitinen@ixonos.com
7       Sami Rämö - sami.ramo@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24
25 #ifndef SITUARESERVICE_H
26 #define SITUARESERVICE_H
27
28 #include <QObject>
29
30 #include "../user/user.h"
31 #include "imagefetcher.h"
32 #include "message.h"
33
34 class Database;
35 class NetworkAccessManager;
36 class QNetworkReply;
37 class QNetworkRequest;
38 class GeoCoordinate;
39 class QUrl;
40
41 /**
42 * @brief SituareService class for communicating with Situare server
43 *
44 * @author Henri Lampela
45 * @author Jussi Laitinen - jussi.laitinen (at) ixonos.com
46 * @author Sami Rämö - sami.ramo (at) ixonos.com
47 */
48 class SituareService : public QObject
49 {
50     Q_OBJECT
51
52 public:
53
54     /**
55     * Unit test class
56     */
57     friend class TestSituareService;
58
59     enum SuccessfulMethod {SuccessfulUpdateLocation, SuccessfulAddTags, SuccessfulRemoveTags,
60                            SuccessfulSendMessage, SuccessfulRemoveMessage};
61
62     /**
63     * @brief Default constructor
64     *
65     * @param parent instance of parent
66     */
67     SituareService(NetworkAccessManager *networkManager, ImageFetcher *imageFetcher,
68                    QObject *parent = 0);
69
70     /**
71     * @brief Destructor
72     *
73     */
74     ~SituareService();
75
76 /*******************************************************************************
77  * MEMBER FUNCTIONS AND SLOTS
78  ******************************************************************************/
79     /**
80     * @brief Retrieves people with similart interest (same tags).
81     *
82     * People is searched from area defined by south-west and north-east bounds.
83     * @param southWest south-west coordinates of bounds
84     * @param northEast north-east coordinates of bounds
85     */
86     void fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
87                                         const GeoCoordinate &northEastCoordinates);
88
89     /**
90     * @brief Retrieves location user and friends information from Situare server
91     *
92     */
93     void fetchLocations();
94
95     /**
96     * @brief Translates coordinates to street address via Situare server
97     *
98     * @param coordinates coordinates to be translated
99     */
100     void reverseGeo(const GeoCoordinate &coordinates);
101
102     /**
103     * @brief Updates location to the Situare server
104     *
105     * @param coordinates current cordinates
106     * @param status message
107     * @param publish publish location on Facebook wall
108     */
109     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
110
111 public slots:
112     /**
113     * @brief Adds tags to the Situare server
114     *
115     * CURRENTLY TAGS ARE UPDATED TO THE LOCAL DATABASE, NOT SITUARE SERVER
116     * @param tags list of user's tags
117     */
118     void addTags(const QStringList &tags);
119
120     /**
121     * @brief Public slot, to clear user data
122     *
123     */
124     void clearUserData();
125
126     /**
127     * @brief Retrieves messages sent to user.
128     */
129     void fetchMessages();
130
131     /**
132     * @brief Retrieves popular tags.
133     *
134     * Tags are fetch from local database instead of Situare server.
135     */
136     void fetchPopularTags();
137
138     /**
139     * @brief Removes message.
140     *
141     * @param id message ID
142     */
143     void removeMessage(const QString &id);
144
145     /**
146     * @brief Removes tags.
147     *
148     * @param tags list of tags to remove
149     */
150     void removeTags(const QStringList &tags);
151
152     /**
153     * @brief Public slot, which indicates when http request has been completed
154     *
155     * @param reply storage for http reply
156     */
157     void requestFinished(QNetworkReply *reply);
158
159     /**
160     * @brief Searches people by tag name.
161     *
162     * @param tag tag name
163     */
164     void searchPeopleByTag(const QString &tag);
165
166     /**
167     * @brief Sends a message to a person.
168     *
169     * @param receiverId Facebook user ID
170     * @param message message text
171     * @param message coordinates
172     */
173     void sendMessage(const QString &receiverId, const QString &message,
174                      const GeoCoordinate &coordinates = GeoCoordinate());
175
176     /**
177       * @brief Update session data
178       *
179       * @param session New session data
180       */
181     void updateSession(const QString &session);
182
183 private:
184     /**
185     * @brief Requests ImageFetcher if user/friend has a profile image
186     *        uses members: m_user and m_friendsList
187     *
188     * @param imageUrlList list of image urls
189     */
190     void addProfileImages(const QHash<QString, QUrl> &imageUrlList);
191
192     /**
193       * @brief Append access_token and other session data to the reques url
194       *
195       * @param[in,out] requestUrl Request URL with other request parameters and ending to &
196       */
197     void appendAccessToken(QString &requestUrl);
198
199     /**
200       * @brief Build and send request
201       *
202       * Appends script pathname and parameters to the server base URL. Access token is appended and
203       * the request sent if the access token is available, otherwise the request is queued.
204       *
205       * @param script Requested script pathname
206       * @param parameters Hash containing parameter key/value pairs.
207       */
208     void buildRequest(const QString &script, const QHash<QString,QString> &parameters);
209
210     /**
211       * @brief Convert coordinate value in degrees (double) to string with enough precision
212       *
213       * @param degrees Coordinate value in degrees
214       * @returns Coordinate value as string
215       */
216     QString degreesToString(double degrees);
217
218     /**
219     * @brief Temporary method to get tags.
220     *
221     * Tags are fetch from local database instead of Situare server.
222     * @param userId
223     * @return QStringList list of tags
224     */
225     QHash<QString, QString> getTags(const QString &userId);
226
227     /**
228     * @brief Parses interesting people data from JSON string
229     *
230     * @param jsonReply JSON string
231     */
232     void parseInterestingPeopleData(const QByteArray &jsonReply);
233
234     /**
235     * @brief Parses messages data from JSON string
236     *
237     * @param jsonReply JSON string
238     */
239     void parseMessagesData(const QByteArray &jsonReply);
240
241     /**
242     * @brief Parses popular tags data from JSON string
243     *
244     * @param jsonReply JSON string
245     */
246     void parsePopularTagsData(const QByteArray &jsonReply);
247
248     /**
249     * @brief Parses user and friend data from JSON string
250     *
251     * @param jsonReply JSON string
252     */
253     void parseUserData(const QByteArray &jsonReply);
254
255     /**
256       * @brief Send request
257       *
258       * @param requestUrl Request URL containing also all parameters
259       */
260     void sendRequest(const QString &requestUrl);
261
262 private slots:
263     /**
264     * @brief Slot for received images
265     *
266     * @param id image ID
267     * @param image image pixmap
268     */
269     void imageReceived(const QString &id, const QPixmap &image);
270
271 /*******************************************************************************
272  * SIGNALS
273  ******************************************************************************/
274 signals:
275     /**
276     * @brief Signals error
277     *
278     * @param context error context
279     * @param error error code
280     */
281     void error(const int context, const int error);
282
283     /**
284     * @brief Signal for image fetching
285     *
286     * @param id Image id
287     * @param url Image url
288     */
289     void fetchImage(const QString &id, const QUrl &url);
290
291     /**
292     * @brief Signals when image is downloaded
293     *
294     * @param id image ID
295     * @param image image pixmap
296     */
297     void imageReady(const QString &id, const QPixmap &image);
298
299     /**
300     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
301     *
302     * @param friends list of friends
303     * @param others list of other people
304     */
305     void interestingPeopleReceived(QList<User> &friends, QList<User> &others);
306
307     /**
308     * @brief Signal when fetchMessages request is finished
309     *
310     * @param messages list of messages sent to user
311     */
312     void messagesReceived(QList<Message> &received, QList<Message> &sent);
313
314     /**
315     * @brief Signals when fetchPopularTags request is finished
316     *
317     * @param popularTags list of popular tags
318     */
319     void popularTagsReceived(QHash<QString, QString> &popularTags);
320
321     /**
322     * @brief Signals when address data is retrieved
323     *
324     * @param address Street address
325     */
326     void reverseGeoReady(const QString &address);
327
328     /**
329     * @brief Signals when updateLocation request finished successfully
330     */
331     void updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod);
332
333     /**
334     * @brief Signals when user data is retrieved
335     *
336     * @param user instance of user
337     * @param friendList list of friends
338     */
339     void userDataChanged(User *user, QList<User *> &friendList);
340
341 /*******************************************************************************
342  * DATA MEMBERS
343  ******************************************************************************/
344 private:
345     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
346
347     QList<QNetworkReply *> m_currentRequests;       ///< List of current http requests
348     QList<User *> m_friendsList;                    ///< List of friends(User)
349
350     QString m_session;                          ///< Session data
351
352     Database *m_database;                       ///< Instance of the database
353     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher    
354     NetworkAccessManager *m_networkManager;     ///< Pointer to QNetworkAccessManager
355     User *m_user;                               ///< Pointer to User
356 };
357
358 #endif // SITUARESERVICE_H