#include "map/mapengine.h"
#include "situareservice/situareservice.h"
#include "ui/mainwindow.h"
+#include "network/networkaccessmanager.h"
+#include "mce.h"
#include <cmath>
#include "engine.h"
: QObject(parent),
m_autoCenteringEnabled(false),
m_automaticUpdateFirstStart(true),
+ m_automaticUpdateRequest(false),
m_userMoved(false),
m_automaticUpdateIntervalTimer(0),
m_lastUpdatedGPSPosition(QPointF())
-{
+{
qDebug() << __PRETTY_FUNCTION__;
m_ui = new MainWindow;
m_ui->updateItemVisibility();
+ m_networkAccessManager = NetworkAccessManager::instance();
+
// build MapEngine
m_mapEngine = new MapEngine(this);
m_ui->setMapViewScene(m_mapEngine->scene());
m_automaticUpdateIntervalTimer = new QTimer(this);
connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
- this, SLOT(automaticUpdateIntervalTimerTimeout()));
+ this, SLOT(startAutomaticUpdate()));
// signals connected, now it's time to show the main window
// but init the MapEngine before so starting location is set
m_gps->setMode(GPSPosition::Default);
initializeGpsAndAutocentering();
+
+ m_mce = new MCE(this);
+ connect(m_mce, SIGNAL(displayStateChanged(bool)), this, SLOT(displayStateChanged(bool)));
}
SituareEngine::~SituareEngine()
settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
}
-void SituareEngine::automaticUpdateIntervalTimerTimeout()
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- if (m_gps->isRunning() && m_userMoved) {
- requestUpdateLocation();
- m_userMoved = false;
- }
-}
-
void SituareEngine::changeAutoCenteringSetting(bool enabled)
{
qDebug() << __PRETTY_FUNCTION__;
m_ui->buildInformationBox(tr("Auto centering disabled"));
}
+void SituareEngine::displayStateChanged(bool enabled)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_gps->enablePowerSave(!enabled);
+
+ if (m_autoCenteringEnabled)
+ enableAutoCentering(enabled);
+}
+
void SituareEngine::enableAutoCentering(bool enabled)
{
qDebug() << __PRETTY_FUNCTION__;
else
m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
+ connect(m_gps, SIGNAL(position(QPointF,qreal)),
+ this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+
m_automaticUpdateIntervalTimer->start();
} else {
+ disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
+ this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
+
m_automaticUpdateIntervalTimer->stop();
}
}
}
-void SituareEngine::error(const int error)
+void SituareEngine::error(const int context, const int error)
{
- qWarning() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
switch(error)
{
- case QNetworkReply::ConnectionRefusedError:
- m_ui->buildInformationBox(tr("Connection refused by the server"), true);
+ case SituareError::ERROR_GENERAL:
+ if(context == ErrorContext::SITUARE) {
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Unknown server error"), true);
+ }
+ break;
+ case SituareError::ERROR_MISSING_ARGUMENT | QNetworkReply::ConnectionRefusedError :
+ m_ui->toggleProgressIndicator(false);
+ if(context == ErrorContext::SITUARE) {
+ m_ui->buildInformationBox(tr("Missing argument from request"), true);
+ } else if(context == ErrorContext::NETWORK) {
+ m_ui->buildInformationBox(tr("Connection refused by the server"), true);
+ }
break;
case QNetworkReply::RemoteHostClosedError:
- m_ui->buildInformationBox(tr("Connection closed by the server"), true);
+ if(context == ErrorContext::NETWORK) {
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Connection closed by the server"), true);
+ }
break;
case QNetworkReply::HostNotFoundError:
- m_ui->buildInformationBox(tr("Remote server not found"), true);
+ if(context == ErrorContext::NETWORK) {
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Remote server not found"), true);
+ }
break;
case QNetworkReply::TimeoutError:
- m_ui->buildInformationBox(tr("Connection timed out"), true);
+ if(context == ErrorContext::NETWORK) {
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Connection timed out"), true);
+ }
break;
case QNetworkReply::UnknownNetworkError:
- m_ui->buildInformationBox(tr("Network error. Please connect to network"), true);
- m_ui->toggleProgressIndicator(false);
+ if(context == ErrorContext::NETWORK) {
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("No network connection"), true);
+ }
break;
case SituareError::SESSION_EXPIRED:
m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
m_ui->loginFailed();
break;
case SituareError::LOGIN_FAILED:
+ m_ui->toggleProgressIndicator(false);
m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
+ m_ui->loginFailed();
break;
case SituareError::UPDATE_FAILED:
+ m_ui->toggleProgressIndicator(false);
m_ui->buildInformationBox(tr("Update failed, please try again"), true);
break;
case SituareError::DATA_RETRIEVAL_FAILED:
+ m_ui->toggleProgressIndicator(false);
m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
break;
case SituareError::ADDRESS_RETRIEVAL_FAILED:
enableGPS(false);
m_ui->buildInformationBox(tr("GPS initialization failed"), true);
break;
- case SituareError::UNKNOWN_REPLY:
- m_ui->buildInformationBox(tr("Unknown server response"), true);
- break;
case SituareError::INVALID_JSON:
m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
m_ui->loggedIn(false);
m_facebookAuthenticator->clearAccountInformation(false); // clean all
break;
+ case SituareError::ERROR_GEOLOCATION_SERVER_UNAVAILABLE:
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Geolocation server not responding"), true);
+ break;
+ case SituareError::ERROR_GEOLOCATION_REQUEST_FAIL:
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Geolocation request failed, please try again"), true);
+ break;
+ case SituareError::ERROR_GEOLOCATION_LONLAT_INVALID:
+ m_ui->toggleProgressIndicator(false);
+ m_ui->buildInformationBox(tr("Invalid lat/lon value, please try again"), true);
+ break;
default:
- qCritical() << "QNetworkReply::NetworkError :" << error;
+ m_ui->toggleProgressIndicator(false);
+ if(context == ErrorContext::NETWORK)
+ qCritical() << "QNetworkReply::NetworkError: " << error;
+ else
+ qCritical() << "Unknown error: " << error;
+
break;
}
}
}
}
-bool SituareEngine::isUserMoved()
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- return m_userMoved;
-}
-
void SituareEngine::loginActionPressed()
{
qDebug() << __PRETTY_FUNCTION__;
- if(m_ui->loginState()) {
- logout();
- m_situareService->clearUserData();
- } else {
- m_facebookAuthenticator->start();
+ if (m_networkAccessManager->isConnected()) {
+ if(m_ui->loginState()) {
+ logout();
+ m_situareService->clearUserData();
+ } else {
+ m_facebookAuthenticator->start();
+ }
+ }
+ else {
+ error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
}
}
{
qDebug() << __PRETTY_FUNCTION__;
- m_ui->toggleProgressIndicator(true);
-
- m_situareService->fetchLocations();
+ if (m_networkAccessManager->isConnected()) {
+ m_ui->toggleProgressIndicator(true);
+ m_situareService->fetchLocations();
+ }
+ else {
+ error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+ }
}
void SituareEngine::requestAddress()
{
qDebug() << __PRETTY_FUNCTION__;
- if (m_gps->isRunning())
- m_situareService->reverseGeo(m_gps->lastPosition());
- else
- m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
+ if (m_networkAccessManager->isConnected()) {
+ if (m_gps->isRunning())
+ m_situareService->reverseGeo(m_gps->lastPosition());
+ else
+ m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
+ }
+ else {
+ error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+ }
}
void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
{
qDebug() << __PRETTY_FUNCTION__;
- m_ui->toggleProgressIndicator(true);
+ if (m_networkAccessManager->isConnected()) {
+ 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);
+ if (m_gps->isRunning())
+ m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
+ else
+ m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
+ }
+ else {
+ error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
+ }
}
-void SituareEngine::saveGPSPosition(QPointF position)
+void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
{
qDebug() << __PRETTY_FUNCTION__;
m_lastUpdatedGPSPosition = position;
m_userMoved = true;
}
+
+ if (m_automaticUpdateRequest && m_userMoved) {
+ requestUpdateLocation(tr("Automatic location update."));
+ m_automaticUpdateRequest = false;
+ m_userMoved = false;
+ }
}
void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
Q_UNUSED(latLonCoordinate);
Q_UNUSED(accuracy);
- if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
+ 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)),
{
qDebug() << __PRETTY_FUNCTION__;
- connect(m_facebookAuthenticator, SIGNAL(error(int)),
- this, SLOT(error(int)));
+ connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
+ this, SLOT(error(int, int)));
connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
m_situareService, SLOT(credentialsReady(FacebookCredentials)));
connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
m_ui, SLOT(startLoginProcess()));
- connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
- m_ui, SLOT(loginFailed()));
-
connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
m_ui, SLOT(saveCookies()));
connect(m_gps, SIGNAL(timeout()),
m_ui, SLOT(gpsTimeout()));
- connect(m_gps, SIGNAL(error(int)),
- this, SLOT(error(int)));
-
- connect(m_gps, SIGNAL(position(QPointF,qreal)),
- this, SLOT(saveGPSPosition(QPointF)));
+ connect(m_gps, SIGNAL(error(int, int)),
+ this, SLOT(error(int, int)));
}
void SituareEngine::signalsFromMainWindow()
{
- qDebug() << __PRETTY_FUNCTION__;
+ qDebug() << __PRETTY_FUNCTION__;
- connect(m_ui, SIGNAL(error(int)),
- this, SLOT(error(int)));
+ connect(m_ui, SIGNAL(error(int, int)),
+ this, SLOT(error(int, int)));
connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
this, SLOT(fetchUsernameFromSettings()));
// signals from map view
connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
- m_mapEngine, SLOT(setLocation(QPoint)));
+ m_mapEngine, SLOT(setCenterPosition(QPoint)));
connect(m_ui, SIGNAL(mapViewResized(QSize)),
m_mapEngine, SLOT(viewResized(QSize)));
this, SLOT(requestUpdateLocation(QString,bool)));
connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
- this, SLOT(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)));
+ m_mapEngine, SLOT(centerToCoordinates(QPointF)));
// signals from friend list tab
connect(m_ui, SIGNAL(findFriend(QPointF)),
- m_mapEngine, SLOT(setViewLocation(QPointF)));
+ m_mapEngine, SLOT(centerToCoordinates(QPointF)));
}
void SituareEngine::signalsFromMapEngine()
{
qDebug() << __PRETTY_FUNCTION__;
- connect(m_mapEngine, SIGNAL(error(int)),
- this, SLOT(error(int)));
+ connect(m_mapEngine, SIGNAL(error(int, int)),
+ this, SLOT(error(int, int)));
connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
{
qDebug() << __PRETTY_FUNCTION__;
- connect(m_situareService, SIGNAL(error(int)),
- this, SLOT(error(int)));
+ connect(m_situareService, SIGNAL(error(int, int)),
+ this, SLOT(error(int, int)));
- connect(m_situareService, SIGNAL(error(int)),
+ connect(m_situareService, SIGNAL(error(int, int)),
m_ui, SIGNAL(messageSendingFailed(int)));
connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
m_ui, SIGNAL(clearUpdateLocationDialogData()));
}
+void SituareEngine::startAutomaticUpdate()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_gps->requestUpdate();
+ m_automaticUpdateRequest = true;
+}
+
void SituareEngine::updateWasSuccessful()
{
qDebug() << __PRETTY_FUNCTION__;
- m_situareService->fetchLocations();
+ if (m_networkAccessManager->isConnected())
+ m_situareService->fetchLocations();
+ else
+ error(ErrorContext::NETWORK, QNetworkReply::UnknownNetworkError);
}
void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
qDebug() << __PRETTY_FUNCTION__;
m_ui->toggleProgressIndicator(false);
+ m_ui->showPanels();
emit userLocationReady(user);
emit friendsLocationsReady(friendsList);