From 2ec2a2b34639077e41f160db1470286644d31e5e Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Mon, 12 May 2008 04:53:42 +0000 Subject: [PATCH] allow finding and running matches by id svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=806697 --- runnercontext.cpp | 28 ++++++++++++++++++++++++++-- runnercontext.h | 11 +++++++++++ runnermanager.cpp | 5 +++++ runnermanager.h | 8 +++++++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/runnercontext.cpp b/runnercontext.cpp index 1236851b4..24fdcdf1d 100644 --- a/runnercontext.cpp +++ b/runnercontext.cpp @@ -32,6 +32,7 @@ #include #include +#include "abstractrunner.h" #include "querymatch.h" //#define LOCK_FOR_READ(context) if (context->d->policy == Shared) { context->d->lock.lockForRead(); } @@ -107,6 +108,7 @@ class RunnerContext::Private : public QSharedData QReadWriteLock lock; QList matches; + QMap matchesById; QString term; QString mimeType; RunnerContext::Type type; @@ -144,6 +146,7 @@ void RunnerContext::reset() // ref count was 1 (e.g. only the RunnerContext is using // the dptr) then we won't get a copy made if (!d->matches.isEmpty()) { + d->matchesById.clear(); d->matches.clear(); emit d->q->matchesChanged(); } @@ -194,7 +197,15 @@ bool RunnerContext::addMatches(const QString& term, const QList &mat } LOCK_FOR_WRITE(this) - d->matches << matches; + foreach (const QueryMatch &match, matches) { + d->matches.append(match); +#ifndef NDEBUG + if (d->matchesById.contains(match.id())) { + kDebug() << "Duplicate match id " << match.id() << "from" << match.runner()->name(); + } +#endif + d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1)); + } UNLOCK(this); //kDebug()<< "add matches"; // A copied searchContext may share the d pointer, @@ -209,7 +220,8 @@ bool RunnerContext::addMatch(const QString &term, const QueryMatch &match) Q_UNUSED(term) LOCK_FOR_WRITE(this) - d->matches << match; + d->matches.append(match); + d->matchesById.insert(match.id(), &d->matches.at(d->matches.size() - 1)); UNLOCK(this); //kDebug()<< "added match" << match->text(); emit d->q->matchesChanged(); @@ -225,6 +237,18 @@ QList RunnerContext::matches() const return matches; } +QueryMatch RunnerContext::match(const QString &id) const +{ + LOCK_FOR_READ(this) + if (d->matchesById.contains(id)) { + return *d->matchesById.value(id); + } + UNLOCK(this) + + return QueryMatch(0); + } +} // Plasma namespace + #include "runnercontext.moc" diff --git a/runnercontext.h b/runnercontext.h index f9558e797..3b28f8905 100644 --- a/runnercontext.h +++ b/runnercontext.h @@ -134,9 +134,20 @@ class PLASMA_EXPORT RunnerContext : public QObject /** * Retrieves all available matches for the current search term. + * + * @return a list of matches */ QList matches() const; + /** + * Retrieves a match by id. + * + * @param id the id of the match to return + * @return the match associated with this id, or an invalid QueryMatch object + * if the id does not eixst + */ + QueryMatch match(const QString &id) const; + Q_SIGNALS: void matchesChanged(); diff --git a/runnermanager.cpp b/runnermanager.cpp index f6b3b2c3f..e848e30bb 100644 --- a/runnermanager.cpp +++ b/runnermanager.cpp @@ -357,6 +357,11 @@ QList RunnerManager::matches() const return d->context.matches(); } +void RunnerManager::run(const QString &id) +{ + run(d->context.match(id)); +} + void RunnerManager::run(const QueryMatch &match) { if (!match.isEnabled()) { diff --git a/runnermanager.h b/runnermanager.h index 5db892e89..3197758c6 100644 --- a/runnermanager.h +++ b/runnermanager.h @@ -69,10 +69,16 @@ class PLASMA_EXPORT RunnerManager : public QObject /** * Runs a given match - * @arg pointer to the match to be executed + * @arg match the match to be executed */ void run(const QueryMatch &match); + /** + * Runs a given match + * @arg id the id of the match to run + */ + void run(const QString &id); + /** * @return the current query term */