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 connect(this, SIGNAL(reverseGeoReady(QString)),
57 m_mapViewScreen, SIGNAL(reverseGeoReady(QString)));
58 connect(m_mapViewScreen, SIGNAL(statusUpdate(QString,bool)),
59 this, SIGNAL(statusUpdate(QString,bool)));
61 connect(this, SIGNAL(userLocationReady(User*)),
62 m_mapViewScreen, SIGNAL(userLocationReady(User*)));
63 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
64 m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
66 connect(this, SIGNAL(autoCentering(bool)),
67 m_mapViewScreen, SIGNAL(enableAutoCentering(bool)));
68 connect(this, SIGNAL(positionReceived(QPointF, qreal)),
69 m_mapViewScreen, SIGNAL(positionReceived(QPointF, qreal)));
70 connect(m_mapViewScreen, SIGNAL(mapLocationChanged()),
71 this, SLOT(mapLocationChanged()));
73 connect(this, SIGNAL(zoomInKeyPressed()),
74 m_mapViewScreen, SIGNAL(zoomInKeyPressed()));
75 connect(this, SIGNAL(zoomOutKeyPressed()),
76 m_mapViewScreen, SIGNAL(zoomOutKeyPressed()));
78 connect(this, SIGNAL(requestOwnLocation()),
79 m_mapViewScreen, SIGNAL(requestOwnLocation()));
80 connect(m_mapViewScreen, SIGNAL(ownLocation(QPointF)),
81 this, SIGNAL(ownLocation(QPointF)));
83 connect(m_mapViewScreen, SIGNAL(requestReverseGeo()),
84 this, SIGNAL(requestReverseGeo()));
86 connect(m_mapViewScreen, SIGNAL(refreshUserData()),
87 this, SIGNAL(refreshUserData()));
89 this->toggleProgressIndicator(true);
94 MainWindow::~MainWindow()
96 qDebug() << __PRETTY_FUNCTION__;
104 void MainWindow::toggleProgressIndicator(bool value)
106 qDebug() << __PRETTY_FUNCTION__;
108 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
111 #endif // Q_WS_MAEMO_5
114 void MainWindow::createMenus()
116 qDebug() << __PRETTY_FUNCTION__;
118 m_toSettingsAct = new QAction(tr("Settings"), this);
119 m_toSettingsAct->setObjectName(tr("Settings"));
120 connect(m_toSettingsAct, SIGNAL(triggered()),
121 this, SLOT(openSettingsDialog()));
122 m_gpsToggleAct = new QAction(tr("GPS"), this);
123 m_gpsToggleAct->setCheckable(true);
124 m_gpsToggleAct->setChecked(true);
125 connect(m_gpsToggleAct, SIGNAL(toggled(bool)),
126 this, SLOT(gpsToggled(bool)));
127 connect(m_gpsToggleAct, SIGNAL(toggled(bool)),
128 m_mapViewScreen, SLOT(setOwnLocationCrosshairVisibility(bool)));
129 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
130 m_autoCenteringAct->setCheckable(true);
131 m_autoCenteringAct->setChecked(true);
132 connect(m_autoCenteringAct, SIGNAL(toggled(bool)),
133 this, SLOT(autoCenteringToggled(bool)));
134 connect(this, SIGNAL(enableGPS(bool)),
135 m_mapViewScreen, SIGNAL(gpsEnabled(bool)));
137 m_viewMenu = menuBar()->addMenu(tr("Main"));
139 m_viewMenu->addAction(m_toSettingsAct);
140 m_viewMenu->addAction(m_gpsToggleAct);
141 m_viewMenu->addAction(m_autoCenteringAct);
142 m_viewMenu->setObjectName(tr("Menu"));
145 void MainWindow::openSettingsDialog()
147 qDebug() << __PRETTY_FUNCTION__;
148 SettingsDialog *dialog = new SettingsDialog(this);
152 void MainWindow::gpsToggled(bool checked)
154 qDebug() << __PRETTY_FUNCTION__;
157 emit enableGPS(true);
159 emit enableGPS(false);
162 void MainWindow::setGPSButton(bool enabled)
164 qDebug() << __PRETTY_FUNCTION__;
167 showMaemoInformationBox(tr("GPS enabled"));
168 m_gpsToggleAct->setChecked(true);
169 m_autoCenteringAct->setVisible(true);
172 showMaemoInformationBox(tr("GPS disabled"));
173 m_gpsToggleAct->setChecked(false);
174 m_autoCenteringAct->setVisible(false);
178 void MainWindow::setAutoCenteringButton(bool enabled)
181 showMaemoInformationBox(tr("Auto centering enabled"));
182 m_autoCenteringAct->setChecked(true);
185 showMaemoInformationBox(tr("Auto centering disabled"));
186 m_autoCenteringAct->setChecked(false);
190 void MainWindow::gpsTimeout()
192 qDebug() << __PRETTY_FUNCTION__;
194 showMaemoInformationBox(tr("GPS timeout"));
197 void MainWindow::gpsError(const QString &message)
199 qDebug() << __PRETTY_FUNCTION__;
201 showMaemoInformationBox(message);
204 void MainWindow::mapLocationChanged()
206 qDebug() << __PRETTY_FUNCTION__;
208 emit enableAutoCentering(false);
211 void MainWindow::autoCenteringToggled(bool checked)
213 qDebug() << __PRETTY_FUNCTION__ << checked;
216 emit enableAutoCentering(true);
218 emit enableAutoCentering(false);
221 void MainWindow::autoCenteringEnabled(bool enabled)
223 qDebug() << __PRETTY_FUNCTION__ << enabled;
225 emit autoCentering(enabled);
228 void MainWindow::showMaemoInformationBox(const QString &message)
230 qDebug() << __PRETTY_FUNCTION__;
233 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
239 void MainWindow::startLoginProcess(const QUrl &url)
241 qDebug() << __PRETTY_FUNCTION__;
244 m_webView = new QWebView;
245 m_loginDialog = new LoginDialog(this);
247 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
248 this, SIGNAL(updateCredentials(QUrl)));
249 connect(m_webView, SIGNAL(loadFinished(bool)),
250 this, SLOT(loadDone(bool)));
252 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
253 this, SLOT(loginDialogDone(QString,QString)));
257 emit fetchUsernameFromSettings();
259 if(!m_email.isEmpty()) {
260 m_loginDialog->setEmailField(m_email);
263 if(m_loginDialog->exec() != QDialog::Accepted) {
264 // if login dialog was canceled we need to stop processing webview
265 // stop and disconnect m_webView;
267 disconnect(m_webView, SIGNAL(loadFinished(bool)),
268 this, SLOT(loadDone(bool)));
269 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
270 this, SLOT(updateCredentials(const QUrl &)));
272 emit cancelLoginProcess();
275 m_webView->load(m_loginUrl);
276 toggleProgressIndicator(true);
281 void MainWindow::loginDialogDone(const QString &email, const QString &password)
283 qDebug() << __PRETTY_FUNCTION__;
286 m_password = password;
289 void MainWindow::loginFailed()
291 qDebug() << __PRETTY_FUNCTION__;
296 toggleProgressIndicator(false);
299 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
300 QMaemo5InformationBox::NoTimeout);
302 #endif // Q_WS_MAEMO_5
304 if(!m_email.isEmpty()) {
305 m_loginDialog->setEmailField(m_email);
308 if(m_loginDialog->exec() != QDialog::Accepted) {
309 // if login dialog was canceled we need to stop processing webview
310 // stop and disconnect m_webView;
312 disconnect(m_webView, SIGNAL(loadFinished(bool)),
313 this, SLOT(loadDone(bool)));
314 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
315 this, SLOT(updateCredentials(const QUrl &)));
317 emit cancelLoginProcess();
320 // re-load login page for webview
321 toggleProgressIndicator(true);
322 m_webView->load(m_loginUrl);
326 void MainWindow::loadDone(bool done)
328 qDebug() << __PRETTY_FUNCTION__;
330 // for the first time the login page is opened, we need to refresh it to get cookies working
338 QWebFrame* frame = m_webView->page()->currentFrame();
342 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
344 foreach (QWebElement element, emailCollection) {
345 element.setAttribute("value", m_email.toAscii());
348 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
349 foreach (QWebElement element, passwordCollection) {
350 element.setAttribute("value", m_password.toAscii());
352 // find connect button
353 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
354 foreach (QWebElement element, buttonCollection)
356 QPoint pos(element.geometry().center());
358 // send a mouse click event to the web page
359 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
361 QApplication::sendEvent(m_webView->page(), &event0);
362 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
364 QApplication::sendEvent(m_webView->page(), &event1);
370 void MainWindow::grabZoomKeys(bool grab)
372 qDebug() << __PRETTY_FUNCTION__;
374 // Can't grab keys unless we have a window id
378 unsigned long val = (grab) ? 1 : 0;
379 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
383 XChangeProperty (QX11Info::display(),
389 reinterpret_cast<unsigned char *>(&val),
393 #endif // Q_WS_MAEMO_5
396 void MainWindow::keyPressEvent(QKeyEvent* event)
398 qDebug() << __PRETTY_FUNCTION__;
400 switch (event->key()) {
403 emit zoomInKeyPressed();
408 emit zoomOutKeyPressed();
411 QWidget::keyPressEvent(event);
414 const QString MainWindow::username()
416 qDebug() << __PRETTY_FUNCTION__;
420 void MainWindow::setUsername(const QString &username)
422 qDebug() << __PRETTY_FUNCTION__;