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"
32 #include "networkhandler.h"
36 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
37 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
39 SituareEngine::SituareEngine(QMainWindow *parent)
41 m_autoCenteringEnabled(false)
43 qDebug() << __PRETTY_FUNCTION__;
44 m_ui = new MainWindow;
46 // build NetworkHandler
47 m_networkHandler = new NetworkHandler(this);
50 m_mapEngine = new MapEngine(this);
51 m_ui->setMapViewScene(m_mapEngine->scene());
54 m_gps = new GPSPosition(this);
55 m_gps->setMode(GPSPosition::Default);
57 // build SituareService
58 m_situareService = new SituareService(this);
60 // build FacebookAuthenticator
61 m_facebookAuthenticator = new FacebookAuthentication(this);
64 signalsFromNetworkHandler();
65 signalsFromMapEngine();
67 signalsFromSituareService();
68 signalsFromMainWindow();
69 signalsFromFacebookAuthenticator();
71 connect(this, SIGNAL(userLocationReady(User*)),
72 m_ui, SIGNAL(userLocationReady(User*)));
74 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
75 m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
77 connect(this, SIGNAL(userLocationReady(User*)),
78 m_mapEngine, SLOT(receiveOwnLocation(User*)));
80 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
81 m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
83 // signals connected, now it's time to show the main window
84 // but init the MapEngine before so starting location is set
85 // m_mapEngine->init();
88 QSettings settings(DIRECTORY_NAME, FILE_NAME);
89 QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
90 QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
92 // set features on / off based on settings
93 changeAutoCenteringSetting(autoCenteringEnabled.toBool());
94 enableGPS(gpsEnabled.toBool());
96 // show messages at startup if features are enabled automatically
97 if (gpsEnabled.toBool())
98 m_ui->showMaemoInformationBox(tr("GPS enabled"));
99 if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
100 m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
102 //Request network state.
103 m_networkHandler->state();
106 SituareEngine::~SituareEngine()
108 qDebug() << __PRETTY_FUNCTION__;
112 QSettings settings(DIRECTORY_NAME, FILE_NAME);
113 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
114 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
115 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
118 void SituareEngine::changeAutoCenteringSetting(bool enabled)
120 qDebug() << __PRETTY_FUNCTION__;
122 m_autoCenteringEnabled = enabled;
123 enableAutoCentering(enabled);
126 void SituareEngine::connectedToNetwork()
128 qDebug() << __PRETTY_FUNCTION__;
130 disconnect(m_networkHandler, SIGNAL(connected()),
131 this, SLOT(connectedToNetwork()));
133 disconnect(m_networkHandler, SIGNAL(disconnected()),
134 this, SLOT(disconnectedFromNetwork()));
136 m_facebookAuthenticator->start();
139 //TODO: Start map image fetching
142 void SituareEngine::disableAutoCentering()
144 qDebug() << __PRETTY_FUNCTION__;
146 changeAutoCenteringSetting(false);
147 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
150 void SituareEngine::disconnectedFromNetwork()
152 qDebug() << __PRETTY_FUNCTION__;
153 m_networkHandler->connect();
154 //TODO: Stop map image fetching
157 void SituareEngine::enableAutoCentering(bool enabled)
159 qDebug() << __PRETTY_FUNCTION__;
161 m_ui->setAutoCenteringButtonEnabled(enabled);
162 m_mapEngine->setAutoCentering(enabled);
165 m_gps->requestLastPosition();
168 void SituareEngine::enableGPS(bool enabled)
170 qDebug() << __PRETTY_FUNCTION__;
172 m_ui->setGPSButtonEnabled(enabled);
173 m_mapEngine->setGPSEnabled(enabled);
177 enableAutoCentering(m_autoCenteringEnabled);
178 m_gps->requestLastPosition();
182 enableAutoCentering(false);
186 void SituareEngine::error(const QString &error)
188 qDebug() << __PRETTY_FUNCTION__;
193 void SituareEngine::fetchUsernameFromSettings()
195 qDebug() << __PRETTY_FUNCTION__;
196 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
199 void SituareEngine::loginOk(bool freshLogin)
201 qDebug() << __PRETTY_FUNCTION__;
204 m_facebookAuthenticator->saveUsername(m_ui->username());
207 m_situareService->fetchLocations(); // request user locations
210 void SituareEngine::loginProcessCancelled()
212 qDebug() << __PRETTY_FUNCTION__;
214 m_ui->toggleProgressIndicator(false);
218 void SituareEngine::refreshUserData()
220 qDebug() << __PRETTY_FUNCTION__;
222 m_ui->toggleProgressIndicator(true);
224 m_situareService->fetchLocations();
227 void SituareEngine::requestAddress()
229 qDebug() << __PRETTY_FUNCTION__;
231 if (m_gps->isRunning())
232 m_situareService->reverseGeo(m_gps->lastPosition());
234 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
237 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
239 qDebug() << __PRETTY_FUNCTION__;
241 m_ui->toggleProgressIndicator(true);
243 if (m_gps->isRunning())
244 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
246 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
249 void SituareEngine::signalsFromFacebookAuthenticator()
251 qDebug() << __PRETTY_FUNCTION__;
253 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
254 m_situareService, SLOT(credentialsReady(bool, FacebookCredentials)));
256 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
257 this, SLOT(loginOk(bool)));
259 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
260 m_ui, SLOT(startLoginProcess(QUrl)));
262 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
263 m_ui, SLOT(loginFailed()));
266 void SituareEngine::signalsFromGPS()
268 qDebug() << __PRETTY_FUNCTION__;
270 connect(m_gps, SIGNAL(position(QPointF,qreal)),
271 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
273 connect(m_gps, SIGNAL(timeout()),
274 m_ui, SLOT(gpsTimeout()));
276 connect(m_gps, SIGNAL(error(QString)),
277 m_ui, SLOT(gpsError(QString)));
280 void SituareEngine::signalsFromMainWindow()
282 qDebug() << __PRETTY_FUNCTION__;
284 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
285 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
287 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
288 this, SLOT(fetchUsernameFromSettings()));
290 // signals from map view
291 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
292 m_mapEngine, SLOT(setLocation(QPoint)));
294 connect(m_ui, SIGNAL(mapViewResized(QSize)),
295 m_mapEngine, SLOT(viewResized(QSize)));
297 connect(m_ui, SIGNAL(viewZoomFinished()),
298 m_mapEngine, SLOT(viewZoomFinished()));
300 // signals from zoom buttons (zoom panel and volume buttons)
301 connect(m_ui, SIGNAL(zoomIn()),
302 m_mapEngine, SLOT(zoomIn()));
304 connect(m_ui, SIGNAL(zoomOut()),
305 m_mapEngine, SLOT(zoomOut()));
307 // signals from menu buttons
308 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
309 this, SLOT(changeAutoCenteringSetting(bool)));
311 connect(m_ui, SIGNAL(gpsTriggered(bool)),
312 this, SLOT(enableGPS(bool)));
314 //signals from dialogs
315 connect(m_ui, SIGNAL(cancelLoginProcess()),
316 this, SLOT(loginProcessCancelled()));
318 connect(m_ui, SIGNAL(requestReverseGeo()),
319 this, SLOT(requestAddress()));
321 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
322 this, SLOT(requestUpdateLocation(QString,bool)));
324 // signals from user info tab
325 connect(m_ui, SIGNAL(refreshUserData()),
326 this, SLOT(refreshUserData()));
328 // signals from friend list tab
329 connect(m_ui, SIGNAL(findFriend(QPointF)),
330 m_mapEngine, SLOT(setViewLocation(QPointF)));
333 void SituareEngine::signalsFromMapEngine()
335 qDebug() << __PRETTY_FUNCTION__;
337 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
338 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
340 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
341 m_ui, SIGNAL(zoomLevelChanged(int)));
343 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
344 this, SLOT(disableAutoCentering()));
346 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
347 m_ui, SIGNAL(maxZoomLevelReached()));
349 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
350 m_ui, SIGNAL(minZoomLevelReached()));
352 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
353 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
356 void SituareEngine::signalsFromNetworkHandler()
358 qDebug() << __PRETTY_FUNCTION__;
360 connect(m_networkHandler, SIGNAL(connected()),
361 this, SLOT(connectedToNetwork()));
363 connect(m_networkHandler, SIGNAL(disconnected()),
364 this, SLOT(disconnectedFromNetwork()));
367 void SituareEngine::signalsFromSituareService()
369 qDebug() << __PRETTY_FUNCTION__;
371 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
372 m_ui, SIGNAL(reverseGeoReady(QString)));
374 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
375 this, SLOT(userDataChanged(User*, QList<User*>&)));
377 connect(m_situareService, SIGNAL(error(QString)),
378 this, SLOT(error(QString)));
380 connect(m_situareService, SIGNAL(updateWasSuccessful()),
381 this, SLOT(updateWasSuccessful()));
384 void SituareEngine::updateWasSuccessful()
386 qDebug() << __PRETTY_FUNCTION__;
388 m_situareService->fetchLocations();
391 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
393 qDebug() << __PRETTY_FUNCTION__;
395 m_ui->toggleProgressIndicator(false);
397 emit userLocationReady(user);
398 emit friendsLocationsReady(friendsList);