FacebookAuthentication window removal
[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 "ui/mainwindow.h"
26 #include "gps/gpspositioninterface.h"
27
28 #ifdef Q_WS_MAEMO_5
29 #include "gps/gpsposition.h"
30 #else
31 #include "gps/gpspositionmockup.h"
32 #endif
33
34 SituareEngine::SituareEngine(QMainWindow *parent)
35     : QObject(parent)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38     m_ui = new MainWindow;
39
40     m_situareService = new SituareService(this);
41
42     m_facebookAuthenticator = new FacebookAuthentication(this);
43
44 #ifdef Q_WS_MAEMO_5
45     m_gps = new GPSPosition(this);
46 #else
47     m_gps = new GPSPositionMockup(this);
48 #endif
49
50     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
51             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
52     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
53             this, SLOT(loginOk()));
54     connect(m_ui, SIGNAL(cancelLoginProcess()),
55             this, SLOT(loginProcessCancelled()));
56
57     connect(m_ui, SIGNAL(requestReverseGeo()),
58             this, SLOT(requestAddress()));
59     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
60             m_ui, SIGNAL(reverseGeoReady(QString)));
61     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
62             this, SLOT(requestUpdateLocation(QString,bool)));
63     connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)),
64             this, SLOT(userDataChanged(User*,QList<User*>&)));
65     connect(this, SIGNAL(userLocationReady(User*)),
66             m_ui, SIGNAL(userLocationReady(User*)));
67     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
68             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
69     connect(m_situareService, SIGNAL(error(QString)),
70             this, SLOT(error(QString)));
71     connect(m_situareService, SIGNAL(updateWasSuccessful()),
72             this, SLOT(updateWasSuccessful()));
73
74     connect(m_ui, SIGNAL(refreshUserData()),
75             this, SLOT(refreshUserData()));
76
77     connect(m_gps, SIGNAL(timeout()),
78             m_ui, SLOT(gpsTimeout()));
79     connect(m_gps, SIGNAL(error(QString)),
80             m_ui, SLOT(gpsError(QString)));
81     connect(m_ui, SIGNAL(enableGPS(bool)),
82             this, SLOT(enableGPS(bool)));
83     connect(m_gps, SIGNAL(position(QPointF)),
84             m_ui, SIGNAL(positionReceived(QPointF)));
85
86     connect(m_facebookAuthenticator, SIGNAL(newLoginRequest(QUrl)),
87             m_ui, SLOT(startLoginProcess(QUrl)));
88     connect(m_ui, SIGNAL(updateCredentials(QUrl)),
89             m_facebookAuthenticator, SLOT(updateCredentials(QUrl)));
90     connect(m_facebookAuthenticator, SIGNAL(loginFailure()),
91             m_ui, SLOT(loginFailed()));
92
93      m_facebookAuthenticator->start();
94 }
95
96 SituareEngine::~SituareEngine()
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99     delete m_ui;
100 }
101
102 void SituareEngine::loginProcessCancelled()
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     m_ui->show();
107     m_ui->toggleProgressIndicator(false);
108     //ToDo: do something
109 }
110
111 void SituareEngine::error(const QString &error)
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114     qDebug() << error;
115     // ToDo: signal UI?
116 }
117
118 void SituareEngine::loginOk()
119 {
120     qDebug() << __PRETTY_FUNCTION__;
121
122     m_ui->show();
123     m_situareService->fetchLocations(); // request user locations
124
125     //Debug, use settings instead
126     enableGPS(true);
127     m_ui->autoCenteringToggled(true);
128 }
129
130 void SituareEngine::requestAddress()
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
135     m_situareService->reverseGeo(coordinates);
136 }
137
138 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
139 {
140     qDebug() << __PRETTY_FUNCTION__;
141
142     m_ui->toggleProgressIndicator(true);
143
144     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
145     m_situareService->updateLocation(coordinates, status, publish);
146 }
147
148 void SituareEngine::updateWasSuccessful()
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     m_situareService->fetchLocations();
153 }
154
155 void SituareEngine::refreshUserData()
156 {
157     qDebug() << __PRETTY_FUNCTION__;
158
159     m_ui->toggleProgressIndicator(true);
160
161     m_situareService->fetchLocations();
162 }
163
164 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
165 {
166     qDebug() << __PRETTY_FUNCTION__;
167
168     m_ui->toggleProgressIndicator(false);
169
170     emit userLocationReady(user);
171     emit friendsLocationsReady(friendsList);
172 }
173
174 void SituareEngine::enableGPS(bool enabled)
175 {
176     qDebug() << __PRETTY_FUNCTION__;
177
178     if (enabled)
179         m_gps->start();
180     else
181         m_gps->stop();
182 }