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,
24 #include <QStringList>
26 #include <QNetworkReply>
27 #include "situareservice.h"
28 #include "situarecommon.h"
29 #include "../cookiehandler/cookiehandler.h"
32 SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
33 : QObject(parent), m_networkManager(manager)
35 qDebug() << __PRETTY_FUNCTION__;
37 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
39 m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
40 connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
41 connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
42 SLOT(imageReceived(QUrl, QPixmap)));
46 SituareService::~SituareService()
48 qDebug() << __PRETTY_FUNCTION__;
50 delete m_networkManager;
56 void SituareService::fetchLocations()
58 qDebug() << __PRETTY_FUNCTION__;
60 CookieHandler cookieHandler;
62 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
63 m_credentials.sessionKey(), m_credentials.sessionSecret(),
64 m_credentials.sig(), EN_LOCALE);
66 QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
68 sendRequest(url, COOKIE, cookie);
71 void SituareService::reverseGeo(const QPointF &coordinates)
73 qDebug() << __PRETTY_FUNCTION__;
75 CookieHandler cookieHandler;
77 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
78 m_credentials.sessionKey(), m_credentials.sessionSecret(),
79 m_credentials.sig(), EN_LOCALE);
81 QString urlParameters = formUrlParameters(coordinates);
82 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
84 sendRequest(url, COOKIE, cookie);
87 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
90 qDebug() << __PRETTY_FUNCTION__;
92 CookieHandler cookieHandler;
94 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
95 m_credentials.sessionKey(), m_credentials.sessionSecret(),
96 m_credentials.sig(), EN_LOCALE);
101 publishValue = PUBLISH_TRUE;
104 publishValue = PUBLISH_FALSE;
106 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
107 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
109 sendRequest(url, COOKIE, cookie);
112 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
114 qDebug() << __PRETTY_FUNCTION__;
117 urlString.append(baseUrl);
118 urlString.append(phpScript);
119 if(urlParameters != NULL)
120 urlString.append(urlParameters);
122 QUrl url = QUrl(urlString);
129 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
133 parameters.append(QUESTION_MARK);
134 parameters.append(LATITUDE);
135 parameters.append(EQUAL_MARK);
136 parameters.append(QString::number(coordinates.x()));
137 parameters.append(AMBERSAND_MARK);
138 parameters.append(LONGTITUDE);
139 parameters.append(EQUAL_MARK);
140 parameters.append(QString::number(coordinates.y()));
142 if(publish.compare(PUBLISH_TRUE) == 0) {
143 parameters.append(AMBERSAND_MARK);
144 parameters.append(PUBLISH);
145 parameters.append(EQUAL_MARK);
146 parameters.append(PUBLISH_TRUE);
148 else if(publish.compare(PUBLISH_FALSE) == 0) {
149 parameters.append(AMBERSAND_MARK);
150 parameters.append(PUBLISH);
151 parameters.append(EQUAL_MARK);
152 parameters.append(PUBLISH_FALSE);
156 parameters.append(AMBERSAND_MARK);
157 parameters.append(DATA);
158 parameters.append(EQUAL_MARK);
159 parameters.append(status);
165 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
167 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
169 QNetworkRequest request;
172 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
174 QNetworkReply *reply = m_networkManager->get(request);
176 m_currentRequests.append(reply);
179 void SituareService::requestFinished(QNetworkReply *reply)
181 qDebug() << __PRETTY_FUNCTION__;
183 QUrl url = reply->url();
184 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
185 if (reply->error()) {
186 qDebug() << reply->errorString();
187 emit error(reply->errorString());
188 // ToDo: some general http error handling etc, signal UI?
191 qint64 max = reply->size();
192 QByteArray replyArray = reply->read(max);
193 qDebug() << "Reply from: " << url << "reply " << replyArray;
194 // ToDo: results handling includes Situare's errors
195 // works like situare's error handling i.e. both lat & lon are missing/wrong
196 // -> we get only error for wrong lon
197 if(replyArray == ERROR_LAT.toAscii()) {
198 qDebug() << "Error: " << ERROR_LAT;
200 emit error(replyArray);
202 else if(replyArray == ERROR_LON.toAscii()) {
203 qDebug() << "Error: " << ERROR_LON;
205 emit error(replyArray);
207 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
208 qDebug() << "Error: " << ERROR_SESSION;
210 emit error(replyArray);
212 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
213 qDebug() << "JSON string";
214 parseUserData(replyArray);
216 else if(replyArray == "") {
217 qDebug() << "No error, update was successful";
220 // Street address ready
221 QString address(replyArray);
222 emit reverseGeoReady(address);
225 m_currentRequests.removeAll(reply);
226 reply->deleteLater();
229 void SituareService::credentialsReady(const FacebookCredentials &credentials)
231 qDebug() << __PRETTY_FUNCTION__;
232 m_credentials = credentials;
236 void SituareService::parseUserData(const QByteArray &jsonReply)
238 qDebug() << __PRETTY_FUNCTION__;
242 m_friendsList.clear();
244 QJson::Parser parser;
247 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
250 qFatal("An error occurred during parsing");
254 QVariant userVariant = result.value("user");
255 QMap<QString, QVariant> userMap = userVariant.toMap();
257 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
259 QUrl imageUrl = userMap["profile_pic"].toUrl();
261 m_user = new User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
262 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
263 true, userMap["uid"].toString());
265 foreach (QVariant friendsVariant, result["friends"].toList()) {
266 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
267 QVariant distance = friendMap["distance"];
268 QMap<QString, QVariant> distanceMap = distance.toMap();
270 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
272 QUrl imageUrl = friendMap["profile_pic"].toUrl();
274 User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
275 friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
276 false, friendMap["uid"].toString(), distanceMap["units"].toString(),
277 distanceMap["value"].toDouble());
279 m_friendsList.append(user);
284 void SituareService::imageReceived(const QUrl &url, const QPixmap &image)
286 qDebug() << __PRETTY_FUNCTION__;
287 qDebug() << "Image URL: " << url << " size :" << image.size();
289 if(m_user->profileImageUrl() == url) {
290 m_user->setProfileImage(image);
293 for(int i=0;i<m_friendsList.count();i++) {
294 if(m_friendsList.at(i)->profileImageUrl() == url) {
295 m_friendsList.at(i)->setProfileImage(image);
296 m_nbrOfImages++; // indicates how many friend profile images has been downloaded
300 if(m_nbrOfImages == m_visited) {
301 qDebug() << "m_nbrOfImages: " << m_nbrOfImages << " m_visited: " << m_visited;
302 qDebug() << "emit userDataChanged";
303 emit userDataChanged(m_user, m_friendsList);
307 void SituareService::addProfileImages()
309 qDebug() << __PRETTY_FUNCTION__;
311 if(m_user->profileImageUrl() != QUrl("")) {
312 emit fetchImage(m_user->profileImageUrl());
315 for(int i=0;i<m_friendsList.count();i++) {
316 if(m_friendsList.at(i)->profileImageUrl() != QUrl("")) {
317 m_visited++; // indicates how many friends that have profile image
318 emit fetchImage(m_friendsList.at(i)->profileImageUrl());