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