Added 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 QList<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 user and friend data from JSON string
194     *
195     * @param jsonReply JSON string
196     */
197     void parseUserData(const QByteArray &jsonReply);
198
199     /**
200     * @brief Sends http request
201     *
202     * @param url destination
203     * @param cookieType type of the cookie
204     * @param cookie http cookie
205     */
206     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
207
208 private slots:
209     /**
210     * @brief Slot for received images
211     *
212     * @param url Image url
213     * @param image Received image
214     */
215     void imageReceived(const QUrl &url, const QPixmap &image);
216
217 /*******************************************************************************
218  * SIGNALS
219  ******************************************************************************/
220 signals:
221     /**
222     * @brief Signals error
223     *
224     * @param context error context
225     * @param error error code
226     */
227     void error(const int context, const int error);
228
229     /**
230     * @brief Signal for image fetching
231     *
232     * @param url Image url
233     */
234     void fetchImage(const QUrl &url);
235
236     /**
237     * @brief Signals when user's/friend's image is downloaded
238     *
239     * @param user Instance of user/friend
240     */
241     void imageReady(User *user);
242
243     /**
244     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
245     *
246     * @param interestingPeople list of interesting people
247     */
248     void interestingPeopleReceived(QList<User *> &interestingPeople);
249
250     /**
251     * @brief Signal when fetchNotifications request is finished
252     *
253     * @param notifications list of notifications sent to user
254     */
255     void notificationsReceived(QList<Notification> &notifications);
256
257     /**
258     * @brief Signals when address data is retrieved
259     *
260     * @param address Street address
261     */
262     void reverseGeoReady(const QString &address);
263
264     /**
265     * @brief Signals when updateLocation request finished successfully
266     *
267     */
268     void updateWasSuccessful();
269
270     /**
271     * @brief Signals when user data is retrieved
272     *
273     * @param user instance of user
274     * @param friendList list of friends
275     */
276     void userDataChanged(User *user, QList<User *> &friendList);
277
278 /*******************************************************************************
279  * DATA MEMBERS
280  ******************************************************************************/
281
282 private:
283
284     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
285
286     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
287     QList<User *> m_friendsList;                ///< List of friends(User)
288
289     Database *m_database;                       ///< Instance of the database
290     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
291     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
292     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
293     User *m_user;                               ///< Pointer to User
294 };
295
296 #endif // SITUARESERVICE_H