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, 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()));
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(gpsToggled(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 connect(this, SIGNAL(enableGPS(bool)),
124 m_mapViewScreen, SIGNAL(gpsEnabled(bool)));
126 m_viewMenu = menuBar()->addMenu(tr("Main"));
128 m_viewMenu->addAction(m_toSettingsAct);
129 m_viewMenu->addAction(m_gpsToggleAct);
130 m_viewMenu->addAction(m_autoCenteringAct);
131 m_viewMenu->setObjectName(tr("Menu"));
134 void MainWindow::openLocationUpdateDialog()
136 qDebug() << __PRETTY_FUNCTION__;
138 emit requestReverseGeo();
139 m_locationDialog->exec();
142 void MainWindow::openSettingsDialog()
144 qDebug() << __PRETTY_FUNCTION__;
145 SettingsDialog *dialog = new SettingsDialog(this);
149 void MainWindow::gpsToggled(bool checked)
151 qDebug() << __PRETTY_FUNCTION__;
154 emit enableGPS(true);
156 emit enableGPS(false);
159 void MainWindow::setGPSButton(bool enabled)
161 qDebug() << __PRETTY_FUNCTION__;
164 showMaemoInformationBox(tr("GPS enabled"));
165 m_gpsToggleAct->setChecked(true);
166 m_autoCenteringAct->setVisible(true);
169 showMaemoInformationBox(tr("GPS disabled"));
170 m_gpsToggleAct->setChecked(false);
171 m_autoCenteringAct->setVisible(false);
175 void MainWindow::setAutoCenteringButton(bool enabled)
178 showMaemoInformationBox(tr("Auto centering enabled"));
179 m_autoCenteringAct->setChecked(true);
182 showMaemoInformationBox(tr("Auto centering disabled"));
183 m_autoCenteringAct->setChecked(false);
187 void MainWindow::gpsTimeout()
189 qDebug() << __PRETTY_FUNCTION__;
191 showMaemoInformationBox(tr("GPS timeout"));
194 void MainWindow::gpsError(const QString &message)
196 qDebug() << __PRETTY_FUNCTION__;
198 showMaemoInformationBox(message);
201 void MainWindow::mapLocationChanged()
203 qDebug() << __PRETTY_FUNCTION__;
205 emit enableAutoCentering(false);
208 void MainWindow::autoCenteringToggled(bool checked)
210 qDebug() << __PRETTY_FUNCTION__ << checked;
213 emit enableAutoCentering(true);
215 emit enableAutoCentering(false);
218 void MainWindow::autoCenteringEnabled(bool enabled)
220 qDebug() << __PRETTY_FUNCTION__ << enabled;
222 emit autoCentering(enabled);
225 void MainWindow::showMaemoInformationBox(const QString &message)
227 qDebug() << __PRETTY_FUNCTION__;
230 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
236 void MainWindow::startLoginProcess(const QUrl &url)
238 qDebug() << __PRETTY_FUNCTION__;
241 m_webView = new QWebView;
242 m_loginDialog = new LoginDialog(this);
244 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
245 this, SIGNAL(updateCredentials(QUrl)));
246 connect(m_webView, SIGNAL(loadFinished(bool)),
247 this, SLOT(loadDone(bool)));
249 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
250 this, SLOT(loginDialogDone(QString,QString)));
254 if(m_loginDialog->exec() != QDialog::Accepted) {
255 // if login dialog was canceled we need to stop processing webview
256 // stop and disconnect m_webView;
258 disconnect(m_webView, SIGNAL(loadFinished(bool)),
259 this, SLOT(loadDone(bool)));
260 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
261 this, SLOT(updateCredentials(const QUrl &)));
263 emit cancelLoginProcess();
266 m_webView->load(m_loginUrl);
267 toggleProgressIndicator(true);
272 void MainWindow::loginDialogDone(const QString &email, const QString &password)
274 qDebug() << __PRETTY_FUNCTION__;
277 m_password = password;
280 void MainWindow::loginFailed()
282 qDebug() << __PRETTY_FUNCTION__;
287 toggleProgressIndicator(false);
290 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
291 QMaemo5InformationBox::NoTimeout);
293 #endif // Q_WS_MAEMO_5
295 if(m_loginDialog->exec() != QDialog::Accepted) {
296 // if login dialog was canceled we need to stop processing webview
297 // stop and disconnect m_webView;
299 disconnect(m_webView, SIGNAL(loadFinished(bool)),
300 this, SLOT(loadDone(bool)));
301 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
302 this, SLOT(updateCredentials(const QUrl &)));
304 emit cancelLoginProcess();
307 // re-load login page for webview
308 toggleProgressIndicator(true);
309 m_webView->load(m_loginUrl);
313 void MainWindow::loadDone(bool done)
315 qDebug() << __PRETTY_FUNCTION__;
317 // for the first time the login page is opened, we need to refresh it to get cookies working
325 QWebFrame* frame = m_webView->page()->currentFrame();
329 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
331 foreach (QWebElement element, emailCollection) {
332 element.setAttribute("value", m_email.toAscii());
335 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
336 foreach (QWebElement element, passwordCollection) {
337 element.setAttribute("value", m_password.toAscii());
339 // find connect button
340 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
341 foreach (QWebElement element, buttonCollection)
343 QPoint pos(element.geometry().center());
345 // send a mouse click event to the web page
346 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
348 QApplication::sendEvent(m_webView->page(), &event0);
349 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
351 QApplication::sendEvent(m_webView->page(), &event1);
357 void MainWindow::grabZoomKeys(bool grab)
359 qDebug() << __PRETTY_FUNCTION__;
361 // Can't grab keys unless we have a window id
365 unsigned long val = (grab) ? 1 : 0;
366 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
370 XChangeProperty (QX11Info::display(),
376 reinterpret_cast<unsigned char *>(&val),
380 #endif // Q_WS_MAEMO_5
383 void MainWindow::keyPressEvent(QKeyEvent* event)
385 qDebug() << __PRETTY_FUNCTION__;
387 switch (event->key()) {
390 emit zoomInKeyPressed();
395 emit zoomOutKeyPressed();
398 QWidget::keyPressEvent(event);