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

#ifndef STOKEN_SOFT
static const QString welcomeString = QObject::tr(
	"<center><h2>sToken CA v1.2</h2></center>");
#else
static const QString welcomeString = QObject::tr(
	"<center><h2>sToken SoftCA v1.2</h2></center>");
#endif

extern QString lang;

MainWindow::MainWindow(QMainWindow *parent): QMainWindow(parent)
{
	createActions();
	createMenus();
	createToolBars();
	createWelcomeWidget();
	createCaInfoWidget();
	createCentralWidget();
	createAboutBox();
	
	mCaActive = 0;
	mCaList = CA::findAll(QDir::current().path());
	
#ifndef STOKEN_SOFT
	setWindowTitle(tr("sToken CA"));
	setWindowIcon(QIcon(QString(":img/stoken_ca_032.png")));
#else
	setWindowTitle(tr("sToken SoftCA"));
	setWindowIcon(QIcon(QString(":img/stoken_softca_032.png")));
#endif
	
	resize(700, 500);
}

void
MainWindow::caNew()
{
	CA ca;
	
	if (ca.create(tr("New CA"), this))
	{
		int i = 0;
		
		while (i < mCaList.size())
		{
			if (ca.name() < mCaList[i].name())
				break;
			
			i++;
		}
		
		mCaList.insert(i, ca);
	}
}

void
MainWindow::caOpen()
{
	QStringList caNames;
	
	for (int i = 0; i < mCaList.size(); i++)
		caNames << mCaList[i].name();
	
	bool ok;
	QString name = QInputDialog::getItem(this, tr("Open"), tr("Select CA"), caNames, 
		0, false, &ok);
	
	if (ok && (0 < mCaList.size()))
	{
		caClose();
		
		for (int i = 0; i < mCaList.size(); i++)
		{
			if (mCaList[i].name() == name)
			{
				mCaActive = &mCaList[i];
				break;
			}
		}
		
		mCaActive->setCertificateTreeWidget(mCertTree);
		mCaActive->setCertificateInfoWidget(mCertInfo);
		mCaActive->setRequestTreeWidget(mReqTree);
		mCaActive->setRequestInfoWidget(mReqInfo);
		
		if (!mCaActive->open(this))
			return;
		
		if (!mCaActive->update(this))
		{
			mCaActive->close(this);
			return;
		}
		
		mLeftPanelTab->setCurrentIndex(0);
		mCaUpdateAct->setEnabled(true);
		mCaCloseAct->setEnabled(true);
		mCaConfigAct->setEnabled(true);
		mCertGetReqAct->setEnabled(true);
		mCertRecvReqAct->setEnabled(true);
		mCertViewAllAct->setEnabled(true);
		mCertViewValidAct->setEnabled(true);
		mCertSaveAsAct->setEnabled(true);
		mCertSendAct->setEnabled(true);
		mCertRevokeAct->setEnabled(true);
		mCrlNewAct->setEnabled(true);
		mCrlSaveAsAct->setEnabled(true);
		mCaImportAct->setEnabled(false);
		mCrlFtpEditAct->setEnabled(true);
		mCrlFtpDistAct->setEnabled(true);
		
		caUpdateInfo();
		
		mRightPanelStack->setCurrentWidget(mCaInfo);
	}
}

void
MainWindow::caUpdate()
{
	if (!mCaActive)
		return;
	
	mCaActive->update(this);
	caUpdateInfo();
}

void
MainWindow::caUpdateInfo()
{
	if (!mCaActive)
		return;
	
	mCaName->setText(QString("<center><h2>") + mCaActive->name() + QString("</h2></center>"));
	mCaGeneralInfo->setText(mCaActive->generalInfo());
	mCaCrlInfo->setText(mCaActive->crlInfo());
	mCaPolicy->setText(mCaActive->policy());
}

void
MainWindow::caClose()
{
	if (!mCaActive)
		return;
	
	mLeftPanelTab->setCurrentIndex(0);
	mCaUpdateAct->setEnabled(false);
	mCaCloseAct->setEnabled(false);
	mCaConfigAct->setEnabled(false);
	mCertGetReqAct->setEnabled(false);
	mCertRecvReqAct->setEnabled(false);
	mCertSignReqAct->setEnabled(false);
	mCertSignReqAllAct->setEnabled(false);
	mCertDeleteReqAct->setEnabled(false);
	mCertViewAllAct->setEnabled(false);
	mCertViewValidAct->setEnabled(false);
	mCertSaveAsAct->setEnabled(false);
	mCertSendAct->setEnabled(false);
	mCertRevokeAct->setEnabled(false);
	mCrlNewAct->setEnabled(false);
	mCrlSaveAsAct->setEnabled(false);
	mCaImportAct->setEnabled(true);
	mCrlFtpEditAct->setEnabled(false);
	mCrlFtpDistAct->setEnabled(false);
	
	mCaActive->close(this);
	mCaActive = 0;
	
	mRightPanelStack->setCurrentWidget(mWelcome);
}

void
MainWindow::caConfig()
{
	if (!mCaActive)
		return;
	
	mCaActive->config(0, this);
}

void
MainWindow::caDelete()
{
	QStringList caNames;
	
	for (int i = 0; i < mCaList.size(); i++)
		caNames << mCaList[i].name();
	
	bool ok;
	QString name = QInputDialog::getItem(this, tr("Delete"), tr("Select CA"), caNames, 
		0, false, &ok);
	
	if (ok)
	{
		for (int i = 0; i < mCaList.size(); i++)
		{
			if (mCaList[i].name() == name)
			{
				if (mCaActive == &mCaList[i])
					caClose();
				
				mCaList[i].remove();
				mCaList.removeAt(i);
				break;
			}
		}
		
	}
}

void
MainWindow::caImport()
{
	QStringList caNames;
	
	for (int i = 0; i < mCaList.size(); i++)
		caNames << mCaList[i].name();
	
	bool result = CA::importCA(caNames, this);
	
	if(result)
	{
		mCaList = CA::findAll(QDir::current().path());
	}
}

void
MainWindow::caExport()
{
	QStringList caNames;
	
	for (int i = 0; i < mCaList.size(); i++)
		caNames << mCaList[i].name();
	
	CA::exportCA(caNames, this);
}

void
MainWindow::changeCertInfo(QTreeWidgetItem *, QTreeWidgetItem *)
{
	mCaActive->changeCertificateInfo();
}

void
MainWindow::changeReqInfo(QTreeWidgetItem *, QTreeWidgetItem *)
{
	mCaActive->changeRequestInfo();
}

void
MainWindow::certGetReq()
{
	if (!mCaActive)
		return;
	
	mCaActive->reqGet(this);
}

void
MainWindow::certRecvReq()
{
	if (!mCaActive)
		return;
	
	mCaActive->reqRecv(this);
}

void
MainWindow::certSignReq()
{
	if (!mCaActive)
		return;
	
	mCaActive->reqSign(this);
}

void
MainWindow::certSignReqAll()
{
	if (!mCaActive)
		return;
	
	mCaActive->reqSignAll(this);
}

void
MainWindow::certDeleteReq()
{
	if (!mCaActive)
		return;
	
	mCaActive->reqDelete(this);
}

void
MainWindow::certViewAll()
{
	if (!mCaActive)
		return;
	
	mCaActive->setCertOnlyValidDisplay(false);
}

void
MainWindow::certViewValid()
{
	if (!mCaActive)
		return;
	
	mCaActive->setCertOnlyValidDisplay(true);
}

void
MainWindow::certSaveAs()
{
	if (!mCaActive)
		return;
	
	mCaActive->certSaveAs(this);
}

void
MainWindow::certSend()
{
	if (!mCaActive)
		return;
	
	mCaActive->certSend(false, this);
}

void
MainWindow::certRevoke()
{
	if (!mCaActive)
		return;
	
	mCaActive->certRevoke(this);
	caUpdateInfo();
}

void
MainWindow::crlNew()
{
	if (!mCaActive)
		return;
	
	mCaActive->crlNew(this);
	caUpdateInfo();
}

void
MainWindow::crlSaveAs()
{
	if (!mCaActive)
		return;
	
	mCaActive->crlSaveAs(this);
}

void
MainWindow::crlFtpEdit()
{
	if (!mCaActive)
		return;
	
	mCaActive->config(1, this);
}

void
MainWindow::crlDistribute()
{
	if (!mCaActive)
		return;
	
	mCaActive->crlDistribute(this);
}

void
MainWindow::changeLeftPanelTab(int index)
{
	if (!mCaActive)
		return;
	
	bool certEnable = true;
	
	if (index == 1)
		certEnable = false;
	
	mCertSignReqAct->setEnabled(!certEnable);
	mCertSignReqAllAct->setEnabled(!certEnable);
	mCertDeleteReqAct->setEnabled(!certEnable);
	mCertSignReqAct->setEnabled(!certEnable);
	mCertSaveAsAct->setEnabled(certEnable);
	mCertSendAct->setEnabled(certEnable);
	mCertRevokeAct->setEnabled(certEnable);
}

void
MainWindow::helpSlot()
{
	mAboutBox->setActiveHelp();
	mAboutBox->exec();
}

void
MainWindow::aboutSlot()
{
	mAboutBox->setActiveAbout();
	mAboutBox->exec();
}

void
MainWindow::langEnSlot()
{
	lang = "";
	langExclamation();
}

void
MainWindow::langPlSlot()
{
	lang = "pl";
	langExclamation();
}

void
MainWindow::langExclamation()
{
	QMessageBox::information(this, tr("Information"), tr("Application language "
		"was changed. Please restart application to apply changes."));
}

void
MainWindow::createActions()
{
	mExitAct = new QAction(QIcon(QString(":img/exit.png")), tr("E&xit"), this);
	mExitAct->setShortcut(tr("Ctrl+Q"));
	connect(mExitAct, SIGNAL(triggered()), this, SLOT(close()));
	
	mCaNewAct = new QAction(QIcon(QString(":img/add.png")), tr("&New"), this);
	connect(mCaNewAct, SIGNAL(triggered()), this, SLOT(caNew()));
	
	mCaOpenAct = new QAction(tr("&Open"), this);
	mCaOpenAct->setShortcut(tr("Ctrl+O"));
	connect(mCaOpenAct, SIGNAL(triggered()), this, SLOT(caOpen()));
	
	mCaUpdateAct = new QAction(tr("&Actualize"), this);
	mCaUpdateAct->setEnabled(false);
	connect(mCaUpdateAct, SIGNAL(triggered()), this, SLOT(caUpdate()));
	
	mCaCloseAct = new QAction(tr("Clo&se"), this);
	mCaCloseAct->setEnabled(false);
	connect(mCaCloseAct, SIGNAL(triggered()), this, SLOT(caClose()));
	
	mCaConfigAct = new QAction(QIcon(QString(":img/configure.png")), tr("CA settings"), this);
	mCaConfigAct->setShortcut(tr("F11"));
	mCaConfigAct->setEnabled(false);
	connect(mCaConfigAct, SIGNAL(triggered()), this, SLOT(caConfig()));
	
	mCaImportAct = new QAction(tr("&Import"), this);
	connect(mCaImportAct, SIGNAL(triggered()), this, SLOT(caImport()));
	
	mCaExportAct = new QAction(tr("&Export"), this);
	connect(mCaExportAct, SIGNAL(triggered()), this, SLOT(caExport()));
	
	mCaDeleteAct = new QAction(tr("&Delete"), this);
	connect(mCaDeleteAct, SIGNAL(triggered()), this, SLOT(caDelete()));
	
	mCertGetReqAct = new QAction(QIcon(QString(":img/add_user.png")), tr("&Select requests"), this);
	mCertGetReqAct->setShortcut(tr("Ctrl+N"));
	mCertGetReqAct->setEnabled(false);
	connect(mCertGetReqAct, SIGNAL(triggered()), this, SLOT(certGetReq()));
	
	mCertRecvReqAct = new QAction(QIcon(QString(":img/get_req.png")), tr("&Receive requests"), this);
	mCertRecvReqAct->setShortcut(tr("Ctrl+R"));
	mCertRecvReqAct->setEnabled(false);
	connect(mCertRecvReqAct, SIGNAL(triggered()), this, SLOT(certRecvReq()));
	
	mCertSignReqAct = new QAction(QIcon(QString(":img/sign.png")), tr("Sign &request"), this);
	mCertSignReqAct->setShortcut(tr("Ctrl+P"));
	mCertSignReqAct->setEnabled(false);
	connect(mCertSignReqAct, SIGNAL(triggered()), this, SLOT(certSignReq()));
	
	mCertSignReqAllAct = new QAction(tr("Sign &all requests"), this);
	mCertSignReqAllAct->setEnabled(false);
	connect(mCertSignReqAllAct, SIGNAL(triggered()), this, SLOT(certSignReqAll()));
	
	mCertDeleteReqAct = new QAction(tr("Delete request"), this);
	mCertDeleteReqAct->setEnabled(false);
	connect(mCertDeleteReqAct, SIGNAL(triggered()), this, SLOT(certDeleteReq()));
	
	mCertViewAllAct = new QAction(QIcon(QString(":img/viewcert_all.png")), tr("Show all certificates"), this);
	mCertViewAllAct->setEnabled(false);
	connect(mCertViewAllAct, SIGNAL(triggered()), this, SLOT(certViewAll()));
	
	mCertViewValidAct = new QAction(QIcon(QString(":img/viewcert_valid.png")), tr("Show valid certificates"), this);
	mCertViewValidAct->setEnabled(false);
	connect(mCertViewValidAct, SIGNAL(triggered()), this, SLOT(certViewValid()));
	
	mCertSaveAsAct = new QAction(QIcon(QString(":img/filecmd_saveas.png")), tr("&Save certificate"), this);
	mCertSaveAsAct->setShortcut(tr("Ctrl+S"));
	mCertSaveAsAct->setEnabled(false);
	connect(mCertSaveAsAct, SIGNAL(triggered()), this, SLOT(certSaveAs()));
	
	mCertSendAct = new QAction(tr("Send certificate"), this);
	mCertSendAct->setEnabled(false);
	connect(mCertSendAct, SIGNAL(triggered()), this, SLOT(certSend()));
	
	mCertRevokeAct = new QAction(QIcon(QString(":img/cancel.png")), tr("Revoke certificate"), this);
	mCertRevokeAct->setShortcut(tr("Ctrl+D"));
	mCertRevokeAct->setEnabled(false);
	connect(mCertRevokeAct, SIGNAL(triggered()), this, SLOT(certRevoke()));
	
	mCrlNewAct = new QAction(QIcon(QString(":img/crl_new.png")), tr("New CRL list"), this);
	mCrlNewAct->setShortcut(tr("Ctrl+L"));
	mCrlNewAct->setEnabled(false);
	connect(mCrlNewAct, SIGNAL(triggered()), this, SLOT(crlNew()));
	
	mCrlSaveAsAct = new QAction(QIcon(QString(":img/crl_saveas.png")), tr("Save CRL as"), this);
	mCrlSaveAsAct->setEnabled(false);
	connect(mCrlSaveAsAct, SIGNAL(triggered()), this, SLOT(crlSaveAs()));
	
	mCrlFtpEditAct = new QAction(tr("Distributing CRL via FTP settings"), this);
	mCrlFtpEditAct->setEnabled(false); 
	connect(mCrlFtpEditAct, SIGNAL(triggered()), this, SLOT(crlFtpEdit()));
	
	mCrlFtpDistAct = new QAction(QIcon(QString(":img/crl_ftp.png")), tr("Distribute CRL via FTP"), this);
	connect(mCrlFtpDistAct, SIGNAL(triggered()), this, SLOT(crlDistribute()));
	mCrlFtpDistAct->setEnabled(false);
	
	mHelpAct = new QAction(QIcon(QString(":img/contents_open.png")), tr("Manual"), this);
	connect(mHelpAct, SIGNAL(triggered()), this, SLOT(helpSlot()));
	mHelpAct->setShortcut(tr("Ctrl+H"));
	
	mAboutAct = new QAction(QIcon(QString(":img/info.png")), tr("About"), this);
	connect(mAboutAct, SIGNAL(triggered()), this, SLOT(aboutSlot()));
	
	mLang = new QActionGroup(this);
	mLangEn = new QAction(tr("English"), this);
	mLangEn->setCheckable(true);
	connect(mLangEn, SIGNAL(triggered()), this, SLOT(langEnSlot()));
	mLangPl = new QAction(tr("Polish"), this);
	mLangPl->setCheckable(true);
	connect(mLangPl, SIGNAL(triggered()), this, SLOT(langPlSlot()));
	
	mLang->addAction(mLangEn);
	mLang->addAction(mLangPl);
}

void
MainWindow::createMenus()
{
	mGeneralMenu = menuBar()->addMenu(tr("General"));
	
	QMenu *language;
	language = mGeneralMenu->addMenu(tr("&Language"));
	language->addAction(mLangEn);
	language->addAction(mLangPl);
	if(lang == "pl")
		mLangPl->setChecked(true);
	else
		mLangEn->setChecked(true);
	
	mGeneralMenu->addSeparator();
	
	mGeneralMenu->addAction(mExitAct);
	
	mCaMenu = menuBar()->addMenu(tr("CA"));
	mCaMenu->addAction(mCaNewAct);
	mCaMenu->addSeparator();
	mCaMenu->addAction(mCaOpenAct);
	mCaMenu->addAction(mCaUpdateAct);
	mCaMenu->addAction(mCaCloseAct);
	mCaMenu->addSeparator();
	mCaMenu->addAction(mCaConfigAct);
	mCaMenu->addSeparator();
	mCaMenu->addAction(mCaImportAct);
	mCaMenu->addAction(mCaExportAct);
	mCaMenu->addSeparator();
	mCaMenu->addAction(mCaDeleteAct);
	
	mCertMenu = menuBar()->addMenu(tr("Certificates"));
	mCertMenu->addAction(mCertGetReqAct);
	mCertMenu->addAction(mCertRecvReqAct);
	mCertMenu->addSeparator();
	mCertMenu->addAction(mCertSignReqAct);
	mCertMenu->addAction(mCertSignReqAllAct);
	mCertMenu->addSeparator();
	mCertMenu->addAction(mCertViewAllAct);
	mCertMenu->addAction(mCertViewValidAct);
	mCertMenu->addSeparator();
	mCertMenu->addAction(mCertSaveAsAct);
	mCertMenu->addSeparator();
	mCertMenu->addAction(mCertRevokeAct);
	
	mCrlMenu = menuBar()->addMenu(tr("CRL lists"));
	mCrlMenu->addAction(mCrlNewAct);
	mCrlMenu->addSeparator();
	mCrlMenu->addAction(mCrlSaveAsAct);
	mCrlMenu->addSeparator();
	mCrlMenu->addAction(mCrlFtpEditAct); 
	mCrlMenu->addAction(mCrlFtpDistAct);
	
	mHelpMenu = menuBar()->addMenu(tr("Help"));
	mHelpMenu->addAction(mHelpAct);
	mHelpMenu->addSeparator();
	mHelpMenu->addAction(mAboutAct);
}

void
MainWindow::createToolBars()
{
	mCertToolBar = addToolBar(tr("Certificates"));
	mCertToolBar->addAction(mCertGetReqAct);
	mCertToolBar->addAction(mCertRecvReqAct);
	mCertToolBar->addAction(mCertSignReqAct);
	mCertToolBar->addSeparator();
	mCertToolBar->addAction(mCertViewAllAct);
	mCertToolBar->addAction(mCertViewValidAct);
	mCertToolBar->addSeparator();
	mCertToolBar->addAction(mCertSaveAsAct);
	mCertToolBar->addSeparator();
	mCertToolBar->addAction(mCertRevokeAct);
	
	mCrlToolBar = addToolBar(tr("CRL"));
	mCrlToolBar->addAction(mCrlNewAct);
	mCrlToolBar->addSeparator();
	mCrlToolBar->addAction(mCrlSaveAsAct);
	mCrlToolBar->addAction(mCrlFtpDistAct);
}

void
MainWindow::createWelcomeWidget()
{
	mWelcome = new QWidget;
	
	QGridLayout *grid = new QGridLayout;
	QLabel *label = new QLabel();
	
	label->setPixmap(QPixmap(QString(":img/stoken_logo_120_60.png")));
	label->setAlignment(Qt::AlignRight);
	grid->addWidget(label, 0, 0);
	grid->addWidget(new QLabel(welcomeString), 1, 0);
	grid->setRowStretch(1, 1);
	grid->setRowStretch(2, 1);
	
	mWelcome->setLayout(grid);
}

void
MainWindow::createCaInfoWidget()
{
	QGridLayout *grid = new QGridLayout;
	QGridLayout *boxGrid;
	QGroupBox *box;
	
	// CA information widget.
	mCaInfo = new QWidget;
	mCaName = new QLabel;
	mCaGeneralInfo = new QLabel;
	mCaCrlInfo = new QLabel;
	mCaPolicy = new QLabel;
	
	QLabel *label = new QLabel();
	label->setPixmap(QPixmap(QString(":img/logo_stoken_120_60.png")));
	label->setAlignment(Qt::AlignRight);
	grid->addWidget(label, 0, 0, 1, 4);
	
	grid->addWidget(mCaName, 1, 1, 1, 2);
	
	box = new QGroupBox(tr("General information"));
	boxGrid = new QGridLayout;
	boxGrid->addWidget(mCaGeneralInfo, 0, 0);
	box->setLayout(boxGrid);
	grid->addWidget(box, 2, 0, 1, 4);
	
	box = new QGroupBox(tr("CA policy"));
	boxGrid = new QGridLayout;
	boxGrid->addWidget(mCaPolicy, 0, 0);
	box->setLayout(boxGrid);
	grid->addWidget(box, 3, 0, 1, 4);
	
	box = new QGroupBox(tr("CRL list"));
	boxGrid = new QGridLayout;
	boxGrid->addWidget(mCaCrlInfo, 0, 0);
	box->setLayout(boxGrid);
	grid->addWidget(box, 4, 0, 1, 4);
	
	grid->setRowStretch(5, 1);
	
	mCaInfo->setLayout(grid);
}

void
MainWindow::createCentralWidget()
{
	QSplitter *centralPanel = new QSplitter(Qt::Horizontal);
	QSplitter *certPanel = new QSplitter(Qt::Vertical);
	QSplitter *reqPanel = new QSplitter(Qt::Vertical);
	
	mRightPanelStack = new QStackedWidget;
	mRightPanelStack->addWidget(mWelcome);
	mRightPanelStack->addWidget(mCaInfo);
	
	mLeftPanelTab = new QTabWidget;
	connect(mLeftPanelTab, SIGNAL(currentChanged(int)), this, SLOT(changeLeftPanelTab(int)));
	
	QStringList certLabels;
	certLabels << tr("Number");
	certLabels << tr("Name");
	certLabels << tr("Organization");
	
	mCertTree = new QTreeWidget;
	mCertTree->setHeaderLabels(certLabels);
	mCertTree->setSortingEnabled(true);
	connect(mCertTree, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, 
		SLOT(changeCertInfo(QTreeWidgetItem *, QTreeWidgetItem *)));
	mCertInfo = new QTextEdit;
	
	QAction *sep1 = new QAction("", this);
	sep1->setSeparator(true);
	
	mCertTree->addAction(mCertSaveAsAct);
	mCertTree->addAction(mCertSendAct);
	mCertTree->addAction(sep1);
	mCertTree->addAction(mCertRevokeAct);
	mCertTree->setContextMenuPolicy(Qt::ActionsContextMenu);
	
	mReqTree = new QTreeWidget;
	mReqTree->setHeaderLabel(tr("Requests"));
	connect(mReqTree, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, 
		SLOT(changeReqInfo(QTreeWidgetItem *, QTreeWidgetItem *)));
	mReqInfo = new QTextEdit;
	
	QAction *sep2 = new QAction("", this);
	sep2->setSeparator(true);
	
	mReqTree->addAction(mCertGetReqAct);
	mReqTree->addAction(mCertRecvReqAct);
	mReqTree->addAction(mCertSignReqAct);
	mReqTree->addAction(sep2);
	mReqTree->addAction(mCertDeleteReqAct);
	mReqTree->setContextMenuPolicy(Qt::ActionsContextMenu);
	
	certPanel->addWidget(mCertTree);
	certPanel->addWidget(mCertInfo);
	certPanel->setStretchFactor(0, 2);
	certPanel->setStretchFactor(1, 1);
	
	reqPanel->addWidget(mReqTree);
	reqPanel->addWidget(mReqInfo);
	reqPanel->setStretchFactor(0, 2);
	reqPanel->setStretchFactor(1, 1);
	
	mLeftPanelTab->addTab(certPanel, tr("Certificates"));
	mLeftPanelTab->addTab(reqPanel, tr("Requests"));
	
	centralPanel->addWidget(mLeftPanelTab);
	centralPanel->addWidget(mRightPanelStack);
	centralPanel->setStretchFactor(0, 1);
	centralPanel->setStretchFactor(1, 3);
	
	mCentral = centralPanel;
	setCentralWidget(mCentral);
}

void
MainWindow::createAboutBox()
{
	mAboutBox = new AboutBox(this);
	
	// About informations.
#ifndef STOKEN_SOFT
	mAboutBox->setAboutPixmap(QPixmap(QString(":img/stoken_about_ca_384.png")));
	mAboutBox->setAboutInfo(tr("<h2>sToken CA v1.2</h2>") +
#else
	mAboutBox->setAboutPixmap(QPixmap(QString(":img/stoken_about_softca_384.png")));
	mAboutBox->setAboutInfo(tr("<h2>sToken SoftCA v1.2</h2>") +
#endif
	tr("<br>This program is free software: you can redistribute it and/or modify "
	"it under the terms of the GNU General Public License as published by the Free "
	"Software Foundation, either version 3 of the License, or (at your option) any "
	"later version.<br><br>") +
#ifndef STOKEN_SOFT
	tr("<a href=\"http://www.stoken.eu/index.en.html\">http://www.stoken.eu</a>")
#else
	tr("<a href=\"http://www.stoken.eu/soft/index.en.html\">http://www.stoken.eu/soft</a>")
#endif
	);
	
	// Help informations.
#ifndef STOKEN_SOFT
	mAboutBox->setHelpPixmap(QPixmap(QString(":img/stoken_about_ca_384.png")));
	mAboutBox->setHelpInfo(tr("<h2>sToken CA v1.2</h2>") +
#else
	mAboutBox->setHelpPixmap(QPixmap(QString(":img/stoken_about_softca_384.png")));
	mAboutBox->setHelpInfo(tr("<h2>sToken SoftCA v1.2</h2>") +
#endif
	tr("<br>Online instructions to this application are avaliable on the following "
	"website: <a href=\"http://www.stoken.eu/products/man_ca/index.en.html\">"
	"http://www.stoken.eu</a>"));
	
	// License.
	QFile file(QString(":LICENSE"));
	file.open(QIODevice::ReadOnly);
	QByteArray license = file.readAll();
	file.close();
	mAboutBox->setLicense(QString(license.data()));
	
	// Thanks.
#ifndef STOKEN_SOFT
	mAboutBox->setThanksPixmap(QPixmap(QString(":img/stoken_about_ca_384.png")));
#else
	mAboutBox->setThanksPixmap(QPixmap(QString(":img/stoken_about_softca_384.png")));
#endif
	
	mAboutBox->setThanksInfo(tr("<h4>Thanks to</h4>"
		"Andrzej Chmielowiec, Wiktor Kazana"
		"<h4>Special thanks</h4>"
		"Special thanks to all people who participate in development process of "
		"<a href=\"www.openssl.org\">OpenSSL</a> library and "
		"<a href=\"www.openvpn.net\">OpenVPN</a> software. Both of this projects "
		"are crucial for this application."
		));
}
