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