Merge branch 'master' of https://vcs.maemo.org/git/situare
[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
9     Situare is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License
11     version 2 as published by the Free Software Foundation.
12
13     Situare is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with Situare; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21     USA.
22  */
23
24 #include "engine.h"
25 #include "situarecommon.h"
26 #include "ui/mainwindow.h"
27 #include "gps/gpspositioninterface.h"
28
29 #ifdef Q_WS_MAEMO_5
30 #include "gps/gpsposition.h"
31 #else
32 #include "gps/gpspositionmockup.h"
33 #endif
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_gpsEnabled(false)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44     m_ui = new MainWindow;
45
46     m_situareService = new SituareService(this);
47
48     m_facebookAuthenticator = new FacebookAuthentication(this);
49
50 #ifdef Q_WS_MAEMO_5
51     m_gps = new GPSPosition(this);
52 #else
53     m_gps = new GPSPositionMockup(this);
54 #endif
55     m_gps->setMode(GPSPositionInterface::Default);
56
57     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
58             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
59     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
60             this, SLOT(loginOk()));
61     connect(m_ui, SIGNAL(cancelLoginProcess()),
62             this, SLOT(loginProcessCancelled()));
63
64     connect(m_ui, SIGNAL(requestReverseGeo()),
65             this, SLOT(requestAddress()));
66     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
67             m_ui, SIGNAL(reverseGeoReady(QString)));
68     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
69             this, SLOT(requestUpdateLocation(QString,bool)));
70     connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)),
71             this, SLOT(userDataChanged(User*,QList<User*>&)));
72     connect(this, SIGNAL(userLocationReady(User*)),
73             m_ui, SIGNAL(userLocationReady(User*)));
74     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
75             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
76     connect(m_situareService, SIGNAL(error(QString)),
77             this, SLOT(error(QString)));
78     connect(m_situareService, SIGNAL(updateWasSuccessful()),
79             this, SLOT(updateWasSuccessful()));
80
81     connect(m_ui, SIGNAL(refreshUserData()),
82             this, SLOT(refreshUserData()));
83
84     connect(m_gps, SIGNAL(timeout()),
85             m_ui, SLOT(gpsTimeout()));
86     connect(m_gps, SIGNAL(error(QString)),
87             m_ui, SLOT(gpsError(QString)));
88     connect(m_ui, SIGNAL(enableGPS(bool)),
89             this, SLOT(enableGPS(bool)));
90     connect(m_gps, SIGNAL(position(QPointF, qreal)),
91             m_ui, SIGNAL(positionReceived(QPointF, qreal)));
92     connect(m_ui, SIGNAL(enableAutoCentering(bool)),
93             this, SLOT(enableAutoCentering(bool)));
94
95     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
96             m_ui, SLOT(startLoginProcess(QUrl)));
97     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
98             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
99     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
100             m_ui, SLOT(loginFailed()));
101
102     QSettings settings(DIRECTORY_NAME, FILE_NAME);
103     QVariant gpsEnabled = settings.value(SETTINGS_GPS_ENABLED);
104     QVariant autoCenteringEnabled = settings.value(SETTINGS_AUTO_CENTERING_ENABLED);
105
106     enableGPS(gpsEnabled.toBool());
107     enableAutoCentering(autoCenteringEnabled.toBool());
108
109      m_facebookAuthenticator->start();
110 }
111
112 SituareEngine::~SituareEngine()
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     delete m_ui;
117
118     QSettings settings(DIRECTORY_NAME, FILE_NAME);
119     qDebug() << __PRETTY_FUNCTION__ << m_gpsEnabled;
120     qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
121     settings.setValue(SETTINGS_GPS_ENABLED, m_gpsEnabled);
122     settings.setValue(SETTINGS_AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
123 }
124
125 void SituareEngine::loginProcessCancelled()
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     m_ui->toggleProgressIndicator(false);
130     //ToDo: do something
131 }
132
133 void SituareEngine::error(const QString &error)
134 {
135     qDebug() << __PRETTY_FUNCTION__;
136     qDebug() << error;
137     // ToDo: signal UI?
138 }
139
140 void SituareEngine::loginOk()
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143
144     m_ui->show();
145     m_situareService->fetchLocations(); // request user locations
146 }
147
148 void SituareEngine::requestAddress()
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     QPointF coordinates;
153
154     if (m_gpsEnabled)
155         coordinates = m_gps->lastPosition();
156     else
157         coordinates = QPointF(0, 0);    //Manual position from map
158
159     m_situareService->reverseGeo(coordinates);
160 }
161
162 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
163 {
164     qDebug() << __PRETTY_FUNCTION__;
165
166     m_ui->toggleProgressIndicator(true);
167
168     QPointF coordinates;
169
170     if (m_gpsEnabled)
171         coordinates = m_gps->lastPosition();
172     else
173         coordinates = QPointF(0, 0);    //Manual position from map
174
175     m_situareService->updateLocation(coordinates, status, publish);
176 }
177
178 void SituareEngine::updateWasSuccessful()
179 {
180     qDebug() << __PRETTY_FUNCTION__;
181
182     m_situareService->fetchLocations();
183 }
184
185 void SituareEngine::refreshUserData()
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     m_ui->toggleProgressIndicator(true);
190
191     m_situareService->fetchLocations();
192 }
193
194 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
195 {
196     qDebug() << __PRETTY_FUNCTION__;
197
198     m_ui->toggleProgressIndicator(false);
199
200     emit userLocationReady(user);
201     emit friendsLocationsReady(friendsList);
202 }
203
204 void SituareEngine::enableGPS(bool enabled)
205 {
206     qDebug() << __PRETTY_FUNCTION__;
207
208     m_gpsEnabled = enabled;
209
210     if (enabled) {
211         m_gps->requestLastPosition();
212         m_gps->start();
213         m_ui->setGPSButton(true);
214     }
215     else {
216         m_gps->stop();
217         m_ui->setGPSButton(false);
218     }
219 }
220
221 void SituareEngine::enableAutoCentering(bool enabled)
222 {
223     qDebug() << __PRETTY_FUNCTION__;
224
225     m_autoCenteringEnabled = enabled;
226
227     if (enabled) {
228         m_ui->setAutoCenteringButton(true);
229         m_ui->autoCenteringEnabled(true);
230         m_gps->requestLastPosition();
231     }
232     else {
233         m_ui->setAutoCenteringButton(false);
234         m_ui->autoCenteringEnabled(false);
235     }
236 }