servicerunner.cpp 18.1 KB
Newer Older
1
2
/*
 *   Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
3
 *   Copyright (C) 2014 Vishesh Handa <vhanda@kde.org>
4
 *   Copyright (C) 2016-2020 Harald Sitter <sitter@kde.org>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2 as
 *   published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "servicerunner.h"

23
24
#include <algorithm>

25
26
27
#include <QMimeData>

#include <QDebug>
28
29
30
#include <QDir>
#include <QIcon>
#include <QStandardPaths>
31
#include <QUrl>
32
33

#include <KActivities/ResourceInstance>
34
#include <KLocalizedString>
35
#include <KNotificationJobUiDelegate>
36
#include <KService>
37
#include <KServiceAction>
38
#include <KServiceTypeTrader>
39
#include <KStringHandler>
40
#include <KSycoca>
41

42
43
#include <KIO/ApplicationLauncherJob>

44
#include "debug.h"
45

46
47
48
49
50
51
52
53
namespace {

int weightedLength(const QString &query) {
    return KStringHandler::logicalLength(query);
}

}  // namespace

54
55
56
57
/**
 * @brief Finds all KServices for a given runner query
 */
class ServiceFinder
58
{
59
60
61
62
public:
    ServiceFinder(ServiceRunner *runner)
         : m_runner(runner)
    {}
63
64


65
66
67
68
69
    void match(Plasma::RunnerContext &context)
    {
        if (!context.isValid()) {
            return;
        }
70

71
72
        KSycoca::disableAutoRebuild();

73
        term = context.query();
74
        weightedTermLength = weightedLength(term);
75

76
77
78
79
        matchExectuables();
        matchNameKeywordAndGenericName();
        matchCategories();
        matchJumpListActions();
80

81
82
        context.addMatches(matches);
    }
83

84
private:
85

86
87
88
89
90
    void seen(const KService::Ptr &service)
    {
        m_seen.insert(service->storageId());
        m_seen.insert(service->exec());
    }
91

92
93
94
    void seen(const KServiceAction &action)
    {
        m_seen.insert(action.exec());
95
96
    }

97
98
    bool hasSeen(const KService::Ptr &service)
    {
99
        return m_seen.contains(service->storageId()) &&
100
               m_seen.contains(service->exec());
101
102
    }

103
104
105
    bool hasSeen(const KServiceAction &action)
    {
        return m_seen.contains(action.exec());
106
107
    }

108
109
110
    bool disqualify(const KService::Ptr &service)
    {
        auto ret = hasSeen(service) || service->noDisplay();
111
        qCDebug(RUNNER_SERVICES) << service->name() << "disqualified?" << ret;
112
113
114
115
        seen(service);
        return ret;
    }

116
    qreal increaseMatchRelavance(const KService::Ptr &service, const QVector<QStringRef> &strList, const QString &category)
117
118
119
120
    {
        //Increment the relevance based on all the words (other than the first) of the query list
        qreal relevanceIncrement = 0;

121
122
        for(int i = 1; i < strList.size(); ++i) {
            const auto &str = strList.at(i);
123
            if (category == QLatin1String("Name")) {
124
                if (service->name().contains(str, Qt::CaseInsensitive)) {
125
126
                    relevanceIncrement += 0.01;
                }
127
            } else if (category == QLatin1String("GenericName")) {
128
                if (service->genericName().contains(str, Qt::CaseInsensitive)) {
129
130
                    relevanceIncrement += 0.01;
                }
131
            } else if (category == QLatin1String("Exec")) {
132
                if (service->exec().contains(str, Qt::CaseInsensitive)) {
133
134
                    relevanceIncrement += 0.01;
                }
135
            } else if (category == QLatin1String("Comment")) {
136
                if (service->comment().contains(str, Qt::CaseInsensitive)) {
137
138
139
140
141
142
143
144
                    relevanceIncrement += 0.01;
                }
            }
        }

        return relevanceIncrement;
    }

Méven Car's avatar
Méven Car committed
145
    QString generateQuery(const QVector<QStringRef> &strList)
146
    {
147
148
149
150
        QString keywordTemplate = QStringLiteral("exist Keywords");
        QString genericNameTemplate = QStringLiteral("exist GenericName");
        QString nameTemplate = QStringLiteral("exist Name");
        QString commentTemplate = QStringLiteral("exist Comment");
151
152
153
154
155
156
157

        // Search for applications which are executable and the term case-insensitive matches any of
        // * a substring of one of the keywords
        // * a substring of the GenericName field
        // * a substring of the Name field
        // Note that before asking for the content of e.g. Keywords and GenericName we need to ask if
        // they exist to prevent a tree evaluation error if they are not defined.
Méven Car's avatar
Méven Car committed
158
        for (const QStringRef &str : strList)
159
        {
160
161
162
163
            keywordTemplate += QStringLiteral(" and '%1' ~subin Keywords").arg(str.toString());
            genericNameTemplate += QStringLiteral(" and '%1' ~~ GenericName").arg(str.toString());
            nameTemplate += QStringLiteral(" and '%1' ~~ Name").arg(str.toString());
            commentTemplate += QStringLiteral(" and '%1' ~~ Comment").arg(str.toString());
164
165
166
167
168
169
170
171
172
        }

        QString finalQuery = QStringLiteral("exist Exec and ( (%1) or (%2) or (%3) or ('%4' ~~ Exec) or (%5) )")
            .arg(keywordTemplate, genericNameTemplate, nameTemplate, strList[0].toString(), commentTemplate);

        qCDebug(RUNNER_SERVICES) << "Final query : " << finalQuery;
        return finalQuery;
    }

173
174
175
176
177
    void setupMatch(const KService::Ptr &service, Plasma::QueryMatch &match)
    {
        const QString name = service->name();

        match.setText(name);
178
179
180
181

        QUrl url(service->storageId());
        url.setScheme(QStringLiteral("applications"));
        match.setData(url);
182

183
184
185
186
        if (!service->genericName().isEmpty() && service->genericName() != name) {
            match.setSubtext(service->genericName());
        } else if (!service->comment().isEmpty()) {
            match.setSubtext(service->comment());
187
188
        }

189
190
191
192
        if (!service->icon().isEmpty()) {
            match.setIconName(service->icon());
        }
    }
193

194
195
    void matchExectuables()
    {
196
        if (weightedTermLength < 2) {
197
            return;
198
199
        }

200
        // Search for applications which are executable and case-insensitively match the search term
201
        // See https://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language
202
203
        // if the following is unclear to you.
        query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term);
Méven Car's avatar
Méven Car committed
204
        const KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
205

206
207
208
        if (services.isEmpty()) {
            return;
        }
209

Méven Car's avatar
Méven Car committed
210
        for (const KService::Ptr &service : services) {
211
            qCDebug(RUNNER_SERVICES) << service->name() << "is an exact match!" << service->storageId() << service->exec();
212
            if (disqualify(service)) {
213
214
                continue;
            }
215
216
217
218
219
220
221
            Plasma::QueryMatch match(m_runner);
            match.setType(Plasma::QueryMatch::ExactMatch);
            setupMatch(service, match);
            match.setRelevance(1);
            matches << match;
        }
    }
222

223
224
    void matchNameKeywordAndGenericName()
    {
225
226
227
        //Splitting the query term to match using subsequences
        QVector<QStringRef> queryList = term.splitRef(QLatin1Char(' '));

228
        // If the term length is < 3, no real point searching the Keywords and GenericName
229
        if (weightedTermLength < 3) {
230
231
            query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term);
        } else {
232
233
            //Match using subsequences (Bug: 262837)
            query = generateQuery(queryList);
234
        }
235

236
237
        KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
        services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query);
238

239
        qCDebug(RUNNER_SERVICES) << "got " << services.count() << " services from " << query;
Méven Car's avatar
Méven Car committed
240
        for (const KService::Ptr &service : qAsConst(services)) {
241
            if (disqualify(service)) {
242
                continue;
243
            }
244

245
246
247
248
249
250
251
252
253
254
255
            const QString id = service->storageId();
            const QString name = service->desktopEntryName();
            const QString exec = service->exec();

            Plasma::QueryMatch match(m_runner);
            match.setType(Plasma::QueryMatch::PossibleMatch);
            setupMatch(service, match);
            qreal relevance(0.6);

            // If the term was < 3 chars and NOT at the beginning of the App's name or Exec, then
            // chances are the user doesn't want that app.
256
            if (weightedTermLength < 3) {
257
                if (name.startsWith(term, Qt::CaseInsensitive) || exec.startsWith(term, Qt::CaseInsensitive)) {
258
259
                    relevance = 0.9;
                } else {
260
261
                    continue;
                }
262
            } else if (service->name().contains(queryList[0], Qt::CaseInsensitive)) {
263
                relevance = 0.8;
264
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("Name"));
265

266
                if (service->name().startsWith(queryList[0], Qt::CaseInsensitive)) {
267
268
                    relevance += 0.1;
                }
269
            } else if (service->genericName().contains(queryList[0], Qt::CaseInsensitive)) {
270
                relevance = 0.65;
271
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("GenericName"));
272

273
                if (service->genericName().startsWith(queryList[0], Qt::CaseInsensitive)) {
274
275
                    relevance += 0.05;
                }
276
            } else if (service->exec().contains(queryList[0], Qt::CaseInsensitive)) {
277
                relevance = 0.7;
278
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("Exec"));
279

280
                if (service->exec().startsWith(queryList[0], Qt::CaseInsensitive)) {
281
                    relevance += 0.05;
282
                }
283
            } else if (service->comment().contains(queryList[0], Qt::CaseInsensitive)) {
284
                relevance = 0.5;
285
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("Comment"));
286

287
                if (service->comment().startsWith(queryList[0], Qt::CaseInsensitive)) {
288
289
                    relevance += 0.05;
                }
290
291
            }

292
293
            const bool isKCM = service->serviceTypes().contains(QLatin1String("KCModule"));
            if (!isKCM && (service->categories().contains(QLatin1String("KDE")) || service->serviceTypes().contains(QLatin1String("KCModule")))) {
294
                qCDebug(RUNNER_SERVICES) << "found a kde thing" << id << match.subtext() << relevance;
295
                relevance += .09;
296
297
            }

298
            if (isKCM) {
Marco Martin's avatar
Marco Martin committed
299
300
301
302
303
                if (service->parentApp() == QStringLiteral("kinfocenter")) {
                    match.setMatchCategory(i18n("System Information"));
                } else {
                    match.setMatchCategory(i18n("System Settings"));
                }
304
305
306
                // KCMs are, on the balance, less relevant. Drop it ever so much. So they may get outscored
                // by an otherwise equally applicable match.
                relevance -= .001;
307
            }
308
309
310
311

            qCDebug(RUNNER_SERVICES) << service->name() << "is this relevant:" << relevance;
            match.setRelevance(relevance);

312
            matches << match;
313
        }
314
315
    }

316
317
318
319
    void matchCategories()
    {
        //search for applications whose categories contains the query
        query = QStringLiteral("exist Exec and (exist Categories and '%1' ~subin Categories)").arg(term);
Méven Car's avatar
Méven Car committed
320
        const auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
321

Méven Car's avatar
Méven Car committed
322
        for (const KService::Ptr &service : services) {
323
            qCDebug(RUNNER_SERVICES) << service->name() << "is an exact match!" << service->storageId() << service->exec();
324
            if (disqualify(service)) {
325
326
327
328
                continue;
            }

            Plasma::QueryMatch match(m_runner);
329
330
331
332
            match.setType(Plasma::QueryMatch::PossibleMatch);
            setupMatch(service, match);

            qreal relevance = 0.6;
Laurent Montel's avatar
Laurent Montel committed
333
            if (service->categories().contains(QLatin1String("X-KDE-More")) ||
334
                    !service->showInCurrentDesktop()) {
335
336
337
338
                relevance = 0.5;
            }

            if (service->isApplication()) {
339
                relevance += .04;
340
341
342
343
344
345
346
            }

            match.setRelevance(relevance);
            matches << match;
        }
    }

347
348
    void matchJumpListActions()
    {
349
        if (weightedTermLength < 3) {
350
351
352
            return;
        }

353
        query = QStringLiteral("exist Actions"); // doesn't work
Méven Car's avatar
Méven Car committed
354
        const auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query);
355

Méven Car's avatar
Méven Car committed
356
        for (const KService::Ptr &service : services) {
357
358
359
360
            if (service->noDisplay()) {
                continue;
            }

361
362
363
364
365
            // Skip SystemSettings as we find KCMs already
            if (service->storageId() == QLatin1String("systemsettings.desktop")) {
                continue;
            }

366
367
            const auto actions = service->actions();
            for (const KServiceAction &action : actions) {
368
                if (action.text().isEmpty() || action.exec().isEmpty() || hasSeen(action)) {
369
370
                    continue;
                }
371
372
                seen(action);

373
374
                const int matchIndex = action.text().indexOf(term, 0, Qt::CaseInsensitive);
                if (matchIndex < 0) {
375
376
377
                    continue;
                }

378
                Plasma::QueryMatch match(m_runner);
379
                match.setType(Plasma::QueryMatch::PossibleMatch);
380
                if (!action.icon().isEmpty()) {
381
                    match.setIconName(action.icon());
382
                } else {
383
                    match.setIconName(service->icon());
384
385
386
                }
                match.setText(i18nc("Jump list search result, %1 is action (eg. open new tab), %2 is application (eg. browser)",
                                    "%1 - %2", action.text(), service->name()));
387
388
389
390
391
392
393
394
395

                QUrl url(service->storageId());
                url.setScheme(QStringLiteral("applications"));

                QUrlQuery query;
                query.addQueryItem(QStringLiteral("action"), action.name());
                url.setQuery(query);

                match.setData(url);
396
397

                qreal relevance = 0.5;
398
                if (matchIndex == 0) {
399
400
401
402
403
404
405
406
407
408
                    relevance += 0.05;
                }

                match.setRelevance(relevance);

                matches << match;
            }
        }
    }

409
410
411
412
413
414
    ServiceRunner *m_runner;
    QSet<QString> m_seen;

    QList<Plasma::QueryMatch> matches;
    QString query;
    QString term;
415
    int weightedTermLength = -1;
416
417
418
419
420
};

ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args)
    : Plasma::AbstractRunner(parent, args)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
421
    setObjectName(QStringLiteral("Application"));
422
423
424
425
426
    setPriority(AbstractRunner::HighestPriority);

    addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds applications whose name or description match :q:")));
}

427
ServiceRunner::~ServiceRunner() = default;
428
429
430
431
432
433
434

void ServiceRunner::match(Plasma::RunnerContext &context)
{
    // This helper class aids in keeping state across numerous
    // different queries that together form the matches set.
    ServiceFinder finder(this);
    finder.match(context);
435
436
437
438
}

void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
439
    Q_UNUSED(context)
440

441
    const QUrl dataUrl = match.data().toUrl();
442

443
444
445
    KService::Ptr service = KService::serviceByStorageId(dataUrl.path());
    if (!service) {
        return;
446
447
    }

448
449
450
451
452
453
    KActivities::ResourceInstance::notifyAccessed(
        QUrl(QStringLiteral("applications:") + service->storageId()),
        QStringLiteral("org.kde.krunner")
    );

    KIO::ApplicationLauncherJob *job = nullptr;
454

455
456
    const QString actionName = QUrlQuery(dataUrl).queryItemValue(QStringLiteral("action"));
    if (actionName.isEmpty()) {
Marco Martin's avatar
Marco Martin committed
457
458
        // We want to load kcms directly with systemsettings,
        // but we can't completely replace kcmshell with systemsettings
459
        // as we need to be able to load kcms without plasma and we can't
Marco Martin's avatar
Marco Martin committed
460
461
462
463
        // implement all kcmshell features into systemsettings
        if (service->serviceTypes().contains(QLatin1String("KCModule"))) {
            if (service->parentApp() == QStringLiteral("kinfocenter")) {
                service->setExec(QStringLiteral("kinfocenter ") + service->desktopEntryName());
464
465
            // We can't display a KCM in systemsettings if it has no parent, BUG: 423612
            } else if (!service->property("X-KDE-System-Settings-Parent-Category").toString().isEmpty()) {
Marco Martin's avatar
Marco Martin committed
466
467
468
                service->setExec(QStringLiteral("systemsettings5 ") + service->desktopEntryName());
            }
        }
469
470
471
472
473
474
475
476
477
        job = new KIO::ApplicationLauncherJob(service);
    } else {
        const auto actions = service->actions();
        auto it = std::find_if(actions.begin(), actions.end(), [&actionName](const KServiceAction &action) {
            return action.name() == actionName;
        });
        Q_ASSERT(it != actions.end());

        job = new KIO::ApplicationLauncherJob(*it);
478
    }
479
480
481
482
483

    auto *delegate = new KNotificationJobUiDelegate;
    delegate->setAutoErrorHandlingEnabled(true);
    job->setUiDelegate(delegate);
    job->start();
484
485
}

486
QMimeData * ServiceRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
487
{
488
489
490
491
492
493
494
495
    const QUrl dataUrl = match.data().toUrl();

    const QString actionName = QUrlQuery(dataUrl).queryItemValue(QStringLiteral("action"));
    if (!actionName.isEmpty()) {
        return nullptr;
    }

    KService::Ptr service = KService::serviceByStorageId(dataUrl.path());
496
497
498
499
500
501
    if (!service) {
        return nullptr;
    }

    QString path = service->entryPath();
    if (!QDir::isAbsolutePath(path)) {
502
        path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kservices5/") + path);
503
504
505
506
    }

    if (path.isEmpty()) {
        return nullptr;
507
508
    }

509
    auto *data = new QMimeData();
510
511
    data->setUrls(QList<QUrl>{QUrl::fromLocalFile(path)});
    return data;
512
}