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::gpsError(const QString &message)
342 qDebug() << __PRETTY_FUNCTION__;
344 showMaemoInformationBox(message);
347 void MainWindow::gpsTimeout()
349 qDebug() << __PRETTY_FUNCTION__;
351 showMaemoInformationBox(tr("GPS timeout"));
354 void MainWindow::grabZoomKeys(bool grab)
356 qDebug() << __PRETTY_FUNCTION__;
359 // Can't grab keys unless we have a window id
363 unsigned long val = (grab) ? 1 : 0;
364 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
368 XChangeProperty (QX11Info::display(),
374 reinterpret_cast<unsigned char *>(&val),
378 #endif // Q_WS_MAEMO_5
381 void MainWindow::keyPressEvent(QKeyEvent* event)
383 qDebug() << __PRETTY_FUNCTION__;
385 switch (event->key()) {
396 QWidget::keyPressEvent(event);
399 void MainWindow::loadCookies()
401 qDebug() << __PRETTY_FUNCTION__;
403 QSettings settings(DIRECTORY_NAME, FILE_NAME);
405 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
407 if(!list.isEmpty()) {
408 QList<QNetworkCookie> cookieList;
409 for(int i=0;i<list.count();i++) {
410 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
414 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
416 m_cookieJar->setAllCookies(cookieList);
417 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
422 void MainWindow::loadDone(bool done)
424 qDebug() << __PRETTY_FUNCTION__;
426 // for the first time the login page is opened, we need to refresh it to get cookies working
434 QWebFrame* frame = m_webView->page()->currentFrame();
438 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
440 foreach (QWebElement element, emailCollection) {
441 element.setAttribute("value", m_email.toAscii());
444 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
445 foreach (QWebElement element, passwordCollection) {
446 element.setAttribute("value", m_password.toAscii());
448 // find connect button
449 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
450 foreach (QWebElement element, buttonCollection)
452 QPoint pos(element.geometry().center());
454 // send a mouse click event to the web page
455 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
457 QApplication::sendEvent(m_webView->page(), &event0);
458 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
460 QApplication::sendEvent(m_webView->page(), &event1);
466 void MainWindow::loggedIn(bool logged)
468 qDebug() << __PRETTY_FUNCTION__;
473 m_loginAct->setText(tr("Logout"));
480 m_loginAct->setText(tr("Login"));
482 updateItemVisibility(m_loggedIn);
485 void MainWindow::loginFailed()
487 qDebug() << __PRETTY_FUNCTION__;
491 toggleProgressIndicator(false);
493 QStringList urlParts;
494 urlParts.append(FACEBOOK_LOGINBASE);
495 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
496 urlParts.append(INTERVAL1);
497 urlParts.append(SITUARE_LOGIN_SUCCESS);
498 urlParts.append(INTERVAL2);
499 urlParts.append(SITUARE_LOGIN_FAILURE);
500 urlParts.append(FACEBOOK_LOGIN_ENDING);
502 startLoginProcess(urlParts.join(EMPTY));
505 void MainWindow::loginUsingCookies()
507 qDebug() << __PRETTY_FUNCTION__;
512 QStringList urlParts;
513 urlParts.append(FACEBOOK_LOGINBASE);
514 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
515 urlParts.append(INTERVAL1);
516 urlParts.append(SITUARE_LOGIN_SUCCESS);
517 urlParts.append(INTERVAL2);
518 urlParts.append(SITUARE_LOGIN_FAILURE);
519 urlParts.append(FACEBOOK_LOGIN_ENDING);
521 m_webView->load(QUrl(urlParts.join(EMPTY)));
525 void MainWindow::openSettingsDialog()
527 qDebug() << __PRETTY_FUNCTION__;
529 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
530 m_settingsDialog->show();
533 void MainWindow::saveCookies()
535 qDebug() << __PRETTY_FUNCTION__;
538 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
540 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
543 for(int i=0;i<cookieList.count();i++) {
544 QNetworkCookie cookie = cookieList.at(i);
545 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
546 list.append(QString(byteArray));
548 list.removeDuplicates();
550 QSettings settings(DIRECTORY_NAME, FILE_NAME);
551 settings.setValue(COOKIES, list);
554 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
556 qDebug() << __PRETTY_FUNCTION__;
558 m_autoCenteringAct->setChecked(enabled);
561 void MainWindow::setGPSButtonEnabled(bool enabled)
563 qDebug() << __PRETTY_FUNCTION__;
565 m_gpsToggleAct->setChecked(enabled);
568 setOwnLocationCrosshairVisibility(!enabled);
570 m_autoCenteringAct->setVisible(enabled);
573 void MainWindow::setMapViewScene(QGraphicsScene *scene)
575 qDebug() << __PRETTY_FUNCTION__;
577 m_mapView->setScene(scene);
580 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
582 qDebug() << __PRETTY_FUNCTION__;
585 m_ownLocationCrosshair->show();
586 m_drawOwnLocationCrosshair = true;
587 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
590 m_ownLocationCrosshair->hide();
591 m_drawOwnLocationCrosshair = false;
595 void MainWindow::setUsername(const QString &username)
597 qDebug() << __PRETTY_FUNCTION__;
602 void MainWindow::setViewPortSize(int width, int height)
604 qDebug() << __PRETTY_FUNCTION__;
606 m_viewPortWidth = width;
607 m_viewPortHeight = height;
610 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
612 qDebug() << __PRETTY_FUNCTION__;
616 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
619 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
623 QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
627 void MainWindow::startLoginProcess(const QUrl &url)
629 qDebug() << __PRETTY_FUNCTION__;
633 LoginDialog loginDialog;
635 emit fetchUsernameFromSettings();
638 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
640 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
642 loginDialog.clearTextFields();
644 if(!m_email.isEmpty())
645 loginDialog.setEmailField(m_email);
647 if(loginDialog.exec() != QDialog::Accepted) {
648 // if login dialog was canceled we need to stop processing webview
651 emit cancelLoginProcess();
654 loginDialog.userInput(m_email, m_password);
655 emit saveUsername(m_email);
656 m_webView->load(url);
657 toggleProgressIndicator(true);
662 void MainWindow::toggleProgressIndicator(bool value)
664 qDebug() << __PRETTY_FUNCTION__;
667 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
670 #endif // Q_WS_MAEMO_5
673 void MainWindow::updateItemVisibility(bool show)
675 qDebug() << __PRETTY_FUNCTION__;
678 m_friendsListPanel->show();
679 m_friendsListPanelSidebar->show();
681 m_userPanelSidebar->show();
683 if(m_drawOwnLocationCrosshair) {
684 m_ownLocationCrosshair->show();
685 setGPSButtonEnabled(false);
686 emit gpsTriggered(false);
690 m_friendsListPanel->closePanel();
691 m_friendsListPanel->hide();
692 m_friendsListPanelSidebar->hide();
693 m_userPanel->closePanel();
695 m_userPanelSidebar->hide();
696 m_ownLocationCrosshair->hide();
700 const QString MainWindow::username()
702 qDebug() << __PRETTY_FUNCTION__;