Added minor changes to userinfo
[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
102 SituareEngine::~SituareEngine()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     delete m_ui;
107
108     QSettings settings(DIRECTORY_NAME, FILE_NAME);
109     qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
110     settings.setValue(SETTINGS_GPS_ENABLED, m_gps->isRunning());
111     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
112 }
113
114 void SituareEngine::changeAutoCenteringSetting(bool enabled)
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     m_autoCenteringEnabled = enabled;
119     enableAutoCentering(enabled);
120 }
121
122 void SituareEngine::disableAutoCentering()
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     changeAutoCenteringSetting(false);
127     m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
128 }
129
130 void SituareEngine::enableAutoCentering(bool enabled)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     m_ui->setAutoCenteringButtonEnabled(enabled);
135     m_mapEngine->setAutoCentering(enabled);
136
137     if (enabled)
138         m_gps->requestLastPosition();
139 }
140
141 void SituareEngine::enableGPS(bool enabled)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     m_ui->setGPSButtonEnabled(enabled);
146     m_mapEngine->setGPSEnabled(enabled);
147
148     if (enabled) {
149         m_gps->start();
150         enableAutoCentering(m_autoCenteringEnabled);
151         m_gps->requestLastPosition();
152     }
153     else {
154         m_gps->stop();
155         enableAutoCentering(false);
156     }
157 }
158
159 void SituareEngine::error(const QString &error)
160 {
161     qDebug() << __PRETTY_FUNCTION__;
162     qDebug() << error;
163     // ToDo: signal UI?
164 }
165
166 void SituareEngine::fetchUsernameFromSettings()
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169     m_ui->setUsername(m_facebookAuthenticator->loadUsername());
170 }
171
172 void SituareEngine::invalidCredentials()
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176     m_facebookAuthenticator->clearAccountInformation(true); // keep username = true
177     m_facebookAuthenticator->start();
178 }
179
180 void SituareEngine::loginActionPressed()
181 {
182     qDebug() << __PRETTY_FUNCTION__;
183
184     if(m_loggedIn) {
185         logout();
186         m_situareService->clearUserData();
187     }
188     else {
189         m_facebookAuthenticator->start();
190     }
191 }
192
193 void SituareEngine::loginOk(bool freshLogin, const FacebookCredentials &credentials)
194 {
195     qDebug() << __PRETTY_FUNCTION__;
196
197     m_loggedIn = true;
198     m_ui->loggedIn(m_loggedIn);
199
200     if(freshLogin) {
201         m_facebookAuthenticator->saveUsername(m_ui->username());
202     }
203     m_ui->show();
204     m_situareService->credentialsReady(credentials);
205     m_situareService->fetchLocations(); // request user locations
206 }
207
208 void SituareEngine::loginProcessCancelled()
209 {
210     qDebug() << __PRETTY_FUNCTION__;
211
212     m_ui->toggleProgressIndicator(false);
213     m_ui->showPanels(m_loggedIn);
214 }
215
216 void SituareEngine::logout()
217 {
218     qDebug() << __PRETTY_FUNCTION__;
219
220     m_loggedIn = false;
221     m_ui->loggedIn(m_loggedIn);
222     m_facebookAuthenticator->clearAccountInformation();
223 }
224
225 void SituareEngine::refreshUserData()
226 {
227     qDebug() << __PRETTY_FUNCTION__;
228
229     m_ui->toggleProgressIndicator(true);
230
231     m_situareService->fetchLocations();
232 }
233
234 void SituareEngine::requestAddress()
235 {
236     qDebug() << __PRETTY_FUNCTION__;
237
238     if (m_gps->isRunning())
239         m_situareService->reverseGeo(m_gps->lastPosition());
240     else
241         m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
242 }
243
244 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
245 {
246     qDebug() << __PRETTY_FUNCTION__;
247
248     m_ui->toggleProgressIndicator(true);
249
250     if (m_gps->isRunning())
251         m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
252     else
253         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
254 }
255
256 void SituareEngine::signalsFromFacebookAuthenticator()
257 {
258     qDebug() << __PRETTY_FUNCTION__;
259
260     connect(m_facebookAuthenticator, SIGNAL(credentialsChanged(FacebookCredentials)),
261             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
262
263     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
264             this, SLOT(loginOk(bool, FacebookCredentials)));
265
266     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
267             m_ui, SLOT(startLoginProcess(QUrl)));
268
269     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
270             m_ui, SLOT(loginFailed()));
271 }
272
273 void SituareEngine::signalsFromGPS()
274 {
275     qDebug() << __PRETTY_FUNCTION__;
276
277     connect(m_gps, SIGNAL(position(QPointF,qreal)),
278             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
279
280     connect(m_gps, SIGNAL(timeout()),
281             m_ui, SLOT(gpsTimeout()));
282
283     connect(m_gps, SIGNAL(error(QString)),
284             m_ui, SLOT(gpsError(QString)));
285 }
286
287 void SituareEngine::signalsFromMainWindow()
288 {
289     qDebug() << __PRETTY_FUNCTION__;    
290
291     connect(m_ui, SIGNAL(loginActionPressed()),
292             this, SLOT(loginActionPressed()));
293
294     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
295             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
296
297     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
298             this, SLOT(fetchUsernameFromSettings()));
299
300     // signals from map view
301     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
302             m_mapEngine, SLOT(setLocation(QPoint)));
303
304     connect(m_ui, SIGNAL(mapViewResized(QSize)),
305             m_mapEngine, SLOT(viewResized(QSize)));
306
307     connect(m_ui, SIGNAL(viewZoomFinished()),
308             m_mapEngine, SLOT(viewZoomFinished()));
309
310     // signals from zoom buttons (zoom panel and volume buttons)
311     connect(m_ui, SIGNAL(zoomIn()),
312             m_mapEngine, SLOT(zoomIn()));
313
314     connect(m_ui, SIGNAL(zoomOut()),
315             m_mapEngine, SLOT(zoomOut()));
316
317     // signals from menu buttons
318     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
319             this, SLOT(changeAutoCenteringSetting(bool)));
320
321     connect(m_ui, SIGNAL(gpsTriggered(bool)),
322             this, SLOT(enableGPS(bool)));
323
324     //signals from dialogs
325     connect(m_ui, SIGNAL(cancelLoginProcess()),
326             this, SLOT(loginProcessCancelled()));
327
328     connect(m_ui, SIGNAL(requestReverseGeo()),
329             this, SLOT(requestAddress()));
330
331     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
332             this, SLOT(requestUpdateLocation(QString,bool)));
333
334     // signals from user info tab
335     connect(m_ui, SIGNAL(refreshUserData()),
336             this, SLOT(refreshUserData()));
337
338     connect(m_ui, SIGNAL(findUser(QPointF)),
339             m_mapEngine, SLOT(setViewLocation(QPointF)));
340
341     // signals from friend list tab
342     connect(m_ui, SIGNAL(findFriend(QPointF)),
343             m_mapEngine, SLOT(setViewLocation(QPointF)));
344 }
345
346 void SituareEngine::signalsFromMapEngine()
347 {
348     qDebug() << __PRETTY_FUNCTION__;
349
350     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
351             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
352
353     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
354             m_ui, SIGNAL(zoomLevelChanged(int)));
355
356     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
357             this, SLOT(disableAutoCentering()));
358
359     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
360             m_ui, SIGNAL(maxZoomLevelReached()));
361
362     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
363             m_ui, SIGNAL(minZoomLevelReached()));
364
365     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
366             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
367 }
368
369 void SituareEngine::signalsFromSituareService()
370 {
371     qDebug() << __PRETTY_FUNCTION__;
372
373     connect(m_situareService, SIGNAL(error(QString)),
374             this, SLOT(error(QString)));
375
376     connect(m_situareService, SIGNAL(invalidSessionCredentials()),
377             this, SLOT(invalidCredentials()));
378
379     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
380             m_ui, SIGNAL(reverseGeoReady(QString)));
381
382     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
383             this, SLOT(userDataChanged(User*, QList<User*>&)));
384
385     connect(m_situareService, SIGNAL(updateWasSuccessful()),
386             this, SLOT(updateWasSuccessful()));
387 }
388
389 void SituareEngine::updateWasSuccessful()
390 {
391     qDebug() << __PRETTY_FUNCTION__;
392
393     m_situareService->fetchLocations();
394 }
395
396 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
397 {
398     qDebug() << __PRETTY_FUNCTION__;
399
400     m_ui->toggleProgressIndicator(false);
401
402     emit userLocationReady(user);
403     emit friendsLocationsReady(friendsList);
404 }