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();
76 m_settingsDialog = new SettingsDialog(this);
77 m_settingsDialog->hide();
78 connect(m_settingsDialog, SIGNAL(enableAutomaticLocationUpdate(bool,int)),
79 this, SIGNAL(enableAutomaticLocationUpdate(bool,int)));
82 setWindowTitle(tr("Situare"));
84 // set stacking order of widgets
85 m_zoomButtonPanel->stackUnder(m_userPanel);
86 m_osmLicense->stackUnder(m_zoomButtonPanel);
87 m_ownLocationCrosshair->stackUnder(m_osmLicense);
88 m_mapView->stackUnder(m_ownLocationCrosshair);
90 this->toggleProgressIndicator(true);
94 // set screen size in desktop matching N900 screen size
95 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
98 MainWindow::~MainWindow()
100 qDebug() << __PRETTY_FUNCTION__;
108 void MainWindow::buildFriendListPanel()
110 qDebug() << __PRETTY_FUNCTION__;
112 m_friendsListPanel = new FriendListPanel(this);
113 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
115 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
117 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
118 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
120 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
121 this, SIGNAL(findFriend(QPointF)));
123 connect(m_mapView, SIGNAL(viewResized(QSize)),
124 m_friendsListPanel, SLOT(screenResized(QSize)));
126 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
127 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
129 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
130 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
133 void MainWindow::buildManualLocationCrosshair()
135 qDebug() << __PRETTY_FUNCTION__;
137 m_ownLocationCrosshair = new QLabel(this);
138 QPixmap crosshairImage(":/res/images/sight.png");
139 m_ownLocationCrosshair->setPixmap(crosshairImage);
140 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
141 m_ownLocationCrosshair->hide();
142 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
144 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
145 this, SLOT(drawOwnLocationCrosshair(int, int)));
148 void MainWindow::buildMap()
150 qDebug() << __PRETTY_FUNCTION__;
152 m_mapView = new MapView(this);
154 buildZoomButtonPanel();
156 m_ownLocationCrosshair = 0;
158 buildManualLocationCrosshair();
160 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
161 this, SIGNAL(mapViewScrolled(QPoint)));
163 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
164 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
166 connect(m_mapView, SIGNAL(viewResized(QSize)),
167 this, SIGNAL(mapViewResized(QSize)));
169 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
170 this, SLOT(setViewPortSize(int, int)));
172 connect(this, SIGNAL(zoomLevelChanged(int)),
173 m_mapView, SLOT(setZoomLevel(int)));
175 connect(m_mapView, SIGNAL(viewZoomFinished()),
176 this, SIGNAL(viewZoomFinished()));
179 void MainWindow::buildOsmLicense()
181 qDebug() << __PRETTY_FUNCTION__;
183 m_osmLicense = new QLabel(this);
184 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
185 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
186 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
187 m_osmLicense->setFont(QFont("Nokia Sans", 9));
188 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
189 m_osmLicense->fontMetrics().height());
191 connect(m_mapView, SIGNAL(viewResized(QSize)),
192 this, SLOT(drawOsmLicense(QSize)));
195 void MainWindow::buildUserInfoPanel()
197 qDebug() << __PRETTY_FUNCTION__;
199 m_userPanel = new UserInfoPanel(this);
200 m_userPanelSidebar = new PanelSideBar(this, LEFT);
202 m_userPanelSidebar->stackUnder(m_friendsListPanel);
203 m_userPanel->stackUnder(m_userPanelSidebar);
205 connect(m_userPanel, SIGNAL(findUser(QPointF)),
206 this, SIGNAL(findUser(QPointF)));
208 connect(this, SIGNAL(userLocationReady(User*)),
209 m_userPanel, SLOT(userDataReceived(User*)));
211 connect(m_userPanel, SIGNAL(requestReverseGeo()),
212 this, SIGNAL(requestReverseGeo()));
214 connect(this, SIGNAL(reverseGeoReady(QString)),
215 m_userPanel, SIGNAL(reverseGeoReady(QString)));
217 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
218 this, SIGNAL(statusUpdate(QString,bool)));
220 connect(m_userPanel, SIGNAL(refreshUserData()),
221 this, SIGNAL(refreshUserData()));
223 connect(m_mapView, SIGNAL(viewResized(QSize)),
224 m_userPanel, SLOT(screenResized(QSize)));
227 void MainWindow::buildWebView()
229 qDebug() << __PRETTY_FUNCTION__;
232 m_webView = new QWebView;
234 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
235 this, SIGNAL(updateCredentials(QUrl)));
236 connect(m_webView, SIGNAL(loadFinished(bool)),
237 this, SLOT(loadDone(bool)));
243 void MainWindow::buildZoomButtonPanel()
245 qDebug() << __PRETTY_FUNCTION__;
247 m_zoomButtonPanel = new ZoomButtonPanel(this);
249 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
250 this, SIGNAL(zoomIn()));
252 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
253 this, SIGNAL(zoomOut()));
255 connect(this, SIGNAL(zoomLevelChanged(int)),
256 m_zoomButtonPanel, SLOT(resetButtons()));
258 connect(this, SIGNAL(maxZoomLevelReached()),
259 m_zoomButtonPanel, SLOT(disableZoomInButton()));
261 connect(this, SIGNAL(minZoomLevelReached()),
262 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
264 connect(m_mapView, SIGNAL(viewResized(QSize)),
265 m_zoomButtonPanel, SLOT(screenResized(QSize)));
268 void MainWindow::clearCookieJar()
270 qDebug() << __PRETTY_FUNCTION__;
275 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
277 QList<QNetworkCookie> emptyList;
280 m_cookieJar->setAllCookies(emptyList);
281 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
284 void MainWindow::createMenus()
286 qDebug() << __PRETTY_FUNCTION__;
289 m_loginAct = new QAction(tr("Login"), this);
290 connect(m_loginAct, SIGNAL(triggered()),
291 this, SIGNAL(loginActionPressed()));
294 m_toSettingsAct = new QAction(tr("Settings"), this);
295 connect(m_toSettingsAct, SIGNAL(triggered()),
296 this, SLOT(openSettingsDialog()));
299 m_gpsToggleAct = new QAction(tr("GPS"), this);
300 m_gpsToggleAct->setCheckable(true);
302 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
303 this, SIGNAL(gpsTriggered(bool)));
305 // automatic centering
306 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
307 m_autoCenteringAct->setCheckable(true);
308 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
309 this, SIGNAL(autoCenteringTriggered(bool)));
311 // build the actual menu
312 m_viewMenu = menuBar()->addMenu(tr("Main"));
313 m_viewMenu->addAction(m_loginAct);
314 m_viewMenu->addAction(m_toSettingsAct);
315 m_viewMenu->addAction(m_gpsToggleAct);
316 m_viewMenu->addAction(m_autoCenteringAct);
317 m_viewMenu->setObjectName(tr("Menu"));
320 void MainWindow::drawOsmLicense(const QSize &size)
322 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
324 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
326 size.height() - m_osmLicense->fontMetrics().height());
330 void MainWindow::drawOwnLocationCrosshair(int width, int height)
332 qDebug() << __PRETTY_FUNCTION__;
334 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
335 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
336 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
340 void MainWindow::gpsTimeout()
342 qDebug() << __PRETTY_FUNCTION__;
344 showMaemoInformationBox(tr("GPS timeout"));
347 void MainWindow::grabZoomKeys(bool grab)
349 qDebug() << __PRETTY_FUNCTION__;
352 // Can't grab keys unless we have a window id
356 unsigned long val = (grab) ? 1 : 0;
357 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
361 XChangeProperty (QX11Info::display(),
367 reinterpret_cast<unsigned char *>(&val),
371 #endif // Q_WS_MAEMO_5
374 void MainWindow::keyPressEvent(QKeyEvent* event)
376 qDebug() << __PRETTY_FUNCTION__;
378 switch (event->key()) {
389 QWidget::keyPressEvent(event);
392 void MainWindow::loadCookies()
394 qDebug() << __PRETTY_FUNCTION__;
396 QSettings settings(DIRECTORY_NAME, FILE_NAME);
398 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
400 if(!list.isEmpty()) {
401 QList<QNetworkCookie> cookieList;
402 for(int i=0;i<list.count();i++) {
403 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
407 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
409 m_cookieJar->setAllCookies(cookieList);
410 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
415 void MainWindow::loadDone(bool done)
417 qDebug() << __PRETTY_FUNCTION__;
419 // for the first time the login page is opened, we need to refresh it to get cookies working
427 QWebFrame* frame = m_webView->page()->currentFrame();
431 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
433 foreach (QWebElement element, emailCollection) {
434 element.setAttribute("value", m_email.toAscii());
437 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
438 foreach (QWebElement element, passwordCollection) {
439 element.setAttribute("value", m_password.toAscii());
441 // find connect button
442 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
443 foreach (QWebElement element, buttonCollection)
445 QPoint pos(element.geometry().center());
447 // send a mouse click event to the web page
448 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
450 QApplication::sendEvent(m_webView->page(), &event0);
451 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
453 QApplication::sendEvent(m_webView->page(), &event1);
459 void MainWindow::loggedIn(bool logged)
461 qDebug() << __PRETTY_FUNCTION__;
466 m_loginAct->setText(tr("Logout"));
473 m_loginAct->setText(tr("Login"));
475 updateItemVisibility(m_loggedIn);
478 void MainWindow::loginFailed()
480 qDebug() << __PRETTY_FUNCTION__;
484 toggleProgressIndicator(false);
486 QStringList urlParts;
487 urlParts.append(FACEBOOK_LOGINBASE);
488 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
489 urlParts.append(INTERVAL1);
490 urlParts.append(SITUARE_LOGIN_SUCCESS);
491 urlParts.append(INTERVAL2);
492 urlParts.append(SITUARE_LOGIN_FAILURE);
493 urlParts.append(FACEBOOK_LOGIN_ENDING);
495 startLoginProcess(urlParts.join(EMPTY));
498 void MainWindow::loginUsingCookies()
500 qDebug() << __PRETTY_FUNCTION__;
505 QStringList urlParts;
506 urlParts.append(FACEBOOK_LOGINBASE);
507 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
508 urlParts.append(INTERVAL1);
509 urlParts.append(SITUARE_LOGIN_SUCCESS);
510 urlParts.append(INTERVAL2);
511 urlParts.append(SITUARE_LOGIN_FAILURE);
512 urlParts.append(FACEBOOK_LOGIN_ENDING);
514 m_webView->load(QUrl(urlParts.join(EMPTY)));
518 void MainWindow::openSettingsDialog()
520 qDebug() << __PRETTY_FUNCTION__;
522 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
523 m_settingsDialog->show();
526 void MainWindow::saveCookies()
528 qDebug() << __PRETTY_FUNCTION__;
531 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
533 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
536 for(int i=0;i<cookieList.count();i++) {
537 QNetworkCookie cookie = cookieList.at(i);
538 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
539 list.append(QString(byteArray));
541 list.removeDuplicates();
543 QSettings settings(DIRECTORY_NAME, FILE_NAME);
544 settings.setValue(COOKIES, list);
547 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
549 qDebug() << __PRETTY_FUNCTION__;
551 m_autoCenteringAct->setChecked(enabled);
554 void MainWindow::setGPSButtonEnabled(bool enabled)
556 qDebug() << __PRETTY_FUNCTION__;
558 m_gpsToggleAct->setChecked(enabled);
561 setOwnLocationCrosshairVisibility(!enabled);
563 m_autoCenteringAct->setVisible(enabled);
566 void MainWindow::setMapViewScene(QGraphicsScene *scene)
568 qDebug() << __PRETTY_FUNCTION__;
570 m_mapView->setScene(scene);
573 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
575 qDebug() << __PRETTY_FUNCTION__;
578 m_ownLocationCrosshair->show();
579 m_drawOwnLocationCrosshair = true;
580 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
583 m_ownLocationCrosshair->hide();
584 m_drawOwnLocationCrosshair = false;
588 void MainWindow::setUsername(const QString &username)
590 qDebug() << __PRETTY_FUNCTION__;
595 void MainWindow::setViewPortSize(int width, int height)
597 qDebug() << __PRETTY_FUNCTION__;
599 m_viewPortWidth = width;
600 m_viewPortHeight = height;
603 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
605 qDebug() << __PRETTY_FUNCTION__;
609 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
612 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
616 QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
620 void MainWindow::startLoginProcess(const QUrl &url)
622 qDebug() << __PRETTY_FUNCTION__;
626 LoginDialog loginDialog;
628 emit fetchUsernameFromSettings();
631 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
633 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
635 loginDialog.clearTextFields();
637 if(!m_email.isEmpty())
638 loginDialog.setEmailField(m_email);
640 if(loginDialog.exec() != QDialog::Accepted) {
641 // if login dialog was canceled we need to stop processing webview
644 emit cancelLoginProcess();
647 loginDialog.userInput(m_email, m_password);
648 emit saveUsername(m_email);
649 m_webView->load(url);
650 toggleProgressIndicator(true);
655 void MainWindow::toggleProgressIndicator(bool value)
657 qDebug() << __PRETTY_FUNCTION__;
660 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
663 #endif // Q_WS_MAEMO_5
666 void MainWindow::updateItemVisibility(bool show)
668 qDebug() << __PRETTY_FUNCTION__;
671 m_friendsListPanel->show();
672 m_friendsListPanelSidebar->show();
674 m_userPanelSidebar->show();
676 if(m_drawOwnLocationCrosshair) {
677 m_ownLocationCrosshair->show();
678 setGPSButtonEnabled(false);
679 emit gpsTriggered(false);
683 m_friendsListPanel->closePanel();
684 m_friendsListPanel->hide();
685 m_friendsListPanelSidebar->hide();
686 m_userPanel->closePanel();
688 m_userPanelSidebar->hide();
689 m_ownLocationCrosshair->hide();
693 const QString MainWindow::username()
695 qDebug() << __PRETTY_FUNCTION__;