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,
29 #include <QtMaemo5/QMaemo5InformationBox>
30 #endif // Q_WS_MAEMO_5
33 #include "facebookservice/facebookauthentication.h"
34 #include "friendlistpanel.h"
35 #include "logindialog.h"
36 #include "map/mapview.h"
37 #include "settingsdialog.h"
38 #include "userinfopanel.h"
39 #include "zoombuttonpanel.h"
41 #include "mainwindow.h"
43 #include <QtGui/QX11Info>
45 #include <X11/Xatom.h>
47 // values for setting screen size in desktop matching N900 screen size
48 const int N900_APP_WIDTH = 800;
49 const int N900_APP_HEIGHT = 449;
51 MainWindow::MainWindow(QWidget *parent)
52 : QMainWindow(parent),
53 m_drawOwnLocationCrosshair(false),
61 qDebug() << __PRETTY_FUNCTION__;
66 QHBoxLayout *layout = new QHBoxLayout;
67 layout->addWidget(m_mapView);
69 setCentralWidget(new QWidget());
70 centralWidget()->setLayout(layout);
72 buildFriendListPanel();
76 setWindowTitle(tr("Situare"));
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 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
109 m_friendsListPanel->stackUnder(m_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(viewResized(QSize)),
118 m_friendsListPanel, SLOT(screenResized(QSize)));
120 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
121 m_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());
132 m_ownLocationCrosshair->hide();
133 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
135 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
136 this, SLOT(drawOwnLocationCrosshair(int, int)));
139 void MainWindow::buildMap()
141 qDebug() << __PRETTY_FUNCTION__;
143 m_mapView = new MapView(this);
145 buildZoomButtonPanel();
147 m_ownLocationCrosshair = 0;
149 buildManualLocationCrosshair();
151 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
152 this, SIGNAL(mapViewScrolled(QPoint)));
154 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
155 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
157 connect(m_mapView, SIGNAL(viewResized(QSize)),
158 this, SIGNAL(mapViewResized(QSize)));
160 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
161 this, SLOT(setViewPortSize(int, int)));
163 connect(this, SIGNAL(zoomLevelChanged(int)),
164 m_mapView, SLOT(setZoomLevel(int)));
166 connect(m_mapView, SIGNAL(viewZoomFinished()),
167 this, SIGNAL(viewZoomFinished()));
170 void MainWindow::buildOsmLicense()
172 qWarning() << __PRETTY_FUNCTION__;
174 m_osmLicense = new QLabel(this);
175 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
176 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
177 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
178 m_osmLicense->setFont(QFont("Nokia Sans", 9));
179 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
180 m_osmLicense->fontMetrics().height());
182 connect(m_mapView, SIGNAL(viewResized(QSize)),
183 this, SLOT(drawOsmLicense(QSize)));
186 void MainWindow::buildUserInfoPanel()
188 qDebug() << __PRETTY_FUNCTION__;
190 m_userPanel = new UserInfoPanel(this);
191 m_userPanelSidebar = new PanelSideBar(this, LEFT);
193 m_userPanelSidebar->stackUnder(m_friendsListPanel);
194 m_userPanel->stackUnder(m_userPanelSidebar);
196 connect(this, SIGNAL(userLocationReady(User*)),
197 m_userPanel, SLOT(userDataReceived(User*)));
199 connect(m_userPanel, SIGNAL(requestReverseGeo()),
200 this, SIGNAL(requestReverseGeo()));
202 connect(this, SIGNAL(reverseGeoReady(QString)),
203 m_userPanel, SIGNAL(reverseGeoReady(QString)));
205 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
206 this, SIGNAL(statusUpdate(QString,bool)));
208 connect(m_userPanel, SIGNAL(refreshUserData()),
209 this, SIGNAL(refreshUserData()));
211 connect(m_mapView, SIGNAL(viewResized(QSize)),
212 m_userPanel, SLOT(screenResized(QSize)));
215 void MainWindow::buildZoomButtonPanel()
217 qDebug() << __PRETTY_FUNCTION__;
219 m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
220 ZOOM_BUTTON_PANEL_POSITION_Y);
222 connect(m_zoomButtonPanel->m_zoomInButton, SIGNAL(clicked()),
223 this, SIGNAL(zoomIn()));
225 connect(m_zoomButtonPanel->m_zoomOutButton, SIGNAL(clicked()),
226 this, SIGNAL(zoomOut()));
228 connect(this, SIGNAL(zoomLevelChanged(int)),
229 m_zoomButtonPanel, SLOT(resetButtons()));
231 connect(this, SIGNAL(maxZoomLevelReached()),
232 m_zoomButtonPanel, SLOT(disableZoomInButton()));
234 connect(this, SIGNAL(minZoomLevelReached()),
235 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
238 void MainWindow::createMenus()
240 qDebug() << __PRETTY_FUNCTION__;
243 m_loginAct = new QAction(tr("Login"), this);
244 connect(m_loginAct, SIGNAL(triggered()),
245 this, SIGNAL(loginPressed()));
248 m_toSettingsAct = new QAction(tr("Settings"), this);
249 connect(m_toSettingsAct, SIGNAL(triggered()),
250 this, SLOT(openSettingsDialog()));
253 m_gpsToggleAct = new QAction(tr("GPS"), this);
254 m_gpsToggleAct->setCheckable(true);
256 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
257 this, SIGNAL(gpsTriggered(bool)));
259 // automatic centering
260 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
261 m_autoCenteringAct->setCheckable(true);
262 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
263 this, SIGNAL(autoCenteringTriggered(bool)));
265 // build the actual menu
266 m_viewMenu = menuBar()->addMenu(tr("Main"));
267 m_viewMenu->addAction(m_loginAct);
268 m_viewMenu->addAction(m_toSettingsAct);
269 m_viewMenu->addAction(m_gpsToggleAct);
270 m_viewMenu->addAction(m_autoCenteringAct);
271 m_viewMenu->setObjectName(tr("Menu"));
274 void MainWindow::drawOsmLicense(const QSize &size)
276 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
278 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
279 size.height() - m_osmLicense->fontMetrics().height());
283 void MainWindow::drawOwnLocationCrosshair(int width, int height)
285 qDebug() << __PRETTY_FUNCTION__;
287 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
288 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
289 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
293 void MainWindow::gpsError(const QString &message)
295 qDebug() << __PRETTY_FUNCTION__;
297 showMaemoInformationBox(message);
300 void MainWindow::gpsTimeout()
302 qDebug() << __PRETTY_FUNCTION__;
304 showMaemoInformationBox(tr("GPS timeout"));
307 void MainWindow::grabZoomKeys(bool grab)
309 qDebug() << __PRETTY_FUNCTION__;
312 // Can't grab keys unless we have a window id
316 unsigned long val = (grab) ? 1 : 0;
317 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
321 XChangeProperty (QX11Info::display(),
327 reinterpret_cast<unsigned char *>(&val),
331 #endif // Q_WS_MAEMO_5
334 void MainWindow::keyPressEvent(QKeyEvent* event)
336 qDebug() << __PRETTY_FUNCTION__;
338 switch (event->key()) {
349 QWidget::keyPressEvent(event);
352 void MainWindow::loadDone(bool done)
354 qDebug() << __PRETTY_FUNCTION__;
356 // for the first time the login page is opened, we need to refresh it to get cookies working
364 QWebFrame* frame = m_webView->page()->currentFrame();
368 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
370 foreach (QWebElement element, emailCollection) {
371 element.setAttribute("value", m_email.toAscii());
374 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
375 foreach (QWebElement element, passwordCollection) {
376 element.setAttribute("value", m_password.toAscii());
378 // find connect button
379 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
380 foreach (QWebElement element, buttonCollection)
382 QPoint pos(element.geometry().center());
384 // send a mouse click event to the web page
385 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
387 QApplication::sendEvent(m_webView->page(), &event0);
388 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
390 QApplication::sendEvent(m_webView->page(), &event1);
396 void MainWindow::loggedIn(bool logged)
398 qDebug() << __PRETTY_FUNCTION__;
403 m_loginAct->setText(tr("Logout"));
406 m_loginAct->setText(tr("Login"));
408 showPanels(m_loggedIn);
411 void MainWindow::loginDialogDone(const QString &email, const QString &password)
413 qDebug() << __PRETTY_FUNCTION__;
416 m_password = password;
419 void MainWindow::loginFailed()
421 qDebug() << __PRETTY_FUNCTION__;
426 toggleProgressIndicator(false);
429 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
430 QMaemo5InformationBox::NoTimeout);
432 #endif // Q_WS_MAEMO_5
434 if(!m_email.isEmpty()) {
435 m_loginDialog->setEmailField(m_email);
438 if(m_loginDialog->exec() != QDialog::Accepted) {
439 // if login dialog was canceled we need to stop processing webview
440 // stop and disconnect m_webView;
442 disconnect(m_webView, SIGNAL(loadFinished(bool)),
443 this, SLOT(loadDone(bool)));
444 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
445 this, SLOT(updateCredentials(const QUrl &)));
447 emit cancelLoginProcess();
450 // re-load login page for webview
451 toggleProgressIndicator(true);
452 m_webView->load(m_loginUrl);
456 void MainWindow::openSettingsDialog()
458 qDebug() << __PRETTY_FUNCTION__;
460 SettingsDialog *dialog = new SettingsDialog(this);
462 dialog->disableSituareSettings();
467 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
469 qDebug() << __PRETTY_FUNCTION__;
471 m_autoCenteringAct->setChecked(enabled);
474 void MainWindow::setGPSButtonEnabled(bool enabled)
476 qDebug() << __PRETTY_FUNCTION__;
478 m_gpsToggleAct->setChecked(enabled);
479 setOwnLocationCrosshairVisibility(!enabled);
480 m_autoCenteringAct->setVisible(enabled);
483 void MainWindow::setMapViewScene(QGraphicsScene *scene)
485 qDebug() << __PRETTY_FUNCTION__;
487 m_mapView->setScene(scene);
490 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
492 qDebug() << __PRETTY_FUNCTION__;
495 m_ownLocationCrosshair->show();
496 m_drawOwnLocationCrosshair = true;
497 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
500 m_ownLocationCrosshair->hide();
501 m_drawOwnLocationCrosshair = false;
505 void MainWindow::setUsername(const QString &username)
507 qDebug() << __PRETTY_FUNCTION__;
511 void MainWindow::setViewPortSize(int width, int height)
513 qDebug() << __PRETTY_FUNCTION__;
515 m_viewPortWidth = width;
516 m_viewPortHeight = height;
519 void MainWindow::showMaemoInformationBox(const QString &message)
521 qDebug() << __PRETTY_FUNCTION__;
524 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
530 void MainWindow::showPanels(bool show)
532 qDebug() << __PRETTY_FUNCTION__;
534 m_friendsListPanel->show();
535 m_friendsListPanelSidebar->show();
537 m_userPanelSidebar->show();
540 m_friendsListPanel->closePanel();
541 m_friendsListPanel->hide();
542 m_friendsListPanelSidebar->hide();
543 m_userPanel->closePanel();
545 m_userPanelSidebar->hide();
549 void MainWindow::startLoginProcess(const QUrl &url)
551 qDebug() << __PRETTY_FUNCTION__;
554 m_webView = new QWebView;
555 m_loginDialog = new LoginDialog(this);
557 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
558 this, SIGNAL(updateCredentials(QUrl)));
559 connect(m_webView, SIGNAL(loadFinished(bool)),
560 this, SLOT(loadDone(bool)));
562 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
563 this, SLOT(loginDialogDone(QString,QString)));
567 emit fetchUsernameFromSettings();
569 if(!m_email.isEmpty()) {
570 m_loginDialog->setEmailField(m_email);
573 if(m_loginDialog->exec() != QDialog::Accepted) {
574 // if login dialog was canceled we need to stop processing webview
575 // stop and disconnect m_webView;
577 disconnect(m_webView, SIGNAL(loadFinished(bool)),
578 this, SLOT(loadDone(bool)));
579 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
580 this, SLOT(updateCredentials(const QUrl &)));
582 emit cancelLoginProcess();
585 m_webView->load(m_loginUrl);
586 toggleProgressIndicator(true);
591 void MainWindow::toggleProgressIndicator(bool value)
593 qDebug() << __PRETTY_FUNCTION__;
596 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
599 #endif // Q_WS_MAEMO_5
602 const QString MainWindow::username()
604 qDebug() << __PRETTY_FUNCTION__;