f74c46cf4743f01d791d193fc6da1f90d488f3f2
[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();
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_facebookAuthenticator, SIGNAL(quitSituare()),
57             this, SLOT(quitApplication()));
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      m_facebookAuthenticator->start();
91 }
92
93 SituareEngine::~SituareEngine()
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     delete m_ui;
98 }
99
100 void SituareEngine::quitApplication()
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     m_facebookAuthenticator->setAttribute(Qt::WA_DeleteOnClose);
105     m_facebookAuthenticator->close();
106 }
107
108 void SituareEngine::error(const QString &error)
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111     qDebug() << error;
112     // ToDo: signal UI?
113 }
114
115 void SituareEngine::loginOk()
116 {
117     qDebug() << __PRETTY_FUNCTION__;
118
119     QSettings settings(DIRECTORY_NAME, FILE_NAME);
120     QVariant gpsEnabled = settings.value(GPS_ENABLED);
121     QVariant autoCenteringEnabled = settings.value(AUTO_CENTERING_ENABLED);
122
123     m_facebookAuthenticator->setAttribute(Qt::WA_DeleteOnClose);
124     m_facebookAuthenticator->close();
125     m_ui->show();
126     m_situareService->fetchLocations(); // request user locations
127
128     enableGPS(gpsEnabled.toBool());
129     enableAutoCentering(autoCenteringEnabled.toBool());
130 }
131
132 void SituareEngine::requestAddress()
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
137     m_situareService->reverseGeo(coordinates);
138 }
139
140 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143
144     m_ui->toggleProgressIndicator(true);
145
146     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
147     m_situareService->updateLocation(coordinates, status, publish);
148 }
149
150 void SituareEngine::updateWasSuccessful()
151 {
152     qDebug() << __PRETTY_FUNCTION__;
153
154     m_situareService->fetchLocations();
155 }
156
157 void SituareEngine::refreshUserData()
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     m_ui->toggleProgressIndicator(true);
162
163     m_situareService->fetchLocations();
164 }
165
166 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     m_ui->toggleProgressIndicator(false);
171
172     emit userLocationReady(user);
173     emit friendsLocationsReady(friendsList);
174 }
175
176 void SituareEngine::enableGPS(bool enabled)
177 {
178     qDebug() << __PRETTY_FUNCTION__ << enabled;
179
180     if (enabled) {
181         m_gps->lastPosition();
182         m_gps->start();
183         m_ui->setGPSButton(true);
184     }
185     else {
186         m_gps->stop();
187         m_ui->setGPSButton(false);
188     }
189 }
190
191 void SituareEngine::enableAutoCentering(bool enabled)
192 {
193     qDebug() << __PRETTY_FUNCTION__ << enabled;
194
195     if (enabled) {
196         m_ui->setAutoCenteringButton(true);
197         m_ui->autoCenteringEnabled(true);
198         m_gps->lastPosition();
199     }
200     else {
201         m_ui->setAutoCenteringButton(false);
202         m_ui->autoCenteringEnabled(false);
203     }
204 }