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