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