2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
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
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.
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.
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,
25 #include <QMessageBox>
26 #include <QNetworkReply>
29 #include "facebookservice/facebookauthentication.h"
30 #include "gps/gpsposition.h"
31 #include "map/mapengine.h"
32 #include "situareservice/situareservice.h"
33 #include "ui/mainwindow.h"
39 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
40 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
41 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12; ///< Default zoom level when GPS available
42 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
43 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
44 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
46 SituareEngine::SituareEngine(QMainWindow *parent)
48 m_autoCenteringEnabled(false),
49 m_automaticUpdateFirstStart(true),
50 m_automaticUpdateRequest(false),
52 m_automaticUpdateIntervalTimer(0),
53 m_lastUpdatedGPSPosition(QPointF())
55 qDebug() << __PRETTY_FUNCTION__;
56 m_ui = new MainWindow;
57 m_ui->updateItemVisibility();
60 m_mapEngine = new MapEngine(this);
61 m_ui->setMapViewScene(m_mapEngine->scene());
64 m_gps = new GPSPosition(this);
66 // build SituareService
67 m_situareService = new SituareService(this);
69 // build FacebookAuthenticator
70 m_facebookAuthenticator = new FacebookAuthentication(this);
73 signalsFromMapEngine();
75 signalsFromSituareService();
76 signalsFromMainWindow();
77 signalsFromFacebookAuthenticator();
79 connect(this, SIGNAL(userLocationReady(User*)),
80 m_ui, SIGNAL(userLocationReady(User*)));
82 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
83 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
85 connect(this, SIGNAL(userLocationReady(User*)),
86 m_mapEngine, SLOT(receiveOwnLocation(User*)));
88 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
89 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
91 m_automaticUpdateIntervalTimer = new QTimer(this);
92 connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
93 this, SLOT(startAutomaticUpdate()));
95 // signals connected, now it's time to show the main window
96 // but init the MapEngine before so starting location is set
100 m_facebookAuthenticator->start();
102 m_gps->setMode(GPSPosition::Default);
103 initializeGpsAndAutocentering();
105 m_mce = new MCE(this);
106 connect(m_mce, SIGNAL(displayStateChanged(bool)), this, SLOT(displayStateChanged(bool)));
109 SituareEngine::~SituareEngine()
111 qDebug() << __PRETTY_FUNCTION__;
115 QSettings settings(DIRECTORY_NAME, FILE_NAME);
116 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
117 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
120 void SituareEngine::changeAutoCenteringSetting(bool enabled)
122 qDebug() << __PRETTY_FUNCTION__;
124 m_autoCenteringEnabled = enabled;
125 enableAutoCentering(enabled);
128 void SituareEngine::disableAutoCentering()
130 qDebug() << __PRETTY_FUNCTION__;
132 changeAutoCenteringSetting(false);
133 m_ui->buildInformationBox(tr("Auto centering disabled"));
136 void SituareEngine::displayStateChanged(bool enabled)
138 qDebug() << __PRETTY_FUNCTION__;
140 m_gps->enablePowerSave(!enabled);
142 if (m_autoCenteringEnabled)
143 enableAutoCentering(enabled);
146 void SituareEngine::enableAutoCentering(bool enabled)
148 qDebug() << __PRETTY_FUNCTION__;
150 m_ui->setAutoCenteringButtonEnabled(enabled);
151 m_mapEngine->setAutoCentering(enabled);
154 m_gps->requestLastPosition();
157 void SituareEngine::enableGPS(bool enabled)
159 qDebug() << __PRETTY_FUNCTION__;
161 m_ui->setOwnLocationCrosshairVisibility(!enabled);
163 if (m_gps->isInitialized()) {
164 m_ui->setGPSButtonEnabled(enabled);
165 m_mapEngine->setGPSEnabled(enabled);
167 if (enabled && !m_gps->isRunning()) {
169 enableAutoCentering(m_autoCenteringEnabled);
170 m_gps->requestLastPosition();
172 if(m_ui->loginState())
173 m_ui->readAutomaticLocationUpdateSettings();
175 else if (!enabled && m_gps->isRunning()) {
177 enableAutoCentering(false);
178 enableAutomaticLocationUpdate(false);
183 m_ui->buildInformationBox(tr("Unable to start GPS"));
184 m_ui->setGPSButtonEnabled(false);
185 m_mapEngine->setGPSEnabled(false);
189 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
191 qDebug() << __PRETTY_FUNCTION__;
193 //Show automatic update confirmation dialog
194 if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
195 m_ui->showEnableAutomaticUpdateLocationDialog(
196 tr("Do you want to enable automatic location update with %1 min update interval?")
197 .arg(updateIntervalMsecs/1000/60));
198 m_automaticUpdateFirstStart = false;
200 if (enabled && m_gps->isRunning()) {
201 m_ui->buildInformationBox(tr("Automatic location update enabled"));
202 if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
203 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
205 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
207 connect(m_gps, SIGNAL(position(QPointF,qreal)),
208 this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
210 m_automaticUpdateIntervalTimer->start();
213 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
214 this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
216 m_automaticUpdateIntervalTimer->stop();
221 void SituareEngine::error(const int context, const int error)
223 qDebug() << __PRETTY_FUNCTION__;
227 case SituareError::ERROR_GENERAL:
228 if(context == ErrorContext::SITUARE) {
229 m_ui->toggleProgressIndicator(false);
230 m_ui->buildInformationBox(tr("Unknown server error"), true);
233 case SituareError::ERROR_MISSING_ARGUMENT | QNetworkReply::ConnectionRefusedError :
234 m_ui->toggleProgressIndicator(false);
235 if(context == ErrorContext::SITUARE) {
236 m_ui->buildInformationBox(tr("Missing argument from request"), true);
237 } else if(context == ErrorContext::NETWORK) {
238 m_ui->buildInformationBox(tr("Connection refused by the server"), true);
241 case QNetworkReply::RemoteHostClosedError:
242 if(context == ErrorContext::NETWORK) {
243 m_ui->toggleProgressIndicator(false);
244 m_ui->buildInformationBox(tr("Connection closed by the server"), true);
247 case QNetworkReply::HostNotFoundError:
248 if(context == ErrorContext::NETWORK) {
249 m_ui->toggleProgressIndicator(false);
250 m_ui->buildInformationBox(tr("Remote server not found"), true);
253 case QNetworkReply::TimeoutError:
254 if(context == ErrorContext::NETWORK) {
255 m_ui->toggleProgressIndicator(false);
256 m_ui->buildInformationBox(tr("Connection timed out"), true);
259 case SituareError::SESSION_EXPIRED:
260 m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
261 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
262 m_situareService->clearUserData();
263 m_ui->loggedIn(false);
266 case SituareError::LOGIN_FAILED:
267 m_ui->toggleProgressIndicator(false);
268 m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
271 case SituareError::UPDATE_FAILED:
272 m_ui->toggleProgressIndicator(false);
273 m_ui->buildInformationBox(tr("Update failed, please try again"), true);
275 case SituareError::DATA_RETRIEVAL_FAILED:
276 m_ui->toggleProgressIndicator(false);
277 m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
279 case SituareError::ADDRESS_RETRIEVAL_FAILED:
280 m_ui->buildInformationBox(tr("Address retrieval failed"), true);
282 case SituareError::IMAGE_DOWNLOAD_FAILED:
283 m_ui->buildInformationBox(tr("Image download failed"), true);
285 case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
286 m_ui->buildInformationBox(tr("Map image download failed"), true);
288 case SituareError::GPS_INITIALIZATION_FAILED:
290 m_ui->buildInformationBox(tr("GPS initialization failed"), true);
292 case SituareError::INVALID_JSON:
293 m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
294 m_ui->loggedIn(false);
295 m_facebookAuthenticator->clearAccountInformation(false); // clean all
297 case SituareError::ERROR_GEOLOCATION_SERVER_UNAVAILABLE:
298 m_ui->toggleProgressIndicator(false);
299 m_ui->buildInformationBox(tr("Geolocation server not responding"), true);
301 case SituareError::ERROR_GEOLOCATION_REQUEST_FAIL:
302 m_ui->toggleProgressIndicator(false);
303 m_ui->buildInformationBox(tr("Geolocation request failed, please try again"), true);
305 case SituareError::ERROR_GEOLOCATION_LONLAT_INVALID:
306 m_ui->toggleProgressIndicator(false);
307 m_ui->buildInformationBox(tr("Invalid lat/lon value, please try again"), true);
310 m_ui->toggleProgressIndicator(false);
311 if(context == ErrorContext::NETWORK)
312 qCritical() << "QNetworkReply::NetworkError: " << error;
314 qCritical() << "Unknown error: " << error;
320 void SituareEngine::fetchUsernameFromSettings()
322 qDebug() << __PRETTY_FUNCTION__;
324 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
327 void SituareEngine::initializeGpsAndAutocentering()
329 qDebug() << __PRETTY_FUNCTION__;
331 QSettings settings(DIRECTORY_NAME, FILE_NAME);
332 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
333 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
335 if (m_gps->isInitialized()) {
337 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
339 connect(m_gps, SIGNAL(position(QPointF,qreal)),
340 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
342 changeAutoCenteringSetting(true);
345 m_ui->buildInformationBox(tr("GPS enabled"));
346 m_ui->buildInformationBox(tr("Auto centering enabled"));
348 } else { // Normal start
349 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
350 enableGPS(gpsEnabled.toBool());
352 if (gpsEnabled.toBool())
353 m_ui->buildInformationBox(tr("GPS enabled"));
355 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
356 m_ui->buildInformationBox(tr("Auto centering enabled"));
363 void SituareEngine::loginActionPressed()
365 qDebug() << __PRETTY_FUNCTION__;
367 if(m_ui->loginState()) {
369 m_situareService->clearUserData();
371 m_facebookAuthenticator->start();
375 void SituareEngine::loginOk()
377 qDebug() << __PRETTY_FUNCTION__;
379 m_ui->loggedIn(true);
382 m_situareService->fetchLocations(); // request user locations
384 if (m_gps->isRunning())
385 m_ui->readAutomaticLocationUpdateSettings();
388 void SituareEngine::loginProcessCancelled()
390 qDebug() << __PRETTY_FUNCTION__;
392 m_ui->toggleProgressIndicator(false);
393 m_ui->updateItemVisibility();
396 void SituareEngine::logout()
398 qDebug() << __PRETTY_FUNCTION__;
400 m_ui->loggedIn(false);
402 // signal to clear locationUpdateDialog's data
403 connect(this, SIGNAL(clearUpdateLocationDialogData()),
404 m_ui, SIGNAL(clearUpdateLocationDialogData()));
405 emit clearUpdateLocationDialogData();
407 m_facebookAuthenticator->clearAccountInformation(); // clear all
408 m_automaticUpdateFirstStart = true;
411 void SituareEngine::refreshUserData()
413 qDebug() << __PRETTY_FUNCTION__;
415 m_ui->toggleProgressIndicator(true);
417 m_situareService->fetchLocations();
420 void SituareEngine::requestAddress()
422 qDebug() << __PRETTY_FUNCTION__;
424 if (m_gps->isRunning())
425 m_situareService->reverseGeo(m_gps->lastPosition());
427 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
430 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
432 qDebug() << __PRETTY_FUNCTION__;
434 m_ui->toggleProgressIndicator(true);
436 if (m_gps->isRunning())
437 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
439 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
442 void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
444 qDebug() << __PRETTY_FUNCTION__;
446 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
447 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
448 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
449 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
451 m_lastUpdatedGPSPosition = position;
455 if (m_automaticUpdateRequest && m_userMoved) {
456 requestUpdateLocation(tr("Automatic location update."));
457 m_automaticUpdateRequest = false;
462 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
464 qDebug() << __PRETTY_FUNCTION__;
466 Q_UNUSED(latLonCoordinate);
469 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
470 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
472 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
473 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
476 void SituareEngine::signalsFromFacebookAuthenticator()
478 qDebug() << __PRETTY_FUNCTION__;
480 connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
481 this, SLOT(error(int, int)));
483 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
484 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
486 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
487 this, SLOT(loginOk()));
489 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
490 m_ui, SLOT(startLoginProcess()));
492 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
493 m_ui, SLOT(saveCookies()));
495 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
496 m_ui, SLOT(loginUsingCookies()));
499 void SituareEngine::signalsFromGPS()
501 qDebug() << __PRETTY_FUNCTION__;
503 connect(m_gps, SIGNAL(position(QPointF,qreal)),
504 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
506 connect(m_gps, SIGNAL(timeout()),
507 m_ui, SLOT(gpsTimeout()));
509 connect(m_gps, SIGNAL(error(int, int)),
510 this, SLOT(error(int, int)));
513 void SituareEngine::signalsFromMainWindow()
515 qDebug() << __PRETTY_FUNCTION__;
517 connect(m_ui, SIGNAL(error(int, int)),
518 this, SLOT(error(int, int)));
520 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
521 this, SLOT(fetchUsernameFromSettings()));
523 connect(m_ui, SIGNAL(loginActionPressed()),
524 this, SLOT(loginActionPressed()));
526 connect(m_ui, SIGNAL(saveUsername(QString)),
527 m_facebookAuthenticator, SLOT(saveUsername(QString)));
529 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
530 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
532 // signals from map view
533 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
534 m_mapEngine, SLOT(setCenterPosition(QPoint)));
536 connect(m_ui, SIGNAL(mapViewResized(QSize)),
537 m_mapEngine, SLOT(viewResized(QSize)));
539 connect(m_ui, SIGNAL(viewZoomFinished()),
540 m_mapEngine, SLOT(viewZoomFinished()));
542 // signals from zoom buttons (zoom panel and volume buttons)
543 connect(m_ui, SIGNAL(zoomIn()),
544 m_mapEngine, SLOT(zoomIn()));
546 connect(m_ui, SIGNAL(zoomOut()),
547 m_mapEngine, SLOT(zoomOut()));
549 // signals from menu buttons
550 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
551 this, SLOT(changeAutoCenteringSetting(bool)));
553 connect(m_ui, SIGNAL(gpsTriggered(bool)),
554 this, SLOT(enableGPS(bool)));
556 //signals from dialogs
557 connect(m_ui, SIGNAL(cancelLoginProcess()),
558 this, SLOT(loginProcessCancelled()));
560 connect(m_ui, SIGNAL(requestReverseGeo()),
561 this, SLOT(requestAddress()));
563 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
564 this, SLOT(requestUpdateLocation(QString,bool)));
566 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
567 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
569 // signals from user info tab
570 connect(m_ui, SIGNAL(refreshUserData()),
571 this, SLOT(refreshUserData()));
573 connect(m_ui, SIGNAL(findUser(QPointF)),
574 m_mapEngine, SLOT(centerToCoordinates(QPointF)));
576 // signals from friend list tab
577 connect(m_ui, SIGNAL(findFriend(QPointF)),
578 m_mapEngine, SLOT(centerToCoordinates(QPointF)));
581 void SituareEngine::signalsFromMapEngine()
583 qDebug() << __PRETTY_FUNCTION__;
585 connect(m_mapEngine, SIGNAL(error(int, int)),
586 this, SLOT(error(int, int)));
588 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
589 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
591 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
592 m_ui, SIGNAL(zoomLevelChanged(int)));
594 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
595 this, SLOT(disableAutoCentering()));
597 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
598 m_ui, SIGNAL(maxZoomLevelReached()));
600 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
601 m_ui, SIGNAL(minZoomLevelReached()));
603 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
604 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
606 connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
607 m_ui, SIGNAL(newMapResolution(qreal)));
610 void SituareEngine::signalsFromSituareService()
612 qDebug() << __PRETTY_FUNCTION__;
614 connect(m_situareService, SIGNAL(error(int, int)),
615 this, SLOT(error(int, int)));
617 connect(m_situareService, SIGNAL(error(int, int)),
618 m_ui, SIGNAL(messageSendingFailed(int)));
620 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
621 m_ui, SIGNAL(reverseGeoReady(QString)));
623 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
624 this, SLOT(userDataChanged(User*, QList<User*>&)));
626 connect(m_situareService, SIGNAL(updateWasSuccessful()),
627 this, SLOT(updateWasSuccessful()));
629 connect(m_situareService, SIGNAL(updateWasSuccessful()),
630 m_ui, SIGNAL(clearUpdateLocationDialogData()));
633 void SituareEngine::startAutomaticUpdate()
635 qDebug() << __PRETTY_FUNCTION__;
637 m_gps->requestUpdate();
638 m_automaticUpdateRequest = true;
641 void SituareEngine::updateWasSuccessful()
643 qDebug() << __PRETTY_FUNCTION__;
645 m_situareService->fetchLocations();
648 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
650 qDebug() << __PRETTY_FUNCTION__;
652 m_ui->toggleProgressIndicator(false);
655 emit userLocationReady(user);
656 emit friendsLocationsReady(friendsList);