a860340c9e0d78d7a0802cc16bccef31b9de9cdb
[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
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 http request has been completed
98     *
99     * @param reply storage for http reply
100     */
101     void requestFinished(QNetworkReply *reply);
102
103     void updateSession(const QString &session);
104
105 private:
106
107     /**
108     * @brief Requests ImageFetcher if user/friend has a profile image
109     *        uses members: m_user and m_friendsList
110     *
111     * @param imageUrlList list of image urls
112     */
113     void addProfileImages(const QList<QUrl> &imageUrlList);
114
115     void appendAccessToken(QString &requestUrl);
116
117     QString degreesToString(double degrees);
118
119     /**
120     * @brief Forms a http cookie
121     *
122     * @param apiKeyValue application key
123     * @param expiresValue session expire date&time from Facebook
124     * @param userValue user id from Facebook
125     * @param sessionKeyValue session key from Facebook
126     * @param sessionSecretValue session secret from Facebook
127     * @param signatureValue md5 generated signature
128     * @param localeValue used locale
129     * @return QString formed cookie
130     */
131     QString formCookie(const QString &apiKeyValue, QString expiresValue, QString userValue,
132                        QString sessionKeyValue, QString sessionSecretValue,
133                        const QString &signatureValue, const QString &localeValue);
134
135     /**
136     * @brief Forms a http url
137     *
138     * @param baseUrl Server url
139     * @param phpScript Server script
140     * @param urlParameters optional parameters for url
141     * @return QUrl formed url
142     */
143     QUrl formUrl(const QString &baseUrl, const QString &phpScript,
144                  QString urlParameters = QString());
145
146     /**
147     * @brief Forms url parameters
148     *
149     * @param coordinates current coordinates
150     * @param status optional status message
151     * @param publish optional publish location on Facebook wall
152     * @return QString
153     */
154     QString formUrlParameters(const GeoCoordinate &coordinates, QString status = QString(),
155                               bool publish = false);
156
157     /**
158     * @brief Parses user and friend data from JSON string
159     *
160     * @param jsonReply JSON string
161     */
162     void parseUserData(const QByteArray &jsonReply);
163
164     /**
165     * @brief Sends http request
166     *
167     * @param url destination
168     * @param cookieType type of the cookie
169     * @param cookie http cookie
170     */
171     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
172
173
174     void buildRequest(const QString &script, const QHash<QString,QString> &parameters);
175
176     void sendRequest(const QString &requestUrl);
177
178 private slots:
179
180     /**
181     * @brief Slot for received images
182     *
183     * @param url Image url
184     * @param image Received image
185     */
186     void imageReceived(const QUrl &url, const QPixmap &image);
187
188 /*******************************************************************************
189  * SIGNALS
190  ******************************************************************************/
191
192 signals:
193
194     /**
195     * @brief Signals error
196     *
197     * @param context error context
198     * @param error error code
199     */
200     void error(const int context, const int error);
201
202     /**
203     * @brief Signal for image fetching
204     *
205     * @param url Image url
206     */
207     void fetchImage(const QUrl &url);
208
209     /**
210     * @brief Signals when user's/friend's image is downloaded
211     *
212     * @param user Instance of user/friend
213     */
214     void imageReady(User *user);
215
216     /**
217     * @brief Signals when address data is retrieved
218     *
219     * @param address Street address
220     */
221     void reverseGeoReady(const QString &address);
222
223     /**
224     * @brief Signals when updateLocation request finished successfully
225     *
226     */
227     void updateWasSuccessful();
228
229     /**
230     * @brief Signals when user data is retrieved
231     *
232     * @param user instance of user
233     * @param friendList list of friends
234     */
235     void userDataChanged(User *user, QList<User *> &friendList);
236
237 /*******************************************************************************
238  * DATA MEMBERS
239  ******************************************************************************/
240
241 private:
242
243     bool m_defaultImage;    ///< Indicates if some of the friends/user doesn't have a image
244
245     QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
246     QList<QString> m_requestsWaitingAccessToken;
247     QList<User *> m_friendsList;                ///< List of friends(User)
248
249     QString m_session;
250
251     NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
252
253     ImageFetcher *m_imageFetcher;               ///< Instance of the image fetcher
254     User *m_user;                               ///< Pointer to User
255 };
256
257 #endif // SITUARESERVICE_H