From 4c7b3462c1765fa44b6c6907a3a10f20ea22d063 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Fri, 6 Aug 2010 23:07:52 +0000 Subject: [PATCH] new and improved jobs/services mechanisms svn path=/trunk/KDE/kdebase/runtime/; revision=1160074 --- .../dataengine/javascriptdataengine.cpp | 1 - .../dataengine/javascriptservice.cpp | 72 ++++++++++++------- .../javascript/dataengine/javascriptservice.h | 35 ++++++++- 3 files changed, 81 insertions(+), 27 deletions(-) diff --git a/scriptengines/javascript/dataengine/javascriptdataengine.cpp b/scriptengines/javascript/dataengine/javascriptdataengine.cpp index fe3deec4f..eca235668 100644 --- a/scriptengines/javascript/dataengine/javascriptdataengine.cpp +++ b/scriptengines/javascript/dataengine/javascriptdataengine.cpp @@ -229,7 +229,6 @@ QScriptValue JavaScriptDataEngine::serviceCtor(QScriptContext *context, QScriptE JavaScriptService *service = new JavaScriptService(serviceName, iFace); if (service->wasFound()) { QScriptValue v = engine->newQObject(service, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents); - service->setScriptValue(v); return v; } diff --git a/scriptengines/javascript/dataengine/javascriptservice.cpp b/scriptengines/javascript/dataengine/javascriptservice.cpp index a76b70164..b79054206 100644 --- a/scriptengines/javascript/dataengine/javascriptservice.cpp +++ b/scriptengines/javascript/dataengine/javascriptservice.cpp @@ -24,6 +24,38 @@ #include "common/scriptenv.h" #include "javascriptdataengine.h" +JavaScriptServiceJob::JavaScriptServiceJob(QScriptEngine *engine, const QString &destination, const QString &operation, + const QMap ¶meters, QObject *parent) + : Plasma::ServiceJob(destination, operation, parameters, parent), + m_thisObject(engine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents)) +{ +} + +void JavaScriptServiceJob::start() +{ + if (!m_startFunction.isFunction()) { + setResult(false); + return; + } + + m_startFunction.call(m_thisObject); +} + +QScriptValue JavaScriptServiceJob::scriptValue() const +{ + return m_thisObject; +} + +QScriptValue JavaScriptServiceJob::startFunction() const +{ + return m_startFunction; +} + +void JavaScriptServiceJob::setStartFunction(const QScriptValue &v) +{ + m_startFunction = v; +} + JavaScriptService::JavaScriptService(const QString &serviceName, JavaScriptDataEngine *engine) : Plasma::Service(engine), m_dataEngine(engine) @@ -36,32 +68,14 @@ JavaScriptService::~JavaScriptService() // kDebug(); } -void JavaScriptService::setScriptValue(QScriptValue &v) -{ - m_scriptValue = v; -} - Plasma::ServiceJob *JavaScriptService::createJob(const QString &operation, QMap ¶meters) { - QMapIterator it(parameters); - if (m_dataEngine) { - ScriptEnv *env = ScriptEnv::findScriptEnv(m_dataEngine.data()->engine()); - if (env) { - QScriptValueList args; - args << m_scriptValue << operation - << qScriptValueFromValue(m_dataEngine.data()->engine(), parameters); - //TODO: Parameters! - QScriptValue func = m_scriptValue.property("createJob"); - /* - kDebug() << "same thing?" << v.equals(sv) << func.isFunction() << - sv.property("createJob").isFunction(); - */ - QScriptValue rv = env->callFunction(func, args, m_scriptValue); - Plasma::ServiceJob *job = dynamic_cast(rv.toQObject()); - if (job) { - return job; - } - } + if (m_setupFunc.isFunction() && m_dataEngine && isOperationEnabled(operation)) { + JavaScriptServiceJob *job = new JavaScriptServiceJob(m_dataEngine.data()->engine(), destination(), operation, parameters, this); + QScriptValueList args; + args << job->scriptValue(); + m_setupFunc.call(QScriptValue(), args); + return job; } return 0; @@ -90,5 +104,15 @@ void JavaScriptService::registerOperationsScheme() setOperationsScheme(&file); } +QScriptValue JavaScriptService::setupJobFunction() const +{ + return m_setupFunc; +} + +void JavaScriptService::setSetupJobFunction(const QScriptValue &v) +{ + m_setupFunc = v; +} + #include "javascriptservice.moc" diff --git a/scriptengines/javascript/dataengine/javascriptservice.h b/scriptengines/javascript/dataengine/javascriptservice.h index 1aba29aef..c69f5d5c8 100644 --- a/scriptengines/javascript/dataengine/javascriptservice.h +++ b/scriptengines/javascript/dataengine/javascriptservice.h @@ -24,6 +24,7 @@ #include #include +#include namespace Plasma { @@ -32,19 +33,49 @@ namespace Plasma class JavaScriptDataEngine; +class JavaScriptServiceJob : public Plasma::ServiceJob +{ + Q_OBJECT + Q_PROPERTY(QString destination READ destination) + Q_PROPERTY(QString operationName READ operationName) + Q_PROPERTY(QVariantMap parameters READ parameters) + Q_PROPERTY(QVariant result READ result WRITE setResult) + Q_PROPERTY(int error READ error WRITE setError) + Q_PROPERTY(QString errorText READ errorText WRITE setErrorText) + Q_PROPERTY(QScriptValue start READ startFunction WRITE setStartFunction) + +public: + JavaScriptServiceJob(QScriptEngine *engine, const QString &destination, const QString &operation, + const QMap ¶meters, QObject *parent = 0); + + void start(); + + QScriptValue startFunction() const; + void setStartFunction(const QScriptValue &v); + + QScriptValue scriptValue() const; + +private: + QScriptValue m_startFunction; + QScriptValue m_thisObject; +}; + class JavaScriptService : public Plasma::Service { Q_OBJECT Q_PROPERTY(QString destination READ destination WRITE setDestination) Q_PROPERTY(QStringList operationNames READ operationNames) Q_PROPERTY(QString name READ name) + Q_PROPERTY(QScriptValue setupJob READ setupJobFunction WRITE setSetupJobFunction) public: JavaScriptService(const QString &serviceName, JavaScriptDataEngine *engine); ~JavaScriptService(); bool wasFound() const; - void setScriptValue(QScriptValue &v); + + QScriptValue setupJobFunction() const; + void setSetupJobFunction(const QScriptValue &v); protected: Plasma::ServiceJob *createJob(const QString &operation, QMap ¶meters); @@ -52,7 +83,7 @@ protected: private: QWeakPointer m_dataEngine; - QScriptValue m_scriptValue; + QScriptValue m_setupFunc; }; #endif