2 * Copyright 2007 by Aaron Seigo <aseigo@kde.org>
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.
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
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.
20 #include "scripting/scriptengine.h"
24 #include <KServiceTypeTrader>
26 #include "abstractrunner.h"
28 #include "dataengine.h"
30 #include "scripting/appletscript.h"
31 #include "scripting/dataenginescript.h"
32 #include "scripting/runnerscript.h"
37 ScriptEngine::ScriptEngine(QObject
*parent
)
43 ScriptEngine::~ScriptEngine()
48 bool ScriptEngine::init()
53 QStringList
knownLanguages(ComponentTypes types
)
55 QString constraintTemplate
= "'%1' in [X-Plasma-ComponentTypes]";
58 if (types
& AppletComponent
) {
59 // currently this if statement is not needed, but this future proofs
60 // the code against someone initializing constraint to something
61 // before we get here.
62 if (!constraint
.isEmpty()) {
63 constraint
.append(" or ");
66 constraint
.append(constraintTemplate
.arg("Applet"));
69 if (types
& DataEngineComponent
) {
70 if (!constraint
.isEmpty()) {
71 constraint
.append(" or ");
74 constraint
.append(constraintTemplate
.arg("DataEngine"));
77 if (types
& RunnerComponent
) {
78 if (!constraint
.isEmpty()) {
79 constraint
.append(" or ");
82 constraint
.append(constraintTemplate
.arg("Runner"));
85 KService::List offers
= KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint
);
86 //kDebug() << "Applet::knownApplets constraint was '" << constraint << "' which got us " << offers.count() << " matches";
88 QStringList languages
;
89 foreach (KService::Ptr service
, offers
) {
90 QString language
= service
->property("X-Plasma-Language").toString();
91 if (!languages
.contains(language
)) {
92 languages
.append(language
);
99 ScriptEngine
* loadEngine(const QString
&language
, ComponentType type
, QObject
*parent
)
101 if (language
.isEmpty()) {
105 QRegExp
re("[^a-zA-Z0-9\\-_]");
106 if (re
.indexIn(language
) != -1) {
107 kDebug() << "invalid language attempted:" << language
;
113 case AppletComponent
:
114 component
= "Applet";
116 case DataEngineComponent
:
117 component
= "DataEngine";
119 case RunnerComponent
:
120 component
= "Runner";
127 QString constraint
= QString("[X-Plasma-Language] == '%1' and "
128 "'%2' in [X-Plasma-ComponentTypes]").arg(language
, component
);
129 KService::List offers
= KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint
);
130 /* kDebug() << "********************* loadingApplet with Plasma/ScriptEngine" << constraint
131 << "resulting in" << offers.count() << "results";*/
132 if (offers
.isEmpty()) {
133 kDebug() << "ScriptEngine::load: no offers for \"" << language
<< "\"";
140 ScriptEngine
*engine
= 0;
141 foreach (KService::Ptr service
, offers
) {
143 case AppletComponent
:
144 engine
= service
->createInstance
<Plasma::AppletScript
>(parent
, args
, &error
);
146 case DataEngineComponent
:
147 engine
= service
->createInstance
<Plasma::DataEngineScript
>(parent
, args
, &error
);
149 case RunnerComponent
:
150 engine
= service
->createInstance
<Plasma::RunnerScript
>(parent
, args
, &error
);
161 kDebug() << "Couldn't load script engine for language " << language
<< "! error reported: " << error
;
167 AppletScript
* loadScriptEngine(const QString
&language
, Applet
*applet
)
169 AppletScript
*engine
= static_cast<AppletScript
*>(loadEngine(language
, AppletComponent
, applet
));
172 engine
->setApplet(applet
);
178 DataEngineScript
* loadScriptEngine(const QString
&language
, DataEngine
*dataEngine
)
180 DataEngineScript
*engine
= static_cast<DataEngineScript
*>(loadEngine(language
, DataEngineComponent
, dataEngine
));
183 engine
->setDataEngine(dataEngine
);
189 RunnerScript
* loadScriptEngine(const QString
&language
, AbstractRunner
*runner
)
191 RunnerScript
* engine
= static_cast<RunnerScript
*>(loadEngine(language
, RunnerComponent
, runner
));
194 engine
->setRunner(runner
);
200 } // namespace Plasma
202 #include <scriptengine.moc>