From a133cea211b77a98f0d35b0b3e3c214bac1dc6b8 Mon Sep 17 00:00:00 2001
From: Kai Koehne <kai.koehne@theqtcompany.com>
Date: Tue, 21 Oct 2014 15:24:37 +0200
Subject: [PATCH] SecureSocketClient: Show general connection errors

So far the example didn't show any errors when the connection to the
server couldn't be established (e.g. because the host/port number was
wrong, but also because the SSL handshake already failed).

The fix shows a modal error dialog in this case.

Change-Id: Ia517bf79c5fb2ccdb1edabded28f86759f3e6dcf
Reviewed-by: Richard J. Moore <rich@kde.org>
---
 examples/network/securesocketclient/sslclient.cpp | 12 +++++++++---
 examples/network/securesocketclient/sslclient.h   |  3 ++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/examples/network/securesocketclient/sslclient.cpp b/examples/network/securesocketclient/sslclient.cpp
index 3ca0a6dc596..23ca7e41e78 100644
--- a/examples/network/securesocketclient/sslclient.cpp
+++ b/examples/network/securesocketclient/sslclient.cpp
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the examples of the Qt Toolkit.
@@ -46,6 +46,7 @@
 #include <QtWidgets/QScrollBar>
 #include <QtWidgets/QStyle>
 #include <QtWidgets/QToolButton>
+#include <QtWidgets/QMessageBox>
 #include <QtNetwork/QSslCipher>
 
 SslClient::SslClient(QWidget *parent)
@@ -96,6 +97,8 @@ void SslClient::secureConnect()
                 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()),
@@ -118,8 +121,6 @@ void SslClient::socketStateChanged(QAbstractSocket::SocketState state)
         form->cipherLabel->setText(tr("<none>"));
         if (padLock)
             padLock->hide();
-        socket->deleteLater();
-        socket = 0;
     }
 }
 
@@ -180,6 +181,11 @@ void SslClient::sendData()
     form->sessionInput->clear();
 }
 
+void SslClient::socketError(QAbstractSocket::SocketError error)
+{
+    QMessageBox::critical(this, tr("Connection error"), socket->errorString());
+}
+
 void SslClient::sslErrors(const QList<QSslError> &errors)
 {
     QDialog errorDialog(this);
diff --git a/examples/network/securesocketclient/sslclient.h b/examples/network/securesocketclient/sslclient.h
index 8ebf2cc5365..efc35697431 100644
--- a/examples/network/securesocketclient/sslclient.h
+++ b/examples/network/securesocketclient/sslclient.h
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the examples of the Qt Toolkit.
@@ -65,6 +65,7 @@ private slots:
     void socketEncrypted();
     void socketReadyRead();
     void sendData();
+    void socketError(QAbstractSocket::SocketError error);
     void sslErrors(const QList<QSslError> &errors);
     void displayCertificateInfo();
 
-- 
GitLab