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"
40 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
41 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
42 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12; ///< Default zoom level when GPS available
43 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
44 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
45 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
47 SituareEngine::SituareEngine(QMainWindow *parent)
49 m_autoCenteringEnabled(false),
50 m_automaticUpdateFirstStart(true),
51 m_automaticUpdateRequest(false),
53 m_automaticUpdateIntervalTimer(0),
54 m_lastUpdatedGPSPosition(QPointF())
56 qDebug() << __PRETTY_FUNCTION__;
57 m_ui = new MainWindow;
58 m_ui->updateItemVisibility();
61 m_mapEngine = new MapEngine(this);
62 m_ui->setMapViewScene(m_mapEngine->scene());
65 m_gps = new GPSPosition(this);
67 // build SituareService
68 m_situareService = new SituareService(this);
70 // build FacebookAuthenticator
71 m_facebookAuthenticator = new FacebookAuthentication(this);
74 signalsFromMapEngine();
76 signalsFromSituareService();
77 signalsFromMainWindow();
78 signalsFromFacebookAuthenticator();
80 connect(this, SIGNAL(userLocationReady(User*)),
81 m_ui, SIGNAL(userLocationReady(User*)));
83 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
84 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
86 connect(this, SIGNAL(userLocationReady(User*)),
87 m_mapEngine, SLOT(receiveOwnLocation(User*)));
89 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
90 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
92 m_automaticUpdateIntervalTimer = new QTimer(this);
93 connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
94 this, SLOT(startAutomaticUpdate()));
96 // signals connected, now it's time to show the main window
97 // but init the MapEngine before so starting location is set
101 m_facebookAuthenticator->start();
103 m_gps->setMode(GPSPosition::Default);
104 initializeGpsAndAutocentering();
106 m_mce = new MCE(this);
107 connect(m_mce, SIGNAL(displayStateChanged(bool)), this, SLOT(displayStateChanged(bool)));
110 SituareEngine::~SituareEngine()
112 qDebug() << __PRETTY_FUNCTION__;
116 QSettings settings(DIRECTORY_NAME, FILE_NAME);
117 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
118 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
121 void SituareEngine::changeAutoCenteringSetting(bool enabled)
123 qDebug() << __PRETTY_FUNCTION__;
125 m_autoCenteringEnabled = enabled;
126 enableAutoCentering(enabled);
129 void SituareEngine::disableAutoCentering()
131 qDebug() << __PRETTY_FUNCTION__;
133 changeAutoCenteringSetting(false);
134 m_ui->buildInformationBox(tr("Auto centering disabled"));
137 void SituareEngine::displayStateChanged(bool enabled)
139 qDebug() << __PRETTY_FUNCTION__;
141 m_gps->enablePowerSave(!enabled);
143 if (m_autoCenteringEnabled)
144 enableAutoCentering(enabled);
147 void SituareEngine::enableAutoCentering(bool enabled)
149 qDebug() << __PRETTY_FUNCTION__;
151 m_ui->setAutoCenteringButtonEnabled(enabled);
152 m_mapEngine->setAutoCentering(enabled);
155 m_gps->requestLastPosition();
158 void SituareEngine::enableGPS(bool enabled)
160 qDebug() << __PRETTY_FUNCTION__;
162 m_ui->setOwnLocationCrosshairVisibility(!enabled);
164 if (m_gps->isInitialized()) {
165 m_ui->setGPSButtonEnabled(enabled);
166 m_mapEngine->setGPSEnabled(enabled);
168 if (enabled && !m_gps->isRunning()) {
170 enableAutoCentering(m_autoCenteringEnabled);
171 m_gps->requestLastPosition();
173 if(m_ui->loginState())
174 m_ui->readAutomaticLocationUpdateSettings();
176 else if (!enabled && m_gps->isRunning()) {
178 enableAutoCentering(false);
179 enableAutomaticLocationUpdate(false);
184 m_ui->buildInformationBox(tr("Unable to start GPS"));
185 m_ui->setGPSButtonEnabled(false);
186 m_mapEngine->setGPSEnabled(false);
190 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
192 qDebug() << __PRETTY_FUNCTION__;
194 //Show automatic update confirmation dialog
195 if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
196 m_ui->showEnableAutomaticUpdateLocationDialog(
197 tr("Do you want to enable automatic location update with %1 min update interval?")
198 .arg(updateIntervalMsecs/1000/60));
199 m_automaticUpdateFirstStart = false;
201 if (enabled && m_gps->isRunning()) {
202 m_ui->buildInformationBox(tr("Automatic location update enabled"));
203 if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
204 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
206 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
208 connect(m_gps, SIGNAL(position(QPointF,qreal)),
209 this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
211 m_automaticUpdateIntervalTimer->start();
214 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
215 this, SLOT(requestAutomaticUpdateIfMoved(QPointF)));
217 m_automaticUpdateIntervalTimer->stop();
222 void SituareEngine::error(const int error)
224 qDebug() << __PRETTY_FUNCTION__;
228 case QNetworkReply::ConnectionRefusedError:
229 m_ui->toggleProgressIndicator(false);
230 m_ui->buildInformationBox(tr("Connection refused by the server"), true);
232 case QNetworkReply::RemoteHostClosedError:
233 m_ui->toggleProgressIndicator(false);
234 m_ui->buildInformationBox(tr("Connection closed by the server"), true);
236 case QNetworkReply::HostNotFoundError:
237 m_ui->toggleProgressIndicator(false);
238 m_ui->buildInformationBox(tr("Remote server not found"), true);
240 case QNetworkReply::TimeoutError:
241 m_ui->toggleProgressIndicator(false);
242 m_ui->buildInformationBox(tr("Connection timed out"), true);
244 case SituareError::SESSION_EXPIRED:
245 m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
246 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
247 m_situareService->clearUserData();
248 m_ui->loggedIn(false);
251 case SituareError::LOGIN_FAILED:
252 m_ui->toggleProgressIndicator(false);
253 m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
256 case SituareError::UPDATE_FAILED:
257 m_ui->toggleProgressIndicator(false);
258 m_ui->buildInformationBox(tr("Update failed, please try again"), true);
260 case SituareError::DATA_RETRIEVAL_FAILED:
261 m_ui->toggleProgressIndicator(false);
262 m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
264 case SituareError::ADDRESS_RETRIEVAL_FAILED:
265 m_ui->buildInformationBox(tr("Address retrieval failed"), true);
267 case SituareError::IMAGE_DOWNLOAD_FAILED:
268 m_ui->buildInformationBox(tr("Image download failed"), true);
270 case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
271 m_ui->buildInformationBox(tr("Map image download failed"), true);
273 case SituareError::GPS_INITIALIZATION_FAILED:
275 m_ui->buildInformationBox(tr("GPS initialization failed"), true);
277 case SituareError::UNKNOWN_REPLY:
278 m_ui->toggleProgressIndicator(false);
279 m_ui->buildInformationBox(tr("Unknown server response"), 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
287 m_ui->toggleProgressIndicator(false);
288 qCritical() << "QNetworkReply::NetworkError :" << error;
293 void SituareEngine::fetchUsernameFromSettings()
295 qDebug() << __PRETTY_FUNCTION__;
297 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
300 void SituareEngine::initializeGpsAndAutocentering()
302 qDebug() << __PRETTY_FUNCTION__;
304 QSettings settings(DIRECTORY_NAME, FILE_NAME);
305 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
306 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
308 if (m_gps->isInitialized()) {
310 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
312 connect(m_gps, SIGNAL(position(QPointF,qreal)),
313 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
315 changeAutoCenteringSetting(true);
318 m_ui->buildInformationBox(tr("GPS enabled"));
319 m_ui->buildInformationBox(tr("Auto centering enabled"));
321 } else { // Normal start
322 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
323 enableGPS(gpsEnabled.toBool());
325 if (gpsEnabled.toBool())
326 m_ui->buildInformationBox(tr("GPS enabled"));
328 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
329 m_ui->buildInformationBox(tr("Auto centering enabled"));
336 void SituareEngine::loginActionPressed()
338 qDebug() << __PRETTY_FUNCTION__;
340 if(m_ui->loginState()) {
342 m_situareService->clearUserData();
344 m_facebookAuthenticator->start();
348 void SituareEngine::loginOk()
350 qDebug() << __PRETTY_FUNCTION__;
352 m_ui->loggedIn(true);
355 m_situareService->fetchLocations(); // request user locations
357 if (m_gps->isRunning())
358 m_ui->readAutomaticLocationUpdateSettings();
361 void SituareEngine::loginProcessCancelled()
363 qDebug() << __PRETTY_FUNCTION__;
365 m_ui->toggleProgressIndicator(false);
366 m_ui->updateItemVisibility();
369 void SituareEngine::logout()
371 qDebug() << __PRETTY_FUNCTION__;
373 m_ui->loggedIn(false);
375 // signal to clear locationUpdateDialog's data
376 connect(this, SIGNAL(clearUpdateLocationDialogData()),
377 m_ui, SIGNAL(clearUpdateLocationDialogData()));
378 emit clearUpdateLocationDialogData();
380 m_facebookAuthenticator->clearAccountInformation(); // clear all
381 m_automaticUpdateFirstStart = true;
384 void SituareEngine::refreshUserData()
386 qDebug() << __PRETTY_FUNCTION__;
388 m_ui->toggleProgressIndicator(true);
390 m_situareService->fetchLocations();
393 void SituareEngine::requestAddress()
395 qDebug() << __PRETTY_FUNCTION__;
397 if (m_gps->isRunning())
398 m_situareService->reverseGeo(m_gps->lastPosition());
400 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
403 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
405 qDebug() << __PRETTY_FUNCTION__;
407 m_ui->toggleProgressIndicator(true);
409 if (m_gps->isRunning())
410 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
412 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
415 void SituareEngine::requestAutomaticUpdateIfMoved(QPointF position)
417 qDebug() << __PRETTY_FUNCTION__;
419 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
420 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
421 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
422 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
424 m_lastUpdatedGPSPosition = position;
428 if (m_automaticUpdateRequest && m_userMoved) {
429 requestUpdateLocation(tr("Automatic location update."));
430 m_automaticUpdateRequest = false;
435 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
437 qDebug() << __PRETTY_FUNCTION__;
439 Q_UNUSED(latLonCoordinate);
442 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
443 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
445 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
446 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
449 void SituareEngine::signalsFromFacebookAuthenticator()
451 qDebug() << __PRETTY_FUNCTION__;
453 connect(m_facebookAuthenticator, SIGNAL(error(int)),
454 this, SLOT(error(int)));
456 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
457 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
459 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
460 this, SLOT(loginOk()));
462 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
463 m_ui, SLOT(startLoginProcess()));
465 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
466 m_ui, SLOT(saveCookies()));
468 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
469 m_ui, SLOT(loginUsingCookies()));
472 void SituareEngine::signalsFromGPS()
474 qDebug() << __PRETTY_FUNCTION__;
476 connect(m_gps, SIGNAL(position(QPointF,qreal)),
477 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
479 connect(m_gps, SIGNAL(timeout()),
480 m_ui, SLOT(gpsTimeout()));
482 connect(m_gps, SIGNAL(error(int)),
483 this, SLOT(error(int)));
486 void SituareEngine::signalsFromMainWindow()
488 qDebug() << __PRETTY_FUNCTION__;
490 connect(m_ui, SIGNAL(error(int)),
491 this, SLOT(error(int)));
493 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
494 this, SLOT(fetchUsernameFromSettings()));
496 connect(m_ui, SIGNAL(loginActionPressed()),
497 this, SLOT(loginActionPressed()));
499 connect(m_ui, SIGNAL(saveUsername(QString)),
500 m_facebookAuthenticator, SLOT(saveUsername(QString)));
502 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
503 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
505 // signals from map view
506 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
507 m_mapEngine, SLOT(setLocation(QPoint)));
509 connect(m_ui, SIGNAL(mapViewResized(QSize)),
510 m_mapEngine, SLOT(viewResized(QSize)));
512 connect(m_ui, SIGNAL(viewZoomFinished()),
513 m_mapEngine, SLOT(viewZoomFinished()));
515 // signals from zoom buttons (zoom panel and volume buttons)
516 connect(m_ui, SIGNAL(zoomIn()),
517 m_mapEngine, SLOT(zoomIn()));
519 connect(m_ui, SIGNAL(zoomOut()),
520 m_mapEngine, SLOT(zoomOut()));
522 // signals from menu buttons
523 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
524 this, SLOT(changeAutoCenteringSetting(bool)));
526 connect(m_ui, SIGNAL(gpsTriggered(bool)),
527 this, SLOT(enableGPS(bool)));
529 //signals from dialogs
530 connect(m_ui, SIGNAL(cancelLoginProcess()),
531 this, SLOT(loginProcessCancelled()));
533 connect(m_ui, SIGNAL(requestReverseGeo()),
534 this, SLOT(requestAddress()));
536 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
537 this, SLOT(requestUpdateLocation(QString,bool)));
539 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
540 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
542 // signals from user info tab
543 connect(m_ui, SIGNAL(refreshUserData()),
544 this, SLOT(refreshUserData()));
546 connect(m_ui, SIGNAL(findUser(QPointF)),
547 m_mapEngine, SLOT(setViewLocation(QPointF)));
549 // signals from friend list tab
550 connect(m_ui, SIGNAL(findFriend(QPointF)),
551 m_mapEngine, SLOT(setViewLocation(QPointF)));
554 void SituareEngine::signalsFromMapEngine()
556 qDebug() << __PRETTY_FUNCTION__;
558 connect(m_mapEngine, SIGNAL(error(int)),
559 this, SLOT(error(int)));
561 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
562 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
564 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
565 m_ui, SIGNAL(zoomLevelChanged(int)));
567 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
568 this, SLOT(disableAutoCentering()));
570 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
571 m_ui, SIGNAL(maxZoomLevelReached()));
573 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
574 m_ui, SIGNAL(minZoomLevelReached()));
576 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
577 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
579 connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
580 m_ui, SIGNAL(newMapResolution(qreal)));
583 void SituareEngine::signalsFromSituareService()
585 qDebug() << __PRETTY_FUNCTION__;
587 connect(m_situareService, SIGNAL(error(int)),
588 this, SLOT(error(int)));
590 connect(m_situareService, SIGNAL(error(int)),
591 m_ui, SIGNAL(messageSendingFailed(int)));
593 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
594 m_ui, SIGNAL(reverseGeoReady(QString)));
596 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
597 this, SLOT(userDataChanged(User*, QList<User*>&)));
599 connect(m_situareService, SIGNAL(updateWasSuccessful()),
600 this, SLOT(updateWasSuccessful()));
602 connect(m_situareService, SIGNAL(updateWasSuccessful()),
603 m_ui, SIGNAL(clearUpdateLocationDialogData()));
606 void SituareEngine::startAutomaticUpdate()
608 qDebug() << __PRETTY_FUNCTION__;
610 m_gps->requestUpdate();
611 m_automaticUpdateRequest = true;
614 void SituareEngine::updateWasSuccessful()
616 qDebug() << __PRETTY_FUNCTION__;
618 m_situareService->fetchLocations();
621 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
623 qDebug() << __PRETTY_FUNCTION__;
625 m_ui->toggleProgressIndicator(false);
628 emit userLocationReady(user);
629 emit friendsLocationsReady(friendsList);