/*
    Copyright (C) 2010 Eduardo Robles Elvira

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "secureclient.h"
#include <QFile>
#include <QtNetwork/QSslCertificate>

SecureClient::SecureClient() : QObject(0)
{
    m_socket = new QSslSocket();
    connect(m_socket, SIGNAL(encrypted()), this, SLOT(connectionEstablished()));
    connect(m_socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(errorOccured(QList<QSslError>)));
    connect(m_socket, SIGNAL(hostFound()), SLOT(hostFound()));
    connect(m_socket, SIGNAL(connected()), SLOT(connected()));
    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)),
        SLOT(error(QAbstractSocket::SocketError)));

    QFile certFile("ca-cert.pem");
    QSslCertificate cert(&certFile);
    m_socket->addCaCertificate(cert);
    m_socket->setPeerVerifyMode(QSslSocket::VerifyPeer);
    m_socket->setPrivateKey("localhost.key");
    m_socket->setLocalCertificate("localhost.pem");

    qDebug() << "caCertificates.first().subjectInfo(QSslCertificate::LocalityName)" << m_socket->caCertificates().first().subjectInfo(QSslCertificate::LocalityName);
}

SecureClient::~SecureClient()
{
    delete m_socket;
}

void SecureClient::error(QAbstractSocket::SocketError socketError)
{
    qDebug() << "SecureClient::error() " << m_socket->errorString();
}

void SecureClient::hostFound()
{
    qDebug() << "SecureClient::hostFound()";
}

void SecureClient::connected()
{
    qDebug() << "SecureClient::connected()";
}

void SecureClient::start(QString hostname, qint16 port)
{
    m_socket->connectToHostEncrypted(hostname, port);
    qDebug() << "SecureClient::start(); called to m_socket->connectToHostEncrypted";
}

void SecureClient::connectionEstablished()
{
    qDebug() << "SecureClient::connectionEstablished()";
    qDebug() << "Peer certificate: is valid? " << m_socket->peerCertificate().isValid() <<
        "\nPeer certificate: CommonName: " <<
        m_socket->peerCertificate().issuerInfo(QSslCertificate::CommonName) <<
        "\nPeer certificate: Organization: " <<
        m_socket->peerCertificate().issuerInfo(QSslCertificate::Organization);
    m_socket->write("hello");
    while (m_socket->waitForReadyRead()) {
     qDebug() << m_socket->readAll().data();
    }
}

void SecureClient::errorOccured(const QList< QSslError >& error)
{
    qDebug() << "SecureClient::errorOccured()";

    m_socket->ignoreSslErrors();
//     m_socket->abort();
}

#include "secureclient.moc"
