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 m_toSettingsAct->setObjectName(tr("Settings"));
250 connect(m_toSettingsAct, SIGNAL(triggered()),
251 this, SLOT(openSettingsDialog()));
254 m_gpsToggleAct = new QAction(tr("GPS"), this);
255 m_gpsToggleAct->setCheckable(true);
257 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
258 this, SIGNAL(gpsTriggered(bool)));
260 // automatic centering
261 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
262 m_autoCenteringAct->setCheckable(true);
263 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
264 this, SIGNAL(autoCenteringTriggered(bool)));
266 // build the actual menu
267 m_viewMenu = menuBar()->addMenu(tr("Main"));
268 m_viewMenu->addAction(m_loginAct);
269 m_viewMenu->addAction(m_toSettingsAct);
270 m_viewMenu->addAction(m_gpsToggleAct);
271 m_viewMenu->addAction(m_autoCenteringAct);
272 m_viewMenu->setObjectName(tr("Menu"));
275 void MainWindow::drawOsmLicense(const QSize &size)
277 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
279 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
280 size.height() - m_osmLicense->fontMetrics().height());
284 void MainWindow::drawOwnLocationCrosshair(int width, int height)
286 qDebug() << __PRETTY_FUNCTION__;
288 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
289 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
290 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
294 void MainWindow::gpsError(const QString &message)
296 qDebug() << __PRETTY_FUNCTION__;
298 showMaemoInformationBox(message);
301 void MainWindow::gpsTimeout()
303 qDebug() << __PRETTY_FUNCTION__;
305 showMaemoInformationBox(tr("GPS timeout"));
308 void MainWindow::grabZoomKeys(bool grab)
310 qDebug() << __PRETTY_FUNCTION__;
313 // Can't grab keys unless we have a window id
317 unsigned long val = (grab) ? 1 : 0;
318 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
322 XChangeProperty (QX11Info::display(),
328 reinterpret_cast<unsigned char *>(&val),
332 #endif // Q_WS_MAEMO_5
335 void MainWindow::keyPressEvent(QKeyEvent* event)
337 qDebug() << __PRETTY_FUNCTION__;
339 switch (event->key()) {
350 QWidget::keyPressEvent(event);
353 void MainWindow::loadDone(bool done)
355 qDebug() << __PRETTY_FUNCTION__;
357 // for the first time the login page is opened, we need to refresh it to get cookies working
365 QWebFrame* frame = m_webView->page()->currentFrame();
369 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
371 foreach (QWebElement element, emailCollection) {
372 element.setAttribute("value", m_email.toAscii());
375 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
376 foreach (QWebElement element, passwordCollection) {
377 element.setAttribute("value", m_password.toAscii());
379 // find connect button
380 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
381 foreach (QWebElement element, buttonCollection)
383 QPoint pos(element.geometry().center());
385 // send a mouse click event to the web page
386 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
388 QApplication::sendEvent(m_webView->page(), &event0);
389 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
391 QApplication::sendEvent(m_webView->page(), &event1);
397 void MainWindow::loggedIn(bool logged)
399 qDebug() << __PRETTY_FUNCTION__;
405 m_loginAct->setText(tr("Logout"));
408 m_loginAct->setText(tr("Login"));
410 showPanels(m_loggedIn);
414 void MainWindow::loginDialogDone(const QString &email, const QString &password)
416 qDebug() << __PRETTY_FUNCTION__;
419 m_password = password;
422 void MainWindow::loginFailed()
424 qDebug() << __PRETTY_FUNCTION__;
429 toggleProgressIndicator(false);
432 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
433 QMaemo5InformationBox::NoTimeout);
435 #endif // Q_WS_MAEMO_5
437 if(!m_email.isEmpty()) {
438 m_loginDialog->setEmailField(m_email);
441 if(m_loginDialog->exec() != QDialog::Accepted) {
442 // if login dialog was canceled we need to stop processing webview
443 // stop and disconnect m_webView;
445 disconnect(m_webView, SIGNAL(loadFinished(bool)),
446 this, SLOT(loadDone(bool)));
447 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
448 this, SLOT(updateCredentials(const QUrl &)));
450 emit cancelLoginProcess();
453 // re-load login page for webview
454 toggleProgressIndicator(true);
455 m_webView->load(m_loginUrl);
459 void MainWindow::openSettingsDialog()
461 qDebug() << __PRETTY_FUNCTION__;
463 SettingsDialog *dialog = new SettingsDialog(this);
465 dialog->disableSituareSettings();
470 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
472 qDebug() << __PRETTY_FUNCTION__;
474 m_autoCenteringAct->setChecked(enabled);
477 void MainWindow::setGPSButtonEnabled(bool enabled)
479 qDebug() << __PRETTY_FUNCTION__;
481 m_gpsToggleAct->setChecked(enabled);
482 setOwnLocationCrosshairVisibility(!enabled);
483 m_autoCenteringAct->setVisible(enabled);
486 void MainWindow::setMapViewScene(QGraphicsScene *scene)
488 qDebug() << __PRETTY_FUNCTION__;
490 m_mapView->setScene(scene);
493 void MainWindow::setUsername(const QString &username)
495 qDebug() << __PRETTY_FUNCTION__;
499 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
501 qDebug() << __PRETTY_FUNCTION__;
504 m_ownLocationCrosshair->show();
505 m_drawOwnLocationCrosshair = true;
506 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
509 m_ownLocationCrosshair->hide();
510 m_drawOwnLocationCrosshair = false;
514 void MainWindow::setViewPortSize(int width, int height)
516 qDebug() << __PRETTY_FUNCTION__;
518 m_viewPortWidth = width;
519 m_viewPortHeight = height;
522 void MainWindow::showMaemoInformationBox(const QString &message)
524 qDebug() << __PRETTY_FUNCTION__;
527 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
533 void MainWindow::showPanels(bool show)
535 qDebug() << __PRETTY_FUNCTION__;
537 // ToDo: opening panels when needed
538 m_friendsListPanel->show();
539 m_friendsListPanelSidebar->show();
541 m_userPanelSidebar->show();
544 m_friendsListPanel->hide();
545 m_friendsListPanelSidebar->hide();
547 m_userPanelSidebar->hide();
551 void MainWindow::startLoginProcess(const QUrl &url)
553 qDebug() << __PRETTY_FUNCTION__;
556 m_webView = new QWebView;
557 m_loginDialog = new LoginDialog(this);
559 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
560 this, SIGNAL(updateCredentials(QUrl)));
561 connect(m_webView, SIGNAL(loadFinished(bool)),
562 this, SLOT(loadDone(bool)));
564 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
565 this, SLOT(loginDialogDone(QString,QString)));
569 emit fetchUsernameFromSettings();
571 if(!m_email.isEmpty()) {
572 m_loginDialog->setEmailField(m_email);
575 if(m_loginDialog->exec() != QDialog::Accepted) {
576 // if login dialog was canceled we need to stop processing webview
577 // stop and disconnect m_webView;
579 disconnect(m_webView, SIGNAL(loadFinished(bool)),
580 this, SLOT(loadDone(bool)));
581 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
582 this, SLOT(updateCredentials(const QUrl &)));
584 emit cancelLoginProcess();
587 m_webView->load(m_loginUrl);
588 toggleProgressIndicator(true);
593 void MainWindow::toggleProgressIndicator(bool value)
595 qDebug() << __PRETTY_FUNCTION__;
598 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
601 #endif // Q_WS_MAEMO_5
604 const QString MainWindow::username()
606 qDebug() << __PRETTY_FUNCTION__;