From da186219aee11ddf01dd30d9704412061168454c Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Mon, 4 Apr 2011 14:42:25 +0200 Subject: [PATCH] give connectAllSources the same magic sauce connectSource does thanks to emilsedgh for having the special super power of using precisely what hasn't been properly implemented ... and then reporting it with nice simple examples we can test with! THAT, my friends, is how it is done. --- .../plasmoid/declarativeappletscript.cpp | 1 + .../plasmoid/simplejavascriptapplet.cpp | 1 + .../simplebindings/dataenginereceiver.cpp | 68 ++++++++++++++----- .../simplebindings/dataenginereceiver.h | 2 + 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/scriptengines/javascript/plasmoid/declarativeappletscript.cpp b/scriptengines/javascript/plasmoid/declarativeappletscript.cpp index 1a56570d4..653726c7c 100644 --- a/scriptengines/javascript/plasmoid/declarativeappletscript.cpp +++ b/scriptengines/javascript/plasmoid/declarativeappletscript.cpp @@ -411,6 +411,7 @@ QScriptValue DeclarativeAppletScript::dataEngine(QScriptContext *context, QScrip Plasma::DataEngine *dataEngine = interface->dataEngine(dataEngineName); QScriptValue v = engine->newQObject(dataEngine, QScriptEngine::QtOwnership, QScriptEngine::PreferExistingWrapperObject); v.setProperty("connectSource", engine->newFunction(DataEngineReceiver::connectSource)); + v.setProperty("connectAllSources", engine->newFunction(DataEngineReceiver::connectAllSources)); v.setProperty("disconnectSource", engine->newFunction(DataEngineReceiver::disconnectSource)); return v; } diff --git a/scriptengines/javascript/plasmoid/simplejavascriptapplet.cpp b/scriptengines/javascript/plasmoid/simplejavascriptapplet.cpp index f39eb6ecb..0076cd882 100644 --- a/scriptengines/javascript/plasmoid/simplejavascriptapplet.cpp +++ b/scriptengines/javascript/plasmoid/simplejavascriptapplet.cpp @@ -591,6 +591,7 @@ QScriptValue SimpleJavaScriptApplet::dataEngine(QScriptContext *context, QScript DataEngine *dataEngine = interface->dataEngine(dataEngineName); QScriptValue v = engine->newQObject(dataEngine, QScriptEngine::QtOwnership, QScriptEngine::PreferExistingWrapperObject); v.setProperty("connectSource", engine->newFunction(DataEngineReceiver::connectSource)); + v.setProperty("connectAllSources", engine->newFunction(DataEngineReceiver::connectAllSources)); v.setProperty("disconnectSource", engine->newFunction(DataEngineReceiver::disconnectSource)); return v; } diff --git a/scriptengines/javascript/simplebindings/dataenginereceiver.cpp b/scriptengines/javascript/simplebindings/dataenginereceiver.cpp index 034cc5eaf..9d01f9e7a 100644 --- a/scriptengines/javascript/simplebindings/dataenginereceiver.cpp +++ b/scriptengines/javascript/simplebindings/dataenginereceiver.cpp @@ -86,24 +86,10 @@ DataEngineReceiver *DataEngineReceiver::getReceiver(Plasma::DataEngine *dataEngi return 0; } -QScriptValue DataEngineReceiver::connectSource(QScriptContext *context, QScriptEngine *engine) +QObject *DataEngineReceiver::extractTargetQObject(QScriptEngine *engine, const QString &source, const QScriptValue &v, Plasma::DataEngine *dataEngine) { - if (context->argumentCount() < 2) { - return engine->undefinedValue(); - } - - DataEngine *dataEngine = qobject_cast(context->thisObject().toQObject()); - if (!dataEngine) { - return engine->undefinedValue(); - } - - const QString source = context->argument(0).toString(); - if (source.isEmpty()) { - return engine->undefinedValue(); - } - QObject *obj = 0; - QScriptValue v = context->argument(1); + // if it's a function we get, then use that directly // next see if it is a qobject with a good slot; if it is then use that directly // otherwise, try to use the object with a dataUpdated method call @@ -127,6 +113,56 @@ QScriptValue DataEngineReceiver::connectSource(QScriptContext *context, QScriptE } } + return obj; +} + +QScriptValue DataEngineReceiver::connectAllSources(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() < 1) { + return engine->undefinedValue(); + } + + DataEngine *dataEngine = qobject_cast(context->thisObject().toQObject()); + if (!dataEngine) { + return engine->undefinedValue(); + } + + int pollingInterval = 0; + Plasma::IntervalAlignment intervalAlignment = Plasma::NoAlignment; + if (context->argumentCount() > 1) { + pollingInterval = context->argument(2).toInt32(); + + if (context->argumentCount() > 2) { + intervalAlignment = static_cast(context->argument(4).toInt32()); + } + } + + QObject *obj = extractTargetQObject(engine, QString(), context->argument(0), dataEngine); + if (!obj) { + return engine->undefinedValue(); + } + + dataEngine->connectAllSources(obj, pollingInterval, intervalAlignment); + return true; +} + +QScriptValue DataEngineReceiver::connectSource(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() < 2) { + return engine->undefinedValue(); + } + + DataEngine *dataEngine = qobject_cast(context->thisObject().toQObject()); + if (!dataEngine) { + return engine->undefinedValue(); + } + + const QString source = context->argument(0).toString(); + if (source.isEmpty()) { + return engine->undefinedValue(); + } + + QObject *obj = extractTargetQObject(engine, source, context->argument(1), dataEngine); if (!obj) { return engine->undefinedValue(); } diff --git a/scriptengines/javascript/simplebindings/dataenginereceiver.h b/scriptengines/javascript/simplebindings/dataenginereceiver.h index b1559d8e6..f1f2a426c 100644 --- a/scriptengines/javascript/simplebindings/dataenginereceiver.h +++ b/scriptengines/javascript/simplebindings/dataenginereceiver.h @@ -36,6 +36,7 @@ public: bool isValid() const; static QScriptValue connectSource(QScriptContext *context, QScriptEngine *engine); + static QScriptValue connectAllSources(QScriptContext *context, QScriptEngine *engine); static QScriptValue disconnectSource(QScriptContext *context, QScriptEngine *engine); static QSet s_receivers; @@ -46,6 +47,7 @@ public Q_SLOTS: private: static DataEngineReceiver *getReceiver(Plasma::DataEngine *dataEngine, const QString &source, const QScriptValue &v); + static QObject *extractTargetQObject(QScriptEngine *engine, const QString &source, const QScriptValue &v, Plasma::DataEngine *dataEngine); const Plasma::DataEngine *m_engine; const QString m_source;