USA.
*/
+#include <QMessageBox>
#include "common.h"
#include "facebookservice/facebookauthentication.h"
#include "map/mapengine.h"
#include "situareservice/situareservice.h"
#include "ui/mainwindow.h"
-#include "networkhandler.h"
+#include <cmath>
#include "engine.h"
-const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
-const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
+const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
+const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
+const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12; ///< Default zoom level when GPS available
+const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
+const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
SituareEngine::SituareEngine(QMainWindow *parent)
: QObject(parent),
- m_autoCenteringEnabled(false)
+ m_autoCenteringEnabled(false),
+ m_automaticUpdateFirstStart(true),
+ m_loggedIn(false),
+ m_automaticUpdateIntervalTimer(0),
+ m_lastUpdatedGPSPosition(QPointF()),
+ m_userMoved(false)
{
qDebug() << __PRETTY_FUNCTION__;
m_ui = new MainWindow;
-
- // build NetworkHandler
- m_networkHandler = new NetworkHandler(this);
+ m_ui->updateItemVisibility(m_loggedIn);
// build MapEngine
m_mapEngine = new MapEngine(this);
m_facebookAuthenticator = new FacebookAuthentication(this);
// connect signals
- signalsFromNetworkHandler();
signalsFromMapEngine();
signalsFromGPS();
signalsFromSituareService();
connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
+ initializeGpsAndAutocentering();
+
// 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_mapEngine->init();
+ m_ui->show();
- //Request network state.
-// m_networkHandler->connect();
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::changeAutoCenteringSetting(bool enabled)
+void SituareEngine::automaticUpdateIntervalTimerTimeout()
{
- qDebug() << __PRETTY_FUNCTION__;
+ qWarning() << __PRETTY_FUNCTION__;
- m_autoCenteringEnabled = enabled;
- enableAutoCentering(enabled);
+ if (m_gps->isRunning() && m_userMoved) {
+// requestUpdateLocation();
+ qWarning() << __PRETTY_FUNCTION__ << "requestUpdateLocation()";
+ m_userMoved = false;
+ }
}
-void SituareEngine::connectedToNetwork()
+void SituareEngine::changeAutoCenteringSetting(bool enabled)
{
qDebug() << __PRETTY_FUNCTION__;
- disconnect(m_networkHandler, SIGNAL(connected()),
- this, SLOT(connectedToNetwork()));
-
- disconnect(m_networkHandler, SIGNAL(disconnected()),
- this, SLOT(disconnectedFromNetwork()));
-
- m_mapEngine->init();
- m_ui->show();
- //TODO: Start map image fetching
+ m_autoCenteringEnabled = enabled;
+ enableAutoCentering(enabled);
}
void SituareEngine::disableAutoCentering()
m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
}
-void SituareEngine::disconnectedFromNetwork()
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- //TODO: Stop map image fetching
-}
-
void SituareEngine::enableAutoCentering(bool enabled)
{
qDebug() << __PRETTY_FUNCTION__;
}
}
+void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
+{
+ qWarning() << __PRETTY_FUNCTION__ << enabled;
+
+ bool accepted = false;
+
+ if (m_automaticUpdateFirstStart && enabled) {
+ accepted = m_ui->showEnableAutomaticUpdateLocationDialog();
+ m_automaticUpdateFirstStart = false;
+ m_ui->automaticLocationUpdateEnabled(accepted);
+ }
+ else {
+ accepted = true;
+ }
+
+ qWarning() << __PRETTY_FUNCTION__ << accepted;
+
+ if (accepted) {
+ if (m_automaticUpdateIntervalTimer) {
+
+ if (enabled && m_gps->isRunning()) {
+ m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
+ m_automaticUpdateIntervalTimer->start();
+ }
+ else
+ m_automaticUpdateIntervalTimer->stop();
+ }
+ }
+}
+
void SituareEngine::error(const QString &error)
{
qDebug() << __PRETTY_FUNCTION__;
- qDebug() << error;
- // ToDo: signal UI?
+
+ m_ui->showMaemoInformationBox(error, true);
+
+ if(error.compare(SESSION_EXPIRED) == 0) {
+ m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
+ m_ui->loggedIn(false);
+ m_ui->loginFailed();
+ }
}
void SituareEngine::fetchUsernameFromSettings()
{
qDebug() << __PRETTY_FUNCTION__;
+
m_ui->setUsername(m_facebookAuthenticator->loadUsername());
}
-void SituareEngine::loginOk(bool freshLogin)
+void SituareEngine::initializeGpsAndAutocentering()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ QSettings settings(DIRECTORY_NAME, FILE_NAME);
+ QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
+ QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
+
+ if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
+
+ connect(m_gps, SIGNAL(position(QPointF,qreal)),
+ this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+
+ changeAutoCenteringSetting(true);
+ enableGPS(true);
+
+ m_ui->showMaemoInformationBox(tr("GPS enabled"));
+ m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
+ } else { // Normal start
+ changeAutoCenteringSetting(autoCenteringEnabled.toBool());
+ enableGPS(gpsEnabled.toBool());
+
+ if (gpsEnabled.toBool())
+ m_ui->showMaemoInformationBox(tr("GPS enabled"));
+
+ if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
+ m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
+ }
+}
+
+bool SituareEngine::isUserMoved()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ return m_userMoved;
+}
+
+void SituareEngine::loginActionPressed()
{
qDebug() << __PRETTY_FUNCTION__;
- if(freshLogin) {
- m_facebookAuthenticator->saveUsername(m_ui->username());
+ if(m_loggedIn) {
+ logout();
+ m_situareService->clearUserData();
+ }
+ else {
+ m_facebookAuthenticator->start();
}
+}
+
+void SituareEngine::loginOk()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_loggedIn = true;
+ m_ui->loggedIn(m_loggedIn);
+
m_ui->show();
m_situareService->fetchLocations(); // request user locations
}
qDebug() << __PRETTY_FUNCTION__;
m_ui->toggleProgressIndicator(false);
- //ToDo: do something
+ m_ui->updateItemVisibility(m_loggedIn);
+}
+
+void SituareEngine::logout()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_loggedIn = false;
+ m_ui->loggedIn(m_loggedIn);
+ m_facebookAuthenticator->clearAccountInformation(); // clear all
}
void SituareEngine::refreshUserData()
m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
}
+void SituareEngine::saveGPSPosition(QPointF position)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
+ USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
+ (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
+ USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
+
+ m_lastUpdatedGPSPosition = position;
+ m_userMoved = true;
+ }
+}
+
+void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ Q_UNUSED(latLonCoordinate);
+ Q_UNUSED(accuracy);
+
+ if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
+ m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
+
+ disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
+ this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
+}
+
void SituareEngine::signalsFromFacebookAuthenticator()
{
qDebug() << __PRETTY_FUNCTION__;
- connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
- m_situareService, SLOT(credentialsReady(bool, FacebookCredentials)));
+ connect(m_facebookAuthenticator, SIGNAL(error(QString)),
+ this, SLOT(error(QString)));
+
+ connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
+ m_situareService, SLOT(credentialsReady(FacebookCredentials)));
- connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
- this, SLOT(loginOk(bool)));
+ connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
+ this, SLOT(loginOk()));
connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
m_ui, SLOT(startLoginProcess(QUrl)));
connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
m_ui, SLOT(loginFailed()));
+
+ connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
+ m_ui, SLOT(saveCookies()));
+
+ connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
+ m_ui, SLOT(loginUsingCookies()));
}
void SituareEngine::signalsFromGPS()
m_ui, SLOT(gpsTimeout()));
connect(m_gps, SIGNAL(error(QString)),
- m_ui, SLOT(gpsError(QString)));
+ this, SLOT(error(QString)));
+
+ connect(m_gps, SIGNAL(position(QPointF,qreal)),
+ this, SLOT(saveGPSPosition(QPointF)));
}
void SituareEngine::signalsFromMainWindow()
{
qDebug() << __PRETTY_FUNCTION__;
- connect(m_ui, SIGNAL(updateCredentials(QUrl)),
- m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
-
connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
this, SLOT(fetchUsernameFromSettings()));
+ connect(m_ui, SIGNAL(loginActionPressed()),
+ this, SLOT(loginActionPressed()));
+
+ connect(m_ui, SIGNAL(saveUsername(QString)),
+ m_facebookAuthenticator, SLOT(saveUsername(QString)));
+
+ connect(m_ui, SIGNAL(updateCredentials(QUrl)),
+ m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
+
// 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_ui, SIGNAL(findUser(QPointF)),
+ m_mapEngine, SLOT(setViewLocation(QPointF)));
+
// signals from friend list tab
connect(m_ui, SIGNAL(findFriend(QPointF)),
m_mapEngine, SLOT(setViewLocation(QPointF)));
{
qDebug() << __PRETTY_FUNCTION__;
+ connect(m_mapEngine, SIGNAL(error(QString)),
+ this, SLOT(error(QString)));
+
connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
m_ui, SIGNAL(locationItemClicked(QList<QString>)));
}
-void SituareEngine::signalsFromNetworkHandler()
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- connect(m_networkHandler, SIGNAL(connected()),
- this, SLOT(connectedToNetwork()));
-
- connect(m_networkHandler, SIGNAL(disconnected()),
- this, SLOT(disconnectedFromNetwork()));
-}
-
void SituareEngine::signalsFromSituareService()
{
qDebug() << __PRETTY_FUNCTION__;
+ connect(m_situareService, SIGNAL(error(QString)),
+ this, SLOT(error(QString)));
+
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()));
}