Changeset View
Changeset View
Standalone View
Standalone View
runners/services/servicerunner.cpp
Show All 28 Lines | |||||
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 | 34 | | |||
35 | #include "debug.h" | 35 | #include "debug.h" | ||
36 | 36 | | |||
37 | namespace { | ||||
38 | | ||||
39 | int weightedLength(const QString &query) { | ||||
40 | int length = 0; | ||||
41 | auto chrs = query.toUcs4(); | ||||
42 | for (auto chr : chrs) { | ||||
43 | auto script = QChar::script(chr); | ||||
44 | if (script == QChar::Script_Han || | ||||
45 | script == QChar::Script_Hangul || | ||||
46 | script == QChar::Script_Hiragana || | ||||
47 | script == QChar::Script_Katakana || | ||||
48 | script == QChar::Script_Yi || | ||||
49 | QChar::isHighSurrogate(chr)) { | ||||
50 | length += 2; | ||||
51 | } else { | ||||
52 | length += 1; | ||||
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. | |||||
53 | } | ||||
54 | } | ||||
55 | return length; | ||||
56 | } | ||||
57 | | ||||
58 | } // namespace | ||||
59 | | ||||
37 | /** | 60 | /** | ||
38 | * @brief Finds all KServices for a given runner query | 61 | * @brief Finds all KServices for a given runner query | ||
39 | */ | 62 | */ | ||
40 | class ServiceFinder | 63 | class ServiceFinder | ||
41 | { | 64 | { | ||
42 | public: | 65 | public: | ||
43 | ServiceFinder(ServiceRunner *runner) | 66 | ServiceFinder(ServiceRunner *runner) | ||
44 | : m_runner(runner) | 67 | : m_runner(runner) | ||
45 | {} | 68 | {} | ||
46 | 69 | | |||
47 | 70 | | |||
48 | void match(Plasma::RunnerContext &context) | 71 | void match(Plasma::RunnerContext &context) | ||
49 | { | 72 | { | ||
50 | if (!context.isValid()) { | 73 | if (!context.isValid()) { | ||
51 | return; | 74 | return; | ||
52 | } | 75 | } | ||
53 | 76 | | |||
54 | term = context.query(); | 77 | term = context.query(); | ||
78 | weightedTermLength = weightedLength(term); | ||||
55 | 79 | | |||
56 | matchExectuables(); | 80 | matchExectuables(); | ||
57 | matchNameKeywordAndGenericName(); | 81 | matchNameKeywordAndGenericName(); | ||
58 | matchCategories(); | 82 | matchCategories(); | ||
59 | matchJumpListActions(); | 83 | matchJumpListActions(); | ||
60 | 84 | | |||
61 | context.addMatches(matches); | 85 | context.addMatches(matches); | ||
62 | } | 86 | } | ||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Line(s) | 178 | { | |||
165 | 189 | | |||
166 | if (!service->icon().isEmpty()) { | 190 | if (!service->icon().isEmpty()) { | ||
167 | match.setIconName(service->icon()); | 191 | match.setIconName(service->icon()); | ||
168 | } | 192 | } | ||
169 | } | 193 | } | ||
170 | 194 | | |||
171 | void matchExectuables() | 195 | void matchExectuables() | ||
172 | { | 196 | { | ||
173 | if (term.length() < 2) { | 197 | if (weightedTermLength < 2) { | ||
174 | return; | 198 | return; | ||
175 | } | 199 | } | ||
176 | 200 | | |||
177 | // Search for applications which are executable and case-insensitively match the search term | 201 | // 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 | 202 | // See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language | ||
179 | // if the following is unclear to you. | 203 | // if the following is unclear to you. | ||
180 | query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term); | 204 | query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term); | ||
181 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | 205 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | ||
Show All 16 Lines | |||||
198 | } | 222 | } | ||
199 | 223 | | |||
200 | void matchNameKeywordAndGenericName() | 224 | void matchNameKeywordAndGenericName() | ||
201 | { | 225 | { | ||
202 | //Splitting the query term to match using subsequences | 226 | //Splitting the query term to match using subsequences | ||
203 | QVector<QStringRef> queryList = term.splitRef(QLatin1Char(' ')); | 227 | QVector<QStringRef> queryList = term.splitRef(QLatin1Char(' ')); | ||
204 | 228 | | |||
205 | // If the term length is < 3, no real point searching the Keywords and GenericName | 229 | // If the term length is < 3, no real point searching the Keywords and GenericName | ||
206 | if (term.length() < 3) { | 230 | if (weightedTermLength < 3) { | ||
207 | query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term); | 231 | query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term); | ||
208 | } else { | 232 | } else { | ||
209 | //Match using subsequences (Bug: 262837) | 233 | //Match using subsequences (Bug: 262837) | ||
210 | query = generateQuery(queryList); | 234 | query = generateQuery(queryList); | ||
211 | } | 235 | } | ||
212 | 236 | | |||
213 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | 237 | KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); | ||
214 | services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query); | 238 | services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query); | ||
Show All 10 Lines | 241 | foreach (const KService::Ptr &service, services) { | |||
225 | 249 | | |||
226 | Plasma::QueryMatch match(m_runner); | 250 | Plasma::QueryMatch match(m_runner); | ||
227 | match.setType(Plasma::QueryMatch::PossibleMatch); | 251 | match.setType(Plasma::QueryMatch::PossibleMatch); | ||
228 | setupMatch(service, match); | 252 | setupMatch(service, match); | ||
229 | qreal relevance(0.6); | 253 | qreal relevance(0.6); | ||
230 | 254 | | |||
231 | // If the term was < 3 chars and NOT at the beginning of the App's name or Exec, then | 255 | // 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. | 256 | // chances are the user doesn't want that app. | ||
233 | if (term.length() < 3) { | 257 | if (weightedTermLength < 3) { | ||
234 | if (name.startsWith(term) || exec.startsWith(term)) { | 258 | if (name.startsWith(term) || exec.startsWith(term)) { | ||
235 | relevance = 0.9; | 259 | relevance = 0.9; | ||
236 | } else { | 260 | } else { | ||
237 | continue; | 261 | continue; | ||
238 | } | 262 | } | ||
239 | } else if (service->name().contains(queryList[0], Qt::CaseInsensitive)) { | 263 | } else if (service->name().contains(queryList[0], Qt::CaseInsensitive)) { | ||
240 | relevance = 0.8; | 264 | relevance = 0.8; | ||
241 | relevance += increaseMatchRelavance(service, queryList, "Name"); | 265 | relevance += increaseMatchRelavance(service, queryList, "Name"); | ||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 330 | foreach (const KService::Ptr &service, services) { | |||
325 | 349 | | |||
326 | match.setRelevance(relevance); | 350 | match.setRelevance(relevance); | ||
327 | matches << match; | 351 | matches << match; | ||
328 | } | 352 | } | ||
329 | } | 353 | } | ||
330 | 354 | | |||
331 | void matchJumpListActions() | 355 | void matchJumpListActions() | ||
332 | { | 356 | { | ||
333 | if (term.length() < 3) { | 357 | if (weightedTermLength < 3) { | ||
334 | return; | 358 | return; | ||
335 | } | 359 | } | ||
336 | 360 | | |||
337 | query = QStringLiteral("exist Actions"); // doesn't work | 361 | query = QStringLiteral("exist Actions"); // doesn't work | ||
338 | auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query); | 362 | auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query); | ||
339 | 363 | | |||
340 | foreach (const KService::Ptr &service, services) { | 364 | foreach (const KService::Ptr &service, services) { | ||
341 | if (service->noDisplay()) { | 365 | if (service->noDisplay()) { | ||
Show All 35 Lines | |||||
377 | } | 401 | } | ||
378 | 402 | | |||
379 | ServiceRunner *m_runner; | 403 | ServiceRunner *m_runner; | ||
380 | QSet<QString> m_seen; | 404 | QSet<QString> m_seen; | ||
381 | 405 | | |||
382 | QList<Plasma::QueryMatch> matches; | 406 | QList<Plasma::QueryMatch> matches; | ||
383 | QString query; | 407 | QString query; | ||
384 | QString term; | 408 | QString term; | ||
409 | int weightedTermLength; | ||||
385 | }; | 410 | }; | ||
386 | 411 | | |||
387 | ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args) | 412 | ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args) | ||
388 | : Plasma::AbstractRunner(parent, args) | 413 | : Plasma::AbstractRunner(parent, args) | ||
389 | { | 414 | { | ||
390 | Q_UNUSED(args) | 415 | Q_UNUSED(args) | ||
391 | 416 | | |||
392 | setObjectName( QStringLiteral("Application" )); | 417 | 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?