Integrated gps to map view.
[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
45     connect(m_ui, SIGNAL(requestReverseGeo()), this, SLOT(requestAddress()));
46     connect(m_situareService, SIGNAL(reverseGeoReady(QString)), m_ui, SIGNAL(reverseGeoReady(QString)));
47     connect(m_ui, SIGNAL(statusUpdate(QString,bool)), this, SLOT(requestUpdateLocation(QString,bool)));
48     connect(m_situareService, SIGNAL(userDataChanged(User*,QList<User*>&)),
49             this, SLOT(userDataChanged(User*,QList<User*>&)));
50     connect(this, SIGNAL(userLocationReady(User*)), m_ui, SIGNAL(userLocationReady(User*)));
51     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)), m_ui, SIGNAL(friendsLocationsReady(QList<User*>&)));
52         connect(m_situareService, SIGNAL(error(QString)), this, SLOT(error(QString)));
53     connect(m_situareService, SIGNAL(updateWasSuccessful()), this, SLOT(updateWasSuccessful()));
54
55     connect(m_ui, SIGNAL(refreshUserData()), this, SLOT(refreshUserData()));
56
57     connect(m_gps, SIGNAL(position(QPointF)),
58             m_ui, SIGNAL(positionReceived(QPointF)));
59
60      m_facebookAuthenticator->start();
61 }
62
63 SituareEngine::~SituareEngine()
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66     delete m_ui;
67     delete m_facebookAuthenticator;
68 }
69
70 void SituareEngine::error(const QString &error)
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73     qDebug() << error;
74     // ToDo: signal UI?
75 }
76
77 void SituareEngine::loginOk()
78 {
79     qDebug() << __PRETTY_FUNCTION__;
80     m_facebookAuthenticator->hide();
81     m_ui->show();
82     m_situareService->fetchLocations(); // request user locations
83     m_gps->start();
84 }
85
86 void SituareEngine::requestAddress()
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89
90     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
91     m_situareService->reverseGeo(coordinates);
92 }
93
94 void SituareEngine::requestUpdateLocation(const QString &status, bool publish)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     m_ui->toggleProgressIndicator(true);
99
100     QPointF coordinates(65.0109, 25.5092); // this will be get from somewhere, map etc...
101     m_situareService->updateLocation(coordinates, status, publish);
102 }
103
104 void SituareEngine::updateWasSuccessful()
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107
108     m_situareService->fetchLocations();
109 }
110
111 void SituareEngine::refreshUserData()
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     m_ui->toggleProgressIndicator(true);
116
117     m_situareService->fetchLocations();
118 }
119
120 void SituareEngine::userDataChanged(User *user, QList<User *> &friendsList)
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     m_ui->toggleProgressIndicator(false);
125
126     emit userLocationReady(user);
127     emit friendsLocationsReady(friendsList);
128 }