author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "certificateinfo.h" |
|
42 |
#include "sslclient.h" |
|
43 |
#include "ui_sslclient.h" |
|
44 |
#include "ui_sslerrors.h" |
|
45 |
||
46 |
#include <QtGui/QScrollBar> |
|
47 |
#include <QtGui/QStyle> |
|
48 |
#include <QtGui/QToolButton> |
|
49 |
#include <QtNetwork/QSslCipher> |
|
50 |
||
51 |
SslClient::SslClient(QWidget *parent) |
|
52 |
: QWidget(parent), socket(0), padLock(0), executingDialog(false) |
|
53 |
{ |
|
54 |
form = new Ui_Form; |
|
55 |
form->setupUi(this); |
|
56 |
form->hostNameEdit->setSelection(0, form->hostNameEdit->text().size()); |
|
57 |
form->sessionOutput->setHtml(tr("<not connected>")); |
|
58 |
||
59 |
connect(form->hostNameEdit, SIGNAL(textChanged(QString)), |
|
60 |
this, SLOT(updateEnabledState())); |
|
61 |
connect(form->connectButton, SIGNAL(clicked()), |
|
62 |
this, SLOT(secureConnect())); |
|
63 |
connect(form->sendButton, SIGNAL(clicked()), |
|
64 |
this, SLOT(sendData())); |
|
65 |
} |
|
66 |
||
67 |
SslClient::~SslClient() |
|
68 |
{ |
|
69 |
delete form; |
|
70 |
} |
|
71 |
||
72 |
void SslClient::updateEnabledState() |
|
73 |
{ |
|
74 |
bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState; |
|
75 |
||
76 |
form->hostNameEdit->setReadOnly(!unconnected); |
|
77 |
form->hostNameEdit->setFocusPolicy(unconnected ? Qt::StrongFocus : Qt::NoFocus); |
|
78 |
||
79 |
form->hostNameLabel->setEnabled(unconnected); |
|
80 |
form->portBox->setEnabled(unconnected); |
|
81 |
form->portLabel->setEnabled(unconnected); |
|
82 |
form->connectButton->setEnabled(unconnected && !form->hostNameEdit->text().isEmpty()); |
|
83 |
||
84 |
bool connected = socket && socket->state() == QAbstractSocket::ConnectedState; |
|
85 |
form->sessionBox->setEnabled(connected); |
|
86 |
form->sessionOutput->setEnabled(connected); |
|
87 |
form->sessionInput->setEnabled(connected); |
|
88 |
form->sessionInputLabel->setEnabled(connected); |
|
89 |
form->sendButton->setEnabled(connected); |
|
90 |
} |
|
91 |
||
92 |
void SslClient::secureConnect() |
|
93 |
{ |
|
94 |
if (!socket) { |
|
95 |
socket = new QSslSocket(this); |
|
96 |
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), |
|
97 |
this, SLOT(socketStateChanged(QAbstractSocket::SocketState))); |
|
98 |
connect(socket, SIGNAL(encrypted()), |
|
99 |
this, SLOT(socketEncrypted())); |
|
100 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), |
|
101 |
this, SLOT(sslErrors(QList<QSslError>))); |
|
102 |
connect(socket, SIGNAL(readyRead()), |
|
103 |
this, SLOT(socketReadyRead())); |
|
104 |
} |
|
105 |
||
106 |
socket->connectToHostEncrypted(form->hostNameEdit->text(), form->portBox->value()); |
|
107 |
updateEnabledState(); |
|
108 |
} |
|
109 |
||
110 |
void SslClient::socketStateChanged(QAbstractSocket::SocketState state) |
|
111 |
{ |
|
112 |
if (executingDialog) |
|
113 |
return; |
|
114 |
||
115 |
updateEnabledState(); |
|
116 |
if (state == QAbstractSocket::UnconnectedState) { |
|
117 |
form->hostNameEdit->setPalette(QPalette()); |
|
118 |
form->hostNameEdit->setFocus(); |
|
119 |
form->cipherLabel->setText(tr("<none>")); |
|
120 |
if (padLock) |
|
121 |
padLock->hide(); |
|
122 |
socket->deleteLater(); |
|
123 |
socket = 0; |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
void SslClient::socketEncrypted() |
|
128 |
{ |
|
129 |
if (!socket) |
|
130 |
return; // might have disconnected already |
|
131 |
||
132 |
form->sessionOutput->clear(); |
|
133 |
form->sessionInput->setFocus(); |
|
134 |
||
135 |
QPalette palette; |
|
136 |
palette.setColor(QPalette::Base, QColor(255, 255, 192)); |
|
137 |
form->hostNameEdit->setPalette(palette); |
|
138 |
||
139 |
QSslCipher ciph = socket->sessionCipher(); |
|
140 |
QString cipher = QString("%1, %2 (%3/%4)").arg(ciph.authenticationMethod()) |
|
141 |
.arg(ciph.name()).arg(ciph.usedBits()).arg(ciph.supportedBits());; |
|
142 |
form->cipherLabel->setText(cipher); |
|
143 |
||
144 |
if (!padLock) { |
|
145 |
padLock = new QToolButton; |
|
146 |
padLock->setIcon(QIcon(":/encrypted.png")); |
|
147 |
#ifndef QT_NO_CURSOR |
|
148 |
padLock->setCursor(Qt::ArrowCursor); |
|
149 |
#endif |
|
150 |
padLock->setToolTip(tr("Display encryption details.")); |
|
151 |
||
152 |
int extent = form->hostNameEdit->height() - 2; |
|
153 |
padLock->resize(extent, extent); |
|
154 |
padLock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); |
|
155 |
||
156 |
QHBoxLayout *layout = new QHBoxLayout(form->hostNameEdit); |
|
157 |
layout->setMargin(form->hostNameEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth)); |
|
158 |
layout->setSpacing(0); |
|
159 |
layout->addStretch(); |
|
160 |
layout->addWidget(padLock); |
|
161 |
||
162 |
form->hostNameEdit->setLayout(layout); |
|
163 |
||
164 |
connect(padLock, SIGNAL(clicked()), |
|
165 |
this, SLOT(displayCertificateInfo())); |
|
166 |
} else { |
|
167 |
padLock->show(); |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
void SslClient::socketReadyRead() |
|
172 |
{ |
|
173 |
appendString(QString::fromUtf8(socket->readAll())); |
|
174 |
} |
|
175 |
||
176 |
void SslClient::sendData() |
|
177 |
{ |
|
178 |
QString input = form->sessionInput->text(); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
appendString(input + '\n'); |
0 | 180 |
socket->write(input.toUtf8() + "\r\n"); |
181 |
form->sessionInput->clear(); |
|
182 |
} |
|
183 |
||
184 |
void SslClient::sslErrors(const QList<QSslError> &errors) |
|
185 |
{ |
|
186 |
QDialog errorDialog(this); |
|
187 |
Ui_SslErrors ui; |
|
188 |
ui.setupUi(&errorDialog); |
|
189 |
connect(ui.certificateChainButton, SIGNAL(clicked()), |
|
190 |
this, SLOT(displayCertificateInfo())); |
|
191 |
||
192 |
foreach (const QSslError &error, errors) |
|
193 |
ui.sslErrorList->addItem(error.errorString()); |
|
194 |
||
195 |
executingDialog = true; |
|
196 |
if (errorDialog.exec() == QDialog::Accepted) |
|
197 |
socket->ignoreSslErrors(); |
|
198 |
executingDialog = false; |
|
199 |
||
200 |
// did the socket state change? |
|
201 |
if (socket->state() != QAbstractSocket::ConnectedState) |
|
202 |
socketStateChanged(socket->state()); |
|
203 |
} |
|
204 |
||
205 |
void SslClient::displayCertificateInfo() |
|
206 |
{ |
|
207 |
CertificateInfo *info = new CertificateInfo(this); |
|
208 |
info->setCertificateChain(socket->peerCertificateChain()); |
|
209 |
info->exec(); |
|
210 |
info->deleteLater(); |
|
211 |
} |
|
212 |
||
213 |
void SslClient::appendString(const QString &line) |
|
214 |
{ |
|
215 |
QTextCursor cursor(form->sessionOutput->textCursor()); |
|
216 |
cursor.movePosition(QTextCursor::End); |
|
217 |
cursor.insertText(line); |
|
218 |
form->sessionOutput->verticalScrollBar()->setValue(form->sessionOutput->verticalScrollBar()->maximum()); |
|
219 |
} |