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