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"
35 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
36 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
38 SituareEngine::SituareEngine(QMainWindow *parent)
40 m_autoCenteringEnabled(false),
43 qDebug() << __PRETTY_FUNCTION__;
44 m_ui = new MainWindow;
45 m_ui->showPanels(m_loggedIn);
48 m_mapEngine = new MapEngine(this);
49 m_ui->setMapViewScene(m_mapEngine->scene());
52 m_gps = new GPSPosition(this);
53 m_gps->setMode(GPSPosition::Default);
55 // build SituareService
56 m_situareService = new SituareService(this);
58 // build FacebookAuthenticator
59 m_facebookAuthenticator = new FacebookAuthentication(this);
62 signalsFromMapEngine();
64 signalsFromSituareService();
65 signalsFromMainWindow();
66 signalsFromFacebookAuthenticator();
68 connect(this, SIGNAL(userLocationReady(User*)),
69 m_ui, SIGNAL(userLocationReady(User*)));
71 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
72 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
74 connect(this, SIGNAL(userLocationReady(User*)),
75 m_mapEngine, SLOT(receiveOwnLocation(User*)));
77 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
78 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
80 // signals connected, now it's time to show the main window
81 // but init the MapEngine before so starting location is set
85 QSettings settings(DIRECTORY_NAME, FILE_NAME);
86 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
87 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
89 // set features on / off based on settings
90 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
91 enableGPS(gpsEnabled.toBool());
93 // show messages at startup if features are enabled automatically
94 if (gpsEnabled.toBool())
95 m_ui->showMaemoInformationBox(tr("GPS enabled"));
96 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
97 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
99 m_facebookAuthenticator->start();
101 m_automaticUpdateIntervalTimer = new QTimer(this);
102 connect(m_automaticUpdateIntervalTimer, SIGNAL(timeout()),
103 this, SLOT(automaticUpdateIntervalTimerTimeout()));
106 SituareEngine::~SituareEngine()
108 qDebug() << __PRETTY_FUNCTION__;
112 QSettings settings(DIRECTORY_NAME, FILE_NAME);
113 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
114 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
115 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
118 void SituareEngine::automaticLocationUpdateIntervalSet(int updateIntervalMsecs)
120 qDebug() << __PRETTY_FUNCTION__;
122 if (m_automaticUpdateIntervalTimer) {
124 m_automaticUpdateIntervalTimer->stop();
126 if ((updateIntervalMsecs > 0) && m_gps->isRunning()) {
127 m_automaticUpdateIntervalTimer->setInterval(updateIntervalMsecs);
128 m_automaticUpdateIntervalTimer->start();
133 void SituareEngine::automaticUpdateIntervalTimerTimeout()
135 qDebug() << __PRETTY_FUNCTION__;
137 if (m_gps->isRunning());
138 //requestUpdateLocation();
141 void SituareEngine::changeAutoCenteringSetting(bool enabled)
143 qDebug() << __PRETTY_FUNCTION__;
145 m_autoCenteringEnabled = enabled;
146 enableAutoCentering(enabled);
149 void SituareEngine::disableAutoCentering()
151 qDebug() << __PRETTY_FUNCTION__;
153 changeAutoCenteringSetting(false);
154 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
157 void SituareEngine::enableAutoCentering(bool enabled)
159 qDebug() << __PRETTY_FUNCTION__;
161 m_ui->setAutoCenteringButtonEnabled(enabled);
162 m_mapEngine->setAutoCentering(enabled);
165 m_gps->requestLastPosition();
168 void SituareEngine::enableGPS(bool enabled)
170 qDebug() << __PRETTY_FUNCTION__;
172 m_ui->setGPSButtonEnabled(enabled);
173 m_mapEngine->setGPSEnabled(enabled);
177 enableAutoCentering(m_autoCenteringEnabled);
178 m_gps->requestLastPosition();
182 enableAutoCentering(false);
183 automaticLocationUpdateIntervalSet(-1);
187 void SituareEngine::error(const QString &error)
189 qDebug() << __PRETTY_FUNCTION__;
194 void SituareEngine::fetchUsernameFromSettings()
196 qDebug() << __PRETTY_FUNCTION__;
197 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
200 void SituareEngine::invalidCredentials()
202 qDebug() << __PRETTY_FUNCTION__;
204 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
205 m_facebookAuthenticator->start();
208 void SituareEngine::loginActionPressed()
210 qDebug() << __PRETTY_FUNCTION__;
214 m_situareService->clearUserData();
217 m_facebookAuthenticator->start();
221 void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
223 qDebug() << __PRETTY_FUNCTION__;
226 m_ui->loggedIn(m_loggedIn);
229 m_facebookAuthenticator->saveUsername(m_ui->username());
232 m_situareService->credentialsReady(credentials);
233 m_situareService->fetchLocations(); // request user locations
236 void SituareEngine::loginProcessCancelled()
238 qDebug() << __PRETTY_FUNCTION__;
240 m_ui->toggleProgressIndicator(false);
241 m_ui->showPanels(m_loggedIn);
244 void SituareEngine::logout()
246 qDebug() << __PRETTY_FUNCTION__;
249 m_ui->loggedIn(m_loggedIn);
250 m_facebookAuthenticator->clearAccountInformation();
253 void SituareEngine::refreshUserData()
255 qDebug() << __PRETTY_FUNCTION__;
257 m_ui->toggleProgressIndicator(true);
259 m_situareService->fetchLocations();
262 void SituareEngine::requestAddress()
264 qDebug() << __PRETTY_FUNCTION__;
266 if (m_gps->isRunning())
267 m_situareService->reverseGeo(m_gps->lastPosition());
269 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
272 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
274 qDebug() << __PRETTY_FUNCTION__;
276 m_ui->toggleProgressIndicator(true);
278 if (m_gps->isRunning())
279 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
281 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
284 void SituareEngine::signalsFromFacebookAuthenticator()
286 qDebug() << __PRETTY_FUNCTION__;
288 connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
289 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
291 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
292 this, SLOT(loginOk(bool, FacebookCredentials)));
294 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
295 m_ui, SLOT(startLoginProcess(QUrl)));
297 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
298 m_ui, SLOT(loginFailed()));
301 void SituareEngine::signalsFromGPS()
303 qDebug() << __PRETTY_FUNCTION__;
305 connect(m_gps, SIGNAL(position(QPointF,qreal)),
306 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
308 connect(m_gps, SIGNAL(timeout()),
309 m_ui, SLOT(gpsTimeout()));
311 connect(m_gps, SIGNAL(error(QString)),
312 m_ui, SLOT(gpsError(QString)));
315 void SituareEngine::signalsFromMainWindow()
317 qDebug() << __PRETTY_FUNCTION__;
319 connect(m_ui, SIGNAL(loginActionPressed()),
320 this, SLOT(loginActionPressed()));
322 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
323 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
325 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
326 this, SLOT(fetchUsernameFromSettings()));
328 // signals from map view
329 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
330 m_mapEngine, SLOT(setLocation(QPoint)));
332 connect(m_ui, SIGNAL(mapViewResized(QSize)),
333 m_mapEngine, SLOT(viewResized(QSize)));
335 connect(m_ui, SIGNAL(viewZoomFinished()),
336 m_mapEngine, SLOT(viewZoomFinished()));
338 // signals from zoom buttons (zoom panel and volume buttons)
339 connect(m_ui, SIGNAL(zoomIn()),
340 m_mapEngine, SLOT(zoomIn()));
342 connect(m_ui, SIGNAL(zoomOut()),
343 m_mapEngine, SLOT(zoomOut()));
345 // signals from menu buttons
346 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
347 this, SLOT(changeAutoCenteringSetting(bool)));
349 connect(m_ui, SIGNAL(gpsTriggered(bool)),
350 this, SLOT(enableGPS(bool)));
352 //signals from dialogs
353 connect(m_ui, SIGNAL(cancelLoginProcess()),
354 this, SLOT(loginProcessCancelled()));
356 connect(m_ui, SIGNAL(requestReverseGeo()),
357 this, SLOT(requestAddress()));
359 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
360 this, SLOT(requestUpdateLocation(QString,bool)));
362 connect(m_ui, SIGNAL(automaticLocationUpdateIntervalSet(int)),
363 this, SLOT(automaticLocationUpdateIntervalSet(int)));
365 // signals from user info tab
366 connect(m_ui, SIGNAL(refreshUserData()),
367 this, SLOT(refreshUserData()));
369 // signals from friend list tab
370 connect(m_ui, SIGNAL(findFriend(QPointF)),
371 m_mapEngine, SLOT(setViewLocation(QPointF)));
374 void SituareEngine::signalsFromMapEngine()
376 qDebug() << __PRETTY_FUNCTION__;
378 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
379 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
381 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
382 m_ui, SIGNAL(zoomLevelChanged(int)));
384 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
385 this, SLOT(disableAutoCentering()));
387 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
388 m_ui, SIGNAL(maxZoomLevelReached()));
390 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
391 m_ui, SIGNAL(minZoomLevelReached()));
393 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
394 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
397 void SituareEngine::signalsFromSituareService()
399 qDebug() << __PRETTY_FUNCTION__;
401 connect(m_situareService, SIGNAL(error(QString)),
402 this, SLOT(error(QString)));
404 connect(m_situareService, SIGNAL(invalidSessionCredentials()),
405 this, SLOT(invalidCredentials()));
407 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
408 m_ui, SIGNAL(reverseGeoReady(QString)));
410 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
411 this, SLOT(userDataChanged(User*, QList<User*>&)));
413 connect(m_situareService, SIGNAL(updateWasSuccessful()),
414 this, SLOT(updateWasSuccessful()));
417 void SituareEngine::updateWasSuccessful()
419 qDebug() << __PRETTY_FUNCTION__;
421 m_situareService->fetchLocations();
424 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
426 qDebug() << __PRETTY_FUNCTION__;
428 m_ui->toggleProgressIndicator(false);
430 emit userLocationReady(user);
431 emit friendsLocationsReady(friendsList);