Kaj Wallin - kaj.wallin@ixonos.com
Henri Lampela - henri.lampela@ixonos.com
+ Jussi Laitinen jussi.laitinen@ixonos.com
+ Sami Rämö - sami.ramo@ixonos.com
Situare is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
USA.
*/
-#include "engine.h"
+
+#include "common.h"
+#include "facebookservice/facebookauthentication.h"
+#include "gps/gpsposition.h"
+#include "map/mapengine.h"
+#include "situareservice/situareservice.h"
#include "ui/mainwindow.h"
+#include <cmath>
+
+#include "engine.h"
+
+const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
+const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
SituareEngine::SituareEngine(QMainWindow *parent)
- : QObject(parent)
+ : QObject(parent),
+ m_autoCenteringEnabled(false),
+ m_loggedIn(false),
+ m_automaticUpdateIntervalTimer(0),
+ m_lastUpdatedGPSPosition(QPointF()),
+ m_userMoved(false)
{
+ qDebug() << __PRETTY_FUNCTION__;
m_ui = new MainWindow;
+ m_ui->showPanels(m_loggedIn);
- m_networkManager = new QNetworkAccessManager;
- m_situareService = new SituareService(this, m_networkManager);
+ // build MapEngine
+ m_mapEngine = new MapEngine(this);
+ m_ui->setMapViewScene(m_mapEngine->scene());
- m_loggedIn = false;
- m_facebookAuthenticator = new FacebookAuthentication();
+ // build GPS
+ m_gps = new GPSPosition(this);
+ m_gps->setMode(GPSPosition::Default);
- connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
- m_situareService, SLOT(credentialsReady(FacebookCredentials)));
- connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
- this, SLOT(loginOk()));
+ // build SituareService
+ m_situareService = new SituareService(this);
+
+ // build FacebookAuthenticator
+ m_facebookAuthenticator = new FacebookAuthentication(this);
+
+ // connect signals
+ signalsFromMapEngine();
+ signalsFromGPS();
+ signalsFromSituareService();
+ signalsFromMainWindow();
+ signalsFromFacebookAuthenticator();
- connect(m_ui, SIGNAL(requestReverseGeo()), this, SLOT(requestAddress()));
- connect(m_situareService, SIGNAL(reverseGeoReady(QString)), m_ui, SIGNAL(reverseGeoReady(QString)));
- connect(m_ui, SIGNAL(statusUpdate(QString,bool)), this, SLOT(requestUpdateLocation(QString,bool)));
- connect(m_situareService, SIGNAL(error(QString)), this, SLOT(error(QString)));
+ connect(this, SIGNAL(userLocationReady(User*)),
+ m_ui, SIGNAL(userLocationReady(User*)));
- connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)), m_ui,
- SIGNAL(userDataChanged(User*,QList<User*>&)));
+ connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
+ m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
- connect(m_ui, SIGNAL(refreshUserData()), this, SLOT(refreshUserData()));
+ connect(this, SIGNAL(userLocationReady(User*)),
+ m_mapEngine, SLOT(receiveOwnLocation(User*)));
- start();
+ connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
+ m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
+
+ // signals connected, now it's time to show the main window
+ // but init the MapEngine before so starting location is set
+ m_mapEngine->init();
+ m_ui->show();
+
+ QSettings settings(DIRECTORY_NAME, FILE_NAME);
+ QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
+ QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
+
+ // set features on / off based on settings
+ changeAutoCenteringSetting(autoCenteringEnabled.toBool());
+ enableGPS(gpsEnabled.toBool());
+
+ // show messages at startup if features are enabled automatically
+ if (gpsEnabled.toBool())
+ m_ui->showMaemoInformationBox(tr("GPS enabled"));
+ if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
+ m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
+
+ m_facebookAuthenticator->start();
+
+ m_automaticUpdateIntervalTimer = new QTimer(this);
+ connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
+ this, SLOT(automaticUpdateIntervalTimerTimeout()));
}
SituareEngine::~SituareEngine()
{
qDebug() << __PRETTY_FUNCTION__;
+
delete m_ui;
- delete m_facebookAuthenticator;
+
+ QSettings settings(DIRECTORY_NAME, FILE_NAME);
+ qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
+ settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
+ settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
+}
+
+void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_automaticUpdateIntervalTimer) {
+
+ if (enabled && m_gps->isRunning()) {
+ m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
+ m_automaticUpdateIntervalTimer->start();
+ }
+ else
+ m_automaticUpdateIntervalTimer->stop();
+ }
+}
+
+void SituareEngine::automaticUpdateIntervalTimerTimeout()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_gps->isRunning() && m_userMoved) {
+ qDebug() << __PRETTY_FUNCTION__ << "requestUpdateLocation()";
+ //requestUpdateLocation();
+ m_userMoved = false;
+ }
+}
+
+void SituareEngine::changeAutoCenteringSetting(bool enabled)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_autoCenteringEnabled = enabled;
+ enableAutoCentering(enabled);
+}
+
+void SituareEngine::disableAutoCentering()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ changeAutoCenteringSetting(false);
+ m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
+}
+
+void SituareEngine::enableAutoCentering(bool enabled)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_ui->setAutoCenteringButtonEnabled(enabled);
+ m_mapEngine->setAutoCentering(enabled);
+
+ if (enabled)
+ m_gps->requestLastPosition();
+}
+
+void SituareEngine::enableGPS(bool enabled)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_ui->setGPSButtonEnabled(enabled);
+ m_mapEngine->setGPSEnabled(enabled);
+
+ if (enabled) {
+ m_gps->start();
+ enableAutoCentering(m_autoCenteringEnabled);
+ m_gps->requestLastPosition();
+ }
+ else {
+ m_gps->stop();
+ enableAutoCentering(false);
+ enableAutomaticLocationUpdate(false);
+ }
}
void SituareEngine::error(const QString &error)
// ToDo: signal UI?
}
-void SituareEngine::start()
+void SituareEngine::fetchUsernameFromSettings()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+ m_ui->setUsername(m_facebookAuthenticator->loadUsername());
+}
+
+void SituareEngine::invalidCredentials()
{
qDebug() << __PRETTY_FUNCTION__;
+ m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
m_facebookAuthenticator->start();
}
-void SituareEngine::loginOk()
+bool SituareEngine::isUserMoved()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ return m_userMoved;
+}
+
+void SituareEngine::loginActionPressed()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if(m_loggedIn) {
+ logout();
+ m_situareService->clearUserData();
+ }
+ else {
+ m_facebookAuthenticator->start();
+ }
+}
+
+void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
{
qDebug() << __PRETTY_FUNCTION__;
m_loggedIn = true;
- m_facebookAuthenticator->hide();
+ m_ui->loggedIn(m_loggedIn);
+
+ if(freshLogin) {
+ m_facebookAuthenticator->saveUsername(m_ui->username());
+ }
m_ui->show();
+ m_situareService->credentialsReady(credentials);
m_situareService->fetchLocations(); // request user locations
}
-void SituareEngine::requestAddress()
+void SituareEngine::loginProcessCancelled()
{
qDebug() << __PRETTY_FUNCTION__;
- QPointF coordinates(65, 25.5); // this will be get from somewhere, map etc...
- m_situareService->reverseGeo(coordinates);
+ m_ui->toggleProgressIndicator(false);
+ m_ui->showPanels(m_loggedIn);
}
-void SituareEngine::requestUpdateLocation(const QString &status, const bool &publish)
+void SituareEngine::logout()
{
qDebug() << __PRETTY_FUNCTION__;
- QPointF coordinates(65, 25.5); // this will be get from somewhere, map etc...
- m_situareService->updateLocation(coordinates, status, publish);
+ m_loggedIn = false;
+ m_ui->loggedIn(m_loggedIn);
+ m_facebookAuthenticator->clearAccountInformation();
}
void SituareEngine::refreshUserData()
{
qDebug() << __PRETTY_FUNCTION__;
+ m_ui->toggleProgressIndicator(true);
+
+ m_situareService->fetchLocations();
+}
+
+void SituareEngine::requestAddress()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (m_gps->isRunning())
+ m_situareService->reverseGeo(m_gps->lastPosition());
+ else
+ m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
+}
+
+void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_ui->toggleProgressIndicator(true);
+
+ if (m_gps->isRunning())
+ m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
+ else
+ m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
+}
+
+void SituareEngine::saveGPSPosition(QPointF position)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ qDebug() << __PRETTY_FUNCTION__ << m_lastUpdatedGPSPosition.x() << m_lastUpdatedGPSPosition.y();
+ qDebug() << __PRETTY_FUNCTION__ << position.x() << position.y();
+ qDebug() << __PRETTY_FUNCTION__ << "=" << fabs(m_lastUpdatedGPSPosition.x() - position.x()) <<
+ fabs(m_lastUpdatedGPSPosition.y() - position.y());
+
+ if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) > 0.003) ||
+ (fabs(m_lastUpdatedGPSPosition.y() - position.y()) > 0.001)) {
+ qDebug() << __PRETTY_FUNCTION__ << "m_userMoved = true";
+ m_lastUpdatedGPSPosition = position;
+ m_userMoved = true;
+ }
+}
+
+void SituareEngine::signalsFromFacebookAuthenticator()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
+ m_situareService, SLOT(credentialsReady(FacebookCredentials)));
+
+ connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
+ this, SLOT(loginOk(bool, FacebookCredentials)));
+
+ connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
+ m_ui, SLOT(startLoginProcess(QUrl)));
+
+ connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
+ m_ui, SLOT(loginFailed()));
+}
+
+void SituareEngine::signalsFromGPS()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ connect(m_gps, SIGNAL(position(QPointF,qreal)),
+ m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
+
+ connect(m_gps, SIGNAL(timeout()),
+ m_ui, SLOT(gpsTimeout()));
+
+ connect(m_gps, SIGNAL(error(QString)),
+ m_ui, SLOT(gpsError(QString)));
+
+ connect(m_gps, SIGNAL(position(QPointF,qreal)),
+ this, SLOT(saveGPSPosition(QPointF)));
+}
+
+void SituareEngine::signalsFromMainWindow()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ connect(m_ui, SIGNAL(loginActionPressed()),
+ this, SLOT(loginActionPressed()));
+
+ connect(m_ui, SIGNAL(updateCredentials(QUrl)),
+ m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
+
+ connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
+ this, SLOT(fetchUsernameFromSettings()));
+
+ // signals from map view
+ connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
+ m_mapEngine, SLOT(setLocation(QPoint)));
+
+ connect(m_ui, SIGNAL(mapViewResized(QSize)),
+ m_mapEngine, SLOT(viewResized(QSize)));
+
+ connect(m_ui, SIGNAL(viewZoomFinished()),
+ m_mapEngine, SLOT(viewZoomFinished()));
+
+ // signals from zoom buttons (zoom panel and volume buttons)
+ connect(m_ui, SIGNAL(zoomIn()),
+ m_mapEngine, SLOT(zoomIn()));
+
+ connect(m_ui, SIGNAL(zoomOut()),
+ m_mapEngine, SLOT(zoomOut()));
+
+ // signals from menu buttons
+ connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
+ this, SLOT(changeAutoCenteringSetting(bool)));
+
+ connect(m_ui, SIGNAL(gpsTriggered(bool)),
+ this, SLOT(enableGPS(bool)));
+
+ //signals from dialogs
+ connect(m_ui, SIGNAL(cancelLoginProcess()),
+ this, SLOT(loginProcessCancelled()));
+
+ connect(m_ui, SIGNAL(requestReverseGeo()),
+ this, SLOT(requestAddress()));
+
+ connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
+ this, SLOT(requestUpdateLocation(QString,bool)));
+
+ connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
+ this, SLOT(enableAutomaticLocationUpdate(bool, int)));
+
+ // signals from user info tab
+ connect(m_ui, SIGNAL(refreshUserData()),
+ this, SLOT(refreshUserData()));
+
+ // signals from friend list tab
+ connect(m_ui, SIGNAL(findFriend(QPointF)),
+ m_mapEngine, SLOT(setViewLocation(QPointF)));
+}
+
+void SituareEngine::signalsFromMapEngine()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
+ m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
+
+ connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
+ m_ui, SIGNAL(zoomLevelChanged(int)));
+
+ connect(m_mapEngine, SIGNAL(mapScrolledManually()),
+ this, SLOT(disableAutoCentering()));
+
+ connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
+ m_ui, SIGNAL(maxZoomLevelReached()));
+
+ connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
+ m_ui, SIGNAL(minZoomLevelReached()));
+
+ connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
+ m_ui, SIGNAL(locationItemClicked(QList<QString>)));
+}
+
+void SituareEngine::signalsFromSituareService()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ connect(m_situareService, SIGNAL(error(QString)),
+ this, SLOT(error(QString)));
+
+ connect(m_situareService, SIGNAL(invalidSessionCredentials()),
+ this, SLOT(invalidCredentials()));
+
+ connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
+ m_ui, SIGNAL(reverseGeoReady(QString)));
+
+ connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
+ this, SLOT(userDataChanged(User*, QList<User*>&)));
+
+ connect(m_situareService, SIGNAL(updateWasSuccessful()),
+ this, SLOT(updateWasSuccessful()));
+}
+
+void SituareEngine::updateWasSuccessful()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
m_situareService->fetchLocations();
}
+
+void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_ui->toggleProgressIndicator(false);
+
+ emit userLocationReady(user);
+ emit friendsLocationsReady(friendsList);
+}