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()));
251 void MainWindow::buildWebView()
253 qDebug() << __PRETTY_FUNCTION__;
256 m_webView = new QWebView;
258 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
259 this, SIGNAL(updateCredentials(QUrl)));
260 connect(m_webView, SIGNAL(loadFinished(bool)),
261 this, SLOT(loadDone(bool)));
267 void MainWindow::buildZoomButtonPanel()
269 qDebug() << __PRETTY_FUNCTION__;
271 m_zoomButtonPanel = new ZoomButtonPanel(this);
273 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
274 this, SIGNAL(zoomIn()));
276 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
277 this, SIGNAL(zoomOut()));
279 connect(this, SIGNAL(zoomLevelChanged(int)),
280 m_zoomButtonPanel, SLOT(resetButtons()));
282 connect(this, SIGNAL(maxZoomLevelReached()),
283 m_zoomButtonPanel, SLOT(disableZoomInButton()));
285 connect(this, SIGNAL(minZoomLevelReached()),
286 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
288 connect(m_mapView, SIGNAL(viewResized(QSize)),
289 m_zoomButtonPanel, SLOT(screenResized(QSize)));
292 void MainWindow::clearCookieJar()
294 qDebug() << __PRETTY_FUNCTION__;
299 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
301 QList<QNetworkCookie> emptyList;
304 m_cookieJar->setAllCookies(emptyList);
305 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
308 void MainWindow::createMenus()
310 qDebug() << __PRETTY_FUNCTION__;
313 m_loginAct = new QAction(tr("Login"), this);
314 connect(m_loginAct, SIGNAL(triggered()),
315 this, SIGNAL(loginActionPressed()));
318 m_toSettingsAct = new QAction(tr("Settings"), this);
319 connect(m_toSettingsAct, SIGNAL(triggered()),
320 this, SLOT(openSettingsDialog()));
323 m_gpsToggleAct = new QAction(tr("GPS"), this);
324 m_gpsToggleAct->setCheckable(true);
326 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
327 this, SIGNAL(gpsTriggered(bool)));
329 // automatic centering
330 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
331 m_autoCenteringAct->setCheckable(true);
332 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
333 this, SIGNAL(autoCenteringTriggered(bool)));
335 // build the actual menu
336 m_viewMenu = menuBar()->addMenu(tr("Main"));
337 m_viewMenu->addAction(m_loginAct);
338 m_viewMenu->addAction(m_toSettingsAct);
339 m_viewMenu->addAction(m_gpsToggleAct);
340 m_viewMenu->addAction(m_autoCenteringAct);
341 m_viewMenu->setObjectName(tr("Menu"));
344 void MainWindow::drawFullScreenButton(const QSize &size)
346 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
348 if(m_fullScreenButton) {
349 m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
351 size.height() - m_fullScreenButton->size().height());
355 void MainWindow::drawOsmLicense(const QSize &size)
357 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
359 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
361 size.height() - m_osmLicense->fontMetrics().height());
364 void MainWindow::drawOwnLocationCrosshair(int width, int height)
366 qDebug() << __PRETTY_FUNCTION__;
368 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
369 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
370 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
374 void MainWindow::gpsTimeout()
376 qDebug() << __PRETTY_FUNCTION__;
378 showMaemoInformationBox(tr("GPS timeout"));
381 void MainWindow::grabZoomKeys(bool grab)
383 qDebug() << __PRETTY_FUNCTION__;
386 // Can't grab keys unless we have a window id
390 unsigned long val = (grab) ? 1 : 0;
391 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
395 XChangeProperty (QX11Info::display(),
401 reinterpret_cast<unsigned char *>(&val),
405 #endif // Q_WS_MAEMO_5
408 void MainWindow::keyPressEvent(QKeyEvent* event)
410 qDebug() << __PRETTY_FUNCTION__;
412 switch (event->key()) {
423 QWidget::keyPressEvent(event);
426 void MainWindow::loadCookies()
428 qDebug() << __PRETTY_FUNCTION__;
430 QSettings settings(DIRECTORY_NAME, FILE_NAME);
432 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
434 if(!list.isEmpty()) {
435 QList<QNetworkCookie> cookieList;
436 for(int i=0;i<list.count();i++) {
437 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
441 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
443 m_cookieJar->setAllCookies(cookieList);
444 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
449 void MainWindow::loadDone(bool done)
451 qDebug() << __PRETTY_FUNCTION__;
453 // for the first time the login page is opened, we need to refresh it to get cookies working
461 QWebFrame* frame = m_webView->page()->currentFrame();
465 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
467 foreach (QWebElement element, emailCollection) {
468 element.setAttribute("value", m_email.toAscii());
471 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
472 foreach (QWebElement element, passwordCollection) {
473 element.setAttribute("value", m_password.toAscii());
475 // find connect button
476 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
477 foreach (QWebElement element, buttonCollection)
479 QPoint pos(element.geometry().center());
481 // send a mouse click event to the web page
482 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
484 QApplication::sendEvent(m_webView->page(), &event0);
485 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
487 QApplication::sendEvent(m_webView->page(), &event1);
493 void MainWindow::loggedIn(bool logged)
495 qDebug() << __PRETTY_FUNCTION__;
500 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::loginUsingCookies()
534 qDebug() << __PRETTY_FUNCTION__;
539 QStringList urlParts;
540 urlParts.append(FACEBOOK_LOGINBASE);
541 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
542 urlParts.append(INTERVAL1);
543 urlParts.append(SITUARE_LOGIN_SUCCESS);
544 urlParts.append(INTERVAL2);
545 urlParts.append(SITUARE_LOGIN_FAILURE);
546 urlParts.append(FACEBOOK_LOGIN_ENDING);
548 m_webView->load(QUrl(urlParts.join(EMPTY)));
552 void MainWindow::openSettingsDialog()
554 qDebug() << __PRETTY_FUNCTION__;
556 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
557 m_settingsDialog->show();
560 void MainWindow::saveCookies()
562 qDebug() << __PRETTY_FUNCTION__;
565 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
567 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
570 for(int i=0;i<cookieList.count();i++) {
571 QNetworkCookie cookie = cookieList.at(i);
572 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
573 list.append(QString(byteArray));
575 list.removeDuplicates();
577 QSettings settings(DIRECTORY_NAME, FILE_NAME);
578 settings.setValue(COOKIES, list);
581 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
583 qDebug() << __PRETTY_FUNCTION__;
585 m_autoCenteringAct->setChecked(enabled);
588 void MainWindow::setGPSButtonEnabled(bool enabled)
590 qDebug() << __PRETTY_FUNCTION__;
592 m_gpsToggleAct->setChecked(enabled);
595 setOwnLocationCrosshairVisibility(!enabled);
597 m_autoCenteringAct->setVisible(enabled);
600 void MainWindow::setMapViewScene(QGraphicsScene *scene)
602 qDebug() << __PRETTY_FUNCTION__;
604 m_mapView->setScene(scene);
607 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
609 qDebug() << __PRETTY_FUNCTION__;
612 m_ownLocationCrosshair->show();
613 m_drawOwnLocationCrosshair = true;
614 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
617 m_ownLocationCrosshair->hide();
618 m_drawOwnLocationCrosshair = false;
622 void MainWindow::setUsername(const QString &username)
624 qDebug() << __PRETTY_FUNCTION__;
629 void MainWindow::setViewPortSize(int width, int height)
631 qDebug() << __PRETTY_FUNCTION__;
633 m_viewPortWidth = width;
634 m_viewPortHeight = height;
637 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
639 qDebug() << __PRETTY_FUNCTION__;
643 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
646 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
650 QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
654 void MainWindow::toggleFullScreen()
656 qDebug() << __PRETTY_FUNCTION__;
658 if(windowState() == Qt::WindowNoState)
664 void MainWindow::startLoginProcess(const QUrl &url)
666 qDebug() << __PRETTY_FUNCTION__;
670 LoginDialog loginDialog;
672 emit fetchUsernameFromSettings();
675 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
677 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
679 loginDialog.clearTextFields();
681 if(!m_email.isEmpty())
682 loginDialog.setEmailField(m_email);
684 if(loginDialog.exec() != QDialog::Accepted) {
685 // if login dialog was canceled we need to stop processing webview
688 emit cancelLoginProcess();
691 loginDialog.userInput(m_email, m_password);
692 emit saveUsername(m_email);
693 m_webView->load(url);
694 toggleProgressIndicator(true);
699 void MainWindow::toggleProgressIndicator(bool value)
701 qDebug() << __PRETTY_FUNCTION__;
704 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
707 #endif // Q_WS_MAEMO_5
710 void MainWindow::updateItemVisibility(bool show)
712 qDebug() << __PRETTY_FUNCTION__;
715 m_friendsListPanel->show();
716 m_friendsListPanelSidebar->show();
718 m_userPanelSidebar->show();
720 if(m_drawOwnLocationCrosshair) {
721 m_ownLocationCrosshair->show();
722 setGPSButtonEnabled(false);
723 emit gpsTriggered(false);
727 m_friendsListPanel->closePanel();
728 m_friendsListPanel->hide();
729 m_friendsListPanelSidebar->hide();
730 m_userPanel->closePanel();
732 m_userPanelSidebar->hide();
733 m_ownLocationCrosshair->hide();
737 const QString MainWindow::username()
739 qDebug() << __PRETTY_FUNCTION__;