some more win32'fication to fix non-ascii filename handling
[kdelibs.git] / plasma / abstractrunner.cpp
blob030b9cc36f8f54541fc570301cb46e463553ccbf
1 /*
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"
22 #include <QAction>
23 #include <QHash>
24 #include <QMutex>
25 #include <QMutexLocker>
26 #include <QTimer>
28 #include <kdebug.h>
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"
40 namespace Plasma
43 class AbstractRunnerPrivate
45 public:
46 AbstractRunnerPrivate(AbstractRunner *r, KService::Ptr service)
47 : priority(AbstractRunner::NormalPriority),
48 speed(AbstractRunner::NormalSpeed),
49 blackListed(0),
50 script(0),
51 runnerDescription(service),
52 runner(r),
53 fastRuns(0),
54 package(0)
56 if (runnerDescription.isValid()) {
57 const QString api = runnerDescription.property("X-Plasma-API").toString();
58 if (!api.isEmpty()) {
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);
67 if (!script) {
68 kDebug() << "Could not create a(n)" << api << "ScriptEngine for the"
69 << runnerDescription.name() << "Runner.";
70 delete package;
71 package = 0;
72 } else {
73 QTimer::singleShot(0, runner, SLOT(init()));
79 ~AbstractRunnerPrivate()
81 delete script;
82 script = 0;
83 delete package;
84 package = 0;
87 bool hasRunOptions;
88 bool hasConfig;
89 AbstractRunner::Priority priority;
90 AbstractRunner::Speed speed;
91 RunnerContext::Types blackListed;
92 RunnerScript *script;
93 KPluginInfo runnerDescription;
94 AbstractRunner *runner;
95 QTime runtime;
96 int fastRuns;
97 Package *package;
98 QHash<QString, QAction*> actions;
101 K_GLOBAL_STATIC(QMutex, s_bigLock)
103 AbstractRunner::AbstractRunner(QObject *parent, const QString &serviceId)
104 : QObject(parent),
105 d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(serviceId)))
109 AbstractRunner::AbstractRunner(QObject *parent, const QVariantList &args)
110 : QObject(parent),
111 d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString())))
115 AbstractRunner::~AbstractRunner()
117 delete d;
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
143 match(localContext);
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
151 // back matches
152 kDebug() << id() << "runner is too slow, putting it on the back burner.";
153 d->fastRuns = 0;
154 setSpeed(SlowSpeed);
157 if (slowed && runtime < fastEnoughTime && localContext.query().size() > 2) {
158 ++d->fastRuns;
160 if (d->fastRuns > 2) {
161 // we reward slowed runners who bring back matches fast enough
162 // 3 times in a row
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)
171 Q_UNUSED(match)
172 QList<QAction*> ret;
173 return ret;
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);
180 return 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);
191 delete a;
194 QAction* AbstractRunner::action(const QString &id) const
196 return d->actions.value(id);
199 QHash<QString, QAction*> AbstractRunner::actions() const
201 return d->actions;
204 void AbstractRunner::clearActions()
206 qDeleteAll(d->actions);
207 d->actions.clear();
210 bool AbstractRunner::hasRunOptions()
212 return d->hasRunOptions;
215 void AbstractRunner::setHasRunOptions(bool hasRunOptions)
217 d->hasRunOptions = hasRunOptions;
220 void AbstractRunner::createRunOptions(QWidget *parent)
222 Q_UNUSED(parent)
225 AbstractRunner::Speed AbstractRunner::speed() const
227 return d->speed;
230 void AbstractRunner::setSpeed(Speed speed)
232 d->speed = speed;
235 AbstractRunner::Priority AbstractRunner::priority() const
237 return d->priority;
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()
262 return s_bigLock;
265 void AbstractRunner::run(const Plasma::RunnerContext &search, const Plasma::QueryMatch &action)
267 if (d->script) {
268 return d->script->run(search, action);
272 void AbstractRunner::match(Plasma::RunnerContext &search)
274 if (d->script) {
275 return d->script->match(search);
279 QString AbstractRunner::name() const
281 if (!d->runnerDescription.isValid()) {
282 return objectName();
284 return d->runnerDescription.name();
287 QString AbstractRunner::id() const
289 if (!d->runnerDescription.isValid()) {
290 return objectName();
292 return d->runnerDescription.pluginName();
295 QString AbstractRunner::description() const
297 if (!d->runnerDescription.isValid()) {
298 return objectName();
300 return d->runnerDescription.property("Comment").toString();
303 const Package* AbstractRunner::package() const
305 return d->package;
308 void AbstractRunner::init()
310 if (d->script) {
311 d->script->init();
315 } // Plasma namespace
317 #include "abstractrunner.moc"