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"
34 #include "situarecommon.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)), this,
61 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()), this, SLOT(mapLocationChanged()));
74 connect(this, SIGNAL(zoomInKeyPressed()),
75 m_mapViewScreen, SIGNAL(zoomInKeyPressed()));
76 connect(this, SIGNAL(zoomOutKeyPressed()),
77 m_mapViewScreen, SIGNAL(zoomOutKeyPressed()));
79 this->toggleProgressIndicator(true);
84 MainWindow::~MainWindow()
86 qDebug() << __PRETTY_FUNCTION__;
94 void MainWindow::toggleProgressIndicator(bool value)
96 qDebug() << __PRETTY_FUNCTION__;
98 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
101 #endif // Q_WS_MAEMO_5
104 void MainWindow::createMenus()
106 qDebug() << __PRETTY_FUNCTION__;
108 m_toSettingsAct = new QAction(tr("Settings"), this);
109 m_toSettingsAct->setObjectName(tr("Settings"));
110 connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
111 m_gpsToggleAct = new QAction(tr("GPS"), this);
112 m_gpsToggleAct->setCheckable(true);
113 m_gpsToggleAct->setChecked(true);
114 connect(m_gpsToggleAct, SIGNAL(toggled(bool)), this, SLOT(gpsToggled(bool)));
115 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
116 m_autoCenteringAct->setCheckable(true);
117 m_autoCenteringAct->setChecked(true);
118 connect(m_autoCenteringAct, SIGNAL(toggled(bool)), this, SLOT(autoCenteringToggled(bool)));
120 m_viewMenu = menuBar()->addMenu(tr("Main"));
122 m_viewMenu->addAction(m_toSettingsAct);
123 m_viewMenu->addAction(m_gpsToggleAct);
124 m_viewMenu->addAction(m_autoCenteringAct);
125 m_viewMenu->setObjectName(tr("Menu"));
128 void MainWindow::openLocationUpdateDialog()
130 qDebug() << __PRETTY_FUNCTION__;
132 emit requestReverseGeo();
133 m_locationDialog->exec();
136 void MainWindow::openSettingsDialog()
138 qDebug() << __PRETTY_FUNCTION__;
139 SettingsDialog *dialog = new SettingsDialog(this);
143 void MainWindow::gpsToggled(bool checked)
145 qDebug() << __PRETTY_FUNCTION__;
148 emit enableGPS(true);
150 emit enableGPS(false);
153 void MainWindow::setGPSButton(bool enabled)
155 qDebug() << __PRETTY_FUNCTION__;
158 showMaemoInformationBox(tr("GPS enabled"));
159 m_gpsToggleAct->setChecked(true);
160 m_autoCenteringAct->setVisible(true);
163 showMaemoInformationBox(tr("GPS disabled"));
164 m_gpsToggleAct->setChecked(false);
165 m_autoCenteringAct->setVisible(false);
169 void MainWindow::setAutoCenteringButton(bool enabled)
172 showMaemoInformationBox(tr("Auto centering enabled"));
173 m_autoCenteringAct->setChecked(true);
176 showMaemoInformationBox(tr("Auto centering disabled"));
177 m_autoCenteringAct->setChecked(false);
181 void MainWindow::gpsTimeout()
183 qDebug() << __PRETTY_FUNCTION__;
185 showMaemoInformationBox(tr("GPS timeout"));
188 void MainWindow::gpsError(const QString &message)
190 qDebug() << __PRETTY_FUNCTION__;
192 showMaemoInformationBox(message);
195 void MainWindow::mapLocationChanged()
197 qDebug() << __PRETTY_FUNCTION__;
199 emit enableAutoCentering(false);
202 void MainWindow::autoCenteringToggled(bool checked)
204 qDebug() << __PRETTY_FUNCTION__ << checked;
207 emit enableAutoCentering(true);
209 emit enableAutoCentering(false);
212 void MainWindow::autoCenteringEnabled(bool enabled)
214 qDebug() << __PRETTY_FUNCTION__ << enabled;
216 emit autoCentering(enabled);
219 void MainWindow::showMaemoInformationBox(const QString &message)
221 qDebug() << __PRETTY_FUNCTION__;
224 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
230 void MainWindow::startLoginProcess(const QUrl &url)
232 qDebug() << __PRETTY_FUNCTION__;
235 m_webView = new QWebView;
236 m_loginDialog = new LoginDialog(this);
238 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
239 this, SIGNAL(updateCredentials(QUrl)));
240 connect(m_webView, SIGNAL(loadFinished(bool)),
241 this, SLOT(loadDone(bool)));
243 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
244 this, SLOT(loginDialogDone(QString,QString)));
248 if(m_loginDialog->exec() != QDialog::Accepted) {
249 // if login dialog was canceled we need to stop processing webview
250 // stop and disconnect m_webView;
252 disconnect(m_webView, SIGNAL(loadFinished(bool)),
253 this, SLOT(loadDone(bool)));
254 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
255 this, SLOT(updateCredentials(const QUrl &)));
257 emit cancelLoginProcess();
260 m_webView->load(m_loginUrl);
261 toggleProgressIndicator(true);
266 void MainWindow::loginDialogDone(const QString &email, const QString &password)
268 qDebug() << __PRETTY_FUNCTION__;
271 m_password = password;
274 void MainWindow::loginFailed()
276 qDebug() << __PRETTY_FUNCTION__;
281 toggleProgressIndicator(false);
284 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
285 QMaemo5InformationBox::NoTimeout);
287 #endif // Q_WS_MAEMO_5
289 if(m_loginDialog->exec() != QDialog::Accepted) {
290 // if login dialog was canceled we need to stop processing webview
291 // stop and disconnect m_webView;
293 disconnect(m_webView, SIGNAL(loadFinished(bool)),
294 this, SLOT(loadDone(bool)));
295 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
296 this, SLOT(updateCredentials(const QUrl &)));
298 emit cancelLoginProcess();
301 // re-load login page for webview
302 toggleProgressIndicator(true);
303 m_webView->load(m_loginUrl);
307 void MainWindow::loadDone(bool done)
309 qDebug() << __PRETTY_FUNCTION__;
311 // for the first time the login page is opened, we need to refresh it to get cookies working
319 QWebFrame* frame = m_webView->page()->currentFrame();
323 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
325 foreach (QWebElement element, emailCollection) {
326 element.setAttribute("value", m_email.toAscii());
329 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
330 foreach (QWebElement element, passwordCollection) {
331 element.setAttribute("value", m_password.toAscii());
333 // find connect button
334 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
335 foreach (QWebElement element, buttonCollection)
337 QPoint pos(element.geometry().center());
339 // send a mouse click event to the web page
340 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
342 QApplication::sendEvent(m_webView->page(), &event0);
343 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
345 QApplication::sendEvent(m_webView->page(), &event1);
351 void MainWindow::grabZoomKeys(bool grab)
353 qDebug() << __PRETTY_FUNCTION__;
355 // Can't grab keys unless we have a window id
359 unsigned long val = (grab) ? 1 : 0;
360 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
364 XChangeProperty (QX11Info::display(),
370 reinterpret_cast<unsigned char *>(&val),
374 #endif // Q_WS_MAEMO_5
377 void MainWindow::keyPressEvent(QKeyEvent* event)
379 qDebug() << __PRETTY_FUNCTION__;
381 switch (event->key()) {
384 emit zoomInKeyPressed();
389 emit zoomOutKeyPressed();
392 QWidget::keyPressEvent(event);