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"
38 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
39 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
40 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12; ///< Default zoom level when GPS available
41 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
42 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
43 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
45 SituareEngine::SituareEngine(QMainWindow *parent)
47 m_autoCenteringEnabled(false),
48 m_automaticUpdateFirstStart(true),
51 m_automaticUpdateEnabled(false),
52 m_automaticUpdateIntervalTimer(0),
53 m_lastUpdatedGPSPosition(QPointF())
55 qDebug() << __PRETTY_FUNCTION__;
56 m_ui = new MainWindow;
57 m_ui->updateItemVisibility(m_loggedIn);
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(automaticUpdateIntervalTimerTimeout()));
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();
106 SituareEngine::~SituareEngine()
108 qDebug() << __PRETTY_FUNCTION__;
112 QSettings settings(DIRECTORY_NAME, FILE_NAME);
113 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
114 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
117 void SituareEngine::automaticUpdateIntervalTimerTimeout()
119 qDebug() << __PRETTY_FUNCTION__;
121 if (m_gps->isRunning() && m_userMoved) {
122 requestUpdateLocation();
127 void SituareEngine::changeAutoCenteringSetting(bool enabled)
129 qDebug() << __PRETTY_FUNCTION__;
131 m_autoCenteringEnabled = enabled;
132 enableAutoCentering(enabled);
135 void SituareEngine::disableAutoCentering()
137 qDebug() << __PRETTY_FUNCTION__;
139 changeAutoCenteringSetting(false);
140 m_ui->buildInformationBox(tr("Auto centering disabled"));
143 void SituareEngine::enableAutoCentering(bool enabled)
145 qDebug() << __PRETTY_FUNCTION__;
147 m_ui->setAutoCenteringButtonEnabled(enabled);
148 m_mapEngine->setAutoCentering(enabled);
151 m_gps->requestLastPosition();
154 void SituareEngine::enableGPS(bool enabled)
156 qDebug() << __PRETTY_FUNCTION__;
158 m_ui->setOwnLocationCrosshairVisibility(!enabled);
160 if (m_gps->isInitialized()) {
161 m_ui->setGPSButtonEnabled(enabled);
162 m_mapEngine->setGPSEnabled(enabled);
164 if (enabled && !m_gps->isRunning()) {
166 enableAutoCentering(m_autoCenteringEnabled);
167 m_gps->requestLastPosition();
169 if (!m_automaticUpdateEnabled && m_loggedIn)
170 m_ui->requestAutomaticLocationUpdateSettings();
172 else if (!enabled && m_gps->isRunning()) {
174 enableAutoCentering(false);
175 enableAutomaticLocationUpdate(false);
180 m_ui->buildInformationBox(tr("Unable to start GPS"));
181 m_ui->setGPSButtonEnabled(false);
182 m_mapEngine->setGPSEnabled(false);
186 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
188 qDebug() << __PRETTY_FUNCTION__;
190 m_automaticUpdateEnabled = enabled;
192 //Show automatic update confirmation dialog
193 if (m_automaticUpdateFirstStart && m_gps->isRunning() && m_automaticUpdateEnabled) {
194 m_ui->showEnableAutomaticUpdateLocationDialog(
195 tr("Do you want to enable automatic location update with %1 min update interval?")
196 .arg(updateIntervalMsecs/1000/60));
197 m_automaticUpdateFirstStart = false;
199 if (m_automaticUpdateEnabled && m_gps->isRunning()) {
200 m_ui->buildInformationBox(tr("Automatic location update enabled"));
201 if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
202 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
204 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
206 m_automaticUpdateIntervalTimer->start();
209 m_automaticUpdateIntervalTimer->stop();
214 void SituareEngine::error(const int error)
216 qDebug() << __PRETTY_FUNCTION__;
220 case QNetworkReply::ConnectionRefusedError:
221 m_ui->buildInformationBox(tr("Connection refused by the server"), true);
223 case QNetworkReply::RemoteHostClosedError:
224 m_ui->buildInformationBox(tr("Connection closed by the server"), true);
226 case QNetworkReply::HostNotFoundError:
227 m_ui->buildInformationBox(tr("Remote server not found"), true);
229 case QNetworkReply::TimeoutError:
230 m_ui->buildInformationBox(tr("Connection timed out"), true);
232 case SituareError::SESSION_EXPIRED:
233 m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
234 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
235 m_ui->loggedIn(false);
238 case SituareError::LOGIN_FAILED:
239 m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
241 case SituareError::UPDATE_FAILED:
242 m_ui->buildInformationBox(tr("Update failed, please try again"), true);
244 case SituareError::DATA_RETRIEVAL_FAILED:
245 m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
247 case SituareError::ADDRESS_RETRIEVAL_FAILED:
248 m_ui->buildInformationBox(tr("Address retrieval failed"), true);
250 case SituareError::IMAGE_DOWNLOAD_FAILED:
251 m_ui->buildInformationBox(tr("Image download failed"), true);
253 case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
254 m_ui->buildInformationBox(tr("Map image download failed"), true);
256 case SituareError::GPS_INITIALIZATION_FAILED:
258 m_ui->buildInformationBox(tr("GPS initialization failed"), true);
260 case SituareError::UNKNOWN_REPLY:
261 m_ui->buildInformationBox(tr("Unknown server response"), true);
264 qCritical() << "QNetworkReply::NetworkError :" << error;
269 void SituareEngine::fetchUsernameFromSettings()
271 qDebug() << __PRETTY_FUNCTION__;
273 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
276 void SituareEngine::initializeGpsAndAutocentering()
278 qDebug() << __PRETTY_FUNCTION__;
280 QSettings settings(DIRECTORY_NAME, FILE_NAME);
281 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
282 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
284 if (m_gps->isInitialized()) {
286 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
288 connect(m_gps, SIGNAL(position(QPointF,qreal)),
289 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
291 changeAutoCenteringSetting(true);
294 m_ui->buildInformationBox(tr("GPS enabled"));
295 m_ui->buildInformationBox(tr("Auto centering enabled"));
297 } else { // Normal start
298 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
299 enableGPS(gpsEnabled.toBool());
301 if (gpsEnabled.toBool())
302 m_ui->buildInformationBox(tr("GPS enabled"));
304 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
305 m_ui->buildInformationBox(tr("Auto centering enabled"));
312 bool SituareEngine::isUserMoved()
314 qDebug() << __PRETTY_FUNCTION__;
319 void SituareEngine::loginActionPressed()
321 qDebug() << __PRETTY_FUNCTION__;
325 m_situareService->clearUserData();
328 m_facebookAuthenticator->start();
332 void SituareEngine::loginOk()
334 qDebug() << __PRETTY_FUNCTION__;
337 m_ui->loggedIn(m_loggedIn);
340 m_situareService->fetchLocations(); // request user locations
342 if (m_gps->isRunning())
343 m_ui->requestAutomaticLocationUpdateSettings();
346 void SituareEngine::loginProcessCancelled()
348 qDebug() << __PRETTY_FUNCTION__;
350 m_ui->toggleProgressIndicator(false);
351 m_ui->updateItemVisibility(m_loggedIn);
354 void SituareEngine::logout()
356 qDebug() << __PRETTY_FUNCTION__;
359 m_ui->loggedIn(m_loggedIn);
360 m_facebookAuthenticator->clearAccountInformation(); // clear all
361 m_automaticUpdateEnabled = false;
362 m_automaticUpdateFirstStart = true;
365 void SituareEngine::refreshUserData()
367 qDebug() << __PRETTY_FUNCTION__;
369 m_ui->toggleProgressIndicator(true);
371 m_situareService->fetchLocations();
374 void SituareEngine::requestAddress()
376 qDebug() << __PRETTY_FUNCTION__;
378 if (m_gps->isRunning())
379 m_situareService->reverseGeo(m_gps->lastPosition());
381 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
384 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
386 qDebug() << __PRETTY_FUNCTION__;
388 m_ui->toggleProgressIndicator(true);
390 if (m_gps->isRunning())
391 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
393 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
396 void SituareEngine::saveGPSPosition(QPointF position)
398 qDebug() << __PRETTY_FUNCTION__;
400 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
401 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
402 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
403 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
405 m_lastUpdatedGPSPosition = position;
410 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
412 qDebug() << __PRETTY_FUNCTION__;
414 Q_UNUSED(latLonCoordinate);
417 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
418 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
420 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
421 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
424 void SituareEngine::signalsFromFacebookAuthenticator()
426 qDebug() << __PRETTY_FUNCTION__;
428 connect(m_facebookAuthenticator, SIGNAL(error(int)),
429 this, SLOT(error(int)));
431 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
432 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
434 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
435 this, SLOT(loginOk()));
437 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
438 m_ui, SLOT(startLoginProcess()));
440 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
441 m_ui, SLOT(loginFailed()));
443 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
444 m_ui, SLOT(saveCookies()));
446 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
447 m_ui, SLOT(loginUsingCookies()));
450 void SituareEngine::signalsFromGPS()
452 qDebug() << __PRETTY_FUNCTION__;
454 connect(m_gps, SIGNAL(position(QPointF,qreal)),
455 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
457 connect(m_gps, SIGNAL(timeout()),
458 m_ui, SLOT(gpsTimeout()));
460 connect(m_gps, SIGNAL(error(int)),
461 this, SLOT(error(int)));
463 connect(m_gps, SIGNAL(position(QPointF,qreal)),
464 this, SLOT(saveGPSPosition(QPointF)));
467 void SituareEngine::signalsFromMainWindow()
469 qDebug() << __PRETTY_FUNCTION__;
471 connect(m_ui, SIGNAL(error(int)),
472 this, SLOT(error(int)));
474 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
475 this, SLOT(fetchUsernameFromSettings()));
477 connect(m_ui, SIGNAL(loginActionPressed()),
478 this, SLOT(loginActionPressed()));
480 connect(m_ui, SIGNAL(saveUsername(QString)),
481 m_facebookAuthenticator, SLOT(saveUsername(QString)));
483 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
484 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
486 // signals from map view
487 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
488 m_mapEngine, SLOT(setLocation(QPoint)));
490 connect(m_ui, SIGNAL(mapViewResized(QSize)),
491 m_mapEngine, SLOT(viewResized(QSize)));
493 connect(m_ui, SIGNAL(viewZoomFinished()),
494 m_mapEngine, SLOT(viewZoomFinished()));
496 // signals from zoom buttons (zoom panel and volume buttons)
497 connect(m_ui, SIGNAL(zoomIn()),
498 m_mapEngine, SLOT(zoomIn()));
500 connect(m_ui, SIGNAL(zoomOut()),
501 m_mapEngine, SLOT(zoomOut()));
503 // signals from menu buttons
504 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
505 this, SLOT(changeAutoCenteringSetting(bool)));
507 connect(m_ui, SIGNAL(gpsTriggered(bool)),
508 this, SLOT(enableGPS(bool)));
510 //signals from dialogs
511 connect(m_ui, SIGNAL(cancelLoginProcess()),
512 this, SLOT(loginProcessCancelled()));
514 connect(m_ui, SIGNAL(requestReverseGeo()),
515 this, SLOT(requestAddress()));
517 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
518 this, SLOT(requestUpdateLocation(QString,bool)));
520 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
521 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
523 // signals from user info tab
524 connect(m_ui, SIGNAL(refreshUserData()),
525 this, SLOT(refreshUserData()));
527 connect(m_ui, SIGNAL(findUser(QPointF)),
528 m_mapEngine, SLOT(setViewLocation(QPointF)));
530 // signals from friend list tab
531 connect(m_ui, SIGNAL(findFriend(QPointF)),
532 m_mapEngine, SLOT(setViewLocation(QPointF)));
535 void SituareEngine::signalsFromMapEngine()
537 qDebug() << __PRETTY_FUNCTION__;
539 connect(m_mapEngine, SIGNAL(error(int)),
540 this, SLOT(error(int)));
542 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
543 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
545 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
546 m_ui, SIGNAL(zoomLevelChanged(int)));
548 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
549 this, SLOT(disableAutoCentering()));
551 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
552 m_ui, SIGNAL(maxZoomLevelReached()));
554 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
555 m_ui, SIGNAL(minZoomLevelReached()));
557 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
558 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
560 connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
561 m_ui, SIGNAL(newMapResolution(qreal)));
564 void SituareEngine::signalsFromSituareService()
566 qDebug() << __PRETTY_FUNCTION__;
568 connect(m_situareService, SIGNAL(error(int)),
569 this, SLOT(error(int)));
571 connect(m_situareService, SIGNAL(error(int)),
572 m_ui, SIGNAL(messageSendingFailed(int)));
574 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
575 m_ui, SIGNAL(reverseGeoReady(QString)));
577 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
578 this, SLOT(userDataChanged(User*, QList<User*>&)));
580 connect(m_situareService, SIGNAL(updateWasSuccessful()),
581 this, SLOT(updateWasSuccessful()));
583 connect(m_situareService, SIGNAL(updateWasSuccessful()),
584 m_ui, SIGNAL(updateWasSuccessful()));
587 void SituareEngine::updateWasSuccessful()
589 qDebug() << __PRETTY_FUNCTION__;
591 m_situareService->fetchLocations();
594 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
596 qDebug() << __PRETTY_FUNCTION__;
598 m_ui->toggleProgressIndicator(false);
600 emit userLocationReady(user);
601 emit friendsLocationsReady(friendsList);