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