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
8 Sami Rämö - sami.ramo@ixonos.com
10 Situare is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 version 2 as published by the Free Software Foundation.
14 Situare is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Situare; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 #include <QtMaemo5/QMaemo5InformationBox>
29 #endif // Q_WS_MAEMO_5
31 #include "mainwindow.h"
32 #include "settingsdialog.h"
33 #include "facebookservice/facebookauthentication.h"
36 #include "friendlistpanel.h"
37 #include "map/mapview.h"
38 #include "panelsidebar.h"
39 #include "userpanel.h"
40 #include "zoombuttonpanel.h"
42 #include <QtGui/QX11Info>
44 #include <X11/Xatom.h>
46 // values for setting screen size in desktop matching N900 screen size
47 const int N900_APP_WIDTH = 800;
48 const int N900_APP_HEIGHT = 449;
50 MainWindow::MainWindow(QWidget *parent)
51 : QMainWindow(parent),
52 m_drawOwnLocationCrosshair(false),
59 qDebug() << __PRETTY_FUNCTION__;
64 QHBoxLayout *layout = new QHBoxLayout;
65 layout->addWidget(m_mapView);
67 setCentralWidget(new QWidget());
68 centralWidget()->setLayout(layout);
70 buildFriendListPanel();
74 setWindowTitle(tr("Situare"));
76 m_locationDialog = new UpdateLocationDialog(this);
78 // set stacking order of widgets
79 m_zoomButtonPanel->stackUnder(m_userPanel);
80 m_osmLicense->stackUnder(m_zoomButtonPanel);
81 m_ownLocationCrosshair->stackUnder(m_osmLicense);
82 m_mapView->stackUnder(m_ownLocationCrosshair);
84 this->toggleProgressIndicator(true);
88 // set screen size in desktop matching N900 screen size
89 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
92 MainWindow::~MainWindow()
94 qDebug() << __PRETTY_FUNCTION__;
102 void MainWindow::buildFriendListPanel()
104 qDebug() << __PRETTY_FUNCTION__;
106 m_friendsListPanel = new FriendListPanel(this);
107 PanelSideBar *friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
109 m_friendsListPanel->stackUnder(friendsListPanelSidebar);
111 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
112 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
114 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
115 this, SIGNAL(findFriend(QPointF)));
117 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
118 m_friendsListPanel, SLOT(reDrawFriendsPanel(int, int)));
120 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
121 friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
124 void MainWindow::buildManualLocationCrosshair()
126 qDebug() << __PRETTY_FUNCTION__;
128 m_ownLocationCrosshair = new QLabel(this);
129 QPixmap crosshairImage(":/res/images/sight.png");
130 m_ownLocationCrosshair->setPixmap(crosshairImage);
131 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
133 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
134 this, SLOT(drawOwnLocationCrosshair(int, int)));
137 void MainWindow::buildMap()
139 qDebug() << __PRETTY_FUNCTION__;
141 m_mapView = new MapView(this);
143 buildZoomButtonPanel();
145 m_ownLocationCrosshair = 0;
147 buildManualLocationCrosshair();
149 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
150 this, SIGNAL(mapViewScrolled(QPoint)));
152 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
153 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
155 connect(m_mapView, SIGNAL(viewResized(QSize)),
156 this, SIGNAL(mapViewResized(QSize)));
158 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
159 this, SLOT(setViewPortSize(int, int)));
161 connect(this, SIGNAL(zoomLevelChanged(int)),
162 m_mapView, SLOT(setZoomLevel(int)));
164 connect(m_mapView, SIGNAL(viewZoomFinished()),
165 this, SIGNAL(viewZoomFinished()));
168 void MainWindow::buildOsmLicense()
170 qDebug() << __PRETTY_FUNCTION__;
172 m_osmLicense = new QLabel(this);
173 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
174 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
175 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
176 m_osmLicense->setFont(QFont("Nokia Sans", 9));
177 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
178 m_osmLicense->fontMetrics().height());
180 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
181 this, SLOT(drawOsmLicense(int, int)));
184 void MainWindow::buildUserInfoPanel()
186 qDebug() << __PRETTY_FUNCTION__;
188 m_userPanel = new UserInfoPanel(this);
189 PanelSideBar *userPanelSidebar = new PanelSideBar(this, LEFT);
191 userPanelSidebar->stackUnder(m_friendsListPanel);
192 m_userPanel->stackUnder(userPanelSidebar);
194 connect(this, SIGNAL(userLocationReady(User*)),
195 m_userPanel, SLOT(userDataReceived(User*)));
197 connect(m_userPanel, SIGNAL(requestReverseGeo()),
198 this, SIGNAL(requestReverseGeo()));
200 connect(this, SIGNAL(reverseGeoReady(QString)),
201 m_userPanel, SIGNAL(reverseGeoReady(QString)));
203 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
204 this, SIGNAL(statusUpdate(QString,bool)));
206 connect(m_userPanel, SIGNAL(refreshUserData()),
207 this, SIGNAL(refreshUserData()));
209 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
210 m_userPanel, SLOT(reDrawUserPanel(int, int)));
213 void MainWindow::buildZoomButtonPanel()
215 qDebug() << __PRETTY_FUNCTION__;
217 m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
218 ZOOM_BUTTON_PANEL_POSITION_Y);
220 connect(m_zoomButtonPanel->m_zoomInButton, SIGNAL(clicked()),
221 this, SIGNAL(zoomIn()));
223 connect(m_zoomButtonPanel->m_zoomOutButton, SIGNAL(clicked()),
224 this, SIGNAL(zoomOut()));
226 connect(this, SIGNAL(zoomLevelChanged(int)),
227 m_zoomButtonPanel, SLOT(resetButtons()));
229 connect(this, SIGNAL(maxZoomLevelReached()),
230 m_zoomButtonPanel, SLOT(disableZoomInButton()));
232 connect(this, SIGNAL(minZoomLevelReached()),
233 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
236 void MainWindow::createMenus()
238 qDebug() << __PRETTY_FUNCTION__;
241 m_toSettingsAct = new QAction(tr("Settings"), this);
242 m_toSettingsAct->setObjectName(tr("Settings"));
243 connect(m_toSettingsAct, SIGNAL(triggered()),
244 this, SLOT(openSettingsDialog()));
247 m_gpsToggleAct = new QAction(tr("GPS"), this);
248 m_gpsToggleAct->setCheckable(true);
250 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
251 this, SIGNAL(gpsTriggered(bool)));
253 // automatic centering
254 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
255 m_autoCenteringAct->setCheckable(true);
256 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
257 this, SIGNAL(autoCenteringTriggered(bool)));
259 // build the actual menu
260 m_viewMenu = menuBar()->addMenu(tr("Main"));
261 m_viewMenu->addAction(m_toSettingsAct);
262 m_viewMenu->addAction(m_gpsToggleAct);
263 m_viewMenu->addAction(m_autoCenteringAct);
264 m_viewMenu->setObjectName(tr("Menu"));
267 void MainWindow::drawOsmLicense(int width, int height)
269 qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
271 m_osmLicense->move(width - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
272 height - m_osmLicense->fontMetrics().height());
275 void MainWindow::drawOwnLocationCrosshair(int width, int height)
277 qDebug() << __PRETTY_FUNCTION__;
279 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
280 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
281 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
285 void MainWindow::gpsError(const QString &message)
287 qDebug() << __PRETTY_FUNCTION__;
289 showMaemoInformationBox(message);
292 void MainWindow::gpsTimeout()
294 qDebug() << __PRETTY_FUNCTION__;
296 showMaemoInformationBox(tr("GPS timeout"));
299 void MainWindow::grabZoomKeys(bool grab)
301 qDebug() << __PRETTY_FUNCTION__;
304 // Can't grab keys unless we have a window id
308 unsigned long val = (grab) ? 1 : 0;
309 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
313 XChangeProperty (QX11Info::display(),
319 reinterpret_cast<unsigned char *>(&val),
323 #endif // Q_WS_MAEMO_5
326 void MainWindow::keyPressEvent(QKeyEvent* event)
328 qDebug() << __PRETTY_FUNCTION__;
330 switch (event->key()) {
341 QWidget::keyPressEvent(event);
344 void MainWindow::loadDone(bool done)
346 qDebug() << __PRETTY_FUNCTION__;
348 // for the first time the login page is opened, we need to refresh it to get cookies working
356 QWebFrame* frame = m_webView->page()->currentFrame();
360 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
362 foreach (QWebElement element, emailCollection) {
363 element.setAttribute("value", m_email.toAscii());
366 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
367 foreach (QWebElement element, passwordCollection) {
368 element.setAttribute("value", m_password.toAscii());
370 // find connect button
371 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
372 foreach (QWebElement element, buttonCollection)
374 QPoint pos(element.geometry().center());
376 // send a mouse click event to the web page
377 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
379 QApplication::sendEvent(m_webView->page(), &event0);
380 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
382 QApplication::sendEvent(m_webView->page(), &event1);
388 void MainWindow::loginDialogDone(const QString &email, const QString &password)
390 qDebug() << __PRETTY_FUNCTION__;
393 m_password = password;
396 void MainWindow::loginFailed()
398 qDebug() << __PRETTY_FUNCTION__;
403 toggleProgressIndicator(false);
406 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
407 QMaemo5InformationBox::NoTimeout);
409 #endif // Q_WS_MAEMO_5
411 if(m_loginDialog->exec() != QDialog::Accepted) {
412 // if login dialog was canceled we need to stop processing webview
413 // stop and disconnect m_webView;
415 disconnect(m_webView, SIGNAL(loadFinished(bool)),
416 this, SLOT(loadDone(bool)));
417 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
418 this, SLOT(updateCredentials(const QUrl &)));
420 emit cancelLoginProcess();
423 // re-load login page for webview
424 toggleProgressIndicator(true);
425 m_webView->load(m_loginUrl);
429 void MainWindow::openSettingsDialog()
431 qDebug() << __PRETTY_FUNCTION__;
433 SettingsDialog *dialog = new SettingsDialog(this);
437 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
439 qDebug() << __PRETTY_FUNCTION__;
441 m_autoCenteringAct->setChecked(enabled);
444 void MainWindow::setGPSButtonEnabled(bool enabled)
446 qDebug() << __PRETTY_FUNCTION__;
448 m_gpsToggleAct->setChecked(enabled);
451 setOwnLocationCrosshairVisibility(false);
453 setOwnLocationCrosshairVisibility(true);
455 m_autoCenteringAct->setVisible(enabled);
458 void MainWindow::setMapViewScene(QGraphicsScene *scene)
460 qDebug() << __PRETTY_FUNCTION__;
462 m_mapView->setScene(scene);
465 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
467 qDebug() << __PRETTY_FUNCTION__;
470 m_ownLocationCrosshair->show();
471 m_drawOwnLocationCrosshair = true;
472 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
475 m_ownLocationCrosshair->hide();
476 m_drawOwnLocationCrosshair = false;
480 void MainWindow::setViewPortSize(int width, int height)
482 qDebug() << __PRETTY_FUNCTION__;
484 m_viewPortWidth = width;
485 m_viewPortHeight = height;
488 void MainWindow::showMaemoInformationBox(const QString &message)
490 qDebug() << __PRETTY_FUNCTION__;
493 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
499 void MainWindow::startLoginProcess(const QUrl &url)
501 qDebug() << __PRETTY_FUNCTION__;
504 m_webView = new QWebView;
505 m_loginDialog = new LoginDialog(this);
507 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
508 this, SIGNAL(updateCredentials(QUrl)));
509 connect(m_webView, SIGNAL(loadFinished(bool)),
510 this, SLOT(loadDone(bool)));
512 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
513 this, SLOT(loginDialogDone(QString,QString)));
517 if(m_loginDialog->exec() != QDialog::Accepted) {
518 // if login dialog was canceled we need to stop processing webview
519 // stop and disconnect m_webView;
521 disconnect(m_webView, SIGNAL(loadFinished(bool)),
522 this, SLOT(loadDone(bool)));
523 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
524 this, SLOT(updateCredentials(const QUrl &)));
526 emit cancelLoginProcess();
529 m_webView->load(m_loginUrl);
530 toggleProgressIndicator(true);
535 void MainWindow::toggleProgressIndicator(bool value)
537 qDebug() << __PRETTY_FUNCTION__;
540 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
543 #endif // Q_WS_MAEMO_5