/*
 *  Copyright (C) 2001-2003, Richard J. Moore <rich@kde.org>
 *
 *  This library 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 library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include <stdio.h>

#include <qobject.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qwidget.h>

#include <kdebug.h>

#include "factory.h"
#include "jsobjectproxy.h"
#include "image_imp.h"
#include "textstream_imp.h"

#include "kjsembedpart.h"
#include "kjsembedpart_imp.h"

//
// KJSEmbedPart Bindings
//
namespace KJSEmbed {
namespace Bindings {

KJSEmbedPartImp::KJSEmbedPartImp( KJSEmbedPart *jsp, int mid, const QString &p )
    : ObjectImp(), part(jsp), id(mid), param(p)
{
}

KJSEmbedPartImp::~KJSEmbedPartImp() {}

KJS::Value KJSEmbedPartImp::call( KJS::ExecState *state, KJS::Object &/*self*/, const KJS::List &args )
{
    QString arg0 = (args.size() > 0) ? args[0].toString(state).qstring() : QString::null;
    QString arg1 = (args.size() > 1) ? args[1].toString(state).qstring() : QString::null;

    if ( id == MethodCreate ) {

	QObject *obj = JSFactory::create( arg0.latin1(), 0, arg1.latin1() );
	if ( obj )
	    return JSFactory::bind( state->interpreter(), obj );

    }
    else if ( id == MethodLoadUI ) {

	QWidget *obj = JSFactory::loadUI( arg0, 0, 0, arg1.latin1() );
	if ( obj )
	    return JSFactory::bind( state->interpreter(), obj );

    }
    else if ( id == MethodLoadScript ) {

	bool ok = part->executeScript( arg0 );
	return KJS::Boolean( ok );

    }
    else if ( id == MethodPrint ) {

	QTextStream out( stdout, IO_WriteOnly );
	out << arg0;

	return KJS::Boolean( true );
    }
    else if ( id == MethodPrintLn ) {

	QTextStream out( stdout, IO_WriteOnly );
	out << arg0 << endl;;

	return KJS::Boolean( true );
    }
    else if ( id == MethodWarn ) {

	QTextStream err( stderr, IO_WriteOnly );
	err << arg0 << endl;

	return KJS::Boolean( true );
    }
    else if ( id == MethodReadLine ) {

	QTextStream in( stdin, IO_ReadOnly );
	QString line = in.readLine();
	if ( !line.isNull() )
	    return KJS::String( line );

    }
    else if ( id == MethodReadFile ) {

	QFile f( arg0 );
	if ( f.open( IO_ReadOnly ) )
	    return KJS::String( QString(f.readAll()) );

    }
    else if ( id == MethodDumpObject ) {
	KJS::Object ob = args[0].toObject(state);
	return KJS::String( dumpObject( state, ob ) );
    }
    else {
	kdWarning() << "KJSEmbedPartImp has no method " << id << endl;
    }

    return KJS::Null();
}

KJS::Object KJSEmbedPartImp::construct( KJS::ExecState *state, const KJS::List &args )
{
    KJS::Interpreter *js = state->interpreter();

    if ( id == ConstructQObject ) {

	// Get parent
	QObject *parent=0;
	if ( args.size() ) {
	    KJS::Value v0 = args[0];
	    KJS::ValueImp *imp = v0.imp();
	    JSObjectProxy *proxy = dynamic_cast<JSObjectProxy *>( imp );
	    if ( proxy )
		parent = proxy->object();
	}

	// Get name
	QCString name;
	QString arg1 = (args.size() > 1) ? args[1].toString(state).qstring() : QString::null;
	if ( !arg1.isNull() )
	    name = arg1.latin1();

	QObject *obj = JSFactory::create( param, parent, name.data() );
	if ( obj ) {
	    return JSFactory::bind( js, obj );
	}
	else {
	    kdWarning() << "KJSEmbedPart::ConstructorImp: Failed to create '" << param << "' instance" << endl;
	}
    }
    else if ( id == ConstructImage )
	return JSFactory::bind( js, new Bindings::Image( part ) );
    else if ( id == ConstructTextStream ) {
	kdWarning() << "Cannot create TextStream - Not Implemented!" << endl;
    }
    else {
	kdDebug() << "KJSEmbedPart::ConstructorImp has no such impl" << endl;
    }

    return KJS::Object( new KJS::ObjectImp );
}

} // namespace KJSEmbed::Bindings
}; // namespace KJSEmbed

// Local Variables:
// c-basic-offset: 4
// End:
