JavaScript DataEngines with JavaScript Services. what's left? custom Jobs.
svn path=/trunk/KDE/kdebase/runtime/; revision=1159737
This commit is contained in:
parent
b5669412c5
commit
e09ceabfbd
@ -72,6 +72,7 @@ install(FILES data/plasma-scriptengine-runner-javascript.desktop DESTINATION ${S
|
||||
|
||||
set(javascript_dataengine_engine_SRCS
|
||||
dataengine/javascriptdataengine.cpp
|
||||
dataengine/javascriptservice.cpp
|
||||
common/scriptenv.cpp
|
||||
simplebindings/dataengine.cpp
|
||||
simplebindings/variant.cpp
|
||||
|
@ -23,8 +23,9 @@
|
||||
|
||||
#include <Plasma/Package>
|
||||
|
||||
#include "authorization.h"
|
||||
#include "scriptenv.h"
|
||||
#include "javascriptservice.h"
|
||||
#include "common/authorization.h"
|
||||
#include "common/scriptenv.h"
|
||||
#include "simplebindings/dataengine.h"
|
||||
#include "simplebindings/i18n.h"
|
||||
|
||||
@ -55,6 +56,7 @@ bool JavaScriptDataEngine::init()
|
||||
global.setProperty("removeAllData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsRemoveAllData));
|
||||
global.setProperty("removeData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsRemoveData));
|
||||
global.setProperty("removeAllSources", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsRemoveAllSources));
|
||||
global.setProperty("Service", m_qscriptEngine->newFunction(JavaScriptDataEngine::serviceCtor));
|
||||
|
||||
registerDataEngineMetaTypes(m_qscriptEngine);
|
||||
|
||||
@ -66,6 +68,11 @@ bool JavaScriptDataEngine::init()
|
||||
return m_env->include(mainScript());
|
||||
}
|
||||
|
||||
QScriptEngine *JavaScriptDataEngine::engine() const
|
||||
{
|
||||
return m_qscriptEngine;
|
||||
}
|
||||
|
||||
void JavaScriptDataEngine::jsSetMaxSourceCount(int count)
|
||||
{
|
||||
setMaxSourceCount(count);
|
||||
@ -202,6 +209,34 @@ QScriptValue JavaScriptDataEngine::jsRemoveAllSources(QScriptContext *context, Q
|
||||
return context->throwError(error);
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::serviceCtor(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
QString error;
|
||||
JavaScriptDataEngine *iFace = extractIFace(engine, error);
|
||||
if (!iFace) {
|
||||
return context->throwError(error);
|
||||
}
|
||||
|
||||
if (context->argumentCount() < 1) {
|
||||
return context->throwError(i18n("Service requires at least one parameter: the name of the service"));
|
||||
}
|
||||
|
||||
const QString &serviceName = context->argument(0).toString();
|
||||
if (serviceName.isEmpty()) {
|
||||
return context->throwError(i18n("Service requires at least one parameter: the name of the service"));
|
||||
}
|
||||
|
||||
JavaScriptService *service = new JavaScriptService(serviceName, iFace);
|
||||
if (service->wasFound()) {
|
||||
QScriptValue v = engine->newQObject(service, QScriptEngine::ScriptOwnership);
|
||||
service->setScriptValue(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
delete service;
|
||||
return context->throwError(i18n("Requested service %1 ws not found in the Package.", serviceName));
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::callFunction(const QString &functionName, const QScriptValueList &args)
|
||||
{
|
||||
QScriptValue func = m_iface.property(functionName);
|
||||
@ -226,7 +261,7 @@ QStringList JavaScriptDataEngine::sources() const
|
||||
return rv.toVariant().toStringList();
|
||||
}
|
||||
|
||||
return QStringList();
|
||||
return DataEngineScript::sources();
|
||||
}
|
||||
|
||||
bool JavaScriptDataEngine::sourceRequestEvent(const QString &name)
|
||||
@ -261,12 +296,25 @@ Plasma::Service *JavaScriptDataEngine::serviceForSource(const QString &source)
|
||||
args << source;
|
||||
QScriptValue rv = callFunction("serviceForSource", args);
|
||||
if (rv.isValid() && rv.isQObject()) {
|
||||
return qobject_cast<Plasma::Service *>(rv.toQObject());
|
||||
Plasma::Service *service = qobject_cast<Plasma::Service *>(rv.toQObject());
|
||||
if (service) {
|
||||
if (service->destination().isEmpty()) {
|
||||
service->setDestination(source);
|
||||
}
|
||||
return service;
|
||||
} else {
|
||||
delete rv.toQObject();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
QString JavaScriptDataEngine::filePath(const char *type, const QString &file) const
|
||||
{
|
||||
return package()->filePath(type, file);
|
||||
}
|
||||
|
||||
bool JavaScriptDataEngine::include(const QString &script)
|
||||
{
|
||||
const QString path = package()->filePath("scripts", script);
|
||||
|
@ -39,6 +39,9 @@ public:
|
||||
JavaScriptDataEngine(QObject *parent, const QVariantList &args);
|
||||
bool init();
|
||||
|
||||
QScriptEngine *engine() const;
|
||||
QString filePath(const char *type, const QString &file) const;
|
||||
|
||||
QStringList sources() const;
|
||||
bool sourceRequestEvent(const QString &name);
|
||||
bool updateSourceEvent(const QString &source);
|
||||
@ -63,6 +66,7 @@ private:
|
||||
static QScriptValue jsRemoveAllData(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue jsRemoveData(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue jsRemoveAllSources(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue serviceCtor(QScriptContext *context, QScriptEngine *engine);
|
||||
|
||||
QScriptValue callFunction(const QString &functionName, const QScriptValueList &args);
|
||||
|
||||
|
89
scriptengines/javascript/dataengine/javascriptservice.cpp
Normal file
89
scriptengines/javascript/dataengine/javascriptservice.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2010 Aaron J. Seigo <aseigo@kde.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library General Public License version 2 as
|
||||
* published by the Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "javascriptservice.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <Plasma/ServiceJob>
|
||||
|
||||
#include "common/scriptenv.h"
|
||||
#include "javascriptdataengine.h"
|
||||
|
||||
JavaScriptService::JavaScriptService(const QString &serviceName, JavaScriptDataEngine *engine)
|
||||
: Plasma::Service(engine),
|
||||
m_dataEngine(engine)
|
||||
{
|
||||
setName(serviceName);
|
||||
}
|
||||
|
||||
void JavaScriptService::setScriptValue(QScriptValue &v)
|
||||
{
|
||||
m_scriptValue = v;
|
||||
}
|
||||
|
||||
Plasma::ServiceJob *JavaScriptService::createJob(const QString &operation, QMap<QString, QVariant> ¶meters)
|
||||
{
|
||||
QMapIterator<QString, QVariant> 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<Plasma::ServiceJob *>(rv.toQObject());
|
||||
if (job) {
|
||||
return job;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool JavaScriptService::wasFound() const
|
||||
{
|
||||
return m_dataEngine;
|
||||
}
|
||||
|
||||
void JavaScriptService::registerOperationsScheme()
|
||||
{
|
||||
if (!m_dataEngine) {
|
||||
return;
|
||||
}
|
||||
|
||||
const QString path = m_dataEngine.data()->filePath("services", name() + ".operations");
|
||||
|
||||
if (path.isEmpty()) {
|
||||
kDebug() << "Cannot find operations description:" << name() << ".operations";
|
||||
m_dataEngine.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
QFile file(path);
|
||||
setOperationsScheme(&file);
|
||||
}
|
||||
|
||||
#include "javascriptservice.moc"
|
||||
|
55
scriptengines/javascript/dataengine/javascriptservice.h
Normal file
55
scriptengines/javascript/dataengine/javascriptservice.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2010 Aaron J. Seigo <aseigo@kde.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library General Public License version 2 as
|
||||
* published by the Free Software Foundation
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this program; if not, write to the
|
||||
* Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef JAVASCRIPTSERVICE_H
|
||||
#define JAVASCRIPTSERVICE_H
|
||||
|
||||
#include <QWeakPointer>
|
||||
#include <QScriptValue>
|
||||
|
||||
#include <Plasma/Package>
|
||||
#include <Plasma/Service>
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
class Package;
|
||||
} // namespace Plasma
|
||||
|
||||
class JavaScriptDataEngine;
|
||||
|
||||
class JavaScriptService : public Plasma::Service
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
JavaScriptService(const QString &serviceName, JavaScriptDataEngine *engine);
|
||||
|
||||
bool wasFound() const;
|
||||
void setScriptValue(QScriptValue &v);
|
||||
|
||||
protected:
|
||||
Plasma::ServiceJob *createJob(const QString &operation, QMap<QString, QVariant> ¶meters);
|
||||
void registerOperationsScheme();
|
||||
|
||||
private:
|
||||
QWeakPointer<JavaScriptDataEngine> m_dataEngine;
|
||||
QScriptValue m_scriptValue;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user