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-05-02 20:46:05 +02:00
|
|
|
#include <plasma/querymatch.h>
|
2008-05-01 09:51:59 +02:00
|
|
|
#include <plasma/package.h>
|
|
|
|
|
2008-02-04 05:41:40 +01:00
|
|
|
#include "scripting/runnerscript.h"
|
2008-05-01 09:51:59 +02:00
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
#include "runnercontext.h"
|
2008-01-30 04:41:36 +01:00
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class AbstractRunnerPrivate
|
2007-10-31 05:44:09 +01:00
|
|
|
{
|
2008-04-13 16:37:10 +02:00
|
|
|
public:
|
2008-10-12 00:48:38 +02:00
|
|
|
AbstractRunnerPrivate(AbstractRunner *r, KService::Ptr service)
|
2008-07-01 20:56:43 +02:00
|
|
|
: priority(AbstractRunner::NormalPriority),
|
|
|
|
speed(AbstractRunner::NormalSpeed),
|
2008-05-01 09:51:59 +02:00
|
|
|
blackListed(0),
|
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",
|
2008-05-09 07:32:15 +02:00
|
|
|
"plasma/runners/" + runnerDescription.pluginName() + '/');
|
2008-10-12 00:48:38 +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-05-09 05:49:11 +02:00
|
|
|
kDebug() << "Could not create a(n)" << api << "ScriptEngine for the"
|
2008-05-06 22:48:44 +02: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-08-23 18:37:04 +02:00
|
|
|
~AbstractRunnerPrivate()
|
|
|
|
{
|
2008-09-25 18:27:49 +02:00
|
|
|
delete script;
|
|
|
|
script = 0;
|
2008-08-23 18:37:04 +02:00
|
|
|
delete package;
|
2008-09-25 18:27:49 +02:00
|
|
|
package = 0;
|
2008-08-23 18:37:04 +02:00
|
|
|
}
|
|
|
|
|
2008-04-26 14:41:16 +02:00
|
|
|
bool hasRunOptions;
|
2008-04-13 16:37:10 +02:00
|
|
|
bool hasConfig;
|
2008-07-01 20:56:43 +02:00
|
|
|
AbstractRunner::Priority priority;
|
|
|
|
AbstractRunner::Speed speed;
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext::Types blackListed;
|
2008-10-12 00:48:38 +02:00
|
|
|
RunnerScript *script;
|
2008-04-13 16:37:10 +02:00
|
|
|
KPluginInfo runnerDescription;
|
2008-10-12 00:48:38 +02:00
|
|
|
AbstractRunner *runner;
|
2008-04-13 16:37:10 +02:00
|
|
|
QTime runtime;
|
|
|
|
int fastRuns;
|
|
|
|
Package *package;
|
2007-10-31 05:44:09 +01:00
|
|
|
};
|
|
|
|
|
2008-05-30 19:04:05 +02:00
|
|
|
K_GLOBAL_STATIC(QMutex, s_bigLock)
|
2008-01-30 07:17:13 +01:00
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
AbstractRunner::AbstractRunner(QObject *parent, const QString &serviceId)
|
2008-05-06 22:48:44 +02:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(serviceId)))
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
AbstractRunner::AbstractRunner(QObject *parent, const QVariantList &args)
|
2007-10-31 05:44:09 +01:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new AbstractRunnerPrivate(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
|
|
|
}
|
|
|
|
|
2008-05-19 19:02:15 +02:00
|
|
|
void AbstractRunner::reloadConfiguration()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
void AbstractRunner::performMatch(Plasma::RunnerContext &globalContext)
|
2007-12-02 08:11:50 +01:00
|
|
|
{
|
2008-02-09 15:59:43 +01:00
|
|
|
static const int reasonableRunTime = 1500;
|
|
|
|
static const int fastEnoughTime = 250;
|
|
|
|
|
|
|
|
d->runtime.restart();
|
2008-05-01 09:51:59 +02:00
|
|
|
//TODO :this is a copy ctor
|
2008-06-14 03:30:39 +02:00
|
|
|
RunnerContext localContext(globalContext, 0);
|
2008-05-09 05:49:11 +02:00
|
|
|
match(localContext);
|
2008-02-09 15:59:43 +01:00
|
|
|
// 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-05-06 20:43:55 +02:00
|
|
|
kDebug() << id() << "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-05-01 09:51:59 +02:00
|
|
|
if (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-05-06 20:43:55 +02:00
|
|
|
kDebug() << id() << "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-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-05-01 20:40:10 +02:00
|
|
|
RunnerContext::Types AbstractRunner::ignoredTypes() const
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
return d->blackListed;
|
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
void AbstractRunner::setIgnoredTypes(RunnerContext::Types types)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
d->blackListed = types;
|
|
|
|
}
|
|
|
|
|
2008-01-30 04:41:36 +01:00
|
|
|
KService::List AbstractRunner::serviceQuery(const QString &serviceType, const QString &constraint) const
|
|
|
|
{
|
2008-05-30 19:04:05 +02:00
|
|
|
QMutexLocker lock(s_bigLock);
|
2008-01-30 04:41:36 +01:00
|
|
|
return KServiceTypeTrader::self()->query(serviceType, constraint);
|
|
|
|
}
|
2008-10-12 00:48:38 +02:00
|
|
|
|
|
|
|
QMutex *AbstractRunner::bigLock() const
|
2008-02-04 21:28:09 +01:00
|
|
|
{
|
2008-05-30 19:04:05 +02:00
|
|
|
return s_bigLock;
|
2008-02-04 21:28:09 +01:00
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
void AbstractRunner::run(const Plasma::RunnerContext &search, const Plasma::QueryMatch &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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
void AbstractRunner::match(Plasma::RunnerContext &search)
|
2008-02-04 05:41:40 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2008-05-06 20:43:55 +02:00
|
|
|
return d->runnerDescription.name();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AbstractRunner::id() const
|
|
|
|
{
|
|
|
|
if (!d->runnerDescription.isValid()) {
|
|
|
|
return objectName();
|
|
|
|
}
|
|
|
|
return d->runnerDescription.pluginName();
|
2008-02-04 05:41:40 +01:00
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
QString AbstractRunner::description() const
|
|
|
|
{
|
|
|
|
if (!d->runnerDescription.isValid()) {
|
|
|
|
return objectName();
|
|
|
|
}
|
|
|
|
return d->runnerDescription.property("Comment").toString();
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
const Package *AbstractRunner::package() const
|
2008-04-13 16:37:10 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-03-02 06:27:33 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-03-20 18:37:44 +01:00
|
|
|
#include "abstractrunner.moc"
|