Merge branch 'master' into network_handler
[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     initializeGpsAndAutocentering();
81
82     // signals connected, now it's time to show the main window
83     // but init the MapEngine before so starting location is set
84     m_mapEngine->init();
85     m_ui->show();
86
87     m_facebookAuthenticator->start();
88 }
89
90 SituareEngine::~SituareEngine()
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     delete m_ui;
95
96     QSettings settings(DIRECTORY_NAME, FILE_NAME);
97     qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
98     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
99     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
100 }
101
102 void SituareEngine::changeAutoCenteringSetting(bool enabled)
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     m_autoCenteringEnabled = enabled;
107     enableAutoCentering(enabled);
108 }
109
110
111 void SituareEngine::disableAutoCentering()
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     changeAutoCenteringSetting(false);
116     m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
117 }
118
119 void SituareEngine::enableAutoCentering(bool enabled)
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     m_ui->setAutoCenteringButtonEnabled(enabled);
124     m_mapEngine->setAutoCentering(enabled);
125
126     if (enabled)
127         m_gps->requestLastPosition();
128 }
129
130 void SituareEngine::enableGPS(bool enabled)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     m_ui->setGPSButtonEnabled(enabled);
135     m_mapEngine->setGPSEnabled(enabled);
136
137     if (enabled) {
138         m_gps->start();
139         enableAutoCentering(m_autoCenteringEnabled);
140         m_gps->requestLastPosition();
141     }
142     else {
143         m_gps->stop();
144         enableAutoCentering(false);
145     }
146 }
147
148 void SituareEngine::error(const QString &error)
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151     qDebug() << error;
152     // ToDo: signal UI?
153 }
154
155 void SituareEngine::fetchUsernameFromSettings()
156 {
157     qDebug() << __PRETTY_FUNCTION__;
158
159     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
160 }
161
162 void SituareEngine::initializeGpsAndAutocentering()
163 {
164     qDebug() << __PRETTY_FUNCTION__;
165
166     QSettings settings(DIRECTORY_NAME, FILE_NAME);
167     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
168     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);     
169
170     if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
171
172         connect(m_gps, SIGNAL(position(QPointF,qreal)),
173                 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
174
175         changeAutoCenteringSetting(true);
176         enableGPS(true);
177
178         m_ui->showMaemoInformationBox(tr("GPS enabled"));
179         m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
180     } else { // Normal start
181         changeAutoCenteringSetting(autoCenteringEnabled.toBool());
182         enableGPS(gpsEnabled.toBool());
183
184         if (gpsEnabled.toBool())
185             m_ui->showMaemoInformationBox(tr("GPS enabled"));
186
187         if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
188             m_ui->showMaemoInformationBox(tr("Auto centering enabled"));        
189     } 
190 }
191
192 void SituareEngine::invalidCredentials()
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
197     m_facebookAuthenticator->start();
198 }
199
200 void SituareEngine::loginActionPressed()
201 {
202     qDebug() << __PRETTY_FUNCTION__;
203
204     if(m_loggedIn) {
205         logout();
206         m_situareService->clearUserData();
207     }
208     else {
209         m_facebookAuthenticator->start();
210     }
211 }
212
213 void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
214 {
215     qDebug() << __PRETTY_FUNCTION__;
216
217     m_loggedIn = true;
218     m_ui->loggedIn(m_loggedIn);
219
220     if(freshLogin) {
221         m_facebookAuthenticator->saveUsername(m_ui->username());
222     }
223     m_ui->show();
224     m_situareService->credentialsReady(credentials);
225     m_situareService->fetchLocations(); // request user locations
226 }
227
228 void SituareEngine::loginProcessCancelled()
229 {
230     qDebug() << __PRETTY_FUNCTION__;
231
232     m_ui->toggleProgressIndicator(false);
233     m_ui->showPanels(m_loggedIn);
234 }
235
236 void SituareEngine::logout()
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     m_loggedIn = false;
241     m_ui->loggedIn(m_loggedIn);
242     m_facebookAuthenticator->clearAccountInformation();
243 }
244
245 void SituareEngine::refreshUserData()
246 {
247     qDebug() << __PRETTY_FUNCTION__;
248
249     m_ui->toggleProgressIndicator(true);
250
251     m_situareService->fetchLocations();
252 }
253
254 void SituareEngine::requestAddress()
255 {
256     qDebug() << __PRETTY_FUNCTION__;
257
258     if (m_gps->isRunning())
259         m_situareService->reverseGeo(m_gps->lastPosition());
260     else
261         m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
262 }
263
264 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
265 {
266     qDebug() << __PRETTY_FUNCTION__;
267
268     m_ui->toggleProgressIndicator(true);
269
270     if (m_gps->isRunning())
271         m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
272     else
273         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
274 }
275
276 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
277 {
278     qDebug() << __PRETTY_FUNCTION__;
279
280     Q_UNUSED(latLonCoordinate);
281     Q_UNUSED(accuracy);
282
283     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled        
284         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
285
286     disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
287                this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
288 }
289
290 void SituareEngine::signalsFromFacebookAuthenticator()
291 {
292     qDebug() << __PRETTY_FUNCTION__;
293
294     connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
295             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
296
297     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
298             this, SLOT(loginOk(bool, FacebookCredentials)));
299
300     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
301             m_ui, SLOT(startLoginProcess(QUrl)));
302
303     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
304             m_ui, SLOT(loginFailed()));
305 }
306
307 void SituareEngine::signalsFromGPS()
308 {
309     qDebug() << __PRETTY_FUNCTION__;
310
311     connect(m_gps, SIGNAL(position(QPointF,qreal)),
312             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
313
314     connect(m_gps, SIGNAL(timeout()),
315             m_ui, SLOT(gpsTimeout()));
316
317     connect(m_gps, SIGNAL(error(QString)),
318             m_ui, SLOT(gpsError(QString)));
319 }
320
321 void SituareEngine::signalsFromMainWindow()
322 {
323     qDebug() << __PRETTY_FUNCTION__;    
324
325     connect(m_ui, SIGNAL(loginActionPressed()),
326             this, SLOT(loginActionPressed()));
327
328     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
329             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
330
331     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
332             this, SLOT(fetchUsernameFromSettings()));
333
334     // signals from map view
335     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
336             m_mapEngine, SLOT(setLocation(QPoint)));
337
338     connect(m_ui, SIGNAL(mapViewResized(QSize)),
339             m_mapEngine, SLOT(viewResized(QSize)));
340
341     connect(m_ui, SIGNAL(viewZoomFinished()),
342             m_mapEngine, SLOT(viewZoomFinished()));
343
344     // signals from zoom buttons (zoom panel and volume buttons)
345     connect(m_ui, SIGNAL(zoomIn()),
346             m_mapEngine, SLOT(zoomIn()));
347
348     connect(m_ui, SIGNAL(zoomOut()),
349             m_mapEngine, SLOT(zoomOut()));
350
351     // signals from menu buttons
352     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
353             this, SLOT(changeAutoCenteringSetting(bool)));
354
355     connect(m_ui, SIGNAL(gpsTriggered(bool)),
356             this, SLOT(enableGPS(bool)));
357
358     //signals from dialogs
359     connect(m_ui, SIGNAL(cancelLoginProcess()),
360             this, SLOT(loginProcessCancelled()));
361
362     connect(m_ui, SIGNAL(requestReverseGeo()),
363             this, SLOT(requestAddress()));
364
365     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
366             this, SLOT(requestUpdateLocation(QString,bool)));
367
368     // signals from user info tab
369     connect(m_ui, SIGNAL(refreshUserData()),
370             this, SLOT(refreshUserData()));
371
372     connect(m_ui, SIGNAL(findUser(QPointF)),
373             m_mapEngine, SLOT(setViewLocation(QPointF)));
374
375     // signals from friend list tab
376     connect(m_ui, SIGNAL(findFriend(QPointF)),
377             m_mapEngine, SLOT(setViewLocation(QPointF)));
378 }
379
380 void SituareEngine::signalsFromMapEngine()
381 {
382     qDebug() << __PRETTY_FUNCTION__;
383
384     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
385             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
386
387     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
388             m_ui, SIGNAL(zoomLevelChanged(int)));
389
390     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
391             this, SLOT(disableAutoCentering()));
392
393     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
394             m_ui, SIGNAL(maxZoomLevelReached()));
395
396     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
397             m_ui, SIGNAL(minZoomLevelReached()));
398
399     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
400             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
401 }
402
403 void SituareEngine::signalsFromSituareService()
404 {
405     qDebug() << __PRETTY_FUNCTION__;
406
407     connect(m_situareService, SIGNAL(error(QString)),
408             this, SLOT(error(QString)));
409
410     connect(m_situareService, SIGNAL(invalidSessionCredentials()),
411             this, SLOT(invalidCredentials()));
412
413     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
414             m_ui, SIGNAL(reverseGeoReady(QString)));
415
416     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
417             this, SLOT(userDataChanged(User*, QList<User*>&)));
418
419     connect(m_situareService, SIGNAL(updateWasSuccessful()),
420             this, SLOT(updateWasSuccessful()));
421 }
422
423 void SituareEngine::updateWasSuccessful()
424 {
425     qDebug() << __PRETTY_FUNCTION__;
426
427     m_situareService->fetchLocations();
428 }
429
430 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
431 {
432     qDebug() << __PRETTY_FUNCTION__;
433
434     m_ui->toggleProgressIndicator(false);
435
436     emit userLocationReady(user);
437     emit friendsLocationsReady(friendsList);
438 }