Merge branch 'list_panel'
[situare] / src / ui / mainwindow.cpp
index e3484b3..e1cb858 100644 (file)
 #include "mapviewscreen.h"
 #include "settingsdialog.h"
 #include "facebookservice/facebookauthentication.h"
-#include "situareservice/situareservice.h"
 #include "situarecommon.h"
 
 MainWindow::MainWindow(QWidget *parent)
-    : QMainWindow(parent)
+    : QMainWindow(parent),
+    m_email(),
+    m_loginUrl(),
+    m_password(),
+    m_refresh(0),
+    m_webView(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
@@ -43,7 +47,7 @@ MainWindow::MainWindow(QWidget *parent)
     setCentralWidget(m_mapViewScreen);
     createMenus();
     setWindowTitle(tr("Situare"));
-    hide();
+       show();
 
     m_locationDialog = new UpdateLocationDialog(this);
 
@@ -57,7 +61,7 @@ MainWindow::MainWindow(QWidget *parent)
     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
             m_mapViewScreen, SIGNAL(friendsLocationsReady(QList<User*>&)));
 
-    connect(this, SIGNAL(autoCentering(bool)),
+       connect(this, SIGNAL(autoCentering(bool)),
             m_mapViewScreen, SLOT(enableAutoCentering(bool)));
     connect(this, SIGNAL(positionReceived(QPointF, qreal)),
             m_mapViewScreen, SLOT(positionReceived(QPointF, qreal)));
@@ -66,6 +70,14 @@ MainWindow::MainWindow(QWidget *parent)
     this->toggleProgressIndicator(true);
 }
 
+MainWindow::~MainWindow()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_webView)
+        delete m_webView;
+}
+
 void MainWindow::toggleProgressIndicator(bool value)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -202,9 +214,123 @@ void MainWindow::showMaemoInformationBox(const QString &message)
 #endif
 }
 
-MainWindow::~MainWindow()
+void MainWindow::startLoginProcess(const QUrl &url)
 {
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    settings.setValue(GPS_ENABLED, m_gpsToggleAct->isChecked());
-    settings.setValue(AUTO_CENTERING_ENABLED, m_autoCenteringAct->isChecked());
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_loginUrl = url;
+    m_webView = new QWebView;
+    m_loginDialog = new LoginDialog(this);
+
+    connect(m_webView, SIGNAL(urlChanged(const QUrl &)),
+            this, SIGNAL(updateCredentials(QUrl)));
+    connect(m_webView, SIGNAL(loadFinished(bool)),
+            this, SLOT(loadDone(bool)));
+
+    connect(m_loginDialog, SIGNAL(loginDialogDone(QString,QString)),
+            this, SLOT(loginDialogDone(QString,QString)));
+
+    m_webView->hide();
+
+    if(m_loginDialog->exec() != QDialog::Accepted) {
+        // if login dialog was canceled we need to stop processing webview
+        // stop and disconnect m_webView;
+        m_webView->stop();
+        disconnect(m_webView, SIGNAL(loadFinished(bool)),
+                   this, SLOT(loadDone(bool)));
+        disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
+                   this, SLOT(updateCredentials(const QUrl &)));
+
+        emit cancelLoginProcess();
+    }
+    else {
+        m_webView->load(m_loginUrl);
+        toggleProgressIndicator(true);
+        m_refresh = true;
+    }
+}
+
+void MainWindow::loginDialogDone(const QString &email, const QString &password)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_email = email;
+    m_password = password;
+}
+
+void MainWindow::loginFailed()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_email.clear();
+    m_password.clear();
+
+    toggleProgressIndicator(false);
+
+#ifdef Q_WS_MAEMO_5
+    QMaemo5InformationBox::information(this, tr("Invalid E-mail address or password"),
+                                       QMaemo5InformationBox::NoTimeout);
+
+#endif // Q_WS_MAEMO_5
+
+    if(m_loginDialog->exec() != QDialog::Accepted) {
+        // if login dialog was canceled we need to stop processing webview
+        // stop and disconnect m_webView;
+        m_webView->stop();
+        disconnect(m_webView, SIGNAL(loadFinished(bool)),
+                   this, SLOT(loadDone(bool)));
+        disconnect(m_webView, SIGNAL(urlChanged(const QUrl &)),
+                   this, SLOT(updateCredentials(const QUrl &)));
+
+        emit cancelLoginProcess();
+    }
+    else {
+        // re-load login page for webview
+        toggleProgressIndicator(true);
+        m_webView->load(m_loginUrl);
+    }
+}
+
+void MainWindow::loadDone(bool done)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    // for the first time the login page is opened, we need to refresh it to get cookies working
+    if(m_refresh) {
+        m_webView->reload();
+        m_refresh = false;
+    }
+
+    if (done)
+    {
+        QWebFrame* frame = m_webView->page()->currentFrame();
+        if (frame!=NULL)
+        {
+            // set email box
+            QWebElementCollection emailCollection = frame->findAllElements("input[name=email]");
+
+            foreach (QWebElement element, emailCollection) {
+                element.setAttribute("value", m_email.toAscii());
+            }
+            // set password box
+            QWebElementCollection passwordCollection = frame->findAllElements("input[name=pass]");
+            foreach (QWebElement element, passwordCollection) {
+                element.setAttribute("value", m_password.toAscii());
+            }
+            // find connect button
+            QWebElementCollection buttonCollection = frame->findAllElements("input[name=login]");
+            foreach (QWebElement element, buttonCollection)
+            {
+                QPoint pos(element.geometry().center());
+
+                // send a mouse click event to the web page
+                QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton,
+                                   Qt::NoModifier);
+                QApplication::sendEvent(m_webView->page(), &event0);
+                QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton,
+                                   Qt::NoModifier);
+                QApplication::sendEvent(m_webView->page(), &event1);
+            }
+        }
+    }
 }