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>
|
|
|
|
#include <KIO/Job>
|
|
|
|
#include <KLocale>
|
|
|
|
#include <KMimeType>
|
|
|
|
#include <KService>
|
|
|
|
#include <KShell>
|
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KRun>
|
|
|
|
|
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-02-22 12:33:21 +01:00
|
|
|
Q_DECLARE_METATYPE(ScriptEnv*)
|
|
|
|
|
|
|
|
ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
|
|
|
|
: QObject(parent),
|
|
|
|
m_allowedUrls(NoUrls),
|
|
|
|
m_engine(engine)
|
2010-01-07 03:04:59 +01:00
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
QScriptValue global = m_engine->globalObject();
|
|
|
|
|
|
|
|
// Add utility functions
|
|
|
|
global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
|
|
|
|
global.setProperty("debug", m_engine->newFunction(ScriptEnv::debug));
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
QScriptValue::ReadOnly|QScriptValue::Undeletable|QScriptValue::SkipInEnumeration);
|
2010-01-07 03:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEnv::~ScriptEnv()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
QScriptEngine *ScriptEnv::engine() const
|
|
|
|
{
|
|
|
|
return m_engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEnv *ScriptEnv::findScriptEnv( QScriptEngine *engine )
|
|
|
|
{
|
|
|
|
QScriptValue global = engine->globalObject();
|
|
|
|
return qscriptvalue_cast<ScriptEnv*>(global.property("__plasma_scriptenv"));
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
void ScriptEnv::registerEnums(QScriptValue &scriptValue, const QMetaObject &meta)
|
|
|
|
{
|
|
|
|
//manually create enum values. ugh
|
|
|
|
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-02-22 12:33:21 +01:00
|
|
|
scriptValue.setProperty(e.key(i), QScriptValue(m_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-02-22 12:33:21 +01:00
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-01-07 03:04:59 +01:00
|
|
|
emit reportError(this, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
KService::Ptr service = KService::serviceByStorageId(app);
|
|
|
|
if (service) {
|
|
|
|
return KRun::run(*service, urls, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString exec = KGlobal::dirs()->findExe(app);
|
|
|
|
if (!exec.isEmpty()) {
|
|
|
|
return KRun::run(exec, urls, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
kDebug() << "findScriptEnv failed";
|
|
|
|
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();
|
|
|
|
kDebug() << "required extensions are" << requiredExtensions;
|
|
|
|
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-02-22 12:33:21 +01:00
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-01-07 03:04:59 +01:00
|
|
|
emit reportError(this, true);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
m_extensions << extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList optionalExtensions = info.service()->property("X-Plasma-OptionalExtensions", QVariant::StringList).toStringList();
|
|
|
|
kDebug() << "optional extensions are" << optionalExtensions;
|
|
|
|
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-02-22 12:33:21 +01:00
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-01-07 03:04:59 +01:00
|
|
|
emit reportError(this, false);
|
|
|
|
} else {
|
|
|
|
m_extensions << extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet<QString> ScriptEnv::loadedExtensions() const
|
|
|
|
{
|
|
|
|
return m_extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::debug(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("debug takes one argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << context->argument(0).toString();
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEnv::print(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("print() takes one argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << context->argument(0).toString().toLocal8Bit().constData() << std::endl;
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-01-10 14:03:53 +01:00
|
|
|
#ifndef USEGUI
|
2010-01-07 03:04:59 +01:00
|
|
|
#include "scriptenv.moc"
|
2010-01-10 14:03:53 +01:00
|
|
|
#endif
|