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
9 Situare is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 version 2 as published by the Free Software Foundation.
13 Situare is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Situare; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <QtMaemo5/QMaemo5InformationBox>
28 #endif // Q_WS_MAEMO_5
30 #include "mainwindow.h"
31 #include "mapviewscreen.h"
32 #include "settingsdialog.h"
33 #include "facebookservice/facebookauthentication.h"
35 MainWindow::MainWindow(QWidget *parent)
36 : QMainWindow(parent),
43 qDebug() << __PRETTY_FUNCTION__;
45 m_mapViewScreen = new MapViewScreen(this);
46 setCentralWidget(m_mapViewScreen);
48 setWindowTitle(tr("Situare"));
50 m_locationDialog = new UpdateLocationDialog(this);
52 connect(this, SIGNAL(reverseGeoReady(QString)),
53 m_locationDialog, SLOT(setAddress(QString)));
54 connect(m_locationDialog, SIGNAL(statusUpdate(QString,bool)), this,
55 SIGNAL(statusUpdate(QString,bool)));
57 connect(this, SIGNAL(userLocationReady(User*)),
58 m_mapViewScreen, SIGNAL(userLocationReady(User*)));
59 connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
60 m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
62 connect(this, SIGNAL(enableAutoCentering(bool)),
63 m_mapViewScreen, SLOT(enableAutoCentering(bool)));
64 connect(this, SIGNAL(positionReceived(QPointF)),
65 m_mapViewScreen, SLOT(positionReceived(QPointF)));
67 this->toggleProgressIndicator(true);
70 MainWindow::~MainWindow()
72 qDebug() << __PRETTY_FUNCTION__;
78 void MainWindow::toggleProgressIndicator(bool value)
80 qDebug() << __PRETTY_FUNCTION__;
82 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, value);
85 #endif // Q_WS_MAEMO_5
88 void MainWindow::createMenus()
90 qDebug() << __PRETTY_FUNCTION__;
92 m_toSettingsAct = new QAction(tr("Settings"), this);
93 m_toSettingsAct->setObjectName(tr("Settings"));
94 connect(m_toSettingsAct, SIGNAL(triggered()), this, SLOT(openSettingsDialog()));
95 m_gpsToggleAct = new QAction(tr("GPS"), this);
96 m_gpsToggleAct->setCheckable(true);
97 m_gpsToggleAct->setChecked(true);
98 connect(m_gpsToggleAct, SIGNAL(toggled(bool)), this, SLOT(gpsActionToggled(bool)));
99 m_autoCenteringAct = new QAction(tr("Auto centering"), this);
100 m_autoCenteringAct->setCheckable(true);
101 m_autoCenteringAct->setChecked(true);
102 connect(m_autoCenteringAct, SIGNAL(toggled(bool)), this, SLOT(autoCenteringToggled(bool)));
104 m_viewMenu = menuBar()->addMenu(tr("Main"));
106 m_viewMenu->addAction(m_toSettingsAct);
107 m_viewMenu->addAction(m_gpsToggleAct);
108 m_viewMenu->addAction(m_autoCenteringAct);
109 m_viewMenu->setObjectName(tr("Menu"));
112 void MainWindow::openLocationUpdateDialog()
114 qDebug() << __PRETTY_FUNCTION__;
116 emit requestReverseGeo();
117 m_locationDialog->exec();
120 void MainWindow::openSettingsDialog()
122 qDebug() << __PRETTY_FUNCTION__;
123 SettingsDialog *dialog = new SettingsDialog(this);
127 void MainWindow::gpsActionToggled(bool checked)
129 qDebug() << __PRETTY_FUNCTION__;
132 emit enableGPS(true);
133 showMaemoInformationBox(tr("GPS enabled"));
134 m_autoCenteringAct->setVisible(true);
137 emit enableGPS(false);
138 showMaemoInformationBox(tr("GPS disabled"));
139 m_autoCenteringAct->setVisible(false);
144 void MainWindow::gpsTimeout()
146 qDebug() << __PRETTY_FUNCTION__;
148 showMaemoInformationBox(tr("GPS timeout"));
151 void MainWindow::gpsError(const QString &message)
153 qDebug() << __PRETTY_FUNCTION__;
155 showMaemoInformationBox(message);
158 void MainWindow::autoCenteringToggled(bool checked)
160 qDebug() << __PRETTY_FUNCTION__ << " " << checked;
163 emit enableAutoCentering(true);
164 showMaemoInformationBox(tr("Auto centering enabled"));
167 emit enableAutoCentering(false);
168 showMaemoInformationBox(tr("Auto centering disabled"));
172 void MainWindow::showMaemoInformationBox(const QString &message)
174 qDebug() << __PRETTY_FUNCTION__;
177 QMaemo5InformationBox::information(this, message, QMaemo5InformationBox::DefaultTimeout);
183 void MainWindow::startLoginProcess(const QUrl &url)
185 qDebug() << __PRETTY_FUNCTION__;
188 m_webView = new QWebView;
189 m_loginDialog = new LoginDialog(this);
191 connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
192 this, SIGNAL(updateCredentials(QUrl)));
193 connect(m_webView, SIGNAL(loadFinished(bool)),
194 this, SLOT(loadDone(bool)));
196 connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
197 this, SLOT(loginDialogDone(QString,QString)));
202 if(m_loginDialog->exec() != QDialog::Accepted) {
203 // if login dialog was canceled we need to stop processing webview
204 // stop and disconnect m_webView;
206 disconnect(m_webView, SIGNAL(loadFinished(bool)),
207 this, SLOT(loadDone(bool)));
208 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
209 this, SLOT(updateCredentials(const QUrl &)));
211 emit cancelLoginProcess();
214 m_webView->load(m_loginUrl);
215 toggleProgressIndicator(true);
220 void MainWindow::loginDialogDone(const QString &email, const QString &password)
222 qDebug() << __PRETTY_FUNCTION__;
225 m_password = password;
228 void MainWindow::loginFailed()
230 qDebug() << __PRETTY_FUNCTION__;
235 toggleProgressIndicator(false);
238 QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
239 QMaemo5InformationBox::NoTimeout);
241 #endif // Q_WS_MAEMO_5
243 if(m_loginDialog->exec() != QDialog::Accepted) {
244 // if login dialog was canceled we need to stop processing webview
245 // stop and disconnect m_webView;
247 disconnect(m_webView, SIGNAL(loadFinished(bool)),
248 this, SLOT(loadDone(bool)));
249 disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
250 this, SLOT(updateCredentials(const QUrl &)));
252 emit cancelLoginProcess();
255 // re-load login page for webview
256 toggleProgressIndicator(true);
257 m_webView->load(m_loginUrl);
261 void MainWindow::loadDone(bool done)
263 qDebug() << __PRETTY_FUNCTION__;
265 // for the first time the login page is opened, we need to refresh it to get cookies working
273 QWebFrame* frame = m_webView->page()->currentFrame();
277 QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
279 foreach (QWebElement element, emailCollection) {
280 element.setAttribute("value", m_email.toAscii());
283 QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
284 foreach (QWebElement element, passwordCollection) {
285 element.setAttribute("value", m_password.toAscii());
287 // find connect button
288 QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
289 foreach (QWebElement element, buttonCollection)
291 QPoint pos(element.geometry().center());
293 // send a mouse click event to the web page
294 QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
296 QApplication::sendEvent(m_webView->page(), &event0);
297 QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
299 QApplication::sendEvent(m_webView->page(), &event1);