/***************************************************************************
 *   Copyright (C) 2005 by xavier helluy                                   *
 *   xavier.helluy@web.de                                                  *
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/

#include <kdebug.h>

#include <qstring.h>
#include <klineedit.h>
#include <kurlrequester.h>
#include <klistbox.h>
#include <kcombobox.h>
#include <qmap.h>
#include <kurl.h>
#include <kconfig.h>
#include <kconfigbase.h>
#include <qcheckbox.h>
#include <kactionselector.h>

#include "ioslaveconfigwidget.h"


#include "helpertestsstring.h"


IOSlaveConfigWidget::IOSlaveConfigWidget(KDialogBase * dialog,
		Settings * settings , 
		QMap<QString , QString > * indexMap ,
		QWidget* parent, const char* name, WFlags fl)
	: IOSlaveConfigwidgetBase(parent,name,fl)
{
	m_dialog = dialog;
	m_settings = settings;
	m_indexMap = indexMap;

	updateEntriesInWidget();
}

IOSlaveConfigWidget::~IOSlaveConfigWidget()
{
}

void IOSlaveConfigWidget::saveSettingsIOSlave()
{
	KConfig * cfg = m_settings->config();
	cfg->setGroup("IOSlaveConfig");
	QStringList available;
	int counter =  this->indexNames->availableListBox()->count();
	for (int k=0;k<counter;++k)
	{
		available.append( this->indexNames->availableListBox()->text(k));
	}
	cfg->writeEntry("available" , available);
	
	QStringList allIndexes = m_indexMap->keys();
	QStringList selected;
	for (QStringList::Iterator it =allIndexes.begin() ;
		    it != allIndexes.end(); ++it)
	{
		QString index = *it;
		QString name = index.mid(14,index.length()); //FIXME: make nicer
		if (available.find(name)==available.end())
		{
			cfg->setGroup(*it);
			QString indexPath =  HelperTestsString::name2path( cfg->readEntry("indexLocation"));
			selected.append( indexPath);
		}
	}
	cfg->setGroup("IOSlaveConfig");
	cfg->writeEntry("selected" , selected);
	cfg->sync();
}

void IOSlaveConfigWidget::updateEntriesInWidget()
{
	
	//check if group exist or not
	KConfig * cfg = m_settings->config();
	cfg->setGroup("IOSlaveConfig");
	QStringList available;
	this->indexNames->availableListBox()->clear();	

	if (!(cfg->groupList().find("IOSlaveConfig")==cfg->groupList().end()) )
	{
		available = cfg->readListEntry("available" );
		if (!(available.count()==0))
			this->indexNames->availableListBox()->insertStringList(cfg->readListEntry("available" ));
	}
	
	QStringList keys = m_indexMap->keys();
		
	this->indexNames->selectedListBox()->clear();	
	for ( QStringList::Iterator it = keys.begin(); it != keys.end(); ++it ) 
	{
		QString group = *it; 
		QString name = group.mid(14,group.length()); //FIXME: make nicer
    
		//new index configs are always added to the available list of indexes, even if not created ! The search daemon
		//must check the index exists. If not it should ignore the index entry.
		if ( available.find(name)==available.end() )
			this->indexNames->selectedListBox()->insertItem(name);
	}
}

void IOSlaveConfigWidget::slotNewIndex(const QString & newName)
{
	kdDebug() << "IOSlaveConfigWidget::slotNewIndex() CALLED :"<< newName <<endl;
	kdDebug() << m_indexMap->keys()<<endl;
	
	//add new selected path
	KConfig * cfg = m_settings->config();
	cfg->setGroup("IOSlaveConfig");
	QStringList selected;
	selected = cfg->readListEntry("selected" );
	QString name = newName.mid(14,newName.length());
	selected.append(name);
	saveSettingsIOSlave();
	updateEntriesInWidget();
}

void IOSlaveConfigWidget::slotRemoveIndex(const QString & removedIndex)
{
	//remove from configuration and update widgets
	kdDebug() << "IOSlaveConfigWidget::slotremoveIndex() CALLED :"<< removedIndex <<endl;
	kdDebug() << m_indexMap->keys()<<endl;
	
	QString name = removedIndex.mid(14,removedIndex.length());
	KConfig * cfg = m_settings->config();
	cfg->setGroup("IOSlaveConfig");
	QStringList available;
	QStringList selected;
	if (!(cfg->groupList().find("IOSlaveConfig")==cfg->groupList().end()) )
	{
		kdDebug() << "IOSlaveConfigWidget::slotremoveIndex() IOSlaveConfig exists:" <<endl;

		available = cfg->readListEntry("available" );
		QStringList::Iterator it =available.find(name);
		if (!(it==available.end()))
		{
			kdDebug() << "IOSlaveConfigWidget::slotremoveIndex() REMOVE AVAILABLE exists:" << *it << endl;
			available.remove(it);
			
			cfg->writeEntry("available" , available);
		}
		selected = cfg->readListEntry("selected" );
		QStringList::Iterator it2 =selected.find(name);
		if (!(it2==selected.end()))
		{
			kdDebug() << "IOSlaveConfigWidget::slotremoveIndex() REMOVE SELECTED exists:" << *it2 << endl;
			selected.remove(it2);
			cfg->writeEntry("selected" , selected);
		}

	}	
	updateEntriesInWidget();
	saveSettingsIOSlave();
}

#include "ioslaveconfigwidget.moc"

