New notification panel.
[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 Retrieves notifications sent to user.
86     */
87     void fetchNotifications();
88
89     /**
90     * @brief Translates coordinates to street address via Situare server
91     *
92     * @param coordinates coordinates to be translated
93     */
94     void reverseGeo(const GeoCoordinate &coordinates);
95
96     /**
97     * @brief Updates location to the Situare server
98     *
99     * @param coordinates current cordinates
100     * @param status message
101     * @param publish publish location on Facebook wall
102     */
103     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
104
105     /**
106     * @brief Updates tags to the Situare server
107     *
108     * CURRENTLY TAGS ARE UPDATED TO THE LOCAL DATABASE, NOT SITUARE SERVER
109     * @param userId user ID
110     * @param tags list of user's tags
111     */
112     void updateTags(const QString &userId, const QStringList &tags);
113
114 public slots:
115
116     /**
117     * @brief Public slot, to clear user data
118     *
119     */
120     void clearUserData();
121
122     /**
123     * @brief Public slot, which indicates when facebook credentials are ready
124     *
125     * @param credentials New credentials
126     */
127     void credentialsReady(const FacebookCredentials &credentials);
128
129     /**
130     * @brief Public slot, which indicates when http request has been completed
131     *
132     * @param reply storage for http reply
133     */
134     void requestFinished(QNetworkReply *reply);
135
136 private:
137     /**
138     * @brief Requests ImageFetcher if user/friend has a profile image
139     *        uses members: m_user and m_friendsList
140     *
141     * @param imageUrlList list of image urls
142     */
143     void addProfileImages(const QHash<QString, QUrl> &imageUrlList);
144
145     /**
146     * @brief Forms a http cookie
147     *
148     * @param apiKeyValue application key
149     * @param expiresValue session expire date&time from Facebook
150     * @param userValue user id from Facebook
151     * @param sessionKeyValue session key from Facebook
152     * @param sessionSecretValue session secret from Facebook
153     * @param signatureValue md5 generated signature
154     * @param localeValue used locale
155     * @return QString formed cookie
156     */
157     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
158                        QString sessionKeyValue, QString sessionSecretValue,
159                        const QString &signatureValue, const QString &localeValue);
160
161     /**
162     * @brief Forms a http url
163     *
164     * @param baseUrl Server url
165     * @param phpScript Server script
166     * @param urlParameters optional parameters for url
167     * @return QUrl formed url
168     */
169     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
170                  QString urlParameters = QString());
171
172     /**
173     * @brief Forms url parameters
174     *
175     * @param coordinates current coordinates
176     * @param status optional status message
177     * @param publish optional publish location on Facebook wall
178     * @return QString
179     */
180     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
181                               bool publish = false);
182
183     /**
184     * @brief Temporary method to get tags.
185     *
186     * Tags are fetch from local database instead of Situare server.
187     * @param userId
188     * @return QStringList list of tags
189     */
190     QStringList getTags(const QString &userId);
191
192     /**
193     * @brief Parses interesting people data from JSON string
194     *
195     * @param jsonReply JSON string
196     */
197     void parseInterestingPeopleData(const QByteArray &jsonReply);
198
199     /**
200     * @brief Parses notifications data from JSON string
201     *
202     * @param jsonReply JSON string
203     */
204     void parseNotificationsData(const QByteArray &jsonReply);
205
206
207     /**
208     * @brief Parses user and friend data from JSON string
209     *
210     * @param jsonReply JSON string
211     */
212     void parseUserData(const QByteArray &jsonReply);
213
214     /**
215     * @brief Sends http request
216     *
217     * @param url destination
218     * @param cookieType type of the cookie
219     * @param cookie http cookie
220     */
221     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
222
223 private slots:
224     /**
225     * @brief Slot for received images
226     *
227     * @param id image ID
228     * @param image image pixmap
229     */
230     void imageReceived(const QString &id, const QPixmap &image);
231
232 /*******************************************************************************
233  * SIGNALS
234  ******************************************************************************/
235 signals:
236     /**
237     * @brief Signals error
238     *
239     * @param context error context
240     * @param error error code
241     */
242     void error(const int context, const int error);
243
244     /**
245     * @brief Signal for image fetching
246     *
247     * @param id Image id
248     * @param url Image url
249     */
250     void fetchImage(const QString &id, const QUrl &url);
251
252     /**
253     * @brief Signals when user's/friend's image is downloaded
254     *
255     * @param user Instance of user/friend
256     */
257     void imageReady(User *user);
258
259     /**
260     * @brief Signals when image is downloaded
261     *
262     * @param id image ID
263     * @param image image pixmap
264     */
265     void imageReady(const QString &id, const QPixmap &image);
266
267     /**
268     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
269     *
270     * @param interestingPeople list of interesting people
271     */
272     void interestingPeopleReceived(QList<User> &interestingPeople);
273
274     /**
275     * @brief Signal when fetchNotifications request is finished
276     *
277     * @param notifications list of notifications sent to user
278     */
279     void notificationsReceived(QList<Notification*> &notifications);
280
281     /**
282     * @brief Signals when address data is retrieved
283     *
284     * @param address Street address
285     */
286     void reverseGeoReady(const QString &address);
287
288     /**
289     * @brief Signals when updateLocation request finished successfully
290     *
291     */
292     void updateWasSuccessful();
293
294     /**
295     * @brief Signals when user data is retrieved
296     *
297     * @param user instance of user
298     * @param friendList list of friends
299     */
300     void userDataChanged(User *user, QList<User *> &friendList);
301
302 /*******************************************************************************
303  * DATA MEMBERS
304  ******************************************************************************/
305
306 private:
307
308     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
309
310     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
311     QList<User *> m_friendsList;                ///< List of friends(User)
312
313     Database *m_database;                       ///< Instance of the database
314     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
315     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
316     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
317     User *m_user;                               ///< Pointer to User
318 };
319
320 #endif // SITUARESERVICE_H