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-03 23:00:25 +02:00
|
|
|
Q_DECLARE_METATYPE(Plasma::Package)
|
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
|
|
|
|
global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::runApplication(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUrl::List urls;
|
|
|
|
if (context->argumentCount() > 1) {
|
|
|
|
urls = qscriptvalue_cast<KUrl::List>(context->argument(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString app = context->argument(0).toString();
|
|
|
|
|
|
|
|
const QString exec = KGlobal::dirs()->findExe(app);
|
|
|
|
if (!exec.isEmpty()) {
|
|
|
|
return KRun::run(exec, urls, 0);
|
|
|
|
}
|
|
|
|
|
2010-03-01 20:24:42 +01:00
|
|
|
KService::Ptr service = KService::serviceByStorageId(app);
|
|
|
|
if (service) {
|
|
|
|
return KRun::run(*service, urls, 0);
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::runCommand(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
Q_UNUSED(engine);
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString exec = KGlobal::dirs()->findExe(context->argument(0).toString());
|
|
|
|
if (!exec.isEmpty()) {
|
|
|
|
QString args;
|
|
|
|
if (context->argumentCount() > 1) {
|
|
|
|
const QStringList argList = qscriptvalue_cast<QStringList>(context->argument(1));
|
|
|
|
if (!argList.isEmpty()) {
|
|
|
|
args = ' ' + KShell::joinArgs(argList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KRun::runCommand(exec + args, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::openUrl(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue v = context->argument(0);
|
|
|
|
KUrl url = v.isString() ? KUrl(v.toString()) : qscriptvalue_cast<KUrl>(v);
|
|
|
|
if (url.isValid()) {
|
|
|
|
return KRun::runUrl(url, KMimeType::findByUrl(url)->name(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
// TODO these should throw an exception
|
2010-01-07 03:04:59 +01:00
|
|
|
QScriptValue ScriptEnv::getUrl(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue v = context->argument(0);
|
|
|
|
KUrl url = v.isString() ? KUrl(v.toString()) : qscriptvalue_cast<KUrl>(v);
|
|
|
|
|
|
|
|
if (!url.isValid()) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
|
|
|
if (!env) {
|
2010-09-04 00:00:12 +02:00
|
|
|
//kDebug() << "findScriptEnv failed";
|
2010-02-22 12:33:21 +01:00
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
if (url.isLocalFile()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
if (!(env->m_allowedUrls & LocalUrls)) {
|
2010-01-07 03:04:59 +01:00
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2010-02-22 12:33:21 +01:00
|
|
|
} else if (!(env->m_allowedUrls & NetworkUrls) &&
|
|
|
|
!((env->m_allowedUrls & HttpUrls) && (url.protocol() == "http" || url.protocol() == "https"))) {
|
2010-01-07 03:04:59 +01:00
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-02-17 22:38:39 +01:00
|
|
|
KIO::Job *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
|
2010-01-07 03:04:59 +01:00
|
|
|
return engine->newQObject(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptEnv::registerGetUrl(QScriptValue &obj)
|
|
|
|
{
|
|
|
|
QScriptValue get = obj.property("getUrl");
|
|
|
|
if (!get.isValid()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
obj.setProperty("getUrl", m_engine->newFunction(ScriptEnv::getUrl));
|
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) {
|
2010-02-22 12:33:21 +01:00
|
|
|
obj.setProperty("runApplication", m_engine->newFunction(ScriptEnv::runApplication));
|
|
|
|
obj.setProperty("runCommand", m_engine->newFunction(ScriptEnv::runCommand));
|
|
|
|
obj.setProperty("openUrl", m_engine->newFunction(ScriptEnv::openUrl));
|
2010-01-07 03:04:59 +01:00
|
|
|
return true;
|
|
|
|
} else if ("http" == extension) {
|
|
|
|
m_allowedUrls |= HttpUrls;
|
|
|
|
registerGetUrl(obj);
|
|
|
|
return true;
|
|
|
|
} else if ("networkio" == extension) {
|
|
|
|
m_allowedUrls |= HttpUrls | NetworkUrls;
|
|
|
|
registerGetUrl(obj);
|
|
|
|
return true;
|
|
|
|
} else if ("localio" == extension) {
|
|
|
|
m_allowedUrls |= LocalUrls;
|
|
|
|
registerGetUrl(obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
innerContext->activationObject().setProperty("__plasma_addon_package", v,
|
|
|
|
QScriptValue::ReadOnly |
|
|
|
|
QScriptValue::Undeletable |
|
|
|
|
QScriptValue::SkipInEnumeration);
|
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);
|
|
|
|
if (env) {
|
|
|
|
return env->checkForErrors(false);
|
|
|
|
}
|
|
|
|
|
2010-08-06 01:00:45 +02:00
|
|
|
return true;
|
2010-08-04 05:53:41 +02:00
|
|
|
}
|
|
|
|
|
2010-08-05 22:08:26 +02:00
|
|
|
/*
|
|
|
|
QScriptValue ScriptEnv::addonFilePath(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
const QString &path = context->thisObject().property("__plasma_addon_filepath").toString();
|
|
|
|
return path + context->argument(0).toString();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
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()) {
|
2010-08-05 22:08:26 +02:00
|
|
|
/*
|
|
|
|
QScriptValue v = func.prototype();
|
|
|
|
v.setProperty("addonFilePath", engine->newFunction(ScriptEnv::addonFilePath));
|
|
|
|
func.setPrototype(v);
|
|
|
|
func.setProperty("test", "bar");
|
|
|
|
*/
|
2010-08-04 05:53:41 +02:00
|
|
|
QScriptValue obj = func.construct();
|
2010-09-03 23:00:25 +02:00
|
|
|
obj.setProperty("__plasma_addon_package",
|
|
|
|
context->activationObject().property("__plasma_addon_package"),
|
|
|
|
QScriptValue::ReadOnly|QScriptValue::Undeletable|
|
|
|
|
QScriptValue::SkipInEnumeration);
|
2010-08-05 22:08:26 +02:00
|
|
|
/*
|
|
|
|
obj.setProperty("test", "bar");
|
|
|
|
obj.setProperty("addonFilePath", engine->newFunction(ScriptEnv::addonFilePath));
|
|
|
|
*/
|
|
|
|
|
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-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)
|
|
|
|
{
|
|
|
|
if (!m_eventListeners.contains(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValueList funcs = m_eventListeners.value(event);
|
|
|
|
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
|