Moved settings saving from mainwindow to engine.
[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 "situarecommon.h"
25 #include "engine.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 SituareEngine::SituareEngine(QMainWindow *parent)
36     : QObject(parent),
37       m_autoCenteringEnabled(false),
38       m_gpsEnabled(false)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41     m_ui = new MainWindow;
42
43     m_situareService = new SituareService(this);
44
45     m_facebookAuthenticator = new FacebookAuthentication(this);
46
47 #ifdef Q_WS_MAEMO_5
48     m_gps = new GPSPosition(this);
49 #else
50     m_gps = new GPSPositionMockup(this);
51 #endif
52     m_gps->setMode(GPSPositionInterface::Default);
53
54     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
55             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
56     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
57             this, SLOT(loginOk()));
58     connect(m_ui, SIGNAL(cancelLoginProcess()),
59             this, SLOT(loginProcessCancelled()));
60
61     connect(m_ui, SIGNAL(requestReverseGeo()),
62             this, SLOT(requestAddress()));
63     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
64             m_ui, SIGNAL(reverseGeoReady(QString)));
65     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
66             this, SLOT(requestUpdateLocation(QString,bool)));
67     connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)),
68             this, SLOT(userDataChanged(User*,QList<User*>&)));
69     connect(this, SIGNAL(userLocationReady(User*)),
70             m_ui, SIGNAL(userLocationReady(User*)));
71     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
72             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
73     connect(m_situareService, SIGNAL(error(QString)),
74             this, SLOT(error(QString)));
75     connect(m_situareService, SIGNAL(updateWasSuccessful()),
76             this, SLOT(updateWasSuccessful()));
77
78     connect(m_ui, SIGNAL(refreshUserData()),
79             this, SLOT(refreshUserData()));
80
81     connect(m_gps, SIGNAL(timeout()),
82             m_ui, SLOT(gpsTimeout()));
83     connect(m_gps, SIGNAL(error(QString)),
84             m_ui, SLOT(gpsError(QString)));
85     connect(m_ui, SIGNAL(enableGPS(bool)),
86             this, SLOT(enableGPS(bool)));
87     connect(m_gps, SIGNAL(position(QPointF, qreal)),
88             m_ui, SIGNAL(positionReceived(QPointF, qreal)));
89     connect(m_ui, SIGNAL(enableAutoCentering(bool)),
90             this, SLOT(enableAutoCentering(bool)));
91
92     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
93             m_ui, SLOT(startLoginProcess(QUrl)));
94     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
95             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
96     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
97             m_ui, SLOT(loginFailed()));
98
99     QSettings settings(DIRECTORY_NAME, FILE_NAME);
100     QVariant gpsEnabled = settings.value(GPS_ENABLED);
101     QVariant autoCenteringEnabled = settings.value(AUTO_CENTERING_ENABLED);
102
103     enableGPS(gpsEnabled.toBool());
104     enableAutoCentering(autoCenteringEnabled.toBool());
105
106      m_facebookAuthenticator->start();
107 }
108
109 SituareEngine::~SituareEngine()
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112
113     delete m_ui;
114
115     QSettings settings(DIRECTORY_NAME, FILE_NAME);
116     qDebug() << __PRETTY_FUNCTION__ << m_gpsEnabled;
117     qDebug() << __PRETTY_FUNCTION__ << m_autoCenteringEnabled;
118     settings.setValue(GPS_ENABLED, m_gpsEnabled);
119     settings.setValue(AUTO_CENTERING_ENABLED, m_autoCenteringEnabled);
120 }
121
122 void SituareEngine::loginProcessCancelled()
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     m_ui->toggleProgressIndicator(false);
127     //ToDo: do something
128 }
129
130 void SituareEngine::error(const QString &error)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133     qDebug() << error;
134     // ToDo: signal UI?
135 }
136
137 void SituareEngine::loginOk()
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     m_ui->show();
142     m_situareService->fetchLocations(); // request user locations
143 }
144
145 void SituareEngine::requestAddress()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
150     m_situareService->reverseGeo(coordinates);
151 }
152
153 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     m_ui->toggleProgressIndicator(true);
158
159     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
160     m_situareService->updateLocation(coordinates, status, publish);
161 }
162
163 void SituareEngine::updateWasSuccessful()
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     m_situareService->fetchLocations();
168 }
169
170 void SituareEngine::refreshUserData()
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174     m_ui->toggleProgressIndicator(true);
175
176     m_situareService->fetchLocations();
177 }
178
179 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
180 {
181     qDebug() << __PRETTY_FUNCTION__;
182
183     m_ui->toggleProgressIndicator(false);
184
185     emit userLocationReady(user);
186     emit friendsLocationsReady(friendsList);
187 }
188
189 void SituareEngine::enableGPS(bool enabled)
190 {
191     qDebug() << __PRETTY_FUNCTION__;
192
193     m_gpsEnabled = enabled;
194
195     if (enabled) {
196         m_gps->lastPosition();
197         m_gps->start();
198         m_ui->setGPSButton(true);
199     }
200     else {
201         m_gps->stop();
202         m_ui->setGPSButton(false);
203     }
204 }
205
206 void SituareEngine::enableAutoCentering(bool enabled)
207 {
208     qDebug() << __PRETTY_FUNCTION__;
209
210     m_autoCenteringEnabled = enabled;
211
212     if (enabled) {
213         m_ui->setAutoCenteringButton(true);
214         m_ui->autoCenteringEnabled(true);
215         m_gps->lastPosition();
216     }
217     else {
218         m_ui->setAutoCenteringButton(false);
219         m_ui->autoCenteringEnabled(false);
220     }
221 }