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),
57 m_ownLocationCrosshair(0),
60 m_fullScreenButton(0),
64 qDebug() << __PRETTY_FUNCTION__;
69 QHBoxLayout *layout = new QHBoxLayout;
70 layout->addWidget(m_mapView);
72 setCentralWidget(new QWidget());
73 centralWidget()->setLayout(layout);
75 buildFriendListPanel();
78 m_settingsDialog = new SettingsDialog(this);
79 m_settingsDialog->hide();
80 connect(m_settingsDialog, SIGNAL(enableAutomaticLocationUpdate(bool,int)),
81 this, SIGNAL(enableAutomaticLocationUpdate(bool,int)));
84 setWindowTitle(tr("Situare"));
86 // set stacking order of widgets
87 m_zoomButtonPanel->stackUnder(m_userPanel);
88 if(m_fullScreenButton) {
89 m_fullScreenButton->stackUnder(m_zoomButtonPanel);
90 m_osmLicense->stackUnder(m_fullScreenButton);
92 m_osmLicense->stackUnder(m_zoomButtonPanel);
93 m_ownLocationCrosshair->stackUnder(m_osmLicense);
94 m_mapView->stackUnder(m_ownLocationCrosshair);
96 this->toggleProgressIndicator(true);
100 // set screen size in desktop matching N900 screen size
101 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
104 MainWindow::~MainWindow()
106 qDebug() << __PRETTY_FUNCTION__;
114 void MainWindow::buildFullScreenButton()
116 qDebug() << __PRETTY_FUNCTION__;
118 m_fullScreenButton = new QToolButton(this);
119 m_fullScreenButton->setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
120 m_fullScreenButton->setFixedSize(m_fullScreenButton->sizeHint());
121 connect(m_fullScreenButton, SIGNAL(clicked()),
122 this, SLOT(toggleFullScreen()));
123 #endif // Q_WS_MAEMO_5
127 void MainWindow::buildFriendListPanel()
129 qDebug() << __PRETTY_FUNCTION__;
131 m_friendsListPanel = new FriendListPanel(this);
132 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
134 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
136 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
137 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
139 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
140 this, SIGNAL(findFriend(QPointF)));
142 connect(m_mapView, SIGNAL(viewResized(QSize)),
143 m_friendsListPanel, SLOT(screenResized(QSize)));
145 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
146 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
148 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
149 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
152 void MainWindow::buildManualLocationCrosshair()
154 qDebug() << __PRETTY_FUNCTION__;
156 m_ownLocationCrosshair = new QLabel(this);
157 QPixmap crosshairImage(":/res/images/sight.png");
158 m_ownLocationCrosshair->setPixmap(crosshairImage);
159 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
160 m_ownLocationCrosshair->hide();
161 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
163 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
164 this, SLOT(drawOwnLocationCrosshair(int, int)));
167 void MainWindow::buildMap()
169 qDebug() << __PRETTY_FUNCTION__;
171 m_mapView = new MapView(this);
173 buildZoomButtonPanel();
175 buildManualLocationCrosshair();
176 buildFullScreenButton();
178 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
179 this, SIGNAL(mapViewScrolled(QPoint)));
181 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
182 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
184 connect(m_mapView, SIGNAL(viewResized(QSize)),
185 this, SIGNAL(mapViewResized(QSize)));
187 connect(m_mapView, SIGNAL(viewResized(QSize)),
188 this, SLOT(drawFullScreenButton(QSize)));
190 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
191 this, SLOT(setViewPortSize(int, int)));
193 connect(this, SIGNAL(zoomLevelChanged(int)),
194 m_mapView, SLOT(setZoomLevel(int)));
196 connect(m_mapView, SIGNAL(viewZoomFinished()),
197 this, SIGNAL(viewZoomFinished()));
200 void MainWindow::buildOsmLicense()
202 qDebug() << __PRETTY_FUNCTION__;
204 m_osmLicense = new QLabel(this);
205 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
206 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
207 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
208 m_osmLicense->setFont(QFont("Nokia Sans", 9));
209 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
210 m_osmLicense->fontMetrics().height());
212 connect(m_mapView, SIGNAL(viewResized(QSize)),
213 this, SLOT(drawOsmLicense(QSize)));
216 void MainWindow::buildUserInfoPanel()
218 qDebug() << __PRETTY_FUNCTION__;
220 m_userPanel = new UserInfoPanel(this);
221 m_userPanelSidebar = new PanelSideBar(this, LEFT);
223 m_userPanelSidebar->stackUnder(m_friendsListPanel);
224 m_userPanel->stackUnder(m_userPanelSidebar);
226 connect(m_userPanel, SIGNAL(findUser(QPointF)),
227 this, SIGNAL(findUser(QPointF)));
229 connect(this, SIGNAL(userLocationReady(User*)),
230 m_userPanel, SLOT(userDataReceived(User*)));
232 connect(m_userPanel, SIGNAL(requestReverseGeo()),
233 this, SIGNAL(requestReverseGeo()));
235 connect(this, SIGNAL(reverseGeoReady(QString)),
236 m_userPanel, SIGNAL(reverseGeoReady(QString)));
238 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
239 this, SIGNAL(statusUpdate(QString,bool)));
241 connect(m_userPanel, SIGNAL(refreshUserData()),
242 this, SIGNAL(refreshUserData()));
244 connect(m_mapView, SIGNAL(viewResized(QSize)),
245 m_userPanel, SLOT(screenResized(QSize)));
247 connect(this, SIGNAL(messageUpdatedToSituare()),
248 m_userPanel, SIGNAL(messageUpdatedToSituare()));
250 connect(this, SIGNAL(messageSendingFailed(QString)),
251 m_userPanel, SIGNAL(messageSendingFailed(QString)));
254 void MainWindow::buildWebView()
256 qDebug() << __PRETTY_FUNCTION__;
259 m_webView = new QWebView;
261 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
262 this, SIGNAL(updateCredentials(QUrl)));
263 connect(m_webView, SIGNAL(loadFinished(bool)),
264 this, SLOT(loadDone(bool)));
270 void MainWindow::buildZoomButtonPanel()
272 qDebug() << __PRETTY_FUNCTION__;
274 m_zoomButtonPanel = new ZoomButtonPanel(this);
276 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
277 this, SIGNAL(zoomIn()));
279 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
280 this, SIGNAL(zoomOut()));
282 connect(this, SIGNAL(zoomLevelChanged(int)),
283 m_zoomButtonPanel, SLOT(resetButtons()));
285 connect(this, SIGNAL(maxZoomLevelReached()),
286 m_zoomButtonPanel, SLOT(disableZoomInButton()));
288 connect(this, SIGNAL(minZoomLevelReached()),
289 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
291 connect(m_mapView, SIGNAL(viewResized(QSize)),
292 m_zoomButtonPanel, SLOT(screenResized(QSize)));
295 void MainWindow::clearCookieJar()
297 qDebug() << __PRETTY_FUNCTION__;
302 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
304 QList<QNetworkCookie> emptyList;
307 m_cookieJar->setAllCookies(emptyList);
308 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
311 void MainWindow::createMenus()
313 qDebug() << __PRETTY_FUNCTION__;
316 m_loginAct = new QAction(tr("Login"), this);
317 connect(m_loginAct, SIGNAL(triggered()),
318 this, SIGNAL(loginActionPressed()));
321 m_toSettingsAct = new QAction(tr("Settings"), this);
322 connect(m_toSettingsAct, SIGNAL(triggered()),
323 this, SLOT(openSettingsDialog()));
326 m_gpsToggleAct = new QAction(tr("GPS"), this);
327 m_gpsToggleAct->setCheckable(true);
329 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
330 this, SIGNAL(gpsTriggered(bool)));
332 // automatic centering
333 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
334 m_autoCenteringAct->setCheckable(true);
335 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
336 this, SIGNAL(autoCenteringTriggered(bool)));
338 // build the actual menu
339 m_viewMenu = menuBar()->addMenu(tr("Main"));
340 m_viewMenu->addAction(m_loginAct);
341 m_viewMenu->addAction(m_toSettingsAct);
342 m_viewMenu->addAction(m_gpsToggleAct);
343 m_viewMenu->addAction(m_autoCenteringAct);
344 m_viewMenu->setObjectName(tr("Menu"));
347 void MainWindow::drawFullScreenButton(const QSize &size)
349 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
351 if(m_fullScreenButton) {
352 m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
354 size.height() - m_fullScreenButton->size().height());
358 void MainWindow::drawOsmLicense(const QSize &size)
360 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
362 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
364 size.height() - m_osmLicense->fontMetrics().height());
367 void MainWindow::drawOwnLocationCrosshair(int width, int height)
369 qDebug() << __PRETTY_FUNCTION__;
371 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
372 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
373 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
377 void MainWindow::gpsTimeout()
379 qDebug() << __PRETTY_FUNCTION__;
381 showMaemoInformationBox(tr("GPS timeout"));
384 void MainWindow::grabZoomKeys(bool grab)
386 qDebug() << __PRETTY_FUNCTION__;
389 // Can't grab keys unless we have a window id
393 unsigned long val = (grab) ? 1 : 0;
394 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
398 XChangeProperty (QX11Info::display(),
404 reinterpret_cast<unsigned char *>(&val),
408 #endif // Q_WS_MAEMO_5
411 void MainWindow::keyPressEvent(QKeyEvent* event)
413 qDebug() << __PRETTY_FUNCTION__;
415 switch (event->key()) {
426 QWidget::keyPressEvent(event);
429 void MainWindow::loadCookies()
431 qDebug() << __PRETTY_FUNCTION__;
433 QSettings settings(DIRECTORY_NAME, FILE_NAME);
435 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
437 if(!list.isEmpty()) {
438 QList<QNetworkCookie> cookieList;
439 for(int i=0;i<list.count();i++) {
440 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
444 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
446 m_cookieJar->setAllCookies(cookieList);
447 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
452 void MainWindow::loadDone(bool done)
454 qDebug() << __PRETTY_FUNCTION__;
456 // for the first time the login page is opened, we need to refresh it to get cookies working
464 QWebFrame* frame = m_webView->page()->currentFrame();
468 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
470 foreach (QWebElement element, emailCollection) {
471 element.setAttribute("value", m_email.toAscii());
474 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
475 foreach (QWebElement element, passwordCollection) {
476 element.setAttribute("value", m_password.toAscii());
478 // find connect button
479 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
480 foreach (QWebElement element, buttonCollection)
482 QPoint pos(element.geometry().center());
484 // send a mouse click event to the web page
485 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
487 QApplication::sendEvent(m_webView->page(), &event0);
488 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
490 QApplication::sendEvent(m_webView->page(), &event1);
496 void MainWindow::loggedIn(bool logged)
498 qDebug() << __PRETTY_FUNCTION__;
503 m_loginAct->setText(tr("Logout"));
510 m_loginAct->setText(tr("Login"));
512 updateItemVisibility(m_loggedIn);
515 void MainWindow::loginFailed()
517 qDebug() << __PRETTY_FUNCTION__;
521 toggleProgressIndicator(false);
523 QStringList urlParts;
524 urlParts.append(FACEBOOK_LOGINBASE);
525 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
526 urlParts.append(INTERVAL1);
527 urlParts.append(SITUARE_LOGIN_SUCCESS);
528 urlParts.append(INTERVAL2);
529 urlParts.append(SITUARE_LOGIN_FAILURE);
530 urlParts.append(FACEBOOK_LOGIN_ENDING);
532 startLoginProcess(urlParts.join(EMPTY));
535 void MainWindow::loginUsingCookies()
537 qDebug() << __PRETTY_FUNCTION__;
542 QStringList urlParts;
543 urlParts.append(FACEBOOK_LOGINBASE);
544 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
545 urlParts.append(INTERVAL1);
546 urlParts.append(SITUARE_LOGIN_SUCCESS);
547 urlParts.append(INTERVAL2);
548 urlParts.append(SITUARE_LOGIN_FAILURE);
549 urlParts.append(FACEBOOK_LOGIN_ENDING);
551 m_webView->load(QUrl(urlParts.join(EMPTY)));
555 void MainWindow::openSettingsDialog()
557 qDebug() << __PRETTY_FUNCTION__;
559 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
560 m_settingsDialog->show();
563 void MainWindow::saveCookies()
565 qDebug() << __PRETTY_FUNCTION__;
568 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
570 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
573 for(int i=0;i<cookieList.count();i++) {
574 QNetworkCookie cookie = cookieList.at(i);
575 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
576 list.append(QString(byteArray));
578 list.removeDuplicates();
580 QSettings settings(DIRECTORY_NAME, FILE_NAME);
581 settings.setValue(COOKIES, list);
584 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
586 qDebug() << __PRETTY_FUNCTION__;
588 m_autoCenteringAct->setChecked(enabled);
591 void MainWindow::setGPSButtonEnabled(bool enabled)
593 qDebug() << __PRETTY_FUNCTION__;
595 m_gpsToggleAct->setChecked(enabled);
598 setOwnLocationCrosshairVisibility(!enabled);
600 m_autoCenteringAct->setVisible(enabled);
603 void MainWindow::setMapViewScene(QGraphicsScene *scene)
605 qDebug() << __PRETTY_FUNCTION__;
607 m_mapView->setScene(scene);
610 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
612 qDebug() << __PRETTY_FUNCTION__;
615 m_ownLocationCrosshair->show();
616 m_drawOwnLocationCrosshair = true;
617 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
620 m_ownLocationCrosshair->hide();
621 m_drawOwnLocationCrosshair = false;
625 void MainWindow::setUsername(const QString &username)
627 qDebug() << __PRETTY_FUNCTION__;
632 void MainWindow::setViewPortSize(int width, int height)
634 qDebug() << __PRETTY_FUNCTION__;
636 m_viewPortWidth = width;
637 m_viewPortHeight = height;
640 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
642 qDebug() << __PRETTY_FUNCTION__;
646 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
649 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
653 QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
657 void MainWindow::toggleFullScreen()
659 qDebug() << __PRETTY_FUNCTION__;
661 if(windowState() == Qt::WindowNoState)
667 void MainWindow::startLoginProcess(const QUrl &url)
669 qDebug() << __PRETTY_FUNCTION__;
673 LoginDialog loginDialog;
675 emit fetchUsernameFromSettings();
678 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
680 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
682 loginDialog.clearTextFields();
684 if(!m_email.isEmpty())
685 loginDialog.setEmailField(m_email);
687 if(loginDialog.exec() != QDialog::Accepted) {
688 // if login dialog was canceled we need to stop processing webview
691 emit cancelLoginProcess();
694 loginDialog.userInput(m_email, m_password);
695 emit saveUsername(m_email);
696 m_webView->load(url);
697 toggleProgressIndicator(true);
702 void MainWindow::toggleProgressIndicator(bool value)
704 qDebug() << __PRETTY_FUNCTION__;
707 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
710 #endif // Q_WS_MAEMO_5
713 void MainWindow::updateItemVisibility(bool show)
715 qDebug() << __PRETTY_FUNCTION__;
718 m_friendsListPanel->show();
719 m_friendsListPanelSidebar->show();
721 m_userPanelSidebar->show();
723 if(m_drawOwnLocationCrosshair) {
724 m_ownLocationCrosshair->show();
725 setGPSButtonEnabled(false);
726 emit gpsTriggered(false);
730 m_friendsListPanel->closePanel();
731 m_friendsListPanel->hide();
732 m_friendsListPanelSidebar->hide();
733 m_userPanel->closePanel();
735 m_userPanelSidebar->hide();
736 m_ownLocationCrosshair->hide();
740 const QString MainWindow::username()
742 qDebug() << __PRETTY_FUNCTION__;