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