diff --git a/shell/scripting/applet.cpp b/shell/scripting/applet.cpp index 878f99194..fb97f43fe 100644 --- a/shell/scripting/applet.cpp +++ b/shell/scripting/applet.cpp @@ -1,274 +1,282 @@ /* * 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 "scriptengine.h" #include #include #include #include #include #include #include namespace WorkspaceScripting { class Applet::Private { public: Private() : configDirty(false), inWallpaperConfig(false) { } + QPointer engine = nullptr; KConfigGroup configGroup; QStringList configGroupPath; KConfigGroup globalConfigGroup; QStringList globalConfigGroupPath; bool configDirty : 1; bool inWallpaperConfig : 1; }; -Applet::Applet(QObject *parent) +Applet::Applet(ScriptEngine *parent) : QObject(parent), d(new Applet::Private) { + d->engine = parent; } Applet::~Applet() { delete d; } void Applet::setCurrentConfigGroup(const QStringList &groupNames) { Plasma::Applet *app = applet(); if (!app) { d->configGroup = KConfigGroup(); d->configGroupPath.clear(); return; } d->configGroup = app->config(); d->configGroupPath = groupNames; foreach (const QString &groupName, groupNames) { d->configGroup = KConfigGroup(&d->configGroup, groupName); } d->inWallpaperConfig = !groupNames.isEmpty() && groupNames.first() == QLatin1String("Wallpaper"); } QStringList Applet::currentConfigGroup() const { return d->configGroupPath; } QStringList Applet::configKeys() const { if (d->configGroup.isValid()) { return d->configGroup.keyList(); } return QStringList(); } QStringList Applet::configGroups() const { if (d->configGroup.isValid()) { return d->configGroup.groupList(); } return QStringList(); } QVariant Applet::readConfig(const QString &key, const QJSValue &def) const { if (d->configGroup.isValid()) { return d->configGroup.readEntry(key, def.toVariant()); } else { return QVariant(); } } void Applet::writeConfig(const QString &key, const QJSValue &value) { if (d->configGroup.isValid()) { if (d->inWallpaperConfig) { //hacky, but only way to make the wallpaper react immediately QObject *wallpaperGraphicsObject = applet()->property("wallpaperGraphicsObject").value(); if (wallpaperGraphicsObject) { KDeclarative::ConfigPropertyMap *config = static_cast(wallpaperGraphicsObject->property("configuration").value()); config->setProperty(key.toLatin1(), value.toVariant()); } } else if (applet()->configScheme()) { //check if it can be written in the applets' configScheme KConfigSkeletonItem *item = applet()->configScheme()->findItemByName(key); if (item) { item->setProperty(value.toVariant()); applet()->configScheme()->blockSignals(true); applet()->configScheme()->save(); //why read? read will update KConfigSkeletonItem::mLoadedValue, //allowing a write operation to be performed next time applet()->configScheme()->read(); applet()->configScheme()->blockSignals(false); emit applet()->configScheme()->configChanged(); } } d->configGroup.writeEntry(key, value.toVariant()); d->configDirty = true; } } void Applet::setCurrentGlobalConfigGroup(const QStringList &groupNames) { Plasma::Applet *app = applet(); if (!app) { d->globalConfigGroup = KConfigGroup(); d->globalConfigGroupPath.clear(); return; } d->globalConfigGroup = app->globalConfig(); d->globalConfigGroupPath = groupNames; foreach (const QString &groupName, groupNames) { d->globalConfigGroup = KConfigGroup(&d->globalConfigGroup, groupName); } } QStringList Applet::currentGlobalConfigGroup() const { return d->globalConfigGroupPath; } QStringList Applet::globalConfigKeys() const { if (d->globalConfigGroup.isValid()) { return d->globalConfigGroup.keyList(); } return QStringList(); } QStringList Applet::globalConfigGroups() const { if (d->globalConfigGroup.isValid()) { return d->globalConfigGroup.groupList(); } return QStringList(); } QVariant Applet::readGlobalConfig(const QString &key, const QJSValue &def) const { if (d->globalConfigGroup.isValid()) { return d->globalConfigGroup.readEntry(key, def.toVariant()); } else { return QVariant(); } } void Applet::writeGlobalConfig(const QString &key, const QJSValue &value) { if (d->globalConfigGroup.isValid()) { d->globalConfigGroup.writeEntry(key, value.toVariant()); d->configDirty = true; } } void Applet::reloadConfigIfNeeded() { if (d->configDirty) { reloadConfig(); } } 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(); } d->configDirty = false; } } QString Applet::version() const { Plasma::Applet *app = applet(); if (!app) { return QString(); } return app->pluginInfo().version(); } void Applet::setLocked(bool locked) { Plasma::Applet *app = applet(); if (!app) { return; } app->setImmutability(locked ? Plasma::Types::UserImmutable : Plasma::Types::Mutable); KConfigGroup cg = app->config(); if (!app->isContainment()) { cg = cg.parent(); } if (cg.isValid()) { cg.writeEntry("immutability", (int)app->immutability()); } } bool Applet::locked() const { Plasma::Applet *app = applet(); if (!app) { return Plasma::Types::Mutable; } return app->immutability() != Plasma::Types::Mutable; } Plasma::Applet *Applet::applet() const { return nullptr; } +ScriptEngine *Applet::engine() const +{ + return d->engine; +} + } diff --git a/shell/scripting/applet.h b/shell/scripting/applet.h index 6064e0624..e720ebbc3 100644 --- a/shell/scripting/applet.h +++ b/shell/scripting/applet.h @@ -1,84 +1,88 @@ /* * 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 namespace Plasma { class Applet; } // namespace Plasma namespace WorkspaceScripting { +class ScriptEngine; + class Applet : public QObject { Q_OBJECT Q_PROPERTY(QStringList currentConfigGroup WRITE setCurrentConfigGroup READ currentConfigGroup) public: - explicit Applet(QObject *parent = nullptr); + explicit Applet(ScriptEngine *parent); ~Applet() override; QStringList configKeys() const; QStringList configGroups() const; void setCurrentConfigGroup(const QStringList &groupNames); QStringList currentConfigGroup() const; QStringList globalConfigKeys() const; QStringList globalConfigGroups() const; void setCurrentGlobalConfigGroup(const QStringList &groupNames); QStringList currentGlobalConfigGroup() const; QString version() const; void setLocked(bool locked); bool locked() const; virtual Plasma::Applet *applet() const; + ScriptEngine *engine() const; + protected: void reloadConfigIfNeeded(); public Q_SLOTS: virtual QVariant readConfig(const QString &key, const QJSValue &def = QString()) const; virtual void writeConfig(const QString &key, const QJSValue &value); virtual QVariant readGlobalConfig(const QString &key, const QJSValue &def = QString()) const; virtual void writeGlobalConfig(const QString &key, const QJSValue &value); virtual void reloadConfig(); private: class Private; Private * const d; }; } #endif diff --git a/shell/scripting/containment.cpp b/shell/scripting/containment.cpp index 1669f4e83..9a34512a5 100644 --- a/shell/scripting/containment.cpp +++ b/shell/scripting/containment.cpp @@ -1,292 +1,290 @@ /* * 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 #include #include #include #include #include "shellcorona.h" #include "scriptengine.h" #include "widget.h" namespace WorkspaceScripting { class Containment::Private { public: - QPointer engine; QPointer containment; ShellCorona *corona; QString oldWallpaperPlugin; QString wallpaperPlugin; QString oldWallpaperMode; QString wallpaperMode; QString type; QString plugin; }; Containment::Containment(Plasma::Containment *containment, ScriptEngine *engine) : Applet(engine), d(new Containment::Private) { - d->engine = engine; d->containment = containment; d->corona = qobject_cast(containment->corona()); setCurrentConfigGroup(QStringList()); setCurrentGlobalConfigGroup(QStringList()); if (containment) { d->oldWallpaperPlugin = d->wallpaperPlugin = containment->wallpaper(); } } Containment::~Containment() { if (d->containment) { Plasma::Containment *containment = d->containment.data(); if (d->oldWallpaperPlugin != d->wallpaperPlugin || d->oldWallpaperMode != d->wallpaperMode) { containment->setWallpaper(d->wallpaperPlugin); } } reloadConfigIfNeeded(); delete d; } ShellCorona *Containment::corona() const { return d->corona; } int Containment::screen() const { if (!d->containment) { return -1; } return d->containment.data()->screen(); } QString Containment::wallpaperPlugin() const { return d->wallpaperPlugin; } void Containment::setWallpaperPlugin(const QString &wallpaperPlugin) { d->wallpaperPlugin = wallpaperPlugin; } QString Containment::wallpaperMode() const { return d->wallpaperMode; } void Containment::setWallpaperMode(const QString &wallpaperMode) { d->wallpaperMode = wallpaperMode; } QString Containment::formFactor() const { if (!d->containment) { return QStringLiteral("Planar"); } switch (d->containment.data()->formFactor()) { case Plasma::Types::Planar: return QStringLiteral("planar"); case Plasma::Types::MediaCenter: return QStringLiteral("mediacenter"); case Plasma::Types::Horizontal: return QStringLiteral("horizontal"); case Plasma::Types::Vertical: return QStringLiteral("vertical"); case Plasma::Types::Application: return QStringLiteral("application"); } return QStringLiteral("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 (d->containment) { foreach (const Plasma::Applet *applet, d->containment.data()->applets()) { w.append(applet->id()); } } return w; } QJSValue Containment::widgetById(const QJSValue ¶mId) const { if (!paramId.isNumber()) { - return d->engine->newError(i18n("widgetById requires an id")); + return engine()->newError(i18n("widgetById requires an id")); } const uint id = paramId.toInt(); if (d->containment) { foreach (Plasma::Applet *w, d->containment.data()->applets()) { if (w->id() == id) { - return d->engine->wrap(w); + return engine()->wrap(w); } } } return QJSValue(); } QJSValue Containment::addWidget(const QJSValue &v, qreal x, qreal y, qreal w, qreal h, const QVariantList &args) { if (!v.isString() && !v.isQObject()) { - return d->engine->newError(i18n("addWidget requires a name of a widget or a widget object")); + return engine()->newError(i18n("addWidget requires a name of a widget or a widget object")); } if (!d->containment) { return QJSValue(); } QRectF geometry(x, y, w, h); Plasma::Applet *applet = nullptr; if (v.isString()) { //A position has been supplied: search for the containment's graphics object QQuickItem *containmentItem = nullptr; if (geometry.x() >= 0 && geometry.y() >= 0) { containmentItem = d->containment.data()->property("_plasma_graphicObject").value(); if (containmentItem) { QMetaObject::invokeMethod(containmentItem , "createApplet", Qt::DirectConnection, Q_RETURN_ARG(Plasma::Applet *, applet), Q_ARG(QString, v.toString()), Q_ARG(QVariantList, args), Q_ARG(QRectF, geometry)); } if (applet) { - return d->engine->wrap(applet); + return engine()->wrap(applet); } - return d->engine->newError(i18n("Could not create the %1 widget!", v.toString())); + return engine()->newError(i18n("Could not create the %1 widget!", v.toString())); } //Case in which either: // * a geometry wasn't provided // * containmentItem wasn't found applet = d->containment.data()->createApplet(v.toString(), args); if (applet) { - return d->engine->wrap(applet); + return engine()->wrap(applet); } - return d->engine->newError(i18n("Could not create the %1 widget!", v.toString())); + return engine()->newError(i18n("Could not create the %1 widget!", v.toString())); } else if (Widget *widget = qobject_cast(v.toQObject())) { applet = widget->applet(); d->containment.data()->addApplet(applet); return v; } return QJSValue(); } QJSValue Containment::widgets(const QString &widgetType) const { if (!d->containment) { return QJSValue(); } - QJSValue widgets = d->engine->newArray(); + QJSValue widgets = engine()->newArray(); int count = 0; foreach (Plasma::Applet *widget, d->containment.data()->applets()) { if (widgetType.isEmpty() || widget->pluginMetaData().pluginId() == widgetType) { - widgets.setProperty(count, d->engine->wrap(widget)); + widgets.setProperty(count, engine()->wrap(widget)); ++count; } } widgets.setProperty(QStringLiteral("length"), count); return widgets; } uint Containment::id() const { if (!d->containment) { return 0; } return d->containment.data()->id(); } QString Containment::type() const { if (!d->containment) { return QString(); } return d->containment.data()->pluginMetaData().pluginId(); } void Containment::remove() { if (d->containment) { d->containment.data()->destroy(); } } void Containment::showConfigurationInterface() { if (d->containment) { QAction *configAction = d->containment.data()->actions()->action(QStringLiteral("configure")); if (configAction && configAction->isEnabled()) { configAction->trigger(); } } } Plasma::Applet *Containment::applet() const { return d->containment.data(); } Plasma::Containment *Containment::containment() const { return d->containment.data(); } } diff --git a/shell/scripting/scriptengine.cpp b/shell/scripting/scriptengine.cpp index e4c7d9c47..41982c845 100644 --- a/shell/scripting/scriptengine.cpp +++ b/shell/scripting/scriptengine.cpp @@ -1,447 +1,447 @@ /* * 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 "scriptengine_v1.h" #include #include #include #include #include #include #include #include #include #include #include #include #include // KIO //#include // no camelcase include #include #include #include #include #include #include #include "appinterface.h" #include "containment.h" #include "configgroup.h" #include "panel.h" #include "widget.h" #include "../shellcorona.h" #include "../standaloneappcorona.h" #include "../screenpool.h" namespace WorkspaceScripting { ScriptEngine::ScriptEngine(Plasma::Corona *corona, QObject *parent) : QJSEngine(parent), m_corona(corona) { Q_ASSERT(m_corona); m_appInterface = new AppInterface(this); connect(m_appInterface, &AppInterface::print, this, &ScriptEngine::print); m_scriptSelf = globalObject(); m_globalScriptEngineObject = new ScriptEngine::V1(this); m_localizedContext = new KLocalizedContext(this); setupEngine(); } ScriptEngine::~ScriptEngine() { } QString ScriptEngine::errorString() const { return m_errorString; } QJSValue ScriptEngine::wrap(Plasma::Applet *w) { - Widget *wrapper = new Widget(w); + Widget *wrapper = new Widget(w, this); return newQObject(wrapper); } QJSValue ScriptEngine::wrap(Plasma::Containment *c) { Containment *wrapper = isPanel(c) ? new Panel(c, this) : new Containment(c, this); return newQObject(wrapper); } int ScriptEngine::defaultPanelScreen() const { return 1; } QJSValue ScriptEngine::newError(const QString &message) { return evaluate(QString("new Error('%1');").arg(message)); } QString ScriptEngine::onlyExec(const QString &commandLine) { if (commandLine.isEmpty()) { return commandLine; } return KShell::splitArgs(commandLine, KShell::TildeExpand).first(); } void ScriptEngine::setupEngine() { QJSValue globalScriptEngineObject = newQObject(m_globalScriptEngineObject); QJSValue localizedContext = newQObject(m_localizedContext); QJSValue appInterface = newQObject(m_appInterface); //AppInterface stuff //FIXME: this line doesn't have much effect for now, if QTBUG-68397 gets fixed, //all the connects to rewrite the properties won't be necessary anymore //globalObject().setPrototype(appInterface); //FIXME: remove __AppInterface if QTBUG-68397 gets solved //as workaround we build manually a js object with getters and setters m_scriptSelf.setProperty(QStringLiteral("__AppInterface"), appInterface); QJSValue res = evaluate("__proto__ = {\ get locked() {return __AppInterface.locked;},\ get hasBattery() {return __AppInterface.hasBattery;},\ get screenCount() {return __AppInterface.screenCount;},\ get activityIds() {return __AppInterface.activityIds;},\ get panelIds() {return __AppInterface.panelIds;},\ get knownPanelTypes() {return __AppInterface.knownPanelTypes;},\ get knownActivityTypes() {return __AppInterface.knownActivityTypes;},\ get knownWidgetTypes() {return __AppInterface.knownWidgetTypes;},\ get theme() {return __AppInterface.theme;},\ set theme(name) {__AppInterface.theme = name;},\ get applicationVersion() {return __AppInterface.applicationVersion;},\ get platformVersion() {return __AppInterface.platformVersion;},\ get scriptingVersion() {return __AppInterface.scriptingVersion;},\ get multihead() {return __AppInterface.multihead;},\ get multiheadScreen() {return __AppInterface.multihead;},\ get locale() {return __AppInterface.locale;},\ get language() {return __AppInterface.language;},\ get languageId() {return __AppInterface.languageId;},\ }"); Q_ASSERT(!res.isError()); //methods from AppInterface m_scriptSelf.setProperty(QStringLiteral("screenGeometry"), appInterface.property("screenGeometry")); m_scriptSelf.setProperty(QStringLiteral("lockCorona"), appInterface.property("lockCorona")); m_scriptSelf.setProperty(QStringLiteral("sleep"), appInterface.property("sleep")); m_scriptSelf.setProperty(QStringLiteral("print"), appInterface.property("print")); m_scriptSelf.setProperty(QStringLiteral("getApiVersion"), globalScriptEngineObject.property("getApiVersion")); //Constructors: prefer them js based as they make the c++ code of panel et al way simpler without hacks to get the engine m_scriptSelf.setProperty(QStringLiteral("__newPanel"), globalScriptEngineObject.property("newPanel")); m_scriptSelf.setProperty(QStringLiteral("__newConfigFile"), globalScriptEngineObject.property("configFile")); //definitions of qrectf properties from documentation //only properties/functions which were already binded are. //TODO KF6: just a plain QRectF binding res = evaluate("function QRectF(x,y,w,h) {\ return {x: x, y: y, width: w, height: h,\ get left() {return this.x},\ get top() {return this.y},\ get right() {return this.x + this.width},\ get bottom() {return this.y + this.height},\ get empty() {return this.width <= 0 || this.height <= 0},\ get null() {return this.width == 0 || this.height == 0},\ get valid() {return !this.empty},\ adjust: function(dx1, dy1, dx2, dy2) {\ this.x += dx1; this.y += dy1;\ this.width = this.width - dx1 + dx2;\ this.height = this.height - dy1 + dy2;},\ adjusted: function(dx1, dy1, dx2, dy2) {\ return new QRectF(this.x + dx1, this.y + dy1,\ this.width - dx1 + dx2,\ this.height - dy1 + dy2)},\ translate: function(dx, dy) {this.x += dx; this.y += dy;},\ setCoords: function(x1, y1, x2, y2) {\ this.x = x1; this.y = y1;\ this.width = x2 - x1;\ this.height = y2 - y1;},\ setRect: function(x1, y1, w1, h1) {\ this.x = x1; this.y = y1;\ this.width = w1; this.height = h1;},\ contains: function(x1, y1) { return x1 >= this.x && x1 <= this.x + this.width && y1 >= this.y && y1 <= this.y + this.height},\ moveBottom: function(bottom1) {this.y = bottom1 - this.height;},\ moveLeft: function(left1) {this.x = left1;},\ moveRight: function(right1) {this.x = right1 - this.width;},\ moveTop: function(top1) {this.y = top1;},\ moveTo: function(x1, y1) {this.x = x1; this.y = y1;}\ }};\ function ConfigFile(config, group){return __newConfigFile(config, group)};\ function Panel(plugin){return __newPanel(plugin)};"); Q_ASSERT(!res.isError()); m_scriptSelf.setProperty(QStringLiteral("createActivity"), globalScriptEngineObject.property("createActivity")); m_scriptSelf.setProperty(QStringLiteral("setCurrentActivity"), globalScriptEngineObject.property("setCurrentActivity")); m_scriptSelf.setProperty(QStringLiteral("currentActivity"), globalScriptEngineObject.property("currentActivity")); m_scriptSelf.setProperty(QStringLiteral("activities"), globalScriptEngineObject.property("activities")); m_scriptSelf.setProperty(QStringLiteral("activityName"), globalScriptEngineObject.property("activityName")); m_scriptSelf.setProperty(QStringLiteral("setActivityName"), globalScriptEngineObject.property("setActivityName")); m_scriptSelf.setProperty(QStringLiteral("loadSerializedLayout"), globalScriptEngineObject.property("loadSerializedLayout")); m_scriptSelf.setProperty(QStringLiteral("desktopsForActivity"), globalScriptEngineObject.property("desktopsForActivity")); m_scriptSelf.setProperty(QStringLiteral("desktops"), globalScriptEngineObject.property("desktops")); m_scriptSelf.setProperty(QStringLiteral("desktopById"), globalScriptEngineObject.property("desktopById")); m_scriptSelf.setProperty(QStringLiteral("desktopForScreen"), globalScriptEngineObject.property("desktopForScreen")); m_scriptSelf.setProperty(QStringLiteral("panelById"), globalScriptEngineObject.property("panelById")); m_scriptSelf.setProperty(QStringLiteral("panels"), globalScriptEngineObject.property("panels")); m_scriptSelf.setProperty(QStringLiteral("fileExists"), globalScriptEngineObject.property("fileExists")); m_scriptSelf.setProperty(QStringLiteral("loadTemplate"), globalScriptEngineObject.property("loadTemplate")); m_scriptSelf.setProperty(QStringLiteral("applicationExists"), globalScriptEngineObject.property("applicationExists")); m_scriptSelf.setProperty(QStringLiteral("defaultApplication"), globalScriptEngineObject.property("defaultApplication")); m_scriptSelf.setProperty(QStringLiteral("userDataPath"), globalScriptEngineObject.property("userDataPath")); m_scriptSelf.setProperty(QStringLiteral("applicationPath"), globalScriptEngineObject.property("applicationPath")); m_scriptSelf.setProperty(QStringLiteral("knownWallpaperPlugins"), globalScriptEngineObject.property("knownWallpaperPlugins")); m_scriptSelf.setProperty(QStringLiteral("gridUnit"), globalScriptEngineObject.property("gridUnit")); m_scriptSelf.setProperty(QStringLiteral("setImmutability"), globalScriptEngineObject.property("setImmutability")); m_scriptSelf.setProperty(QStringLiteral("immutability"), globalScriptEngineObject.property("immutability")); //i18n m_scriptSelf.setProperty(QStringLiteral("i18n"), localizedContext.property("i18n")); m_scriptSelf.setProperty(QStringLiteral("i18nc"), localizedContext.property("i18nc")); m_scriptSelf.setProperty(QStringLiteral("i18np"), localizedContext.property("i18np")); m_scriptSelf.setProperty(QStringLiteral("i18ncp"), localizedContext.property("i18ncp")); m_scriptSelf.setProperty(QStringLiteral("i18nd"), localizedContext.property("i18nd")); m_scriptSelf.setProperty(QStringLiteral("i18ndc"), localizedContext.property("i18ndc")); m_scriptSelf.setProperty(QStringLiteral("i18ndp"), localizedContext.property("i18ndp")); m_scriptSelf.setProperty(QStringLiteral("i18ndcp"), localizedContext.property("i18ndcp")); m_scriptSelf.setProperty(QStringLiteral("xi18n"), localizedContext.property("xi18n")); m_scriptSelf.setProperty(QStringLiteral("xi18nc"), localizedContext.property("xi18nc")); m_scriptSelf.setProperty(QStringLiteral("xi18np"), localizedContext.property("xi18np")); m_scriptSelf.setProperty(QStringLiteral("xi18ncp"), localizedContext.property("xi18ncp")); m_scriptSelf.setProperty(QStringLiteral("xi18nd"), localizedContext.property("xi18nd")); m_scriptSelf.setProperty(QStringLiteral("xi18ndc"), localizedContext.property("xi18ndc")); m_scriptSelf.setProperty(QStringLiteral("xi18ndp"), localizedContext.property("xi18ndp")); m_scriptSelf.setProperty(QStringLiteral("xi18ndcp"), localizedContext.property("xi18ndcp")); } bool ScriptEngine::isPanel(const Plasma::Containment *c) { if (!c) { return false; } return c->containmentType() == Plasma::Types::PanelContainment || c->containmentType() == Plasma::Types::CustomPanelContainment; } Plasma::Corona *ScriptEngine::corona() const { return m_corona; } bool ScriptEngine::evaluateScript(const QString &script, const QString &path) { m_errorString = QString(); QJSValue result = evaluate(script, path); if (result.isError()) { //qDebug() << "catch the exception!"; QString error = i18n("Error: %1 at line %2\n\nBacktrace:\n%3", result.toString(), result.property("lineNumber").toInt(), result.property("stack").toVariant().value().join(QLatin1String("\n "))); emit printError(error); emit exception(result); m_errorString = error; return false; } return true; } void ScriptEngine::exception(const QJSValue &value) { //qDebug() << "exception caught!" << value.toVariant(); emit printError(value.toVariant().toString()); } QStringList ScriptEngine::pendingUpdateScripts(Plasma::Corona *corona) { if (!corona->package().metadata().isValid()) { qWarning() << "Warning: corona package invalid"; return QStringList(); } const QString appName = corona->package().metadata().pluginName(); QStringList scripts; const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "plasma/shells/" + appName + QStringLiteral("/contents/updates"), QStandardPaths::LocateDirectory); for (const QString& dir : dirs) { QDirIterator it(dir, QStringList() << QStringLiteral("*.js")); while (it.hasNext()) { scripts.append(it.next()); } } QStringList scriptPaths; if (scripts.isEmpty()) { //qDebug() << "no update scripts"; return scriptPaths; } KConfigGroup cg(KSharedConfig::openConfig(), "Updates"); QStringList performed = cg.readEntry("performed", QStringList()); const QString localXdgDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); foreach (const QString &script, scripts) { if (performed.contains(script)) { continue; } if (script.startsWith(localXdgDir)) { // qDebug() << "skipping user local script: " << script; continue; } scriptPaths.append(script); performed.append(script); } cg.writeEntry("performed", performed); KSharedConfig::openConfig()->sync(); return scriptPaths; } QStringList ScriptEngine::availableActivities() const { ShellCorona *sc = qobject_cast(m_corona); StandaloneAppCorona *ac = qobject_cast(m_corona); if (sc) { return sc->availableActivities(); } else if (ac) { return ac->availableActivities(); } return QStringList(); } QList ScriptEngine::desktopsForActivity(const QString &id) { QList result; // confirm this activity actually exists bool found = false; for (const QString &act: availableActivities()) { if (act == id) { found = true; break; } } if (!found) { return result; } foreach (Plasma::Containment *c, m_corona->containments()) { if (c->activity() == id && !isPanel(c)) { result << new Containment(c, this); } } if (result.count() == 0) { // we have no desktops for this activity, so lets make them now // this can happen when the activity already exists but has never been activated // with the current shell package and layout.js is run to set up the shell for the // first time ShellCorona *sc = qobject_cast(m_corona); StandaloneAppCorona *ac = qobject_cast(m_corona); if (sc) { foreach (int i, sc->screenIds()) { result << new Containment(sc->createContainmentForActivity(id, i), this); } } else if (ac) { const int numScreens = m_corona->numScreens(); for (int i = 0; i < numScreens; ++i) { result << new Containment(ac->createContainmentForActivity(id, i), this); } } } return result; } Plasma::Containment *ScriptEngine::createContainment(const QString &type, const QString &plugin) { bool exists = false; const KPluginInfo::List list = Plasma::PluginLoader::listContainmentsOfType(type); foreach (const KPluginInfo &info, list) { if (info.pluginName() == plugin) { exists = true; break; } } if (!exists) { return nullptr; } Plasma::Containment *c = nullptr; if (type == QLatin1String("Panel")) { ShellCorona *sc = qobject_cast(m_corona); StandaloneAppCorona *ac = qobject_cast(m_corona); if (sc) { c = sc->addPanel(plugin); } else if (ac) { c = ac->addPanel(plugin); } } else { c = m_corona->createContainment(plugin); } if (c) { if (type == QLatin1String("Panel")) { // some defaults c->setFormFactor(Plasma::Types::Horizontal); c->setLocation(Plasma::Types::TopEdge); //we have to force lastScreen of the newly created containment, //or it won't have a screen yet at that point, breaking JS code //that relies on it //NOTE: if we'll allow setting a panel screen from JS, it will have to use the following lines as well KConfigGroup cg=c->config(); cg.writeEntry(QStringLiteral("lastScreen"), 0); c->restore(cg); } c->updateConstraints(Plasma::Types::AllConstraints | Plasma::Types::StartupCompletedConstraint); c->flushPendingConstraintsEvents(); } return c; } Containment *ScriptEngine::createContainmentWrapper(const QString &type, const QString &plugin) { Plasma::Containment *c = createContainment(type, plugin); return isPanel(c) ? new Panel(c, this) : new Containment(c, this); } } // namespace WorkspaceScripting diff --git a/shell/scripting/widget.cpp b/shell/scripting/widget.cpp index daf02d24f..f97649c37 100644 --- a/shell/scripting/widget.cpp +++ b/shell/scripting/widget.cpp @@ -1,199 +1,209 @@ /* * 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 "scriptengine.h" #include #include +#include #include #include #include namespace WorkspaceScripting { class Widget::Private { public: Private() { } QPointer applet; }; -Widget::Widget(Plasma::Applet *applet, QObject *parent) +Widget::Widget(Plasma::Applet *applet, ScriptEngine *parent) : Applet(parent), d(new Widget::Private) { d->applet = applet; setCurrentConfigGroup(QStringList()); setCurrentGlobalConfigGroup(QStringList()); } Widget::~Widget() { reloadConfigIfNeeded(); delete d; } uint Widget::id() const { if (d->applet) { return d->applet.data()->id(); } return 0; } QString Widget::type() const { if (d->applet) { return d->applet.data()->pluginMetaData().pluginId(); } return QString(); } void Widget::remove() { if (d->applet) { d->applet.data()->destroy(); d->applet.clear(); } } void Widget::setGlobalShortcut(const QString &shortcut) { if (d->applet) { d->applet.data()->setGlobalShortcut(QKeySequence(shortcut)); } } QString Widget::globalShorcut() const { if (d->applet) { return d->applet.data()->globalShortcut().toString(); } return QString(); } Plasma::Applet *Widget::applet() const { return d->applet.data(); } int Widget::index() const { if (!d->applet) { return -1; } Plasma::Applet *applet = d->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) { Q_UNUSED(index) /* if (!d->applet) { return; } Plasma::Applet *applet = d->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);*/ } QJSValue Widget::geometry() const { - /*if (d->applet) { - return d->applet.data()->geometry(); + QQuickItem *appletItem = d->applet.data()->property("_plasma_graphicObject").value(); + + if (appletItem) { + QJSValue rect = engine()->newObject(); + const QPointF pos = appletItem->mapToScene(QPointF(0,0)); + rect.setProperty(QStringLiteral("x"), pos.x()); + rect.setProperty(QStringLiteral("y"), pos.y()); + rect.setProperty(QStringLiteral("width"), appletItem->width()); + rect.setProperty(QStringLiteral("height"), appletItem->height()); + return rect; } -*/ + return QJSValue(); } void Widget::setGeometry(const QJSValue &geometry) { Q_UNUSED(geometry) /*if (d->applet) { d->applet.data()->setGeometry(geometry); KConfigGroup cg = d->applet.data()->config().parent(); if (cg.isValid()) { cg.writeEntry("geometry", geometry); } }*/ } void Widget::showConfigurationInterface() { /* if (d->applet) { d->applet.data()->showConfigurationInterface(); }*/ } QString Widget::userBackgroundHints() const { QMetaEnum hintEnum = QMetaEnum::fromType(); return hintEnum.valueToKey(applet()->userBackgroundHints()); } void Widget::setUserBackgroundHints(QString hint) { QMetaEnum hintEnum = QMetaEnum::fromType(); bool ok; int value = hintEnum.keyToValue(hint.toUtf8().constData(), &ok); if (ok) { applet()->setUserBackgroundHints(Plasma::Types::BackgroundHints(value)); } } } diff --git a/shell/scripting/widget.h b/shell/scripting/widget.h index d1401d9d0..32a8d3a5d 100644 --- a/shell/scripting/widget.h +++ b/shell/scripting/widget.h @@ -1,87 +1,87 @@ /* * 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 #include "applet.h" namespace Plasma { class Applet; } // namespace Plasma namespace WorkspaceScripting { class Widget : public Applet { Q_OBJECT Q_PROPERTY(QString type READ type) Q_PROPERTY(QString version READ version) Q_PROPERTY(int id READ id) Q_PROPERTY(QStringList configKeys READ configKeys) Q_PROPERTY(QStringList configGroups READ configGroups) Q_PROPERTY(QStringList globalConfigKeys READ globalConfigKeys) Q_PROPERTY(QStringList globalConfigGroups READ globalConfigGroups) Q_PROPERTY(int index WRITE setIndex READ index) //We pass our js based QRect wrapper instead of a simple QRectF Q_PROPERTY(QJSValue geometry WRITE setGeometry READ geometry) Q_PROPERTY(QStringList currentConfigGroup WRITE setCurrentConfigGroup READ currentConfigGroup) Q_PROPERTY(QString globalShortcut WRITE setGlobalShortcut READ globalShorcut) Q_PROPERTY(bool locked READ locked WRITE setLocked) Q_PROPERTY(QString userBackgroundHints WRITE setUserBackgroundHints READ userBackgroundHints) public: - explicit Widget(Plasma::Applet *applet, QObject *parent = nullptr); + explicit Widget(Plasma::Applet *applet, ScriptEngine *parent = nullptr); ~Widget() override; uint id() const; QString type() const; int index() const; void setIndex(int index); QJSValue geometry() const; void setGeometry(const QJSValue &geometry); void setGlobalShortcut(const QString &shortcut); QString globalShorcut() const; QString userBackgroundHints() const; void setUserBackgroundHints(QString hint); Plasma::Applet *applet() const override; public Q_SLOTS: void remove(); void showConfigurationInterface(); private: class Private; Private * const d; }; } #endif