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";
37 const int DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE = 12;
39 SituareEngine::SituareEngine(QMainWindow *parent)
41 m_autoCenteringEnabled(false),
44 qDebug() << __PRETTY_FUNCTION__;
45 m_ui = new MainWindow;
46 m_ui->showPanels(m_loggedIn);
49 m_mapEngine = new MapEngine(this);
50 m_ui->setMapViewScene(m_mapEngine->scene());
53 m_gps = new GPSPosition(this);
54 m_gps->setMode(GPSPosition::Default);
56 // build SituareService
57 m_situareService = new SituareService(this);
59 // build FacebookAuthenticator
60 m_facebookAuthenticator = new FacebookAuthentication(this);
63 signalsFromMapEngine();
65 signalsFromSituareService();
66 signalsFromMainWindow();
67 signalsFromFacebookAuthenticator();
69 connect(this, SIGNAL(userLocationReady(User*)),
70 m_ui, SIGNAL(userLocationReady(User*)));
72 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
73 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
75 connect(this, SIGNAL(userLocationReady(User*)),
76 m_mapEngine, SLOT(receiveOwnLocation(User*)));
78 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
79 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
81 initializeGpsAndAutocentering();
83 // signals connected, now it's time to show the main window
84 // but init the MapEngine before so starting location is set
88 m_facebookAuthenticator->start();
91 SituareEngine::~SituareEngine()
93 qDebug() << __PRETTY_FUNCTION__;
97 QSettings settings(DIRECTORY_NAME, FILE_NAME);
98 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
99 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
100 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
103 void SituareEngine::changeAutoCenteringSetting(bool enabled)
105 qDebug() << __PRETTY_FUNCTION__;
107 m_autoCenteringEnabled = enabled;
108 enableAutoCentering(enabled);
112 void SituareEngine::disableAutoCentering()
114 qDebug() << __PRETTY_FUNCTION__;
116 changeAutoCenteringSetting(false);
117 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
120 void SituareEngine::enableAutoCentering(bool enabled)
122 qDebug() << __PRETTY_FUNCTION__;
124 m_ui->setAutoCenteringButtonEnabled(enabled);
125 m_mapEngine->setAutoCentering(enabled);
128 m_gps->requestLastPosition();
131 void SituareEngine::enableGPS(bool enabled)
133 qDebug() << __PRETTY_FUNCTION__;
135 m_ui->setGPSButtonEnabled(enabled);
136 m_mapEngine->setGPSEnabled(enabled);
140 enableAutoCentering(m_autoCenteringEnabled);
141 m_gps->requestLastPosition();
145 enableAutoCentering(false);
149 void SituareEngine::error(const QString &error)
151 qDebug() << __PRETTY_FUNCTION__;
156 void SituareEngine::fetchUsernameFromSettings()
158 qDebug() << __PRETTY_FUNCTION__;
160 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
163 void SituareEngine::initializeGpsAndAutocentering()
165 qDebug() << __PRETTY_FUNCTION__;
167 QSettings settings(DIRECTORY_NAME, FILE_NAME);
168 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
169 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
171 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
173 connect(m_gps, SIGNAL(position(QPointF,qreal)),
174 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
176 changeAutoCenteringSetting(true);
179 m_ui->showMaemoInformationBox(tr("GPS enabled"));
180 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
181 } else { // Normal start
182 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
183 enableGPS(gpsEnabled.toBool());
185 if (gpsEnabled.toBool())
186 m_ui->showMaemoInformationBox(tr("GPS enabled"));
188 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
189 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
193 void SituareEngine::invalidCredentials()
195 qDebug() << __PRETTY_FUNCTION__;
197 m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
198 m_ui->clearCookieJar();
199 m_ui->showMaemoInformationBox(tr("Session expired. Please login again"), true);
200 m_facebookAuthenticator->start();
203 void SituareEngine::loginActionPressed()
205 qDebug() << __PRETTY_FUNCTION__;
209 m_situareService->clearUserData();
212 m_facebookAuthenticator->start();
216 void SituareEngine::loginOk()
218 qDebug() << __PRETTY_FUNCTION__;
221 m_ui->loggedIn(m_loggedIn);
223 if(!m_ui->username().isEmpty())
224 m_facebookAuthenticator->saveUsername(m_ui->username());
227 m_situareService->fetchLocations(); // request user locations
230 void SituareEngine::loginProcessCancelled()
232 qDebug() << __PRETTY_FUNCTION__;
234 m_ui->toggleProgressIndicator(false);
235 m_ui->showPanels(m_loggedIn);
238 void SituareEngine::logout()
240 qDebug() << __PRETTY_FUNCTION__;
243 m_ui->loggedIn(m_loggedIn);
244 m_facebookAuthenticator->clearAccountInformation(); // clear all
247 void SituareEngine::refreshUserData()
249 qDebug() << __PRETTY_FUNCTION__;
251 m_ui->toggleProgressIndicator(true);
253 m_situareService->fetchLocations();
256 void SituareEngine::requestAddress()
258 qDebug() << __PRETTY_FUNCTION__;
260 if (m_gps->isRunning())
261 m_situareService->reverseGeo(m_gps->lastPosition());
263 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
266 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
268 qDebug() << __PRETTY_FUNCTION__;
270 m_ui->toggleProgressIndicator(true);
272 if (m_gps->isRunning())
273 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
275 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
278 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
280 qDebug() << __PRETTY_FUNCTION__;
282 Q_UNUSED(latLonCoordinate);
285 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
286 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
288 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
289 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
292 void SituareEngine::signalsFromFacebookAuthenticator()
294 qDebug() << __PRETTY_FUNCTION__;
296 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
297 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
299 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
300 this, SLOT(loginOk()));
302 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
303 m_ui, SLOT(startLoginProcess(QUrl)));
305 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
306 m_ui, SLOT(loginFailed()));
308 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
309 m_ui, SLOT(saveCookies()));
311 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
312 m_ui, SLOT(loginUsingCookies()));
315 void SituareEngine::signalsFromGPS()
317 qDebug() << __PRETTY_FUNCTION__;
319 connect(m_gps, SIGNAL(position(QPointF,qreal)),
320 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
322 connect(m_gps, SIGNAL(timeout()),
323 m_ui, SLOT(gpsTimeout()));
325 connect(m_gps, SIGNAL(error(QString)),
326 m_ui, SLOT(gpsError(QString)));
329 void SituareEngine::signalsFromMainWindow()
331 qDebug() << __PRETTY_FUNCTION__;
333 connect(m_ui, SIGNAL(loginActionPressed()),
334 this, SLOT(loginActionPressed()));
336 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
337 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
339 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
340 this, SLOT(fetchUsernameFromSettings()));
342 // signals from map view
343 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
344 m_mapEngine, SLOT(setLocation(QPoint)));
346 connect(m_ui, SIGNAL(mapViewResized(QSize)),
347 m_mapEngine, SLOT(viewResized(QSize)));
349 connect(m_ui, SIGNAL(viewZoomFinished()),
350 m_mapEngine, SLOT(viewZoomFinished()));
352 // signals from zoom buttons (zoom panel and volume buttons)
353 connect(m_ui, SIGNAL(zoomIn()),
354 m_mapEngine, SLOT(zoomIn()));
356 connect(m_ui, SIGNAL(zoomOut()),
357 m_mapEngine, SLOT(zoomOut()));
359 // signals from menu buttons
360 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
361 this, SLOT(changeAutoCenteringSetting(bool)));
363 connect(m_ui, SIGNAL(gpsTriggered(bool)),
364 this, SLOT(enableGPS(bool)));
366 //signals from dialogs
367 connect(m_ui, SIGNAL(cancelLoginProcess()),
368 this, SLOT(loginProcessCancelled()));
370 connect(m_ui, SIGNAL(requestReverseGeo()),
371 this, SLOT(requestAddress()));
373 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
374 this, SLOT(requestUpdateLocation(QString,bool)));
376 // signals from user info tab
377 connect(m_ui, SIGNAL(refreshUserData()),
378 this, SLOT(refreshUserData()));
380 connect(m_ui, SIGNAL(findUser(QPointF)),
381 m_mapEngine, SLOT(setViewLocation(QPointF)));
383 // signals from friend list tab
384 connect(m_ui, SIGNAL(findFriend(QPointF)),
385 m_mapEngine, SLOT(setViewLocation(QPointF)));
388 void SituareEngine::signalsFromMapEngine()
390 qDebug() << __PRETTY_FUNCTION__;
392 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
393 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
395 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
396 m_ui, SIGNAL(zoomLevelChanged(int)));
398 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
399 this, SLOT(disableAutoCentering()));
401 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
402 m_ui, SIGNAL(maxZoomLevelReached()));
404 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
405 m_ui, SIGNAL(minZoomLevelReached()));
407 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
408 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
411 void SituareEngine::signalsFromSituareService()
413 qDebug() << __PRETTY_FUNCTION__;
415 connect(m_situareService, SIGNAL(error(QString)),
416 this, SLOT(error(QString)));
418 connect(m_situareService, SIGNAL(invalidSessionCredentials()),
419 this, SLOT(invalidCredentials()));
421 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
422 m_ui, SIGNAL(reverseGeoReady(QString)));
424 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
425 this, SLOT(userDataChanged(User*, QList<User*>&)));
427 connect(m_situareService, SIGNAL(updateWasSuccessful()),
428 this, SLOT(updateWasSuccessful()));
431 void SituareEngine::updateWasSuccessful()
433 qDebug() << __PRETTY_FUNCTION__;
435 m_situareService->fetchLocations();
438 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
440 qDebug() << __PRETTY_FUNCTION__;
442 m_ui->toggleProgressIndicator(false);
444 emit userLocationReady(user);
445 emit friendsLocationsReady(friendsList);