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_facebookAuthenticator->start();
199 m_ui->loginUsingCookies();
202 void SituareEngine::loginActionPressed()
204 qDebug() << __PRETTY_FUNCTION__;
208 m_situareService->clearUserData();
211 m_facebookAuthenticator->start();
215 void SituareEngine::loginOk()
217 qDebug() << __PRETTY_FUNCTION__;
220 m_ui->loggedIn(m_loggedIn);
222 if(!m_ui->username().isEmpty())
223 m_facebookAuthenticator->saveUsername(m_ui->username());
226 m_situareService->fetchLocations(); // request user locations
229 void SituareEngine::loginProcessCancelled()
231 qDebug() << __PRETTY_FUNCTION__;
233 m_ui->toggleProgressIndicator(false);
234 m_ui->showPanels(m_loggedIn);
237 void SituareEngine::logout()
239 qDebug() << __PRETTY_FUNCTION__;
242 m_ui->loggedIn(m_loggedIn);
243 m_facebookAuthenticator->clearAccountInformation();
246 void SituareEngine::refreshUserData()
248 qDebug() << __PRETTY_FUNCTION__;
250 m_ui->toggleProgressIndicator(true);
252 m_situareService->fetchLocations();
255 void SituareEngine::requestAddress()
257 qDebug() << __PRETTY_FUNCTION__;
259 if (m_gps->isRunning())
260 m_situareService->reverseGeo(m_gps->lastPosition());
262 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
265 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
267 qDebug() << __PRETTY_FUNCTION__;
269 m_ui->toggleProgressIndicator(true);
271 if (m_gps->isRunning())
272 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
274 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
277 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
279 qDebug() << __PRETTY_FUNCTION__;
281 Q_UNUSED(latLonCoordinate);
284 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
285 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
287 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
288 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
291 void SituareEngine::signalsFromFacebookAuthenticator()
293 qDebug() << __PRETTY_FUNCTION__;
295 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
296 m_situareService, SLOT(credentialsReady(FacebookCredentials)));
298 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
299 this, SLOT(loginOk()));
301 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
302 m_ui, SLOT(startLoginProcess(QUrl)));
304 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
305 m_ui, SLOT(loginFailed()));
307 connect(m_facebookAuthenticator, SIGNAL(saveCookiesRequest()),
308 m_ui, SLOT(saveCookies()));
310 connect(m_facebookAuthenticator, SIGNAL(loginUsingCookies()),
311 m_ui, SLOT(loginUsingCookies()));
314 void SituareEngine::signalsFromGPS()
316 qDebug() << __PRETTY_FUNCTION__;
318 connect(m_gps, SIGNAL(position(QPointF,qreal)),
319 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
321 connect(m_gps, SIGNAL(timeout()),
322 m_ui, SLOT(gpsTimeout()));
324 connect(m_gps, SIGNAL(error(QString)),
325 m_ui, SLOT(gpsError(QString)));
328 void SituareEngine::signalsFromMainWindow()
330 qDebug() << __PRETTY_FUNCTION__;
332 connect(m_ui, SIGNAL(loginActionPressed()),
333 this, SLOT(loginActionPressed()));
335 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
336 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
338 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
339 this, SLOT(fetchUsernameFromSettings()));
341 // signals from map view
342 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
343 m_mapEngine, SLOT(setLocation(QPoint)));
345 connect(m_ui, SIGNAL(mapViewResized(QSize)),
346 m_mapEngine, SLOT(viewResized(QSize)));
348 connect(m_ui, SIGNAL(viewZoomFinished()),
349 m_mapEngine, SLOT(viewZoomFinished()));
351 // signals from zoom buttons (zoom panel and volume buttons)
352 connect(m_ui, SIGNAL(zoomIn()),
353 m_mapEngine, SLOT(zoomIn()));
355 connect(m_ui, SIGNAL(zoomOut()),
356 m_mapEngine, SLOT(zoomOut()));
358 // signals from menu buttons
359 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
360 this, SLOT(changeAutoCenteringSetting(bool)));
362 connect(m_ui, SIGNAL(gpsTriggered(bool)),
363 this, SLOT(enableGPS(bool)));
365 //signals from dialogs
366 connect(m_ui, SIGNAL(cancelLoginProcess()),
367 this, SLOT(loginProcessCancelled()));
369 connect(m_ui, SIGNAL(requestReverseGeo()),
370 this, SLOT(requestAddress()));
372 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
373 this, SLOT(requestUpdateLocation(QString,bool)));
375 // signals from user info tab
376 connect(m_ui, SIGNAL(refreshUserData()),
377 this, SLOT(refreshUserData()));
379 connect(m_ui, SIGNAL(findUser(QPointF)),
380 m_mapEngine, SLOT(setViewLocation(QPointF)));
382 // signals from friend list tab
383 connect(m_ui, SIGNAL(findFriend(QPointF)),
384 m_mapEngine, SLOT(setViewLocation(QPointF)));
387 void SituareEngine::signalsFromMapEngine()
389 qDebug() << __PRETTY_FUNCTION__;
391 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
392 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
394 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
395 m_ui, SIGNAL(zoomLevelChanged(int)));
397 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
398 this, SLOT(disableAutoCentering()));
400 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
401 m_ui, SIGNAL(maxZoomLevelReached()));
403 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
404 m_ui, SIGNAL(minZoomLevelReached()));
406 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
407 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
410 void SituareEngine::signalsFromSituareService()
412 qDebug() << __PRETTY_FUNCTION__;
414 connect(m_situareService, SIGNAL(error(QString)),
415 this, SLOT(error(QString)));
417 connect(m_situareService, SIGNAL(invalidSessionCredentials()),
418 this, SLOT(invalidCredentials()));
420 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
421 m_ui, SIGNAL(reverseGeoReady(QString)));
423 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
424 this, SLOT(userDataChanged(User*, QList<User*>&)));
426 connect(m_situareService, SIGNAL(updateWasSuccessful()),
427 this, SLOT(updateWasSuccessful()));
430 void SituareEngine::updateWasSuccessful()
432 qDebug() << __PRETTY_FUNCTION__;
434 m_situareService->fetchLocations();
437 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
439 qDebug() << __PRETTY_FUNCTION__;
441 m_ui->toggleProgressIndicator(false);
443 emit userLocationReady(user);
444 emit friendsLocationsReady(friendsList);