diff --git a/plasma/abstractrunner.cpp b/plasma/abstractrunner.cpp index 68197bcf3b..016f54063d 100644 --- a/plasma/abstractrunner.cpp +++ b/plasma/abstractrunner.cpp @@ -1,269 +1,256 @@ /* * Copyright 2006-2007 Aaron Seigo * * This program is free software; you can redistribute it and/or modify * 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. * * 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. */ #include "abstractrunner.h" #include #include #include #include #include #include #include "scripting/runnerscript.h" #include "searchcontext.h" namespace Plasma { class AbstractRunner::Private { public: bool hasMatchOptions; bool hasConfig; Priority priority; Speed speed; - int tier; RunnerScript* script; KPluginInfo runnerDescription; AbstractRunner* runner; Private(AbstractRunner* r, KService::Ptr service) : priority(NormalPriority), speed(NormalSpeed), - tier(0), script(0), runnerDescription(service), runner(r) { if (runnerDescription.isValid()) { QString language = runnerDescription.property("X-Plasma-Language").toString(); if (!language.isEmpty()) { script = Plasma::loadScriptEngine(language, runner); if (!script) { kDebug() << "Could not create a" << language << "ScriptEngine for the" << runnerDescription.name() << "Runner."; } else { QTimer::singleShot(0, runner, SLOT(init())); } } } } static QMutex bigLock; }; QMutex AbstractRunner::Private::bigLock; AbstractRunner::AbstractRunner(QObject* parent, const QString& serviceId) : QObject(parent), d(new Private(this, KService::serviceByStorageId(serviceId))) { } AbstractRunner::AbstractRunner(QObject* parent, const QVariantList& args) : QObject(parent), d(new Private(this, KService::serviceByStorageId(args.count() > 0 ? args[0].toString() : QString()))) { } AbstractRunner::~AbstractRunner() { delete d; } KConfigGroup AbstractRunner::config() const { QString group = objectName(); if (group.isEmpty()) { group = "UnnamedRunner"; } KConfigGroup runners(KGlobal::config(), "Runners"); return KConfigGroup(&runners, group); } void AbstractRunner::performMatch( Plasma::SearchContext &globalContext ) { Plasma::SearchContext localContext( 0, globalContext ); //Keep track of global context list sizes so we know which pointers are our responsibility to delete int exactEnd = localContext.exactMatches().count(); int possibleEnd = localContext.possibleMatches().count(); int infoEnd = localContext.informationalMatches().count(); match( &localContext ); QList exact = localContext.exactMatches().mid(exactEnd); QList possible = localContext.possibleMatches().mid(possibleEnd); QList info = localContext.informationalMatches().mid(infoEnd); //If matches were not added, delete items on the heap if (!globalContext.addMatches(localContext.searchTerm(), exact, possible, info)) { qDeleteAll(exact); qDeleteAll(possible); qDeleteAll(info); } } bool AbstractRunner::hasMatchOptions() { return d->hasMatchOptions; } void AbstractRunner::setHasMatchOptions(bool hasMatchOptions) { d->hasMatchOptions = hasMatchOptions; } void AbstractRunner::createMatchOptions(QWidget *parent) { Q_UNUSED(parent) } bool AbstractRunner::isConfigurable() { return d->hasConfig; } void AbstractRunner::setIsConfigurable(bool hasConfig) { d->hasConfig = hasConfig; } void AbstractRunner::createConfigurationInterface(QWidget *widget) { Q_UNUSED(widget) } AbstractRunner::Speed AbstractRunner::speed() const { return d->speed; } void AbstractRunner::setSpeed(Speed speed) { d->speed = speed; } -// For 4.1: -// int AbstractRunner::tier() const -// { -// return d->tier; -// } -// -// void AbstractRunner::setTier(int tier) -// { -// d->tier = tier; -// } - AbstractRunner::Priority AbstractRunner::priority() const { return d->priority; } void AbstractRunner::setPriority(Priority priority) { d->priority = priority; } KService::List AbstractRunner::serviceQuery(const QString &serviceType, const QString &constraint) const { QMutexLocker lock(&Private::bigLock); return KServiceTypeTrader::self()->query(serviceType, constraint); } const QMutex& AbstractRunner::bigLock() const { return Private::bigLock; } void AbstractRunner::exec(Plasma::SearchMatch *action) { if (d->script) { return d->script->exec(action); } } void AbstractRunner::match(Plasma::SearchContext *search) { if (d->script) { return d->script->match(search); } } QString AbstractRunner::runnerName() const { if (!d->runnerDescription.isValid()) { return QString(); } return d->runnerDescription.property("X-Plasma-RunnerName").toString(); } void AbstractRunner::init() { if (d->script) { d->script->init(); } } AbstractRunner::List AbstractRunner::loadRunners(QObject* parent, const QStringList& whitelist) { List firstRunners; List runners; List lastRunners; KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner"); QString error; QVariantList allArgs; foreach (KService::Ptr service, offers) { if( whitelist.empty() || whitelist.contains( service->name() ) ) { allArgs << service->storageId(); QString language = service->property("X-Plasma-Language").toString(); AbstractRunner* runner; if (language.isEmpty()) { runner = service->createInstance(parent, allArgs, &error); } else { runner = new AbstractRunner(parent, service->storageId()); } if (runner) { //kDebug() << "loaded runner : " << service->name(); QString phase = service->property("X-Plasma-RunnerPhase").toString(); if (phase == "last") { lastRunners.append(runner); } else if (phase == "first") { firstRunners.append(runner); } else { runners.append(runner); } } else { kDebug() << "failed to load runner : " << service->name() << ". error reported: " << error; } } } firstRunners << runners << lastRunners; return firstRunners; } } // Plasma namespace #include "abstractrunner.moc" diff --git a/plasma/abstractrunner.h b/plasma/abstractrunner.h index e70e4e32d1..c6495980ae 100644 --- a/plasma/abstractrunner.h +++ b/plasma/abstractrunner.h @@ -1,241 +1,234 @@ /* * Copyright 2006-2007 Aaron Seigo * * This program is free software; you can redistribute it and/or modify * 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. * * 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. */ #ifndef RUNNER_H #define RUNNER_H #include #include #include #include #include #include #include #include class KCompletion; namespace Plasma { class RunnerScript; /** * An abstract base class for Plasma Runner plugins * * Be aware that runners have to be thread-safe. This is due to * the fact that each runner is executed in its own thread for * each new term. Thus, a runner may be executed more than once * at the same time. */ class PLASMA_EXPORT AbstractRunner : public QObject { Q_OBJECT public: enum Speed { NormalSpeed, SlowSpeed }; enum Priority { LowestPriority = 0, LowPriority, NormalPriority, HighPriority, HighestPriority }; typedef QList List; /** * Static method is called to load and get a list available of Runners. * * @param whitelist An optional whitelist of runners to load */ static List loadRunners(QObject* parent, const QStringList& whitelist = QStringList() ); /** * Constructs a Runner object. Since AbstractRunner has pure virtuals, * this constructor can not be called directly. Rather a subclass must * be created */ explicit AbstractRunner(QObject* parent = 0, const QString& serviceId = QString()); AbstractRunner(QObject* parent, const QVariantList& args); virtual ~AbstractRunner(); /** * Provides access to the runner's configuration object. */ KConfigGroup config() const; /** * This is the main query method. It should trigger creation of * SearchMatch instances through SearchContext::addInformationalMatch, * SearchContext::addExactMatch, and SearchContext::addPossibleMatch. * * If the runner can run precisely the requested term (SearchContext::searchTerm), * it should create an exact match (SearchContext::addExactMatch). * The first runner that creates a SearchMatch will be the * default runner. Other runner's matches will be suggested in the * interface. Non-exact matches should be offered via SearchContext::addPossibleMatch. * * The match will be activated if the user selects it. * * If this runner's exact match is selected, it will be passed into * the exec method. * @see exec * * Since each runner is executed in its own thread there is no need * to return from this method right away, nor to create all matches * here. */ virtual void match(Plasma::SearchContext *search); /** * Triggers a call to match. * * @arg globalContext the search context used in executing this match. */ void performMatch(Plasma::SearchContext &globalContext); /** * If the runner has options that the user can interact with to modify * what happens when exec or one of the actions created in fillMatches * is called, the runner should return true */ bool hasMatchOptions(); /** * If hasMatchOptions() returns true, this method may be called to get * a widget displaying the options the user can interact with to modify * the behaviour of what happens when a given match is selected. * * @param widget the parent of the options widgets. */ virtual void createMatchOptions(QWidget *widget); /** * If the runner itself has configuration options, this method returns true */ bool isConfigurable(); /** * If isConfigurable() returns true, this method may to get * a widget displaying the options the user can interact with to modify * the behaviour of what happens when a given match is selected. * * @param widget the parent of the options widgets. */ virtual void createConfigurationInterface(QWidget *widget); /** * Called whenever an exact or possible match associated with this * runner is triggered. */ virtual void exec(Plasma::SearchMatch *action); /** * The nominal speed of the runner. * @see setSpeed */ Speed speed() const; -// For 4.1 -// /** -// * The tier of the runner. -// * @see setTier -// */ -// int tier() const; - /** * The priority of the runner. * @see setPriority */ Priority priority() const; /** * Returns the engine name for the Runner */ QString runnerName() const; protected: /** * Sets whether or not the the runner has options for matches */ void setHasMatchOptions(bool hasMatchOptions); /** * Sets whether or not the runner has configuration options itself */ void setIsConfigurable(bool canBeConfigured); /** * Sets the nominal speed of the runner. Only slow runners need * to call this within their constructor because the default * speed is NormalSpeed. Runners that use DBUS should call * this within their constructors. */ void setSpeed(Speed newSpeed); //For 4.1 // /** // * Sets the run tier of the runner. Higher tier runners execute only // * after all lower-level runners execute. Call this method if your runner // * depends on the output of previous runners. // */ // void setTier(int tier); /** * Sets the priority of the runner. Lower priority runners are executed * only after higher priority runners. */ void setPriority(Priority newPriority); /** * A blocking method to do queries of installed Services which can provide * a measure of safety for runners running their own threads. This should * be used instead of calling KServiceTypeTrader::query(..) directly. * * @arg serviceType a service type like "Plasma/Applet" or "KFilePlugin" * @arg constraint a constraint to limit the the choices returned. * @see KServiceTypeTrader::query(const QString&, const QString&) * * @return a list of services that satisfy the query. */ KService::List serviceQuery(const QString &serviceType, const QString &constraint = QString()) const; const QMutex& bigLock() const; protected slots: void init(); private: class Private; Private* const d; }; } // Plasma namespace #define K_EXPORT_PLASMA_RUNNER( libname, classname ) \ K_PLUGIN_FACTORY(factory, registerPlugin();) \ K_EXPORT_PLUGIN(factory("plasma_runner_" #libname)) #endif