new and improved jobs/services mechanisms

svn path=/trunk/KDE/kdebase/runtime/; revision=1160074
This commit is contained in:
Aaron J. Seigo 2010-08-06 23:07:52 +00:00
parent dca84d9d21
commit 4c7b3462c1
3 changed files with 81 additions and 27 deletions

View File

@ -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;
}

View File

@ -24,6 +24,38 @@
#include "common/scriptenv.h"
#include "javascriptdataengine.h"
JavaScriptServiceJob::JavaScriptServiceJob(QScriptEngine *engine, const QString &destination, const QString &operation,
const QMap<QString, QVariant> &parameters, 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,33 +68,15 @@ JavaScriptService::~JavaScriptService()
// kDebug();
}
void JavaScriptService::setScriptValue(QScriptValue &v)
{
m_scriptValue = v;
}
Plasma::ServiceJob *JavaScriptService::createJob(const QString &operation, QMap<QString, QVariant> &parameters)
{
QMapIterator<QString, QVariant> it(parameters);
if (m_dataEngine) {
ScriptEnv *env = ScriptEnv::findScriptEnv(m_dataEngine.data()->engine());
if (env) {
if (m_setupFunc.isFunction() && m_dataEngine && isOperationEnabled(operation)) {
JavaScriptServiceJob *job = new JavaScriptServiceJob(m_dataEngine.data()->engine(), destination(), operation, parameters, this);
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<Plasma::ServiceJob *>(rv.toQObject());
if (job) {
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"

View File

@ -24,6 +24,7 @@
#include <Plasma/Package>
#include <Plasma/Service>
#include <Plasma/ServiceJob>
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<QString, QVariant> &parameters, 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<QString, QVariant> &parameters);
@ -52,7 +83,7 @@ protected:
private:
QWeakPointer<JavaScriptDataEngine> m_dataEngine;
QScriptValue m_scriptValue;
QScriptValue m_setupFunc;
};
#endif