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

#include "STokenKeysFolderItem.h"
#include "KeyFolderWidget.h"
#include "STokenKeyItem.h"

#include "crd_ut10xx.h"

STokenKeysFolderItem::STokenKeysFolderItem(CRD_UT10XX_t *ut, TreeItem *parent): TreeItem(parent)
{
	mUt = ut;
	mMainWidget = new KeyFolderWidget;
	
	connect(mMainWidget, SIGNAL(deleteKeySig(int)), this, SLOT(deleteKeySlot(int)));
	connect(mMainWidget, SIGNAL(generateKeySig(const char*, int)), this, 
		SLOT(generateKeySlot(const char*, int)));
	
	setPage(mMainWidget);
	
	QVariant keysFolderName = tr("Keys");
	QVariant keysFolderIcon = QIcon(QString(":img/keys_folder_blue.png"));
	
	setData(keysFolderName, Qt::DisplayRole);
	setData(keysFolderIcon, Qt::DecorationRole);
}

STokenKeysFolderItem::~STokenKeysFolderItem()
{
}

void
STokenKeysFolderItem::refresh()
{
	mMainWidget->setInfo(mUt->keys);
	
	int i = 0;
	
	// Remove keys which are not generated and present.
	while (i < childCount())
	{
		int keyId = static_cast<STokenKeyItem*>(child(i))->id();
		
		if ((mUt->keys + keyId)->info == 0)
			model()->deleteChild(this, i);
		
		else
			i++;
	}
	
	// Add keys which are generated and not present.
	for (i = 0; i < 4; i++)
	{
		if ((mUt->keys + i)->info == 0x01)
		{
			int j = 0;
			
			while (j < childCount())
			{
				int keyId = static_cast<STokenKeyItem*>(child(j))->id();
				
				if (i == keyId)
				{
					j = -1;
					break;
				}
				else if (i < keyId)
				{
					break;
				}
				
				j++;
			}
			
			if (j < 0)
				continue;
			
			STokenKeyItem *item = new STokenKeyItem(i, mUt, this);
			model()->insertChild(this, j, item);
			
			connect(this, SIGNAL(userLoginSig()), item, SLOT(userLoginSlot()));
			connect(this, SIGNAL(logoutSig()), item, SLOT(logoutSlot()));
			connect(item, SIGNAL(generateCertReqSig(int, QString&, QStringList&, QString&)), 
				this, SLOT(generateCertReqSlot(int, QString&, QStringList&, QString&)));
		}
	}
	
	// Refresh all present keys.
	for (i = 0; i < childCount(); i++)
		child(i)->refresh();
}

void
STokenKeysFolderItem::soLoginSlot()
{
	mMainWidget->enable();
}

void
STokenKeysFolderItem::userLoginSlot()
{
	mMainWidget->enable();
	emit userLoginSig();
}

void
STokenKeysFolderItem::logoutSlot()
{
	mMainWidget->disable();
	emit logoutSig();
}

void
STokenKeysFolderItem::generateKeySlot(const char *name, int id)
{
	emit generateKeySig(name, id);
}

void
STokenKeysFolderItem::deleteKeySlot(int id)
{
	emit deleteKeySig(id);
}

void
STokenKeysFolderItem::generateCertReqSlot(int keyId, QString &fileName, QStringList &dn, QString &mail)
{
	emit generateCertReqSig(keyId, fileName, dn, mail);
}


