initial draft of a ECMAScript driven DataEngine.
TODO: * testing * binding Plasma::Service svn path=/trunk/KDE/kdebase/runtime/; revision=991101
This commit is contained in:
parent
5ee36b5e83
commit
d7c89e409e
@ -1,3 +1,5 @@
|
||||
# APPLET
|
||||
|
||||
set(simple_javascript_engine_SRCS
|
||||
simplejavascriptapplet.cpp
|
||||
appletinterface.cpp
|
||||
@ -30,6 +32,8 @@ target_link_libraries(plasma_appletscript_simple_javascript
|
||||
install(TARGETS plasma_appletscript_simple_javascript DESTINATION ${PLUGIN_INSTALL_DIR})
|
||||
install(FILES plasma-scriptengine-applet-simple-javascript.desktop DESTINATION ${SERVICES_INSTALL_DIR} )
|
||||
|
||||
# RUNNER
|
||||
|
||||
set(javascript_runner_engine_SRCS
|
||||
javascriptrunner.cpp
|
||||
)
|
||||
@ -46,3 +50,20 @@ target_link_libraries(plasma_runnerscript_javascript
|
||||
|
||||
install(TARGETS plasma_runnerscript_javascript DESTINATION ${PLUGIN_INSTALL_DIR})
|
||||
install(FILES plasma-scriptengine-runner-javascript.desktop DESTINATION ${SERVICES_INSTALL_DIR})
|
||||
|
||||
|
||||
# DATAENGINE
|
||||
|
||||
set(javascript_dataengine_engine_SRCS
|
||||
javascriptdataengine.cpp
|
||||
)
|
||||
kde4_add_plugin(plasma_dataenginescript_javascript ${javascript_dataengine_engine_SRCS})
|
||||
|
||||
target_link_libraries(plasma_dataenginescript_javascript
|
||||
${KDE4_KDECORE_LIBS}
|
||||
${KDE4_PLASMA_LIBS}
|
||||
${QT_QTSCRIPT_LIBRARY})
|
||||
|
||||
install(TARGETS plasma_dataenginescript_javascript DESTINATION ${PLUGIN_INSTALL_DIR})
|
||||
install(FILES plasma-scriptengine-dataengine-javascript.desktop DESTINATION ${SERVICES_INSTALL_DIR})
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright 2008 Chani Armitage <chani@kde.org>
|
||||
* Copyright 2008, 2009 Aaron 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 as
|
||||
@ -47,6 +48,7 @@ class AppletInterface : public QObject
|
||||
Q_ENUMS(QtOrientation)
|
||||
Q_ENUMS(QtAlignment)
|
||||
Q_PROPERTY(QString activeConfig WRITE setActiveConfig READ activeConfig)
|
||||
Q_PROPERTY(bool busy WRITE setBusy READ isBusy)
|
||||
|
||||
public:
|
||||
AppletInterface(SimpleJavaScriptApplet *parent);
|
||||
|
@ -73,5 +73,68 @@ int qScriptRegisterMapMetaType(
|
||||
qScriptValueToMap, prototype);
|
||||
}
|
||||
|
||||
/*
|
||||
* Workaround the fact that QtScripts handling of variants seems a bit broken.
|
||||
*/
|
||||
QScriptValue variantToScriptValue(QScriptEngine *engine, QVariant var)
|
||||
{
|
||||
if (var.isNull()) {
|
||||
return engine->nullValue();
|
||||
}
|
||||
|
||||
switch(var.type())
|
||||
{
|
||||
case QVariant::Invalid:
|
||||
return engine->nullValue();
|
||||
case QVariant::Bool:
|
||||
return QScriptValue(engine, var.toBool());
|
||||
case QVariant::Date:
|
||||
return engine->newDate(var.toDateTime());
|
||||
case QVariant::DateTime:
|
||||
return engine->newDate(var.toDateTime());
|
||||
case QVariant::Double:
|
||||
return QScriptValue(engine, var.toDouble());
|
||||
case QVariant::Int:
|
||||
case QVariant::LongLong:
|
||||
return QScriptValue(engine, var.toInt());
|
||||
case QVariant::String:
|
||||
return QScriptValue(engine, var.toString());
|
||||
case QVariant::Time: {
|
||||
QDateTime t(QDate::currentDate(), var.toTime());
|
||||
return engine->newDate(t);
|
||||
}
|
||||
case QVariant::UInt:
|
||||
return QScriptValue(engine, var.toUInt());
|
||||
default:
|
||||
if (var.typeName() == QLatin1String("KUrl")) {
|
||||
return QScriptValue(engine, var.value<KUrl>().prettyUrl());
|
||||
} else if (var.typeName() == QLatin1String("QColor")) {
|
||||
return QScriptValue(engine, var.value<QColor>().name());
|
||||
} else if (var.typeName() == QLatin1String("QUrl")) {
|
||||
return QScriptValue(engine, var.value<QUrl>().toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return qScriptValueFromValue(engine, var);
|
||||
}
|
||||
QScriptValue qScriptValueFromData(QScriptEngine *engine, const DataEngine::Data &data)
|
||||
{
|
||||
DataEngine::Data::const_iterator begin = data.begin();
|
||||
DataEngine::Data::const_iterator end = data.end();
|
||||
DataEngine::Data::const_iterator it;
|
||||
|
||||
QScriptValue obj = engine->newObject();
|
||||
|
||||
for (it = begin; it != end; ++it) {
|
||||
//kDebug() << "setting" << it.key() << "to" << it.value();
|
||||
QString prop = it.key();
|
||||
prop.replace(' ', '_');
|
||||
obj.setProperty(prop, variantToScriptValue(engine, it.value()));
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
#endif // BIND_DATAENGINE_H
|
||||
|
||||
|
101
scriptengines/javascript/bind_i18n.h
Normal file
101
scriptengines/javascript/bind_i18n.h
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2009 Aaron Seigo <aseigo@kde.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 JAVASCRIPTBINDI18N_H
|
||||
#define JAVASCRIPTBINDI18N_H
|
||||
|
||||
QScriptValue jsi18n(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 1) {
|
||||
return context->throwError(i18n("i18n() takes at least one argument"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18n(context->argument(0).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 1; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue jsi18nc(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 2) {
|
||||
return context->throwError(i18n("i18nc() takes at least two arguments"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18nc(context->argument(0).toString().toUtf8(),
|
||||
context->argument(1).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 2; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue jsi18np(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 2) {
|
||||
return context->throwError(i18n("i18np() takes at least two arguments"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18np(context->argument(0).toString().toUtf8(),
|
||||
context->argument(1).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 2; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue jsi18ncp(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 3) {
|
||||
return context->throwError(i18n("i18ncp() takes at least three arguments"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18ncp(context->argument(0).toString().toUtf8(),
|
||||
context->argument(1).toString().toUtf8(),
|
||||
context->argument(2).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 3; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
void bindI18N(QScriptEngine *engine)
|
||||
{
|
||||
QScriptValue global = engine->globalObject();
|
||||
global.setProperty("i18n", engine->newFunction(jsi18n));
|
||||
global.setProperty("i18nc", engine->newFunction(jsi18nc));
|
||||
global.setProperty("i18np", engine->newFunction(jsi18np));
|
||||
global.setProperty("i18ncp", engine->newFunction(jsi18ncp));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
260
scriptengines/javascript/javascriptdataengine.cpp
Normal file
260
scriptengines/javascript/javascriptdataengine.cpp
Normal file
@ -0,0 +1,260 @@
|
||||
/*
|
||||
* Copyright 2009 Aaron Seigo <aseigo@kde.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 "javascriptdataengine.h"
|
||||
|
||||
#include <QScriptEngine>
|
||||
#include <QScriptContext>
|
||||
|
||||
#include "bind_dataengine.h"
|
||||
#include "bind_i18n.h"
|
||||
|
||||
JavaScriptDataEngine::JavaScriptDataEngine(QObject *parent, const QVariantList &args)
|
||||
: DataEngineScript(parent),
|
||||
m_qscriptEngine(new QScriptEngine(this))
|
||||
{
|
||||
Q_UNUSED(args)
|
||||
}
|
||||
|
||||
bool JavaScriptDataEngine::init()
|
||||
{
|
||||
QScriptValue global = m_qscriptEngine->globalObject();
|
||||
|
||||
bindI18N(m_qscriptEngine);
|
||||
|
||||
QScriptValue m_iface = m_qscriptEngine->newQObject(this);
|
||||
m_iface.setScope(global);
|
||||
global.setProperty("engine", m_iface);
|
||||
|
||||
global.setProperty("setData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsSetData));
|
||||
global.setProperty("removeAllData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsRemoveAllData));
|
||||
global.setProperty("removeData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsRemoveData));
|
||||
global.setProperty("removeAllSources", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsRemoveAllSources));
|
||||
|
||||
qScriptRegisterMetaType<DataEngine::Data>(m_qscriptEngine, qScriptValueFromData, 0, QScriptValue());
|
||||
/**
|
||||
TODO: Service bindings
|
||||
m_qscriptEngine->setDefaultPrototype(qMetaTypeId<Service*>(), m_qscriptEngine->newQObject(new DummyService()));
|
||||
m_qscriptEngine->setDefaultPrototype(qMetaTypeId<ServiceJob*>(), m_qscriptEngine->newQObject(new ServiceJob(QString(), QString(), QMap<QString, QVariant>())));
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void JavaScriptDataEngine::jsSetMaxSourceCount(int count)
|
||||
{
|
||||
setMaxSourceCount(count);
|
||||
}
|
||||
|
||||
int JavaScriptDataEngine::maxSourceCount() const
|
||||
{
|
||||
return dataEngine()->maxSourceCount();
|
||||
}
|
||||
|
||||
void JavaScriptDataEngine::jsSetMinimumPollingInterval(int interval)
|
||||
{
|
||||
setMinimumPollingInterval(interval);
|
||||
}
|
||||
|
||||
int JavaScriptDataEngine::minimumPollingInterval() const
|
||||
{
|
||||
return minimumPollingInterval();
|
||||
}
|
||||
|
||||
void JavaScriptDataEngine::jsSetPollingInterval(int interval)
|
||||
{
|
||||
setPollingInterval(interval);
|
||||
}
|
||||
|
||||
int JavaScriptDataEngine::pollingInterval() const
|
||||
{
|
||||
return pollingInterval();
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::jsSetData(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 2) {
|
||||
return context->throwError(i18n("setData() takes at least two arguments"));
|
||||
}
|
||||
|
||||
QString error;
|
||||
JavaScriptDataEngine *iFace = extractIFace(engine, error);
|
||||
|
||||
if (!iFace) {
|
||||
return context->throwError(error);
|
||||
}
|
||||
|
||||
QString source = context->argument(0).toString();
|
||||
QString value = context->argument(1).toString();
|
||||
|
||||
if (context->argumentCount() > 2) {
|
||||
QString key = value;
|
||||
QString value = context->argument(2).toString();
|
||||
iFace->setData(source, key, value);
|
||||
} else {
|
||||
iFace->setData(source, value);
|
||||
}
|
||||
|
||||
return engine->newVariant(true);
|
||||
}
|
||||
|
||||
JavaScriptDataEngine *JavaScriptDataEngine::extractIFace(QScriptEngine *engine, QString &error)
|
||||
{
|
||||
JavaScriptDataEngine *interface = 0;
|
||||
QScriptValue engineValue = engine->globalObject().property("engine");
|
||||
QObject *engineObject = engineValue.toQObject();
|
||||
|
||||
if (!engineObject) {
|
||||
error = i18n("Could not extract the DataEngineObject");
|
||||
} else {
|
||||
interface = qobject_cast<JavaScriptDataEngine *>(engineObject);
|
||||
if (!interface) {
|
||||
error = i18n("Could not extract the DataEngine");
|
||||
}
|
||||
}
|
||||
|
||||
return interface;
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::jsRemoveAllData(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 1) {
|
||||
return context->throwError(i18n("removeAllData() takes at least one argument (the source name)"));
|
||||
}
|
||||
|
||||
QString source = context->argument(0).toString();
|
||||
QString error;
|
||||
JavaScriptDataEngine *iFace = extractIFace(engine, error);
|
||||
|
||||
if (iFace) {
|
||||
iFace->removeAllData(source);
|
||||
return engine->newVariant(true);
|
||||
}
|
||||
|
||||
return context->throwError(error);
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::jsRemoveData(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 2) {
|
||||
return context->throwError(i18n("removeData() takes at least two arguments (the source and key names"));
|
||||
}
|
||||
|
||||
QString source = context->argument(0).toString();
|
||||
QString key = context->argument(1).toString();
|
||||
QString error;
|
||||
JavaScriptDataEngine *iFace = extractIFace(engine, error);
|
||||
|
||||
if (iFace) {
|
||||
iFace->removeData(source, key);
|
||||
return engine->newVariant(true);
|
||||
}
|
||||
|
||||
return context->throwError(error);
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::jsRemoveAllSources(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
QString error;
|
||||
JavaScriptDataEngine *iFace = extractIFace(engine, error);
|
||||
|
||||
if (iFace) {
|
||||
iFace->removeAllSources();
|
||||
return engine->newVariant(true);
|
||||
}
|
||||
|
||||
return context->throwError(error);
|
||||
}
|
||||
|
||||
QScriptValue JavaScriptDataEngine::callFunction(const QString &functionName, const QScriptValueList &args) const
|
||||
{
|
||||
QScriptValue fun = m_iface.property(functionName);
|
||||
if (fun.isFunction()) {
|
||||
QScriptContext *ctx = m_qscriptEngine->pushContext();
|
||||
ctx->setActivationObject(m_iface);
|
||||
QScriptValue rv = fun.call(m_iface, args);
|
||||
m_qscriptEngine->popContext();
|
||||
|
||||
if (m_qscriptEngine->hasUncaughtException()) {
|
||||
reportError();
|
||||
} else {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
return QScriptValue();
|
||||
}
|
||||
|
||||
void JavaScriptDataEngine::reportError() const
|
||||
{
|
||||
kDebug() << "Error: " << m_qscriptEngine->uncaughtException().toString()
|
||||
<< " at line " << m_qscriptEngine->uncaughtExceptionLineNumber() << endl;
|
||||
kDebug() << m_qscriptEngine->uncaughtExceptionBacktrace();
|
||||
}
|
||||
|
||||
QStringList JavaScriptDataEngine::sources() const
|
||||
{
|
||||
QScriptValueList args;
|
||||
QScriptValue rv = callFunction("sources", args);
|
||||
if (rv.isValid() && rv.isVariant()) {
|
||||
return rv.toVariant().toStringList();
|
||||
}
|
||||
|
||||
return QStringList();
|
||||
}
|
||||
|
||||
bool JavaScriptDataEngine::sourceRequestEvent(const QString &name)
|
||||
{
|
||||
QScriptValueList args;
|
||||
args << name;
|
||||
QScriptValue rv = callFunction("sourceRequestEvent", args);
|
||||
if (rv.isValid() && rv.isBool()) {
|
||||
return rv.toBool();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool JavaScriptDataEngine::updateSourceEvent(const QString &source)
|
||||
{
|
||||
QScriptValueList args;
|
||||
args << source;
|
||||
QScriptValue rv = callFunction("updateSourceEvent", args);
|
||||
if (rv.isValid() && rv.isBool()) {
|
||||
return rv.toBool();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
Plasma::Service *JavaScriptDataEngine::serviceForSource(const QString &source)
|
||||
{
|
||||
QScriptValue rv = callFunction("updateSourceEvent");
|
||||
if (rv.isValid() && rv.isVariant()) {
|
||||
return rv.toVariant().value<Plasma::Service*>();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
K_EXPORT_PLASMA_DATAENGINESCRIPTENGINE(javascriptdataengine, JavaScriptDataEngine)
|
||||
|
||||
#include <javascriptdataengine.moc>
|
||||
|
67
scriptengines/javascript/javascriptdataengine.h
Normal file
67
scriptengines/javascript/javascriptdataengine.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2009 Aaron Seigo <aseigo@kde.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 JAVASCRIPTDATAENGINE_H
|
||||
#define JAVASCRIPTDATAENGINE_H
|
||||
|
||||
#include <Plasma/DataEngineScript>
|
||||
|
||||
#include <QScriptValue>
|
||||
|
||||
class QScriptEngine;
|
||||
class QScriptContext;
|
||||
|
||||
class JavaScriptDataEngine : public Plasma::DataEngineScript
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int sourceCount WRITE jsSetMaxSourceCount READ maxSourceCount)
|
||||
Q_PROPERTY(int minimumPollingInterval WRITE jsSetMinimumPollingInterval READ minimumPollingInterval)
|
||||
Q_PROPERTY(int pollingInterval WRITE jsSetPollingInterval READ pollingInterval)
|
||||
|
||||
public:
|
||||
JavaScriptDataEngine(QObject *parent, const QVariantList &args);
|
||||
bool init();
|
||||
|
||||
QStringList sources() const;
|
||||
bool sourceRequestEvent(const QString &name);
|
||||
bool updateSourceEvent(const QString &source);
|
||||
//TODO: Plasma::Service *serviceForSource(const QString &source);
|
||||
|
||||
int maxSourceCount() const;
|
||||
void jsSetMaxSourceCount(int count);
|
||||
void jsSetMinimumPollingInterval(int interval);
|
||||
int minimumPollingInterval() const;
|
||||
void jsSetPollingInterval(int interval);
|
||||
int pollingInterval() const;
|
||||
|
||||
private:
|
||||
static JavaScriptDataEngine *extractIFace(QScriptEngine *engine, QString &error);
|
||||
static QScriptValue jsSetData(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue jsRemoveAllData(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue jsRemoveData(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue jsRemoveAllSources(QScriptContext *context, QScriptEngine *engine);
|
||||
|
||||
QScriptValue callFunction(const QString &functionName, const QScriptValueList &args) const;
|
||||
void reportError() const;
|
||||
|
||||
QScriptEngine *m_qscriptEngine;
|
||||
QScriptValue m_iface;
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
[Desktop Entry]
|
||||
Name=JavaScript DataEngine
|
||||
X-KDE-ServiceTypes=Plasma/ScriptEngine
|
||||
Type=Service
|
||||
Icon=text-x-script
|
||||
X-KDE-Library=plasma_dataenginescript_javascript
|
||||
X-Plasma-API=javascript
|
||||
X-Plasma-ComponentTypes=DataEngine
|
@ -40,6 +40,7 @@
|
||||
using namespace Plasma;
|
||||
|
||||
#include "bind_dataengine.h"
|
||||
#include "bind_i18n.h"
|
||||
|
||||
Q_DECLARE_METATYPE(QPainter*)
|
||||
Q_DECLARE_METATYPE(QStyleOptionGraphicsItem*)
|
||||
@ -78,52 +79,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Workaround the fact that QtScripts handling of variants seems a bit broken.
|
||||
*/
|
||||
QScriptValue variantToScriptValue(QScriptEngine *engine, QVariant var)
|
||||
{
|
||||
if (var.isNull()) {
|
||||
return engine->nullValue();
|
||||
}
|
||||
|
||||
switch(var.type())
|
||||
{
|
||||
case QVariant::Invalid:
|
||||
return engine->nullValue();
|
||||
case QVariant::Bool:
|
||||
return QScriptValue(engine, var.toBool());
|
||||
case QVariant::Date:
|
||||
return engine->newDate(var.toDateTime());
|
||||
case QVariant::DateTime:
|
||||
return engine->newDate(var.toDateTime());
|
||||
case QVariant::Double:
|
||||
return QScriptValue(engine, var.toDouble());
|
||||
case QVariant::Int:
|
||||
case QVariant::LongLong:
|
||||
return QScriptValue(engine, var.toInt());
|
||||
case QVariant::String:
|
||||
return QScriptValue(engine, var.toString());
|
||||
case QVariant::Time: {
|
||||
QDateTime t(QDate::currentDate(), var.toTime());
|
||||
return engine->newDate(t);
|
||||
}
|
||||
case QVariant::UInt:
|
||||
return QScriptValue(engine, var.toUInt());
|
||||
default:
|
||||
if (var.typeName() == QLatin1String("KUrl")) {
|
||||
return QScriptValue(engine, var.value<KUrl>().prettyUrl());
|
||||
} else if (var.typeName() == QLatin1String("QColor")) {
|
||||
return QScriptValue(engine, var.value<QColor>().name());
|
||||
} else if (var.typeName() == QLatin1String("QUrl")) {
|
||||
return QScriptValue(engine, var.value<QUrl>().toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return qScriptValueFromValue(engine, var);
|
||||
}
|
||||
|
||||
QScriptValue qScriptValueFromControls(QScriptEngine *engine, const Controls &controls)
|
||||
{
|
||||
return QScriptValue(engine, controls);
|
||||
@ -156,24 +111,6 @@ void controlsFromScriptValue(const QScriptValue& obj, Controls &controls)
|
||||
}
|
||||
}
|
||||
|
||||
QScriptValue qScriptValueFromData(QScriptEngine *engine, const DataEngine::Data &data)
|
||||
{
|
||||
DataEngine::Data::const_iterator begin = data.begin();
|
||||
DataEngine::Data::const_iterator end = data.end();
|
||||
DataEngine::Data::const_iterator it;
|
||||
|
||||
QScriptValue obj = engine->newObject();
|
||||
|
||||
for (it = begin; it != end; ++it) {
|
||||
//kDebug() << "setting" << it.key() << "to" << it.value();
|
||||
QString prop = it.key();
|
||||
prop.replace(' ', '_');
|
||||
obj.setProperty(prop, variantToScriptValue(engine, it.value()));
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
QScriptValue qScriptValueFromKConfigGroup(QScriptEngine *engine, const KConfigGroup &config)
|
||||
{
|
||||
QScriptValue obj = engine->newObject();
|
||||
@ -431,10 +368,7 @@ void SimpleJavaScriptApplet::setupObjects()
|
||||
m_engine->setDefaultPrototype(qMetaTypeId<Service*>(), m_engine->newQObject(new DummyService()));
|
||||
m_engine->setDefaultPrototype(qMetaTypeId<ServiceJob*>(), m_engine->newQObject(new ServiceJob(QString(), QString(), QMap<QString, QVariant>())));
|
||||
|
||||
global.setProperty("i18n", m_engine->newFunction(SimpleJavaScriptApplet::jsi18n));
|
||||
global.setProperty("i18nc", m_engine->newFunction(SimpleJavaScriptApplet::jsi18nc));
|
||||
global.setProperty("i18np", m_engine->newFunction(SimpleJavaScriptApplet::jsi18np));
|
||||
global.setProperty("i18ncp", m_engine->newFunction(SimpleJavaScriptApplet::jsi18ncp));
|
||||
bindI18N(m_engine);
|
||||
global.setProperty("dataEngine", m_engine->newFunction(SimpleJavaScriptApplet::dataEngine));
|
||||
global.setProperty("service", m_engine->newFunction(SimpleJavaScriptApplet::service));
|
||||
qScriptRegisterMetaType<DataEngine::Data>(m_engine, qScriptValueFromData, 0, QScriptValue());
|
||||
@ -497,89 +431,6 @@ void SimpleJavaScriptApplet::debug(const QString &msg)
|
||||
kDebug() << msg;
|
||||
}
|
||||
|
||||
#if 0
|
||||
QScriptValue SimpleJavaScriptApplet::dataEngine(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() != 1)
|
||||
return context->throwError("dataEngine takes one argument");
|
||||
|
||||
QString dataEngine = context->argument(0).toString();
|
||||
|
||||
Script *self = engine->fromScriptValue<Script*>(context->thisObject());
|
||||
|
||||
DataEngine *data = self->dataEngine(dataEngine);
|
||||
return engine->newQObject(data);
|
||||
}
|
||||
#endif
|
||||
|
||||
QScriptValue SimpleJavaScriptApplet::jsi18n(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 1) {
|
||||
return context->throwError(i18n("i18n() takes at least one argument"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18n(context->argument(0).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 1; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue SimpleJavaScriptApplet::jsi18nc(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 2) {
|
||||
return context->throwError(i18n("i18nc() takes at least two arguments"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18nc(context->argument(0).toString().toUtf8(),
|
||||
context->argument(1).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 2; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue SimpleJavaScriptApplet::jsi18np(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 2) {
|
||||
return context->throwError(i18n("i18np() takes at least two arguments"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18np(context->argument(0).toString().toUtf8(),
|
||||
context->argument(1).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 2; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue SimpleJavaScriptApplet::jsi18ncp(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() < 3) {
|
||||
return context->throwError(i18n("i18ncp() takes at least three arguments"));
|
||||
}
|
||||
|
||||
KLocalizedString message = ki18ncp(context->argument(0).toString().toUtf8(),
|
||||
context->argument(1).toString().toUtf8(),
|
||||
context->argument(2).toString().toUtf8());
|
||||
|
||||
int numArgs = context->argumentCount();
|
||||
for (int i = 3; i < numArgs; ++i) {
|
||||
message.subs(context->argument(i).toString());
|
||||
}
|
||||
|
||||
return engine->newVariant(message.toString());
|
||||
}
|
||||
|
||||
QScriptValue SimpleJavaScriptApplet::dataEngine(QScriptContext *context, QScriptEngine *engine)
|
||||
{
|
||||
if (context->argumentCount() != 1) {
|
||||
|
Loading…
Reference in New Issue
Block a user