From a93be26e8b0eb36df157464ae4d3d7da5af220b2 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Fri, 9 Apr 2010 01:03:05 +0000 Subject: [PATCH] allow templates to be loaded in a script with template svn path=/trunk/KDE/kdebase/workspace/; revision=1112738 --- scriptengine.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++---- scriptengine.h | 3 ++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/scriptengine.cpp b/scriptengine.cpp index 31a54c478..16bfa8270 100644 --- a/scriptengine.cpp +++ b/scriptengine.cpp @@ -23,16 +23,19 @@ #include #include +#include #include #include #include #include #include +#include #include "appinterface.h" #include "containment.h" #include "widget.h" +#include "layouttemplatepackagestructure.h" QScriptValue constructQRectFClass(QScriptEngine *engine); @@ -45,7 +48,6 @@ ScriptEngine::ScriptEngine(Plasma::Corona *corona, QObject *parent) connect(interface, SIGNAL(print(QString)), this, SIGNAL(print(QString))); m_scriptSelf = newQObject(interface, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassProperties | QScriptEngine::ExcludeSuperClassMethods); - kDebug( )<< "*****************************"; setupEngine(); connect(this, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(exception(QScriptValue))); } @@ -202,6 +204,55 @@ QScriptValue ScriptEngine::fileExists(QScriptContext *context, QScriptEngine *en return f.exists(); } +QScriptValue ScriptEngine::loadTemplate(QScriptContext *context, QScriptEngine *engine) +{ + Q_UNUSED(engine) + if (context->argumentCount() == 0) { + return false; + } + + const QString layout = context->argument(0).toString(); + if (layout.isEmpty() || layout.contains("'")) { + 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()) { + 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()) { + return false; + } + + Plasma::Package p(path, structure); + const QString scriptFile = p.filePath("mainscript"); + if (scriptFile.isEmpty()) { + 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()) { + return false; + } + + ScriptEngine *env = envFor(engine); + env->evaluateScript(script, path); + return true; +} + void ScriptEngine::setupEngine() { QScriptValue v = globalObject(); @@ -221,6 +272,7 @@ void ScriptEngine::setupEngine() m_scriptSelf.setProperty("activityForScreen", newFunction(ScriptEngine::activityForScreen)); m_scriptSelf.setProperty("panelById", newFunction(ScriptEngine::panelById)); m_scriptSelf.setProperty("fileExists", newFunction(ScriptEngine::fileExists)); + m_scriptSelf.setProperty("loadTemplate", newFunction(ScriptEngine::loadTemplate)); setGlobalObject(m_scriptSelf); } @@ -231,10 +283,10 @@ bool ScriptEngine::isPanel(const Plasma::Containment *c) c->containmentType() == Plasma::Containment::CustomPanelContainment; } -void ScriptEngine::evaluateScript(const QString &script) +bool ScriptEngine::evaluateScript(const QString &script, const QString &path) { //kDebug() << "evaluating" << m_editor->toPlainText(); - evaluate(script); + evaluate(script, path); if (hasUncaughtException()) { //kDebug() << "catch the exception!"; QString error = i18n("Error: %1 at line %2\n\nBacktrace:\n%3", @@ -242,7 +294,10 @@ void ScriptEngine::evaluateScript(const QString &script) QString::number(uncaughtExceptionLineNumber()), uncaughtExceptionBacktrace().join("\n ")); emit printError(error); + return false; } + + return true; } void ScriptEngine::exception(const QScriptValue &value) @@ -303,7 +358,6 @@ QStringList ScriptEngine::defaultLayoutScripts() QSet scriptNames; foreach (const QString &script, scripts) { if (script.startsWith(localDir) || script.startsWith(localXdgDir)) { - kDebug() << "skipping user local script: " << script; kDebug() << "skipping user local script: " << script; continue; } diff --git a/scriptengine.h b/scriptengine.h index 96a5092cb..393415a88 100644 --- a/scriptengine.h +++ b/scriptengine.h @@ -45,7 +45,7 @@ public: static QStringList pendingUpdateScripts(); static QStringList defaultLayoutScripts(); - void evaluateScript(const QString &script); + bool evaluateScript(const QString &script, const QString &path = QString()); static bool isPanel(const Plasma::Containment *c); QScriptValue wrap(Plasma::Applet *w, QScriptEngine *engine); virtual QScriptValue wrap(Plasma::Containment *c, QScriptEngine *engine); @@ -68,6 +68,7 @@ private: static QScriptValue activityForScreen(QScriptContext *context, QScriptEngine *engine); static QScriptValue panelById(QScriptContext *context, QScriptEngine *engine); static QScriptValue fileExists(QScriptContext *context, QScriptEngine *engine); + static QScriptValue loadTemplate(QScriptContext *context, QScriptEngine *engine); // helpers static QScriptValue createContainment(const QString &type, const QString &defautPlugin,