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