summaryrefslogtreecommitdiffstats
path: root/src/app/KrJS
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/KrJS')
-rw-r--r--src/app/KrJS/Makefile.am13
-rw-r--r--src/app/KrJS/krjs.cpp89
-rw-r--r--src/app/KrJS/krjs.h32
3 files changed, 134 insertions, 0 deletions
diff --git a/src/app/KrJS/Makefile.am b/src/app/KrJS/Makefile.am
new file mode 100644
index 0000000..af10182
--- /dev/null
+++ b/src/app/KrJS/Makefile.am
@@ -0,0 +1,13 @@
+# This should not be nessesary because this subdir is only visited if KJSEmbed should be used...
+#if include_libkjsembed
+#AM_CPPFLAGS = -D__KJSEMBED__
+#endif
+
+noinst_LIBRARIES = libKrJS.a
+
+INCLUDES = $(all_includes)
+
+libKrJS_a_METASOURCES = AUTO
+
+libKrJS_a_SOURCES = \
+ krjs.cpp
diff --git a/src/app/KrJS/krjs.cpp b/src/app/KrJS/krjs.cpp
new file mode 100644
index 0000000..8d1cffb
--- /dev/null
+++ b/src/app/KrJS/krjs.cpp
@@ -0,0 +1,89 @@
+// Implementation of our JavaScript-Interpreter
+//
+// Author: Jonas B�r (C) 2005
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+
+#include "krjs.h"
+
+#include "../krusader.h"
+#include "../krusaderview.h"
+#include "../panelmanager.h"
+
+#include <tdemessagebox.h>
+
+KrJS::KrJS() : KJSEmbed::KJSEmbedPart() {
+
+ // make this object, the object Krusader, available for scripting as "Krusader":
+ addObject( krApp, "Krusader" );
+
+ // make this object available for scripting
+ addObject( ACTIVE_MNG, "PanelManager" );
+}
+
+bool KrJS::runFile(const TQString & filename) {
+
+ KJS::ExecState *exec = globalExec();
+
+ // set up the variable scriptDir with the directory of the current script
+ putValue( "scriptDir", KJSEmbed::convertToValue(exec, KURL(filename).directory(false) ) );
+
+ bool ok = KJSEmbedPart::runFile(filename);
+
+ KJS::Completion jsres = completion();
+
+ // this is based on this example-code http://webcvs.kde.org/tdebindings/kjsembed/kjscmd.cpp?rev=1.28&view=auto
+ if ( jsres.complType() != KJS::Normal) {
+ switch ( jsres.complType() ) {
+ case KJS::Break:
+ case KJS::Continue:
+ // TODO: find out what this means
+ krOut << "JavaScript: " << jsres.value().toString(exec).qstring() << endl;
+ break;
+ case KJS::ReturnValue:
+ // that's only needed when a specific function is called
+ //someString = jsres.value().toString(exec).qstring();
+ break;
+ case KJS::Throw:
+ /*
+ This is the interesting part: If the JS throws an exception which is not handled inside the script,
+ we can catch it here and handle it with c++
+ */
+ {
+#if KDE_IS_VERSION(3,4,0)
+ KJS::Object exception = jsres.value().toObject(exec);
+ int line = int( exception.get( exec, KJS::Identifier("line") ).toNumber(exec) );
+ TQString type = exception.get( exec, KJS::Identifier("name") ).toString(exec).qstring();
+ TQString message = exception.get( exec, KJS::Identifier("message") ).toString(exec).qstring();
+
+ krOut << "JavaScript: Uncaught " << type << " exception at line " << line << " in " << filename << endl;
+ krOut << message << endl;
+
+ KMessageBox::error ( 0, //parent
+ ( line < 0 ?
+ TQString( i18n("In %1:\nUncaught JavaScript exception '%2'\n%3") ).arg(filename).arg(type).arg(message) :
+ TQString( i18n("In %1:\nUncaught JavaScript exception '%2' at line %3\n%4") ).arg(filename).arg(type).arg(line).arg(message)
+ ), //text
+ i18n("JavaScript error"), //caption
+ KMessageBox::Dangerous) ;
+#else
+ KMessageBox::error ( 0, //parent
+ TQString(i18n("In %1:\nThere is an error in the JavaScript")).arg(filename), //text
+ i18n("JavaScript error"), //caption
+ KMessageBox::Dangerous) ;
+#endif
+ break;
+ }
+ default:
+ krOut << "JavaScript: Unknown error." << endl;
+ break;
+ } // switch
+ } // if
+
+
+ krOut << "JS: done" << endl;
+
+ return ok;
+}
+
diff --git a/src/app/KrJS/krjs.h b/src/app/KrJS/krjs.h
new file mode 100644
index 0000000..256d07a
--- /dev/null
+++ b/src/app/KrJS/krjs.h
@@ -0,0 +1,32 @@
+// Interface for our JavaScript-Interpreter
+//
+// Author: Jonas Bähr (C) 2005
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+
+#ifndef KRJS_H
+#define KRJS_H
+
+#include <kjsembed/kjsembedpart.h>
+
+/**
+ * Our own version of KJSEmbedPart. Here are all the Krusader-specific extensions implemented.
+ *
+ * @author Jonas Bähr (http://jonas-baehr.de/)
+*/
+
+class KrJS: public KJSEmbed::KJSEmbedPart {
+public:
+ KrJS();
+
+ /**
+ * This loads and runs a file. In addition to the original runFile function it displays an popup
+ * on errors and sets some variables
+ *
+ * @par filename The file to run
+ */
+ bool runFile(const TQString & filename);
+};
+
+#endif //KRJS_H