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