QtNetwork (examples) - update secure socket client example
This patch contains: - some cosmetic changes to make example look more like modern C++; - UI initialization code and SSL signals handling were split into separate member-functions; - useless checks 'if (socket)' were deleted; - widget's minimum size is now fixed + font size in 'CertInfo' dialog increased to make it readable. Change-Id: I7aadb78896832a989494d280d6da0635045f948c Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
7896efdd9f
commit
ddb191e47a
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -57,8 +57,8 @@ CertificateInfo::CertificateInfo(QWidget *parent)
|
|||||||
form = new Ui_CertificateInfo;
|
form = new Ui_CertificateInfo;
|
||||||
form->setupUi(this);
|
form->setupUi(this);
|
||||||
|
|
||||||
connect(form->certificationPathView, SIGNAL(currentIndexChanged(int)),
|
connect(form->certificationPathView, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||||
this, SLOT(updateCertificateInfo(int)));
|
this, &CertificateInfo::updateCertificateInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
CertificateInfo::~CertificateInfo()
|
CertificateInfo::~CertificateInfo()
|
||||||
@ -68,25 +68,23 @@ CertificateInfo::~CertificateInfo()
|
|||||||
|
|
||||||
void CertificateInfo::setCertificateChain(const QList<QSslCertificate> &chain)
|
void CertificateInfo::setCertificateChain(const QList<QSslCertificate> &chain)
|
||||||
{
|
{
|
||||||
this->chain = chain;
|
certificateChain = chain;
|
||||||
|
|
||||||
form->certificationPathView->clear();
|
form->certificationPathView->clear();
|
||||||
|
for (int i = 0; i < certificateChain.size(); ++i) {
|
||||||
for (int i = 0; i < chain.size(); ++i) {
|
const QSslCertificate &cert = certificateChain.at(i);
|
||||||
const QSslCertificate &cert = chain.at(i);
|
|
||||||
form->certificationPathView->addItem(tr("%1%2 (%3)").arg(!i ? QString() : tr("Issued by: "))
|
form->certificationPathView->addItem(tr("%1%2 (%3)").arg(!i ? QString() : tr("Issued by: "))
|
||||||
.arg(cert.subjectInfo(QSslCertificate::Organization).join(QLatin1Char(' ')))
|
.arg(cert.subjectInfo(QSslCertificate::Organization).join(QLatin1Char(' ')))
|
||||||
.arg(cert.subjectInfo(QSslCertificate::CommonName).join(QLatin1Char(' '))));
|
.arg(cert.subjectInfo(QSslCertificate::CommonName).join(QLatin1Char(' '))));
|
||||||
}
|
}
|
||||||
|
|
||||||
form->certificationPathView->setCurrentIndex(0);
|
form->certificationPathView->setCurrentIndex(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CertificateInfo::updateCertificateInfo(int index)
|
void CertificateInfo::updateCertificateInfo(int index)
|
||||||
{
|
{
|
||||||
form->certificateInfoView->clear();
|
form->certificateInfoView->clear();
|
||||||
if (index >= 0 && index < chain.size()) {
|
if (index >= 0 && index < certificateChain.size()) {
|
||||||
const QSslCertificate &cert = chain.at(index);
|
const QSslCertificate &cert = certificateChain.at(index);
|
||||||
QStringList lines;
|
QStringList lines;
|
||||||
lines << tr("Organization: %1").arg(cert.subjectInfo(QSslCertificate::Organization).join(QLatin1Char(' ')))
|
lines << tr("Organization: %1").arg(cert.subjectInfo(QSslCertificate::Organization).join(QLatin1Char(' ')))
|
||||||
<< tr("Subunit: %1").arg(cert.subjectInfo(QSslCertificate::OrganizationalUnitName).join(QLatin1Char(' ')))
|
<< tr("Subunit: %1").arg(cert.subjectInfo(QSslCertificate::OrganizationalUnitName).join(QLatin1Char(' ')))
|
||||||
@ -101,9 +99,7 @@ void CertificateInfo::updateCertificateInfo(int index)
|
|||||||
<< tr("Issuer Locality: %1").arg(cert.issuerInfo(QSslCertificate::LocalityName).join(QLatin1Char(' ')))
|
<< tr("Issuer Locality: %1").arg(cert.issuerInfo(QSslCertificate::LocalityName).join(QLatin1Char(' ')))
|
||||||
<< tr("Issuer State/Province: %1").arg(cert.issuerInfo(QSslCertificate::StateOrProvinceName).join(QLatin1Char(' ')))
|
<< tr("Issuer State/Province: %1").arg(cert.issuerInfo(QSslCertificate::StateOrProvinceName).join(QLatin1Char(' ')))
|
||||||
<< tr("Issuer Common Name: %1").arg(cert.issuerInfo(QSslCertificate::CommonName).join(QLatin1Char(' ')));
|
<< tr("Issuer Common Name: %1").arg(cert.issuerInfo(QSslCertificate::CommonName).join(QLatin1Char(' ')));
|
||||||
foreach (QString line, lines)
|
for (const auto &line : lines)
|
||||||
form->certificateInfoView->addItem(line);
|
form->certificateInfoView->addItem(line);
|
||||||
} else {
|
|
||||||
form->certificateInfoView->clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -51,8 +51,9 @@
|
|||||||
#ifndef CERTIFICATEINFO_H
|
#ifndef CERTIFICATEINFO_H
|
||||||
#define CERTIFICATEINFO_H
|
#define CERTIFICATEINFO_H
|
||||||
|
|
||||||
#include <QtWidgets/QDialog>
|
#include <QDialog>
|
||||||
#include <QtNetwork/QSslCertificate>
|
#include <QList>
|
||||||
|
#include <QSslCertificate>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class Ui_CertificateInfo;
|
class Ui_CertificateInfo;
|
||||||
@ -62,7 +63,7 @@ class CertificateInfo : public QDialog
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
CertificateInfo(QWidget *parent = 0);
|
explicit CertificateInfo(QWidget *parent = nullptr);
|
||||||
~CertificateInfo();
|
~CertificateInfo();
|
||||||
|
|
||||||
void setCertificateChain(const QList<QSslCertificate> &chain);
|
void setCertificateChain(const QList<QSslCertificate> &chain);
|
||||||
@ -71,8 +72,8 @@ private slots:
|
|||||||
void updateCertificateInfo(int index);
|
void updateCertificateInfo(int index);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui_CertificateInfo *form;
|
Ui_CertificateInfo *form = nullptr;
|
||||||
QList<QSslCertificate> chain;
|
QList<QSslCertificate> certificateChain;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
<widget class="QListWidget" name="certificateInfoView">
|
<widget class="QListWidget" name="certificateInfoView">
|
||||||
<property name="font">
|
<property name="font">
|
||||||
<font>
|
<font>
|
||||||
<pointsize>8</pointsize>
|
<pointsize>10</pointsize>
|
||||||
</font>
|
</font>
|
||||||
</property>
|
</property>
|
||||||
<property name="wordWrap">
|
<property name="wordWrap">
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -50,6 +50,9 @@
|
|||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QtNetwork>
|
||||||
|
|
||||||
|
QT_REQUIRE_CONFIG(ssl);
|
||||||
|
|
||||||
#include "sslclient.h"
|
#include "sslclient.h"
|
||||||
|
|
||||||
@ -61,7 +64,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
if (!QSslSocket::supportsSsl()) {
|
if (!QSslSocket::supportsSsl()) {
|
||||||
QMessageBox::information(0, "Secure Socket Client",
|
QMessageBox::information(0, "Secure Socket Client",
|
||||||
"This system does not support OpenSSL.");
|
"This system does not support SSL/TLS.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
requires(qtHaveModule(network))
|
||||||
|
|
||||||
HEADERS += certificateinfo.h \
|
HEADERS += certificateinfo.h \
|
||||||
sslclient.h
|
sslclient.h
|
||||||
SOURCES += certificateinfo.cpp \
|
SOURCES += certificateinfo.cpp \
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -50,29 +50,17 @@
|
|||||||
|
|
||||||
#include "certificateinfo.h"
|
#include "certificateinfo.h"
|
||||||
#include "sslclient.h"
|
#include "sslclient.h"
|
||||||
|
|
||||||
#include "ui_sslclient.h"
|
#include "ui_sslclient.h"
|
||||||
#include "ui_sslerrors.h"
|
#include "ui_sslerrors.h"
|
||||||
|
|
||||||
#include <QtWidgets/QScrollBar>
|
#include <QtCore>
|
||||||
#include <QtWidgets/QStyle>
|
|
||||||
#include <QtWidgets/QToolButton>
|
|
||||||
#include <QtWidgets/QMessageBox>
|
|
||||||
#include <QtNetwork/QSslCipher>
|
|
||||||
|
|
||||||
SslClient::SslClient(QWidget *parent)
|
SslClient::SslClient(QWidget *parent)
|
||||||
: QWidget(parent), socket(0), padLock(0), executingDialog(false)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
form = new Ui_Form;
|
setupUi();
|
||||||
form->setupUi(this);
|
setupSecureSocket();
|
||||||
form->hostNameEdit->setSelection(0, form->hostNameEdit->text().size());
|
|
||||||
form->sessionOutput->setHtml(tr("<not connected>"));
|
|
||||||
|
|
||||||
connect(form->hostNameEdit, SIGNAL(textChanged(QString)),
|
|
||||||
this, SLOT(updateEnabledState()));
|
|
||||||
connect(form->connectButton, SIGNAL(clicked()),
|
|
||||||
this, SLOT(secureConnect()));
|
|
||||||
connect(form->sendButton, SIGNAL(clicked()),
|
|
||||||
this, SLOT(sendData()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SslClient::~SslClient()
|
SslClient::~SslClient()
|
||||||
@ -82,17 +70,15 @@ SslClient::~SslClient()
|
|||||||
|
|
||||||
void SslClient::updateEnabledState()
|
void SslClient::updateEnabledState()
|
||||||
{
|
{
|
||||||
bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState;
|
const bool unconnected = socket->state() == QAbstractSocket::UnconnectedState;
|
||||||
|
|
||||||
form->hostNameEdit->setReadOnly(!unconnected);
|
form->hostNameEdit->setReadOnly(!unconnected);
|
||||||
form->hostNameEdit->setFocusPolicy(unconnected ? Qt::StrongFocus : Qt::NoFocus);
|
form->hostNameEdit->setFocusPolicy(unconnected ? Qt::StrongFocus : Qt::NoFocus);
|
||||||
|
|
||||||
form->hostNameLabel->setEnabled(unconnected);
|
form->hostNameLabel->setEnabled(unconnected);
|
||||||
form->portBox->setEnabled(unconnected);
|
form->portBox->setEnabled(unconnected);
|
||||||
form->portLabel->setEnabled(unconnected);
|
form->portLabel->setEnabled(unconnected);
|
||||||
form->connectButton->setEnabled(unconnected && !form->hostNameEdit->text().isEmpty());
|
form->connectButton->setEnabled(unconnected && !form->hostNameEdit->text().isEmpty());
|
||||||
|
|
||||||
bool connected = socket && socket->state() == QAbstractSocket::ConnectedState;
|
const bool connected = socket->state() == QAbstractSocket::ConnectedState;
|
||||||
form->sessionOutput->setEnabled(connected);
|
form->sessionOutput->setEnabled(connected);
|
||||||
form->sessionInput->setEnabled(connected);
|
form->sessionInput->setEnabled(connected);
|
||||||
form->sessionInputLabel->setEnabled(connected);
|
form->sessionInputLabel->setEnabled(connected);
|
||||||
@ -101,20 +87,6 @@ void SslClient::updateEnabledState()
|
|||||||
|
|
||||||
void SslClient::secureConnect()
|
void SslClient::secureConnect()
|
||||||
{
|
{
|
||||||
if (!socket) {
|
|
||||||
socket = new QSslSocket(this);
|
|
||||||
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
|
|
||||||
this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
|
|
||||||
connect(socket, SIGNAL(encrypted()),
|
|
||||||
this, SLOT(socketEncrypted()));
|
|
||||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
|
|
||||||
this, SLOT(socketError(QAbstractSocket::SocketError)));
|
|
||||||
connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
|
|
||||||
this, SLOT(sslErrors(QList<QSslError>)));
|
|
||||||
connect(socket, SIGNAL(readyRead()),
|
|
||||||
this, SLOT(socketReadyRead()));
|
|
||||||
}
|
|
||||||
|
|
||||||
socket->connectToHostEncrypted(form->hostNameEdit->text(), form->portBox->value());
|
socket->connectToHostEncrypted(form->hostNameEdit->text(), form->portBox->value());
|
||||||
updateEnabledState();
|
updateEnabledState();
|
||||||
}
|
}
|
||||||
@ -125,20 +97,18 @@ void SslClient::socketStateChanged(QAbstractSocket::SocketState state)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
updateEnabledState();
|
updateEnabledState();
|
||||||
|
|
||||||
if (state == QAbstractSocket::UnconnectedState) {
|
if (state == QAbstractSocket::UnconnectedState) {
|
||||||
|
form->sessionInput->clear();
|
||||||
form->hostNameEdit->setPalette(QPalette());
|
form->hostNameEdit->setPalette(QPalette());
|
||||||
form->hostNameEdit->setFocus();
|
form->hostNameEdit->setFocus();
|
||||||
form->cipherLabel->setText(tr("<none>"));
|
form->cipherLabel->setText(tr("<none>"));
|
||||||
if (padLock)
|
padLock->hide();
|
||||||
padLock->hide();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SslClient::socketEncrypted()
|
void SslClient::socketEncrypted()
|
||||||
{
|
{
|
||||||
if (!socket)
|
|
||||||
return; // might have disconnected already
|
|
||||||
|
|
||||||
form->sessionOutput->clear();
|
form->sessionOutput->clear();
|
||||||
form->sessionInput->setFocus();
|
form->sessionInput->setFocus();
|
||||||
|
|
||||||
@ -146,36 +116,12 @@ void SslClient::socketEncrypted()
|
|||||||
palette.setColor(QPalette::Base, QColor(255, 255, 192));
|
palette.setColor(QPalette::Base, QColor(255, 255, 192));
|
||||||
form->hostNameEdit->setPalette(palette);
|
form->hostNameEdit->setPalette(palette);
|
||||||
|
|
||||||
QSslCipher ciph = socket->sessionCipher();
|
const QSslCipher cipher = socket->sessionCipher();
|
||||||
QString cipher = QString("%1, %2 (%3/%4)").arg(ciph.authenticationMethod())
|
const QString cipherInfo = QString("%1, %2 (%3/%4)").arg(cipher.authenticationMethod())
|
||||||
.arg(ciph.name()).arg(ciph.usedBits()).arg(ciph.supportedBits());;
|
.arg(cipher.name()).arg(cipher.usedBits())
|
||||||
form->cipherLabel->setText(cipher);
|
.arg(cipher.supportedBits());;
|
||||||
|
form->cipherLabel->setText(cipherInfo);
|
||||||
if (!padLock) {
|
padLock->show();
|
||||||
padLock = new QToolButton;
|
|
||||||
padLock->setIcon(QIcon(":/encrypted.png"));
|
|
||||||
#ifndef QT_NO_CURSOR
|
|
||||||
padLock->setCursor(Qt::ArrowCursor);
|
|
||||||
#endif
|
|
||||||
padLock->setToolTip(tr("Display encryption details."));
|
|
||||||
|
|
||||||
int extent = form->hostNameEdit->height() - 2;
|
|
||||||
padLock->resize(extent, extent);
|
|
||||||
padLock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
|
|
||||||
|
|
||||||
QHBoxLayout *layout = new QHBoxLayout(form->hostNameEdit);
|
|
||||||
layout->setMargin(form->hostNameEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth));
|
|
||||||
layout->setSpacing(0);
|
|
||||||
layout->addStretch();
|
|
||||||
layout->addWidget(padLock);
|
|
||||||
|
|
||||||
form->hostNameEdit->setLayout(layout);
|
|
||||||
|
|
||||||
connect(padLock, SIGNAL(clicked()),
|
|
||||||
this, SLOT(displayCertificateInfo()));
|
|
||||||
} else {
|
|
||||||
padLock->show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SslClient::socketReadyRead()
|
void SslClient::socketReadyRead()
|
||||||
@ -185,7 +131,7 @@ void SslClient::socketReadyRead()
|
|||||||
|
|
||||||
void SslClient::sendData()
|
void SslClient::sendData()
|
||||||
{
|
{
|
||||||
QString input = form->sessionInput->text();
|
const QString input = form->sessionInput->text();
|
||||||
appendString(input + '\n');
|
appendString(input + '\n');
|
||||||
socket->write(input.toUtf8() + "\r\n");
|
socket->write(input.toUtf8() + "\r\n");
|
||||||
form->sessionInput->clear();
|
form->sessionInput->clear();
|
||||||
@ -193,7 +139,12 @@ void SslClient::sendData()
|
|||||||
|
|
||||||
void SslClient::socketError(QAbstractSocket::SocketError)
|
void SslClient::socketError(QAbstractSocket::SocketError)
|
||||||
{
|
{
|
||||||
|
if (handlingSocketError)
|
||||||
|
return;
|
||||||
|
|
||||||
|
handlingSocketError = true;
|
||||||
QMessageBox::critical(this, tr("Connection error"), socket->errorString());
|
QMessageBox::critical(this, tr("Connection error"), socket->errorString());
|
||||||
|
handlingSocketError = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SslClient::sslErrors(const QList<QSslError> &errors)
|
void SslClient::sslErrors(const QList<QSslError> &errors)
|
||||||
@ -201,10 +152,10 @@ void SslClient::sslErrors(const QList<QSslError> &errors)
|
|||||||
QDialog errorDialog(this);
|
QDialog errorDialog(this);
|
||||||
Ui_SslErrors ui;
|
Ui_SslErrors ui;
|
||||||
ui.setupUi(&errorDialog);
|
ui.setupUi(&errorDialog);
|
||||||
connect(ui.certificateChainButton, SIGNAL(clicked()),
|
connect(ui.certificateChainButton, &QPushButton::clicked,
|
||||||
this, SLOT(displayCertificateInfo()));
|
this, &SslClient::displayCertificateInfo);
|
||||||
|
|
||||||
foreach (const QSslError &error, errors)
|
for (const auto &error : errors)
|
||||||
ui.sslErrorList->addItem(error.errorString());
|
ui.sslErrorList->addItem(error.errorString());
|
||||||
|
|
||||||
executingDialog = true;
|
executingDialog = true;
|
||||||
@ -219,10 +170,69 @@ void SslClient::sslErrors(const QList<QSslError> &errors)
|
|||||||
|
|
||||||
void SslClient::displayCertificateInfo()
|
void SslClient::displayCertificateInfo()
|
||||||
{
|
{
|
||||||
CertificateInfo *info = new CertificateInfo(this);
|
CertificateInfo info;
|
||||||
info->setCertificateChain(socket->peerCertificateChain());
|
info.setCertificateChain(socket->peerCertificateChain());
|
||||||
info->exec();
|
info.exec();
|
||||||
info->deleteLater();
|
}
|
||||||
|
|
||||||
|
void SslClient::setupUi()
|
||||||
|
{
|
||||||
|
if (form)
|
||||||
|
return;
|
||||||
|
|
||||||
|
form = new Ui_Form;
|
||||||
|
form->setupUi(this);
|
||||||
|
form->hostNameEdit->setSelection(0, form->hostNameEdit->text().size());
|
||||||
|
form->sessionOutput->setHtml(tr("<not connected>"));
|
||||||
|
|
||||||
|
connect(form->hostNameEdit, SIGNAL(textChanged(QString)),
|
||||||
|
this, SLOT(updateEnabledState()));
|
||||||
|
connect(form->connectButton, SIGNAL(clicked()),
|
||||||
|
this, SLOT(secureConnect()));
|
||||||
|
connect(form->sendButton, SIGNAL(clicked()),
|
||||||
|
this, SLOT(sendData()));
|
||||||
|
|
||||||
|
padLock = new QToolButton;
|
||||||
|
padLock->setIcon(QIcon(":/encrypted.png"));
|
||||||
|
connect(padLock, SIGNAL(clicked()), this, SLOT(displayCertificateInfo()));
|
||||||
|
|
||||||
|
#if QT_CONFIG(cursor)
|
||||||
|
padLock->setCursor(Qt::ArrowCursor);
|
||||||
|
#endif
|
||||||
|
padLock->setToolTip(tr("Display encryption details."));
|
||||||
|
|
||||||
|
const int extent = form->hostNameEdit->height() - 2;
|
||||||
|
padLock->resize(extent, extent);
|
||||||
|
padLock->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
|
||||||
|
|
||||||
|
QHBoxLayout *layout = new QHBoxLayout(form->hostNameEdit);
|
||||||
|
layout->setMargin(form->hostNameEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth));
|
||||||
|
layout->setSpacing(0);
|
||||||
|
layout->addStretch();
|
||||||
|
layout->addWidget(padLock);
|
||||||
|
|
||||||
|
form->hostNameEdit->setLayout(layout);
|
||||||
|
padLock->hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SslClient::setupSecureSocket()
|
||||||
|
{
|
||||||
|
if (socket)
|
||||||
|
return;
|
||||||
|
|
||||||
|
socket = new QSslSocket(this);
|
||||||
|
|
||||||
|
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
|
||||||
|
this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
|
||||||
|
connect(socket, SIGNAL(encrypted()),
|
||||||
|
this, SLOT(socketEncrypted()));
|
||||||
|
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
|
||||||
|
this, SLOT(socketError(QAbstractSocket::SocketError)));
|
||||||
|
connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
|
||||||
|
this, SLOT(sslErrors(QList<QSslError>)));
|
||||||
|
connect(socket, SIGNAL(readyRead()),
|
||||||
|
this, SLOT(socketReadyRead()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SslClient::appendString(const QString &line)
|
void SslClient::appendString(const QString &line)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -51,13 +51,13 @@
|
|||||||
#ifndef SSLCLIENT_H
|
#ifndef SSLCLIENT_H
|
||||||
#define SSLCLIENT_H
|
#define SSLCLIENT_H
|
||||||
|
|
||||||
#include <QtWidgets/QWidget>
|
#include <QtNetwork>
|
||||||
#include <QtNetwork/QAbstractSocket>
|
|
||||||
#include <QtNetwork/QSslSocket>
|
QT_REQUIRE_CONFIG(ssl);
|
||||||
|
|
||||||
|
#include <QtWidgets>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QSslSocket;
|
|
||||||
class QToolButton;
|
|
||||||
class Ui_Form;
|
class Ui_Form;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ class SslClient : public QWidget
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
SslClient(QWidget *parent = 0);
|
explicit SslClient(QWidget *parent = nullptr);
|
||||||
~SslClient();
|
~SslClient();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
@ -80,12 +80,15 @@ private slots:
|
|||||||
void displayCertificateInfo();
|
void displayCertificateInfo();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void setupUi();
|
||||||
|
void setupSecureSocket();
|
||||||
void appendString(const QString &line);
|
void appendString(const QString &line);
|
||||||
|
|
||||||
QSslSocket *socket;
|
QSslSocket *socket = nullptr;
|
||||||
QToolButton *padLock;
|
QToolButton *padLock = nullptr;
|
||||||
Ui_Form *form;
|
Ui_Form *form = nullptr;
|
||||||
bool executingDialog;
|
bool handlingSocketError = false;
|
||||||
|
bool executingDialog = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,6 +10,12 @@
|
|||||||
<height>320</height>
|
<height>320</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="minimumSize">
|
||||||
|
<size>
|
||||||
|
<width>343</width>
|
||||||
|
<height>320</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>Secure Socket Client</string>
|
<string>Secure Socket Client</string>
|
||||||
</property>
|
</property>
|
||||||
@ -114,8 +120,8 @@
|
|||||||
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||||
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
||||||
p, li { white-space: pre-wrap; }
|
p, li { white-space: pre-wrap; }
|
||||||
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
|
</style></head><body style=" font-family:'.SF NS Text'; font-size:13pt; font-weight:400; font-style:normal;">
|
||||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"></p></body></html></string>
|
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html></string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user