6fd9bbb7e5e78c42487998c55feaad57c6f5abf4
[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 "situarecommon.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, SLOT(enableAutoCentering(bool)));
70     connect(this, SIGNAL(positionReceived(QPointF, qreal)),
71             m_mapViewScreen, SLOT(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(gpsActionToggled(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         
124         m_viewMenu = menuBar()->addMenu(tr("Main"));
125
126     m_viewMenu->addAction(m_toSettingsAct);
127         m_viewMenu->addAction(m_gpsToggleAct);
128     m_viewMenu->addAction(m_autoCenteringAct);
129     m_viewMenu->setObjectName(tr("Menu"));
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::gpsToggled(bool checked)
148 {
149     qDebug() << __PRETTY_FUNCTION__;
150
151     if (checked)
152         emit enableGPS(true);
153     else
154         emit enableGPS(false);
155 }
156
157 void MainWindow::setGPSButton(bool enabled)
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     if (enabled) {
162         showMaemoInformationBox(tr("GPS enabled"));
163         m_gpsToggleAct->setChecked(true);
164         m_autoCenteringAct->setVisible(true);
165     }
166     else {
167         showMaemoInformationBox(tr("GPS disabled"));
168         m_gpsToggleAct->setChecked(false);
169         m_autoCenteringAct->setVisible(false);
170     }
171 }
172
173 void MainWindow::setAutoCenteringButton(bool enabled)
174 {
175     if (enabled) {
176         showMaemoInformationBox(tr("Auto centering enabled"));
177         m_autoCenteringAct->setChecked(true);
178     }
179     else {
180         showMaemoInformationBox(tr("Auto centering disabled"));
181         m_autoCenteringAct->setChecked(false);
182     }
183 }
184
185 void MainWindow::gpsTimeout()
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     showMaemoInformationBox(tr("GPS timeout"));
190 }
191
192 void MainWindow::gpsError(const QString &message)
193 {
194     qDebug() << __PRETTY_FUNCTION__;
195
196     showMaemoInformationBox(message);
197 }
198
199 void MainWindow::mapLocationChanged()
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     emit enableAutoCentering(false);
204 }
205
206 void MainWindow::autoCenteringToggled(bool checked)
207 {
208     qDebug() << __PRETTY_FUNCTION__ << checked;
209
210     if (checked)
211         emit enableAutoCentering(true);
212     else
213         emit enableAutoCentering(false);
214 }
215
216 void MainWindow::autoCenteringEnabled(bool enabled)
217 {
218     qDebug() << __PRETTY_FUNCTION__ << enabled;
219
220     emit autoCentering(enabled);
221 }
222
223 void MainWindow::showMaemoInformationBox(const QString &message)
224 {
225     qDebug() << __PRETTY_FUNCTION__;
226
227 #ifdef Q_WS_MAEMO_5
228     QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
229 #else
230     Q_UNUSED(message);
231 #endif
232 }
233
234 void MainWindow::startLoginProcess(const QUrl &url)
235 {
236     qDebug() << __PRETTY_FUNCTION__;
237
238     m_loginUrl = url;
239     m_webView = new QWebView;
240     m_loginDialog = new LoginDialog(this);
241
242     connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
243             this, SIGNAL(updateCredentials(QUrl)));
244     connect(m_webView, SIGNAL(loadFinished(bool)),
245             this, SLOT(loadDone(bool)));
246
247     connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
248             this, SLOT(loginDialogDone(QString,QString)));
249
250     m_webView->hide();
251
252     if(m_loginDialog->exec() != QDialog::Accepted) {
253         // if login dialog was canceled we need to stop processing webview
254         // stop and disconnect m_webView;
255         m_webView->stop();
256         disconnect(m_webView, SIGNAL(loadFinished(bool)),
257                    this, SLOT(loadDone(bool)));
258         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
259                    this, SLOT(updateCredentials(const QUrl &)));
260
261         emit cancelLoginProcess();
262     }
263     else {
264         m_webView->load(m_loginUrl);
265         toggleProgressIndicator(true);
266         m_refresh = true;
267     }
268 }
269
270 void MainWindow::loginDialogDone(const QString &email, const QString &password)
271 {
272     qDebug() << __PRETTY_FUNCTION__;
273
274     m_email = email;
275     m_password = password;
276 }
277
278 void MainWindow::loginFailed()
279 {
280     qDebug() << __PRETTY_FUNCTION__;
281
282     m_email.clear();
283     m_password.clear();
284
285     toggleProgressIndicator(false);
286
287 #ifdef Q_WS_MAEMO_5
288     QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
289                                        QMaemo5InformationBox::NoTimeout);
290
291 #endif // Q_WS_MAEMO_5
292
293     if(m_loginDialog->exec() != QDialog::Accepted) {
294         // if login dialog was canceled we need to stop processing webview
295         // stop and disconnect m_webView;
296         m_webView->stop();
297         disconnect(m_webView, SIGNAL(loadFinished(bool)),
298                    this, SLOT(loadDone(bool)));
299         disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
300                    this, SLOT(updateCredentials(const QUrl &)));
301
302         emit cancelLoginProcess();
303     }
304     else {
305         // re-load login page for webview
306         toggleProgressIndicator(true);
307         m_webView->load(m_loginUrl);
308     }
309 }
310
311 void MainWindow::loadDone(bool done)
312 {
313     qDebug() << __PRETTY_FUNCTION__;
314
315     // for the first time the login page is opened, we need to refresh it to get cookies working
316     if(m_refresh) {
317         m_webView->reload();
318         m_refresh = false;
319     }
320
321     if (done)
322     {
323         QWebFrame* frame = m_webView->page()->currentFrame();
324         if (frame!=NULL)
325         {
326             // set email box
327             QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
328
329             foreach (QWebElement element, emailCollection) {
330                 element.setAttribute("value", m_email.toAscii());
331             }
332             // set password box
333             QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
334             foreach (QWebElement element, passwordCollection) {
335                 element.setAttribute("value", m_password.toAscii());
336             }
337             // find connect button
338             QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
339             foreach (QWebElement element, buttonCollection)
340             {
341                 QPoint pos(element.geometry().center());
342
343                 // send a mouse click event to the web page
344                 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
345                                    Qt::NoModifier);
346                 QApplication::sendEvent(m_webView->page(), &event0);
347                 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
348                                    Qt::NoModifier);
349                 QApplication::sendEvent(m_webView->page(), &event1);
350             }
351         }
352     }
353 }
354
355 void MainWindow::grabZoomKeys(bool grab)
356 {
357     qDebug() << __PRETTY_FUNCTION__;
358 #ifdef Q_WS_MAEMO_5
359     // Can't grab keys unless we have a window id
360     if (!winId())
361         return;
362
363     unsigned long val = (grab) ? 1 : 0;
364     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
365     if (!atom)
366         return;
367
368     XChangeProperty (QX11Info::display(),
369                      winId(),
370                      atom,
371                      XA_INTEGER,
372                      32,
373                      PropModeReplace,
374                      reinterpret_cast<unsigned char *>(&val),
375                      1);
376 #endif // Q_WS_MAEMO_5
377 }
378
379 void MainWindow::keyPressEvent(QKeyEvent* event)
380 {
381     qDebug() << __PRETTY_FUNCTION__;
382
383     switch (event->key()) {
384     case Qt::Key_F7:
385         event->accept();
386         emit zoomInKeyPressed();
387         break;
388
389     case Qt::Key_F8:
390         event->accept();
391         emit zoomOutKeyPressed();
392         break;
393     }
394     QWidget::keyPressEvent(event);
395 }