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