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