[Kst] extragear/graphics/kst/kst/extensions/js

George Staikos staikos at kde.org
Mon Dec 12 22:45:57 CET 2005


SVN commit 488022 by staikos:

add a loadScript() function


 M  +36 -0     js.cpp  
 M  +4 -0      js.h  


--- trunk/extragear/graphics/kst/kst/extensions/js/js.cpp #488021:488022
@@ -34,6 +34,7 @@
 #include <kaction.h>
 #include <kfiledialog.h>
 #include <kgenericfactory.h>
+#include <kjs/function.h>
 #include <kjsembed/jsbuiltin.h>
 #include <kjsembed/jsconsolewidget.h>
 #include <kjsembed/jsfactory.h>
@@ -72,6 +73,39 @@
 K_EXPORT_COMPONENT_FACTORY(kstextension_js, KGenericFactory<KstJS>)
 
 
+class Function : public KJS::ObjectImp {
+  public:
+    virtual bool implementsCall() const {
+      return true;
+    }
+};
+
+
+class LoadScript : public Function {
+  public:
+  LoadScript(KstJS *ext) : Function(), _e(ext) {}
+  virtual KJS::Value call(KJS::ExecState* exec, KJS::Object&, const KJS::List& args) {
+    if (args.size() != 1) {
+      KJS::Object eobj = KJS::Error::create(exec, KJS::SyntaxError, "Requires exactly one argument.");
+      exec->setException(eobj);
+      return KJS::Undefined();
+    }
+    if (!_e) {
+      return KJS::Boolean(false);
+    }
+    QString file = args[0].toString(exec).qstring();
+    if (_e->_jsPart->runFile(file)) {
+      if (!_e->_scripts.contains(file)) {
+        _e->_scripts.append(file);
+      }
+      return KJS::Boolean(true);
+    }
+    return KJS::Boolean(false);
+  }
+  QGuardedPtr<KstJS> _e;
+};
+
+
 KstJS::KstJS(QObject *parent, const char *name, const QStringList& l) : KstExtension(parent, name, l), KXMLGUIClient() {
   KJSEmbed::JSSecurityPolicy::setDefaultPolicy(KJSEmbed::JSSecurityPolicy::CapabilityAll);
   _jsPart = new KJSEmbed::KJSEmbedPart(0L, "javascript", this, "kjsembedpart");
@@ -151,6 +185,8 @@
   new KstBindDebug(exec, &globalObj);
   new KstBindKst(exec, &globalObj, this);
 
+  globalObj.put(exec, "loadScript", KJS::Object(new LoadScript(this)));
+
   createRegistry();
 }
 
--- trunk/extragear/graphics/kst/kst/extensions/js/js.h #488021:488022
@@ -60,8 +60,12 @@
   private:
     void createBindings();
 
+  protected:
+    friend class LoadScript;
     QStringList _scripts;
     KJSEmbed::KJSEmbedPart *_jsPart;
+
+  private:
     JSIfaceImpl *_iface;
     QGuardedPtr<KParts::Part> _konsolePart;
     QGuardedPtr<QSplitter> _splitter;


More information about the Kst mailing list