From 3600fe5ed86b3be0b703c0d84f674a8b9f11e9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Thu, 1 Mar 2012 11:29:24 +0100 Subject: [PATCH] Support loading/unloading of scripts on config change Whenever the KWin configuration is changed, scripting has to unload scripts no longer loaded and load the scripts which have been added. For this new methods are introduced to check whether the script is loaded. REVIEW: 104037 --- scripting/scripting.cpp | 33 +++++++++++++++++++++++++++++++++ scripting/scripting.h | 14 ++++++-------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/scripting/scripting.cpp b/scripting/scripting.cpp index d1b76d1e86..bc41770fa4 100644 --- a/scripting/scripting.cpp +++ b/scripting/scripting.cpp @@ -25,6 +25,7 @@ along with this program. If not, see . #include "workspace_wrapper.h" #include "../thumbnailitem.h" #include "../options.h" +#include "../workspace.h" // KDE #include #include @@ -229,6 +230,7 @@ KWin::Scripting::Scripting(QObject *parent) { QDBusConnection::sessionBus().registerObject("/Scripting", this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables); QDBusConnection::sessionBus().registerService("org.kde.kwin.Scripting"); + connect(Workspace::self(), SIGNAL(configChanged()), SLOT(start())); } void KWin::Scripting::start() @@ -248,6 +250,10 @@ void KWin::Scripting::start() } if (!plugininfo.isPluginEnabled()) { + if (isScriptLoaded(plugininfo.pluginName())) { + // unload the script + unloadScript(plugininfo.pluginName()); + } continue; } const QString pluginName = service->property("X-KDE-PluginInfo-Name").toString(); @@ -267,6 +273,27 @@ void KWin::Scripting::start() runScripts(); } +bool KWin::Scripting::isScriptLoaded(const QString &pluginName) const +{ + foreach (AbstractScript *script, scripts) { + if (script->pluginName() == pluginName) { + return true; + } + } + return false; +} + +bool KWin::Scripting::unloadScript(const QString &pluginName) +{ + foreach (AbstractScript *script, scripts) { + if (script->pluginName() == pluginName) { + script->deleteLater(); + return true; + } + } + return false; +} + void KWin::Scripting::runScripts() { for (int i = 0; i < scripts.size(); i++) { @@ -281,6 +308,9 @@ void KWin::Scripting::scriptDestroyed(QObject *object) int KWin::Scripting::loadScript(const QString &filePath, const QString& pluginName) { + if (isScriptLoaded(pluginName)) { + return -1; + } const int id = scripts.size(); KWin::Script *script = new KWin::Script(id, filePath, pluginName, this); connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*))); @@ -290,6 +320,9 @@ int KWin::Scripting::loadScript(const QString &filePath, const QString& pluginNa int KWin::Scripting::loadDeclarativeScript(const QString& filePath, const QString& pluginName) { + if (isScriptLoaded(pluginName)) { + return -1; + } const int id = scripts.size(); KWin::DeclarativeScript *script = new KWin::DeclarativeScript(id, filePath, pluginName, this); connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*))); diff --git a/scripting/scripting.h b/scripting/scripting.h index d9d0bdb641..67184b4c60 100644 --- a/scripting/scripting.h +++ b/scripting/scripting.h @@ -43,6 +43,9 @@ public: QString fileName() const { return m_scriptFile.fileName(); } + const QString &pluginName() { + return m_pluginName; + } void printMessage(const QString &message); @@ -59,9 +62,6 @@ protected: QFile &scriptFile() { return m_scriptFile; } - const QString &pluginName() { - return m_pluginName; - } bool running() const { return m_running; } @@ -143,17 +143,15 @@ private: public: Scripting(QObject *parent = NULL); - /** - * Start running scripts. This was essential to have KWin::Scripting - * be initialized on stack and also have the option to disable scripting. - */ - void start(); ~Scripting(); Q_SCRIPTABLE Q_INVOKABLE int loadScript(const QString &filePath, const QString &pluginName = QString()); Q_SCRIPTABLE Q_INVOKABLE int loadDeclarativeScript(const QString &filePath, const QString &pluginName = QString()); + Q_SCRIPTABLE Q_INVOKABLE bool isScriptLoaded(const QString &pluginName) const; + Q_SCRIPTABLE Q_INVOKABLE bool unloadScript(const QString &pluginName); public Q_SLOTS: void scriptDestroyed(QObject *object); + void start(); }; }