Added coordinates to Messages, added Message type.
[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
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 #include "../facebookservice/facebookcredentials.h"
29 #include "../user/user.h"
30 #include "imagefetcher.h"
31 #include "message.h"
32
33 class Database;
34 class NetworkAccessManager;
35 class QNetworkReply;
36 class QNetworkRequest;
37 class GeoCoordinate;
38 class QUrl;
39
40 /**
41 * @brief SituareService class for communicating with Situare server
42 *
43 * @author Henri Lampela
44 * @class SituareService situareservice.h "situareservice/situareservice.h"
45 */
46 class SituareService : public QObject
47 {
48     Q_OBJECT
49
50 public:
51
52     /**
53     * Unit test class
54     */
55     friend class TestSituareService;
56
57     enum SuccessfulMethod {SuccessfulUpdateLocation, SuccessfulAddTags, SuccessfulRemoveTags,
58                            SuccessfulSendMessage, SuccessfulRemoveMessage};
59
60     /**
61     * @brief Default constructor
62     *
63     * @param parent instance of parent
64     */
65     SituareService(NetworkAccessManager *networkManager, ImageFetcher *imageFetcher,
66                    QObject *parent = 0);
67
68     /**
69     * @brief Destructor
70     *
71     */
72     ~SituareService();
73
74 /*******************************************************************************
75  * MEMBER FUNCTIONS AND SLOTS
76  ******************************************************************************/
77     /**
78     * @brief Retrieves people with similart interest (same tags).
79     *
80     * People is searched from area defined by south-west and north-east bounds.
81     * @param southWest south-west coordinates of bounds
82     * @param northEast north-east coordinates of bounds
83     */
84     void fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
85                                         const GeoCoordinate &northEastCoordinates);
86
87     /**
88     * @brief Retrieves location user and friends information from Situare server
89     *
90     */
91     void fetchLocations();
92
93     /**
94     * @brief Translates coordinates to street address via Situare server
95     *
96     * @param coordinates coordinates to be translated
97     */
98     void reverseGeo(const GeoCoordinate &coordinates);
99
100     /**
101     * @brief Updates location to the Situare server
102     *
103     * @param coordinates current cordinates
104     * @param status message
105     * @param publish publish location on Facebook wall
106     */
107     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
108
109 public slots:
110     /**
111     * @brief Adds tags to the Situare server
112     *
113     * CURRENTLY TAGS ARE UPDATED TO THE LOCAL DATABASE, NOT SITUARE SERVER
114     * @param tags list of user's tags
115     */
116     void addTags(const QStringList &tags);
117
118     /**
119     * @brief Retrieves messages sent to user.
120     */
121     void fetchMessages();
122
123     /**
124     * @brief Public slot, to clear user data
125     *
126     */
127     void clearUserData();
128
129     /**
130     * @brief Public slot, which indicates when facebook credentials are ready
131     *
132     * @param credentials New credentials
133     */
134     void credentialsReady(const FacebookCredentials &credentials);
135
136     /**
137     * @brief Removes message.
138     *
139     * @param id message ID
140     */
141     void removeMessage(const QString &id);
142
143     /**
144     * @brief Removes tags.
145     *
146     * @param tags list of tags to remove
147     */
148     void removeTags(const QStringList &tags);
149
150     /**
151     * @brief Public slot, which indicates when http request has been completed
152     *
153     * @param reply storage for http reply
154     */
155     void requestFinished(QNetworkReply *reply);
156
157     /**
158     * @brief Searches people by tag name.
159     *
160     * @param tag tag name
161     */
162     void searchPeopleByTag(const QString &tag);
163
164     /**
165     * @brief Sends a message to a person.
166     *
167     * @param receiverId Facebook user ID
168     * @param message message text
169     * @param message coordinates
170     */
171     void sendMessage(const QString &receiverId, const QString &message,
172                      const GeoCoordinate &coordinates = GeoCoordinate());
173
174 private:
175     /**
176     * @brief Requests ImageFetcher if user/friend has a profile image
177     *        uses members: m_user and m_friendsList
178     *
179     * @param imageUrlList list of image urls
180     */
181     void addProfileImages(const QHash<QString, QUrl> &imageUrlList);
182
183     /**
184     * @brief Forms a http cookie
185     *
186     * @param apiKeyValue application key
187     * @param expiresValue session expire date&time from Facebook
188     * @param userValue user id from Facebook
189     * @param sessionKeyValue session key from Facebook
190     * @param sessionSecretValue session secret from Facebook
191     * @param signatureValue md5 generated signature
192     * @param localeValue used locale
193     * @return QString formed cookie
194     */
195     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
196                        QString sessionKeyValue, QString sessionSecretValue,
197                        const QString &signatureValue, const QString &localeValue);
198
199     /**
200     * @brief Forms a http url
201     *
202     * @param baseUrl Server url
203     * @param phpScript Server script
204     * @param urlParameters optional parameters for url
205     * @return QUrl formed url
206     */
207     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
208                  QString urlParameters = QString());
209
210     /**
211     * @brief Forms url parameters
212     *
213     * @param coordinates current coordinates
214     * @param status optional status message
215     * @param publish optional publish location on Facebook wall
216     * @return QString
217     */
218     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
219                               bool publish = false);
220
221     /**
222     * @brief Temporary method to get tags.
223     *
224     * Tags are fetch from local database instead of Situare server.
225     * @param userId
226     * @return QStringList list of tags
227     */
228     QHash<QString, QString> getTags(const QString &userId);
229
230     /**
231     * @brief Parses interesting people data from JSON string
232     *
233     * @param jsonReply JSON string
234     */
235     void parseInterestingPeopleData(const QByteArray &jsonReply);
236
237     /**
238     * @brief Parses messages data from JSON string
239     *
240     * @param jsonReply JSON string
241     */
242     void parseMessagesData(const QByteArray &jsonReply);
243
244
245     /**
246     * @brief Parses user and friend data from JSON string
247     *
248     * @param jsonReply JSON string
249     */
250     void parseUserData(const QByteArray &jsonReply);
251
252     /**
253     * @brief Sends http request
254     *
255     * @param url destination
256     * @param cookieType type of the cookie
257     * @param cookie http cookie
258     */
259     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
260
261 private slots:
262     /**
263     * @brief Slot for received images
264     *
265     * @param id image ID
266     * @param image image pixmap
267     */
268     void imageReceived(const QString &id, const QPixmap &image);
269
270 /*******************************************************************************
271  * SIGNALS
272  ******************************************************************************/
273 signals:
274     /**
275     * @brief Signals error
276     *
277     * @param context error context
278     * @param error error code
279     */
280     void error(const int context, const int error);
281
282     /**
283     * @brief Signal for image fetching
284     *
285     * @param id Image id
286     * @param url Image url
287     */
288     void fetchImage(const QString &id, const QUrl &url);
289
290     /**
291     * @brief Signals when user's/friend's image is downloaded
292     *
293     * @param user Instance of user/friend
294     */
295     void imageReady(User *user);
296
297     /**
298     * @brief Signals when image is downloaded
299     *
300     * @param id image ID
301     * @param image image pixmap
302     */
303     void imageReady(const QString &id, const QPixmap &image);
304
305     /**
306     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
307     *
308     * @param interestingPeople list of interesting people
309     */
310     void interestingPeopleReceived(QList<User> &interestingPeople);
311
312     /**
313     * @brief Signal when fetchMessages request is finished
314     *
315     * @param messages list of messages sent to user
316     */
317     void messagesReceived(QList<Message> &received, QList<Message> &sent);
318
319     /**
320     * @brief Signals when address data is retrieved
321     *
322     * @param address Street address
323     */
324     void reverseGeoReady(const QString &address);
325
326     /**
327     * @brief Signals when updateLocation request finished successfully
328     *
329     */
330     void updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod);
331
332     /**
333     * @brief Signals when user data is retrieved
334     *
335     * @param user instance of user
336     * @param friendList list of friends
337     */
338     void userDataChanged(User *user, QList<User *> &friendList);
339
340 /*******************************************************************************
341  * DATA MEMBERS
342  ******************************************************************************/
343 private:
344     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
345
346     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
347     QList<User *> m_friendsList;                ///< List of friends(User)
348
349     Database *m_database;                       ///< Instance of the database
350     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
351     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
352     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
353     User *m_user;                               ///< Pointer to User
354 };
355
356 #endif // SITUARESERVICE_H