/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "sslerrorsdialog.h"
#include <QDateTime>

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	SslErrorsDialog::SslErrorsDialog (QWidget *parent)
	: QDialog (parent)
	{
		Ui_.setupUi (this);
	}

	SslErrorsDialog::SslErrorsDialog (const QString& msg,
			const QList<QSslError>& errors,
			QWidget *parent)
	: QDialog (parent)
	{
		Ui_.setupUi (this);
		Update (msg, errors);
	}

	SslErrorsDialog::~SslErrorsDialog ()
	{
	}

	void SslErrorsDialog::Update (const QString& msg,
			const QList<QSslError>& errors)
	{
		Ui_.Description_->setText (msg);
		for (QList<QSslError>::const_iterator i = errors.begin (),
				end = errors.end (); i != end; ++i)
			PopulateTree (*i);
		Ui_.Errors_->expandAll ();
	}

	SslErrorsDialog::RememberChoice SslErrorsDialog::GetRememberChoice () const
	{
		if (Ui_.RememberNot_->isChecked ())
			return RCNot;
		else if (Ui_.RememberFile_->isChecked ())
			return RCFile;
		else
			return RCHost;
	}

	void SslErrorsDialog::PopulateTree (const QSslError& error)
	{
		QTreeWidgetItem *item = new QTreeWidgetItem (Ui_.Errors_,
				QStringList ("Error:") << error.errorString ());

		QSslCertificate cer = error.certificate ();
		if (cer.isNull ())
		{
			new QTreeWidgetItem (item,
					QStringList (tr ("Certificate")) <<
						tr ("(No certificate available for this error)"));
			return;
		}

		new QTreeWidgetItem (item, QStringList (tr ("Valid:")) <<
					(cer.isValid () ? tr ("yes") : tr ("no")));
		new QTreeWidgetItem (item, QStringList (tr ("Effective date:")) <<
					cer.effectiveDate ().toString ());
		new QTreeWidgetItem (item, QStringList (tr ("Expiry date:")) <<
					cer.expiryDate ().toString ());
		new QTreeWidgetItem (item, QStringList (tr ("Version:")) <<
					cer.version ());
		new QTreeWidgetItem (item, QStringList (tr ("Serial number:")) <<
					cer.serialNumber ());
		new QTreeWidgetItem (item, QStringList (tr ("MD5 digest:")) <<
					cer.digest ().toHex ());
		new QTreeWidgetItem (item, QStringList (tr ("SHA1 digest:")) <<
					cer.digest (QCryptographicHash::Sha1).toHex ());

		QTreeWidgetItem *issuer = new QTreeWidgetItem (item,
				QStringList (tr ("Issuer info")));

		QString tmpString;
		tmpString = cer.issuerInfo (QSslCertificate::Organization);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (issuer,
					QStringList (tr ("Organization:")) << tmpString);

		tmpString = cer.issuerInfo (QSslCertificate::CommonName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (issuer,
					QStringList (tr ("Common name:")) << tmpString);

		tmpString = cer.issuerInfo (QSslCertificate::LocalityName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (issuer,
					QStringList (tr ("Locality:")) << tmpString);

		tmpString = cer.issuerInfo (QSslCertificate::OrganizationalUnitName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (issuer,
					QStringList (tr ("Organizational unit name:")) << tmpString);

		tmpString = cer.issuerInfo (QSslCertificate::CountryName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (issuer,
					QStringList (tr ("Country name:")) << tmpString);

		tmpString = cer.issuerInfo (QSslCertificate::StateOrProvinceName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (issuer,
					QStringList (tr ("State or province name:")) << tmpString);

		QTreeWidgetItem *subject = new QTreeWidgetItem (item,
				QStringList (tr ("Subject info")));

		tmpString = cer.subjectInfo (QSslCertificate::Organization);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (subject,
					QStringList (tr ("Organization:")) << tmpString);

		tmpString = cer.subjectInfo (QSslCertificate::CommonName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (subject,
					QStringList (tr ("Common name:")) << tmpString);

		tmpString = cer.subjectInfo (QSslCertificate::LocalityName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (subject,
					QStringList (tr ("Locality:")) << tmpString);

		tmpString = cer.subjectInfo (QSslCertificate::OrganizationalUnitName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (subject,
					QStringList (tr ("Organizational unit name:")) << tmpString);

		tmpString = cer.subjectInfo (QSslCertificate::CountryName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (subject,
					QStringList (tr ("Country name:")) << tmpString);

		tmpString = cer.subjectInfo (QSslCertificate::StateOrProvinceName);
		if (!tmpString.isEmpty ())
			new QTreeWidgetItem (subject,
					QStringList (tr ("State or province name:")) << tmpString);
	}
}
}
}
