Commit description: Renamed engine instance
[situare] / src / ui / mainwindow.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6       Kaj Wallin - kaj.wallin@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include <QtGui>
24 #include "mainwindow.h"
25 #include "listviewscreen.h"
26 #include "mapviewscreen.h"
27 #include "facebookservice/facebookauthentication.h"
28 #include "situareservice/situareservice.h"
29 #include "engine/engine.h"
30
31 MainWindow::MainWindow(QWidget *parent)
32     : QMainWindow(parent)
33 {
34     qDebug() << __PRETTY_FUNCTION__;
35     m_loggedIn = false;
36     createViews();
37     setCentralWidget(m_situareViews);
38     createMenus();
39     setWindowTitle(tr("List view"));
40     this->hide();
41
42     m_facebookAuthenticator = new FacebookAuthentication(this);
43     connect(m_facebookAuthenticator, SIGNAL(credentialsReady()), this, SLOT(loginOK()));
44     connect(m_facebookAuthenticator, SIGNAL(userExit()), this, SLOT(loginScreenClosed()));
45     m_facebookAuthenticator->start();
46
47     m_networkManager = new QNetworkAccessManager;
48     m_situareService = new SituareService(this,m_networkManager);
49
50     QWidget *situareEngine = new SituareEngine(this);
51 }
52
53 MainWindow::~MainWindow()
54 {
55     qDebug() << __PRETTY_FUNCTION__;
56     delete m_toListViewAct;
57     delete m_toMapViewAct;
58     delete m_situareViews;
59 }
60
61 void MainWindow::createMenus()
62 {
63     qDebug() << __PRETTY_FUNCTION__;
64     m_toListViewAct = new QAction(tr("List"), this);
65     m_toListViewAct->setObjectName(tr("List"));
66     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
67     m_toMapViewAct = new QAction(tr("Map"), this);
68     m_toMapViewAct->setObjectName(tr("Map"));
69     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
70     m_viewMenu = menuBar()->addMenu(tr("View"));
71     m_viewMenu->addAction(m_toListViewAct);
72     m_viewMenu->addAction(m_toMapViewAct);
73     m_viewMenu->setObjectName(tr("View Menu"));
74 }
75
76 void MainWindow::createViews()
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79     m_situareViews = new QStackedWidget;
80     m_situareViews->addWidget(new ListViewScreen);
81     m_situareViews->addWidget(new MapViewScreen);
82 }
83
84 void MainWindow::toListView()
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87     switchView(0);
88 }
89
90 void MainWindow::toMapView()
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93     switchView(1);
94 }
95
96 void MainWindow::switchView(int nextIndex)
97 {
98     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
99     if (nextIndex < 0 || nextIndex > 1) {
100         qDebug() << "Illegal parameter value in MainWindow::switchView";
101         return;
102     }
103     m_situareViews->setCurrentIndex(nextIndex);
104     switch (m_situareViews->currentIndex()) {
105         case 0:
106             setWindowTitle(tr("List view"));
107             break;
108         case 1:
109             setWindowTitle(tr("Map view"));
110             break;
111         default:
112             qDebug() << "Illegal switch value in MainWindow::switchView";
113             break;
114     }
115 }
116
117 void MainWindow::loginScreenClosed()
118 {
119     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
120     if (m_loggedIn) {
121         this->show();
122         return;
123     }
124     else {
125         this->close();
126     }
127 }
128
129 void MainWindow::loginOK()
130 {
131     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
132     m_loggedIn = true;
133     m_facebookAuthenticator->close();
134 }