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(this, SIGNAL(locationItemClicked(QList<QString>)),
121 m_friendsListPanel, SLOT(showFriendsInList(QList<QString>)));
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()));
241 void MainWindow::createMenus()
243 qDebug() << __PRETTY_FUNCTION__;
246 m_toSettingsAct = new QAction(tr("Settings"), this);
247 m_toSettingsAct->setObjectName(tr("Settings"));
248 connect(m_toSettingsAct, SIGNAL(triggered()),
249 this, SLOT(openSettingsDialog()));
252 m_gpsToggleAct = new QAction(tr("GPS"), this);
253 m_gpsToggleAct->setCheckable(true);
255 connect(m_gpsToggleAct, SIGNAL(triggered(bool)),
256 this, SIGNAL(gpsTriggered(bool)));
258 // automatic centering
259 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
260 m_autoCenteringAct->setCheckable(true);
261 connect(m_autoCenteringAct, SIGNAL(triggered(bool)),
262 this, SIGNAL(autoCenteringTriggered(bool)));
264 // build the actual menu
265 m_viewMenu = menuBar()->addMenu(tr("Main"));
266 m_viewMenu->addAction(m_toSettingsAct);
267 m_viewMenu->addAction(m_gpsToggleAct);
268 m_viewMenu->addAction(m_autoCenteringAct);
269 m_viewMenu->setObjectName(tr("Menu"));
272 void MainWindow::drawOsmLicense(const QSize &size)
274 qDebug() << __PRETTY_FUNCTION__ << size.width() << "x" << size.height();
276 m_osmLicense->move(size.width() - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
277 size.height() - m_osmLicense->fontMetrics().height());
281 void MainWindow::drawOwnLocationCrosshair(int width, int height)
283 qDebug() << __PRETTY_FUNCTION__;
285 if (m_drawOwnLocationCrosshair && m_ownLocationCrosshair != 0) {
286 m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
287 height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
291 void MainWindow::gpsError(const QString &message)
293 qDebug() << __PRETTY_FUNCTION__;
295 showMaemoInformationBox(message);
298 void MainWindow::gpsTimeout()
300 qDebug() << __PRETTY_FUNCTION__;
302 showMaemoInformationBox(tr("GPS timeout"));
305 void MainWindow::grabZoomKeys(bool grab)
307 qDebug() << __PRETTY_FUNCTION__;
310 // Can't grab keys unless we have a window id
314 unsigned long val = (grab) ? 1 : 0;
315 Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
319 XChangeProperty (QX11Info::display(),
325 reinterpret_cast<unsigned char *>(&val),
329 #endif // Q_WS_MAEMO_5
332 void MainWindow::keyPressEvent(QKeyEvent* event)
334 qDebug() << __PRETTY_FUNCTION__;
336 switch (event->key()) {
347 QWidget::keyPressEvent(event);
350 void MainWindow::loadDone(bool done)
352 qDebug() << __PRETTY_FUNCTION__;
354 // for the first time the login page is opened, we need to refresh it to get cookies working
362 QWebFrame* frame = m_webView->page()->currentFrame();
366 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
368 foreach (QWebElement element, emailCollection) {
369 element.setAttribute("value", m_email.toAscii());
372 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
373 foreach (QWebElement element, passwordCollection) {
374 element.setAttribute("value", m_password.toAscii());
376 // find connect button
377 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
378 foreach (QWebElement element, buttonCollection)
380 QPoint pos(element.geometry().center());
382 // send a mouse click event to the web page
383 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
385 QApplication::sendEvent(m_webView->page(), &event0);
386 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
388 QApplication::sendEvent(m_webView->page(), &event1);
394 void MainWindow::loginDialogDone(const QString &email, const QString &password)
396 qDebug() << __PRETTY_FUNCTION__;
399 m_password = password;
402 void MainWindow::loginFailed()
404 qDebug() << __PRETTY_FUNCTION__;
409 toggleProgressIndicator(false);
412 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
413 QMaemo5InformationBox::NoTimeout);
415 #endif // Q_WS_MAEMO_5
417 if(!m_email.isEmpty()) {
418 m_loginDialog->setEmailField(m_email);
421 if(m_loginDialog->exec() != QDialog::Accepted) {
422 // if login dialog was canceled we need to stop processing webview
423 // stop and disconnect m_webView;
425 disconnect(m_webView, SIGNAL(loadFinished(bool)),
426 this, SLOT(loadDone(bool)));
427 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
428 this, SLOT(updateCredentials(const QUrl &)));
430 emit cancelLoginProcess();
433 // re-load login page for webview
434 toggleProgressIndicator(true);
435 m_webView->load(m_loginUrl);
439 void MainWindow::openSettingsDialog()
441 qDebug() << __PRETTY_FUNCTION__;
443 SettingsDialog *dialog = new SettingsDialog(this);
447 void MainWindow::setAutoCenteringButtonEnabled(bool enabled)
449 qDebug() << __PRETTY_FUNCTION__;
451 m_autoCenteringAct->setChecked(enabled);
454 void MainWindow::setGPSButtonEnabled(bool enabled)
456 qDebug() << __PRETTY_FUNCTION__;
458 m_gpsToggleAct->setChecked(enabled);
459 setOwnLocationCrosshairVisibility(!enabled);
460 m_autoCenteringAct->setVisible(enabled);
463 void MainWindow::setMapViewScene(QGraphicsScene *scene)
465 qDebug() << __PRETTY_FUNCTION__;
467 m_mapView->setScene(scene);
470 void MainWindow::setUsername(const QString &username)
472 qDebug() << __PRETTY_FUNCTION__;
476 void MainWindow::setOwnLocationCrosshairVisibility(bool visibility)
478 qDebug() << __PRETTY_FUNCTION__;
481 m_ownLocationCrosshair->show();
482 m_drawOwnLocationCrosshair = true;
483 drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
486 m_ownLocationCrosshair->hide();
487 m_drawOwnLocationCrosshair = false;
491 void MainWindow::setViewPortSize(int width, int height)
493 qDebug() << __PRETTY_FUNCTION__;
495 m_viewPortWidth = width;
496 m_viewPortHeight = height;
499 void MainWindow::showMaemoInformationBox(const QString &message)
501 qDebug() << __PRETTY_FUNCTION__;
504 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
510 void MainWindow::startLoginProcess(const QUrl &url)
512 qDebug() << __PRETTY_FUNCTION__;
515 m_webView = new QWebView;
516 m_loginDialog = new LoginDialog(this);
518 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
519 this, SIGNAL(updateCredentials(QUrl)));
520 connect(m_webView, SIGNAL(loadFinished(bool)),
521 this, SLOT(loadDone(bool)));
523 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
524 this, SLOT(loginDialogDone(QString,QString)));
528 emit fetchUsernameFromSettings();
530 if(!m_email.isEmpty()) {
531 m_loginDialog->setEmailField(m_email);
534 if(m_loginDialog->exec() != QDialog::Accepted) {
535 // if login dialog was canceled we need to stop processing webview
536 // stop and disconnect m_webView;
538 disconnect(m_webView, SIGNAL(loadFinished(bool)),
539 this, SLOT(loadDone(bool)));
540 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
541 this, SLOT(updateCredentials(const QUrl &)));
543 emit cancelLoginProcess();
546 m_webView->load(m_loginUrl);
547 toggleProgressIndicator(true);
552 void MainWindow::toggleProgressIndicator(bool value)
554 qDebug() << __PRETTY_FUNCTION__;
557 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
560 #endif // Q_WS_MAEMO_5
563 const QString MainWindow::username()
565 qDebug() << __PRETTY_FUNCTION__;