2007-02-28 01:41:09 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2007-02-28 01:41:09 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-08-06 13:20:02 +02:00
|
|
|
* 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.
|
2007-02-28 01:41:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include "abstractrunner.h"
|
2008-01-30 04:41:36 +01:00
|
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
2007-05-21 16:28:03 +02:00
|
|
|
|
2007-10-06 00:21:25 +02:00
|
|
|
#include <KDebug>
|
2008-02-04 05:41:40 +01:00
|
|
|
#include <KPluginInfo>
|
2007-10-31 05:44:09 +01:00
|
|
|
#include <KServiceTypeTrader>
|
2008-04-13 16:37:10 +02:00
|
|
|
#include <KStandardDirs>
|
2008-02-04 05:41:40 +01:00
|
|
|
#include <QTimer>
|
2007-02-28 01:41:09 +01:00
|
|
|
|
2008-04-13 16:37:10 +02:00
|
|
|
#include "package.h"
|
2008-02-04 05:41:40 +01:00
|
|
|
#include "scripting/runnerscript.h"
|
2008-01-30 04:41:36 +01:00
|
|
|
#include "searchcontext.h"
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-10-31 05:44:09 +01:00
|
|
|
class AbstractRunner::Private
|
|
|
|
{
|
2008-04-13 16:37:10 +02:00
|
|
|
public:
|
2008-02-04 05:41:40 +01:00
|
|
|
Private(AbstractRunner* r, KService::Ptr service)
|
2008-01-08 01:21:10 +01:00
|
|
|
: priority(NormalPriority),
|
|
|
|
speed(NormalSpeed),
|
2008-02-04 05:41:40 +01:00
|
|
|
script(0),
|
|
|
|
runnerDescription(service),
|
2008-02-09 15:59:43 +01:00
|
|
|
runner(r),
|
2008-04-13 16:37:10 +02:00
|
|
|
fastRuns(0),
|
|
|
|
package(0)
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
|
|
|
if (runnerDescription.isValid()) {
|
2008-04-29 18:24:14 +02:00
|
|
|
const QString api = runnerDescription.property("X-Plasma-API").toString();
|
|
|
|
if (!api.isEmpty()) {
|
2008-04-13 16:37:10 +02:00
|
|
|
const QString path = KStandardDirs::locate("data",
|
|
|
|
"plasma/runners/" + runnerDescription.pluginName() + "/");
|
2008-04-29 18:24:14 +02:00
|
|
|
PackageStructure::Ptr structure = Plasma::packageStructure(api, Plasma::RunnerComponent);
|
2008-04-13 16:37:10 +02:00
|
|
|
structure->setPath(path);
|
|
|
|
package = new Package(path, structure);
|
|
|
|
|
2008-04-29 18:24:14 +02:00
|
|
|
script = Plasma::loadScriptEngine(api, runner);
|
2008-02-04 05:41:40 +01:00
|
|
|
if (!script) {
|
2008-04-29 18:24:14 +02:00
|
|
|
kDebug() << "Could not create a" << api << "ScriptEngine for the"
|
2008-02-04 05:41:40 +01:00
|
|
|
<< runnerDescription.name() << "Runner.";
|
2008-04-13 16:37:10 +02:00
|
|
|
delete package;
|
|
|
|
package = 0;
|
2008-02-04 05:41:40 +01:00
|
|
|
} else {
|
|
|
|
QTimer::singleShot(0, runner, SLOT(init()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-30 07:17:13 +01:00
|
|
|
|
2008-04-26 14:41:16 +02:00
|
|
|
bool hasRunOptions;
|
2008-04-13 16:37:10 +02:00
|
|
|
bool hasConfig;
|
|
|
|
Priority priority;
|
|
|
|
Speed speed;
|
|
|
|
RunnerScript* script;
|
|
|
|
KPluginInfo runnerDescription;
|
|
|
|
AbstractRunner* runner;
|
|
|
|
QTime runtime;
|
|
|
|
int fastRuns;
|
|
|
|
Package *package;
|
|
|
|
|
2008-02-04 21:28:09 +01:00
|
|
|
static QMutex bigLock;
|
2007-10-31 05:44:09 +01:00
|
|
|
};
|
|
|
|
|
2008-02-04 21:28:09 +01:00
|
|
|
QMutex AbstractRunner::Private::bigLock;
|
2008-01-30 07:17:13 +01:00
|
|
|
|
2008-04-13 16:37:10 +02:00
|
|
|
AbstractRunner::AbstractRunner(QObject* parent, const QString& serviceId)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private(this, KService::serviceByStorageId(serviceId)))
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractRunner::AbstractRunner(QObject* parent, const QVariantList& args)
|
2007-10-31 05:44:09 +01:00
|
|
|
: QObject(parent),
|
2008-02-04 05:41:40 +01:00
|
|
|
d(new Private(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString())))
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractRunner::~AbstractRunner()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-12-11 17:53:40 +01:00
|
|
|
KConfigGroup AbstractRunner::config() const
|
|
|
|
{
|
|
|
|
QString group = objectName();
|
|
|
|
if (group.isEmpty()) {
|
|
|
|
group = "UnnamedRunner";
|
|
|
|
}
|
|
|
|
|
2007-12-11 23:30:05 +01:00
|
|
|
KConfigGroup runners(KGlobal::config(), "Runners");
|
|
|
|
return KConfigGroup(&runners, group);
|
2007-12-11 17:53:40 +01:00
|
|
|
}
|
|
|
|
|
2007-12-02 08:11:50 +01:00
|
|
|
void AbstractRunner::performMatch( Plasma::SearchContext &globalContext )
|
|
|
|
{
|
2008-02-09 15:59:43 +01:00
|
|
|
static const int reasonableRunTime = 1500;
|
|
|
|
static const int fastEnoughTime = 250;
|
|
|
|
|
|
|
|
d->runtime.restart();
|
2008-02-11 07:04:20 +01:00
|
|
|
SearchContext localContext(0, globalContext);
|
2008-02-09 15:59:43 +01:00
|
|
|
|
|
|
|
match(&localContext);
|
|
|
|
|
|
|
|
// automatically rate limit runners that become slooow
|
|
|
|
const int runtime = d->runtime.elapsed();
|
|
|
|
bool slowed = speed() == SlowSpeed;
|
|
|
|
|
|
|
|
if (!slowed && runtime > reasonableRunTime) {
|
|
|
|
// we punish runners that return too slowly, even if they don't bring
|
|
|
|
// back matches
|
2008-04-26 14:20:29 +02:00
|
|
|
kDebug() << name() << "runner is too slow, putting it on the back burner.";
|
2008-02-09 15:59:43 +01:00
|
|
|
d->fastRuns = 0;
|
|
|
|
setSpeed(SlowSpeed);
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
|
|
|
|
//If matches were not added, delete items on the heap
|
2008-04-24 17:37:12 +02:00
|
|
|
if (localContext.moveMatchesTo(globalContext) &&
|
2008-02-11 07:04:20 +01:00
|
|
|
slowed && runtime < fastEnoughTime) {
|
2008-02-09 15:59:43 +01:00
|
|
|
++d->fastRuns;
|
|
|
|
|
|
|
|
if (d->fastRuns > 2) {
|
|
|
|
// we reward slowed runners who bring back matches fast enough
|
|
|
|
// 3 times in a row
|
2008-04-26 14:20:29 +02:00
|
|
|
kDebug() << name() << "runner is faster than we thought, kicking it up a notch";
|
2008-02-09 15:59:43 +01:00
|
|
|
setSpeed(NormalSpeed);
|
|
|
|
}
|
2007-12-02 08:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-26 14:41:16 +02:00
|
|
|
bool AbstractRunner::hasRunOptions()
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
2008-04-26 14:41:16 +02:00
|
|
|
return d->hasRunOptions;
|
2007-10-31 05:44:09 +01:00
|
|
|
}
|
|
|
|
|
2008-04-26 14:41:16 +02:00
|
|
|
void AbstractRunner::setHasRunOptions(bool hasRunOptions)
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
2008-04-26 14:41:16 +02:00
|
|
|
d->hasRunOptions = hasRunOptions;
|
2007-10-31 05:44:09 +01:00
|
|
|
}
|
|
|
|
|
2008-04-26 14:54:25 +02:00
|
|
|
void AbstractRunner::createRunOptions(QWidget *parent)
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
}
|
|
|
|
|
2007-11-22 10:08:58 +01:00
|
|
|
bool AbstractRunner::isConfigurable()
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
|
|
|
return d->hasConfig;
|
|
|
|
}
|
|
|
|
|
2007-11-22 10:08:58 +01:00
|
|
|
void AbstractRunner::setIsConfigurable(bool hasConfig)
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
|
|
|
d->hasConfig = hasConfig;
|
|
|
|
}
|
|
|
|
|
2007-10-31 06:25:07 +01:00
|
|
|
void AbstractRunner::createConfigurationInterface(QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
}
|
|
|
|
|
2007-12-10 17:20:46 +01:00
|
|
|
AbstractRunner::Speed AbstractRunner::speed() const
|
|
|
|
{
|
|
|
|
return d->speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractRunner::setSpeed(Speed speed)
|
|
|
|
{
|
|
|
|
d->speed = speed;
|
|
|
|
}
|
|
|
|
|
2008-01-08 01:21:10 +01:00
|
|
|
AbstractRunner::Priority AbstractRunner::priority() const
|
|
|
|
{
|
|
|
|
return d->priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractRunner::setPriority(Priority priority)
|
|
|
|
{
|
|
|
|
d->priority = priority;
|
|
|
|
}
|
|
|
|
|
2008-01-30 04:41:36 +01:00
|
|
|
KService::List AbstractRunner::serviceQuery(const QString &serviceType, const QString &constraint) const
|
|
|
|
{
|
2008-02-04 21:28:09 +01:00
|
|
|
QMutexLocker lock(&Private::bigLock);
|
2008-01-30 04:41:36 +01:00
|
|
|
return KServiceTypeTrader::self()->query(serviceType, constraint);
|
|
|
|
}
|
|
|
|
|
2008-02-19 07:27:31 +01:00
|
|
|
QMutex* AbstractRunner::bigLock() const
|
2008-02-04 21:28:09 +01:00
|
|
|
{
|
2008-02-19 07:27:31 +01:00
|
|
|
return &Private::bigLock;
|
2008-02-04 21:28:09 +01:00
|
|
|
}
|
|
|
|
|
2008-04-28 19:11:12 +02:00
|
|
|
void AbstractRunner::run(const Plasma::SearchContext *search, const Plasma::SearchMatch *action)
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
2008-02-04 05:41:40 +01:00
|
|
|
if (d->script) {
|
2008-04-28 19:11:12 +02:00
|
|
|
return d->script->run(search, action);
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractRunner::match(Plasma::SearchContext *search)
|
|
|
|
{
|
|
|
|
if (d->script) {
|
|
|
|
return d->script->match(search);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-26 14:20:29 +02:00
|
|
|
QString AbstractRunner::name() const
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
|
|
|
if (!d->runnerDescription.isValid()) {
|
2008-02-08 20:21:25 +01:00
|
|
|
return objectName();
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
|
|
|
return d->runnerDescription.property("X-Plasma-RunnerName").toString();
|
|
|
|
}
|
|
|
|
|
2008-04-13 16:37:10 +02:00
|
|
|
const Package* AbstractRunner::package() const
|
|
|
|
{
|
|
|
|
return d->package;
|
|
|
|
}
|
|
|
|
|
2008-02-04 05:41:40 +01:00
|
|
|
void AbstractRunner::init()
|
|
|
|
{
|
|
|
|
if (d->script) {
|
|
|
|
d->script->init();
|
|
|
|
}
|
2007-02-28 04:55:22 +01:00
|
|
|
}
|
|
|
|
|
2008-04-26 15:10:37 +02:00
|
|
|
AbstractRunner::List AbstractRunner::loadAll(QObject* parent, const QStringList& whitelist)
|
2007-03-02 01:06:52 +01:00
|
|
|
{
|
2007-10-09 05:20:02 +02:00
|
|
|
List firstRunners;
|
2007-03-02 01:06:52 +01:00
|
|
|
List runners;
|
2007-10-09 05:20:02 +02:00
|
|
|
List lastRunners;
|
|
|
|
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner");
|
2007-08-29 05:06:48 +02:00
|
|
|
QString error;
|
2008-04-26 18:19:00 +02:00
|
|
|
foreach (const KService::Ptr &service, offers) {
|
2008-02-08 20:21:25 +01:00
|
|
|
if (whitelist.empty() || whitelist.contains(service->name())) {
|
2008-04-29 18:24:14 +02:00
|
|
|
QString api = service->property("X-Plasma-API").toString();
|
2008-02-08 20:21:25 +01:00
|
|
|
AbstractRunner* runner = 0;
|
|
|
|
|
2008-04-29 18:24:14 +02:00
|
|
|
if (api.isEmpty()) {
|
2008-02-08 20:21:25 +01:00
|
|
|
QVariantList args;
|
|
|
|
args << service->storageId();
|
|
|
|
runner = service->createInstance<AbstractRunner>(parent, args, &error);
|
2008-02-04 05:41:40 +01:00
|
|
|
} else {
|
|
|
|
runner = new AbstractRunner(parent, service->storageId());
|
|
|
|
}
|
2008-02-08 20:21:25 +01:00
|
|
|
|
2007-11-22 10:08:58 +01:00
|
|
|
if (runner) {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "loaded runner : " << service->name();
|
2007-11-22 10:08:58 +01:00
|
|
|
QString phase = service->property("X-Plasma-RunnerPhase").toString();
|
|
|
|
if (phase == "last") {
|
|
|
|
lastRunners.append(runner);
|
|
|
|
} else if (phase == "first") {
|
|
|
|
firstRunners.append(runner);
|
|
|
|
} else {
|
|
|
|
runners.append(runner);
|
|
|
|
}
|
2008-02-08 20:21:25 +01:00
|
|
|
} else {
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << "failed to load runner : " << service->name() << ". error reported: " << error;
|
2007-10-09 05:20:02 +02:00
|
|
|
}
|
2007-03-02 01:06:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-09 05:20:02 +02:00
|
|
|
firstRunners << runners << lastRunners;
|
|
|
|
return firstRunners;
|
2007-03-02 01:06:52 +01:00
|
|
|
}
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-03-20 18:37:44 +01:00
|
|
|
#include "abstractrunner.moc"
|