Kate::PluginConfigPage* PluginKateKJSWrapper::configPage (uint id, QWidget *w, const char */*name*/) { kdDebug()<<"PluginKateKJSWrapper::configPage"<<endl; if (id>=configPages()) return 0; KJS::Interpreter *js = m_part->interpreter(); KJS::Object constr=getObj(js,m_configPageFactories,id); if (js->globalExec()->hadException()) { kdDebug()<<"PluginKateKJSWrapper::configPage: exit 1"<<endl; js->globalExec()->clearException(); return 0; } if (!constr.implementsConstruct()) { kdWarning()<<"config page factory has to be an object constructor"<<endl; return 0; } KateKJSWrapperConfigPage *p=new KateKJSWrapperConfigPage(constr,this,w); return (Kate::PluginConfigPage*)p; /* KateKJSWrapperConfigPage* p = new KateKJSWrapperConfigPage(this, w); //init connect( p, SIGNAL(configPageApplyRequest(KateKJSWrapperConfigPage*)), this, SLOT(applyConfig(KateKJSWrapperConfigPage*)) ); return (Kate::PluginConfigPage*);*/ }
int main( int argc, char **argv ) { KAboutData about( "test-kjsembed", I18N_NOOP("KJS Embed Test"), "0.1", I18N_NOOP("Test"), KAboutData::License_LGPL, I18N_NOOP("(c) 2001-2003 Richard Moore") ); KCmdLineArgs::init( argc, argv, &about ); KApplication app; // Setup Interpreter KJS::Interpreter *js = KJSEmbed::JSFactory::defaultJS(); KJS::Object global = js->globalObject(); // Create Console KJSEmbed::JSConsoleWidget *console = new KJSEmbed::JSConsoleWidget( js ); console->addBindings( global ); JSFactory::publish( &app, js, global, "app" ); JSFactory::publish( console, js, global, "console" ); // Setup Window app.setMainWidget( console ); app.connect( &app, SIGNAL( lastWindowClosed() ), SLOT(quit()) ); console->resize( 600, 450 ); console->show(); return app.exec(); }
// Variant value must be released with NPReleaseVariantValue() void convertValueToNPVariant (KJS::ExecState *exec, const KJS::Value &value, NPVariant *result) { Type type = value.type(); if (type == StringType) { UString ustring = value.toString(exec); CString cstring = ustring.UTF8String(); NPString string = { (const NPUTF8 *)cstring.c_str(), cstring.size() }; NPN_InitializeVariantWithStringCopy (result, &string ); } else if (type == NumberType) { NPN_InitializeVariantWithDouble (result, value.toNumber(exec)); } else if (type == BooleanType) { NPN_InitializeVariantWithBool (result, value.toBoolean(exec)); } else if (type == UnspecifiedType) { NPN_InitializeVariantAsUndefined(result); } else if (type == NullType) { NPN_InitializeVariantAsNull(result); } else if (type == ObjectType) { KJS::ObjectImp *objectImp = static_cast<KJS::ObjectImp*>(value.imp()); if (objectImp->classInfo() == &KJS::RuntimeObjectImp::info) { KJS::RuntimeObjectImp *imp = static_cast<KJS::RuntimeObjectImp *>(value.imp()); CInstance *instance = static_cast<CInstance*>(imp->getInternalInstance()); NPN_InitializeVariantWithObject (result, instance->getObject()); } else { KJS::Interpreter *originInterpreter = exec->interpreter(); const Bindings::RootObject *originExecutionContext = rootForInterpreter(originInterpreter); KJS::Interpreter *interpreter = 0; if (originInterpreter->isGlobalObject(value)) { interpreter = originInterpreter->interpreterForGlobalObject (value.imp()); } if (!interpreter) interpreter = originInterpreter; const Bindings::RootObject *executionContext = rootForInterpreter(interpreter); if (!executionContext) { Bindings::RootObject *newExecutionContext = new KJS::Bindings::RootObject(0); newExecutionContext->setInterpreter (interpreter); executionContext = newExecutionContext; } NPObject *obj = (NPObject *)exec->interpreter()->createLanguageInstanceForValue (exec, Instance::CLanguage, value.toObject(exec), originExecutionContext, executionContext); NPN_InitializeVariantWithObject (result, obj); _NPN_ReleaseObject (obj); } } else NPN_InitializeVariantAsUndefined(result); }
KateKJSWrapperConfigPage::KateKJSWrapperConfigPage(KJS::Object pageConstructor,PluginKateKJSWrapper* parent, QWidget *parentWidget) : Kate::PluginConfigPage( parentWidget ),m_plugin(parent) { QVBoxLayout *l=new QVBoxLayout(this); l->setAutoAdd(true); l->activate(); KJS::Interpreter *js = parent->m_part->interpreter(); KJS::ExecState *exec = js->globalExec(); exec->clearException(); KJS::List param; param.append(parent->m_part->factory()->createProxy(exec,this,0)); m_pageObject=pageConstructor.construct(exec,param); }
PluginKateKJSWrapperView *PluginKateKJSWrapper::getViewObject(Kate::MainWindow *win) { PluginKateKJSWrapperView * view=m_views[win]; if (!view) { view=new PluginKateKJSWrapperView(); view->win=win; connect(win,SIGNAL(destroyed()),this,SLOT(slotWindowDestroyed())); m_views.insert(win,view); KJS::Interpreter *js = m_part->interpreter(); KJS::ExecState *exec = js->globalExec(); view->actionCollectionObj=m_part->factory()->createProxy(exec,view->actionCollection()); view->winObj=m_part->factory()->createProxy(exec,win); } else kdDebug()<<"returning cached View/Window Object"<<endl; return view; }
QString PluginKateKJSWrapper::configPageFullName(uint id) const { if (id>=configPages()) return ""; KJS::Interpreter *js = m_part->interpreter(); KJS::Object constr=getObj(js,m_configPageFactories,id); KJS::Value o=constr.get(js->globalExec(),KJS::Identifier("fullName")); QString retVal( o.toString(js->globalExec()).qstring() ); kdDebug()<<"=============================================================================================="<<endl; kdDebug()<<"PluginKateKJSWrapper::configPageFullName: "<<retVal<<endl; kdDebug()<<"=============================================================================================="<<endl; js->globalExec()->clearException(); return retVal; }
uint PluginKateKJSWrapper::configPages () const { KJS::Interpreter *js = m_part->interpreter(); KJS::ExecState *exec=js->globalExec(); if (! (m_configPageFactories.isNull() || (m_configPageFactories.type()==KJS::NullType))) { KJS::Object constrs=m_configPageFactories.toObject(exec); if (!exec->hadException()) { if (QString(constrs.classInfo()->className)=="Array") { kdDebug()<<"config page constructor array detected"<<endl; uint size=constrs.get(exec,KJS::Identifier("length")).toInteger(exec); if (exec->hadException()) { exec->clearException(); kdDebug()<<"Error while retrieving array length"<<endl; } else return size; } else return 1; } } exec->clearException(); return 0; }
static void callJS(KJSEmbed::KJSEmbedPart *p,KJS::Object o,const QString& funcName){ KJS::Interpreter *js = p->interpreter(); KJS::ExecState *exec = js->globalExec(); KJS::List param; exec->clearException(); KJS::Value funcV=o.get(exec,KJS::Identifier(funcName)); if (exec->hadException()) { #warning clear exception ? return; } KJS::Object func=funcV.toObject(exec); if (exec->hadException()) { #warning clear exception ? return; } if (func.implementsCall()) { func.call(exec,o,param); if (js->globalExec()->hadException()) { #warning clear exception ? return; } } }
PluginKateKJSWrapper::PluginKateKJSWrapper( QObject* parent, const char* name, const QStringList& list) : Kate::Plugin ( (Kate::Application *)parent, name ) { m_views.setAutoDelete(true); m_scriptname=list[0]; m_kateAppBindings=new Kate::JS::Bindings(this); KJSEmbed::JSSecurityPolicy::setDefaultPolicy( KJSEmbed::JSSecurityPolicy::CapabilityAll ); m_part = new KJSEmbed::KJSEmbedPart(this); KJS::Interpreter *js = m_part->interpreter(); KJSEmbed::JSFactory *factory=m_part->factory(); /* factories for kate app classes */ factory->addQObjectPlugin("Kate::Application",m_kateAppBindings); factory->addQObjectPlugin("Kate::DocumentManager",m_kateAppBindings); factory->addQObjectPlugin("Kate::MainWindow",m_kateAppBindings); factory->addQObjectPlugin("Kate::PluginManager",m_kateAppBindings); factory->addQObjectPlugin("Kate::InitPluginManager",m_kateAppBindings); factory->addQObjectPlugin("Kate::ProjectManager",m_kateAppBindings); factory->addQObjectPlugin("Kate::Project",m_kateAppBindings); factory->addQObjectPlugin("Kate::ViewManager",m_kateAppBindings); factory->addQObjectPlugin("Kate::View",m_kateAppBindings); /* toplevel objects*/ KJS::Object appobj=m_part->addObject(Kate::application(),"KATE"); js->globalObject().put( js->globalExec(), "addConfigPage", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::AddConfigPage,this ))); js->globalObject().put( js->globalExec(), "setConfigPages", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::SetConfigPages,this ))); js->globalObject().put( js->globalExec(), "removeConfigPage", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::RemoveConfigPage,this ))); js->globalObject().put( js->globalExec(), "setWindowConfiguration", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::SetWindowConfiguration,this ))); js->globalObject().put( js->globalExec(), "KJSConsole", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::KJSConsole,this ))); /* KJSEmbed::JSConsoleWidget *w=m_part->view(); w->show(); //w->show();*/ kdDebug()<<"m_scriptname="<<m_scriptname<<endl; m_part->runFile(locate("appdata",QString("plugins/%1/%2.js").arg(m_scriptname).arg(m_scriptname))); //"/home/jowenn/development/kde/cvs/kdeaddons/kate/kjswrapper/samples/test1.js"); }
int main( int argc, char **argv ) { QTime time; time.start(); #ifdef _WIN32 # ifdef CONSOLEIO RedirectIOToConsole(); # endif #endif // Handle arguments QString appName = argv[0]; QStringList args; for (int i = 1; i < argc; i++ ) { args << argv[i]; } QString script; KJS::List scriptArgs; bool gui = true; #ifndef QT_ONLY /* #ifdef __GNUC__ #warning "KDE Support enabled" #endif */ bool kde = true; #else /* #ifdef __GNUC__ #warning "KDE Support disabled" #endif */ #endif if (argc > 1) { while (!args.isEmpty()) { QString arg = args.takeFirst(); if (arg.contains('-')) { if ((arg == "--version") || (arg == "-v")) { printf("Qt: %s\n", qVersion()); #ifndef QT_ONLY printf("KDE: %s\n", KDE_VERSION_STRING); #endif return 0; } if ((arg == "--exec") || (arg == "-e")) { gui = false; } else if ((arg == "--interactive") || (arg == "-i")) (*KJSEmbed::conout()) << "Interactive"; #ifndef QT_ONLY else if ((arg == "-n") || (arg == "--no-kde")) { kde = false; } #endif else { printUsage(appName); return 0; } } else { if (!script.isEmpty()) scriptArgs.append(KJS::jsString(arg)); else script = arg; } } } else { printUsage(appName); return 0; } // Setup QApplication QCoreApplication *app; #ifndef QT_ONLY if (kde) { KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2", ki18n("" "Utility for running KJSEmbed scripts \n" ), KAboutData::License_LGPL, ki18n("(C) 2005-2006 The KJSEmbed Authors") ); KCmdLineOptions options; options.add("e", ki18n("Execute script without gui support")); options.add("exec", ki18n("Execute script without gui support")); options.add("i", ki18n("start interactive kjs interpreter")); options.add("interactive", ki18n("start interactive kjs interpreter")); options.add("n", ki18n("start without KDE KApplication support.")); options.add("no-kde", ki18n("start without KDE KApplication support.")); options.add("!+command", ki18n("Script to execute")); KCmdLineArgs::addCmdLineOptions( options ); // Add our own options. KCmdLineArgs::init( argc, argv, &aboutData ); app = new KApplication(gui); } else #endif if (gui) { qDebug("no KDE"); app = new QApplication( argc, argv ); dynamic_cast<QApplication*>(app)->connect( app, SIGNAL(lastWindowClosed()), SLOT(quit()) ); } else { qDebug("no GUI"); app = new QCoreApplication(argc, argv); } qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed()); app->setApplicationName( appName ); // Setup Interpreter time.restart(); Engine kernel; qDebug(" New engine %dms", time.elapsed()); time.restart(); KJS::Interpreter *js = kernel.interpreter(); js->setShouldPrintExceptions(true); KJS::ExecState *exec = js->globalExec(); // Publish bindings KJS::JSObject *appObject = kernel.addObject( app, "Application" ); KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs ); appObject->put( exec, "args", argObject ); Engine::ExitStatus result = Engine::Failure; if (!script.isEmpty()) { result = kernel.runFile(toUString(script)); } else // exec shell { result = kernel.runFile( ":/console.js" ); } if ( result != Engine::Success ) { KJS::Completion jsres = kernel.completion(); (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl; } return (int)result; }
void PluginKateKJSWrapper::addView(Kate::MainWindow *win) { PluginKateKJSWrapperView * view=getViewObject(win); // this is needed to ensure correct caching the javascript object KJS::Interpreter *js = m_part->interpreter(); KJS::ExecState *exec = js->globalExec(); exec->clearException(); kdDebug()<<"****************************************************************************************"<<endl; kdDebug()<<"PluginKateKJSWrapper::addView"<<endl; kdDebug()<<"****************************************************************************************"<<endl; kdDebug()<<"checking for newWindowHandler"<<endl; if (!m_newWindowHandler.isNull()) { KJS::List param; param.append(view->winObj); KJS::Object newWinFunc=m_newWindowHandler.toObject(exec); if (exec->hadException()) { exec->clearException(); } else { if (newWinFunc.implementsCall()) { newWinFunc.call(exec,js->globalObject(),param); if (exec->hadException()) { kdDebug()<<"Error while calling newWindowHandler"<<endl; exec->clearException(); } } } } if (exec->hadException()) kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): exec had an exception - 1"<<endl; kdDebug()<<"checking for toolview constructors"<<endl; if (! (m_toolViewConstructors.isNull() || (m_toolViewConstructors.type()==KJS::NullType))) { KJS::Object constrs=m_toolViewConstructors.toObject(exec); if (!exec->hadException()) { if (QString(constrs.classInfo()->className)=="Array") { kdDebug()<<"Toolview constructor array detected"<<endl; int size=constrs.get(exec,KJS::Identifier("length")).toInteger(exec); if (exec->hadException()) { exec->clearException(); kdDebug()<<"Error while retrieving array length"<<endl; } else { for (int i=0;i<size;i++) { KJS::Object constrO=constrs.get(exec,i).toObject(exec); if (exec->hadException()) { exec->clearException(); } else { KMDI::ToolViewAccessor *w=createToolView(m_part->factory(),js,win,view->winObj,constrO); if (w) { view->toolviews.append(QGuardedPtr<KMDI::ToolViewAccessor>(w)); } exec->clearException(); } } } } else { kdDebug()<<"Single toolview constructor detected"<<endl; if (!constrs.implementsConstruct()) { kdWarning()<<"wrong object type"<<endl; } else { KMDI::ToolViewAccessor *w=createToolView(m_part->factory(),js,win,view->winObj,constrs); if (w) { view->toolviews.append(QGuardedPtr<KMDI::ToolViewAccessor>(w)); } exec->clearException(); } } } } else kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): no toolview constructors"<<endl; if (exec->hadException()) kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): exec had an exception - 2"<<endl; view->setInstance (new KInstance("kate")); view->setXMLFile(QString("plugins/%1/%2.rc").arg(m_scriptname).arg(m_scriptname)); win->guiFactory()->addClient (view); }