53b9e87e9277c6cc53ae8c146323dfc8a9565fb0
[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 #include <QMessageBox>
26 #include <QNetworkReply>
27
28 #include "common.h"
29 #include "facebookservice/facebookauthentication.h"
30 #include "gps/gpsposition.h"
31 #include "map/mapengine.h"
32 #include "situareservice/situareservice.h"
33 #include "ui/mainwindow.h"
34 #include <cmath>
35
36 #include "engine.h"
37
38 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
39 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
40 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12;  ///< Default zoom level when GPS available
41 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
42 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
43 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
44
45 SituareEngine::SituareEngine(QMainWindow *parent)
46     : QObject(parent),
47       m_autoCenteringEnabled(false),
48       m_automaticUpdateFirstStart(true),
49       m_loggedIn(false),
50       m_userMoved(false),
51       m_automaticUpdateIntervalTimer(0),
52       m_lastUpdatedGPSPosition(QPointF())
53 {    
54     qDebug() << __PRETTY_FUNCTION__;
55     m_ui = new MainWindow;
56     m_ui->updateItemVisibility(m_loggedIn);
57
58     // build MapEngine
59     m_mapEngine = new MapEngine(this);
60     m_ui->setMapViewScene(m_mapEngine->scene());
61
62     // build GPS
63     m_gps = new GPSPosition(this);
64
65     // build SituareService
66     m_situareService = new SituareService(this);
67
68     // build FacebookAuthenticator
69     m_facebookAuthenticator = new FacebookAuthentication(this);
70
71     // connect signals
72     signalsFromMapEngine();
73     signalsFromGPS();
74     signalsFromSituareService();
75     signalsFromMainWindow();
76     signalsFromFacebookAuthenticator();
77
78     connect(this, SIGNAL(userLocationReady(User*)),
79             m_ui, SIGNAL(userLocationReady(User*)));
80
81     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
82             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
83
84     connect(this, SIGNAL(userLocationReady(User*)),
85             m_mapEngine, SLOT(receiveOwnLocation(User*)));
86
87     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
88             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
89
90     m_automaticUpdateIntervalTimer = new QTimer(this);
91     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
92             this, SLOT(automaticUpdateIntervalTimerTimeout()));
93
94     // signals connected, now it's time to show the main window
95     // but init the MapEngine before so starting location is set
96     m_mapEngine->init();
97     m_ui->show();
98
99     m_facebookAuthenticator->start();
100
101     m_gps->setMode(GPSPosition::Default);
102     initializeGpsAndAutocentering();
103 }
104
105 SituareEngine::~SituareEngine()
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     delete m_ui;
110
111     QSettings settings(DIRECTORY_NAME, FILE_NAME);
112     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
113     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
114 }
115
116 void SituareEngine::automaticUpdateIntervalTimerTimeout()
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     if (m_gps->isRunning() && m_userMoved) {
121         requestUpdateLocation();
122         m_userMoved = false;
123     }
124 }
125
126 void SituareEngine::changeAutoCenteringSetting(bool enabled)
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     m_autoCenteringEnabled = enabled;
131     enableAutoCentering(enabled);
132 }
133
134 void SituareEngine::disableAutoCentering()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     changeAutoCenteringSetting(false);
139     m_ui->buildInformationBox(tr("Auto centering disabled"));
140 }
141
142 void SituareEngine::enableAutoCentering(bool enabled)
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     m_ui->setAutoCenteringButtonEnabled(enabled);
147     m_mapEngine->setAutoCentering(enabled);
148
149     if (enabled)
150         m_gps->requestLastPosition();
151 }
152
153 void SituareEngine::enableGPS(bool enabled)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_ui->setOwnLocationCrosshairVisibility(!enabled);
158
159     if (m_gps->isInitialized()) {
160         m_ui->setGPSButtonEnabled(enabled);
161         m_mapEngine->setGPSEnabled(enabled);
162
163         if (enabled && !m_gps->isRunning()) {
164             m_gps->start();
165             enableAutoCentering(m_autoCenteringEnabled);
166             m_gps->requestLastPosition();
167
168             if (m_loggedIn)
169                 m_ui->readAutomaticLocationUpdateSettings();
170         }
171         else if (!enabled && m_gps->isRunning()) {
172             m_gps->stop();
173             enableAutoCentering(false);
174             enableAutomaticLocationUpdate(false);
175         }
176     }
177     else {
178         if (enabled)
179             m_ui->buildInformationBox(tr("Unable to start GPS"));
180         m_ui->setGPSButtonEnabled(false);
181         m_mapEngine->setGPSEnabled(false);
182     }
183 }
184
185 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     //Show automatic update confirmation dialog
190     if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
191         m_ui->showEnableAutomaticUpdateLocationDialog(
192                 tr("Do you want to enable automatic location update with %1 min update interval?")
193                 .arg(updateIntervalMsecs/1000/60));
194         m_automaticUpdateFirstStart = false;
195     } else {
196         if (enabled && m_gps->isRunning()) {
197             m_ui->buildInformationBox(tr("Automatic location update enabled"));
198             if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
199                 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
200             else
201                 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
202
203             m_automaticUpdateIntervalTimer->start();
204
205         } else {
206             m_automaticUpdateIntervalTimer->stop();
207         }
208     }
209 }
210
211 void SituareEngine::error(const int error)
212 {
213     qDebug() << __PRETTY_FUNCTION__;    
214
215     switch(error)
216     {
217     case QNetworkReply::ConnectionRefusedError:
218         m_ui->buildInformationBox(tr("Connection refused by the server"), true);
219         break;
220     case QNetworkReply::RemoteHostClosedError:
221         m_ui->buildInformationBox(tr("Connection closed by the server"), true);
222         break;
223     case QNetworkReply::HostNotFoundError:
224         m_ui->buildInformationBox(tr("Remote server not found"), true);
225         break;
226     case QNetworkReply::TimeoutError:
227         m_ui->buildInformationBox(tr("Connection timed out"), true);
228         break;
229     case SituareError::SESSION_EXPIRED:
230         m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
231         m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
232         m_loggedIn = false;
233         m_situareService->clearUserData();
234         m_ui->loggedIn(false);
235         m_ui->loginFailed();
236         break;
237     case SituareError::LOGIN_FAILED:
238         m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
239         break;
240     case SituareError::UPDATE_FAILED:
241         m_ui->buildInformationBox(tr("Update failed, please try again"), true);
242         break;
243     case SituareError::DATA_RETRIEVAL_FAILED:
244         m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
245         break;
246     case SituareError::ADDRESS_RETRIEVAL_FAILED:
247         m_ui->buildInformationBox(tr("Address retrieval failed"), true);
248         break;
249     case SituareError::IMAGE_DOWNLOAD_FAILED:
250         m_ui->buildInformationBox(tr("Image download failed"), true);
251         break;
252     case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
253         m_ui->buildInformationBox(tr("Map image download failed"), true);
254         break;
255     case SituareError::GPS_INITIALIZATION_FAILED:
256         enableGPS(false);
257         m_ui->buildInformationBox(tr("GPS initialization failed"), true);
258         break;
259     case SituareError::UNKNOWN_REPLY:
260         m_ui->buildInformationBox(tr("Unknown server response"), true);
261         break;
262     case SituareError::INVALID_JSON:
263         m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
264         m_loggedIn = false;
265         m_ui->loggedIn(false);
266         m_facebookAuthenticator->clearAccountInformation(false); // clean all
267         break;
268     default:
269         qCritical() << "QNetworkReply::NetworkError :" << error;
270         break;
271     }
272 }
273
274 void SituareEngine::fetchUsernameFromSettings()
275 {
276     qDebug() << __PRETTY_FUNCTION__;
277
278     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
279 }
280
281 void SituareEngine::initializeGpsAndAutocentering()
282 {
283     qDebug() << __PRETTY_FUNCTION__;
284
285     QSettings settings(DIRECTORY_NAME, FILE_NAME);
286     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
287     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
288
289     if (m_gps->isInitialized()) {
290
291         if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
292
293             connect(m_gps, SIGNAL(position(QPointF,qreal)),
294                     this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
295
296             changeAutoCenteringSetting(true);
297             enableGPS(true);
298
299             m_ui->buildInformationBox(tr("GPS enabled"));
300             m_ui->buildInformationBox(tr("Auto centering enabled"));
301
302         } else { // Normal start
303             changeAutoCenteringSetting(autoCenteringEnabled.toBool());
304             enableGPS(gpsEnabled.toBool());
305
306             if (gpsEnabled.toBool())
307                 m_ui->buildInformationBox(tr("GPS enabled"));
308
309             if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
310                 m_ui->buildInformationBox(tr("Auto centering enabled"));
311         }
312     } else {
313         enableGPS(false);
314     }
315 }
316
317 bool SituareEngine::isUserMoved()
318 {
319     qDebug() << __PRETTY_FUNCTION__;
320
321     return m_userMoved;
322 }
323
324 void SituareEngine::loginActionPressed()
325 {
326     qDebug() << __PRETTY_FUNCTION__;
327
328     if(m_loggedIn) {
329         logout();
330         m_situareService->clearUserData();
331     }
332     else {
333         m_facebookAuthenticator->start();
334     }
335 }
336
337 void SituareEngine::loginOk()
338 {
339     qDebug() << __PRETTY_FUNCTION__;
340
341     m_loggedIn = true;
342     m_ui->loggedIn(m_loggedIn);
343
344     m_ui->show();
345     m_situareService->fetchLocations(); // request user locations
346
347     if (m_gps->isRunning())
348         m_ui->readAutomaticLocationUpdateSettings();
349 }
350
351 void SituareEngine::loginProcessCancelled()
352 {
353     qDebug() << __PRETTY_FUNCTION__;
354
355     m_ui->toggleProgressIndicator(false);
356     m_ui->updateItemVisibility(m_loggedIn);
357 }
358
359 void SituareEngine::logout()
360 {
361     qDebug() << __PRETTY_FUNCTION__;
362
363     m_loggedIn = false;
364     m_ui->loggedIn(m_loggedIn);
365
366     // signal to clear locationUpdateDialog's data
367     connect(this, SIGNAL(clearUpdateLocationDialogData()),
368             m_ui, SIGNAL(clearUpdateLocationDialogData()));
369     emit clearUpdateLocationDialogData();
370
371     m_facebookAuthenticator->clearAccountInformation(); // clear all
372     m_automaticUpdateFirstStart = true;
373 }
374
375 void SituareEngine::refreshUserData()
376 {
377     qDebug() << __PRETTY_FUNCTION__;
378
379     m_ui->toggleProgressIndicator(true);
380
381     m_situareService->fetchLocations();
382 }
383
384 void SituareEngine::requestAddress()
385 {
386     qDebug() << __PRETTY_FUNCTION__;
387
388     if (m_gps->isRunning())
389         m_situareService->reverseGeo(m_gps->lastPosition());
390     else
391         m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
392 }
393
394 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
395 {
396     qDebug() << __PRETTY_FUNCTION__;
397
398     m_ui->toggleProgressIndicator(true);
399
400     if (m_gps->isRunning())
401         m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
402     else
403         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
404 }
405
406 void SituareEngine::saveGPSPosition(QPointF position)
407 {
408     qDebug() << __PRETTY_FUNCTION__;
409
410     if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
411          USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
412         (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
413          USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
414
415         m_lastUpdatedGPSPosition = position;
416         m_userMoved = true;
417     }
418 }
419
420 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
421 {
422     qDebug() << __PRETTY_FUNCTION__;
423
424     Q_UNUSED(latLonCoordinate);
425     Q_UNUSED(accuracy);
426
427     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled        
428         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
429
430     disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
431                this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
432 }
433
434 void SituareEngine::signalsFromFacebookAuthenticator()
435 {
436     qDebug() << __PRETTY_FUNCTION__;
437
438     connect(m_facebookAuthenticator, SIGNAL(error(int)),
439             this, SLOT(error(int)));
440
441     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
442             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
443
444     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
445             this, SLOT(loginOk()));
446
447     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
448             m_ui, SLOT(startLoginProcess()));
449
450     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
451             m_ui, SLOT(loginFailed()));
452
453     connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
454             m_ui, SLOT(saveCookies()));
455
456     connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
457             m_ui, SLOT(loginUsingCookies()));
458 }
459
460 void SituareEngine::signalsFromGPS()
461 {
462     qDebug() << __PRETTY_FUNCTION__;
463
464     connect(m_gps, SIGNAL(position(QPointF,qreal)),
465             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
466
467     connect(m_gps, SIGNAL(timeout()),
468             m_ui, SLOT(gpsTimeout()));
469
470     connect(m_gps, SIGNAL(error(int)),
471             this, SLOT(error(int)));
472
473     connect(m_gps, SIGNAL(position(QPointF,qreal)),
474             this, SLOT(saveGPSPosition(QPointF)));
475 }
476
477 void SituareEngine::signalsFromMainWindow()
478 {
479     qDebug() << __PRETTY_FUNCTION__;    
480
481     connect(m_ui, SIGNAL(error(int)),
482             this, SLOT(error(int)));
483
484     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
485             this, SLOT(fetchUsernameFromSettings()));
486
487     connect(m_ui, SIGNAL(loginActionPressed()),
488             this, SLOT(loginActionPressed()));
489
490     connect(m_ui, SIGNAL(saveUsername(QString)),
491             m_facebookAuthenticator, SLOT(saveUsername(QString)));
492
493     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
494             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
495
496     // signals from map view
497     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
498             m_mapEngine, SLOT(setLocation(QPoint)));
499
500     connect(m_ui, SIGNAL(mapViewResized(QSize)),
501             m_mapEngine, SLOT(viewResized(QSize)));
502
503     connect(m_ui, SIGNAL(viewZoomFinished()),
504             m_mapEngine, SLOT(viewZoomFinished()));
505
506     // signals from zoom buttons (zoom panel and volume buttons)
507     connect(m_ui, SIGNAL(zoomIn()),
508             m_mapEngine, SLOT(zoomIn()));
509
510     connect(m_ui, SIGNAL(zoomOut()),
511             m_mapEngine, SLOT(zoomOut()));
512
513     // signals from menu buttons
514     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
515             this, SLOT(changeAutoCenteringSetting(bool)));
516
517     connect(m_ui, SIGNAL(gpsTriggered(bool)),
518             this, SLOT(enableGPS(bool)));
519
520     //signals from dialogs
521     connect(m_ui, SIGNAL(cancelLoginProcess()),
522             this, SLOT(loginProcessCancelled()));
523
524     connect(m_ui, SIGNAL(requestReverseGeo()),
525             this, SLOT(requestAddress()));
526
527     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
528             this, SLOT(requestUpdateLocation(QString,bool)));
529
530     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
531             this, SLOT(enableAutomaticLocationUpdate(bool, int)));    
532
533     // signals from user info tab
534     connect(m_ui, SIGNAL(refreshUserData()),
535             this, SLOT(refreshUserData()));
536
537     connect(m_ui, SIGNAL(findUser(QPointF)),
538             m_mapEngine, SLOT(setViewLocation(QPointF)));
539
540     // signals from friend list tab
541     connect(m_ui, SIGNAL(findFriend(QPointF)),
542             m_mapEngine, SLOT(setViewLocation(QPointF)));
543 }
544
545 void SituareEngine::signalsFromMapEngine()
546 {
547     qDebug() << __PRETTY_FUNCTION__;
548
549     connect(m_mapEngine, SIGNAL(error(int)),
550             this, SLOT(error(int)));
551
552     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
553             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
554
555     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
556             m_ui, SIGNAL(zoomLevelChanged(int)));
557
558     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
559             this, SLOT(disableAutoCentering()));
560
561     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
562             m_ui, SIGNAL(maxZoomLevelReached()));
563
564     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
565             m_ui, SIGNAL(minZoomLevelReached()));
566
567     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
568             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
569
570     connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
571             m_ui, SIGNAL(newMapResolution(qreal)));
572 }
573
574 void SituareEngine::signalsFromSituareService()
575 {
576     qDebug() << __PRETTY_FUNCTION__;
577
578     connect(m_situareService, SIGNAL(error(int)),
579             this, SLOT(error(int)));
580
581     connect(m_situareService, SIGNAL(error(int)),
582             m_ui, SIGNAL(messageSendingFailed(int)));
583
584     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
585             m_ui, SIGNAL(reverseGeoReady(QString)));
586
587     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
588             this, SLOT(userDataChanged(User*, QList<User*>&)));
589
590     connect(m_situareService, SIGNAL(updateWasSuccessful()),
591             this, SLOT(updateWasSuccessful()));
592
593     connect(m_situareService, SIGNAL(updateWasSuccessful()),
594             m_ui, SIGNAL(clearUpdateLocationDialogData()));
595 }
596
597 void SituareEngine::updateWasSuccessful()
598 {
599     qDebug() << __PRETTY_FUNCTION__;
600
601     m_situareService->fetchLocations();
602 }
603
604 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
605 {
606     qDebug() << __PRETTY_FUNCTION__;
607
608     m_ui->toggleProgressIndicator(false);
609
610     emit userLocationReady(user);
611     emit friendsLocationsReady(friendsList);
612 }