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