Merge branch 'master' into gps
[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/gpsposition.h"
27
28 SituareEngine::SituareEngine(QMainWindow *parent)
29     : QObject(parent)
30 {
31     qDebug() << __PRETTY_FUNCTION__;
32     m_ui = new MainWindow;
33
34     m_situareService = new SituareService(this);
35
36     m_facebookAuthenticator = new FacebookAuthentication();
37
38     m_gps = new GPSPosition(this);
39
40     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
41             m_situareService, SLOT(credentialsReady(FacebookCredentials)));
42     connect(m_facebookAuthenticator, SIGNAL(credentialsReady(FacebookCredentials)),
43             this, SLOT(loginOk()));
44     connect(m_facebookAuthenticator, SIGNAL(quitSituare()),
45             this, SLOT(quitApplication()));
46
47     connect(m_ui, SIGNAL(requestReverseGeo()),
48             this, SLOT(requestAddress()));
49     connect(m_situareService, SIGNAL(reverseGeoReady(QString)),
50             m_ui, SIGNAL(reverseGeoReady(QString)));
51     connect(m_ui, SIGNAL(statusUpdate(QString,bool)),
52             this, SLOT(requestUpdateLocation(QString,bool)));
53     connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)),
54             this, SLOT(userDataChanged(User*,QList<User*>&)));
55     connect(this, SIGNAL(userLocationReady(User*)),
56             m_ui, SIGNAL(userLocationReady(User*)));
57     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
58             m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
59     connect(m_situareService, SIGNAL(error(QString)),
60             this, SLOT(error(QString)));
61     connect(m_situareService, SIGNAL(updateWasSuccessful()),
62             this, SLOT(updateWasSuccessful()));
63
64     connect(m_ui, SIGNAL(refreshUserData()),
65             this, SLOT(refreshUserData()));
66
67     connect(m_gps, SIGNAL(timeout()), m_ui, SLOT(gpsTimeout()));
68     connect(m_gps, SIGNAL(error(QString)), m_ui, SLOT(gpsError(QString)));
69     connect(m_ui, SIGNAL(enableGPS(bool)), this, SLOT(enableGPS(bool)));
70     connect(m_gps, SIGNAL(position(QPointF)),
71             m_ui, SIGNAL(positionReceived(QPointF)));
72
73      m_facebookAuthenticator->start();
74 }
75
76 SituareEngine::~SituareEngine()
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79     delete m_ui;
80 }
81
82 void SituareEngine::quitApplication()
83 {
84     qDebug() << __PRETTY_FUNCTION__;
85
86     m_facebookAuthenticator->setAttribute(Qt::WA_DeleteOnClose);
87     m_facebookAuthenticator->close();
88 }
89
90 void SituareEngine::error(const QString &error)
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93     qDebug() << error;
94     // ToDo: signal UI?
95 }
96
97 void SituareEngine::loginOk()
98 {
99     qDebug() << __PRETTY_FUNCTION__;
100
101     m_facebookAuthenticator->setAttribute(Qt::WA_DeleteOnClose);
102     m_facebookAuthenticator->close();
103     m_ui->show();
104     m_situareService->fetchLocations(); // request user locations
105     enableGPS(true);
106 }
107
108 void SituareEngine::requestAddress()
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
113     m_situareService->reverseGeo(coordinates);
114 }
115
116 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     m_ui->toggleProgressIndicator(true);
121
122     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
123     m_situareService->updateLocation(coordinates, status, publish);
124 }
125
126 void SituareEngine::updateWasSuccessful()
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     m_situareService->fetchLocations();
131 }
132
133 void SituareEngine::refreshUserData()
134 {
135     qDebug() << __PRETTY_FUNCTION__;
136
137     m_ui->toggleProgressIndicator(true);
138
139     m_situareService->fetchLocations();
140 }
141
142 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     m_ui->toggleProgressIndicator(false);
147
148     emit userLocationReady(user);
149     emit friendsLocationsReady(friendsList);
150 }
151
152 void SituareEngine::enableGPS(bool enabled)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     if (enabled)
157         m_gps->start();
158     else
159         m_gps->stop();
160 }