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::automaticLocationUpdateEnabled(bool enabled)
116 m_settingsDialog->setAutomaticLocationUpdateSettings(enabled);
119 void MainWindow::buildFullScreenButton()
121 qDebug() << __PRETTY_FUNCTION__;
123 m_fullScreenButton = new QToolButton(this);
124 m_fullScreenButton->setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
125 m_fullScreenButton->setFixedSize(m_fullScreenButton->sizeHint());
126 connect(m_fullScreenButton, SIGNAL(clicked()),
127 this, SLOT(toggleFullScreen()));
128 #endif // Q_WS_MAEMO_5
132 void MainWindow::buildFriendListPanel()
134 qDebug() << __PRETTY_FUNCTION__;
136 m_friendsListPanel = new FriendListPanel(this);
137 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
139 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
141 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
142 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
144 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
145 this, SIGNAL(findFriend(QPointF)));
147 connect(m_mapView, SIGNAL(viewResized(QSize)),
148 m_friendsListPanel, SLOT(screenResized(QSize)));
150 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
151 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
153 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
154 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
157 void MainWindow::buildManualLocationCrosshair()
159 qDebug() << __PRETTY_FUNCTION__;
161 m_ownLocationCrosshair = new QLabel(this);
162 QPixmap crosshairImage(":/res/images/sight.png");
163 m_ownLocationCrosshair->setPixmap(crosshairImage);
164 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
165 m_ownLocationCrosshair->hide();
166 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
168 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
169 this, SLOT(drawOwnLocationCrosshair(int, int)));
172 void MainWindow::buildMap()
174 qDebug() << __PRETTY_FUNCTION__;
176 m_mapView = new MapView(this);
178 buildZoomButtonPanel();
180 buildManualLocationCrosshair();
181 buildFullScreenButton();
183 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
184 this, SIGNAL(mapViewScrolled(QPoint)));
186 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
187 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
189 connect(m_mapView, SIGNAL(viewResized(QSize)),
190 this, SIGNAL(mapViewResized(QSize)));
192 connect(m_mapView, SIGNAL(viewResized(QSize)),
193 this, SLOT(drawFullScreenButton(QSize)));
195 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
196 this, SLOT(setViewPortSize(int, int)));
198 connect(this, SIGNAL(zoomLevelChanged(int)),
199 m_mapView, SLOT(setZoomLevel(int)));
201 connect(m_mapView, SIGNAL(viewZoomFinished()),
202 this, SIGNAL(viewZoomFinished()));
205 void MainWindow::buildOsmLicense()
207 qDebug() << __PRETTY_FUNCTION__;
209 m_osmLicense = new QLabel(this);
210 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
211 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
212 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
213 m_osmLicense->setFont(QFont("Nokia Sans", 9));
214 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
215 m_osmLicense->fontMetrics().height());
217 connect(m_mapView, SIGNAL(viewResized(QSize)),
218 this, SLOT(drawOsmLicense(QSize)));
221 void MainWindow::buildUserInfoPanel()
223 qDebug() << __PRETTY_FUNCTION__;
225 m_userPanel = new UserInfoPanel(this);
226 m_userPanelSidebar = new PanelSideBar(this, LEFT);
228 m_userPanelSidebar->stackUnder(m_friendsListPanel);
229 m_userPanel->stackUnder(m_userPanelSidebar);
231 connect(m_userPanel, SIGNAL(findUser(QPointF)),
232 this, SIGNAL(findUser(QPointF)));
234 connect(this, SIGNAL(userLocationReady(User*)),
235 m_userPanel, SLOT(userDataReceived(User*)));
237 connect(m_userPanel, SIGNAL(requestReverseGeo()),
238 this, SIGNAL(requestReverseGeo()));
240 connect(this, SIGNAL(reverseGeoReady(QString)),
241 m_userPanel, SIGNAL(reverseGeoReady(QString)));
243 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
244 this, SIGNAL(statusUpdate(QString,bool)));
246 connect(m_userPanel, SIGNAL(refreshUserData()),
247 this, SIGNAL(refreshUserData()));
249 connect(m_mapView, SIGNAL(viewResized(QSize)),
250 m_userPanel, SLOT(screenResized(QSize)));
253 void MainWindow::buildWebView()
255 qDebug() << __PRETTY_FUNCTION__;
258 m_webView = new QWebView;
260 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
261 this, SIGNAL(updateCredentials(QUrl)));
262 connect(m_webView, SIGNAL(loadFinished(bool)),
263 this, SLOT(loadDone(bool)));
269 void MainWindow::buildZoomButtonPanel()
271 qDebug() << __PRETTY_FUNCTION__;
273 m_zoomButtonPanel = new ZoomButtonPanel(this);
275 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
276 this, SIGNAL(zoomIn()));
278 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
279 this, SIGNAL(zoomOut()));
281 connect(this, SIGNAL(zoomLevelChanged(int)),
282 m_zoomButtonPanel, SLOT(resetButtons()));
284 connect(this, SIGNAL(maxZoomLevelReached()),
285 m_zoomButtonPanel, SLOT(disableZoomInButton()));
287 connect(this, SIGNAL(minZoomLevelReached()),
288 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
290 connect(m_mapView, SIGNAL(viewResized(QSize)),
291 m_zoomButtonPanel, SLOT(screenResized(QSize)));
294 void MainWindow::clearCookieJar()
296 qDebug() << __PRETTY_FUNCTION__;
301 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
303 QList<QNetworkCookie> emptyList;
306 m_cookieJar->setAllCookies(emptyList);
307 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
310 void MainWindow::createMenus()
312 qDebug() << __PRETTY_FUNCTION__;
315 m_loginAct = new QAction(tr("Login"), this);
316 connect(m_loginAct, SIGNAL(triggered()),
317 this, SIGNAL(loginActionPressed()));
320 m_toSettingsAct = new QAction(tr("Settings"), this);
321 connect(m_toSettingsAct, SIGNAL(triggered()),
322 this, SLOT(openSettingsDialog()));
325 m_gpsToggleAct = new QAction(tr("GPS"), this);
326 m_gpsToggleAct->setCheckable(true);
328 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
329 this, SIGNAL(gpsTriggered(bool)));
331 // automatic centering
332 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
333 m_autoCenteringAct->setCheckable(true);
334 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
335 this, SIGNAL(autoCenteringTriggered(bool)));
337 // build the actual menu
338 m_viewMenu = menuBar()->addMenu(tr("Main"));
339 m_viewMenu->addAction(m_loginAct);
340 m_viewMenu->addAction(m_toSettingsAct);
341 m_viewMenu->addAction(m_gpsToggleAct);
342 m_viewMenu->addAction(m_autoCenteringAct);
343 m_viewMenu->setObjectName(tr("Menu"));
346 void MainWindow::drawFullScreenButton(const QSize &size)
348 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
350 if(m_fullScreenButton) {
351 m_fullScreenButton->move(size.width() - m_fullScreenButton->size().width()
353 size.height() - m_fullScreenButton->size().height());
357 void MainWindow::drawOsmLicense(const QSize &size)
359 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
361 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
363 size.height() - m_osmLicense->fontMetrics().height());
366 void MainWindow::drawOwnLocationCrosshair(int width, int height)
368 qDebug() << __PRETTY_FUNCTION__;
370 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
371 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
372 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
376 void MainWindow::gpsTimeout()
378 qDebug() << __PRETTY_FUNCTION__;
380 showMaemoInformationBox(tr("GPS timeout"));
383 void MainWindow::grabZoomKeys(bool grab)
385 qDebug() << __PRETTY_FUNCTION__;
388 // Can't grab keys unless we have a window id
392 unsigned long val = (grab) ? 1 : 0;
393 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
397 XChangeProperty (QX11Info::display(),
403 reinterpret_cast<unsigned char *>(&val),
407 #endif // Q_WS_MAEMO_5
410 void MainWindow::keyPressEvent(QKeyEvent* event)
412 qDebug() << __PRETTY_FUNCTION__;
414 switch (event->key()) {
425 QWidget::keyPressEvent(event);
428 void MainWindow::loadCookies()
430 qDebug() << __PRETTY_FUNCTION__;
432 QSettings settings(DIRECTORY_NAME, FILE_NAME);
434 QStringList list = settings.value(COOKIES, EMPTY).toStringList();
436 if(!list.isEmpty()) {
437 QList<QNetworkCookie> cookieList;
438 for(int i=0;i<list.count();i++) {
439 cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
443 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
445 m_cookieJar->setAllCookies(cookieList);
446 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
451 void MainWindow::loadDone(bool done)
453 qDebug() << __PRETTY_FUNCTION__;
455 // for the first time the login page is opened, we need to refresh it to get cookies working
463 QWebFrame* frame = m_webView->page()->currentFrame();
467 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
469 foreach (QWebElement element, emailCollection) {
470 element.setAttribute("value", m_email.toAscii());
473 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
474 foreach (QWebElement element, passwordCollection) {
475 element.setAttribute("value", m_password.toAscii());
477 // find connect button
478 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
479 foreach (QWebElement element, buttonCollection)
481 QPoint pos(element.geometry().center());
483 // send a mouse click event to the web page
484 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
486 QApplication::sendEvent(m_webView->page(), &event0);
487 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
489 QApplication::sendEvent(m_webView->page(), &event1);
495 void MainWindow::loggedIn(bool logged)
497 qDebug() << __PRETTY_FUNCTION__;
502 m_loginAct->setText(tr("Logout"));
509 m_loginAct->setText(tr("Login"));
511 updateItemVisibility(m_loggedIn);
514 void MainWindow::loginFailed()
516 qDebug() << __PRETTY_FUNCTION__;
520 toggleProgressIndicator(false);
522 QStringList urlParts;
523 urlParts.append(FACEBOOK_LOGINBASE);
524 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
525 urlParts.append(INTERVAL1);
526 urlParts.append(SITUARE_LOGIN_SUCCESS);
527 urlParts.append(INTERVAL2);
528 urlParts.append(SITUARE_LOGIN_FAILURE);
529 urlParts.append(FACEBOOK_LOGIN_ENDING);
531 startLoginProcess(urlParts.join(EMPTY));
534 void MainWindow::loginUsingCookies()
536 qDebug() << __PRETTY_FUNCTION__;
541 QStringList urlParts;
542 urlParts.append(FACEBOOK_LOGINBASE);
543 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
544 urlParts.append(INTERVAL1);
545 urlParts.append(SITUARE_LOGIN_SUCCESS);
546 urlParts.append(INTERVAL2);
547 urlParts.append(SITUARE_LOGIN_FAILURE);
548 urlParts.append(FACEBOOK_LOGIN_ENDING);
550 m_webView->load(QUrl(urlParts.join(EMPTY)));
554 void MainWindow::openSettingsDialog()
556 qDebug() << __PRETTY_FUNCTION__;
558 m_settingsDialog->enableSituareSettings(m_gpsToggleAct->isChecked() && m_loggedIn);
559 m_settingsDialog->show();
562 void MainWindow::saveCookies()
564 qDebug() << __PRETTY_FUNCTION__;
567 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
569 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
572 for(int i=0;i<cookieList.count();i++) {
573 QNetworkCookie cookie = cookieList.at(i);
574 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
575 list.append(QString(byteArray));
577 list.removeDuplicates();
579 QSettings settings(DIRECTORY_NAME, FILE_NAME);
580 settings.setValue(COOKIES, list);
583 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
585 qDebug() << __PRETTY_FUNCTION__;
587 m_autoCenteringAct->setChecked(enabled);
590 void MainWindow::setGPSButtonEnabled(bool enabled)
592 qDebug() << __PRETTY_FUNCTION__;
594 m_gpsToggleAct->setChecked(enabled);
597 setOwnLocationCrosshairVisibility(!enabled);
599 m_autoCenteringAct->setVisible(enabled);
602 void MainWindow::setMapViewScene(QGraphicsScene *scene)
604 qDebug() << __PRETTY_FUNCTION__;
606 m_mapView->setScene(scene);
609 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
611 qDebug() << __PRETTY_FUNCTION__;
614 m_ownLocationCrosshair->show();
615 m_drawOwnLocationCrosshair = true;
616 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
619 m_ownLocationCrosshair->hide();
620 m_drawOwnLocationCrosshair = false;
624 void MainWindow::setUsername(const QString &username)
626 qDebug() << __PRETTY_FUNCTION__;
631 void MainWindow::setViewPortSize(int width, int height)
633 qDebug() << __PRETTY_FUNCTION__;
635 m_viewPortWidth = width;
636 m_viewPortHeight = height;
639 bool MainWindow::showEnableAutomaticUpdateLocationDialog()
641 QMessageBox msgBox(QMessageBox::Warning, tr("Automatic location update"),
642 tr("Are you sure you want to enable automatic location update?"),
643 QMessageBox::Ok | QMessageBox::Cancel, 0);
644 msgBox.button(QMessageBox::Ok)->setText(tr("Ok"));
645 int ret = msgBox.exec();
647 if (ret == QMessageBox::Ok)
653 void MainWindow::showMaemoInformationBox(const QString &message, bool modal)
655 qDebug() << __PRETTY_FUNCTION__;
659 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::NoTimeout);
662 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
666 QMessageBox::information(this, tr("Situare"), message, QMessageBox::Ok);
670 void MainWindow::toggleFullScreen()
672 qDebug() << __PRETTY_FUNCTION__;
674 if(windowState() == Qt::WindowNoState)
680 void MainWindow::startLoginProcess(const QUrl &url)
682 qDebug() << __PRETTY_FUNCTION__;
686 LoginDialog loginDialog;
688 emit fetchUsernameFromSettings();
691 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
693 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
695 loginDialog.clearTextFields();
697 if(!m_email.isEmpty())
698 loginDialog.setEmailField(m_email);
700 if(loginDialog.exec() != QDialog::Accepted) {
701 // if login dialog was canceled we need to stop processing webview
704 emit cancelLoginProcess();
707 loginDialog.userInput(m_email, m_password);
708 emit saveUsername(m_email);
709 m_webView->load(url);
710 toggleProgressIndicator(true);
715 void MainWindow::toggleProgressIndicator(bool value)
717 qDebug() << __PRETTY_FUNCTION__;
720 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
723 #endif // Q_WS_MAEMO_5
726 void MainWindow::updateItemVisibility(bool show)
728 qDebug() << __PRETTY_FUNCTION__;
731 m_friendsListPanel->show();
732 m_friendsListPanelSidebar->show();
734 m_userPanelSidebar->show();
736 if(m_drawOwnLocationCrosshair) {
737 m_ownLocationCrosshair->show();
738 setGPSButtonEnabled(false);
739 emit gpsTriggered(false);
743 m_friendsListPanel->closePanel();
744 m_friendsListPanel->hide();
745 m_friendsListPanelSidebar->hide();
746 m_userPanel->closePanel();
748 m_userPanelSidebar->hide();
749 m_ownLocationCrosshair->hide();
753 const QString MainWindow::username()
755 qDebug() << __PRETTY_FUNCTION__;