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 qWarning() << __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, ZOOM_BUTTON_PANEL_POSITION_X,
224 ZOOM_BUTTON_PANEL_POSITION_Y);
226 connect(m_zoomButtonPanel->m_zoomInButton, SIGNAL(clicked()),
227 this, SIGNAL(zoomIn()));
229 connect(m_zoomButtonPanel->m_zoomOutButton, SIGNAL(clicked()),
230 this, SIGNAL(zoomOut()));
232 connect(this, SIGNAL(zoomLevelChanged(int)),
233 m_zoomButtonPanel, SLOT(resetButtons()));
235 connect(this, SIGNAL(maxZoomLevelReached()),
236 m_zoomButtonPanel, SLOT(disableZoomInButton()));
238 connect(this, SIGNAL(minZoomLevelReached()),
239 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
242 void MainWindow::createMenus()
244 qDebug() << __PRETTY_FUNCTION__;
247 m_loginAct = new QAction(tr("Login"), this);
248 connect(m_loginAct, SIGNAL(triggered()),
249 this, SIGNAL(loginPressed()));
252 m_toSettingsAct = new QAction(tr("Settings"), this);
253 connect(m_toSettingsAct, SIGNAL(triggered()),
254 this, SLOT(openSettingsDialog()));
257 m_gpsToggleAct = new QAction(tr("GPS"), this);
258 m_gpsToggleAct->setCheckable(true);
260 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
261 this, SIGNAL(gpsTriggered(bool)));
263 // automatic centering
264 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
265 m_autoCenteringAct->setCheckable(true);
266 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
267 this, SIGNAL(autoCenteringTriggered(bool)));
269 // build the actual menu
270 m_viewMenu = menuBar()->addMenu(tr("Main"));
271 m_viewMenu->addAction(m_loginAct);
272 m_viewMenu->addAction(m_toSettingsAct);
273 m_viewMenu->addAction(m_gpsToggleAct);
274 m_viewMenu->addAction(m_autoCenteringAct);
275 m_viewMenu->setObjectName(tr("Menu"));
278 void MainWindow::drawOsmLicense(const QSize &size)
280 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
282 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
283 size.height() - m_osmLicense->fontMetrics().height());
287 void MainWindow::drawOwnLocationCrosshair(int width, int height)
289 qDebug() << __PRETTY_FUNCTION__;
291 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
292 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
293 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
297 void MainWindow::gpsError(const QString &message)
299 qDebug() << __PRETTY_FUNCTION__;
301 showMaemoInformationBox(message);
304 void MainWindow::gpsTimeout()
306 qDebug() << __PRETTY_FUNCTION__;
308 showMaemoInformationBox(tr("GPS timeout"));
311 void MainWindow::grabZoomKeys(bool grab)
313 qDebug() << __PRETTY_FUNCTION__;
316 // Can't grab keys unless we have a window id
320 unsigned long val = (grab) ? 1 : 0;
321 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
325 XChangeProperty (QX11Info::display(),
331 reinterpret_cast<unsigned char *>(&val),
335 #endif // Q_WS_MAEMO_5
338 void MainWindow::keyPressEvent(QKeyEvent* event)
340 qDebug() << __PRETTY_FUNCTION__;
342 switch (event->key()) {
353 QWidget::keyPressEvent(event);
356 void MainWindow::loadDone(bool done)
358 qDebug() << __PRETTY_FUNCTION__;
360 // for the first time the login page is opened, we need to refresh it to get cookies working
368 QWebFrame* frame = m_webView->page()->currentFrame();
372 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
374 foreach (QWebElement element, emailCollection) {
375 element.setAttribute("value", m_email.toAscii());
378 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
379 foreach (QWebElement element, passwordCollection) {
380 element.setAttribute("value", m_password.toAscii());
382 // find connect button
383 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
384 foreach (QWebElement element, buttonCollection)
386 QPoint pos(element.geometry().center());
388 // send a mouse click event to the web page
389 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
391 QApplication::sendEvent(m_webView->page(), &event0);
392 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
394 QApplication::sendEvent(m_webView->page(), &event1);
400 void MainWindow::loggedIn(bool logged)
402 qDebug() << __PRETTY_FUNCTION__;
407 m_loginAct->setText(tr("Logout"));
410 m_loginAct->setText(tr("Login"));
412 showPanels(m_loggedIn);
415 void MainWindow::loginDialogDone(const QString &email, const QString &password)
417 qDebug() << __PRETTY_FUNCTION__;
420 m_password = password;
423 void MainWindow::loginFailed()
425 qDebug() << __PRETTY_FUNCTION__;
430 toggleProgressIndicator(false);
433 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
434 QMaemo5InformationBox::NoTimeout);
436 #endif // Q_WS_MAEMO_5
438 if(!m_email.isEmpty()) {
439 m_loginDialog->setEmailField(m_email);
442 if(m_loginDialog->exec() != QDialog::Accepted) {
443 // if login dialog was canceled we need to stop processing webview
444 // stop and disconnect m_webView;
446 disconnect(m_webView, SIGNAL(loadFinished(bool)),
447 this, SLOT(loadDone(bool)));
448 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
449 this, SLOT(updateCredentials(const QUrl &)));
451 emit cancelLoginProcess();
454 // re-load login page for webview
455 toggleProgressIndicator(true);
456 m_webView->load(m_loginUrl);
460 void MainWindow::openSettingsDialog()
462 qDebug() << __PRETTY_FUNCTION__;
464 SettingsDialog *dialog = new SettingsDialog(this);
466 dialog->disableSituareSettings();
471 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
473 qDebug() << __PRETTY_FUNCTION__;
475 m_autoCenteringAct->setChecked(enabled);
478 void MainWindow::setGPSButtonEnabled(bool enabled)
480 qDebug() << __PRETTY_FUNCTION__;
482 m_gpsToggleAct->setChecked(enabled);
483 setOwnLocationCrosshairVisibility(!enabled);
484 m_autoCenteringAct->setVisible(enabled);
487 void MainWindow::setMapViewScene(QGraphicsScene *scene)
489 qDebug() << __PRETTY_FUNCTION__;
491 m_mapView->setScene(scene);
494 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
496 qDebug() << __PRETTY_FUNCTION__;
499 m_ownLocationCrosshair->show();
500 m_drawOwnLocationCrosshair = true;
501 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
504 m_ownLocationCrosshair->hide();
505 m_drawOwnLocationCrosshair = false;
509 void MainWindow::setUsername(const QString &username)
511 qDebug() << __PRETTY_FUNCTION__;
515 void MainWindow::setViewPortSize(int width, int height)
517 qDebug() << __PRETTY_FUNCTION__;
519 m_viewPortWidth = width;
520 m_viewPortHeight = height;
523 void MainWindow::showMaemoInformationBox(const QString &message)
525 qDebug() << __PRETTY_FUNCTION__;
528 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
534 void MainWindow::showPanels(bool show)
536 qDebug() << __PRETTY_FUNCTION__;
538 m_friendsListPanel->show();
539 m_friendsListPanelSidebar->show();
541 m_userPanelSidebar->show();
544 m_friendsListPanel->closePanel();
545 m_friendsListPanel->hide();
546 m_friendsListPanelSidebar->hide();
547 m_userPanel->closePanel();
549 m_userPanelSidebar->hide();
553 void MainWindow::startLoginProcess(const QUrl &url)
555 qDebug() << __PRETTY_FUNCTION__;
558 m_webView = new QWebView;
559 m_loginDialog = new LoginDialog(this);
561 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
562 this, SIGNAL(updateCredentials(QUrl)));
563 connect(m_webView, SIGNAL(loadFinished(bool)),
564 this, SLOT(loadDone(bool)));
566 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
567 this, SLOT(loginDialogDone(QString,QString)));
571 emit fetchUsernameFromSettings();
573 if(!m_email.isEmpty()) {
574 m_loginDialog->setEmailField(m_email);
577 if(m_loginDialog->exec() != QDialog::Accepted) {
578 // if login dialog was canceled we need to stop processing webview
579 // stop and disconnect m_webView;
581 disconnect(m_webView, SIGNAL(loadFinished(bool)),
582 this, SLOT(loadDone(bool)));
583 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
584 this, SLOT(updateCredentials(const QUrl &)));
586 emit cancelLoginProcess();
589 m_webView->load(m_loginUrl);
590 toggleProgressIndicator(true);
595 void MainWindow::toggleProgressIndicator(bool value)
597 qDebug() << __PRETTY_FUNCTION__;
600 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
603 #endif // Q_WS_MAEMO_5
606 const QString MainWindow::username()
608 qDebug() << __PRETTY_FUNCTION__;