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