2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Henri Lampela - henri.lampela@ixonos.com
6 Kaj Wallin - kaj.wallin@ixonos.com
7 Jussi Laitinen jussi.laitinen@ixonos.com
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.
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.
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,
27 #include <QtMaemo5/QMaemo5InformationBox>
28 #endif // Q_WS_MAEMO_5
30 #include "mainwindow.h"
31 #include "mapviewscreen.h"
32 #include "settingsdialog.h"
33 #include "facebookservice/facebookauthentication.h"
36 #include <QtGui/QX11Info>
38 #include <X11/Xatom.h>
40 MainWindow::MainWindow(QWidget *parent)
41 : QMainWindow(parent),
48 qDebug() << __PRETTY_FUNCTION__;
50 m_mapViewScreen = new MapViewScreen(this);
51 setCentralWidget(m_mapViewScreen);
53 setWindowTitle(tr("Situare"));
56 m_locationDialog = new UpdateLocationDialog(this);
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)));
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*>&)));
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()));
75 connect(this, SIGNAL(zoomInKeyPressed()),
76 m_mapViewScreen, SIGNAL(zoomInKeyPressed()));
77 connect(this, SIGNAL(zoomOutKeyPressed()),
78 m_mapViewScreen, SIGNAL(zoomOutKeyPressed()));
80 connect(this, SIGNAL(requestOwnLocation()),
81 m_mapViewScreen, SIGNAL(requestOwnLocation()));
82 connect(m_mapViewScreen, SIGNAL(ownLocation(QPointF)),
83 this, SIGNAL(ownLocation(QPointF)));
85 this->toggleProgressIndicator(true);
90 MainWindow::~MainWindow()
92 qDebug() << __PRETTY_FUNCTION__;
100 void MainWindow::toggleProgressIndicator(bool value)
102 qDebug() << __PRETTY_FUNCTION__;
104 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
107 #endif // Q_WS_MAEMO_5
110 void MainWindow::createMenus()
112 qDebug() << __PRETTY_FUNCTION__;
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)));
131 m_viewMenu = menuBar()->addMenu(tr("Main"));
133 m_viewMenu->addAction(m_toSettingsAct);
134 m_viewMenu->addAction(m_gpsToggleAct);
135 m_viewMenu->addAction(m_autoCenteringAct);
136 m_viewMenu->setObjectName(tr("Menu"));
139 void MainWindow::openLocationUpdateDialog()
141 qDebug() << __PRETTY_FUNCTION__;
143 emit requestReverseGeo();
144 m_locationDialog->exec();
147 void MainWindow::openSettingsDialog()
149 qDebug() << __PRETTY_FUNCTION__;
150 SettingsDialog *dialog = new SettingsDialog(this);
154 void MainWindow::gpsToggled(bool checked)
156 qDebug() << __PRETTY_FUNCTION__;
159 emit enableGPS(true);
161 emit enableGPS(false);
164 void MainWindow::setGPSButton(bool enabled)
166 qDebug() << __PRETTY_FUNCTION__;
169 showMaemoInformationBox(tr("GPS enabled"));
170 m_gpsToggleAct->setChecked(true);
171 m_autoCenteringAct->setVisible(true);
174 showMaemoInformationBox(tr("GPS disabled"));
175 m_gpsToggleAct->setChecked(false);
176 m_autoCenteringAct->setVisible(false);
180 void MainWindow::setAutoCenteringButton(bool enabled)
183 showMaemoInformationBox(tr("Auto centering enabled"));
184 m_autoCenteringAct->setChecked(true);
187 showMaemoInformationBox(tr("Auto centering disabled"));
188 m_autoCenteringAct->setChecked(false);
192 void MainWindow::gpsTimeout()
194 qDebug() << __PRETTY_FUNCTION__;
196 showMaemoInformationBox(tr("GPS timeout"));
199 void MainWindow::gpsError(const QString &message)
201 qDebug() << __PRETTY_FUNCTION__;
203 showMaemoInformationBox(message);
206 void MainWindow::mapLocationChanged()
208 qDebug() << __PRETTY_FUNCTION__;
210 emit enableAutoCentering(false);
213 void MainWindow::autoCenteringToggled(bool checked)
215 qDebug() << __PRETTY_FUNCTION__ << checked;
218 emit enableAutoCentering(true);
220 emit enableAutoCentering(false);
223 void MainWindow::autoCenteringEnabled(bool enabled)
225 qDebug() << __PRETTY_FUNCTION__ << enabled;
227 emit autoCentering(enabled);
230 void MainWindow::showMaemoInformationBox(const QString &message)
232 qDebug() << __PRETTY_FUNCTION__;
235 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
241 void MainWindow::startLoginProcess(const QUrl &url)
243 qDebug() << __PRETTY_FUNCTION__;
246 m_webView = new QWebView;
247 m_loginDialog = new LoginDialog(this);
249 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
250 this, SIGNAL(updateCredentials(QUrl)));
251 connect(m_webView, SIGNAL(loadFinished(bool)),
252 this, SLOT(loadDone(bool)));
254 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
255 this, SLOT(loginDialogDone(QString,QString)));
259 if(m_loginDialog->exec() != QDialog::Accepted) {
260 // if login dialog was canceled we need to stop processing webview
261 // stop and disconnect m_webView;
263 disconnect(m_webView, SIGNAL(loadFinished(bool)),
264 this, SLOT(loadDone(bool)));
265 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
266 this, SLOT(updateCredentials(const QUrl &)));
268 emit cancelLoginProcess();
271 m_webView->load(m_loginUrl);
272 toggleProgressIndicator(true);
277 void MainWindow::loginDialogDone(const QString &email, const QString &password)
279 qDebug() << __PRETTY_FUNCTION__;
282 m_password = password;
285 void MainWindow::loginFailed()
287 qDebug() << __PRETTY_FUNCTION__;
292 toggleProgressIndicator(false);
295 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
296 QMaemo5InformationBox::NoTimeout);
298 #endif // Q_WS_MAEMO_5
300 if(m_loginDialog->exec() != QDialog::Accepted) {
301 // if login dialog was canceled we need to stop processing webview
302 // stop and disconnect m_webView;
304 disconnect(m_webView, SIGNAL(loadFinished(bool)),
305 this, SLOT(loadDone(bool)));
306 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
307 this, SLOT(updateCredentials(const QUrl &)));
309 emit cancelLoginProcess();
312 // re-load login page for webview
313 toggleProgressIndicator(true);
314 m_webView->load(m_loginUrl);
318 void MainWindow::loadDone(bool done)
320 qDebug() << __PRETTY_FUNCTION__;
322 // for the first time the login page is opened, we need to refresh it to get cookies working
330 QWebFrame* frame = m_webView->page()->currentFrame();
334 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
336 foreach (QWebElement element, emailCollection) {
337 element.setAttribute("value", m_email.toAscii());
340 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
341 foreach (QWebElement element, passwordCollection) {
342 element.setAttribute("value", m_password.toAscii());
344 // find connect button
345 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
346 foreach (QWebElement element, buttonCollection)
348 QPoint pos(element.geometry().center());
350 // send a mouse click event to the web page
351 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
353 QApplication::sendEvent(m_webView->page(), &event0);
354 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
356 QApplication::sendEvent(m_webView->page(), &event1);
362 void MainWindow::grabZoomKeys(bool grab)
364 qDebug() << __PRETTY_FUNCTION__;
366 // Can't grab keys unless we have a window id
370 unsigned long val = (grab) ? 1 : 0;
371 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
375 XChangeProperty (QX11Info::display(),
381 reinterpret_cast<unsigned char *>(&val),
385 #endif // Q_WS_MAEMO_5
388 void MainWindow::keyPressEvent(QKeyEvent* event)
390 qDebug() << __PRETTY_FUNCTION__;
392 switch (event->key()) {
395 emit zoomInKeyPressed();
400 emit zoomOutKeyPressed();
403 QWidget::keyPressEvent(event);