Added method to read messages.
[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 "message.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 messages sent to user.
86     */
87     void fetchMessages();
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     /**
139     * @brief Requests ImageFetcher if user/friend has a profile image
140     *        uses members: m_user and m_friendsList
141     *
142     * @param imageUrlList list of image urls
143     */
144     void addProfileImages(const QList<QUrl> &imageUrlList);
145
146     /**
147     * @brief Forms a http cookie
148     *
149     * @param apiKeyValue application key
150     * @param expiresValue session expire date&time from Facebook
151     * @param userValue user id from Facebook
152     * @param sessionKeyValue session key from Facebook
153     * @param sessionSecretValue session secret from Facebook
154     * @param signatureValue md5 generated signature
155     * @param localeValue used locale
156     * @return QString formed cookie
157     */
158     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
159                        QString sessionKeyValue, QString sessionSecretValue,
160                        const QString &signatureValue, const QString &localeValue);
161
162     /**
163     * @brief Forms a http url
164     *
165     * @param baseUrl Server url
166     * @param phpScript Server script
167     * @param urlParameters optional parameters for url
168     * @return QUrl formed url
169     */
170     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
171                  QString urlParameters = QString());
172
173     /**
174     * @brief Forms url parameters
175     *
176     * @param coordinates current coordinates
177     * @param status optional status message
178     * @param publish optional publish location on Facebook wall
179     * @return QString
180     */
181     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
182                               bool publish = false);
183
184     /**
185     * @brief Temporary method to get tags.
186     *
187     * Tags are fetch from local database instead of Situare server.
188     * @param userId
189     * @return QStringList list of tags
190     */
191     QStringList getTags(const QString &userId);
192
193     /**
194     * @brief Parses user and friend data from JSON string
195     *
196     * @param jsonReply JSON string
197     */
198     void parseUserData(const QByteArray &jsonReply);
199
200     /**
201     * @brief Sends http request
202     *
203     * @param url destination
204     * @param cookieType type of the cookie
205     * @param cookie http cookie
206     */
207     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
208
209 private slots:
210     /**
211     * @brief Slot for received images
212     *
213     * @param url Image url
214     * @param image Received image
215     */
216     void imageReceived(const QUrl &url, const QPixmap &image);
217
218 /*******************************************************************************
219  * SIGNALS
220  ******************************************************************************/
221 signals:
222     /**
223     * @brief Signals error
224     *
225     * @param context error context
226     * @param error error code
227     */
228     void error(const int context, const int error);
229
230     /**
231     * @brief Signal for image fetching
232     *
233     * @param url Image url
234     */
235     void fetchImage(const QUrl &url);
236
237     /**
238     * @brief Signals when user's/friend's image is downloaded
239     *
240     * @param user Instance of user/friend
241     */
242     void imageReady(User *user);
243
244     /**
245     * @brief Signal when fetchPeopleWithSimilarInterest request is finished
246     *
247     * @param interestingPeople list of interesting people
248     */
249     void interestingPeopleReceived(QList<User *> &interestingPeople);
250
251     /**
252     * @brief Signal when fetchMessages request is finished
253     *
254     * @param messages list of messages sent to user
255     */
256     void messagesReceived(QList<Message> &messages);
257
258     /**
259     * @brief Signals when address data is retrieved
260     *
261     * @param address Street address
262     */
263     void reverseGeoReady(const QString &address);
264
265     /**
266     * @brief Signals when updateLocation request finished successfully
267     *
268     */
269     void updateWasSuccessful();
270
271     /**
272     * @brief Signals when user data is retrieved
273     *
274     * @param user instance of user
275     * @param friendList list of friends
276     */
277     void userDataChanged(User *user, QList<User *> &friendList);
278
279 /*******************************************************************************
280  * DATA MEMBERS
281  ******************************************************************************/
282
283 private:
284
285     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
286
287     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
288     QList<User *> m_friendsList;                ///< List of friends(User)
289
290     Database *m_database;                       ///< Instance of the database
291     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
292     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
293     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
294     User *m_user;                               ///< Pointer to User
295 };
296
297 #endif // SITUARESERVICE_H