/*
 *  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 "Subject.h"

#include <QStringList>

void
Subject::setCountryName(const QString &name)
{
	mCountryName = name;
}

void
Subject::setStateOrProvinceName(const QString &name)
{
	mStateOrProvinceName = name;
}

void
Subject::setLocalityName(const QString &name)
{
	mLocalityName = name;
}

void
Subject::setOrganizationName(const QString &name)
{
	mOrganizationName = name;
}

void
Subject::setOrganizationalUnitName(const QString &name)
{
	mOrganizationalUnitName = name;
}

void
Subject::setCommonName(const QString &name)
{
	mCommonName = name;
}

void
Subject::setEmailAddress(const QString &email)
{
	mEmailAddress = email;
}

#include <QMessageBox>

void
Subject::setAll(const QString &subj)
{
	QStringList fields = subj.split(QChar('/'), QString::SkipEmptyParts);
	
	mCountryName = QString();
	mStateOrProvinceName = QString();
	mLocalityName = QString();
	mOrganizationName = QString();
	mOrganizationalUnitName = QString();
	mCommonName = QString();
	mEmailAddress = QString();
	
	for (int i = 0; i < fields.size(); i++)
	{
		QStringList sfield = fields[i].split(QChar('='));
		QString dn = sfield[0];
		QString name = sfield[1];
		
		if (dn == QString("C"))
			mCountryName = name;
		else if (dn == QString("ST"))
			mStateOrProvinceName = name;
		else if (dn == QString("L"))
			mLocalityName = name;
		else if (dn == QString("O"))
			mOrganizationName = name;
		else if (dn == QString("OU"))
			mOrganizationalUnitName = name;
		else if (dn == QString("CN"))
			mCommonName = name;
		else if (dn == QString("emailAddress"))
			mEmailAddress = name;
	}
}

QString
Subject::countryName()
{
	return mCountryName;
}

QString
Subject::stateOrProvinceName()
{
	return mStateOrProvinceName;
}

QString
Subject::localityName()
{
	return mLocalityName;
}

QString
Subject::organizationName()
{
	return mOrganizationName;
}

QString
Subject::organizationalUnitName()
{
	return mOrganizationalUnitName;
}

QString
Subject::commonName()
{
	return mCommonName;
}

QString
Subject::emailAddress()
{
	return mEmailAddress;
}

QString
Subject::all()
{
	QString subj;
	
	if (mCountryName != QString())
		subj.append(QString("/C=")).append(mCountryName);
	
	if (mStateOrProvinceName != QString())
		subj.append(QString("/ST=")).append(mStateOrProvinceName);
	
	if (mLocalityName != QString())
		subj.append(QString("/L=")).append(mLocalityName);
	
	if (mOrganizationName != QString())
		subj.append(QString("/O=")).append(mOrganizationName);
	
	if (mOrganizationalUnitName != QString())
		subj.append(QString("/OU=")).append(mOrganizationalUnitName);
	
	if (mCommonName != QString())
		subj.append(QString("/CN=")).append(mCommonName);
	
	if (mEmailAddress != QString())
		subj.append(QString("/emailAddress=")).append(mEmailAddress);
	
	return subj;
}
