/***************************************************************************
*   Copyright (C) 2006 by Ian Reinhart Geiser   *
*   geiseri@kde.org   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU Library 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 Library 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.             *
***************************************************************************/
#include "command.h"
#include "messageparser.h"
#include "messagebuilder.h"
#include "xmlrpc.h"
#include "system.h"
#include "directory.h"

#include <QIODevice>
#include <QDataStream>

#include <qdebug.h>

KRPC::Command::Command( QIODevice *iodevice )
: QObject( 0 ), m_device(iodevice)
{
	connect( m_device, SIGNAL(readyRead()),this, SLOT(exec()));
}


KRPC::Command::~Command()
{

}

void KRPC::Command::exec( )
{
	MessageParser parser(m_device);
	MessageBuilder builder(m_device);

	if( parser.readMessage() == -1 )
	{
		qDebug() << "Error reading messagae.";
		emit finished();
		return;
	}

	if( parser.requestHeader().method() == "POST")
	{
		XMLRPC::RequestMessage request( parser.content() );
		if( request.isValid() )
		{
// 			qDebug() << "Handle" << parser.content();
			QString interface = parser.requestHeader().path().toLatin1();
			// Dispatch to {interface}/{method}{}
			if( interface == "/krpc/system")
			{
				System system;
				QVariant result = system.dispatch(request.method(), request.args());
				if( result.isValid())
					builder.writeResponseMessage( XMLRPC::ResponseMessage( result ) );
				else
				{
					XMLRPC::FaultMessage message(104, QString("Invalid method '%1'.").arg(request.method().data()));
					builder.writeResponseMessage( message );
				}
			}
			else if( interface == "/krpc/directory")
			{
				Directory directory;
				QVariant result = directory.dispatch(request.method(), request.args());
				if( result.isValid())
					builder.writeResponseMessage( XMLRPC::ResponseMessage( result ) );
				else
				{
					XMLRPC::FaultMessage message(105, QString("Invalid method '%1'.").arg(request.method().data()));
					builder.writeResponseMessage( message );
				}
			}
			else
			{
				XMLRPC::FaultMessage message(103, QString("Invalid interface '%1'.").arg(interface));
				builder.writeResponseMessage( message );
			}
		}
		else
		{
			qDebug() << "not a valid XMLRPC response.";
			XMLRPC::FaultMessage message(100, "Not a valid XMLRPC Message.");
			builder.writeResponseMessage( message );
		}
	}
	else
	{
		qDebug() << "invalid request";
		XMLRPC::FaultMessage message(101, "Invalid request.");
		builder.writeResponseMessage( message );
	}

	emit finished();
}



