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