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