/**********************************************************************
 * 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 "sslstatedialog.h"
#include <util/sys/extensionsdata.h>
#include <interfaces/core/icoreproxy.h>
#include <interfaces/core/iiconthememanager.h>
#include "webpagesslwatcher.h"
#include "core.h"

namespace LeechCraft
{
namespace Poshuku
{
	SslStateDialog::SslStateDialog (const WebPageSslWatcher *watcher, QWidget *parent)
	: QDialog { parent }
	{
		Ui_.setupUi (this);

		QString iconName;
		QString title;
		bool hasInsecure = false;
		switch (watcher->GetPageState ())
		{
		case WebPageSslWatcher::State::NoSsl:
			title = tr ("SSL encryption is not used.");
			return;
		case WebPageSslWatcher::State::SslErrors:
			iconName = "security-low";
			title = tr ("Some SSL errors where encountered.");
			break;
		case WebPageSslWatcher::State::UnencryptedElems:
			hasInsecure = true;
			iconName = "security-medium";
			title = tr ("Some elements were loaded via unencrypted connection.");
			break;
		case WebPageSslWatcher::State::FullSsl:
			iconName = "security-high";
			title = tr ("Everything is secure!");
			break;
		}

		if (!hasInsecure)
		{
			const auto insecureIdx = Ui_.TabWidget_->indexOf (Ui_.InsecureTab_);
			Ui_.TabWidget_->removeTab (insecureIdx);
		}
		else
			for (const auto& url : watcher->GetNonSslUrls ())
			{
				auto item = new QTreeWidgetItem ({ url.toString () });

				const auto& urlExt = url.path ().section ('.', -1, -1);
				item->setIcon (0, Util::ExtensionsData::Instance ().GetExtIcon (urlExt));

				Ui_.InsecureList_->addTopLevelItem (item);
			}

		if (!iconName.isEmpty ())
		{
			const auto itm = Core::Instance ().GetProxy ()->GetIconThemeManager ();
			const auto& icon = itm->GetIcon (iconName);
			Ui_.IconLabel_->setPixmap (icon.pixmap (16, 16));
		}

		Ui_.OverallStateLabel_->setText (title);

		const auto& config = watcher->GetPageConfiguration ();
		Certs_ = config.peerCertificateChain ();

		for (const auto& cert : Certs_)
		{
			auto name = cert.subjectInfo (QSslCertificate::CommonName);
			if (name.isEmpty ())
				name = cert.subjectInfo (QSslCertificate::Organization);
			Ui_.CertChainBox_->addItem (name);
		}
	}

	void SslStateDialog::on_CertChainBox__currentIndexChanged (int index)
	{
		const auto& cert = Certs_.value (index);

		auto setSubjectInfo = [&cert] (QLabel *label, QSslCertificate::SubjectInfo key)
		{
			label->setText (cert.subjectInfo (key));
		};
		auto setIssuerInfo = [&cert] (QLabel *label, QSslCertificate::SubjectInfo key)
		{
			label->setText (cert.issuerInfo (key));
		};

		setSubjectInfo (Ui_.SubjectCommonName_, QSslCertificate::CommonName);
		setSubjectInfo (Ui_.SubjectOrganization_, QSslCertificate::Organization);
		setSubjectInfo (Ui_.SubjectUnit_, QSslCertificate::OrganizationalUnitName);
		setSubjectInfo (Ui_.SubjectCountry_, QSslCertificate::CountryName);
		setSubjectInfo (Ui_.SubjectState_, QSslCertificate::StateOrProvinceName);
		setSubjectInfo (Ui_.SubjectCity_, QSslCertificate::LocalityName);
		setIssuerInfo (Ui_.IssuerCommonName_, QSslCertificate::CommonName);
		setIssuerInfo (Ui_.IssuerOrganization_, QSslCertificate::Organization);
		setIssuerInfo (Ui_.IssuerUnit_, QSslCertificate::OrganizationalUnitName);
		setIssuerInfo (Ui_.IssuerCountry_, QSslCertificate::CountryName);
		setIssuerInfo (Ui_.IssuerState_, QSslCertificate::StateOrProvinceName);
		setIssuerInfo (Ui_.IssuerCity_, QSslCertificate::LocalityName);

		Ui_.SerialNumber_->setText (cert.serialNumber ());
		Ui_.Md5_->setText (cert.digest (QCryptographicHash::Md5).toHex ());
		Ui_.Sha1_->setText (cert.digest (QCryptographicHash::Sha1).toHex ());

		Ui_.StartDate_->setText (QLocale {}.toString (cert.effectiveDate ()));
		Ui_.EndDate_->setText (QLocale {}.toString (cert.expiryDate ()));
	}
}
}
