#include "common.h"
#include "facebookservice/facebookauthentication.h"
-#include "gps/gpspositioninterface.h"
-
-#ifdef Q_WS_MAEMO_5
#include "gps/gpsposition.h"
-#else
-#include "gps/gpspositionmockup.h"
-#endif
-
#include "map/mapengine.h"
#include "situareservice/situareservice.h"
#include "ui/mainwindow.h"
+#include <cmath>
#include "engine.h"
SituareEngine::SituareEngine(QMainWindow *parent)
: QObject(parent),
- m_autoCenteringEnabled(false)
+ 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);
// build MapEngine
m_mapEngine = new MapEngine(this);
m_ui->setMapViewScene(m_mapEngine->scene());
// build GPS
-#ifdef Q_WS_MAEMO_5
m_gps = new GPSPosition(this);
-#else
- m_gps = new GPSPositionMockup(this);
-#endif
- m_gps->setMode(GPSPositionInterface::Default);
+ m_gps->setMode(GPSPosition::Default);
// build SituareService
m_situareService = new SituareService(this);
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()
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__;
else {
m_gps->stop();
enableAutoCentering(false);
+ enableAutomaticLocationUpdate(false);
}
}
// ToDo: signal UI?
}
-void SituareEngine::loginOk()
+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();
+}
+
+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_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
}
qDebug() << __PRETTY_FUNCTION__;
m_ui->toggleProgressIndicator(false);
- //ToDo: do something
+ m_ui->showPanels(m_loggedIn);
+}
+
+void SituareEngine::logout()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_loggedIn = false;
+ m_ui->loggedIn(m_loggedIn);
+ m_facebookAuthenticator->clearAccountInformation();
}
void SituareEngine::refreshUserData()
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(credentialsReady(FacebookCredentials)),
+ connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
m_situareService, SLOT(credentialsReady(FacebookCredentials)));
- connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
- this, SLOT(loginOk()));
+ 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_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__;
+ 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(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()));
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(error(QString)),
- this, SLOT(error(QString)));
-
connect(m_situareService, SIGNAL(updateWasSuccessful()),
this, SLOT(updateWasSuccessful()));
}