/***************************************************************************
*   Copyright (C) 2006 by Ian Reinhart Geiser   *
*   geiseri@sourcextreme.com   *
*                                                                         *
*   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 "remotemethod.h"
#include "xmlrpc.h"

#include <QHostAddress>
#include <QVariant>
#include <QEventLoop>
#include <QHttp>
#include <QBuffer>

#include "qdebug.h"

namespace KRPC
{
}

KRPC::RemoteMethod::RemoteMethod( QObject* parent ) : QObject( parent )
{
	m_localLoop = new QEventLoop(this);
	m_responseBuffer = new QBuffer(this);
}


KRPC::RemoteMethod::~RemoteMethod()
{

}

void KRPC::RemoteMethod::slotStateChanged( int state )
{
	qDebug("State %d", state);
}

void KRPC::RemoteMethod::slotRequestFinished( int id, bool error )
{
	XMLRPC::ResponseMessage message( m_responseBuffer->data() );
	if( error )
		qDebug() << "Error" << m_http->errorString();
// 	qDebug() << "Response" << m_responseBuffer->data();
	QList<QVariant> result = message.values();
	m_currentResponse = result.value(0);
	m_localLoop->quit();
}

void KRPC::RemoteMethod::invokeMethod( const QHostAddress & host, uint port, const QByteArray & interface, const QByteArray & method, const QList< QVariant > & args )
{
	QByteArray message = XMLRPC::RequestMessage( method, args).xml();

	m_http = new QHttp(host.toString(), port, this);
	connect( m_http, SIGNAL(requestFinished( int, bool )), this, SLOT(slotRequestFinished(int,bool)));

	QHttpRequestHeader request("POST", interface, 1, 1  );
	request.setContentType("text/xml");
	request.setContentLength(message.size());

// 	qDebug() << "Send" << request.toString() << message;

	m_http->request(request, message, m_responseBuffer );
}

QVariant KRPC::RemoteMethod::invoke( const QHostAddress & host, uint port, const QByteArray & interface, const QByteArray & method )
{
	RemoteMethod remoteMethod;
	QList<QVariant> args;
	remoteMethod.invokeMethod(host,port,interface, method,args);
	remoteMethod.m_localLoop->exec();

	return remoteMethod.m_currentResponse;
}

QVariant KRPC::RemoteMethod::invoke( const QHostAddress & host, uint port, const QByteArray & interface, const QByteArray & method, const QVariant & arg1 )
{
	RemoteMethod remoteMethod;
	QList<QVariant> args;
	args << arg1;
	remoteMethod.invokeMethod(host,port,interface, method,args);
	remoteMethod.m_localLoop->exec();

	return remoteMethod.m_currentResponse;
}

QVariant KRPC::RemoteMethod::invoke( const QHostAddress & host, uint port, const QByteArray & interface, const QByteArray & method, const QVariant & arg1, const QVariant & arg2 )
{
	RemoteMethod remoteMethod;
	QList<QVariant> args;
	args << arg1 << arg2;
	remoteMethod.invokeMethod(host,port,interface, method,args);
	remoteMethod.m_localLoop->exec();

	return remoteMethod.m_currentResponse;
}


