Fixed bug that causes application to ask for new login even if
[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
30 MainWindow::MainWindow(QWidget *parent)
31     : QMainWindow(parent)
32 {
33     qDebug() << __PRETTY_FUNCTION__;
34     m_loggedIn = false;
35     createViews();
36     setCentralWidget(m_situareViews);
37     createMenus();
38     setWindowTitle(tr("List view"));
39     this->hide();
40
41     m_facebookAuthenticator = new FacebookAuthentication(this);
42     connect(m_facebookAuthenticator, SIGNAL(credentialsReady()), this, SLOT(loginOK()));
43     connect(m_facebookAuthenticator, SIGNAL(userExit()), this, SLOT(loginScreenClosed()));
44     m_facebookAuthenticator->start();
45
46     m_networkManager = new QNetworkAccessManager;
47     m_situareService = new SituareService(this,m_networkManager);
48 }
49
50 MainWindow::~MainWindow()
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53     delete m_toListViewAct;
54     delete m_toMapViewAct;
55     delete m_situareViews;
56 }
57
58 void MainWindow::createMenus()
59 {
60     qDebug() << __PRETTY_FUNCTION__;
61     m_toListViewAct = new QAction(tr("List"), this);
62     m_toListViewAct->setObjectName(tr("List"));
63     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
64     m_toMapViewAct = new QAction(tr("Map"), this);
65     m_toMapViewAct->setObjectName(tr("Map"));
66     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
67     m_viewMenu = menuBar()->addMenu(tr("View"));
68     m_viewMenu->addAction(m_toListViewAct);
69     m_viewMenu->addAction(m_toMapViewAct);
70     m_viewMenu->setObjectName(tr("View Menu"));
71 }
72
73 void MainWindow::createViews()
74 {
75     qDebug() << __PRETTY_FUNCTION__;
76     m_situareViews = new QStackedWidget;
77     m_situareViews->addWidget(new ListViewScreen);
78     m_situareViews->addWidget(new MapViewScreen);
79 }
80
81 void MainWindow::toListView()
82 {
83     qDebug() << __PRETTY_FUNCTION__;
84     switchView(0);
85 }
86
87 void MainWindow::toMapView()
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90     switchView(1);
91 }
92
93 void MainWindow::switchView(int nextIndex)
94 {
95     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
96     if (nextIndex < 0 || nextIndex > 1) {
97         qDebug() << "Illegal parameter value in MainWindow::switchView";
98         return;
99     }
100     m_situareViews->setCurrentIndex(nextIndex);
101     switch (m_situareViews->currentIndex()) {
102         case 0:
103             setWindowTitle(tr("List view"));
104             break;
105         case 1:
106             setWindowTitle(tr("Map view"));
107             break;
108         default:
109             qDebug() << "Illegal switch value in MainWindow::switchView";
110             break;
111     }
112 }
113
114 void MainWindow::loginScreenClosed()
115 {
116     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
117     if (m_loggedIn) {
118         this->show();
119         return;
120     }
121     else {
122         this->close();
123     }
124 }
125
126 void MainWindow::loginOK()
127 {
128     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
129     m_loggedIn = true;
130     m_facebookAuthenticator->close();
131 }