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