finalising this feature branch
[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     initializeGpsAndAutocentering();
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     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
166         connect(m_gps, SIGNAL(position(QPointF,qreal)),
167                 this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
168
169         changeAutoCenteringSetting(true);
170         enableGPS(true);
171
172         m_ui->showMaemoInformationBox(tr("GPS enabled"));
173         m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
174     }
175
176     else { // Normal start
177         changeAutoCenteringSetting(autoCenteringEnabled.toBool());
178         enableGPS(gpsEnabled.toBool());
179
180         if (gpsEnabled.toBool())
181             m_ui->showMaemoInformationBox(tr("GPS enabled"));
182
183         if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
184             m_ui->showMaemoInformationBox(tr("Auto centering enabled"));        
185     } 
186 }
187
188 void SituareEngine::loginOk(bool freshLogin)
189 {
190     qDebug() << __PRETTY_FUNCTION__;
191
192     if(freshLogin) {
193         m_facebookAuthenticator->saveUsername(m_ui->username());
194     }
195     m_ui->show();
196     m_situareService->fetchLocations(); // request user locations
197 }
198
199 void SituareEngine::loginProcessCancelled()
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     m_ui->toggleProgressIndicator(false);
204     //ToDo: do something
205 }
206
207 void SituareEngine::refreshUserData()
208 {
209     qDebug() << __PRETTY_FUNCTION__;
210
211     m_ui->toggleProgressIndicator(true);
212
213     m_situareService->fetchLocations();
214 }
215
216 void SituareEngine::requestAddress()
217 {
218     qDebug() << __PRETTY_FUNCTION__;
219
220     if (m_gps->isRunning())
221         m_situareService->reverseGeo(m_gps->lastPosition());
222     else
223         m_situareService->reverseGeo(m_mapEngine->centerGeoCoordinate());
224 }
225
226 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
227 {
228     qDebug() << __PRETTY_FUNCTION__;
229
230     m_ui->toggleProgressIndicator(true);
231
232     if (m_gps->isRunning())
233         m_situareService->updateLocation(m_gps->lastPosition(), status, publish);
234     else
235         m_situareService->updateLocation(m_mapEngine->centerGeoCoordinate(), status, publish);
236 }
237
238 void SituareEngine::setFirstStartZoomLevel(QPointF latLonCoordinate, qreal accuracy)
239 {
240     Q_UNUSED(latLonCoordinate);
241     Q_UNUSED(accuracy);
242
243     if (m_autoCenteringEnabled) // autocentering is disabled when map is scrolled
244         m_mapEngine->setZoomLevel(DEFAULT_ZOOM_LEVEL_WHEN_GPS_IS_AVAILABLE);
245
246     disconnect(m_gps, SIGNAL(position(QPointF,qreal)),
247                this, SLOT(setFirstStartZoomLevel(QPointF,qreal)));
248 }
249
250 void SituareEngine::signalsFromFacebookAuthenticator()
251 {
252     qDebug() << __PRETTY_FUNCTION__;
253
254     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
255             m_situareService, SLOT(credentialsReady(bool, FacebookCredentials)));
256
257     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(bool, FacebookCredentials)),
258             this, SLOT(loginOk(bool)));
259
260     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
261             m_ui, SLOT(startLoginProcess(QUrl)));
262
263     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
264             m_ui, SLOT(loginFailed()));
265 }
266
267 void SituareEngine::signalsFromGPS()
268 {
269     qDebug() << __PRETTY_FUNCTION__;
270
271     connect(m_gps, SIGNAL(position(QPointF,qreal)),
272             m_mapEngine, SLOT(gpsPositionUpdate(QPointF,qreal)));
273
274     connect(m_gps, SIGNAL(timeout()),
275             m_ui, SLOT(gpsTimeout()));
276
277     connect(m_gps, SIGNAL(error(QString)),
278             m_ui, SLOT(gpsError(QString)));
279 }
280
281 void SituareEngine::signalsFromMainWindow()
282 {
283     qDebug() << __PRETTY_FUNCTION__;    
284
285     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
286             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
287
288     connect(m_ui, SIGNAL(fetchUsernameFromSettings()),
289             this, SLOT(fetchUsernameFromSettings()));
290
291     // signals from map view
292     connect(m_ui, SIGNAL(mapViewScrolled(QPoint)),
293             m_mapEngine, SLOT(setLocation(QPoint)));
294
295     connect(m_ui, SIGNAL(mapViewResized(QSize)),
296             m_mapEngine, SLOT(viewResized(QSize)));
297
298     connect(m_ui, SIGNAL(viewZoomFinished()),
299             m_mapEngine, SLOT(viewZoomFinished()));
300
301     // signals from zoom buttons (zoom panel and volume buttons)
302     connect(m_ui, SIGNAL(zoomIn()),
303             m_mapEngine, SLOT(zoomIn()));
304
305     connect(m_ui, SIGNAL(zoomOut()),
306             m_mapEngine, SLOT(zoomOut()));
307
308     // signals from menu buttons
309     connect(m_ui, SIGNAL(autoCenteringTriggered(bool)),
310             this, SLOT(changeAutoCenteringSetting(bool)));
311
312     connect(m_ui, SIGNAL(gpsTriggered(bool)),
313             this, SLOT(enableGPS(bool)));
314
315     //signals from dialogs
316     connect(m_ui, SIGNAL(cancelLoginProcess()),
317             this, SLOT(loginProcessCancelled()));
318
319     connect(m_ui, SIGNAL(requestReverseGeo()),
320             this, SLOT(requestAddress()));
321
322     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
323             this, SLOT(requestUpdateLocation(QString,bool)));
324
325     // signals from user info tab
326     connect(m_ui, SIGNAL(refreshUserData()),
327             this, SLOT(refreshUserData()));
328
329     // signals from friend list tab
330     connect(m_ui, SIGNAL(findFriend(QPointF)),
331             m_mapEngine, SLOT(setViewLocation(QPointF)));
332 }
333
334 void SituareEngine::signalsFromMapEngine()
335 {
336     qDebug() << __PRETTY_FUNCTION__;
337
338     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
339             m_ui, SIGNAL(centerToSceneCoordinates(QPoint)));
340
341     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
342             m_ui, SIGNAL(zoomLevelChanged(int)));
343
344     connect(m_mapEngine, SIGNAL(mapScrolledManually()),
345             this, SLOT(disableAutoCentering()));
346
347     connect(m_mapEngine, SIGNAL(maxZoomLevelReached()),
348             m_ui, SIGNAL(maxZoomLevelReached()));
349
350     connect(m_mapEngine, SIGNAL(minZoomLevelReached()),
351             m_ui, SIGNAL(minZoomLevelReached()));
352
353     connect(m_mapEngine, SIGNAL(locationItemClicked(QList<QString>)),
354             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
355 }
356
357 void SituareEngine::signalsFromSituareService()
358 {
359     qDebug() << __PRETTY_FUNCTION__;
360
361     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
362             m_ui, SIGNAL(reverseGeoReady(QString)));
363
364     connect(m_situareService, SIGNAL(userDataChanged(User*, QList<User*>&)),
365             this, SLOT(userDataChanged(User*, QList<User*>&)));
366
367     connect(m_situareService, SIGNAL(error(QString)),
368             this, SLOT(error(QString)));
369
370     connect(m_situareService, SIGNAL(updateWasSuccessful()),
371             this, SLOT(updateWasSuccessful()));
372 }
373
374 void SituareEngine::updateWasSuccessful()
375 {
376     qDebug() << __PRETTY_FUNCTION__;
377
378     m_situareService->fetchLocations();
379 }
380
381 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
382 {
383     qDebug() << __PRETTY_FUNCTION__;
384
385     m_ui->toggleProgressIndicator(false);
386
387     emit userLocationReady(user);
388     emit friendsLocationsReady(friendsList);
389 }