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