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
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22
23 #ifndef SITUARESERVICE_H
24 #define SITUARESERVICE_H
25
26 #include <QObject>
27 #include "../facebookservice/facebookcredentials.h"
28 #include "../user/user.h"
29 #include "imagefetcher.h"
30 #include "notification.h"
31
32 class Database;
33 class NetworkAccessManager;
34 class QNetworkReply;
35 class QNetworkRequest;
36 class GeoCoordinate;
37 class QUrl;
38
39 /**
40 * @brief SituareService class for communicating with Situare server
41 *
42 * @author Henri Lampela
43 * @class SituareService situareservice.h "situareservice/situareservice.h"
44 */
45 class SituareService : public QObject
46 {
47     Q_OBJECT
48
49 public:
50
51     /**
52     * @brief Default constructor
53     *
54     * @param parent instance of parent
55     */
56     SituareService(QObject *parent = 0);
57
58     /**
59     * @brief Destructor
60     *
61     */
62     ~SituareService();
63
64 /*******************************************************************************
65  * MEMBER FUNCTIONS AND SLOTS
66  ******************************************************************************/
67
68     /**
69     * @brief Retrieves people with similart interest (same tags).
70     *
71     * People is searched from area defined by south-west and north-east bounds.
72     * @param southWest south-west coordinates of bounds
73     * @param northEast north-east coordinates of bounds
74     */
75     void fetchPeopleWithSimilarInterest(const GeoCoordinate &southWestCoordinates,
76                                         const GeoCoordinate &northEastCoordinates);
77
78     /**
79     * @brief Retrieves location user and friends information from Situare server
80     *
81     */
82     void fetchLocations();
83
84     /**
85     * @brief Translates coordinates to street address via Situare server
86     *
87     * @param coordinates coordinates to be translated
88     */
89     void reverseGeo(const GeoCoordinate &coordinates);
90
91     /**
92     * @brief Updates location to the Situare server
93     *
94     * @param coordinates current cordinates
95     * @param status message
96     * @param publish publish location on Facebook wall
97     */
98     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
99
100     /**
101     * @brief Updates tags to the Situare server
102     *
103     * CURRENTLY TAGS ARE UPDATED TO THE LOCAL DATABASE, NOT SITUARE SERVER
104     * @param userId user ID
105     * @param tags list of user's tags
106     */
107     void updateTags(const QString &userId, const QStringList &tags);
108
109 public slots:
110     /**
111     * @brief Retrieves notifications sent to user.
112     */
113     void fetchNotifications();
114
115     /**
116     * @brief Public slot, to clear user data
117     *
118     */
119     void clearUserData();
120
121     /**
122     * @brief Public slot, which indicates when facebook credentials are ready
123     *
124     * @param credentials New credentials
125     */
126     void credentialsReady(const FacebookCredentials &credentials);
127
128     /**
129     * @brief Public slot, which indicates when http request has been completed
130     *
131     * @param reply storage for http reply
132     */
133     void requestFinished(QNetworkReply *reply);
134
135 private:
136     /**
137     * @brief Requests ImageFetcher if user/friend has a profile image
138     *        uses members: m_user and m_friendsList
139     *
140     * @param imageUrlList list of image urls
141     */
142     void addProfileImages(const QHash<QString, QUrl> &imageUrlList);
143
144     /**
145     * @brief Forms a http cookie
146     *
147     * @param apiKeyValue application key
148     * @param expiresValue session expire date&time from Facebook
149     * @param userValue user id from Facebook
150     * @param sessionKeyValue session key from Facebook
151     * @param sessionSecretValue session secret from Facebook
152     * @param signatureValue md5 generated signature
153     * @param localeValue used locale
154     * @return QString formed cookie
155     */
156     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
157                        QString sessionKeyValue, QString sessionSecretValue,
158                        const QString &signatureValue, const QString &localeValue);
159
160     /**
161     * @brief Forms a http url
162     *
163     * @param baseUrl Server url
164     * @param phpScript Server script
165     * @param urlParameters optional parameters for url
166     * @return QUrl formed url
167     */
168     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
169                  QString urlParameters = QString());
170
171     /**
172     * @brief Forms url parameters
173     *
174     * @param coordinates current coordinates
175     * @param status optional status message
176     * @param publish optional publish location on Facebook wall
177     * @return QString
178     */
179     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
180                               bool publish = false);
181
182     /**
183     * @brief Temporary method to get tags.
184     *
185     * Tags are fetch from local database instead of Situare server.
186     * @param userId
187     * @return QStringList list of tags
188     */
189     QStringList getTags(const QString &userId);
190
191     /**
192     * @brief Parses interesting people data from JSON string
193     *
194     * @param jsonReply JSON string
195     */
196     void parseInterestingPeopleData(const QByteArray &jsonReply);
197
198     /**
199     * @brief Parses notifications data from JSON string
200     *
201     * @param jsonReply JSON string
202     */
203     void parseNotificationsData(const QByteArray &jsonReply);
204
205
206     /**
207     * @brief Parses user and friend data from JSON string
208     *
209     * @param jsonReply JSON string
210     */
211     void parseUserData(const QByteArray &jsonReply);
212
213     /**
214     * @brief Sends http request
215     *
216     * @param url destination
217     * @param cookieType type of the cookie
218     * @param cookie http cookie
219     */
220     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
221
222 private slots:
223     /**
224     * @brief Slot for received images
225     *
226     * @param id image ID
227     * @param image image pixmap
228     */
229     void imageReceived(const QString &id, const QPixmap &image);
230
231 /*******************************************************************************
232  * SIGNALS
233  ******************************************************************************/
234 signals:
235     /**
236     * @brief Signals error
237     *
238     * @param context error context
239     * @param error error code
240     */
241     void error(const int context, const int error);
242
243     /**
244     * @brief Signal for image fetching
245     *
246     * @param id Image id
247     * @param url Image url
248     */
249     void fetchImage(const QString &id, const QUrl &url);
250
251     /**
252     * @brief Signals when user's/friend's image is downloaded
253     *
254     * @param user Instance of user/friend
255     */
256     void imageReady(User *user);
257
258     /**
259     * @brief Signals when image is downloaded
260     *
261     * @param id image ID
262     * @param image image pixmap
263     */
264     void imageReady(const QString &id, const QPixmap &image);
265
266     /**
267     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
268     *
269     * @param interestingPeople list of interesting people
270     */
271     void interestingPeopleReceived(QList<User> &interestingPeople);
272
273     /**
274     * @brief Signal when fetchNotifications request is finished
275     *
276     * @param notifications list of notifications sent to user
277     */
278     void notificationsReceived(QList<Notification> &notifications);
279
280     /**
281     * @brief Signals when address data is retrieved
282     *
283     * @param address Street address
284     */
285     void reverseGeoReady(const QString &address);
286
287     /**
288     * @brief Signals when updateLocation request finished successfully
289     *
290     */
291     void updateWasSuccessful();
292
293     /**
294     * @brief Signals when user data is retrieved
295     *
296     * @param user instance of user
297     * @param friendList list of friends
298     */
299     void userDataChanged(User *user, QList<User *> &friendList);
300
301 /*******************************************************************************
302  * DATA MEMBERS
303  ******************************************************************************/
304
305 private:
306
307     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
308
309     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
310     QList<User *> m_friendsList;                ///< List of friends(User)
311
312     Database *m_database;                       ///< Instance of the database
313     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
314     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
315     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
316     User *m_user;                               ///< Pointer to User
317 };
318
319 #endif // SITUARESERVICE_H