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();
102 SituareEngine::~SituareEngine()
104 qDebug() << __PRETTY_FUNCTION__;
108 QSettings settings(DIRECTORY_NAME, FILE_NAME);
109 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
110 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
111 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
114 void SituareEngine::changeAutoCenteringSetting(bool enabled)
116 qDebug() << __PRETTY_FUNCTION__;
118 m_autoCenteringEnabled = enabled;
119 enableAutoCentering(enabled);
122 void SituareEngine::disableAutoCentering()
124 qDebug() << __PRETTY_FUNCTION__;
126 changeAutoCenteringSetting(false);
127 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
130 void SituareEngine::enableAutoCentering(bool enabled)
132 qDebug() << __PRETTY_FUNCTION__;
134 m_ui->setAutoCenteringButtonEnabled(enabled);
135 m_mapEngine->setAutoCentering(enabled);
138 m_gps->requestLastPosition();
141 void SituareEngine::enableGPS(bool enabled)
143 qDebug() << __PRETTY_FUNCTION__;
145 m_ui->setGPSButtonEnabled(enabled);
146 m_mapEngine->setGPSEnabled(enabled);
150 enableAutoCentering(m_autoCenteringEnabled);
151 m_gps->requestLastPosition();
155 enableAutoCentering(false);
159 void SituareEngine::error(const QString &error)
161 qDebug() << __PRETTY_FUNCTION__;
166 void SituareEngine::fetchUsernameFromSettings()
168 qDebug() << __PRETTY_FUNCTION__;
169 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
172 void SituareEngine::invalidCredentials()
174 qDebug() << __PRETTY_FUNCTION__;
176 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
177 m_facebookAuthenticator->start();
180 void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
182 qDebug() << __PRETTY_FUNCTION__;
185 m_ui->loggedIn(m_loggedIn);
188 m_facebookAuthenticator->saveUsername(m_ui->username());
191 m_situareService->credentialsReady(credentials);
192 m_situareService->fetchLocations(); // request user locations
195 void SituareEngine::loginPressed()
197 qDebug() << __PRETTY_FUNCTION__;
201 m_situareService->clearUserData();
204 m_facebookAuthenticator->start();
208 void SituareEngine::loginProcessCancelled()
210 qDebug() << __PRETTY_FUNCTION__;
212 m_ui->toggleProgressIndicator(false);
213 m_ui->showPanels(m_loggedIn);
216 void SituareEngine::logout()
218 qDebug() << __PRETTY_FUNCTION__;
221 m_ui->loggedIn(m_loggedIn);
222 m_facebookAuthenticator->clearAccountInformation();
225 void SituareEngine::refreshUserData()
227 qDebug() << __PRETTY_FUNCTION__;
229 m_ui->toggleProgressIndicator(true);
231 m_situareService->fetchLocations();
234 void SituareEngine::requestAddress()
236 qDebug() << __PRETTY_FUNCTION__;
238 if (m_gps->isRunning())
239 m_situareService->reverseGeo(m_gps->lastPosition());
241 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
244 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
246 qDebug() << __PRETTY_FUNCTION__;
248 m_ui->toggleProgressIndicator(true);
250 if (m_gps->isRunning())
251 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
253 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
256 void SituareEngine::signalsFromFacebookAuthenticator()
258 qDebug() << __PRETTY_FUNCTION__;
260 connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
261 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
263 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
264 this, SLOT(loginOk(bool, FacebookCredentials)));
266 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
267 m_ui, SLOT(startLoginProcess(QUrl)));
269 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
270 m_ui, SLOT(loginFailed()));
273 void SituareEngine::signalsFromGPS()
275 qDebug() << __PRETTY_FUNCTION__;
277 connect(m_gps, SIGNAL(position(QPointF,qreal)),
278 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
280 connect(m_gps, SIGNAL(timeout()),
281 m_ui, SLOT(gpsTimeout()));
283 connect(m_gps, SIGNAL(error(QString)),
284 m_ui, SLOT(gpsError(QString)));
287 void SituareEngine::signalsFromMainWindow()
289 qDebug() << __PRETTY_FUNCTION__;
291 connect(m_ui, SIGNAL(loginPressed()),
292 this, SLOT(loginPressed()));
294 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
295 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
297 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
298 this, SLOT(fetchUsernameFromSettings()));
300 // signals from map view
301 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
302 m_mapEngine, SLOT(setLocation(QPoint)));
304 connect(m_ui, SIGNAL(mapViewResized(QSize)),
305 m_mapEngine, SLOT(viewResized(QSize)));
307 connect(m_ui, SIGNAL(viewZoomFinished()),
308 m_mapEngine, SLOT(viewZoomFinished()));
310 // signals from zoom buttons (zoom panel and volume buttons)
311 connect(m_ui, SIGNAL(zoomIn()),
312 m_mapEngine, SLOT(zoomIn()));
314 connect(m_ui, SIGNAL(zoomOut()),
315 m_mapEngine, SLOT(zoomOut()));
317 // signals from menu buttons
318 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
319 this, SLOT(changeAutoCenteringSetting(bool)));
321 connect(m_ui, SIGNAL(gpsTriggered(bool)),
322 this, SLOT(enableGPS(bool)));
324 //signals from dialogs
325 connect(m_ui, SIGNAL(cancelLoginProcess()),
326 this, SLOT(loginProcessCancelled()));
328 connect(m_ui, SIGNAL(requestReverseGeo()),
329 this, SLOT(requestAddress()));
331 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
332 this, SLOT(requestUpdateLocation(QString,bool)));
334 // signals from user info tab
335 connect(m_ui, SIGNAL(refreshUserData()),
336 this, SLOT(refreshUserData()));
338 // signals from friend list tab
339 connect(m_ui, SIGNAL(findFriend(QPointF)),
340 m_mapEngine, SLOT(setViewLocation(QPointF)));
343 void SituareEngine::signalsFromMapEngine()
345 qDebug() << __PRETTY_FUNCTION__;
347 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
348 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
350 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
351 m_ui, SIGNAL(zoomLevelChanged(int)));
353 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
354 this, SLOT(disableAutoCentering()));
356 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
357 m_ui, SIGNAL(maxZoomLevelReached()));
359 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
360 m_ui, SIGNAL(minZoomLevelReached()));
363 void SituareEngine::signalsFromSituareService()
365 qDebug() << __PRETTY_FUNCTION__;
367 connect(m_situareService, SIGNAL(error(QString)),
368 this, SLOT(error(QString)));
370 connect(m_situareService, SIGNAL(invalidSessionCredentials()),
371 this, SLOT(invalidCredentials()));
373 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
374 m_ui, SIGNAL(reverseGeoReady(QString)));
376 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
377 this, SLOT(userDataChanged(User*, QList<User*>&)));
379 connect(m_situareService, SIGNAL(updateWasSuccessful()),
380 this, SLOT(updateWasSuccessful()));
383 void SituareEngine::updateWasSuccessful()
385 qDebug() << __PRETTY_FUNCTION__;
387 m_situareService->fetchLocations();
390 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
392 qDebug() << __PRETTY_FUNCTION__;
394 m_ui->toggleProgressIndicator(false);
396 emit userLocationReady(user);
397 emit friendsLocationsReady(friendsList);