add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / javascript / javascriptrunner.cpp
blob221115a96baf6610fd02cbd072e383719c6d9bf4
1 /*
2 * Copyright 2008 Richard J. Moore <rich@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "javascriptrunner.h"
21 #include <QScriptEngine>
22 #include <QFile>
24 #include <Plasma/AbstractRunner>
25 #include <Plasma/QueryMatch>
28 typedef const Plasma::RunnerContext* ConstRunnerContextStar;
29 typedef const Plasma::QueryMatch* ConstSearchMatchStar;
31 Q_DECLARE_METATYPE(Plasma::QueryMatch*)
32 Q_DECLARE_METATYPE(Plasma::RunnerContext*)
33 Q_DECLARE_METATYPE(ConstRunnerContextStar)
34 Q_DECLARE_METATYPE(ConstSearchMatchStar)
36 JavaScriptRunner::JavaScriptRunner(QObject *parent, const QVariantList &args)
37 : RunnerScript(parent)
39 m_engine = new QScriptEngine( this );
40 importExtensions();
43 JavaScriptRunner::~JavaScriptRunner()
47 Plasma::AbstractRunner* JavaScriptRunner::runner() const
49 return RunnerScript::runner();
52 bool JavaScriptRunner::init()
54 setupObjects();
56 QFile file(mainScript());
57 if ( !file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
58 kWarning() << "Unable to load script file";
59 return false;
62 QString script = file.readAll();
63 kDebug() << "Script says" << script;
65 m_engine->evaluate( script );
66 if ( m_engine->hasUncaughtException() ) {
67 reportError();
68 return false;
71 return true;
74 void JavaScriptRunner::match(Plasma::RunnerContext *search)
76 QScriptValue fun = m_self.property( "match" );
77 if ( !fun.isFunction() ) {
78 kDebug() << "Script: match is not a function, " << fun.toString();
79 return;
82 QScriptValueList args;
83 args << m_engine->toScriptValue(search);
85 QScriptContext *ctx = m_engine->pushContext();
86 ctx->setActivationObject( m_self );
87 fun.call( m_self, args );
88 m_engine->popContext();
90 if ( m_engine->hasUncaughtException() ) {
91 reportError();
95 void JavaScriptRunner::exec(const Plasma::RunnerContext *search, const Plasma::QueryMatch *action)
97 QScriptValue fun = m_self.property( "exec" );
98 if ( !fun.isFunction() ) {
99 kDebug() << "Script: exec is not a function, " << fun.toString();
100 return;
103 QScriptValueList args;
104 args << m_engine->toScriptValue(search);
105 args << m_engine->toScriptValue(action);
107 QScriptContext *ctx = m_engine->pushContext();
108 ctx->setActivationObject( m_self );
109 fun.call( m_self, args );
110 m_engine->popContext();
112 if ( m_engine->hasUncaughtException() ) {
113 reportError();
117 void JavaScriptRunner::setupObjects()
119 QScriptValue global = m_engine->globalObject();
121 // Expose an applet
122 m_self = m_engine->newQObject( this );
123 m_self.setScope( global );
125 global.setProperty("runner", m_self);
128 void JavaScriptRunner::importExtensions()
130 QStringList extensions;
131 //extensions << "qt.core" << "qt.gui" << "qt.svg" << "qt.xml" << "org.kde.plasma";
132 extensions << "qt.core" << "qt.gui" << "qt.xml";
133 foreach (const QString &ext, extensions) {
134 kDebug() << "importing " << ext << "...";
135 QScriptValue ret = m_engine->importExtension(ext);
136 if (ret.isError())
137 kDebug() << "failed to import extension" << ext << ":" << ret.toString();
139 kDebug() << "done importing extensions.";
142 void JavaScriptRunner::reportError()
144 kDebug() << "Error: " << m_engine->uncaughtException().toString()
145 << " at line " << m_engine->uncaughtExceptionLineNumber() << endl;
146 kDebug() << m_engine->uncaughtExceptionBacktrace();
149 #include "javascriptrunner.moc"