/*
 *  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.
 */

#ifndef CA_H
#define CA_H

#include <QString>
#include <QList>
#include "Request.h"
#include "Certificate.h"
#include "CaOptions.h"

class QWidget;
class QTreeWidget;
class QTextEdit;

class CA
{
public:
	CA();
	virtual ~CA();
	
	void setName(const QString &name);
	void setCaKeyName(const QString &name);
	void setDefaultCertDays(int days);
	void setDefaultCrlDays(int days);
	void setDefaultMD(const QString &md);
	void setPolicy(const QString &countryName, const QString &stateOrProvinceName,
		const QString &localityName, const QString &organizationName,
		const QString &organizationalUnitName, const QString &commonName,
		const QString &emailAddress);
	
	void setInfoWidget(QWidget *widget);
	void setCertOnlyValidDisplay(bool b = true);
	void setCertificateTreeWidget(QTreeWidget *tree);
	void setCertificateInfoWidget(QTextEdit *edit);
	void setRequestTreeWidget(QTreeWidget *tree);
	void setRequestInfoWidget(QTextEdit *edit);
	
	QString name();
	QString caKeyName();
	QString generalInfo();
	QString crlInfo();
	QString policy();
	
	bool create(QString title = QString(), QWidget *parent = 0);
	bool open(QWidget *parent = 0);
	bool update(QWidget *parent = 0);
	bool close(QWidget *parent = 0);
	bool remove(QWidget *parent = 0);
	void changeCertificateInfo();
	void changeRequestInfo();
	
	bool reqGet(QWidget *parent = 0);
	bool reqRecv(QWidget *parent = 0);
	bool reqSign(QWidget *parent = 0);
	bool reqSignAll(QWidget *parent = 0);
	bool reqDelete(QWidget *parent = 0);
	bool certSaveAs(QWidget *parent = 0);
	bool certRevoke(QWidget *parent = 0);
	bool certSend(bool lastSigned = false, QWidget *parent = 0);
	bool certGetData(Certificate *cert, QByteArray &certContents, QString &fileName, QWidget *parent = 0);
	bool crlNew(QWidget *parent = 0);
	bool crlSaveAs(QWidget *parent = 0);
	bool crlDistribute(QWidget *parent = 0);
	bool config(int page = 0, QWidget *parent = 0);
	
	static QList<CA> findAll(QString path);
	static bool importCA(const QStringList &caNames, QWidget *parent = 0);
	static bool exportCA(const QStringList &caNames, QWidget *parent = 0);
	
private:
	void createConfDir();
	void deleteConfDir();
	bool readConfiguration();
	bool writeConfiguration();
	bool readXmlConfig();
	bool writeXmlConfig();
	bool readCaCrl();
	bool readCertificateDb();
	bool readNewRequests(QWidget *parent = 0);
	bool getUserPin(QWidget *parent = 0);
#ifdef STOKEN_SOFT
	bool generatePIN(QWidget *parent = 0);
#endif

	void updateInfo();
	void updateCertificateTree();
	void updateCertificateInfo();
	void updateRequestTree();
	void updateRequestInfo();
	
	QDateTime readOpensslDateTime(QString dateTime);
	bool runOpenssl();
	
private:
	// OpenSSL command name and arguments.
	QString mOpensslCmd;
	QStringList mOpensslArgs;
	QString mOpensslOut;
	QString mOpensslError;
	
	// SToken Data.
	QString mUserPin;
	QString mSTokenName;
	QString mSTokenSerial;
	
	// CA configuration data.
	QString mName;
	QString mConfFileName;
	QString mConfDirPath;
	QString mXmlConfFileName;
	QString mReceivedFileName;
	
	QString mCertsDirName;
	QString mNewCertsDirName;
	QString mCrlDirName;
	QString mPrivateDirName;
	QString mReqDirName;
	QString mNewReqDirName;
	QString mDbFileName;
	QString mCaKeyName;
	QString mCaCertFileName;
	QString mCertSerialFileName;
	QString mCrlFileName;
	QString mCrlSerialFileName;
#ifdef STOKEN_SOFT
	QString mCaKeyDir;
#endif
	
	int mDefaultCertDays;
	int mDefaultCrlDays;
	QString mDefaultMD;
	
	QString mPolicyCountryName;
	QString mPolicyStateOrProvinceName;
	QString mPolicyLocalityName;
	QString mPolicyOrganizationName;
	QString mPolicyOrganizationalUnitName;
	QString mPolicyCommonName;
	QString mPolicyEmailAddress;
	
	// CA certificates and requests.
	QList<Certificate> mCertList;
	QList<Request> mReqList;
	
	// CRL informations.
	bool mCrlIsActual;
	QDateTime mCrlLastUpdate;
	QDateTime mCrlNextUpdate;
	
	// CA display external widgets.
	QWidget *mInfoWidget;
	bool mCertOnlyValidDisplay;
	QTreeWidget *mCertTreeWidget;
	QTextEdit *mCertInfoTextEdit;
	QTreeWidget *mReqTreeWidget;
	QTextEdit *mReqInfoTextEdit;

	// CA options struct
	CaOptions mCaOptions;
};

#endif
