/***************************************************************************
 *   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 "converters.h"
#include <qfileinfo.h>
#include <kdebug.h>

Converters::Converters()
{
}

Converters::~Converters()
{
}

//TODO: chains of converters. like koconverter can do. tex relyx lyx txt
//TODO: NULL proc = do nothing
//TODO: mimetype groups: text/   : do nothing
//ad checkbox : builtin html text readers

//if $$o then make tmp file_out
//if !$$o, cp input file $$i and get $$i.pattern


int Converters::mimePos(QString mimeType)
{
	kdDebug() <<"Converters:mimePos" << endl;

	return this->m_mimeList.findIndex(mimeType); 
}

int Converters::patternPos(QString extension)
{
	kdDebug() <<"Converters::patternPos" << endl;

	return this->m_patternList.findIndex(extension); 
}

bool Converters::acceptOutputTmpFile(bool isMime , int id)
{
	if (isMime)
	{
		//FIXME:check 
		QStringList::Iterator it = this->m_mimeArgs.at(id);
		QString str = *it;
		int res = str.find("$$o");
		if (res>-1)
			return TRUE;
		else
			return FALSE;
	}
	else
	{
		//FIXME:check 
		QStringList::Iterator it = this->m_patternArgs.at(id);
		QString str = *it;
		int res = str.find("$$o");
		if (res>-1)
			return TRUE;
		else
			return FALSE;
	}
		
}


QString Converters::getProc(bool isMime , int id)
{
	if (isMime)
	{
		return *(this->m_mimeProc.at(id));
	}
	else
	{
		return *(this->m_patternProc.at(id));
	}	
}

QString Converters::getArgs(bool isMime , int id , QString in , QString out)
{
	if (isMime)
	{
		QString str = *(this->m_mimeArgs.at(id));
		str.replace("$$i" , "'" + in + "'");
		str.replace("$$o" , "'" + out + "'");
		return str;
	}
	else
	{
		QString str = *(this->m_patternArgs.at(id));
		str.replace("$$i" , "'" + in  + "'");
		str.replace("$$o" , "'" + out + "'");
		return str;
	}	
}

QString Converters::getArgs(bool isMime , int id, QString in)
{
	if (isMime)
	{
		QString str = *(this->m_mimeArgs.at(id));
		str.replace("$$i" , "'" + in + "'");
		return str;
	}
	else
	{
		QString str = *(this->m_patternArgs.at(id));
		str.replace("$$i" , "'" + in + "'");
		return str;
	}	
}

QString Converters::getOutput(bool isMime , int id , QString in)
{
	QFileInfo fi( in );
	//TODO should it be true always, or should I parse it
	QString in_extension	= fi.extension(TRUE);  //returns tar.gz FALSE would return gz
	
	if (isMime)
	{
		QString out_extension	= *(this->m_mimeOutput.at(id));
		int keepChar = in.length()-in_extension.length();
		QString str = in.left( keepChar);
		QString str2 = str.append(out_extension);
		return str2;
	}
	else
	{
		QString out_extension	= *(this->m_patternOutput.at(id));
		int keepChar = in.length()-in_extension.length();
		QString str = in.left( keepChar);
		QString str2 = str.append(out_extension);
		return str2;
	}
}
