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
9 Ville Tiensuu - ville.tiensuu@ixonos.com
11 Situare is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 version 2 as published by the Free Software Foundation.
15 Situare is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Situare; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
30 #include <QtMaemo5/QMaemo5InformationBox>
31 #endif // Q_WS_MAEMO_5
34 #include "facebookservice/facebookauthentication.h"
35 #include "friendlistpanel.h"
36 #include "logindialog.h"
37 #include "map/mapview.h"
38 #include "settingsdialog.h"
39 #include "userinfopanel.h"
40 #include "zoombuttonpanel.h"
42 #include "mainwindow.h"
44 #include <QtGui/QX11Info>
46 #include <X11/Xatom.h>
48 // values for setting screen size in desktop matching N900 screen size
49 const int N900_APP_WIDTH = 800;
50 const int N900_APP_HEIGHT = 449;
52 MainWindow::MainWindow(QWidget *parent)
53 : QMainWindow(parent),
54 m_drawOwnLocationCrosshair(false),
62 qDebug() << __PRETTY_FUNCTION__;
67 QHBoxLayout *layout = new QHBoxLayout;
68 layout->addWidget(m_mapView);
70 setCentralWidget(new QWidget());
71 centralWidget()->setLayout(layout);
73 buildFriendListPanel();
76 m_settingsDialog = new SettingsDialog(this);
77 m_settingsDialog->hide();
78 connect(m_settingsDialog, SIGNAL(automaticLocationUpdateIntervalSet(int)),
79 this, SIGNAL(automaticLocationUpdateIntervalSet(int)));
82 setWindowTitle(tr("Situare"));
84 // set stacking order of widgets
85 m_zoomButtonPanel->stackUnder(m_userPanel);
86 m_osmLicense->stackUnder(m_zoomButtonPanel);
87 m_ownLocationCrosshair->stackUnder(m_osmLicense);
88 m_mapView->stackUnder(m_ownLocationCrosshair);
90 this->toggleProgressIndicator(true);
94 // set screen size in desktop matching N900 screen size
95 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
98 MainWindow::~MainWindow()
100 qDebug() << __PRETTY_FUNCTION__;
108 void MainWindow::buildFriendListPanel()
110 qDebug() << __PRETTY_FUNCTION__;
112 m_friendsListPanel = new FriendListPanel(this);
113 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
115 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
117 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
118 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
120 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
121 this, SIGNAL(findFriend(QPointF)));
123 connect(m_mapView, SIGNAL(viewResized(QSize)),
124 m_friendsListPanel, SLOT(screenResized(QSize)));
126 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
127 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
129 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
130 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
133 void MainWindow::buildManualLocationCrosshair()
135 qDebug() << __PRETTY_FUNCTION__;
137 m_ownLocationCrosshair = new QLabel(this);
138 QPixmap crosshairImage(":/res/images/sight.png");
139 m_ownLocationCrosshair->setPixmap(crosshairImage);
140 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
141 m_ownLocationCrosshair->hide();
142 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
144 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
145 this, SLOT(drawOwnLocationCrosshair(int, int)));
148 void MainWindow::buildMap()
150 qDebug() << __PRETTY_FUNCTION__;
152 m_mapView = new MapView(this);
154 buildZoomButtonPanel();
156 m_ownLocationCrosshair = 0;
158 buildManualLocationCrosshair();
160 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
161 this, SIGNAL(mapViewScrolled(QPoint)));
163 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
164 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
166 connect(m_mapView, SIGNAL(viewResized(QSize)),
167 this, SIGNAL(mapViewResized(QSize)));
169 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
170 this, SLOT(setViewPortSize(int, int)));
172 connect(this, SIGNAL(zoomLevelChanged(int)),
173 m_mapView, SLOT(setZoomLevel(int)));
175 connect(m_mapView, SIGNAL(viewZoomFinished()),
176 this, SIGNAL(viewZoomFinished()));
179 void MainWindow::buildOsmLicense()
181 qWarning() << __PRETTY_FUNCTION__;
183 m_osmLicense = new QLabel(this);
184 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
185 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
186 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
187 m_osmLicense->setFont(QFont("Nokia Sans", 9));
188 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
189 m_osmLicense->fontMetrics().height());
191 connect(m_mapView, SIGNAL(viewResized(QSize)),
192 this, SLOT(drawOsmLicense(QSize)));
195 void MainWindow::buildUserInfoPanel()
197 qDebug() << __PRETTY_FUNCTION__;
199 m_userPanel = new UserInfoPanel(this);
200 m_userPanelSidebar = new PanelSideBar(this, LEFT);
202 m_userPanelSidebar->stackUnder(m_friendsListPanel);
203 m_userPanel->stackUnder(m_userPanelSidebar);
205 connect(this, SIGNAL(userLocationReady(User*)),
206 m_userPanel, SLOT(userDataReceived(User*)));
208 connect(m_userPanel, SIGNAL(requestReverseGeo()),
209 this, SIGNAL(requestReverseGeo()));
211 connect(this, SIGNAL(reverseGeoReady(QString)),
212 m_userPanel, SIGNAL(reverseGeoReady(QString)));
214 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
215 this, SIGNAL(statusUpdate(QString,bool)));
217 connect(m_userPanel, SIGNAL(refreshUserData()),
218 this, SIGNAL(refreshUserData()));
220 connect(m_mapView, SIGNAL(viewResized(QSize)),
221 m_userPanel, SLOT(screenResized(QSize)));
224 void MainWindow::buildZoomButtonPanel()
226 qDebug() << __PRETTY_FUNCTION__;
228 m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
229 ZOOM_BUTTON_PANEL_POSITION_Y);
231 connect(m_zoomButtonPanel->m_zoomInButton, SIGNAL(clicked()),
232 this, SIGNAL(zoomIn()));
234 connect(m_zoomButtonPanel->m_zoomOutButton, SIGNAL(clicked()),
235 this, SIGNAL(zoomOut()));
237 connect(this, SIGNAL(zoomLevelChanged(int)),
238 m_zoomButtonPanel, SLOT(resetButtons()));
240 connect(this, SIGNAL(maxZoomLevelReached()),
241 m_zoomButtonPanel, SLOT(disableZoomInButton()));
243 connect(this, SIGNAL(minZoomLevelReached()),
244 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
247 void MainWindow::createMenus()
249 qDebug() << __PRETTY_FUNCTION__;
252 m_loginAct = new QAction(tr("Login"), this);
253 connect(m_loginAct, SIGNAL(triggered()),
254 this, SIGNAL(loginActionPressed()));
257 m_toSettingsAct = new QAction(tr("Settings"), this);
258 connect(m_toSettingsAct, SIGNAL(triggered()),
259 this, SLOT(openSettingsDialog()));
262 m_gpsToggleAct = new QAction(tr("GPS"), this);
263 m_gpsToggleAct->setCheckable(true);
265 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
266 this, SIGNAL(gpsTriggered(bool)));
268 // automatic centering
269 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
270 m_autoCenteringAct->setCheckable(true);
271 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
272 this, SIGNAL(autoCenteringTriggered(bool)));
274 // build the actual menu
275 m_viewMenu = menuBar()->addMenu(tr("Main"));
276 m_viewMenu->addAction(m_loginAct);
277 m_viewMenu->addAction(m_toSettingsAct);
278 m_viewMenu->addAction(m_gpsToggleAct);
279 m_viewMenu->addAction(m_autoCenteringAct);
280 m_viewMenu->setObjectName(tr("Menu"));
283 void MainWindow::drawOsmLicense(const QSize &size)
285 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
287 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
288 size.height() - m_osmLicense->fontMetrics().height());
292 void MainWindow::drawOwnLocationCrosshair(int width, int height)
294 qDebug() << __PRETTY_FUNCTION__;
296 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
297 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
298 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
302 void MainWindow::gpsError(const QString &message)
304 qDebug() << __PRETTY_FUNCTION__;
306 showMaemoInformationBox(message);
309 void MainWindow::gpsTimeout()
311 qDebug() << __PRETTY_FUNCTION__;
313 showMaemoInformationBox(tr("GPS timeout"));
316 void MainWindow::grabZoomKeys(bool grab)
318 qDebug() << __PRETTY_FUNCTION__;
321 // Can't grab keys unless we have a window id
325 unsigned long val = (grab) ? 1 : 0;
326 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
330 XChangeProperty (QX11Info::display(),
336 reinterpret_cast<unsigned char *>(&val),
340 #endif // Q_WS_MAEMO_5
343 void MainWindow::keyPressEvent(QKeyEvent* event)
345 qDebug() << __PRETTY_FUNCTION__;
347 switch (event->key()) {
358 QWidget::keyPressEvent(event);
361 void MainWindow::loadDone(bool done)
363 qDebug() << __PRETTY_FUNCTION__;
365 // for the first time the login page is opened, we need to refresh it to get cookies working
373 QWebFrame* frame = m_webView->page()->currentFrame();
377 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
379 foreach (QWebElement element, emailCollection) {
380 element.setAttribute("value", m_email.toAscii());
383 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
384 foreach (QWebElement element, passwordCollection) {
385 element.setAttribute("value", m_password.toAscii());
387 // find connect button
388 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
389 foreach (QWebElement element, buttonCollection)
391 QPoint pos(element.geometry().center());
393 // send a mouse click event to the web page
394 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
396 QApplication::sendEvent(m_webView->page(), &event0);
397 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
399 QApplication::sendEvent(m_webView->page(), &event1);
405 void MainWindow::loggedIn(bool logged)
407 qDebug() << __PRETTY_FUNCTION__;
412 m_loginAct->setText(tr("Logout"));
415 m_loginAct->setText(tr("Login"));
417 showPanels(m_loggedIn);
420 void MainWindow::loginDialogDone(const QString &email, const QString &password)
422 qDebug() << __PRETTY_FUNCTION__;
425 m_password = password;
428 void MainWindow::loginFailed()
430 qDebug() << __PRETTY_FUNCTION__;
435 toggleProgressIndicator(false);
438 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
439 QMaemo5InformationBox::NoTimeout);
441 #endif // Q_WS_MAEMO_5
443 if(!m_email.isEmpty()) {
444 m_loginDialog->setEmailField(m_email);
447 if(m_loginDialog->exec() != QDialog::Accepted) {
448 // if login dialog was canceled we need to stop processing webview
449 // stop and disconnect m_webView;
451 disconnect(m_webView, SIGNAL(loadFinished(bool)),
452 this, SLOT(loadDone(bool)));
453 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
454 this, SLOT(updateCredentials(const QUrl &)));
456 emit cancelLoginProcess();
459 // re-load login page for webview
460 toggleProgressIndicator(true);
461 m_webView->load(m_loginUrl);
465 void MainWindow::openSettingsDialog()
467 qDebug() << __PRETTY_FUNCTION__;
469 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
471 m_settingsDialog->show();
474 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
476 qDebug() << __PRETTY_FUNCTION__;
478 m_autoCenteringAct->setChecked(enabled);
481 void MainWindow::setGPSButtonEnabled(bool enabled)
483 qDebug() << __PRETTY_FUNCTION__;
485 m_gpsToggleAct->setChecked(enabled);
486 setOwnLocationCrosshairVisibility(!enabled);
487 m_autoCenteringAct->setVisible(enabled);
490 void MainWindow::setMapViewScene(QGraphicsScene *scene)
492 qDebug() << __PRETTY_FUNCTION__;
494 m_mapView->setScene(scene);
497 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
499 qDebug() << __PRETTY_FUNCTION__;
502 m_ownLocationCrosshair->show();
503 m_drawOwnLocationCrosshair = true;
504 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
507 m_ownLocationCrosshair->hide();
508 m_drawOwnLocationCrosshair = false;
512 void MainWindow::setUsername(const QString &username)
514 qDebug() << __PRETTY_FUNCTION__;
518 void MainWindow::setViewPortSize(int width, int height)
520 qDebug() << __PRETTY_FUNCTION__;
522 m_viewPortWidth = width;
523 m_viewPortHeight = height;
526 void MainWindow::showMaemoInformationBox(const QString &message)
528 qDebug() << __PRETTY_FUNCTION__;
531 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
537 void MainWindow::showPanels(bool show)
539 qDebug() << __PRETTY_FUNCTION__;
541 m_friendsListPanel->show();
542 m_friendsListPanelSidebar->show();
544 m_userPanelSidebar->show();
547 m_friendsListPanel->closePanel();
548 m_friendsListPanel->hide();
549 m_friendsListPanelSidebar->hide();
550 m_userPanel->closePanel();
552 m_userPanelSidebar->hide();
556 void MainWindow::startLoginProcess(const QUrl &url)
558 qDebug() << __PRETTY_FUNCTION__;
561 m_webView = new QWebView;
562 m_loginDialog = new LoginDialog(this);
564 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
565 this, SIGNAL(updateCredentials(QUrl)));
566 connect(m_webView, SIGNAL(loadFinished(bool)),
567 this, SLOT(loadDone(bool)));
569 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
570 this, SLOT(loginDialogDone(QString,QString)));
574 emit fetchUsernameFromSettings();
576 if(!m_email.isEmpty()) {
577 m_loginDialog->setEmailField(m_email);
580 if(m_loginDialog->exec() != QDialog::Accepted) {
581 // if login dialog was canceled we need to stop processing webview
582 // stop and disconnect m_webView;
584 disconnect(m_webView, SIGNAL(loadFinished(bool)),
585 this, SLOT(loadDone(bool)));
586 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
587 this, SLOT(updateCredentials(const QUrl &)));
589 emit cancelLoginProcess();
592 m_webView->load(m_loginUrl);
593 toggleProgressIndicator(true);
598 void MainWindow::toggleProgressIndicator(bool value)
600 qDebug() << __PRETTY_FUNCTION__;
603 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
606 #endif // Q_WS_MAEMO_5
609 const QString MainWindow::username()
611 qDebug() << __PRETTY_FUNCTION__;