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