Added show friend button to MeetPeoplePanel.
[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     */
170     void sendMessage(const QString &receiverId, const QString &message);
171
172 private:
173     /**
174     * @brief Requests ImageFetcher if user/friend has a profile image
175     *        uses members: m_user and m_friendsList
176     *
177     * @param imageUrlList list of image urls
178     */
179     void addProfileImages(const QHash<QString, QUrl> &imageUrlList);
180
181     /**
182     * @brief Forms a http cookie
183     *
184     * @param apiKeyValue application key
185     * @param expiresValue session expire date&time from Facebook
186     * @param userValue user id from Facebook
187     * @param sessionKeyValue session key from Facebook
188     * @param sessionSecretValue session secret from Facebook
189     * @param signatureValue md5 generated signature
190     * @param localeValue used locale
191     * @return QString formed cookie
192     */
193     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
194                        QString sessionKeyValue, QString sessionSecretValue,
195                        const QString &signatureValue, const QString &localeValue);
196
197     /**
198     * @brief Forms a http url
199     *
200     * @param baseUrl Server url
201     * @param phpScript Server script
202     * @param urlParameters optional parameters for url
203     * @return QUrl formed url
204     */
205     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
206                  QString urlParameters = QString());
207
208     /**
209     * @brief Forms url parameters
210     *
211     * @param coordinates current coordinates
212     * @param status optional status message
213     * @param publish optional publish location on Facebook wall
214     * @return QString
215     */
216     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
217                               bool publish = false);
218
219     /**
220     * @brief Temporary method to get tags.
221     *
222     * Tags are fetch from local database instead of Situare server.
223     * @param userId
224     * @return QStringList list of tags
225     */
226     QHash<QString, QString> getTags(const QString &userId);
227
228     /**
229     * @brief Parses interesting people data from JSON string
230     *
231     * @param jsonReply JSON string
232     */
233     void parseInterestingPeopleData(const QByteArray &jsonReply);
234
235     /**
236     * @brief Parses messages data from JSON string
237     *
238     * @param jsonReply JSON string
239     */
240     void parseMessagesData(const QByteArray &jsonReply);
241
242
243     /**
244     * @brief Parses user and friend data from JSON string
245     *
246     * @param jsonReply JSON string
247     */
248     void parseUserData(const QByteArray &jsonReply);
249
250     /**
251     * @brief Sends http request
252     *
253     * @param url destination
254     * @param cookieType type of the cookie
255     * @param cookie http cookie
256     */
257     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
258
259 private slots:
260     /**
261     * @brief Slot for received images
262     *
263     * @param id image ID
264     * @param image image pixmap
265     */
266     void imageReceived(const QString &id, const QPixmap &image);
267
268 /*******************************************************************************
269  * SIGNALS
270  ******************************************************************************/
271 signals:
272     /**
273     * @brief Signals error
274     *
275     * @param context error context
276     * @param error error code
277     */
278     void error(const int context, const int error);
279
280     /**
281     * @brief Signal for image fetching
282     *
283     * @param id Image id
284     * @param url Image url
285     */
286     void fetchImage(const QString &id, const QUrl &url);
287
288     /**
289     * @brief Signals when user's/friend's image is downloaded
290     *
291     * @param user Instance of user/friend
292     */
293     void imageReady(User *user);
294
295     /**
296     * @brief Signals when image is downloaded
297     *
298     * @param id image ID
299     * @param image image pixmap
300     */
301     void imageReady(const QString &id, const QPixmap &image);
302
303     /**
304     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
305     *
306     * @param interestingPeople list of interesting people
307     */
308     void interestingPeopleReceived(QList<User> &interestingPeople);
309
310     /**
311     * @brief Signal when fetchMessages request is finished
312     *
313     * @param messages list of messages sent to user
314     */
315     void messagesReceived(QList<Message> &messages);
316
317     /**
318     * @brief Signals when address data is retrieved
319     *
320     * @param address Street address
321     */
322     void reverseGeoReady(const QString &address);
323
324     /**
325     * @brief Signals when updateLocation request finished successfully
326     *
327     */
328     void updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod);
329
330     /**
331     * @brief Signals when user data is retrieved
332     *
333     * @param user instance of user
334     * @param friendList list of friends
335     */
336     void userDataChanged(User *user, QList<User *> &friendList);
337
338 /*******************************************************************************
339  * DATA MEMBERS
340  ******************************************************************************/
341 private:
342     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
343
344     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
345     QList<User *> m_friendsList;                ///< List of friends(User)
346
347     Database *m_database;                       ///< Instance of the database
348     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
349     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
350     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
351     User *m_user;                               ///< Pointer to User
352 };
353
354 #endif // SITUARESERVICE_H