From dc295eca7015b79909018d2c2736ea905638bdeb Mon Sep 17 00:00:00 2001 From: Jacopo De Simoi Date: Tue, 24 Nov 2009 12:05:46 +0000 Subject: [PATCH] Add removeMatch(es) methods Review: http://reviewboard.kde.org/r/2260 svn path=/trunk/KDE/kdelibs/; revision=1053618 --- querymatch.cpp | 5 +++++ querymatch.h | 1 + runnercontext.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ runnercontext.h | 24 ++++++++++++++++++++ 4 files changed, 87 insertions(+) diff --git a/querymatch.cpp b/querymatch.cpp index 8ddb0cea5..e23fdc1dc 100644 --- a/querymatch.cpp +++ b/querymatch.cpp @@ -212,6 +212,11 @@ QueryMatch &QueryMatch::operator=(const QueryMatch &other) return *this; } +bool QueryMatch::operator==(const QueryMatch &other) const +{ + return (d == other.d); +} + void QueryMatch::run(const RunnerContext &context) const { //kDebug() << "we run the term" << context->query() << "whose type is" << context->mimetype(); diff --git a/querymatch.h b/querymatch.h index 1bfd43c7e..7e3133c30 100644 --- a/querymatch.h +++ b/querymatch.h @@ -83,6 +83,7 @@ class PLASMA_EXPORT QueryMatch ~QueryMatch(); QueryMatch &operator=(const QueryMatch &other); + bool operator==(const QueryMatch &other) const; bool operator<(const QueryMatch &other) const; diff --git a/runnercontext.cpp b/runnercontext.cpp index 3d183939a..557c4865d 100644 --- a/runnercontext.cpp +++ b/runnercontext.cpp @@ -396,6 +396,63 @@ bool RunnerContext::addMatch(const QString &term, const QueryMatch &match) return true; } +bool RunnerContext::removeMatches(const QStringList matchIdList) +{ + if (!isValid()) { + return false; + } + + QStringList presentMatchIdList; + QList presentMatchList; + + LOCK_FOR_READ(d) + foreach(QString matchId, matchIdList) { + const QueryMatch* match = d->matchesById.value(matchId, 0); + if (match) { + presentMatchList << match; + presentMatchIdList << matchId; + } + } + UNLOCK(d) + + if (presentMatchIdList.isEmpty()) { + return false; + } + + LOCK_FOR_WRITE(d) + foreach(const QueryMatch *match, presentMatchList) { + d->matches.removeAll(*match); + } + foreach(QString matchId, presentMatchIdList) { + d->matchesById.remove(matchId); + } + UNLOCK(d) + + emit d->q->matchesChanged(); + + return true; +} + +bool RunnerContext::removeMatch(const QString matchId) +{ + if (!isValid()) { + return false; + } + LOCK_FOR_READ(d) + const QueryMatch* match = d->matchesById.value(matchId, 0); + UNLOCK(d) + if (!match) { + return false; + } + LOCK_FOR_WRITE(d) + d->matches.removeAll(*match); + d->matchesById.remove(matchId); + UNLOCK(d) + emit d->q->matchesChanged(); + + return true; +} + QList RunnerContext::matches() const { LOCK_FOR_READ(d) diff --git a/runnercontext.h b/runnercontext.h index 3dd218b5b..9d20dfa0a 100644 --- a/runnercontext.h +++ b/runnercontext.h @@ -154,6 +154,30 @@ class PLASMA_EXPORT RunnerContext : public QObject // plus: it is Q_UNUSED bool addMatch(const QString &term, const QueryMatch &match); + /** + * Removes a match from the existing list of matches. + * + * If you are going to be removing multiple matches, use removeMatches instead. + * + * @arg matchId the id of match to remove + * + * @return true if the match was removed, false otherwise. + * @since 4.4 + */ + bool removeMatch(const QString matchId); + + /** + * Removes lists of matches from the existing list of matches. + * + * This method is thread safe and causes the matchesChanged() signal to be emitted. + * + * @arg matchIdList the list of matches id to remove + * + * @return true if at least one match was removed, false otherwise. + * @since 4.4 + */ + bool removeMatches(const QStringList matchIdList); + /** * Retrieves all available matches for the current search term. *