2009-05-13 02:55:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007-2008 Richard J. Moore <rich@kde.org>
|
2009-12-16 23:05:46 +01:00
|
|
|
* Copyright 2009 Aaron J. Seigo <aseigo@kde.org>
|
2009-05-13 02:55:25 +02:00
|
|
|
*
|
|
|
|
* 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 "simplejavascriptapplet.h"
|
|
|
|
|
2009-11-14 02:09:44 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <QScriptEngine>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QUiLoader>
|
|
|
|
#include <QGraphicsLayout>
|
2009-12-06 16:34:41 +01:00
|
|
|
#include <QParallelAnimationGroup>
|
|
|
|
#include <QSequentialAnimationGroup>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <QWidget>
|
|
|
|
|
2009-11-28 01:42:22 +01:00
|
|
|
#include <KConfigGroup>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <KDebug>
|
2009-11-14 08:28:16 +01:00
|
|
|
#include <KFileDialog>
|
2009-11-11 03:09:25 +01:00
|
|
|
#include <KIcon>
|
2009-12-16 23:05:46 +01:00
|
|
|
#include <KIO/Job>
|
2009-11-28 01:42:22 +01:00
|
|
|
#include <KMimeType>
|
|
|
|
#include <KShell>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <KStandardDirs>
|
2009-11-28 01:42:22 +01:00
|
|
|
#include <KLocale>
|
|
|
|
#include <KRun>
|
2009-05-13 02:55:25 +02:00
|
|
|
|
2009-12-03 20:32:42 +01:00
|
|
|
#include <Plasma/Animation>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/Applet>
|
|
|
|
#include <Plasma/Svg>
|
|
|
|
#include <Plasma/FrameSvg>
|
|
|
|
#include <Plasma/Package>
|
2009-12-16 23:05:46 +01:00
|
|
|
#include <Plasma/PopupApplet>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/VideoWidget>
|
|
|
|
|
2009-12-14 22:54:23 +01:00
|
|
|
#include "simplebindings/animationgroup.h"
|
2009-12-16 23:05:46 +01:00
|
|
|
#include "simplebindings/dataengine.h"
|
|
|
|
#include "simplebindings/i18n.h"
|
2009-11-27 20:34:04 +01:00
|
|
|
#include "simplebindings/appletinterface.h"
|
2009-12-16 23:05:46 +01:00
|
|
|
#include "simplebindings/bytearrayclass.h"
|
2009-11-18 23:40:38 +01:00
|
|
|
#include "simplebindings/filedialogproxy.h"
|
2009-12-16 23:05:46 +01:00
|
|
|
#include "simplebindings/variant.h"
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
using namespace Plasma;
|
|
|
|
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(QPainter*)
|
|
|
|
Q_DECLARE_METATYPE(QStyleOptionGraphicsItem*)
|
|
|
|
|
2009-11-14 08:28:16 +01:00
|
|
|
QScriptValue constructColorClass(QScriptEngine *engine);
|
2010-01-13 00:34:07 +01:00
|
|
|
QScriptValue constructEasingCurveClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructFontClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructGraphicsItemClass(QScriptEngine *engine);
|
|
|
|
QScriptValue constructKUrlClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructLinearLayoutClass(QScriptEngine *engine);
|
2009-11-29 05:17:30 +01:00
|
|
|
QScriptValue constructGridLayoutClass(QScriptEngine *engine);
|
2009-11-03 04:06:04 +01:00
|
|
|
QScriptValue constructAnchorLayoutClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructPainterClass(QScriptEngine *engine);
|
2009-11-27 21:29:14 +01:00
|
|
|
QScriptValue constructPenClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructQPixmapClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructQPointClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructQRectFClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue constructQSizeFClass(QScriptEngine *engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
QScriptValue constructTimerClass(QScriptEngine *engine);
|
2009-12-16 23:05:46 +01:00
|
|
|
void registerSimpleAppletMetaTypes(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
class DummyService : public Service
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ServiceJob *createJob(const QString &operation, QMap<QString, QVariant> ¶meters)
|
|
|
|
{
|
|
|
|
Q_UNUSED(operation)
|
|
|
|
Q_UNUSED(parameters)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerEnums(QScriptEngine *engine, QScriptValue &scriptValue, const QMetaObject &meta)
|
|
|
|
{
|
|
|
|
//manually create enum values. ugh
|
2009-11-11 03:09:25 +01:00
|
|
|
for (int i = 0; i < meta.enumeratorCount(); ++i) {
|
2009-05-13 02:55:25 +02:00
|
|
|
QMetaEnum e = meta.enumerator(i);
|
|
|
|
//kDebug() << e.name();
|
|
|
|
for (int i=0; i < e.keyCount(); ++i) {
|
|
|
|
//kDebug() << e.key(i) << e.value(i);
|
|
|
|
scriptValue.setProperty(e.key(i), QScriptValue(engine, e.value(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KSharedPtr<UiLoader> SimpleJavaScriptApplet::s_widgetLoader;
|
2009-10-21 04:26:44 +02:00
|
|
|
QHash<QString, Plasma::Animator::Animation> SimpleJavaScriptApplet::s_animationDefs;
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantList &args)
|
|
|
|
: Plasma::AppletScript(parent)
|
|
|
|
{
|
|
|
|
Q_UNUSED(args)
|
|
|
|
// kDebug() << "Script applet launched, args" << applet()->startupArguments();
|
|
|
|
|
|
|
|
m_engine = new QScriptEngine(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleJavaScriptApplet::~SimpleJavaScriptApplet()
|
|
|
|
{
|
|
|
|
if (s_widgetLoader.count() == 1) {
|
|
|
|
s_widgetLoader.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:09:25 +01:00
|
|
|
void SimpleJavaScriptApplet::reportError(QScriptEngine *engine, bool fatal)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-11-11 03:09:25 +01:00
|
|
|
SimpleJavaScriptApplet *jsApplet = qobject_cast<SimpleJavaScriptApplet *>(engine->parent());
|
|
|
|
const QString failureMsg = i18n("Script failure on line %1:\n%2",
|
|
|
|
QString::number(engine->uncaughtExceptionLineNumber()),
|
|
|
|
engine->uncaughtException().toString());
|
|
|
|
if (jsApplet) {
|
|
|
|
if (fatal) {
|
|
|
|
jsApplet->setFailedToLaunch(true, failureMsg);
|
|
|
|
} else {
|
|
|
|
jsApplet->showMessage(KIcon("dialog-error"), failureMsg, Plasma::ButtonNone);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kDebug() << failureMsg;
|
|
|
|
}
|
|
|
|
|
2009-10-22 03:55:53 +02:00
|
|
|
kDebug() << engine->uncaughtExceptionBacktrace();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::configChanged()
|
|
|
|
{
|
|
|
|
QScriptValue fun = m_self.property("configChanged");
|
|
|
|
if (!fun.isFunction()) {
|
|
|
|
kDebug() << "Script: plasmoid.configChanged is not a function, " << fun.toString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
//kDebug() << "calling plasmoid";
|
|
|
|
fun.call(m_self);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2009-10-22 03:55:53 +02:00
|
|
|
reportError(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine::Data &data)
|
|
|
|
{
|
|
|
|
QScriptValue fun = m_self.property("dataUpdate");
|
|
|
|
if (!fun.isFunction()) {
|
|
|
|
kDebug() << "Script: dataUpdate is not a function, " << fun.toString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data);
|
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self, args);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2009-10-22 03:55:53 +02:00
|
|
|
reportError(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::executeAction(const QString &name)
|
|
|
|
{
|
2009-10-22 03:55:53 +02:00
|
|
|
//callFunction("action_" + name);
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue fun = m_self.property("action_" + name);
|
|
|
|
if (fun.isFunction()) {
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2009-10-22 03:55:53 +02:00
|
|
|
reportError(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
2009-10-22 03:55:53 +02:00
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(contentsRect)
|
|
|
|
|
|
|
|
//kDebug() << "paintInterface() (c++)";
|
|
|
|
QScriptValue fun = m_self.property("paintInterface");
|
|
|
|
if (!fun.isFunction()) {
|
|
|
|
//kDebug() << "Script: paintInterface is not a function, " << fun.toString();
|
|
|
|
AppletScript::paintInterface(p, option, contentsRect);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->toScriptValue(p);
|
|
|
|
args << m_engine->toScriptValue(const_cast<QStyleOptionGraphicsItem*>(option));
|
|
|
|
args << m_engine->toScriptValue(contentsRect);
|
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self, args);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2009-10-22 03:55:53 +02:00
|
|
|
reportError(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QAction*> SimpleJavaScriptApplet::contextualActions()
|
|
|
|
{
|
|
|
|
return m_interface->contextualActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::callFunction(const QString &functionName, const QScriptValueList &args)
|
|
|
|
{
|
|
|
|
QScriptValue fun = m_self.property(functionName);
|
|
|
|
if (fun.isFunction()) {
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self, args);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2009-10-22 03:55:53 +02:00
|
|
|
reportError(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::constraintsEvent(Plasma::Constraints constraints)
|
|
|
|
{
|
|
|
|
QString functionName;
|
|
|
|
|
|
|
|
if (constraints & Plasma::FormFactorConstraint) {
|
|
|
|
callFunction("formFactorChanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::LocationConstraint) {
|
|
|
|
callFunction("locationChanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::ContextConstraint) {
|
2009-11-13 23:21:56 +01:00
|
|
|
callFunction("currentActivityChanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::SizeConstraint) {
|
|
|
|
callFunction("sizeChanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints & Plasma::ImmutableConstraint) {
|
|
|
|
callFunction("immutabilityChanged");
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-14 06:19:19 +01:00
|
|
|
bool SimpleJavaScriptApplet::include(const QString &path)
|
|
|
|
{
|
|
|
|
QFile file(path);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
kWarning() << i18n("Unable to load script file: %1", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString script = file.readAll();
|
|
|
|
//kDebug() << "Script says" << script;
|
|
|
|
|
|
|
|
m_engine->evaluate(script);
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
|
|
|
reportError(m_engine, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-21 04:26:44 +02:00
|
|
|
void SimpleJavaScriptApplet::populateAnimationsHash()
|
|
|
|
{
|
|
|
|
if (s_animationDefs.isEmpty()) {
|
|
|
|
s_animationDefs.insert("fade", Plasma::Animator::FadeAnimation);
|
2009-12-08 10:24:24 +01:00
|
|
|
s_animationDefs.insert("geometry", Plasma::Animator::GeometryAnimation);
|
2009-10-21 04:26:44 +02:00
|
|
|
s_animationDefs.insert("grow", Plasma::Animator::GrowAnimation);
|
|
|
|
s_animationDefs.insert("pulse", Plasma::Animator::PulseAnimation);
|
|
|
|
s_animationDefs.insert("rotate", Plasma::Animator::RotationAnimation);
|
|
|
|
s_animationDefs.insert("rotateStacked", Plasma::Animator::RotationStackedAnimation);
|
|
|
|
s_animationDefs.insert("slide", Plasma::Animator::SlideAnimation);
|
2009-12-08 10:24:24 +01:00
|
|
|
s_animationDefs.insert("zoom", Plasma::Animator::ZoomAnimation);
|
2009-10-21 04:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
bool SimpleJavaScriptApplet::init()
|
|
|
|
{
|
|
|
|
setupObjects();
|
|
|
|
|
2009-11-11 03:09:25 +01:00
|
|
|
if (!importExtensions()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
kDebug() << "ScriptName:" << applet()->name();
|
|
|
|
kDebug() << "ScriptCategory:" << applet()->category();
|
|
|
|
|
2009-11-14 06:19:19 +01:00
|
|
|
return include(mainScript());
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-11-28 01:42:22 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::runApplication(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUrl::List urls;
|
|
|
|
if (context->argumentCount() > 1) {
|
|
|
|
urls = qscriptvalue_cast<KUrl::List>(context->argument(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString app = context->argument(0).toString();
|
|
|
|
|
|
|
|
KService::Ptr service = KService::serviceByStorageId(app);
|
|
|
|
if (service) {
|
|
|
|
return KRun::run(*service, urls, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString exec = KGlobal::dirs()->findExe(app);
|
|
|
|
if (!exec.isEmpty()) {
|
|
|
|
return KRun::run(exec, urls, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::runCommand(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString exec = KGlobal::dirs()->findExe(context->argument(0).toString());
|
|
|
|
if (!exec.isEmpty()) {
|
|
|
|
QString args;
|
|
|
|
if (context->argumentCount() > 1) {
|
|
|
|
const QStringList argList = qscriptvalue_cast<QStringList>(context->argument(1));
|
|
|
|
if (!argList.isEmpty()) {
|
|
|
|
args = ' ' + KShell::joinArgs(argList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KRun::runCommand(exec + args, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::openUrl(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue v = context->argument(0);
|
|
|
|
KUrl url = v.isString() ? KUrl(v.toString()) : qscriptvalue_cast<KUrl>(v);
|
|
|
|
if (url.isValid()) {
|
|
|
|
return KRun::runUrl(url, KMimeType::findByUrl(url)->name(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:28:16 +01:00
|
|
|
bool SimpleJavaScriptApplet::importBuiltinExtesion(const QString &extension)
|
|
|
|
{
|
2009-11-17 04:56:06 +01:00
|
|
|
kDebug() << extension;
|
2009-11-14 08:28:16 +01:00
|
|
|
if ("filedialog" == extension) {
|
2009-11-17 04:56:06 +01:00
|
|
|
FileDialogProxy::registerWithRuntime(m_engine);
|
2009-11-14 08:28:16 +01:00
|
|
|
return true;
|
|
|
|
} else if ("launchapp" == extension) {
|
2009-11-28 01:42:22 +01:00
|
|
|
m_self.setProperty("runApplication", m_engine->newFunction(SimpleJavaScriptApplet::runApplication));
|
|
|
|
m_self.setProperty("runCommand", m_engine->newFunction(SimpleJavaScriptApplet::runCommand));
|
|
|
|
m_self.setProperty("openUrl", m_engine->newFunction(SimpleJavaScriptApplet::openUrl));
|
2009-11-14 08:28:16 +01:00
|
|
|
return true;
|
|
|
|
} else if ("http" == extension) {
|
2009-12-16 23:05:46 +01:00
|
|
|
m_interface->setAllowedUrls(m_interface->allowedUrls() | HttpUrls);
|
|
|
|
registerGetUrl();
|
2009-11-14 08:28:16 +01:00
|
|
|
return true;
|
|
|
|
} else if ("networkio" == extension) {
|
2009-12-16 23:05:46 +01:00
|
|
|
m_interface->setAllowedUrls(m_interface->allowedUrls() | HttpUrls | NetworkUrls);
|
|
|
|
registerGetUrl();
|
2009-11-14 08:28:16 +01:00
|
|
|
return true;
|
|
|
|
} else if ("localio" == extension) {
|
2009-12-16 23:05:46 +01:00
|
|
|
m_interface->setAllowedUrls(m_interface->allowedUrls() | LocalUrls);
|
|
|
|
registerGetUrl();
|
2009-11-14 08:28:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:09:25 +01:00
|
|
|
bool SimpleJavaScriptApplet::importExtensions()
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-11-11 03:09:25 +01:00
|
|
|
KPluginInfo info = description();
|
2009-11-17 04:56:06 +01:00
|
|
|
QStringList requiredExtensions = info.service()->property("X-Plasma-RequiredExtensions", QVariant::StringList).toStringList();
|
2009-11-11 03:09:25 +01:00
|
|
|
kDebug() << "required extensions are" << requiredExtensions;
|
2009-11-14 08:28:16 +01:00
|
|
|
foreach (const QString &ext, requiredExtensions) {
|
|
|
|
QString extension = ext.toLower();
|
|
|
|
if (m_extensions.contains(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:09:25 +01:00
|
|
|
if (!applet()->hasAuthorization(extension)) {
|
|
|
|
setFailedToLaunch(true,
|
|
|
|
i18n("Authorization for required extension '%1' was denied.",
|
|
|
|
extension));
|
|
|
|
return false;
|
2009-11-11 03:12:40 +01:00
|
|
|
}
|
|
|
|
|
2009-11-14 08:28:16 +01:00
|
|
|
if (!importBuiltinExtesion(extension)) {
|
|
|
|
m_engine->importExtension(extension);
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:12:40 +01:00
|
|
|
if (m_engine->hasUncaughtException()) {
|
|
|
|
reportError(m_engine, true);
|
|
|
|
return false;
|
2009-11-14 08:28:16 +01:00
|
|
|
} else {
|
|
|
|
m_extensions << extension;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
2009-11-11 03:09:25 +01:00
|
|
|
|
2009-11-17 04:56:06 +01:00
|
|
|
QStringList optionalExtensions = info.service()->property("X-Plasma-OptionalExtensions", QVariant::StringList).toStringList();
|
2009-11-28 01:42:22 +01:00
|
|
|
kDebug() << "optional extensions are" << optionalExtensions;
|
|
|
|
foreach (const QString &ext, optionalExtensions) {
|
2009-11-14 08:28:16 +01:00
|
|
|
QString extension = ext.toLower();
|
|
|
|
|
|
|
|
if (m_extensions.contains(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:12:40 +01:00
|
|
|
if (!applet()->hasAuthorization(extension)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:28:16 +01:00
|
|
|
if (!importBuiltinExtesion(extension)) {
|
|
|
|
m_engine->importExtension(extension);
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:12:40 +01:00
|
|
|
if (m_engine->hasUncaughtException()) {
|
|
|
|
reportError(m_engine);
|
2009-11-14 08:28:16 +01:00
|
|
|
} else {
|
|
|
|
m_extensions << extension;
|
2009-11-11 03:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-11 03:09:25 +01:00
|
|
|
return true;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::setupObjects()
|
|
|
|
{
|
|
|
|
QScriptValue global = m_engine->globalObject();
|
|
|
|
|
2009-10-21 04:26:44 +02:00
|
|
|
// Bindings for animations
|
|
|
|
global.setProperty("animation", m_engine->newFunction(SimpleJavaScriptApplet::animation));
|
2009-10-21 09:53:49 +02:00
|
|
|
global.setProperty("AnimationGroup", m_engine->newFunction(SimpleJavaScriptApplet::animationGroup));
|
2009-12-06 16:34:41 +01:00
|
|
|
global.setProperty("ParallelAnimationGroup", m_engine->newFunction(SimpleJavaScriptApplet::parallelAnimationGroup));
|
2009-10-21 04:26:44 +02:00
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
// Bindings for data engine
|
2009-12-03 21:58:17 +01:00
|
|
|
m_engine->setDefaultPrototype(qMetaTypeId<DataEngine*>(), m_engine->newQObject(new DataEngine(), QScriptEngine::ScriptOwnership));
|
|
|
|
m_engine->setDefaultPrototype(qMetaTypeId<Service*>(), m_engine->newQObject(new DummyService(), QScriptEngine::ScriptOwnership));
|
|
|
|
m_engine->setDefaultPrototype(qMetaTypeId<ServiceJob*>(),
|
|
|
|
m_engine->newQObject(new ServiceJob(QString(), QString(), QMap<QString, QVariant>()),
|
|
|
|
QScriptEngine::ScriptOwnership ));
|
2009-05-13 02:55:25 +02:00
|
|
|
|
2009-07-04 06:30:57 +02:00
|
|
|
bindI18N(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("dataEngine", m_engine->newFunction(SimpleJavaScriptApplet::dataEngine));
|
|
|
|
global.setProperty("service", m_engine->newFunction(SimpleJavaScriptApplet::service));
|
|
|
|
|
|
|
|
// Expose applet interface
|
2009-12-16 23:05:46 +01:00
|
|
|
const bool isPopupApplet = qobject_cast<Plasma::PopupApplet *>(applet());
|
|
|
|
m_interface = isPopupApplet ? new PopupAppletInterface(this) : new AppletInterface(this);
|
2009-05-13 02:55:25 +02:00
|
|
|
m_self = m_engine->newQObject(m_interface);
|
|
|
|
m_self.setScope(global);
|
|
|
|
global.setProperty("plasmoid", m_self);
|
|
|
|
|
|
|
|
QScriptValue args = m_engine->newArray();
|
|
|
|
int i = 0;
|
|
|
|
foreach (const QVariant &arg, applet()->startupArguments()) {
|
2009-12-16 23:05:46 +01:00
|
|
|
args.setProperty(i, ::variantToScriptValue(m_engine, arg));
|
2009-05-13 02:55:25 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
global.setProperty("startupArguments", args);
|
|
|
|
|
|
|
|
registerEnums(m_engine, global, AppletInterface::staticMetaObject);
|
|
|
|
|
|
|
|
|
|
|
|
// Add a global loadui method for ui files
|
|
|
|
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>();
|
|
|
|
|
2009-10-21 01:31:12 +02:00
|
|
|
// Add stuff from Qt
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QPainter", constructPainterClass(m_engine));
|
|
|
|
global.setProperty("QGraphicsItem", constructGraphicsItemClass(m_engine));
|
|
|
|
global.setProperty("QTimer", constructTimerClass(m_engine));
|
|
|
|
global.setProperty("QFont", constructFontClass(m_engine));
|
2009-11-14 08:28:16 +01:00
|
|
|
global.setProperty("QColor", constructColorClass(m_engine));
|
2010-01-13 00:34:07 +01:00
|
|
|
global.setProperty("QEasingCurve", constructEasingCurveClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QRectF", constructQRectFClass(m_engine));
|
2009-11-27 21:29:14 +01:00
|
|
|
global.setProperty("QPen", constructPenClass(m_engine));
|
2009-10-21 01:31:12 +02:00
|
|
|
global.setProperty("QPixmap", constructQPixmapClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("QSizeF", constructQSizeFClass(m_engine));
|
|
|
|
global.setProperty("QPoint", constructQPointClass(m_engine));
|
|
|
|
global.setProperty("LinearLayout", constructLinearLayoutClass(m_engine));
|
2009-11-29 05:17:30 +01:00
|
|
|
global.setProperty("GridLayout", constructGridLayoutClass(m_engine));
|
2009-11-03 04:06:04 +01:00
|
|
|
global.setProperty("AnchorLayout", constructAnchorLayoutClass(m_engine));
|
2009-12-16 23:05:46 +01:00
|
|
|
ByteArrayClass *baClass = new ByteArrayClass(m_engine);
|
|
|
|
global.setProperty("ByteArray", baClass->constructor());
|
2009-10-21 01:31:12 +02:00
|
|
|
|
|
|
|
// Add stuff from KDE libs
|
2009-11-28 01:42:22 +01:00
|
|
|
qScriptRegisterSequenceMetaType<KUrl::List>(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
global.setProperty("Url", constructKUrlClass(m_engine));
|
|
|
|
|
2009-10-21 01:31:12 +02:00
|
|
|
// Add stuff from Plasma
|
|
|
|
global.setProperty("PlasmaSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg));
|
|
|
|
global.setProperty("PlasmaFrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
|
2009-11-27 03:21:49 +01:00
|
|
|
global.setProperty("Svg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg));
|
|
|
|
global.setProperty("FrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
|
2009-10-21 01:31:12 +02:00
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
registerSimpleAppletMetaTypes(m_engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
installWidgets(m_engine);
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:32:00 +01:00
|
|
|
QSet<QString> SimpleJavaScriptApplet::loadedExtensions() const
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-11-14 08:32:00 +01:00
|
|
|
return m_extensions;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
AppletInterface *SimpleJavaScriptApplet::extractAppletInterface(QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QScriptValue appletValue = engine->globalObject().property("plasmoid");
|
|
|
|
return qobject_cast<AppletInterface*>(appletValue.toQObject());
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::dataEngine(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("dataEngine() takes one argument"));
|
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
AppletInterface *interface = extractAppletInterface(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
|
|
|
return context->throwError(i18n("Could not extract the Applet"));
|
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
const QString dataEngine = context->argument(0).toString();
|
2009-05-13 02:55:25 +02:00
|
|
|
DataEngine *data = interface->dataEngine(dataEngine);
|
|
|
|
return engine->newQObject(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::service(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 2) {
|
|
|
|
return context->throwError(i18n("service() takes two arguments"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString dataEngine = context->argument(0).toString();
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
AppletInterface *interface = extractAppletInterface(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
|
|
|
return context->throwError(i18n("Could not extract the Applet"));
|
|
|
|
}
|
|
|
|
|
|
|
|
DataEngine *data = interface->dataEngine(dataEngine);
|
|
|
|
QString source = context->argument(1).toString();
|
|
|
|
Service *service = data->serviceForSource(source);
|
|
|
|
//kDebug( )<< "lets try to get" << source << "from" << dataEngine;
|
2009-12-03 21:58:17 +01:00
|
|
|
return engine->newQObject(service, QScriptEngine::AutoOwnership);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 04:26:44 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::animation(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("animation() takes one argument"));
|
|
|
|
}
|
|
|
|
|
2009-12-04 20:14:40 +01:00
|
|
|
populateAnimationsHash();
|
2009-10-21 04:26:44 +02:00
|
|
|
QString animName = context->argument(0).toString().toLower();
|
2009-12-04 20:14:40 +01:00
|
|
|
const bool isPause = animName == "pause";
|
2010-01-12 12:18:32 +01:00
|
|
|
const bool isProperty = animName == "property";
|
|
|
|
if (!isPause && !isProperty && !s_animationDefs.contains(animName)) {
|
2009-10-21 04:26:44 +02:00
|
|
|
return context->throwError(i18n("%1 is not a known animation type", animName));
|
|
|
|
}
|
|
|
|
|
2009-12-14 22:52:40 +01:00
|
|
|
bool parentIsApplet = false;
|
|
|
|
QGraphicsWidget *parent = extractParent(context, engine, 0, &parentIsApplet);
|
2009-12-04 20:14:40 +01:00
|
|
|
if (isPause) {
|
2009-12-06 16:34:41 +01:00
|
|
|
QPauseAnimation *pause = new QPauseAnimation(parent);
|
2009-12-04 20:14:40 +01:00
|
|
|
return engine->newQObject(pause);
|
2010-01-12 12:18:32 +01:00
|
|
|
} else if (isProperty) {
|
|
|
|
QPropertyAnimation *propertyAnim = new QPropertyAnimation(parent);
|
|
|
|
return engine->newQObject(propertyAnim);
|
2009-12-04 20:14:40 +01:00
|
|
|
} else {
|
2009-12-06 16:34:41 +01:00
|
|
|
Plasma::Animation *anim = Plasma::Animator::create(s_animationDefs.value(animName), parent);
|
2009-12-14 22:52:40 +01:00
|
|
|
if (anim) {
|
|
|
|
if (!parentIsApplet) {
|
2010-01-04 10:24:13 +01:00
|
|
|
anim->setTargetWidget(parent);
|
2009-12-14 22:52:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return engine->newQObject(anim);
|
|
|
|
}
|
2009-12-04 20:14:40 +01:00
|
|
|
}
|
2009-12-14 22:52:40 +01:00
|
|
|
|
|
|
|
return context->throwError(i18n("%1 is not a known animation type", animName));
|
2009-10-21 04:26:44 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 09:53:49 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::animationGroup(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *parent = extractParent(context, engine);
|
2009-12-14 22:54:23 +01:00
|
|
|
SequentialAnimationGroup *group = new SequentialAnimationGroup(parent);
|
2009-12-06 16:34:41 +01:00
|
|
|
return engine->newQObject(group);
|
|
|
|
}
|
2009-10-21 09:53:49 +02:00
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::parallelAnimationGroup(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *parent = extractParent(context, engine);
|
2009-12-14 22:54:23 +01:00
|
|
|
ParallelAnimationGroup *group = new ParallelAnimationGroup(parent);
|
2009-10-21 09:53:49 +02:00
|
|
|
return engine->newQObject(group);
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::loadui(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("loadui() takes one argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = context->argument(0).toString();
|
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
return context->throwError(i18n("Unable to open '%1'",filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
QUiLoader loader;
|
|
|
|
QWidget *w = loader.load(&f);
|
|
|
|
f.close();
|
|
|
|
|
2009-12-03 21:58:17 +01:00
|
|
|
return engine->newQObject(w, QScriptEngine::AutoOwnership);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-10-21 01:15:33 +02:00
|
|
|
QString SimpleJavaScriptApplet::findImageFile(QScriptEngine *engine, const QString &file)
|
|
|
|
{
|
2009-12-16 23:05:46 +01:00
|
|
|
AppletInterface *interface = extractAppletInterface(engine);
|
2009-10-21 01:15:33 +02:00
|
|
|
if (!interface) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return interface->package()->filePath("images", file);
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
QString SimpleJavaScriptApplet::findSvg(QScriptEngine *engine, const QString &file)
|
|
|
|
{
|
2009-12-16 23:05:46 +01:00
|
|
|
AppletInterface *interface = extractAppletInterface(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = interface->package()->filePath("images", file + ".svg");
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
path = interface->package()->filePath("images", file + ".svgz");
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::newPlasmaSvg(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("Constructor takes at least 1 argument"));
|
|
|
|
}
|
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
const QString filename = context->argument(0).toString();
|
2009-05-13 02:55:25 +02:00
|
|
|
bool parentedToApplet = false;
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *parent = extractParent(context, engine, 1, &parentedToApplet);
|
2009-05-13 02:55:25 +02:00
|
|
|
Svg *svg = new Svg(parent);
|
2009-10-21 02:01:26 +02:00
|
|
|
svg->setImagePath(parentedToApplet ? findSvg(engine, filename) : filename);
|
2010-01-13 04:15:17 +01:00
|
|
|
QScriptValue fun = engine->newQObject(svg);
|
2010-01-13 08:20:16 +01:00
|
|
|
registerEnums(fun, *svg->metaObject());
|
2010-01-13 04:15:17 +01:00
|
|
|
return fun;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::newPlasmaFrameSvg(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() == 0) {
|
|
|
|
return context->throwError(i18n("Constructor takes at least 1 argument"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filename = context->argument(0).toString();
|
|
|
|
|
|
|
|
bool parentedToApplet = false;
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *parent = extractParent(context, engine, 1, &parentedToApplet);
|
2009-05-13 02:55:25 +02:00
|
|
|
FrameSvg *frameSvg = new FrameSvg(parent);
|
|
|
|
frameSvg->setImagePath(parentedToApplet ? filename : findSvg(engine, filename));
|
2010-01-13 04:15:17 +01:00
|
|
|
|
|
|
|
QScriptValue fun = engine->newQObject(frameSvg);
|
|
|
|
// FIXME: why is this necessary when it is clearly declared in FrameSvg's moc?
|
2010-01-13 08:20:16 +01:00
|
|
|
registerEnums(fun, *frameSvg->metaObject());
|
2010-01-13 04:15:17 +01:00
|
|
|
return fun;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-11-27 21:47:31 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::widgetAdjustSize(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(context->thisObject().toQObject());
|
|
|
|
if (widget) {
|
|
|
|
widget->adjustSize();
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QScriptValue globalObject = engine->globalObject();
|
|
|
|
if (!s_widgetLoader) {
|
|
|
|
s_widgetLoader = new UiLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &widget, s_widgetLoader->availableWidgets()) {
|
|
|
|
QScriptValue fun = engine->newFunction(createWidget);
|
|
|
|
QScriptValue name = engine->toScriptValue(widget);
|
|
|
|
fun.setProperty(QString("functionName"), name,
|
|
|
|
QScriptValue::ReadOnly | QScriptValue::Undeletable | QScriptValue::SkipInEnumeration);
|
2009-12-16 23:05:46 +01:00
|
|
|
fun.setProperty(QString("prototype"), engine->newObject());
|
2009-05-13 02:55:25 +02:00
|
|
|
globalObject.setProperty(widget, fun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
QGraphicsWidget *SimpleJavaScriptApplet::extractParent(QScriptContext *context, QScriptEngine *engine,
|
|
|
|
int argIndex, bool *parentedToApplet)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-12-06 16:34:41 +01:00
|
|
|
if (parentedToApplet) {
|
|
|
|
*parentedToApplet = false;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *parent = 0;
|
2009-12-06 16:34:41 +01:00
|
|
|
if (context->argumentCount() >= argIndex) {
|
|
|
|
parent = qobject_cast<QGraphicsWidget*>(context->argument(argIndex).toQObject());
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!parent) {
|
2009-12-16 23:05:46 +01:00
|
|
|
AppletInterface *interface = extractAppletInterface(engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!interface) {
|
2009-12-06 16:34:41 +01:00
|
|
|
return 0;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-10-22 03:55:53 +02:00
|
|
|
//kDebug() << "got the applet!";
|
2009-05-13 02:55:25 +02:00
|
|
|
parent = interface->applet();
|
2009-12-06 16:34:41 +01:00
|
|
|
|
|
|
|
if (parentedToApplet) {
|
|
|
|
*parentedToApplet = true;
|
|
|
|
}
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:34:41 +01:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::createWidget(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
QGraphicsWidget *parent = extractParent(context, engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
QString self = context->callee().property("functionName").toString();
|
|
|
|
if (!s_widgetLoader) {
|
|
|
|
s_widgetLoader = new UiLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *w = s_widgetLoader->createWidget(self, parent);
|
|
|
|
|
|
|
|
if (!w) {
|
|
|
|
return QScriptValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue fun = engine->newQObject(w);
|
|
|
|
fun.setPrototype(context->callee().property("prototype"));
|
2009-11-27 21:47:31 +01:00
|
|
|
fun.setProperty("adjustSize", engine->newFunction(widgetAdjustSize));
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
//register enums will be accessed for instance as frame.Sunken for Frame shadow...
|
|
|
|
registerEnums(engine, fun, *w->metaObject());
|
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::notSupported(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Q_UNUSED(engine)
|
|
|
|
QString message = context->callee().property("message").toString();
|
|
|
|
return context->throwError(i18n("This operation was not supported, %1", message) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QScriptValue SimpleJavaScriptApplet::print(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (context->argumentCount() != 1) {
|
|
|
|
return context->throwError(i18n("print() takes one argument"));
|
|
|
|
}
|
|
|
|
|
2009-11-14 02:09:44 +01:00
|
|
|
//TODO: a GUI console? :)
|
|
|
|
std::cout << context->argument(0).toString().toLocal8Bit().constData() << std::endl;
|
2009-05-13 02:55:25 +02:00
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
void SimpleJavaScriptApplet::collectGarbage()
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2009-12-16 23:05:46 +01:00
|
|
|
m_engine->collectGarbage();
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 23:05:46 +01:00
|
|
|
/*
|
|
|
|
* Workaround the fact that QtScripts handling of variants seems a bit broken.
|
|
|
|
*/
|
2009-05-13 02:55:25 +02:00
|
|
|
QScriptValue SimpleJavaScriptApplet::variantToScriptValue(QVariant var)
|
|
|
|
{
|
|
|
|
return ::variantToScriptValue(m_engine, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
K_EXPORT_PLASMA_APPLETSCRIPTENGINE(qscriptapplet, SimpleJavaScriptApplet)
|
|
|
|
#include "simplejavascriptapplet.moc"
|