2010-03-17 19:00:07 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Aaron 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 as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 "scriptengine.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
2010-03-19 19:07:53 +01:00
|
|
|
#include <QFileInfo>
|
2010-03-17 19:00:07 +01:00
|
|
|
#include <QScriptValueIterator>
|
|
|
|
|
2010-11-10 17:03:55 +01:00
|
|
|
#include <KDebug>
|
2010-04-27 22:55:51 +02:00
|
|
|
#include <kdeversion.h>
|
2011-01-12 02:46:00 +01:00
|
|
|
#include <KMimeTypeTrader>
|
2010-04-09 03:03:05 +02:00
|
|
|
#include <KServiceTypeTrader>
|
2010-03-17 19:00:07 +01:00
|
|
|
#include <KShell>
|
2010-03-19 19:07:53 +01:00
|
|
|
#include <KStandardDirs>
|
2010-03-17 19:00:07 +01:00
|
|
|
|
2011-01-12 02:46:00 +01:00
|
|
|
// KIO
|
|
|
|
#include <kemailsettings.h> // no camelcase include
|
|
|
|
|
2010-03-17 19:00:07 +01:00
|
|
|
#include <Plasma/Applet>
|
|
|
|
#include <Plasma/Containment>
|
|
|
|
#include <Plasma/Corona>
|
2010-04-09 03:03:05 +02:00
|
|
|
#include <Plasma/Package>
|
2010-03-17 19:00:07 +01:00
|
|
|
|
|
|
|
#include "appinterface.h"
|
|
|
|
#include "containment.h"
|
|
|
|
#include "widget.h"
|
2010-04-09 03:03:05 +02:00
|
|
|
#include "layouttemplatepackagestructure.h"
|
2010-03-17 19:00:07 +01:00
|
|
|
|
|
|
|
QScriptValue constructQRectFClass(QScriptEngine *engine);
|
|
|
|
|
2010-05-19 19:13:29 +02:00
|
|
|
namespace WorkspaceScripting
|
|
|
|
{
|
|
|
|
|
2010-03-17 19:00:07 +01:00
|
|
|
ScriptEngine::ScriptEngine(Plasma::Corona *corona, QObject *parent)
|
|
|
|
: QScriptEngine(parent),
|
|
|
|
m_corona(corona)
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_corona);
|
2010-11-18 02:16:57 +01:00
|
|
|
AppInterface *interface = new AppInterface(this);
|
2010-03-17 19:00:07 +01:00
|
|
|
connect(interface, SIGNAL(print(QString)), this, SIGNAL(print(QString)));
|
|
|
|
m_scriptSelf = newQObject(interface, QScriptEngine::QtOwnership,
|
2011-01-12 02:46:00 +01:00
|
|
|
QScriptEngine::ExcludeSuperClassProperties |
|
|
|
|
QScriptEngine::ExcludeSuperClassMethods);
|
2010-03-17 19:00:07 +01:00
|
|
|
setupEngine();
|
|
|
|
connect(this, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(exception(QScriptValue)));
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEngine::~ScriptEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::activityById(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("activityById requires an id"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint id = context->argument(0).toInt32();
|
|
|
|
ScriptEngine *env = envFor(engine);
|
|
|
|
foreach (Plasma::Containment *c, env->m_corona->containments()) {
|
|
|
|
if (c->id() == id && !isPanel(c)) {
|
2010-04-27 21:41:40 +02:00
|
|
|
return env->wrap(c);
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::activityForScreen(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("activityForScreen requires a screen id"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint screen = context->argument(0).toInt32();
|
|
|
|
const uint desktop = context->argumentCount() > 1 ? context->argument(1).toInt32() : -1;
|
|
|
|
ScriptEngine *env = envFor(engine);
|
2010-04-27 21:41:40 +02:00
|
|
|
return env->wrap(env->m_corona->containmentForScreen(screen, desktop));
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::newActivity(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return createContainment("desktop", "desktop", context, engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::newPanel(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return createContainment("panel", "panel", context, engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::createContainment(const QString &type, const QString &defaultPlugin,
|
|
|
|
QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QString plugin = context->argumentCount() > 0 ? context->argument(0).toString() :
|
|
|
|
defaultPlugin;
|
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
const KPluginInfo::List list = Plasma::Containment::listContainmentsOfType(type);
|
|
|
|
foreach (const KPluginInfo &info, list) {
|
|
|
|
if (info.pluginName() == plugin) {
|
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
return context->throwError(i18n("Could not find a plugin for %1 named %2.", type, plugin));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ScriptEngine *env = envFor(engine);
|
|
|
|
Plasma::Containment *c = env->m_corona->addContainment(plugin);
|
|
|
|
if (c) {
|
|
|
|
if (type == "panel") {
|
|
|
|
// some defaults
|
|
|
|
c->setScreen(0);
|
|
|
|
c->setLocation(Plasma::TopEdge);
|
|
|
|
}
|
|
|
|
c->updateConstraints(Plasma::AllConstraints | Plasma::StartupCompletedConstraint);
|
|
|
|
c->flushPendingConstraintsEvents();
|
|
|
|
emit env->createPendingPanelViews();
|
|
|
|
}
|
|
|
|
|
2010-04-27 21:41:40 +02:00
|
|
|
return env->wrap(c);
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
2010-04-27 21:41:40 +02:00
|
|
|
QScriptValue ScriptEngine::wrap(Plasma::Applet *w)
|
2010-03-17 19:00:07 +01:00
|
|
|
{
|
|
|
|
Widget *wrapper = new Widget(w);
|
2010-04-27 21:41:40 +02:00
|
|
|
QScriptValue v = newQObject(wrapper, QScriptEngine::ScriptOwnership,
|
|
|
|
QScriptEngine::ExcludeSuperClassProperties |
|
|
|
|
QScriptEngine::ExcludeSuperClassMethods);
|
2010-03-17 19:00:07 +01:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2010-04-27 21:41:40 +02:00
|
|
|
QScriptValue ScriptEngine::wrap(Plasma::Containment *c)
|
2010-03-17 19:00:07 +01:00
|
|
|
{
|
|
|
|
Containment *wrapper = new Containment(c);
|
2010-04-27 21:41:40 +02:00
|
|
|
return wrap(wrapper);
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
2010-04-27 21:41:40 +02:00
|
|
|
QScriptValue ScriptEngine::wrap(Containment *c)
|
2010-03-17 19:00:07 +01:00
|
|
|
{
|
2010-04-27 21:41:40 +02:00
|
|
|
QScriptValue v = newQObject(c, QScriptEngine::ScriptOwnership,
|
|
|
|
QScriptEngine::ExcludeSuperClassProperties |
|
|
|
|
QScriptEngine::ExcludeSuperClassMethods);
|
|
|
|
v.setProperty("widgetById", newFunction(Containment::widgetById));
|
|
|
|
v.setProperty("addWidget", newFunction(Containment::addWidget));
|
2010-04-27 22:07:08 +02:00
|
|
|
v.setProperty("widgets", newFunction(Containment::widgets));
|
2010-03-17 19:00:07 +01:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEngine *ScriptEngine::envFor(QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QObject *object = engine->globalObject().toQObject();
|
|
|
|
Q_ASSERT(object);
|
|
|
|
|
|
|
|
AppInterface *interface = qobject_cast<AppInterface *>(object);
|
|
|
|
Q_ASSERT(interface);
|
|
|
|
|
|
|
|
ScriptEngine *env = qobject_cast<ScriptEngine *>(interface->parent());
|
|
|
|
Q_ASSERT(env);
|
|
|
|
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::panelById(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("activityById requires an id"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint id = context->argument(0).toInt32();
|
|
|
|
ScriptEngine *env = envFor(engine);
|
|
|
|
foreach (Plasma::Containment *c, env->m_corona->containments()) {
|
|
|
|
if (c->id() == id && isPanel(c)) {
|
2010-04-27 21:41:40 +02:00
|
|
|
return env->wrap(c);
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2010-04-27 21:44:53 +02:00
|
|
|
QScriptValue ScriptEngine::panels(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(context)
|
|
|
|
|
|
|
|
QScriptValue panels = engine->newArray();
|
|
|
|
ScriptEngine *env = envFor(engine);
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
foreach (Plasma::Containment *c, env->m_corona->containments()) {
|
|
|
|
if (isPanel(c)) {
|
|
|
|
panels.setProperty(count, env->wrap(c));
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panels.setProperty("length", count);
|
|
|
|
return panels;
|
|
|
|
}
|
|
|
|
|
2010-03-17 19:00:07 +01:00
|
|
|
QScriptValue ScriptEngine::fileExists(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString path = context->argument(0).toString();
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile f(KShell::tildeExpand(path));
|
|
|
|
return f.exists();
|
|
|
|
}
|
|
|
|
|
2010-04-09 03:03:05 +02:00
|
|
|
QScriptValue ScriptEngine::loadTemplate(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
|
2010-04-09 03:03:05 +02:00
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "no arguments";
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString layout = context->argument(0).toString();
|
|
|
|
if (layout.isEmpty() || layout.contains("'")) {
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "layout is empty";
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString constraint = QString("[X-Plasma-Shell] == '%1' and [X-KDE-PluginInfo-Name] == '%2'")
|
|
|
|
.arg(KGlobal::mainComponent().componentName(),layout);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/LayoutTemplate", constraint);
|
|
|
|
|
|
|
|
if (offers.isEmpty()) {
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "offers fail" << constraint;
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::PackageStructure::Ptr structure(new LayoutTemplatePackageStructure);
|
|
|
|
KPluginInfo info(offers.first());
|
|
|
|
const QString path = KStandardDirs::locate("data", structure->defaultPackageRoot() + '/' + info.pluginName() + '/');
|
|
|
|
if (path.isEmpty()) {
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "script path is empty";
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-25 21:31:40 +02:00
|
|
|
Plasma::Package package(path, structure);
|
|
|
|
const QString scriptFile = package.filePath("mainscript");
|
2010-04-09 03:03:05 +02:00
|
|
|
if (scriptFile.isEmpty()) {
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "scriptfile is empty";
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(scriptFile);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
kWarning() << i18n("Unable to load script file: %1", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString script = file.readAll();
|
|
|
|
if (script.isEmpty()) {
|
2010-04-25 21:31:40 +02:00
|
|
|
kDebug() << "script is empty";
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptEngine *env = envFor(engine);
|
2010-04-30 18:05:46 +02:00
|
|
|
env->globalObject().setProperty("templateName", env->newVariant(info.name()), QScriptValue::ReadOnly | QScriptValue::Undeletable);
|
|
|
|
env->globalObject().setProperty("templateComment", env->newVariant(info.comment()), QScriptValue::ReadOnly | QScriptValue::Undeletable);
|
2010-09-01 01:29:59 +02:00
|
|
|
|
|
|
|
QScriptValue rv = env->newObject();
|
|
|
|
QScriptContext *ctx = env->pushContext();
|
|
|
|
ctx->setThisObject(rv);
|
|
|
|
|
2010-04-09 03:03:05 +02:00
|
|
|
env->evaluateScript(script, path);
|
2010-09-01 01:29:59 +02:00
|
|
|
|
|
|
|
env->popContext();
|
|
|
|
return rv;
|
2010-04-09 03:03:05 +02:00
|
|
|
}
|
|
|
|
|
2011-01-12 02:46:00 +01:00
|
|
|
QScriptValue ScriptEngine::applicationExists(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString application = context->argument(0).toString();
|
|
|
|
if (application.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first, check for it in $PATH
|
|
|
|
if (!KStandardDirs::findExe(application).isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KService::serviceByStorageId(application)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (application.contains("'")) {
|
|
|
|
// apostrophes just screw up the trader lookups below, so check for it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// next, consult ksycoca for an app by that name
|
|
|
|
if (!KServiceTypeTrader::self()->query("Application", QString("Name =~ '%1'").arg(application)).isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// next, consult ksycoca for an app by that generic name
|
|
|
|
if (!KServiceTypeTrader::self()->query("Application", QString("GenericName =~ '%1'").arg(application)).isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue ScriptEngine::defaultApplication(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString application = context->argument(0).toString();
|
|
|
|
if (application.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool storageId = context->argumentCount() < 2 ? false : context->argument(1).toBool();
|
|
|
|
|
|
|
|
// FIXME: there are some pretty horrible hacks below, in the sense that they assume a very
|
|
|
|
// specific implementation system. there is much room for improvement here. see
|
|
|
|
// kdebase-runtime/kcontrol/componentchooser/ for all the gory details ;)
|
|
|
|
if (application.compare("mailer", Qt::CaseInsensitive) == 0) {
|
|
|
|
KEMailSettings settings;
|
|
|
|
|
|
|
|
// in KToolInvocation, the default is kmail; but let's be friendlier :)
|
|
|
|
QString command = settings.getSetting(KEMailSettings::ClientProgram);
|
|
|
|
if (command.isEmpty()) {
|
|
|
|
if (KService::Ptr kontact = KService::serviceByStorageId("kontact")) {
|
|
|
|
return storageId ? kontact->storageId() : kontact->exec();
|
|
|
|
} else if (KService::Ptr kmail = KService::serviceByStorageId("kmail")) {
|
|
|
|
return storageId ? kmail->storageId() : kmail->exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!command.isEmpty()) {
|
|
|
|
if (settings.getSetting(KEMailSettings::ClientTerminal) == "true") {
|
|
|
|
KConfigGroup confGroup(KGlobal::config(), "General");
|
|
|
|
const QString preferredTerminal = confGroup.readPathEntry("TerminalApplication",
|
|
|
|
QString::fromLatin1("konsole"));
|
|
|
|
command = preferredTerminal + QString::fromLatin1(" -e ") + command;
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
} else if (application.compare("browser", Qt::CaseInsensitive) == 0) {
|
|
|
|
KConfigGroup config(KGlobal::config(), "General");
|
|
|
|
QString browserApp = config.readPathEntry("BrowserApplication", QString());
|
|
|
|
if (browserApp.isEmpty()) {
|
|
|
|
const KService::Ptr htmlApp = KMimeTypeTrader::self()->preferredService(QLatin1String("text/html"));
|
|
|
|
if (htmlApp) {
|
|
|
|
browserApp = storageId ? htmlApp->storageId() : htmlApp->exec();
|
|
|
|
}
|
|
|
|
} else if (browserApp.startsWith('!')) {
|
|
|
|
browserApp = browserApp.mid(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return browserApp;
|
|
|
|
} else if (application.compare("terminal", Qt::CaseInsensitive) == 0) {
|
|
|
|
KConfigGroup confGroup(KGlobal::config(), "General");
|
|
|
|
return confGroup.readPathEntry("TerminalApplication", QString::fromLatin1("konsole"));
|
|
|
|
} else if (application.compare("filemanager", Qt::CaseInsensitive) == 0) {
|
|
|
|
KService::Ptr service = KMimeTypeTrader::self()->preferredService("inode/directory");
|
|
|
|
if (service) {
|
|
|
|
return storageId ? service->storageId() : service->exec();
|
|
|
|
}
|
|
|
|
} else if (application.compare("windowmanager", Qt::CaseInsensitive) == 0) {
|
|
|
|
KConfig cfg("ksmserverrc", KConfig::NoGlobals);
|
|
|
|
KConfigGroup confGroup(&cfg, "General");
|
|
|
|
return confGroup.readEntry("windowManager", QString::fromLatin1("konsole"));
|
|
|
|
} else if (KService::Ptr service = KMimeTypeTrader::self()->preferredService(application)) {
|
|
|
|
return storageId ? service->storageId() : service->exec();
|
|
|
|
} else {
|
|
|
|
// try the files in share/apps/kcm_componentchooser/
|
|
|
|
const QStringList services = KGlobal::dirs()->findAllResources("data","kcm_componentchooser/*.desktop", KStandardDirs::NoDuplicates);
|
|
|
|
kDebug() << "ok, trying in" << services.count();
|
|
|
|
foreach (const QString &service, services) {
|
|
|
|
KConfig config(service, KConfig::SimpleConfig);
|
|
|
|
KConfigGroup cg = config.group(QByteArray());
|
|
|
|
const QString type = cg.readEntry("valueName", QString());
|
|
|
|
kDebug() << " checking" << service << type << application;
|
|
|
|
if (type.compare(application, Qt::CaseInsensitive) == 0) {
|
|
|
|
KConfig store(cg.readPathEntry("storeInFile", "null"));
|
|
|
|
KConfigGroup storeCg(&store, cg.readEntry("valueSection", QString()));
|
|
|
|
const QString exec = storeCg.readPathEntry(cg.readEntry("valueName", "kcm_componenchooser_null"),
|
|
|
|
cg.readEntry("defaultImplementation", QString()));
|
|
|
|
if (!exec.isEmpty()) {
|
|
|
|
return exec;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-17 19:00:07 +01:00
|
|
|
void ScriptEngine::setupEngine()
|
|
|
|
{
|
|
|
|
QScriptValue v = globalObject();
|
|
|
|
QScriptValueIterator it(v);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
// we provide our own print implementation, but we want the rest
|
|
|
|
if (it.name() != "print") {
|
|
|
|
m_scriptSelf.setProperty(it.name(), it.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_scriptSelf.setProperty("QRectF", constructQRectFClass(this));
|
|
|
|
m_scriptSelf.setProperty("Activity", newFunction(ScriptEngine::newActivity));
|
|
|
|
m_scriptSelf.setProperty("Panel", newFunction(ScriptEngine::newPanel));
|
2010-04-27 21:52:56 +02:00
|
|
|
m_scriptSelf.setProperty("activities", newFunction(ScriptEngine::activities));
|
2010-03-17 19:00:07 +01:00
|
|
|
m_scriptSelf.setProperty("activityById", newFunction(ScriptEngine::activityById));
|
|
|
|
m_scriptSelf.setProperty("activityForScreen", newFunction(ScriptEngine::activityForScreen));
|
|
|
|
m_scriptSelf.setProperty("panelById", newFunction(ScriptEngine::panelById));
|
2010-04-27 21:44:53 +02:00
|
|
|
m_scriptSelf.setProperty("panels", newFunction(ScriptEngine::panels));
|
2010-03-17 19:00:07 +01:00
|
|
|
m_scriptSelf.setProperty("fileExists", newFunction(ScriptEngine::fileExists));
|
2010-04-09 03:03:05 +02:00
|
|
|
m_scriptSelf.setProperty("loadTemplate", newFunction(ScriptEngine::loadTemplate));
|
2011-01-12 02:46:00 +01:00
|
|
|
m_scriptSelf.setProperty("applicationExists", newFunction(ScriptEngine::applicationExists));
|
|
|
|
m_scriptSelf.setProperty("defaultApplication", newFunction(ScriptEngine::defaultApplication));
|
2010-03-17 19:00:07 +01:00
|
|
|
|
|
|
|
setGlobalObject(m_scriptSelf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEngine::isPanel(const Plasma::Containment *c)
|
|
|
|
{
|
2010-09-07 07:28:37 +02:00
|
|
|
if (!c) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-17 19:00:07 +01:00
|
|
|
return c->containmentType() == Plasma::Containment::PanelContainment ||
|
|
|
|
c->containmentType() == Plasma::Containment::CustomPanelContainment;
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:16:57 +01:00
|
|
|
QScriptValue ScriptEngine::activities(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(context)
|
|
|
|
|
|
|
|
QScriptValue containments = engine->newArray();
|
|
|
|
ScriptEngine *env = envFor(engine);
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
foreach (Plasma::Containment *c, env->corona()->containments()) {
|
|
|
|
if (!isPanel(c)) {
|
|
|
|
containments.setProperty(count, env->wrap(c));
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
containments.setProperty("length", count);
|
|
|
|
return containments;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::Corona *ScriptEngine::corona() const
|
|
|
|
{
|
|
|
|
return m_corona;
|
|
|
|
}
|
|
|
|
|
2010-04-09 03:03:05 +02:00
|
|
|
bool ScriptEngine::evaluateScript(const QString &script, const QString &path)
|
2010-03-17 19:00:07 +01:00
|
|
|
{
|
|
|
|
//kDebug() << "evaluating" << m_editor->toPlainText();
|
2010-04-09 03:03:05 +02:00
|
|
|
evaluate(script, path);
|
2010-03-17 19:00:07 +01:00
|
|
|
if (hasUncaughtException()) {
|
|
|
|
//kDebug() << "catch the exception!";
|
|
|
|
QString error = i18n("Error: %1 at line %2\n\nBacktrace:\n%3",
|
|
|
|
uncaughtException().toString(),
|
|
|
|
QString::number(uncaughtExceptionLineNumber()),
|
|
|
|
uncaughtExceptionBacktrace().join("\n "));
|
|
|
|
emit printError(error);
|
2010-04-09 03:03:05 +02:00
|
|
|
return false;
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
2010-04-09 03:03:05 +02:00
|
|
|
|
|
|
|
return true;
|
2010-03-17 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptEngine::exception(const QScriptValue &value)
|
|
|
|
{
|
|
|
|
//kDebug() << "exception caught!" << value.toVariant();
|
|
|
|
emit printError(value.toVariant().toString());
|
|
|
|
}
|
|
|
|
|
2010-03-19 19:07:53 +01:00
|
|
|
QStringList ScriptEngine::pendingUpdateScripts()
|
|
|
|
{
|
|
|
|
const QString appName = KGlobal::activeComponent().aboutData()->appName();
|
|
|
|
QStringList scripts = KGlobal::dirs()->findAllResources("data", appName + "/updates/*.js");
|
|
|
|
QStringList scriptPaths;
|
|
|
|
|
|
|
|
if (scripts.isEmpty()) {
|
|
|
|
//kDebug() << "no update scripts";
|
|
|
|
return scriptPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
KConfigGroup cg(KGlobal::config(), "Updates");
|
|
|
|
QStringList performed = cg.readEntry("performed", QStringList());
|
|
|
|
const QString localDir = KGlobal::dirs()->localkdedir();
|
|
|
|
const QString localXdgDir = KGlobal::dirs()->localxdgdatadir();
|
|
|
|
|
|
|
|
foreach (const QString &script, scripts) {
|
|
|
|
if (performed.contains(script)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script.startsWith(localDir) || script.startsWith(localXdgDir)) {
|
|
|
|
kDebug() << "skipping user local script: " << script;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptPaths.append(script);
|
|
|
|
performed.append(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
cg.writeEntry("performed", performed);
|
|
|
|
KGlobal::config()->sync();
|
|
|
|
return scriptPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ScriptEngine::defaultLayoutScripts()
|
|
|
|
{
|
|
|
|
const QString appName = KGlobal::activeComponent().aboutData()->appName();
|
|
|
|
QStringList scripts = KGlobal::dirs()->findAllResources("data", appName + "/init/*.js");
|
|
|
|
QStringList scriptPaths;
|
|
|
|
|
|
|
|
if (scripts.isEmpty()) {
|
|
|
|
//kDebug() << "no javascript based layouts";
|
|
|
|
return scriptPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString localDir = KGlobal::dirs()->localkdedir();
|
|
|
|
const QString localXdgDir = KGlobal::dirs()->localxdgdatadir();
|
|
|
|
|
|
|
|
QSet<QString> scriptNames;
|
|
|
|
foreach (const QString &script, scripts) {
|
|
|
|
if (script.startsWith(localDir) || script.startsWith(localXdgDir)) {
|
|
|
|
kDebug() << "skipping user local script: " << script;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo f(script);
|
|
|
|
QString filename = f.fileName();
|
|
|
|
if (!scriptNames.contains(filename)) {
|
|
|
|
scriptNames.insert(filename);
|
|
|
|
scriptPaths.append(script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scriptPaths;
|
|
|
|
}
|
|
|
|
|
2010-05-19 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
2010-03-17 19:00:07 +01:00
|
|
|
#include "scriptengine.moc"
|
|
|
|
|