/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  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.
 */

#include "Certificate.h"

void
Certificate::setState(const QString &state)
{
	mState = state;
	QIcon icon;
	
	if (state == QString("V")) icon = QIcon(QString(":img/ok.png"));
	else if (state == QString("R")) icon = QIcon(QString(":img/cancel.png"));
	else if (state == QString("E")) icon = QIcon();
	else if (state == QString("S")) icon = QIcon(QString(":img/lock.png"));
	else icon = QIcon(QString(":img/help.png"));
	
	QTreeWidgetItem::setIcon(0, icon);
}

void
Certificate::setSerial(const QString &serial)
{
	mSerial = serial.toInt(0, 16);
	mFileName = serial + QString(".pem");
	QTreeWidgetItem::setText(0, QString("%1").arg(mSerial, 6, 10, QLatin1Char(' ')));
	QTreeWidgetItem::setText(1, Subject::commonName());
	QTreeWidgetItem::setText(2, Subject::organizationName());
}

void
Certificate::setFileName(const QString &name)
{
	mFileName = name;
}

void
Certificate::setText(const QString &text)
{
	mText = text;
	
	mSigAlg = mText;
	int i = mSigAlg.indexOf(QString("Signature Algorithm: "));
	int j = mSigAlg.indexOf(QChar('\n'), i);
	mSigAlg.chop(mSigAlg.count() - j);
	mSigAlg.remove(0, i + QString("Signature Algorithm: ").count());
}

void
Certificate::setSubject(const QString &subj)
{
	Subject::setAll(subj);
	QTreeWidgetItem::setText(0, QString("%1").arg(mSerial, 6, 10, QLatin1Char(' ')));
	QTreeWidgetItem::setText(1, Subject::commonName());
	QTreeWidgetItem::setText(2, Subject::organizationName());
}

void
Certificate::setNotBefore(QString time)
{
	time.remove(QChar('Z'));
	time.prepend(QString("20"));
	mNotBefore = QDateTime::fromString(time, QString("yyyyMMddhhmmss"));
}

void
Certificate::setNotAfter(QString time)
{
	time.remove(QChar('Z'));
	time.prepend(QString("20"));
	mNotAfter = QDateTime::fromString(time, QString("yyyyMMddhhmmss"));
}

void
Certificate::setRevocationDate(QString time)
{
	time.remove(QChar('Z'));
	time.prepend(QString("20"));
	mRevocationDate = QDateTime::fromString(time, QString("yyyyMMddhhmmss"));
}

void
Certificate::setRevocationReason(QString reason)
{
	mRevocationReason = reason;
}

QString
Certificate::state()
{
	return mState;
}

int
Certificate::serial()
{
	return mSerial;
}

QString
Certificate::fileName()
{
	return mFileName;
}

QString
Certificate::text()
{
	return mText;
}

QString
Certificate::subject()
{
	return Subject::all();
}

QString
Certificate::signatureAlgorithm()
{
	return mSigAlg;
}

QDateTime
Certificate::notBefore()
{
	return mNotBefore;
}

QDateTime
Certificate::notAfter()
{
	return mNotAfter;
}

QDateTime
Certificate::revocationDate()
{
	return mRevocationDate;
}

QString
Certificate::revocationReason()
{
	return mRevocationReason;
}
