2009-05-13 02:55:25 +02:00
|
|
|
/*
|
2010-02-22 12:33:21 +01:00
|
|
|
* Copyright 2007-2008,2010 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 <QFile>
|
|
|
|
#include <QUiLoader>
|
|
|
|
#include <QGraphicsLayout>
|
2009-12-06 16:34:41 +01:00
|
|
|
#include <QParallelAnimationGroup>
|
2010-02-13 01:53:46 +01:00
|
|
|
#include <QPauseAnimation>
|
2009-12-06 16:34:41 +01:00
|
|
|
#include <QSequentialAnimationGroup>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <QWidget>
|
2010-02-22 12:33:21 +01:00
|
|
|
#include <QScriptEngine>
|
2009-05-13 02:55:25 +02:00
|
|
|
|
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>
|
2010-01-20 22:40:55 +01:00
|
|
|
#include <Plasma/Extender>
|
|
|
|
#include <Plasma/ExtenderItem>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/FrameSvg>
|
|
|
|
#include <Plasma/Package>
|
2009-12-16 23:05:46 +01:00
|
|
|
#include <Plasma/PopupApplet>
|
2010-01-20 22:40:55 +01:00
|
|
|
#include <Plasma/Svg>
|
2009-05-13 02:55:25 +02:00
|
|
|
#include <Plasma/VideoWidget>
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
#include "appletauthorization.h"
|
|
|
|
#include "scriptenv.h"
|
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"
|
|
|
|
#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:32:56 +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);
|
2010-02-24 01:35:37 +01:00
|
|
|
QScriptValue constructIconClass(QScriptEngine *engine);
|
2009-05-13 02:55:25 +02:00
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
Q_UNUSED(args);
|
2009-05-13 02:55:25 +02:00
|
|
|
// kDebug() << "Script applet launched, args" << applet()->startupArguments();
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
// TODO this will be set to the engine we get from QML
|
|
|
|
m_engine = new QScriptEngine(this);
|
|
|
|
m_env = new ScriptEnv(this, m_engine);
|
|
|
|
connect(m_env, SIGNAL(reportError(ScriptEnv*,bool)), this, SLOT(engineReportsError(ScriptEnv*,bool)));
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleJavaScriptApplet::~SimpleJavaScriptApplet()
|
|
|
|
{
|
|
|
|
if (s_widgetLoader.count() == 1) {
|
|
|
|
s_widgetLoader.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
void SimpleJavaScriptApplet::engineReportsError(ScriptEnv *engine, bool fatal)
|
|
|
|
{
|
|
|
|
reportError(engine, fatal);
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
void SimpleJavaScriptApplet::reportError(ScriptEnv *env, bool fatal)
|
2009-05-13 02:55:25 +02:00
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
SimpleJavaScriptApplet *jsApplet = qobject_cast<SimpleJavaScriptApplet *>(env->parent());
|
|
|
|
AppletInterface *interface = extractAppletInterface(env->engine());
|
|
|
|
const QScriptValue error = env->engine()->uncaughtException();
|
2010-02-16 18:48:54 +01:00
|
|
|
QString file = error.property("fileName").toString();
|
|
|
|
if (interface) {
|
|
|
|
file.remove(interface->package()->path());
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString failureMsg = i18n("Error in %1 on line %2.<br><br>%3",
|
|
|
|
file, error.property("lineNumber").toString(),
|
|
|
|
error.toString());
|
2009-11-11 03:09:25 +01:00
|
|
|
if (jsApplet) {
|
|
|
|
if (fatal) {
|
|
|
|
jsApplet->setFailedToLaunch(true, failureMsg);
|
|
|
|
} else {
|
|
|
|
jsApplet->showMessage(KIcon("dialog-error"), failureMsg, Plasma::ButtonNone);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kDebug() << failureMsg;
|
|
|
|
}
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
kDebug() << env->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()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine::Data &data)
|
|
|
|
{
|
2010-01-20 22:12:24 +01:00
|
|
|
QScriptValue fun = m_self.property("dataUpdated");
|
2009-05-13 02:55:25 +02:00
|
|
|
if (!fun.isFunction()) {
|
2010-01-20 22:12:24 +01:00
|
|
|
kDebug() << "Script: dataUpdated is not a function, " << fun.toString();
|
2009-05-13 02:55:25 +02:00
|
|
|
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()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-20 22:40:55 +01:00
|
|
|
void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item)
|
|
|
|
{
|
|
|
|
QScriptValueList args;
|
|
|
|
args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
|
|
|
|
callFunction("initExtenderItem", args);
|
|
|
|
}
|
|
|
|
|
2010-02-28 17:11:48 +01:00
|
|
|
void SimpleJavaScriptApplet::popupEvent(bool popped)
|
|
|
|
{
|
|
|
|
QScriptValueList args;
|
|
|
|
args << popped;
|
|
|
|
callFunction("popupEvent", args);
|
|
|
|
}
|
|
|
|
|
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()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env);
|
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));
|
2010-02-16 01:10:30 +01:00
|
|
|
args << m_engine->toScriptValue(QRectF(contentsRect));
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
QScriptContext *ctx = m_engine->pushContext();
|
|
|
|
ctx->setActivationObject(m_self);
|
|
|
|
fun.call(m_self, args);
|
|
|
|
m_engine->popContext();
|
|
|
|
|
|
|
|
if (m_engine->hasUncaughtException()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env);
|
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()) {
|
2010-02-22 12:33:21 +01:00
|
|
|
reportError(m_env);
|
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)
|
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
return m_env->include(path);
|
2009-11-14 06:19:19 +01:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2010-01-20 22:40:55 +01:00
|
|
|
connect(applet(), SIGNAL(extenderItemRestored(Plasma::ExtenderItem*)),
|
|
|
|
this, SLOT(extenderItemRestored(Plasma::ExtenderItem*)));
|
2009-05-13 02:55:25 +02:00
|
|
|
setupObjects();
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
AppletAuthorization auth(this);
|
2010-02-22 12:33:21 +01:00
|
|
|
if (!m_env->importExtensions(description(), m_self, auth)) {
|
2009-11-11 03:09:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
kDebug() << "ScriptName:" << applet()->name();
|
|
|
|
kDebug() << "ScriptCategory:" << applet()->category();
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
return m_env->include(mainScript());
|
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-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);
|
|
|
|
|
2010-02-28 17:11:48 +01:00
|
|
|
if (isPopupApplet) {
|
|
|
|
connect(applet(), SIGNAL(popupEvent(bool)), this, SLOT(popupEvent(bool)));
|
|
|
|
}
|
|
|
|
|
2009-05-13 02:55:25 +02:00
|
|
|
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);
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
m_env->registerEnums(global, AppletInterface::staticMetaObject);
|
2009-05-13 02:55:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Add a global loadui method for ui files
|
|
|
|
QScriptValue fun = m_engine->newFunction(SimpleJavaScriptApplet::loadui);
|
|
|
|
global.setProperty("loadui", 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));
|
2010-02-24 01:35:37 +01:00
|
|
|
global.setProperty("QIcon", constructIconClass(m_engine));
|
2009-05-13 02:55:25 +02:00
|
|
|
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:32:56 +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));
|
2010-01-20 22:40:55 +01:00
|
|
|
global.setProperty("ExtenderItem", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaExtenderItem));
|
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
|
|
|
{
|
2010-02-22 12:33:21 +01:00
|
|
|
return m_env->loadedExtensions();
|
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);
|
2010-02-01 17:18:49 +01:00
|
|
|
return engine->newQObject(data, QScriptEngine::QtOwnership, QScriptEngine::PreferExistingWrapperObject);
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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:16:33 +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:16:33 +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
|
|
|
}
|
|
|
|
|
2010-02-15 19:08:32 +01:00
|
|
|
QScriptValue value = engine->newQObject(anim);
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv::findScriptEnv(engine)->registerEnums(value, *anim->metaObject());
|
2010-02-15 19:08:32 +01:00
|
|
|
return value;
|
2009-12-14 22:52:40 +01:00
|
|
|
}
|
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:14:02 +01:00
|
|
|
QScriptValue fun = engine->newQObject(svg);
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv::findScriptEnv(engine)->registerEnums(fun, *svg->metaObject());
|
2010-01-13 04:14:02 +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:14:02 +01:00
|
|
|
|
|
|
|
QScriptValue fun = engine->newQObject(frameSvg);
|
|
|
|
// FIXME: why is this necessary when it is clearly declared in FrameSvg's moc?
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv::findScriptEnv(engine)->registerEnums(fun, *frameSvg->metaObject());
|
2010-01-13 04:14:02 +01:00
|
|
|
return fun;
|
2009-05-13 02:55:25 +02:00
|
|
|
}
|
|
|
|
|
2010-01-20 22:40:55 +01:00
|
|
|
QScriptValue SimpleJavaScriptApplet::newPlasmaExtenderItem(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
Plasma::Extender *extender = 0;
|
|
|
|
if (context->argumentCount() > 0) {
|
|
|
|
extender = qobject_cast<Plasma::Extender *>(context->argument(0).toQObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!extender) {
|
|
|
|
AppletInterface *interface = extractAppletInterface(engine);
|
|
|
|
if (!interface) {
|
|
|
|
engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
extender = interface->extender();
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::ExtenderItem *extenderItem = new Plasma::ExtenderItem(extender);
|
|
|
|
QScriptValue fun = engine->newQObject(extenderItem);
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv::findScriptEnv(engine)->registerEnums(fun, *extenderItem->metaObject());
|
2010-01-20 22:40:55 +01:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
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...
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv::findScriptEnv(engine)->registerEnums(fun, *w->metaObject());
|
2009-05-13 02:55:25 +02:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
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)
|
2010-02-16 18:48:54 +01:00
|
|
|
|