File indexing completed on 2024-05-12 05:38:24

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "applet.h"
0008 #include "scriptengine.h"
0009 
0010 #include <QAction>
0011 #include <QQmlPropertyMap>
0012 
0013 #include <kconfigloader.h>
0014 #include <kservice.h>
0015 
0016 #include <Plasma/Applet>
0017 #include <Plasma/Containment>
0018 #include <Plasma/Corona>
0019 
0020 namespace WorkspaceScripting
0021 {
0022 class Applet::Private
0023 {
0024 public:
0025     Private()
0026         : configDirty(false)
0027         , inWallpaperConfig(false)
0028     {
0029     }
0030 
0031     QPointer<ScriptEngine> engine = nullptr;
0032     KConfigGroup configGroup;
0033     QStringList configGroupPath;
0034     KConfigGroup globalConfigGroup;
0035     QStringList globalConfigGroupPath;
0036     bool configDirty : 1;
0037     bool inWallpaperConfig : 1;
0038 };
0039 
0040 Applet::Applet(ScriptEngine *parent)
0041     : QObject(parent)
0042     , d(new Applet::Private)
0043 {
0044     d->engine = parent;
0045 }
0046 
0047 Applet::~Applet()
0048 {
0049     delete d;
0050 }
0051 
0052 void Applet::setCurrentConfigGroup(const QStringList &groupNames)
0053 {
0054     Plasma::Applet *app = applet();
0055     if (!app) {
0056         d->configGroup = KConfigGroup();
0057         d->configGroupPath.clear();
0058         return;
0059     }
0060 
0061     d->configGroup = app->config();
0062     d->configGroupPath = groupNames;
0063 
0064     for (const QString &groupName : groupNames) {
0065         d->configGroup = KConfigGroup(&d->configGroup, groupName);
0066     }
0067 
0068     d->inWallpaperConfig = !groupNames.isEmpty() && groupNames.first() == QLatin1String("Wallpaper");
0069 }
0070 
0071 QStringList Applet::currentConfigGroup() const
0072 {
0073     return d->configGroupPath;
0074 }
0075 
0076 QStringList Applet::configKeys() const
0077 {
0078     if (d->configGroup.isValid()) {
0079         return d->configGroup.keyList();
0080     }
0081 
0082     return QStringList();
0083 }
0084 
0085 QStringList Applet::configGroups() const
0086 {
0087     if (d->configGroup.isValid()) {
0088         return d->configGroup.groupList();
0089     }
0090 
0091     return QStringList();
0092 }
0093 
0094 QVariant Applet::readConfig(const QString &key, const QJSValue &def) const
0095 {
0096     if (d->configGroup.isValid()) {
0097         return d->configGroup.readEntry(key, def.toVariant());
0098     } else {
0099         return QVariant();
0100     }
0101 }
0102 
0103 void Applet::writeConfig(const QString &key, const QJSValue &value)
0104 {
0105     if (d->configGroup.isValid()) {
0106         if (d->inWallpaperConfig) {
0107             // hacky, but only way to make the wallpaper react immediately
0108             QObject *wallpaperGraphicsObject = applet()->property("wallpaperGraphicsObject").value<QObject *>();
0109             if (wallpaperGraphicsObject) {
0110                 auto *config = static_cast<QQmlPropertyMap *>(wallpaperGraphicsObject->property("configuration").value<QObject *>());
0111                 config->setProperty(key.toLatin1(), value.toVariant());
0112             }
0113         } else if (applet()->configScheme()) {
0114             // check if it can be written in the applets' configScheme
0115             KConfigSkeletonItem *item = applet()->configScheme()->findItemByName(key);
0116             if (item) {
0117                 item->setProperty(value.toVariant());
0118                 applet()->configScheme()->blockSignals(true);
0119                 applet()->configScheme()->save();
0120                 // why read? read will update KConfigSkeletonItem::mLoadedValue,
0121                 // allowing a write operation to be performed next time
0122                 applet()->configScheme()->read();
0123                 applet()->configScheme()->blockSignals(false);
0124                 Q_EMIT applet()->configScheme()->configChanged();
0125             }
0126         }
0127 
0128         d->configGroup.writeEntry(key, value.toVariant());
0129         d->configDirty = true;
0130     }
0131 }
0132 
0133 void Applet::setCurrentGlobalConfigGroup(const QStringList &groupNames)
0134 {
0135     Plasma::Applet *app = applet();
0136     if (!app) {
0137         d->globalConfigGroup = KConfigGroup();
0138         d->globalConfigGroupPath.clear();
0139         return;
0140     }
0141 
0142     d->globalConfigGroup = app->globalConfig();
0143     d->globalConfigGroupPath = groupNames;
0144 
0145     for (const QString &groupName : groupNames) {
0146         d->globalConfigGroup = KConfigGroup(&d->globalConfigGroup, groupName);
0147     }
0148 }
0149 
0150 QStringList Applet::currentGlobalConfigGroup() const
0151 {
0152     return d->globalConfigGroupPath;
0153 }
0154 
0155 QStringList Applet::globalConfigKeys() const
0156 {
0157     if (d->globalConfigGroup.isValid()) {
0158         return d->globalConfigGroup.keyList();
0159     }
0160 
0161     return QStringList();
0162 }
0163 
0164 QStringList Applet::globalConfigGroups() const
0165 {
0166     if (d->globalConfigGroup.isValid()) {
0167         return d->globalConfigGroup.groupList();
0168     }
0169 
0170     return QStringList();
0171 }
0172 
0173 QVariant Applet::readGlobalConfig(const QString &key, const QJSValue &def) const
0174 {
0175     if (d->globalConfigGroup.isValid()) {
0176         return d->globalConfigGroup.readEntry(key, def.toVariant());
0177     } else {
0178         return QVariant();
0179     }
0180 }
0181 
0182 void Applet::writeGlobalConfig(const QString &key, const QJSValue &value)
0183 {
0184     if (d->globalConfigGroup.isValid()) {
0185         d->globalConfigGroup.writeEntry(key, value.toVariant());
0186         d->configDirty = true;
0187     }
0188 }
0189 
0190 void Applet::reloadConfigIfNeeded()
0191 {
0192     if (d->configDirty) {
0193         reloadConfig();
0194     }
0195 }
0196 
0197 void Applet::reloadConfig()
0198 {
0199     Plasma::Applet *app = applet();
0200     if (app) {
0201         KConfigGroup cg = app->config();
0202 
0203         if (!app->isContainment()) {
0204             app->restore(cg);
0205         }
0206 
0207         app->configChanged();
0208 
0209         if (app->containment() && app->containment()->corona()) {
0210             app->containment()->corona()->requestConfigSync();
0211         }
0212 
0213         d->configDirty = false;
0214     }
0215 }
0216 
0217 QString Applet::version() const
0218 {
0219     Plasma::Applet *app = applet();
0220     if (!app) {
0221         return QString();
0222     }
0223 
0224     return app->pluginMetaData().version();
0225 }
0226 
0227 void Applet::setLocked(bool locked)
0228 {
0229     Plasma::Applet *app = applet();
0230     if (!app) {
0231         return;
0232     }
0233 
0234     app->setImmutability(locked ? Plasma::Types::UserImmutable : Plasma::Types::Mutable);
0235     KConfigGroup cg = app->config();
0236     if (!app->isContainment()) {
0237         cg = cg.parent();
0238     }
0239 
0240     if (cg.isValid()) {
0241         cg.writeEntry("immutability", (int)app->immutability());
0242     }
0243 }
0244 
0245 bool Applet::locked() const
0246 {
0247     Plasma::Applet *app = applet();
0248     if (!app) {
0249         return Plasma::Types::Mutable;
0250     }
0251 
0252     return app->immutability() != Plasma::Types::Mutable;
0253 }
0254 
0255 Plasma::Applet *Applet::applet() const
0256 {
0257     return nullptr;
0258 }
0259 
0260 ScriptEngine *Applet::engine() const
0261 {
0262     return d->engine;
0263 }
0264 
0265 }