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 "panelsidebar.h"
39 #include "settingsdialog.h"
40 #include "userinfopanel.h"
41 #include "zoombuttonpanel.h"
43 #include "mainwindow.h"
45 #include <QtGui/QX11Info>
47 #include <X11/Xatom.h>
49 // values for setting screen size in desktop matching N900 screen size
50 const int N900_APP_WIDTH = 800;
51 const int N900_APP_HEIGHT = 449;
53 MainWindow::MainWindow(QWidget *parent)
54 : QMainWindow(parent),
55 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 PanelSideBar *friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
110 m_friendsListPanel->stackUnder(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 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 PanelSideBar *userPanelSidebar = new PanelSideBar(this, LEFT);
197 userPanelSidebar->stackUnder(m_friendsListPanel);
198 m_userPanel->stackUnder(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_toSettingsAct = new QAction(tr("Settings"), this);
248 m_toSettingsAct->setObjectName(tr("Settings"));
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_toSettingsAct);
268 m_viewMenu->addAction(m_gpsToggleAct);
269 m_viewMenu->addAction(m_autoCenteringAct);
270 m_viewMenu->setObjectName(tr("Menu"));
273 void MainWindow::drawOsmLicense(const QSize &size)
275 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
277 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
278 size.height() - m_osmLicense->fontMetrics().height());
282 void MainWindow::drawOwnLocationCrosshair(int width, int height)
284 qDebug() << __PRETTY_FUNCTION__;
286 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
287 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
288 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
292 void MainWindow::gpsError(const QString &message)
294 qDebug() << __PRETTY_FUNCTION__;
296 showMaemoInformationBox(message);
299 void MainWindow::gpsTimeout()
301 qDebug() << __PRETTY_FUNCTION__;
303 showMaemoInformationBox(tr("GPS timeout"));
306 void MainWindow::grabZoomKeys(bool grab)
308 qDebug() << __PRETTY_FUNCTION__;
311 // Can't grab keys unless we have a window id
315 unsigned long val = (grab) ? 1 : 0;
316 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
320 XChangeProperty (QX11Info::display(),
326 reinterpret_cast<unsigned char *>(&val),
330 #endif // Q_WS_MAEMO_5
333 void MainWindow::keyPressEvent(QKeyEvent* event)
335 qDebug() << __PRETTY_FUNCTION__;
337 switch (event->key()) {
348 QWidget::keyPressEvent(event);
351 void MainWindow::loadDone(bool done)
353 qDebug() << __PRETTY_FUNCTION__;
355 // for the first time the login page is opened, we need to refresh it to get cookies working
363 QWebFrame* frame = m_webView->page()->currentFrame();
367 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
369 foreach (QWebElement element, emailCollection) {
370 element.setAttribute("value", m_email.toAscii());
373 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
374 foreach (QWebElement element, passwordCollection) {
375 element.setAttribute("value", m_password.toAscii());
377 // find connect button
378 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
379 foreach (QWebElement element, buttonCollection)
381 QPoint pos(element.geometry().center());
383 // send a mouse click event to the web page
384 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
386 QApplication::sendEvent(m_webView->page(), &event0);
387 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
389 QApplication::sendEvent(m_webView->page(), &event1);
395 void MainWindow::loginDialogDone(const QString &email, const QString &password)
397 qDebug() << __PRETTY_FUNCTION__;
400 m_password = password;
403 void MainWindow::loginFailed()
405 qDebug() << __PRETTY_FUNCTION__;
410 toggleProgressIndicator(false);
413 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
414 QMaemo5InformationBox::NoTimeout);
416 #endif // Q_WS_MAEMO_5
418 if(!m_email.isEmpty()) {
419 m_loginDialog->setEmailField(m_email);
422 if(m_loginDialog->exec() != QDialog::Accepted) {
423 // if login dialog was canceled we need to stop processing webview
424 // stop and disconnect m_webView;
426 disconnect(m_webView, SIGNAL(loadFinished(bool)),
427 this, SLOT(loadDone(bool)));
428 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
429 this, SLOT(updateCredentials(const QUrl &)));
431 emit cancelLoginProcess();
434 // re-load login page for webview
435 toggleProgressIndicator(true);
436 m_webView->load(m_loginUrl);
440 void MainWindow::openSettingsDialog()
442 qDebug() << __PRETTY_FUNCTION__;
444 SettingsDialog *dialog = new SettingsDialog(this);
448 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
450 qDebug() << __PRETTY_FUNCTION__;
452 m_autoCenteringAct->setChecked(enabled);
455 void MainWindow::setGPSButtonEnabled(bool enabled)
457 qDebug() << __PRETTY_FUNCTION__;
459 m_gpsToggleAct->setChecked(enabled);
460 setOwnLocationCrosshairVisibility(!enabled);
461 m_autoCenteringAct->setVisible(enabled);
464 void MainWindow::setMapViewScene(QGraphicsScene *scene)
466 qDebug() << __PRETTY_FUNCTION__;
468 m_mapView->setScene(scene);
471 void MainWindow::setUsername(const QString &username)
473 qDebug() << __PRETTY_FUNCTION__;
477 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
479 qDebug() << __PRETTY_FUNCTION__;
482 m_ownLocationCrosshair->show();
483 m_drawOwnLocationCrosshair = true;
484 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
487 m_ownLocationCrosshair->hide();
488 m_drawOwnLocationCrosshair = false;
492 void MainWindow::setViewPortSize(int width, int height)
494 qDebug() << __PRETTY_FUNCTION__;
496 m_viewPortWidth = width;
497 m_viewPortHeight = height;
500 void MainWindow::showMaemoInformationBox(const QString &message)
502 qDebug() << __PRETTY_FUNCTION__;
505 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
511 void MainWindow::startLoginProcess(const QUrl &url)
513 qDebug() << __PRETTY_FUNCTION__;
516 m_webView = new QWebView;
517 m_loginDialog = new LoginDialog(this);
519 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
520 this, SIGNAL(updateCredentials(QUrl)));
521 connect(m_webView, SIGNAL(loadFinished(bool)),
522 this, SLOT(loadDone(bool)));
524 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
525 this, SLOT(loginDialogDone(QString,QString)));
529 emit fetchUsernameFromSettings();
531 if(!m_email.isEmpty()) {
532 m_loginDialog->setEmailField(m_email);
535 if(m_loginDialog->exec() != QDialog::Accepted) {
536 // if login dialog was canceled we need to stop processing webview
537 // stop and disconnect m_webView;
539 disconnect(m_webView, SIGNAL(loadFinished(bool)),
540 this, SLOT(loadDone(bool)));
541 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
542 this, SLOT(updateCredentials(const QUrl &)));
544 emit cancelLoginProcess();
547 m_webView->load(m_loginUrl);
548 toggleProgressIndicator(true);
553 void MainWindow::toggleProgressIndicator(bool value)
555 qDebug() << __PRETTY_FUNCTION__;
558 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
561 #endif // Q_WS_MAEMO_5
564 const QString MainWindow::username()
566 qDebug() << __PRETTY_FUNCTION__;