/*
 *  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 "STokenFilesFolderItem.h"
#include "STokenFileItem.h"
#include "FileFolderWidget.h"

#include "crd_ut10xx.h"

STokenFilesFolderItem::STokenFilesFolderItem(CRD_UT10XX_t *ut, TreeItem *parent): TreeItem(parent)
{
	mUt = ut;
	mMainWidget = new FileFolderWidget;
	
	connect(mMainWidget, SIGNAL(addFileSig(QString&, QString&)), this, 
		SLOT(addFileSlot(QString&, QString&)));
	connect(mMainWidget, SIGNAL(deleteFileSig(QString&)), this, 
		SLOT(deleteFileSlot(QString&)));
	connect(mMainWidget, SIGNAL(saveFileSig(QString&, QString&)), this, 
		SLOT(saveFileSlot(QString&, QString&)));
	
	setPage(mMainWidget);
	
	QVariant filesFolderName = tr("Files");
	QVariant filesFolderIcon = QIcon(QString(":img/files_folder_blue.png"));
	
	setData(filesFolderName, Qt::DisplayRole);
	setData(filesFolderIcon, Qt::DecorationRole);
}

STokenFilesFolderItem::~STokenFilesFolderItem()
{
}

void
STokenFilesFolderItem::refresh()
{
	int i = 0;
	
	// Remove files which are not present.
	while (i < childCount())
	{
		STokenFileItem *item = static_cast<STokenFileItem*>(child(i));
		int fileId = item->id();
		
		if (mUt->file_types[fileId] == CRD_UT10XX_FT_EMPTY)
		{
			mMainWidget->removeFileWidgetItem(item->treeWidgetItem());
			model()->deleteChild(this, i);
		}
		else
		{
			i++;
		}
	}
	
	// Add files which are present.
	for (i = 0; i < mUt->files_no; i++)
	{
		if (mUt->file_types[i] != CRD_UT10XX_FT_EMPTY)
		{
			int j = 0;
			
			while (j < childCount())
			{
				int fileId = static_cast<STokenFileItem*>(child(j))->id();
				
				if (i == fileId)
				{
					j = -1;
					break;
				}
				
				j++;
			}
			
			if (j < 0)
				continue;
			
			j = 0;
			
			while (j < childCount())
			{
				int fileId = static_cast<STokenFileItem*>(child(j))->id();
				
				if (trUtf8(mUt->file_names[i]) < trUtf8(mUt->file_names[fileId]))
					break;
				
				j++;
			}
			
			STokenFileItem *item = new STokenFileItem(i, mUt, this);
			mMainWidget->insertFileWidgetItem(item->treeWidgetItem());
			model()->insertChild(this, j, item);
		}
	}
	
	// Refresh all present files.
	for (i = 0; i < childCount(); i++)
		child(i)->refresh();
	
	mMainWidget->sortFilesByName();
	mMainWidget->setInfo(mUt);
}

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

void STokenFilesFolderItem::userLoginSlot()
{
	mMainWidget->enable();
}

void STokenFilesFolderItem::logoutSlot()
{
	mMainWidget->disable();
}

void
STokenFilesFolderItem::addFileSlot(QString &srcFileName, QString &dstFileName)
{
	emit addFileSig(srcFileName, dstFileName);
}

void
STokenFilesFolderItem::deleteFileSlot(QString &fileName)
{
	emit deleteFileSig(fileName);
}

void
STokenFilesFolderItem::saveFileSlot(QString &srcFileName, QString &dstFileName)
{
	emit saveFileSig(srcFileName, dstFileName);
}
