Merge branch 'master' of https://vcs.maemo.org/git/situare
[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 #include "common.h"
35
36 #include <QtGui/QX11Info>
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39
40 MainWindow::MainWindow(QWidget *parent)
41     : QMainWindow(parent),
42     m_email(),
43     m_loginUrl(),
44     m_password(),
45     m_refresh(0),
46     m_webView(0)
47 {
48     qDebug() << __PRETTY_FUNCTION__;
49
50     m_mapViewScreen = new MapViewScreen(this);
51     setCentralWidget(m_mapViewScreen);
52     createMenus();
53     setWindowTitle(tr("Situare"));
54         show();
55
56     m_locationDialog = new UpdateLocationDialog(this);
57
58     connect(this, SIGNAL(reverseGeoReady(QString)),
59             m_locationDialog, SLOT(setAddress(QString)));
60     connect(m_locationDialog, SIGNAL(statusUpdate(QString, bool)),
61             this, SIGNAL(statusUpdate(QString, bool)));
62
63     connect(this, SIGNAL(userLocationReady(User*)),
64             m_mapViewScreen, SIGNAL(userLocationReady(User*)));
65     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
66             m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
67
68         connect(this, SIGNAL(autoCentering(bool)),
69             m_mapViewScreen, SIGNAL(enableAutoCentering(bool)));
70     connect(this, SIGNAL(positionReceived(QPointF, qreal)),
71             m_mapViewScreen, SIGNAL(positionReceived(QPointF, qreal)));
72     connect(m_mapViewScreen, SIGNAL(mapLocationChanged()),
73             this, SLOT(mapLocationChanged()));
74
75     connect(this, SIGNAL(zoomInKeyPressed()),
76             m_mapViewScreen, SIGNAL(zoomInKeyPressed()));
77     connect(this, SIGNAL(zoomOutKeyPressed()),
78             m_mapViewScreen, SIGNAL(zoomOutKeyPressed()));
79
80     this->toggleProgressIndicator(true);
81
82     grabZoomKeys(true);
83 }
84
85 MainWindow::~MainWindow()
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     grabZoomKeys(false);
90
91     if(m_webView)
92         delete m_webView;
93 }
94
95 void MainWindow::toggleProgressIndicator(bool value)
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98 #ifdef Q_WS_MAEMO_5
99     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
100 #else
101     Q_UNUSED(value);
102 #endif // Q_WS_MAEMO_5
103 }
104
105 void MainWindow::createMenus()
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     m_toSettingsAct = new QAction(tr("Settings"), this);
110     m_toSettingsAct->setObjectName(tr("Settings"));
111     connect(m_toSettingsAct, SIGNAL(triggered()),
112         this, SLOT(openSettingsDialog()));
113     m_gpsToggleAct = new QAction(tr("GPS"), this);
114     m_gpsToggleAct->setCheckable(true);
115     m_gpsToggleAct->setChecked(true);
116     connect(m_gpsToggleAct, SIGNAL(toggled(bool)),
117         this, SLOT(gpsToggled(bool)));
118     m_autoCenteringAct = new QAction(tr("Auto centering"), this);
119     m_autoCenteringAct->setCheckable(true);
120     m_autoCenteringAct->setChecked(true);
121     connect(m_autoCenteringAct, SIGNAL(toggled(bool)),
122         this, SLOT(autoCenteringToggled(bool))); 
123     connect(this, SIGNAL(enableGPS(bool)),
124             m_mapViewScreen, SIGNAL(gpsEnabled(bool)));
125
126     m_viewMenu = menuBar()->addMenu(tr("Main"));
127
128     m_viewMenu->addAction(m_toSettingsAct);
129         m_viewMenu->addAction(m_gpsToggleAct);
130     m_viewMenu->addAction(m_autoCenteringAct);
131     m_viewMenu->setObjectName(tr("Menu"));
132 }
133
134 void MainWindow::openLocationUpdateDialog()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     emit requestReverseGeo();
139     m_locationDialog->exec();
140 }
141
142 void MainWindow::openSettingsDialog()
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145     SettingsDialog *dialog = new SettingsDialog(this);
146     dialog->show();
147 }
148
149 void MainWindow::gpsToggled(bool checked)
150 {
151     qDebug() << __PRETTY_FUNCTION__;
152
153     if (checked)
154         emit enableGPS(true);
155     else
156         emit enableGPS(false);
157 }
158
159 void MainWindow::setGPSButton(bool enabled)
160 {
161     qDebug() << __PRETTY_FUNCTION__;
162
163     if (enabled) {
164         showMaemoInformationBox(tr("GPS enabled"));
165         m_gpsToggleAct->setChecked(true);
166         m_autoCenteringAct->setVisible(true);
167     }
168     else {
169         showMaemoInformationBox(tr("GPS disabled"));
170         m_gpsToggleAct->setChecked(false);
171         m_autoCenteringAct->setVisible(false);
172     }
173 }
174
175 void MainWindow::setAutoCenteringButton(bool enabled)
176 {
177     if (enabled) {
178         showMaemoInformationBox(tr("Auto centering enabled"));
179         m_autoCenteringAct->setChecked(true);
180     }
181     else {
182         showMaemoInformationBox(tr("Auto centering disabled"));
183         m_autoCenteringAct->setChecked(false);
184     }
185 }
186
187 void MainWindow::gpsTimeout()
188 {
189     qDebug() << __PRETTY_FUNCTION__;
190
191     showMaemoInformationBox(tr("GPS timeout"));
192 }
193
194 void MainWindow::gpsError(const QString &message)
195 {
196     qDebug() << __PRETTY_FUNCTION__;
197
198     showMaemoInformationBox(message);
199 }
200
201 void MainWindow::mapLocationChanged()
202 {
203     qDebug() << __PRETTY_FUNCTION__;
204
205     emit enableAutoCentering(false);
206 }
207
208 void MainWindow::autoCenteringToggled(bool checked)
209 {
210     qDebug() << __PRETTY_FUNCTION__ << checked;
211
212     if (checked)
213         emit enableAutoCentering(true);
214     else
215         emit enableAutoCentering(false);
216 }
217
218 void MainWindow::autoCenteringEnabled(bool enabled)
219 {
220     qDebug() << __PRETTY_FUNCTION__ << enabled;
221
222     emit autoCentering(enabled);
223 }
224
225 void MainWindow::showMaemoInformationBox(const QString &message)
226 {
227     qDebug() << __PRETTY_FUNCTION__;
228
229 #ifdef Q_WS_MAEMO_5
230     QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
231 #else
232     Q_UNUSED(message);
233 #endif
234 }
235
236 void MainWindow::startLoginProcess(const QUrl &url)
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     m_loginUrl = url;
241     m_webView = new QWebView;
242     m_loginDialog = new LoginDialog(this);
243
244     connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
245             this, SIGNAL(updateCredentials(QUrl)));
246     connect(m_webView, SIGNAL(loadFinished(bool)),
247             this, SLOT(loadDone(bool)));
248
249     connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
250             this, SLOT(loginDialogDone(QString,QString)));
251
252     m_webView->hide();
253
254     if(m_loginDialog->exec() != QDialog::Accepted) {
255         // if login dialog was canceled we need to stop processing webview
256         // stop and disconnect m_webView;
257         m_webView->stop();
258         disconnect(m_webView, SIGNAL(loadFinished(bool)),
259                    this, SLOT(loadDone(bool)));
260         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
261                    this, SLOT(updateCredentials(const QUrl &)));
262
263         emit cancelLoginProcess();
264     }
265     else {
266         m_webView->load(m_loginUrl);
267         toggleProgressIndicator(true);
268         m_refresh = true;
269     }
270 }
271
272 void MainWindow::loginDialogDone(const QString &email, const QString &password)
273 {
274     qDebug() << __PRETTY_FUNCTION__;
275
276     m_email = email;
277     m_password = password;
278 }
279
280 void MainWindow::loginFailed()
281 {
282     qDebug() << __PRETTY_FUNCTION__;
283
284     m_email.clear();
285     m_password.clear();
286
287     toggleProgressIndicator(false);
288
289 #ifdef Q_WS_MAEMO_5
290     QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
291                                        QMaemo5InformationBox::NoTimeout);
292
293 #endif // Q_WS_MAEMO_5
294
295     if(m_loginDialog->exec() != QDialog::Accepted) {
296         // if login dialog was canceled we need to stop processing webview
297         // stop and disconnect m_webView;
298         m_webView->stop();
299         disconnect(m_webView, SIGNAL(loadFinished(bool)),
300                    this, SLOT(loadDone(bool)));
301         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
302                    this, SLOT(updateCredentials(const QUrl &)));
303
304         emit cancelLoginProcess();
305     }
306     else {
307         // re-load login page for webview
308         toggleProgressIndicator(true);
309         m_webView->load(m_loginUrl);
310     }
311 }
312
313 void MainWindow::loadDone(bool done)
314 {
315     qDebug() << __PRETTY_FUNCTION__;
316
317     // for the first time the login page is opened, we need to refresh it to get cookies working
318     if(m_refresh) {
319         m_webView->reload();
320         m_refresh = false;
321     }
322
323     if (done)
324     {
325         QWebFrame* frame = m_webView->page()->currentFrame();
326         if (frame!=NULL)
327         {
328             // set email box
329             QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
330
331             foreach (QWebElement element, emailCollection) {
332                 element.setAttribute("value", m_email.toAscii());
333             }
334             // set password box
335             QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
336             foreach (QWebElement element, passwordCollection) {
337                 element.setAttribute("value", m_password.toAscii());
338             }
339             // find connect button
340             QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
341             foreach (QWebElement element, buttonCollection)
342             {
343                 QPoint pos(element.geometry().center());
344
345                 // send a mouse click event to the web page
346                 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
347                                    Qt::NoModifier);
348                 QApplication::sendEvent(m_webView->page(), &event0);
349                 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
350                                    Qt::NoModifier);
351                 QApplication::sendEvent(m_webView->page(), &event1);
352             }
353         }
354     }
355 }
356
357 void MainWindow::grabZoomKeys(bool grab)
358 {
359     qDebug() << __PRETTY_FUNCTION__;
360 #ifdef Q_WS_MAEMO_5
361     // Can't grab keys unless we have a window id
362     if (!winId())
363         return;
364
365     unsigned long val = (grab) ? 1 : 0;
366     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
367     if (!atom)
368         return;
369
370     XChangeProperty (QX11Info::display(),
371                      winId(),
372                      atom,
373                      XA_INTEGER,
374                      32,
375                      PropModeReplace,
376                      reinterpret_cast<unsigned char *>(&val),
377                      1);
378 #else
379     Q_UNUSED(grab);
380 #endif // Q_WS_MAEMO_5
381 }
382
383 void MainWindow::keyPressEvent(QKeyEvent* event)
384 {
385     qDebug() << __PRETTY_FUNCTION__;
386
387     switch (event->key()) {
388     case Qt::Key_F7:
389         event->accept();
390         emit zoomInKeyPressed();
391         break;
392
393     case Qt::Key_F8:
394         event->accept();
395         emit zoomOutKeyPressed();
396         break;
397     }
398     QWidget::keyPressEvent(event);
399 }