Moving login related stuff from Engine to FacebookAuthentication
[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 #include "application.h"
31 #include "common.h"
32 #include "contactmanager.h"
33 #include "../error.h"
34 #include "ui/facebookloginbrowser.h"
35 #include "facebookservice/facebookauthentication.h"
36 #include "gps/gpsposition.h"
37 #include "map/mapengine.h"
38 #include "routing/geocodingservice.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     Application *application = static_cast<Application *>(qApp);
68     application->registerWindow(m_ui->winId());
69
70     connect(application, SIGNAL(topmostWindowChanged(bool)),
71             this, SLOT(topmostWindowChanged(bool)));
72
73     m_networkAccessManager = new NetworkAccessManager(this);
74
75     // build MapEngine
76     m_mapEngine = new MapEngine(this);
77     m_ui->setMapViewScene(m_mapEngine->scene());
78
79     // build GPS
80     m_gps = new GPSPosition(this);
81
82     // build SituareService
83     m_situareService = new SituareService(this);
84
85     // build FacebookAuthenticator
86     m_facebookAuthenticator = new FacebookAuthentication(this);
87
88     // build routing service
89     m_routingService = new RoutingService(this); // create this when needed, not in constructor!
90
91     // build geocoding service
92     m_geocodingService = new GeocodingService(this);
93
94     // connect signals
95     signalsFromMapEngine();
96     signalsFromGeocodingService();
97     signalsFromGPS();
98     signalsFromRoutingService();
99     signalsFromSituareService();
100     signalsFromMainWindow();
101     signalsFromFacebookAuthenticator();
102
103     connect(this, SIGNAL(userLocationReady(User*)),
104             m_ui, SIGNAL(userLocationReady(User*)));
105
106     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
107             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
108
109     connect(this, SIGNAL(userLocationReady(User*)),
110             m_mapEngine, SLOT(receiveOwnLocation(User*)));
111
112     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
113             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
114
115     connect(this, SIGNAL(friendImageReady(User*)),
116             m_ui, SIGNAL(friendImageReady(User*)));
117
118     connect(this, SIGNAL(friendImageReady(User*)),
119             m_mapEngine, SIGNAL(friendImageReady(User*)));
120
121     m_automaticUpdateIntervalTimer = new QTimer(this);
122     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
123             this, SLOT(startAutomaticUpdate()));
124
125     // signals connected, now it's time to show the main window
126     // but init the MapEngine before so starting location is set
127     m_mapEngine->init();
128     m_ui->show();
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(setPowerSaving(bool)));
135
136     m_contactManager = new ContactManager(this);
137     m_contactManager->requestContactGuids();
138
139     m_facebookAuthenticator->login();
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::error(const int context, const int error)
209 {
210     qDebug() << __PRETTY_FUNCTION__;
211
212     switch(error)
213     {
214     case SituareError::ERROR_GENERAL:
215         if(context == ErrorContext::SITUARE) {
216             m_ui->toggleProgressIndicator(false);
217             m_ui->buildInformationBox(tr("Unknown server error"), true);
218         }
219         break;
220     case 1: //errors: SituareError::ERROR_MISSING_ARGUMENT and QNetworkReply::ConnectionRefusedError
221         m_ui->toggleProgressIndicator(false);
222         if(context == ErrorContext::SITUARE) {
223             m_ui->buildInformationBox(tr("Missing parameter from request"), true);
224         } else if(context == ErrorContext::NETWORK) {
225             m_ui->buildInformationBox(tr("Connection refused by the server"), true);
226         }
227         break;
228     case QNetworkReply::RemoteHostClosedError:
229         if(context == ErrorContext::NETWORK) {
230             m_ui->toggleProgressIndicator(false);
231             m_ui->buildInformationBox(tr("Connection closed by the server"), true);
232         }
233         break;
234     case QNetworkReply::HostNotFoundError:
235         if(context == ErrorContext::NETWORK) {
236             m_ui->toggleProgressIndicator(false);
237             m_ui->buildInformationBox(tr("Remote server not found"), true);
238         }
239         break;
240     case QNetworkReply::TimeoutError:
241         if(context == ErrorContext::NETWORK) {
242             m_ui->toggleProgressIndicator(false);
243             m_ui->buildInformationBox(tr("Connection timed out"), true);
244         }
245         break;
246     case QNetworkReply::UnknownNetworkError:
247         if(context == ErrorContext::NETWORK) {
248             m_ui->toggleProgressIndicator(false);
249             m_ui->buildInformationBox(tr("No network connection"), true);
250         }
251         break;
252     case SituareError::SESSION_EXPIRED:
253         m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
254         m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
255         m_situareService->clearUserData();
256         m_ui->loggedIn(false);
257         m_ui->loginFailed();
258         break;
259     case SituareError::LOGIN_FAILED:
260         m_ui->toggleProgressIndicator(false);
261         m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
262         m_ui->loginFailed();
263         break;
264     case SituareError::UPDATE_FAILED:
265         m_ui->toggleProgressIndicator(false);
266         m_ui->buildInformationBox(tr("Update failed, please try again"), true);
267         break;
268     case SituareError::DATA_RETRIEVAL_FAILED:
269         m_ui->toggleProgressIndicator(false);
270         m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
271         break;
272     case SituareError::ADDRESS_RETRIEVAL_FAILED:
273         m_ui->toggleProgressIndicator(false);
274         m_ui->buildInformationBox(tr("Address retrieval failed"), true);
275         break;
276     case SituareError::IMAGE_DOWNLOAD_FAILED:
277         m_ui->buildInformationBox(tr("Image download failed"), true);
278         break;
279     case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
280         m_ui->buildInformationBox(tr("Map image download failed"), true);
281         break;
282     case SituareError::GPS_INITIALIZATION_FAILED:
283         setGPS(false);
284         m_ui->buildInformationBox(tr("GPS initialization failed"), true);
285         break;
286     case SituareError::INVALID_JSON:
287         m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
288         m_ui->loggedIn(false);
289         m_facebookAuthenticator->clearAccountInformation(false); // clean all
290         break;
291     case SituareError::ERROR_ROUTING_FAILED:
292         m_ui->toggleProgressIndicator(false);
293         m_ui->buildInformationBox(tr("Routing failed"), true);
294         break;
295     case SituareError::ERROR_LOCATION_SEARCH_FAILED:
296         m_ui->buildInformationBox(tr("No results found"), true);
297         break;
298     default:
299         m_ui->toggleProgressIndicator(false);
300         if(context == ErrorContext::NETWORK)
301             qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
302         else
303             qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
304         break;
305     }
306 }
307
308 void SituareEngine::imageReady(User *user)
309 {
310     qDebug() << __PRETTY_FUNCTION__;
311
312     if(user->type())
313         emit userLocationReady(user);
314     else
315         emit friendImageReady(user);
316 }
317
318 void SituareEngine::initializeGpsAndAutocentering()
319 {
320     qDebug() << __PRETTY_FUNCTION__;
321
322     QSettings settings(DIRECTORY_NAME, FILE_NAME);
323     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
324     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
325
326     if (m_gps->isInitialized()) {
327
328         if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
329
330             connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
331                     this, SLOT(setFirstStartZoomLevel()));
332
333             changeAutoCenteringSetting(true);
334             setGPS(true);
335
336             m_ui->buildInformationBox(tr("GPS enabled"));
337
338         } else { // Normal start
339             changeAutoCenteringSetting(autoCenteringEnabled.toBool());
340             setGPS(gpsEnabled.toBool());
341
342             if (gpsEnabled.toBool())
343                 m_ui->buildInformationBox(tr("GPS enabled"));
344         }
345     } else {
346         setGPS(false);
347     }
348 }
349
350 void SituareEngine::locationSearch(QString location)
351 {
352     qDebug() << __PRETTY_FUNCTION__;
353
354     if(!location.isEmpty())
355         m_geocodingService->requestLocation(location);
356 }
357
358 void SituareEngine::loggedIn()
359 {
360     qWarning() << __PRETTY_FUNCTION__;
361
362     m_ui->destroyFacebookLoginBrowser();
363
364     loginOk();
365 }
366
367 void SituareEngine::loginActionPressed()
368 {
369     qDebug() << __PRETTY_FUNCTION__;
370
371     if (m_networkAccessManager->isConnected()) {
372         if(m_ui->loginState()) {
373             logout();
374             m_situareService->clearUserData();
375         } else {
376             m_facebookAuthenticator->login();
377         }
378     }
379     else {
380         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
381     }
382 }
383
384 void SituareEngine::loginOk()
385 {
386     qDebug() << __PRETTY_FUNCTION__;
387
388     m_ui->loggedIn(true);
389
390     m_ui->show();
391     m_situareService->fetchLocations(); // request user locations
392
393     if (m_gps->isRunning())
394         m_ui->readAutomaticLocationUpdateSettings();
395 }
396
397 void SituareEngine::loginProcessCancelled()
398 {
399     qDebug() << __PRETTY_FUNCTION__;
400
401     m_ui->toggleProgressIndicator(false);
402     m_ui->updateItemVisibility();
403 }
404
405 void SituareEngine::logout()
406 {
407     qDebug() << __PRETTY_FUNCTION__;
408
409     m_ui->loggedIn(false);
410
411     // signal to clear locationUpdateDialog's data
412     connect(this, SIGNAL(clearUpdateLocationDialogData()),
413             m_ui, SIGNAL(clearUpdateLocationDialogData()));
414     emit clearUpdateLocationDialogData();
415
416     m_facebookAuthenticator->clearAccountInformation(); // clear all
417     m_automaticUpdateFirstStart = true;
418 }
419
420 void SituareEngine::refreshUserData()
421 {
422     qDebug() << __PRETTY_FUNCTION__;
423
424     if (m_networkAccessManager->isConnected()) {
425         m_ui->toggleProgressIndicator(true);
426         m_situareService->fetchLocations();
427     }
428     else {
429         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
430     }
431 }
432
433 void SituareEngine::requestAddress()
434 {
435     qDebug() << __PRETTY_FUNCTION__;
436
437     if (m_networkAccessManager->isConnected()) {
438         if (m_gps->isRunning())
439             m_situareService->reverseGeo(m_gps->lastPosition());
440         else
441             m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
442     }
443     else {
444         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
445     }
446 }
447
448 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
449 {
450     qDebug() << __PRETTY_FUNCTION__;
451
452     if (m_networkAccessManager->isConnected()) {
453         m_ui->toggleProgressIndicator(true);
454
455         if (m_gps->isRunning())
456             m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
457         else
458             m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
459     }
460     else {
461         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
462     }
463 }
464
465 void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
466 {
467     qDebug() << __PRETTY_FUNCTION__;
468
469     if ((fabs(m_lastUpdatedGPSPosition.longitude() - position.longitude()) >
470          USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
471         (fabs(m_lastUpdatedGPSPosition.latitude() - position.latitude()) >
472          USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
473
474         m_lastUpdatedGPSPosition = position;
475         m_userMoved = true;
476     }
477
478     if (m_automaticUpdateRequest && m_userMoved) {
479         requestUpdateLocation(tr("Automatic location update"));
480         m_automaticUpdateRequest = false;
481         m_userMoved = false;
482     }
483 }
484
485 void SituareEngine::routeParsed(Route &route)
486 {
487     qDebug() << __PRETTY_FUNCTION__;
488
489     Q_UNUSED(route);
490
491     m_ui->toggleProgressIndicator(false);
492 }
493
494 void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
495 {
496     qDebug() << __PRETTY_FUNCTION__;
497
498     m_ui->toggleProgressIndicator(true);
499
500     if (m_gps->isRunning())
501         m_routingService->requestRoute(m_gps->lastPosition(), endPointCoordinates);
502     else
503         m_routingService->requestRoute(m_mapEngine->centerGeoCoordinate(), endPointCoordinates);
504 }
505
506 void SituareEngine::routeToCursor()
507 {
508     qDebug() << __PRETTY_FUNCTION__;
509
510     routeTo(m_mapEngine->centerGeoCoordinate());
511 }
512
513 void SituareEngine::setAutoCentering(bool enabled)
514 {
515     qDebug() << __PRETTY_FUNCTION__ << enabled;
516
517     m_ui->setIndicatorButtonEnabled(enabled);
518     m_mapEngine->setAutoCentering(enabled);
519     m_ui->setCrosshairVisibility(!enabled);
520
521     if (enabled) {
522         setGPS(true);
523         m_gps->requestLastPosition();
524     }
525 }
526
527 void SituareEngine::setFirstStartZoomLevel()
528 {
529     qDebug() << __PRETTY_FUNCTION__;
530
531     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
532         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
533
534     disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
535                this, SLOT(setFirstStartZoomLevel()));
536 }
537
538 void SituareEngine::setGPS(bool enabled)
539 {
540     qDebug() << __PRETTY_FUNCTION__ << enabled;
541
542     if (m_gps->isInitialized()) {
543         m_ui->setGPSButtonEnabled(enabled);
544         m_mapEngine->setGPSEnabled(enabled);
545
546         if (enabled && !m_gps->isRunning()) {
547             m_gps->start();
548             m_gps->requestLastPosition();
549
550             if(m_ui->loginState())
551                 m_ui->readAutomaticLocationUpdateSettings();
552         }
553         else if (!enabled && m_gps->isRunning()) {
554             m_gps->stop();
555             changeAutoCenteringSetting(false);
556             enableAutomaticLocationUpdate(false);
557         }
558     }
559     else {
560         if (enabled)
561             m_ui->buildInformationBox(tr("Unable to start GPS"));
562         m_ui->setGPSButtonEnabled(false);
563         m_mapEngine->setGPSEnabled(false);
564     }
565 }
566
567 void SituareEngine::setPowerSaving(bool enabled)
568 {
569     qDebug() << __PRETTY_FUNCTION__ << enabled;
570
571     m_gps->enablePowerSave(enabled);
572
573     if(m_autoCenteringEnabled)
574         m_mapEngine->setAutoCentering(!enabled);
575 }
576
577 void SituareEngine::showContactDialog(const QString &facebookId)
578 {
579     qDebug() << __PRETTY_FUNCTION__;
580
581     QString guid = m_contactManager->contactGuid(facebookId);
582
583     if (!guid.isEmpty())
584         m_ui->showContactDialog(guid);
585     else
586         m_ui->buildInformationBox(tr("Unable to find contact.\nAdd Facebook IM "
587                                      "account from Conversations to use this feature."), true);
588 }
589
590 void SituareEngine::signalsFromFacebookAuthenticator()
591 {
592     qDebug() << __PRETTY_FUNCTION__;
593
594     connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
595             this, SLOT(error(int, int)));
596
597     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
598             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
599
600     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
601             this, SLOT(loginOk()));
602
603     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
604             m_ui, SLOT(startLoginProcess()));
605
606     connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
607             m_ui, SLOT(saveCookies()));
608
609     connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
610             m_ui, SLOT(loginUsingCookies()));
611
612     connect(m_facebookAuthenticator, SIGNAL(buildLoginBrowser()),
613             m_ui, SLOT(buildFacebookLoginBrowser()));
614
615     connect(m_facebookAuthenticator, SIGNAL(loggedIn(QString)),
616             m_situareService, SLOT(updateSession(QString)));
617
618     connect(m_facebookAuthenticator, SIGNAL(loggedIn(QString)),
619             this, SLOT(loggedIn()));
620 }
621
622 void SituareEngine::signalsFromGeocodingService()
623 {
624     qDebug() << __PRETTY_FUNCTION__;
625
626     connect(m_geocodingService, SIGNAL(locationDataParsed(const QList<Location>&)),
627             m_ui, SIGNAL(locationDataParsed(const QList<Location>&)));
628
629     connect(m_geocodingService, SIGNAL(error(int, int)),
630             this, SLOT(error(int, int)));
631 }
632
633 void SituareEngine::signalsFromGPS()
634 {
635     qDebug() << __PRETTY_FUNCTION__;
636
637     connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
638             m_mapEngine, SLOT(gpsPositionUpdate(GeoCoordinate, qreal)));
639
640     connect(m_gps, SIGNAL(timeout()),
641             m_ui, SLOT(gpsTimeout()));
642
643     connect(m_gps, SIGNAL(error(int, int)),
644             this, SLOT(error(int, int)));
645 }
646
647 void SituareEngine::signalsFromMainWindow()
648 {
649     qDebug() << __PRETTY_FUNCTION__;
650
651     connect(m_ui, SIGNAL(error(int, int)),
652             this, SLOT(error(int, int)));
653
654     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
655             this, SLOT(fetchUsernameFromSettings()));
656
657     connect(m_ui, SIGNAL(loginActionPressed()),
658             this, SLOT(loginActionPressed()));
659
660     connect(m_ui, SIGNAL(saveUsername(QString)),
661             m_facebookAuthenticator, SLOT(saveUsername(QString)));
662
663     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
664             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
665
666     connect(m_ui, SIGNAL(loginBrowserCreated(FacebookLoginBrowser*)),
667             m_facebookAuthenticator, SLOT(setBrowser(FacebookLoginBrowser*)));
668
669     // signals from map view
670     connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate)),
671             m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
672
673     connect(m_ui, SIGNAL(mapViewResized(QSize)),
674             m_mapEngine, SLOT(viewResized(QSize)));
675
676     connect(m_ui, SIGNAL(viewZoomFinished()),
677             m_mapEngine, SLOT(viewZoomFinished()));
678
679     // signals from zoom buttons (zoom panel and volume buttons)
680     connect(m_ui, SIGNAL(zoomIn()),
681             m_mapEngine, SLOT(zoomIn()));
682
683     connect(m_ui, SIGNAL(zoomOut()),
684             m_mapEngine, SLOT(zoomOut()));
685
686     // signals from menu buttons
687     connect(m_ui, SIGNAL(gpsTriggered(bool)),
688             this, SLOT(setGPS(bool)));
689
690     //signals from dialogs
691     connect(m_ui, SIGNAL(cancelLoginProcess()),
692             this, SLOT(loginProcessCancelled()));
693
694     connect(m_ui, SIGNAL(requestReverseGeo()),
695             this, SLOT(requestAddress()));
696
697     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
698             this, SLOT(requestUpdateLocation(QString,bool)));
699
700     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
701             this, SLOT(enableAutomaticLocationUpdate(bool, int)));
702
703     // signals from user info tab
704     connect(m_ui, SIGNAL(refreshUserData()),
705             this, SLOT(refreshUserData()));
706
707     connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
708             m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
709
710     // routing signal from friend list tab & search location tab
711     connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
712             this, SLOT(routeTo(const GeoCoordinate&)));
713
714     // signals from location search panel
715     connect(m_ui,
716             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
717             m_mapEngine,
718             SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
719
720     connect(m_ui, SIGNAL(searchHistoryItemClicked(QString)),
721             this, SLOT(locationSearch(QString)));
722
723     // signals from routing tab
724     connect(m_ui, SIGNAL(clearRoute()),
725             m_mapEngine, SLOT(clearRoute()));
726
727     connect(m_ui, SIGNAL(routeToCursor()),
728             this, SLOT(routeToCursor()));
729
730     // signals from distance indicator button
731     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
732             this, SLOT(changeAutoCenteringSetting(bool)));
733
734     connect(m_ui, SIGNAL(draggingModeTriggered()),
735             this, SLOT(draggingModeTriggered()));
736
737     // signal from search location dialog
738     connect(m_ui, SIGNAL(searchForLocation(QString)),
739             this, SLOT(locationSearch(QString)));
740
741     // signal from friend list panel
742     connect(m_ui, SIGNAL(requestContactDialog(const QString &)),
743             this, SLOT(showContactDialog(const QString &)));
744 }
745
746 void SituareEngine::signalsFromMapEngine()
747 {
748     qDebug() << __PRETTY_FUNCTION__;
749
750     connect(m_mapEngine, SIGNAL(error(int, int)),
751             this, SLOT(error(int, int)));
752
753     connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate)),
754             m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate)));
755
756     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
757             m_ui, SIGNAL(zoomLevelChanged(int)));
758
759     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
760             this, SLOT(disableAutoCentering()));
761
762     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
763             m_ui, SIGNAL(maxZoomLevelReached()));
764
765     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
766             m_ui, SIGNAL(minZoomLevelReached()));
767
768     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
769             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
770
771     connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
772             m_ui, SIGNAL(newMapResolution(qreal)));
773
774     connect(m_mapEngine, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
775             m_ui, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)));
776 }
777
778 void SituareEngine::signalsFromRoutingService()
779 {
780     qDebug() << __PRETTY_FUNCTION__;
781
782     connect(m_routingService, SIGNAL(routeParsed(Route&)),
783             this, SLOT(routeParsed(Route&)));
784
785     connect(m_routingService, SIGNAL(routeParsed(Route&)),
786             m_mapEngine, SLOT(setRoute(Route&)));
787
788     connect(m_routingService, SIGNAL(routeParsed(Route&)),
789             m_ui, SIGNAL(routeParsed(Route&)));
790
791     connect(m_routingService, SIGNAL(error(int, int)),
792             this, SLOT(error(int, int)));
793 }
794
795 void SituareEngine::signalsFromSituareService()
796 {
797     qDebug() << __PRETTY_FUNCTION__;
798
799     connect(m_situareService, SIGNAL(error(int, int)),
800             this, SLOT(error(int, int)));
801
802     connect(m_situareService, SIGNAL(imageReady(User*)),
803             this, SLOT(imageReady(User*)));
804
805     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
806             m_ui, SIGNAL(reverseGeoReady(QString)));
807
808     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
809             this, SLOT(userDataChanged(User*, QList<User*>&)));
810
811     connect(m_situareService, SIGNAL(updateWasSuccessful()),
812             this, SLOT(updateWasSuccessful()));
813
814     connect(m_situareService, SIGNAL(updateWasSuccessful()),
815             m_ui, SIGNAL(clearUpdateLocationDialogData()));
816 }
817
818 void SituareEngine::startAutomaticUpdate()
819 {
820     qDebug() << __PRETTY_FUNCTION__;
821
822     m_gps->requestUpdate();
823     m_automaticUpdateRequest = true;
824 }
825
826 void SituareEngine::topmostWindowChanged(bool isMainWindow)
827 {
828     qDebug() << __PRETTY_FUNCTION__;
829
830     setPowerSaving(!isMainWindow);
831 }
832
833 void SituareEngine::updateWasSuccessful()
834 {
835     qDebug() << __PRETTY_FUNCTION__;
836
837     if (m_networkAccessManager->isConnected())
838         m_situareService->fetchLocations();
839     else
840         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
841 }
842
843 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
844 {
845     qDebug() << __PRETTY_FUNCTION__;
846
847     m_ui->toggleProgressIndicator(false);
848
849     emit userLocationReady(user);
850     emit friendsLocationsReady(friendsList);
851 }