Changeset View
Changeset View
Standalone View
Standalone View
runners/services/servicerunner.cpp
Show All 25 Lines | |||||
26 | #include <QDebug> | 26 | #include <QDebug> | ||
27 | #include <QUrl> | 27 | #include <QUrl> | ||
28 | 28 | | |||
29 | #include <KActivities/ResourceInstance> | 29 | #include <KActivities/ResourceInstance> | ||
30 | #include <KLocalizedString> | 30 | #include <KLocalizedString> | ||
31 | #include <KRun> | 31 | #include <KRun> | ||
32 | #include <KService> | 32 | #include <KService> | ||
33 | #include <KServiceTypeTrader> | 33 | #include <KServiceTypeTrader> | ||
34 | #include <kcoreaddons_version.h> | ||||
35 | #if KCOREADDONS_VERSION >= QT_VERSION_CHECK(5, 41, 0) | ||||
36 | #include <KStringHandler> | ||||
37 | #endif | ||||
34 | 38 | | |||
35 | #include "debug.h" | 39 | #include "debug.h" | ||
36 | 40 | | |||
41 | namespace { | ||||
42 | | ||||
43 | int weightedLength(const QString &query) { | ||||
44 | #if KCOREADDONS_VERSION >= QT_VERSION_CHECK(5, 41, 0) | ||||
45 | return KStringHandler::logicalLength(query); | ||||
46 | #else | ||||
47 | return query.length(); | ||||
48 | #endif | ||||
49 | } | ||||
50 | | ||||
51 | } // namespace | ||||
52 | | ||||
37 | /** | 53 | /** | ||
38 | * @brief Finds all KServices for a given runner query | 54 | * @brief Finds all KServices for a given runner query | ||
39 | */ | 55 | */ | ||
40 | class ServiceFinder | 56 | class ServiceFinder | ||
cfeck: Since there is no 'else', the length is increased by 3 for Han characters. Is this intended? | |||||
xuetianweng: Yeah you're right.. I intended to use else here. | |||||
41 | { | 57 | { | ||
42 | public: | 58 | public: | ||
43 | ServiceFinder(ServiceRunner *runner) | 59 | ServiceFinder(ServiceRunner *runner) | ||
44 | : m_runner(runner) | 60 | : m_runner(runner) | ||
45 | {} | 61 | {} | ||
46 | 62 | | |||
47 | 63 | | |||
48 | void match(Plasma::RunnerContext &context) | 64 | void match(Plasma::RunnerContext &context) | ||
49 | { | 65 | { | ||
50 | if (!context.isValid()) { | 66 | if (!context.isValid()) { | ||
51 | return; | 67 | return; | ||
52 | } | 68 | } | ||
53 | 69 | | |||
54 | term = context.query(); | 70 | term = context.query(); | ||
71 | weightedTermLength = weightedLength(term); | ||||
55 | 72 | | |||
56 | matchExectuables(); | 73 | matchExectuables(); | ||
57 | matchNameKeywordAndGenericName(); | 74 | matchNameKeywordAndGenericName(); | ||
58 | matchCategories(); | 75 | matchCategories(); | ||
59 | matchJumpListActions(); | 76 | matchJumpListActions(); | ||
60 | 77 | | |||
61 | context.addMatches(matches); | 78 | context.addMatches(matches); | ||
62 | } | 79 | } | ||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Line(s) | 171 | { | |||
165 | 182 | | |||
166 | if (!service->icon().isEmpty()) { | 183 | if (!service->icon().isEmpty()) { | ||
167 | match.setIconName(service->icon()); | 184 | match.setIconName(service->icon()); | ||
168 | } | 185 | } | ||
169 | } | 186 | } | ||
170 | 187 | | |||
171 | void matchExectuables() | 188 | void matchExectuables() | ||
172 | { | 189 | { | ||
173 | if (term.length() < 2) { | 190 | if (weightedTermLength < 2) { | ||
174 | return; | 191 | return; | ||
175 | } | 192 | } | ||
176 | 193 | | |||
177 | // Search for applications which are executable and case-insensitively match the search term | 194 | // Search for applications which are executable and case-insensitively match the search term | ||
178 | // See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language | 195 | // See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language | ||
179 | // if the following is unclear to you. | 196 | // if the following is unclear to you. | ||
180 | query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term); | 197 | query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term); | ||
181 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | 198 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | ||
Show All 16 Lines | |||||
198 | } | 215 | } | ||
199 | 216 | | |||
200 | void matchNameKeywordAndGenericName() | 217 | void matchNameKeywordAndGenericName() | ||
201 | { | 218 | { | ||
202 | //Splitting the query term to match using subsequences | 219 | //Splitting the query term to match using subsequences | ||
203 | QVector<QStringRef> queryList = term.splitRef(QLatin1Char(' ')); | 220 | QVector<QStringRef> queryList = term.splitRef(QLatin1Char(' ')); | ||
204 | 221 | | |||
205 | // If the term length is < 3, no real point searching the Keywords and GenericName | 222 | // If the term length is < 3, no real point searching the Keywords and GenericName | ||
206 | if (term.length() < 3) { | 223 | if (weightedTermLength < 3) { | ||
207 | query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term); | 224 | query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term); | ||
208 | } else { | 225 | } else { | ||
209 | //Match using subsequences (Bug: 262837) | 226 | //Match using subsequences (Bug: 262837) | ||
210 | query = generateQuery(queryList); | 227 | query = generateQuery(queryList); | ||
211 | } | 228 | } | ||
212 | 229 | | |||
213 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | 230 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | ||
214 | services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query); | 231 | services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query); | ||
Show All 10 Lines | 234 | foreach (const KService::Ptr &service, services) { | |||
225 | 242 | | |||
226 | Plasma::QueryMatch match(m_runner); | 243 | Plasma::QueryMatch match(m_runner); | ||
227 | match.setType(Plasma::QueryMatch::PossibleMatch); | 244 | match.setType(Plasma::QueryMatch::PossibleMatch); | ||
228 | setupMatch(service, match); | 245 | setupMatch(service, match); | ||
229 | qreal relevance(0.6); | 246 | qreal relevance(0.6); | ||
230 | 247 | | |||
231 | // If the term was < 3 chars and NOT at the beginning of the App's name or Exec, then | 248 | // If the term was < 3 chars and NOT at the beginning of the App's name or Exec, then | ||
232 | // chances are the user doesn't want that app. | 249 | // chances are the user doesn't want that app. | ||
233 | if (term.length() < 3) { | 250 | if (weightedTermLength < 3) { | ||
234 | if (name.startsWith(term) || exec.startsWith(term)) { | 251 | if (name.startsWith(term) || exec.startsWith(term)) { | ||
235 | relevance = 0.9; | 252 | relevance = 0.9; | ||
236 | } else { | 253 | } else { | ||
237 | continue; | 254 | continue; | ||
238 | } | 255 | } | ||
239 | } else if (service->name().contains(queryList[0], Qt::CaseInsensitive)) { | 256 | } else if (service->name().contains(queryList[0], Qt::CaseInsensitive)) { | ||
240 | relevance = 0.8; | 257 | relevance = 0.8; | ||
241 | relevance += increaseMatchRelavance(service, queryList, "Name"); | 258 | relevance += increaseMatchRelavance(service, queryList, "Name"); | ||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 323 | foreach (const KService::Ptr &service, services) { | |||
325 | 342 | | |||
326 | match.setRelevance(relevance); | 343 | match.setRelevance(relevance); | ||
327 | matches << match; | 344 | matches << match; | ||
328 | } | 345 | } | ||
329 | } | 346 | } | ||
330 | 347 | | |||
331 | void matchJumpListActions() | 348 | void matchJumpListActions() | ||
332 | { | 349 | { | ||
333 | if (term.length() < 3) { | 350 | if (weightedTermLength < 3) { | ||
334 | return; | 351 | return; | ||
335 | } | 352 | } | ||
336 | 353 | | |||
337 | query = QStringLiteral("exist Actions"); // doesn't work | 354 | query = QStringLiteral("exist Actions"); // doesn't work | ||
338 | auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query); | 355 | auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query); | ||
339 | 356 | | |||
340 | foreach (const KService::Ptr &service, services) { | 357 | foreach (const KService::Ptr &service, services) { | ||
341 | if (service->noDisplay()) { | 358 | if (service->noDisplay()) { | ||
Show All 35 Lines | |||||
377 | } | 394 | } | ||
378 | 395 | | |||
379 | ServiceRunner *m_runner; | 396 | ServiceRunner *m_runner; | ||
380 | QSet<QString> m_seen; | 397 | QSet<QString> m_seen; | ||
381 | 398 | | |||
382 | QList<Plasma::QueryMatch> matches; | 399 | QList<Plasma::QueryMatch> matches; | ||
383 | QString query; | 400 | QString query; | ||
384 | QString term; | 401 | QString term; | ||
402 | int weightedTermLength; | ||||
385 | }; | 403 | }; | ||
386 | 404 | | |||
387 | ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args) | 405 | ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args) | ||
388 | : Plasma::AbstractRunner(parent, args) | 406 | : Plasma::AbstractRunner(parent, args) | ||
389 | { | 407 | { | ||
390 | Q_UNUSED(args) | 408 | Q_UNUSED(args) | ||
391 | 409 | | |||
392 | setObjectName( QStringLiteral("Application" )); | 410 | setObjectName( QStringLiteral("Application" )); | ||
▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |
Since there is no 'else', the length is increased by 3 for Han characters. Is this intended?