Merge branch 'master' into fb_auth_window_removal
[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       Jussi Laitinen jussi.laitinen@ixonos.com
8
9    Situare is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as published by the Free Software Foundation.
12
13    Situare is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Situare; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
21    USA.
22 */
23
24 #include <QtGui>
25
26 #ifdef Q_WS_MAEMO_5
27 #include <QtMaemo5/QMaemo5InformationBox>
28 #endif // Q_WS_MAEMO_5
29
30 #include "mainwindow.h"
31 #include "mapviewscreen.h"
32 #include "settingsdialog.h"
33 #include "facebookservice/facebookauthentication.h"
34
35 MainWindow::MainWindow(QWidget *parent)
36     : QMainWindow(parent),
37     m_email(),
38     m_loginUrl(),
39     m_password(),
40     m_refresh(0),
41     m_webView(0)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     m_mapViewScreen = new MapViewScreen(this);
46     setCentralWidget(m_mapViewScreen);
47     createMenus();
48     setWindowTitle(tr("Situare"));
49
50     m_locationDialog = new UpdateLocationDialog(this);
51
52     connect(this, SIGNAL(reverseGeoReady(QString)),
53             m_locationDialog, SLOT(setAddress(QString)));
54     connect(m_locationDialog, SIGNAL(statusUpdate(QString,bool)), this,
55             SIGNAL(statusUpdate(QString,bool)));
56
57     connect(this, SIGNAL(userLocationReady(User*)),
58             m_mapViewScreen, SIGNAL(userLocationReady(User*)));
59     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
60             m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
61
62     connect(this, SIGNAL(enableAutoCentering(bool)),
63             m_mapViewScreen, SLOT(enableAutoCentering(bool)));
64     connect(this, SIGNAL(positionReceived(QPointF)),
65             m_mapViewScreen, SLOT(positionReceived(QPointF)));
66
67     this->toggleProgressIndicator(true);
68 }
69
70 MainWindow::~MainWindow()
71 {
72     qDebug() << __PRETTY_FUNCTION__;
73
74     if(m_webView)
75         delete m_webView;
76 }
77
78 void MainWindow::toggleProgressIndicator(bool value)
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81 #ifdef Q_WS_MAEMO_5
82     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
83 #else
84     Q_UNUSED(value);
85 #endif // Q_WS_MAEMO_5
86 }
87
88 void MainWindow::createMenus()
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     m_toSettingsAct = new QAction(tr("Settings"), this);
93     m_toSettingsAct->setObjectName(tr("Settings"));
94     connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
95     m_gpsToggleAct = new QAction(tr("GPS"), this);
96     m_gpsToggleAct->setCheckable(true);
97     m_gpsToggleAct->setChecked(true);
98     connect(m_gpsToggleAct, SIGNAL(toggled(bool)), this, SLOT(gpsActionToggled(bool)));
99     m_autoCenteringAct = new QAction(tr("Auto centering"), this);
100     m_autoCenteringAct->setCheckable(true);
101     m_autoCenteringAct->setChecked(true);
102     connect(m_autoCenteringAct, SIGNAL(toggled(bool)), this, SLOT(autoCenteringToggled(bool)));    
103         
104         m_viewMenu = menuBar()->addMenu(tr("Main"));
105
106     m_viewMenu->addAction(m_toSettingsAct);
107         m_viewMenu->addAction(m_gpsToggleAct);
108     m_viewMenu->addAction(m_autoCenteringAct);
109     m_viewMenu->setObjectName(tr("Menu"));
110 }
111
112 void MainWindow::openLocationUpdateDialog()
113 {
114     qDebug() << __PRETTY_FUNCTION__;
115
116     emit requestReverseGeo();
117     m_locationDialog->exec();
118 }
119
120 void MainWindow::openSettingsDialog()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123     SettingsDialog *dialog = new SettingsDialog(this);
124     dialog->show();
125 }
126
127 void MainWindow::gpsActionToggled(bool checked)
128 {
129     qDebug() << __PRETTY_FUNCTION__;
130
131     if (checked) {
132         emit enableGPS(true);
133         showMaemoInformationBox(tr("GPS enabled"));
134         m_autoCenteringAct->setVisible(true);
135     }
136     else {
137         emit enableGPS(false);
138         showMaemoInformationBox(tr("GPS disabled"));
139         m_autoCenteringAct->setVisible(false);
140
141     }
142 }
143
144 void MainWindow::gpsTimeout()
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     showMaemoInformationBox(tr("GPS timeout"));
149 }
150
151 void MainWindow::gpsError(const QString &message)
152 {
153     qDebug() << __PRETTY_FUNCTION__;
154
155     showMaemoInformationBox(message);
156 }
157
158 void MainWindow::autoCenteringToggled(bool checked)
159 {
160     qDebug() << __PRETTY_FUNCTION__ << " " << checked;
161
162     if (checked) {
163         emit enableAutoCentering(true);
164         showMaemoInformationBox(tr("Auto centering enabled"));
165     }
166     else {
167         emit enableAutoCentering(false);
168         showMaemoInformationBox(tr("Auto centering disabled"));
169     }
170 }
171
172 void MainWindow::showMaemoInformationBox(const QString &message)
173 {
174     qDebug() << __PRETTY_FUNCTION__;
175
176 #ifdef Q_WS_MAEMO_5
177     QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
178 #else
179     Q_UNUSED(message);
180 #endif
181 }
182
183 void MainWindow::startLoginProcess(const QUrl &url)
184 {
185     qDebug() << __PRETTY_FUNCTION__;
186
187     m_loginUrl = url;
188     m_webView = new QWebView;
189     m_loginDialog = new LoginDialog(this);
190
191     connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
192             this, SIGNAL(updateCredentials(QUrl)));
193     connect(m_webView, SIGNAL(loadFinished(bool)),
194             this, SLOT(loadDone(bool)));
195
196     connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
197             this, SLOT(loginDialogDone(QString,QString)));
198
199     m_webView->hide();
200     this->show();
201
202     if(m_loginDialog->exec() != QDialog::Accepted) {
203         // if login dialog was canceled we need to stop processing webview
204         // stop and disconnect m_webView;
205         m_webView->stop();
206         disconnect(m_webView, SIGNAL(loadFinished(bool)),
207                    this, SLOT(loadDone(bool)));
208         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
209                    this, SLOT(updateCredentials(const QUrl &)));
210
211         emit cancelLoginProcess();
212     }
213     else {
214         m_webView->load(m_loginUrl);
215         toggleProgressIndicator(true);
216         m_refresh = true;
217     }
218 }
219
220 void MainWindow::loginDialogDone(const QString &email, const QString &password)
221 {
222     qDebug() << __PRETTY_FUNCTION__;
223
224     m_email = email;
225     m_password = password;
226 }
227
228 void MainWindow::loginFailed()
229 {
230     qDebug() << __PRETTY_FUNCTION__;
231
232     m_email.clear();
233     m_password.clear();
234
235     toggleProgressIndicator(false);
236
237 #ifdef Q_WS_MAEMO_5
238     QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
239                                        QMaemo5InformationBox::NoTimeout);
240
241 #endif // Q_WS_MAEMO_5
242
243     if(m_loginDialog->exec() != QDialog::Accepted) {
244         // if login dialog was canceled we need to stop processing webview
245         // stop and disconnect m_webView;
246         m_webView->stop();
247         disconnect(m_webView, SIGNAL(loadFinished(bool)),
248                    this, SLOT(loadDone(bool)));
249         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
250                    this, SLOT(updateCredentials(const QUrl &)));
251
252         emit cancelLoginProcess();
253     }
254     else {
255         // re-load login page for webview
256         toggleProgressIndicator(true);
257         m_webView->load(m_loginUrl);
258     }
259 }
260
261 void MainWindow::loadDone(bool done)
262 {
263     qDebug() << __PRETTY_FUNCTION__;
264
265     // for the first time the login page is opened, we need to refresh it to get cookies working
266     if(m_refresh) {
267         m_webView->reload();
268         m_refresh = false;
269     }
270
271     if (done)
272     {
273         QWebFrame* frame = m_webView->page()->currentFrame();
274         if (frame!=NULL)
275         {
276             // set email box
277             QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
278
279             foreach (QWebElement element, emailCollection) {
280                 element.setAttribute("value", m_email.toAscii());
281             }
282             // set password box
283             QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
284             foreach (QWebElement element, passwordCollection) {
285                 element.setAttribute("value", m_password.toAscii());
286             }
287             // find connect button
288             QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
289             foreach (QWebElement element, buttonCollection)
290             {
291                 QPoint pos(element.geometry().center());
292
293                 // send a mouse click event to the web page
294                 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
295                                    Qt::NoModifier);
296                 QApplication::sendEvent(m_webView->page(), &event0);
297                 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
298                                    Qt::NoModifier);
299                 QApplication::sendEvent(m_webView->page(), &event1);
300             }
301         }
302     }
303 }