save fetching the env multiple times when we already have it

svn path=/trunk/KDE/kdebase/runtime/; revision=1171426
This commit is contained in:
Aaron J. Seigo 2010-09-03 21:00:25 +00:00
parent 871d541d9f
commit 6a9b6a302c
3 changed files with 80 additions and 38 deletions

View File

@ -45,6 +45,7 @@
#include "javascriptaddonpackagestructure.h"
Q_DECLARE_METATYPE(ScriptEnv*)
Q_DECLARE_METATYPE(Plasma::Package)
ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
: QObject(parent),
@ -233,7 +234,7 @@ QScriptValue ScriptEnv::getUrl(QScriptContext *context, QScriptEngine *engine)
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
if (!env) {
kDebug() << "findScriptEnv failed";
//kDebug() << "findScriptEnv failed";
return engine->undefinedValue();
}
@ -291,7 +292,10 @@ bool ScriptEnv::importBuiltinExtension(const QString &extension, QScriptValue &o
bool ScriptEnv::importExtensions(const KPluginInfo &info, QScriptValue &obj, Authorization &auth)
{
QStringList requiredExtensions = info.service()->property("X-Plasma-RequiredExtensions", QVariant::StringList).toStringList();
kDebug() << "required extensions are" << requiredExtensions;
if (!requiredExtensions.isEmpty()) {
kDebug() << "required extensions are" << requiredExtensions;
}
foreach (const QString &ext, requiredExtensions) {
QString extension = ext.toLower();
if (m_extensions.contains(extension)) {
@ -316,7 +320,10 @@ bool ScriptEnv::importExtensions(const KPluginInfo &info, QScriptValue &obj, Aut
}
QStringList optionalExtensions = info.service()->property("X-Plasma-OptionalExtensions", QVariant::StringList).toStringList();
kDebug() << "optional extensions are" << optionalExtensions;
if (!optionalExtensions.isEmpty()) {
kDebug() << "optional extensions are" << optionalExtensions;
}
foreach (const QString &ext, optionalExtensions) {
QString extension = ext.toLower();
@ -432,6 +439,11 @@ QScriptValue ScriptEnv::loadAddon(QScriptContext *context, QScriptEngine *engine
QScriptContext *innerContext = engine->pushContext();
innerContext->activationObject().setProperty("registerAddon", engine->newFunction(ScriptEnv::registerAddon));
QScriptValue v = engine->newVariant(QVariant::fromValue(package));
innerContext->activationObject().setProperty("__plasma_addon_package", v,
QScriptValue::ReadOnly |
QScriptValue::Undeletable |
QScriptValue::SkipInEnumeration);
engine->evaluate(code, file.fileName());
engine->popContext();
@ -458,9 +470,11 @@ QScriptValue ScriptEnv::registerAddon(QScriptContext *context, QScriptEngine *en
func.setProperty("test", "bar");
*/
QScriptValue obj = func.construct();
obj.setProperty("__plasma_addon_package",
context->activationObject().property("__plasma_addon_package"),
QScriptValue::ReadOnly|QScriptValue::Undeletable|
QScriptValue::SkipInEnumeration);
/*
obj.setProperty("__plasma_addon_filepath", "/fake/path/",
QScriptValue::ReadOnly|QScriptValue::Undeletable|QScriptValue::SkipInEnumeration);
obj.setProperty("test", "bar");
obj.setProperty("addonFilePath", engine->newFunction(ScriptEnv::addonFilePath));
*/

View File

@ -149,28 +149,37 @@ void SimpleJavaScriptApplet::reportError(ScriptEnv *env, bool fatal)
void SimpleJavaScriptApplet::configChanged()
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners("configchanged")) {
callPlasmoidFunction("configChanged");
if (!env || !env->callEventListeners("configchangd")) {
callPlasmoidFunction("configChanged", QScriptValueList(), env);
}
}
void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine::Data &data)
{
QScriptValueList args;
args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data);
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners("dataUpdated")) {
callPlasmoidFunction("dataUpdated", args);
if (!env) {
return;
}
if (!env->callEventListeners("dataUpdated")) {
QScriptValueList args;
args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data);
callPlasmoidFunction("dataUpdated", args, env);
}
}
void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item)
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env) {
return;
}
QScriptValueList args;
args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners("initExtenderItem")) {
callPlasmoidFunction("initExtenderItem", args);
if (!env->callEventListeners("initExtenderItem")) {
callPlasmoidFunction("initExtenderItem", args, env);
}
}
@ -178,39 +187,52 @@ void SimpleJavaScriptApplet::activate()
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners("activate")) {
callPlasmoidFunction("activate");
callPlasmoidFunction("activate", QScriptValueList(), env);
}
}
void SimpleJavaScriptApplet::popupEvent(bool popped)
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env) {
return;
}
QScriptValueList args;
args << popped;
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners("popupEvent", args)) {
callPlasmoidFunction("popupEvent", args);
if (!env->callEventListeners("popupEvent", args)) {
callPlasmoidFunction("popupEvent", args, env);
}
}
void SimpleJavaScriptApplet::executeAction(const QString &name)
{
const QString func("action_" + name);
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners(func)) {
callPlasmoidFunction(func);
if (!env) {
return;
}
const QString func("action_" + name);
if (!env->callEventListeners(func)) {
callPlasmoidFunction(func, QScriptValueList(), env);
}
}
void SimpleJavaScriptApplet::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env) {
return;
}
QScriptValueList args;
args << m_engine->toScriptValue(p);
args << m_engine->toScriptValue(const_cast<QStyleOptionGraphicsItem*>(option));
args << m_engine->toScriptValue(QRectF(contentsRect));
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env || !env->callEventListeners("paintInterface")) {
callPlasmoidFunction("paintInterface", args);
if (!env->callEventListeners("paintInterface")) {
callPlasmoidFunction("paintInterface", args, env);
}
}
@ -219,11 +241,14 @@ QList<QAction*> SimpleJavaScriptApplet::contextualActions()
return m_interface->contextualActions();
}
void SimpleJavaScriptApplet::callPlasmoidFunction(const QString &functionName, const QScriptValueList &args)
void SimpleJavaScriptApplet::callPlasmoidFunction(const QString &functionName, const QScriptValueList &args, ScriptEnv *env)
{
QScriptValue func = m_self.property(functionName);
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env) {
env = ScriptEnv::findScriptEnv(m_engine);
}
if (env) {
QScriptValue func = m_self.property(functionName);
env->callFunction(func, args, m_self);
}
}
@ -247,34 +272,37 @@ void SimpleJavaScriptApplet::removeEventListener(const QString &event, const QSc
void SimpleJavaScriptApplet::constraintsEvent(Plasma::Constraints constraints)
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (!env) {
return;
}
if (constraints & Plasma::FormFactorConstraint) {
if (!env || !env->callEventListeners("formFactorChanged")) {
callPlasmoidFunction("formFactorChanged");
if (!env->callEventListeners("formFactorChanged")) {
callPlasmoidFunction("formFactorChanged", QScriptValueList(), env);
}
}
if (constraints & Plasma::LocationConstraint) {
if (!env || !env->callEventListeners("locationChanged")) {
callPlasmoidFunction("locationChanged");
if (!env->callEventListeners("locationChanged")) {
callPlasmoidFunction("locationChanged", QScriptValueList(), env);
}
}
if (constraints & Plasma::ContextConstraint) {
if (!env || !env->callEventListeners("currentActivityChanged")) {
callPlasmoidFunction("currentActivityChanged");
if (!env->callEventListeners("currentActivityChanged")) {
callPlasmoidFunction("currentActivityChanged", QScriptValueList(), env);
}
}
if (constraints & Plasma::SizeConstraint) {
if (!env || !env->callEventListeners("sizeChanged")) {
callPlasmoidFunction("sizeChanged");
callPlasmoidFunction("sizeChanged", QScriptValueList(), env);
}
}
if (constraints & Plasma::ImmutableConstraint) {
if (!env || !env->callEventListeners("immutabilityChanged")) {
callPlasmoidFunction("immutabilityChanged");
if (!env->callEventListeners("immutabilityChanged")) {
callPlasmoidFunction("immutabilityChanged", QScriptValueList(), env);
}
}
}

View File

@ -61,7 +61,7 @@ public:
bool eventFilter(QObject *watched, QEvent *event);
public Q_SLOTS:
void dataUpdated( const QString &name, const Plasma::DataEngine::Data &data );
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data);
void configChanged();
void executeAction(const QString &name);
void collectGarbage();
@ -76,7 +76,7 @@ private:
bool importExtensions();
bool importBuiltinExtension(const QString &extension);
void setupObjects();
void callPlasmoidFunction(const QString &functionName, const QScriptValueList &args = QScriptValueList());
void callPlasmoidFunction(const QString &functionName, const QScriptValueList &args = QScriptValueList(), ScriptEnv *env = 0);
QScriptValue createKeyEventObject(QKeyEvent *event);
QScriptValue createHoverEventObject(QGraphicsSceneHoverEvent *event);
QScriptValue createMouseEventObject(QGraphicsSceneMouseEvent *event);