huge cleanup with lots of inter-twined changes, but this should be more or less ready for 4.4.0 at this point! changes include:

* discovered QByteArrays were completely opaque to the runtime, obviously not good seeing how many things use them. fortunately, Qt has bindings for them ... in the examples! so just pulled those over (license compatible, thankfully) and now that works
* implement the remaining security hooks so it knows the difference between different kinds of urls
* export KIO::Job into the runtime; i really wish we could have had more time to do something more elegant than just pushing the object wholesale into the runtime as it exposes a LOT of API, but this works, should be safe (security wise) and as we are in feature freeze there's really nothing better that can be done to replace it at this point in time
* move some of the utility bindings (i18n, DataEngine) into simplebindings/ for consistency
* break out the qvariant stuff that is used outside of the DataEngine bindings for clarity
* brea out a bunch of the bookkeeping setup stuff from simplejavascriptapplet.cpp (it was getting messy) and put it into a separate .cpp file

svn path=/trunk/KDE/kdebase/runtime/; revision=1063070
This commit is contained in:
Aaron J. Seigo 2009-12-16 22:05:46 +00:00
parent 18a9f087a4
commit 47deeea77c
16 changed files with 1076 additions and 279 deletions

View File

@ -5,7 +5,10 @@ set(simple_javascript_engine_SRCS
simplebindings/animationgroup.cpp
simplebindings/appletinterface.cpp
simplebindings/anchorlayout.cpp
simplebindings/bytearrayclass.cpp
simplebindings/bytearrayprototype.cpp
simplebindings/color.cpp
simplebindings/dataengine.cpp
simplebindings/font.cpp
simplebindings/filedialogproxy.cpp
simplebindings/graphicsitem.cpp
@ -16,10 +19,12 @@ set(simple_javascript_engine_SRCS
simplebindings/pixmap.cpp
simplebindings/point.cpp
simplebindings/rect.cpp
simplebindings/qscriptbookkeeping.cpp
simplebindings/size.cpp
simplebindings/timer.cpp
simplebindings/uiloader.cpp
simplebindings/url.cpp
simplebindings/variant.cpp
)
include_directories(${PHONON_INCLUDES})
@ -63,6 +68,8 @@ install(FILES plasma-scriptengine-runner-javascript.desktop DESTINATION ${SERVIC
set(javascript_dataengine_engine_SRCS
javascriptdataengine.cpp
simplebindings/dataengine.cpp
simplebindings/variant.cpp
)
kde4_add_plugin(plasma_dataenginescript_javascript ${javascript_dataengine_engine_SRCS})

View File

@ -22,8 +22,8 @@
#include <QScriptEngine>
#include <QScriptContext>
#include "bind_dataengine.h"
#include "bind_i18n.h"
#include "simplebindings/dataengine.h"
#include "simplebindings/i18n.h"
JavaScriptDataEngine::JavaScriptDataEngine(QObject *parent, const QVariantList &args)
: DataEngineScript(parent),

View File

@ -21,6 +21,7 @@
#include <QAction>
#include <QFile>
#include <QScriptEngine>
#include <QSignalMapper>
#include <QTimer>
@ -37,7 +38,8 @@
AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent)
: QObject(parent),
m_appletScriptEngine(parent),
m_actionSignals(0)
m_actionSignals(0),
m_allowedUrls(SimpleJavaScriptApplet::NoUrls)
{
connect(this, SIGNAL(releaseVisualFocus()), applet(), SIGNAL(releaseVisualFocus()));
connect(this, SIGNAL(configNeedsSaving()), applet(), SIGNAL(configNeedsSaving()));
@ -317,6 +319,16 @@ int AppletInterface::apiVersion() const
return offers.first()->property("X-KDE-PluginInfo-Version", QVariant::Int).toInt();
}
SimpleJavaScriptApplet::AllowedUrls AppletInterface::allowedUrls() const
{
return m_allowedUrls;
}
void AppletInterface::setAllowedUrls(const SimpleJavaScriptApplet::AllowedUrls &allowedUrls)
{
m_allowedUrls = allowedUrls;
}
bool AppletInterface::include(const QString &script)
{
const QString path = package()->filePath("scripts", script);
@ -342,4 +354,25 @@ void AppletInterface::gc()
QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage()));
}
PopupAppletInterface::PopupAppletInterface(SimpleJavaScriptApplet *parent)
: AppletInterface(parent)
{
}
void PopupAppletInterface::setPopupIcon(const QIcon &icon)
{
popupApplet()->setPopupIcon(icon);
}
QIcon PopupAppletInterface::popupIcon()
{
return popupApplet()->popupIcon();
}
void PopupAppletInterface::setPopupIconByName(const QString &name)
{
return popupApplet()->setPopupIcon(name);
}
#include "appletinterface.moc"

View File

@ -26,6 +26,7 @@
#include <QScriptValue>
#include <Plasma/Applet>
#include <Plasma/PopupApplet>
#include <Plasma/DataEngine>
#include "simplejavascriptapplet.h"
@ -233,22 +234,44 @@ enum QtScrollBarPolicy {
bool immutable() const;
int apiVersion() const;
SimpleJavaScriptApplet::AllowedUrls allowedUrls() const;
void setAllowedUrls(const SimpleJavaScriptApplet::AllowedUrls &allowedUrls);
inline Plasma::Applet *applet() const { return m_appletScriptEngine->applet(); }
Q_SIGNALS:
void releaseVisualFocus();
void configNeedsSaving();
public Q_SLOTS:
void dataUpdated(QString source, Plasma::DataEngine::Data data);
private:
protected:
SimpleJavaScriptApplet *m_appletScriptEngine;
private:
QSet<QString> m_actions;
QSignalMapper *m_actionSignals;
QString m_currentConfig;
QMap<QString, Plasma::ConfigLoader*> m_configs;
SimpleJavaScriptApplet::AllowedUrls m_allowedUrls;
};
class PopupAppletInterface : public AppletInterface
{
Q_OBJECT
Q_PROPERTY(QIcon popupIcon READ popupIcon WRITE setPopupIcon)
public:
PopupAppletInterface(SimpleJavaScriptApplet *parent);
void setPopupIcon(const QIcon &icon);
QIcon popupIcon();
inline Plasma::PopupApplet *popupApplet() const { return static_cast<Plasma::PopupApplet *>(m_appletScriptEngine->applet()); }
public Q_SLOTS:
void setPopupIconByName(const QString &name);
};
#endif

View File

@ -0,0 +1,296 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtScript/QScriptClassPropertyIterator>
#include <QtScript/QScriptEngine>
#include "bytearrayclass.h"
#include "bytearrayprototype.h"
#include <stdlib.h>
Q_DECLARE_METATYPE(QByteArray*)
Q_DECLARE_METATYPE(ByteArrayClass*)
class ByteArrayClassPropertyIterator : public QScriptClassPropertyIterator
{
public:
ByteArrayClassPropertyIterator(const QScriptValue &object);
~ByteArrayClassPropertyIterator();
bool hasNext() const;
void next();
bool hasPrevious() const;
void previous();
void toFront();
void toBack();
QScriptString name() const;
uint id() const;
private:
int m_index;
int m_last;
};
//! [0]
ByteArrayClass::ByteArrayClass(QScriptEngine *engine)
: QObject(engine), QScriptClass(engine)
{
qScriptRegisterMetaType<QByteArray>(engine, toScriptValue, fromScriptValue);
length = engine->toStringHandle(QLatin1String("length"));
proto = engine->newQObject(new ByteArrayPrototype(this),
QScriptEngine::QtOwnership,
QScriptEngine::SkipMethodsInEnumeration
| QScriptEngine::ExcludeSuperClassMethods
| QScriptEngine::ExcludeSuperClassProperties);
QScriptValue global = engine->globalObject();
proto.setPrototype(global.property("Object").property("prototype"));
ctor = engine->newFunction(construct, proto);
ctor.setData(qScriptValueFromValue(engine, this));
}
//! [0]
ByteArrayClass::~ByteArrayClass()
{
}
//! [3]
QScriptClass::QueryFlags ByteArrayClass::queryProperty(const QScriptValue &object,
const QScriptString &name,
QueryFlags flags, uint *id)
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
if (!ba)
return 0;
if (name == length) {
return flags;
} else {
bool isArrayIndex;
qint32 pos = name.toArrayIndex(&isArrayIndex);
if (!isArrayIndex)
return 0;
*id = pos;
if ((flags & HandlesReadAccess) && (pos >= ba->size()))
flags &= ~HandlesReadAccess;
return flags;
}
}
//! [3]
//! [4]
QScriptValue ByteArrayClass::property(const QScriptValue &object,
const QScriptString &name, uint id)
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
if (!ba)
return QScriptValue();
if (name == length) {
return ba->length();
} else {
qint32 pos = id;
if ((pos < 0) || (pos >= ba->size()))
return QScriptValue();
return uint(ba->at(pos)) & 255;
}
return QScriptValue();
}
//! [4]
//! [5]
void ByteArrayClass::setProperty(QScriptValue &object,
const QScriptString &name,
uint id, const QScriptValue &value)
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
if (!ba)
return;
if (name == length) {
ba->resize(value.toInt32());
} else {
qint32 pos = id;
if (pos < 0)
return;
if (ba->size() <= pos)
ba->resize(pos + 1);
(*ba)[pos] = char(value.toInt32());
}
}
//! [5]
//! [6]
QScriptValue::PropertyFlags ByteArrayClass::propertyFlags(
const QScriptValue &/*object*/, const QScriptString &name, uint /*id*/)
{
if (name == length) {
return QScriptValue::Undeletable
| QScriptValue::SkipInEnumeration;
}
return QScriptValue::Undeletable;
}
//! [6]
//! [7]
QScriptClassPropertyIterator *ByteArrayClass::newIterator(const QScriptValue &object)
{
return new ByteArrayClassPropertyIterator(object);
}
//! [7]
QString ByteArrayClass::name() const
{
return QLatin1String("ByteArray");
}
QScriptValue ByteArrayClass::prototype() const
{
return proto;
}
QScriptValue ByteArrayClass::constructor()
{
return ctor;
}
QScriptValue ByteArrayClass::newInstance(int size)
{
return newInstance(QByteArray(size, /*ch=*/0));
}
//! [1]
QScriptValue ByteArrayClass::newInstance(const QByteArray &ba)
{
QScriptValue data = engine()->newVariant(qVariantFromValue(ba));
return engine()->newObject(this, data);
}
//! [1]
//! [2]
QScriptValue ByteArrayClass::construct(QScriptContext *ctx, QScriptEngine *)
{
ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctx->callee().data());
if (!cls)
return QScriptValue();
QScriptValue arg = ctx->argument(0);
if (arg.instanceOf(ctx->callee()))
return cls->newInstance(qscriptvalue_cast<QByteArray>(arg));
int size = arg.toInt32();
return cls->newInstance(size);
}
//! [2]
QScriptValue ByteArrayClass::toScriptValue(QScriptEngine *eng, const QByteArray &ba)
{
QScriptValue ctor = eng->globalObject().property("ByteArray");
ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctor.data());
if (!cls)
return eng->newVariant(qVariantFromValue(ba));
return cls->newInstance(ba);
}
void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba)
{
ba = qvariant_cast<QByteArray>(obj.data().toVariant());
}
ByteArrayClassPropertyIterator::ByteArrayClassPropertyIterator(const QScriptValue &object)
: QScriptClassPropertyIterator(object)
{
toFront();
}
ByteArrayClassPropertyIterator::~ByteArrayClassPropertyIterator()
{
}
//! [8]
bool ByteArrayClassPropertyIterator::hasNext() const
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
return m_index < ba->size();
}
void ByteArrayClassPropertyIterator::next()
{
m_last = m_index;
++m_index;
}
bool ByteArrayClassPropertyIterator::hasPrevious() const
{
return (m_index > 0);
}
void ByteArrayClassPropertyIterator::previous()
{
--m_index;
m_last = m_index;
}
void ByteArrayClassPropertyIterator::toFront()
{
m_index = 0;
m_last = -1;
}
void ByteArrayClassPropertyIterator::toBack()
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
m_index = ba->size();
m_last = -1;
}
QScriptString ByteArrayClassPropertyIterator::name() const
{
return object().engine()->toStringHandle(QString::number(m_last));
}
uint ByteArrayClassPropertyIterator::id() const
{
return m_last;
}
//! [8]

View File

@ -0,0 +1,90 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef BYTEARRAYCLASS_H
#define BYTEARRAYCLASS_H
#include <QtCore/QObject>
#include <QtScript/QScriptClass>
#include <QtScript/QScriptString>
class ByteArrayClass : public QObject, public QScriptClass
{
public:
ByteArrayClass(QScriptEngine *engine);
~ByteArrayClass();
QScriptValue constructor();
QScriptValue newInstance(int size = 0);
QScriptValue newInstance(const QByteArray &ba);
QueryFlags queryProperty(const QScriptValue &object,
const QScriptString &name,
QueryFlags flags, uint *id);
QScriptValue property(const QScriptValue &object,
const QScriptString &name, uint id);
void setProperty(QScriptValue &object, const QScriptString &name,
uint id, const QScriptValue &value);
QScriptValue::PropertyFlags propertyFlags(
const QScriptValue &object, const QScriptString &name, uint id);
QScriptClassPropertyIterator *newIterator(const QScriptValue &object);
QString name() const;
QScriptValue prototype() const;
private:
static QScriptValue construct(QScriptContext *ctx, QScriptEngine *eng);
static QScriptValue toScriptValue(QScriptEngine *eng, const QByteArray &ba);
static void fromScriptValue(const QScriptValue &obj, QByteArray &ba);
QScriptString length;
QScriptValue proto;
QScriptValue ctor;
};
#endif

View File

@ -0,0 +1,136 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "bytearrayprototype.h"
#include <QtScript/QScriptEngine>
Q_DECLARE_METATYPE(QByteArray*)
ByteArrayPrototype::ByteArrayPrototype(QObject *parent)
: QObject(parent)
{
}
ByteArrayPrototype::~ByteArrayPrototype()
{
}
//! [0]
QByteArray *ByteArrayPrototype::thisByteArray() const
{
return qscriptvalue_cast<QByteArray*>(thisObject().data());
}
//! [0]
void ByteArrayPrototype::chop(int n)
{
thisByteArray()->chop(n);
}
bool ByteArrayPrototype::equals(const QByteArray &other)
{
return *thisByteArray() == other;
}
QByteArray ByteArrayPrototype::left(int len) const
{
return thisByteArray()->left(len);
}
//! [1]
QByteArray ByteArrayPrototype::mid(int pos, int len) const
{
return thisByteArray()->mid(pos, len);
}
QScriptValue ByteArrayPrototype::remove(int pos, int len)
{
thisByteArray()->remove(pos, len);
return thisObject();
}
//! [1]
QByteArray ByteArrayPrototype::right(int len) const
{
return thisByteArray()->right(len);
}
QByteArray ByteArrayPrototype::simplified() const
{
return thisByteArray()->simplified();
}
QByteArray ByteArrayPrototype::toBase64() const
{
return thisByteArray()->toBase64();
}
QByteArray ByteArrayPrototype::toLower() const
{
return thisByteArray()->toLower();
}
QByteArray ByteArrayPrototype::toUpper() const
{
return thisByteArray()->toUpper();
}
QByteArray ByteArrayPrototype::trimmed() const
{
return thisByteArray()->trimmed();
}
void ByteArrayPrototype::truncate(int pos)
{
thisByteArray()->truncate(pos);
}
QString ByteArrayPrototype::toLatin1String() const
{
return QString::fromLatin1(*thisByteArray());
}
//! [2]
QScriptValue ByteArrayPrototype::valueOf() const
{
return thisObject().data();
}
//! [2]

View File

@ -0,0 +1,80 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef BYTEARRAYPROTOTYPE_H
#define BYTEARRAYPROTOTYPE_H
#include <QtCore/QByteArray>
#include <QtCore/QObject>
#include <QtScript/QScriptable>
#include <QtScript/QScriptValue>
//! [0]
class ByteArrayPrototype : public QObject, public QScriptable
{
Q_OBJECT
public:
ByteArrayPrototype(QObject *parent = 0);
~ByteArrayPrototype();
public slots:
void chop(int n);
bool equals(const QByteArray &other);
QByteArray left(int len) const;
QByteArray mid(int pos, int len = -1) const;
QScriptValue remove(int pos, int len);
QByteArray right(int len) const;
QByteArray simplified() const;
QByteArray toBase64() const;
QByteArray toLower() const;
QByteArray toUpper() const;
QByteArray trimmed() const;
void truncate(int pos);
QString toLatin1String() const;
QScriptValue valueOf() const;
private:
QByteArray *thisByteArray() const;
};
//! [0]
#endif

View File

@ -0,0 +1,38 @@
/*
* Copyright 2007 Richard J. Moore <rich@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "dataengine.h"
QScriptValue qScriptValueFromData(QScriptEngine *engine, const DataEngine::Data &data)
{
DataEngine::Data::const_iterator begin = data.begin();
DataEngine::Data::const_iterator end = data.end();
DataEngine::Data::const_iterator it;
QScriptValue obj = engine->newObject();
for (it = begin; it != end; ++it) {
//kDebug() << "setting" << it.key() << "to" << it.value();
QString prop = it.key();
prop.replace(' ', '_');
obj.setProperty(prop, variantToScriptValue(engine, it.value()));
}
return obj;
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 2007 Richard J. Moore <rich@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef DATAENGINE_H
#define DATAENGINE_H
#include <QtScript/QtScript>
#include <KDebug>
#include <Plasma/DataEngine>
#include <Plasma/Service>
#include <Plasma/ServiceJob>
#include "variant.h"
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)
{
kDebug() << "qScriptValueFromMap called";
QScriptValue obj = eng->newObject();
typename M::const_iterator begin = map.constBegin();
typename M::const_iterator end = map.constEnd();
typename M::const_iterator it;
for (it = begin; it != end; ++it)
obj.setProperty(it.key(), qScriptValueFromValue(eng, it.value()));
return obj;
}
template <class M>
void qScriptValueToMap(const QScriptValue &value, M &map)
{
QScriptValueIterator it(value);
while (it.hasNext()) {
it.next();
map[it.name()] = qscriptvalue_cast<typename M::mapped_type>(it.value());
}
}
template<typename T>
int qScriptRegisterMapMetaType(
QScriptEngine *engine,
const QScriptValue &prototype = QScriptValue()
#ifndef qdoc
, T * /* dummy */ = 0
#endif
)
{
return qScriptRegisterMetaType<T>(engine, qScriptValueFromMap,
qScriptValueToMap, prototype);
}
QScriptValue qScriptValueFromData(QScriptEngine *engine, const DataEngine::Data &data);
#endif // DATAENGINE_H

View File

@ -0,0 +1,166 @@
/*
* Copyright 2007-2008 Richard J. Moore <rich@kde.org>
* Copyright 2009 Aaron J. Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QGraphicsWidget>
#include <QScriptEngine>
#include <Plasma/Applet>
#include <Plasma/Animation>
#include <Plasma/VideoWidget>
#include <KConfigGroup>
#include <KIO/Job>
#include "appletinterface.h"
#include "dataengine.h"
#include "variant.h"
//Q_DECLARE_METATYPE(SimpleJavaScriptApplet*)
Q_DECLARE_METATYPE(AppletInterface*)
Q_DECLARE_METATYPE(Plasma::Applet*)
Q_DECLARE_METATYPE(QGraphicsWidget*)
Q_DECLARE_METATYPE(QGraphicsLayout*)
Q_DECLARE_METATYPE(KConfigGroup)
Q_DECLARE_METATYPE(Plasma::Animation*)
Q_DECLARE_METATYPE(Plasma::VideoWidget::Controls)
//Q_SCRIPT_DECLARE_QMETAOBJECT(AppletInterface, SimpleJavaScriptApplet*)
QScriptValue qScriptValueFromControls(QScriptEngine *engine, const Plasma::VideoWidget::Controls &controls)
{
return QScriptValue(engine, controls);
}
void controlsFromScriptValue(const QScriptValue& obj, Plasma::VideoWidget::Controls &controls)
{
int flagValue = obj.toInteger();
//FIXME: it has to be a less ugly way to do that :)
if (flagValue & Plasma::VideoWidget::Play) {
controls |= Plasma::VideoWidget::Play;
}
if (flagValue & Plasma::VideoWidget::Pause) {
controls |= Plasma::VideoWidget::Pause;
}
if (flagValue & Plasma::VideoWidget::Stop) {
controls |= Plasma::VideoWidget::Stop;
}
if (flagValue & Plasma::VideoWidget::PlayPause) {
controls |= Plasma::VideoWidget::PlayPause;
}
if (flagValue & Plasma::VideoWidget::Progress) {
controls |= Plasma::VideoWidget::Progress;
}
if (flagValue & Plasma::VideoWidget::Volume) {
controls |= Plasma::VideoWidget::Volume;
}
if (flagValue & Plasma::VideoWidget::OpenFile) {
controls |= Plasma::VideoWidget::OpenFile;
}
}
Q_DECLARE_METATYPE(KIO::Job *)
typedef KIO::Job* KioJobPtr;
QScriptValue qScriptValueFromKIOJob(QScriptEngine *engine, const KioJobPtr &job)
{
return engine->newQObject(const_cast<KIO::Job *>(job), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void qKIOJobFromQScriptValue(const QScriptValue &scriptValue, KioJobPtr &job)
{
QObject *obj = scriptValue.toQObject();
job = static_cast<KIO::Job *>(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.constBegin();
QMap<QString, QString>::const_iterator begin = it;
QMap<QString, QString>::const_iterator end = entryMap.constEnd();
//setting the group name
obj.setProperty("__name", QScriptValue(engine, config.name()));
//setting the key/value pairs
for (it = begin; it != end; ++it) {
//kDebug() << "setting" << it.key() << "to" << it.value();
QString prop = it.key();
prop.replace(' ', '_');
obj.setProperty(prop, variantToScriptValue(engine, it.value()));
}
return obj;
}
void kConfigGroupFromScriptValue(const QScriptValue& obj, KConfigGroup &config)
{
KConfigSkeleton *skel = new KConfigSkeleton();
config = KConfigGroup(skel->config(), obj.property("__name").toString());
QScriptValueIterator it(obj);
while (it.hasNext()) {
it.next();
//kDebug() << it.name() << "is" << it.value().toString();
if (it.name() != "__name") {
config.writeEntry(it.name(), it.value().toString());
}
}
}
typedef Plasma::Animation* AnimationPtr;
QScriptValue qScriptValueFromAnimation(QScriptEngine *engine, const AnimationPtr &anim)
{
return engine->newQObject(const_cast<Animation *>(anim), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void abstractAnimationFromQScriptValue(const QScriptValue &scriptValue, AnimationPtr &anim)
{
QObject *obj = scriptValue.toQObject();
anim = static_cast<Animation *>(obj);
}
typedef QGraphicsWidget * QGraphicsWidgetPtr;
QScriptValue qScriptValueFromQGraphicsWidget(QScriptEngine *engine, const QGraphicsWidgetPtr &anim)
{
return engine->newQObject(const_cast<QGraphicsWidget *>(anim), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void qGraphicsWidgetFromQScriptValue(const QScriptValue &scriptValue, QGraphicsWidgetPtr &anim)
{
QObject *obj = scriptValue.toQObject();
anim = static_cast<QGraphicsWidget *>(obj);
}
void registerSimpleAppletMetaTypes(QScriptEngine *engine)
{
qScriptRegisterMetaType<Plasma::DataEngine::Data>(engine, qScriptValueFromData, 0, QScriptValue());
qScriptRegisterMetaType<KConfigGroup>(engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue());
qScriptRegisterMetaType<Plasma::VideoWidget::Controls>(engine, qScriptValueFromControls, controlsFromScriptValue, QScriptValue());
qScriptRegisterMetaType<KIO::Job *>(engine, qScriptValueFromKIOJob, qKIOJobFromQScriptValue);
qScriptRegisterMetaType<Plasma::Animation*>(engine, qScriptValueFromAnimation, abstractAnimationFromQScriptValue);
qScriptRegisterMetaType<QGraphicsWidget*>(engine, qScriptValueFromQGraphicsWidget, qGraphicsWidgetFromQScriptValue);
}

View File

@ -16,66 +16,15 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef BIND_DATAENGINE_H
#define BIND_DATAENGINE_H
#include <QColor>
#include <QDate>
#include <QDateTime>
#include <QScriptEngine>
#include <QScriptValue>
#include <QVariant>
#include <QtScript/QtScript>
#include <KDebug>
#include <KUrl>
#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)
{
kDebug() << "qScriptValueFromMap called";
QScriptValue obj = eng->newObject();
typename M::const_iterator begin = map.constBegin();
typename M::const_iterator end = map.constEnd();
typename M::const_iterator it;
for (it = begin; it != end; ++it)
obj.setProperty(it.key(), qScriptValueFromValue(eng, it.value()));
return obj;
}
template <class M>
void qScriptValueToMap(const QScriptValue &value, M &map)
{
QScriptValueIterator it(value);
while (it.hasNext()) {
it.next();
map[it.name()] = qscriptvalue_cast<typename M::mapped_type>(it.value());
}
}
template<typename T>
int qScriptRegisterMapMetaType(
QScriptEngine *engine,
const QScriptValue &prototype = QScriptValue()
#ifndef qdoc
, T * /* dummy */ = 0
#endif
)
{
return qScriptRegisterMetaType<T>(engine, qScriptValueFromMap,
qScriptValueToMap, prototype);
}
/*
* Workaround the fact that QtScripts handling of variants seems a bit broken.
*/
QScriptValue variantToScriptValue(QScriptEngine *engine, QVariant var)
{
if (var.isNull()) {
@ -118,23 +67,4 @@ QScriptValue variantToScriptValue(QScriptEngine *engine, QVariant var)
return qScriptValueFromValue(engine, var);
}
QScriptValue qScriptValueFromData(QScriptEngine *engine, const DataEngine::Data &data)
{
DataEngine::Data::const_iterator begin = data.begin();
DataEngine::Data::const_iterator end = data.end();
DataEngine::Data::const_iterator it;
QScriptValue obj = engine->newObject();
for (it = begin; it != end; ++it) {
//kDebug() << "setting" << it.key() << "to" << it.value();
QString prop = it.key();
prop.replace(' ', '_');
obj.setProperty(prop, variantToScriptValue(engine, it.value()));
}
return obj;
}
#endif // BIND_DATAENGINE_H

View File

@ -0,0 +1,24 @@
/*
* Copyright 2007 Richard J. Moore <rich@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef VARIANT_H
#define VARIANT_H
QScriptValue variantToScriptValue(QScriptEngine *engine, QVariant var);
#endif

View File

@ -1,5 +1,6 @@
/*
* Copyright 2007-2008 Richard J. Moore <rich@kde.org>
* Copyright 2009 Aaron J. Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
@ -32,6 +33,7 @@
#include <KDebug>
#include <KFileDialog>
#include <KIcon>
#include <KIO/Job>
#include <KMimeType>
#include <KShell>
#include <KStandardDirs>
@ -43,28 +45,22 @@
#include <Plasma/Svg>
#include <Plasma/FrameSvg>
#include <Plasma/Package>
#include <Plasma/PopupApplet>
#include <Plasma/VideoWidget>
#include "simplebindings/animationgroup.h"
#include "simplebindings/dataengine.h"
#include "simplebindings/i18n.h"
#include "simplebindings/appletinterface.h"
#include "simplebindings/bytearrayclass.h"
#include "simplebindings/filedialogproxy.h"
#include "simplebindings/variant.h"
using namespace Plasma;
#include "bind_dataengine.h"
#include "bind_i18n.h"
Q_DECLARE_METATYPE(QPainter*)
Q_DECLARE_METATYPE(QStyleOptionGraphicsItem*)
Q_DECLARE_METATYPE(SimpleJavaScriptApplet*)
Q_DECLARE_METATYPE(AppletInterface*)
Q_DECLARE_METATYPE(Applet*)
Q_DECLARE_METATYPE(QGraphicsWidget*)
Q_DECLARE_METATYPE(QGraphicsLayout*)
Q_DECLARE_METATYPE(KConfigGroup)
Q_DECLARE_METATYPE(Plasma::Animation *)
Q_SCRIPT_DECLARE_QMETAOBJECT(AppletInterface, SimpleJavaScriptApplet*)
QScriptValue constructColorClass(QScriptEngine *engine);
QScriptValue constructFontClass(QScriptEngine *engine);
@ -80,10 +76,7 @@ QScriptValue constructQPointClass(QScriptEngine *engine);
QScriptValue constructQRectFClass(QScriptEngine *engine);
QScriptValue constructQSizeFClass(QScriptEngine *engine);
QScriptValue constructTimerClass(QScriptEngine *engine);
//typedef VideoWidget::Control Control;
Q_DECLARE_FLAGS(Controls, VideoWidget::Control)
Q_DECLARE_METATYPE(Controls)
void registerSimpleAppletMetaTypes(QScriptEngine *engine);
class DummyService : public Service
{
@ -96,81 +89,6 @@ public:
}
};
QScriptValue qScriptValueFromControls(QScriptEngine *engine, const Controls &controls)
{
return QScriptValue(engine, controls);
}
void controlsFromScriptValue(const QScriptValue& obj, Controls &controls)
{
int flagValue = obj.toInteger();
//FIXME: it has to be a less ugly way to do that :)
if (flagValue&VideoWidget::Play) {
controls |= VideoWidget::Play;
}
if (flagValue&VideoWidget::Pause) {
controls |= VideoWidget::Pause;
}
if (flagValue&VideoWidget::Stop) {
controls |= VideoWidget::Stop;
}
if (flagValue&VideoWidget::PlayPause) {
controls |= VideoWidget::PlayPause;
}
if (flagValue&VideoWidget::Progress) {
controls |= VideoWidget::Progress;
}
if (flagValue&VideoWidget::Volume) {
controls |= VideoWidget::Volume;
}
if (flagValue&VideoWidget::OpenFile) {
controls |= VideoWidget::OpenFile;
}
}
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.constBegin();
QMap<QString, QString>::const_iterator begin = it;
QMap<QString, QString>::const_iterator end = entryMap.constEnd();
//setting the group name
obj.setProperty("__name", QScriptValue(engine, config.name()));
//setting the key/value pairs
for (it = begin; it != end; ++it) {
//kDebug() << "setting" << it.key() << "to" << it.value();
QString prop = it.key();
prop.replace(' ', '_');
obj.setProperty(prop, variantToScriptValue(engine, it.value()));
}
return obj;
}
void kConfigGroupFromScriptValue(const QScriptValue& obj, KConfigGroup &config)
{
KConfigSkeleton *skel = new KConfigSkeleton();
config = KConfigGroup(skel->config(), obj.property("__name").toString());
QScriptValueIterator it(obj);
while (it.hasNext()) {
it.next();
//kDebug() << it.name() << "is" << it.value().toString();
if (it.name() != "__name") {
config.writeEntry(it.name(), it.value().toString());
}
}
}
void registerEnums(QScriptEngine *engine, QScriptValue &scriptValue, const QMetaObject &meta)
{
//manually create enum values. ugh
@ -465,6 +383,42 @@ QScriptValue SimpleJavaScriptApplet::openUrl(QScriptContext *context, QScriptEng
return false;
}
QScriptValue SimpleJavaScriptApplet::getUrl(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() == 0) {
return engine->undefinedValue();
}
QScriptValue v = context->argument(0);
KUrl url = v.isString() ? KUrl(v.toString()) : qscriptvalue_cast<KUrl>(v);
if (!url.isValid()) {
return engine->undefinedValue();
}
AppletInterface *interface = extractAppletInterface(engine);
AllowedUrls allowed = interface ? interface->allowedUrls() : NoUrls;
if (url.isLocalFile()) {
if (!(allowed & LocalUrls)) {
return engine->undefinedValue();
}
} else if (!(allowed & NetworkUrls) &&
!((allowed & HttpUrls) && (url.protocol() == "http" || url.protocol() == "https"))) {
return engine->undefinedValue();
}
KIO::Job *job = KIO::get(url);
return engine->newQObject(job);
}
void SimpleJavaScriptApplet::registerGetUrl()
{
QScriptValue get = m_self.property("getUrl");
if (!get.isValid()) {
m_self.setProperty("getUrl", m_engine->newFunction(SimpleJavaScriptApplet::getUrl));
}
}
bool SimpleJavaScriptApplet::importBuiltinExtesion(const QString &extension)
{
kDebug() << extension;
@ -477,10 +431,16 @@ bool SimpleJavaScriptApplet::importBuiltinExtesion(const QString &extension)
m_self.setProperty("openUrl", m_engine->newFunction(SimpleJavaScriptApplet::openUrl));
return true;
} else if ("http" == extension) {
m_interface->setAllowedUrls(m_interface->allowedUrls() | HttpUrls);
registerGetUrl();
return true;
} else if ("networkio" == extension) {
m_interface->setAllowedUrls(m_interface->allowedUrls() | HttpUrls | NetworkUrls);
registerGetUrl();
return true;
} else if ("localio" == extension) {
m_interface->setAllowedUrls(m_interface->allowedUrls() | LocalUrls);
registerGetUrl();
return true;
}
@ -544,37 +504,12 @@ bool SimpleJavaScriptApplet::importExtensions()
return true;
}
typedef Animation* AnimationPtr;
QScriptValue qScriptValueFromAnimation(QScriptEngine *engine, const AnimationPtr &anim)
{
return engine->newQObject(const_cast<Animation *>(anim), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void abstractAnimationFromQScriptValue(const QScriptValue &scriptValue, AnimationPtr &anim)
{
QObject *obj = scriptValue.toQObject();
anim = static_cast<Animation *>(obj);
}
typedef QGraphicsWidget * QGraphicsWidgetPtr;
QScriptValue qScriptValueFromQGraphicsWidget(QScriptEngine *engine, const QGraphicsWidgetPtr &anim)
{
return engine->newQObject(const_cast<QGraphicsWidget *>(anim), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void qGraphicsWidgetFromQScriptValue(const QScriptValue &scriptValue, QGraphicsWidgetPtr &anim)
{
QObject *obj = scriptValue.toQObject();
anim = static_cast<QGraphicsWidget *>(obj);
}
void SimpleJavaScriptApplet::setupObjects()
{
QScriptValue global = m_engine->globalObject();
// Bindings for animations
global.setProperty("animation", m_engine->newFunction(SimpleJavaScriptApplet::animation));
qScriptRegisterMetaType<Animation*>(m_engine, qScriptValueFromAnimation, abstractAnimationFromQScriptValue);
global.setProperty("AnimationGroup", m_engine->newFunction(SimpleJavaScriptApplet::animationGroup));
global.setProperty("ParallelAnimationGroup", m_engine->newFunction(SimpleJavaScriptApplet::parallelAnimationGroup));
@ -588,11 +523,10 @@ void SimpleJavaScriptApplet::setupObjects()
bindI18N(m_engine);
global.setProperty("dataEngine", m_engine->newFunction(SimpleJavaScriptApplet::dataEngine));
global.setProperty("service", m_engine->newFunction(SimpleJavaScriptApplet::service));
qScriptRegisterMetaType<DataEngine::Data>(m_engine, qScriptValueFromData, 0, QScriptValue());
qScriptRegisterMetaType<KConfigGroup>(m_engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue());
// Expose applet interface
m_interface = new AppletInterface(this);
const bool isPopupApplet = qobject_cast<Plasma::PopupApplet *>(applet());
m_interface = isPopupApplet ? new PopupAppletInterface(this) : new AppletInterface(this);
m_self = m_engine->newQObject(m_interface);
m_self.setScope(global);
global.setProperty("plasmoid", m_self);
@ -600,7 +534,7 @@ void SimpleJavaScriptApplet::setupObjects()
QScriptValue args = m_engine->newArray();
int i = 0;
foreach (const QVariant &arg, applet()->startupArguments()) {
args.setProperty(i, variantToScriptValue(arg));
args.setProperty(i, ::variantToScriptValue(m_engine, arg));
++i;
}
global.setProperty("startupArguments", args);
@ -609,14 +543,12 @@ void SimpleJavaScriptApplet::setupObjects()
// Add a global loadui method for ui files
qScriptRegisterMetaType<QGraphicsWidget*>(m_engine, qScriptValueFromQGraphicsWidget, qGraphicsWidgetFromQScriptValue);
QScriptValue fun = m_engine->newFunction(SimpleJavaScriptApplet::loadui);
global.setProperty("loadui", fun);
fun = m_engine->newFunction(SimpleJavaScriptApplet::print);
global.setProperty("print", fun);
// Work around bug in 4.3.0
qMetaTypeId<QVariant>();
@ -634,6 +566,8 @@ void SimpleJavaScriptApplet::setupObjects()
global.setProperty("LinearLayout", constructLinearLayoutClass(m_engine));
global.setProperty("GridLayout", constructGridLayoutClass(m_engine));
global.setProperty("AnchorLayout", constructAnchorLayoutClass(m_engine));
ByteArrayClass *baClass = new ByteArrayClass(m_engine);
global.setProperty("ByteArray", baClass->constructor());
// Add stuff from KDE libs
qScriptRegisterSequenceMetaType<KUrl::List>(m_engine);
@ -645,6 +579,7 @@ void SimpleJavaScriptApplet::setupObjects()
global.setProperty("Svg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg));
global.setProperty("FrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
registerSimpleAppletMetaTypes(m_engine);
installWidgets(m_engine);
}
@ -653,27 +588,24 @@ QSet<QString> SimpleJavaScriptApplet::loadedExtensions() const
return m_extensions;
}
AppletInterface *SimpleJavaScriptApplet::extractAppletInterface(QScriptEngine *engine)
{
QScriptValue appletValue = engine->globalObject().property("plasmoid");
return qobject_cast<AppletInterface*>(appletValue.toQObject());
}
QScriptValue SimpleJavaScriptApplet::dataEngine(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() != 1) {
return context->throwError(i18n("dataEngine() takes one argument"));
}
QString dataEngine = context->argument(0).toString();
QScriptValue appletValue = engine->globalObject().property("plasmoid");
//kDebug() << "appletValue is " << appletValue.toString();
QObject *appletObject = appletValue.toQObject();
if (!appletObject) {
return context->throwError(i18n("Could not extract the AppletObject"));
}
AppletInterface *interface = qobject_cast<AppletInterface*>(appletObject);
AppletInterface *interface = extractAppletInterface(engine);
if (!interface) {
return context->throwError(i18n("Could not extract the Applet"));
}
const QString dataEngine = context->argument(0).toString();
DataEngine *data = interface->dataEngine(dataEngine);
return engine->newQObject(data);
}
@ -686,15 +618,7 @@ QScriptValue SimpleJavaScriptApplet::service(QScriptContext *context, QScriptEng
QString dataEngine = context->argument(0).toString();
QScriptValue appletValue = engine->globalObject().property("plasmoid");
//kDebug() << "appletValue is " << appletValue.toString();
QObject *appletObject = appletValue.toQObject();
if (!appletObject) {
return context->throwError(i18n("Could not extract the AppletObject"));
}
AppletInterface *interface = qobject_cast<AppletInterface*>(appletObject);
AppletInterface *interface = extractAppletInterface(engine);
if (!interface) {
return context->throwError(i18n("Could not extract the Applet"));
}
@ -773,15 +697,7 @@ QScriptValue SimpleJavaScriptApplet::loadui(QScriptContext *context, QScriptEngi
QString SimpleJavaScriptApplet::findImageFile(QScriptEngine *engine, const QString &file)
{
QScriptValue appletValue = engine->globalObject().property("plasmoid");
//kDebug() << "appletValue is " << appletValue.toString();
QObject *appletObject = appletValue.toQObject();
if (!appletObject) {
return QString();
}
AppletInterface *interface = qobject_cast<AppletInterface*>(appletObject);
AppletInterface *interface = extractAppletInterface(engine);
if (!interface) {
return QString();
}
@ -791,15 +707,7 @@ QString SimpleJavaScriptApplet::findImageFile(QScriptEngine *engine, const QStri
QString SimpleJavaScriptApplet::findSvg(QScriptEngine *engine, const QString &file)
{
QScriptValue appletValue = engine->globalObject().property("plasmoid");
//kDebug() << "appletValue is " << appletValue.toString();
QObject *appletObject = appletValue.toQObject();
if (!appletObject) {
return file;
}
AppletInterface *interface = qobject_cast<AppletInterface*>(appletObject);
AppletInterface *interface = extractAppletInterface(engine);
if (!interface) {
return file;
}
@ -866,8 +774,7 @@ void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine)
QScriptValue name = engine->toScriptValue(widget);
fun.setProperty(QString("functionName"), name,
QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration);
fun.setProperty(QString("prototype"), createPrototype(engine, name.toString()));
fun.setProperty(QString("prototype"), engine->newObject());
globalObject.setProperty(widget, fun);
}
}
@ -885,15 +792,7 @@ QGraphicsWidget *SimpleJavaScriptApplet::extractParent(QScriptContext *context,
}
if (!parent) {
QScriptValue appletValue = engine->globalObject().property("plasmoid");
//kDebug() << "appletValue is " << appletValue.toString();
QObject *appletObject = appletValue.toQObject();
if (!appletObject) {
return 0;
}
AppletInterface *interface = qobject_cast<AppletInterface*>(appletObject);
AppletInterface *interface = extractAppletInterface(engine);
if (!interface) {
return 0;
}
@ -929,12 +828,6 @@ QScriptValue SimpleJavaScriptApplet::createWidget(QScriptContext *context, QScri
//register enums will be accessed for instance as frame.Sunken for Frame shadow...
registerEnums(engine, fun, *w->metaObject());
//FIXME: still don't have a better approach than try to cast for every widget that could have flags..
if (qobject_cast<VideoWidget *>(w)) {
qScriptRegisterMetaType<Controls>(engine, qScriptValueFromControls, controlsFromScriptValue, QScriptValue());
}
return fun;
}
@ -957,27 +850,18 @@ QScriptValue SimpleJavaScriptApplet::print(QScriptContext *context, QScriptEngin
return engine->undefinedValue();
}
QScriptValue SimpleJavaScriptApplet::createPrototype(QScriptEngine *engine, const QString &name)
{
Q_UNUSED(name)
QScriptValue proto = engine->newObject();
// Hook for adding extra properties/methods
return proto;
}
QScriptValue SimpleJavaScriptApplet::variantToScriptValue(QVariant var)
{
return ::variantToScriptValue(m_engine, var);
}
void SimpleJavaScriptApplet::collectGarbage()
{
m_engine->collectGarbage();
}
/*
* Workaround the fact that QtScripts handling of variants seems a bit broken.
*/
QScriptValue SimpleJavaScriptApplet::variantToScriptValue(QVariant var)
{
return ::variantToScriptValue(m_engine, var);
}
K_EXPORT_PLASMA_APPLETSCRIPTENGINE(qscriptapplet, SimpleJavaScriptApplet)
#include "simplejavascriptapplet.moc"

View File

@ -37,7 +37,13 @@ class SimpleJavaScriptApplet : public Plasma::AppletScript
Q_OBJECT
public:
SimpleJavaScriptApplet( QObject *parent, const QVariantList &args );
enum AllowedUrl { NoUrls = 0,
HttpUrls = 1,
NetworkUrls = 2,
LocalUrls = 4 };
Q_DECLARE_FLAGS(AllowedUrls, AllowedUrl)
SimpleJavaScriptApplet(QObject *parent, const QVariantList &args);
~SimpleJavaScriptApplet();
bool init();
@ -48,7 +54,6 @@ public:
void constraintsEvent(Plasma::Constraints constraints);
bool include(const QString &path);
QSet<QString> loadedExtensions() const;
QScriptValue variantToScriptValue(QVariant var);
static QString findImageFile(QScriptEngine *engine, const QString &file);
@ -60,6 +65,7 @@ public slots:
void collectGarbage();
private:
void registerGetUrl();
bool importExtensions();
bool importBuiltinExtesion(const QString &extension);
void setupObjects();
@ -80,11 +86,10 @@ private:
static QScriptValue newPlasmaSvg(QScriptContext *context, QScriptEngine *engine);
static QScriptValue newPlasmaFrameSvg(QScriptContext *context, QScriptEngine *engine);
void installWidgets( QScriptEngine *engine );
void installWidgets(QScriptEngine *engine);
static QScriptValue createWidget(QScriptContext *context, QScriptEngine *engine);
static QScriptValue notSupported(QScriptContext *context, QScriptEngine *engine);
static QScriptValue print(QScriptContext *context, QScriptEngine *engine);
static QScriptValue createPrototype(QScriptEngine *engine, const QString &name);
static QScriptValue widgetAdjustSize(QScriptContext *context, QScriptEngine *engine);
// run extension
@ -92,6 +97,10 @@ private:
static QScriptValue runCommand(QScriptContext *context, QScriptEngine *engine);
static QScriptValue openUrl(QScriptContext *context, QScriptEngine *engine);
// file io extensions
static QScriptValue getUrl(QScriptContext *context, QScriptEngine *engine);
static AppletInterface *extractAppletInterface(QScriptEngine *engine);
static QGraphicsWidget *extractParent(QScriptContext *context,
QScriptEngine *engine,
int parentIndex = 0,
@ -108,6 +117,7 @@ private:
friend class AppletInterface;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(SimpleJavaScriptApplet::AllowedUrls)
#endif // SCRIPT_H