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