Code reviewed by Ville Tiensuu, findings fixed. Removed teststringformations unit...
[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 <QDebug>
23 #include <QtGlobal>
24 #include <QStringList>
25 #include "situareservice.h"
26 #include "situarecommon.h"
27 #include "../cookiehandler/cookiehandler.h"
28
29 SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
30         : QObject(parent), m_networkManager(manager)
31 {
32     qDebug() << __PRETTY_FUNCTION__;
33
34     connect(&m_facebookAuthentication, SIGNAL(credentialsReady()), SLOT(credentialsReady()));
35
36     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
37
38     m_credentials = m_facebookAuthentication.loginCredentials();
39 }
40
41 SituareService::~SituareService()
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     delete m_networkManager;
46 }
47
48 void SituareService::updateLocation(const QPointF &coordinates, const QString &status,
49                                     const bool &publish)
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52
53     CookieHandler cookieHandler;
54
55     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
56                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
57                                               m_credentials.sig(), EN_LOCALE);
58
59
60     QString publishValue;
61     if(publish) {
62         publishValue = PUBLISH_TRUE;
63     }
64     else {
65         publishValue = PUBLISH_FALSE;
66     }
67     QString urlParameters = formUrlParameters(coordinates, status, publishValue);
68     QUrl url = formUrl(SITUARE_URL, UPDATE_LOCATION, urlParameters);
69
70     sendRequest(url, COOKIE, cookie);
71 }
72
73 void SituareService::reverseGeo(const QPointF &coordinates)
74 {
75     qDebug() << __PRETTY_FUNCTION__;
76
77     CookieHandler cookieHandler;
78
79     QString cookie = cookieHandler.formCookie(API_KEY, m_credentials.expires(), m_credentials.userID(),
80                                               m_credentials.sessionKey(), m_credentials.sessionSecret(),
81                                               m_credentials.sig(), EN_LOCALE);
82
83     QString urlParameters = formUrlParameters(coordinates);
84     QUrl url = formUrl(SITUARE_URL, REVERSE_GEO, urlParameters);
85
86     sendRequest(url, COOKIE, cookie);
87 }
88
89 QUrl SituareService::formUrl(const QString &baseUrl, const QString &phpScript, QString urlParameters)
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92     QString urlString;
93
94     urlString.append(baseUrl);
95     urlString.append(phpScript);
96     if(urlParameters != NULL)
97         urlString.append(urlParameters);
98
99     QUrl url = QUrl(urlString);
100
101     qDebug() << url;
102
103     return url;
104 }
105
106 QString SituareService::formUrlParameters(const QPointF &coordinates, QString status, QString publish)
107 {
108     QString parameters;
109
110     parameters.append(QUESTION_MARK);
111     parameters.append(LATITUDE);
112     parameters.append(EQUAL_MARK);
113     parameters.append(QString::number(coordinates.x()));
114     parameters.append(AMBERSAND_MARK);
115     parameters.append(LONGTITUDE);
116     parameters.append(EQUAL_MARK);
117     parameters.append(QString::number(coordinates.y()));
118
119     if(publish.compare(PUBLISH_TRUE) == 0) {
120         parameters.append(AMBERSAND_MARK);
121         parameters.append(PUBLISH);
122         parameters.append(EQUAL_MARK);
123         parameters.append(PUBLISH_TRUE);
124     }
125     else if(publish.compare(PUBLISH_FALSE) == 0) {
126         parameters.append(AMBERSAND_MARK);
127         parameters.append(PUBLISH);
128         parameters.append(EQUAL_MARK);
129         parameters.append(PUBLISH_FALSE);
130     }
131
132     if(status != NULL) {
133         parameters.append(AMBERSAND_MARK);
134         parameters.append(DATA);
135         parameters.append(EQUAL_MARK);
136         parameters.append(status);
137     }
138
139     return parameters;
140 }
141
142 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
143 {
144     qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
145
146     QNetworkRequest request;
147
148     request.setUrl(url);
149     request.setRawHeader(cookieType.toAscii(), cookie.toUtf8());
150
151     QNetworkReply *reply = m_networkManager->get(request);
152
153     m_currentRequests.append(reply);
154 }
155
156 void SituareService::requestFinished(QNetworkReply *reply)
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     QUrl url = reply->url();
161     qDebug() << "BytesAvailable: " << reply->bytesAvailable();
162     if (reply->error()) {
163         qDebug() << reply->errorString();
164         emit error(reply->errorString());
165         // ToDo: some general http error handling etc, signal UI?
166     }
167     else {
168         qint64 max = reply->size();
169         QByteArray replyArray = reply->read(max);
170         qDebug() << "Reply from: " << url << "reply " << replyArray;
171         // ToDo: results handling includes Situare's errors
172         // works like situare's error handling i.e. both lat & lon are missing/wrong
173         // -> we get only error for wrong lon
174         if(replyArray == ERROR_LAT.toAscii()) {
175             qDebug() << "Error: " << ERROR_LAT;
176             // ToDo: signal UI?
177             emit error(replyArray);
178         }
179         else if(replyArray == ERROR_LON.toAscii()) {
180             qDebug() << "Error: " << ERROR_LON;
181             // ToDo: signal UI?
182             emit error(replyArray);
183         }
184         else if(replyArray.contains(ERROR_SESSION.toAscii())) {
185             qDebug() << "Error: " << ERROR_SESSION;
186             // ToDo: signal UI?
187             emit error(replyArray);
188         }
189         else {
190             // no error -> update was successful
191             // ToDo: signal UI?
192             qDebug() << reply->read(max);
193         }
194     }
195
196     m_currentRequests.removeAll(reply);
197     reply->deleteLater();
198 }
199
200 void SituareService::credentialsReady()
201 {
202     qDebug() << __PRETTY_FUNCTION__;
203     m_credentials = m_facebookAuthentication.loginCredentials();
204 }