Error message is shown and progress indicator is stopped when routing fails
[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 <cmath>
26
27 #include <QMessageBox>
28 #include <QNetworkReply>
29
30 #include "application.h"
31 #include "common.h"
32 #include "../error.h"
33 #include "facebookservice/facebookauthentication.h"
34 #include "gps/gpsposition.h"
35 #include "map/mapengine.h"
36 #include "routing/geocodingservice.h"
37 #include "routing/routingservice.h"
38 #include "mce.h"
39 #include "network/networkaccessmanager.h"
40 #include "situareservice/situareservice.h"
41 #include "ui/mainwindow.h"
42
43 #include "engine.h"
44
45 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
46 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
47 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12;  ///< Default zoom level when GPS available
48 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
49 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
50 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
51
52 SituareEngine::SituareEngine()
53     : m_autoCenteringEnabled(false),
54       m_automaticUpdateFirstStart(true),
55       m_automaticUpdateRequest(false),
56       m_userMoved(false),
57       m_automaticUpdateIntervalTimer(0),
58       m_lastUpdatedGPSPosition(GeoCoordinate())
59 {
60     qDebug() << __PRETTY_FUNCTION__;
61
62     m_ui = new MainWindow;
63     m_ui->updateItemVisibility();
64
65     Application *application = static_cast<Application *>(qApp);
66     application->registerWindow(m_ui->winId());
67
68     connect(application, SIGNAL(topmostWindowChanged(bool)),
69             this, SLOT(topmostWindowChanged(bool)));
70
71     m_networkAccessManager = new NetworkAccessManager(this);
72
73     // build MapEngine
74     m_mapEngine = new MapEngine(this);
75     m_ui->setMapViewScene(m_mapEngine->scene());
76
77     // build GPS
78     m_gps = new GPSPosition(this);
79
80     // build SituareService
81     m_situareService = new SituareService(this);
82
83     // build FacebookAuthenticator
84     m_facebookAuthenticator = new FacebookAuthentication(this);
85
86     // build routing service
87     m_routingService = new RoutingService(this); // create this when needed, not in constructor!
88
89     // build geocoding service
90     m_geocodingService = new GeocodingService(this);
91
92     // connect signals
93     signalsFromMapEngine();
94     signalsFromGeocodingService();
95     signalsFromGPS();
96     signalsFromRoutingService();
97     signalsFromSituareService();
98     signalsFromMainWindow();
99     signalsFromFacebookAuthenticator();
100
101     connect(this, SIGNAL(userLocationReady(User*)),
102             m_ui, SIGNAL(userLocationReady(User*)));
103
104     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
105             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
106
107     connect(this, SIGNAL(userLocationReady(User*)),
108             m_mapEngine, SLOT(receiveOwnLocation(User*)));
109
110     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
111             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
112
113     connect(this, SIGNAL(friendImageReady(User*)),
114             m_ui, SIGNAL(friendImageReady(User*)));
115
116     connect(this, SIGNAL(friendImageReady(User*)),
117             m_mapEngine, SIGNAL(friendImageReady(User*)));
118
119     m_automaticUpdateIntervalTimer = new QTimer(this);
120     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
121             this, SLOT(startAutomaticUpdate()));
122
123     // signals connected, now it's time to show the main window
124     // but init the MapEngine before so starting location is set
125     m_mapEngine->init();
126     m_ui->show();
127
128     m_facebookAuthenticator->start();
129
130     m_gps->setMode(GPSPosition::Default);
131     initializeGpsAndAutocentering();
132
133     m_mce = new MCE(this);
134     connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(setPowerSaving(bool)));
135 }
136
137 SituareEngine::~SituareEngine()
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     delete m_ui;
142
143     QSettings settings(DIRECTORY_NAME, FILE_NAME);
144     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
145     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
146 }
147
148 void SituareEngine::changeAutoCenteringSetting(bool enabled)
149 {
150     qDebug() << __PRETTY_FUNCTION__ << enabled;
151
152     m_autoCenteringEnabled = enabled;
153     setAutoCentering(enabled);
154 }
155
156 void SituareEngine::disableAutoCentering()
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     changeAutoCenteringSetting(false);
161 }
162
163 void SituareEngine::draggingModeTriggered()
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     if (m_mce)
168         m_mce->vibrationFeedback();
169 }
170
171 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
172 {
173     qDebug() << __PRETTY_FUNCTION__;
174
175     //Show automatic update confirmation dialog
176     if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
177         m_ui->showEnableAutomaticUpdateLocationDialog(
178                 tr("Do you want to enable automatic location update with %1 min update interval?")
179                 .arg(updateIntervalMsecs/1000/60));
180         m_automaticUpdateFirstStart = false;
181     } else {
182         if (enabled && m_gps->isRunning()) {
183             m_ui->buildInformationBox(tr("Automatic location update enabled"));
184             if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
185                 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
186             else
187                 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
188
189             connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
190                     this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
191
192             m_automaticUpdateIntervalTimer->start();
193
194         } else {
195             disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
196                     this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
197
198             m_automaticUpdateIntervalTimer->stop();
199         }
200     }
201 }
202
203 void SituareEngine::error(const int context, const int error)
204 {
205     qDebug() << __PRETTY_FUNCTION__;
206
207     switch(error)
208     {
209     case SituareError::ERROR_GENERAL:
210         if(context == ErrorContext::SITUARE) {
211             m_ui->toggleProgressIndicator(false);
212             m_ui->buildInformationBox(tr("Unknown server error"), true);
213         }
214         break;
215     case 1: //errors: SituareError::ERROR_MISSING_ARGUMENT and QNetworkReply::ConnectionRefusedError
216         m_ui->toggleProgressIndicator(false);
217         if(context == ErrorContext::SITUARE) {
218             m_ui->buildInformationBox(tr("Missing parameter from request"), true);
219         } else if(context == ErrorContext::NETWORK) {
220             m_ui->buildInformationBox(tr("Connection refused by the server"), true);
221         }
222         break;
223     case QNetworkReply::RemoteHostClosedError:
224         if(context == ErrorContext::NETWORK) {
225             m_ui->toggleProgressIndicator(false);
226             m_ui->buildInformationBox(tr("Connection closed by the server"), true);
227         }
228         break;
229     case QNetworkReply::HostNotFoundError:
230         if(context == ErrorContext::NETWORK) {
231             m_ui->toggleProgressIndicator(false);
232             m_ui->buildInformationBox(tr("Remote server not found"), true);
233         }
234         break;
235     case QNetworkReply::TimeoutError:
236         if(context == ErrorContext::NETWORK) {
237             m_ui->toggleProgressIndicator(false);
238             m_ui->buildInformationBox(tr("Connection timed out"), true);
239         }
240         break;
241     case QNetworkReply::UnknownNetworkError:
242         if(context == ErrorContext::NETWORK) {
243             m_ui->toggleProgressIndicator(false);
244             m_ui->buildInformationBox(tr("No network connection"), true);
245         }
246         break;
247     case SituareError::SESSION_EXPIRED:
248         m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
249         m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
250         m_situareService->clearUserData();
251         m_ui->loggedIn(false);
252         m_ui->loginFailed();
253         break;
254     case SituareError::LOGIN_FAILED:
255         m_ui->toggleProgressIndicator(false);
256         m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
257         m_ui->loginFailed();
258         break;
259     case SituareError::UPDATE_FAILED:
260         m_ui->toggleProgressIndicator(false);
261         m_ui->buildInformationBox(tr("Update failed, please try again"), true);
262         break;
263     case SituareError::DATA_RETRIEVAL_FAILED:
264         m_ui->toggleProgressIndicator(false);
265         m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
266         break;
267     case SituareError::ADDRESS_RETRIEVAL_FAILED:
268     case SituareError::ERROR_GEOLOCATION_REQUEST_FAIL:
269     case SituareError::ERROR_GEOLOCATION_LONLAT_INVALID:
270         m_ui->toggleProgressIndicator(false);
271         m_ui->buildInformationBox(tr("Address retrieval failed"), true);
272         break;
273     case SituareError::IMAGE_DOWNLOAD_FAILED:
274         m_ui->buildInformationBox(tr("Image download failed"), true);
275         break;
276     case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
277         m_ui->buildInformationBox(tr("Map image download failed"), true);
278         break;
279     case SituareError::GPS_INITIALIZATION_FAILED:
280         setGPS(false);
281         m_ui->buildInformationBox(tr("GPS initialization failed"), true);
282         break;
283     case SituareError::INVALID_JSON:
284         m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
285         m_ui->loggedIn(false);
286         m_facebookAuthenticator->clearAccountInformation(false); // clean all
287         break;
288     case SituareError::ERROR_GEOLOCATION_SERVER_UNAVAILABLE:
289         m_ui->toggleProgressIndicator(false);
290         m_ui->buildInformationBox(tr("Address server not responding"), true);
291         break;
292     case SituareError::ERROR_ROUTING_FAILED:
293         m_ui->toggleProgressIndicator(false);
294         m_ui->buildInformationBox(tr("Routing failed"), true);
295         break;
296     default:
297         m_ui->toggleProgressIndicator(false);
298         if(context == ErrorContext::NETWORK)
299             qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
300         else
301             qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
302         break;
303     }
304 }
305
306 void SituareEngine::fetchUsernameFromSettings()
307 {
308     qDebug() << __PRETTY_FUNCTION__;
309
310     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
311 }
312
313 void SituareEngine::imageReady(User *user)
314 {
315     qDebug() << __PRETTY_FUNCTION__;
316
317     if(user->type())
318         emit userLocationReady(user);
319     else
320         emit friendImageReady(user);
321 }
322
323 void SituareEngine::initializeGpsAndAutocentering()
324 {
325     qDebug() << __PRETTY_FUNCTION__;
326
327     QSettings settings(DIRECTORY_NAME, FILE_NAME);
328     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
329     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
330
331     if (m_gps->isInitialized()) {
332
333         if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
334
335             connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
336                     this, SLOT(setFirstStartZoomLevel()));
337
338             changeAutoCenteringSetting(true);
339             setGPS(true);
340
341             m_ui->buildInformationBox(tr("GPS enabled"));
342
343         } else { // Normal start
344             changeAutoCenteringSetting(autoCenteringEnabled.toBool());
345             setGPS(gpsEnabled.toBool());
346
347             if (gpsEnabled.toBool())
348                 m_ui->buildInformationBox(tr("GPS enabled"));
349         }
350     } else {
351         setGPS(false);
352     }
353 }
354
355 void SituareEngine::locationSearch(QString location)
356 {
357     qDebug() << __PRETTY_FUNCTION__;
358
359     if(!location.isEmpty())
360         m_geocodingService->requestLocation(location);
361 }
362
363 void SituareEngine::loginActionPressed()
364 {
365     qDebug() << __PRETTY_FUNCTION__;
366
367     if (m_networkAccessManager->isConnected()) {
368         if(m_ui->loginState()) {
369             logout();
370             m_situareService->clearUserData();
371         } else {
372             m_facebookAuthenticator->start();
373         }
374     }
375     else {
376         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
377     }
378 }
379
380 void SituareEngine::loginOk()
381 {
382     qDebug() << __PRETTY_FUNCTION__;
383
384     m_ui->loggedIn(true);
385
386     m_ui->show();
387     m_situareService->fetchLocations(); // request user locations
388
389     if (m_gps->isRunning())
390         m_ui->readAutomaticLocationUpdateSettings();
391 }
392
393 void SituareEngine::loginProcessCancelled()
394 {
395     qDebug() << __PRETTY_FUNCTION__;
396
397     m_ui->toggleProgressIndicator(false);
398     m_ui->updateItemVisibility();
399 }
400
401 void SituareEngine::logout()
402 {
403     qDebug() << __PRETTY_FUNCTION__;
404
405     m_ui->loggedIn(false);
406
407     // signal to clear locationUpdateDialog's data
408     connect(this, SIGNAL(clearUpdateLocationDialogData()),
409             m_ui, SIGNAL(clearUpdateLocationDialogData()));
410     emit clearUpdateLocationDialogData();
411
412     m_facebookAuthenticator->clearAccountInformation(); // clear all
413     m_automaticUpdateFirstStart = true;
414 }
415
416 void SituareEngine::refreshUserData()
417 {
418     qDebug() << __PRETTY_FUNCTION__;
419
420     if (m_networkAccessManager->isConnected()) {
421         m_ui->toggleProgressIndicator(true);
422         m_situareService->fetchLocations();
423     }
424     else {
425         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
426     }
427 }
428
429 void SituareEngine::requestAddress()
430 {
431     qDebug() << __PRETTY_FUNCTION__;
432
433     if (m_networkAccessManager->isConnected()) {
434         if (m_gps->isRunning())
435             m_situareService->reverseGeo(m_gps->lastPosition());
436         else
437             m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
438     }
439     else {
440         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
441     }
442 }
443
444 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
445 {
446     qDebug() << __PRETTY_FUNCTION__;
447
448     if (m_networkAccessManager->isConnected()) {
449         m_ui->toggleProgressIndicator(true);
450
451         if (m_gps->isRunning())
452             m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
453         else
454             m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
455     }
456     else {
457         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
458     }
459 }
460
461 void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
462 {
463     qDebug() << __PRETTY_FUNCTION__;
464
465     if ((fabs(m_lastUpdatedGPSPosition.longitude() - position.longitude()) >
466          USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
467         (fabs(m_lastUpdatedGPSPosition.latitude() - position.latitude()) >
468          USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
469
470         m_lastUpdatedGPSPosition = position;
471         m_userMoved = true;
472     }
473
474     if (m_automaticUpdateRequest && m_userMoved) {
475         requestUpdateLocation(tr("Automatic location update"));
476         m_automaticUpdateRequest = false;
477         m_userMoved = false;
478     }
479 }
480
481 void SituareEngine::routeParsed(Route &route)
482 {
483     qDebug() << __PRETTY_FUNCTION__;
484
485     Q_UNUSED(route);
486
487     m_ui->toggleProgressIndicator(false);
488 }
489
490 void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
491 {
492     qDebug() << __PRETTY_FUNCTION__;
493
494     m_ui->toggleProgressIndicator(true);
495
496     if (m_gps->isRunning())
497         m_routingService->requestRoute(m_gps->lastPosition(), endPointCoordinates);
498     else
499         m_routingService->requestRoute(m_mapEngine->centerGeoCoordinate(), endPointCoordinates);
500 }
501
502 void SituareEngine::setAutoCentering(bool enabled)
503 {
504     qDebug() << __PRETTY_FUNCTION__ << enabled;
505
506     m_ui->setIndicatorButtonEnabled(enabled);
507     m_mapEngine->setAutoCentering(enabled);
508     m_ui->setCrosshairVisibility(!enabled);
509
510     if (enabled) {
511         setGPS(true);
512         m_gps->requestLastPosition();
513     }
514 }
515
516 void SituareEngine::setFirstStartZoomLevel()
517 {
518     qDebug() << __PRETTY_FUNCTION__;
519
520     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
521         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
522
523     disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
524                this, SLOT(setFirstStartZoomLevel()));
525 }
526
527 void SituareEngine::setGPS(bool enabled)
528 {
529     qDebug() << __PRETTY_FUNCTION__ << enabled;
530
531     if (m_gps->isInitialized()) {
532         m_ui->setGPSButtonEnabled(enabled);
533         m_mapEngine->setGPSEnabled(enabled);
534
535         if (enabled && !m_gps->isRunning()) {
536             m_gps->start();
537             m_gps->requestLastPosition();
538
539             if(m_ui->loginState())
540                 m_ui->readAutomaticLocationUpdateSettings();
541         }
542         else if (!enabled && m_gps->isRunning()) {
543             m_gps->stop();
544             changeAutoCenteringSetting(false);
545             enableAutomaticLocationUpdate(false);
546         }
547     }
548     else {
549         if (enabled)
550             m_ui->buildInformationBox(tr("Unable to start GPS"));
551         m_ui->setGPSButtonEnabled(false);
552         m_mapEngine->setGPSEnabled(false);
553     }
554 }
555
556 void SituareEngine::setPowerSaving(bool enabled)
557 {
558     qDebug() << __PRETTY_FUNCTION__ << enabled;
559
560     m_gps->enablePowerSave(enabled);
561
562     if(m_autoCenteringEnabled)
563         m_mapEngine->setAutoCentering(!enabled);
564 }
565
566 void SituareEngine::signalsFromFacebookAuthenticator()
567 {
568     qDebug() << __PRETTY_FUNCTION__;
569
570     connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
571             this, SLOT(error(int, int)));
572
573     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
574             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
575
576     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
577             this, SLOT(loginOk()));
578
579     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
580             m_ui, SLOT(startLoginProcess()));
581
582     connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
583             m_ui, SLOT(saveCookies()));
584
585     connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
586             m_ui, SLOT(loginUsingCookies()));
587 }
588
589 void SituareEngine::signalsFromGeocodingService()
590 {
591     qDebug() << __PRETTY_FUNCTION__;
592
593     connect(m_geocodingService, SIGNAL(locationDataParsed(const QList<Location>&)),
594             m_ui, SIGNAL(locationDataParsed(const QList<Location>&)));
595 }
596
597 void SituareEngine::signalsFromGPS()
598 {
599     qDebug() << __PRETTY_FUNCTION__;
600
601     connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
602             m_mapEngine, SLOT(gpsPositionUpdate(GeoCoordinate, qreal)));
603
604     connect(m_gps, SIGNAL(timeout()),
605             m_ui, SLOT(gpsTimeout()));
606
607     connect(m_gps, SIGNAL(error(int, int)),
608             this, SLOT(error(int, int)));
609 }
610
611 void SituareEngine::signalsFromMainWindow()
612 {
613     qDebug() << __PRETTY_FUNCTION__;
614
615     connect(m_ui, SIGNAL(error(int, int)),
616             this, SLOT(error(int, int)));
617
618     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
619             this, SLOT(fetchUsernameFromSettings()));
620
621     connect(m_ui, SIGNAL(loginActionPressed()),
622             this, SLOT(loginActionPressed()));
623
624     connect(m_ui, SIGNAL(saveUsername(QString)),
625             m_facebookAuthenticator, SLOT(saveUsername(QString)));
626
627     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
628             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
629
630     // signals from map view
631     connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate)),
632             m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
633
634     connect(m_ui, SIGNAL(mapViewResized(QSize)),
635             m_mapEngine, SLOT(viewResized(QSize)));
636
637     connect(m_ui, SIGNAL(viewZoomFinished()),
638             m_mapEngine, SLOT(viewZoomFinished()));
639
640     // signals from zoom buttons (zoom panel and volume buttons)
641     connect(m_ui, SIGNAL(zoomIn()),
642             m_mapEngine, SLOT(zoomIn()));
643
644     connect(m_ui, SIGNAL(zoomOut()),
645             m_mapEngine, SLOT(zoomOut()));
646
647     // signals from menu buttons
648     connect(m_ui, SIGNAL(gpsTriggered(bool)),
649             this, SLOT(setGPS(bool)));
650
651     //signals from dialogs
652     connect(m_ui, SIGNAL(cancelLoginProcess()),
653             this, SLOT(loginProcessCancelled()));
654
655     connect(m_ui, SIGNAL(requestReverseGeo()),
656             this, SLOT(requestAddress()));
657
658     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
659             this, SLOT(requestUpdateLocation(QString,bool)));
660
661     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
662             this, SLOT(enableAutomaticLocationUpdate(bool, int)));
663
664     // signals from user info tab
665     connect(m_ui, SIGNAL(refreshUserData()),
666             this, SLOT(refreshUserData()));
667
668     connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
669             m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
670
671     // signals from routing tab
672     connect(m_ui,
673             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
674             m_mapEngine,
675             SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
676
677     // signals from distence indicator button
678     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
679             this, SLOT(changeAutoCenteringSetting(bool)));
680
681     connect(m_ui, SIGNAL(searchForLocation(QString)),
682             this, SLOT(locationSearch(QString)));
683
684     connect(m_ui, SIGNAL(draggingModeTriggered()),
685             this, SLOT(draggingModeTriggered()));
686
687     connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
688             this, SLOT(routeTo(const GeoCoordinate&)));
689 }
690
691 void SituareEngine::signalsFromMapEngine()
692 {
693     qDebug() << __PRETTY_FUNCTION__;
694
695     connect(m_mapEngine, SIGNAL(error(int, int)),
696             this, SLOT(error(int, int)));
697
698     connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate)),
699             m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate)));
700
701     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
702             m_ui, SIGNAL(zoomLevelChanged(int)));
703
704     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
705             this, SLOT(disableAutoCentering()));
706
707     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
708             m_ui, SIGNAL(maxZoomLevelReached()));
709
710     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
711             m_ui, SIGNAL(minZoomLevelReached()));
712
713     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
714             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
715
716     connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
717             m_ui, SIGNAL(newMapResolution(qreal)));
718
719     connect(m_mapEngine, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
720             m_ui, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)));
721 }
722
723 void SituareEngine::signalsFromRoutingService()
724 {
725     qDebug() << __PRETTY_FUNCTION__;
726
727     connect(m_routingService, SIGNAL(routeParsed(Route&)),
728             this, SLOT(routeParsed(Route&)));
729
730     connect(m_routingService, SIGNAL(routeParsed(Route&)),
731             m_mapEngine, SLOT(setRoute(Route&)));
732
733     connect(m_routingService, SIGNAL(routeParsed(Route&)),
734             m_ui, SIGNAL(routeParsed(Route&)));
735
736     connect(m_routingService, SIGNAL(error(int, int)),
737             this, SLOT(error(int, int)));
738 }
739
740 void SituareEngine::signalsFromSituareService()
741 {
742     qDebug() << __PRETTY_FUNCTION__;
743
744     connect(m_situareService, SIGNAL(error(int, int)),
745             this, SLOT(error(int, int)));
746
747     connect(m_situareService, SIGNAL(imageReady(User*)),
748             this, SLOT(imageReady(User*)));
749
750     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
751             m_ui, SIGNAL(reverseGeoReady(QString)));
752
753     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
754             this, SLOT(userDataChanged(User*, QList<User*>&)));
755
756     connect(m_situareService, SIGNAL(updateWasSuccessful()),
757             this, SLOT(updateWasSuccessful()));
758
759     connect(m_situareService, SIGNAL(updateWasSuccessful()),
760             m_ui, SIGNAL(clearUpdateLocationDialogData()));
761 }
762
763 void SituareEngine::startAutomaticUpdate()
764 {
765     qDebug() << __PRETTY_FUNCTION__;
766
767     m_gps->requestUpdate();
768     m_automaticUpdateRequest = true;
769 }
770
771 void SituareEngine::topmostWindowChanged(bool isMainWindow)
772 {
773     qDebug() << __PRETTY_FUNCTION__;
774
775     setPowerSaving(!isMainWindow);
776 }
777
778 void SituareEngine::updateWasSuccessful()
779 {
780     qDebug() << __PRETTY_FUNCTION__;
781
782     if (m_networkAccessManager->isConnected())
783         m_situareService->fetchLocations();
784     else
785         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
786 }
787
788 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
789 {
790     qDebug() << __PRETTY_FUNCTION__;
791
792     m_ui->toggleProgressIndicator(false);
793     m_ui->showPanels();
794
795     emit userLocationReady(user);
796     emit friendsLocationsReady(friendsList);
797 }