2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Henri Lampela - henri.lampela@ixonos.com
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.
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.
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,
22 #include <QtAlgorithms>
26 #include <QStringList>
28 #include <QNetworkReply>
29 #include "situareservice.h"
30 #include "situarecommon.h"
31 #include "../cookiehandler/cookiehandler.h"
34 SituareService::SituareService(QObject *parent)
37 qDebug() << __PRETTY_FUNCTION__;
39 m_networkManager = new QNetworkAccessManager;
40 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
42 m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
43 connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
44 connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
45 SLOT(imageReceived(QUrl, QPixmap)));
46 connect(m_imageFetcher, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
51 SituareService::~SituareService()
53 qDebug() << __PRETTY_FUNCTION__;
55 delete m_networkManager;
61 delete m_imageFetcher;
64 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
65 m_friendsList.clear();
68 void SituareService::debugData()
70 qWarning() << __PRETTY_FUNCTION__;
71 QFile file("/home/ramosam-local/situare_debug_data.txt");
72 qWarning() << __PRETTY_FUNCTION__ << "file size: " << file.size();
73 qWarning() << __PRETTY_FUNCTION__ << "file open success?" << file.open(QIODevice::ReadOnly);
74 QByteArray debugReplyArray = file.readAll();
75 qWarning() << __PRETTY_FUNCTION__ << "did read" << debugReplyArray.size();
76 parseUserData(debugReplyArray);
79 void SituareService::fetchLocations()
81 qDebug() << __PRETTY_FUNCTION__;
83 CookieHandler cookieHandler;
85 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
86 m_credentials.sessionKey(), m_credentials.sessionSecret(),
87 m_credentials.sig(), EN_LOCALE);
89 QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
90 sendRequest(url, COOKIE, cookie);
93 void SituareService::reverseGeo(const QPointF &coordinates)
95 qDebug() << __PRETTY_FUNCTION__;
97 CookieHandler cookieHandler;
99 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
100 m_credentials.sessionKey(), m_credentials.sessionSecret(),
101 m_credentials.sig(), EN_LOCALE);
103 QString urlParameters = formUrlParameters(coordinates);
104 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
106 sendRequest(url, COOKIE, cookie);
109 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
112 qDebug() << __PRETTY_FUNCTION__;
114 CookieHandler cookieHandler;
116 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
117 m_credentials.sessionKey(), m_credentials.sessionSecret(),
118 m_credentials.sig(), EN_LOCALE);
121 QString publishValue;
123 publishValue = PUBLISH_TRUE;
126 publishValue = PUBLISH_FALSE;
128 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
129 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
131 sendRequest(url, COOKIE, cookie);
134 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
136 qDebug() << __PRETTY_FUNCTION__;
139 urlString.append(baseUrl);
140 urlString.append(phpScript);
141 if(!urlParameters.isEmpty())
142 urlString.append(urlParameters);
144 QUrl url = QUrl(urlString);
151 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
155 parameters.append(QUESTION_MARK);
156 parameters.append(LATITUDE);
157 parameters.append(EQUAL_MARK);
158 parameters.append(QString::number(coordinates.x()));
159 parameters.append(AMBERSAND_MARK);
160 parameters.append(LONGTITUDE);
161 parameters.append(EQUAL_MARK);
162 parameters.append(QString::number(coordinates.y()));
164 if(publish.compare(PUBLISH_TRUE) == 0) {
165 parameters.append(AMBERSAND_MARK);
166 parameters.append(PUBLISH);
167 parameters.append(EQUAL_MARK);
168 parameters.append(PUBLISH_TRUE);
170 else if(publish.compare(PUBLISH_FALSE) == 0) {
171 parameters.append(AMBERSAND_MARK);
172 parameters.append(PUBLISH);
173 parameters.append(EQUAL_MARK);
174 parameters.append(PUBLISH_FALSE);
177 if(!status.isEmpty()) {
178 parameters.append(AMBERSAND_MARK);
179 parameters.append(DATA);
180 parameters.append(EQUAL_MARK);
181 parameters.append(status);
187 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
189 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
191 QNetworkRequest request;
194 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
196 QNetworkReply *reply = m_networkManager->get(request);
198 m_currentRequests.append(reply);
201 void SituareService::requestFinished(QNetworkReply *reply)
203 qDebug() << __PRETTY_FUNCTION__;
205 QUrl url = reply->url();
206 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
207 if (reply->error()) {
208 qDebug() << reply->errorString();
209 emit error(reply->errorString());
212 qint64 max = reply->size();
213 QByteArray replyArray = reply->read(max);
214 qDebug() << "Reply from: " << url << "reply " << replyArray;
215 // ToDo: results handling includes Situare's errors
216 // works like situare's error handling i.e. both lat & lon are missing/wrong
217 // -> we get only error for wrong lon
218 if(replyArray == ERROR_LAT.toAscii()) {
219 qDebug() << "Error: " << ERROR_LAT;
220 emit error(replyArray);
222 else if(replyArray == ERROR_LON.toAscii()) {
223 qDebug() << "Error: " << ERROR_LON;
224 emit error(replyArray);
226 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
227 qDebug() << "Error: " << ERROR_SESSION;
228 emit error(replyArray);
230 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
231 qDebug() << "JSON string";
232 parseUserData(replyArray);
234 else if(replyArray == "") {
235 qDebug() << "No error, update was successful";
236 emit updateWasSuccessful();
239 // Street address ready
240 QString address(replyArray);
241 emit reverseGeoReady(address);
244 m_currentRequests.removeAll(reply);
245 reply->deleteLater();
248 void SituareService::credentialsReady(const FacebookCredentials &credentials)
250 qDebug() << __PRETTY_FUNCTION__;
251 m_credentials = credentials;
255 void SituareService::parseUserData(const QByteArray &jsonReply)
257 qDebug() << __PRETTY_FUNCTION__;
261 m_defaultImage = false;
262 qDeleteAll(m_friendsList.begin(), m_friendsList.end());
263 m_friendsList.clear();
270 QJson::Parser parser;
273 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
276 qFatal("An error occurred during parsing");
280 QVariant userVariant = result.value("user");
281 QMap<QString, QVariant> userMap = userVariant.toMap();
283 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
285 QUrl imageUrl = userMap["profile_pic"].toUrl();
287 if(imageUrl.isEmpty()) {
288 // user doesn't have profile image, so we need to get him a silhouette image
289 m_defaultImage = true;
292 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
293 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
294 true, userMap["uid"].toString());
296 foreach (QVariant friendsVariant, result["friends"].toList()) {
297 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
298 QVariant distance = friendMap["distance"];
299 QMap<QString, QVariant> distanceMap = distance.toMap();
301 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
303 QUrl imageUrl = friendMap["profile_pic"].toUrl();
305 if(imageUrl.isEmpty()) {
306 // friend doesn't have profile image, so we need to get him a silhouette image
307 m_defaultImage = true;
310 User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
311 friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
312 false, friendMap["uid"].toString(), distanceMap["units"].toString(),
313 distanceMap["value"].toDouble());
315 m_friendsList.append(user);
320 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
322 qDebug() << __PRETTY_FUNCTION__;
323 qDebug() << "Image URL: " << url << " size :" << image.size();
325 // assign facebook silhouette image to all who doesn't have a profile image
326 if(url == QUrl(SILHOUETTE_URL)) {
327 if(m_user->profileImageUrl().isEmpty()) {
328 m_user->setProfileImage(image);
330 for(int i=0;i < m_friendsList.count();i++) {
331 if(m_friendsList.at(i)->profileImageUrl().isEmpty()) {
332 m_friendsList.at(i)->setProfileImage(image);
337 if(m_user->profileImageUrl() == url) {
338 m_user->setProfileImage(image);
341 for(int i=0;i<m_friendsList.count();i++) {
342 if(m_friendsList.at(i)->profileImageUrl() == url) {
343 m_friendsList.at(i)->setProfileImage(image);
344 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
348 if(m_nbrOfImages == m_visited) {
349 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
350 qDebug() << "emit userDataChanged";
351 emit userDataChanged(m_user, m_friendsList);
355 void SituareService::addProfileImages()
357 qDebug() << __PRETTY_FUNCTION__;
359 // reduce net traffic by sending only one download request for facebook silhouette image
361 emit fetchImage(QUrl(SILHOUETTE_URL));
364 if(!m_user->profileImageUrl().isEmpty() && m_user->profileImageUrl().isValid()) {
365 emit fetchImage(m_user->profileImageUrl());
368 for(int i=0;i<m_friendsList.count();i++) {
369 if(!m_friendsList.at(i)->profileImageUrl().isEmpty() &&
370 m_friendsList.at(i)->profileImageUrl().isValid()) {
371 m_visited++; // indicates how many friends that have profile image
372 emit fetchImage(m_friendsList.at(i)->profileImageUrl());