0
|
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 "certificateinfo.h"
|
|
43 |
#include "sslclient.h"
|
|
44 |
#include "ui_sslclient.h"
|
|
45 |
#include "ui_sslerrors.h"
|
|
46 |
|
|
47 |
#include <QtGui/QScrollBar>
|
|
48 |
#include <QtGui/QStyle>
|
|
49 |
#include <QtGui/QToolButton>
|
|
50 |
#include <QtNetwork/QSslCipher>
|
|
51 |
|
|
52 |
SslClient::SslClient(QWidget *parent)
|
|
53 |
: QWidget(parent), socket(0), padLock(0), executingDialog(false)
|
|
54 |
{
|
|
55 |
form = new Ui_Form;
|
|
56 |
form->setupUi(this);
|
|
57 |
form->hostNameEdit->setSelection(0, form->hostNameEdit->text().size());
|
|
58 |
form->sessionOutput->setHtml(tr("<not connected>"));
|
|
59 |
|
|
60 |
connect(form->hostNameEdit, SIGNAL(textChanged(QString)),
|
|
61 |
this, SLOT(updateEnabledState()));
|
|
62 |
connect(form->connectButton, SIGNAL(clicked()),
|
|
63 |
this, SLOT(secureConnect()));
|
|
64 |
connect(form->sendButton, SIGNAL(clicked()),
|
|
65 |
this, SLOT(sendData()));
|
|
66 |
}
|
|
67 |
|
|
68 |
SslClient::~SslClient()
|
|
69 |
{
|
|
70 |
delete form;
|
|
71 |
}
|
|
72 |
|
|
73 |
void SslClient::updateEnabledState()
|
|
74 |
{
|
|
75 |
bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState;
|
|
76 |
|
|
77 |
form->hostNameEdit->setReadOnly(!unconnected);
|
|
78 |
form->hostNameEdit->setFocusPolicy(unconnected ? Qt::StrongFocus : Qt::NoFocus);
|
|
79 |
|
|
80 |
form->hostNameLabel->setEnabled(unconnected);
|
|
81 |
form->portBox->setEnabled(unconnected);
|
|
82 |
form->portLabel->setEnabled(unconnected);
|
|
83 |
form->connectButton->setEnabled(unconnected && !form->hostNameEdit->text().isEmpty());
|
|
84 |
|
|
85 |
bool connected = socket && socket->state() == QAbstractSocket::ConnectedState;
|
|
86 |
form->sessionBox->setEnabled(connected);
|
|
87 |
form->sessionOutput->setEnabled(connected);
|
|
88 |
form->sessionInput->setEnabled(connected);
|
|
89 |
form->sessionInputLabel->setEnabled(connected);
|
|
90 |
form->sendButton->setEnabled(connected);
|
|
91 |
}
|
|
92 |
|
|
93 |
void SslClient::secureConnect()
|
|
94 |
{
|
|
95 |
if (!socket) {
|
|
96 |
socket = new QSslSocket(this);
|
|
97 |
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
|
|
98 |
this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
|
|
99 |
connect(socket, SIGNAL(encrypted()),
|
|
100 |
this, SLOT(socketEncrypted()));
|
|
101 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
|
|
102 |
this, SLOT(sslErrors(QList<QSslError>)));
|
|
103 |
connect(socket, SIGNAL(readyRead()),
|
|
104 |
this, SLOT(socketReadyRead()));
|
|
105 |
}
|
|
106 |
|
|
107 |
socket->connectToHostEncrypted(form->hostNameEdit->text(), form->portBox->value());
|
|
108 |
updateEnabledState();
|
|
109 |
}
|
|
110 |
|
|
111 |
void SslClient::socketStateChanged(QAbstractSocket::SocketState state)
|
|
112 |
{
|
|
113 |
if (executingDialog)
|
|
114 |
return;
|
|
115 |
|
|
116 |
updateEnabledState();
|
|
117 |
if (state == QAbstractSocket::UnconnectedState) {
|
|
118 |
form->hostNameEdit->setPalette(QPalette());
|
|
119 |
form->hostNameEdit->setFocus();
|
|
120 |
form->cipherLabel->setText(tr("<none>"));
|
|
121 |
if (padLock)
|
|
122 |
padLock->hide();
|
|
123 |
socket->deleteLater();
|
|
124 |
socket = 0;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
void SslClient::socketEncrypted()
|
|
129 |
{
|
|
130 |
if (!socket)
|
|
131 |
return; // might have disconnected already
|
|
132 |
|
|
133 |
form->sessionOutput->clear();
|
|
134 |
form->sessionInput->setFocus();
|
|
135 |
|
|
136 |
QPalette palette;
|
|
137 |
palette.setColor(QPalette::Base, QColor(255, 255, 192));
|
|
138 |
form->hostNameEdit->setPalette(palette);
|
|
139 |
|
|
140 |
QSslCipher ciph = socket->sessionCipher();
|
|
141 |
QString cipher = QString("%1, %2 (%3/%4)").arg(ciph.authenticationMethod())
|
|
142 |
.arg(ciph.name()).arg(ciph.usedBits()).arg(ciph.supportedBits());;
|
|
143 |
form->cipherLabel->setText(cipher);
|
|
144 |
|
|
145 |
if (!padLock) {
|
|
146 |
padLock = new QToolButton;
|
|
147 |
padLock->setIcon(QIcon(":/encrypted.png"));
|
|
148 |
#ifndef QT_NO_CURSOR
|
|
149 |
padLock->setCursor(Qt::ArrowCursor);
|
|
150 |
#endif
|
|
151 |
padLock->setToolTip(tr("Display encryption details."));
|
|
152 |
|
|
153 |
int extent = form->hostNameEdit->height() - 2;
|
|
154 |
padLock->resize(extent, extent);
|
|
155 |
padLock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
|
|
156 |
|
|
157 |
QHBoxLayout *layout = new QHBoxLayout(form->hostNameEdit);
|
|
158 |
layout->setMargin(form->hostNameEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth));
|
|
159 |
layout->setSpacing(0);
|
|
160 |
layout->addStretch();
|
|
161 |
layout->addWidget(padLock);
|
|
162 |
|
|
163 |
form->hostNameEdit->setLayout(layout);
|
|
164 |
|
|
165 |
connect(padLock, SIGNAL(clicked()),
|
|
166 |
this, SLOT(displayCertificateInfo()));
|
|
167 |
} else {
|
|
168 |
padLock->show();
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
void SslClient::socketReadyRead()
|
|
173 |
{
|
|
174 |
appendString(QString::fromUtf8(socket->readAll()));
|
|
175 |
}
|
|
176 |
|
|
177 |
void SslClient::sendData()
|
|
178 |
{
|
|
179 |
QString input = form->sessionInput->text();
|
|
180 |
appendString(input + "\n");
|
|
181 |
socket->write(input.toUtf8() + "\r\n");
|
|
182 |
form->sessionInput->clear();
|
|
183 |
}
|
|
184 |
|
|
185 |
void SslClient::sslErrors(const QList<QSslError> &errors)
|
|
186 |
{
|
|
187 |
QDialog errorDialog(this);
|
|
188 |
Ui_SslErrors ui;
|
|
189 |
ui.setupUi(&errorDialog);
|
|
190 |
connect(ui.certificateChainButton, SIGNAL(clicked()),
|
|
191 |
this, SLOT(displayCertificateInfo()));
|
|
192 |
|
|
193 |
foreach (const QSslError &error, errors)
|
|
194 |
ui.sslErrorList->addItem(error.errorString());
|
|
195 |
|
|
196 |
executingDialog = true;
|
|
197 |
if (errorDialog.exec() == QDialog::Accepted)
|
|
198 |
socket->ignoreSslErrors();
|
|
199 |
executingDialog = false;
|
|
200 |
|
|
201 |
// did the socket state change?
|
|
202 |
if (socket->state() != QAbstractSocket::ConnectedState)
|
|
203 |
socketStateChanged(socket->state());
|
|
204 |
}
|
|
205 |
|
|
206 |
void SslClient::displayCertificateInfo()
|
|
207 |
{
|
|
208 |
CertificateInfo *info = new CertificateInfo(this);
|
|
209 |
info->setCertificateChain(socket->peerCertificateChain());
|
|
210 |
info->exec();
|
|
211 |
info->deleteLater();
|
|
212 |
}
|
|
213 |
|
|
214 |
void SslClient::appendString(const QString &line)
|
|
215 |
{
|
|
216 |
QTextCursor cursor(form->sessionOutput->textCursor());
|
|
217 |
cursor.movePosition(QTextCursor::End);
|
|
218 |
cursor.insertText(line);
|
|
219 |
form->sessionOutput->verticalScrollBar()->setValue(form->sessionOutput->verticalScrollBar()->maximum());
|
|
220 |
}
|