Cleanup & commenting
[situare] / src / engine / engine.cpp
1  /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
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
9
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.
13
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.
18
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,
22     USA.
23  */
24
25 #include "engine.h"
26 #include "common.h"
27 #include "ui/mainwindow.h"
28 #include "gps/gpspositioninterface.h"
29 #include "map/mapengine.h"
30 #include "map/mapview.h"
31
32 #ifdef Q_WS_MAEMO_5
33 #include "gps/gpsposition.h"
34 #else
35 #include "gps/gpspositionmockup.h"
36 #endif
37
38 const QString SETTINGS_GPS_ENABLED = "GPS_ENABLED";
39 const QString SETTINGS_AUTO_CENTERING_ENABLED = "AUTO_CENTERING_ENABLED";
40
41 SituareEngine::SituareEngine(QMainWindow *parent)
42     : QObject(parent),
43       m_autoCenteringEnabled(false),
44       m_latestLocation()
45 {
46     qDebug() << __PRETTY_FUNCTION__;
47     m_ui = new MainWindow;
48
49     // build MapEngine
50     m_mapEngine = new MapEngine(this);
51     m_ui->setMapViewScene(m_mapEngine->scene());
52
53     // build GPS
54 #ifdef Q_WS_MAEMO_5
55     m_gps = new GPSPosition(this);
56 #else
57     m_gps = new GPSPositionMockup(this);
58 #endif
59     m_gps->setMode(GPSPositionInterface::Default);
60
61     // build SituareService
62     m_situareService = new SituareService(this);
63
64     // build FacebookAuthenticator
65     m_facebookAuthenticator = new FacebookAuthentication(this);
66
67     // connect signals
68     signalsFromMapEngine();
69     signalsFromGPS();
70     signalsFromSituareService();
71     signalsFromMainWindow();
72     signalsFromFacebookAuthenticator();
73
74     connect(this, SIGNAL(userLocationReady(User*)),
75             m_ui, SIGNAL(userLocationReady(User*)));
76
77     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
78             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
79
80     QSettings settings(DIRECTORY_NAME, FILE_NAME);
81     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
82     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
83
84     // set features on / off based on settings
85     changeAutoCenteringSetting(autoCenteringEnabled.toBool());
86     enableGPS(gpsEnabled.toBool());
87
88     // show messages at startup if features are enabled automatically
89     if (gpsEnabled.toBool())
90         m_ui->showMaemoInformationBox(tr("GPS enabled"));
91     if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
92         m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
93
94     m_mapEngine->init();
95
96     m_facebookAuthenticator->start();
97 }
98
99 SituareEngine::~SituareEngine()
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102
103     delete m_ui;
104
105     QSettings settings(DIRECTORY_NAME, FILE_NAME);
106     qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
107     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
108     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
109 }
110
111 void SituareEngine::changeAutoCenteringSetting(bool enabled)
112 {
113     m_autoCenteringEnabled = enabled;
114     enableAutoCentering(enabled);
115 }
116
117 void SituareEngine::disableAutoCentering()
118 {
119     changeAutoCenteringSetting(false);
120     m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
121 }
122
123 void SituareEngine::enableAutoCentering(bool enabled)
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     m_ui->setAutoCenteringButtonEnabled(enabled);
128     m_mapEngine->setAutoCentering(enabled);
129
130     if (enabled)
131         m_gps->requestLastPosition();
132 }
133
134 void SituareEngine::enableGPS(bool enabled)
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     m_ui->setGPSButtonEnabled(enabled);
139     m_mapEngine->setGPSEnabled(enabled);
140
141     if (enabled) {
142         m_gps->start();
143         enableAutoCentering(m_autoCenteringEnabled);
144         m_gps->requestLastPosition();
145     }
146     else {
147         m_gps->stop();
148         enableAutoCentering(false);
149     }
150 }
151
152 void SituareEngine::error(const QString &error)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155     qDebug() << error;
156     // ToDo: signal UI?
157 }
158
159 void SituareEngine::loginOk()
160 {
161     qDebug() << __PRETTY_FUNCTION__;
162
163     m_ui->show();
164     m_situareService->fetchLocations(); // request user locations
165 }
166
167 void SituareEngine::loginProcessCancelled()
168 {
169     qDebug() << __PRETTY_FUNCTION__;
170
171     m_ui->toggleProgressIndicator(false);
172     //ToDo: do something
173 }
174
175 void SituareEngine::refreshUserData()
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     m_ui->toggleProgressIndicator(true);
180
181     m_situareService->fetchLocations();
182 }
183
184 void SituareEngine::requestAddress()
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     if (m_gps->isRunning()) {
189         m_latestLocation = m_gps->lastPosition();
190         m_situareService->reverseGeo(m_latestLocation);
191     }
192     else {
193         m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
194     }
195 }
196
197 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
198 {
199     qDebug() << __PRETTY_FUNCTION__;
200
201     m_ui->toggleProgressIndicator(true);
202
203     if (m_gps->isRunning()) {
204         m_latestLocation = m_gps->lastPosition();
205         m_situareService->updateLocation(m_latestLocation, status, publish);
206     }
207     else {
208         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
209     }
210 }
211
212 void SituareEngine::signalsFromFacebookAuthenticator()
213 {
214     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
215             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
216
217     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
218             this, SLOT(loginOk()));
219
220     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
221             m_ui, SLOT(startLoginProcess(QUrl)));
222
223     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
224             m_ui, SLOT(loginFailed()));
225 }
226
227 void SituareEngine::signalsFromGPS()
228 {
229     connect(m_gps, SIGNAL(position(QPointF,qreal)),
230             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
231
232     connect(m_gps, SIGNAL(timeout()),
233             m_ui, SLOT(gpsTimeout()));
234
235     connect(m_gps, SIGNAL(error(QString)),
236             m_ui, SLOT(gpsError(QString)));
237 }
238
239 void SituareEngine::signalsFromMainWindow()
240 {
241     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
242             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
243
244     // signals from map view
245     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
246             m_mapEngine, SLOT(setLocation(QPoint)));
247
248     connect(m_ui, SIGNAL(mapViewResized(QSize)),
249             m_mapEngine, SLOT(viewResized(QSize)));
250
251     connect(m_ui, SIGNAL(viewZoomFinished()),
252             m_mapEngine, SLOT(viewZoomFinished()));
253
254     // signals from zoom buttons (zoom panel and volume buttons)
255     connect(m_ui, SIGNAL(zoomIn()),
256             m_mapEngine, SLOT(zoomIn()));
257
258     connect(m_ui, SIGNAL(zoomOut()),
259             m_mapEngine, SLOT(zoomOut()));
260
261     // signals from menu buttons
262     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
263             this, SLOT(changeAutoCenteringSetting(bool)));
264
265     connect(m_ui, SIGNAL(gpsTriggered(bool)),
266             this, SLOT(enableGPS(bool)));
267
268     //signals from dialogs
269     connect(m_ui, SIGNAL(cancelLoginProcess()),
270             this, SLOT(loginProcessCancelled()));
271
272     connect(m_ui, SIGNAL(requestReverseGeo()),
273             this, SLOT(requestAddress()));
274
275     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
276             this, SLOT(requestUpdateLocation(QString,bool)));
277
278     // signals from user info tab
279     connect(m_ui, SIGNAL(refreshUserData()),
280             this, SLOT(refreshUserData()));
281
282     // signals from friend list tab
283     connect(m_ui, SIGNAL(findFriend(QPointF)),
284             m_mapEngine, SLOT(setViewLocation(QPointF)));
285 }
286
287 void SituareEngine::signalsFromMapEngine()
288 {
289     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
290             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
291
292     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
293             m_ui, SIGNAL(zoomLevelChanged(int)));
294
295     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
296             this, SLOT(disableAutoCentering()));
297
298     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
299             m_ui, SIGNAL(maxZoomLevelReached()));
300
301     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
302             m_ui, SIGNAL(minZoomLevelReached()));
303 }
304
305 void SituareEngine::signalsFromSituareService()
306 {
307     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
308             m_ui, SIGNAL(reverseGeoReady(QString)));
309
310     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
311             this, SLOT(userDataChanged(User*, QList<User*>&)));
312
313     connect(m_situareService, SIGNAL(error(QString)),
314             this, SLOT(error(QString)));
315
316     connect(m_situareService, SIGNAL(updateWasSuccessful()),
317             this, SLOT(updateWasSuccessful()));
318
319     connect(this, SIGNAL(userLocationReady(User*)),
320             m_mapEngine, SLOT(receiveOwnLocation(User*)));
321
322     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
323             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
324 }
325
326 void SituareEngine::updateWasSuccessful()
327 {
328     qDebug() << __PRETTY_FUNCTION__;
329
330     m_situareService->fetchLocations();
331 }
332
333 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
334 {
335     qDebug() << __PRETTY_FUNCTION__;
336
337     m_ui->toggleProgressIndicator(false);
338
339     emit userLocationReady(user);
340     emit friendsLocationsReady(friendsList);
341 }