2009-05-13 02:55:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Richard J. Moore <rich@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "javascriptrunner.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
2010-11-10 17:03:55 +01:00
|
|
|
#include <KDebug>
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/AbstractRunner>
|
2010-06-30 01:44:27 +02:00
|
|
|
#include <Plasma/Package>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/QueryMatch>
|
|
|
|
|
2010-01-07 03:39:49 +01:00
|
|
|
#include "authorization.h"
|
|
|
|
#include "scriptenv.h"
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
typedef const Plasma::RunnerContext* ConstRunnerContextStar;
|
|
|
|
typedef const Plasma::QueryMatch* ConstSearchMatchStar;
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(Plasma::QueryMatch*)
|
|
|
|
Q_DECLARE_METATYPE(Plasma::RunnerContext*)
|
|
|
|
Q_DECLARE_METATYPE(ConstRunnerContextStar)
|
|
|
|
Q_DECLARE_METATYPE(ConstSearchMatchStar)
|
|
|
|
|
|
|
|
JavaScriptRunner::JavaScriptRunner(QObject *parent, const QVariantList &args)
|
2010-02-22 12:33:21 +01:00
|
|
|
: RunnerScript(parent)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-11-15 16:41:16 +01:00
|
|
|
Q_UNUSED(args);
|
2010-02-22 12:33:21 +01:00
|
|
|
|
|
|
|
m_engine = new QScriptEngine(this);
|
|
|
|
m_env = new ScriptEnv(this, m_engine);
|
|
|
|
|
2010-01-07 03:39:49 +01:00
|
|
|
connect(m_engine, SIGNAL(reportError(ScriptEnv*,bool)), this, SLOT(reportError(ScriptEnv*,bool)));
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JavaScriptRunner::~JavaScriptRunner()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::AbstractRunner* JavaScriptRunner::runner() const
|
|
|
|
{
|
|
|
|
return RunnerScript::runner();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JavaScriptRunner::init()
|
|
|
|
{
|
|
|
|
setupObjects();
|
|
|
|
|
2010-01-07 03:39:49 +01:00
|
|
|
Authorization auth;
|
2010-02-22 12:33:21 +01:00
|
|
|
if (!m_env->importExtensions(description(), m_self, auth)) {
|
2010-01-07 03:39:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QFile file(mainScript());
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
|
|
|
|
kWarning() << "Unable to load script file";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString script = file.readAll();
|
|
|
|
kDebug() << "Script says" << script;
|
|
|
|
|
|
|
|
m_engine->evaluate(script);
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env, true);
|
2009-05-13 02:55:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-22 04:08:33 +02:00
|
|
|
return m_env->include(mainScript());
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-01-07 03:39:49 +01:00
|
|
|
void JavaScriptRunner::match(Plasma::RunnerContext &search)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
|
|
|
QScriptValue fun = m_self.property("match");
|
|
|
|
if (!fun.isFunction()) {
|
|
|
|
kDebug() << "Script: match is not a function, " << fun.toString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValueList args;
|
2010-01-07 03:39:49 +01:00
|
|
|
args << m_engine->toScriptValue(&search);
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self, args);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env, false);
|
2010-05-08 01:37:25 +02:00
|
|
|
m_engine->clearExceptions();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaScriptRunner::exec(const Plasma::RunnerContext *search, const Plasma::QueryMatch *action)
|
|
|
|
{
|
|
|
|
QScriptValue fun = m_self.property("exec");
|
|
|
|
if (!fun.isFunction()) {
|
|
|
|
kDebug() << "Script: exec is not a function, " << fun.toString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->toScriptValue(search);
|
|
|
|
args << m_engine->toScriptValue(action);
|
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self, args);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env, false);
|
2010-05-08 01:37:25 +02:00
|
|
|
m_engine->clearExceptions();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaScriptRunner::setupObjects()
|
|
|
|
{
|
|
|
|
QScriptValue global = m_engine->globalObject();
|
|
|
|
|
|
|
|
// Expose the runner
|
|
|
|
m_self = m_engine->newQObject(this);
|
|
|
|
m_self.setScope(global);
|
2010-08-05 21:05:37 +02:00
|
|
|
m_env->addMainObjectProperties(m_self);
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
global.setProperty("runner", m_self);
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
void JavaScriptRunner::reportError(ScriptEnv *env, bool fatal)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2010-01-07 03:39:49 +01:00
|
|
|
Q_UNUSED(fatal)
|
2010-02-22 12:33:21 +01:00
|
|
|
kDebug() << "Error: " << env->engine()->uncaughtException().toString()
|
|
|
|
<< " at line " << env->engine()->uncaughtExceptionLineNumber() << endl;
|
|
|
|
kDebug() << env->engine()->uncaughtExceptionBacktrace();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-09-05 00:51:48 +02:00
|
|
|
QString JavaScriptRunner::filePath(const char *type, const QString &file) const
|
|
|
|
{
|
|
|
|
const QString path = m_env->filePathFromScriptContext(type, file);
|
2010-09-16 02:33:09 +02:00
|
|
|
if (!path.isEmpty()) {
|
2010-09-05 00:51:48 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return package()->filePath(type, file);
|
|
|
|
}
|
|
|
|
|
2010-06-30 01:44:27 +02:00
|
|
|
bool JavaScriptRunner::include(const QString &script)
|
|
|
|
{
|
2010-09-05 00:51:48 +02:00
|
|
|
const QString path = filePath("scripts", script);
|
2010-06-30 01:44:27 +02:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_env->include(path);
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
#include "javascriptrunner.moc"
|