/***************************************************************************
 *   Copyright (C) 2008 by Andreas Silberstorff   *
 *   taki@Rivendell   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
//
// Derived from kalvaprofile from kalva (0.9)
//
// Description: 
//
//
// Author: Andreas Silberstorff <ml@andreas-silberstorff.de>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "propertyloader.h"

#include <qstring.h>
#include <qstringlist.h>
#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qmap.h>
#include <qregexp.h>

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>

namespace KalvaUtil {

QStringList PropertyLoader::readDir() const
{
	QString dir = getDir();

	QDir d = QDir( dir );
	if ( !d.exists() )
	{
	       kWarning() << i18n( "Cannot find the %1 directory %2", m_type, dir );
	       return QStringList(); // empty list
	}
	kDebug() << i18n( "Read directory %1", dir );
	d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
	QStringList profiles = d.entryList();
	return profiles;
}

bool PropertyLoader::read( const QString& basename )
{
	m_filename = basename;
	m_filename = m_filename.remove(QRegExp("^.*/"));
	m_profileData.clear();
	QString filename = getFilename();
	kDebug() << "try to read profile from " << filename;

	QFile file( filename );
	if ( file.open( QIODevice::ReadOnly ) == false )
	{
		kDebug() << i18n( "could not open profile %1 for reading", file.fileName() );
		return false;
	}
	QTextStream prfFromFile( &file );
	QStringList prf = prfFromFile.readAll().split( "\n", QString::SkipEmptyParts );
	file.close();

	QString str;
	QString key;
	QString cat = "";
	QString val;

	for ( QStringList::Iterator it = prf.begin(); it != prf.end(); ++it ) {
		str = *it;
		kDebug() << i18n("read... %1", str );
		if ( str.startsWith( ("[") ) ) {
			cat = str;
		}  else  {
			key = str.section("=",0,0);
			val = str.section("=",1);
			setValue( key, cat, val );
		}
	}
	return true;
}

bool PropertyLoader::write( const QString& name )
{
	kDebug() << i18n( "write... %1", name );

//?????	m_profileData::Iterator it;
//        for ( it = m_profileData.begin(); it != m_profileData.end(); ++it ) {
//            printf( "key = %s, value = %s\n",
//                    it.key().latin1(),
//                    it.data().latin1() );
//        }

	return true;
}

QString PropertyLoader::getDir() const {
	return KGlobal::dirs()->locateLocal( "data", "kalva2/" + m_type + "/", true);
}

QString PropertyLoader::getFilename() const {
	return getDir() + "/" + m_filename;
}

QString PropertyLoader::getFilename( QString basename ) {
	m_filename = basename.remove(QRegExp("^.*/"));
	return getFilename();
}

Value PropertyLoader::getValue( const QString& key ) const  {
	return m_profileData[ key ];
}

QString PropertyLoader::getCategory( const QString& key ) const  {
	return getValue( key ).getCategory();
}

QString PropertyLoader::getValueString( const QString& key ) const {
	return getValue( key ).getValue();
}

int PropertyLoader::getValueInt( const QString& key ) const {
	return getValueString( key ).toInt();
}

double PropertyLoader::getValueDouble( const QString& key ) const {
	return getValueString( key ).toDouble();
}

float PropertyLoader::getValueFloat( const QString& key ) const {
	return getValueString( key ).toFloat();
}

bool PropertyLoader::getValueBoolean( const QString& key ) const {
	QString val = getValueString( key );
	if ( val.contains( "true" ) > 0 )
		return true;
	return false;
}

void PropertyLoader::setValue( const QString& key, const QString& cat, const QString& val ) {
	kDebug() << i18n( "setValue( %1, %2, %3 )", key, cat, val );
	m_profileData[ key ] = Value( cat, val );
}

}