BINARY INCOMPATIBLE CHANGE
* create a subdir for our scripting classes * break ScriptEngine into two: a base class and AppletScript * add stub classes for RunnerScript and DataEngineScript * provide generic loading methods for all three svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=765971
This commit is contained in:
parent
b39fa0c7db
commit
0e0d1122ca
@ -45,7 +45,6 @@ set(plasma_LIB_SRCS
|
||||
phase.cpp
|
||||
plasma.cpp
|
||||
plasma_export.h
|
||||
scriptengine.cpp
|
||||
searchmatch.cpp
|
||||
searchcontext.cpp
|
||||
shadowitem.cpp
|
||||
@ -54,6 +53,10 @@ set(plasma_LIB_SRCS
|
||||
desktoptoolbox.cpp
|
||||
uiloader.cpp
|
||||
view.cpp
|
||||
scripting/appletscript.cpp
|
||||
scripting/dataenginescript.cpp
|
||||
scripting/runnerscript.cpp
|
||||
scripting/scriptengine.cpp
|
||||
widgets/checkbox.cpp
|
||||
widgets/flash.cpp
|
||||
widgets/icon.cpp
|
||||
@ -118,7 +121,10 @@ set(plasma_LIB_INCLUDES
|
||||
phase.h
|
||||
plasma.h
|
||||
plasma_export.h
|
||||
scriptengine.h
|
||||
scripting/appletscript.h
|
||||
scripting/dataenginescript.h
|
||||
scripting/runnerscript.h
|
||||
scripting/scriptengine.h
|
||||
searchmatch.h
|
||||
searchcontext.h
|
||||
shadowitem_p.h
|
||||
@ -166,6 +172,14 @@ install(FILES
|
||||
layouts/layoutitem.h
|
||||
DESTINATION ${INCLUDE_INSTALL_DIR}/plasma/layouts)
|
||||
|
||||
install(FILES
|
||||
scripting/appletscript.h
|
||||
scripting/dataenginescript.h
|
||||
scripting/runnerscript.h
|
||||
scripting/scriptengine.h
|
||||
DESTINATION ${INCLUDE_INSTALL_DIR}/plasma/scripting)
|
||||
|
||||
|
||||
install(FILES
|
||||
includes/AbstractRunner
|
||||
includes/Animator
|
||||
|
158
scriptengine.cpp
158
scriptengine.cpp
@ -1,158 +0,0 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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)
|
||||
: QObject(parent),
|
||||
d(new Private)
|
||||
{
|
||||
d->applet = 0;
|
||||
}
|
||||
|
||||
ScriptEngine::~ScriptEngine()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void ScriptEngine::init(Applet* applet)
|
||||
{
|
||||
d->applet = applet;
|
||||
if (!init()) {
|
||||
d->applet->setFailedToLaunch(true);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::paintInterface(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRect &contentsRect)
|
||||
{
|
||||
Q_UNUSED(painter)
|
||||
Q_UNUSED(option)
|
||||
Q_UNUSED(contentsRect)
|
||||
}
|
||||
|
||||
QSizeF ScriptEngine::size() const
|
||||
{
|
||||
if (applet()) {
|
||||
return applet()->contentSize();
|
||||
}
|
||||
|
||||
return QSizeF();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
DataEngine* ScriptEngine::dataEngine(const QString &engine) const
|
||||
{
|
||||
Q_ASSERT(d->applet);
|
||||
return d->applet->dataEngine(engine);
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
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 << "\"";
|
||||
return 0;
|
||||
}
|
||||
|
||||
QVariantList args;
|
||||
QString error;
|
||||
ScriptEngine* engine = 0;
|
||||
foreach (KService::Ptr service, offers) {
|
||||
engine = service->createInstance<Plasma::ScriptEngine>(applet, args, &error);
|
||||
|
||||
if (engine) {
|
||||
break;
|
||||
}
|
||||
|
||||
kDebug() << "Couldn't load script engine for language " << language << "! error reported: " << error;
|
||||
}
|
||||
|
||||
if (!engine) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
engine->init(applet);
|
||||
return engine;
|
||||
}
|
||||
|
||||
} // namespace Plasma
|
||||
|
||||
#include <scriptengine.moc>
|
||||
|
93
scripting/appletscript.cpp
Normal file
93
scripting/appletscript.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 "scripting/appletscript.h"
|
||||
|
||||
#include "applet.h"
|
||||
#include "package.h"
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class AppletScript::Private
|
||||
{
|
||||
public:
|
||||
Applet* applet;
|
||||
};
|
||||
|
||||
AppletScript::AppletScript(QObject *parent)
|
||||
: ScriptEngine(parent),
|
||||
d(new Private)
|
||||
{
|
||||
d->applet = 0;
|
||||
}
|
||||
|
||||
AppletScript::~AppletScript()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void AppletScript::setApplet(Plasma::Applet *applet)
|
||||
{
|
||||
d->applet = applet;
|
||||
}
|
||||
|
||||
Applet* AppletScript::applet() const
|
||||
{
|
||||
Q_ASSERT(d->applet);
|
||||
return d->applet;
|
||||
}
|
||||
|
||||
void AppletScript::paintInterface(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRect &contentsRect)
|
||||
{
|
||||
Q_UNUSED(painter)
|
||||
Q_UNUSED(option)
|
||||
Q_UNUSED(contentsRect)
|
||||
}
|
||||
|
||||
QSizeF AppletScript::size() const
|
||||
{
|
||||
if (applet()) {
|
||||
return applet()->contentSize();
|
||||
}
|
||||
|
||||
return QSizeF();
|
||||
}
|
||||
|
||||
DataEngine* AppletScript::dataEngine(const QString &engine) const
|
||||
{
|
||||
Q_ASSERT(d->applet);
|
||||
return d->applet->dataEngine(engine);
|
||||
}
|
||||
|
||||
QString AppletScript::mainScript() const
|
||||
{
|
||||
Q_ASSERT(d->applet);
|
||||
return d->applet->package()->filePath("mainscript");
|
||||
}
|
||||
|
||||
const Package* AppletScript::package() const
|
||||
{
|
||||
Q_ASSERT(d->applet);
|
||||
return d->applet->package();
|
||||
}
|
||||
|
||||
} // Plasma namespace
|
||||
|
||||
#include "appletscript.moc"
|
@ -17,10 +17,11 @@
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef PLASMA_SCRIPTENGINE_H
|
||||
#define PLASMA_SCRIPTENGINE_H
|
||||
#ifndef PLASMA_APPLETSCRIPT_H
|
||||
#define PLASMA_APPLETSCRIPT_H
|
||||
|
||||
#include <plasma/plasma_export.h>
|
||||
#include <plasma/scripting/scriptengine.h>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QRect>
|
||||
@ -32,52 +33,30 @@ class QStyleOptionGraphicsItem;
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class Applet;
|
||||
class Package;
|
||||
class DataEngine;
|
||||
|
||||
/**
|
||||
* @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 PLASMA_EXPORT ScriptEngine : public QObject
|
||||
class PLASMA_EXPORT AppletScript : public ScriptEngine
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* The default constructor for a ScriptEngine
|
||||
*
|
||||
* @param applet the Applet object that will house the plasmoid
|
||||
**/
|
||||
explicit ScriptEngine(QObject *parent);
|
||||
virtual ~ScriptEngine();
|
||||
|
||||
void init(Applet* applet);
|
||||
* Default constructor for an AppletScript.
|
||||
* Subclasses should not attempt to access the Plasma::Applet
|
||||
* associated with this AppletScript in the constructor. All
|
||||
* such set up that requires the Applet itself should be done
|
||||
* in the init() method.
|
||||
*/
|
||||
explicit AppletScript(QObject *parent = 0);
|
||||
~AppletScript();
|
||||
|
||||
/**
|
||||
* @return a list of all supported languages
|
||||
**/
|
||||
static QStringList knownLanguages();
|
||||
* Sets the applet associated with this AppletScript
|
||||
*/
|
||||
void setApplet(Plasma::Applet *applet);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
* Returns the Plasma::Applet associated with this script component
|
||||
*/
|
||||
Plasma::Applet* applet() const;
|
||||
|
||||
/**
|
||||
* Called when the script should paint the applet
|
||||
@ -85,16 +64,18 @@ public:
|
||||
* @param painter the QPainter to use
|
||||
* @param option the style option containing such flags as selection, level of detail, etc
|
||||
**/
|
||||
virtual void paintInterface(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRect &contentsRect);
|
||||
virtual void paintInterface(QPainter* painter,
|
||||
const QStyleOptionGraphicsItem* option,
|
||||
const QRect &contentsRect);
|
||||
|
||||
Q_INVOKABLE QSizeF size() const;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Called when the script should set up the script environment and
|
||||
* start the script itself
|
||||
**/
|
||||
virtual bool init();
|
||||
* @arg engine name of the engine
|
||||
* @return a data engine associated with this plasmoid
|
||||
*/
|
||||
Q_INVOKABLE DataEngine* dataEngine(const QString &engine) const;
|
||||
|
||||
/**
|
||||
* @return absolute path to the main script file for this plasmoid
|
||||
@ -108,29 +89,15 @@ protected:
|
||||
*/
|
||||
const Package* package() const;
|
||||
|
||||
/**
|
||||
* @return the Applet that contains this plasmoid
|
||||
**/
|
||||
Applet* applet() const;
|
||||
|
||||
/**
|
||||
* @return a data engine associated with this plasmoid
|
||||
*
|
||||
* @arg engine name of the engine
|
||||
*/
|
||||
Q_INVOKABLE DataEngine* dataEngine(const QString &engine) const;
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private * const d;
|
||||
};
|
||||
|
||||
#define K_EXPORT_PLASMA_SCRIPTENGINE(libname, classname) \
|
||||
#define K_EXPORT_PLASMA_APPLETSCRIPTENGINE(libname, classname) \
|
||||
K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
|
||||
K_EXPORT_PLUGIN(factory("plasma_scriptengine_" #libname))
|
||||
K_EXPORT_PLUGIN(factory("plasma_appletscriptengine_" #libname))
|
||||
|
||||
|
||||
} // namespace Plasma
|
||||
} //Plasma namespace
|
||||
|
||||
#endif
|
||||
|
56
scripting/dataenginescript.cpp
Normal file
56
scripting/dataenginescript.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 "dataenginescript.h"
|
||||
|
||||
#include "dataengine.h"
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class DataEngineScript::Private
|
||||
{
|
||||
public:
|
||||
DataEngine* dataEngine;
|
||||
};
|
||||
|
||||
DataEngineScript::DataEngineScript(QObject *parent)
|
||||
: ScriptEngine(parent),
|
||||
d(0)
|
||||
{
|
||||
}
|
||||
|
||||
DataEngineScript::~DataEngineScript()
|
||||
{
|
||||
// delete d;
|
||||
}
|
||||
|
||||
void DataEngineScript::setDataEngine(DataEngine *dataEngine)
|
||||
{
|
||||
d->dataEngine = dataEngine;
|
||||
}
|
||||
|
||||
DataEngine* DataEngineScript::dataEngine() const
|
||||
{
|
||||
return d->dataEngine;
|
||||
}
|
||||
|
||||
} // Plasma namespace
|
||||
|
||||
#include "dataenginescript.moc"
|
67
scripting/dataenginescript.h
Normal file
67
scripting/dataenginescript.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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_DATAENGINESCRIPT_H
|
||||
#define PLASMA_DATAENGINESCRIPT_H
|
||||
|
||||
#include <plasma/plasma_export.h>
|
||||
#include <plasma/scripting/scriptengine.h>
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class DataEngine;
|
||||
|
||||
class PLASMA_EXPORT DataEngineScript : public ScriptEngine
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default constructor for a DataEngineScript.
|
||||
* Subclasses should not attempt to access the Plasma::DataEngine
|
||||
* associated with this DataEngineScript in the constructor. All
|
||||
* such set up that requires the DataEngine itself should be done
|
||||
* in the init() method.
|
||||
*/
|
||||
explicit DataEngineScript(QObject *parent = 0);
|
||||
~DataEngineScript();
|
||||
|
||||
/**
|
||||
* Sets the Plasma::DataEngine associated with this DataEngineScript
|
||||
*/
|
||||
void setDataEngine(DataEngine *dataEngine);
|
||||
|
||||
/**
|
||||
* Returns the Plasma::DataEngine associated with this script component
|
||||
*/
|
||||
DataEngine* dataEngine() const;
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private * const d;
|
||||
};
|
||||
|
||||
#define K_EXPORT_PLASMA_DATAENGINESCRIPTENGINE(libname, classname) \
|
||||
K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
|
||||
K_EXPORT_PLUGIN(factory("plasma_dataenginescriptengine_" #libname))
|
||||
|
||||
} //Plasma namespace
|
||||
|
||||
#endif
|
56
scripting/runnerscript.cpp
Normal file
56
scripting/runnerscript.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 "runnerscript.h"
|
||||
|
||||
#include "abstractrunner.h"
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class RunnerScript::Private
|
||||
{
|
||||
public:
|
||||
AbstractRunner* runner;
|
||||
};
|
||||
|
||||
RunnerScript::RunnerScript(QObject *parent)
|
||||
: ScriptEngine(parent),
|
||||
d(0)
|
||||
{
|
||||
}
|
||||
|
||||
RunnerScript::~RunnerScript()
|
||||
{
|
||||
// delete d;
|
||||
}
|
||||
|
||||
void RunnerScript::setRunner(AbstractRunner *runner)
|
||||
{
|
||||
d->runner = runner;
|
||||
}
|
||||
|
||||
AbstractRunner* RunnerScript::runner() const
|
||||
{
|
||||
return d->runner;
|
||||
}
|
||||
|
||||
} // Plasma namespace
|
||||
|
||||
#include "runnerscript.moc"
|
67
scripting/runnerscript.h
Normal file
67
scripting/runnerscript.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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_RUNNERSCRIPT_H
|
||||
#define PLASMA_RUNNERSCRIPT_H
|
||||
|
||||
#include <plasma/plasma_export.h>
|
||||
#include <plasma/scripting/scriptengine.h>
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class AbstractRunner;
|
||||
|
||||
class PLASMA_EXPORT RunnerScript : public ScriptEngine
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default constructor for a RunnerScript.
|
||||
* Subclasses should not attempt to access the Plasma::AbstractRunner
|
||||
* associated with this RunnerScript in the constructor. All
|
||||
* such set up that requires the AbstractRunner itself should be done
|
||||
* in the init() method.
|
||||
*/
|
||||
explicit RunnerScript(QObject *parent = 0);
|
||||
~RunnerScript();
|
||||
|
||||
/**
|
||||
* Sets the Plasma::AbstractRunner associated with this RunnerScript
|
||||
*/
|
||||
void setRunner(AbstractRunner *runner);
|
||||
|
||||
/**
|
||||
* Returns the Plasma::AbstractRunner associated with this script component
|
||||
*/
|
||||
AbstractRunner* runner() const;
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private * const d;
|
||||
};
|
||||
|
||||
#define K_EXPORT_PLASMA_RUNNERSCRIPTENGINE(libname, classname) \
|
||||
K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
|
||||
K_EXPORT_PLUGIN(factory("plasma_runnerscriptengine_" #libname))
|
||||
|
||||
} //Plasma namespace
|
||||
|
||||
#endif
|
191
scripting/scriptengine.cpp
Normal file
191
scripting/scriptengine.cpp
Normal file
@ -0,0 +1,191 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 "scripting/scriptengine.h"
|
||||
|
||||
#include <KDebug>
|
||||
#include <KService>
|
||||
#include <KServiceTypeTrader>
|
||||
|
||||
#include "abstractrunner.h"
|
||||
#include "applet.h"
|
||||
#include "dataengine.h"
|
||||
#include "package.h"
|
||||
#include "scripting/appletscript.h"
|
||||
#include "scripting/dataenginescript.h"
|
||||
#include "scripting/runnerscript.h"
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
ScriptEngine::ScriptEngine(QObject *parent)
|
||||
: QObject(parent),
|
||||
d(0)
|
||||
{
|
||||
}
|
||||
|
||||
ScriptEngine::~ScriptEngine()
|
||||
{
|
||||
// delete d;
|
||||
}
|
||||
|
||||
bool ScriptEngine::init()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QStringList knownLanguages(ComponentTypes types)
|
||||
{
|
||||
QString constraintTemplate = "'%1' in [X-Plasma-ComponentTypes]";
|
||||
QString constraint;
|
||||
|
||||
if (types & AppletComponent) {
|
||||
// currently this if statement is not needed, but this future proofs
|
||||
// the code against someone initializing constraint to something
|
||||
// before we get here.
|
||||
if (!constraint.isEmpty()) {
|
||||
constraint.append(" or ");
|
||||
}
|
||||
|
||||
constraint.append(constraintTemplate.arg("Applet"));
|
||||
}
|
||||
|
||||
if (types & DataEngineComponent) {
|
||||
if (!constraint.isEmpty()) {
|
||||
constraint.append(" or ");
|
||||
}
|
||||
|
||||
constraint.append(constraintTemplate.arg("DataEngine"));
|
||||
}
|
||||
|
||||
if (types & RunnerComponent) {
|
||||
if (!constraint.isEmpty()) {
|
||||
constraint.append(" or ");
|
||||
}
|
||||
|
||||
constraint.append(constraintTemplate.arg("Runner"));
|
||||
}
|
||||
|
||||
KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
|
||||
//kDebug() << "Applet::knownApplets constraint was '" << constraint << "' which got us " << offers.count() << " matches";
|
||||
|
||||
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* loadEngine(const QString &language, ComponentType type, QObject *parent)
|
||||
{
|
||||
if (language.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
QRegExp re("[^a-zA-Z0-9\\-_]");
|
||||
if (re.indexIn(language) != -1) {
|
||||
kDebug() << "invalid language attempted:" << language;
|
||||
return 0;
|
||||
}
|
||||
|
||||
QString component;
|
||||
switch (type) {
|
||||
case AppletComponent:
|
||||
component = "Applet";
|
||||
break;
|
||||
case DataEngineComponent:
|
||||
component = "DataEngine";
|
||||
break;
|
||||
case RunnerComponent:
|
||||
component = "Runner";
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
|
||||
QString constraint = QString("[X-Plasma-Language] == '%1' and "
|
||||
"'%2' in [X-Plasma-ComponentTypes]").arg(language, component);
|
||||
KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
|
||||
/* kDebug() << "********************* loadingApplet with Plasma/ScriptEngine" << constraint
|
||||
<< "resulting in" << offers.count() << "results";*/
|
||||
if (offers.isEmpty()) {
|
||||
kDebug() << "ScriptEngine::load: no offers for \"" << language << "\"";
|
||||
return 0;
|
||||
}
|
||||
|
||||
QVariantList args;
|
||||
QString error;
|
||||
|
||||
ScriptEngine *engine = 0;
|
||||
foreach (KService::Ptr service, offers) {
|
||||
switch (type) {
|
||||
case AppletComponent:
|
||||
engine = service->createInstance<Plasma::AppletScript>(parent, args, &error);
|
||||
break;
|
||||
case DataEngineComponent:
|
||||
engine = service->createInstance<Plasma::DataEngineScript>(parent, args, &error);
|
||||
break;
|
||||
case RunnerComponent:
|
||||
engine = service->createInstance<Plasma::RunnerScript>(parent, args, &error);
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (engine) {
|
||||
return engine;
|
||||
}
|
||||
|
||||
kDebug() << "Couldn't load script engine for language " << language << "! error reported: " << error;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
AppletScript* loadScriptEngine(const QString &language, Applet *applet)
|
||||
{
|
||||
AppletScript *engine = static_cast<AppletScript*>(loadEngine(language, AppletComponent, applet));
|
||||
|
||||
if (engine) {
|
||||
engine->setApplet(applet);
|
||||
}
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
DataEngineScript* loadScriptEngine(const QString &language, DataEngine *dataEngine)
|
||||
{
|
||||
return static_cast<DataEngineScript*>(loadEngine(language, DataEngineComponent, dataEngine));
|
||||
}
|
||||
|
||||
RunnerScript* loadScriptEngine(const QString &language, AbstractRunner *runner)
|
||||
{
|
||||
return static_cast<RunnerScript*>(loadEngine(language, RunnerComponent, runner));
|
||||
}
|
||||
|
||||
} // namespace Plasma
|
||||
|
||||
#include <scriptengine.moc>
|
||||
|
117
scripting/scriptengine.h
Normal file
117
scripting/scriptengine.h
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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 as
|
||||
* published by the Free Software Foundation; either version 2, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 <plasma/plasma_export.h>
|
||||
#include <plasma/plasma.h>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QRect>
|
||||
#include <QtCore/QSizeF>
|
||||
|
||||
class QPainter;
|
||||
class QStyleOptionGraphicsItem;
|
||||
|
||||
namespace Plasma
|
||||
{
|
||||
|
||||
class AbstractRunner;
|
||||
class Applet;
|
||||
class AppletScript;
|
||||
class DataEngine;
|
||||
class DataEngineScript;
|
||||
class RunnerScript;
|
||||
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 scripting APIas ...
|
||||
* 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 PLASMA_EXPORT ScriptEngine : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ScriptEngine(QObject *parent = 0);
|
||||
~ScriptEngine();
|
||||
|
||||
/**
|
||||
* Called when it is safe to initialize the internal state of the engine
|
||||
*/
|
||||
virtual bool init();
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private * const d;
|
||||
};
|
||||
|
||||
/**
|
||||
* @arg types a set of ComponentTypes flags for which to look up the
|
||||
* language support for
|
||||
* @return a list of all supported languages for the given type(s).
|
||||
**/
|
||||
QStringList knownLanguages(ComponentTypes types);
|
||||
|
||||
/**
|
||||
* Loads an Applet script engine for the given language.
|
||||
*
|
||||
* @param language the language to load for
|
||||
* @param applet the Plasma::Applet for this script
|
||||
* @return pointer to the AppletScript or 0 on failure; the caller is responsible
|
||||
* for the return object which will be parented to the Applet
|
||||
**/
|
||||
AppletScript* loadScriptEngine(const QString &language, Applet *applet);
|
||||
|
||||
/**
|
||||
* Loads an DataEngine script engine for the given language.
|
||||
*
|
||||
* @param language the language to load for
|
||||
* @param dataEngine the Plasma::DataEngine for this script;
|
||||
* @return pointer to the DataEngineScript or 0 on failure; the caller is responsible
|
||||
* for the return object which will be parented to the DataEngine
|
||||
**/
|
||||
DataEngineScript* loadScriptEngine(const QString &language, DataEngine *dataEngine);
|
||||
|
||||
/**
|
||||
* Loads an Applet script engine for the given language.
|
||||
*
|
||||
* @param language the language to load for
|
||||
* @param runner the Plasma::AbstractRunner for this script
|
||||
* @return pointer to the RunnerScript or 0 on failure; the caller is responsible
|
||||
* for the return object which will be parented to the AbstractRunner
|
||||
**/
|
||||
RunnerScript* loadScriptEngine(const QString &language, AbstractRunner *runner);
|
||||
|
||||
} // namespace Plasma
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user