1658c1b03e02f72d9ec54b828aea6ef589d0fdc1
[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(false);
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(m_networkAccessManager,
83                                           new ImageFetcher(m_networkAccessManager, this), this);
84
85     // build FacebookAuthenticator
86     m_facebookAuthenticator = new FacebookAuthentication(m_ui, this);
87
88     // build routing service
89     m_routingService = new RoutingService(this); // create this when needed, not in constructor!
90
91     // build geocoding service
92     m_geocodingService = new GeocodingService(this);
93
94     // connect signals
95     signalsFromMapEngine();
96     signalsFromGeocodingService();
97     signalsFromGPS();
98     signalsFromRoutingService();
99     signalsFromSituareService();
100     signalsFromMainWindow();
101     signalsFromFacebookAuthenticator();
102
103     connect(this, SIGNAL(userLocationReady(User*)),
104             m_ui, SIGNAL(userLocationReady(User*)));
105
106     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
107             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
108
109     connect(this, SIGNAL(userLocationReady(User*)),
110             m_mapEngine, SLOT(receiveOwnLocation(User*)));
111
112     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
113             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
114
115     connect(this, SIGNAL(userImageReady(QString,QPixmap)),
116             m_ui, SIGNAL(userImageReady(QString,QPixmap)));
117
118     connect(this, SIGNAL(friendImageReady(QString,QPixmap)),
119             m_ui, SIGNAL(friendImageReady(QString,QPixmap)));
120
121     connect(this, SIGNAL(friendImageReady(QString,QPixmap)),
122             m_mapEngine, SIGNAL(friendImageReady(QString,QPixmap)));
123
124     m_automaticUpdateIntervalTimer = new QTimer(this);
125     connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
126             this, SLOT(startAutomaticUpdate()));
127
128     // signals connected, now it's time to show the main window
129     // but init the MapEngine before so starting location is set
130     m_mapEngine->init();
131     m_ui->show();
132
133     m_gps->setMode(GPSPosition::Default);
134     initializeGpsAndAutocentering();
135
136     m_mce = new MCE(this);
137     connect(m_mce, SIGNAL(displayOff(bool)), this, SLOT(setPowerSaving(bool)));
138
139     m_contactManager = new ContactManager(this);
140     m_contactManager->requestContactGuids();
141
142     m_facebookAuthenticator->login();
143 }
144
145 SituareEngine::~SituareEngine()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     delete m_ui;
150
151     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
152     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
153     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
154 }
155
156 void SituareEngine::changeAutoCenteringSetting(bool enabled)
157 {
158     qDebug() << __PRETTY_FUNCTION__ << enabled;
159
160     m_autoCenteringEnabled = enabled;
161     setAutoCentering(enabled);
162 }
163
164 void SituareEngine::disableAutoCentering()
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     changeAutoCenteringSetting(false);
169 }
170
171 void SituareEngine::draggingModeTriggered()
172 {
173     qDebug() << __PRETTY_FUNCTION__;
174
175     if (m_mce)
176         m_mce->vibrationFeedback();
177 }
178
179 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
180 {
181     qDebug() << __PRETTY_FUNCTION__;
182
183     //Show automatic update confirmation dialog
184     if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
185         m_ui->showEnableAutomaticUpdateLocationDialog(
186                 tr("Do you want to enable automatic location update with %1 min update interval?")
187                 .arg(updateIntervalMsecs/1000/60));
188         m_automaticUpdateFirstStart = false;
189     } else {
190         if (enabled && m_gps->isRunning()) {
191             m_ui->buildInformationBox(tr("Automatic location update enabled"));
192             if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
193                 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
194             else
195                 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
196
197             connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
198                     this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
199
200             m_automaticUpdateIntervalTimer->start();
201
202         } else {
203             disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
204                     this, SLOT(requestAutomaticUpdateIfMoved(GeoCoordinate)));
205
206             m_automaticUpdateIntervalTimer->stop();
207         }
208     }
209 }
210
211 void SituareEngine::error(const int context, const int error)
212 {
213     qDebug() << __PRETTY_FUNCTION__;
214
215     switch(error)
216     {
217     case SituareError::ERROR_GENERAL:
218         if(context == ErrorContext::SITUARE) {
219             m_ui->toggleProgressIndicator(false);
220             m_ui->buildInformationBox(tr("Unknown server error"), true);
221         }
222         break;
223     case 1: //errors: SituareError::ERROR_MISSING_ARGUMENT and QNetworkReply::ConnectionRefusedError
224         m_ui->toggleProgressIndicator(false);
225         if(context == ErrorContext::SITUARE) {
226             m_ui->buildInformationBox(tr("Missing parameter from request"), true);
227         } else if(context == ErrorContext::NETWORK) {
228             m_ui->buildInformationBox(tr("Connection refused by the server"), true);
229         }
230         break;
231     case QNetworkReply::RemoteHostClosedError:
232         if(context == ErrorContext::NETWORK) {
233             m_ui->toggleProgressIndicator(false);
234             m_ui->buildInformationBox(tr("Connection closed by the server"), true);
235         }
236         break;
237     case QNetworkReply::HostNotFoundError:
238         if(context == ErrorContext::NETWORK) {
239             m_ui->toggleProgressIndicator(false);
240             m_ui->buildInformationBox(tr("Remote server not found"), true);
241         }
242         break;
243     case QNetworkReply::TimeoutError:
244         if(context == ErrorContext::NETWORK) {
245             m_ui->toggleProgressIndicator(false);
246             m_ui->buildInformationBox(tr("Connection timed out"), true);
247         }
248         break;
249     case QNetworkReply::UnknownNetworkError:
250         if(context == ErrorContext::NETWORK) {
251             m_ui->toggleProgressIndicator(false);
252             m_ui->buildInformationBox(tr("No network connection"), true);
253         }
254         break;
255     case SituareError::SESSION_EXPIRED:
256         m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
257         m_facebookAuthenticator->logOut();
258         m_facebookAuthenticator->login();
259         break;
260     case SituareError::UPDATE_FAILED:
261         m_ui->toggleProgressIndicator(false);
262         m_ui->buildInformationBox(tr("Update failed, please try again"), true);
263         break;
264     case SituareError::DATA_RETRIEVAL_FAILED:
265         m_ui->toggleProgressIndicator(false);
266         m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
267         break;
268     case SituareError::ADDRESS_RETRIEVAL_FAILED:
269         m_ui->toggleProgressIndicator(false);
270         m_ui->buildInformationBox(tr("Address retrieval failed"), true);
271         break;
272     case SituareError::IMAGE_DOWNLOAD_FAILED:
273         m_ui->buildInformationBox(tr("Image download failed"), true);
274         break;
275     case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
276         m_ui->buildInformationBox(tr("Map image download failed"), true);
277         break;
278     case SituareError::GPS_INITIALIZATION_FAILED:
279         setGPS(false);
280         m_ui->buildInformationBox(tr("GPS initialization failed"), true);
281         break;
282     case SituareError::INVALID_JSON:
283         m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
284         m_ui->loggedIn(false);
285         m_facebookAuthenticator->clearAccountInformation(false); // clean all
286         break;
287     case SituareError::ERROR_ROUTING_FAILED:
288         m_ui->toggleProgressIndicator(false);
289         m_ui->buildInformationBox(tr("Routing failed"), true);
290         break;
291     case SituareError::ERROR_LOCATION_SEARCH_FAILED:
292         m_ui->buildInformationBox(tr("No results found"), true);
293         break;
294     default:
295         m_ui->toggleProgressIndicator(false);
296         if(context == ErrorContext::NETWORK)
297             qCritical() << __PRETTY_FUNCTION__ << "QNetworkReply::NetworkError: " << error;
298         else
299             qCritical() << __PRETTY_FUNCTION__ << "Unknown error: " << error;
300         break;
301     }
302 }
303
304 void SituareEngine::imageReady(const QString &id, const QPixmap &image)
305 {
306     qDebug() << __PRETTY_FUNCTION__;
307
308     if (id.isNull())
309         emit userImageReady(id, image);
310     else
311         emit friendImageReady(id, image);
312 }
313
314 void SituareEngine::initializeGpsAndAutocentering()
315 {
316     qDebug() << __PRETTY_FUNCTION__;
317
318     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
319     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
320     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
321
322     if (m_gps->isInitialized()) {
323
324         if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
325
326             connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
327                     this, SLOT(setFirstStartZoomLevel()));
328
329             changeAutoCenteringSetting(true);
330             setGPS(true);
331
332             m_ui->buildInformationBox(tr("GPS enabled"));
333
334         } else { // Normal start
335             changeAutoCenteringSetting(autoCenteringEnabled.toBool());
336             setGPS(gpsEnabled.toBool());
337
338             if (gpsEnabled.toBool())
339                 m_ui->buildInformationBox(tr("GPS enabled"));
340         }
341     } else {
342         setGPS(false);
343     }
344 }
345
346 void SituareEngine::locationSearch(QString location)
347 {
348     qDebug() << __PRETTY_FUNCTION__;
349
350     if(!location.isEmpty())
351         m_geocodingService->requestLocation(location);
352 }
353
354 void SituareEngine::loginActionPressed()
355 {
356     qDebug() << __PRETTY_FUNCTION__;
357
358     if (m_facebookAuthenticator->isLoggedIn())
359         m_facebookAuthenticator->logOut(true);
360     else if (m_networkAccessManager->isConnected())
361         m_facebookAuthenticator->login();
362     else
363         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
364 }
365
366 void SituareEngine::onLogin()
367 {
368     qDebug() << __PRETTY_FUNCTION__;
369
370     m_ui->loggedIn(true);
371
372     m_situareService->fetchLocations();
373
374     if (m_gps->isRunning())
375         m_ui->readAutomaticLocationUpdateSettings();
376 }
377
378 void SituareEngine::onLogout()
379 {
380     qDebug() << __PRETTY_FUNCTION__;
381
382     m_ui->loggedIn(false);
383     m_situareService->updateSession(""); // empty session string means logged out
384     m_automaticUpdateFirstStart = true;
385 }
386
387 void SituareEngine::refreshUserData()
388 {
389     qDebug() << __PRETTY_FUNCTION__;
390
391     if (m_networkAccessManager->isConnected()) {
392         m_situareService->fetchLocations();
393     }
394     else {
395         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
396     }
397 }
398
399 void SituareEngine::requestAddress()
400 {
401     qDebug() << __PRETTY_FUNCTION__;
402
403     if (m_networkAccessManager->isConnected()) {
404         if (m_gps->isRunning())
405             m_situareService->reverseGeo(m_gps->lastPosition());
406         else
407             m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
408     }
409     else {
410         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
411     }
412 }
413
414 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
415 {
416     qDebug() << __PRETTY_FUNCTION__;
417
418     if (m_networkAccessManager->isConnected()) {
419
420         if (m_gps->isRunning())
421             m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
422         else
423             m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
424     }
425     else {
426         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
427     }
428 }
429
430 void SituareEngine::requestAutomaticUpdateIfMoved(GeoCoordinate position)
431 {
432     qDebug() << __PRETTY_FUNCTION__;
433
434     if ((fabs(m_lastUpdatedGPSPosition.longitude() - position.longitude()) >
435          USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
436         (fabs(m_lastUpdatedGPSPosition.latitude() - position.latitude()) >
437          USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
438
439         m_lastUpdatedGPSPosition = position;
440         m_userMoved = true;
441     }
442
443     if (m_automaticUpdateRequest && m_userMoved) {
444         requestUpdateLocation(tr("Automatic location update"));
445         m_automaticUpdateRequest = false;
446         m_userMoved = false;
447     }
448 }
449
450 void SituareEngine::requestInterestingPeople()
451 {
452     qDebug() << __PRETTY_FUNCTION__;
453
454     QRectF currentSceneRect = m_mapEngine->currentViewSceneRect();
455     GeoCoordinate centerGeoCoordinate = m_mapEngine->centerGeoCoordinate();
456     SceneCoordinate topCenterSceneCoordinate(
457                 currentSceneRect.left() - currentSceneRect.width() / 2, currentSceneRect.top());
458
459     m_situareService->fetchPeopleWithSimilarInterest(
460                 centerGeoCoordinate.distanceTo(GeoCoordinate(topCenterSceneCoordinate)));
461 }
462
463 void SituareEngine::requestSendMessage(const QString &receiverId, const QString &message,
464                                 bool addCoordinates)
465 {
466     qDebug() << __PRETTY_FUNCTION__;
467
468     if (addCoordinates)
469         m_situareService->sendMessage(receiverId, message, m_mapEngine->centerGeoCoordinate());
470     else
471         m_situareService->sendMessage(receiverId, message);
472 }
473
474 void SituareEngine::routeParsed(Route &route)
475 {
476     qDebug() << __PRETTY_FUNCTION__;
477
478     Q_UNUSED(route);
479 }
480
481 void SituareEngine::routeTo(const GeoCoordinate &endPointCoordinates)
482 {
483     qDebug() << __PRETTY_FUNCTION__;
484
485     if (m_gps->isRunning())
486         m_routingService->requestRoute(m_gps->lastPosition(), endPointCoordinates);
487     else
488         m_routingService->requestRoute(m_mapEngine->centerGeoCoordinate(), endPointCoordinates);
489 }
490
491 void SituareEngine::routeToCursor()
492 {
493     qDebug() << __PRETTY_FUNCTION__;
494
495     routeTo(m_mapEngine->centerGeoCoordinate());
496 }
497
498 void SituareEngine::setAutoCentering(bool enabled)
499 {
500     qDebug() << __PRETTY_FUNCTION__ << enabled;
501
502     m_ui->setIndicatorButtonEnabled(enabled);
503     m_mapEngine->setAutoCentering(enabled);
504     m_ui->setCrosshairVisibility(!enabled);
505
506     if (enabled) {
507         setGPS(true);
508         m_gps->requestLastPosition();
509     }
510 }
511
512 void SituareEngine::setFirstStartZoomLevel()
513 {
514     qDebug() << __PRETTY_FUNCTION__;
515
516     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
517         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
518
519     disconnect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
520                this, SLOT(setFirstStartZoomLevel()));
521 }
522
523 void SituareEngine::setGPS(bool enabled)
524 {
525     qDebug() << __PRETTY_FUNCTION__ << enabled;
526
527     if (m_gps->isInitialized()) {
528         m_ui->setGPSButtonEnabled(enabled);
529         m_mapEngine->setGPSEnabled(enabled);
530
531         if (enabled && !m_gps->isRunning()) {
532             m_gps->start();
533             m_gps->requestLastPosition();
534
535             if(m_facebookAuthenticator->isLoggedIn())
536                 m_ui->readAutomaticLocationUpdateSettings();
537         }
538         else if (!enabled && m_gps->isRunning()) {
539             m_gps->stop();
540             changeAutoCenteringSetting(false);
541             enableAutomaticLocationUpdate(false);
542         }
543     }
544     else {
545         if (enabled)
546             m_ui->buildInformationBox(tr("Unable to start GPS"));
547         m_ui->setGPSButtonEnabled(false);
548         m_mapEngine->setGPSEnabled(false);
549     }
550 }
551
552 void SituareEngine::setPowerSaving(bool enabled)
553 {
554     qDebug() << __PRETTY_FUNCTION__ << enabled;
555
556     m_gps->enablePowerSave(enabled);
557
558     if(m_autoCenteringEnabled)
559         m_mapEngine->setAutoCentering(!enabled);
560 }
561
562 void SituareEngine::setProgressIndicatorDisabled()
563 {
564     m_ui->toggleProgressIndicator(false);
565 }
566
567 void SituareEngine::setProgressIndicatorEnabled()
568 {
569     m_ui->toggleProgressIndicator(true);
570 }
571
572 void SituareEngine::showContactDialog(const QString &facebookId)
573 {
574     qDebug() << __PRETTY_FUNCTION__;
575
576     QString guid = m_contactManager->contactGuid(facebookId);
577
578     if (!guid.isEmpty())
579         m_ui->showContactDialog(guid);
580     else
581         m_ui->buildInformationBox(tr("Unable to find contact.\nAdd Facebook IM "
582                                      "account from Conversations to use this feature."), true);
583 }
584
585 void SituareEngine::showMessageDialog(const QPair<QString, QString> &receiver)
586 {
587     qDebug() << __PRETTY_FUNCTION__;
588
589     m_ui->showMessageDialog(receiver);
590 }
591
592 void SituareEngine::signalsFromFacebookAuthenticator()
593 {
594     qDebug() << __PRETTY_FUNCTION__;
595
596     connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
597             this, SLOT(error(int, int)));
598
599     connect(m_facebookAuthenticator, SIGNAL(loggedIn(QString, bool)),
600             m_situareService, SLOT(updateSession(QString)));
601
602     connect(m_facebookAuthenticator, SIGNAL(loggedIn(QString, bool)),
603             this, SLOT(onLogin()));
604
605     connect(m_facebookAuthenticator, SIGNAL(loggedOut()), this, SLOT(onLogout()));
606 }
607
608 void SituareEngine::signalsFromGeocodingService()
609 {
610     qDebug() << __PRETTY_FUNCTION__;
611
612     connect(m_geocodingService, SIGNAL(locationDataParsed(const QList<Location>&)),
613             m_ui, SIGNAL(locationDataParsed(const QList<Location>&)));
614
615     connect(m_geocodingService, SIGNAL(error(int, int)),
616             this, SLOT(error(int, int)));
617 }
618
619 void SituareEngine::signalsFromGPS()
620 {
621     qDebug() << __PRETTY_FUNCTION__;
622
623     connect(m_gps, SIGNAL(position(GeoCoordinate, qreal)),
624             m_mapEngine, SLOT(gpsPositionUpdate(GeoCoordinate, qreal)));
625
626     connect(m_gps, SIGNAL(timeout()),
627             m_ui, SLOT(gpsTimeout()));
628
629     connect(m_gps, SIGNAL(error(int, int)),
630             this, SLOT(error(int, int)));
631 }
632
633 void SituareEngine::signalsFromMainWindow()
634 {
635     qDebug() << __PRETTY_FUNCTION__;
636
637     connect(m_ui, SIGNAL(error(int, int)),
638             this, SLOT(error(int, int)));
639
640     connect(m_ui, SIGNAL(loginActionPressed()),
641             this, SLOT(loginActionPressed()));
642
643     // signals from map view
644     connect(m_ui, SIGNAL(mapViewScrolled(SceneCoordinate)),
645             m_mapEngine, SLOT(setCenterPosition(SceneCoordinate)));
646
647     connect(m_ui, SIGNAL(mapViewResized(QSize)),
648             m_mapEngine, SLOT(viewResized(QSize)));
649
650     connect(m_ui, SIGNAL(viewZoomFinished()),
651             m_mapEngine, SLOT(viewZoomFinished()));
652
653     // signals from zoom buttons (zoom panel and volume buttons)
654     connect(m_ui, SIGNAL(zoomIn()),
655             m_mapEngine, SLOT(zoomIn()));
656
657     connect(m_ui, SIGNAL(zoomOut()),
658             m_mapEngine, SLOT(zoomOut()));
659
660     // signals from menu buttons
661     connect(m_ui, SIGNAL(gpsTriggered(bool)),
662             this, SLOT(setGPS(bool)));
663
664     connect(m_ui, SIGNAL(requestReverseGeo()),
665             this, SLOT(requestAddress()));
666
667     connect(m_ui, SIGNAL(locationUpdate(QString,bool)),
668             this, SLOT(setProgressIndicatorEnabled()));
669
670     connect(m_ui, SIGNAL(locationUpdate(QString,bool)),
671             this, SLOT(requestUpdateLocation(QString,bool)));
672
673     connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
674             this, SLOT(enableAutomaticLocationUpdate(bool, int)));
675
676     connect(m_ui, SIGNAL(addTags(QStringList)),
677             this, SLOT(setProgressIndicatorEnabled()));
678
679     connect(m_ui, SIGNAL(addTags(QStringList)),
680             m_situareService, SLOT(addTags(QStringList)));
681
682     connect(m_ui, SIGNAL(removeTags(QStringList)),
683             this, SLOT(setProgressIndicatorEnabled()));
684
685     connect(m_ui, SIGNAL(removeTags(QStringList)),
686             m_situareService, SLOT(removeTags(QStringList)));
687
688     // signals from user info tab
689     connect(m_ui, SIGNAL(refreshUserData()),
690             this, SLOT(setProgressIndicatorEnabled()));
691
692     connect(m_ui, SIGNAL(refreshUserData()),
693             this, SLOT(refreshUserData()));
694
695     connect(m_ui, SIGNAL(centerToCoordinates(GeoCoordinate)),
696             m_mapEngine, SLOT(centerToCoordinates(GeoCoordinate)));
697
698     connect(m_ui, SIGNAL(requestPopularTags()),
699             this, SLOT(setProgressIndicatorEnabled()));
700
701     connect(m_ui, SIGNAL(requestPopularTags()),
702             m_situareService, SLOT(fetchPopularTags()));
703
704     // routing signal from friend list tab & search location tab
705     connect(m_ui, SIGNAL(routeTo(GeoCoordinate)),
706             this, SLOT(setProgressIndicatorEnabled()));
707
708     connect(m_ui, SIGNAL(routeTo(const GeoCoordinate&)),
709             this, SLOT(routeTo(const GeoCoordinate&)));
710
711     connect(m_ui, SIGNAL(requestContactDialog(const QString &)),
712             this, SLOT(showContactDialog(const QString &)));
713
714     // signals from location search panel
715     connect(m_ui,
716             SIGNAL(locationItemClicked(const GeoCoordinate&, const GeoCoordinate&)),
717             m_mapEngine,
718             SLOT(showMapArea(const GeoCoordinate&, const GeoCoordinate&)));
719
720     connect(m_ui, SIGNAL(searchHistoryItemClicked(QString)),
721             this, SLOT(locationSearch(QString)));
722
723     // signals from routing tab
724     connect(m_ui, SIGNAL(clearRoute()),
725             m_mapEngine, SLOT(clearRoute()));
726
727     connect(m_ui, SIGNAL(routeToCursor()),
728             this, SLOT(routeToCursor()));
729
730     // signals from distance indicator button
731     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
732             this, SLOT(changeAutoCenteringSetting(bool)));
733
734     connect(m_ui, SIGNAL(draggingModeTriggered()),
735             this, SLOT(draggingModeTriggered()));
736
737     // signal from search search dialogs
738     connect(m_ui, SIGNAL(searchForLocation(QString)),
739             this, SLOT(locationSearch(QString)));
740
741     connect(m_ui, SIGNAL(requestSearchPeopleByTag(QString)),
742             this, SLOT(setProgressIndicatorEnabled()));
743
744     connect(m_ui, SIGNAL(requestSearchPeopleByTag(QString)),
745             m_situareService, SLOT(searchPeopleByTag(QString)));
746
747     // signals from meet people panel
748     connect(m_ui, SIGNAL(requestInterestingPeople()),
749             this, SLOT(setProgressIndicatorEnabled()));
750
751     connect(m_ui, SIGNAL(requestInterestingPeople()),
752             this, SLOT(requestInterestingPeople()));
753
754     connect(m_ui, SIGNAL(requestMessageDialog(QPair<QString, QString>)),
755             this, SLOT(showMessageDialog(QPair<QString, QString>)));
756
757     connect(m_ui, SIGNAL(sendMessage(QString,QString,bool)),
758             this, SLOT(setProgressIndicatorEnabled()));
759
760     connect(m_ui, SIGNAL(sendMessage(QString,QString,bool)),
761             this, SLOT(requestSendMessage(QString,QString,bool)));
762
763     // signals from message panel
764     connect(m_ui, SIGNAL(requestMessages()),
765             this, SLOT(setProgressIndicatorEnabled()));
766
767     connect(m_ui, SIGNAL(requestMessages()),
768             m_situareService, SLOT(fetchMessages()));
769
770     connect(m_ui, SIGNAL(requestRemoveMessage(QString)),
771             this, SLOT(setProgressIndicatorEnabled()));
772
773     connect(m_ui, SIGNAL(requestRemoveMessage(QString)),
774             m_situareService, SLOT(removeMessage(QString)));
775 }
776
777 void SituareEngine::signalsFromMapEngine()
778 {
779     qDebug() << __PRETTY_FUNCTION__;
780
781     connect(m_mapEngine, SIGNAL(error(int, int)),
782             this, SLOT(error(int, int)));
783
784     connect(m_mapEngine, SIGNAL(locationChanged(SceneCoordinate)),
785             m_ui, SIGNAL(centerToSceneCoordinates(SceneCoordinate)));
786
787     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
788             m_ui, SIGNAL(zoomLevelChanged(int)));
789
790     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
791             this, SLOT(disableAutoCentering()));
792
793     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
794             m_ui, SIGNAL(maxZoomLevelReached()));
795
796     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
797             m_ui, SIGNAL(minZoomLevelReached()));
798
799     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
800             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
801
802     connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
803             m_ui, SIGNAL(newMapResolution(qreal)));
804
805     connect(m_mapEngine, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
806             m_ui, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)));
807 }
808
809 void SituareEngine::signalsFromRoutingService()
810 {
811     qDebug() << __PRETTY_FUNCTION__;
812
813     connect(m_routingService, SIGNAL(routeParsed(Route&)),
814             this, SLOT(setProgressIndicatorDisabled()));
815
816     connect(m_routingService, SIGNAL(routeParsed(Route&)),
817             m_mapEngine, SLOT(setRoute(Route&)));
818
819     connect(m_routingService, SIGNAL(routeParsed(Route&)),
820             m_ui, SIGNAL(routeParsed(Route&)));
821
822     connect(m_routingService, SIGNAL(error(int, int)),
823             this, SLOT(error(int, int)));
824 }
825
826 void SituareEngine::signalsFromSituareService()
827 {
828     qDebug() << __PRETTY_FUNCTION__;
829
830     connect(m_situareService, SIGNAL(error(int, int)),
831             this, SLOT(error(int, int)));
832
833     connect(m_situareService, SIGNAL(imageReady(QString,QPixmap)),
834             this, SLOT(imageReady(QString,QPixmap)));
835
836     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
837             m_ui, SIGNAL(reverseGeoReady(QString)));
838
839     connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)),
840             this, SLOT(setProgressIndicatorDisabled()));
841
842     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
843             this, SLOT(userDataChanged(User*, QList<User*>&)));
844
845     connect(m_situareService, SIGNAL(updateWasSuccessful(SituareService::SuccessfulMethod)),
846             this, SLOT(updateWasSuccessful(SituareService::SuccessfulMethod)));
847
848     connect(m_situareService, SIGNAL(interestingPeopleReceived(QList<User>&,QList<User>&)),
849             m_ui, SIGNAL(interestingPeopleReceived(QList<User>&,QList<User>&)));
850
851     connect(m_situareService, SIGNAL(interestingPeopleReceived(QList<User>&,QList<User>&)),
852             this, SLOT(setProgressIndicatorDisabled()));
853
854     connect(m_situareService, SIGNAL(messagesReceived(QList<Message>&, QList<Message> &)),
855             this, SLOT(setProgressIndicatorDisabled()));
856
857
858     connect(m_situareService, SIGNAL(messagesReceived(QList<Message>&, QList<Message> &)),
859             m_ui, SIGNAL(messagesReceived(QList<Message>&, QList<Message>&)));
860
861     connect(m_situareService, SIGNAL(popularTagsReceived(QHash<QString,QString>&)),
862             this, SLOT(setProgressIndicatorDisabled()));
863
864     connect(m_situareService, SIGNAL(popularTagsReceived(QHash<QString,QString>&)),
865             m_ui, SIGNAL(popularTagsReceived(QHash<QString,QString>&)));
866 }
867
868 void SituareEngine::startAutomaticUpdate()
869 {
870     qDebug() << __PRETTY_FUNCTION__;
871
872     m_gps->requestUpdate();
873     m_automaticUpdateRequest = true;
874 }
875
876 void SituareEngine::topmostWindowChanged(bool isMainWindow)
877 {
878     qDebug() << __PRETTY_FUNCTION__;
879
880     setPowerSaving(!isMainWindow);
881 }
882
883 void SituareEngine::updateWasSuccessful(SituareService::SuccessfulMethod successfulMethod)
884 {
885     qDebug() << __PRETTY_FUNCTION__;
886
887     if (m_networkAccessManager->isConnected()) {
888         if (successfulMethod == SituareService::SuccessfulUpdateLocation) {
889             m_situareService->fetchLocations();
890         } else  if ((successfulMethod == SituareService::SuccessfulRemoveMessage) ||
891                     (successfulMethod == SituareService::SuccessfulSendMessage)) {
892             m_situareService->fetchMessages();
893         } else if ((successfulMethod == SituareService::SuccessfulAddTags) ||
894                    (successfulMethod == SituareService::SuccessfulRemoveTags)) {
895             m_situareService->fetchLocations();
896         }
897     } else {
898         error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
899     }
900 }
901
902 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
903 {
904     qDebug() << __PRETTY_FUNCTION__;
905
906     emit userLocationReady(user);
907     emit friendsLocationsReady(friendsList);
908 }