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 "facebookservice/facebookauthentication.h"
31 #include "friendlistpanel.h"
32 #include "logindialog.h"
33 #include "map/mapview.h"
34 #include "settingsdialog.h"
35 #include "userinfopanel.h"
36 #include "zoombuttonpanel.h"
38 #include "mainwindow.h"
40 // These MUST BE HERE, compiling for Maemo fails if moved
42 #include <QtMaemo5/QMaemo5InformationBox>
43 #include <QtGui/QX11Info>
44 #include <X11/Xatom.h>
46 #endif // Q_WS_MAEMO_5
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::buildWebView()
224 qDebug() << __PRETTY_FUNCTION__;
227 m_webView = new QWebView;
229 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
230 this, SIGNAL(updateCredentials(QUrl)));
231 connect(m_webView, SIGNAL(loadFinished(bool)),
232 this, SLOT(loadDone(bool)));
238 void MainWindow::buildZoomButtonPanel()
240 qDebug() << __PRETTY_FUNCTION__;
242 m_zoomButtonPanel = new ZoomButtonPanel(this);
244 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
245 this, SIGNAL(zoomIn()));
247 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
248 this, SIGNAL(zoomOut()));
250 connect(this, SIGNAL(zoomLevelChanged(int)),
251 m_zoomButtonPanel, SLOT(resetButtons()));
253 connect(this, SIGNAL(maxZoomLevelReached()),
254 m_zoomButtonPanel, SLOT(disableZoomInButton()));
256 connect(this, SIGNAL(minZoomLevelReached()),
257 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
259 connect(m_mapView, SIGNAL(viewResized(QSize)),
260 m_zoomButtonPanel, SLOT(screenResized(QSize)));
263 void MainWindow::clearCookieJar()
265 qDebug() << __PRETTY_FUNCTION__;
270 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
272 QList<QNetworkCookie> emptyList;
275 m_cookieJar->setAllCookies(emptyList);
276 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
279 void MainWindow::createMenus()
281 qDebug() << __PRETTY_FUNCTION__;
284 m_loginAct = new QAction(tr("Login"), this);
285 connect(m_loginAct, SIGNAL(triggered()),
286 this, SIGNAL(loginActionPressed()));
289 m_toSettingsAct = new QAction(tr("Settings"), this);
290 connect(m_toSettingsAct, SIGNAL(triggered()),
291 this, SLOT(openSettingsDialog()));
294 m_gpsToggleAct = new QAction(tr("GPS"), this);
295 m_gpsToggleAct->setCheckable(true);
297 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
298 this, SIGNAL(gpsTriggered(bool)));
300 // automatic centering
301 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
302 m_autoCenteringAct->setCheckable(true);
303 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
304 this, SIGNAL(autoCenteringTriggered(bool)));
306 // build the actual menu
307 m_viewMenu = menuBar()->addMenu(tr("Main"));
308 m_viewMenu->addAction(m_loginAct);
309 m_viewMenu->addAction(m_toSettingsAct);
310 m_viewMenu->addAction(m_gpsToggleAct);
311 m_viewMenu->addAction(m_autoCenteringAct);
312 m_viewMenu->setObjectName(tr("Menu"));
315 void MainWindow::drawOsmLicense(const QSize &size)
317 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
319 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
321 size.height() - m_osmLicense->fontMetrics().height());
325 void MainWindow::drawOwnLocationCrosshair(int width, int height)
327 qDebug() << __PRETTY_FUNCTION__;
329 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
330 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
331 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
335 void MainWindow::gpsError(const QString &message)
337 qDebug() << __PRETTY_FUNCTION__;
339 showMaemoInformationBox(message);
342 void MainWindow::gpsTimeout()
344 qDebug() << __PRETTY_FUNCTION__;
346 showMaemoInformationBox(tr("GPS timeout"));
349 void MainWindow::grabZoomKeys(bool grab)
351 qDebug() << __PRETTY_FUNCTION__;
354 // Can't grab keys unless we have a window id
358 unsigned long val = (grab) ? 1 : 0;
359 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
363 XChangeProperty (QX11Info::display(),
369 reinterpret_cast<unsigned char *>(&val),
373 #endif // Q_WS_MAEMO_5
376 void MainWindow::keyPressEvent(QKeyEvent* event)
378 qDebug() << __PRETTY_FUNCTION__;
380 switch (event->key()) {
391 QWidget::keyPressEvent(event);
394 void MainWindow::loadCookies()
396 qDebug() << __PRETTY_FUNCTION__;
398 QSettings settings(DIRECTORY_NAME, FILE_NAME);
400 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
402 if(!list.isEmpty()) {
403 QList<QNetworkCookie> cookieList;
404 for(int i=0;i<list.count();i++) {
405 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
409 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
411 m_cookieJar->setAllCookies(cookieList);
412 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
417 void MainWindow::loadDone(bool done)
419 qDebug() << __PRETTY_FUNCTION__;
421 // for the first time the login page is opened, we need to refresh it to get cookies working
429 QWebFrame* frame = m_webView->page()->currentFrame();
433 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
435 foreach (QWebElement element, emailCollection) {
436 element.setAttribute("value", m_email.toAscii());
439 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
440 foreach (QWebElement element, passwordCollection) {
441 element.setAttribute("value", m_password.toAscii());
443 // find connect button
444 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
445 foreach (QWebElement element, buttonCollection)
447 QPoint pos(element.geometry().center());
449 // send a mouse click event to the web page
450 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
452 QApplication::sendEvent(m_webView->page(), &event0);
453 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
455 QApplication::sendEvent(m_webView->page(), &event1);
461 void MainWindow::loggedIn(bool logged)
463 qDebug() << __PRETTY_FUNCTION__;
468 m_loginAct->setText(tr("Logout"));
475 m_loginAct->setText(tr("Login"));
477 updateItemVisibility(m_loggedIn);
480 void MainWindow::loginFailed()
482 qDebug() << __PRETTY_FUNCTION__;
486 toggleProgressIndicator(false);
488 showMaemoInformationBox(tr("Invalid E-mail address or password"), true);
490 QStringList urlParts;
491 urlParts.append(FACEBOOK_LOGINBASE);
492 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
493 urlParts.append(INTERVAL1);
494 urlParts.append(SITUARE_LOGIN_SUCCESS);
495 urlParts.append(INTERVAL2);
496 urlParts.append(SITUARE_LOGIN_FAILURE);
497 urlParts.append(FACEBOOK_LOGIN_ENDING);
499 startLoginProcess(urlParts.join(EMPTY));
502 void MainWindow::loginUsingCookies()
504 qDebug() << __PRETTY_FUNCTION__;
509 QStringList urlParts;
510 urlParts.append(FACEBOOK_LOGINBASE);
511 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
512 urlParts.append(INTERVAL1);
513 urlParts.append(SITUARE_LOGIN_SUCCESS);
514 urlParts.append(INTERVAL2);
515 urlParts.append(SITUARE_LOGIN_FAILURE);
516 urlParts.append(FACEBOOK_LOGIN_ENDING);
518 m_webView->load(QUrl(urlParts.join(EMPTY)));
522 void MainWindow::openSettingsDialog()
524 qDebug() << __PRETTY_FUNCTION__;
526 SettingsDialog *dialog = new SettingsDialog(this);
528 dialog->disableSituareSettings();
533 void MainWindow::saveCookies()
535 qDebug() << __PRETTY_FUNCTION__;
538 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
540 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
543 for(int i=0;i<cookieList.count();i++) {
544 QNetworkCookie cookie = cookieList.at(i);
545 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
546 list.append(QString(byteArray));
548 list.removeDuplicates();
550 QSettings settings(DIRECTORY_NAME, FILE_NAME);
551 settings.setValue(COOKIES, list);
554 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
556 qDebug() << __PRETTY_FUNCTION__;
558 m_autoCenteringAct->setChecked(enabled);
561 void MainWindow::setGPSButtonEnabled(bool enabled)
563 qDebug() << __PRETTY_FUNCTION__;
565 m_gpsToggleAct->setChecked(enabled);
568 setOwnLocationCrosshairVisibility(!enabled);
570 m_autoCenteringAct->setVisible(enabled);
573 void MainWindow::setMapViewScene(QGraphicsScene *scene)
575 qDebug() << __PRETTY_FUNCTION__;
577 m_mapView->setScene(scene);
580 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
582 qDebug() << __PRETTY_FUNCTION__;
585 m_ownLocationCrosshair->show();
586 m_drawOwnLocationCrosshair = true;
587 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
590 m_ownLocationCrosshair->hide();
591 m_drawOwnLocationCrosshair = false;
595 void MainWindow::setUsername(const QString &username)
597 qDebug() << __PRETTY_FUNCTION__;
602 void MainWindow::setViewPortSize(int width, int height)
604 qDebug() << __PRETTY_FUNCTION__;
606 m_viewPortWidth = width;
607 m_viewPortHeight = height;
610 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
612 qDebug() << __PRETTY_FUNCTION__;
616 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
619 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
623 QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
627 void MainWindow::startLoginProcess(const QUrl &url)
629 qDebug() << __PRETTY_FUNCTION__;
633 LoginDialog loginDialog;
635 emit fetchUsernameFromSettings();
638 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
640 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
642 loginDialog.clearTextFields();
644 if(!m_email.isEmpty())
645 loginDialog.setEmailField(m_email);
647 if(loginDialog.exec() != QDialog::Accepted) {
648 // if login dialog was canceled we need to stop processing webview
651 emit cancelLoginProcess();
654 loginDialog.userInput(m_email, m_password);
655 m_webView->load(url);
656 toggleProgressIndicator(true);
661 void MainWindow::toggleProgressIndicator(bool value)
663 qDebug() << __PRETTY_FUNCTION__;
666 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
669 #endif // Q_WS_MAEMO_5
672 void MainWindow::updateItemVisibility(bool show)
674 qDebug() << __PRETTY_FUNCTION__;
677 m_friendsListPanel->show();
678 m_friendsListPanelSidebar->show();
680 m_userPanelSidebar->show();
682 if(m_drawOwnLocationCrosshair) {
683 m_ownLocationCrosshair->show();
684 setGPSButtonEnabled(false);
685 emit gpsTriggered(false);
689 m_friendsListPanel->closePanel();
690 m_friendsListPanel->hide();
691 m_friendsListPanelSidebar->hide();
692 m_userPanel->closePanel();
694 m_userPanelSidebar->hide();
695 m_ownLocationCrosshair->hide();
699 const QString MainWindow::username()
701 qDebug() << __PRETTY_FUNCTION__;