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>
28 #include "facebookservice/facebookauthentication.h"
29 #include "gps/gpsposition.h"
30 #include "map/mapengine.h"
31 #include "situareservice/situareservice.h"
32 #include "ui/mainwindow.h"
37 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
38 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
39 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12; ///< Default zoom level when GPS available
40 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
41 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
42 const int MIN_UPDATE_INTERVAL_MSECS = 5*60*1000;
44 SituareEngine::SituareEngine(QMainWindow *parent)
46 m_autoCenteringEnabled(false),
47 m_automaticUpdateFirstStart(true),
49 m_automaticUpdateIntervalTimer(0),
50 m_lastUpdatedGPSPosition(QPointF()),
52 m_automaticUpdateEnabled(false)
54 qDebug() << __PRETTY_FUNCTION__;
55 m_ui = new MainWindow;
56 m_ui->updateItemVisibility(m_loggedIn);
59 m_mapEngine = new MapEngine(this);
60 m_ui->setMapViewScene(m_mapEngine->scene());
63 m_gps = new GPSPosition(this);
64 m_gps->setMode(GPSPosition::Default);
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 initializeGpsAndAutocentering();
105 SituareEngine::~SituareEngine()
107 qDebug() << __PRETTY_FUNCTION__;
111 QSettings settings(DIRECTORY_NAME, FILE_NAME);
112 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
113 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
116 void SituareEngine::automaticUpdateIntervalTimerTimeout()
118 qDebug() << __PRETTY_FUNCTION__;
120 if (m_gps->isRunning() && m_userMoved) {
121 requestUpdateLocation();
126 void SituareEngine::changeAutoCenteringSetting(bool enabled)
128 qDebug() << __PRETTY_FUNCTION__;
130 m_autoCenteringEnabled = enabled;
131 enableAutoCentering(enabled);
134 void SituareEngine::disableAutoCentering()
136 qDebug() << __PRETTY_FUNCTION__;
138 changeAutoCenteringSetting(false);
139 m_ui->buildInformationBox(tr("Auto centering disabled"));
142 void SituareEngine::enableAutoCentering(bool enabled)
144 qDebug() << __PRETTY_FUNCTION__;
146 m_ui->setAutoCenteringButtonEnabled(enabled);
147 m_mapEngine->setAutoCentering(enabled);
150 m_gps->requestLastPosition();
153 void SituareEngine::enableGPS(bool enabled)
155 qDebug() << __PRETTY_FUNCTION__;
157 m_ui->setGPSButtonEnabled(enabled);
158 m_mapEngine->setGPSEnabled(enabled);
160 if (enabled && !m_gps->isRunning()) {
162 enableAutoCentering(m_autoCenteringEnabled);
163 m_gps->requestLastPosition();
165 if (!m_automaticUpdateEnabled && m_loggedIn)
166 m_ui->requestAutomaticLocationUpdateSettings();
168 else if (!enabled && m_gps->isRunning()) {
170 enableAutoCentering(false);
171 enableAutomaticLocationUpdate(false);
175 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
177 qDebug() << __PRETTY_FUNCTION__;
179 bool accepted = true;
181 m_automaticUpdateEnabled = enabled;
183 if (m_automaticUpdateFirstStart && m_gps->isRunning() && m_automaticUpdateEnabled) {
184 accepted = m_ui->showEnableAutomaticUpdateLocationDialog(
185 tr("Do you want to enable automatic location update with %1 min update interval?")
186 .arg(updateIntervalMsecs/1000/60));
187 m_automaticUpdateFirstStart = false;
188 m_ui->automaticLocationUpdateEnabled(accepted);
190 if (accepted && !m_gps->isRunning()) {
192 m_ui->buildInformationBox(tr("GPS enabled"));
196 if (!m_automaticUpdateFirstStart && m_automaticUpdateEnabled && accepted)
197 m_ui->buildInformationBox(tr("Automatic location update enabled"));
199 if (accepted && m_gps->isRunning() && m_automaticUpdateEnabled) {
200 if (updateIntervalMsecs < MIN_UPDATE_INTERVAL_MSECS)
201 m_automaticUpdateIntervalTimer->setInterval(MIN_UPDATE_INTERVAL_MSECS);
203 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
205 m_automaticUpdateIntervalTimer->start();
208 m_automaticUpdateIntervalTimer->stop();
212 void SituareEngine::error(const QString &error)
214 qDebug() << __PRETTY_FUNCTION__;
215 qDebug() << "ERROR MESSAGE: " << error;
217 m_ui->buildInformationBox(error, true);
219 if(error.compare(SESSION_EXPIRED) == 0) {
220 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
221 m_ui->loggedIn(false);
226 void SituareEngine::fetchUsernameFromSettings()
228 qDebug() << __PRETTY_FUNCTION__;
230 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
233 void SituareEngine::initializeGpsAndAutocentering()
235 qDebug() << __PRETTY_FUNCTION__;
237 QSettings settings(DIRECTORY_NAME, FILE_NAME);
238 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
239 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
241 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
243 connect(m_gps, SIGNAL(position(QPointF,qreal)),
244 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
246 changeAutoCenteringSetting(true);
249 m_ui->buildInformationBox(tr("GPS enabled"));
250 m_ui->buildInformationBox(tr("Auto centering enabled"));
252 } else { // Normal start
253 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
254 enableGPS(gpsEnabled.toBool());
256 if (gpsEnabled.toBool())
257 m_ui->buildInformationBox(tr("GPS enabled"));
259 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
260 m_ui->buildInformationBox(tr("Auto centering enabled"));
264 bool SituareEngine::isUserMoved()
266 qDebug() << __PRETTY_FUNCTION__;
271 void SituareEngine::loginActionPressed()
273 qDebug() << __PRETTY_FUNCTION__;
277 m_situareService->clearUserData();
280 m_facebookAuthenticator->start();
284 void SituareEngine::loginOk()
286 qDebug() << __PRETTY_FUNCTION__;
289 m_ui->loggedIn(m_loggedIn);
292 m_situareService->fetchLocations(); // request user locations
294 if (m_gps->isRunning())
295 m_ui->requestAutomaticLocationUpdateSettings();
298 void SituareEngine::loginProcessCancelled()
300 qDebug() << __PRETTY_FUNCTION__;
302 m_ui->toggleProgressIndicator(false);
303 m_ui->updateItemVisibility(m_loggedIn);
306 void SituareEngine::logout()
308 qDebug() << __PRETTY_FUNCTION__;
311 m_ui->loggedIn(m_loggedIn);
312 m_facebookAuthenticator->clearAccountInformation(); // clear all
315 void SituareEngine::refreshUserData()
317 qDebug() << __PRETTY_FUNCTION__;
319 m_ui->toggleProgressIndicator(true);
321 m_situareService->fetchLocations();
324 void SituareEngine::requestAddress()
326 qDebug() << __PRETTY_FUNCTION__;
328 if (m_gps->isRunning())
329 m_situareService->reverseGeo(m_gps->lastPosition());
331 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
334 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
336 qDebug() << __PRETTY_FUNCTION__;
338 m_ui->toggleProgressIndicator(true);
340 if (m_gps->isRunning())
341 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
343 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
346 void SituareEngine::saveGPSPosition(QPointF position)
348 qDebug() << __PRETTY_FUNCTION__;
350 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
351 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
352 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
353 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
355 m_lastUpdatedGPSPosition = position;
360 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
362 qDebug() << __PRETTY_FUNCTION__;
364 Q_UNUSED(latLonCoordinate);
367 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
368 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
370 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
371 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
374 void SituareEngine::signalsFromFacebookAuthenticator()
376 qDebug() << __PRETTY_FUNCTION__;
378 connect(m_facebookAuthenticator, SIGNAL(error(QString)),
379 this, SLOT(error(QString)));
381 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
382 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
384 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
385 this, SLOT(loginOk()));
387 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest()),
388 m_ui, SLOT(startLoginProcess()));
390 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
391 m_ui, SLOT(loginFailed()));
393 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
394 m_ui, SLOT(saveCookies()));
396 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
397 m_ui, SLOT(loginUsingCookies()));
400 void SituareEngine::signalsFromGPS()
402 qDebug() << __PRETTY_FUNCTION__;
404 connect(m_gps, SIGNAL(position(QPointF,qreal)),
405 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
407 connect(m_gps, SIGNAL(timeout()),
408 m_ui, SLOT(gpsTimeout()));
410 connect(m_gps, SIGNAL(error(QString)),
411 this, SLOT(error(QString)));
413 connect(m_gps, SIGNAL(position(QPointF,qreal)),
414 this, SLOT(saveGPSPosition(QPointF)));
417 void SituareEngine::signalsFromMainWindow()
419 qDebug() << __PRETTY_FUNCTION__;
421 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
422 this, SLOT(fetchUsernameFromSettings()));
424 connect(m_ui, SIGNAL(loginActionPressed()),
425 this, SLOT(loginActionPressed()));
427 connect(m_ui, SIGNAL(saveUsername(QString)),
428 m_facebookAuthenticator, SLOT(saveUsername(QString)));
430 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
431 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
433 // signals from map view
434 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
435 m_mapEngine, SLOT(setLocation(QPoint)));
437 connect(m_ui, SIGNAL(mapViewResized(QSize)),
438 m_mapEngine, SLOT(viewResized(QSize)));
440 connect(m_ui, SIGNAL(viewZoomFinished()),
441 m_mapEngine, SLOT(viewZoomFinished()));
443 // signals from zoom buttons (zoom panel and volume buttons)
444 connect(m_ui, SIGNAL(zoomIn()),
445 m_mapEngine, SLOT(zoomIn()));
447 connect(m_ui, SIGNAL(zoomOut()),
448 m_mapEngine, SLOT(zoomOut()));
450 // signals from menu buttons
451 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
452 this, SLOT(changeAutoCenteringSetting(bool)));
454 connect(m_ui, SIGNAL(gpsTriggered(bool)),
455 this, SLOT(enableGPS(bool)));
457 //signals from dialogs
458 connect(m_ui, SIGNAL(cancelLoginProcess()),
459 this, SLOT(loginProcessCancelled()));
461 connect(m_ui, SIGNAL(requestReverseGeo()),
462 this, SLOT(requestAddress()));
464 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
465 this, SLOT(requestUpdateLocation(QString,bool)));
467 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
468 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
470 // signals from user info tab
471 connect(m_ui, SIGNAL(refreshUserData()),
472 this, SLOT(refreshUserData()));
474 connect (m_ui, SIGNAL(notificateUpdateFailing(QString)),
475 this, SLOT(error(QString)));
477 connect(m_ui, SIGNAL(findUser(QPointF)),
478 m_mapEngine, SLOT(setViewLocation(QPointF)));
480 // signals from friend list tab
481 connect(m_ui, SIGNAL(findFriend(QPointF)),
482 m_mapEngine, SLOT(setViewLocation(QPointF)));
485 void SituareEngine::signalsFromMapEngine()
487 qDebug() << __PRETTY_FUNCTION__;
489 connect(m_mapEngine, SIGNAL(error(QString)),
490 this, SLOT(error(QString)));
492 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
493 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
495 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
496 m_ui, SIGNAL(zoomLevelChanged(int)));
498 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
499 this, SLOT(disableAutoCentering()));
501 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
502 m_ui, SIGNAL(maxZoomLevelReached()));
504 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
505 m_ui, SIGNAL(minZoomLevelReached()));
507 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
508 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
511 void SituareEngine::signalsFromSituareService()
513 qDebug() << __PRETTY_FUNCTION__;
515 connect(m_situareService, SIGNAL(error(QString)),
516 this, SLOT(error(QString)));
518 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
519 m_ui, SIGNAL(reverseGeoReady(QString)));
521 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
522 this, SLOT(userDataChanged(User*, QList<User*>&)));
524 connect(m_situareService, SIGNAL(updateWasSuccessful()),
525 this, SLOT(updateWasSuccessful()));
527 connect(m_situareService, SIGNAL(updateWasSuccessful()),
528 m_ui, SIGNAL(updateWasSuccessful()));
530 connect(m_situareService, SIGNAL(error(QString)),
531 m_ui, SIGNAL(messageSendingFailed(QString)));
534 void SituareEngine::updateWasSuccessful()
536 qDebug() << __PRETTY_FUNCTION__;
538 m_situareService->fetchLocations();
541 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
543 qDebug() << __PRETTY_FUNCTION__;
545 m_ui->toggleProgressIndicator(false);
547 emit userLocationReady(user);
548 emit friendsLocationsReady(friendsList);