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,
27 #include "facebookservice/facebookauthentication.h"
28 #include "gps/gpsposition.h"
29 #include "map/mapengine.h"
30 #include "situareservice/situareservice.h"
31 #include "ui/mainwindow.h"
36 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED"; ///< GPS setting
37 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";///< Auto centering setting
38 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12; ///< Default zoom level when GPS available
39 const qreal USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE = 0.003;///< Min value for user move latitude
40 const qreal USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE = 0.001;///< Min value for user move longitude
42 SituareEngine::SituareEngine(QMainWindow *parent)
44 m_autoCenteringEnabled(false),
46 m_automaticUpdateIntervalTimer(0),
47 m_lastUpdatedGPSPosition(QPointF()),
50 qDebug() << __PRETTY_FUNCTION__;
51 m_ui = new MainWindow;
52 m_ui->updateItemVisibility(m_loggedIn);
55 m_mapEngine = new MapEngine(this);
56 m_ui->setMapViewScene(m_mapEngine->scene());
59 m_gps = new GPSPosition(this);
60 m_gps->setMode(GPSPosition::Default);
62 // build SituareService
63 m_situareService = new SituareService(this);
65 // build FacebookAuthenticator
66 m_facebookAuthenticator = new FacebookAuthentication(this);
69 signalsFromMapEngine();
71 signalsFromSituareService();
72 signalsFromMainWindow();
73 signalsFromFacebookAuthenticator();
75 connect(this, SIGNAL(userLocationReady(User*)),
76 m_ui, SIGNAL(userLocationReady(User*)));
78 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
79 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
81 connect(this, SIGNAL(userLocationReady(User*)),
82 m_mapEngine, SLOT(receiveOwnLocation(User*)));
84 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
85 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
87 initializeGpsAndAutocentering();
89 // signals connected, now it's time to show the main window
90 // but init the MapEngine before so starting location is set
94 m_facebookAuthenticator->start();
96 m_automaticUpdateIntervalTimer = new QTimer(this);
97 connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
98 this, SLOT(automaticUpdateIntervalTimerTimeout()));
101 SituareEngine::~SituareEngine()
103 qDebug() << __PRETTY_FUNCTION__;
107 QSettings settings(DIRECTORY_NAME, FILE_NAME);
108 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
109 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
110 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
113 void SituareEngine::automaticUpdateIntervalTimerTimeout()
115 qDebug() << __PRETTY_FUNCTION__;
117 if (m_gps->isRunning() && m_userMoved) {
118 requestUpdateLocation();
123 void SituareEngine::changeAutoCenteringSetting(bool enabled)
125 qDebug() << __PRETTY_FUNCTION__;
127 m_autoCenteringEnabled = enabled;
128 enableAutoCentering(enabled);
131 void SituareEngine::disableAutoCentering()
133 qDebug() << __PRETTY_FUNCTION__;
135 changeAutoCenteringSetting(false);
136 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
139 void SituareEngine::enableAutoCentering(bool enabled)
141 qDebug() << __PRETTY_FUNCTION__;
143 m_ui->setAutoCenteringButtonEnabled(enabled);
144 m_mapEngine->setAutoCentering(enabled);
147 m_gps->requestLastPosition();
150 void SituareEngine::enableGPS(bool enabled)
152 qDebug() << __PRETTY_FUNCTION__;
154 m_ui->setGPSButtonEnabled(enabled);
155 m_mapEngine->setGPSEnabled(enabled);
159 enableAutoCentering(m_autoCenteringEnabled);
160 m_gps->requestLastPosition();
164 enableAutoCentering(false);
168 void SituareEngine::enableAutomaticLocationUpdate(bool enabled, int updateIntervalMsecs)
170 qDebug() << __PRETTY_FUNCTION__;
172 if (m_automaticUpdateIntervalTimer) {
174 if (enabled && m_gps->isRunning()) {
175 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
176 m_automaticUpdateIntervalTimer->start();
179 m_automaticUpdateIntervalTimer->stop();
183 void SituareEngine::error(const QString &error)
185 qDebug() << __PRETTY_FUNCTION__;
187 m_ui->showMaemoInformationBox(error, true);
189 if(error.compare(SESSION_EXPIRED) == 0) {
190 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
191 m_ui->loggedIn(false);
196 void SituareEngine::fetchUsernameFromSettings()
198 qDebug() << __PRETTY_FUNCTION__;
200 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
203 void SituareEngine::initializeGpsAndAutocentering()
205 qDebug() << __PRETTY_FUNCTION__;
207 QSettings settings(DIRECTORY_NAME, FILE_NAME);
208 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
209 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
211 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
213 connect(m_gps, SIGNAL(position(QPointF,qreal)),
214 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
216 changeAutoCenteringSetting(true);
219 m_ui->showMaemoInformationBox(tr("GPS enabled"));
220 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
221 } else { // Normal start
222 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
223 enableGPS(gpsEnabled.toBool());
225 if (gpsEnabled.toBool())
226 m_ui->showMaemoInformationBox(tr("GPS enabled"));
228 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
229 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
233 bool SituareEngine::isUserMoved()
235 qDebug() << __PRETTY_FUNCTION__;
240 void SituareEngine::loginActionPressed()
242 qDebug() << __PRETTY_FUNCTION__;
246 m_situareService->clearUserData();
249 m_facebookAuthenticator->start();
253 void SituareEngine::loginOk()
255 qDebug() << __PRETTY_FUNCTION__;
258 m_ui->loggedIn(m_loggedIn);
261 m_situareService->fetchLocations(); // request user locations
264 void SituareEngine::loginProcessCancelled()
266 qDebug() << __PRETTY_FUNCTION__;
268 m_ui->toggleProgressIndicator(false);
269 m_ui->updateItemVisibility(m_loggedIn);
272 void SituareEngine::logout()
274 qDebug() << __PRETTY_FUNCTION__;
277 m_ui->loggedIn(m_loggedIn);
278 m_facebookAuthenticator->clearAccountInformation(); // clear all
281 void SituareEngine::refreshUserData()
283 qDebug() << __PRETTY_FUNCTION__;
285 m_ui->toggleProgressIndicator(true);
287 m_situareService->fetchLocations();
290 void SituareEngine::requestAddress()
292 qDebug() << __PRETTY_FUNCTION__;
294 if (m_gps->isRunning())
295 m_situareService->reverseGeo(m_gps->lastPosition());
297 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
300 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
302 qDebug() << __PRETTY_FUNCTION__;
304 m_ui->toggleProgressIndicator(true);
306 if (m_gps->isRunning())
307 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
309 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
312 void SituareEngine::saveGPSPosition(QPointF position)
314 qDebug() << __PRETTY_FUNCTION__;
316 if ((fabs(m_lastUpdatedGPSPosition.x() - position.x()) >
317 USER_MOVEMENT_MINIMUM_LONGITUDE_DIFFERENCE) ||
318 (fabs(m_lastUpdatedGPSPosition.y() - position.y()) >
319 USER_MOVEMENT_MINIMUM_LATITUDE_DIFFERENCE)) {
321 m_lastUpdatedGPSPosition = position;
326 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
328 qDebug() << __PRETTY_FUNCTION__;
330 Q_UNUSED(latLonCoordinate);
333 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
334 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
336 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
337 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
340 void SituareEngine::signalsFromFacebookAuthenticator()
342 qDebug() << __PRETTY_FUNCTION__;
344 connect(m_facebookAuthenticator, SIGNAL(error(QString)),
345 this, SLOT(error(QString)));
347 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
348 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
350 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
351 this, SLOT(loginOk()));
353 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
354 m_ui, SLOT(startLoginProcess(QUrl)));
356 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
357 m_ui, SLOT(loginFailed()));
359 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
360 m_ui, SLOT(saveCookies()));
362 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
363 m_ui, SLOT(loginUsingCookies()));
366 void SituareEngine::signalsFromGPS()
368 qDebug() << __PRETTY_FUNCTION__;
370 connect(m_gps, SIGNAL(position(QPointF,qreal)),
371 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
373 connect(m_gps, SIGNAL(timeout()),
374 m_ui, SLOT(gpsTimeout()));
376 connect(m_gps, SIGNAL(error(QString)),
377 this, SLOT(error(QString)));
379 connect(m_gps, SIGNAL(position(QPointF,qreal)),
380 this, SLOT(saveGPSPosition(QPointF)));
383 void SituareEngine::signalsFromMainWindow()
385 qDebug() << __PRETTY_FUNCTION__;
387 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
388 this, SLOT(fetchUsernameFromSettings()));
390 connect(m_ui, SIGNAL(loginActionPressed()),
391 this, SLOT(loginActionPressed()));
393 connect(m_ui, SIGNAL(saveUsername(QString)),
394 m_facebookAuthenticator, SLOT(saveUsername(QString)));
396 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
397 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
399 // signals from map view
400 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
401 m_mapEngine, SLOT(setLocation(QPoint)));
403 connect(m_ui, SIGNAL(mapViewResized(QSize)),
404 m_mapEngine, SLOT(viewResized(QSize)));
406 connect(m_ui, SIGNAL(viewZoomFinished()),
407 m_mapEngine, SLOT(viewZoomFinished()));
409 // signals from zoom buttons (zoom panel and volume buttons)
410 connect(m_ui, SIGNAL(zoomIn()),
411 m_mapEngine, SLOT(zoomIn()));
413 connect(m_ui, SIGNAL(zoomOut()),
414 m_mapEngine, SLOT(zoomOut()));
416 // signals from menu buttons
417 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
418 this, SLOT(changeAutoCenteringSetting(bool)));
420 connect(m_ui, SIGNAL(gpsTriggered(bool)),
421 this, SLOT(enableGPS(bool)));
423 //signals from dialogs
424 connect(m_ui, SIGNAL(cancelLoginProcess()),
425 this, SLOT(loginProcessCancelled()));
427 connect(m_ui, SIGNAL(requestReverseGeo()),
428 this, SLOT(requestAddress()));
430 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
431 this, SLOT(requestUpdateLocation(QString,bool)));
433 connect(m_ui, SIGNAL(enableAutomaticLocationUpdate(bool, int)),
434 this, SLOT(enableAutomaticLocationUpdate(bool, int)));
436 // signals from user info tab
437 connect(m_ui, SIGNAL(refreshUserData()),
438 this, SLOT(refreshUserData()));
440 connect(m_ui, SIGNAL(findUser(QPointF)),
441 m_mapEngine, SLOT(setViewLocation(QPointF)));
443 // signals from friend list tab
444 connect(m_ui, SIGNAL(findFriend(QPointF)),
445 m_mapEngine, SLOT(setViewLocation(QPointF)));
448 void SituareEngine::signalsFromMapEngine()
450 qDebug() << __PRETTY_FUNCTION__;
452 connect(m_mapEngine, SIGNAL(error(QString)),
453 this, SLOT(error(QString)));
455 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
456 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
458 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
459 m_ui, SIGNAL(zoomLevelChanged(int)));
461 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
462 this, SLOT(disableAutoCentering()));
464 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
465 m_ui, SIGNAL(maxZoomLevelReached()));
467 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
468 m_ui, SIGNAL(minZoomLevelReached()));
470 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
471 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
474 void SituareEngine::signalsFromSituareService()
476 qDebug() << __PRETTY_FUNCTION__;
478 connect(m_situareService, SIGNAL(error(QString)),
479 this, SLOT(error(QString)));
481 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
482 m_ui, SIGNAL(reverseGeoReady(QString)));
484 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
485 this, SLOT(userDataChanged(User*, QList<User*>&)));
487 connect(m_situareService, SIGNAL(updateWasSuccessful()),
488 this, SLOT(updateWasSuccessful()));
490 connect(m_situareService, SIGNAL(updateWasSuccessful()),
491 m_ui, SIGNAL(messageUpdatedToSituare()));
493 connect(m_situareService, SIGNAL(error(QString)),
494 m_ui, SIGNAL(messageSendingFailed(QString)));
497 void SituareEngine::updateWasSuccessful()
499 qDebug() << __PRETTY_FUNCTION__;
501 m_situareService->fetchLocations();
504 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
506 qDebug() << __PRETTY_FUNCTION__;
508 m_ui->toggleProgressIndicator(false);
510 emit userLocationReady(user);
511 emit friendsLocationsReady(friendsList);