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)
42 qDebug() << __PRETTY_FUNCTION__;
43 m_ui = new MainWindow;
46 m_mapEngine = new MapEngine(this);
47 m_ui->setMapViewScene(m_mapEngine->scene());
50 m_gps = new GPSPosition(this);
51 m_gps->setMode(GPSPosition::Default);
53 // build SituareService
54 m_situareService = new SituareService(this);
56 // build FacebookAuthenticator
57 m_facebookAuthenticator = new FacebookAuthentication(this);
60 signalsFromMapEngine();
62 signalsFromSituareService();
63 signalsFromMainWindow();
64 signalsFromFacebookAuthenticator();
66 connect(this, SIGNAL(userLocationReady(User*)),
67 m_ui, SIGNAL(userLocationReady(User*)));
69 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
70 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
72 connect(this, SIGNAL(userLocationReady(User*)),
73 m_mapEngine, SLOT(receiveOwnLocation(User*)));
75 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
76 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
78 initializeGpsAndAutocentering();
80 // signals connected, now it's time to show the main window
81 // but init the MapEngine before so starting location is set
85 m_facebookAuthenticator->start();
88 SituareEngine::~SituareEngine()
90 qDebug() << __PRETTY_FUNCTION__;
94 QSettings settings(DIRECTORY_NAME, FILE_NAME);
95 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
96 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
97 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
100 void SituareEngine::changeAutoCenteringSetting(bool enabled)
102 qDebug() << __PRETTY_FUNCTION__;
104 m_autoCenteringEnabled = enabled;
105 enableAutoCentering(enabled);
108 void SituareEngine::disableAutoCentering()
110 qDebug() << __PRETTY_FUNCTION__;
112 changeAutoCenteringSetting(false);
113 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
116 void SituareEngine::enableAutoCentering(bool enabled)
118 qDebug() << __PRETTY_FUNCTION__;
120 m_ui->setAutoCenteringButtonEnabled(enabled);
121 m_mapEngine->setAutoCentering(enabled);
124 m_gps->requestLastPosition();
127 void SituareEngine::enableGPS(bool enabled)
129 qDebug() << __PRETTY_FUNCTION__;
131 m_ui->setGPSButtonEnabled(enabled);
132 m_mapEngine->setGPSEnabled(enabled);
136 enableAutoCentering(m_autoCenteringEnabled);
137 m_gps->requestLastPosition();
141 enableAutoCentering(false);
145 void SituareEngine::error(const QString &error)
147 qDebug() << __PRETTY_FUNCTION__;
152 void SituareEngine::fetchUsernameFromSettings()
154 qDebug() << __PRETTY_FUNCTION__;
155 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
158 void SituareEngine::initializeGpsAndAutocentering()
160 QSettings settings(DIRECTORY_NAME, FILE_NAME);
161 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
162 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
164 if (gpsEnabled.toString().isEmpty()) { // First start. Situare.conf file does not exists
166 connect(m_gps, SIGNAL(position(QPointF,qreal)),
167 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
169 changeAutoCenteringSetting(true);
172 m_ui->showMaemoInformationBox(tr("GPS enabled"));
173 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
176 else { // Normal start
177 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
178 enableGPS(gpsEnabled.toBool());
180 if (gpsEnabled.toBool())
181 m_ui->showMaemoInformationBox(tr("GPS enabled"));
183 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
184 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
188 void SituareEngine::loginOk(bool freshLogin)
190 qDebug() << __PRETTY_FUNCTION__;
193 m_facebookAuthenticator->saveUsername(m_ui->username());
196 m_situareService->fetchLocations(); // request user locations
199 void SituareEngine::loginProcessCancelled()
201 qDebug() << __PRETTY_FUNCTION__;
203 m_ui->toggleProgressIndicator(false);
207 void SituareEngine::refreshUserData()
209 qDebug() << __PRETTY_FUNCTION__;
211 m_ui->toggleProgressIndicator(true);
213 m_situareService->fetchLocations();
216 void SituareEngine::requestAddress()
218 qDebug() << __PRETTY_FUNCTION__;
220 if (m_gps->isRunning())
221 m_situareService->reverseGeo(m_gps->lastPosition());
223 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
226 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
228 qDebug() << __PRETTY_FUNCTION__;
230 m_ui->toggleProgressIndicator(true);
232 if (m_gps->isRunning())
233 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
235 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
238 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
240 Q_UNUSED(latLonCoordinate);
243 if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
244 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
246 disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
247 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
250 void SituareEngine::signalsFromFacebookAuthenticator()
252 qDebug() << __PRETTY_FUNCTION__;
254 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
255 m_situareService, SLOT(credentialsReady(bool, FacebookCredentials)));
257 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
258 this, SLOT(loginOk(bool)));
260 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
261 m_ui, SLOT(startLoginProcess(QUrl)));
263 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
264 m_ui, SLOT(loginFailed()));
267 void SituareEngine::signalsFromGPS()
269 qDebug() << __PRETTY_FUNCTION__;
271 connect(m_gps, SIGNAL(position(QPointF,qreal)),
272 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
274 connect(m_gps, SIGNAL(timeout()),
275 m_ui, SLOT(gpsTimeout()));
277 connect(m_gps, SIGNAL(error(QString)),
278 m_ui, SLOT(gpsError(QString)));
281 void SituareEngine::signalsFromMainWindow()
283 qDebug() << __PRETTY_FUNCTION__;
285 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
286 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
288 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
289 this, SLOT(fetchUsernameFromSettings()));
291 // signals from map view
292 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
293 m_mapEngine, SLOT(setLocation(QPoint)));
295 connect(m_ui, SIGNAL(mapViewResized(QSize)),
296 m_mapEngine, SLOT(viewResized(QSize)));
298 connect(m_ui, SIGNAL(viewZoomFinished()),
299 m_mapEngine, SLOT(viewZoomFinished()));
301 // signals from zoom buttons (zoom panel and volume buttons)
302 connect(m_ui, SIGNAL(zoomIn()),
303 m_mapEngine, SLOT(zoomIn()));
305 connect(m_ui, SIGNAL(zoomOut()),
306 m_mapEngine, SLOT(zoomOut()));
308 // signals from menu buttons
309 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
310 this, SLOT(changeAutoCenteringSetting(bool)));
312 connect(m_ui, SIGNAL(gpsTriggered(bool)),
313 this, SLOT(enableGPS(bool)));
315 //signals from dialogs
316 connect(m_ui, SIGNAL(cancelLoginProcess()),
317 this, SLOT(loginProcessCancelled()));
319 connect(m_ui, SIGNAL(requestReverseGeo()),
320 this, SLOT(requestAddress()));
322 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
323 this, SLOT(requestUpdateLocation(QString,bool)));
325 // signals from user info tab
326 connect(m_ui, SIGNAL(refreshUserData()),
327 this, SLOT(refreshUserData()));
329 // signals from friend list tab
330 connect(m_ui, SIGNAL(findFriend(QPointF)),
331 m_mapEngine, SLOT(setViewLocation(QPointF)));
334 void SituareEngine::signalsFromMapEngine()
336 qDebug() << __PRETTY_FUNCTION__;
338 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
339 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
341 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
342 m_ui, SIGNAL(zoomLevelChanged(int)));
344 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
345 this, SLOT(disableAutoCentering()));
347 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
348 m_ui, SIGNAL(maxZoomLevelReached()));
350 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
351 m_ui, SIGNAL(minZoomLevelReached()));
353 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
354 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
357 void SituareEngine::signalsFromSituareService()
359 qDebug() << __PRETTY_FUNCTION__;
361 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
362 m_ui, SIGNAL(reverseGeoReady(QString)));
364 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
365 this, SLOT(userDataChanged(User*, QList<User*>&)));
367 connect(m_situareService, SIGNAL(error(QString)),
368 this, SLOT(error(QString)));
370 connect(m_situareService, SIGNAL(updateWasSuccessful()),
371 this, SLOT(updateWasSuccessful()));
374 void SituareEngine::updateWasSuccessful()
376 qDebug() << __PRETTY_FUNCTION__;
378 m_situareService->fetchLocations();
381 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
383 qDebug() << __PRETTY_FUNCTION__;
385 m_ui->toggleProgressIndicator(false);
387 emit userLocationReady(user);
388 emit friendsLocationsReady(friendsList);