KRunner

querymatch.cpp
1 /*
2  * Copyright 2006-2007 Aaron Seigo <[email protected]>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "querymatch.h"
21 
22 #include <QAction>
23 #include <QIcon>
24 #include <QReadWriteLock>
25 #include <QSharedData>
26 #include <QVariant>
27 
28 #include "krunner_debug.h"
29 
30 #include "abstractrunner.h"
31 
32 namespace Plasma
33 {
34 
35 class QueryMatchPrivate : public QSharedData
36 {
37  public:
38  QueryMatchPrivate(AbstractRunner *r)
39  : QSharedData(),
40  lock(new QReadWriteLock(QReadWriteLock::Recursive)),
41  runner(r),
42  type(QueryMatch::ExactMatch),
43  relevance(.7),
44  selAction(nullptr),
45  enabled(true),
46  idSetByData(false)
47  {
48  }
49 
50  QueryMatchPrivate(const QueryMatchPrivate &other)
51  : QSharedData(other),
52  lock(new QReadWriteLock(QReadWriteLock::Recursive))
53  {
54  QReadLocker l(other.lock);
55  runner = other.runner;
56  type = other.type;
57  relevance = other.relevance;
58  selAction = other.selAction;
59  enabled = other.enabled;
60  idSetByData = other.idSetByData;
61  matchCategory = other.matchCategory;
62  id = other.id;
63  text = other.text;
64  subtext = other.subtext;
65  icon = other.icon;
66  iconName = other.iconName;
67  data = other.data;
68  mimeType = other.mimeType;
69  urls = other.urls;
70  }
71 
72  ~QueryMatchPrivate()
73  {
74  delete lock;
75  }
76 
77  QReadWriteLock *lock;
80  QString matchCategory;
81  QString id;
82  QString text;
83  QString subtext;
85  QList<QUrl> urls;
86  QIcon icon;
87  QString iconName;
88  QVariant data;
89  qreal relevance;
90  QAction *selAction;
91  bool enabled : 1;
92  bool idSetByData : 1;
93 };
94 
96  : d(new QueryMatchPrivate(runner))
97 {
98 // qCDebug(KRUNNER) << "new match created";
99 }
100 
102  : d(other.d)
103 {
104 }
105 
106 QueryMatch::~QueryMatch()
107 {
108 }
109 
111 {
112  return d->runner != nullptr;
113 }
114 
116 {
117  if (d->id.isEmpty() && d->runner) {
118  return d->runner.data()->id();
119  }
120 
121  return d->id;
122 }
123 
125 {
126  d->type = type;
127 }
128 
130 {
131  return d->type;
132 }
133 
135 {
136  d->matchCategory = category;
137 }
138 
140 {
141  if (d->matchCategory.isEmpty() && d->runner) {
142  return d->runner->name();
143  }
144  return d->matchCategory;
145 }
146 
148 {
149  d->relevance = qMax(qreal(0.0), relevance);
150 }
151 
153 {
154  return d->relevance;
155 }
156 
158 {
159  return d->runner.data();
160 }
161 
163 {
164  QWriteLocker locker(d->lock);
165  d->text = text;
166 }
167 
169 {
170  QWriteLocker locker(d->lock);
171  d->subtext = subtext;
172 }
173 
175 {
176  QWriteLocker locker(d->lock);
177  d->data = data;
178 
179  if (d->id.isEmpty() || d->idSetByData) {
180  const QString id = data.toString();
181  if (!id.isEmpty()) {
182  setId(data.toString());
183  d->idSetByData = true;
184  }
185  }
186 }
187 
188 void QueryMatch::setId(const QString &id)
189 {
190  QWriteLocker locker(d->lock);
191  if (d->runner) {
192  d->id = d->runner.data()->id();
193  }
194 
195  if (!id.isEmpty()) {
196  d->id.append(QLatin1Char('_')).append(id);
197  }
198 
199  d->idSetByData = false;
200 }
201 
203 {
204  QWriteLocker locker(d->lock);
205  d->icon = icon;
206 }
207 
209 {
210  QWriteLocker locker(d->lock);
211  d->iconName = iconName;
212 }
213 
215 {
216  QReadLocker locker(d->lock);
217  return d->data;
218 }
219 
221 {
222  QReadLocker locker(d->lock);
223  return d->text;
224 }
225 
227 {
228  QReadLocker locker(d->lock);
229  return d->subtext;
230 }
231 
233 {
234  QReadLocker locker(d->lock);
235  return d->icon;
236 }
237 
239 {
240  QReadLocker locker(d->lock);
241  return d->iconName;
242 }
243 
245 {
246  QWriteLocker locker(d->lock);
247  d->mimeType = mimeType;
248 }
249 
251 {
252  QReadLocker locker(d->lock);
253  return d->mimeType;
254 }
255 
257 {
258  QWriteLocker locker(d->lock);
259  d->urls = urls;
260 }
261 
263 {
264  QReadLocker locker(d->lock);
265  return d->urls;
266 }
267 
268 void QueryMatch::setEnabled(bool enabled)
269 {
270  d->enabled = enabled;
271 }
272 
274 {
275  return d->enabled && d->runner;
276 }
277 
279 {
280  return d->selAction;
281 }
282 
284 {
285  d->selAction = action;
286 }
287 
288 bool QueryMatch::operator<(const QueryMatch &other) const
289 {
290  if (d->type == other.d->type) {
291  if (isEnabled() != other.isEnabled()) {
292  return other.isEnabled();
293  }
294 
295  if (!qFuzzyCompare(d->relevance, other.d->relevance)) {
296  return d->relevance < other.d->relevance;
297  }
298 
299  QReadLocker locker(d->lock);
300  QReadLocker otherLocker(other.d->lock);
301  // when resorting to sort by alpha, we want the
302  // reverse sort order!
303  return d->text > other.d->text;
304  }
305 
306  return d->type < other.d->type;
307 }
308 
309 QueryMatch &QueryMatch::operator=(const QueryMatch &other)
310 {
311  if (d != other.d) {
312  d = other.d;
313  }
314 
315  return *this;
316 }
317 
318 bool QueryMatch::operator==(const QueryMatch &other) const
319 {
320  return (d == other.d);
321 }
322 
323 bool QueryMatch::operator!=(const QueryMatch &other) const
324 {
325  return (d != other.d);
326 }
327 
328 void QueryMatch::run(const RunnerContext &context) const
329 {
330  //qCDebug(KRUNNER) << "we run the term" << context.query() << "whose type is" << d->mimeType;
331  if (d->runner) {
332  d->runner.data()->run(context, *this);
333  }
334 }
335 
336 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 71)
338 {
339  return false;
340 }
341 #endif
342 
343 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 71)
345 {
346  Q_UNUSED(parent)
347 }
348 #endif
349 
350 } // Plasma namespace
351 
void setMatchCategory(const QString &category)
Sets information about the type of the match which can be used to categorize the match.
Definition: querymatch.cpp:134
qreal relevance() const
The relevance of this action to the search.
Definition: querymatch.cpp:152
An abstract base class for Plasma Runner plugins.
void setSelectedAction(QAction *action)
Sets the selected action.
Definition: querymatch.cpp:283
void setEnabled(bool enable)
Sets whether or not this match can be activited.
Definition: querymatch.cpp:268
QList< QUrl > urls() const
Definition: querymatch.cpp:262
QueryMatch(AbstractRunner *runner=nullptr)
Constructs a PossibleMatch associated with a given RunnerContext and runner.
Definition: querymatch.cpp:95
void setId(const QString &id)
Sets the id for this match; useful if the id does not match data().toString().
Definition: querymatch.cpp:188
void createConfigurationInterface(QWidget *parent)
If hasConfigurationInterface() returns true, this method may be called to get a widget displaying the...
Definition: querymatch.cpp:344
Type
The type of match.
Definition: querymatch.h:54
QIcon icon() const
Definition: querymatch.cpp:232
void run(const RunnerContext &context) const
Requests this match to activae using the given context.
Definition: querymatch.cpp:328
void setIcon(const QIcon &icon)
Sets the icon associated with this match.
Definition: querymatch.cpp:202
bool isEnabled() const
Definition: querymatch.cpp:273
void setIconName(const QString &iconName)
Sets the icon name associated with this match.
Definition: querymatch.cpp:208
QString text() const
Definition: querymatch.cpp:220
void setMimeType(const QString &mimeType)
Sets the MimeType, if any, associated with this match.
Definition: querymatch.cpp:244
void setSubtext(const QString &text)
Sets the descriptive text for this match; can be longer than the main title text. ...
Definition: querymatch.cpp:168
Type type(const QSqlDatabase &db)
QString subtext() const
Definition: querymatch.cpp:226
Type type() const
The type of action this is.
Definition: querymatch.cpp:129
void setText(const QString &text)
Sets the main title text for this match; should be short enough to fit nicely on one line in a user i...
Definition: querymatch.cpp:162
void setData(const QVariant &data)
Sets data to be used internally by the associated AbstractRunner.
Definition: querymatch.cpp:174
QVariant data() const
Definition: querymatch.cpp:214
void setType(Type type)
Sets the type of match this action represents.
Definition: querymatch.cpp:124
QString id() const
Definition: querymatch.cpp:115
The RunnerContext class provides information related to a search, including the search term...
Definition: runnercontext.h:45
QAction * selectedAction() const
The current action.
Definition: querymatch.cpp:278
QString iconName() const
Definition: querymatch.cpp:238
A match returned by an AbstractRunner in response to a given RunnerContext.
Definition: querymatch.h:48
void setUrls(const QList< QUrl > &urls)
Sets the urls, if any, associated with this match.
Definition: querymatch.cpp:256
QString matchCategory() const
Extra information about the match which can be used to categorize the type.
Definition: querymatch.cpp:139
void setRelevance(qreal relevance)
Sets the relevance of this action for the search it was created for.
Definition: querymatch.cpp:147
AbstractRunner * runner() const
Definition: querymatch.cpp:157
QString mimeType() const
Definition: querymatch.cpp:250
QString mimeType(Type)
QString toString() const const
bool hasConfigurationInterface() const
Definition: querymatch.cpp:337
bool isValid() const
Definition: querymatch.cpp:110
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 22:54:26 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.