kwin/scripting/scripting.cpp

118 lines
4.2 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2010 Rohan Prabhu <rohan@rohanprabhu.com>
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"
#include <kstandarddirs.h>
KWin::Scripting::Scripting()
2011-01-30 14:34:42 +00:00
{
// Default constructor no longer used, scripting can
// be disabled by calling kwin --noscript
2011-01-30 14:34:42 +00:00
}
void KWin::Scripting::start()
2011-01-30 14:34:42 +00:00
{
QStringList scriptFilters;
QString sDirectory = KStandardDirs::locateLocal("data", "kwin/scripts/");
2011-01-30 14:34:42 +00:00
if (sDirectory.isEmpty()) {
// Abort scripting setup. No location found to locate scripts
return;
}
2011-01-30 14:34:42 +00:00
scriptFilters << "*.kwinscript" << "*.kws" << "*.kwinqs";
scriptsDir.setPath(sDirectory);
2011-01-30 14:34:42 +00:00
scriptList = scriptsDir.entryList(scriptFilters, QDir::Files | QDir::Readable | QDir::Executable);
2011-01-30 14:34:42 +00:00
for (int i = 0; i < scriptList.size(); i++) {
scripts.append(new KWin::Script(new QScriptEngine(), scriptsDir.filePath(scriptList.at(i)), scriptsDir));
2011-01-30 14:34:42 +00:00
}
// Initialize singletons. Currently, only KWin::Workspace.
SWrapper::Workspace::initialize(KWin::Workspace::self());
runScripts();
2011-01-30 14:34:42 +00:00
}
void KWin::Scripting::runScript(KWin::Script* script)
2011-01-30 14:34:42 +00:00
{
if (script->scriptFile.open(QIODevice::ReadOnly)) {
script->workspace = new SWrapper::Workspace(script->engine);
(script->workspace)->attach(script->engine);
((script->engine)->globalObject()).setProperty("QTimer", constructTimerClass((script->engine)));
((script->engine)->globalObject()).setProperty("ClientGroup", SWrapper::ClientGroup::publishClientGroupClass((script->engine)));
((script->engine)->globalObject()).setProperty("chelate", KWin::Chelate::publishChelate(script->engine));
((script->engine)->globalObject()).setProperty("ch", KWin::Chelate::publishChelate(script->engine));
2011-08-17 21:51:55 +00:00
QObject::connect((script->engine), SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue)));
KWin::MetaScripting::registration(script->engine);
2011-01-30 14:34:42 +00:00
if (scriptsDir.exists(script->configFile)) {
QSettings scriptSettings(scriptsDir.filePath(script->configFile), QSettings::IniFormat);
QHash<QString, QVariant> scriptConfig;
QStringList keys = scriptSettings.allKeys();
2011-01-30 14:34:42 +00:00
for (int i = 0; i < keys.size(); i++) {
scriptConfig.insert(keys.at(i), scriptSettings.value(keys.at(i)));
2011-01-30 14:34:42 +00:00
}
KWin::MetaScripting::supplyConfig(script->engine, QVariant(scriptConfig));
2011-01-30 14:34:42 +00:00
} else {
KWin::MetaScripting::supplyConfig(script->engine);
2011-01-30 14:34:42 +00:00
}
QScriptValue ret = (script->engine)->evaluate(QString((script->scriptFile).readAll()));
2011-01-30 14:34:42 +00:00
if (ret.isError()) {
sigException(ret);
}
}
2011-01-30 14:34:42 +00:00
}
void KWin::Scripting::runScripts()
2011-01-30 14:34:42 +00:00
{
for (int i = 0; i < scripts.size(); i++) {
runScript(scripts.at(i));
}
2011-01-30 14:34:42 +00:00
}
void KWin::Scripting::sigException(const QScriptValue& exception)
2011-01-30 14:34:42 +00:00
{
QScriptValue ret = exception;
2011-01-30 14:34:42 +00:00
if (ret.isError()) {
kDebug(1212) << "defaultscript encountered an error at [Line " << uncaughtExceptionLineNumber() << "]";
kDebug(1212) << "Message: " << ret.toString();
kDebug(1212) << "-----------------";
QScriptValueIterator iter(ret);
2011-01-30 14:34:42 +00:00
while (iter.hasNext()) {
iter.next();
2011-01-30 14:34:42 +00:00
qDebug() << " " << iter.name() << ": " << iter.value().toString();
}
}
2011-01-30 14:34:42 +00:00
}
KWin::Scripting::~Scripting()
2011-01-30 14:34:42 +00:00
{
for (int i = 0; i < scripts.size(); i++) {
delete scripts.at(i);
}
2011-01-30 14:34:42 +00:00
}