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>
25 #include "situareservice.h"
26 #include "situarecommon.h"
27 #include "../cookiehandler/cookiehandler.h"
30 SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
31 : QObject(parent), m_networkManager(manager)
33 qDebug() << __PRETTY_FUNCTION__;
35 connect(&m_facebookAuthentication, SIGNAL(credentialsReady()), SLOT(credentialsReady()));
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,QImage)), this,
42 SLOT(imageReceived(QUrl, QImage)));
44 m_credentials = m_facebookAuthentication.loginCredentials();
47 SituareService::~SituareService()
49 qDebug() << __PRETTY_FUNCTION__;
51 delete m_networkManager;
54 void SituareService::fetchLocations()
56 qDebug() << __PRETTY_FUNCTION__;
58 CookieHandler cookieHandler;
60 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
61 m_credentials.sessionKey(), m_credentials.sessionSecret(),
62 m_credentials.sig(), EN_LOCALE);
64 QUrl url = formUrl(SITUARE_URL, GET_LOCATIONS);
66 sendRequest(url, COOKIE, cookie);
69 void SituareService::reverseGeo(const QPointF &coordinates)
71 qDebug() << __PRETTY_FUNCTION__;
73 CookieHandler cookieHandler;
75 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
76 m_credentials.sessionKey(), m_credentials.sessionSecret(),
77 m_credentials.sig(), EN_LOCALE);
79 QString urlParameters = formUrlParameters(coordinates);
80 QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
82 sendRequest(url, COOKIE, cookie);
85 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
88 qDebug() << __PRETTY_FUNCTION__;
90 CookieHandler cookieHandler;
92 QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
93 m_credentials.sessionKey(), m_credentials.sessionSecret(),
94 m_credentials.sig(), EN_LOCALE);
99 publishValue = PUBLISH_TRUE;
102 publishValue = PUBLISH_FALSE;
104 QString urlParameters = formUrlParameters(coordinates, status, publishValue);
105 QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
107 sendRequest(url, COOKIE, cookie);
110 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
112 qDebug() << __PRETTY_FUNCTION__;
115 urlString.append(baseUrl);
116 urlString.append(phpScript);
117 if(urlParameters != NULL)
118 urlString.append(urlParameters);
120 QUrl url = QUrl(urlString);
127 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
131 parameters.append(QUESTION_MARK);
132 parameters.append(LATITUDE);
133 parameters.append(EQUAL_MARK);
134 parameters.append(QString::number(coordinates.x()));
135 parameters.append(AMBERSAND_MARK);
136 parameters.append(LONGTITUDE);
137 parameters.append(EQUAL_MARK);
138 parameters.append(QString::number(coordinates.y()));
140 if(publish.compare(PUBLISH_TRUE) == 0) {
141 parameters.append(AMBERSAND_MARK);
142 parameters.append(PUBLISH);
143 parameters.append(EQUAL_MARK);
144 parameters.append(PUBLISH_TRUE);
146 else if(publish.compare(PUBLISH_FALSE) == 0) {
147 parameters.append(AMBERSAND_MARK);
148 parameters.append(PUBLISH);
149 parameters.append(EQUAL_MARK);
150 parameters.append(PUBLISH_FALSE);
154 parameters.append(AMBERSAND_MARK);
155 parameters.append(DATA);
156 parameters.append(EQUAL_MARK);
157 parameters.append(status);
163 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
165 qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
167 QNetworkRequest request;
170 request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
172 QNetworkReply *reply = m_networkManager->get(request);
174 m_currentRequests.append(reply);
177 void SituareService::requestFinished(QNetworkReply *reply)
179 qDebug() << __PRETTY_FUNCTION__;
181 QUrl url = reply->url();
182 qDebug() << "BytesAvailable: " << reply->bytesAvailable();
183 if (reply->error()) {
184 qDebug() << reply->errorString();
185 emit error(reply->errorString());
186 // ToDo: some general http error handling etc, signal UI?
189 qint64 max = reply->size();
190 QByteArray replyArray = reply->read(max);
191 qDebug() << "Reply from: " << url << "reply " << replyArray;
192 // ToDo: results handling includes Situare's errors
193 // works like situare's error handling i.e. both lat & lon are missing/wrong
194 // -> we get only error for wrong lon
195 if(replyArray == ERROR_LAT.toAscii()) {
196 qDebug() << "Error: " << ERROR_LAT;
198 emit error(replyArray);
200 else if(replyArray == ERROR_LON.toAscii()) {
201 qDebug() << "Error: " << ERROR_LON;
203 emit error(replyArray);
205 else if(replyArray.contains(ERROR_SESSION.toAscii())) {
206 qDebug() << "Error: " << ERROR_SESSION;
208 emit error(replyArray);
210 else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
211 qDebug() << "JSON string";
212 parseUserData(replyArray);
214 else if(replyArray == "") {
215 qDebug() << "No error, update was successful";
218 // Street address ready
219 QString address(replyArray);
220 emit reverseGeoReady(address);
223 m_currentRequests.removeAll(reply);
224 reply->deleteLater();
227 void SituareService::credentialsReady()
229 qDebug() << __PRETTY_FUNCTION__;
230 m_credentials = m_facebookAuthentication.loginCredentials();
233 void SituareService::parseUserData(const QByteArray &jsonReply)
235 qDebug() << __PRETTY_FUNCTION__;
237 QJson::Parser parser;
240 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
243 qFatal("An error occurred during parsing");
247 QList<User *> friendsList;
249 QVariant userVariant = result.value("user");
250 QMap<QString, QVariant> userMap = userVariant.toMap();
252 QPointF coordinates(userMap["longitude"].toReal(), userMap["latitude"].toReal());
254 QUrl imageUrl = userMap["profile_pic"].toUrl();
256 User user = User(userMap["address"].toString(), coordinates, userMap["name"].toString(),
257 userMap["note"].toString(), imageUrl, userMap["timestamp"].toString(),
258 true, userMap["uid"].toString());
260 foreach (QVariant friendsVariant, result["friends"].toList()) {
261 QMap<QString, QVariant> friendMap = friendsVariant.toMap();
262 QVariant distance = friendMap["distance"];
263 QMap<QString, QVariant> distanceMap = distance.toMap();
265 QPointF coordinates(friendMap["longitude"].toReal(), friendMap["latitude"].toReal());
267 QUrl imageUrl = friendMap["profile_pic"].toUrl();
269 User *user = new User(friendMap["address"].toString(), coordinates, friendMap["name"].toString(),
270 friendMap["note"].toString(), imageUrl, friendMap["timestamp"].toString(),
271 false, friendMap["uid"].toString(), distanceMap["units"].toString(),
272 distanceMap["value"].toDouble());
274 friendsList.append(user);
276 emit userDataChanged(user, friendsList);
279 void SituareService::imageReceived(const QUrl &url, const QImage &image)
281 qDebug() << __PRETTY_FUNCTION__;
282 qDebug() << "Image URL: " << url << " size :" << image.size();