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->connect();
104 // m_facebookAuthenticator->start();
107 SituareEngine::~SituareEngine()
109 qDebug() << __PRETTY_FUNCTION__;
113 QSettings settings(DIRECTORY_NAME, FILE_NAME);
114 qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
115 settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
116 settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
119 void SituareEngine::changeAutoCenteringSetting(bool enabled)
121 qDebug() << __PRETTY_FUNCTION__;
123 m_autoCenteringEnabled = enabled;
124 enableAutoCentering(enabled);
127 void SituareEngine::connectedToNetwork()
129 qDebug() << __PRETTY_FUNCTION__;
133 m_facebookAuthenticator->start();
134 //TODO: Start map image fetching
137 void SituareEngine::disableAutoCentering()
139 qDebug() << __PRETTY_FUNCTION__;
141 changeAutoCenteringSetting(false);
142 m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
145 void SituareEngine::disconnectedFromNetwork()
147 qDebug() << __PRETTY_FUNCTION__;
149 //TODO: Stop map image fetching
152 void SituareEngine::enableAutoCentering(bool enabled)
154 qDebug() << __PRETTY_FUNCTION__;
156 m_ui->setAutoCenteringButtonEnabled(enabled);
157 m_mapEngine->setAutoCentering(enabled);
160 m_gps->requestLastPosition();
163 void SituareEngine::enableGPS(bool enabled)
165 qDebug() << __PRETTY_FUNCTION__;
167 m_ui->setGPSButtonEnabled(enabled);
168 m_mapEngine->setGPSEnabled(enabled);
172 enableAutoCentering(m_autoCenteringEnabled);
173 m_gps->requestLastPosition();
177 enableAutoCentering(false);
181 void SituareEngine::error(const QString &error)
183 qDebug() << __PRETTY_FUNCTION__;
188 void SituareEngine::fetchUsernameFromSettings()
190 qDebug() << __PRETTY_FUNCTION__;
191 m_ui->setUsername(m_facebookAuthenticator->loadUsername());
194 void SituareEngine::loginOk(bool freshLogin)
196 qDebug() << __PRETTY_FUNCTION__;
199 m_facebookAuthenticator->saveUsername(m_ui->username());
202 m_situareService->fetchLocations(); // request user locations
205 void SituareEngine::loginProcessCancelled()
207 qDebug() << __PRETTY_FUNCTION__;
209 m_ui->toggleProgressIndicator(false);
213 void SituareEngine::refreshUserData()
215 qDebug() << __PRETTY_FUNCTION__;
217 m_ui->toggleProgressIndicator(true);
219 m_situareService->fetchLocations();
222 void SituareEngine::requestAddress()
224 qDebug() << __PRETTY_FUNCTION__;
226 if (m_gps->isRunning())
227 m_situareService->reverseGeo(m_gps->lastPosition());
229 m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
232 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
234 qDebug() << __PRETTY_FUNCTION__;
236 m_ui->toggleProgressIndicator(true);
238 if (m_gps->isRunning())
239 m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
241 m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
244 void SituareEngine::signalsFromFacebookAuthenticator()
246 qDebug() << __PRETTY_FUNCTION__;
248 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
249 m_situareService, SLOT(credentialsReady(bool, FacebookCredentials)));
251 connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
252 this, SLOT(loginOk(bool)));
254 connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
255 m_ui, SLOT(startLoginProcess(QUrl)));
257 connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
258 m_ui, SLOT(loginFailed()));
261 void SituareEngine::signalsFromGPS()
263 qDebug() << __PRETTY_FUNCTION__;
265 connect(m_gps, SIGNAL(position(QPointF,qreal)),
266 m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
268 connect(m_gps, SIGNAL(timeout()),
269 m_ui, SLOT(gpsTimeout()));
271 connect(m_gps, SIGNAL(error(QString)),
272 m_ui, SLOT(gpsError(QString)));
275 void SituareEngine::signalsFromMainWindow()
277 qDebug() << __PRETTY_FUNCTION__;
279 connect(m_ui, SIGNAL(updateCredentials(QUrl)),
280 m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
282 connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
283 this, SLOT(fetchUsernameFromSettings()));
285 // signals from map view
286 connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
287 m_mapEngine, SLOT(setLocation(QPoint)));
289 connect(m_ui, SIGNAL(mapViewResized(QSize)),
290 m_mapEngine, SLOT(viewResized(QSize)));
292 connect(m_ui, SIGNAL(viewZoomFinished()),
293 m_mapEngine, SLOT(viewZoomFinished()));
295 // signals from zoom buttons (zoom panel and volume buttons)
296 connect(m_ui, SIGNAL(zoomIn()),
297 m_mapEngine, SLOT(zoomIn()));
299 connect(m_ui, SIGNAL(zoomOut()),
300 m_mapEngine, SLOT(zoomOut()));
302 // signals from menu buttons
303 connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
304 this, SLOT(changeAutoCenteringSetting(bool)));
306 connect(m_ui, SIGNAL(gpsTriggered(bool)),
307 this, SLOT(enableGPS(bool)));
309 //signals from dialogs
310 connect(m_ui, SIGNAL(cancelLoginProcess()),
311 this, SLOT(loginProcessCancelled()));
313 connect(m_ui, SIGNAL(requestReverseGeo()),
314 this, SLOT(requestAddress()));
316 connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
317 this, SLOT(requestUpdateLocation(QString,bool)));
319 // signals from user info tab
320 connect(m_ui, SIGNAL(refreshUserData()),
321 this, SLOT(refreshUserData()));
323 // signals from friend list tab
324 connect(m_ui, SIGNAL(findFriend(QPointF)),
325 m_mapEngine, SLOT(setViewLocation(QPointF)));
328 void SituareEngine::signalsFromMapEngine()
330 qDebug() << __PRETTY_FUNCTION__;
332 connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
333 m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
335 connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
336 m_ui, SIGNAL(zoomLevelChanged(int)));
338 connect(m_mapEngine, SIGNAL(mapScrolledManually()),
339 this, SLOT(disableAutoCentering()));
341 connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
342 m_ui, SIGNAL(maxZoomLevelReached()));
344 connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
345 m_ui, SIGNAL(minZoomLevelReached()));
347 connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
348 m_ui, SIGNAL(locationItemClicked(QList<QString>)));
351 void SituareEngine::signalsFromNetworkHandler()
353 qDebug() << __PRETTY_FUNCTION__;
355 connect(m_networkHandler, SIGNAL(connected()),
356 this, SLOT(connectedToNetwork()));
358 connect(m_networkHandler, SIGNAL(disconnected()),
359 this, SLOT(disconnectedFromNetwork()));
362 void SituareEngine::signalsFromSituareService()
364 qDebug() << __PRETTY_FUNCTION__;
366 connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
367 m_ui, SIGNAL(reverseGeoReady(QString)));
369 connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
370 this, SLOT(userDataChanged(User*, QList<User*>&)));
372 connect(m_situareService, SIGNAL(error(QString)),
373 this, SLOT(error(QString)));
375 connect(m_situareService, SIGNAL(updateWasSuccessful()),
376 this, SLOT(updateWasSuccessful()));
379 void SituareEngine::updateWasSuccessful()
381 qDebug() << __PRETTY_FUNCTION__;
383 m_situareService->fetchLocations();
386 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
388 qDebug() << __PRETTY_FUNCTION__;
390 m_ui->toggleProgressIndicator(false);
392 emit userLocationReady(user);
393 emit friendsLocationsReady(friendsList);