diff --git a/appinterface.cpp b/appinterface.cpp new file mode 100644 index 000000000..12072b961 --- /dev/null +++ b/appinterface.cpp @@ -0,0 +1,107 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 "appinterface.h" + +#include +#include + +#include +#include +#include + +#include "scriptengine.h" + +AppInterface::AppInterface(Plasma::Corona *corona, QObject *parent) + : QObject(parent), + m_corona(corona) +{ + +} + +int AppInterface::screenCount() const +{ + return m_corona->numScreens(); +} + +QRectF AppInterface::screenGeometry(int screen) const +{ + return m_corona->screenGeometry(screen); +} + +QList AppInterface::activityIds() const +{ + //FIXME: the ints could overflow since Applet::id() returns a uint, + // however QScript deals with QList very, very poory + QList containments; + + foreach (Plasma::Containment *c, m_corona->containments()) { + if (!ScriptEngine::isPanel(c)) { + containments.append(c->id()); + } + } + + return containments; +} + +QList AppInterface::panelIds() const +{ + //FIXME: the ints could overflow since Applet::id() returns a uint, + // however QScript deals with QList very, very poory + QList panels; + + foreach (Plasma::Containment *c, m_corona->containments()) { + //kDebug() << "checking" << (QObject*)c << isPanel(c); + if (ScriptEngine::isPanel(c)) { + panels.append(c->id()); + } + } + + return panels; +} + +void AppInterface::lockCorona(bool locked) +{ + m_corona->setImmutability(locked ? Plasma::UserImmutable : Plasma::Mutable); +} + +bool AppInterface::coronaLocked() const +{ + return m_corona->immutability() != Plasma::Mutable; +} + +void AppInterface::sleep(int ms) +{ + QEventLoop loop; + QTimer::singleShot(ms, &loop, SLOT(quit())); + loop.exec(); +} + +bool AppInterface::hasBattery() const +{ + Plasma::DataEngineManager *engines = Plasma::DataEngineManager::self(); + Plasma::DataEngine *power = engines->loadEngine("powermanagement"); + + const QStringList batteries = power->query("Battery")["sources"].toStringList(); + engines->unloadEngine("powermanagement"); + return !batteries.isEmpty(); +} + +#include "appinterface.moc" + diff --git a/appinterface.h b/appinterface.h new file mode 100644 index 000000000..6d25a4b01 --- /dev/null +++ b/appinterface.h @@ -0,0 +1,66 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 APPINTERFACE +#define APPINTERFACE + +#include +#include + +#include "../plasmagenericshell_export.h" + +namespace Plasma +{ + class Applet; + class Containment; + class Corona; +} // namespace Plasma + +class PLASMAGENERICSHELL_EXPORT AppInterface : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool locked READ coronaLocked WRITE lockCorona) + Q_PROPERTY(bool hasBattery READ hasBattery) + Q_PROPERTY(int screenCount READ screenCount) + Q_PROPERTY(QList activityIds READ activityIds) + Q_PROPERTY(QList panelIds READ panelIds) + +public: + AppInterface(Plasma::Corona *corona, QObject *parent = 0); + + bool hasBattery() const; + int screenCount() const; + QList activityIds() const; + QList panelIds() const; + bool coronaLocked() const; + +public Q_SLOTS: + QRectF screenGeometry(int screen) const; + void lockCorona(bool locked); + void sleep(int ms); + +Q_SIGNALS: + void print(const QString &string); + +private: + Plasma::Corona *m_corona; +}; + +#endif + diff --git a/applet.cpp b/applet.cpp new file mode 100644 index 000000000..96fc80f1c --- /dev/null +++ b/applet.cpp @@ -0,0 +1,125 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 "applet.h" + +#include +#include + +#include +#include +#include + +Applet::Applet(QObject *parent) + : QObject(parent), + m_configDirty(false) +{ +} + +Applet::~Applet() +{ + if (m_configDirty) { + reloadConfig(); + } +} + +void Applet::setCurrentConfigGroup(const QStringList &groupNames) +{ + Plasma::Applet *app = applet(); + if (!app) { + m_configGroup = KConfigGroup(); + m_configGroupPath.clear(); + return; + } + + m_configGroup = app->config(); + m_configGroupPath = groupNames; + + foreach (const QString &groupName, groupNames) { + m_configGroup = KConfigGroup(&m_configGroup, groupName); + } +} + +QStringList Applet::currentConfigGroup() const +{ + return m_configGroupPath; +} + +QStringList Applet::configKeys() const +{ + if (m_configGroup.isValid()) { + return m_configGroup.keyList(); + } + + return QStringList(); +} + +QStringList Applet::configGroups() const +{ + if (m_configGroup.isValid()) { + return m_configGroup.groupList(); + } + + return QStringList(); +} + +QVariant Applet::readConfig(const QString &key, const QVariant &def) const +{ + if (m_configGroup.isValid()) { + return m_configGroup.readEntry(key, def); + } else { + return QVariant(); + } +} + +void Applet::writeConfig(const QString &key, const QVariant &value) +{ + if (m_configGroup.isValid()) { + m_configGroup.writeEntry(key, value); + m_configDirty = true; + } +} + +void Applet::reloadConfig() +{ + Plasma::Applet *app = applet(); + if (app) { + KConfigGroup cg = app->config(); + + if (!app->isContainment()) { + app->restore(cg); + } + + app->configChanged(); + + if (app->containment() && app->containment()->corona()) { + app->containment()->corona()->requestConfigSync(); + } + + m_configDirty = false; + } +} + +Plasma::Applet *Applet::applet() const +{ + return 0; +} + +#include "applet.moc" + diff --git a/applet.h b/applet.h new file mode 100644 index 000000000..83be84c00 --- /dev/null +++ b/applet.h @@ -0,0 +1,64 @@ +/* + * Copyright 2010 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 APPLET +#define APPLET + +#include +#include + +#include + +#include "../plasmagenericshell_export.h" + +namespace Plasma +{ + class Applet; +} // namespace Plasma + +class PLASMAGENERICSHELL_EXPORT Applet : public QObject +{ + Q_OBJECT + +public: + Applet(QObject *parent = 0); + ~Applet(); + + QStringList configKeys() const; + QStringList configGroups() const; + + void setCurrentConfigGroup(const QStringList &groupNames); + QStringList currentConfigGroup() const; + + virtual Plasma::Applet *applet() const; + +public Q_SLOTS: + virtual QVariant readConfig(const QString &key, const QVariant &def = QString()) const; + virtual void writeConfig(const QString &key, const QVariant &value); + virtual void reloadConfig(); + +private: + KConfigGroup m_configGroup; + QStringList m_configGroupPath; + bool m_configDirty; +}; + + +#endif + diff --git a/backportglobal.h b/backportglobal.h new file mode 100644 index 000000000..9d32711dc --- /dev/null +++ b/backportglobal.h @@ -0,0 +1,365 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved. +** +** This file is part of the plugins of the Qt Toolkit. +** +** This file may be used under the terms of the GNU General Public +** License version 2.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of +** this file. Please review the following information to ensure GNU +** General Public Licensing requirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/ +** +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** In addition, as a special exception, Trolltech gives you certain +** additional rights. These rights are described in the Trolltech GPL +** Exception version 1.0, which can be found at +** http://www.trolltech.com/products/qt/gplexception/ and in the file +** GPL_EXCEPTION.txt in this package. +** +** In addition, as a special exception, Trolltech, as the sole copyright +** holder for Qt Designer, grants users of the Qt/Eclipse Integration +** plug-in the right for the Qt/Eclipse Integration to link to +** functionality provided by Qt Designer and its related libraries. +** +** Trolltech reserves all rights not expressly granted herein. +** +** Trolltech ASA (c) 2007 +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +****************************************************************************/ +#ifndef QTSCRIPTEXTENSIONS_GLOBAL_H +#define QTSCRIPTEXTENSIONS_GLOBAL_H + +#include + +#include "../plasmagenericshell_export.h" + +#define DECLARE_SELF(Class, __fn__) \ + Class* self = qscriptvalue_cast(ctx->thisObject()); \ + if (!self) { \ + return ctx->throwError(QScriptContext::TypeError, \ + QString::fromLatin1("%0.prototype.%1: this object is not a %0") \ + .arg(#Class).arg(#__fn__)); \ + } + +#define DECLARE_SELF2(Class, __fn__, __ret__) \ + Class* self = qscriptvalue_cast(thisObject()); \ + if (!self) { \ + context()->throwError(QScriptContext::TypeError, \ + QString::fromLatin1("%0.prototype.%1: this object is not a %0") \ + .arg(#Class).arg(#__fn__)); \ + return __ret__; \ + } + + + +#define ADD_METHOD(__p__, __f__) \ + __p__.setProperty(#__f__, __p__.engine()->newFunction(__f__)) + +#define ADD_GET_METHOD(__p__, __get__) \ + ADD_METHOD(__p__, __get__) + +#define ADD_GET_SET_METHODS(__p__, __get__, __set__) \ +do { \ + ADD_METHOD(__p__, __get__); \ + ADD_METHOD(__p__, __set__); \ +} while (0); + +#define ADD_CTOR_FUNCTION(__c__, __f__) ADD_METHOD(__c__, __f__) + +#define ADD_ENUM_VALUE(__c__, __ns__, __v__) \ + __c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__)) + + +#define BEGIN_DECLARE_METHOD(Class, __mtd__) \ +static QScriptValue __mtd__(QScriptContext *ctx, QScriptEngine *eng) \ +{ \ + DECLARE_SELF(Class, __mtd__); + +#define END_DECLARE_METHOD \ +} + + +#define DECLARE_GET_METHOD(Class, __get__) \ +BEGIN_DECLARE_METHOD(Class, __get__) { \ + return qScriptValueFromValue(eng, self->__get__()); \ +} END_DECLARE_METHOD + +#define DECLARE_SET_METHOD(Class, T, __set__) \ +BEGIN_DECLARE_METHOD(Class, __set__) { \ + self->__set__(qscriptvalue_cast(ctx->argument(0))); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_GET_SET_METHODS(Class, T, __get__, __set__) \ +DECLARE_GET_METHOD(Class, /*T,*/ __get__) \ +DECLARE_SET_METHOD(Class, T, __set__) + + + +#define DECLARE_SIMPLE_GET_METHOD(Class, __get__) \ +BEGIN_DECLARE_METHOD(Class, __get__) { \ + return QScriptValue(eng, self->__get__()); \ +} END_DECLARE_METHOD + +#define DECLARE_SIMPLE_SET_METHOD(Class, ToType, __set__) \ +BEGIN_DECLARE_METHOD(Class, __set__) { \ + self->__set__(ctx->argument(0).ToType()); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_BOOLEAN_GET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_GET_METHOD(Class, __set__) +#define DECLARE_BOOLEAN_SET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_SET_METHOD(Class, toBoolean, __set__) + +#define DECLARE_INT_GET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_GET_METHOD(Class, __set__) +#define DECLARE_INT_SET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_SET_METHOD(Class, toInt32, __set__) + +#define DECLARE_NUMBER_GET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_GET_METHOD(Class, __set__) +#define DECLARE_NUMBER_SET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_SET_METHOD(Class, toNumber, __set__) + +#define DECLARE_STRING_GET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_GET_METHOD(Class, __set__) +#define DECLARE_STRING_SET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_SET_METHOD(Class, toString, __set__) + +#define DECLARE_QOBJECT_GET_METHOD(Class, __get__) \ +BEGIN_DECLARE_METHOD(Class, __get__) { \ + return eng->newQObject(self->__get__()); \ +} END_DECLARE_METHOD +#define DECLARE_QOBJECT_SET_METHOD(Class, __set__) \ + DECLARE_SIMPLE_SET_METHOD(Class, toQObject, __set__) + +#define DECLARE_BOOLEAN_GET_SET_METHODS(Class, __get__, __set__) \ + DECLARE_BOOLEAN_GET_METHOD(Class, __get__) \ + DECLARE_BOOLEAN_SET_METHOD(Class, __set__) + +#define DECLARE_NUMBER_GET_SET_METHODS(Class, __get__, __set__) \ + DECLARE_NUMBER_GET_METHOD(Class, __get__) \ + DECLARE_NUMBER_SET_METHOD(Class, __set__) + +#define DECLARE_INT_GET_SET_METHODS(Class, __get__, __set__) \ + DECLARE_INT_GET_METHOD(Class, __get__) \ + DECLARE_INT_SET_METHOD(Class, __set__) + +#define DECLARE_STRING_GET_SET_METHODS(Class, __get__, __set__) \ + DECLARE_STRING_GET_METHOD(Class, __get__) \ + DECLARE_STRING_SET_METHOD(Class, __set__) + +#define DECLARE_QOBJECT_GET_SET_METHODS(Class, __get__, __set__) \ + DECLARE_QOBJECT_GET_METHOD(Class, __get__) \ + DECLARE_QOBJECT_SET_METHOD(Class, __set__) + + +#define DECLARE_VOID_METHOD(Class, __fun__) \ +BEGIN_DECLARE_METHOD(Class, __fun__) { \ + self->__fun__(); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_VOID_NUMBER_METHOD(Class, __fun__) \ +BEGIN_DECLARE_METHOD(Class, __fun__) { \ + self->__fun__(ctx->argument(0).toNumber()); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_VOID_NUMBER_NUMBER_METHOD(Class, __fun__) \ +BEGIN_DECLARE_METHOD(Class, __fun__) { \ + self->__fun__(ctx->argument(0).toNumber(), ctx->argument(1).toNumber()); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_VOID_QUAD_NUMBER_METHOD(Class, __fun__) \ +BEGIN_DECLARE_METHOD(Class, __fun__) { \ + self->__fun__(ctx->argument(0).toNumber(), ctx->argument(1).toNumber(), ctx->argument(2).toNumber(), ctx->argument(3).toNumber()); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_VOID_1ARG_METHOD(Class, ArgType, __fun__) \ +BEGIN_DECLARE_METHOD(Class, __fun__) { \ + self->__fun__(qscriptvalue_cast(ctx->argument(0))); \ + return eng->undefinedValue(); \ +} END_DECLARE_METHOD + +#define DECLARE_BOOLEAN_1ARG_METHOD(Class, ArgType, __fun__) \ +BEGIN_DECLARE_METHOD(Class, __fun__) { \ + return QScriptValue(eng, self->__fun__(qscriptvalue_cast(ctx->argument(0)))); \ +} END_DECLARE_METHOD + + +#define DECLARE_POINTER_METATYPE(T) \ + Q_DECLARE_METATYPE(T*) \ + Q_DECLARE_METATYPE(QScript::Pointer::wrapped_pointer_type) + +namespace QScript +{ + +enum { + UserOwnership = 1 +}; + +template +class PLASMAGENERICSHELL_EXPORT Pointer : public QSharedData +{ +public: + typedef T* pointer_type; + typedef QExplicitlySharedDataPointer > wrapped_pointer_type; + + ~Pointer() + { + if (!(m_flags & UserOwnership)) + delete m_value; + } + + operator T*() + { + return m_value; + } + + operator const T*() const + { + return m_value; + } + + static wrapped_pointer_type create(T *value, uint flags = 0) + { + return wrapped_pointer_type(new Pointer(value, flags)); + } + + static QScriptValue toScriptValue(QScriptEngine *engine, T* const &source) + { + if (!source) + return engine->nullValue(); + return engine->newVariant(qVariantFromValue(source)); + } + + static void fromScriptValue(const QScriptValue &value, T* &target) + { + if (value.isVariant()) { + QVariant var = value.toVariant(); + if (qVariantCanConvert(var)) { + target = qvariant_cast(var); + } else if (qVariantCanConvert(var)) { + target = qvariant_cast(var)->operator T*(); + } else { + // look in prototype chain + target = 0; + int type = qMetaTypeId(); + int pointerType = qMetaTypeId(); + QScriptValue proto = value.prototype(); + while (proto.isObject() && proto.isVariant()) { + int protoType = proto.toVariant().userType(); + if ((type == protoType) || (pointerType == protoType)) { + QByteArray name = QMetaType::typeName(var.userType()); + if (name.startsWith("QScript::Pointer<")) { + target = (*reinterpret_cast(var.data()))->operator T*(); + break; + } else { + target = static_cast(var.data()); + break; + } + } + proto = proto.prototype(); + } + } + } else if (value.isQObject()) { + QObject *qobj = value.toQObject(); + QByteArray typeName = QMetaType::typeName(qMetaTypeId()); + target = reinterpret_cast(qobj->qt_metacast(typeName.left(typeName.size()-1))); + } else { + target = 0; + } + } + + uint flags() const + { return m_flags; } + void setFlags(uint flags) + { m_flags = flags; } + void unsetFlags(uint flags) + { m_flags &= ~flags; } + +protected: + Pointer(T* value, uint flags) + : m_flags(flags), m_value(value) + {} + +private: + uint m_flags; + T* m_value; +}; + +template +int registerPointerMetaType( + QScriptEngine *eng, + const QScriptValue &prototype = QScriptValue(), + T * /* dummy */ = 0 +) +{ + QScriptValue (*mf)(QScriptEngine *, T* const &) = Pointer::toScriptValue; + void (*df)(const QScriptValue &, T* &) = Pointer::fromScriptValue; + const int id = qMetaTypeId(); + qScriptRegisterMetaType_helper( + eng, id, reinterpret_cast(mf), + reinterpret_cast(df), + prototype); + eng->setDefaultPrototype(qMetaTypeId::wrapped_pointer_type>(), prototype); + return id; +} + +inline void maybeReleaseOwnership(const QScriptValue &value) +{ + if (value.isVariant()) { + QVariant var = value.toVariant(); + QByteArray name = QMetaType::typeName(var.userType()); + if (name.startsWith("QScript::Pointer<")) + (*reinterpret_cast::wrapped_pointer_type *>(var.data()))->setFlags(UserOwnership); + } +} + +inline void maybeTakeOwnership(const QScriptValue &value) +{ + if (value.isVariant()) { + QVariant var = value.toVariant(); + QByteArray name = QMetaType::typeName(var.userType()); + if (name.startsWith("QScript::Pointer<")) + (*reinterpret_cast::wrapped_pointer_type *>(var.data()))->unsetFlags(UserOwnership); + } +} + +template +inline QScriptValue wrapPointer(QScriptEngine *eng, T *ptr, uint flags = 0) +{ + return eng->newVariant(qVariantFromValue(Pointer::create(ptr, flags))); +} + +} // namespace QScript + +#ifdef QGRAPHICSITEM_H + +namespace QScript { + +template +inline QScriptValue wrapGVPointer(QScriptEngine *eng, T *item) +{ + uint flags = item->parentItem() ? UserOwnership : 0; + return wrapPointer(eng, item, flags); +} + +} // namespace QScript + +#endif // QGRAPHICSITEM_H + +#endif // QTSCRIPTEXTENSIONS_GLOBAL_H diff --git a/containment.cpp b/containment.cpp new file mode 100644 index 000000000..e53892f18 --- /dev/null +++ b/containment.cpp @@ -0,0 +1,228 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 "containment.h" + +#include + +#include +#include + +#include "scriptengine.h" +#include "widget.h" + +Containment::Containment(Plasma::Containment *containment, QObject *parent) + : Applet(parent), + m_containment(containment) +{ + setCurrentConfigGroup(QStringList()); +} + +Containment::~Containment() +{ +} + +int Containment::screen() const +{ + if (!m_containment) { + return -1; + } + + return m_containment.data()->screen(); +} + +void Containment::setScreen(int screen) +{ + if (m_containment) { + m_containment.data()->setScreen(screen); + } +} + +int Containment::desktop() const +{ + if (!m_containment) { + return -1; + } + + return m_containment.data()->desktop(); +} + +void Containment::setDesktop(int desktop) +{ + if (m_containment) { + m_containment.data()->setScreen(m_containment.data()->screen(), desktop); + } +} + +QString Containment::formFactor() const +{ + if (!m_containment) { + return "Planar"; + } + + switch (m_containment.data()->formFactor()) { + case Plasma::Planar: + return "planar"; + break; + case Plasma::MediaCenter: + return "mediacenter"; + break; + case Plasma::Horizontal: + return "horizontal"; + break; + case Plasma::Vertical: + return "vertical"; + break; + } + + return "Planar"; +} + +QList Containment::widgetIds() const +{ + //FIXME: the ints could overflow since Applet::id() returns a uint, + // however QScript deals with QList very, very poory + QList w; + + if (m_containment) { + foreach (const Plasma::Applet *applet, m_containment.data()->applets()) { + w.append(applet->id()); + } + } + + return w; +} + +QScriptValue Containment::widgetById(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() == 0) { + return context->throwError(i18n("widgetById requires an id")); + } + + const uint id = context->argument(0).toInt32(); + Containment *c = qobject_cast(context->thisObject().toQObject()); + + if (!c) { + return engine->undefinedValue(); + } + + if (c->m_containment) { + foreach (Plasma::Applet *w, c->m_containment.data()->applets()) { + if (w->id() == id) { + ScriptEngine *env = ScriptEngine::envFor(engine); + return env->wrap(w, engine); + } + } + } + + return engine->undefinedValue(); +} + +QScriptValue Containment::addWidget(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() == 0) { + return context->throwError(i18n("widgetById requires a name of a widget or a widget object")); + } + + Containment *c = qobject_cast(context->thisObject().toQObject()); + + if (!c || !c->m_containment) { + return engine->undefinedValue(); + } + + QScriptValue v = context->argument(0); + Plasma::Applet *applet = 0; + if (v.isString()) { + applet = c->m_containment.data()->addApplet(v.toString()); + if (applet) { + ScriptEngine *env = ScriptEngine::envFor(engine); + return env->wrap(applet, engine); + } + } else if (Widget *widget = qobject_cast(v.toQObject())) { + applet = widget->applet(); + c->m_containment.data()->addApplet(applet); + return v; + } + + return engine->undefinedValue(); +} + +uint Containment::id() const +{ + if (!m_containment) { + return 0; + } + + return m_containment.data()->id(); +} + +QString Containment::name() const +{ + if (!m_containment) { + return QString(); + } + + return m_containment.data()->activity(); +} + +void Containment::setName(const QString &name) +{ + if (m_containment) { + m_containment.data()->setActivity(name); + } +} + +QString Containment::type() const +{ + if (!m_containment) { + return QString(); + } + + return m_containment.data()->pluginName(); +} + +void Containment::remove() +{ + if (m_containment) { + m_containment.data()->destroy(false); + } +} + +void Containment::showConfigurationInterface() +{ + if (m_containment) { + QAction *configAction = m_containment.data()->action("configure"); + if (configAction && configAction->isEnabled()) { + configAction->trigger(); + } + } +} + +Plasma::Applet *Containment::applet() const +{ + return m_containment.data(); +} + +Plasma::Containment *Containment::containment() const +{ + return m_containment.data(); +} + +#include "containment.moc" + diff --git a/containment.h b/containment.h new file mode 100644 index 000000000..27d3153c1 --- /dev/null +++ b/containment.h @@ -0,0 +1,92 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 CONTAINMENT +#define CONTAINMENT + +#include +#include +#include + +#include "applet.h" + +#include "../plasmagenericshell_export.h" + +namespace Plasma +{ + class Containment; +} // namespace Plasma + +class Widget; +class PanelView; + +class PLASMAGENERICSHELL_EXPORT Containment : public Applet +{ + Q_OBJECT + Q_PROPERTY(QStringList configKeys READ configKeys) + Q_PROPERTY(QStringList configGroups READ configGroups) + Q_PROPERTY(QStringList currentConfigGroup WRITE setCurrentConfigGroup READ currentConfigGroup) + + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString type READ type) + Q_PROPERTY(QString formFactor READ formFactor) + Q_PROPERTY(QList widgetIds READ widgetIds) + Q_PROPERTY(int screen READ screen WRITE setScreen) + Q_PROPERTY(int desktop READ desktop WRITE setDesktop) + Q_PROPERTY(int id READ id) + +public: + Containment(Plasma::Containment *containment, QObject *parent = 0); + ~Containment(); + + uint id() const; + QString type() const; + QString formFactor() const; + QList widgetIds() const; + + QString name() const; + void setName(const QString &name); + + int desktop() const; + void setDesktop(int desktop); + + int screen() const; + void setScreen(int screen); + + Plasma::Applet *applet() const; + Plasma::Containment *containment() const; + + static QScriptValue widgetById(QScriptContext *context, QScriptEngine *engine); + static QScriptValue addWidget(QScriptContext *context, QScriptEngine *engine); + +public Q_SLOTS: + void remove(); + void showConfigurationInterface(); + + // from the applet interface + QVariant readConfig(const QString &key, const QVariant &def = QString()) const { return Applet::readConfig(key, def); } + void writeConfig(const QString &key, const QVariant &value) { Applet::writeConfig(key, value); } + void reloadConfig() { Applet::reloadConfig(); } + +private: + QWeakPointer m_containment; +}; + +#endif + diff --git a/rect.cpp b/rect.cpp new file mode 100644 index 000000000..855f198a4 --- /dev/null +++ b/rect.cpp @@ -0,0 +1,330 @@ +/* + * Copyright 2007 Richard J. Moore + * + * 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 +#include +#include +#include +#include "backportglobal.h" + +Q_DECLARE_METATYPE(QRectF*) +Q_DECLARE_METATYPE(QRectF) + +static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *eng) +{ + if (ctx->argumentCount() == 4) + { + qreal x = ctx->argument(0).toNumber(); + qreal y = ctx->argument(1).toNumber(); + qreal width = ctx->argument(2).toNumber(); + qreal height = ctx->argument(3).toNumber(); + return qScriptValueFromValue(eng, QRectF(x, y, width, height)); + } + + return qScriptValueFromValue(eng, QRectF()); +} + +static QScriptValue adjust(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, adjust); + qreal dx1 = ctx->argument(0).toNumber(); + qreal dy1 = ctx->argument(1).toNumber(); + qreal dx2 = ctx->argument(2).toNumber(); + qreal dy2 = ctx->argument(3).toNumber(); + + self->adjust(dx1, dy1, dx2, dy2); + return QScriptValue(); +} + +static QScriptValue adjusted(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, adjusted); + qreal dx1 = ctx->argument(0).toNumber(); + qreal dy1 = ctx->argument(1).toNumber(); + qreal dx2 = ctx->argument(2).toNumber(); + qreal dy2 = ctx->argument(3).toNumber(); + + QRectF tmp = self->adjusted(dx1, dy1, dx2, dy2); + return qScriptValueFromValue(eng, tmp); +} + +static QScriptValue bottom(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, bottom); + + if (ctx->argumentCount() > 0) { + int bottom = ctx->argument(0).toInt32(); + self->setBottom(bottom); + } + + return QScriptValue(eng, self->bottom()); +} + +static QScriptValue top(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, top); + + if (ctx->argumentCount() > 0) { + int top = ctx->argument(0).toInt32(); + self->setTop(top); + } + + return QScriptValue(eng, self->top()); +} + +static QScriptValue contains(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, contains); + qreal x = ctx->argument(0).toNumber(); + qreal y = ctx->argument(1).toNumber(); + return QScriptValue(eng, self->contains(x, y)); +} + +static QScriptValue height(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, height); + + if (ctx->argumentCount() > 0) { + int height = ctx->argument(0).toInt32(); + self->setHeight(height); + } + + return QScriptValue(eng, self->height()); +} + +static QScriptValue empty(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, empty); + return QScriptValue(eng, self->isEmpty()); +} + +static QScriptValue null(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, null); + return QScriptValue(eng, self->isNull()); +} + +static QScriptValue valid(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, valid); + return QScriptValue(eng, self->isValid()); +} + +static QScriptValue left(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, left); + + if (ctx->argumentCount() > 0) { + int left = ctx->argument(0).toInt32(); + self->setLeft(left); + } + + return QScriptValue(eng, self->left()); +} + +static QScriptValue moveBottom(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, moveBottom); + qreal bottom = ctx->argument(0).toNumber(); + self->moveBottom(bottom); + return QScriptValue(); +} + +static QScriptValue moveLeft(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, moveLeft); + qreal left = ctx->argument(0).toNumber(); + self->moveBottom(left); + return QScriptValue(); +} + +static QScriptValue moveRight(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, moveRight); + qreal right = ctx->argument(0).toNumber(); + self->moveBottom(right); + return QScriptValue(); +} + + +static QScriptValue moveTo(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, moveTo); + qreal x = ctx->argument(0).toNumber(); + qreal y = ctx->argument(1).toNumber(); + self->moveTo(x, y); + return QScriptValue(); +} + +static QScriptValue moveTop(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, moveTop); + qreal top = ctx->argument(0).toNumber(); + self->moveTop(top); + return QScriptValue(); +} + +static QScriptValue right(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, right); + + if (ctx->argumentCount() > 0) { + int right = ctx->argument(0).toInt32(); + self->setRight(right); + } + + return QScriptValue(eng, self->right()); +} + +static QScriptValue setCoords(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, setCoords); + qreal x1 = ctx->argument(0).toNumber(); + qreal y1 = ctx->argument(1).toNumber(); + qreal x2 = ctx->argument(2).toNumber(); + qreal y2 = ctx->argument(3).toNumber(); + self->setCoords(x1, y1, x2, y2); + return QScriptValue(); +} + +static QScriptValue setRect(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, setRect); + qreal x = ctx->argument(0).toNumber(); + qreal y = ctx->argument(1).toNumber(); + qreal width = ctx->argument(2).toNumber(); + qreal height = ctx->argument(3).toNumber(); + self->setRect(x, y, width, height); + return QScriptValue(); +} + +static QScriptValue translate(QScriptContext *ctx, QScriptEngine *) +{ + DECLARE_SELF(QRectF, translate); + qreal dx = ctx->argument(0).toNumber(); + qreal dy = ctx->argument(1).toNumber(); + self->translate(dx, dy); + return QScriptValue(); +} + +static QScriptValue width(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, width); + + if (ctx->argumentCount() > 0) { + int width = ctx->argument(0).toInt32(); + self->setWidth(width); + } + + return QScriptValue(eng, self->width()); +} + +static QScriptValue x(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, x); + + if (ctx->argumentCount() > 0) { + int x = ctx->argument(0).toInt32(); + self->setX(x); + } + + return QScriptValue(eng, self->x()); +} + +static QScriptValue y(QScriptContext *ctx, QScriptEngine *eng) +{ + DECLARE_SELF(QRectF, y); + + if (ctx->argumentCount() > 0) { + int y = ctx->argument(0).toInt32(); + self->setY(y); + } + + return QScriptValue(eng, self->y()); +} + +/* Not Implemented Yet */ +// QPointF bottomLeft () const +// QPointF bottomRight () const +// QPointF center () const +// bool contains ( const QPointF & point ) const +// bool contains ( const QRectF & rectangle ) const +// void getCoords ( qreal * x1, qreal * y1, qreal * x2, qreal * y2 ) const +// void getRect ( qreal * x, qreal * y, qreal * width, qreal * height ) const +// QRectF intersected ( const QRectF & rectangle ) const +// bool intersects ( const QRectF & rectangle ) const +// void moveBottomLeft ( const QPointF & position ) +// void moveBottomRight ( const QPointF & position ) +// void moveCenter ( const QPointF & position ) +// void moveTo ( const QPointF & position ) +// void moveTopLeft ( const QPointF & position ) +// void moveTopRight ( const QPointF & position ) +// QRectF normalized () const +// void setBottomLeft ( const QPointF & position ) +// void setBottomRight ( const QPointF & position ) +// void setSize ( const QSizeF & size ) +// void setTopLeft ( const QPointF & position ) +// void setTopRight ( const QPointF & position ) +// QSizeF size () const +// QRect toAlignedRect () const +// QRect toRect () const +// QPointF topLeft () const +// QPointF topRight () const +// void translate ( const QPointF & offset ) +// QRectF translated ( qreal dx, qreal dy ) const +// QRectF translated ( const QPointF & offset ) const +// QRectF united ( const QRectF & rectangle ) const + +QScriptValue constructQRectFClass(QScriptEngine *eng) +{ + QScriptValue proto = qScriptValueFromValue(eng, QRectF()); + QScriptValue::PropertyFlags getter = QScriptValue::PropertyGetter; + QScriptValue::PropertyFlags setter = QScriptValue::PropertySetter; + + proto.setProperty("adjust", eng->newFunction(adjust)); + proto.setProperty("adjusted", eng->newFunction(adjusted), getter); + proto.setProperty("translate", eng->newFunction(translate)); + proto.setProperty("setCoords", eng->newFunction(setCoords)); + proto.setProperty("setRect", eng->newFunction(setRect)); + + proto.setProperty("contains", eng->newFunction(contains)); + + proto.setProperty("moveBottom", eng->newFunction(moveBottom)); + proto.setProperty("moveLeft", eng->newFunction(moveLeft)); + proto.setProperty("moveRight", eng->newFunction(moveRight)); + proto.setProperty("moveTo", eng->newFunction(moveTo)); + proto.setProperty("moveTop", eng->newFunction(moveTop)); + + proto.setProperty("empty", eng->newFunction(empty), getter); + proto.setProperty("null", eng->newFunction(null), getter); + proto.setProperty("valid", eng->newFunction(valid), getter); + + proto.setProperty("left", eng->newFunction(left), getter | setter); + proto.setProperty("top", eng->newFunction(top), getter | setter); + proto.setProperty("bottom", eng->newFunction(bottom), getter | setter); + proto.setProperty("right", eng->newFunction(right), getter | setter); + proto.setProperty("height", eng->newFunction(height), getter | setter); + proto.setProperty("width", eng->newFunction(width), getter | setter); + proto.setProperty("x", eng->newFunction(x), getter | setter); + proto.setProperty("y", eng->newFunction(y), getter | setter); + + eng->setDefaultPrototype(qMetaTypeId(), proto); + eng->setDefaultPrototype(qMetaTypeId(), proto); + + return eng->newFunction(ctor, proto); +} diff --git a/scriptengine.cpp b/scriptengine.cpp new file mode 100644 index 000000000..f187c6495 --- /dev/null +++ b/scriptengine.cpp @@ -0,0 +1,253 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 "scriptengine.h" + +#include +#include + +#include + +#include +#include +#include + +#include "appinterface.h" +#include "containment.h" +#include "widget.h" + +QScriptValue constructQRectFClass(QScriptEngine *engine); + +ScriptEngine::ScriptEngine(Plasma::Corona *corona, QObject *parent) + : QScriptEngine(parent), + m_corona(corona) +{ + Q_ASSERT(m_corona); + AppInterface *interface = new AppInterface(corona, this); + connect(interface, SIGNAL(print(QString)), this, SIGNAL(print(QString))); + m_scriptSelf = newQObject(interface, QScriptEngine::QtOwnership, + QScriptEngine::ExcludeSuperClassProperties | QScriptEngine::ExcludeSuperClassMethods); + kDebug( )<< "*****************************"; + setupEngine(); + connect(this, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(exception(QScriptValue))); +} + +ScriptEngine::~ScriptEngine() +{ +} + +QScriptValue ScriptEngine::activityById(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() == 0) { + return context->throwError(i18n("activityById requires an id")); + } + + const uint id = context->argument(0).toInt32(); + ScriptEngine *env = envFor(engine); + foreach (Plasma::Containment *c, env->m_corona->containments()) { + if (c->id() == id && !isPanel(c)) { + return env->wrap(c, engine); + } + } + + return engine->undefinedValue(); +} + +QScriptValue ScriptEngine::activityForScreen(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() == 0) { + return context->throwError(i18n("activityForScreen requires a screen id")); + } + + const uint screen = context->argument(0).toInt32(); + const uint desktop = context->argumentCount() > 1 ? context->argument(1).toInt32() : -1; + ScriptEngine *env = envFor(engine); + return env->wrap(env->m_corona->containmentForScreen(screen, desktop), engine); +} + +QScriptValue ScriptEngine::newActivity(QScriptContext *context, QScriptEngine *engine) +{ + return createContainment("desktop", "desktop", context, engine); +} + +QScriptValue ScriptEngine::newPanel(QScriptContext *context, QScriptEngine *engine) +{ + return createContainment("panel", "panel", context, engine); +} + +QScriptValue ScriptEngine::createContainment(const QString &type, const QString &defaultPlugin, + QScriptContext *context, QScriptEngine *engine) +{ + QString plugin = context->argumentCount() > 0 ? context->argument(0).toString() : + defaultPlugin; + + bool exists = false; + const KPluginInfo::List list = Plasma::Containment::listContainmentsOfType(type); + foreach (const KPluginInfo &info, list) { + if (info.pluginName() == plugin) { + exists = true; + break; + } + } + + if (!exists) { + return context->throwError(i18n("Could not find a plugin for %1 named %2.", type, plugin)); + } + + + ScriptEngine *env = envFor(engine); + Plasma::Containment *c = env->m_corona->addContainment(plugin); + if (c) { + if (type == "panel") { + // some defaults + c->setScreen(0); + c->setLocation(Plasma::TopEdge); + } + c->updateConstraints(Plasma::AllConstraints | Plasma::StartupCompletedConstraint); + c->flushPendingConstraintsEvents(); + emit env->createPendingPanelViews(); + } + + return env->wrap(c, engine); +} + +QScriptValue ScriptEngine::wrap(Plasma::Applet *w, QScriptEngine *engine) +{ + Widget *wrapper = new Widget(w); + QScriptValue v = engine->newQObject(wrapper, QScriptEngine::ScriptOwnership, + QScriptEngine::ExcludeSuperClassProperties | + QScriptEngine::ExcludeSuperClassMethods); + return v; +} + +QScriptValue ScriptEngine::wrap(Plasma::Containment *c, QScriptEngine *engine) +{ + Containment *wrapper = new Containment(c); + return wrap(wrapper, engine); +} + +QScriptValue ScriptEngine::wrap(Containment *c, QScriptEngine *engine) +{ + QScriptValue v = engine->newQObject(c, QScriptEngine::ScriptOwnership, + QScriptEngine::ExcludeSuperClassProperties | + QScriptEngine::ExcludeSuperClassMethods); + v.setProperty("widgetById", engine->newFunction(Containment::widgetById)); + v.setProperty("addWidget", engine->newFunction(Containment::addWidget)); + + return v; +} + +ScriptEngine *ScriptEngine::envFor(QScriptEngine *engine) +{ + QObject *object = engine->globalObject().toQObject(); + Q_ASSERT(object); + + AppInterface *interface = qobject_cast(object); + Q_ASSERT(interface); + + ScriptEngine *env = qobject_cast(interface->parent()); + Q_ASSERT(env); + + return env; +} + +QScriptValue ScriptEngine::panelById(QScriptContext *context, QScriptEngine *engine) +{ + if (context->argumentCount() == 0) { + return context->throwError(i18n("activityById requires an id")); + } + + const uint id = context->argument(0).toInt32(); + ScriptEngine *env = envFor(engine); + foreach (Plasma::Containment *c, env->m_corona->containments()) { + if (c->id() == id && isPanel(c)) { + return env->wrap(c, engine); + } + } + + return engine->undefinedValue(); +} + +QScriptValue ScriptEngine::fileExists(QScriptContext *context, QScriptEngine *engine) +{ + Q_UNUSED(engine) + if (context->argumentCount() == 0) { + return false; + } + + const QString path = context->argument(0).toString(); + if (path.isEmpty()) { + return false; + } + + QFile f(KShell::tildeExpand(path)); + return f.exists(); +} + +void ScriptEngine::setupEngine() +{ + QScriptValue v = globalObject(); + QScriptValueIterator it(v); + while (it.hasNext()) { + it.next(); + // we provide our own print implementation, but we want the rest + if (it.name() != "print") { + m_scriptSelf.setProperty(it.name(), it.value()); + } + } + + m_scriptSelf.setProperty("QRectF", constructQRectFClass(this)); + m_scriptSelf.setProperty("Activity", newFunction(ScriptEngine::newActivity)); + m_scriptSelf.setProperty("Panel", newFunction(ScriptEngine::newPanel)); + m_scriptSelf.setProperty("activityById", newFunction(ScriptEngine::activityById)); + m_scriptSelf.setProperty("activityForScreen", newFunction(ScriptEngine::activityForScreen)); + m_scriptSelf.setProperty("panelById", newFunction(ScriptEngine::panelById)); + m_scriptSelf.setProperty("fileExists", newFunction(ScriptEngine::fileExists)); + + setGlobalObject(m_scriptSelf); +} + +bool ScriptEngine::isPanel(const Plasma::Containment *c) +{ + return c->containmentType() == Plasma::Containment::PanelContainment || + c->containmentType() == Plasma::Containment::CustomPanelContainment; +} + +void ScriptEngine::evaluateScript(const QString &script) +{ + //kDebug() << "evaluating" << m_editor->toPlainText(); + evaluate(script); + if (hasUncaughtException()) { + //kDebug() << "catch the exception!"; + QString error = i18n("Error: %1 at line %2\n\nBacktrace:\n%3", + uncaughtException().toString(), + QString::number(uncaughtExceptionLineNumber()), + uncaughtExceptionBacktrace().join("\n ")); + emit printError(error); + } +} + +void ScriptEngine::exception(const QScriptValue &value) +{ + //kDebug() << "exception caught!" << value.toVariant(); + emit printError(value.toVariant().toString()); +} + +#include "scriptengine.moc" + diff --git a/scriptengine.h b/scriptengine.h new file mode 100644 index 000000000..5a714e6db --- /dev/null +++ b/scriptengine.h @@ -0,0 +1,82 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 SCRIPTENGINE +#define SCRIPTENGINE + +#include +#include + +#include "../plasmagenericshell_export.h" + +namespace Plasma +{ + class Applet; + class Containment; + class Corona; +} // namespace Plasma + +class Containment; + +class PLASMAGENERICSHELL_EXPORT ScriptEngine : public QScriptEngine +{ + Q_OBJECT + +public: + ScriptEngine(Plasma::Corona *corona, QObject *parent = 0); + ~ScriptEngine(); + + void evaluateScript(const QString &script); + static bool isPanel(const Plasma::Containment *c); + QScriptValue wrap(Plasma::Applet *w, QScriptEngine *engine); + virtual QScriptValue wrap(Plasma::Containment *c, QScriptEngine *engine); + QScriptValue wrap(Containment *c, QScriptEngine *engine); + + static ScriptEngine *envFor(QScriptEngine *engine); + +Q_SIGNALS: + void print(const QString &string); + void printError(const QString &string); + void createPendingPanelViews(); + +private: + void setupEngine(); + + // containment accessors + static QScriptValue newActivity(QScriptContext *context, QScriptEngine *engine); + static QScriptValue newPanel(QScriptContext *context, QScriptEngine *engine); + static QScriptValue activityById(QScriptContext *context, QScriptEngine *engine); + static QScriptValue activityForScreen(QScriptContext *context, QScriptEngine *engine); + static QScriptValue panelById(QScriptContext *context, QScriptEngine *engine); + static QScriptValue fileExists(QScriptContext *context, QScriptEngine *engine); + + // helpers + static QScriptValue createContainment(const QString &type, const QString &defautPlugin, + QScriptContext *context, QScriptEngine *engine); + +private Q_SLOTS: + void exception(const QScriptValue &value); + +private: + Plasma::Corona *m_corona; + QScriptValue m_scriptSelf; +}; + +#endif + diff --git a/widget.cpp b/widget.cpp new file mode 100644 index 000000000..5ec793224 --- /dev/null +++ b/widget.cpp @@ -0,0 +1,159 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 "widget.h" + +#include +#include + +#include +#include +#include + +Widget::Widget(Plasma::Applet *applet, QObject *parent) + : Applet(parent), + m_applet(applet) +{ + setCurrentConfigGroup(QStringList()); +} + +Widget::~Widget() +{ +} + +uint Widget::id() const +{ + if (m_applet) { + return m_applet.data()->id(); + } + + return 0; +} + +QString Widget::type() const +{ + if (m_applet) { + return m_applet.data()->pluginName(); + } + + return QString(); +} + +void Widget::remove() +{ + if (m_applet) { + m_applet.data()->destroy(); + m_applet.clear(); + } +} + +void Widget::setGlobalShortcut(const QString &shortcut) +{ + if (m_applet) { + m_applet.data()->setGlobalShortcut(KShortcut(shortcut)); + } +} + +QString Widget::globalShorcut() const +{ + if (m_applet) { + return m_applet.data()->globalShortcut().toString(); + } + + return QString(); +} + +Plasma::Applet *Widget::applet() const +{ + return m_applet.data(); +} + +int Widget::index() const +{ + if (!m_applet) { + return -1; + } + + Plasma::Applet *applet = m_applet.data(); + Plasma::Containment *c = applet->containment(); + if (!c) { + return -1; + } + + QGraphicsLayout *layout = c->layout(); + if (!layout) { + return - 1; + } + + for (int i = 0; i < layout->count(); ++i) { + if (layout->itemAt(i) == applet) { + return i; + } + } + + return -1; +} + +void Widget::setIndex(int index) +{ + if (!m_applet) { + return; + } + + Plasma::Applet *applet = m_applet.data(); + Plasma::Containment *c = applet->containment(); + if (!c) { + return; + } + + //FIXME: this is hackish. would be nice to define this for gridlayouts too + QGraphicsLinearLayout *layout = dynamic_cast(c->layout()); + if (!layout) { + return; + } + + layout->insertItem(index, applet); +} + +QRectF Widget::geometry() const +{ + if (m_applet) { + return m_applet.data()->geometry(); + } + + return QRectF(); +} + +void Widget::setGeometry(const QRectF &geometry) +{ + if (m_applet) { + m_applet.data()->setGeometry(geometry); + } +} + +void Widget::showConfigurationInterface() +{ + if (m_applet) { + m_applet.data()->showConfigurationInterface(); + } +} + + +#include "widget.moc" + diff --git a/widget.h b/widget.h new file mode 100644 index 000000000..d48a6396e --- /dev/null +++ b/widget.h @@ -0,0 +1,79 @@ +/* + * Copyright 2009 Aaron Seigo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * 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 WIDGET +#define WIDGET + +#include + +#include "applet.h" + +#include "../plasmagenericshell_export.h" + +namespace Plasma +{ + class Applet; +} // namespace Plasma + +class PLASMAGENERICSHELL_EXPORT Widget : public Applet +{ + Q_OBJECT + Q_PROPERTY(QString type READ type) + Q_PROPERTY(int id READ id) + Q_PROPERTY(QStringList configKeys READ configKeys) + Q_PROPERTY(QStringList configGroups READ configGroups) + Q_PROPERTY(int index WRITE setIndex READ index) + Q_PROPERTY(QRectF geometry WRITE setGeometry READ geometry) + Q_PROPERTY(QStringList currentConfigGroup WRITE setCurrentConfigGroup READ currentConfigGroup) + Q_PROPERTY(QString globalShortcut WRITE setGlobalShortcut READ globalShorcut) + + +public: + Widget(Plasma::Applet *applet, QObject *parent = 0); + ~Widget(); + + uint id() const; + QString type() const; + + int index() const; + void setIndex(int index); + + QRectF geometry() const; + void setGeometry(const QRectF &geometry); + + void setGlobalShortcut(const QString &shortcut); + QString globalShorcut() const; + + Plasma::Applet *applet() const; + +public Q_SLOTS: + void remove(); + void showConfigurationInterface(); + + // from the applet interface + QVariant readConfig(const QString &key, const QVariant &def = QString()) const { return Applet::readConfig(key, def); } + void writeConfig(const QString &key, const QVariant &value) { Applet::writeConfig(key, value); } + void reloadConfig() { Applet::reloadConfig(); } + +private: + QWeakPointer m_applet; +}; + +#endif +