first draft of the ScriptEngine plugin

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=691244
This commit is contained in:
Aaron J. Seigo 2007-07-23 07:42:29 +00:00
parent 6b9842aa5e
commit 39422a9a6b
5 changed files with 281 additions and 18 deletions

View File

@ -36,6 +36,7 @@
#include "plasma/package.h" #include "plasma/package.h"
#include "plasma/packages_p.h" #include "plasma/packages_p.h"
#include "plasma/plasma.h" #include "plasma/plasma.h"
#include "plasma/scriptengine.h"
#include "plasma/svg.h" #include "plasma/svg.h"
#include "plasma/widgets/widget.h" #include "plasma/widgets/widget.h"
@ -67,22 +68,6 @@ public:
if (appletId > s_maxAppletId) { if (appletId > s_maxAppletId) {
s_maxAppletId = appletId; s_maxAppletId = appletId;
} }
if (appletDescription.isValid() &&
!appletDescription.property("X-Plasma-Language").toString().isEmpty()) {
QString path = KStandardDirs::locate("appdata",
"plasmoids/" +
appletDescription.pluginName());
if (!path.isEmpty()) {
package = new Package(path,
appletDescription.pluginName(),
PlasmoidStructure());
if (!package->isValid()) {
delete package;
package = 0;
}
}
}
} }
~Private() ~Private()
@ -100,9 +85,44 @@ public:
applet->setFailedToLaunch(true); applet->setFailedToLaunch(true);
} }
QString language = appletDescription.property("X-Plasma-Language").toString();
// we have a scripted plasmoid
if (!language.isEmpty()) {
// find where the Package is
QString path = KStandardDirs::locate("appdata",
"plasmoids/" +
appletDescription.pluginName());
if (!path.isEmpty()) {
// create the package and see if we have something real
package = new Package(path,
appletDescription.pluginName(),
PlasmoidStructure());
if (package->isValid()) {
// now we try and set up the script engine.
// it will be parented to this applet and so will get
// deleted when the applet does
//TODO: do we need to hold on to the engine for later use?
ScriptEngine* engine = ScriptEngine::load(language, applet);
if (!engine) {
delete package;
package = 0;
}
} else {
delete package;
package = 0;
}
if (!package) {
applet->setFailedToLaunch(true);
}
}
}
applet->setImmutable(applet->globalConfig().isImmutable() || applet->setImmutable(applet->globalConfig().isImmutable() ||
applet->config().isImmutable()); applet->config().isImmutable());
} }
static uint nextId() static uint nextId()

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005 by Aaron Seigo <aseigo@kde.org> * Copyright (C) 2006-2007 by Aaron Seigo <aseigo@kde.org>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as * it under the terms of the GNU Library General Public License version 2 as

1
includes/ScriptEngine Normal file
View File

@ -0,0 +1 @@
#include "../../plasma/scriptengine.h"

131
scriptengine.cpp Normal file
View File

@ -0,0 +1,131 @@
/*
* Copyright 2007 by Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* 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 <KDebug>
#include <KService>
#include <KServiceTypeTrader>
#include "applet.h"
#include "package.h"
namespace Plasma
{
class ScriptEngine::Private
{
public:
Applet* applet;
};
ScriptEngine::ScriptEngine(QObject *parent, const QStringList &args)
: QObject(parent),
d(new Private)
{
Q_UNUSED(args)
d->applet = 0;
}
ScriptEngine::~ScriptEngine()
{
}
void ScriptEngine::init(Applet* applet)
{
d->applet = applet;
if (!init()) {
d->applet->setFailedToLaunch(true);
}
}
bool ScriptEngine::init()
{
Q_ASSERT(d->applet);
return false;
}
QString ScriptEngine::mainScript() const
{
Q_ASSERT(d->applet);
return d->applet->package()->filePath("mainscript");
}
Applet* ScriptEngine::applet() const
{
Q_ASSERT(d->applet);
return d->applet;
}
const Package* ScriptEngine::package() const
{
Q_ASSERT(d->applet);
return d->applet->package();
}
QStringList ScriptEngine::knownLanguages()
{
KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine");
//kDebug() << "Applet::knownApplets constraint was '" << constraint << "' which got us " << offers.count() << " matches" << endl;
QStringList languages;
foreach (KService::Ptr service, offers) {
QString language = service->property("X-Plasma-Language").toString();
if (!languages.contains(language)) {
languages.append(language);
}
}
return languages;
}
ScriptEngine* ScriptEngine::load(const QString &language, Applet *applet)
{
if (language.isEmpty()) {
return 0;
}
QString constraint = QString("[X-Plasma-Language] == '%1'").arg(language);
KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
if (offers.isEmpty()) {
kDebug() << "ScriptEngine::load: no offers for \"" << language << "\"" << endl;
return 0;
}
QStringList args;
ScriptEngine* engine = 0;
foreach (KService::Ptr service, offers) {
engine = KService::createInstance<Plasma::ScriptEngine>(service, applet, args);
if (engine) {
break;
}
}
if (!engine) {
kDebug() << "Couldn't load script engine for language " << language << "!" << endl;
}
engine->init(applet);
return engine;
}
} // namespace Plasma
#include <scriptengine.moc>

111
scriptengine.h Normal file
View File

@ -0,0 +1,111 @@
/*
* Copyright 2007 by Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* 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 PLASMA_SCRIPTENGINE_H
#define PLASMA_SCRIPTENGINE_H
#include <QtCore/QObject>
namespace Plasma
{
class Applet;
class Package;
/**
* @brief The base class for scripting interfaces to be used in loading
* plasmoids of a given language.
*
* All ScriptEngines should export as consistent an interface as possible
* so that the learning curve is limited. In particular, the following
* API should be made available in the script environment:
*
* TODO: define the actual API ...
* PlasmaApplet - the applet of this plasmoid
* LoadUserInterface(String uiFile) - loads and returns a given UI file
* LoadImage - loads an image resource out of the plasmoid's package
* PlasmaSvg - creates and returns an Svg file
**/
class ScriptEngine : public QObject
{
Q_OBJECT
public:
/**
* The default constructor for a ScriptEngine
*
* @param applet the Applet object that will house the plasmoid
**/
explicit ScriptEngine(QObject *parent, const QStringList &args);
virtual ~ScriptEngine();
void init(Applet* applet);
/**
* @return a list of all supported languages
**/
static QStringList knownLanguages();
/**
* Loads a script engine for the given language.
*
* @param language the language to load an engine for
* @param applet the applet for this plasmoid
* @return pointer to the ScriptEngine or 0 on failure
**/
static ScriptEngine* load(const QString &language, Applet *applet);
protected:
/**
* Called when the script should set up the script environment and
* start the script itself
**/
virtual bool init();
/**
* @return absolute path to the main script file for this plasmoid
**/
QString mainScript() const;
/**
* @return the Package associated with this plasmoid which can
* be used to request resources, such as images and
* interface files.
*/
const Package* package() const;
/**
* @return the Applet that contains this plasmoid
**/
Applet* applet() const;
private:
class Private;
Private * const d;
};
#define K_EXPORT_PLASMA_SCRIPENGINE(libname, classname) \
K_EXPORT_COMPONENT_FACTORY( \
plasma_scriptengine_##libname, \
KGenericFactory<classname>("plasma_scriptengine_" #libname))
} // namespace Plasma
#endif