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
10 Situare is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 version 2 as published by the Free Software Foundation.
14 Situare is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Situare; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #include <QtMaemo5/QMaemo5InformationBox>
30 #endif // Q_WS_MAEMO_5
33 #include "facebookservice/facebookauthentication.h"
34 #include "friendlistpanel.h"
35 #include "logindialog.h"
36 #include "map/mapview.h"
37 #include "panelsidebar.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;
52 MainWindow::MainWindow(QWidget *parent)
53 : QMainWindow(parent),
54 m_drawOwnLocationCrosshair(false),
61 qDebug() << __PRETTY_FUNCTION__;
66 QHBoxLayout *layout = new QHBoxLayout;
67 layout->addWidget(m_mapView);
69 setCentralWidget(new QWidget());
70 centralWidget()->setLayout(layout);
72 buildFriendListPanel();
76 setWindowTitle(tr("Situare"));
78 // set stacking order of widgets
79 m_zoomButtonPanel->stackUnder(m_userPanel);
80 m_osmLicense->stackUnder(m_zoomButtonPanel);
81 m_ownLocationCrosshair->stackUnder(m_osmLicense);
82 m_mapView->stackUnder(m_ownLocationCrosshair);
84 this->toggleProgressIndicator(true);
88 // set screen size in desktop matching N900 screen size
89 resize(N900_APP_WIDTH, N900_APP_HEIGHT);
92 MainWindow::~MainWindow()
94 qDebug() << __PRETTY_FUNCTION__;
102 void MainWindow::buildFriendListPanel()
104 qDebug() << __PRETTY_FUNCTION__;
106 m_friendsListPanel = new FriendListPanel(this);
107 PanelSideBar *friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
109 m_friendsListPanel->stackUnder(friendsListPanelSidebar);
111 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
112 m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
114 connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
115 this, SIGNAL(findFriend(QPointF)));
117 connect(m_mapView, SIGNAL(viewResized(QSize)),
118 m_friendsListPanel, SLOT(screenResized(QSize)));
120 connect(m_mapView, SIGNAL(viewResized(QSize)),
121 m_zoomButtonPanel, SLOT(screenResized(QSize)));
123 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
124 friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
127 void MainWindow::buildManualLocationCrosshair()
129 qDebug() << __PRETTY_FUNCTION__;
131 m_ownLocationCrosshair = new QLabel(this);
132 QPixmap crosshairImage(":/res/images/sight.png");
133 m_ownLocationCrosshair->setPixmap(crosshairImage);
134 m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
135 m_ownLocationCrosshair->hide();
136 m_ownLocationCrosshair->setAttribute(Qt::WA_TransparentForMouseEvents, true);
138 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
139 this, SLOT(drawOwnLocationCrosshair(int, int)));
142 void MainWindow::buildMap()
144 qDebug() << __PRETTY_FUNCTION__;
146 m_mapView = new MapView(this);
148 buildZoomButtonPanel();
150 m_ownLocationCrosshair = 0;
152 buildManualLocationCrosshair();
154 connect(m_mapView, SIGNAL(viewScrolled(QPoint)),
155 this, SIGNAL(mapViewScrolled(QPoint)));
157 connect(this, SIGNAL(centerToSceneCoordinates(QPoint)),
158 m_mapView, SLOT(centerToSceneCoordinates(QPoint)));
160 connect(m_mapView, SIGNAL(viewResized(QSize)),
161 this, SIGNAL(mapViewResized(QSize)));
163 connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
164 this, SLOT(setViewPortSize(int, int)));
166 connect(this, SIGNAL(zoomLevelChanged(int)),
167 m_mapView, SLOT(setZoomLevel(int)));
169 connect(m_mapView, SIGNAL(viewZoomFinished()),
170 this, SIGNAL(viewZoomFinished()));
173 void MainWindow::buildOsmLicense()
175 qWarning() << __PRETTY_FUNCTION__;
177 m_osmLicense = new QLabel(this);
178 m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
179 m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
180 m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
181 m_osmLicense->setFont(QFont("Nokia Sans", 9));
182 m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
183 m_osmLicense->fontMetrics().height());
185 connect(m_mapView, SIGNAL(viewResized(QSize)),
186 this, SLOT(drawOsmLicense(QSize)));
189 void MainWindow::buildUserInfoPanel()
191 qDebug() << __PRETTY_FUNCTION__;
193 m_userPanel = new UserInfoPanel(this);
194 PanelSideBar *userPanelSidebar = new PanelSideBar(this, LEFT);
196 userPanelSidebar->stackUnder(m_friendsListPanel);
197 m_userPanel->stackUnder(userPanelSidebar);
199 connect(this, SIGNAL(userLocationReady(User*)),
200 m_userPanel, SLOT(userDataReceived(User*)));
202 connect(m_userPanel, SIGNAL(requestReverseGeo()),
203 this, SIGNAL(requestReverseGeo()));
205 connect(this, SIGNAL(reverseGeoReady(QString)),
206 m_userPanel, SIGNAL(reverseGeoReady(QString)));
208 connect(m_userPanel, SIGNAL(statusUpdate(QString,bool)),
209 this, SIGNAL(statusUpdate(QString,bool)));
211 connect(m_userPanel, SIGNAL(refreshUserData()),
212 this, SIGNAL(refreshUserData()));
214 connect(m_mapView, SIGNAL(viewResized(QSize)),
215 m_userPanel, SLOT(screenResized(QSize)));
218 void MainWindow::buildZoomButtonPanel()
220 qDebug() << __PRETTY_FUNCTION__;
222 m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
223 ZOOM_BUTTON_PANEL_POSITION_Y);
225 connect(m_zoomButtonPanel->m_zoomInButton, SIGNAL(clicked()),
226 this, SIGNAL(zoomIn()));
228 connect(m_zoomButtonPanel->m_zoomOutButton, SIGNAL(clicked()),
229 this, SIGNAL(zoomOut()));
231 connect(this, SIGNAL(zoomLevelChanged(int)),
232 m_zoomButtonPanel, SLOT(resetButtons()));
234 connect(this, SIGNAL(maxZoomLevelReached()),
235 m_zoomButtonPanel, SLOT(disableZoomInButton()));
237 connect(this, SIGNAL(minZoomLevelReached()),
238 m_zoomButtonPanel, SLOT(disableZoomOutButton()));
240 QSettings settings(DIRECTORY_NAME, FILE_NAME);
241 m_zoomButtonPanel->move(settings.value(ZOOMPANEL_POSITION,
242 QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
243 ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint());
246 void MainWindow::createMenus()
248 qDebug() << __PRETTY_FUNCTION__;
251 m_toSettingsAct = new QAction(tr("Settings"), this);
252 m_toSettingsAct->setObjectName(tr("Settings"));
253 connect(m_toSettingsAct, SIGNAL(triggered()),
254 this, SLOT(openSettingsDialog()));
257 m_gpsToggleAct = new QAction(tr("GPS"), this);
258 m_gpsToggleAct->setCheckable(true);
260 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
261 this, SIGNAL(gpsTriggered(bool)));
263 // automatic centering
264 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
265 m_autoCenteringAct->setCheckable(true);
266 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
267 this, SIGNAL(autoCenteringTriggered(bool)));
269 // build the actual menu
270 m_viewMenu = menuBar()->addMenu(tr("Main"));
271 m_viewMenu->addAction(m_toSettingsAct);
272 m_viewMenu->addAction(m_gpsToggleAct);
273 m_viewMenu->addAction(m_autoCenteringAct);
274 m_viewMenu->setObjectName(tr("Menu"));
277 void MainWindow::drawOsmLicense(const QSize &size)
279 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
281 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
282 size.height() - m_osmLicense->fontMetrics().height());
286 void MainWindow::drawOwnLocationCrosshair(int width, int height)
288 qDebug() << __PRETTY_FUNCTION__;
290 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
291 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
292 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
296 void MainWindow::gpsError(const QString &message)
298 qDebug() << __PRETTY_FUNCTION__;
300 showMaemoInformationBox(message);
303 void MainWindow::gpsTimeout()
305 qDebug() << __PRETTY_FUNCTION__;
307 showMaemoInformationBox(tr("GPS timeout"));
310 void MainWindow::grabZoomKeys(bool grab)
312 qDebug() << __PRETTY_FUNCTION__;
315 // Can't grab keys unless we have a window id
319 unsigned long val = (grab) ? 1 : 0;
320 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
324 XChangeProperty (QX11Info::display(),
330 reinterpret_cast<unsigned char *>(&val),
334 #endif // Q_WS_MAEMO_5
337 void MainWindow::keyPressEvent(QKeyEvent* event)
339 qDebug() << __PRETTY_FUNCTION__;
341 switch (event->key()) {
352 QWidget::keyPressEvent(event);
355 void MainWindow::loadDone(bool done)
357 qDebug() << __PRETTY_FUNCTION__;
359 // for the first time the login page is opened, we need to refresh it to get cookies working
367 QWebFrame* frame = m_webView->page()->currentFrame();
371 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
373 foreach (QWebElement element, emailCollection) {
374 element.setAttribute("value", m_email.toAscii());
377 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
378 foreach (QWebElement element, passwordCollection) {
379 element.setAttribute("value", m_password.toAscii());
381 // find connect button
382 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
383 foreach (QWebElement element, buttonCollection)
385 QPoint pos(element.geometry().center());
387 // send a mouse click event to the web page
388 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
390 QApplication::sendEvent(m_webView->page(), &event0);
391 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
393 QApplication::sendEvent(m_webView->page(), &event1);
399 void MainWindow::loginDialogDone(const QString &email, const QString &password)
401 qDebug() << __PRETTY_FUNCTION__;
404 m_password = password;
407 void MainWindow::loginFailed()
409 qDebug() << __PRETTY_FUNCTION__;
414 toggleProgressIndicator(false);
417 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
418 QMaemo5InformationBox::NoTimeout);
420 #endif // Q_WS_MAEMO_5
422 if(!m_email.isEmpty()) {
423 m_loginDialog->setEmailField(m_email);
426 if(m_loginDialog->exec() != QDialog::Accepted) {
427 // if login dialog was canceled we need to stop processing webview
428 // stop and disconnect m_webView;
430 disconnect(m_webView, SIGNAL(loadFinished(bool)),
431 this, SLOT(loadDone(bool)));
432 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
433 this, SLOT(updateCredentials(const QUrl &)));
435 emit cancelLoginProcess();
438 // re-load login page for webview
439 toggleProgressIndicator(true);
440 m_webView->load(m_loginUrl);
444 void MainWindow::openSettingsDialog()
446 qDebug() << __PRETTY_FUNCTION__;
448 SettingsDialog *dialog = new SettingsDialog(this);
452 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
454 qDebug() << __PRETTY_FUNCTION__;
456 m_autoCenteringAct->setChecked(enabled);
459 void MainWindow::setGPSButtonEnabled(bool enabled)
461 qDebug() << __PRETTY_FUNCTION__;
463 m_gpsToggleAct->setChecked(enabled);
464 setOwnLocationCrosshairVisibility(!enabled);
465 m_autoCenteringAct->setVisible(enabled);
468 void MainWindow::setMapViewScene(QGraphicsScene *scene)
470 qDebug() << __PRETTY_FUNCTION__;
472 m_mapView->setScene(scene);
475 void MainWindow::setUsername(const QString &username)
477 qDebug() << __PRETTY_FUNCTION__;
481 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
483 qDebug() << __PRETTY_FUNCTION__;
486 m_ownLocationCrosshair->show();
487 m_drawOwnLocationCrosshair = true;
488 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
491 m_ownLocationCrosshair->hide();
492 m_drawOwnLocationCrosshair = false;
496 void MainWindow::setViewPortSize(int width, int height)
498 qDebug() << __PRETTY_FUNCTION__;
500 m_viewPortWidth = width;
501 m_viewPortHeight = height;
504 void MainWindow::showMaemoInformationBox(const QString &message)
506 qDebug() << __PRETTY_FUNCTION__;
509 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
515 void MainWindow::startLoginProcess(const QUrl &url)
517 qDebug() << __PRETTY_FUNCTION__;
520 m_webView = new QWebView;
521 m_loginDialog = new LoginDialog(this);
523 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
524 this, SIGNAL(updateCredentials(QUrl)));
525 connect(m_webView, SIGNAL(loadFinished(bool)),
526 this, SLOT(loadDone(bool)));
528 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
529 this, SLOT(loginDialogDone(QString,QString)));
533 emit fetchUsernameFromSettings();
535 if(!m_email.isEmpty()) {
536 m_loginDialog->setEmailField(m_email);
539 if(m_loginDialog->exec() != QDialog::Accepted) {
540 // if login dialog was canceled we need to stop processing webview
541 // stop and disconnect m_webView;
543 disconnect(m_webView, SIGNAL(loadFinished(bool)),
544 this, SLOT(loadDone(bool)));
545 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
546 this, SLOT(updateCredentials(const QUrl &)));
548 emit cancelLoginProcess();
551 m_webView->load(m_loginUrl);
552 toggleProgressIndicator(true);
557 void MainWindow::toggleProgressIndicator(bool value)
559 qDebug() << __PRETTY_FUNCTION__;
562 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
565 #endif // Q_WS_MAEMO_5
568 const QString MainWindow::username()
570 qDebug() << __PRETTY_FUNCTION__;