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