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.
This commit is contained in:
Aaron Seigo 2011-04-04 14:42:25 +02:00
parent 9a07bb2f93
commit da186219ae
4 changed files with 56 additions and 16 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<DataEngine *>(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<DataEngine *>(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<Plasma::IntervalAlignment>(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<DataEngine *>(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();
}

View File

@ -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<DataEngineReceiver*> 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;