examples/webkit/googlechat/googlechat.cpp
branchRCL_3
changeset 4 3b1da2848fc7
parent 0 1918ee327afb
child 30 5dc02b23752f
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the examples of the Qt Toolkit.
     7 ** This file is part of the examples of the Qt Toolkit.
     8 **
     8 **
    78 void GoogleChat::showError(const QString &msg) {
    78 void GoogleChat::showError(const QString &msg) {
    79     form.progressBar->hide();
    79     form.progressBar->hide();
    80     showStatus(QString("Error: %1").arg(msg));
    80     showStatus(QString("Error: %1").arg(msg));
    81 }
    81 }
    82 
    82 
    83 QString GoogleChat::evalJS(const QString &js) {
    83 QWebElement GoogleChat::document() const {
    84     QWebFrame *frame = form.webView->page()->mainFrame();
    84     return form.webView->page()->mainFrame()->documentElement();
    85     return frame->evaluateJavaScript(js).toString();
       
    86 }
    85 }
    87 
    86 
    88 void GoogleChat::adjustLoginButton() {
    87 void GoogleChat::adjustLoginButton() {
    89     userName = form.userNameEdit->text();
    88     userName = form.userNameEdit->text();
    90     password = form.passwordEdit->text();
    89     password = form.passwordEdit->text();
   110     connect(form.webView, SIGNAL(loadProgress(int)),
   109     connect(form.webView, SIGNAL(loadProgress(int)),
   111             form.progressBar, SLOT(setValue(int)));
   110             form.progressBar, SLOT(setValue(int)));
   112     showStatus("Logging in...");
   111     showStatus("Logging in...");
   113 
   112 
   114     QString userEmail = userName + "@gmail.com";
   113     QString userEmail = userName + "@gmail.com";
   115     evalJS(QString("document.getElementById('Email').value = \"%1\";").arg(userEmail));
   114 
   116     evalJS(QString("document.getElementById('Passwd').value = \"%1\";").arg(password));
   115     document().findFirst("#Email").setAttribute("value", userEmail);
   117     evalJS("document.getElementById('gaia_loginform').submit();");
   116     document().findFirst("#Passwd").setAttribute("value", password);
       
   117     document().findFirst("#gaia_loginform").evaluateJavaScript("this.submit();");
       
   118 
   118 }
   119 }
   119 
   120 
   120 void GoogleChat::initialPage(bool ok) {
   121 void GoogleChat::initialPage(bool ok) {
   121     if (!QSslSocket::supportsSsl()) {
   122     if (!QSslSocket::supportsSsl()) {
   122         showError("This example requires SSL support.");
   123         showError("This example requires SSL support.");
   123         return;
   124         return;
   124     }
   125     }
   125 
   126 
   126     if (ok) {
   127     if (ok) {
   127         QString s1 = evalJS("document.getElementById('Email').name");
   128         QWebElement email = document().findFirst("#Email");
   128         QString s2 = evalJS("document.getElementById('Passwd').name");
   129         QWebElement passwd = document().findFirst("#Passwd");
   129         QString s3 = evalJS("document.getElementById('gaia_loginform').id");
   130         QWebElement loginForm = document().findFirst("#gaia_loginform");
   130         if (s1 == "Email" && s2 == "Passwd" && s3 == "gaia_loginform") {
   131         if (!email.isNull() && !passwd.isNull() && !loginForm.isNull()) {
   131             form.stackedWidget->setCurrentIndex(1);
   132             form.stackedWidget->setCurrentIndex(1);
       
   133             form.userNameEdit->setFocus();
   132             form.webView->disconnect();
   134             form.webView->disconnect();
   133             return;
   135             return;
   134         }
   136         }
   135     }
   137     }
   136 
   138 
   137     showError("SERVICE unavailable.");
   139     showError("SERVICE unavailable.");
   138 }
   140 }
   139 
   141 
   140 void GoogleChat::hideElements()
   142 void GoogleChat::hideElements()
   141 {
   143 {
   142     evalJS("var e = document.getElementsByClassName('footer-footer')[0]; e.parentElement.removeChild(e)");
   144     document().findFirst(".footer-footer").removeFromDocument();
   143     evalJS("var e = document.getElementsByClassName('title-bar-bg title-bar')[0]; e.parentElement.removeChild(e)");
   145     document().findFirst(".title-bar-bg .title-bar").removeFromDocument();
   144     QTimer::singleShot(2000, this, SLOT(hideElements()));
   146     QTimer::singleShot(2000, this, SLOT(hideElements()));
   145 }
   147 }
   146 
   148 
   147 void GoogleChat::loginPage(bool ok) {
   149 void GoogleChat::loginPage(bool ok) {
   148     QString location = form.webView->url().toString();
   150     QString location = form.webView->url().toString();
   150         if (location.indexOf("CheckCookie"))
   152         if (location.indexOf("CheckCookie"))
   151             return;
   153             return;
   152         showError("Service unavailable");
   154         showError("Service unavailable");
   153     } else {
   155     } else {
   154         // check for any error message
   156         // check for any error message
   155         QString c = evalJS("document.getElementsByClassName('errormsg').length");
   157 
   156         if (c == "0") {
   158         QWebElement  e = document().findFirst(".errormsg");
       
   159         if (e.isNull()) {
   157             form.stackedWidget->setCurrentIndex(2);
   160             form.stackedWidget->setCurrentIndex(2);
   158             QTimer::singleShot(500, this, SLOT(hideElements()));
   161             QTimer::singleShot(500, this, SLOT(hideElements()));
   159             return;
   162             return;
   160         }
   163         }
   161 
   164 
   162         QString err = "Unknown login failure.";
   165        QString err = "Unknown login failure.";
   163         if (c == "1") {
   166        const QString errorMessage = e.toPlainText();
   164             err = evalJS("document.getElementsByClassName('errormsg')[0].textContent");
   167         if (!errorMessage.isEmpty()) {
       
   168             err = errorMessage;
   165             err = err.simplified();
   169             err = err.simplified();
   166         }
   170         }
   167         showError(err);
   171         showError(err);
   168     }
   172     }
   169 }
   173 }