diff --git a/CMakeLists.txt b/CMakeLists.txt index e3bbddf27..b79b95465 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ set(plasma_LIB_SRCS private/applethandle.cpp private/associatedapplicationmanager.cpp private/datacontainer_p.cpp + private/dataenginebindings.cpp private/dataengineconsumer.cpp private/dataengineservice.cpp private/denyallauthorization.cpp diff --git a/private/dataenginebindings.cpp b/private/dataenginebindings.cpp new file mode 100644 index 000000000..8fb608028 --- /dev/null +++ b/private/dataenginebindings.cpp @@ -0,0 +1,66 @@ +/* + * Copyright 2007 Richard J. Moore + * + * 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 "dataenginebindings_p.h" + +typedef Plasma::Service *ServicePtr; +QScriptValue qScriptValueFromService(QScriptEngine *engine, const ServicePtr &service) +{ + return engine->newQObject(const_cast(service), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); +} + +void serviceFromQScriptValue(const QScriptValue &scriptValue, ServicePtr &service) +{ + QObject *obj = scriptValue.toQObject(); + service = static_cast(obj); +} + +typedef Plasma::DataEngine *DataEnginePtr; +QScriptValue qScriptValueFromDataEngine(QScriptEngine *engine, const DataEnginePtr &dataEngine) +{ + return engine->newQObject(const_cast(dataEngine), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); +} + +void dataEngineFromQScriptValue(const QScriptValue &scriptValue, DataEnginePtr &dataEngine) +{ + QObject *obj = scriptValue.toQObject(); + dataEngine = static_cast(obj); +} + +typedef Plasma::ServiceJob *ServiceJobPtr; +QScriptValue qScriptValueFromServiceJob(QScriptEngine *engine, const ServiceJobPtr &serviceJob) +{ + return engine->newQObject(const_cast(serviceJob), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); +} + +void serviceJobFromQScriptValue(const QScriptValue &scriptValue, ServiceJobPtr &serviceJob) +{ + QObject *obj = scriptValue.toQObject(); + serviceJob = static_cast(obj); +} + +void registerDataEngineMetaTypes(QScriptEngine *engine) +{ + qRegisterMetaType("Plasma::DataEngine::Data"); + qRegisterMetaType("DataEngine::Data"); + qScriptRegisterMapMetaType(engine); + qScriptRegisterMetaType(engine, qScriptValueFromService, serviceFromQScriptValue); + qScriptRegisterMetaType(engine, qScriptValueFromDataEngine, dataEngineFromQScriptValue); + qScriptRegisterMetaType(engine, qScriptValueFromServiceJob, serviceJobFromQScriptValue); +} + diff --git a/private/dataenginebindings_p.h b/private/dataenginebindings_p.h new file mode 100644 index 000000000..671d7896d --- /dev/null +++ b/private/dataenginebindings_p.h @@ -0,0 +1,85 @@ +/* + * Copyright 2007 Richard J. Moore + * + * 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 DATAENGINE_H +#define DATAENGINE_H + +#include +#include +#include +#include + +#include +#include +#include + +using namespace Plasma; + +Q_DECLARE_METATYPE(Service*) +Q_DECLARE_METATYPE(ServiceJob*) +Q_DECLARE_METATYPE(DataEngine::Dict) +Q_DECLARE_METATYPE(DataEngine::Data) + +template +QScriptValue qScriptValueFromMap(QScriptEngine *eng, const M &map) +{ + //kDebug() << "qScriptValueFromMap called"; + QScriptValue obj = eng->newObject(); + typename M::const_iterator begin = map.constBegin(); + typename M::const_iterator end = map.constEnd(); + typename M::const_iterator it; + for (it = begin; it != end; ++it) { + if (it.value().type() == QVariant::Hash) { + obj.setProperty(it.key(), qScriptValueFromMap(eng, it.value().toHash())); + } else if (it.value().type() == QVariant::Map) { + obj.setProperty(it.key(), qScriptValueFromMap(eng, it.value().toMap())); + } else { + obj.setProperty(it.key(), qScriptValueFromValue(eng, it.value())); + } + } + + return obj; +} + +template +void qScriptValueToMap(const QScriptValue &value, M &map) +{ + //kDebug() << "qScriptValueToMap called"; + QScriptValueIterator it(value); + while (it.hasNext()) { + it.next(); + map[it.name()] = qscriptvalue_cast(it.value()); + } +} + +template +int qScriptRegisterMapMetaType( + QScriptEngine *engine, + const QScriptValue &prototype = QScriptValue() +#ifndef qdoc + , T * /* dummy */ = 0 +#endif +) +{ + return qScriptRegisterMetaType(engine, qScriptValueFromMap, qScriptValueToMap, prototype); +} + +void registerDataEngineMetaTypes(QScriptEngine *engine); + +#endif // DATAENGINE_H + diff --git a/widgets/declarativewidget.cpp b/widgets/declarativewidget.cpp index 24721e269..917945bfb 100644 --- a/widgets/declarativewidget.cpp +++ b/widgets/declarativewidget.cpp @@ -35,6 +35,7 @@ #include #include "private/declarative/declarativenetworkaccessmanagerfactory_p.h" +#include "private/dataenginebindings_p.h" namespace Plasma { @@ -99,6 +100,7 @@ void DeclarativeWidgetPrivate::execute(const QString &fileName) //binds things like kconfig and icons kdeclarative.setupBindings(); scriptEngine = kdeclarative.scriptEngine(); + registerDataEngineMetaTypes(scriptEngine); if (delay) { QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));