Added error signals to engine from ImageFetcher and SituareService.
[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     * @param manager instance of QNetworkAccessManager
54     */
55     SituareService(QObject *parent = 0, QNetworkAccessManager *manager = 0);
56
57     /**
58     * @brief Destructor
59     *
60     */
61     ~SituareService();
62
63 /*******************************************************************************
64  * MEMBER FUNCTIONS AND SLOTS
65  ******************************************************************************/
66
67     /**
68     * @brief Retrieves location user and friends information from Situare server
69     *
70     */
71     void fetchLocations();
72
73     /**
74     * @brief Translates coordinates to street address via Situare server
75     *
76     * @param coordinates coordinates to be translated
77     */
78     void reverseGeo(const QPointF &coordinates);
79
80     /**
81     * @brief Updates location to the Situare server
82     *
83     * @param coordinates current cordinates
84     * @param status message
85     * @param publish publish location on Facebook wall (true/false)
86     */
87     void updateLocation(const QPointF &coordinates, const QString &status, const bool &publish);
88
89 public slots:
90
91     /**
92     * @brief Public slot, which indicates when http request has been completed
93     *
94     * @param reply storage for http reply
95     */
96     void requestFinished(QNetworkReply *reply);
97
98     /**
99     * @brief Public slot, which indicates when facebook credentials are ready
100     *
101     * @param credentials New credentials
102     */
103     void credentialsReady(const FacebookCredentials &credentials);
104
105 private:
106
107     /**
108     * @brief Forms a http url
109     *
110     * @param baseUrl Server url
111     * @param phpScript Server script
112     * @param urlParameters optional parameters for url
113     * @return QUrl formed url
114     */
115     QUrl formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters = 0);
116
117     /**
118     * @brief Forms url parameters
119     *
120     * @param coordinates current coordinates
121     * @param status optional status message
122     * @param publish optional publish location on Facebook wall (true/false)
123     * @return QString
124     */
125     QString formUrlParameters(const QPointF &coordinates, QString status = 0, QString publish = 0);
126
127     /**
128     * @brief Parses user and friend data from JSON string
129     *
130     * @param jsonReply JSON string
131     */
132     void parseUserData(const QByteArray &jsonReply);
133
134     /**
135     * @brief Sends http request
136     *
137     * @param url destination
138     * @param cookieType type of the cookie
139     * @param cookie http cookie
140     */
141     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
142
143     /**
144     * @brief Requests ImageFetcher if user/friend has a profile image
145     *        uses members: m_user and m_friendsList
146     *
147     */
148     void addProfileImages();
149
150 private slots:
151
152     /**
153     * @brief Slot for received images
154     *
155     * @param url Image url
156     * @param image Received image
157     */
158     void imageReceived(const QUrl &url, const QPixmap &image);
159
160 /*******************************************************************************
161  * SIGNALS
162  ******************************************************************************/
163
164 signals:
165
166     /**
167     * @brief Signals error
168     *
169     * @param error error message
170     */
171     void error(const QString &error);
172
173     /**
174     * @brief Signal for image fetching
175     *
176     * @param url Image url
177     */
178     void fetchImage(const QUrl &url);
179
180     /**
181     * @brief Signals when address data is retrieved
182     *
183     * @param address Street address
184     */
185     void reverseGeoReady(const QString &address);
186
187     /**
188     * @brief Signals when user data is retrieved
189     *
190     * @param user instance of user
191     * @param friendList list of friends
192     */
193     void userDataChanged(User *user, QList<User *> &friendList);
194
195 /*******************************************************************************
196  * DATA MEMBERS
197  ******************************************************************************/
198
199 private:
200
201     FacebookCredentials m_credentials; ///< handle for FacebookCredentials
202     QList<QNetworkReply *> m_currentRequests; ///< List of current http requests
203     QNetworkAccessManager *m_networkManager; ///< Pointer to QNetworkAccessManager
204     ImageFetcher *m_imageFetcher;
205
206     User *m_user; ///< Pointer to User
207     QList<User *> m_friendsList; ///< List of friends(User)
208     int m_visited; ///< Indicates number of friends with profile images
209     int m_nbrOfImages; ///< Indicates number of friends whose profile images have been downloaded
210     bool m_defaultImage; ///< Indicates if some of the friends or the user does not have a profile image
211 };
212
213 #endif // SITUARESERVICE_H