|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 #include <QtWebKit> |
|
44 #include <QSslSocket> |
|
45 |
|
46 #include "googlechat.h" |
|
47 |
|
48 #define GOOGLECHAT_URL "http://talkgadget.google.com/talkgadget/m" |
|
49 |
|
50 GoogleChat::GoogleChat(): QWidget() { |
|
51 form.setupUi(this); |
|
52 setFixedSize(320, 480); |
|
53 |
|
54 form.userNameEdit->setFocus(); |
|
55 connect(form.userNameEdit, SIGNAL(textChanged(QString)), SLOT(adjustLoginButton())); |
|
56 connect(form.userNameEdit, SIGNAL(returnPressed()), SLOT(inputPassword())); |
|
57 |
|
58 connect(form.passwordEdit, SIGNAL(textChanged(QString)), SLOT(adjustLoginButton())); |
|
59 connect(form.passwordEdit, SIGNAL(returnPressed()), SLOT(doLogin())); |
|
60 |
|
61 form.loginButton->setEnabled(false); |
|
62 connect(form.loginButton, SIGNAL(clicked()), SLOT(doLogin())); |
|
63 |
|
64 connect(form.webView, SIGNAL(loadFinished(bool)), SLOT(initialPage(bool))); |
|
65 connect(form.webView, SIGNAL(loadProgress(int)), |
|
66 form.progressBar, SLOT(setValue(int))); |
|
67 form.webView->setUrl((QUrl(GOOGLECHAT_URL))); |
|
68 form.webView->setContextMenuPolicy(Qt::PreventContextMenu); |
|
69 |
|
70 showStatus("Wait..."); |
|
71 } |
|
72 |
|
73 void GoogleChat::showStatus(const QString &msg) { |
|
74 form.statusLabel->setText(msg); |
|
75 form.stackedWidget->setCurrentIndex(0); |
|
76 } |
|
77 |
|
78 void GoogleChat::showError(const QString &msg) { |
|
79 form.progressBar->hide(); |
|
80 showStatus(QString("Error: %1").arg(msg)); |
|
81 } |
|
82 |
|
83 QString GoogleChat::evalJS(const QString &js) { |
|
84 QWebFrame *frame = form.webView->page()->mainFrame(); |
|
85 return frame->evaluateJavaScript(js).toString(); |
|
86 } |
|
87 |
|
88 void GoogleChat::adjustLoginButton() { |
|
89 userName = form.userNameEdit->text(); |
|
90 password = form.passwordEdit->text(); |
|
91 bool ok = !userName.isEmpty() && !password.isEmpty(); |
|
92 form.loginButton->setEnabled(ok); |
|
93 } |
|
94 |
|
95 void GoogleChat::inputPassword() { |
|
96 if (!form.userNameEdit->text().isEmpty()) |
|
97 form.passwordEdit->setFocus(); |
|
98 } |
|
99 |
|
100 void GoogleChat::doLogin() { |
|
101 userName = form.userNameEdit->text(); |
|
102 password = form.passwordEdit->text(); |
|
103 bool ok = !userName.isEmpty() && !password.isEmpty(); |
|
104 if (!ok) |
|
105 return; |
|
106 |
|
107 form.progressBar->setValue(0); |
|
108 form.progressBar->show(); |
|
109 connect(form.webView, SIGNAL(loadFinished(bool)), SLOT(loginPage(bool))); |
|
110 connect(form.webView, SIGNAL(loadProgress(int)), |
|
111 form.progressBar, SLOT(setValue(int))); |
|
112 showStatus("Logging in..."); |
|
113 |
|
114 QString userEmail = userName + "@gmail.com"; |
|
115 evalJS(QString("document.getElementById('Email').value = \"%1\";").arg(userEmail)); |
|
116 evalJS(QString("document.getElementById('Passwd').value = \"%1\";").arg(password)); |
|
117 evalJS("document.getElementById('gaia_loginform').submit();"); |
|
118 } |
|
119 |
|
120 void GoogleChat::initialPage(bool ok) { |
|
121 if (!QSslSocket::supportsSsl()) { |
|
122 showError("This example requires SSL support."); |
|
123 return; |
|
124 } |
|
125 |
|
126 if (ok) { |
|
127 QString s1 = evalJS("document.getElementById('Email').name"); |
|
128 QString s2 = evalJS("document.getElementById('Passwd').name"); |
|
129 QString s3 = evalJS("document.getElementById('gaia_loginform').id"); |
|
130 if (s1 == "Email" && s2 == "Passwd" && s3 == "gaia_loginform") { |
|
131 form.stackedWidget->setCurrentIndex(1); |
|
132 form.webView->disconnect(); |
|
133 return; |
|
134 } |
|
135 } |
|
136 |
|
137 showError("SERVICE unavailable."); |
|
138 } |
|
139 |
|
140 void GoogleChat::hideElements() |
|
141 { |
|
142 evalJS("var e = document.getElementsByClassName('footer-footer')[0]; e.parentElement.removeChild(e)"); |
|
143 evalJS("var e = document.getElementsByClassName('title-bar-bg title-bar')[0]; e.parentElement.removeChild(e)"); |
|
144 QTimer::singleShot(2000, this, SLOT(hideElements())); |
|
145 } |
|
146 |
|
147 void GoogleChat::loginPage(bool ok) { |
|
148 QString location = form.webView->url().toString(); |
|
149 if (!ok) { |
|
150 if (location.indexOf("CheckCookie")) |
|
151 return; |
|
152 showError("Service unavailable"); |
|
153 } else { |
|
154 // check for any error message |
|
155 QString c = evalJS("document.getElementsByClassName('errormsg').length"); |
|
156 if (c == "0") { |
|
157 form.stackedWidget->setCurrentIndex(2); |
|
158 QTimer::singleShot(500, this, SLOT(hideElements())); |
|
159 return; |
|
160 } |
|
161 |
|
162 QString err = "Unknown login failure."; |
|
163 if (c == "1") { |
|
164 err = evalJS("document.getElementsByClassName('errormsg')[0].textContent"); |
|
165 err = err.simplified(); |
|
166 } |
|
167 showError(err); |
|
168 } |
|
169 } |