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