From c56e70ca7c2ee61d777cdaaca99ae42bb8f5a973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Fri, 23 Dec 2011 08:49:28 +0100 Subject: [PATCH] Make Script a proper class --- scripting/scripting.cpp | 123 ++++++++++++++++++++++------------------ scripting/scripting.h | 38 ++++++------- 2 files changed, 85 insertions(+), 76 deletions(-) diff --git a/scripting/scripting.cpp b/scripting/scripting.cpp index 0a7184988f..b635514e06 100644 --- a/scripting/scripting.cpp +++ b/scripting/scripting.cpp @@ -21,6 +21,71 @@ along with this program. If not, see . #include "scripting.h" #include + +KWin::Script::Script(QString scriptName, QDir dir, QObject *parent) + : QObject(parent) + , m_engine(new QScriptEngine(this)) + , m_scriptDir(dir) + , m_configFile(QFileInfo(m_scriptFile).completeBaseName() + QString(".kwscfg")) + , m_workspace(new SWrapper::Workspace(m_engine)) +{ + m_scriptFile.setFileName(dir.filePath(scriptName)); +} + +KWin::Script::~Script() +{ +} + +void KWin::Script::run() +{ + if (m_scriptFile.open(QIODevice::ReadOnly)) { + m_workspace->attach(m_engine); + m_engine->globalObject().setProperty("QTimer", constructTimerClass(m_engine)); + m_engine->globalObject().setProperty("ClientGroup", SWrapper::ClientGroup::publishClientGroupClass(m_engine)); + m_engine->globalObject().setProperty("chelate", KWin::Chelate::publishChelate(m_engine)); + m_engine->globalObject().setProperty("ch", KWin::Chelate::publishChelate(m_engine)); + QObject::connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue))); + KWin::MetaScripting::registration(m_engine); + + if (m_scriptDir.exists(m_configFile)) { + QSettings scriptSettings(m_scriptDir.filePath(m_configFile), QSettings::IniFormat); + QHash scriptConfig; + QStringList keys = scriptSettings.allKeys(); + + for (int i = 0; i < keys.size(); i++) { + scriptConfig.insert(keys.at(i), scriptSettings.value(keys.at(i))); + } + + KWin::MetaScripting::supplyConfig(m_engine, QVariant(scriptConfig)); + } else { + KWin::MetaScripting::supplyConfig(m_engine); + } + + QScriptValue ret = m_engine->evaluate(m_scriptFile.readAll()); + + if (ret.isError()) { + sigException(ret); + } + } +} + +void KWin::Script::sigException(const QScriptValue& exception) +{ + QScriptValue ret = exception; + if (ret.isError()) { + kDebug(1212) << "defaultscript encountered an error at [Line " << m_engine->uncaughtExceptionLineNumber() << "]"; + kDebug(1212) << "Message: " << ret.toString(); + kDebug(1212) << "-----------------"; + + QScriptValueIterator iter(ret); + while (iter.hasNext()) { + iter.next(); + qDebug() << " " << iter.name() << ": " << iter.value().toString(); + } + } +} + + KWin::Scripting::Scripting() { // Default constructor no longer used, scripting can @@ -42,7 +107,7 @@ void KWin::Scripting::start() scriptList = scriptsDir.entryList(scriptFilters, QDir::Files | QDir::Readable | QDir::Executable); for (int i = 0; i < scriptList.size(); i++) { - scripts.append(new KWin::Script(new QScriptEngine(), scriptsDir.filePath(scriptList.at(i)), scriptsDir)); + scripts.append(new KWin::Script(scriptsDir.filePath(scriptList.at(i)), scriptsDir, this)); } // Initialize singletons. Currently, only KWin::Workspace. @@ -51,67 +116,13 @@ void KWin::Scripting::start() runScripts(); } -void KWin::Scripting::runScript(KWin::Script* script) -{ - 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)); - QObject::connect((script->engine), SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue))); - KWin::MetaScripting::registration(script->engine); - - if (scriptsDir.exists(script->configFile)) { - QSettings scriptSettings(scriptsDir.filePath(script->configFile), QSettings::IniFormat); - QHash scriptConfig; - QStringList keys = scriptSettings.allKeys(); - - for (int i = 0; i < keys.size(); i++) { - scriptConfig.insert(keys.at(i), scriptSettings.value(keys.at(i))); - } - - KWin::MetaScripting::supplyConfig(script->engine, QVariant(scriptConfig)); - } else { - KWin::MetaScripting::supplyConfig(script->engine); - } - - QScriptValue ret = (script->engine)->evaluate(QString((script->scriptFile).readAll())); - - if (ret.isError()) { - sigException(ret); - } - } -} - - void KWin::Scripting::runScripts() { for (int i = 0; i < scripts.size(); i++) { - runScript(scripts.at(i)); - } -} - -void KWin::Scripting::sigException(const QScriptValue& exception) -{ - QScriptValue ret = exception; - if (ret.isError()) { - kDebug(1212) << "defaultscript encountered an error at [Line " << uncaughtExceptionLineNumber() << "]"; - kDebug(1212) << "Message: " << ret.toString(); - kDebug(1212) << "-----------------"; - - QScriptValueIterator iter(ret); - while (iter.hasNext()) { - iter.next(); - qDebug() << " " << iter.name() << ": " << iter.value().toString(); - } + scripts.at(i)->run(); } } KWin::Scripting::~Scripting() { - for (int i = 0; i < scripts.size(); i++) { - delete scripts.at(i); - } } diff --git a/scripting/scripting.h b/scripting/scripting.h index b0d00f95af..cf9d6ca533 100644 --- a/scripting/scripting.h +++ b/scripting/scripting.h @@ -44,23 +44,28 @@ namespace KWin * the scriptfile, the configfile and the QScriptEngine * that will run this script */ -class Script +class Script : public QObject { + Q_OBJECT public: - QScriptEngine* engine; - QFile scriptFile; - QString configFile; - SWrapper::Workspace* workspace; - Script(QScriptEngine* _engine, QString scriptName, QDir dir) : - engine(_engine) { - scriptFile.setFileName(dir.filePath(scriptName)); - configFile = (QFileInfo(scriptFile).completeBaseName() + QString(".kwscfg")); - } + Script(QString scriptName, QDir dir, QObject *parent = NULL); + virtual ~Script(); + void run(); - ~Script() { - delete engine; - } +private slots: + /** + * A nice clean way to handle exceptions in scripting. + * TODO: Log to file, show from notifier.. + */ + void sigException(const QScriptValue &exception); + +private: + QScriptEngine *m_engine; + QDir m_scriptDir; + QFile m_scriptFile; + QString m_configFile; + SWrapper::Workspace *m_workspace; }; /** @@ -83,13 +88,6 @@ private: // An interface to run scripts at runtime void runScript(KWin::Script*); -public slots: - /** - * A nice clean way to handle exceptions in scripting. - * TODO: Log to file, show from notifier.. - */ - void sigException(const QScriptValue&); - public: Scripting(); /**