added base class for own and friend locations. added new class for
[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(FacebookCredentials)), 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     connect(this, SIGNAL(userLocationReady(User*)),
59             m_mapViewScreen, SLOT(userLocationReady(User*)));
60     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
61             m_mapViewScreen, SLOT(friendsLocationsReady(QList<User*>&)));
62 }
63
64 MainWindow::~MainWindow()
65 {
66     qDebug() << __PRETTY_FUNCTION__;
67     delete m_toListViewAct;
68     delete m_toMapViewAct;
69     delete m_toSettingsAct;
70     delete m_situareViews;
71 }
72
73 void MainWindow::createMenus()
74 {
75     qDebug() << __PRETTY_FUNCTION__;
76     m_toListViewAct = new QAction(tr("List"), this);
77     m_toListViewAct->setObjectName(tr("List"));
78     connect(m_toListViewAct, SIGNAL(triggered()), this, SLOT(toListView()));
79     m_toMapViewAct = new QAction(tr("Map"), this);
80     m_toMapViewAct->setObjectName(tr("Map"));
81     connect(m_toMapViewAct, SIGNAL(triggered()), this, SLOT(toMapView()));
82     m_toSettingsAct = new QAction(tr("Settings"), this);
83     m_toSettingsAct->setObjectName(tr("Settings"));
84     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
85     m_viewMenu = menuBar()->addMenu(tr("View"));
86     m_viewMenu->addAction(m_toListViewAct);
87     m_viewMenu->addAction(m_toMapViewAct);
88     m_viewMenu->addAction(m_toSettingsAct);
89     m_viewMenu->setObjectName(tr("View Menu"));
90 }
91
92 void MainWindow::createViews()
93 {
94     qDebug() << __PRETTY_FUNCTION__;
95     m_listViewScreen = new ListViewScreen(this);
96     m_mapViewScreen = new MapViewScreen(this);
97
98     m_situareViews = new QStackedWidget;
99     m_situareViews->addWidget(m_listViewScreen);
100     m_situareViews->addWidget(m_mapViewScreen);
101 }
102
103 void MainWindow::toListView()
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106     switchView(0);
107 }
108
109 void MainWindow::toMapView()
110 {
111     qDebug() << __PRETTY_FUNCTION__;
112     switchView(1);
113 }
114
115 void MainWindow::switchView(int nextIndex)
116 {
117     qDebug() << __PRETTY_FUNCTION__ << ":" << nextIndex;
118     if (nextIndex < 0 || nextIndex > 1) {
119         qDebug() << "Illegal parameter value in MainWindow::switchView";
120         return;
121     }
122     m_situareViews->setCurrentIndex(nextIndex);
123     switch (m_situareViews->currentIndex()) {
124         case 0:
125             setWindowTitle(tr("List view"));
126             break;
127         case 1:
128             setWindowTitle(tr("Map view"));
129             break;
130         default:
131             qDebug() << "Illegal switch value in MainWindow::switchView";
132             break;
133     }
134 }
135
136 void MainWindow::openLocationUpdateDialog()
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     emit requestReverseGeo();
141     m_locationDialog->exec();
142 }
143
144 void MainWindow::openSettingsDialog()
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147     SettingsDialog *dialog = new SettingsDialog(this);
148     dialog->show();
149 }
150
151 void MainWindow::loginScreenClosed()
152 {
153     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
154     if (m_loggedIn) {
155         this->show();
156         return;
157     }
158     else {
159         this->close();
160     }
161 }
162
163 void MainWindow::loginOK()
164 {
165     qDebug() << __PRETTY_FUNCTION__ << m_loggedIn;
166     m_loggedIn = true;
167     //m_facebookAuthenticator->close();
168 }
169
170 //void MainWindow::userLocationReady(User &user)
171 //{
172 //    emit userLocationReady(user);
173 //}
174 //
175 //void MainWindow::friendsLocationsReady(QList<User *>friendsList)
176 //{
177 //    emit friendsLocationsReady(friendsList);
178 //}
179