/*
 *  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 "GenCertReqWidget.h"
#include "FileDialogWPR.h"
#include "MainWindow.h"

QLineEditFiltered::QLineEditFiltered(QWidget *parent) : QLineEdit(parent)
{
	connect(this, SIGNAL(textChanged(const QString&)), this, 
		SLOT(textCheck(const QString&)));
}

void
QLineEditFiltered::textCheck(const QString &name)
{
	foreach (QChar c, name)
	{
		if ((c < 'a' || c > 'z') 
			&& (c < 'A' || c > 'Z') 
			&& (c < '0' || c > '9') 
			&& c != '-' && c != '_'
			&& c != ' ')
		{
			QMessageBox::information(this, tr("Incorrect data"), 
				tr("This field can store alphanumeric characters "
				"[a-z][A-Z][0-9], '_', '-' and ' ' only."));
			setText(mCorrectText);
		}
	}
	
	mCorrectText = text();
}


GenCertReqWidget::GenCertReqWidget(QWidget *parent): QWidget(parent)
{
	mCountryCode = new QLineEdit;
	mCountryCode->setMaxLength(2);
	mStateOrProvinceName = new QLineEditFiltered();
	mLocalityName = new QLineEditFiltered();
	mOrganizationName = new QLineEditFiltered();
	mOrganizationalUnitName = new QLineEditFiltered();
	mCommonName = new QLineEditFiltered();
	mEmailAddress = new QLineEdit();
	
	QGridLayout *layout = new QGridLayout;
	layout->addWidget(new QLabel(tr("Country code")), 0, 0);
	layout->addWidget(mCountryCode, 0, 1);
	layout->addWidget(new QLabel(tr("Region")), 1, 0);
	layout->addWidget(mStateOrProvinceName, 1, 1);
	layout->addWidget(new QLabel(tr("City")), 2, 0);
	layout->addWidget(mLocalityName, 2, 1);
	layout->addWidget(new QLabel(tr("Organization")), 3, 0);
	layout->addWidget(mOrganizationName, 3, 1);
	layout->addWidget(new QLabel(tr("Department")), 4, 0);
	layout->addWidget(mOrganizationalUnitName, 4, 1);
	layout->addWidget(new QLabel(tr("Common name<sup>*</sup>")), 5, 0);
	layout->addWidget(mCommonName, 5, 1);
	layout->addWidget(new QLabel(tr("E-mail")), 6, 0);
	layout->addWidget(mEmailAddress, 6, 1);
	layout->setRowStretch(7, 1);
	layout->setColumnStretch(1, 1);
	setLayout(layout);
}

QString
GenCertReqWidget::subject()
{
	QString subject;
	
	if (mStateOrProvinceName->text() != QString())
		subject += QString("/C=").append(mCountryCode->text());
	
	if (mStateOrProvinceName->text() != QString())
		subject += QString("/ST=").append(mStateOrProvinceName->text());
	
	if (mLocalityName->text() != QString())
		subject += QString("/L=").append(mLocalityName->text());
	
	if (mOrganizationName->text() != QString())
		subject += QString("/O=").append(mOrganizationName->text());
	
	if (mOrganizationalUnitName->text() != QString())
		subject += QString("/OU=").append(mOrganizationalUnitName->text());
	
	if (mCommonName->text() != QString())
		subject += QString("/CN=").append(mCommonName->text());
	
	if (mEmailAddress->text() != QString())
		subject += QString("/emailAddress=").append(mEmailAddress->text());
	
	QString commonName = mCommonName->text().replace(' ', '_');
	
	return subject;
}

bool
GenCertReqWidget::isReady()
{
	if (mCommonName->text() != QString())
		return true;
	
	return false;
}
