Fixed memory leak and unused variable (roundness in situareuser.cpp)
[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)
34         : QObject(parent)
35 {
36     qDebug() << __PRETTY_FUNCTION__;
37
38     m_networkManager = new QNetworkAccessManager;
39     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
40
41     m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
42     connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
43     connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
44             SLOT(imageReceived(QUrl, QPixmap)));
45     connect(m_imageFetcher, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
46
47     m_user = 0;
48 }
49
50 SituareService::~SituareService()
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     delete m_networkManager;
55     m_networkManager = 0;
56     if(m_user) {
57         delete m_user;
58         m_user = 0;
59     }
60     delete m_imageFetcher;
61     m_imageFetcher = 0;
62
63     qDeleteAll(m_friendsList.begin(), m_friendsList.end());
64     m_friendsList.clear();
65 }
66
67 void SituareService::fetchLocations()
68 {
69     qDebug() << __PRETTY_FUNCTION__;
70
71     CookieHandler cookieHandler;
72
73     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
74                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
75                                               m_credentials.sig(), EN_LOCALE);
76
77     QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
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.isEmpty())
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.isEmpty()) {
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             emit updateWasSuccessful();
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     if(m_user) {
254         delete m_user;
255         m_user = 0;
256     }
257
258     QJson::Parser parser;
259     bool ok;
260
261     QVariantMap result = parser.parse (jsonReply, &ok).toMap();
262     if (!ok) {
263
264         qFatal("An error occurred during parsing");
265         exit (1);
266     }
267
268     QVariant userVariant = result.value("user");
269     QMap<QString, QVariant> userMap = userVariant.toMap();
270
271     QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
272
273     QUrl imageUrl = userMap["profile_pic"].toUrl();
274
275     if(imageUrl.isEmpty()) {
276         // user doesn't have profile image, so we need to get him a silhouette image
277         m_defaultImage = true;
278     }
279
280     m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
281                   userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
282                   true, userMap["uid"].toString());
283
284     foreach (QVariant friendsVariant, result["friends"].toList()) {
285       QMap<QString, QVariant> friendMap = friendsVariant.toMap();
286       QVariant distance = friendMap["distance"];
287       QMap<QString, QVariant> distanceMap = distance.toMap();
288
289       QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
290
291       QUrl imageUrl = friendMap["profile_pic"].toUrl();
292
293       if(imageUrl.isEmpty()) {
294           // friend doesn't have profile image, so we need to get him a silhouette image
295           m_defaultImage = true;
296       }
297
298       User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
299                             friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
300                             false, friendMap["uid"].toString(), distanceMap["units"].toString(),
301                             distanceMap["value"].toDouble());
302
303       m_friendsList.append(user);
304     }
305     addProfileImages();
306 }
307
308 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
309 {
310     qDebug() << __PRETTY_FUNCTION__;
311     qDebug() << "Image URL: " << url << " size :" << image.size();
312
313     // assign facebook silhouette image to all who doesn't have a profile image
314     if(url == QUrl(SILHOUETTE_URL)) {
315         if(m_user->profileImageUrl().isEmpty()) {
316             m_user->setProfileImage(image);
317         }
318         for(int i=0;i < m_friendsList.count();i++) {
319             if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
320                 m_friendsList.at(i)->setProfileImage(image);
321             }
322         }
323     }
324
325     if(m_user->profileImageUrl() == url) {
326         m_user->setProfileImage(image);
327     }
328
329     for(int i=0;i<m_friendsList.count();i++) {
330         if(m_friendsList.at(i)->profileImageUrl() == url) {
331             m_friendsList.at(i)->setProfileImage(image);
332             m_nbrOfImages++; // indicates how many friend profile images has been downloaded
333         }
334     }
335
336     if(m_nbrOfImages == m_visited) {
337         qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
338         qDebug() << "emit userDataChanged";
339         emit userDataChanged(m_user, m_friendsList);
340     }
341 }
342
343 void SituareService::addProfileImages()
344 {
345     qDebug() << __PRETTY_FUNCTION__;
346
347     // reduce net traffic by sending only one download request for facebook silhouette image
348     if(m_defaultImage) {
349         emit fetchImage(QUrl(SILHOUETTE_URL));
350     }
351
352     if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
353         emit fetchImage(m_user->profileImageUrl());
354     }
355
356     for(int i=0;i<m_friendsList.count();i++) {
357         if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
358            m_friendsList.at(i)->profileImageUrl().isValid()) {
359             m_visited++; // indicates how many friends that have profile image
360             emit fetchImage(m_friendsList.at(i)->profileImageUrl());
361         }
362     }
363 }