|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 QWebElement GoogleChat::document() const { |
|
84 return form.webView->page()->mainFrame()->documentElement(); |
|
85 } |
|
86 |
|
87 void GoogleChat::adjustLoginButton() { |
|
88 userName = form.userNameEdit->text(); |
|
89 password = form.passwordEdit->text(); |
|
90 bool ok = !userName.isEmpty() && !password.isEmpty(); |
|
91 form.loginButton->setEnabled(ok); |
|
92 } |
|
93 |
|
94 void GoogleChat::inputPassword() { |
|
95 if (!form.userNameEdit->text().isEmpty()) |
|
96 form.passwordEdit->setFocus(); |
|
97 } |
|
98 |
|
99 void GoogleChat::doLogin() { |
|
100 userName = form.userNameEdit->text(); |
|
101 password = form.passwordEdit->text(); |
|
102 bool ok = !userName.isEmpty() && !password.isEmpty(); |
|
103 if (!ok) |
|
104 return; |
|
105 |
|
106 form.progressBar->setValue(0); |
|
107 form.progressBar->show(); |
|
108 connect(form.webView, SIGNAL(loadFinished(bool)), SLOT(loginPage(bool))); |
|
109 connect(form.webView, SIGNAL(loadProgress(int)), |
|
110 form.progressBar, SLOT(setValue(int))); |
|
111 showStatus("Logging in..."); |
|
112 |
|
113 QString userEmail = userName + "@gmail.com"; |
|
114 |
|
115 document().findFirst("#Email").setAttribute("value", userEmail); |
|
116 document().findFirst("#Passwd").setAttribute("value", password); |
|
117 document().findFirst("#gaia_loginform").evaluateJavaScript("this.submit();"); |
|
118 |
|
119 } |
|
120 |
|
121 void GoogleChat::initialPage(bool ok) { |
|
122 if (!QSslSocket::supportsSsl()) { |
|
123 showError("This example requires SSL support."); |
|
124 return; |
|
125 } |
|
126 |
|
127 if (ok) { |
|
128 QWebElement email = document().findFirst("#Email"); |
|
129 QWebElement passwd = document().findFirst("#Passwd"); |
|
130 QWebElement loginForm = document().findFirst("#gaia_loginform"); |
|
131 if (!email.isNull() && !passwd.isNull() && !loginForm.isNull()) { |
|
132 form.stackedWidget->setCurrentIndex(1); |
|
133 form.userNameEdit->setFocus(); |
|
134 form.webView->disconnect(); |
|
135 return; |
|
136 } |
|
137 } |
|
138 |
|
139 showError("SERVICE unavailable."); |
|
140 } |
|
141 |
|
142 void GoogleChat::hideElements() |
|
143 { |
|
144 document().findFirst(".footer-footer").removeFromDocument(); |
|
145 document().findFirst(".title-bar-bg .title-bar").removeFromDocument(); |
|
146 QTimer::singleShot(2000, this, SLOT(hideElements())); |
|
147 } |
|
148 |
|
149 void GoogleChat::loginPage(bool ok) { |
|
150 QString location = form.webView->url().toString(); |
|
151 if (!ok) { |
|
152 if (location.indexOf("CheckCookie")) |
|
153 return; |
|
154 showError("Service unavailable"); |
|
155 } else { |
|
156 // check for any error message |
|
157 |
|
158 QWebElement e = document().findFirst(".errormsg"); |
|
159 if (e.isNull()) { |
|
160 form.stackedWidget->setCurrentIndex(2); |
|
161 QTimer::singleShot(500, this, SLOT(hideElements())); |
|
162 return; |
|
163 } |
|
164 |
|
165 QString err = "Unknown login failure."; |
|
166 const QString errorMessage = e.toPlainText(); |
|
167 if (!errorMessage.isEmpty()) { |
|
168 err = errorMessage; |
|
169 err = err.simplified(); |
|
170 } |
|
171 showError(err); |
|
172 } |
|
173 } |