2010-01-07 03:04:59 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007-2008 Richard J. Moore <rich@kde.org>
|
|
|
|
* Copyright 2009 Aaron J. Seigo <aseigo@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 "scriptenv.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QMetaEnum>
|
|
|
|
|
|
|
|
#include <KDebug>
|
2010-08-04 05:53:41 +02:00
|
|
|
#include <KDesktopFile>
|
2010-01-07 03:04:59 +01:00
|
|
|
#include <KIO/Job>
|
|
|
|
#include <KLocale>
|
|
|
|
#include <KMimeType>
|
2010-08-04 05:53:41 +02:00
|
|
|
#include <KPluginInfo>
|
2010-01-07 03:04:59 +01:00
|
|
|
#include <KService>
|
2010-08-04 05:53:41 +02:00
|
|
|
#include <KServiceTypeTrader>
|
2010-01-07 03:04:59 +01:00
|
|
|
#include <KShell>
|
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KRun>
|
|
|
|
|
2010-08-04 05:53:41 +02:00
|
|
|
#include <Plasma/Package>
|
|
|
|
|
2010-01-07 03:26:44 +01:00
|
|
|
#ifdef USEGUI
|
2010-01-07 03:04:59 +01:00
|
|
|
#include "simplebindings/filedialogproxy.h"
|
2010-01-07 03:26:44 +01:00
|
|
|
#endif
|
2010-01-07 03:04:59 +01:00
|
|
|
|
2010-08-04 05:53:41 +02:00
|
|
|
#include "javascriptaddonpackagestructure.h"
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
Q_DECLARE_METATYPE(ScriptEnv*)
|
2010-09-05 00:45:55 +02:00
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
|
|
|
|
: QObject(parent),
|
|
|
|
m_allowedUrls(NoUrls),
|
|
|
|
m_engine(engine)
|
2010-01-07 03:04:59 +01:00
|
|
|
{
|
2010-08-05 21:05:37 +02:00
|
|
|
connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(signalException()));
|
2010-02-22 12:33:21 +01:00
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
setupGlobalObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEnv::~ScriptEnv()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptEnv::setupGlobalObject()
|
|
|
|
{
|
|
|
|
QScriptValue global = m_engine->globalObject();
|
2010-02-22 12:33:21 +01:00
|
|
|
|
|
|
|
// Add an accessor so we can find the scriptenv given only the engine. The
|
|
|
|
// property is hidden from scripts.
|
|
|
|
global.setProperty("__plasma_scriptenv", m_engine->newQObject(this),
|
2010-04-30 00:00:19 +02:00
|
|
|
QScriptValue::ReadOnly|QScriptValue::Undeletable|QScriptValue::SkipInEnumeration);
|
2010-08-05 21:05:37 +02:00
|
|
|
// Add utility functions
|
2010-10-20 21:40:30 +02:00
|
|
|
#ifndef DECLARATIVE
|
2010-08-05 21:05:37 +02:00
|
|
|
global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
|
2010-10-20 21:40:30 +02:00
|
|
|
#endif
|
2010-08-05 21:05:37 +02:00
|
|
|
global.setProperty("debug", m_engine->newFunction(ScriptEnv::debug));
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
void ScriptEnv::addMainObjectProperties(QScriptValue &value)
|
2010-01-07 03:04:59 +01:00
|
|
|
{
|
2010-08-05 21:05:37 +02:00
|
|
|
value.setProperty("listAddons", m_engine->newFunction(ScriptEnv::listAddons));
|
|
|
|
value.setProperty("loadAddon", m_engine->newFunction(ScriptEnv::loadAddon));
|
|
|
|
value.setProperty("addEventListener", m_engine->newFunction(ScriptEnv::addEventListener));
|
|
|
|
value.setProperty("removeEventListener", m_engine->newFunction(ScriptEnv::removeEventListener));
|
2010-09-03 23:31:37 +02:00
|
|
|
value.setProperty("hasExtension", m_engine->newFunction(ScriptEnv::hasExtension));
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
QScriptEngine *ScriptEnv::engine() const
|
|
|
|
{
|
|
|
|
return m_engine;
|
|
|
|
}
|
|
|
|
|
2010-03-09 23:18:45 +01:00
|
|
|
ScriptEnv *ScriptEnv::findScriptEnv(QScriptEngine *engine)
|
2010-02-22 12:33:21 +01:00
|
|
|
{
|
|
|
|
QScriptValue global = engine->globalObject();
|
|
|
|
return qscriptvalue_cast<ScriptEnv*>(global.property("__plasma_scriptenv"));
|
|
|
|
}
|
|
|
|
|
2010-05-08 01:37:25 +02:00
|
|
|
void ScriptEnv::signalException()
|
|
|
|
{
|
|
|
|
checkForErrors(false);
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
void ScriptEnv::registerEnums(QScriptValue &scriptValue, const QMetaObject &meta)
|
|
|
|
{
|
|
|
|
//manually create enum values. ugh
|
2010-03-25 00:31:08 +01:00
|
|
|
QScriptEngine *engine = scriptValue.engine();
|
2010-01-07 03:04:59 +01:00
|
|
|
for (int i = 0; i < meta.enumeratorCount(); ++i) {
|
|
|
|
QMetaEnum e = meta.enumerator(i);
|
|
|
|
//kDebug() << e.name();
|
|
|
|
for (int i=0; i < e.keyCount(); ++i) {
|
|
|
|
//kDebug() << e.key(i) << e.value(i);
|
2010-03-25 00:31:08 +01:00
|
|
|
scriptValue.setProperty(e.key(i), QScriptValue(engine, e.value(i)));
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEnv::include(const QString &path)
|
|
|
|
{
|
|
|
|
QFile file(path);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
kWarning() << i18n("Unable to load script file: %1", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString script = file.readAll();
|
|
|
|
//kDebug() << "Script says" << script;
|
|
|
|
|
2010-01-15 23:23:00 +01:00
|
|
|
// change the context to the parent context so that the include is actually
|
|
|
|
// executed in the same context as the caller; seems to be what javascript
|
|
|
|
// coders expect :)
|
2010-02-22 12:33:21 +01:00
|
|
|
QScriptContext *ctx = m_engine->currentContext();
|
2010-01-15 23:23:00 +01:00
|
|
|
if (ctx && ctx->parentContext()) {
|
|
|
|
ctx->setActivationObject(ctx->parentContext()->activationObject());
|
|
|
|
ctx->setThisObject(ctx->parentContext()->thisObject());
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
m_engine->evaluate(script, path);
|
2010-01-15 23:23:00 +01:00
|
|
|
|
2010-03-09 23:18:45 +01:00
|
|
|
return !checkForErrors(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEnv::checkForErrors(bool fatal)
|
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-03-09 23:18:45 +01:00
|
|
|
emit reportError(this, fatal);
|
2010-05-08 01:37:25 +02:00
|
|
|
if (!fatal) {
|
|
|
|
m_engine->clearExceptions();
|
|
|
|
}
|
2010-03-09 23:18:45 +01:00
|
|
|
return true;
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
2010-03-09 23:18:45 +01:00
|
|
|
return false;
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEnv::importBuiltinExtension(const QString &extension, QScriptValue &obj)
|
|
|
|
{
|
|
|
|
kDebug() << extension;
|
|
|
|
if ("filedialog" == extension) {
|
2010-01-07 03:26:44 +01:00
|
|
|
#ifdef USEGUI
|
2010-02-22 12:33:21 +01:00
|
|
|
FileDialogProxy::registerWithRuntime(m_engine);
|
2010-01-07 03:04:59 +01:00
|
|
|
return true;
|
2010-01-07 03:26:44 +01:00
|
|
|
#endif
|
2010-01-07 03:04:59 +01:00
|
|
|
} else if ("launchapp" == extension) {
|
2011-04-09 20:56:02 +02:00
|
|
|
m_allowedUrls |= AppLaunching;
|
2010-02-22 12:33:21 +01:00
|
|
|
obj.setProperty("runApplication", m_engine->newFunction(ScriptEnv::runApplication));
|
|
|
|
obj.setProperty("runCommand", m_engine->newFunction(ScriptEnv::runCommand));
|
2011-04-09 20:56:02 +02:00
|
|
|
registerOpenUrl(obj);
|
2010-01-07 03:04:59 +01:00
|
|
|
return true;
|
|
|
|
} else if ("http" == extension) {
|
|
|
|
m_allowedUrls |= HttpUrls;
|
|
|
|
registerGetUrl(obj);
|
2011-04-09 20:56:02 +02:00
|
|
|
registerOpenUrl(obj);
|
2010-01-07 03:04:59 +01:00
|
|
|
return true;
|
|
|
|
} else if ("networkio" == extension) {
|
|
|
|
m_allowedUrls |= HttpUrls | NetworkUrls;
|
|
|
|
registerGetUrl(obj);
|
|
|
|
return true;
|
|
|
|
} else if ("localio" == extension) {
|
|
|
|
m_allowedUrls |= LocalUrls;
|
|
|
|
registerGetUrl(obj);
|
2011-07-07 16:29:42 +02:00
|
|
|
obj.setProperty("userDataPath", m_engine->newFunction(ScriptEnv::userDataPath));
|
|
|
|
obj.setProperty("runCommand", m_engine->newFunction(ScriptEnv::runCommand));
|
2010-01-07 03:04:59 +01:00
|
|
|
return true;
|
2011-12-05 22:40:16 +01:00
|
|
|
} else if ("download" == extension) {
|
|
|
|
obj.setProperty("download", m_engine->newFunction(ScriptEnv::download));
|
|
|
|
return true;
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEnv::importExtensions(const KPluginInfo &info, QScriptValue &obj, Authorization &auth)
|
|
|
|
{
|
|
|
|
QStringList requiredExtensions = info.service()->property("X-Plasma-RequiredExtensions", QVariant::StringList).toStringList();
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!requiredExtensions.isEmpty()) {
|
|
|
|
kDebug() << "required extensions are" << requiredExtensions;
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
foreach (const QString &ext, requiredExtensions) {
|
|
|
|
QString extension = ext.toLower();
|
|
|
|
if (m_extensions.contains(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!auth.authorizeRequiredExtension(extension)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!importBuiltinExtension(extension, obj)) {
|
2010-01-08 01:55:46 +01:00
|
|
|
if (auth.authorizeExternalExtensions()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
m_engine->importExtension(extension);
|
2010-01-08 01:55:46 +01:00
|
|
|
}
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
2010-03-09 23:18:45 +01:00
|
|
|
if (checkForErrors(true)) {
|
2010-01-07 03:04:59 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
m_extensions << extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList optionalExtensions = info.service()->property("X-Plasma-OptionalExtensions", QVariant::StringList).toStringList();
|
2010-09-03 23:00:25 +02:00
|
|
|
if (!optionalExtensions.isEmpty()) {
|
|
|
|
kDebug() << "optional extensions are" << optionalExtensions;
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
foreach (const QString &ext, optionalExtensions) {
|
|
|
|
QString extension = ext.toLower();
|
|
|
|
|
|
|
|
if (m_extensions.contains(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!auth.authorizeOptionalExtension(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!importBuiltinExtension(extension, obj)) {
|
2010-01-08 01:55:46 +01:00
|
|
|
if (auth.authorizeExternalExtensions()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
m_engine->importExtension(extension);
|
2010-01-08 01:55:46 +01:00
|
|
|
}
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
2010-03-09 23:18:45 +01:00
|
|
|
if (!checkForErrors(false)) {
|
2010-01-07 03:04:59 +01:00
|
|
|
m_extensions << extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> ScriptEnv::loadedExtensions() const
|
|
|
|
{
|
|
|
|
return m_extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::debug(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("debug takes one argument"), context, engine);
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << context->argument(0).toString();
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-09-04 00:36:27 +02:00
|
|
|
QScriptValue ScriptEnv::throwNonFatalError(const QString &msg, QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QScriptValue rv = context->throwError(msg);
|
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (env) {
|
|
|
|
env->checkForErrors(false);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
QScriptValue ScriptEnv::print(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("print() takes one argument"), context, engine);
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << context->argument(0).toString().toLocal8Bit().constData() << std::endl;
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-08-04 05:53:41 +02:00
|
|
|
QScriptValue ScriptEnv::listAddons(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() < 1) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("listAddons takes one argument: addon type"), context, engine);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString type = context->argument(0).toString();
|
|
|
|
|
|
|
|
if (type.isEmpty()) {
|
2010-08-06 01:17:07 +02:00
|
|
|
return engine->undefinedValue();
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1'").arg(type);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/JavascriptAddon", constraint);
|
|
|
|
|
2010-08-06 01:17:07 +02:00
|
|
|
int i = 0;
|
|
|
|
QScriptValue addons = engine->newArray(offers.count());
|
2010-08-04 05:53:41 +02:00
|
|
|
foreach (KService::Ptr offer, offers) {
|
|
|
|
KPluginInfo info(offer);
|
2010-08-06 01:17:07 +02:00
|
|
|
QScriptValue v = engine->newObject();
|
|
|
|
v.setProperty("id", info.pluginName(), QScriptValue::ReadOnly);
|
|
|
|
v.setProperty("name", info.name(), QScriptValue::ReadOnly);
|
|
|
|
addons.setProperty(i++, v);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 01:17:07 +02:00
|
|
|
return addons;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::loadAddon(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() < 2) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("loadAddon takes two arguments: addon type and addon name to load"), context, engine);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString type = context->argument(0).toString();
|
|
|
|
const QString plugin = context->argument(1).toString();
|
|
|
|
|
|
|
|
if (type.isEmpty() || plugin.isEmpty()) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("loadAddon takes two arguments: addon type and addon name to load"), context, engine);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1' and [X-KDE-PluginInfo-Name] == '%2'")
|
|
|
|
.arg(type, plugin);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/JavascriptAddon", constraint);
|
|
|
|
|
|
|
|
if (offers.isEmpty()) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("Failed to find Addon %1 of type %2", plugin, type), context, engine);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::PackageStructure::Ptr structure(new JavascriptAddonPackageStructure);
|
|
|
|
const QString subPath = structure->defaultPackageRoot() + '/' + plugin + '/';
|
|
|
|
const QString path = KStandardDirs::locate("data", subPath);
|
|
|
|
Plasma::Package package(path, structure);
|
|
|
|
|
|
|
|
QFile file(package.filePath("mainscript"));
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2010-09-04 00:36:27 +02:00
|
|
|
return throwNonFatalError(i18n("Failed to open script file for Addon %1: %2", plugin, package.filePath("mainscript")), context, engine);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream buffer(&file);
|
|
|
|
QString code(buffer.readAll());
|
|
|
|
|
|
|
|
QScriptContext *innerContext = engine->pushContext();
|
|
|
|
innerContext->activationObject().setProperty("registerAddon", engine->newFunction(ScriptEnv::registerAddon));
|
2010-09-03 23:00:25 +02:00
|
|
|
QScriptValue v = engine->newVariant(QVariant::fromValue(package));
|
2010-09-04 02:05:30 +02:00
|
|
|
innerContext->activationObject().setProperty("__plasma_package", v,
|
|
|
|
QScriptValue::ReadOnly |
|
2010-09-03 23:00:25 +02:00
|
|
|
QScriptValue::Undeletable |
|
|
|
|
QScriptValue::SkipInEnumeration);
|
2010-09-05 00:45:55 +02:00
|
|
|
//kDebug() << "context is" << innerContext;
|
2010-08-04 05:53:41 +02:00
|
|
|
engine->evaluate(code, file.fileName());
|
|
|
|
engine->popContext();
|
2010-09-04 00:00:12 +02:00
|
|
|
|
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
2010-09-04 02:05:30 +02:00
|
|
|
if (env && env->checkForErrors(false)) {
|
|
|
|
return false;
|
2010-09-04 00:00:12 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 01:00:45 +02:00
|
|
|
return true;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::registerAddon(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() > 0) {
|
|
|
|
QScriptValue func = context->argument(0);
|
|
|
|
if (func.isFunction()) {
|
|
|
|
QScriptValue obj = func.construct();
|
2010-09-04 02:05:30 +02:00
|
|
|
obj.setProperty("__plasma_package",
|
|
|
|
context->parentContext()->activationObject().property("__plasma_package"),
|
2010-09-16 02:33:09 +02:00
|
|
|
QScriptValue::ReadOnly |
|
2010-09-04 02:05:30 +02:00
|
|
|
QScriptValue::Undeletable |
|
2010-09-03 23:00:25 +02:00
|
|
|
QScriptValue::SkipInEnumeration);
|
2010-08-05 22:08:26 +02:00
|
|
|
|
2010-08-04 05:53:41 +02:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (env) {
|
2010-08-05 22:08:26 +02:00
|
|
|
QScriptValueList args;
|
|
|
|
args << obj;
|
|
|
|
env->callEventListeners("addoncreated", args);
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-09-05 00:45:55 +02:00
|
|
|
QString ScriptEnv::filePathFromScriptContext(const char *type, const QString &file) const
|
|
|
|
{
|
|
|
|
//kDebug() << type << file;
|
|
|
|
QScriptContext *c = m_engine->currentContext();
|
|
|
|
while (c) {
|
|
|
|
QScriptValue v = c->activationObject().property("__plasma_package");
|
|
|
|
//kDebug() << "variant in parent context?" << v.isVariant();
|
|
|
|
if (v.isVariant()) {
|
|
|
|
const QString path = v.toVariant().value<Plasma::Package>().filePath(type, file);
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = c->parentContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "fail";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
QScriptValue ScriptEnv::addEventListener(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (!env) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return env->addEventListener(context->argument(0).toString(), context->argument(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::removeEventListener(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (!env) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return env->removeEventListener(context->argument(0).toString(), context->argument(1));
|
|
|
|
}
|
|
|
|
|
2010-09-03 23:31:37 +02:00
|
|
|
QScriptValue ScriptEnv::hasExtension(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (!env) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return env->m_extensions.contains(context->argument(0).toString().toLower());
|
|
|
|
}
|
|
|
|
|
2010-08-05 23:13:13 +02:00
|
|
|
QScriptValue ScriptEnv::callFunction(QScriptValue &func, const QScriptValueList &args, const QScriptValue &activator)
|
2010-08-04 05:53:41 +02:00
|
|
|
{
|
|
|
|
if (!func.isFunction()) {
|
2010-08-05 23:13:13 +02:00
|
|
|
return m_engine->undefinedValue();
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(activator);
|
2010-08-05 23:13:13 +02:00
|
|
|
QScriptValue rv = func.call(activator, args);
|
2010-08-04 05:53:41 +02:00
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
|
|
|
emit reportError(this, false);
|
|
|
|
m_engine->clearExceptions();
|
2010-08-05 23:13:13 +02:00
|
|
|
return m_engine->undefinedValue();
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
2010-08-05 23:13:13 +02:00
|
|
|
|
|
|
|
return rv;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEnv::hasEventListeners(const QString &event) const
|
|
|
|
{
|
|
|
|
return m_eventListeners.contains(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEnv::callEventListeners(const QString &event, const QScriptValueList &args)
|
|
|
|
{
|
2010-10-05 15:36:05 +02:00
|
|
|
if (!m_eventListeners.contains(event.toLower())) {
|
2010-08-04 05:53:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-05 15:36:05 +02:00
|
|
|
QScriptValueList funcs = m_eventListeners.value(event.toLower());
|
2010-08-04 05:53:41 +02:00
|
|
|
QMutableListIterator<QScriptValue> it(funcs);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
callFunction(it.next(), args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
bool ScriptEnv::addEventListener(const QString &event, const QScriptValue &func)
|
2010-08-04 05:53:41 +02:00
|
|
|
{
|
2010-08-05 21:05:37 +02:00
|
|
|
if (func.isFunction() && !event.isEmpty()) {
|
2010-08-04 05:53:41 +02:00
|
|
|
m_eventListeners[event.toLower()].append(func);
|
2010-08-05 21:05:37 +02:00
|
|
|
return true;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
2010-08-05 21:05:37 +02:00
|
|
|
|
|
|
|
return false;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
bool ScriptEnv::removeEventListener(const QString &event, const QScriptValue &func)
|
2010-08-04 05:53:41 +02:00
|
|
|
{
|
2010-08-05 21:05:37 +02:00
|
|
|
bool found = false;
|
|
|
|
|
2010-08-04 05:53:41 +02:00
|
|
|
if (func.isFunction()) {
|
2010-08-05 21:05:37 +02:00
|
|
|
QScriptValueList funcs = m_eventListeners.value(event);
|
|
|
|
QMutableListIterator<QScriptValue> it(funcs);
|
2010-08-04 05:53:41 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
if (it.next().equals(func)) {
|
|
|
|
it.remove();
|
2010-08-05 21:05:37 +02:00
|
|
|
found = true;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (funcs.isEmpty()) {
|
|
|
|
m_eventListeners.remove(event.toLower());
|
|
|
|
} else {
|
|
|
|
m_eventListeners.insert(event.toLower(), funcs);
|
|
|
|
}
|
|
|
|
}
|
2010-08-05 21:05:37 +02:00
|
|
|
|
|
|
|
return found;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
2010-08-05 06:42:39 +02:00
|
|
|
#ifndef USEGUI
|
2010-01-07 03:04:59 +01:00
|
|
|
#include "scriptenv.moc"
|
2010-08-05 06:42:39 +02:00
|
|
|
#endif
|