allow the common properties implemented in ScriptEnv (for code sharing, consistency) to be assigned to any object (e.g. the m_self/m_iface script values created in the DataEngine and Runner ScriptEngines)
svn path=/trunk/KDE/kdebase/runtime/; revision=1159605
This commit is contained in:
parent
7e92e302af
commit
003d75fadb
@ -47,6 +47,8 @@ bool JavaScriptDataEngine::init()
|
|||||||
|
|
||||||
m_iface = m_qscriptEngine->newQObject(this);
|
m_iface = m_qscriptEngine->newQObject(this);
|
||||||
m_iface.setScope(global);
|
m_iface.setScope(global);
|
||||||
|
m_env->addMainObjectProperties(m_iface);
|
||||||
|
|
||||||
global.setProperty("engine", m_iface);
|
global.setProperty("engine", m_iface);
|
||||||
|
|
||||||
global.setProperty("setData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsSetData));
|
global.setProperty("setData", m_qscriptEngine->newFunction(JavaScriptDataEngine::jsSetData));
|
||||||
|
@ -134,6 +134,7 @@ void JavaScriptRunner::setupObjects()
|
|||||||
// Expose the runner
|
// Expose the runner
|
||||||
m_self = m_engine->newQObject(this);
|
m_self = m_engine->newQObject(this);
|
||||||
m_self.setScope(global);
|
m_self.setScope(global);
|
||||||
|
m_env->addMainObjectProperties(m_self);
|
||||||
|
|
||||||
global.setProperty("runner", m_self);
|
global.setProperty("runner", m_self);
|
||||||
}
|
}
|
||||||
|
@ -51,23 +51,35 @@ ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
|
|||||||
m_allowedUrls(NoUrls),
|
m_allowedUrls(NoUrls),
|
||||||
m_engine(engine)
|
m_engine(engine)
|
||||||
{
|
{
|
||||||
QScriptValue global = m_engine->globalObject();
|
connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(signalException()));
|
||||||
|
|
||||||
// Add utility functions
|
setupGlobalObject();
|
||||||
global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
|
}
|
||||||
global.setProperty("debug", m_engine->newFunction(ScriptEnv::debug));
|
|
||||||
global.setProperty("listAddons", m_engine->newFunction(ScriptEnv::listAddons));
|
ScriptEnv::~ScriptEnv()
|
||||||
global.setProperty("loadAddon", m_engine->newFunction(ScriptEnv::loadAddon));
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptEnv::setupGlobalObject()
|
||||||
|
{
|
||||||
|
QScriptValue global = m_engine->globalObject();
|
||||||
|
|
||||||
// Add an accessor so we can find the scriptenv given only the engine. The
|
// Add an accessor so we can find the scriptenv given only the engine. The
|
||||||
// property is hidden from scripts.
|
// property is hidden from scripts.
|
||||||
global.setProperty("__plasma_scriptenv", m_engine->newQObject(this),
|
global.setProperty("__plasma_scriptenv", m_engine->newQObject(this),
|
||||||
QScriptValue::ReadOnly|QScriptValue::Undeletable|QScriptValue::SkipInEnumeration);
|
QScriptValue::ReadOnly|QScriptValue::Undeletable|QScriptValue::SkipInEnumeration);
|
||||||
connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(signalException()));
|
|
||||||
|
// Add utility functions
|
||||||
|
global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
|
||||||
|
global.setProperty("debug", m_engine->newFunction(ScriptEnv::debug));
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptEnv::~ScriptEnv()
|
void ScriptEnv::addMainObjectProperties(QScriptValue &value)
|
||||||
{
|
{
|
||||||
|
value.setProperty("listAddons", m_engine->newFunction(ScriptEnv::listAddons));
|
||||||
|
value.setProperty("loadAddon", m_engine->newFunction(ScriptEnv::loadAddon));
|
||||||
|
value.setProperty("addEventListener", m_engine->newFunction(ScriptEnv::addEventListener));
|
||||||
|
value.setProperty("removeEventListener", m_engine->newFunction(ScriptEnv::removeEventListener));
|
||||||
}
|
}
|
||||||
|
|
||||||
QScriptEngine *ScriptEnv::engine() const
|
QScriptEngine *ScriptEnv::engine() const
|
||||||
@ -441,6 +453,34 @@ QScriptValue ScriptEnv::registerAddon(QScriptContext *context, QScriptEngine *en
|
|||||||
return engine->undefinedValue();
|
return engine->undefinedValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QScriptValue ScriptEnv::addEventListener(QScriptContext *context, QScriptEngine *engine)
|
||||||
|
{
|
||||||
|
if (context->argumentCount() < 2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
||||||
|
if (!env) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return env->addEventListener(context->argument(0).toString(), context->argument(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
QScriptValue ScriptEnv::removeEventListener(QScriptContext *context, QScriptEngine *engine)
|
||||||
|
{
|
||||||
|
if (context->argumentCount() < 2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
|
||||||
|
if (!env) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return env->removeEventListener(context->argument(0).toString(), context->argument(1));
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptEnv::callFunction(QScriptValue &func, const QScriptValueList &args, const QScriptValue &activator)
|
void ScriptEnv::callFunction(QScriptValue &func, const QScriptValueList &args, const QScriptValue &activator)
|
||||||
{
|
{
|
||||||
if (!func.isFunction()) {
|
if (!func.isFunction()) {
|
||||||
@ -478,21 +518,27 @@ bool ScriptEnv::callEventListeners(const QString &event, const QScriptValueList
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEnv::addEventListener(const QString &event, const QScriptValue &func)
|
bool ScriptEnv::addEventListener(const QString &event, const QScriptValue &func)
|
||||||
{
|
{
|
||||||
if (func.isFunction()) {
|
if (func.isFunction() && !event.isEmpty()) {
|
||||||
m_eventListeners[event.toLower()].append(func);
|
m_eventListeners[event.toLower()].append(func);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEnv::removeEventListener(const QString &event, const QScriptValue &func)
|
bool ScriptEnv::removeEventListener(const QString &event, const QScriptValue &func)
|
||||||
{
|
{
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
if (func.isFunction()) {
|
if (func.isFunction()) {
|
||||||
QScriptValueList funcs = m_eventListeners.value("mousepress");
|
QScriptValueList funcs = m_eventListeners.value(event);
|
||||||
QMutableListIterator<QScriptValue> it(funcs);//m_eventListeners.value("mousepress"));
|
QMutableListIterator<QScriptValue> it(funcs);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
if (it.next().equals(func)) {
|
if (it.next().equals(func)) {
|
||||||
it.remove();
|
it.remove();
|
||||||
|
found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -502,6 +548,8 @@ void ScriptEnv::removeEventListener(const QString &event, const QScriptValue &fu
|
|||||||
m_eventListeners.insert(event.toLower(), funcs);
|
m_eventListeners.insert(event.toLower(), funcs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef USEGUI
|
#ifndef USEGUI
|
||||||
|
@ -40,6 +40,12 @@ public:
|
|||||||
ScriptEnv(QObject *parent, QScriptEngine *engine);
|
ScriptEnv(QObject *parent, QScriptEngine *engine);
|
||||||
~ScriptEnv();
|
~ScriptEnv();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds common properties (e.g. registerEventFilter)
|
||||||
|
* for a 'main' object (e.g. plasmoid, engine, runner)
|
||||||
|
*/
|
||||||
|
void addMainObjectProperties(QScriptValue &value);
|
||||||
|
|
||||||
/** Returns the QScriptEngine in use. */
|
/** Returns the QScriptEngine in use. */
|
||||||
QScriptEngine *engine() const;
|
QScriptEngine *engine() const;
|
||||||
|
|
||||||
@ -54,8 +60,8 @@ public:
|
|||||||
|
|
||||||
bool checkForErrors(bool fatal);
|
bool checkForErrors(bool fatal);
|
||||||
|
|
||||||
void addEventListener(const QString &event, const QScriptValue &func);
|
bool addEventListener(const QString &event, const QScriptValue &func);
|
||||||
void removeEventListener(const QString &event, const QScriptValue &func);
|
bool removeEventListener(const QString &event, const QScriptValue &func);
|
||||||
|
|
||||||
void callFunction(QScriptValue &func, const QScriptValueList &args = QScriptValueList(), const QScriptValue &activator = QScriptValue());
|
void callFunction(QScriptValue &func, const QScriptValueList &args = QScriptValueList(), const QScriptValue &activator = QScriptValue());
|
||||||
bool callEventListeners(const QString &event, const QScriptValueList &args = QScriptValueList());
|
bool callEventListeners(const QString &event, const QScriptValueList &args = QScriptValueList());
|
||||||
@ -77,11 +83,16 @@ private:
|
|||||||
static QScriptValue listAddons(QScriptContext *context, QScriptEngine *engine);
|
static QScriptValue listAddons(QScriptContext *context, QScriptEngine *engine);
|
||||||
static QScriptValue loadAddon(QScriptContext *context, QScriptEngine *engine);
|
static QScriptValue loadAddon(QScriptContext *context, QScriptEngine *engine);
|
||||||
static QScriptValue registerAddon(QScriptContext *context, QScriptEngine *engine);
|
static QScriptValue registerAddon(QScriptContext *context, QScriptEngine *engine);
|
||||||
|
static QScriptValue addEventListener(QScriptContext *context, QScriptEngine *engine);
|
||||||
|
static QScriptValue removeEventListener(QScriptContext *context, QScriptEngine *engine);
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void signalException();
|
void signalException();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/** Call to set common properties on the global object **/
|
||||||
|
void setupGlobalObject();
|
||||||
|
|
||||||
QSet<QString> m_extensions;
|
QSet<QString> m_extensions;
|
||||||
AllowedUrls m_allowedUrls;
|
AllowedUrls m_allowedUrls;
|
||||||
QScriptEngine *m_engine;
|
QScriptEngine *m_engine;
|
||||||
|
Loading…
Reference in New Issue
Block a user