• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • scripting
scriptengine.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2007 by Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "scripting/scriptengine.h"
21
22#include <kdebug.h>
23#include <kservice.h>
24#include <kservicetypetrader.h>
25
26#include "abstractrunner.h"
27#include "applet.h"
28#include "dataengine.h"
29#include "package.h"
30#include "private/componentinstaller_p.h"
31#include "scripting/appletscript.h"
32#include "scripting/dataenginescript.h"
33#include "scripting/runnerscript.h"
34#include "scripting/wallpaperscript.h"
35
36#include "private/packages_p.h"
37
38namespace Plasma
39{
40
41ScriptEngine::ScriptEngine(QObject *parent)
42 : QObject(parent),
43 d(0)
44{
45}
46
47ScriptEngine::~ScriptEngine()
48{
49// delete d;
50}
51
52bool ScriptEngine::init()
53{
54 return true;
55}
56
57const Package *ScriptEngine::package() const
58{
59 return 0;
60}
61
62QString ScriptEngine::mainScript() const
63{
64 return QString();
65}
66
67QStringList knownLanguages(ComponentTypes types)
68{
69 QString constraintTemplate = "'%1' in [X-Plasma-ComponentTypes]";
70 QString constraint;
71
72 if (types & AppletComponent) {
73 // currently this if statement is not needed, but this future proofs
74 // the code against someone initializing constraint to something
75 // before we get here.
76 if (!constraint.isEmpty()) {
77 constraint.append(" or ");
78 }
79
80 constraint.append(constraintTemplate.arg("Applet"));
81 }
82
83 if (types & DataEngineComponent) {
84 if (!constraint.isEmpty()) {
85 constraint.append(" or ");
86 }
87
88 constraint.append(constraintTemplate.arg("DataEngine"));
89 }
90
91 if (types & RunnerComponent) {
92 if (!constraint.isEmpty()) {
93 constraint.append(" or ");
94 }
95
96 constraint.append(constraintTemplate.arg("Runner"));
97 }
98
99 if (types & WallpaperComponent) {
100 if (!constraint.isEmpty()) {
101 constraint.append(" or ");
102 }
103
104 constraint.append(constraintTemplate.arg("Wallpaper"));
105 }
106
107 KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
108 //kDebug() << "Applet::knownApplets constraint was '" << constraint
109 // << "' which got us " << offers.count() << " matches";
110
111 QStringList languages;
112 foreach (const KService::Ptr &service, offers) {
113 QString language = service->property("X-Plasma-API").toString();
114 if (!languages.contains(language)) {
115 languages.append(language);
116 }
117 }
118
119 return languages;
120}
121
122KService::List engineOffers(const QString &language, ComponentType type)
123{
124 if (language.isEmpty()) {
125 return KService::List();
126 }
127
128 QRegExp re("[^a-zA-Z0-9\\-_]");
129 if (re.indexIn(language) != -1) {
130 kDebug() << "invalid language attempted:" << language;
131 return KService::List();
132 }
133
134 QString component;
135 switch (type) {
136 case AppletComponent:
137 component = "Applet";
138 break;
139 case DataEngineComponent:
140 component = "DataEngine";
141 break;
142 case RunnerComponent:
143 component = "Runner";
144 break;
145 case WallpaperComponent:
146 component = "Wallpaper";
147 break;
148 default:
149 return KService::List();
150 break;
151 }
152
153 QString constraint = QString("[X-Plasma-API] == '%1' and "
154 "'%2' in [X-Plasma-ComponentTypes]").arg(language, component);
155 KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
156 /* kDebug() << "********************* loadingApplet with Plasma/ScriptEngine" << constraint
157 << "resulting in" << offers.count() << "results";*/
158 if (offers.isEmpty()) {
159 kDebug() << "No offers for \"" << language << "\"";
160 }
161
162 return offers;
163}
164
165ScriptEngine *loadEngine(const QString &language, ComponentType type, QObject *parent)
166{
167 KService::List offers = engineOffers(language, type);
168
169 QVariantList args;
170 QString error;
171
172 ScriptEngine *engine = 0;
173 foreach (const KService::Ptr &service, offers) {
174 switch (type) {
175 case AppletComponent:
176 engine = service->createInstance<Plasma::AppletScript>(parent, args, &error);
177 break;
178 case DataEngineComponent:
179 engine = service->createInstance<Plasma::DataEngineScript>(parent, args, &error);
180 break;
181 case RunnerComponent:
182 engine = service->createInstance<Plasma::RunnerScript>(parent, args, &error);
183 break;
184 case WallpaperComponent:
185 engine = service->createInstance<Plasma::WallpaperScript>(parent, args, &error);
186 break;
187 default:
188 return 0;
189 break;
190 }
191
192 if (engine) {
193 return engine;
194 }
195
196 kDebug() << "Couldn't load script engine for language " << language
197 << "! error reported: " << error;
198 }
199
200 // Try installing the engine. However, it's too late for this request.
201 ComponentInstaller::self()->installMissingComponent("scriptengine", language);
202
203 return 0;
204}
205
206AppletScript *loadScriptEngine(const QString &language, Applet *applet)
207{
208 AppletScript *engine =
209 static_cast<AppletScript*>(loadEngine(language, AppletComponent, applet));
210
211 if (engine) {
212 engine->setApplet(applet);
213 }
214
215 return engine;
216}
217
218DataEngineScript *loadScriptEngine(const QString &language, DataEngine *dataEngine)
219{
220 DataEngineScript *engine =
221 static_cast<DataEngineScript*>(loadEngine(language, DataEngineComponent, dataEngine));
222
223 if (engine) {
224 engine->setDataEngine(dataEngine);
225 }
226
227 return engine;
228}
229
230RunnerScript *loadScriptEngine(const QString &language, AbstractRunner *runner)
231{
232 RunnerScript *engine =
233 static_cast<RunnerScript*>(loadEngine(language, RunnerComponent, runner));
234
235 if (engine) {
236 engine->setRunner(runner);
237 }
238
239 return engine;
240}
241
242WallpaperScript *loadScriptEngine(const QString &language, Wallpaper *wallpaper)
243{
244 WallpaperScript *engine =
245 static_cast<WallpaperScript*>(loadEngine(language, WallpaperComponent, wallpaper));
246
247 if (engine) {
248 engine->setWallpaper(wallpaper);
249 }
250
251 return engine;
252}
253
254PackageStructure::Ptr defaultPackageStructure(ComponentType type)
255{
256 switch (type) {
257 case AppletComponent:
258 case WallpaperComponent:
259 case RunnerComponent:
260 case GenericComponent:
261 return PackageStructure::Ptr(new PlasmoidPackage());
262 break;
263 case DataEngineComponent:
264 return PackageStructure::Ptr(new DataEnginePackage());
265 break;
266 default:
267 // TODO: we don't have any special structures for other components yet
268 break;
269 }
270
271 return PackageStructure::Ptr(new PackageStructure());
272}
273
274PackageStructure::Ptr packageStructure(const QString &language, ComponentType type)
275{
276 KService::List offers = engineOffers(language, type);
277
278 if (offers.isEmpty()) {
279 return defaultPackageStructure(type);
280 }
281
282 KService::Ptr offer = offers.first();
283 QString packageFormat = offer->property("X-Plasma-PackageFormat").toString();
284
285 if (packageFormat.isEmpty()) {
286 return defaultPackageStructure(type);
287 } else {
288 PackageStructure::Ptr structure = PackageStructure::load(packageFormat);
289 return structure;
290 }
291}
292
293} // namespace Plasma
294
295#include <scriptengine.moc>
296
abstractrunner.h
applet.h
appletscript.h
Plasma::AbstractRunner
An abstract base class for Plasma Runner plugins.
Definition: abstractrunner.h:64
Plasma::AppletScript
Provides a restricted interface for scripted applets.
Definition: appletscript.h:52
Plasma::AppletScript::setApplet
void setApplet(Plasma::Applet *applet)
Sets the applet associated with this AppletScript.
Definition: appletscript.cpp:52
Plasma::Applet
The base Applet class.
Definition: applet.h:78
Plasma::DataEngineScript
Provides a restricted interface for scripting a DataEngine.
Definition: dataenginescript.h:42
Plasma::DataEngineScript::setDataEngine
void setDataEngine(DataEngine *dataEngine)
Sets the Plasma::DataEngine associated with this DataEngineScript.
Definition: dataenginescript.cpp:46
Plasma::DataEngine
Data provider for plasmoids (Plasma plugins)
Definition: dataengine.h:59
Plasma::PackageStructure
A description of the expected file structure of a given package type.
Definition: packagestructure.h:73
Plasma::PackageStructure::Ptr
KSharedPtr< PackageStructure > Ptr
Definition: packagestructure.h:77
Plasma::Package
object representing an installed Plasmagik package
Definition: package.h:43
Plasma::RunnerScript
Provides a restricted interface for scripting a runner.
Definition: runnerscript.h:41
Plasma::RunnerScript::setRunner
void setRunner(AbstractRunner *runner)
Sets the Plasma::AbstractRunner associated with this RunnerScript.
Definition: runnerscript.cpp:47
Plasma::ScriptEngine
The base class for scripting interfaces to be used in loading plasmoids of a given language.
Definition: scriptengine.h:66
Plasma::WallpaperScript
Provides a restricted interface for scripting a Wallpaper.
Definition: wallpaperscript.h:43
Plasma::WallpaperScript::setWallpaper
void setWallpaper(Wallpaper *wallpaper)
Sets the Plasma::Wallpaper associated with this WallpaperScript.
Definition: wallpaperscript.cpp:45
Plasma::Wallpaper
The base Wallpaper class.
Definition: wallpaper.h:57
QObject
dataengine.h
dataenginescript.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
Plasma::packageStructure
PackageStructure::Ptr packageStructure(const QString &language, ComponentType type)
Loads an appropriate PackageStructure for the given language and type.
Definition: scriptengine.cpp:274
Plasma::engineOffers
KService::List engineOffers(const QString &language, ComponentType type)
Definition: scriptengine.cpp:122
Plasma::loadScriptEngine
AppletScript * loadScriptEngine(const QString &language, Applet *applet)
Loads an Applet script engine for the given language.
Definition: scriptengine.cpp:206
Plasma::knownLanguages
QStringList knownLanguages(ComponentTypes types)
Definition: scriptengine.cpp:67
Plasma::ComponentType
ComponentType
The ComonentType enumeration refers to the various types of components, or plugins,...
Definition: plasma.h:225
Plasma::WallpaperComponent
@ WallpaperComponent
Plasma::Wallpaper based plugins.
Definition: plasma.h:231
Plasma::DataEngineComponent
@ DataEngineComponent
Plasma::DataEngine based plugins.
Definition: plasma.h:227
Plasma::AppletComponent
@ AppletComponent
Plasma::Applet based plugins.
Definition: plasma.h:226
Plasma::GenericComponent
@ GenericComponent
Definition: plasma.h:232
Plasma::RunnerComponent
@ RunnerComponent
Plasma::AbstractRunner based plugsin.
Definition: plasma.h:228
Plasma::defaultPackageStructure
PackageStructure::Ptr defaultPackageStructure(ComponentType type)
Definition: scriptengine.cpp:254
Plasma::loadEngine
ScriptEngine * loadEngine(const QString &language, ComponentType type, QObject *parent)
Definition: scriptengine.cpp:165
package.h
runnerscript.h
scriptengine.h
wallpaperscript.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal