First draft, credentials are still saved to settings
[situare] / src / engine / engine.cpp
1  /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6         Henri Lampela - henri.lampela@ixonos.com
7         Jussi Laitinen jussi.laitinen@ixonos.com
8         Sami Rämö - sami.ramo@ixonos.com
9
10     Situare is free software; you can redistribute it and/or
11     modify it under the terms of the GNU General Public License
12     version 2 as published by the Free Software Foundation.
13
14     Situare is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with Situare; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22     USA.
23  */
24
25
26 #include "common.h"
27 #include "facebookservice/facebookauthentication.h"
28 #include "gps/gpsposition.h"
29 #include "map/mapengine.h"
30 #include "situareservice/situareservice.h"
31 #include "ui/mainwindow.h"
32
33 #include "engine.h"
34
35 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
36 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
37
38 SituareEngine::SituareEngine(QMainWindow *parent)
39     : QObject(parent),
40       m_autoCenteringEnabled(false),
41       m_loggedIn(false)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44     m_ui = new MainWindow;
45     m_ui->showPanels(m_loggedIn);
46
47     // build MapEngine
48     m_mapEngine = new MapEngine(this);
49     m_ui->setMapViewScene(m_mapEngine->scene());
50
51     // build GPS
52     m_gps = new GPSPosition(this);
53     m_gps->setMode(GPSPosition::Default);
54
55     // build SituareService
56     m_situareService = new SituareService(this);
57
58     // build FacebookAuthenticator
59     m_facebookAuthenticator = new FacebookAuthentication(this);
60
61     // connect signals
62     signalsFromMapEngine();
63     signalsFromGPS();
64     signalsFromSituareService();
65     signalsFromMainWindow();
66     signalsFromFacebookAuthenticator();
67
68     connect(this, SIGNAL(userLocationReady(User*)),
69             m_ui, SIGNAL(userLocationReady(User*)));
70
71     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
72             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
73
74     connect(this, SIGNAL(userLocationReady(User*)),
75             m_mapEngine, SLOT(receiveOwnLocation(User*)));
76
77     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
78             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
79
80     // signals connected, now it's time to show the main window
81     // but init the MapEngine before so starting location is set
82     m_mapEngine->init();
83     m_ui->show();
84
85     QSettings settings(DIRECTORY_NAME, FILE_NAME);
86     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
87     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
88
89     // set features on / off based on settings
90     changeAutoCenteringSetting(autoCenteringEnabled.toBool());
91     enableGPS(gpsEnabled.toBool());
92
93     // show messages at startup if features are enabled automatically
94     if (gpsEnabled.toBool())
95         m_ui->showMaemoInformationBox(tr("GPS enabled"));
96     if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
97         m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
98
99     m_facebookAuthenticator->start();
100 }
101
102 SituareEngine::~SituareEngine()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     delete m_ui;
107
108     QSettings settings(DIRECTORY_NAME, FILE_NAME);
109     qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
110     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
111     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
112 }
113
114 void SituareEngine::changeAutoCenteringSetting(bool enabled)
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     m_autoCenteringEnabled = enabled;
119     enableAutoCentering(enabled);
120 }
121
122 void SituareEngine::disableAutoCentering()
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     changeAutoCenteringSetting(false);
127     m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
128 }
129
130 void SituareEngine::enableAutoCentering(bool enabled)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     m_ui->setAutoCenteringButtonEnabled(enabled);
135     m_mapEngine->setAutoCentering(enabled);
136
137     if (enabled)
138         m_gps->requestLastPosition();
139 }
140
141 void SituareEngine::enableGPS(bool enabled)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     m_ui->setGPSButtonEnabled(enabled);
146     m_mapEngine->setGPSEnabled(enabled);
147
148     if (enabled) {
149         m_gps->start();
150         enableAutoCentering(m_autoCenteringEnabled);
151         m_gps->requestLastPosition();
152     }
153     else {
154         m_gps->stop();
155         enableAutoCentering(false);
156     }
157 }
158
159 void SituareEngine::error(const QString &error)
160 {
161     qDebug() << __PRETTY_FUNCTION__;
162     qDebug() << error;
163     // ToDo: signal UI?
164 }
165
166 void SituareEngine::fetchUsernameFromSettings()
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
170 }
171
172 void SituareEngine::invalidCredentials()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
177     //m_facebookAuthenticator->start();
178     m_ui->loginUsingCookies();
179 }
180
181 void SituareEngine::loginActionPressed()
182 {
183     qDebug() << __PRETTY_FUNCTION__;
184
185     if(m_loggedIn) {
186         logout();
187         m_situareService->clearUserData();
188     }
189     else {
190         m_facebookAuthenticator->start();
191     }
192 }
193
194 void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
195 {
196     qDebug() << __PRETTY_FUNCTION__;
197
198     m_loggedIn = true;
199     m_ui->loggedIn(m_loggedIn);
200
201     if(freshLogin) {
202         m_facebookAuthenticator->saveUsername(m_ui->username());
203     }
204     m_ui->show();
205     m_situareService->credentialsReady(credentials);
206     m_situareService->fetchLocations(); // request user locations
207 }
208
209 void SituareEngine::loginProcessCancelled()
210 {
211     qDebug() << __PRETTY_FUNCTION__;
212
213     m_ui->toggleProgressIndicator(false);
214     m_ui->showPanels(m_loggedIn);
215 }
216
217 void SituareEngine::logout()
218 {
219     qDebug() << __PRETTY_FUNCTION__;
220
221     m_loggedIn = false;
222     m_ui->loggedIn(m_loggedIn);
223     m_facebookAuthenticator->clearAccountInformation();
224 }
225
226 void SituareEngine::refreshUserData()
227 {
228     qDebug() << __PRETTY_FUNCTION__;
229
230     m_ui->toggleProgressIndicator(true);
231
232     m_situareService->fetchLocations();
233 }
234
235 void SituareEngine::requestAddress()
236 {
237     qDebug() << __PRETTY_FUNCTION__;
238
239     if (m_gps->isRunning())
240         m_situareService->reverseGeo(m_gps->lastPosition());
241     else
242         m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
243 }
244
245 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     m_ui->toggleProgressIndicator(true);
250
251     if (m_gps->isRunning())
252         m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
253     else
254         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
255 }
256
257 void SituareEngine::signalsFromFacebookAuthenticator()
258 {
259     qDebug() << __PRETTY_FUNCTION__;
260
261     connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
262             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
263
264     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
265             this, SLOT(loginOk(bool, FacebookCredentials)));
266
267     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
268             m_ui, SLOT(startLoginProcess(QUrl)));
269
270     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
271             m_ui, SLOT(loginFailed()));
272
273     connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
274             m_ui, SLOT(saveCookies()));
275
276     connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
277             m_ui, SLOT(loginUsingCookies()));
278 }
279
280 void SituareEngine::signalsFromGPS()
281 {
282     qDebug() << __PRETTY_FUNCTION__;
283
284     connect(m_gps, SIGNAL(position(QPointF,qreal)),
285             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
286
287     connect(m_gps, SIGNAL(timeout()),
288             m_ui, SLOT(gpsTimeout()));
289
290     connect(m_gps, SIGNAL(error(QString)),
291             m_ui, SLOT(gpsError(QString)));
292 }
293
294 void SituareEngine::signalsFromMainWindow()
295 {
296     qDebug() << __PRETTY_FUNCTION__;    
297
298     connect(m_ui, SIGNAL(loginActionPressed()),
299             this, SLOT(loginActionPressed()));
300
301     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
302             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
303
304     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
305             this, SLOT(fetchUsernameFromSettings()));
306
307     // signals from map view
308     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
309             m_mapEngine, SLOT(setLocation(QPoint)));
310
311     connect(m_ui, SIGNAL(mapViewResized(QSize)),
312             m_mapEngine, SLOT(viewResized(QSize)));
313
314     connect(m_ui, SIGNAL(viewZoomFinished()),
315             m_mapEngine, SLOT(viewZoomFinished()));
316
317     // signals from zoom buttons (zoom panel and volume buttons)
318     connect(m_ui, SIGNAL(zoomIn()),
319             m_mapEngine, SLOT(zoomIn()));
320
321     connect(m_ui, SIGNAL(zoomOut()),
322             m_mapEngine, SLOT(zoomOut()));
323
324     // signals from menu buttons
325     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
326             this, SLOT(changeAutoCenteringSetting(bool)));
327
328     connect(m_ui, SIGNAL(gpsTriggered(bool)),
329             this, SLOT(enableGPS(bool)));
330
331     //signals from dialogs
332     connect(m_ui, SIGNAL(cancelLoginProcess()),
333             this, SLOT(loginProcessCancelled()));
334
335     connect(m_ui, SIGNAL(requestReverseGeo()),
336             this, SLOT(requestAddress()));
337
338     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
339             this, SLOT(requestUpdateLocation(QString,bool)));
340
341     // signals from user info tab
342     connect(m_ui, SIGNAL(refreshUserData()),
343             this, SLOT(refreshUserData()));
344
345     // signals from friend list tab
346     connect(m_ui, SIGNAL(findFriend(QPointF)),
347             m_mapEngine, SLOT(setViewLocation(QPointF)));
348 }
349
350 void SituareEngine::signalsFromMapEngine()
351 {
352     qDebug() << __PRETTY_FUNCTION__;
353
354     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
355             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
356
357     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
358             m_ui, SIGNAL(zoomLevelChanged(int)));
359
360     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
361             this, SLOT(disableAutoCentering()));
362
363     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
364             m_ui, SIGNAL(maxZoomLevelReached()));
365
366     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
367             m_ui, SIGNAL(minZoomLevelReached()));
368
369     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
370             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
371 }
372
373 void SituareEngine::signalsFromSituareService()
374 {
375     qDebug() << __PRETTY_FUNCTION__;
376
377     connect(m_situareService, SIGNAL(error(QString)),
378             this, SLOT(error(QString)));
379
380     connect(m_situareService, SIGNAL(invalidSessionCredentials()),
381             this, SLOT(invalidCredentials()));
382
383     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
384             m_ui, SIGNAL(reverseGeoReady(QString)));
385
386     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
387             this, SLOT(userDataChanged(User*, QList<User*>&)));
388
389     connect(m_situareService, SIGNAL(updateWasSuccessful()),
390             this, SLOT(updateWasSuccessful()));
391 }
392
393 void SituareEngine::updateWasSuccessful()
394 {
395     qDebug() << __PRETTY_FUNCTION__;
396
397     m_situareService->fetchLocations();
398 }
399
400 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
401 {
402     qDebug() << __PRETTY_FUNCTION__;
403
404     m_ui->toggleProgressIndicator(false);
405
406     emit userLocationReady(user);
407     emit friendsLocationsReady(friendsList);
408 }