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