1/*
2 SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "querymatch.h"
8#include "action.h"
9
10#include <QIcon>
11#include <QPointer>
12#include <QReadWriteLock>
13#include <QSharedData>
14#include <QVariant>
15
16#include "abstractrunner.h"
17#include "abstractrunner_p.h"
18
19namespace KRunner
20{
21class QueryMatchPrivate : public QSharedData
22{
23public:
24 explicit QueryMatchPrivate(AbstractRunner *r)
25 : QSharedData()
26 , runner(r)
27 {
28 }
29
30 QueryMatchPrivate(const QueryMatchPrivate &other)
31 : QSharedData(other)
32 {
33 QReadLocker l(&other.lock);
34 runner = other.runner;
35 categoryRelevance = other.categoryRelevance;
36 relevance = other.relevance;
37 selAction = other.selAction;
38 enabled = other.enabled;
39 idSetByData = other.idSetByData;
40 matchCategory = other.matchCategory;
41 id = other.id;
42 text = other.text;
43 subtext = other.subtext;
44 icon = other.icon;
45 iconName = other.iconName;
46 data = other.data;
47 urls = other.urls;
48 actions = other.actions;
49 multiLine = other.multiLine;
50 }
51
52 void setId(const QString &newId)
53 {
54 if (runner && runner->d->hasUniqueResults) {
55 id = newId;
56 } else {
57 if (runner) {
58 id = runner->id();
59 }
60 if (!id.isEmpty()) {
61 id.append(c: QLatin1Char('_')).append(s: newId);
62 }
63 }
64 idSetByData = false;
65 }
66
67 mutable QReadWriteLock lock;
68 QPointer<AbstractRunner> runner;
69 QString matchCategory;
70 QString id;
71 QString text;
72 QString subtext;
73 QString mimeType;
74 QList<QUrl> urls;
75 QIcon icon;
76 QString iconName;
77 QVariant data;
78 qreal categoryRelevance = 50;
79 qreal relevance = .7;
80 KRunner::Action selAction;
81 KRunner::Actions actions;
82 bool enabled = true;
83 bool idSetByData = false;
84 bool multiLine = false;
85};
86
87QueryMatch::QueryMatch(AbstractRunner *runner)
88 : d(new QueryMatchPrivate(runner))
89{
90}
91
92QueryMatch::QueryMatch(const QueryMatch &other)
93
94 = default;
95
96QueryMatch::~QueryMatch() = default;
97
98bool QueryMatch::isValid() const
99{
100 return d->runner != nullptr;
101}
102
103QString QueryMatch::id() const
104{
105 if (d->id.isEmpty() && d->runner) {
106 return d->runner->id();
107 }
108
109 return d->id;
110}
111
112void QueryMatch::setCategoryRelevance(qreal relevance)
113{
114 d->categoryRelevance = qBound(min: 0.0, val: relevance, max: 100.0);
115}
116
117qreal QueryMatch::categoryRelevance() const
118{
119 return d->categoryRelevance;
120}
121
122void QueryMatch::setMatchCategory(const QString &category)
123{
124 d->matchCategory = category;
125}
126
127QString QueryMatch::matchCategory() const
128{
129 if (d->matchCategory.isEmpty() && d->runner) {
130 return d->runner->name();
131 }
132 return d->matchCategory;
133}
134
135void QueryMatch::setRelevance(qreal relevance)
136{
137 d->relevance = qMax(a: qreal(0.0), b: relevance);
138}
139
140qreal QueryMatch::relevance() const
141{
142 return d->relevance;
143}
144
145AbstractRunner *QueryMatch::runner() const
146{
147 return d->runner.data();
148}
149
150void QueryMatch::setText(const QString &text)
151{
152 QWriteLocker locker(&d->lock);
153 d->text = text;
154}
155
156void QueryMatch::setSubtext(const QString &subtext)
157{
158 QWriteLocker locker(&d->lock);
159 d->subtext = subtext;
160}
161
162void QueryMatch::setData(const QVariant &data)
163{
164 QWriteLocker locker(&d->lock);
165 d->data = data;
166
167 if (d->id.isEmpty() || d->idSetByData) {
168 const QString matchId = data.toString();
169 if (!matchId.isEmpty()) {
170 d->setId(matchId);
171 d->idSetByData = true;
172 }
173 }
174}
175
176void QueryMatch::setId(const QString &id)
177{
178 QWriteLocker locker(&d->lock);
179 d->setId(id);
180}
181
182void QueryMatch::setIcon(const QIcon &icon)
183{
184 QWriteLocker locker(&d->lock);
185 d->icon = icon;
186}
187
188void QueryMatch::setIconName(const QString &iconName)
189{
190 QWriteLocker locker(&d->lock);
191 d->iconName = iconName;
192}
193
194QVariant QueryMatch::data() const
195{
196 QReadLocker locker(&d->lock);
197 return d->data;
198}
199
200QString QueryMatch::text() const
201{
202 QReadLocker locker(&d->lock);
203 return d->text;
204}
205
206QString QueryMatch::subtext() const
207{
208 QReadLocker locker(&d->lock);
209 return d->subtext;
210}
211
212QIcon QueryMatch::icon() const
213{
214 QReadLocker locker(&d->lock);
215 return d->icon;
216}
217
218QString QueryMatch::iconName() const
219{
220 QReadLocker locker(&d->lock);
221 return d->iconName;
222}
223
224void QueryMatch::setUrls(const QList<QUrl> &urls)
225{
226 QWriteLocker locker(&d->lock);
227 d->urls = urls;
228}
229
230QList<QUrl> QueryMatch::urls() const
231{
232 QReadLocker locker(&d->lock);
233 return d->urls;
234}
235
236void QueryMatch::setEnabled(bool enabled)
237{
238 d->enabled = enabled;
239}
240
241bool QueryMatch::isEnabled() const
242{
243 return d->enabled && d->runner;
244}
245
246KRunner::Action QueryMatch::selectedAction() const
247{
248 return d->selAction;
249}
250
251void QueryMatch::setSelectedAction(const KRunner::Action &action)
252{
253 d->selAction = action;
254}
255
256void QueryMatch::setMultiLine(bool multiLine)
257{
258 d->multiLine = multiLine;
259}
260
261bool QueryMatch::isMultiLine() const
262{
263 return d->multiLine;
264}
265
266QueryMatch &QueryMatch::operator=(const QueryMatch &other)
267{
268 if (d != other.d) {
269 d = other.d;
270 }
271
272 return *this;
273}
274
275bool QueryMatch::operator==(const QueryMatch &other) const
276{
277 return (d == other.d);
278}
279
280bool QueryMatch::operator!=(const QueryMatch &other) const
281{
282 return (d != other.d);
283}
284
285void QueryMatch::setActions(const QList<KRunner::Action> &actions)
286{
287 QWriteLocker locker(&d->lock);
288 d->actions = actions;
289}
290
291void QueryMatch::addAction(const KRunner::Action &action)
292{
293 QWriteLocker locker(&d->lock);
294 d->actions << action;
295}
296
297KRunner::Actions QueryMatch::actions() const
298{
299 QReadLocker locker(&d->lock);
300 return d->actions;
301}
302
303QDebug operator<<(QDebug debug, const KRunner::QueryMatch &match)
304{
305 QDebugStateSaver saver(debug);
306 debug.nospace() << "QueryMatch(category: " << match.matchCategory() << " text:" << match.text() << ")";
307 return debug;
308}
309
310} // KRunner namespace
311

source code of krunner/src/querymatch.cpp