8#include "runnercontext.h"
13#include <QReadWriteLock>
14#include <QRegularExpression>
18#include <KConfigGroup>
21#include "abstractrunner.h"
22#include "abstractrunner_p.h"
23#include "querymatch.h"
24#include "runnermanager.h"
28KRUNNER_EXPORT
int __changeCountBeforeSaving = 5;
32 explicit RunnerContextPrivate(RunnerManager *manager)
38 RunnerContextPrivate(
const RunnerContextPrivate &p)
40 , m_manager(p.m_manager)
41 , launchCounts(p.launchCounts)
42 , changedLaunchCounts(p.changedLaunchCounts)
46 ~RunnerContextPrivate()
55 void addMatch(
const QueryMatch &match)
57 if (
match.runner() &&
match.runner()->d->hasUniqueResults) {
58 if (uniqueIds.contains(
match.id())) {
59 const QueryMatch &existentMatch = uniqueIds.value(
match.id());
60 if (existentMatch.runner() && existentMatch.runner()->d->hasWeakResults) {
62 matches.removeOne(existentMatch);
63 matches.append(match);
67 uniqueIds.insert(
match.id(), match);
68 matches.append(match);
72 matches.append(match);
85 bool m_isValid =
true;
88 int changedLaunchCounts = 0;
90 bool singleRunnerQueryMode =
false;
91 bool shouldIgnoreCurrentMatchForHistory =
false;
94 int requestedCursorPosition = 0;
95 qint64 queryStartTs = 0;
98RunnerContext::RunnerContext(RunnerManager *manager)
99 : d(new RunnerContextPrivate(manager))
110RunnerContext::~RunnerContext()
116 if (this->d == other.d) {
134void RunnerContext::reset()
158 d->uniqueIds.clear();
159 d->singleRunnerQueryMode =
false;
160 d->shouldIgnoreCurrentMatchForHistory =
false;
163void RunnerContext::setQuery(
const QString &term)
165 if (!this->query().isEmpty()) {
173 d->requestedText.clear();
185bool RunnerContext::isValid()
const
193 if (matches.
isEmpty() || !isValid()) {
203 if (
int count = d->launchCounts.value(match.id())) {
204 match.setRelevance(match.relevance() + 0.5 * (1 - exp(-count * 0.3)));
216 return addMatches({match});
226void RunnerContext::requestQueryStringUpdate(
const QString &text,
int cursorPosition)
const
228 d->requestedText = text;
229 d->requestedCursorPosition = cursorPosition;
232void RunnerContext::setSingleRunnerQueryMode(
bool enabled)
234 d->singleRunnerQueryMode = enabled;
237bool RunnerContext::singleRunnerQueryMode()
const
239 return d->singleRunnerQueryMode;
242void RunnerContext::ignoreCurrentMatchForHistory()
const
244 d->shouldIgnoreCurrentMatchForHistory =
true;
247bool RunnerContext::shouldIgnoreCurrentMatchForHistory()
const
249 return d->shouldIgnoreCurrentMatchForHistory;
266 for (
const QString &entry : cfgList) {
267 if (
int idx = entry.indexOf(
QLatin1Char(
' ')); idx != -1) {
268 const int count = entry.mid(0, idx).toInt();
270 d->launchCounts[id] = count;
277 if (d->changedLaunchCounts < __changeCountBeforeSaving) {
280 d->changedLaunchCounts = 0;
282 countList.
reserve(d->launchCounts.size());
283 for (
auto it = d->launchCounts.cbegin(), end = d->launchCounts.cend(); it != end; ++it) {
291void RunnerContext::increaseLaunchCount(
const QueryMatch &match)
293 ++d->launchCounts[
match.id()];
294 ++d->changedLaunchCounts;
297QString RunnerContext::requestedQueryString()
const
299 return d->requestedText;
301int RunnerContext::requestedCursorPosition()
const
303 return d->requestedCursorPosition;
306void RunnerContext::setJobStartTs(qint64 queryStartTs)
308 d->queryStartTs = queryStartTs;
310QString RunnerContext::runnerJobId(AbstractRunner *runner)
const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
A match returned by an AbstractRunner in response to a given RunnerContext.
The RunnerContext class provides information related to a search, including the search term and colle...
std::optional< QSqlQuery > query(const QString &queryStatement)
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
bool isEmpty() const const
void reserve(qsizetype size)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
QString number(double n, char format, int precision)