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 initializeGpsAndAutocentering();
82 // signals connected, now it's time to show the main window
83 // but init the MapEngine before so starting location is set
87 m_facebookAuthenticator->start();
90 SituareEngine::~SituareEngine()
92 qDebug() << __PRETTY_FUNCTION__;
96 QSettings settings(DIRECTORY_NAME, FILE_NAME);
97 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
98 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
99 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
102 void SituareEngine::changeAutoCenteringSetting(bool enabled)
104 qDebug() << __PRETTY_FUNCTION__;
106 m_autoCenteringEnabled = enabled;
107 enableAutoCentering(enabled);
111 void SituareEngine::disableAutoCentering()
113 qDebug() << __PRETTY_FUNCTION__;
115 changeAutoCenteringSetting(false);
116 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
119 void SituareEngine::enableAutoCentering(bool enabled)
121 qDebug() << __PRETTY_FUNCTION__;
123 m_ui->setAutoCenteringButtonEnabled(enabled);
124 m_mapEngine->setAutoCentering(enabled);
127 m_gps->requestLastPosition();
130 void SituareEngine::enableGPS(bool enabled)
132 qDebug() << __PRETTY_FUNCTION__;
134 m_ui->setGPSButtonEnabled(enabled);
135 m_mapEngine->setGPSEnabled(enabled);
139 enableAutoCentering(m_autoCenteringEnabled);
140 m_gps->requestLastPosition();
144 enableAutoCentering(false);
148 void SituareEngine::error(const QString &error)
150 qDebug() << __PRETTY_FUNCTION__;
155 void SituareEngine::fetchUsernameFromSettings()
157 qDebug() << __PRETTY_FUNCTION__;
159 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
162 void SituareEngine::initializeGpsAndAutocentering()
164 qDebug() << __PRETTY_FUNCTION__;
166 QSettings settings(DIRECTORY_NAME, FILE_NAME);
167 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
168 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
170 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
172 connect(m_gps, SIGNAL(position(QPointF,qreal)),
173 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
175 changeAutoCenteringSetting(true);
178 m_ui->showMaemoInformationBox(tr("GPS enabled"));
179 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
180 } else { // Normal start
181 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
182 enableGPS(gpsEnabled.toBool());
184 if (gpsEnabled.toBool())
185 m_ui->showMaemoInformationBox(tr("GPS enabled"));
187 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
188 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
192 void SituareEngine::invalidCredentials()
194 qDebug() << __PRETTY_FUNCTION__;
196 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
197 m_facebookAuthenticator->start();
200 void SituareEngine::loginActionPressed()
202 qDebug() << __PRETTY_FUNCTION__;
206 m_situareService->clearUserData();
209 m_facebookAuthenticator->start();
213 void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
215 qDebug() << __PRETTY_FUNCTION__;
218 m_ui->loggedIn(m_loggedIn);
221 m_facebookAuthenticator->saveUsername(m_ui->username());
224 m_situareService->credentialsReady(credentials);
225 m_situareService->fetchLocations(); // request user locations
228 void SituareEngine::loginProcessCancelled()
230 qDebug() << __PRETTY_FUNCTION__;
232 m_ui->toggleProgressIndicator(false);
233 m_ui->showPanels(m_loggedIn);
236 void SituareEngine::logout()
238 qDebug() << __PRETTY_FUNCTION__;
241 m_ui->loggedIn(m_loggedIn);
242 m_facebookAuthenticator->clearAccountInformation();
245 void SituareEngine::refreshUserData()
247 qDebug() << __PRETTY_FUNCTION__;
249 m_ui->toggleProgressIndicator(true);
251 m_situareService->fetchLocations();
254 void SituareEngine::requestAddress()
256 qDebug() << __PRETTY_FUNCTION__;
258 if (m_gps->isRunning())
259 m_situareService->reverseGeo(m_gps->lastPosition());
261 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
264 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
266 qDebug() << __PRETTY_FUNCTION__;
268 m_ui->toggleProgressIndicator(true);
270 if (m_gps->isRunning())
271 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
273 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
276 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
278 qDebug() << __PRETTY_FUNCTION__;
280 Q_UNUSED(latLonCoordinate);
283 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
284 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
286 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
287 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
290 void SituareEngine::signalsFromFacebookAuthenticator()
292 qDebug() << __PRETTY_FUNCTION__;
294 connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
295 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
297 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
298 this, SLOT(loginOk(bool, FacebookCredentials)));
300 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
301 m_ui, SLOT(startLoginProcess(QUrl)));
303 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
304 m_ui, SLOT(loginFailed()));
307 void SituareEngine::signalsFromGPS()
309 qDebug() << __PRETTY_FUNCTION__;
311 connect(m_gps, SIGNAL(position(QPointF,qreal)),
312 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
314 connect(m_gps, SIGNAL(timeout()),
315 m_ui, SLOT(gpsTimeout()));
317 connect(m_gps, SIGNAL(error(QString)),
318 m_ui, SLOT(gpsError(QString)));
321 void SituareEngine::signalsFromMainWindow()
323 qDebug() << __PRETTY_FUNCTION__;
325 connect(m_ui, SIGNAL(loginActionPressed()),
326 this, SLOT(loginActionPressed()));
328 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
329 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
331 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
332 this, SLOT(fetchUsernameFromSettings()));
334 // signals from map view
335 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
336 m_mapEngine, SLOT(setLocation(QPoint)));
338 connect(m_ui, SIGNAL(mapViewResized(QSize)),
339 m_mapEngine, SLOT(viewResized(QSize)));
341 connect(m_ui, SIGNAL(viewZoomFinished()),
342 m_mapEngine, SLOT(viewZoomFinished()));
344 // signals from zoom buttons (zoom panel and volume buttons)
345 connect(m_ui, SIGNAL(zoomIn()),
346 m_mapEngine, SLOT(zoomIn()));
348 connect(m_ui, SIGNAL(zoomOut()),
349 m_mapEngine, SLOT(zoomOut()));
351 // signals from menu buttons
352 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
353 this, SLOT(changeAutoCenteringSetting(bool)));
355 connect(m_ui, SIGNAL(gpsTriggered(bool)),
356 this, SLOT(enableGPS(bool)));
358 //signals from dialogs
359 connect(m_ui, SIGNAL(cancelLoginProcess()),
360 this, SLOT(loginProcessCancelled()));
362 connect(m_ui, SIGNAL(requestReverseGeo()),
363 this, SLOT(requestAddress()));
365 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
366 this, SLOT(requestUpdateLocation(QString,bool)));
368 // signals from user info tab
369 connect(m_ui, SIGNAL(refreshUserData()),
370 this, SLOT(refreshUserData()));
372 connect(m_ui, SIGNAL(findUser(QPointF)),
373 m_mapEngine, SLOT(setViewLocation(QPointF)));
375 // signals from friend list tab
376 connect(m_ui, SIGNAL(findFriend(QPointF)),
377 m_mapEngine, SLOT(setViewLocation(QPointF)));
380 void SituareEngine::signalsFromMapEngine()
382 qDebug() << __PRETTY_FUNCTION__;
384 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
385 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
387 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
388 m_ui, SIGNAL(zoomLevelChanged(int)));
390 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
391 this, SLOT(disableAutoCentering()));
393 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
394 m_ui, SIGNAL(maxZoomLevelReached()));
396 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
397 m_ui, SIGNAL(minZoomLevelReached()));
399 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
400 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
403 void SituareEngine::signalsFromSituareService()
405 qDebug() << __PRETTY_FUNCTION__;
407 connect(m_situareService, SIGNAL(error(QString)),
408 this, SLOT(error(QString)));
410 connect(m_situareService, SIGNAL(invalidSessionCredentials()),
411 this, SLOT(invalidCredentials()));
413 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
414 m_ui, SIGNAL(reverseGeoReady(QString)));
416 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
417 this, SLOT(userDataChanged(User*, QList<User*>&)));
419 connect(m_situareService, SIGNAL(updateWasSuccessful()),
420 this, SLOT(updateWasSuccessful()));
423 void SituareEngine::updateWasSuccessful()
425 qDebug() << __PRETTY_FUNCTION__;
427 m_situareService->fetchLocations();
430 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
432 qDebug() << __PRETTY_FUNCTION__;
434 m_ui->toggleProgressIndicator(false);
436 emit userLocationReady(user);
437 emit friendsLocationsReady(friendsList);