allow finding and running matches by id

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=806697
This commit is contained in:
Aaron J. Seigo 2008-05-12 04:53:42 +00:00
parent d14534ba12
commit 2ec2a2b346
4 changed files with 49 additions and 3 deletions

View File

@ -32,6 +32,7 @@
#include <KStandardDirs> #include <KStandardDirs>
#include <KUrl> #include <KUrl>
#include "abstractrunner.h"
#include "querymatch.h" #include "querymatch.h"
//#define LOCK_FOR_READ(context) if (context->d->policy == Shared) { context->d->lock.lockForRead(); } //#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; QReadWriteLock lock;
QList<QueryMatch> matches; QList<QueryMatch> matches;
QMap<QString, const QueryMatch*> matchesById;
QString term; QString term;
QString mimeType; QString mimeType;
RunnerContext::Type type; RunnerContext::Type type;
@ -144,6 +146,7 @@ void RunnerContext::reset()
// ref count was 1 (e.g. only the RunnerContext is using // ref count was 1 (e.g. only the RunnerContext is using
// the dptr) then we won't get a copy made // the dptr) then we won't get a copy made
if (!d->matches.isEmpty()) { if (!d->matches.isEmpty()) {
d->matchesById.clear();
d->matches.clear(); d->matches.clear();
emit d->q->matchesChanged(); emit d->q->matchesChanged();
} }
@ -194,7 +197,15 @@ bool RunnerContext::addMatches(const QString& term, const QList<QueryMatch> &mat
} }
LOCK_FOR_WRITE(this) 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); UNLOCK(this);
//kDebug()<< "add matches"; //kDebug()<< "add matches";
// A copied searchContext may share the d pointer, // A copied searchContext may share the d pointer,
@ -209,7 +220,8 @@ bool RunnerContext::addMatch(const QString &term, const QueryMatch &match)
Q_UNUSED(term) Q_UNUSED(term)
LOCK_FOR_WRITE(this) 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); UNLOCK(this);
//kDebug()<< "added match" << match->text(); //kDebug()<< "added match" << match->text();
emit d->q->matchesChanged(); emit d->q->matchesChanged();
@ -225,6 +237,18 @@ QList<QueryMatch> RunnerContext::matches() const
return matches; 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" #include "runnercontext.moc"

View File

@ -134,9 +134,20 @@ class PLASMA_EXPORT RunnerContext : public QObject
/** /**
* Retrieves all available matches for the current search term. * Retrieves all available matches for the current search term.
*
* @return a list of matches
*/ */
QList<QueryMatch> matches() const; QList<QueryMatch> 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: Q_SIGNALS:
void matchesChanged(); void matchesChanged();

View File

@ -357,6 +357,11 @@ QList<QueryMatch> RunnerManager::matches() const
return d->context.matches(); return d->context.matches();
} }
void RunnerManager::run(const QString &id)
{
run(d->context.match(id));
}
void RunnerManager::run(const QueryMatch &match) void RunnerManager::run(const QueryMatch &match)
{ {
if (!match.isEnabled()) { if (!match.isEnabled()) {

View File

@ -69,10 +69,16 @@ class PLASMA_EXPORT RunnerManager : public QObject
/** /**
* Runs a given match * Runs a given match
* @arg pointer to the match to be executed * @arg match the match to be executed
*/ */
void run(const QueryMatch &match); 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 * @return the current query term
*/ */