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(m_userPanel, SIGNAL(findUser(QPointF)),
201 this, SIGNAL(findUser(QPointF)));
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);
228 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
229 this, SIGNAL(zoomIn()));
231 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
232 this, SIGNAL(zoomOut()));
234 connect(this, SIGNAL(zoomLevelChanged(int)),
235 m_zoomButtonPanel, SLOT(resetButtons()));
237 connect(this, SIGNAL(maxZoomLevelReached()),
238 m_zoomButtonPanel, SLOT(disableZoomInButton()));
240 connect(this, SIGNAL(minZoomLevelReached()),
241 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
243 connect(m_mapView, SIGNAL(viewResized(QSize)),
244 m_zoomButtonPanel, SLOT(screenResized(QSize)));
247 void MainWindow::createMenus()
249 qDebug() << __PRETTY_FUNCTION__;
252 m_loginAct = new QAction(tr("Login"), this);
253 connect(m_loginAct, SIGNAL(triggered()),
254 this, SIGNAL(loginActionPressed()));
257 m_toSettingsAct = new QAction(tr("Settings"), this);
258 connect(m_toSettingsAct, SIGNAL(triggered()),
259 this, SLOT(openSettingsDialog()));
262 m_gpsToggleAct = new QAction(tr("GPS"), this);
263 m_gpsToggleAct->setCheckable(true);
265 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
266 this, SIGNAL(gpsTriggered(bool)));
268 // automatic centering
269 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
270 m_autoCenteringAct->setCheckable(true);
271 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
272 this, SIGNAL(autoCenteringTriggered(bool)));
274 // build the actual menu
275 m_viewMenu = menuBar()->addMenu(tr("Main"));
276 m_viewMenu->addAction(m_loginAct);
277 m_viewMenu->addAction(m_toSettingsAct);
278 m_viewMenu->addAction(m_gpsToggleAct);
279 m_viewMenu->addAction(m_autoCenteringAct);
280 m_viewMenu->setObjectName(tr("Menu"));
283 void MainWindow::drawOsmLicense(const QSize &size)
285 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
287 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
289 size.height() - m_osmLicense->fontMetrics().height());
293 void MainWindow::drawOwnLocationCrosshair(int width, int height)
295 qDebug() << __PRETTY_FUNCTION__;
297 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
298 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
299 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
303 void MainWindow::gpsError(const QString &message)
305 qDebug() << __PRETTY_FUNCTION__;
307 showMaemoInformationBox(message);
310 void MainWindow::gpsTimeout()
312 qDebug() << __PRETTY_FUNCTION__;
314 showMaemoInformationBox(tr("GPS timeout"));
317 void MainWindow::grabZoomKeys(bool grab)
319 qDebug() << __PRETTY_FUNCTION__;
322 // Can't grab keys unless we have a window id
326 unsigned long val = (grab) ? 1 : 0;
327 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
331 XChangeProperty (QX11Info::display(),
337 reinterpret_cast<unsigned char *>(&val),
341 #endif // Q_WS_MAEMO_5
344 void MainWindow::keyPressEvent(QKeyEvent* event)
346 qDebug() << __PRETTY_FUNCTION__;
348 switch (event->key()) {
359 QWidget::keyPressEvent(event);
362 void MainWindow::loadDone(bool done)
364 qDebug() << __PRETTY_FUNCTION__;
366 // for the first time the login page is opened, we need to refresh it to get cookies working
374 QWebFrame* frame = m_webView->page()->currentFrame();
378 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
380 foreach (QWebElement element, emailCollection) {
381 element.setAttribute("value", m_email.toAscii());
384 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
385 foreach (QWebElement element, passwordCollection) {
386 element.setAttribute("value", m_password.toAscii());
388 // find connect button
389 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
390 foreach (QWebElement element, buttonCollection)
392 QPoint pos(element.geometry().center());
394 // send a mouse click event to the web page
395 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
397 QApplication::sendEvent(m_webView->page(), &event0);
398 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
400 QApplication::sendEvent(m_webView->page(), &event1);
406 void MainWindow::loggedIn(bool logged)
408 qDebug() << __PRETTY_FUNCTION__;
413 m_loginAct->setText(tr("Logout"));
416 m_loginAct->setText(tr("Login"));
418 showPanels(m_loggedIn);
421 void MainWindow::loginDialogDone(const QString &email, const QString &password)
423 qDebug() << __PRETTY_FUNCTION__;
426 m_password = password;
429 void MainWindow::loginFailed()
431 qDebug() << __PRETTY_FUNCTION__;
436 toggleProgressIndicator(false);
439 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
440 QMaemo5InformationBox::NoTimeout);
442 #endif // Q_WS_MAEMO_5
444 if(!m_email.isEmpty()) {
445 m_loginDialog->setEmailField(m_email);
448 if(m_loginDialog->exec() != QDialog::Accepted) {
449 // if login dialog was canceled we need to stop processing webview
450 // stop and disconnect m_webView;
452 disconnect(m_webView, SIGNAL(loadFinished(bool)),
453 this, SLOT(loadDone(bool)));
454 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
455 this, SLOT(updateCredentials(const QUrl &)));
457 emit cancelLoginProcess();
460 // re-load login page for webview
461 toggleProgressIndicator(true);
462 m_webView->load(m_loginUrl);
466 void MainWindow::openSettingsDialog()
468 qDebug() << __PRETTY_FUNCTION__;
470 SettingsDialog *dialog = new SettingsDialog(this);
472 dialog->disableSituareSettings();
477 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
479 qDebug() << __PRETTY_FUNCTION__;
481 m_autoCenteringAct->setChecked(enabled);
484 void MainWindow::setGPSButtonEnabled(bool enabled)
486 qDebug() << __PRETTY_FUNCTION__;
488 m_gpsToggleAct->setChecked(enabled);
489 setOwnLocationCrosshairVisibility(!enabled);
490 m_autoCenteringAct->setVisible(enabled);
493 void MainWindow::setMapViewScene(QGraphicsScene *scene)
495 qDebug() << __PRETTY_FUNCTION__;
497 m_mapView->setScene(scene);
500 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
502 qDebug() << __PRETTY_FUNCTION__;
505 m_ownLocationCrosshair->show();
506 m_drawOwnLocationCrosshair = true;
507 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
510 m_ownLocationCrosshair->hide();
511 m_drawOwnLocationCrosshair = false;
515 void MainWindow::setUsername(const QString &username)
517 qDebug() << __PRETTY_FUNCTION__;
521 void MainWindow::setViewPortSize(int width, int height)
523 qDebug() << __PRETTY_FUNCTION__;
525 m_viewPortWidth = width;
526 m_viewPortHeight = height;
529 void MainWindow::showMaemoInformationBox(const QString &message)
531 qDebug() << __PRETTY_FUNCTION__;
534 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
540 void MainWindow::showPanels(bool show)
542 qDebug() << __PRETTY_FUNCTION__;
544 m_friendsListPanel->show();
545 m_friendsListPanelSidebar->show();
547 m_userPanelSidebar->show();
550 m_friendsListPanel->closePanel();
551 m_friendsListPanel->hide();
552 m_friendsListPanelSidebar->hide();
553 m_userPanel->closePanel();
555 m_userPanelSidebar->hide();
559 void MainWindow::startLoginProcess(const QUrl &url)
561 qDebug() << __PRETTY_FUNCTION__;
564 m_webView = new QWebView;
565 m_loginDialog = new LoginDialog(this);
567 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
568 this, SIGNAL(updateCredentials(QUrl)));
569 connect(m_webView, SIGNAL(loadFinished(bool)),
570 this, SLOT(loadDone(bool)));
572 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
573 this, SLOT(loginDialogDone(QString,QString)));
577 emit fetchUsernameFromSettings();
579 if(!m_email.isEmpty()) {
580 m_loginDialog->setEmailField(m_email);
583 if(m_loginDialog->exec() != QDialog::Accepted) {
584 // if login dialog was canceled we need to stop processing webview
585 // stop and disconnect m_webView;
587 disconnect(m_webView, SIGNAL(loadFinished(bool)),
588 this, SLOT(loadDone(bool)));
589 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
590 this, SLOT(updateCredentials(const QUrl &)));
592 emit cancelLoginProcess();
595 m_webView->load(m_loginUrl);
596 toggleProgressIndicator(true);
601 void MainWindow::toggleProgressIndicator(bool value)
603 qDebug() << __PRETTY_FUNCTION__;
606 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
609 #endif // Q_WS_MAEMO_5
612 const QString MainWindow::username()
614 qDebug() << __PRETTY_FUNCTION__;