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_zoomButtonPanel, SLOT(screenResized(QSize)));
121 connect(m_mapView, SIGNAL(viewResized(QSize)),
122 m_friendsListPanel, SLOT(screenResized(QSize)));
124 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
125 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
127 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
128 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
131 void MainWindow::buildManualLocationCrosshair()
133 qDebug() << __PRETTY_FUNCTION__;
135 m_ownLocationCrosshair = new QLabel(this);
136 QPixmap crosshairImage(":/res/images/sight.png");
137 m_ownLocationCrosshair->setPixmap(crosshairImage);
138 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
139 m_ownLocationCrosshair->hide();
140 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
142 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
143 this, SLOT(drawOwnLocationCrosshair(int, int)));
146 void MainWindow::buildMap()
148 qDebug() << __PRETTY_FUNCTION__;
150 m_mapView = new MapView(this);
152 buildZoomButtonPanel();
154 m_ownLocationCrosshair = 0;
156 buildManualLocationCrosshair();
158 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
159 this, SIGNAL(mapViewScrolled(QPoint)));
161 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
162 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
164 connect(m_mapView, SIGNAL(viewResized(QSize)),
165 this, SIGNAL(mapViewResized(QSize)));
167 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
168 this, SLOT(setViewPortSize(int, int)));
170 connect(this, SIGNAL(zoomLevelChanged(int)),
171 m_mapView, SLOT(setZoomLevel(int)));
173 connect(m_mapView, SIGNAL(viewZoomFinished()),
174 this, SIGNAL(viewZoomFinished()));
177 void MainWindow::buildOsmLicense()
179 qDebug() << __PRETTY_FUNCTION__;
181 m_osmLicense = new QLabel(this);
182 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
183 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
184 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
185 m_osmLicense->setFont(QFont("Nokia Sans", 9));
186 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
187 m_osmLicense->fontMetrics().height());
189 connect(m_mapView, SIGNAL(viewResized(QSize)),
190 this, SLOT(drawOsmLicense(QSize)));
193 void MainWindow::buildUserInfoPanel()
195 qDebug() << __PRETTY_FUNCTION__;
197 m_userPanel = new UserInfoPanel(this);
198 m_userPanelSidebar = new PanelSideBar(this, LEFT);
200 m_userPanelSidebar->stackUnder(m_friendsListPanel);
201 m_userPanel->stackUnder(m_userPanelSidebar);
203 connect(this, SIGNAL(userLocationReady(User*)),
204 m_userPanel, SLOT(userDataReceived(User*)));
206 connect(m_userPanel, SIGNAL(requestReverseGeo()),
207 this, SIGNAL(requestReverseGeo()));
209 connect(this, SIGNAL(reverseGeoReady(QString)),
210 m_userPanel, SIGNAL(reverseGeoReady(QString)));
212 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
213 this, SIGNAL(statusUpdate(QString,bool)));
215 connect(m_userPanel, SIGNAL(refreshUserData()),
216 this, SIGNAL(refreshUserData()));
218 connect(m_mapView, SIGNAL(viewResized(QSize)),
219 m_userPanel, SLOT(screenResized(QSize)));
222 void MainWindow::buildZoomButtonPanel()
224 qDebug() << __PRETTY_FUNCTION__;
226 m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
227 ZOOM_BUTTON_PANEL_POSITION_Y);
229 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
230 this, SIGNAL(zoomIn()));
232 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
233 this, SIGNAL(zoomOut()));
235 connect(this, SIGNAL(zoomLevelChanged(int)),
236 m_zoomButtonPanel, SLOT(resetButtons()));
238 connect(this, SIGNAL(maxZoomLevelReached()),
239 m_zoomButtonPanel, SLOT(disableZoomInButton()));
241 connect(this, SIGNAL(minZoomLevelReached()),
242 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
244 QSettings settings(DIRECTORY_NAME, FILE_NAME);
245 m_zoomButtonPanel->move(settings.value(ZOOMPANEL_POSITION,
246 QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
247 ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint());
250 void MainWindow::createMenus()
252 qDebug() << __PRETTY_FUNCTION__;
255 m_loginAct = new QAction(tr("Login"), this);
256 connect(m_loginAct, SIGNAL(triggered()),
257 this, SIGNAL(loginActionPressed()));
260 m_toSettingsAct = new QAction(tr("Settings"), this);
261 connect(m_toSettingsAct, SIGNAL(triggered()),
262 this, SLOT(openSettingsDialog()));
265 m_gpsToggleAct = new QAction(tr("GPS"), this);
266 m_gpsToggleAct->setCheckable(true);
268 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
269 this, SIGNAL(gpsTriggered(bool)));
271 // automatic centering
272 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
273 m_autoCenteringAct->setCheckable(true);
274 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
275 this, SIGNAL(autoCenteringTriggered(bool)));
277 // build the actual menu
278 m_viewMenu = menuBar()->addMenu(tr("Main"));
279 m_viewMenu->addAction(m_loginAct);
280 m_viewMenu->addAction(m_toSettingsAct);
281 m_viewMenu->addAction(m_gpsToggleAct);
282 m_viewMenu->addAction(m_autoCenteringAct);
283 m_viewMenu->setObjectName(tr("Menu"));
286 void MainWindow::drawOsmLicense(const QSize &size)
288 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
290 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
291 size.height() - m_osmLicense->fontMetrics().height());
295 void MainWindow::drawOwnLocationCrosshair(int width, int height)
297 qDebug() << __PRETTY_FUNCTION__;
299 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
300 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
301 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
305 void MainWindow::gpsError(const QString &message)
307 qDebug() << __PRETTY_FUNCTION__;
309 showMaemoInformationBox(message);
312 void MainWindow::gpsTimeout()
314 qDebug() << __PRETTY_FUNCTION__;
316 showMaemoInformationBox(tr("GPS timeout"));
319 void MainWindow::grabZoomKeys(bool grab)
321 qDebug() << __PRETTY_FUNCTION__;
324 // Can't grab keys unless we have a window id
328 unsigned long val = (grab) ? 1 : 0;
329 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
333 XChangeProperty (QX11Info::display(),
339 reinterpret_cast<unsigned char *>(&val),
343 #endif // Q_WS_MAEMO_5
346 void MainWindow::keyPressEvent(QKeyEvent* event)
348 qDebug() << __PRETTY_FUNCTION__;
350 switch (event->key()) {
361 QWidget::keyPressEvent(event);
364 void MainWindow::loadDone(bool done)
366 qDebug() << __PRETTY_FUNCTION__;
368 // for the first time the login page is opened, we need to refresh it to get cookies working
376 QWebFrame* frame = m_webView->page()->currentFrame();
380 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
382 foreach (QWebElement element, emailCollection) {
383 element.setAttribute("value", m_email.toAscii());
386 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
387 foreach (QWebElement element, passwordCollection) {
388 element.setAttribute("value", m_password.toAscii());
390 // find connect button
391 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
392 foreach (QWebElement element, buttonCollection)
394 QPoint pos(element.geometry().center());
396 // send a mouse click event to the web page
397 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
399 QApplication::sendEvent(m_webView->page(), &event0);
400 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
402 QApplication::sendEvent(m_webView->page(), &event1);
408 void MainWindow::loggedIn(bool logged)
410 qDebug() << __PRETTY_FUNCTION__;
415 m_loginAct->setText(tr("Logout"));
418 m_loginAct->setText(tr("Login"));
420 showPanels(m_loggedIn);
423 void MainWindow::loginDialogDone(const QString &email, const QString &password)
425 qDebug() << __PRETTY_FUNCTION__;
428 m_password = password;
431 void MainWindow::loginFailed()
433 qDebug() << __PRETTY_FUNCTION__;
438 toggleProgressIndicator(false);
441 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
442 QMaemo5InformationBox::NoTimeout);
444 #endif // Q_WS_MAEMO_5
446 if(!m_email.isEmpty()) {
447 m_loginDialog->setEmailField(m_email);
450 if(m_loginDialog->exec() != QDialog::Accepted) {
451 // if login dialog was canceled we need to stop processing webview
452 // stop and disconnect m_webView;
454 disconnect(m_webView, SIGNAL(loadFinished(bool)),
455 this, SLOT(loadDone(bool)));
456 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
457 this, SLOT(updateCredentials(const QUrl &)));
459 emit cancelLoginProcess();
462 // re-load login page for webview
463 toggleProgressIndicator(true);
464 m_webView->load(m_loginUrl);
468 void MainWindow::openSettingsDialog()
470 qDebug() << __PRETTY_FUNCTION__;
472 SettingsDialog *dialog = new SettingsDialog(this);
474 dialog->disableSituareSettings();
479 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
481 qDebug() << __PRETTY_FUNCTION__;
483 m_autoCenteringAct->setChecked(enabled);
486 void MainWindow::setGPSButtonEnabled(bool enabled)
488 qDebug() << __PRETTY_FUNCTION__;
490 m_gpsToggleAct->setChecked(enabled);
491 setOwnLocationCrosshairVisibility(!enabled);
492 m_autoCenteringAct->setVisible(enabled);
495 void MainWindow::setMapViewScene(QGraphicsScene *scene)
497 qDebug() << __PRETTY_FUNCTION__;
499 m_mapView->setScene(scene);
502 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
504 qDebug() << __PRETTY_FUNCTION__;
507 m_ownLocationCrosshair->show();
508 m_drawOwnLocationCrosshair = true;
509 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
512 m_ownLocationCrosshair->hide();
513 m_drawOwnLocationCrosshair = false;
517 void MainWindow::setUsername(const QString &username)
519 qDebug() << __PRETTY_FUNCTION__;
523 void MainWindow::setViewPortSize(int width, int height)
525 qDebug() << __PRETTY_FUNCTION__;
527 m_viewPortWidth = width;
528 m_viewPortHeight = height;
531 void MainWindow::showMaemoInformationBox(const QString &message)
533 qDebug() << __PRETTY_FUNCTION__;
536 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
542 void MainWindow::showPanels(bool show)
544 qDebug() << __PRETTY_FUNCTION__;
546 m_friendsListPanel->show();
547 m_friendsListPanelSidebar->show();
549 m_userPanelSidebar->show();
552 m_friendsListPanel->closePanel();
553 m_friendsListPanel->hide();
554 m_friendsListPanelSidebar->hide();
555 m_userPanel->closePanel();
557 m_userPanelSidebar->hide();
561 void MainWindow::startLoginProcess(const QUrl &url)
563 qDebug() << __PRETTY_FUNCTION__;
566 m_webView = new QWebView;
567 m_loginDialog = new LoginDialog(this);
569 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
570 this, SIGNAL(updateCredentials(QUrl)));
571 connect(m_webView, SIGNAL(loadFinished(bool)),
572 this, SLOT(loadDone(bool)));
574 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
575 this, SLOT(loginDialogDone(QString,QString)));
579 emit fetchUsernameFromSettings();
581 if(!m_email.isEmpty()) {
582 m_loginDialog->setEmailField(m_email);
585 if(m_loginDialog->exec() != QDialog::Accepted) {
586 // if login dialog was canceled we need to stop processing webview
587 // stop and disconnect m_webView;
589 disconnect(m_webView, SIGNAL(loadFinished(bool)),
590 this, SLOT(loadDone(bool)));
591 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
592 this, SLOT(updateCredentials(const QUrl &)));
594 emit cancelLoginProcess();
597 m_webView->load(m_loginUrl);
598 toggleProgressIndicator(true);
603 void MainWindow::toggleProgressIndicator(bool value)
605 qDebug() << __PRETTY_FUNCTION__;
608 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
611 #endif // Q_WS_MAEMO_5
614 const QString MainWindow::username()
616 qDebug() << __PRETTY_FUNCTION__;