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(automaticUpdateIntervalTimerTimeout()));
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(displayOn(bool)), this, SLOT(displayOn(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::automaticUpdateIntervalTimerTimeout()
123 qDebug() << __PRETTY_FUNCTION__;
125 m_gps->requestUpdate();
126 m_automaticUpdateRequest = true;
129 void SituareEngine::changeAutoCenteringSetting(bool enabled)
131 qDebug() << __PRETTY_FUNCTION__;
133 m_autoCenteringEnabled = enabled;
134 enableAutoCentering(enabled);
137 void SituareEngine::disableAutoCentering()
139 qDebug() << __PRETTY_FUNCTION__;
141 changeAutoCenteringSetting(false);
142 m_ui->buildInformationBox(tr("Auto centering disabled"));
145 void SituareEngine::displayOn(bool on)
147 qDebug() << __PRETTY_FUNCTION__;
149 m_gps->enablePowerSave(!on);
151 if (m_autoCenteringEnabled)
152 enableAutoCentering(on);
155 void SituareEngine::enableAutoCentering(bool enabled)
157 qDebug() << __PRETTY_FUNCTION__;
159 m_ui->setAutoCenteringButtonEnabled(enabled);
160 m_mapEngine->setAutoCentering(enabled);
163 m_gps->requestLastPosition();
166 void SituareEngine::enableGPS(bool enabled)
168 qDebug() << __PRETTY_FUNCTION__;
170 m_ui->setOwnLocationCrosshairVisibility(!enabled);
172 if (m_gps->isInitialized()) {
173 m_ui->setGPSButtonEnabled(enabled);
174 m_mapEngine->setGPSEnabled(enabled);
176 if (enabled && !m_gps->isRunning()) {
178 enableAutoCentering(m_autoCenteringEnabled);
179 m_gps->requestLastPosition();
181 if(m_ui->loginState())
182 m_ui->readAutomaticLocationUpdateSettings();
184 else if (!enabled && m_gps->isRunning()) {
186 enableAutoCentering(false);
187 enableAutomaticLocationUpdate(false);
192 m_ui->buildInformationBox(tr("Unable to start GPS"));
193 m_ui->setGPSButtonEnabled(false);
194 m_mapEngine->setGPSEnabled(false);
198 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
200 qDebug() << __PRETTY_FUNCTION__;
202 //Show automatic update confirmation dialog
203 if (m_automaticUpdateFirstStart && m_gps->isRunning() && enabled) {
204 m_ui->showEnableAutomaticUpdateLocationDialog(
205 tr("Do you want to enable automatic location update with %1 min update interval?")
206 .arg(updateIntervalMsecs/1000/60));
207 m_automaticUpdateFirstStart = false;
209 if (enabled && m_gps->isRunning()) {
210 m_ui->buildInformationBox(tr("Automatic location update enabled"));
211 if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
212 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
214 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
216 connect(m_gps, SIGNAL(position(QPointF,qreal)),
217 this, SLOT(saveGPSPosition(QPointF)));
219 m_automaticUpdateIntervalTimer->start();
222 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
223 this, SLOT(saveGPSPosition(QPointF)));
225 m_automaticUpdateIntervalTimer->stop();
230 void SituareEngine::error(const int error)
232 qDebug() << __PRETTY_FUNCTION__;
236 case QNetworkReply::ConnectionRefusedError:
237 m_ui->toggleProgressIndicator(false);
238 m_ui->buildInformationBox(tr("Connection refused by the server"), true);
240 case QNetworkReply::RemoteHostClosedError:
241 m_ui->toggleProgressIndicator(false);
242 m_ui->buildInformationBox(tr("Connection closed by the server"), true);
244 case QNetworkReply::HostNotFoundError:
245 m_ui->toggleProgressIndicator(false);
246 m_ui->buildInformationBox(tr("Remote server not found"), true);
248 case QNetworkReply::TimeoutError:
249 m_ui->toggleProgressIndicator(false);
250 m_ui->buildInformationBox(tr("Connection timed out"), true);
252 case SituareError::SESSION_EXPIRED:
253 m_ui->buildInformationBox(tr("Session expired. Please login again"), true);
254 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
255 m_situareService->clearUserData();
256 m_ui->loggedIn(false);
259 case SituareError::LOGIN_FAILED:
260 m_ui->toggleProgressIndicator(false);
261 m_ui->buildInformationBox(tr("Invalid E-mail address or password"), true);
264 case SituareError::UPDATE_FAILED:
265 m_ui->toggleProgressIndicator(false);
266 m_ui->buildInformationBox(tr("Update failed, please try again"), true);
268 case SituareError::DATA_RETRIEVAL_FAILED:
269 m_ui->toggleProgressIndicator(false);
270 m_ui->buildInformationBox(tr("Data retrieval failed, please try again"), true);
272 case SituareError::ADDRESS_RETRIEVAL_FAILED:
273 m_ui->buildInformationBox(tr("Address retrieval failed"), true);
275 case SituareError::IMAGE_DOWNLOAD_FAILED:
276 m_ui->buildInformationBox(tr("Image download failed"), true);
278 case SituareError::MAP_IMAGE_DOWNLOAD_FAILED:
279 m_ui->buildInformationBox(tr("Map image download failed"), true);
281 case SituareError::GPS_INITIALIZATION_FAILED:
283 m_ui->buildInformationBox(tr("GPS initialization failed"), true);
285 case SituareError::UNKNOWN_REPLY:
286 m_ui->toggleProgressIndicator(false);
287 m_ui->buildInformationBox(tr("Unknown server response"), true);
289 case SituareError::INVALID_JSON:
290 m_ui->buildInformationBox(tr("Malformatted reply from server"), true);
291 m_ui->loggedIn(false);
292 m_facebookAuthenticator->clearAccountInformation(false); // clean all
295 m_ui->toggleProgressIndicator(false);
296 qCritical() << "QNetworkReply::NetworkError :" << error;
301 void SituareEngine::fetchUsernameFromSettings()
303 qDebug() << __PRETTY_FUNCTION__;
305 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
308 void SituareEngine::initializeGpsAndAutocentering()
310 qDebug() << __PRETTY_FUNCTION__;
312 QSettings settings(DIRECTORY_NAME, FILE_NAME);
313 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
314 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
316 if (m_gps->isInitialized()) {
318 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
320 connect(m_gps, SIGNAL(position(QPointF,qreal)),
321 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
323 changeAutoCenteringSetting(true);
326 m_ui->buildInformationBox(tr("GPS enabled"));
327 m_ui->buildInformationBox(tr("Auto centering enabled"));
329 } else { // Normal start
330 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
331 enableGPS(gpsEnabled.toBool());
333 if (gpsEnabled.toBool())
334 m_ui->buildInformationBox(tr("GPS enabled"));
336 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
337 m_ui->buildInformationBox(tr("Auto centering enabled"));
344 void SituareEngine::loginActionPressed()
346 qDebug() << __PRETTY_FUNCTION__;
348 if(m_ui->loginState()) {
350 m_situareService->clearUserData();
352 m_facebookAuthenticator->start();
356 void SituareEngine::loginOk()
358 qDebug() << __PRETTY_FUNCTION__;
360 m_ui->loggedIn(true);
363 m_situareService->fetchLocations(); // request user locations
365 if (m_gps->isRunning())
366 m_ui->readAutomaticLocationUpdateSettings();
369 void SituareEngine::loginProcessCancelled()
371 qDebug() << __PRETTY_FUNCTION__;
373 m_ui->toggleProgressIndicator(false);
374 m_ui->updateItemVisibility();
377 void SituareEngine::logout()
379 qDebug() << __PRETTY_FUNCTION__;
381 m_ui->loggedIn(false);
383 // signal to clear locationUpdateDialog's data
384 connect(this, SIGNAL(clearUpdateLocationDialogData()),
385 m_ui, SIGNAL(clearUpdateLocationDialogData()));
386 emit clearUpdateLocationDialogData();
388 m_facebookAuthenticator->clearAccountInformation(); // clear all
389 m_automaticUpdateFirstStart = true;
392 void SituareEngine::refreshUserData()
394 qDebug() << __PRETTY_FUNCTION__;
396 m_ui->toggleProgressIndicator(true);
398 m_situareService->fetchLocations();
401 void SituareEngine::requestAddress()
403 qDebug() << __PRETTY_FUNCTION__;
405 if (m_gps->isRunning())
406 m_situareService->reverseGeo(m_gps->lastPosition());
408 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
411 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
413 qDebug() << __PRETTY_FUNCTION__;
415 m_ui->toggleProgressIndicator(true);
417 if (m_gps->isRunning())
418 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
420 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
423 void SituareEngine::saveGPSPosition(QPointF position)
425 qDebug() << __PRETTY_FUNCTION__;
427 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
428 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
429 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
430 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
432 m_lastUpdatedGPSPosition = position;
436 if (m_automaticUpdateRequest && m_userMoved) {
437 requestUpdateLocation(tr("Automatic location update."));
438 m_automaticUpdateRequest = false;
443 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
445 qDebug() << __PRETTY_FUNCTION__;
447 Q_UNUSED(latLonCoordinate);
450 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
451 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
453 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
454 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
457 void SituareEngine::signalsFromFacebookAuthenticator()
459 qDebug() << __PRETTY_FUNCTION__;
461 connect(m_facebookAuthenticator, SIGNAL(error(int)),
462 this, SLOT(error(int)));
464 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
465 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
467 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
468 this, SLOT(loginOk()));
470 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
471 m_ui, SLOT(startLoginProcess()));
473 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
474 m_ui, SLOT(saveCookies()));
476 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
477 m_ui, SLOT(loginUsingCookies()));
480 void SituareEngine::signalsFromGPS()
482 qDebug() << __PRETTY_FUNCTION__;
484 connect(m_gps, SIGNAL(position(QPointF,qreal)),
485 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
487 connect(m_gps, SIGNAL(timeout()),
488 m_ui, SLOT(gpsTimeout()));
490 connect(m_gps, SIGNAL(error(int)),
491 this, SLOT(error(int)));
494 void SituareEngine::signalsFromMainWindow()
496 qDebug() << __PRETTY_FUNCTION__;
498 connect(m_ui, SIGNAL(error(int)),
499 this, SLOT(error(int)));
501 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
502 this, SLOT(fetchUsernameFromSettings()));
504 connect(m_ui, SIGNAL(loginActionPressed()),
505 this, SLOT(loginActionPressed()));
507 connect(m_ui, SIGNAL(saveUsername(QString)),
508 m_facebookAuthenticator, SLOT(saveUsername(QString)));
510 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
511 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
513 // signals from map view
514 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
515 m_mapEngine, SLOT(setLocation(QPoint)));
517 connect(m_ui, SIGNAL(mapViewResized(QSize)),
518 m_mapEngine, SLOT(viewResized(QSize)));
520 connect(m_ui, SIGNAL(viewZoomFinished()),
521 m_mapEngine, SLOT(viewZoomFinished()));
523 // signals from zoom buttons (zoom panel and volume buttons)
524 connect(m_ui, SIGNAL(zoomIn()),
525 m_mapEngine, SLOT(zoomIn()));
527 connect(m_ui, SIGNAL(zoomOut()),
528 m_mapEngine, SLOT(zoomOut()));
530 // signals from menu buttons
531 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
532 this, SLOT(changeAutoCenteringSetting(bool)));
534 connect(m_ui, SIGNAL(gpsTriggered(bool)),
535 this, SLOT(enableGPS(bool)));
537 //signals from dialogs
538 connect(m_ui, SIGNAL(cancelLoginProcess()),
539 this, SLOT(loginProcessCancelled()));
541 connect(m_ui, SIGNAL(requestReverseGeo()),
542 this, SLOT(requestAddress()));
544 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
545 this, SLOT(requestUpdateLocation(QString,bool)));
547 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
548 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
550 // signals from user info tab
551 connect(m_ui, SIGNAL(refreshUserData()),
552 this, SLOT(refreshUserData()));
554 connect(m_ui, SIGNAL(findUser(QPointF)),
555 m_mapEngine, SLOT(setViewLocation(QPointF)));
557 // signals from friend list tab
558 connect(m_ui, SIGNAL(findFriend(QPointF)),
559 m_mapEngine, SLOT(setViewLocation(QPointF)));
562 void SituareEngine::signalsFromMapEngine()
564 qDebug() << __PRETTY_FUNCTION__;
566 connect(m_mapEngine, SIGNAL(error(int)),
567 this, SLOT(error(int)));
569 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
570 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
572 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
573 m_ui, SIGNAL(zoomLevelChanged(int)));
575 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
576 this, SLOT(disableAutoCentering()));
578 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
579 m_ui, SIGNAL(maxZoomLevelReached()));
581 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
582 m_ui, SIGNAL(minZoomLevelReached()));
584 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
585 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
587 connect(m_mapEngine, SIGNAL(newMapResolution(qreal)),
588 m_ui, SIGNAL(newMapResolution(qreal)));
591 void SituareEngine::signalsFromSituareService()
593 qDebug() << __PRETTY_FUNCTION__;
595 connect(m_situareService, SIGNAL(error(int)),
596 this, SLOT(error(int)));
598 connect(m_situareService, SIGNAL(error(int)),
599 m_ui, SIGNAL(messageSendingFailed(int)));
601 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
602 m_ui, SIGNAL(reverseGeoReady(QString)));
604 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
605 this, SLOT(userDataChanged(User*, QList<User*>&)));
607 connect(m_situareService, SIGNAL(updateWasSuccessful()),
608 this, SLOT(updateWasSuccessful()));
610 connect(m_situareService, SIGNAL(updateWasSuccessful()),
611 m_ui, SIGNAL(clearUpdateLocationDialogData()));
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);