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),
50 m_automaticUpdateIntervalTimer(0),
51 m_lastUpdatedGPSPosition(QPointF())
53 qDebug() << __PRETTY_FUNCTION__;
54 m_ui = new MainWindow;
55 m_ui->updateItemVisibility();
58 m_mapEngine = new MapEngine(this);
59 m_ui->setMapViewScene(m_mapEngine->scene());
62 m_gps = new GPSPosition(this);
64 // build SituareService
65 m_situareService = new SituareService(this);
67 // build FacebookAuthenticator
68 m_facebookAuthenticator = new FacebookAuthentication(this);
71 signalsFromMapEngine();
73 signalsFromSituareService();
74 signalsFromMainWindow();
75 signalsFromFacebookAuthenticator();
77 connect(this, SIGNAL(userLocationReady(User*)),
78 m_ui, SIGNAL(userLocationReady(User*)));
80 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
81 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
83 connect(this, SIGNAL(userLocationReady(User*)),
84 m_mapEngine, SLOT(receiveOwnLocation(User*)));
86 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
87 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
89 m_automaticUpdateIntervalTimer = new QTimer(this);
90 connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
91 this, SLOT(automaticUpdateIntervalTimerTimeout()));
93 // signals connected, now it's time to show the main window
94 // but init the MapEngine before so starting location is set
98 m_facebookAuthenticator->start();
100 m_gps->setMode(GPSPosition::Default);
101 initializeGpsAndAutocentering();
104 SituareEngine::~SituareEngine()
106 qDebug() << __PRETTY_FUNCTION__;
110 QSettings settings(DIRECTORY_NAME, FILE_NAME);
111 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
112 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
115 void SituareEngine::automaticUpdateIntervalTimerTimeout()
117 qDebug() << __PRETTY_FUNCTION__;
119 if (m_gps->isRunning() && m_userMoved) {
120 requestUpdateLocation();
125 void SituareEngine::changeAutoCenteringSetting(bool enabled)
127 qDebug() << __PRETTY_FUNCTION__;
129 m_autoCenteringEnabled = enabled;
130 enableAutoCentering(enabled);
133 void SituareEngine::disableAutoCentering()
135 qDebug() << __PRETTY_FUNCTION__;
137 changeAutoCenteringSetting(false);
138 m_ui->buildInformationBox(tr("Auto centering disabled"));
141 void SituareEngine::enableAutoCentering(bool enabled)
143 qDebug() << __PRETTY_FUNCTION__;
145 m_ui->setAutoCenteringButtonEnabled(enabled);
146 m_mapEngine->setAutoCentering(enabled);
149 m_gps->requestLastPosition();
152 void SituareEngine::enableGPS(bool enabled)
154 qDebug() << __PRETTY_FUNCTION__;
156 m_ui->setOwnLocationCrosshairVisibility(!enabled);
158 if (m_gps->isInitialized()) {
159 m_ui->setGPSButtonEnabled(enabled);
160 m_mapEngine->setGPSEnabled(enabled);
162 if (enabled && !m_gps->isRunning()) {
164 enableAutoCentering(m_autoCenteringEnabled);
165 m_gps->requestLastPosition();
167 if(m_ui->loginState())
168 m_ui->readAutomaticLocationUpdateSettings();
170 else if (!enabled && m_gps->isRunning()) {
172 enableAutoCentering(false);
173 enableAutomaticLocationUpdate(false);
178 m_ui->buildInformationBox(tr("Unable to start GPS"));
179 m_ui->setGPSButtonEnabled(false);
180 m_mapEngine->setGPSEnabled(false);
184 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
186 qDebug() << __PRETTY_FUNCTION__;
188 //Show automatic update confirmation dialog
189 if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
190 m_ui->showEnableAutomaticUpdateLocationDialog(
191 tr("Do you want to enable automatic location update with %1 min update interval?")
192 .arg(updateIntervalMsecs/1000/60));
193 m_automaticUpdateFirstStart = false;
195 if (enabled && m_gps->isRunning()) {
196 m_ui->buildInformationBox(tr("Automatic location update enabled"));
197 if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
198 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
200 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
202 m_automaticUpdateIntervalTimer->start();
205 m_automaticUpdateIntervalTimer->stop();
210 void SituareEngine::error(const int context, const int error)
212 qDebug() << __PRETTY_FUNCTION__;
216 case SituareError::ERROR_GENERAL:
217 if(context == ErrorContext::SITUARE) {
218 m_ui->toggleProgressIndicator(false);
219 m_ui->buildInformationBox(tr("Unknown server error"), true);
222 case SituareError::ERROR_MISSING_ARGUMENT | QNetworkReply::ConnectionRefusedError :
223 m_ui->toggleProgressIndicator(false);
224 if(context == ErrorContext::SITUARE) {
225 m_ui->buildInformationBox(tr("Missing argument from request"), true);
226 } else if(context == ErrorContext::NETWORK) {
227 m_ui->buildInformationBox(tr("Connection refused by the server"), true);
230 case QNetworkReply::RemoteHostClosedError:
231 if(context == ErrorContext::NETWORK) {
232 m_ui->toggleProgressIndicator(false);
233 m_ui->buildInformationBox(tr("Connection closed by the server"), true);
236 case QNetworkReply::HostNotFoundError:
237 if(context == ErrorContext::NETWORK) {
238 m_ui->toggleProgressIndicator(false);
239 m_ui->buildInformationBox(tr("Remote server not found"), true);
242 case QNetworkReply::TimeoutError:
243 if(context == ErrorContext::NETWORK) {
244 m_ui->toggleProgressIndicator(false);
245 m_ui->buildInformationBox(tr("Connection timed out"), true);
248 case SituareError::SESSION_EXPIRED:
249 m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
250 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
251 m_situareService->clearUserData();
252 m_ui->loggedIn(false);
255 case SituareError::LOGIN_FAILED:
256 m_ui->toggleProgressIndicator(false);
257 m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
260 case SituareError::UPDATE_FAILED:
261 m_ui->toggleProgressIndicator(false);
262 m_ui->buildInformationBox(tr("Update failed, please try again"), true);
264 case SituareError::DATA_RETRIEVAL_FAILED:
265 m_ui->toggleProgressIndicator(false);
266 m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
268 case SituareError::ADDRESS_RETRIEVAL_FAILED:
269 m_ui->buildInformationBox(tr("Address retrieval failed"), true);
271 case SituareError::IMAGE_DOWNLOAD_FAILED:
272 m_ui->buildInformationBox(tr("Image download failed"), true);
274 case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
275 m_ui->buildInformationBox(tr("Map image download failed"), true);
277 case SituareError::GPS_INITIALIZATION_FAILED:
279 m_ui->buildInformationBox(tr("GPS initialization failed"), true);
281 case SituareError::INVALID_JSON:
282 m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
283 m_ui->loggedIn(false);
284 m_facebookAuthenticator->clearAccountInformation(false); // clean all
286 case SituareError::ERROR_GEOLOCATION_SERVER_UNAVAILABLE:
287 m_ui->toggleProgressIndicator(false);
288 m_ui->buildInformationBox(tr("Geolocation server not responding"), true);
290 case SituareError::ERROR_GEOLOCATION_REQUEST_FAIL:
291 m_ui->toggleProgressIndicator(false);
292 m_ui->buildInformationBox(tr("Geolocation request failed, please try again"), true);
294 case SituareError::ERROR_GEOLOCATION_LONLAT_INVALID:
295 m_ui->toggleProgressIndicator(false);
296 m_ui->buildInformationBox(tr("Invalid lat/lon value, please try again"), true);
299 m_ui->toggleProgressIndicator(false);
300 if(context == ErrorContext::NETWORK)
301 qCritical() << "QNetworkReply::NetworkError: " << error;
303 qCritical() << "Unknown error: " << error;
309 void SituareEngine::fetchUsernameFromSettings()
311 qDebug() << __PRETTY_FUNCTION__;
313 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
316 void SituareEngine::initializeGpsAndAutocentering()
318 qDebug() << __PRETTY_FUNCTION__;
320 QSettings settings(DIRECTORY_NAME, FILE_NAME);
321 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
322 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
324 if (m_gps->isInitialized()) {
326 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
328 connect(m_gps, SIGNAL(position(QPointF,qreal)),
329 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
331 changeAutoCenteringSetting(true);
334 m_ui->buildInformationBox(tr("GPS enabled"));
335 m_ui->buildInformationBox(tr("Auto centering enabled"));
337 } else { // Normal start
338 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
339 enableGPS(gpsEnabled.toBool());
341 if (gpsEnabled.toBool())
342 m_ui->buildInformationBox(tr("GPS enabled"));
344 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
345 m_ui->buildInformationBox(tr("Auto centering enabled"));
352 bool SituareEngine::isUserMoved()
354 qDebug() << __PRETTY_FUNCTION__;
359 void SituareEngine::loginActionPressed()
361 qDebug() << __PRETTY_FUNCTION__;
363 if(m_ui->loginState()) {
365 m_situareService->clearUserData();
367 m_facebookAuthenticator->start();
371 void SituareEngine::loginOk()
373 qDebug() << __PRETTY_FUNCTION__;
375 m_ui->loggedIn(true);
378 m_situareService->fetchLocations(); // request user locations
380 if (m_gps->isRunning())
381 m_ui->readAutomaticLocationUpdateSettings();
384 void SituareEngine::loginProcessCancelled()
386 qDebug() << __PRETTY_FUNCTION__;
388 m_ui->toggleProgressIndicator(false);
389 m_ui->updateItemVisibility();
392 void SituareEngine::logout()
394 qDebug() << __PRETTY_FUNCTION__;
396 m_ui->loggedIn(false);
398 // signal to clear locationUpdateDialog's data
399 connect(this, SIGNAL(clearUpdateLocationDialogData()),
400 m_ui, SIGNAL(clearUpdateLocationDialogData()));
401 emit clearUpdateLocationDialogData();
403 m_facebookAuthenticator->clearAccountInformation(); // clear all
404 m_automaticUpdateFirstStart = true;
407 void SituareEngine::refreshUserData()
409 qDebug() << __PRETTY_FUNCTION__;
411 m_ui->toggleProgressIndicator(true);
413 m_situareService->fetchLocations();
416 void SituareEngine::requestAddress()
418 qDebug() << __PRETTY_FUNCTION__;
420 if (m_gps->isRunning())
421 m_situareService->reverseGeo(m_gps->lastPosition());
423 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
426 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
428 qDebug() << __PRETTY_FUNCTION__;
430 m_ui->toggleProgressIndicator(true);
432 if (m_gps->isRunning())
433 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
435 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
438 void SituareEngine::saveGPSPosition(QPointF position)
440 qDebug() << __PRETTY_FUNCTION__;
442 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
443 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
444 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
445 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
447 m_lastUpdatedGPSPosition = position;
452 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
454 qDebug() << __PRETTY_FUNCTION__;
456 Q_UNUSED(latLonCoordinate);
459 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
460 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
462 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
463 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
466 void SituareEngine::signalsFromFacebookAuthenticator()
468 qDebug() << __PRETTY_FUNCTION__;
470 connect(m_facebookAuthenticator, SIGNAL(error(int, int)),
471 this, SLOT(error(int, int)));
473 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
474 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
476 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
477 this, SLOT(loginOk()));
479 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
480 m_ui, SLOT(startLoginProcess()));
482 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
483 m_ui, SLOT(saveCookies()));
485 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
486 m_ui, SLOT(loginUsingCookies()));
489 void SituareEngine::signalsFromGPS()
491 qDebug() << __PRETTY_FUNCTION__;
493 connect(m_gps, SIGNAL(position(QPointF,qreal)),
494 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
496 connect(m_gps, SIGNAL(timeout()),
497 m_ui, SLOT(gpsTimeout()));
499 connect(m_gps, SIGNAL(error(int, int)),
500 this, SLOT(error(int, int)));
502 connect(m_gps, SIGNAL(position(QPointF,qreal)),
503 this, SLOT(saveGPSPosition(QPointF)));
506 void SituareEngine::signalsFromMainWindow()
508 qDebug() << __PRETTY_FUNCTION__;
510 connect(m_ui, SIGNAL(error(int, int)),
511 this, SLOT(error(int, int)));
513 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
514 this, SLOT(fetchUsernameFromSettings()));
516 connect(m_ui, SIGNAL(loginActionPressed()),
517 this, SLOT(loginActionPressed()));
519 connect(m_ui, SIGNAL(saveUsername(QString)),
520 m_facebookAuthenticator, SLOT(saveUsername(QString)));
522 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
523 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
525 // signals from map view
526 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
527 m_mapEngine, SLOT(setLocation(QPoint)));
529 connect(m_ui, SIGNAL(mapViewResized(QSize)),
530 m_mapEngine, SLOT(viewResized(QSize)));
532 connect(m_ui, SIGNAL(viewZoomFinished()),
533 m_mapEngine, SLOT(viewZoomFinished()));
535 // signals from zoom buttons (zoom panel and volume buttons)
536 connect(m_ui, SIGNAL(zoomIn()),
537 m_mapEngine, SLOT(zoomIn()));
539 connect(m_ui, SIGNAL(zoomOut()),
540 m_mapEngine, SLOT(zoomOut()));
542 // signals from menu buttons
543 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
544 this, SLOT(changeAutoCenteringSetting(bool)));
546 connect(m_ui, SIGNAL(gpsTriggered(bool)),
547 this, SLOT(enableGPS(bool)));
549 //signals from dialogs
550 connect(m_ui, SIGNAL(cancelLoginProcess()),
551 this, SLOT(loginProcessCancelled()));
553 connect(m_ui, SIGNAL(requestReverseGeo()),
554 this, SLOT(requestAddress()));
556 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
557 this, SLOT(requestUpdateLocation(QString,bool)));
559 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
560 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
562 // signals from user info tab
563 connect(m_ui, SIGNAL(refreshUserData()),
564 this, SLOT(refreshUserData()));
566 connect(m_ui, SIGNAL(findUser(QPointF)),
567 m_mapEngine, SLOT(setViewLocation(QPointF)));
569 // signals from friend list tab
570 connect(m_ui, SIGNAL(findFriend(QPointF)),
571 m_mapEngine, SLOT(setViewLocation(QPointF)));
574 void SituareEngine::signalsFromMapEngine()
576 qDebug() << __PRETTY_FUNCTION__;
578 connect(m_mapEngine, SIGNAL(error(int, int)),
579 this, SLOT(error(int, int)));
581 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
582 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
584 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
585 m_ui, SIGNAL(zoomLevelChanged(int)));
587 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
588 this, SLOT(disableAutoCentering()));
590 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
591 m_ui, SIGNAL(maxZoomLevelReached()));
593 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
594 m_ui, SIGNAL(minZoomLevelReached()));
596 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
597 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
599 connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
600 m_ui, SIGNAL(newMapResolution(qreal)));
603 void SituareEngine::signalsFromSituareService()
605 qDebug() << __PRETTY_FUNCTION__;
607 connect(m_situareService, SIGNAL(error(int, int)),
608 this, SLOT(error(int, int)));
610 connect(m_situareService, SIGNAL(error(int, int)),
611 m_ui, SIGNAL(messageSendingFailed(int)));
613 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
614 m_ui, SIGNAL(reverseGeoReady(QString)));
616 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
617 this, SLOT(userDataChanged(User*, QList<User*>&)));
619 connect(m_situareService, SIGNAL(updateWasSuccessful()),
620 this, SLOT(updateWasSuccessful()));
622 connect(m_situareService, SIGNAL(updateWasSuccessful()),
623 m_ui, SIGNAL(clearUpdateLocationDialogData()));
626 void SituareEngine::updateWasSuccessful()
628 qDebug() << __PRETTY_FUNCTION__;
630 m_situareService->fetchLocations();
633 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
635 qDebug() << __PRETTY_FUNCTION__;
637 m_ui->toggleProgressIndicator(false);
640 emit userLocationReady(user);
641 emit friendsLocationsReady(friendsList);