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;
286 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
288 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
290 connect(m_webView->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)),
291 this, SLOT(webViewRequestFinished(QNetworkReply*)));
292 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
293 this, SIGNAL(updateCredentials(QUrl)));
294 connect(m_webView, SIGNAL(loadFinished(bool)),
295 this, SLOT(loadDone(bool)));
301 void MainWindow::buildZoomButtonPanel()
303 qDebug() << __PRETTY_FUNCTION__;
305 m_zoomButtonPanel = new ZoomButtonPanel(this);
307 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
308 this, SIGNAL(zoomIn()));
310 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
311 this, SIGNAL(zoomOut()));
313 connect(this, SIGNAL(zoomLevelChanged(int)),
314 m_zoomButtonPanel, SLOT(resetButtons()));
316 connect(this, SIGNAL(maxZoomLevelReached()),
317 m_zoomButtonPanel, SLOT(disableZoomInButton()));
319 connect(this, SIGNAL(minZoomLevelReached()),
320 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
322 connect(m_mapView, SIGNAL(viewResized(QSize)),
323 m_zoomButtonPanel, SLOT(screenResized(QSize)));
326 void MainWindow::clearCookieJar()
328 qDebug() << __PRETTY_FUNCTION__;
333 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
335 QList<QNetworkCookie> emptyList;
338 m_cookieJar->setAllCookies(emptyList);
339 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
342 void MainWindow::createMenus()
344 qDebug() << __PRETTY_FUNCTION__;
347 m_loginAct = new QAction(tr("Login"), this);
348 connect(m_loginAct, SIGNAL(triggered()),
349 this, SIGNAL(loginActionPressed()));
352 m_toSettingsAct = new QAction(tr("Settings"), this);
353 connect(m_toSettingsAct, SIGNAL(triggered()),
354 this, SLOT(openSettingsDialog()));
357 m_gpsToggleAct = new QAction(tr("GPS"), this);
358 m_gpsToggleAct->setCheckable(true);
360 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
361 this, SIGNAL(gpsTriggered(bool)));
363 // automatic centering
364 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
365 m_autoCenteringAct->setCheckable(true);
366 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
367 this, SIGNAL(autoCenteringTriggered(bool)));
369 // build the actual menu
370 m_viewMenu = menuBar()->addMenu(tr("Main"));
371 m_viewMenu->addAction(m_loginAct);
372 m_viewMenu->addAction(m_toSettingsAct);
373 m_viewMenu->addAction(m_gpsToggleAct);
374 m_viewMenu->addAction(m_autoCenteringAct);
375 m_viewMenu->setObjectName(tr("Menu"));
378 void MainWindow::dialogFinished(int status)
380 qDebug() << __PRETTY_FUNCTION__;
382 QDialog *dialog = m_queue.takeFirst();
383 if(!dialog->objectName().isEmpty()) {
386 static_cast<LoginDialog *>(dialog)->userInput(m_email, m_password);
388 QStringList urlParts;
389 urlParts.append(FACEBOOK_LOGINBASE);
390 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
391 urlParts.append(INTERVAL1);
392 urlParts.append(SITUARE_LOGIN_SUCCESS);
393 urlParts.append(INTERVAL2);
394 urlParts.append(SITUARE_LOGIN_FAILURE);
395 urlParts.append(FACEBOOK_LOGIN_ENDING);
397 emit saveUsername(m_email);
399 m_webView->load(QUrl(urlParts.join(EMPTY)));
400 toggleProgressIndicator(true);
402 emit cancelLoginProcess();
406 dialog->deleteLater();
408 if(!m_queue.isEmpty())
409 showInformationBox();
412 void MainWindow::drawFullScreenButton(const QSize &size)
414 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
416 if(m_fullScreenButton) {
417 m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
419 size.height() - m_fullScreenButton->size().height());
423 void MainWindow::drawOsmLicense(const QSize &size)
425 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
427 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
429 size.height() - m_osmLicense->fontMetrics().height());
432 void MainWindow::drawOwnLocationCrosshair(int width, int height)
434 qDebug() << __PRETTY_FUNCTION__;
436 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
437 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
438 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
442 void MainWindow::gpsTimeout()
444 qDebug() << __PRETTY_FUNCTION__;
446 buildInformationBox(tr("GPS timeout"));
449 void MainWindow::grabZoomKeys(bool grab)
451 qDebug() << __PRETTY_FUNCTION__;
454 // Can't grab keys unless we have a window id
458 unsigned long val = (grab) ? 1 : 0;
459 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
463 XChangeProperty (QX11Info::display(),
469 reinterpret_cast<unsigned char *>(&val),
473 #endif // Q_WS_MAEMO_5
476 void MainWindow::keyPressEvent(QKeyEvent* event)
478 qDebug() << __PRETTY_FUNCTION__;
480 switch (event->key()) {
491 QWidget::keyPressEvent(event);
494 void MainWindow::loadCookies()
496 qDebug() << __PRETTY_FUNCTION__;
498 QSettings settings(DIRECTORY_NAME, FILE_NAME);
500 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
502 if(!list.isEmpty()) {
503 QList<QNetworkCookie> cookieList;
504 for(int i=0;i<list.count();i++) {
505 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
509 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
511 m_cookieJar->setAllCookies(cookieList);
512 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
517 void MainWindow::loadDone(bool done)
519 qDebug() << __PRETTY_FUNCTION__;
521 // for the first time the login page is opened, we need to refresh it to get cookies working
529 QWebFrame* frame = m_webView->page()->currentFrame();
533 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
535 foreach (QWebElement element, emailCollection) {
536 element.setAttribute("value", m_email.toAscii());
539 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
540 foreach (QWebElement element, passwordCollection) {
541 element.setAttribute("value", m_password.toAscii());
543 // find connect button
544 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
545 foreach (QWebElement element, buttonCollection)
547 QPoint pos(element.geometry().center());
549 // send a mouse click event to the web page
550 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
552 QApplication::sendEvent(m_webView->page(), &event0);
553 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
555 QApplication::sendEvent(m_webView->page(), &event1);
561 void MainWindow::loggedIn(bool logged)
563 qDebug() << __PRETTY_FUNCTION__;
568 m_loginAct->setText(tr("Logout"));
574 m_loginAct->setText(tr("Login"));
576 updateItemVisibility(m_loggedIn);
579 void MainWindow::loginFailed()
581 qDebug() << __PRETTY_FUNCTION__;
585 toggleProgressIndicator(false);
590 void MainWindow::loginUsingCookies()
592 qDebug() << __PRETTY_FUNCTION__;
597 QStringList urlParts;
598 urlParts.append(FACEBOOK_LOGINBASE);
599 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
600 urlParts.append(INTERVAL1);
601 urlParts.append(SITUARE_LOGIN_SUCCESS);
602 urlParts.append(INTERVAL2);
603 urlParts.append(SITUARE_LOGIN_FAILURE);
604 urlParts.append(FACEBOOK_LOGIN_ENDING);
606 m_webView->load(QUrl(urlParts.join(EMPTY)));
610 void MainWindow::openSettingsDialog()
612 qDebug() << __PRETTY_FUNCTION__;
614 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
615 m_settingsDialog->show();
618 void MainWindow::queueDialog(QDialog *dialog)
620 qDebug() << __PRETTY_FUNCTION__;
622 m_queue.append(dialog);
624 if(m_queue.count() == 1)
625 showInformationBox();
628 void MainWindow::saveCookies()
630 qDebug() << __PRETTY_FUNCTION__;
633 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
635 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
638 for(int i=0;i<cookieList.count();i++) {
639 QNetworkCookie cookie = cookieList.at(i);
640 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
641 list.append(QString(byteArray));
643 list.removeDuplicates();
645 QSettings settings(DIRECTORY_NAME, FILE_NAME);
646 settings.setValue(COOKIES, list);
649 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
651 qDebug() << __PRETTY_FUNCTION__;
653 m_autoCenteringAct->setChecked(enabled);
656 void MainWindow::setGPSButtonEnabled(bool enabled)
658 qDebug() << __PRETTY_FUNCTION__;
660 m_gpsToggleAct->setChecked(enabled);
663 setOwnLocationCrosshairVisibility(!enabled);
665 m_autoCenteringAct->setVisible(enabled);
668 void MainWindow::setMapViewScene(QGraphicsScene *scene)
670 qDebug() << __PRETTY_FUNCTION__;
672 m_mapView->setScene(scene);
675 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
677 qDebug() << __PRETTY_FUNCTION__;
680 m_ownLocationCrosshair->show();
681 m_drawOwnLocationCrosshair = true;
682 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
684 m_ownLocationCrosshair->hide();
685 m_drawOwnLocationCrosshair = false;
689 void MainWindow::setUsername(const QString &username)
691 qDebug() << __PRETTY_FUNCTION__;
696 void MainWindow::setViewPortSize(int width, int height)
698 qDebug() << __PRETTY_FUNCTION__;
700 m_viewPortWidth = width;
701 m_viewPortHeight = height;
704 void MainWindow::toggleFullScreen()
706 qDebug() << __PRETTY_FUNCTION__;
708 if(windowState() == Qt::WindowNoState)
714 void MainWindow::showInformationBox()
716 qDebug() << __PRETTY_FUNCTION__;
718 QDialog *dialog = m_queue.takeFirst();
719 connect(dialog, SIGNAL(finished(int)),
720 this, SLOT(dialogFinished(int)));
722 m_queue.insert(0, dialog);
725 void MainWindow::startLoginProcess()
727 qDebug() << __PRETTY_FUNCTION__;
729 LoginDialog *loginDialog = new LoginDialog();
730 loginDialog->setObjectName(QString("loginDialog"));
732 emit fetchUsernameFromSettings();
734 loginDialog->clearTextFields();
736 if(!m_email.isEmpty())
737 loginDialog->setEmailField(m_email);
739 queueDialog(loginDialog);
742 void MainWindow::toggleProgressIndicator(bool value)
744 qDebug() << __PRETTY_FUNCTION__;
747 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
750 #endif // Q_WS_MAEMO_5
753 void MainWindow::updateItemVisibility(bool show)
755 qDebug() << __PRETTY_FUNCTION__;
758 m_friendsListPanel->show();
759 m_friendsListPanelSidebar->show();
761 m_userPanelSidebar->show();
763 if(m_drawOwnLocationCrosshair) {
764 m_ownLocationCrosshair->show();
765 setGPSButtonEnabled(false);
766 emit gpsTriggered(false);
769 m_friendsListPanel->closePanel();
770 m_friendsListPanel->hide();
771 m_friendsListPanelSidebar->hide();
772 m_userPanel->closePanel();
774 m_userPanelSidebar->hide();
775 m_ownLocationCrosshair->hide();
779 const QString MainWindow::username()
781 qDebug() << __PRETTY_FUNCTION__;
786 void MainWindow::webViewRequestFinished(QNetworkReply *reply)
788 qDebug() << __PRETTY_FUNCTION__;
791 qDebug() << reply->error() << reply->errorString();
792 toggleProgressIndicator(false);