an embryo of Service access

svn path=/trunk/KDE/kdebase/workspace/plasma/scriptengines/javascript/; revision=906206
This commit is contained in:
Marco Martin 2009-01-05 19:16:44 +00:00
parent c83a293c40
commit 35a255722e
3 changed files with 66 additions and 1 deletions

View File

@ -23,14 +23,19 @@
#include <KDebug>
#include <Plasma/DataEngine>
#include <Plasma/Service>
#include <Plasma/ServiceJob>
using namespace Plasma;
Q_DECLARE_METATYPE(DataEngine*)
Q_DECLARE_METATYPE(Service*)
Q_DECLARE_METATYPE(ServiceJob*)
Q_DECLARE_METATYPE(QVariant)
Q_DECLARE_METATYPE(DataEngine::Dict)
Q_DECLARE_METATYPE(DataEngine::Data)
template <class M>
QScriptValue qScriptValueFromMap(QScriptEngine *eng, const M &map)
{

View File

@ -27,6 +27,7 @@
#include <KDebug>
#include <KLocale>
#include <KStandardDirs>
#include <KConfigGroup>
#include <Plasma/Applet>
#include <Plasma/Svg>
@ -47,6 +48,7 @@ Q_DECLARE_METATYPE(AppletInterface*)
Q_DECLARE_METATYPE(Applet*)
Q_DECLARE_METATYPE(QGraphicsWidget*)
Q_DECLARE_METATYPE(QGraphicsLayout*)
Q_DECLARE_METATYPE(KConfigGroup)
Q_SCRIPT_DECLARE_QMETAOBJECT(AppletInterface, SimpleJavaScriptApplet*)
@ -59,6 +61,15 @@ QScriptValue constructQRectFClass(QScriptEngine *engine);
QScriptValue constructQPointClass(QScriptEngine *engine);
QScriptValue constructQSizeFClass(QScriptEngine *engine);
class DummyService : public Service
{
ServiceJob *createJob(const QString &operation,
QMap<QString, QVariant> &parameters)
{
return 0;
}
};
/*
* Workaround the fact that QtScripts handling of variants seems a bit broken.
*/
@ -116,6 +127,37 @@ QScriptValue qScriptValueFromData(QScriptEngine *engine, const DataEngine::Data
return obj;
}
QScriptValue qScriptValueFromKConfigGroup(QScriptEngine *engine, const KConfigGroup &config)
{
QScriptValue obj = engine->newObject();
if (!config.isValid()) {
return obj;
}
QMap<QString, QString> entryMap = config.entryMap();
QMap<QString, QString>::const_iterator it = entryMap.begin();
QMap<QString, QString>::const_iterator begin = it;
QMap<QString, QString>::const_iterator end = entryMap.end();
for (it = begin; it != end; ++it) {
//kDebug() << "setting" << it.key() << "to" << it.value();
QString prop = it.key();
prop.replace(' ', '_');
obj.setProperty(prop, variant2ScriptValue(engine, it.value()));
}
return obj;
}
void kConfigGroupFromScriptValue(const QScriptValue& obj, KConfigGroup &config)
{
QScriptValueIterator it(obj);
while (it.hasNext()) {
config.writeEntry(it.name(), it.value().toString());
}
}
SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantList &args)
: Plasma::AppletScript(parent)
@ -329,6 +371,8 @@ void SimpleJavaScriptApplet::setupObjects()
// Bindings for data engine
m_engine->setDefaultPrototype(qMetaTypeId<DataEngine*>(), m_engine->newQObject(new DataEngine()));
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>())));
#if 0
fun = m_engine->newFunction(SimpleJavaScriptApplet::dataEngine);
m_self.setProperty("dataEngine", fun);
@ -339,6 +383,7 @@ void SimpleJavaScriptApplet::setupObjects()
// qScriptRegisterMapMetaType<DataEngine::Dict>(m_engine);
// qScriptRegisterMapMetaType<DataEngine::Data>(m_engine);
qScriptRegisterMetaType<DataEngine::Data>(m_engine, qScriptValueFromData, 0, QScriptValue());
qScriptRegisterMetaType<KConfigGroup>(m_engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue());
// Expose applet interface
m_interface = new AppletInterface(this);

View File

@ -1,3 +1,8 @@
engine = dataEngine("nowplaying");
players = engine.sources;
watchingPlayer = players[0];
controller = engine.serviceForSource(watchingPlayer);
plasmoid.dataUpdate = function(a, b)
{
label.text = "Playing " + b.Title + " by " + b.Artist + ". time: " +
@ -6,6 +11,13 @@ plasmoid.dataUpdate = function(a, b)
plasmoid.stop = function()
{
data = engine.serviceForSource(watchingPlayer).operationDescription("stop");
print(data+controller.name());
for ( var i in data ) {
print(i + ' -> ' + data[i] );
}
controller.startOperationCall(controller.operationDescription("stop"));
print("stopping");
}
@ -18,6 +30,9 @@ stop = new PushButton();
stop.text = "Stop";
layout.addItem(stop);
controller.associateWidget(stop, "stop");
print(controller.operationNames());
stop["clicked()"].connect(plasmoid.stop);
plasmoid.dataEngine("nowplaying").connectSource("org.mpris.amarok", plasmoid, 500);
engine.connectSource(watchingPlayer, plasmoid, 500);