2009-07-04 06:30:57 +02:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2010-06-30 00:11:15 +02:00
|
|
|
#include <Plasma/Package>
|
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
#include "javascriptservice.h"
|
|
|
|
#include "common/authorization.h"
|
|
|
|
#include "common/scriptenv.h"
|
2009-12-16 23:05:46 +01:00
|
|
|
#include "simplebindings/i18n.h"
|
2010-09-16 03:14:08 +02:00
|
|
|
#include "simplebindings/dataengine.h"
|
|
|
|
|
|
|
|
void registerNonGuiMetaTypes(QScriptEngine *engine);
|
2009-07-04 06:30:57 +02:00
|
|
|
|
|
|
|
JavaScriptDataEngine::JavaScriptDataEngine(QObject *parent, const QVariantList &args)
|
2010-02-22 12:33:21 +01:00
|
|
|
: DataEngineScript(parent)
|
2009-07-04 06:30:57 +02:00
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
Q_UNUSED(args);
|
2010-05-19 23:58:18 +02:00
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
m_qscriptEngine = new QScriptEngine(this);
|
|
|
|
m_env = new ScriptEnv(this, m_qscriptEngine);
|
|
|
|
|
2010-08-05 20:44:51 +02:00
|
|
|
connect(m_env, SIGNAL(reportError(ScriptEnv*,bool)), this, SLOT(reportError(ScriptEnv*,bool)));
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JavaScriptDataEngine::init()
|
|
|
|
{
|
|
|
|
QScriptValue global = m_qscriptEngine->globalObject();
|
|
|
|
|
|
|
|
bindI18N(m_qscriptEngine);
|
|
|
|
|
2010-06-22 03:55:53 +02:00
|
|
|
m_iface = m_qscriptEngine->newQObject(this);
|
|
|
|
m_iface.setScope(global);
|
2010-08-05 21:05:37 +02:00
|
|
|
m_env->addMainObjectProperties(m_iface);
|
|
|
|
|
2010-06-22 03:55:53 +02:00
|
|
|
global.setProperty("engine", m_iface);
|
2009-07-04 06:30:57 +02:00
|
|
|
|
|
|
|
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));
|
2010-08-06 06:21:42 +02:00
|
|
|
global.setProperty("Service", m_qscriptEngine->newFunction(JavaScriptDataEngine::serviceCtor));
|
2009-07-04 06:30:57 +02:00
|
|
|
|
2010-09-16 03:14:08 +02:00
|
|
|
registerNonGuiMetaTypes(m_qscriptEngine);
|
2009-07-04 06:30:57 +02:00
|
|
|
|
2010-01-07 03:27:54 +01:00
|
|
|
Authorization auth;
|
2010-06-22 03:55:53 +02:00
|
|
|
if (!m_env->importExtensions(description(), m_iface, auth)) {
|
2010-01-07 03:27:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-22 03:55:53 +02:00
|
|
|
return m_env->include(mainScript());
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
QScriptEngine *JavaScriptDataEngine::engine() const
|
|
|
|
{
|
|
|
|
return m_qscriptEngine;
|
|
|
|
}
|
|
|
|
|
2009-07-04 06:30:57 +02:00
|
|
|
void JavaScriptDataEngine::jsSetMaxSourceCount(int count)
|
|
|
|
{
|
|
|
|
setMaxSourceCount(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int JavaScriptDataEngine::maxSourceCount() const
|
|
|
|
{
|
|
|
|
return dataEngine()->maxSourceCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaScriptDataEngine::jsSetMinimumPollingInterval(int interval)
|
|
|
|
{
|
|
|
|
setMinimumPollingInterval(interval);
|
|
|
|
}
|
|
|
|
|
2010-05-19 23:58:18 +02:00
|
|
|
int JavaScriptDataEngine::jsMinimumPollingInterval() const
|
2009-07-04 06:30:57 +02:00
|
|
|
{
|
|
|
|
return minimumPollingInterval();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaScriptDataEngine::jsSetPollingInterval(int interval)
|
|
|
|
{
|
2010-05-19 23:58:18 +02:00
|
|
|
m_pollingInterval = interval;
|
2009-07-04 06:30:57 +02:00
|
|
|
setPollingInterval(interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int JavaScriptDataEngine::pollingInterval() const
|
|
|
|
{
|
2010-05-19 23:58:18 +02:00
|
|
|
return m_pollingInterval;
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue JavaScriptDataEngine::jsSetData(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2010-09-16 09:02:19 +02:00
|
|
|
if (context->argumentCount() < 1) {
|
|
|
|
return context->throwError(i18n("setData() takes at least one argument"));
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString error;
|
|
|
|
JavaScriptDataEngine *iFace = extractIFace(engine, error);
|
|
|
|
|
|
|
|
if (!iFace) {
|
|
|
|
return context->throwError(error);
|
|
|
|
}
|
|
|
|
|
2010-05-19 23:58:18 +02:00
|
|
|
const QString source = context->argument(0).toString();
|
2010-09-16 09:02:19 +02:00
|
|
|
if (context->argumentCount() == 1) {
|
|
|
|
iFace->setData(source, DataEngine::Data());
|
|
|
|
} else if (context->argument(1).isArray() || context->argument(1).isObject()) {
|
|
|
|
kDebug( )<< "array or object";
|
2010-05-20 00:22:52 +02:00
|
|
|
QScriptValueIterator it(context->argument(1));
|
|
|
|
DataEngine::Data data;
|
2010-09-16 07:36:28 +02:00
|
|
|
|
2010-05-20 00:22:52 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
data.insert(it.name(), it.value().toVariant());
|
|
|
|
}
|
|
|
|
|
|
|
|
iFace->setData(source, data);
|
2009-07-04 06:30:57 +02:00
|
|
|
} else {
|
2010-09-16 09:02:19 +02:00
|
|
|
const QString value = context->argument(1).toString();
|
2010-05-20 00:22:52 +02:00
|
|
|
if (context->argumentCount() > 2) {
|
2010-09-16 07:36:28 +02:00
|
|
|
if (context->argument(2).isArray() || context->argument(2).isObject()) {
|
|
|
|
QScriptValueIterator it(context->argument(2));
|
|
|
|
DataEngine::Data data;
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
data.insert(it.name(), it.value().toVariant());
|
|
|
|
}
|
|
|
|
|
|
|
|
iFace->setData(source, value, data);
|
|
|
|
} else {
|
|
|
|
iFace->setData(source, value, context->argument(2).toString());
|
|
|
|
}
|
2010-05-20 00:22:52 +02:00
|
|
|
} else {
|
|
|
|
iFace->setData(source, value);
|
|
|
|
}
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2009-07-08 14:51:41 +02:00
|
|
|
return context->throwError(i18n("removeData() takes at least two arguments (the source and key names)"));
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
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()) {
|
2010-08-06 21:42:08 +02:00
|
|
|
QScriptValue v = engine->newQObject(service, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);
|
2010-08-06 06:21:42 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete service;
|
2010-08-07 07:03:08 +02:00
|
|
|
return context->throwError(i18n("Requested service %1 was not found in the Package.", serviceName));
|
2010-08-06 06:21:42 +02:00
|
|
|
}
|
|
|
|
|
2010-08-05 23:14:00 +02:00
|
|
|
QScriptValue JavaScriptDataEngine::callFunction(const QString &functionName, const QScriptValueList &args)
|
2009-07-04 06:30:57 +02:00
|
|
|
{
|
2010-08-05 23:14:00 +02:00
|
|
|
QScriptValue func = m_iface.property(functionName);
|
|
|
|
return m_env->callFunction(func, args, m_iface);
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
void JavaScriptDataEngine::reportError(ScriptEnv *env, bool fatal) const
|
2009-07-04 06:30:57 +02:00
|
|
|
{
|
2010-01-07 03:27:54 +01:00
|
|
|
Q_UNUSED(fatal)
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
kDebug() << "Error: " << env->engine()->uncaughtException().toString()
|
|
|
|
<< " at line " << env->engine()->uncaughtExceptionLineNumber() << endl;
|
|
|
|
kDebug() << env->engine()->uncaughtExceptionBacktrace();
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList JavaScriptDataEngine::sources() const
|
|
|
|
{
|
2010-08-05 23:14:00 +02:00
|
|
|
JavaScriptDataEngine *unconst = const_cast<JavaScriptDataEngine *>(this);
|
2009-07-04 06:30:57 +02:00
|
|
|
QScriptValueList args;
|
2010-08-05 23:14:00 +02:00
|
|
|
QScriptValue rv = unconst->callFunction("sources", args);
|
2010-08-02 21:59:49 +02:00
|
|
|
if (rv.isValid() && (rv.isVariant() || rv.isArray())) {
|
2009-07-04 06:30:57 +02:00
|
|
|
return rv.toVariant().toStringList();
|
|
|
|
}
|
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
return DataEngineScript::sources();
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JavaScriptDataEngine::sourceRequestEvent(const QString &name)
|
|
|
|
{
|
|
|
|
QScriptValueList args;
|
|
|
|
args << name;
|
2010-08-05 23:14:00 +02:00
|
|
|
m_env->callEventListeners("sourceRequestEvent", args);
|
2009-07-04 06:30:57 +02:00
|
|
|
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;
|
2010-08-05 23:14:00 +02:00
|
|
|
m_env->callEventListeners("updateSourcEvent", args);
|
2009-07-04 06:30:57 +02:00
|
|
|
QScriptValue rv = callFunction("updateSourceEvent", args);
|
|
|
|
if (rv.isValid() && rv.isBool()) {
|
|
|
|
return rv.toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::Service *JavaScriptDataEngine::serviceForSource(const QString &source)
|
|
|
|
{
|
2010-01-21 00:29:53 +01:00
|
|
|
QScriptValueList args;
|
|
|
|
args << source;
|
2010-08-05 23:14:00 +02:00
|
|
|
QScriptValue rv = callFunction("serviceForSource", args);
|
2010-01-21 00:29:53 +01:00
|
|
|
if (rv.isValid() && rv.isQObject()) {
|
2010-08-06 06:21:42 +02:00
|
|
|
Plasma::Service *service = qobject_cast<Plasma::Service *>(rv.toQObject());
|
|
|
|
if (service) {
|
|
|
|
if (service->destination().isEmpty()) {
|
|
|
|
service->setDestination(source);
|
|
|
|
}
|
2010-08-06 21:34:59 +02:00
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
return service;
|
|
|
|
} else {
|
|
|
|
delete rv.toQObject();
|
|
|
|
}
|
2009-07-04 06:30:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-21 00:29:53 +01:00
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
QString JavaScriptDataEngine::filePath(const char *type, const QString &file) const
|
|
|
|
{
|
2010-09-05 00:51:32 +02:00
|
|
|
const QString path = m_env->filePathFromScriptContext(type, file);
|
2010-09-16 02:33:09 +02:00
|
|
|
if (!path.isEmpty()) {
|
2010-09-05 00:51:32 +02:00
|
|
|
return path;
|
2010-09-05 00:22:40 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 06:21:42 +02:00
|
|
|
return package()->filePath(type, file);
|
|
|
|
}
|
|
|
|
|
2010-06-30 00:11:15 +02:00
|
|
|
bool JavaScriptDataEngine::include(const QString &script)
|
|
|
|
{
|
2010-09-05 00:22:40 +02:00
|
|
|
const QString path = filePath("scripts", script);
|
2010-06-30 00:11:15 +02:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_env->include(path);
|
|
|
|
}
|
|
|
|
|
2009-07-04 06:30:57 +02:00
|
|
|
K_EXPORT_PLASMA_DATAENGINESCRIPTENGINE(javascriptdataengine, JavaScriptDataEngine)
|
|
|
|
|
|
|
|
#include <javascriptdataengine.moc>
|
|
|
|
|