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