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 <QtMaemo5/QMaemo5InformationBox>
31 #endif // Q_WS_MAEMO_5
34 #include "facebookservice/facebookauthentication.h"
35 #include "friendlistpanel.h"
36 #include "logindialog.h"
37 #include "map/mapview.h"
38 #include "settingsdialog.h"
39 #include "userinfopanel.h"
40 #include "zoombuttonpanel.h"
42 #include "mainwindow.h"
44 #include <QtGui/QX11Info>
46 #include <X11/Xatom.h>
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;
53 MainWindow::MainWindow(QWidget *parent)
54 : QMainWindow(parent),
55 m_drawOwnLocationCrosshair(false),
63 qDebug() << __PRETTY_FUNCTION__;
68 QHBoxLayout *layout = new QHBoxLayout;
69 layout->addWidget(m_mapView);
71 setCentralWidget(new QWidget());
72 centralWidget()->setLayout(layout);
74 buildFriendListPanel();
78 setWindowTitle(tr("Situare"));
80 // set stacking order of widgets
81 m_zoomButtonPanel->stackUnder(m_userPanel);
82 m_osmLicense->stackUnder(m_zoomButtonPanel);
83 m_ownLocationCrosshair->stackUnder(m_osmLicense);
84 m_mapView->stackUnder(m_ownLocationCrosshair);
86 this->toggleProgressIndicator(true);
90 // set screen size in desktop matching N900 screen size
91 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
94 MainWindow::~MainWindow()
96 qDebug() << __PRETTY_FUNCTION__;
104 void MainWindow::buildFriendListPanel()
106 qDebug() << __PRETTY_FUNCTION__;
108 m_friendsListPanel = new FriendListPanel(this);
109 m_friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
111 m_friendsListPanel->stackUnder(m_friendsListPanelSidebar);
113 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
114 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
116 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
117 this, SIGNAL(findFriend(QPointF)));
119 connect(m_mapView, SIGNAL(viewResized(QSize)),
120 m_friendsListPanel, SLOT(screenResized(QSize)));
122 connect(this, SIGNAL(locationItemClicked(QList<QString>)),
123 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
125 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
126 m_friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
129 void MainWindow::buildLoginDialog()
131 qDebug() << __PRETTY_FUNCTION__;
134 m_loginDialog = new LoginDialog(this);
136 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
137 this, SLOT(loginDialogDone(QString,QString)));
141 void MainWindow::buildManualLocationCrosshair()
143 qDebug() << __PRETTY_FUNCTION__;
145 m_ownLocationCrosshair = new QLabel(this);
146 QPixmap crosshairImage(":/res/images/sight.png");
147 m_ownLocationCrosshair->setPixmap(crosshairImage);
148 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
149 m_ownLocationCrosshair->hide();
150 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
152 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
153 this, SLOT(drawOwnLocationCrosshair(int, int)));
156 void MainWindow::buildMap()
158 qDebug() << __PRETTY_FUNCTION__;
160 m_mapView = new MapView(this);
162 buildZoomButtonPanel();
164 m_ownLocationCrosshair = 0;
166 buildManualLocationCrosshair();
168 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
169 this, SIGNAL(mapViewScrolled(QPoint)));
171 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
172 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
174 connect(m_mapView, SIGNAL(viewResized(QSize)),
175 this, SIGNAL(mapViewResized(QSize)));
177 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
178 this, SLOT(setViewPortSize(int, int)));
180 connect(this, SIGNAL(zoomLevelChanged(int)),
181 m_mapView, SLOT(setZoomLevel(int)));
183 connect(m_mapView, SIGNAL(viewZoomFinished()),
184 this, SIGNAL(viewZoomFinished()));
187 void MainWindow::buildOsmLicense()
189 qDebug() << __PRETTY_FUNCTION__;
191 m_osmLicense = new QLabel(this);
192 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
193 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
194 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
195 m_osmLicense->setFont(QFont("Nokia Sans", 9));
196 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
197 m_osmLicense->fontMetrics().height());
199 connect(m_mapView, SIGNAL(viewResized(QSize)),
200 this, SLOT(drawOsmLicense(QSize)));
203 void MainWindow::buildUserInfoPanel()
205 qDebug() << __PRETTY_FUNCTION__;
207 m_userPanel = new UserInfoPanel(this);
208 m_userPanelSidebar = new PanelSideBar(this, LEFT);
210 m_userPanelSidebar->stackUnder(m_friendsListPanel);
211 m_userPanel->stackUnder(m_userPanelSidebar);
213 connect(m_userPanel, SIGNAL(findUser(QPointF)),
214 this, SIGNAL(findUser(QPointF)));
216 connect(this, SIGNAL(userLocationReady(User*)),
217 m_userPanel, SLOT(userDataReceived(User*)));
219 connect(m_userPanel, SIGNAL(requestReverseGeo()),
220 this, SIGNAL(requestReverseGeo()));
222 connect(this, SIGNAL(reverseGeoReady(QString)),
223 m_userPanel, SIGNAL(reverseGeoReady(QString)));
225 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
226 this, SIGNAL(statusUpdate(QString,bool)));
228 connect(m_userPanel, SIGNAL(refreshUserData()),
229 this, SIGNAL(refreshUserData()));
231 connect(m_mapView, SIGNAL(viewResized(QSize)),
232 m_userPanel, SLOT(screenResized(QSize)));
235 void MainWindow::buildWebView()
237 qDebug() << __PRETTY_FUNCTION__;
240 m_webView = new QWebView;
242 connectWebViewSignals(true);
248 void MainWindow::buildZoomButtonPanel()
250 qDebug() << __PRETTY_FUNCTION__;
252 m_zoomButtonPanel = new ZoomButtonPanel(this);
254 connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
255 this, SIGNAL(zoomIn()));
257 connect(m_zoomButtonPanel->zoomOutButton(), SIGNAL(clicked()),
258 this, SIGNAL(zoomOut()));
260 connect(this, SIGNAL(zoomLevelChanged(int)),
261 m_zoomButtonPanel, SLOT(resetButtons()));
263 connect(this, SIGNAL(maxZoomLevelReached()),
264 m_zoomButtonPanel, SLOT(disableZoomInButton()));
266 connect(this, SIGNAL(minZoomLevelReached()),
267 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
269 connect(m_mapView, SIGNAL(viewResized(QSize)),
270 m_zoomButtonPanel, SLOT(screenResized(QSize)));
273 void MainWindow::clearCookieJar()
275 qDebug() << __PRETTY_FUNCTION__;
280 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
281 qDebug() << "create cookie jar";
283 QList<QNetworkCookie> emptyList;
286 m_cookieJar->setAllCookies(emptyList);
287 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
290 void MainWindow::connectWebViewSignals(bool connectSignals)
292 qDebug() << __PRETTY_FUNCTION__;
295 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
296 this, SIGNAL(updateCredentials(QUrl)));
297 connect(m_webView, SIGNAL(loadFinished(bool)),
298 this, SLOT(loadDone(bool)));
301 disconnect(m_webView, SIGNAL(loadFinished(bool)),
302 this, SLOT(loadDone(bool)));
303 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
304 this, SLOT(updateCredentials(const QUrl &)));
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::drawOsmLicense(const QSize &size)
346 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
348 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE)
350 size.height() - m_osmLicense->fontMetrics().height());
354 void MainWindow::drawOwnLocationCrosshair(int width, int height)
356 qDebug() << __PRETTY_FUNCTION__;
358 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
359 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
360 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
364 void MainWindow::gpsError(const QString &message)
366 qDebug() << __PRETTY_FUNCTION__;
368 showMaemoInformationBox(message);
371 void MainWindow::gpsTimeout()
373 qDebug() << __PRETTY_FUNCTION__;
375 showMaemoInformationBox(tr("GPS timeout"));
378 void MainWindow::grabZoomKeys(bool grab)
380 qDebug() << __PRETTY_FUNCTION__;
383 // Can't grab keys unless we have a window id
387 unsigned long val = (grab) ? 1 : 0;
388 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
392 XChangeProperty (QX11Info::display(),
398 reinterpret_cast<unsigned char *>(&val),
402 #endif // Q_WS_MAEMO_5
405 void MainWindow::keyPressEvent(QKeyEvent* event)
407 qDebug() << __PRETTY_FUNCTION__;
409 switch (event->key()) {
420 QWidget::keyPressEvent(event);
423 void MainWindow::loadCookies()
425 qDebug() << __PRETTY_FUNCTION__;
427 QSettings settings(DIRECTORY_NAME, FILE_NAME);
429 QString cookies = settings.value(COOKIES, EMPTY).toString();
430 if(!cookies.isEmpty()) {
431 QStringList list = cookies.split("|");
432 QList<QNetworkCookie> cookieList;
434 for(int i=0;i<list.count();i++) {
435 QNetworkCookie tmpCookie;
436 QList<QNetworkCookie> parserList = tmpCookie.parseCookies(list.at(i).toAscii());
437 cookieList.append(parserList.at(0));
440 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
441 qDebug() << "create cookie jar";
444 m_cookieJar->setAllCookies(cookieList);
445 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"));
508 m_loginDialog->clearTextFields();
510 m_loginAct->setText(tr("Login"));
512 showPanels(m_loggedIn);
515 void MainWindow::loginDialogDone(const QString &email, const QString &password)
517 qDebug() << __PRETTY_FUNCTION__;
520 m_password = password;
523 void MainWindow::loginFailed()
525 qDebug() << __PRETTY_FUNCTION__;
527 emit fetchUsernameFromSettings();
529 toggleProgressIndicator(false);
532 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
533 QMaemo5InformationBox::NoTimeout);
535 #endif // Q_WS_MAEMO_5
539 if(!m_email.isEmpty()) {
540 m_loginDialog->setEmailField(m_email);
543 if(m_loginDialog->exec() != QDialog::Accepted) {
544 // if login dialog was canceled we need to stop processing webview
545 // stop and disconnect m_webView;
547 connectWebViewSignals(false);
549 emit cancelLoginProcess();
552 // re-load login page for webview
553 toggleProgressIndicator(true);
555 // connect webview signals again since they might have been disconnected
556 connectWebViewSignals(true);
558 QStringList urlParts;
559 urlParts.append(FACEBOOK_LOGINBASE);
560 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
561 urlParts.append(INTERVAL1);
562 urlParts.append(SITUARE_LOGIN_SUCCESS);
563 urlParts.append(INTERVAL2);
564 urlParts.append(SITUARE_LOGIN_FAILURE);
565 urlParts.append(FACEBOOK_LOGIN_ENDING);
567 m_webView->load(QUrl(urlParts.join(EMPTY)));
571 void MainWindow::loginUsingCookies()
573 qDebug() << __PRETTY_FUNCTION__;
578 QStringList urlParts;
579 urlParts.append(FACEBOOK_LOGINBASE);
580 urlParts.append(SITUARE_PUBLIC_FACEBOOKAPI_KEY);
581 urlParts.append(INTERVAL1);
582 urlParts.append(SITUARE_LOGIN_SUCCESS);
583 urlParts.append(INTERVAL2);
584 urlParts.append(SITUARE_LOGIN_FAILURE);
585 urlParts.append(FACEBOOK_LOGIN_ENDING);
587 m_webView->load(QUrl(urlParts.join(EMPTY)));
591 void MainWindow::openSettingsDialog()
593 qDebug() << __PRETTY_FUNCTION__;
595 SettingsDialog *dialog = new SettingsDialog(this);
597 dialog->disableSituareSettings();
602 void MainWindow::saveCookies()
604 qDebug() << __PRETTY_FUNCTION__;
607 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
609 QList<QNetworkCookie> cookieList = m_cookieJar->allCookies();
612 for(int i=0;i<cookieList.count();i++) {
613 QNetworkCookie cookie = cookieList.at(i);
614 QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
615 list.append(QString(byteArray));
617 QSettings settings(DIRECTORY_NAME, FILE_NAME);
619 settings.setValue(COOKIES, list.join("|"));
622 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
624 qDebug() << __PRETTY_FUNCTION__;
626 m_autoCenteringAct->setChecked(enabled);
629 void MainWindow::setGPSButtonEnabled(bool enabled)
631 qDebug() << __PRETTY_FUNCTION__;
633 m_gpsToggleAct->setChecked(enabled);
634 setOwnLocationCrosshairVisibility(!enabled);
635 m_autoCenteringAct->setVisible(enabled);
638 void MainWindow::setMapViewScene(QGraphicsScene *scene)
640 qDebug() << __PRETTY_FUNCTION__;
642 m_mapView->setScene(scene);
645 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
647 qDebug() << __PRETTY_FUNCTION__;
650 m_ownLocationCrosshair->show();
651 m_drawOwnLocationCrosshair = true;
652 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
655 m_ownLocationCrosshair->hide();
656 m_drawOwnLocationCrosshair = false;
660 void MainWindow::setUsername(const QString &username)
662 qDebug() << __PRETTY_FUNCTION__;
666 void MainWindow::setViewPortSize(int width, int height)
668 qDebug() << __PRETTY_FUNCTION__;
670 m_viewPortWidth = width;
671 m_viewPortHeight = height;
674 void MainWindow::showMaemoInformationBox(const QString &message)
676 qDebug() << __PRETTY_FUNCTION__;
679 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
685 void MainWindow::showPanels(bool show)
687 qDebug() << __PRETTY_FUNCTION__;
689 m_friendsListPanel->show();
690 m_friendsListPanelSidebar->show();
692 m_userPanelSidebar->show();
695 m_friendsListPanel->closePanel();
696 m_friendsListPanel->hide();
697 m_friendsListPanelSidebar->hide();
698 m_userPanel->closePanel();
700 m_userPanelSidebar->hide();
704 void MainWindow::startLoginProcess(const QUrl &url)
706 qDebug() << __PRETTY_FUNCTION__;
711 emit fetchUsernameFromSettings();
714 m_cookieJar = new NetworkCookieJar(new QNetworkCookieJar(this));
716 m_webView->page()->networkAccessManager()->setCookieJar(m_cookieJar);
718 if(!m_email.isEmpty()) {
719 m_loginDialog->setEmailField(m_email);
722 if(m_loginDialog->exec() != QDialog::Accepted) {
723 // if login dialog was canceled we need to stop processing webview
724 // stop and disconnect m_webView;
726 connectWebViewSignals(false);
728 emit cancelLoginProcess();
731 // connect webview signals again since they might have been disconnected
732 connectWebViewSignals(true);
734 m_webView->load(url);
735 toggleProgressIndicator(true);
740 void MainWindow::toggleProgressIndicator(bool value)
742 qDebug() << __PRETTY_FUNCTION__;
745 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
748 #endif // Q_WS_MAEMO_5
751 const QString MainWindow::username()
753 qDebug() << __PRETTY_FUNCTION__;