Plasma
abstractrunner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "abstractrunner.h"
00021
00022 #include <QAction>
00023 #include <QHash>
00024 #include <QMutex>
00025 #include <QMutexLocker>
00026 #include <QTimer>
00027
00028 #include <kdebug.h>
00029 #include <kplugininfo.h>
00030 #include <kservicetypetrader.h>
00031 #include <kstandarddirs.h>
00032
00033 #include <plasma/querymatch.h>
00034 #include <plasma/package.h>
00035
00036 #include "scripting/runnerscript.h"
00037
00038 #include "runnercontext.h"
00039
00040 namespace Plasma
00041 {
00042
00043 class AbstractRunnerPrivate
00044 {
00045 public:
00046 AbstractRunnerPrivate(AbstractRunner *r, KService::Ptr service)
00047 : priority(AbstractRunner::NormalPriority),
00048 speed(AbstractRunner::NormalSpeed),
00049 blackListed(0),
00050 script(0),
00051 runnerDescription(service),
00052 runner(r),
00053 fastRuns(0),
00054 package(0)
00055 {
00056 if (runnerDescription.isValid()) {
00057 const QString api = runnerDescription.property("X-Plasma-API").toString();
00058 if (!api.isEmpty()) {
00059 const QString path = KStandardDirs::locate("data",
00060 "plasma/runners/" + runnerDescription.pluginName() + '/');
00061 PackageStructure::Ptr structure =
00062 Plasma::packageStructure(api, Plasma::RunnerComponent);
00063 structure->setPath(path);
00064 package = new Package(path, structure);
00065
00066 script = Plasma::loadScriptEngine(api, runner);
00067 if (!script) {
00068 kDebug() << "Could not create a(n)" << api << "ScriptEngine for the"
00069 << runnerDescription.name() << "Runner.";
00070 delete package;
00071 package = 0;
00072 } else {
00073 QTimer::singleShot(0, runner, SLOT(init()));
00074 }
00075 }
00076 }
00077 }
00078
00079 ~AbstractRunnerPrivate()
00080 {
00081 delete script;
00082 script = 0;
00083 delete package;
00084 package = 0;
00085 }
00086
00087 bool hasRunOptions;
00088 bool hasConfig;
00089 AbstractRunner::Priority priority;
00090 AbstractRunner::Speed speed;
00091 RunnerContext::Types blackListed;
00092 RunnerScript *script;
00093 KPluginInfo runnerDescription;
00094 AbstractRunner *runner;
00095 QTime runtime;
00096 int fastRuns;
00097 Package *package;
00098 QHash<QString, QAction*> actions;
00099 };
00100
00101 K_GLOBAL_STATIC(QMutex, s_bigLock)
00102
00103 AbstractRunner::AbstractRunner(QObject *parent, const QString &serviceId)
00104 : QObject(parent),
00105 d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(serviceId)))
00106 {
00107 }
00108
00109 AbstractRunner::AbstractRunner(QObject *parent, const QVariantList &args)
00110 : QObject(parent),
00111 d(new AbstractRunnerPrivate(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString())))
00112 {
00113 }
00114
00115 AbstractRunner::~AbstractRunner()
00116 {
00117 delete d;
00118 }
00119
00120 KConfigGroup AbstractRunner::config() const
00121 {
00122 QString group = objectName();
00123 if (group.isEmpty()) {
00124 group = "UnnamedRunner";
00125 }
00126
00127 KConfigGroup runners(KGlobal::config(), "Runners");
00128 return KConfigGroup(&runners, group);
00129 }
00130
00131 void AbstractRunner::reloadConfiguration()
00132 {
00133 }
00134
00135 void AbstractRunner::performMatch(Plasma::RunnerContext &globalContext)
00136 {
00137 static const int reasonableRunTime = 1500;
00138 static const int fastEnoughTime = 250;
00139
00140 d->runtime.restart();
00141
00142 RunnerContext localContext(globalContext, 0);
00143 match(localContext);
00144
00145 const int runtime = d->runtime.elapsed();
00146 bool slowed = speed() == SlowSpeed;
00147
00148 if (!slowed && runtime > reasonableRunTime) {
00149
00150
00151 kDebug() << id() << "runner is too slow, putting it on the back burner.";
00152 d->fastRuns = 0;
00153 setSpeed(SlowSpeed);
00154 }
00155
00156
00157 if (slowed && runtime < fastEnoughTime) {
00158 ++d->fastRuns;
00159
00160 if (d->fastRuns > 2) {
00161
00162
00163 kDebug() << id() << "runner is faster than we thought, kicking it up a notch";
00164 setSpeed(NormalSpeed);
00165 }
00166 }
00167 }
00168
00169 QList<QAction*> AbstractRunner::actionsForMatch(const Plasma::QueryMatch &match)
00170 {
00171 Q_UNUSED(match)
00172 QList<QAction*> ret;
00173 return ret;
00174 }
00175
00176 QAction* AbstractRunner::addAction(const QString &id, const QIcon &icon, const QString &text)
00177 {
00178 QAction *a = new QAction(icon, text, this);
00179 d->actions.insert(id, a);
00180 return a;
00181 }
00182
00183 void AbstractRunner::addAction(const QString &id, QAction *action)
00184 {
00185 d->actions.insert(id, action);
00186 }
00187
00188 void AbstractRunner::removeAction(const QString &id)
00189 {
00190 QAction *a = d->actions.take(id);
00191 delete a;
00192 }
00193
00194 QAction* AbstractRunner::action(const QString &id) const
00195 {
00196 return d->actions.value(id);
00197 }
00198
00199 QHash<QString, QAction*> AbstractRunner::actions() const
00200 {
00201 return d->actions;
00202 }
00203
00204 void AbstractRunner::clearActions()
00205 {
00206 qDeleteAll(d->actions);
00207 d->actions.clear();
00208 }
00209
00210 bool AbstractRunner::hasRunOptions()
00211 {
00212 return d->hasRunOptions;
00213 }
00214
00215 void AbstractRunner::setHasRunOptions(bool hasRunOptions)
00216 {
00217 d->hasRunOptions = hasRunOptions;
00218 }
00219
00220 void AbstractRunner::createRunOptions(QWidget *parent)
00221 {
00222 Q_UNUSED(parent)
00223 }
00224
00225 AbstractRunner::Speed AbstractRunner::speed() const
00226 {
00227 return d->speed;
00228 }
00229
00230 void AbstractRunner::setSpeed(Speed speed)
00231 {
00232 d->speed = speed;
00233 }
00234
00235 AbstractRunner::Priority AbstractRunner::priority() const
00236 {
00237 return d->priority;
00238 }
00239
00240 void AbstractRunner::setPriority(Priority priority)
00241 {
00242 d->priority = priority;
00243 }
00244
00245 RunnerContext::Types AbstractRunner::ignoredTypes() const
00246 {
00247 return d->blackListed;
00248 }
00249
00250 void AbstractRunner::setIgnoredTypes(RunnerContext::Types types)
00251 {
00252 d->blackListed = types;
00253 }
00254
00255 KService::List AbstractRunner::serviceQuery(const QString &serviceType, const QString &constraint) const
00256 {
00257 QMutexLocker lock(s_bigLock);
00258 return KServiceTypeTrader::self()->query(serviceType, constraint);
00259 }
00260
00261 QMutex* AbstractRunner::bigLock()
00262 {
00263 return s_bigLock;
00264 }
00265
00266 void AbstractRunner::run(const Plasma::RunnerContext &search, const Plasma::QueryMatch &action)
00267 {
00268 if (d->script) {
00269 return d->script->run(search, action);
00270 }
00271 }
00272
00273 void AbstractRunner::match(Plasma::RunnerContext &search)
00274 {
00275 if (d->script) {
00276 return d->script->match(search);
00277 }
00278 }
00279
00280 QString AbstractRunner::name() const
00281 {
00282 if (!d->runnerDescription.isValid()) {
00283 return objectName();
00284 }
00285 return d->runnerDescription.name();
00286 }
00287
00288 QString AbstractRunner::id() const
00289 {
00290 if (!d->runnerDescription.isValid()) {
00291 return objectName();
00292 }
00293 return d->runnerDescription.pluginName();
00294 }
00295
00296 QString AbstractRunner::description() const
00297 {
00298 if (!d->runnerDescription.isValid()) {
00299 return objectName();
00300 }
00301 return d->runnerDescription.property("Comment").toString();
00302 }
00303
00304 const Package* AbstractRunner::package() const
00305 {
00306 return d->package;
00307 }
00308
00309 void AbstractRunner::init()
00310 {
00311 if (d->script) {
00312 d->script->init();
00313 }
00314 }
00315
00316 }
00317
00318 #include "abstractrunner.moc"