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::buildManualLocationCrosshair()
158 qDebug() << __PRETTY_FUNCTION__;
160 m_ownLocationCrosshair = new QLabel(this);
161 QPixmap crosshairImage(":/res/images/sight.png");
162 m_ownLocationCrosshair->setPixmap(crosshairImage);
163 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
164 m_ownLocationCrosshair->hide();
165 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
167 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
168 this, SLOT(drawOwnLocationCrosshair(int, int)));
171 void MainWindow::buildMap()
173 qDebug() << __PRETTY_FUNCTION__;
175 m_mapView = new MapView(this);
177 buildZoomButtonPanel();
179 buildManualLocationCrosshair();
180 buildFullScreenButton();
182 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
183 this, SIGNAL(mapViewScrolled(QPoint)));
185 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
186 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
188 connect(m_mapView, SIGNAL(viewResized(QSize)),
189 this, SIGNAL(mapViewResized(QSize)));
191 connect(m_mapView, SIGNAL(viewResized(QSize)),
192 this, SLOT(drawFullScreenButton(QSize)));
194 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
195 this, SLOT(setViewPortSize(int, int)));
197 connect(this, SIGNAL(zoomLevelChanged(int)),
198 m_mapView, SLOT(setZoomLevel(int)));
200 connect(m_mapView, SIGNAL(viewZoomFinished()),
201 this, SIGNAL(viewZoomFinished()));
204 void MainWindow::buildOsmLicense()
206 qDebug() << __PRETTY_FUNCTION__;
208 m_osmLicense = new QLabel(this);
209 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
210 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
211 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
212 m_osmLicense->setFont(QFont("Nokia Sans", 9));
213 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
214 m_osmLicense->fontMetrics().height());
216 connect(m_mapView, SIGNAL(viewResized(QSize)),
217 this, SLOT(drawOsmLicense(QSize)));
220 void MainWindow::buildUserInfoPanel()
222 qDebug() << __PRETTY_FUNCTION__;
224 m_userPanel = new UserInfoPanel(this);
225 m_userPanelSidebar = new PanelSideBar(this, LEFT);
227 m_userPanelSidebar->stackUnder(m_friendsListPanel);
228 m_userPanel->stackUnder(m_userPanelSidebar);
230 connect(m_userPanel, SIGNAL(findUser(QPointF)),
231 this, SIGNAL(findUser(QPointF)));
233 connect(this, SIGNAL(userLocationReady(User*)),
234 m_userPanel, SLOT(userDataReceived(User*)));
236 connect(m_userPanel, SIGNAL(requestReverseGeo()),
237 this, SIGNAL(requestReverseGeo()));
239 connect(this, SIGNAL(reverseGeoReady(QString)),
240 m_userPanel, SIGNAL(reverseGeoReady(QString)));
242 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
243 this, SIGNAL(statusUpdate(QString,bool)));
245 connect(m_userPanel, SIGNAL(refreshUserData()),
246 this, SIGNAL(refreshUserData()));
248 connect(m_mapView, SIGNAL(viewResized(QSize)),
249 m_userPanel, SLOT(screenResized(QSize)));
252 void MainWindow::buildWebView()
254 qDebug() << __PRETTY_FUNCTION__;
257 m_webView = new QWebView;
259 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
260 this, SIGNAL(updateCredentials(QUrl)));
261 connect(m_webView, SIGNAL(loadFinished(bool)),
262 this, SLOT(loadDone(bool)));
268 void MainWindow::buildZoomButtonPanel()
270 qDebug() << __PRETTY_FUNCTION__;
272 m_zoomButtonPanel = new ZoomButtonPanel(this);
274 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
275 this, SIGNAL(zoomIn()));
277 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
278 this, SIGNAL(zoomOut()));
280 connect(this, SIGNAL(zoomLevelChanged(int)),
281 m_zoomButtonPanel, SLOT(resetButtons()));
283 connect(this, SIGNAL(maxZoomLevelReached()),
284 m_zoomButtonPanel, SLOT(disableZoomInButton()));
286 connect(this, SIGNAL(minZoomLevelReached()),
287 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
289 connect(m_mapView, SIGNAL(viewResized(QSize)),
290 m_zoomButtonPanel, SLOT(screenResized(QSize)));
293 void MainWindow::clearCookieJar()
295 qDebug() << __PRETTY_FUNCTION__;
300 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
302 QList<QNetworkCookie> emptyList;
305 m_cookieJar->setAllCookies(emptyList);
306 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
309 void MainWindow::createMenus()
311 qDebug() << __PRETTY_FUNCTION__;
314 m_loginAct = new QAction(tr("Login"), this);
315 connect(m_loginAct, SIGNAL(triggered()),
316 this, SIGNAL(loginActionPressed()));
319 m_toSettingsAct = new QAction(tr("Settings"), this);
320 connect(m_toSettingsAct, SIGNAL(triggered()),
321 this, SLOT(openSettingsDialog()));
324 m_gpsToggleAct = new QAction(tr("GPS"), this);
325 m_gpsToggleAct->setCheckable(true);
327 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
328 this, SIGNAL(gpsTriggered(bool)));
330 // automatic centering
331 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
332 m_autoCenteringAct->setCheckable(true);
333 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
334 this, SIGNAL(autoCenteringTriggered(bool)));
336 // build the actual menu
337 m_viewMenu = menuBar()->addMenu(tr("Main"));
338 m_viewMenu->addAction(m_loginAct);
339 m_viewMenu->addAction(m_toSettingsAct);
340 m_viewMenu->addAction(m_gpsToggleAct);
341 m_viewMenu->addAction(m_autoCenteringAct);
342 m_viewMenu->setObjectName(tr("Menu"));
345 void MainWindow::drawFullScreenButton(const QSize &size)
347 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
349 if(m_fullScreenButton) {
350 m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
352 size.height() - m_fullScreenButton->size().height());
356 void MainWindow::drawOsmLicense(const QSize &size)
358 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
360 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
362 size.height() - m_osmLicense->fontMetrics().height());
365 void MainWindow::drawOwnLocationCrosshair(int width, int height)
367 qDebug() << __PRETTY_FUNCTION__;
369 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
370 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
371 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
375 void MainWindow::gpsTimeout()
377 qDebug() << __PRETTY_FUNCTION__;
379 queueInformationBox(tr("GPS timeout"));
382 void MainWindow::grabZoomKeys(bool grab)
384 qDebug() << __PRETTY_FUNCTION__;
387 // Can't grab keys unless we have a window id
391 unsigned long val = (grab) ? 1 : 0;
392 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
396 XChangeProperty (QX11Info::display(),
402 reinterpret_cast<unsigned char *>(&val),
406 #endif // Q_WS_MAEMO_5
409 void MainWindow::keyPressEvent(QKeyEvent* event)
411 qDebug() << __PRETTY_FUNCTION__;
413 switch (event->key()) {
424 QWidget::keyPressEvent(event);
427 void MainWindow::loadCookies()
429 qDebug() << __PRETTY_FUNCTION__;
431 QSettings settings(DIRECTORY_NAME, FILE_NAME);
433 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
435 if(!list.isEmpty()) {
436 QList<QNetworkCookie> cookieList;
437 for(int i=0;i<list.count();i++) {
438 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
442 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
444 m_cookieJar->setAllCookies(cookieList);
445 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
450 void MainWindow::loadDone(bool done)
452 qDebug() << __PRETTY_FUNCTION__;
454 // for the first time the login page is opened, we need to refresh it to get cookies working
462 QWebFrame* frame = m_webView->page()->currentFrame();
466 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
468 foreach (QWebElement element, emailCollection) {
469 element.setAttribute("value", m_email.toAscii());
472 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
473 foreach (QWebElement element, passwordCollection) {
474 element.setAttribute("value", m_password.toAscii());
476 // find connect button
477 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
478 foreach (QWebElement element, buttonCollection)
480 QPoint pos(element.geometry().center());
482 // send a mouse click event to the web page
483 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
485 QApplication::sendEvent(m_webView->page(), &event0);
486 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
488 QApplication::sendEvent(m_webView->page(), &event1);
494 void MainWindow::loggedIn(bool logged)
496 qDebug() << __PRETTY_FUNCTION__;
501 m_loginAct->setText(tr("Logout"));
507 m_loginAct->setText(tr("Login"));
509 updateItemVisibility(m_loggedIn);
512 void MainWindow::loginFailed()
514 qDebug() << __PRETTY_FUNCTION__;
518 toggleProgressIndicator(false);
520 QStringList urlParts;
521 urlParts.append(FACEBOOK_LOGINBASE);
522 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
523 urlParts.append(INTERVAL1);
524 urlParts.append(SITUARE_LOGIN_SUCCESS);
525 urlParts.append(INTERVAL2);
526 urlParts.append(SITUARE_LOGIN_FAILURE);
527 urlParts.append(FACEBOOK_LOGIN_ENDING);
529 startLoginProcess(urlParts.join(EMPTY));
532 void MainWindow::noteFinished()
534 qDebug() << __PRETTY_FUNCTION__;
536 NoteData *data = m_queue.takeFirst();
539 if(!m_queue.isEmpty())
540 showInformationBox();
543 void MainWindow::loginUsingCookies()
545 qDebug() << __PRETTY_FUNCTION__;
550 QStringList urlParts;
551 urlParts.append(FACEBOOK_LOGINBASE);
552 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
553 urlParts.append(INTERVAL1);
554 urlParts.append(SITUARE_LOGIN_SUCCESS);
555 urlParts.append(INTERVAL2);
556 urlParts.append(SITUARE_LOGIN_FAILURE);
557 urlParts.append(FACEBOOK_LOGIN_ENDING);
559 m_webView->load(QUrl(urlParts.join(EMPTY)));
563 void MainWindow::openSettingsDialog()
565 qDebug() << __PRETTY_FUNCTION__;
567 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
568 m_settingsDialog->show();
571 void MainWindow::queueInformationBox(const QString &message, bool modal)
573 qDebug() << __PRETTY_FUNCTION__;
575 NoteData *data = new NoteData(message, modal);
577 m_queue.append(data);
579 if(m_queue.count() == 1)
580 showInformationBox();
583 void MainWindow::saveCookies()
585 qDebug() << __PRETTY_FUNCTION__;
588 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
590 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
593 for(int i=0;i<cookieList.count();i++) {
594 QNetworkCookie cookie = cookieList.at(i);
595 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
596 list.append(QString(byteArray));
598 list.removeDuplicates();
600 QSettings settings(DIRECTORY_NAME, FILE_NAME);
601 settings.setValue(COOKIES, list);
604 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
606 qDebug() << __PRETTY_FUNCTION__;
608 m_autoCenteringAct->setChecked(enabled);
611 void MainWindow::setGPSButtonEnabled(bool enabled)
613 qDebug() << __PRETTY_FUNCTION__;
615 m_gpsToggleAct->setChecked(enabled);
618 setOwnLocationCrosshairVisibility(!enabled);
620 m_autoCenteringAct->setVisible(enabled);
623 void MainWindow::setMapViewScene(QGraphicsScene *scene)
625 qDebug() << __PRETTY_FUNCTION__;
627 m_mapView->setScene(scene);
630 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
632 qDebug() << __PRETTY_FUNCTION__;
635 m_ownLocationCrosshair->show();
636 m_drawOwnLocationCrosshair = true;
637 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
639 m_ownLocationCrosshair->hide();
640 m_drawOwnLocationCrosshair = false;
644 void MainWindow::setUsername(const QString &username)
646 qDebug() << __PRETTY_FUNCTION__;
651 void MainWindow::setViewPortSize(int width, int height)
653 qDebug() << __PRETTY_FUNCTION__;
655 m_viewPortWidth = width;
656 m_viewPortHeight = height;
659 void MainWindow::toggleFullScreen()
661 qDebug() << __PRETTY_FUNCTION__;
663 if(windowState() == Qt::WindowNoState)
669 void MainWindow::showInformationBox()
671 qDebug() << __PRETTY_FUNCTION__;
673 NoteData *data = m_queue.takeFirst();
676 QMaemo5InformationBox *msgBox = new QMaemo5InformationBox(this);
677 QLabel *label = new QLabel(msgBox);
678 label->setAlignment(Qt::AlignCenter);
679 label->setText(data->message());
682 msgBox->setTimeout(QMaemo5InformationBox::NoTimeout);
684 msgBox->setTimeout(QMaemo5InformationBox::DefaultTimeout);
687 QMessageBox *msgBox = new QMessageBox(this);
688 msgBox->button(QMessageBox::Ok);
689 msgBox->setText(data->message());
690 msgBox->setModal(data->modal());
693 connect(msgBox, SIGNAL(finished(int)),
694 this, SLOT(noteFinished()));
698 m_queue.insert(0, data);
702 void MainWindow::startLoginProcess(const QUrl &url)
704 qDebug() << __PRETTY_FUNCTION__;
708 LoginDialog loginDialog;
710 emit fetchUsernameFromSettings();
713 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
715 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
717 loginDialog.clearTextFields();
719 if(!m_email.isEmpty())
720 loginDialog.setEmailField(m_email);
722 if(loginDialog.exec() != QDialog::Accepted) {
723 // if login dialog was canceled we need to stop processing webview
726 emit cancelLoginProcess();
728 loginDialog.userInput(m_email, m_password);
729 emit saveUsername(m_email);
730 m_webView->load(url);
731 toggleProgressIndicator(true);
736 void MainWindow::toggleProgressIndicator(bool value)
738 qDebug() << __PRETTY_FUNCTION__;
741 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
744 #endif // Q_WS_MAEMO_5
747 void MainWindow::updateItemVisibility(bool show)
749 qDebug() << __PRETTY_FUNCTION__;
752 m_friendsListPanel->show();
753 m_friendsListPanelSidebar->show();
755 m_userPanelSidebar->show();
757 if(m_drawOwnLocationCrosshair) {
758 m_ownLocationCrosshair->show();
759 setGPSButtonEnabled(false);
760 emit gpsTriggered(false);
763 m_friendsListPanel->closePanel();
764 m_friendsListPanel->hide();
765 m_friendsListPanelSidebar->hide();
766 m_userPanel->closePanel();
768 m_userPanelSidebar->hide();
769 m_ownLocationCrosshair->hide();
773 const QString MainWindow::username()
775 qDebug() << __PRETTY_FUNCTION__;