Added error signals to engine from ImageFetcher and SituareService.
[situare] / src / situareservice / situareservice.cpp
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 #include <QtAlgorithms>
23 #include <QDebug>
24 #include <QtGlobal>
25 #include <QStringList>
26 #include <QPixmap>
27 #include <QNetworkReply>
28 #include "situareservice.h"
29 #include "situarecommon.h"
30 #include "../cookiehandler/cookiehandler.h"
31 #include "parser.h"
32
33 SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
34         : QObject(parent), m_networkManager(manager)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37
38     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
39
40     m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
41     connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
42     connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
43             SLOT(imageReceived(QUrl, QPixmap)));
44     connect(m_imageFetcher, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
45
46     m_user = NULL;
47 }
48
49 SituareService::~SituareService()
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52
53     delete m_networkManager;
54     m_networkManager = NULL;
55     if(m_user) {
56         delete m_user;
57         m_user = NULL;
58     }
59     delete m_imageFetcher;
60     m_imageFetcher = NULL;
61
62     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
63     m_friendsList.clear();
64 }
65
66 void SituareService::fetchLocations()
67 {
68     qDebug() << __PRETTY_FUNCTION__;
69
70     CookieHandler cookieHandler;
71
72     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
73                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
74                                               m_credentials.sig(), EN_LOCALE);
75
76     QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
77
78     sendRequest(url, COOKIE, cookie);
79 }
80
81 void SituareService::reverseGeo(const QPointF &coordinates)
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84
85     CookieHandler cookieHandler;
86
87     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
88                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
89                                               m_credentials.sig(), EN_LOCALE);
90
91     QString urlParameters = formUrlParameters(coordinates);
92     QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
93
94     sendRequest(url, COOKIE, cookie);
95 }
96
97 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
98                                     const bool &publish)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     CookieHandler cookieHandler;
103
104     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
105                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
106                                               m_credentials.sig(), EN_LOCALE);
107
108
109     QString publishValue;
110     if(publish) {
111         publishValue = PUBLISH_TRUE;
112     }
113     else {
114         publishValue = PUBLISH_FALSE;
115     }
116     QString urlParameters = formUrlParameters(coordinates, status, publishValue);
117     QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
118
119     sendRequest(url, COOKIE, cookie);
120 }
121
122 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125     QString urlString;
126
127     urlString.append(baseUrl);
128     urlString.append(phpScript);
129     if(urlParameters != NULL)
130         urlString.append(urlParameters);
131
132     QUrl url = QUrl(urlString);
133
134     qDebug() << url;
135
136     return url;
137 }
138
139 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
140 {
141     QString parameters;
142
143     parameters.append(QUESTION_MARK);
144     parameters.append(LATITUDE);
145     parameters.append(EQUAL_MARK);
146     parameters.append(QString::number(coordinates.x()));
147     parameters.append(AMBERSAND_MARK);
148     parameters.append(LONGTITUDE);
149     parameters.append(EQUAL_MARK);
150     parameters.append(QString::number(coordinates.y()));
151
152     if(publish.compare(PUBLISH_TRUE) == 0) {
153         parameters.append(AMBERSAND_MARK);
154         parameters.append(PUBLISH);
155         parameters.append(EQUAL_MARK);
156         parameters.append(PUBLISH_TRUE);
157     }
158     else if(publish.compare(PUBLISH_FALSE) == 0) {
159         parameters.append(AMBERSAND_MARK);
160         parameters.append(PUBLISH);
161         parameters.append(EQUAL_MARK);
162         parameters.append(PUBLISH_FALSE);
163     }
164
165     if(status != NULL) {
166         parameters.append(AMBERSAND_MARK);
167         parameters.append(DATA);
168         parameters.append(EQUAL_MARK);
169         parameters.append(status);
170     }
171
172     return parameters;
173 }
174
175 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
176 {
177     qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
178
179     QNetworkRequest request;
180
181     request.setUrl(url);
182     request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
183
184     QNetworkReply *reply = m_networkManager->get(request);
185
186     m_currentRequests.append(reply);
187 }
188
189 void SituareService::requestFinished(QNetworkReply *reply)
190 {
191     qDebug() << __PRETTY_FUNCTION__;
192
193     QUrl url = reply->url();
194     qDebug() << "BytesAvailable: " << reply->bytesAvailable();
195     if (reply->error()) {
196         qDebug() << reply->errorString();
197         emit error(reply->errorString());
198     }
199     else {
200         qint64 max = reply->size();
201         QByteArray replyArray = reply->read(max);
202         qDebug() << "Reply from: " << url << "reply " << replyArray;
203         // ToDo: results handling includes Situare's errors
204         // works like situare's error handling i.e. both lat & lon are missing/wrong
205         // -> we get only error for wrong lon
206         if(replyArray == ERROR_LAT.toAscii()) {
207             qDebug() << "Error: " << ERROR_LAT;
208             emit error(replyArray);
209         }
210         else if(replyArray == ERROR_LON.toAscii()) {
211             qDebug() << "Error: " << ERROR_LON;
212             emit error(replyArray);
213         }
214         else if(replyArray.contains(ERROR_SESSION.toAscii())) {
215             qDebug() << "Error: " << ERROR_SESSION;
216             emit error(replyArray);
217         }
218         else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
219             qDebug() << "JSON string";
220             parseUserData(replyArray);
221         }
222         else if(replyArray == "") {
223             qDebug() << "No error, update was successful";
224             // ToDo: signal UI?
225         }
226         else {
227             // Street address ready
228             QString address(replyArray);
229             emit reverseGeoReady(address);
230         }
231     }
232     m_currentRequests.removeAll(reply);
233     reply->deleteLater();
234 }
235
236 void SituareService::credentialsReady(const FacebookCredentials &credentials)
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239     m_credentials = credentials;
240     
241 }
242
243 void SituareService::parseUserData(const QByteArray &jsonReply)
244 {
245     qDebug() << __PRETTY_FUNCTION__;
246
247     m_visited = 0;
248     m_nbrOfImages = 0;
249     m_defaultImage = false;
250     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
251     m_friendsList.clear();
252
253     QJson::Parser parser;
254     bool ok;
255
256     QVariantMap result = parser.parse (jsonReply, &ok).toMap();
257     if (!ok) {
258
259         qFatal("An error occurred during parsing");
260         exit (1);
261     }
262
263     QVariant userVariant = result.value("user");
264     QMap<QString, QVariant> userMap = userVariant.toMap();
265
266     QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
267
268     QUrl imageUrl = userMap["profile_pic"].toUrl();
269
270     if(imageUrl.isEmpty()) {
271         m_defaultImage = true;
272     }
273
274     m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
275                   userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
276                   true, userMap["uid"].toString());
277
278     foreach (QVariant friendsVariant, result["friends"].toList()) {
279       QMap<QString, QVariant> friendMap = friendsVariant.toMap();
280       QVariant distance = friendMap["distance"];
281       QMap<QString, QVariant> distanceMap = distance.toMap();
282
283       QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
284
285       QUrl imageUrl = friendMap["profile_pic"].toUrl();
286
287       if(imageUrl.isEmpty()) {
288           m_defaultImage = true;
289       }
290
291       User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
292                             friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
293                             false, friendMap["uid"].toString(), distanceMap["units"].toString(),
294                             distanceMap["value"].toDouble());
295
296       m_friendsList.append(user);
297     }
298     addProfileImages();
299 }
300
301 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
302 {
303     qDebug() << __PRETTY_FUNCTION__;
304     qDebug() << "Image URL: " << url << " size :" << image.size();
305
306     // assign facebook silhouette image to all who doesn't have a profile image
307     if(url == QUrl(SILHOUETTE_URL)) {
308         if(m_user->profileImageUrl().isEmpty()) {
309             m_user->setProfileImage(image);
310         }
311         for(int i=0;i < m_friendsList.count();i++) {
312             if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
313                 m_friendsList.at(i)->setProfileImage(image);
314             }
315         }
316     }
317
318     if(m_user->profileImageUrl() == url) {
319         m_user->setProfileImage(image);
320     }
321
322     for(int i=0;i<m_friendsList.count();i++) {
323         if(m_friendsList.at(i)->profileImageUrl() == url) {
324             m_friendsList.at(i)->setProfileImage(image);
325             m_nbrOfImages++; // indicates how many friend profile images has been downloaded
326         }
327     }
328
329     if(m_nbrOfImages == m_visited) {
330         qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
331         qDebug() << "emit userDataChanged";
332         emit userDataChanged(m_user, m_friendsList);
333     }
334 }
335
336 void SituareService::addProfileImages()
337 {
338     qDebug() << __PRETTY_FUNCTION__;
339
340     // reduce net traffic by sending only one download request for facebook silhouette image
341     if(m_defaultImage) {
342         emit fetchImage(QUrl(SILHOUETTE_URL));
343     }
344
345     if(!m_user->profileImageUrl().isEmpty()) {
346         emit fetchImage(m_user->profileImageUrl());
347     }
348
349     for(int i=0;i<m_friendsList.count();i++) {
350         if(!m_friendsList.at(i)->profileImageUrl().isEmpty()) {
351             m_visited++; // indicates how many friends that have profile image
352             emit fetchImage(m_friendsList.at(i)->profileImageUrl());
353         }
354     }
355 }