/*
 *  Copyright (C) 2001-2004, 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 <kjs/interpreter.h>
#include <kjsembed/kjsembedpart.h>

#include "builtins/stddialog_imp.h"

#ifndef QT_ONLY
#include "builtins/stdaction_imp.h"
#include "builtins/resources.h"
#include "bindings/kconfig_imp.h"
#endif // QT_ONLY

#include "jsfactory_imp.h"
#include "jsproxy.h"

#include "jsbuiltin_imp.h"
#include "jsbuiltin.h"

namespace KJSEmbed {

typedef Bindings::JSBuiltInImp JSBuiltInImp;
typedef Bindings::JSFactoryImp JSFactoryImp;
typedef JSProxy::MethodTable MethodTable;

#ifndef QT_ONLY
typedef Bindings::Config Config;
#endif // QT_ONLY

JSBuiltIn::JSBuiltIn( KJSEmbedPart *part )
    : jspart(part)
{
    init( jspart->globalExec() );
}

JSBuiltIn::~JSBuiltIn()
{
}

void JSBuiltIn::init( KJS::ExecState *exec )
{
    JSFactory *jsfact = jspart->factory();

    //
    // Setup the Factory object
    //
    factory = exec->interpreter()->builtinObject().construct( exec, KJS::List() );

    MethodTable factoryMethods[] = {
	{ JSFactoryImp::MethodCreateROPart, "createROPart" },
	{ JSFactoryImp::MethodLoadUI, "loadui" },
	{ JSFactoryImp::MethodWidgets, "widgets" },
	{ JSFactoryImp::MethodConstructors, "constructors" },
	{ JSFactoryImp::MethodIsSupported, "isSupported" },
	{ JSFactoryImp::MethodTypes, "types" },
	{ JSFactoryImp::MethodListPlugins, "listBindingPlugins" },
	{ JSFactoryImp::MethodCreateRWPart, "createRWPart" },
	{ 0, 0 }
    };

    int i = 0;
    do {
	JSFactoryImp *obj = new JSFactoryImp( exec, jsfact,
					      factoryMethods[i].id, factoryMethods[i].name );
	factory.put( exec , obj->name(), KJS::Object(obj) );
	i++;
    } while( factoryMethods[i].id );

    //
    // Setup the System object
    //
    system = exec->interpreter()->builtinObject().construct( exec, KJS::List() );

    MethodTable systemMethods[] = {
	{ JSBuiltInImp::MethodOpenFile, "openFile" },
	{ JSBuiltInImp::MethodReadFile, "readFile" },
	{ JSBuiltInImp::MethodWriteFile, "writeFile" },
	{ JSBuiltInImp::MethodExit, "exit" },
	{ 0, 0 }
    };

    i = 0;
    do {
	JSBuiltInImp *obj = new JSBuiltInImp( this, systemMethods[i].id, systemMethods[i].name );
	system.put( exec , obj->name(), KJS::Object(obj) );
	i++;
    } while( systemMethods[i].id );

    // The stdio streams
    system.put( exec ,"stdin", jsfact->createProxy( exec, new QTextStream(stdin, IO_ReadOnly) ) );
    system.put( exec ,"stdout", jsfact->createProxy( exec, new QTextStream(stdout, IO_WriteOnly) ) );
    system.put( exec ,"stderr", jsfact->createProxy( exec, new QTextStream(stderr, IO_WriteOnly) ) );

#ifndef QT_ONLY
    //  The global config object
    Config *scriptCfg = new Config( jspart, "KJSConfig", "kjsembedrc");

    system.put( exec, "KJSConfig", jsfact->createProxy( exec, scriptCfg ) );
#endif // QT_ONLY

    //
    // Setup builtin KDE objects
    //
    stddialog = exec->interpreter()->builtinObject().construct( exec, KJS::List() );
    BuiltIns::StdDialogImp::addBindings( exec, stddialog );

#ifndef QT_ONLY
    stdaction = exec->interpreter()->builtinObject().construct( exec, KJS::List() );
    BuiltIns::StdActionImp::addBindings( jsfact, exec, stdaction );

    stddrs = exec->interpreter()->builtinObject().construct( exec, KJS::List() );
    BuiltIns::StdDirsImp::addBindings( exec, stddrs );

    stdicns = exec->interpreter()->builtinObject().construct( exec, KJS::List() );
    BuiltIns::StdIconsImp::addBindings( jsfact, exec, stdicns );
#endif // QT_ONLY
}

void JSBuiltIn::add( KJS::ExecState *exec, KJS::Object &parent )
{
    // Add the static objects
    parent.put( exec, "Factory", builtinFactory() );
    parent.put( exec, "System", builtinSystem() );
    parent.put( exec, "StdDialog", builtinStdDialog() );
#ifndef QT_ONLY
    parent.put( exec, "StdAction", builtinStdAction() );
    parent.put( exec, "StdDirs", builtinStdDirs() );
    parent.put( exec, "StdIcons", builtinStdIcons() );
#endif // QT_ONLY

    parent.put( exec, "Global", exec->interpreter()->globalObject() );

    // Add the global functions
    addGlobals( exec, parent );
}

void JSBuiltIn::addGlobals( KJS::ExecState *exec, KJS::Object &parent )
{
    MethodTable methods[] = {
	{ JSBuiltInImp::MethodLoadScript,"load" },
	{ JSBuiltInImp::MethodDumpObject,"dump" },
	{ JSBuiltInImp::MethodRunKSpy,"run_kspy" },
	{ JSBuiltInImp::MethodPrint,"print" },
	{ JSBuiltInImp::MethodPrintLn, "println" },
	{ JSBuiltInImp::MethodWarn,"warn" },
	{ JSBuiltInImp::MethodReadLine,"readLine" },
	{ JSBuiltInImp::MethodExit,"exit" },
	{ JSBuiltInImp::MethodDumpCompletion,"dumpCompletion" },
	{ JSBuiltInImp::MethodSaxLoadFile,"saxLoadFile" },
	{ JSBuiltInImp::MethodAlert,"alert" },
	{ JSBuiltInImp::MethodConfirm,"confirm" },
	{ JSBuiltInImp::MethodPrompt,"prompt" },
	{ JSBuiltInImp::MethodI18n,"i18n" },
	{ JSBuiltInImp::MethodImport,"include" },
	{ JSBuiltInImp::MethodShell, "shell" },
	//{ JSBuiltInImp::MethodImport,"import" }, reserved keyword
	{ 0, 0 }
    };

    int i = 0;
    do {
	JSBuiltInImp *obj = new JSBuiltInImp( this, methods[i].id, methods[i].name );
	parent.put( exec , obj->name(), KJS::Object(obj) );
	i++;
    } while( methods[i].id );
}

} // namespace KJSEmbed

