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)),
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 this->toggleProgressIndicator(true);
85 MainWindow::~MainWindow()
87 qDebug() << __PRETTY_FUNCTION__;
95 void MainWindow::toggleProgressIndicator(bool value)
97 qDebug() << __PRETTY_FUNCTION__;
99 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
102 #endif // Q_WS_MAEMO_5
105 void MainWindow::createMenus()
107 qDebug() << __PRETTY_FUNCTION__;
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)));
124 m_viewMenu = menuBar()->addMenu(tr("Main"));
126 m_viewMenu->addAction(m_toSettingsAct);
127 m_viewMenu->addAction(m_gpsToggleAct);
128 m_viewMenu->addAction(m_autoCenteringAct);
129 m_viewMenu->setObjectName(tr("Menu"));
132 void MainWindow::openLocationUpdateDialog()
134 qDebug() << __PRETTY_FUNCTION__;
136 emit requestReverseGeo();
137 m_locationDialog->exec();
140 void MainWindow::openSettingsDialog()
142 qDebug() << __PRETTY_FUNCTION__;
143 SettingsDialog *dialog = new SettingsDialog(this);
147 void MainWindow::gpsToggled(bool checked)
149 qDebug() << __PRETTY_FUNCTION__;
152 emit enableGPS(true);
154 emit enableGPS(false);
157 void MainWindow::setGPSButton(bool enabled)
159 qDebug() << __PRETTY_FUNCTION__;
162 showMaemoInformationBox(tr("GPS enabled"));
163 m_gpsToggleAct->setChecked(true);
164 m_autoCenteringAct->setVisible(true);
167 showMaemoInformationBox(tr("GPS disabled"));
168 m_gpsToggleAct->setChecked(false);
169 m_autoCenteringAct->setVisible(false);
173 void MainWindow::setAutoCenteringButton(bool enabled)
176 showMaemoInformationBox(tr("Auto centering enabled"));
177 m_autoCenteringAct->setChecked(true);
180 showMaemoInformationBox(tr("Auto centering disabled"));
181 m_autoCenteringAct->setChecked(false);
185 void MainWindow::gpsTimeout()
187 qDebug() << __PRETTY_FUNCTION__;
189 showMaemoInformationBox(tr("GPS timeout"));
192 void MainWindow::gpsError(const QString &message)
194 qDebug() << __PRETTY_FUNCTION__;
196 showMaemoInformationBox(message);
199 void MainWindow::mapLocationChanged()
201 qDebug() << __PRETTY_FUNCTION__;
203 emit enableAutoCentering(false);
206 void MainWindow::autoCenteringToggled(bool checked)
208 qDebug() << __PRETTY_FUNCTION__ << checked;
211 emit enableAutoCentering(true);
213 emit enableAutoCentering(false);
216 void MainWindow::autoCenteringEnabled(bool enabled)
218 qDebug() << __PRETTY_FUNCTION__ << enabled;
220 emit autoCentering(enabled);
223 void MainWindow::showMaemoInformationBox(const QString &message)
225 qDebug() << __PRETTY_FUNCTION__;
228 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
234 void MainWindow::startLoginProcess(const QUrl &url)
236 qDebug() << __PRETTY_FUNCTION__;
239 m_webView = new QWebView;
240 m_loginDialog = new LoginDialog(this);
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)));
247 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
248 this, SLOT(loginDialogDone(QString,QString)));
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;
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 &)));
261 emit cancelLoginProcess();
264 m_webView->load(m_loginUrl);
265 toggleProgressIndicator(true);
270 void MainWindow::loginDialogDone(const QString &email, const QString &password)
272 qDebug() << __PRETTY_FUNCTION__;
275 m_password = password;
278 void MainWindow::loginFailed()
280 qDebug() << __PRETTY_FUNCTION__;
285 toggleProgressIndicator(false);
288 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
289 QMaemo5InformationBox::NoTimeout);
291 #endif // Q_WS_MAEMO_5
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;
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 &)));
302 emit cancelLoginProcess();
305 // re-load login page for webview
306 toggleProgressIndicator(true);
307 m_webView->load(m_loginUrl);
311 void MainWindow::loadDone(bool done)
313 qDebug() << __PRETTY_FUNCTION__;
315 // for the first time the login page is opened, we need to refresh it to get cookies working
323 QWebFrame* frame = m_webView->page()->currentFrame();
327 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
329 foreach (QWebElement element, emailCollection) {
330 element.setAttribute("value", m_email.toAscii());
333 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
334 foreach (QWebElement element, passwordCollection) {
335 element.setAttribute("value", m_password.toAscii());
337 // find connect button
338 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
339 foreach (QWebElement element, buttonCollection)
341 QPoint pos(element.geometry().center());
343 // send a mouse click event to the web page
344 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
346 QApplication::sendEvent(m_webView->page(), &event0);
347 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
349 QApplication::sendEvent(m_webView->page(), &event1);
355 void MainWindow::grabZoomKeys(bool grab)
357 qDebug() << __PRETTY_FUNCTION__;
359 // Can't grab keys unless we have a window id
363 unsigned long val = (grab) ? 1 : 0;
364 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
368 XChangeProperty (QX11Info::display(),
374 reinterpret_cast<unsigned char *>(&val),
376 #endif // Q_WS_MAEMO_5
379 void MainWindow::keyPressEvent(QKeyEvent* event)
381 qDebug() << __PRETTY_FUNCTION__;
383 switch (event->key()) {
386 emit zoomInKeyPressed();
391 emit zoomOutKeyPressed();
394 QWidget::keyPressEvent(event);