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 // signals connected, now it's time to show the main window
79 // but init the MapEngine before so starting location is set
83 initializeGpsAndAutocentering();
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
165 changeAutoCenteringSetting(true);
168 m_ui->showMaemoInformationBox(tr("GPS enabled"));
169 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
171 m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
174 else { // Normal start
175 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
176 enableGPS(gpsEnabled.toBool());
178 if (gpsEnabled.toBool())
179 m_ui->showMaemoInformationBox(tr("GPS enabled"));
181 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
182 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
186 void SituareEngine::loginOk(bool freshLogin)
188 qDebug() << __PRETTY_FUNCTION__;
191 m_facebookAuthenticator->saveUsername(m_ui->username());
194 m_situareService->fetchLocations(); // request user locations
197 void SituareEngine::loginProcessCancelled()
199 qDebug() << __PRETTY_FUNCTION__;
201 m_ui->toggleProgressIndicator(false);
205 void SituareEngine::refreshUserData()
207 qDebug() << __PRETTY_FUNCTION__;
209 m_ui->toggleProgressIndicator(true);
211 m_situareService->fetchLocations();
214 void SituareEngine::requestAddress()
216 qDebug() << __PRETTY_FUNCTION__;
218 if (m_gps->isRunning())
219 m_situareService->reverseGeo(m_gps->lastPosition());
221 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
224 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
226 qDebug() << __PRETTY_FUNCTION__;
228 m_ui->toggleProgressIndicator(true);
230 if (m_gps->isRunning())
231 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
233 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
236 void SituareEngine::signalsFromFacebookAuthenticator()
238 qDebug() << __PRETTY_FUNCTION__;
240 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
241 m_situareService, SLOT(credentialsReady(bool, FacebookCredentials)));
243 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
244 this, SLOT(loginOk(bool)));
246 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
247 m_ui, SLOT(startLoginProcess(QUrl)));
249 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
250 m_ui, SLOT(loginFailed()));
253 void SituareEngine::signalsFromGPS()
255 qDebug() << __PRETTY_FUNCTION__;
257 connect(m_gps, SIGNAL(position(QPointF,qreal)),
258 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
260 connect(m_gps, SIGNAL(timeout()),
261 m_ui, SLOT(gpsTimeout()));
263 connect(m_gps, SIGNAL(error(QString)),
264 m_ui, SLOT(gpsError(QString)));
267 void SituareEngine::signalsFromMainWindow()
269 qDebug() << __PRETTY_FUNCTION__;
271 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
272 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
274 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
275 this, SLOT(fetchUsernameFromSettings()));
277 // signals from map view
278 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
279 m_mapEngine, SLOT(setLocation(QPoint)));
281 connect(m_ui, SIGNAL(mapViewResized(QSize)),
282 m_mapEngine, SLOT(viewResized(QSize)));
284 connect(m_ui, SIGNAL(viewZoomFinished()),
285 m_mapEngine, SLOT(viewZoomFinished()));
287 // signals from zoom buttons (zoom panel and volume buttons)
288 connect(m_ui, SIGNAL(zoomIn()),
289 m_mapEngine, SLOT(zoomIn()));
291 connect(m_ui, SIGNAL(zoomOut()),
292 m_mapEngine, SLOT(zoomOut()));
294 // signals from menu buttons
295 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
296 this, SLOT(changeAutoCenteringSetting(bool)));
298 connect(m_ui, SIGNAL(gpsTriggered(bool)),
299 this, SLOT(enableGPS(bool)));
301 //signals from dialogs
302 connect(m_ui, SIGNAL(cancelLoginProcess()),
303 this, SLOT(loginProcessCancelled()));
305 connect(m_ui, SIGNAL(requestReverseGeo()),
306 this, SLOT(requestAddress()));
308 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
309 this, SLOT(requestUpdateLocation(QString,bool)));
311 // signals from user info tab
312 connect(m_ui, SIGNAL(refreshUserData()),
313 this, SLOT(refreshUserData()));
315 // signals from friend list tab
316 connect(m_ui, SIGNAL(findFriend(QPointF)),
317 m_mapEngine, SLOT(setViewLocation(QPointF)));
320 void SituareEngine::signalsFromMapEngine()
322 qDebug() << __PRETTY_FUNCTION__;
324 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
325 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
327 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
328 m_ui, SIGNAL(zoomLevelChanged(int)));
330 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
331 this, SLOT(disableAutoCentering()));
333 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
334 m_ui, SIGNAL(maxZoomLevelReached()));
336 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
337 m_ui, SIGNAL(minZoomLevelReached()));
339 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
340 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
343 void SituareEngine::signalsFromSituareService()
345 qDebug() << __PRETTY_FUNCTION__;
347 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
348 m_ui, SIGNAL(reverseGeoReady(QString)));
350 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
351 this, SLOT(userDataChanged(User*, QList<User*>&)));
353 connect(m_situareService, SIGNAL(error(QString)),
354 this, SLOT(error(QString)));
356 connect(m_situareService, SIGNAL(updateWasSuccessful()),
357 this, SLOT(updateWasSuccessful()));
360 void SituareEngine::updateWasSuccessful()
362 qDebug() << __PRETTY_FUNCTION__;
364 m_situareService->fetchLocations();
367 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
369 qDebug() << __PRETTY_FUNCTION__;
371 m_ui->toggleProgressIndicator(false);
373 emit userLocationReady(user);
374 emit friendsLocationsReady(friendsList);