• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

abstractrunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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 //TODO :this is a copy ctor
00142     RunnerContext localContext(globalContext, 0);
00143     match(localContext);
00144     // automatically rate limit runners that become slooow
00145     const int runtime = d->runtime.elapsed();
00146     bool slowed = speed() == SlowSpeed;
00147 
00148     if (!slowed && runtime > reasonableRunTime) {
00149         // we punish runners that return too slowly, even if they don't bring
00150         // back matches
00151         kDebug() << id() << "runner is too slow, putting it on the back burner.";
00152         d->fastRuns = 0;
00153         setSpeed(SlowSpeed);
00154     }
00155 
00156     //If matches were not added, delete items on the heap
00157     if (slowed && runtime < fastEnoughTime) {
00158         ++d->fastRuns;
00159 
00160         if (d->fastRuns > 2) {
00161             // we reward slowed runners who bring back matches fast enough
00162             // 3 times in a row
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 } // Plasma namespace
00317 
00318 #include "abstractrunner.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal