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