Updated user and friendslist signals and image fetching
[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 QNetworkAccessManager;
32 class QNetworkReply;
33 class QNetworkRequest;
34 class QPointF;
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 QPointF &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 (true/false)
85     */
86     void updateLocation(const QPointF &coordinates, const QString &status, const bool &publish);
87
88 public slots:
89
90     /**
91     * @brief Public slot, which indicates when http request has been completed
92     *
93     * @param reply storage for http reply
94     */
95     void requestFinished(QNetworkReply *reply);
96
97     /**
98     * @brief Public slot, which indicates when facebook credentials are ready
99     *
100     * @param credentials New credentials
101     */
102     void credentialsReady(const FacebookCredentials &credentials);
103
104 private:
105
106     /**
107     * @brief Forms a http url
108     *
109     * @param baseUrl Server url
110     * @param phpScript Server script
111     * @param urlParameters optional parameters for url
112     * @return QUrl formed url
113     */
114     QUrl formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters = 0);
115
116     /**
117     * @brief Forms url parameters
118     *
119     * @param coordinates current coordinates
120     * @param status optional status message
121     * @param publish optional publish location on Facebook wall (true/false)
122     * @return QString
123     */
124     QString formUrlParameters(const QPointF &coordinates, QString status = 0, QString publish = 0);
125
126     /**
127     * @brief Parses user and friend data from JSON string
128     *
129     * @param jsonReply JSON string
130     */
131     void parseUserData(const QByteArray &jsonReply);
132
133     /**
134     * @brief Sends http request
135     *
136     * @param url destination
137     * @param cookieType type of the cookie
138     * @param cookie http cookie
139     */
140     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
141
142     /**
143     * @brief Requests ImageFetcher if user/friend has a profile image
144     *        uses members: m_user and m_friendsList
145     *
146     */
147     void addProfileImages();
148
149 private slots:
150
151     /**
152     * @brief Slot for received images
153     *
154     * @param url Image url
155     * @param image Received image
156     */
157     void imageReceived(const QUrl &url, const QPixmap &image);
158
159 /*******************************************************************************
160  * SIGNALS
161  ******************************************************************************/
162
163 signals:
164
165     /**
166     * @brief Signals error
167     *
168     * @param error error message
169     */
170     void error(const QString &error);
171
172     /**
173     * @brief Signal for image fetching
174     *
175     * @param url Image url
176     */
177     void fetchImage(const QUrl &url);
178
179     /**
180     * @brief Signals when address data is retrieved
181     *
182     * @param address Street address
183     */
184     void reverseGeoReady(const QString &address);
185
186     /**
187     * @brief Signals when updateLocation request finished successfully
188     *
189     */
190     void updateWasSuccessful();
191
192     /**
193     * @brief Signals when user data is retrieved
194     *
195     * @param user instance of user
196     * @param friendList list of friends
197     */
198     void userDataChanged(User *user, QList<User *> &friendList);
199
200 /*******************************************************************************
201  * DATA MEMBERS
202  ******************************************************************************/
203
204 private:
205
206     FacebookCredentials m_credentials; ///< handle for FacebookCredentials
207     QList<QNetworkReply *> m_currentRequests; ///< List of current http requests
208     QNetworkAccessManager *m_networkManager; ///< Pointer to QNetworkAccessManager
209     ImageFetcher *m_imageFetcher; ///< Instance of the image fetcher
210
211     User *m_user; ///< Pointer to User
212     QList<User *> m_friendsList; ///< List of friends(User)
213     int m_visited; ///< Indicates number of friends with profile images
214     int m_nbrOfImages; ///< Indicates number of friends whose profile images have been downloaded
215     bool m_defaultImage; ///< Indicates if some of the friends or the user does not have a profile image
216 };
217
218 #endif // SITUARESERVICE_H