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();
77 setWindowTitle(tr("Situare"));
79 // set stacking order of widgets
80 m_zoomButtonPanel->stackUnder(m_userPanel);
81 m_osmLicense->stackUnder(m_zoomButtonPanel);
82 m_ownLocationCrosshair->stackUnder(m_osmLicense);
83 m_mapView->stackUnder(m_ownLocationCrosshair);
85 this->toggleProgressIndicator(true);
89 // set screen size in desktop matching N900 screen size
90 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
93 MainWindow::~MainWindow()
95 qDebug() << __PRETTY_FUNCTION__;
103 void MainWindow::buildFriendListPanel()
105 qDebug() << __PRETTY_FUNCTION__;
107 m_friendsListPanel = new FriendListPanel(this);
108 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
110 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
112 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
113 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
115 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
116 this, SIGNAL(findFriend(QPointF)));
118 connect(m_mapView, SIGNAL(viewResized(QSize)),
119 m_friendsListPanel, SLOT(screenResized(QSize)));
121 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
122 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
124 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
125 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
128 void MainWindow::buildManualLocationCrosshair()
130 qDebug() << __PRETTY_FUNCTION__;
132 m_ownLocationCrosshair = new QLabel(this);
133 QPixmap crosshairImage(":/res/images/sight.png");
134 m_ownLocationCrosshair->setPixmap(crosshairImage);
135 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
136 m_ownLocationCrosshair->hide();
137 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
139 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
140 this, SLOT(drawOwnLocationCrosshair(int, int)));
143 void MainWindow::buildMap()
145 qDebug() << __PRETTY_FUNCTION__;
147 m_mapView = new MapView(this);
149 buildZoomButtonPanel();
151 m_ownLocationCrosshair = 0;
153 buildManualLocationCrosshair();
155 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
156 this, SIGNAL(mapViewScrolled(QPoint)));
158 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
159 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
161 connect(m_mapView, SIGNAL(viewResized(QSize)),
162 this, SIGNAL(mapViewResized(QSize)));
164 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
165 this, SLOT(setViewPortSize(int, int)));
167 connect(this, SIGNAL(zoomLevelChanged(int)),
168 m_mapView, SLOT(setZoomLevel(int)));
170 connect(m_mapView, SIGNAL(viewZoomFinished()),
171 this, SIGNAL(viewZoomFinished()));
174 void MainWindow::buildOsmLicense()
176 qDebug() << __PRETTY_FUNCTION__;
178 m_osmLicense = new QLabel(this);
179 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
180 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
181 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
182 m_osmLicense->setFont(QFont("Nokia Sans", 9));
183 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
184 m_osmLicense->fontMetrics().height());
186 connect(m_mapView, SIGNAL(viewResized(QSize)),
187 this, SLOT(drawOsmLicense(QSize)));
190 void MainWindow::buildUserInfoPanel()
192 qDebug() << __PRETTY_FUNCTION__;
194 m_userPanel = new UserInfoPanel(this);
195 m_userPanelSidebar = new PanelSideBar(this, LEFT);
197 m_userPanelSidebar->stackUnder(m_friendsListPanel);
198 m_userPanel->stackUnder(m_userPanelSidebar);
200 connect(this, SIGNAL(userLocationReady(User*)),
201 m_userPanel, SLOT(userDataReceived(User*)));
203 connect(m_userPanel, SIGNAL(requestReverseGeo()),
204 this, SIGNAL(requestReverseGeo()));
206 connect(this, SIGNAL(reverseGeoReady(QString)),
207 m_userPanel, SIGNAL(reverseGeoReady(QString)));
209 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
210 this, SIGNAL(statusUpdate(QString,bool)));
212 connect(m_userPanel, SIGNAL(refreshUserData()),
213 this, SIGNAL(refreshUserData()));
215 connect(m_mapView, SIGNAL(viewResized(QSize)),
216 m_userPanel, SLOT(screenResized(QSize)));
219 void MainWindow::buildZoomButtonPanel()
221 qDebug() << __PRETTY_FUNCTION__;
223 m_zoomButtonPanel = new ZoomButtonPanel(this);
225 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
226 this, SIGNAL(zoomIn()));
228 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
229 this, SIGNAL(zoomOut()));
231 connect(this, SIGNAL(zoomLevelChanged(int)),
232 m_zoomButtonPanel, SLOT(resetButtons()));
234 connect(this, SIGNAL(maxZoomLevelReached()),
235 m_zoomButtonPanel, SLOT(disableZoomInButton()));
237 connect(this, SIGNAL(minZoomLevelReached()),
238 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
240 connect(m_mapView, SIGNAL(viewResized(QSize)),
241 m_zoomButtonPanel, SLOT(screenResized(QSize)));
244 void MainWindow::createMenus()
246 qDebug() << __PRETTY_FUNCTION__;
249 m_loginAct = new QAction(tr("Login"), this);
250 connect(m_loginAct, SIGNAL(triggered()),
251 this, SIGNAL(loginActionPressed()));
254 m_toSettingsAct = new QAction(tr("Settings"), this);
255 connect(m_toSettingsAct, SIGNAL(triggered()),
256 this, SLOT(openSettingsDialog()));
259 m_gpsToggleAct = new QAction(tr("GPS"), this);
260 m_gpsToggleAct->setCheckable(true);
262 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
263 this, SIGNAL(gpsTriggered(bool)));
265 // automatic centering
266 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
267 m_autoCenteringAct->setCheckable(true);
268 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
269 this, SIGNAL(autoCenteringTriggered(bool)));
271 // build the actual menu
272 m_viewMenu = menuBar()->addMenu(tr("Main"));
273 m_viewMenu->addAction(m_loginAct);
274 m_viewMenu->addAction(m_toSettingsAct);
275 m_viewMenu->addAction(m_gpsToggleAct);
276 m_viewMenu->addAction(m_autoCenteringAct);
277 m_viewMenu->setObjectName(tr("Menu"));
280 void MainWindow::drawOsmLicense(const QSize &size)
282 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
284 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
286 size.height() - m_osmLicense->fontMetrics().height());
290 void MainWindow::drawOwnLocationCrosshair(int width, int height)
292 qDebug() << __PRETTY_FUNCTION__;
294 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
295 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
296 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
300 void MainWindow::gpsError(const QString &message)
302 qDebug() << __PRETTY_FUNCTION__;
304 showMaemoInformationBox(message);
307 void MainWindow::gpsTimeout()
309 qDebug() << __PRETTY_FUNCTION__;
311 showMaemoInformationBox(tr("GPS timeout"));
314 void MainWindow::grabZoomKeys(bool grab)
316 qDebug() << __PRETTY_FUNCTION__;
319 // Can't grab keys unless we have a window id
323 unsigned long val = (grab) ? 1 : 0;
324 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
328 XChangeProperty (QX11Info::display(),
334 reinterpret_cast<unsigned char *>(&val),
338 #endif // Q_WS_MAEMO_5
341 void MainWindow::keyPressEvent(QKeyEvent* event)
343 qDebug() << __PRETTY_FUNCTION__;
345 switch (event->key()) {
356 QWidget::keyPressEvent(event);
359 void MainWindow::loadDone(bool done)
361 qDebug() << __PRETTY_FUNCTION__;
363 // for the first time the login page is opened, we need to refresh it to get cookies working
371 QWebFrame* frame = m_webView->page()->currentFrame();
375 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
377 foreach (QWebElement element, emailCollection) {
378 element.setAttribute("value", m_email.toAscii());
381 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
382 foreach (QWebElement element, passwordCollection) {
383 element.setAttribute("value", m_password.toAscii());
385 // find connect button
386 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
387 foreach (QWebElement element, buttonCollection)
389 QPoint pos(element.geometry().center());
391 // send a mouse click event to the web page
392 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
394 QApplication::sendEvent(m_webView->page(), &event0);
395 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
397 QApplication::sendEvent(m_webView->page(), &event1);
403 void MainWindow::loggedIn(bool logged)
405 qDebug() << __PRETTY_FUNCTION__;
410 m_loginAct->setText(tr("Logout"));
413 m_loginAct->setText(tr("Login"));
415 showPanels(m_loggedIn);
418 void MainWindow::loginDialogDone(const QString &email, const QString &password)
420 qDebug() << __PRETTY_FUNCTION__;
423 m_password = password;
426 void MainWindow::loginFailed()
428 qDebug() << __PRETTY_FUNCTION__;
433 toggleProgressIndicator(false);
436 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
437 QMaemo5InformationBox::NoTimeout);
439 #endif // Q_WS_MAEMO_5
441 if(!m_email.isEmpty()) {
442 m_loginDialog->setEmailField(m_email);
445 if(m_loginDialog->exec() != QDialog::Accepted) {
446 // if login dialog was canceled we need to stop processing webview
447 // stop and disconnect m_webView;
449 disconnect(m_webView, SIGNAL(loadFinished(bool)),
450 this, SLOT(loadDone(bool)));
451 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
452 this, SLOT(updateCredentials(const QUrl &)));
454 emit cancelLoginProcess();
457 // re-load login page for webview
458 toggleProgressIndicator(true);
459 m_webView->load(m_loginUrl);
463 void MainWindow::openSettingsDialog()
465 qDebug() << __PRETTY_FUNCTION__;
467 SettingsDialog *dialog = new SettingsDialog(this);
469 dialog->disableSituareSettings();
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__;