author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
QWebElement GoogleChat::document() const { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
return form.webView->page()->mainFrame()->documentElement(); |
0 | 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"; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
114 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
document().findFirst("#Email").setAttribute("value", userEmail); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
document().findFirst("#Passwd").setAttribute("value", password); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
document().findFirst("#gaia_loginform").evaluateJavaScript("this.submit();"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
118 |
|
0 | 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) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
QWebElement email = document().findFirst("#Email"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
QWebElement passwd = document().findFirst("#Passwd"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
QWebElement loginForm = document().findFirst("#gaia_loginform"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
if (!email.isNull() && !passwd.isNull() && !loginForm.isNull()) { |
0 | 132 |
form.stackedWidget->setCurrentIndex(1); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
form.userNameEdit->setFocus(); |
0 | 134 |
form.webView->disconnect(); |
135 |
return; |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
showError("SERVICE unavailable."); |
|
140 |
} |
|
141 |
||
142 |
void GoogleChat::hideElements() |
|
143 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
document().findFirst(".footer-footer").removeFromDocument(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
document().findFirst(".title-bar-bg .title-bar").removeFromDocument(); |
0 | 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 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
QWebElement e = document().findFirst(".errormsg"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
if (e.isNull()) { |
0 | 160 |
form.stackedWidget->setCurrentIndex(2); |
161 |
QTimer::singleShot(500, this, SLOT(hideElements())); |
|
162 |
return; |
|
163 |
} |
|
164 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
QString err = "Unknown login failure."; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
const QString errorMessage = e.toPlainText(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
if (!errorMessage.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
err = errorMessage; |
0 | 169 |
err = err.simplified(); |
170 |
} |
|
171 |
showError(err); |
|
172 |
} |
|
173 |
} |