e0c3e03880326daf8516daa786f81d5deae76a21
[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 "settingsdialog.h"
28 #include "facebookservice/facebookauthentication.h"
29 #include "situareservice/situareservice.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_locationDialog = new UpdateLocationDialog(this);
48     connect(m_listViewScreen->m_personalInfo,SIGNAL(launchMessageUpdate()),
49             this,SLOT(openLocationUpdateDialog()));
50
51     connect(this, SIGNAL(reverseGeoReady(QString)), m_locationDialog, SLOT(setAddress(QString)));
52     connect(m_locationDialog, SIGNAL(statusUpdate(QString,bool)), this,
53             SIGNAL(statusUpdate(QString,bool)));
54     
55     m_networkManager = new QNetworkAccessManager;
56     m_situareService = new SituareService(this,m_networkManager);
57 }
58
59 MainWindow::~MainWindow()
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62     delete m_toListViewAct;
63     delete m_toMapViewAct;
64     delete m_toSettingsAct;
65     delete m_situareViews;
66 }
67
68 void MainWindow::createMenus()
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71     m_toListViewAct = new QAction(tr("List"), this);
72     m_toListViewAct->setObjectName(tr("List"));
73     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
74     m_toMapViewAct = new QAction(tr("Map"), this);
75     m_toMapViewAct->setObjectName(tr("Map"));
76     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
77     m_toSettingsAct = new QAction(tr("Settings"), this);
78     m_toSettingsAct->setObjectName(tr("Settings"));
79     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
80     m_viewMenu = menuBar()->addMenu(tr("View"));
81     m_viewMenu->addAction(m_toListViewAct);
82     m_viewMenu->addAction(m_toMapViewAct);
83     m_viewMenu->addAction(m_toSettingsAct);
84     m_viewMenu->setObjectName(tr("View Menu"));
85 }
86
87 void MainWindow::createViews()
88 {
89     qDebug() << __PRETTY_FUNCTION__;
90     m_listViewScreen = new ListViewScreen(this);
91     m_mapViewScreen = new MapViewScreen(this);
92
93     m_situareViews = new QStackedWidget;
94     m_situareViews->addWidget(m_listViewScreen);
95     m_situareViews->addWidget(m_mapViewScreen);
96 }
97
98 void MainWindow::toListView()
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101     switchView(0);
102 }
103
104 void MainWindow::toMapView()
105 {
106     qDebug() << __PRETTY_FUNCTION__;
107     switchView(1);
108 }
109
110 void MainWindow::switchView(int nextIndex)
111 {
112     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
113     if (nextIndex < 0 || nextIndex > 1) {
114         qDebug() << "Illegal parameter value in MainWindow::switchView";
115         return;
116     }
117     m_situareViews->setCurrentIndex(nextIndex);
118     switch (m_situareViews->currentIndex()) {
119         case 0:
120             setWindowTitle(tr("List view"));
121             break;
122         case 1:
123             setWindowTitle(tr("Map view"));
124             break;
125         default:
126             qDebug() << "Illegal switch value in MainWindow::switchView";
127             break;
128     }
129 }
130
131 void MainWindow::openLocationUpdateDialog()
132 {
133     qDebug() << __PRETTY_FUNCTION__;
134
135     emit requestReverseGeo();
136     m_locationDialog->exec();
137 }
138
139 void MainWindow::openSettingsDialog()
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142     SettingsDialog *dialog = new SettingsDialog(this);
143     dialog->show();
144 }
145
146 void MainWindow::loginScreenClosed()
147 {
148     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
149     if (m_loggedIn) {
150         this->show();
151         return;
152     }
153     else {
154         this->close();
155     }
156 }
157
158 void MainWindow::loginOK()
159 {
160     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
161     m_loggedIn = true;
162     m_facebookAuthenticator->close();
163 }