2008-05-01 09:51:59 +02:00
|
|
|
/*
|
2008-09-11 08:37:09 +02:00
|
|
|
* Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright (C) 2007 Ryan P. Bitanga <ryan.bitanga@gmail.com>
|
|
|
|
* Copyright (2) 2008 Jordi Polo <mumismo@gmail.com>
|
2008-05-01 09:51:59 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-05-09 07:32:15 +02:00
|
|
|
#include "runnermanager.h"
|
|
|
|
|
2008-09-30 17:41:21 +02:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
#include <KDebug>
|
2008-09-30 17:41:21 +02:00
|
|
|
#include <KPluginInfo>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KStandardDirs>
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
#include <Solid/Device>
|
|
|
|
#include <Solid/DeviceInterface>
|
|
|
|
|
|
|
|
#include <threadweaver/DebuggingAids.h>
|
|
|
|
#include <ThreadWeaver/Thread>
|
|
|
|
#include <ThreadWeaver/Job>
|
|
|
|
#include <ThreadWeaver/QueuePolicy>
|
|
|
|
#include <ThreadWeaver/Weaver>
|
|
|
|
|
2008-05-02 20:46:05 +02:00
|
|
|
#include "querymatch.h"
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
using ThreadWeaver::Weaver;
|
|
|
|
using ThreadWeaver::Job;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************
|
|
|
|
* RunnerRestrictionPolicy class
|
|
|
|
* Restricts simultaneous jobs of the same type
|
|
|
|
* Similar to ResourceRestrictionPolicy but check the object type first
|
|
|
|
******************************************************/
|
|
|
|
class RunnerRestrictionPolicy : public ThreadWeaver::QueuePolicy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~RunnerRestrictionPolicy();
|
|
|
|
|
|
|
|
static RunnerRestrictionPolicy& instance();
|
|
|
|
|
|
|
|
void setCap(int cap)
|
|
|
|
{
|
|
|
|
m_cap = cap;
|
|
|
|
}
|
|
|
|
int cap() const
|
|
|
|
{
|
|
|
|
return m_cap;
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
bool canRun(Job *job);
|
|
|
|
void free(Job *job);
|
|
|
|
void release(Job *job);
|
|
|
|
void destructed(Job *job);
|
2008-05-01 09:51:59 +02:00
|
|
|
private:
|
|
|
|
RunnerRestrictionPolicy();
|
|
|
|
|
|
|
|
int m_count;
|
|
|
|
int m_cap;
|
|
|
|
QMutex m_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
RunnerRestrictionPolicy::RunnerRestrictionPolicy()
|
|
|
|
: QueuePolicy(),
|
2008-06-21 12:39:03 +02:00
|
|
|
m_count(0),
|
|
|
|
m_cap(2)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RunnerRestrictionPolicy::~RunnerRestrictionPolicy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RunnerRestrictionPolicy& RunnerRestrictionPolicy::instance()
|
|
|
|
{
|
|
|
|
static RunnerRestrictionPolicy policy;
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
bool RunnerRestrictionPolicy::canRun(Job *job)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(job)
|
|
|
|
QMutexLocker l(&m_mutex);
|
2008-09-11 08:37:09 +02:00
|
|
|
if (m_count > m_cap) {
|
2008-05-01 09:51:59 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
++m_count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
void RunnerRestrictionPolicy::free(Job *job)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(job)
|
|
|
|
QMutexLocker l(&m_mutex);
|
|
|
|
--m_count;
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
void RunnerRestrictionPolicy::release(Job *job)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
free(job);
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
void RunnerRestrictionPolicy::destructed(Job *job)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************
|
|
|
|
* FindMatchesJob class
|
|
|
|
* Class to run queries in different threads
|
|
|
|
******************************************************/
|
|
|
|
class FindMatchesJob : public Job
|
|
|
|
{
|
|
|
|
public:
|
2008-09-11 08:37:09 +02:00
|
|
|
FindMatchesJob(Plasma::AbstractRunner *runner, Plasma::RunnerContext *context, QObject *parent = 0);
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
int priority() const;
|
2008-05-09 19:19:27 +02:00
|
|
|
Plasma::AbstractRunner* runner() const;
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void run();
|
|
|
|
private:
|
2008-09-11 08:37:09 +02:00
|
|
|
Plasma::RunnerContext *m_context;
|
|
|
|
Plasma::AbstractRunner *m_runner;
|
2008-05-01 09:51:59 +02:00
|
|
|
};
|
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
FindMatchesJob::FindMatchesJob(Plasma::AbstractRunner *runner,
|
|
|
|
Plasma::RunnerContext *context, QObject *parent)
|
2008-05-01 09:51:59 +02:00
|
|
|
: ThreadWeaver::Job(parent),
|
|
|
|
m_context(context),
|
|
|
|
m_runner(runner)
|
|
|
|
{
|
|
|
|
if (runner->speed() == Plasma::AbstractRunner::SlowSpeed) {
|
|
|
|
assignQueuePolicy(&RunnerRestrictionPolicy::instance());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindMatchesJob::run()
|
|
|
|
{
|
|
|
|
// kDebug() << "Running match for " << m_runner->objectName() << " in Thread " << thread()->id() << endl;
|
|
|
|
m_runner->performMatch(*m_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FindMatchesJob::priority() const
|
|
|
|
{
|
|
|
|
return m_runner->priority();
|
|
|
|
}
|
|
|
|
|
2008-05-09 19:19:27 +02:00
|
|
|
Plasma::AbstractRunner* FindMatchesJob::runner() const
|
|
|
|
{
|
|
|
|
return m_runner;
|
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
/*****************************************************
|
|
|
|
* RunnerManager::Private class
|
|
|
|
*
|
|
|
|
*****************************************************/
|
2008-07-01 20:56:43 +02:00
|
|
|
class RunnerManagerPrivate
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
RunnerManagerPrivate(RunnerManager *parent)
|
2008-05-09 19:19:27 +02:00
|
|
|
: q(parent),
|
|
|
|
deferredRun(0)
|
2008-05-01 19:42:34 +02:00
|
|
|
{
|
2008-05-07 00:42:01 +02:00
|
|
|
matchChangeTimer.setSingleShot(true);
|
2008-07-01 20:56:43 +02:00
|
|
|
QObject::connect(&matchChangeTimer, SIGNAL(timeout()), q, SLOT(matchesChanged()));
|
|
|
|
QObject::connect(&context, SIGNAL(matchesChanged()), q, SLOT(scheduleMatchesChanged()));
|
2008-05-06 21:52:50 +02:00
|
|
|
}
|
|
|
|
|
2008-05-07 00:42:01 +02:00
|
|
|
void scheduleMatchesChanged()
|
|
|
|
{
|
2008-07-31 02:23:57 +02:00
|
|
|
matchChangeTimer.start(0);
|
2008-05-07 00:42:01 +02:00
|
|
|
}
|
|
|
|
|
2008-05-06 21:52:50 +02:00
|
|
|
void matchesChanged()
|
|
|
|
{
|
|
|
|
emit q->matchesChanged(context.matches());
|
2008-05-01 19:42:34 +02:00
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
void loadConfiguration(KConfigGroup &conf)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
2008-05-01 19:42:34 +02:00
|
|
|
config = conf;
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
//The number of threads used scales with the number of processors.
|
|
|
|
const int numProcs = qMax(Solid::Device::listFromType(Solid::DeviceInterface::Processor).count(), 1);
|
|
|
|
//This entry allows to define a hard upper limit independent of the number of processors.
|
|
|
|
const int maxThreads = config.readEntry("maxThreads",16);
|
|
|
|
const int numThreads = qMin(maxThreads, 2 + ((numProcs - 1) * 2));
|
2008-05-19 19:02:15 +02:00
|
|
|
//kDebug() << "setting up" << numThreads << "threads for" << numProcs << "processors";
|
2008-05-01 09:51:59 +02:00
|
|
|
Weaver::instance()->setMaximumNumberOfThreads(numThreads);
|
|
|
|
|
|
|
|
//Preferred order of execution of runners
|
|
|
|
//prioritylist = config.readEntry("priority", QStringList());
|
|
|
|
|
|
|
|
//If set, this list defines which runners won't be used at runtime
|
2008-05-19 19:02:15 +02:00
|
|
|
//blacklist = config.readEntry("blacklist", QStringList());
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
2008-05-01 19:42:34 +02:00
|
|
|
|
2008-05-19 19:02:15 +02:00
|
|
|
void loadRunners()
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner");
|
2008-05-19 19:02:15 +02:00
|
|
|
|
|
|
|
bool loadAll = config.readEntry("loadAll", false);
|
2008-09-24 20:13:21 +02:00
|
|
|
KConfigGroup conf(&config, "Plugins");
|
2008-05-19 19:02:15 +02:00
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
foreach (const KService::Ptr &service, offers) {
|
2008-05-06 22:49:24 +02:00
|
|
|
//kDebug() << "Loading runner: " << service->name() << service->storageId();
|
2008-09-30 17:41:21 +02:00
|
|
|
QString tryExec = service->property("TryExec", QVariant::String).toString();
|
|
|
|
kDebug() << "tryExec is" << tryExec;
|
|
|
|
if (!tryExec.isEmpty() && KStandardDirs::findExe(tryExec).isEmpty()) {
|
|
|
|
// we don't actually have this application!
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
KPluginInfo description(service);
|
2008-05-06 20:43:55 +02:00
|
|
|
QString runnerName = description.pluginName();
|
2008-05-19 19:02:15 +02:00
|
|
|
description.load(conf);
|
|
|
|
|
|
|
|
bool loaded = runners.contains(runnerName);
|
|
|
|
bool selected = loadAll || description.isPluginEnabled();
|
|
|
|
|
|
|
|
if (selected) {
|
|
|
|
if (!loaded) {
|
|
|
|
QString api = service->property("X-Plasma-API").toString();
|
|
|
|
QString error;
|
2008-09-11 08:37:09 +02:00
|
|
|
AbstractRunner *runner = 0;
|
2008-05-19 19:02:15 +02:00
|
|
|
|
|
|
|
if (api.isEmpty()) {
|
|
|
|
QVariantList args;
|
|
|
|
args << service->storageId();
|
2008-07-17 18:26:29 +02:00
|
|
|
if (Plasma::isPluginVersionCompatible(KPluginLoader(*service).pluginVersion())) {
|
|
|
|
runner = service->createInstance<AbstractRunner>(q, args, &error);
|
|
|
|
}
|
2008-05-19 19:02:15 +02:00
|
|
|
} else {
|
|
|
|
//kDebug() << "got a script runner known as" << api;
|
|
|
|
runner = new AbstractRunner(q, service->storageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runner) {
|
|
|
|
kDebug() << "loading runner:" << service->name();
|
|
|
|
runners.insert(runnerName, runner);
|
|
|
|
} else {
|
|
|
|
kDebug() << "failed to load runner:" << service->name() << ". error reported:" << error;
|
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
2008-05-19 19:02:15 +02:00
|
|
|
} else if (loaded) {
|
|
|
|
//Remove runner
|
2008-09-11 08:37:09 +02:00
|
|
|
AbstractRunner *runner = runners.take(runnerName);
|
2008-05-19 19:02:15 +02:00
|
|
|
kDebug() << "Removing runner: " << runnerName;
|
|
|
|
delete runner;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
2008-05-01 19:42:34 +02:00
|
|
|
|
2008-05-06 22:49:24 +02:00
|
|
|
//kDebug() << "All runners loaded, total:" << runners.count();
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
2008-05-06 22:49:24 +02:00
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
void jobDone(ThreadWeaver::Job *job)
|
2008-05-09 19:19:27 +02:00
|
|
|
{
|
|
|
|
FindMatchesJob *runJob = static_cast<FindMatchesJob*>(job);
|
|
|
|
if (deferredRun.isEnabled() && runJob->runner() == deferredRun.runner()) {
|
|
|
|
//kDebug() << "job actually done, running now **************";
|
|
|
|
deferredRun.run(context);
|
|
|
|
deferredRun = QueryMatch(0);
|
|
|
|
}
|
|
|
|
searchJobs.removeAll(runJob);
|
|
|
|
delete runJob;
|
|
|
|
}
|
|
|
|
|
2008-05-06 22:49:24 +02:00
|
|
|
RunnerManager *q;
|
2008-05-09 19:19:27 +02:00
|
|
|
QueryMatch deferredRun;
|
2008-05-06 22:49:24 +02:00
|
|
|
RunnerContext context;
|
2008-05-07 00:42:01 +02:00
|
|
|
QTimer matchChangeTimer;
|
2008-05-19 19:02:15 +02:00
|
|
|
QHash<QString, AbstractRunner*> runners;
|
2008-05-09 19:19:27 +02:00
|
|
|
QList<FindMatchesJob*> searchJobs;
|
2008-05-19 19:02:15 +02:00
|
|
|
// QStringList prioritylist;
|
|
|
|
bool loadAll;
|
2008-05-06 22:49:24 +02:00
|
|
|
KConfigGroup config;
|
2008-05-01 09:51:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************
|
|
|
|
* RunnerManager::Public class
|
|
|
|
*
|
|
|
|
*****************************************************/
|
|
|
|
RunnerManager::RunnerManager(QObject *parent)
|
2008-09-11 08:37:09 +02:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new RunnerManagerPrivate(this))
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
KConfigGroup config(KGlobal::config(), "PlasmaRunnerManager");
|
2008-05-01 19:42:34 +02:00
|
|
|
d->loadConfiguration(config);
|
2008-05-01 09:51:59 +02:00
|
|
|
//ThreadWeaver::setDebugLevel(true, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 20:13:21 +02:00
|
|
|
RunnerManager::RunnerManager(KConfigGroup &c, QObject *parent)
|
2008-09-11 08:37:09 +02:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new RunnerManagerPrivate(this))
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
2008-09-25 14:40:13 +02:00
|
|
|
// Should this be really needed? Maybe d->loadConfiguration(c) would make
|
|
|
|
// more sense.
|
|
|
|
KConfigGroup config(&c, "PlasmaRunnerManager");
|
2008-05-01 19:42:34 +02:00
|
|
|
d->loadConfiguration(config);
|
2008-05-01 09:51:59 +02:00
|
|
|
//ThreadWeaver::setDebugLevel(true, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
RunnerManager::~RunnerManager()
|
|
|
|
{
|
2008-05-09 09:04:36 +02:00
|
|
|
delete d;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
2008-05-19 19:02:15 +02:00
|
|
|
void RunnerManager::reloadConfiguration()
|
|
|
|
{
|
|
|
|
d->loadConfiguration(d->config);
|
|
|
|
d->loadRunners();
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
AbstractRunner* RunnerManager::runner(const QString &name) const
|
|
|
|
{
|
2008-05-10 01:46:46 +02:00
|
|
|
if (d->runners.isEmpty()) {
|
2008-05-19 19:02:15 +02:00
|
|
|
d->loadRunners();
|
2008-05-10 01:46:46 +02:00
|
|
|
}
|
|
|
|
|
2008-05-19 19:02:15 +02:00
|
|
|
return d->runners.value(name);
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
2008-05-01 20:40:10 +02:00
|
|
|
RunnerContext* RunnerManager::searchContext() const
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
return &d->context;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Reordering is here so data is not reordered till strictly needed
|
2008-05-09 05:49:11 +02:00
|
|
|
QList<QueryMatch> RunnerManager::matches() const
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
|
|
|
return d->context.matches();
|
|
|
|
}
|
|
|
|
|
2008-05-12 06:53:42 +02:00
|
|
|
void RunnerManager::run(const QString &id)
|
|
|
|
{
|
|
|
|
run(d->context.match(id));
|
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
void RunnerManager::run(const QueryMatch &match)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
2008-05-09 19:19:27 +02:00
|
|
|
if (!match.isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
//TODO: this function is not const as it may be used for learning
|
2008-05-09 19:19:27 +02:00
|
|
|
AbstractRunner *runner = match.runner();
|
|
|
|
|
|
|
|
foreach (FindMatchesJob *job, d->searchJobs) {
|
|
|
|
if (job->runner() == runner && !job->isFinished()) {
|
|
|
|
//kDebug() << "!!!!!!!!!!!!!!!!!!! uh oh!";
|
|
|
|
d->deferredRun = match;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-09 05:49:11 +02:00
|
|
|
match.run(d->context);
|
2008-05-09 19:19:27 +02:00
|
|
|
|
|
|
|
if (d->deferredRun.isValid()) {
|
|
|
|
d->deferredRun = QueryMatch(0);
|
|
|
|
}
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
2008-05-10 01:14:00 +02:00
|
|
|
void RunnerManager::launchQuery(const QString &term)
|
|
|
|
{
|
|
|
|
launchQuery(term, QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunnerManager::launchQuery(const QString &term, const QString &runnerName)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
2008-05-10 01:46:46 +02:00
|
|
|
if (d->runners.isEmpty()) {
|
2008-05-19 19:02:15 +02:00
|
|
|
d->loadRunners();
|
2008-05-10 01:46:46 +02:00
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
if (term.isEmpty()) {
|
|
|
|
reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:17:09 +02:00
|
|
|
if (d->context.query() == term) {
|
2008-05-01 09:51:59 +02:00
|
|
|
// we already are searching for this!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset();
|
|
|
|
// kDebug() << "runners searching for" << term << "on" << runnerName;
|
2008-05-02 20:17:09 +02:00
|
|
|
d->context.setQuery(term);
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
AbstractRunner::List runable;
|
|
|
|
|
|
|
|
//if the name is not empty we will launch only the specified runner
|
|
|
|
if (!runnerName.isEmpty()) {
|
|
|
|
runable.append(runner(runnerName));
|
|
|
|
} else {
|
2008-09-11 08:37:09 +02:00
|
|
|
runable = d->runners.values();
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
2008-05-01 19:42:34 +02:00
|
|
|
|
2008-09-11 08:37:09 +02:00
|
|
|
foreach (Plasma::AbstractRunner *r, runable) {
|
2008-05-01 09:51:59 +02:00
|
|
|
if ((r->ignoredTypes() & d->context.type()) == 0) {
|
|
|
|
// kDebug() << "launching" << r->name();
|
2008-05-09 19:19:27 +02:00
|
|
|
FindMatchesJob *job = new FindMatchesJob(r, &d->context, this);
|
|
|
|
connect(job, SIGNAL(done(ThreadWeaver::Job*)), this, SLOT(jobDone(ThreadWeaver::Job*)));
|
|
|
|
Weaver::instance()->enqueue(job);
|
|
|
|
d->searchJobs.append(job);
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-10 01:14:00 +02:00
|
|
|
bool RunnerManager::execQuery(const QString &term)
|
|
|
|
{
|
|
|
|
return execQuery(term, QString());
|
|
|
|
}
|
|
|
|
|
2008-05-06 22:49:24 +02:00
|
|
|
bool RunnerManager::execQuery(const QString &term, const QString &runnerName)
|
2008-05-01 09:51:59 +02:00
|
|
|
{
|
2008-05-13 07:18:51 +02:00
|
|
|
if (d->runners.isEmpty()) {
|
2008-05-19 19:02:15 +02:00
|
|
|
d->loadRunners();
|
2008-05-13 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
if (term.isEmpty()) {
|
|
|
|
reset();
|
2008-05-01 19:42:34 +02:00
|
|
|
return false;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
2008-05-02 20:17:09 +02:00
|
|
|
if (d->context.query() == term) {
|
2008-05-01 09:51:59 +02:00
|
|
|
// we already are searching for this!
|
2008-05-06 22:49:24 +02:00
|
|
|
emit matchesChanged(d->context.matches());
|
2008-05-01 19:42:34 +02:00
|
|
|
return false;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reset();
|
2008-05-06 22:49:24 +02:00
|
|
|
//kDebug() << "executing query about " << term << "on" << runnerName;
|
2008-05-02 20:17:09 +02:00
|
|
|
d->context.setQuery(term);
|
2008-05-01 09:51:59 +02:00
|
|
|
AbstractRunner *r = runner(runnerName);
|
|
|
|
|
|
|
|
if (!r) {
|
2008-05-06 22:49:24 +02:00
|
|
|
//kDebug() << "failed to find the runner";
|
2008-05-01 19:42:34 +02:00
|
|
|
return false;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((r->ignoredTypes() & d->context.type()) != 0) {
|
2008-05-06 22:49:24 +02:00
|
|
|
//kDebug() << "ignored!";
|
2008-05-01 19:42:34 +02:00
|
|
|
return false;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r->performMatch(d->context);
|
2008-05-06 22:49:24 +02:00
|
|
|
//kDebug() << "succeeded with" << d->context.matches().count() << "results";
|
2008-05-06 21:52:50 +02:00
|
|
|
emit matchesChanged(d->context.matches());
|
2008-05-01 19:42:34 +02:00
|
|
|
return true;
|
2008-05-01 09:51:59 +02:00
|
|
|
}
|
|
|
|
|
2008-05-09 19:19:27 +02:00
|
|
|
QString RunnerManager::query() const
|
|
|
|
{
|
|
|
|
return d->context.query();
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
void RunnerManager::reset()
|
2008-05-01 19:42:34 +02:00
|
|
|
{
|
2008-05-01 09:51:59 +02:00
|
|
|
// If ThreadWeaver is idle, it is safe to clear previous jobs
|
2008-05-01 19:42:34 +02:00
|
|
|
if (Weaver::instance()->isIdle()) {
|
|
|
|
qDeleteAll(d->searchJobs);
|
2008-05-01 09:51:59 +02:00
|
|
|
d->searchJobs.clear();
|
2008-05-01 19:42:34 +02:00
|
|
|
} else {
|
2008-05-01 09:51:59 +02:00
|
|
|
Weaver::instance()->dequeue();
|
|
|
|
}
|
2008-05-09 19:19:27 +02:00
|
|
|
|
|
|
|
if (d->deferredRun.isEnabled()) {
|
|
|
|
//kDebug() << "job actually done, running now **************";
|
|
|
|
d->deferredRun.run(d->context);
|
|
|
|
d->deferredRun = QueryMatch(0);
|
|
|
|
}
|
|
|
|
|
2008-05-01 09:51:59 +02:00
|
|
|
d->context.reset();
|
|
|
|
}
|
|
|
|
|
2008-05-09 19:19:27 +02:00
|
|
|
} // Plasma namespace
|
2008-05-01 09:51:59 +02:00
|
|
|
|
|
|
|
#include "runnermanager.moc"
|