From 2e8ece735d6a31f4657192e77b9dc53fb2a03e9c Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Thu, 23 Apr 2009 14:13:35 +0200 Subject: Added Google Chat client as a new WebKit example Reviewed-by: Simon Hausmann --- demos/qtdemo/xml/examples.xml | 1 + doc/src/examples.qdoc | 1 + doc/src/examples/googlechat.qdoc | 51 +++++++ doc/src/images/googlechat-example.png | Bin 0 -> 52624 bytes examples/webkit/googlechat/form.ui | 214 ++++++++++++++++++++++++++++++ examples/webkit/googlechat/googlechat.cpp | 163 +++++++++++++++++++++++ examples/webkit/googlechat/googlechat.h | 71 ++++++++++ examples/webkit/googlechat/googlechat.pro | 11 ++ examples/webkit/googlechat/main.cpp | 51 +++++++ examples/webkit/webkit.pro | 3 +- 10 files changed, 565 insertions(+), 1 deletion(-) create mode 100644 doc/src/examples/googlechat.qdoc create mode 100644 doc/src/images/googlechat-example.png create mode 100644 examples/webkit/googlechat/form.ui create mode 100644 examples/webkit/googlechat/googlechat.cpp create mode 100644 examples/webkit/googlechat/googlechat.h create mode 100644 examples/webkit/googlechat/googlechat.pro create mode 100644 examples/webkit/googlechat/main.cpp diff --git a/demos/qtdemo/xml/examples.xml b/demos/qtdemo/xml/examples.xml index 07c2691..667f9f2 100644 --- a/demos/qtdemo/xml/examples.xml +++ b/demos/qtdemo/xml/examples.xml @@ -217,6 +217,7 @@ + diff --git a/doc/src/examples.qdoc b/doc/src/examples.qdoc index 2b7aea3..efe9b2f 100644 --- a/doc/src/examples.qdoc +++ b/doc/src/examples.qdoc @@ -348,6 +348,7 @@ \list \o \l{webkit/previewer}{Previewer}\raisedaster \o \l{webkit/formextractor}{Form Extractor} + \o \l{webkit/googlechat}{Google Chat} \o \l{webkit/fancybrowser}{Fancy Browser} \endlist diff --git a/doc/src/examples/googlechat.qdoc b/doc/src/examples/googlechat.qdoc new file mode 100644 index 0000000..f2d61cf --- /dev/null +++ b/doc/src/examples/googlechat.qdoc @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webkit/googlechat + \title Google Chat Example + + The Google Chat example shows how to use implement a simple Google Chat + client with QtWebKit. + + \image googlechat-example.png + +*/ diff --git a/doc/src/images/googlechat-example.png b/doc/src/images/googlechat-example.png new file mode 100644 index 0000000..0338445 Binary files /dev/null and b/doc/src/images/googlechat-example.png differ diff --git a/examples/webkit/googlechat/form.ui b/examples/webkit/googlechat/form.ui new file mode 100644 index 0000000..3b9fb82 --- /dev/null +++ b/examples/webkit/googlechat/form.ui @@ -0,0 +1,214 @@ + + + Form + + + + 0 + 0 + 286 + 413 + + + + Google Talk Client + + + + 0 + + + 0 + + + + + 1 + + + + + + + Qt::Vertical + + + + 20 + 170 + + + + + + + + TextLabel + + + Qt::AlignCenter + + + + + + + 24 + + + + + + + Qt::Vertical + + + + 20 + 169 + + + + + + + + + + + + Qt::Vertical + + + + 20 + 119 + + + + + + + + Google username: + + + + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 17 + + + + + + + + Password: + + + + + + + QLineEdit::Password + + + + + + + Qt::Vertical + + + + 20 + 118 + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Login + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + 0 + + + 0 + + + + + + about:blank + + + + + + + + + + + + + QWebView + QWidget +
QtWebKit/QWebView
+
+
+ + +
diff --git a/examples/webkit/googlechat/googlechat.cpp b/examples/webkit/googlechat/googlechat.cpp new file mode 100644 index 0000000..9ac2cd2 --- /dev/null +++ b/examples/webkit/googlechat/googlechat.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "googlechat.h" + +#define GOOGLECHAT_URL "http://talkgadget.google.com/talkgadget/m" + +GoogleChat::GoogleChat(): QWidget() { + form.setupUi(this); + setFixedSize(320, 480); + + form.userNameEdit->setFocus(); + connect(form.userNameEdit, SIGNAL(textChanged(QString)), SLOT(adjustLoginButton())); + connect(form.userNameEdit, SIGNAL(returnPressed()), SLOT(inputPassword())); + + connect(form.passwordEdit, SIGNAL(textChanged(QString)), SLOT(adjustLoginButton())); + connect(form.passwordEdit, SIGNAL(returnPressed()), SLOT(doLogin())); + + form.loginButton->setEnabled(false); + connect(form.loginButton, SIGNAL(clicked()), SLOT(doLogin())); + + connect(form.webView, SIGNAL(loadFinished(bool)), SLOT(initialPage(bool))); + connect(form.webView, SIGNAL(loadProgress(int)), + form.progressBar, SLOT(setValue(int))); + form.webView->setUrl((QUrl(GOOGLECHAT_URL))); + form.webView->setContextMenuPolicy(Qt::PreventContextMenu); + + showStatus("Wait..."); +} + +void GoogleChat::showStatus(const QString &msg) { + form.statusLabel->setText(msg); + form.stackedWidget->setCurrentIndex(0); +} + +void GoogleChat::showError(const QString &msg) { + form.progressBar->hide(); + showStatus(QString("Error: %1").arg(msg)); +} + +QString GoogleChat::evalJS(const QString &js) { + QWebFrame *frame = form.webView->page()->mainFrame(); + return frame->evaluateJavaScript(js).toString(); +} + +void GoogleChat::adjustLoginButton() { + userName = form.userNameEdit->text(); + password = form.passwordEdit->text(); + bool ok = !userName.isEmpty() && !password.isEmpty(); + form.loginButton->setEnabled(ok); +} + +void GoogleChat::inputPassword() { + if (!form.userNameEdit->text().isEmpty()) + form.passwordEdit->setFocus(); +} + +void GoogleChat::doLogin() { + userName = form.userNameEdit->text(); + password = form.passwordEdit->text(); + bool ok = !userName.isEmpty() && !password.isEmpty(); + if (!ok) + return; + + form.progressBar->setValue(0); + form.progressBar->show(); + connect(form.webView, SIGNAL(loadFinished(bool)), SLOT(loginPage(bool))); + connect(form.webView, SIGNAL(loadProgress(int)), + form.progressBar, SLOT(setValue(int))); + showStatus("Logging in..."); + + QString userEmail = userName + "@gmail.com"; + evalJS(QString("document.getElementById('Email').value = \"%1\";").arg(userEmail)); + evalJS(QString("document.getElementById('Passwd').value = \"%1\";").arg(password)); + evalJS("document.getElementById('gaia_loginform').submit();"); +} + +void GoogleChat::initialPage(bool ok) { + if (ok) { + QString s1 = evalJS("document.getElementById('Email').name"); + QString s2 = evalJS("document.getElementById('Passwd').name"); + QString s3 = evalJS("document.getElementById('gaia_loginform').id"); + if (s1 == "Email" && s2 == "Passwd" && s3 == "gaia_loginform") { + form.stackedWidget->setCurrentIndex(1); + form.webView->disconnect(); + return; + } + } + + showError("SERVICE unavailable."); +} + +void GoogleChat::hideElements() +{ + evalJS("var e = document.getElementsByClassName('footer-footer')[0]; e.parentElement.removeChild(e)"); + evalJS("var e = document.getElementsByClassName('title-bar-bg title-bar')[0]; e.parentElement.removeChild(e)"); + QTimer::singleShot(2000, this, SLOT(hideElements())); +} + +void GoogleChat::loginPage(bool ok) { + QString location = form.webView->url().toString(); + if (!ok) { + if (location.indexOf("CheckCookie")) + return; + showError("Service unavailable"); + } else { + // check for any error message + QString c = evalJS("document.getElementsByClassName('errormsg').length"); + if (c == "0") { + form.stackedWidget->setCurrentIndex(2); + QTimer::singleShot(500, this, SLOT(hideElements())); + return; + } + + QString err = "Unknown login failure."; + if (c == "1") { + err = evalJS("document.getElementsByClassName('errormsg')[0].textContent"); + err = err.simplified(); + } + showError(err); + } +} diff --git a/examples/webkit/googlechat/googlechat.h b/examples/webkit/googlechat/googlechat.h new file mode 100644 index 0000000..c92632c --- /dev/null +++ b/examples/webkit/googlechat/googlechat.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "ui_form.h" + +class GoogleChat: public QWidget +{ + Q_OBJECT + +public: + GoogleChat(); + +protected: + void showStatus(const QString &msg); + void showError(const QString &msg); + QString evalJS(const QString &js); + +private slots: + + void adjustLoginButton(); + void inputPassword(); + void doLogin(); + void initialPage(bool ok); + void loginPage(bool ok); + void hideElements(); + +private: + Ui::Form form; + QString userName; + QString password; +}; diff --git a/examples/webkit/googlechat/googlechat.pro b/examples/webkit/googlechat/googlechat.pro new file mode 100644 index 0000000..14b7085 --- /dev/null +++ b/examples/webkit/googlechat/googlechat.pro @@ -0,0 +1,11 @@ +QT += webkit +HEADERS = googlechat.h +SOURCES = main.cpp \ + googlechat.cpp +FORMS = form.ui + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/webkit/googlechat +sources.files = $$SOURCES $$HEADERS $$FORMS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/webkit/googlechat +INSTALLS += target sources diff --git a/examples/webkit/googlechat/main.cpp b/examples/webkit/googlechat/main.cpp new file mode 100644 index 0000000..26feeea --- /dev/null +++ b/examples/webkit/googlechat/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "googlechat.h" + +int main(int argc, char * argv[]) +{ + QApplication app(argc, argv); + GoogleChat *chat = new GoogleChat; + chat->show(); + return app.exec(); +} diff --git a/examples/webkit/webkit.pro b/examples/webkit/webkit.pro index a49830a..225816a 100644 --- a/examples/webkit/webkit.pro +++ b/examples/webkit/webkit.pro @@ -1,7 +1,8 @@ TEMPLATE = subdirs SUBDIRS += formextractor \ previewer \ - fancybrowser + fancybrowser \ + googlechat # install target.path = $$[QT_INSTALL_EXAMPLES]/webkit -- cgit v0.12