2010-09-21 14:31:40 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2010 Rohan Prabhu <rohan@rohanprabhu.com>
|
2011-12-23 10:52:06 +00:00
|
|
|
Copyright (C) 2011 Martin Gräßlin <mgraesslin@kde.org>
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, 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 General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "scripting.h"
|
2011-12-23 10:52:06 +00:00
|
|
|
// own
|
|
|
|
#include "meta.h"
|
2012-03-25 07:59:01 +00:00
|
|
|
#include "scriptingutils.h"
|
2012-01-22 11:38:03 +00:00
|
|
|
#include "workspace_wrapper.h"
|
2012-03-24 09:49:21 +00:00
|
|
|
#include "../client.h"
|
2012-02-19 14:43:24 +00:00
|
|
|
#include "../thumbnailitem.h"
|
2012-03-01 09:18:39 +00:00
|
|
|
#include "../options.h"
|
2012-03-01 10:29:24 +00:00
|
|
|
#include "../workspace.h"
|
2011-12-23 10:52:06 +00:00
|
|
|
// KDE
|
2011-01-01 09:37:08 +00:00
|
|
|
#include <kstandarddirs.h>
|
2012-02-18 09:34:27 +00:00
|
|
|
#include <KDE/KConfigGroup>
|
2012-01-21 09:04:47 +00:00
|
|
|
#include <KDE/KDebug>
|
2012-02-18 09:34:27 +00:00
|
|
|
#include <KDE/KPluginInfo>
|
|
|
|
#include <KDE/KServiceTypeTrader>
|
2012-02-19 14:43:24 +00:00
|
|
|
#include <kdeclarative.h>
|
2011-12-23 10:52:06 +00:00
|
|
|
// Qt
|
|
|
|
#include <QtDBus/QDBusConnection>
|
2012-04-28 07:55:11 +00:00
|
|
|
#include <QtCore/QFutureWatcher>
|
2011-12-23 10:52:06 +00:00
|
|
|
#include <QtCore/QSettings>
|
2012-04-28 07:55:11 +00:00
|
|
|
#include <QtCore/QtConcurrentRun>
|
2012-02-19 14:43:24 +00:00
|
|
|
#include <QtDeclarative/QDeclarativeContext>
|
|
|
|
#include <QtDeclarative/QDeclarativeEngine>
|
|
|
|
#include <QtDeclarative/QDeclarativeView>
|
|
|
|
#include <QtDeclarative/qdeclarative.h>
|
2011-12-23 10:52:06 +00:00
|
|
|
#include <QtScript/QScriptEngine>
|
|
|
|
#include <QtScript/QScriptValue>
|
|
|
|
|
|
|
|
QScriptValue kwinScriptPrint(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2012-03-01 10:28:27 +00:00
|
|
|
KWin::AbstractScript *script = qobject_cast<KWin::Script*>(context->callee().data().toQObject());
|
|
|
|
if (!script) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
QString result;
|
|
|
|
for (int i = 0; i < context->argumentCount(); ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
result.append(" ");
|
|
|
|
}
|
|
|
|
result.append(context->argument(i).toString());
|
|
|
|
}
|
|
|
|
script->printMessage(result);
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
QScriptValue kwinScriptReadConfig(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
KWin::AbstractScript *script = qobject_cast<KWin::AbstractScript*>(context->callee().data().toQObject());
|
2012-03-01 10:28:27 +00:00
|
|
|
if (!script) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2012-02-25 10:30:45 +00:00
|
|
|
if (context->argumentCount() < 1 || context->argumentCount() > 2) {
|
|
|
|
kDebug(1212) << "Incorrect number of arguments";
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
const QString key = context->argument(0).toString();
|
|
|
|
QVariant defaultValue;
|
|
|
|
if (context->argumentCount() == 2) {
|
|
|
|
defaultValue = context->argument(1).toVariant();
|
|
|
|
}
|
|
|
|
return engine->newVariant(script->config().readEntry(key, defaultValue));
|
|
|
|
}
|
|
|
|
|
2012-03-25 07:59:01 +00:00
|
|
|
QScriptValue kwinScriptGlobalShortcut(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::globalShortcut<KWin::AbstractScript*>(context, engine);
|
|
|
|
}
|
|
|
|
|
2012-05-06 10:06:10 +00:00
|
|
|
QScriptValue kwinAssertTrue(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::scriptingAssert<bool>(context, engine, 1, 2, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertFalse(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::scriptingAssert<bool>(context, engine, 1, 2, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertEquals(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::scriptingAssert<QVariant>(context, engine, 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertNull(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (!KWin::validateParameters(context, 1, 2)) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
if (!context->argument(0).isNull()) {
|
|
|
|
if (context->argumentCount() == 2) {
|
|
|
|
context->throwError(QScriptContext::UnknownError, context->argument(1).toString());
|
|
|
|
} else {
|
|
|
|
context->throwError(QScriptContext::UnknownError,
|
|
|
|
i18nc("Assertion failed in KWin script with given value",
|
|
|
|
"Assertion failed: %1 is not null", context->argument(0).toString()));
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertNotNull(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (!KWin::validateParameters(context, 1, 2)) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
if (context->argument(0).isNull()) {
|
|
|
|
if (context->argumentCount() == 2) {
|
|
|
|
context->throwError(QScriptContext::UnknownError, context->argument(1).toString());
|
|
|
|
} else {
|
|
|
|
context->throwError(QScriptContext::UnknownError,
|
|
|
|
i18nc("Assertion failed in KWin script",
|
|
|
|
"Assertion failed: argument is null"));
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:09:36 +00:00
|
|
|
QScriptValue kwinRegisterScreenEdge(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::registerScreenEdge<KWin::AbstractScript*>(context, engine);
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::AbstractScript::AbstractScript(int id, QString scriptName, QString pluginName, QObject *parent)
|
2011-12-23 07:49:28 +00:00
|
|
|
: QObject(parent)
|
2012-02-19 14:43:24 +00:00
|
|
|
, m_scriptId(id)
|
2012-02-25 10:30:45 +00:00
|
|
|
, m_pluginName(pluginName)
|
2011-12-23 10:52:06 +00:00
|
|
|
, m_running(false)
|
2012-02-19 14:43:24 +00:00
|
|
|
, m_workspace(new WorkspaceWrapper(this))
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-02-18 12:58:00 +00:00
|
|
|
m_scriptFile.setFileName(scriptName);
|
2012-02-25 10:30:45 +00:00
|
|
|
if (m_pluginName.isNull()) {
|
|
|
|
m_pluginName = scriptName;
|
|
|
|
}
|
2012-05-10 14:09:36 +00:00
|
|
|
#ifdef KWIN_BUILD_SCREENEDGES
|
|
|
|
connect(KWin::Workspace::self()->screenEdge(), SIGNAL(activated(ElectricBorder)), SLOT(borderActivated(ElectricBorder)));
|
|
|
|
#endif
|
2012-02-19 14:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KWin::AbstractScript::~AbstractScript()
|
|
|
|
{
|
2012-05-10 14:09:36 +00:00
|
|
|
#ifdef KWIN_BUILD_SCREENEDGES
|
|
|
|
for (QHash<int, QList<QScriptValue> >::const_iterator it = m_screenEdgeCallbacks.constBegin();
|
|
|
|
it != m_screenEdgeCallbacks.constEnd();
|
|
|
|
++it) {
|
|
|
|
KWin::Workspace::self()->screenEdge()->unreserve(static_cast<KWin::ElectricBorder>(it.key()));
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-19 14:43:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KConfigGroup KWin::AbstractScript::config() const
|
|
|
|
{
|
|
|
|
return KGlobal::config()->group("Script-" + m_pluginName);
|
|
|
|
}
|
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
void KWin::AbstractScript::stop()
|
|
|
|
{
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
2012-03-01 10:28:27 +00:00
|
|
|
void KWin::AbstractScript::printMessage(const QString &message)
|
|
|
|
{
|
|
|
|
kDebug(1212) << scriptFile().fileName() << ":" << message;
|
|
|
|
emit print(message);
|
|
|
|
}
|
|
|
|
|
2012-03-25 07:59:01 +00:00
|
|
|
void KWin::AbstractScript::registerShortcut(QAction *a, QScriptValue callback)
|
|
|
|
{
|
|
|
|
m_shortcutCallbacks.insert(a, callback);
|
|
|
|
connect(a, SIGNAL(triggered(bool)), SLOT(globalShortcutTriggered()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::AbstractScript::globalShortcutTriggered()
|
|
|
|
{
|
|
|
|
callGlobalShortcutCallback<KWin::AbstractScript*>(this, sender());
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:09:36 +00:00
|
|
|
void KWin::AbstractScript::borderActivated(KWin::ElectricBorder edge)
|
|
|
|
{
|
|
|
|
screenEdgeActivated(this, edge);
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
void KWin::AbstractScript::installScriptFunctions(QScriptEngine* engine)
|
|
|
|
{
|
|
|
|
// add our print
|
|
|
|
QScriptValue printFunc = engine->newFunction(kwinScriptPrint);
|
|
|
|
printFunc.setData(engine->newQObject(this));
|
|
|
|
engine->globalObject().setProperty("print", printFunc);
|
|
|
|
// add read config
|
|
|
|
QScriptValue configFunc = engine->newFunction(kwinScriptReadConfig);
|
|
|
|
configFunc.setData(engine->newQObject(this));
|
|
|
|
engine->globalObject().setProperty("readConfig", configFunc);
|
2012-03-25 07:59:01 +00:00
|
|
|
// add global Shortcut
|
|
|
|
registerGlobalShortcutFunction(this, engine, kwinScriptGlobalShortcut);
|
2012-05-10 14:09:36 +00:00
|
|
|
// add screen edge
|
|
|
|
registerScreenEdgeFunction(this, engine, kwinRegisterScreenEdge);
|
2012-05-06 10:06:10 +00:00
|
|
|
// add assertions
|
|
|
|
QScriptValue assertTrueFunc = engine->newFunction(kwinAssertTrue);
|
|
|
|
engine->globalObject().setProperty("assertTrue", assertTrueFunc);
|
|
|
|
engine->globalObject().setProperty("assert", assertTrueFunc);
|
|
|
|
QScriptValue assertFalseFunc = engine->newFunction(kwinAssertFalse);
|
|
|
|
engine->globalObject().setProperty("assertFalse", assertFalseFunc);
|
|
|
|
QScriptValue assertEqualsFunc = engine->newFunction(kwinAssertEquals);
|
|
|
|
engine->globalObject().setProperty("assertEquals", assertEqualsFunc);
|
|
|
|
QScriptValue assertNullFunc = engine->newFunction(kwinAssertNull);
|
|
|
|
engine->globalObject().setProperty("assertNull", assertNullFunc);
|
|
|
|
engine->globalObject().setProperty("assertEquals", assertEqualsFunc);
|
|
|
|
QScriptValue assertNotNullFunc = engine->newFunction(kwinAssertNotNull);
|
|
|
|
engine->globalObject().setProperty("assertNotNull", assertNotNullFunc);
|
2012-04-19 20:12:06 +00:00
|
|
|
// global properties
|
|
|
|
engine->globalObject().setProperty("KWin", engine->newQMetaObject(&WorkspaceWrapper::staticMetaObject));
|
|
|
|
QScriptValue workspace = engine->newQObject(AbstractScript::workspace(), QScriptEngine::QtOwnership,
|
|
|
|
QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeDeleteLater);
|
|
|
|
engine->globalObject().setProperty("workspace", workspace, QScriptValue::Undeletable);
|
|
|
|
// install meta functions
|
|
|
|
KWin::MetaScripting::registration(engine);
|
2012-02-25 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KWin::Script::Script(int id, QString scriptName, QString pluginName, QObject* parent)
|
|
|
|
: AbstractScript(id, scriptName, pluginName, parent)
|
2012-02-19 14:43:24 +00:00
|
|
|
, m_engine(new QScriptEngine(this))
|
2012-04-29 17:30:12 +00:00
|
|
|
, m_starting(false)
|
2012-05-06 13:26:41 +00:00
|
|
|
, m_agent(new ScriptUnloaderAgent(this))
|
2012-02-19 14:43:24 +00:00
|
|
|
{
|
|
|
|
QDBusConnection::sessionBus().registerObject('/' + QString::number(scriptId()), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
KWin::Script::~Script()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-02-19 14:43:24 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject('/' + QString::number(scriptId()));
|
2011-12-23 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Script::run()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-29 17:30:12 +00:00
|
|
|
if (running() || m_starting) {
|
2011-12-23 10:52:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-04-29 17:30:12 +00:00
|
|
|
m_starting = true;
|
|
|
|
QFutureWatcher<QByteArray> *watcher = new QFutureWatcher<QByteArray>(this);
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(slotScriptLoadedFromFile()));
|
|
|
|
watcher->setFuture(QtConcurrent::run(this, &KWin::Script::loadScriptFromFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray KWin::Script::loadScriptFromFile()
|
|
|
|
{
|
|
|
|
if (!scriptFile().open(QIODevice::ReadOnly)) {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
QByteArray result(scriptFile().readAll());
|
|
|
|
scriptFile().close();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Script::slotScriptLoadedFromFile()
|
|
|
|
{
|
|
|
|
QFutureWatcher<QByteArray> *watcher = dynamic_cast< QFutureWatcher< QByteArray>* >(sender());
|
|
|
|
if (!watcher) {
|
|
|
|
// not invoked from a QFutureWatcher
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (watcher->result().isNull()) {
|
|
|
|
// do not load empty script
|
|
|
|
deleteLater();
|
|
|
|
watcher->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QScriptValue optionsValue = m_engine->newQObject(options, QScriptEngine::QtOwnership,
|
|
|
|
QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeDeleteLater);
|
|
|
|
m_engine->globalObject().setProperty("options", optionsValue, QScriptValue::Undeletable);
|
|
|
|
m_engine->globalObject().setProperty("QTimer", constructTimerClass(m_engine));
|
|
|
|
QObject::connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue)));
|
|
|
|
KWin::MetaScripting::supplyConfig(m_engine);
|
|
|
|
installScriptFunctions(m_engine);
|
|
|
|
|
|
|
|
QScriptValue ret = m_engine->evaluate(watcher->result());
|
|
|
|
|
|
|
|
if (ret.isError()) {
|
|
|
|
sigException(ret);
|
|
|
|
deleteLater();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
2012-04-29 17:30:12 +00:00
|
|
|
|
|
|
|
watcher->deleteLater();
|
2012-02-19 14:43:24 +00:00
|
|
|
setRunning(true);
|
2012-04-29 17:30:12 +00:00
|
|
|
m_starting = false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Script::sigException(const QScriptValue& exception)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue ret = exception;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (ret.isError()) {
|
2011-12-23 07:49:28 +00:00
|
|
|
kDebug(1212) << "defaultscript encountered an error at [Line " << m_engine->uncaughtExceptionLineNumber() << "]";
|
2011-01-30 14:34:42 +00:00
|
|
|
kDebug(1212) << "Message: " << ret.toString();
|
|
|
|
kDebug(1212) << "-----------------";
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
QScriptValueIterator iter(ret);
|
2011-01-30 14:34:42 +00:00
|
|
|
while (iter.hasNext()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
iter.next();
|
2011-01-30 14:34:42 +00:00
|
|
|
qDebug() << " " << iter.name() << ": " << iter.value().toString();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
emit printError(exception.toString());
|
2012-05-06 13:26:41 +00:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
KWin::ScriptUnloaderAgent::ScriptUnloaderAgent(KWin::Script *script)
|
|
|
|
: QScriptEngineAgent(script->engine())
|
|
|
|
, m_script(script)
|
|
|
|
{
|
|
|
|
script->engine()->setAgent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::ScriptUnloaderAgent::scriptUnload(qint64 id)
|
|
|
|
{
|
2012-05-10 19:03:35 +00:00
|
|
|
Q_UNUSED(id)
|
2012-05-06 13:26:41 +00:00
|
|
|
m_script->stop();
|
2011-12-23 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::DeclarativeScript::DeclarativeScript(int id, QString scriptName, QString pluginName, QObject* parent)
|
|
|
|
: AbstractScript(id, scriptName, pluginName, parent)
|
2012-02-19 14:43:24 +00:00
|
|
|
, m_view(new QDeclarativeView())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KWin::DeclarativeScript::~DeclarativeScript()
|
2011-12-23 10:52:06 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
void KWin::DeclarativeScript::run()
|
|
|
|
{
|
|
|
|
if (running()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_view->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
m_view->setWindowFlags(Qt::X11BypassWindowManagerHint);
|
|
|
|
m_view->setResizeMode(QDeclarativeView::SizeViewToRootObject);
|
|
|
|
QPalette pal = m_view->palette();
|
|
|
|
pal.setColor(m_view->backgroundRole(), Qt::transparent);
|
|
|
|
m_view->setPalette(pal);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (const QString &importPath, KGlobal::dirs()->findDirs("module", "imports")) {
|
|
|
|
m_view->engine()->addImportPath(importPath);
|
|
|
|
}
|
2012-02-25 10:30:45 +00:00
|
|
|
|
|
|
|
// add read config
|
2012-02-19 14:43:24 +00:00
|
|
|
KDeclarative kdeclarative;
|
|
|
|
kdeclarative.setDeclarativeEngine(m_view->engine());
|
|
|
|
kdeclarative.initialize();
|
|
|
|
kdeclarative.setupBindings();
|
2012-02-25 10:30:45 +00:00
|
|
|
installScriptFunctions(kdeclarative.scriptEngine());
|
2012-02-19 14:43:24 +00:00
|
|
|
qmlRegisterType<ThumbnailItem>("org.kde.kwin", 0, 1, "ThumbnailItem");
|
2012-03-24 09:49:21 +00:00
|
|
|
qmlRegisterType<KWin::Client>();
|
2012-02-19 14:43:24 +00:00
|
|
|
|
2012-03-01 09:18:39 +00:00
|
|
|
m_view->rootContext()->setContextProperty("options", options);
|
2012-02-19 14:43:24 +00:00
|
|
|
|
|
|
|
m_view->setSource(QUrl::fromLocalFile(scriptFile().fileName()));
|
|
|
|
setRunning(true);
|
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2011-12-23 07:56:25 +00:00
|
|
|
KWin::Scripting::Scripting(QObject *parent)
|
|
|
|
: QObject(parent)
|
2012-04-28 07:55:11 +00:00
|
|
|
, m_scriptsLock(new QMutex(QMutex::Recursive))
|
2011-12-23 07:49:28 +00:00
|
|
|
{
|
2011-12-23 10:52:06 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject("/Scripting", this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables);
|
|
|
|
QDBusConnection::sessionBus().registerService("org.kde.kwin.Scripting");
|
2012-03-01 10:29:24 +00:00
|
|
|
connect(Workspace::self(), SIGNAL(configChanged()), SLOT(start()));
|
2012-04-13 09:27:50 +00:00
|
|
|
connect(Workspace::self(), SIGNAL(workspaceInitialized()), SLOT(start()));
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Scripting::start()
|
2012-04-28 07:55:11 +00:00
|
|
|
{
|
|
|
|
// perform querying for the services in a thread
|
|
|
|
QFutureWatcher<LoadScriptList> *watcher = new QFutureWatcher<LoadScriptList>(this);
|
|
|
|
connect(watcher, SIGNAL(finished()), this, SLOT(slotScriptsQueried()));
|
2012-02-18 09:34:27 +00:00
|
|
|
KSharedConfig::Ptr _config = KGlobal::config();
|
2012-06-02 07:33:25 +00:00
|
|
|
QMap<QString,QString> pluginStates = KConfigGroup(_config, "Plugins").entryMap();
|
|
|
|
watcher->setFuture(QtConcurrent::run(this, &KWin::Scripting::queryScriptsToLoad, pluginStates));
|
2012-05-05 15:36:30 +00:00
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2012-06-02 07:33:25 +00:00
|
|
|
LoadScriptList KWin::Scripting::queryScriptsToLoad(QMap<QString,QString> &pluginStates)
|
2012-05-05 15:36:30 +00:00
|
|
|
{
|
2012-02-18 09:34:27 +00:00
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("KWin/Script");
|
2012-04-28 07:55:11 +00:00
|
|
|
LoadScriptList scriptsToLoad;
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2012-02-18 09:34:27 +00:00
|
|
|
foreach (const KService::Ptr & service, offers) {
|
|
|
|
KPluginInfo plugininfo(service);
|
2012-06-02 07:33:25 +00:00
|
|
|
const QString value = pluginStates.value(plugininfo.pluginName() + QString::fromLatin1("Enabled"), QString());
|
|
|
|
plugininfo.setPluginEnabled(value.isNull() ? plugininfo.isPluginEnabledByDefault() : QVariant(value).toBool());
|
2012-02-19 14:43:24 +00:00
|
|
|
const bool javaScript = service->property("X-Plasma-API").toString() == "javascript";
|
|
|
|
const bool declarativeScript = service->property("X-Plasma-API").toString() == "declarativescript";
|
|
|
|
if (!javaScript && !declarativeScript) {
|
2012-02-18 09:34:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2012-02-18 09:34:27 +00:00
|
|
|
if (!plugininfo.isPluginEnabled()) {
|
2012-03-01 10:29:24 +00:00
|
|
|
if (isScriptLoaded(plugininfo.pluginName())) {
|
|
|
|
// unload the script
|
|
|
|
unloadScript(plugininfo.pluginName());
|
|
|
|
}
|
2012-02-18 09:34:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const QString pluginName = service->property("X-KDE-PluginInfo-Name").toString();
|
|
|
|
const QString scriptName = service->property("X-Plasma-MainScript").toString();
|
2012-03-16 07:37:09 +00:00
|
|
|
const QString file = KStandardDirs::locate("data", QLatin1String(KWIN_NAME) + "/scripts/" + pluginName + "/contents/" + scriptName);
|
2012-02-18 09:34:27 +00:00
|
|
|
if (file.isNull()) {
|
|
|
|
kDebug(1212) << "Could not find script file for " << pluginName;
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-28 07:55:11 +00:00
|
|
|
scriptsToLoad << qMakePair(javaScript, qMakePair(file, pluginName));
|
|
|
|
}
|
|
|
|
return scriptsToLoad;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Scripting::slotScriptsQueried()
|
|
|
|
{
|
|
|
|
QFutureWatcher<LoadScriptList> *watcher = dynamic_cast< QFutureWatcher<LoadScriptList>* >(sender());
|
|
|
|
if (!watcher) {
|
|
|
|
// slot invoked not from a FutureWatcher
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadScriptList scriptsToLoad = watcher->result();
|
|
|
|
for (LoadScriptList::const_iterator it = scriptsToLoad.constBegin();
|
|
|
|
it != scriptsToLoad.constEnd();
|
|
|
|
++it) {
|
|
|
|
if (it->first) {
|
|
|
|
loadScript(it->second.first, it->second.second);
|
|
|
|
} else {
|
|
|
|
loadDeclarativeScript(it->second.first, it->second.second);
|
2012-02-19 14:43:24 +00:00
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runScripts();
|
2012-04-28 07:55:11 +00:00
|
|
|
watcher->deleteLater();
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 10:29:24 +00:00
|
|
|
bool KWin::Scripting::isScriptLoaded(const QString &pluginName) const
|
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
foreach (AbstractScript *script, scripts) {
|
|
|
|
if (script->pluginName() == pluginName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KWin::Scripting::unloadScript(const QString &pluginName)
|
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
foreach (AbstractScript *script, scripts) {
|
|
|
|
if (script->pluginName() == pluginName) {
|
|
|
|
script->deleteLater();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Scripting::runScripts()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < scripts.size(); i++) {
|
2011-12-23 07:49:28 +00:00
|
|
|
scripts.at(i)->run();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2011-12-23 10:52:06 +00:00
|
|
|
void KWin::Scripting::scriptDestroyed(QObject *object)
|
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2011-12-23 10:52:06 +00:00
|
|
|
scripts.removeAll(static_cast<KWin::Script*>(object));
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
int KWin::Scripting::loadScript(const QString &filePath, const QString& pluginName)
|
2011-12-23 10:52:06 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
if (isScriptLoaded(pluginName)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
const int id = scripts.size();
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::Script *script = new KWin::Script(id, filePath, pluginName, this);
|
2011-12-23 10:52:06 +00:00
|
|
|
connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*)));
|
|
|
|
scripts.append(script);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
int KWin::Scripting::loadDeclarativeScript(const QString& filePath, const QString& pluginName)
|
2012-02-19 14:43:24 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
if (isScriptLoaded(pluginName)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-19 14:43:24 +00:00
|
|
|
const int id = scripts.size();
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::DeclarativeScript *script = new KWin::DeclarativeScript(id, filePath, pluginName, this);
|
2012-02-19 14:43:24 +00:00
|
|
|
connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*)));
|
|
|
|
scripts.append(script);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
KWin::Scripting::~Scripting()
|
|
|
|
{
|
2011-12-23 10:52:06 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject("/Scripting");
|
|
|
|
QDBusConnection::sessionBus().unregisterService("org.kde.kwin.Scripting");
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
|
|
|
|
#include "scripting.moc"
|