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