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,
28 #include <QtAlgorithms>
31 #include "facebookservice/facebookauthentication.h"
32 #include "friendlistpanel.h"
33 #include "logindialog.h"
34 #include "map/mapview.h"
35 #include "settingsdialog.h"
36 #include "userinfopanel.h"
37 #include "zoombuttonpanel.h"
39 #include "mainwindow.h"
41 // These MUST BE HERE, compiling for Maemo fails if moved
43 #include <QtMaemo5/QMaemo5InformationBox>
44 #include <QtGui/QX11Info>
45 #include <X11/Xatom.h>
47 #endif // Q_WS_MAEMO_5
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),
58 m_ownLocationCrosshair(0),
61 m_fullScreenButton(0),
65 qDebug() << __PRETTY_FUNCTION__;
70 QHBoxLayout *layout = new QHBoxLayout;
71 layout->addWidget(m_mapView);
73 setCentralWidget(new QWidget());
74 centralWidget()->setLayout(layout);
76 buildFriendListPanel();
79 m_settingsDialog = new SettingsDialog(this);
80 m_settingsDialog->hide();
81 connect(m_settingsDialog, SIGNAL(enableAutomaticLocationUpdate(bool,int)),
82 this, SIGNAL(enableAutomaticLocationUpdate(bool,int)));
85 setWindowTitle(tr("Situare"));
87 // set stacking order of widgets
88 m_zoomButtonPanel->stackUnder(m_userPanel);
89 if(m_fullScreenButton) {
90 m_fullScreenButton->stackUnder(m_zoomButtonPanel);
91 m_osmLicense->stackUnder(m_fullScreenButton);
93 m_osmLicense->stackUnder(m_zoomButtonPanel);
94 m_ownLocationCrosshair->stackUnder(m_osmLicense);
95 m_mapView->stackUnder(m_ownLocationCrosshair);
97 this->toggleProgressIndicator(true);
101 // set screen size in desktop matching N900 screen size
102 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
105 MainWindow::~MainWindow()
107 qDebug() << __PRETTY_FUNCTION__;
114 qDeleteAll(m_queue.begin(), m_queue.end());
118 void MainWindow::buildFullScreenButton()
120 qDebug() << __PRETTY_FUNCTION__;
122 m_fullScreenButton = new QToolButton(this);
123 m_fullScreenButton->setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
124 m_fullScreenButton->setFixedSize(m_fullScreenButton->sizeHint());
125 connect(m_fullScreenButton, SIGNAL(clicked()),
126 this, SLOT(toggleFullScreen()));
127 #endif // Q_WS_MAEMO_5
131 void MainWindow::buildFriendListPanel()
133 qDebug() << __PRETTY_FUNCTION__;
135 m_friendsListPanel = new FriendListPanel(this);
136 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
138 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
140 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
141 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
143 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
144 this, SIGNAL(findFriend(QPointF)));
146 connect(m_mapView, SIGNAL(viewResized(QSize)),
147 m_friendsListPanel, SLOT(screenResized(QSize)));
149 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
150 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
152 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
153 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
156 void MainWindow::buildInformationBox(const QString &message, bool modal)
158 qDebug() << __PRETTY_FUNCTION__;
161 QMaemo5InformationBox *msgBox = new QMaemo5InformationBox(this);
162 QLabel *label = new QLabel(msgBox);
163 label->setAlignment(Qt::AlignCenter);
164 label->setText(message);
165 msgBox->setWidget(label);
168 msgBox->setTimeout(QMaemo5InformationBox::NoTimeout);
170 msgBox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
173 QMessageBox *msgBox = new QMessageBox(this);
174 msgBox->button(QMessageBox::Ok);
175 msgBox->setText(message);
176 msgBox->setModal(modal);
182 void MainWindow::buildManualLocationCrosshair()
184 qDebug() << __PRETTY_FUNCTION__;
186 m_ownLocationCrosshair = new QLabel(this);
187 QPixmap crosshairImage(":/res/images/sight.png");
188 m_ownLocationCrosshair->setPixmap(crosshairImage);
189 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
190 m_ownLocationCrosshair->hide();
191 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
193 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
194 this, SLOT(drawOwnLocationCrosshair(int, int)));
197 void MainWindow::buildMap()
199 qDebug() << __PRETTY_FUNCTION__;
201 m_mapView = new MapView(this);
203 buildZoomButtonPanel();
205 buildManualLocationCrosshair();
206 buildFullScreenButton();
208 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
209 this, SIGNAL(mapViewScrolled(QPoint)));
211 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
212 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
214 connect(m_mapView, SIGNAL(viewResized(QSize)),
215 this, SIGNAL(mapViewResized(QSize)));
217 connect(m_mapView, SIGNAL(viewResized(QSize)),
218 this, SLOT(drawFullScreenButton(QSize)));
220 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
221 this, SLOT(setViewPortSize(int, int)));
223 connect(this, SIGNAL(zoomLevelChanged(int)),
224 m_mapView, SLOT(setZoomLevel(int)));
226 connect(m_mapView, SIGNAL(viewZoomFinished()),
227 this, SIGNAL(viewZoomFinished()));
230 void MainWindow::buildOsmLicense()
232 qDebug() << __PRETTY_FUNCTION__;
234 m_osmLicense = new QLabel(this);
235 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
236 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
237 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
238 m_osmLicense->setFont(QFont("Nokia Sans", 9));
239 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
240 m_osmLicense->fontMetrics().height());
242 connect(m_mapView, SIGNAL(viewResized(QSize)),
243 this, SLOT(drawOsmLicense(QSize)));
246 void MainWindow::buildUserInfoPanel()
248 qDebug() << __PRETTY_FUNCTION__;
250 m_userPanel = new UserInfoPanel(this);
251 m_userPanelSidebar = new PanelSideBar(this, LEFT);
253 m_userPanelSidebar->stackUnder(m_friendsListPanel);
254 m_userPanel->stackUnder(m_userPanelSidebar);
256 connect(m_userPanel, SIGNAL(findUser(QPointF)),
257 this, SIGNAL(findUser(QPointF)));
259 connect(this, SIGNAL(userLocationReady(User*)),
260 m_userPanel, SLOT(userDataReceived(User*)));
262 connect(m_userPanel, SIGNAL(requestReverseGeo()),
263 this, SIGNAL(requestReverseGeo()));
265 connect(this, SIGNAL(reverseGeoReady(QString)),
266 m_userPanel, SIGNAL(reverseGeoReady(QString)));
268 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
269 this, SIGNAL(statusUpdate(QString,bool)));
271 connect(m_userPanel, SIGNAL(refreshUserData()),
272 this, SIGNAL(refreshUserData()));
274 connect(m_mapView, SIGNAL(viewResized(QSize)),
275 m_userPanel, SLOT(screenResized(QSize)));
278 void MainWindow::buildWebView()
280 qDebug() << __PRETTY_FUNCTION__;
283 m_webView = new QWebView;
285 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
286 this, SIGNAL(updateCredentials(QUrl)));
287 connect(m_webView, SIGNAL(loadFinished(bool)),
288 this, SLOT(loadDone(bool)));
294 void MainWindow::buildZoomButtonPanel()
296 qDebug() << __PRETTY_FUNCTION__;
298 m_zoomButtonPanel = new ZoomButtonPanel(this);
300 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
301 this, SIGNAL(zoomIn()));
303 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
304 this, SIGNAL(zoomOut()));
306 connect(this, SIGNAL(zoomLevelChanged(int)),
307 m_zoomButtonPanel, SLOT(resetButtons()));
309 connect(this, SIGNAL(maxZoomLevelReached()),
310 m_zoomButtonPanel, SLOT(disableZoomInButton()));
312 connect(this, SIGNAL(minZoomLevelReached()),
313 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
315 connect(m_mapView, SIGNAL(viewResized(QSize)),
316 m_zoomButtonPanel, SLOT(screenResized(QSize)));
319 void MainWindow::clearCookieJar()
321 qDebug() << __PRETTY_FUNCTION__;
326 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
328 QList<QNetworkCookie> emptyList;
331 m_cookieJar->setAllCookies(emptyList);
332 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
335 void MainWindow::createMenus()
337 qDebug() << __PRETTY_FUNCTION__;
340 m_loginAct = new QAction(tr("Login"), this);
341 connect(m_loginAct, SIGNAL(triggered()),
342 this, SIGNAL(loginActionPressed()));
345 m_toSettingsAct = new QAction(tr("Settings"), this);
346 connect(m_toSettingsAct, SIGNAL(triggered()),
347 this, SLOT(openSettingsDialog()));
350 m_gpsToggleAct = new QAction(tr("GPS"), this);
351 m_gpsToggleAct->setCheckable(true);
353 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
354 this, SIGNAL(gpsTriggered(bool)));
356 // automatic centering
357 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
358 m_autoCenteringAct->setCheckable(true);
359 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
360 this, SIGNAL(autoCenteringTriggered(bool)));
362 // build the actual menu
363 m_viewMenu = menuBar()->addMenu(tr("Main"));
364 m_viewMenu->addAction(m_loginAct);
365 m_viewMenu->addAction(m_toSettingsAct);
366 m_viewMenu->addAction(m_gpsToggleAct);
367 m_viewMenu->addAction(m_autoCenteringAct);
368 m_viewMenu->setObjectName(tr("Menu"));
371 void MainWindow::dialogFinished(int status)
373 qDebug() << __PRETTY_FUNCTION__;
375 QDialog *dialog = m_queue.takeFirst();
376 if(!dialog->objectName().isEmpty()) {
379 static_cast<LoginDialog *>(dialog)->userInput(m_email, m_password);
382 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
384 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
386 QStringList urlParts;
387 urlParts.append(FACEBOOK_LOGINBASE);
388 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
389 urlParts.append(INTERVAL1);
390 urlParts.append(SITUARE_LOGIN_SUCCESS);
391 urlParts.append(INTERVAL2);
392 urlParts.append(SITUARE_LOGIN_FAILURE);
393 urlParts.append(FACEBOOK_LOGIN_ENDING);
395 emit saveUsername(m_email);
396 m_webView->load(QUrl(urlParts.join(EMPTY)));
397 toggleProgressIndicator(true);
401 emit cancelLoginProcess();
405 dialog->deleteLater();
407 if(!m_queue.isEmpty())
408 showInformationBox();
411 void MainWindow::drawFullScreenButton(const QSize &size)
413 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
415 if(m_fullScreenButton) {
416 m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
418 size.height() - m_fullScreenButton->size().height());
422 void MainWindow::drawOsmLicense(const QSize &size)
424 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
426 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
428 size.height() - m_osmLicense->fontMetrics().height());
431 void MainWindow::drawOwnLocationCrosshair(int width, int height)
433 qDebug() << __PRETTY_FUNCTION__;
435 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
436 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
437 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
441 void MainWindow::gpsTimeout()
443 qDebug() << __PRETTY_FUNCTION__;
445 buildInformationBox(tr("GPS timeout"));
448 void MainWindow::grabZoomKeys(bool grab)
450 qDebug() << __PRETTY_FUNCTION__;
453 // Can't grab keys unless we have a window id
457 unsigned long val = (grab) ? 1 : 0;
458 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
462 XChangeProperty (QX11Info::display(),
468 reinterpret_cast<unsigned char *>(&val),
472 #endif // Q_WS_MAEMO_5
475 void MainWindow::keyPressEvent(QKeyEvent* event)
477 qDebug() << __PRETTY_FUNCTION__;
479 switch (event->key()) {
490 QWidget::keyPressEvent(event);
493 void MainWindow::loadCookies()
495 qDebug() << __PRETTY_FUNCTION__;
497 QSettings settings(DIRECTORY_NAME, FILE_NAME);
499 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
501 if(!list.isEmpty()) {
502 QList<QNetworkCookie> cookieList;
503 for(int i=0;i<list.count();i++) {
504 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
508 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
510 m_cookieJar->setAllCookies(cookieList);
511 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
516 void MainWindow::loadDone(bool done)
518 qDebug() << __PRETTY_FUNCTION__;
520 // for the first time the login page is opened, we need to refresh it to get cookies working
528 QWebFrame* frame = m_webView->page()->currentFrame();
532 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
534 foreach (QWebElement element, emailCollection) {
535 element.setAttribute("value", m_email.toAscii());
538 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
539 foreach (QWebElement element, passwordCollection) {
540 element.setAttribute("value", m_password.toAscii());
542 // find connect button
543 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
544 foreach (QWebElement element, buttonCollection)
546 QPoint pos(element.geometry().center());
548 // send a mouse click event to the web page
549 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
551 QApplication::sendEvent(m_webView->page(), &event0);
552 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
554 QApplication::sendEvent(m_webView->page(), &event1);
560 void MainWindow::loggedIn(bool logged)
562 qDebug() << __PRETTY_FUNCTION__;
567 m_loginAct->setText(tr("Logout"));
573 m_loginAct->setText(tr("Login"));
575 updateItemVisibility(m_loggedIn);
578 void MainWindow::loginFailed()
580 qDebug() << __PRETTY_FUNCTION__;
584 toggleProgressIndicator(false);
589 void MainWindow::loginUsingCookies()
591 qDebug() << __PRETTY_FUNCTION__;
596 QStringList urlParts;
597 urlParts.append(FACEBOOK_LOGINBASE);
598 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
599 urlParts.append(INTERVAL1);
600 urlParts.append(SITUARE_LOGIN_SUCCESS);
601 urlParts.append(INTERVAL2);
602 urlParts.append(SITUARE_LOGIN_FAILURE);
603 urlParts.append(FACEBOOK_LOGIN_ENDING);
605 m_webView->load(QUrl(urlParts.join(EMPTY)));
609 void MainWindow::openSettingsDialog()
611 qDebug() << __PRETTY_FUNCTION__;
613 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
614 m_settingsDialog->show();
617 void MainWindow::queueDialog(QDialog *dialog)
619 qDebug() << __PRETTY_FUNCTION__;
621 if(dialog->isModal()) {
622 m_queue.insert(0, dialog);
624 m_queue.append(dialog);
627 if(m_queue.count() == 1)
628 this->showInformationBox();
631 void MainWindow::saveCookies()
633 qDebug() << __PRETTY_FUNCTION__;
636 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
638 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
641 for(int i=0;i<cookieList.count();i++) {
642 QNetworkCookie cookie = cookieList.at(i);
643 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
644 list.append(QString(byteArray));
646 list.removeDuplicates();
648 QSettings settings(DIRECTORY_NAME, FILE_NAME);
649 settings.setValue(COOKIES, list);
652 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
654 qDebug() << __PRETTY_FUNCTION__;
656 m_autoCenteringAct->setChecked(enabled);
659 void MainWindow::setGPSButtonEnabled(bool enabled)
661 qDebug() << __PRETTY_FUNCTION__;
663 m_gpsToggleAct->setChecked(enabled);
666 setOwnLocationCrosshairVisibility(!enabled);
668 m_autoCenteringAct->setVisible(enabled);
671 void MainWindow::setMapViewScene(QGraphicsScene *scene)
673 qDebug() << __PRETTY_FUNCTION__;
675 m_mapView->setScene(scene);
678 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
680 qDebug() << __PRETTY_FUNCTION__;
683 m_ownLocationCrosshair->show();
684 m_drawOwnLocationCrosshair = true;
685 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
687 m_ownLocationCrosshair->hide();
688 m_drawOwnLocationCrosshair = false;
692 void MainWindow::setUsername(const QString &username)
694 qDebug() << __PRETTY_FUNCTION__;
699 void MainWindow::setViewPortSize(int width, int height)
701 qDebug() << __PRETTY_FUNCTION__;
703 m_viewPortWidth = width;
704 m_viewPortHeight = height;
707 void MainWindow::toggleFullScreen()
709 qDebug() << __PRETTY_FUNCTION__;
711 if(windowState() == Qt::WindowNoState)
717 void MainWindow::showInformationBox()
719 qDebug() << __PRETTY_FUNCTION__;
721 QDialog *dialog = m_queue.takeFirst();
722 connect(dialog, SIGNAL(finished(int)),
723 this, SLOT(dialogFinished(int)));
725 m_queue.insert(0, dialog);
728 void MainWindow::startLoginProcess()
730 qDebug() << __PRETTY_FUNCTION__;
732 LoginDialog *loginDialog = new LoginDialog();
734 loginDialog->setObjectName(QString("loginDialog"));
736 emit fetchUsernameFromSettings();
738 loginDialog->clearTextFields();
740 if(!m_email.isEmpty())
741 loginDialog->setEmailField(m_email);
743 queueDialog(loginDialog);
746 void MainWindow::toggleProgressIndicator(bool value)
748 qDebug() << __PRETTY_FUNCTION__;
751 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
754 #endif // Q_WS_MAEMO_5
757 void MainWindow::updateItemVisibility(bool show)
759 qDebug() << __PRETTY_FUNCTION__;
762 m_friendsListPanel->show();
763 m_friendsListPanelSidebar->show();
765 m_userPanelSidebar->show();
767 if(m_drawOwnLocationCrosshair) {
768 m_ownLocationCrosshair->show();
769 setGPSButtonEnabled(false);
770 emit gpsTriggered(false);
773 m_friendsListPanel->closePanel();
774 m_friendsListPanel->hide();
775 m_friendsListPanelSidebar->hide();
776 m_userPanel->closePanel();
778 m_userPanelSidebar->hide();
779 m_ownLocationCrosshair->hide();
783 const QString MainWindow::username()
785 qDebug() << __PRETTY_FUNCTION__;