Plasma

scriptengine.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Aaron Seigo <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "scripting/scriptengine.h"
8 
9 #include <QDebug>
10 #include <QGlobalStatic>
11 
12 #include "applet.h"
13 #include "dataengine.h"
14 #include "debug_p.h"
15 #include "package.h"
16 #include "scripting/appletscript.h"
17 #include "scripting/dataenginescript.h"
18 
19 namespace Plasma
20 {
21 static QVector<KPluginMetaData> listEngines(Types::ComponentTypes types, std::function<bool(const KPluginMetaData &)> filter)
22 {
24  const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("plasma/scriptengines"));
25  ret.reserve(plugins.size());
26  for (const auto &plugin : plugins) {
27  if (!filter(plugin)) {
28  continue;
29  }
30  const QStringList componentTypes = plugins.first().value(QStringLiteral("X-Plasma-ComponentTypes"), QStringList());
31  if (((types & Types::AppletComponent) && componentTypes.contains(QLatin1String("Applet")))
32  || ((types & Types::DataEngineComponent) && componentTypes.contains(QLatin1String("DataEngine")))) {
33  ret << plugin;
34  }
35  }
36  return ret;
37 }
38 
39 ScriptEngine::ScriptEngine(QObject *parent)
40  : QObject(parent)
41  , d(nullptr)
42 {
43 }
44 
45 ScriptEngine::~ScriptEngine()
46 {
47  // delete d;
48 }
49 
50 bool ScriptEngine::init()
51 {
52  return true;
53 }
54 
55 #if PLASMA_BUILD_DEPRECATED_SINCE(5, 83)
56 Package ScriptEngine::package() const
57 {
58  return Package();
59 }
60 #endif
61 
62 QString ScriptEngine::mainScript() const
63 {
64  return QString();
65 }
66 
68 {
69  QStringList languages;
70  const QVector<KPluginMetaData> plugins = listEngines(types, [](const KPluginMetaData &) -> bool {
71  return true;
72  });
73 
74  for (const auto &plugin : plugins) {
75  languages << plugin.value(QStringLiteral("X-Plasma-API"));
76  }
77 
78  return languages;
79 }
80 
81 typedef QHash<QString, KPluginMetaData> EngineCache;
82 Q_GLOBAL_STATIC(EngineCache, engines)
83 
84 ScriptEngine *loadEngine(const QString &language, Types::ComponentType type, QObject *parent, const QVariantList &args = QVariantList())
85 {
86  Q_UNUSED(parent);
87 
88  const KPluginMetaData cacheData = engines->value(language);
89  if (cacheData.isValid()) {
90  return KPluginFactory::instantiatePlugin<Plasma::ScriptEngine>(cacheData, nullptr, args).plugin;
91  }
92 
93  auto filter = [&language](const KPluginMetaData &md) -> bool {
94  return md.value(QStringLiteral("X-Plasma-API")) == language;
95  };
96 
97  const QVector<KPluginMetaData> plugins = listEngines(type, filter);
98  if (!plugins.isEmpty()) {
99  const KPluginMetaData metaData = plugins.first();
100  engines->insert(language, metaData);
101  if (auto res = KPluginFactory::instantiatePlugin<Plasma::ScriptEngine>(metaData, nullptr, args)) {
102  return res.plugin;
103  } else {
104  qCWarning(LOG_PLASMA) << "Unable to load" << plugins.first().name() << "ScriptEngine";
105  }
106  }
107 
108  return nullptr;
109 }
110 
111 AppletScript *loadScriptEngine(const QString &language, Applet *applet, const QVariantList &args)
112 {
113  AppletScript *engine = static_cast<AppletScript *>(loadEngine(language, Types::AppletComponent, applet, args));
114 
115  if (engine) {
116  engine->setApplet(applet);
117  }
118 
119  return engine;
120 }
121 
122 DataEngineScript *loadScriptEngine(const QString &language, DataEngine *dataEngine, const QVariantList &args)
123 {
124  DataEngineScript *engine = static_cast<DataEngineScript *>(loadEngine(language, Types::DataEngineComponent, dataEngine, args));
125 
126  if (engine) {
127  engine->setDataEngine(dataEngine);
128  }
129 
130  return engine;
131 }
132 
133 } // namespace Plasma
134 
135 #include "moc_scriptengine.cpp"
Provides a restricted interface for scripting a DataEngine.
T & first()
bool isEmpty() const const
Data provider for plasmoids (Plasma plugins)
Definition: dataengine.h:50
Namespace for everything in libplasma.
Definition: datamodel.cpp:14
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool isValid() const
T & first()
Provides a restricted interface for scripted applets.
Definition: appletscript.h:35
@ AppletComponent
Plasma::Applet based plugins.
Definition: plasma.h:249
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
AppletScript * loadScriptEngine(const QString &language, Applet *applet, const QVariantList &args)
Loads an Applet script engine for the given language.
void reserve(int size)
QStringList knownLanguages(Types::ComponentTypes types)
bool value(const QString &key, bool defaultValue) const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter, KPluginMetaDataOption option)
@ DataEngineComponent
Plasma::DataEngine based plugins.
Definition: plasma.h:250
int size() const const
object representing an installed Plasma package
Definition: package.h:76
void setApplet(Plasma::Applet *applet)
Sets the applet associated with this AppletScript.
void setDataEngine(DataEngine *dataEngine)
Sets the Plasma::DataEngine associated with this DataEngineScript.
The base Applet class.
Definition: applet.h:71
T value(int i) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:15:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.