5e97115de3d74a28ca466c638de7507684d3e15a
[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
31 class NetworkAccessManager;
32 class QNetworkReply;
33 class QNetworkRequest;
34 class GeoCoordinate;
35 class QUrl;
36
37 /**
38 * @brief SituareService class for communicating with Situare server
39 *
40 * @author Henri Lampela
41 * @class SituareService situareservice.h "situareservice/situareservice.h"
42 */
43 class SituareService : public QObject
44 {
45     Q_OBJECT
46
47 public:
48
49     /**
50     * @brief Default constructor
51     *
52     * @param parent instance of parent
53     */
54     SituareService(QObject *parent = 0);
55
56     /**
57     * @brief Destructor
58     *
59     */
60     ~SituareService();
61
62 /*******************************************************************************
63  * MEMBER FUNCTIONS AND SLOTS
64  ******************************************************************************/
65
66     /**
67     * @brief Retrieves location user and friends information from Situare server
68     *
69     */
70     void fetchLocations();
71
72     /**
73     * @brief Translates coordinates to street address via Situare server
74     *
75     * @param coordinates coordinates to be translated
76     */
77     void reverseGeo(const GeoCoordinate &coordinates);
78
79     /**
80     * @brief Updates location to the Situare server
81     *
82     * @param coordinates current cordinates
83     * @param status message
84     * @param publish publish location on Facebook wall
85     */
86     void updateLocation(const GeoCoordinate &coordinates, const QString &status, const bool &publish);
87
88 public slots:
89
90     /**
91     * @brief Public slot, to clear user data
92     *
93     */
94     void clearUserData();
95
96     /**
97     * @brief Public slot, which indicates when facebook credentials are ready
98     *
99     * @param credentials New credentials
100     */
101     void credentialsReady(const FacebookCredentials &credentials);
102
103     /**
104     * @brief Public slot, which indicates when http request has been completed
105     *
106     * @param reply storage for http reply
107     */
108     void requestFinished(QNetworkReply *reply);
109
110     void updateAccessToken(const QString &accessToken);
111
112 private:
113
114     /**
115     * @brief Requests ImageFetcher if user/friend has a profile image
116     *        uses members: m_user and m_friendsList
117     *
118     * @param imageUrlList list of image urls
119     */
120     void addProfileImages(const QList<QUrl> &imageUrlList);
121
122     void appendAccessToken(QString &requestUrl);
123
124     QString degreesToString(double degrees);
125
126     /**
127     * @brief Forms a http cookie
128     *
129     * @param apiKeyValue application key
130     * @param expiresValue session expire date&time from Facebook
131     * @param userValue user id from Facebook
132     * @param sessionKeyValue session key from Facebook
133     * @param sessionSecretValue session secret from Facebook
134     * @param signatureValue md5 generated signature
135     * @param localeValue used locale
136     * @return QString formed cookie
137     */
138     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
139                        QString sessionKeyValue, QString sessionSecretValue,
140                        const QString &signatureValue, const QString &localeValue);
141
142     /**
143     * @brief Forms a http url
144     *
145     * @param baseUrl Server url
146     * @param phpScript Server script
147     * @param urlParameters optional parameters for url
148     * @return QUrl formed url
149     */
150     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
151                  QString urlParameters = QString());
152
153     /**
154     * @brief Forms url parameters
155     *
156     * @param coordinates current coordinates
157     * @param status optional status message
158     * @param publish optional publish location on Facebook wall
159     * @return QString
160     */
161     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
162                               bool publish = false);
163
164     /**
165     * @brief Parses user and friend data from JSON string
166     *
167     * @param jsonReply JSON string
168     */
169     void parseUserData(const QByteArray &jsonReply);
170
171     /**
172     * @brief Sends http request
173     *
174     * @param url destination
175     * @param cookieType type of the cookie
176     * @param cookie http cookie
177     */
178     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
179
180
181     void buildRequest(const QString &script, const QHash<QString,QString> &parameters);
182
183     void sendRequest(const QString &requestUrl);
184
185 private slots:
186
187     /**
188     * @brief Slot for received images
189     *
190     * @param url Image url
191     * @param image Received image
192     */
193     void imageReceived(const QUrl &url, const QPixmap &image);
194
195 /*******************************************************************************
196  * SIGNALS
197  ******************************************************************************/
198
199 signals:
200
201     /**
202     * @brief Signals error
203     *
204     * @param context error context
205     * @param error error code
206     */
207     void error(const int context, const int error);
208
209     /**
210     * @brief Signal for image fetching
211     *
212     * @param url Image url
213     */
214     void fetchImage(const QUrl &url);
215
216     /**
217     * @brief Signals when user's/friend's image is downloaded
218     *
219     * @param user Instance of user/friend
220     */
221     void imageReady(User *user);
222
223     /**
224     * @brief Signals when address data is retrieved
225     *
226     * @param address Street address
227     */
228     void reverseGeoReady(const QString &address);
229
230     /**
231     * @brief Signals when updateLocation request finished successfully
232     *
233     */
234     void updateWasSuccessful();
235
236     /**
237     * @brief Signals when user data is retrieved
238     *
239     * @param user instance of user
240     * @param friendList list of friends
241     */
242     void userDataChanged(User *user, QList<User *> &friendList);
243
244 /*******************************************************************************
245  * DATA MEMBERS
246  ******************************************************************************/
247
248 private:
249
250     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
251
252     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
253     QList<QString> m_requestsWaitingAccessToken;
254     QList<User *> m_friendsList;                ///< List of friends(User)
255
256     QString m_accessToken;
257
258     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
259
260     FacebookCredentials m_credentials;          ///< handle for FacebookCredentials
261     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
262     User *m_user;                               ///< Pointer to User
263 };
264
265 #endif // SITUARESERVICE_H