2 * Copyright 2006-2007 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 "abstractrunner.h"
25 #include <QMutexLocker>
29 #include <kplugininfo.h>
30 #include <kservicetypetrader.h>
31 #include <kstandarddirs.h>
33 #include <plasma/querymatch.h>
34 #include <plasma/package.h>
36 #include "scripting/runnerscript.h"
38 #include "runnercontext.h"
43 class AbstractRunnerPrivate
46 AbstractRunnerPrivate(AbstractRunner
*r
, KService::Ptr service
)
47 : priority(AbstractRunner::NormalPriority
),
48 speed(AbstractRunner::NormalSpeed
),
51 runnerDescription(service
),
56 if (runnerDescription
.isValid()) {
57 const QString api
= runnerDescription
.property("X-Plasma-API").toString();
59 const QString path
= KStandardDirs::locate("data",
60 "plasma/runners/" + runnerDescription
.pluginName() + '/');
61 PackageStructure::Ptr structure
=
62 Plasma::packageStructure(api
, Plasma::RunnerComponent
);
63 structure
->setPath(path
);
64 package
= new Package(path
, structure
);
66 script
= Plasma::loadScriptEngine(api
, runner
);
68 kDebug() << "Could not create a(n)" << api
<< "ScriptEngine for the"
69 << runnerDescription
.name() << "Runner.";
73 QTimer::singleShot(0, runner
, SLOT(init()));
79 ~AbstractRunnerPrivate()
89 AbstractRunner::Priority priority
;
90 AbstractRunner::Speed speed
;
91 RunnerContext::Types blackListed
;
93 KPluginInfo runnerDescription
;
94 AbstractRunner
*runner
;
98 QHash
<QString
, QAction
*> actions
;
101 K_GLOBAL_STATIC(QMutex
, s_bigLock
)
103 AbstractRunner::AbstractRunner(QObject
*parent
, const QString
&serviceId
)
105 d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(serviceId
)))
109 AbstractRunner::AbstractRunner(QObject
*parent
, const QVariantList
&args
)
111 d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(args
.count() > 0 ? args
[0].toString() : QString())))
115 AbstractRunner::~AbstractRunner()
120 KConfigGroup
AbstractRunner::config() const
122 QString group
= objectName();
123 if (group
.isEmpty()) {
124 group
= "UnnamedRunner";
127 KConfigGroup
runners(KGlobal::config(), "Runners");
128 return KConfigGroup(&runners
, group
);
131 void AbstractRunner::reloadConfiguration()
135 void AbstractRunner::performMatch(Plasma::RunnerContext
&localContext
)
137 static const int reasonableRunTime
= 1500;
138 static const int fastEnoughTime
= 250;
140 d
->runtime
.restart();
142 //The local copy is already obtained in the job
145 // automatically rate limit runners that become slooow
146 const int runtime
= d
->runtime
.elapsed();
147 bool slowed
= speed() == SlowSpeed
;
149 if (!slowed
&& runtime
> reasonableRunTime
) {
150 // we punish runners that return too slowly, even if they don't bring
152 kDebug() << id() << "runner is too slow, putting it on the back burner.";
157 if (slowed
&& runtime
< fastEnoughTime
&& localContext
.query().size() > 2) {
160 if (d
->fastRuns
> 2) {
161 // we reward slowed runners who bring back matches fast enough
163 kDebug() << id() << "runner is faster than we thought, kicking it up a notch";
164 setSpeed(NormalSpeed
);
169 QList
<QAction
*> AbstractRunner::actionsForMatch(const Plasma::QueryMatch
&match
)
176 QAction
* AbstractRunner::addAction(const QString
&id
, const QIcon
&icon
, const QString
&text
)
178 QAction
*a
= new QAction(icon
, text
, this);
179 d
->actions
.insert(id
, a
);
183 void AbstractRunner::addAction(const QString
&id
, QAction
*action
)
185 d
->actions
.insert(id
, action
);
188 void AbstractRunner::removeAction(const QString
&id
)
190 QAction
*a
= d
->actions
.take(id
);
194 QAction
* AbstractRunner::action(const QString
&id
) const
196 return d
->actions
.value(id
);
199 QHash
<QString
, QAction
*> AbstractRunner::actions() const
204 void AbstractRunner::clearActions()
206 qDeleteAll(d
->actions
);
210 bool AbstractRunner::hasRunOptions()
212 return d
->hasRunOptions
;
215 void AbstractRunner::setHasRunOptions(bool hasRunOptions
)
217 d
->hasRunOptions
= hasRunOptions
;
220 void AbstractRunner::createRunOptions(QWidget
*parent
)
225 AbstractRunner::Speed
AbstractRunner::speed() const
230 void AbstractRunner::setSpeed(Speed speed
)
235 AbstractRunner::Priority
AbstractRunner::priority() const
240 void AbstractRunner::setPriority(Priority priority
)
242 d
->priority
= priority
;
245 RunnerContext::Types
AbstractRunner::ignoredTypes() const
247 return d
->blackListed
;
250 void AbstractRunner::setIgnoredTypes(RunnerContext::Types types
)
252 d
->blackListed
= types
;
255 KService::List
AbstractRunner::serviceQuery(const QString
&serviceType
, const QString
&constraint
) const
257 return KServiceTypeTrader::self()->query(serviceType
, constraint
);
260 QMutex
* AbstractRunner::bigLock()
265 void AbstractRunner::run(const Plasma::RunnerContext
&search
, const Plasma::QueryMatch
&action
)
268 return d
->script
->run(search
, action
);
272 void AbstractRunner::match(Plasma::RunnerContext
&search
)
275 return d
->script
->match(search
);
279 QString
AbstractRunner::name() const
281 if (!d
->runnerDescription
.isValid()) {
284 return d
->runnerDescription
.name();
287 QString
AbstractRunner::id() const
289 if (!d
->runnerDescription
.isValid()) {
292 return d
->runnerDescription
.pluginName();
295 QString
AbstractRunner::description() const
297 if (!d
->runnerDescription
.isValid()) {
300 return d
->runnerDescription
.property("Comment").toString();
303 const Package
* AbstractRunner::package() const
308 void AbstractRunner::init()
315 } // Plasma namespace
317 #include "abstractrunner.moc"