servicerunner.cpp 17.9 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>
Ahmad Samir's avatar
Ahmad Samir committed
32
#include <QUrlQuery>
33 34

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

42
#include <KIO/ApplicationLauncherJob>
43
#include <KIO/DesktopExecParser>
44

45
#include "debug.h"
46

Alexander Lohnau's avatar
Alexander Lohnau committed
47 48 49 50
namespace
{
int weightedLength(const QString &query)
{
51 52 53
    return KStringHandler::logicalLength(query);
}

Alexander Lohnau's avatar
Alexander Lohnau committed
54
} // namespace
55

56 57 58 59
/**
 * @brief Finds all KServices for a given runner query
 */
class ServiceFinder
60
{
61 62
public:
    ServiceFinder(ServiceRunner *runner)
Alexander Lohnau's avatar
Alexander Lohnau committed
63 64 65
        : m_runner(runner)
    {
    }
66

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

73 74
        KSycoca::disableAutoRebuild();

75
        term = context.query();
76
        weightedTermLength = weightedLength(term);
77

78 79 80 81
        matchExectuables();
        matchNameKeywordAndGenericName();
        matchCategories();
        matchJumpListActions();
82

83 84
        context.addMatches(matches);
    }
85

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

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

98 99
    bool hasSeen(const KService::Ptr &service)
    {
Alexander Lohnau's avatar
Alexander Lohnau committed
100
        return m_seen.contains(service->storageId()) && 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
    {
Alexander Lohnau's avatar
Alexander Lohnau committed
118
        // Increment the relevance based on all the words (other than the first) of the query list
119 120
        qreal relevanceIncrement = 0;

Alexander Lohnau's avatar
Alexander Lohnau committed
121
        for (int i = 1; i < strList.size(); ++i) {
122
            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.
Alexander Lohnau's avatar
Alexander Lohnau committed
158
        for (const QStringRef &str : strList) {
159
            keywordTemplate += QStringLiteral(" and '%1' ~subin Keywords").arg(str.toString());
160 161 162
            genericNameTemplate += QStringLiteral(" and '%1' ~~ GenericName").arg(str.toString());
            nameTemplate += QStringLiteral(" and '%1' ~~ Name").arg(str.toString());
            commentTemplate += QStringLiteral(" and '%1' ~~ Comment").arg(str.toString());
163 164
        }

165
        QString finalQuery = QStringLiteral("exist Exec and ( (%1) or (%2) or (%3) or ('%4' ~~ Exec) or (%5) )")
Alexander Lohnau's avatar
Alexander Lohnau committed
166
                                 .arg(keywordTemplate, genericNameTemplate, nameTemplate, strList[0].toString(), commentTemplate);
167 168 169 170 171

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

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

        match.setText(name);
177 178 179 180

        QUrl url(service->storageId());
        url.setScheme(QStringLiteral("applications"));
        match.setData(url);
181 182 183 184 185 186
        QString exec = service->exec();
        // We have a snap, remove the ENV variable
        if (exec.contains(QLatin1String("BAMF_DESKTOP_FILE_HINT"))) {
            const static QRegularExpression snapCleanupRegex(QStringLiteral("env BAMF_DESKTOP_FILE_HINT=.+ "));
            exec.remove(snapCleanupRegex);
        }
187 188
        const QStringList resultingArgs = KIO::DesktopExecParser(KService(QString(), exec, QString()), {}).resultingArguments();
        match.setId(QStringLiteral("exec://") + resultingArgs.join(QLatin1Char(' ')));
189 190 191 192
        if (!service->genericName().isEmpty() && service->genericName() != name) {
            match.setSubtext(service->genericName());
        } else if (!service->comment().isEmpty()) {
            match.setSubtext(service->comment());
193 194
        }

195 196 197 198
        if (!service->icon().isEmpty()) {
            match.setIconName(service->icon());
        }
    }
199

200 201
    void matchExectuables()
    {
202
        if (weightedTermLength < 2) {
203
            return;
204 205
        }

206
        // Search for applications which are executable and case-insensitively match the search term
207
        // See https://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language
208 209
        // 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
210
        const KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
211

212 213 214
        if (services.isEmpty()) {
            return;
        }
215

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

229 230
    void matchNameKeywordAndGenericName()
    {
Alexander Lohnau's avatar
Alexander Lohnau committed
231
        // Splitting the query term to match using subsequences
232 233
        QVector<QStringRef> queryList = term.splitRef(QLatin1Char(' '));

234
        // If the term length is < 3, no real point searching the Keywords and GenericName
235
        if (weightedTermLength < 3) {
236
            query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term);
237
        } else {
Alexander Lohnau's avatar
Alexander Lohnau committed
238
            // Match using subsequences (Bug: 262837)
239
            query = generateQuery(queryList);
240
        }
241

242 243
        KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
        services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query);
244

245
        qCDebug(RUNNER_SERVICES) << "got " << services.count() << " services from " << query;
Méven Car's avatar
Méven Car committed
246
        for (const KService::Ptr &service : qAsConst(services)) {
247
            if (disqualify(service)) {
248
                continue;
249
            }
250

251 252 253 254 255 256 257 258 259 260 261
            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.
262
            if (weightedTermLength < 3) {
263
                if (name.startsWith(term, Qt::CaseInsensitive) || exec.startsWith(term, Qt::CaseInsensitive)) {
264 265
                    relevance = 0.9;
                } else {
266 267
                    continue;
                }
268
            } else if (service->name().contains(queryList[0], Qt::CaseInsensitive)) {
269
                relevance = 0.8;
270
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("Name"));
271

272
                if (service->name().startsWith(queryList[0], Qt::CaseInsensitive)) {
273 274
                    relevance += 0.1;
                }
275
            } else if (service->genericName().contains(queryList[0], Qt::CaseInsensitive)) {
276
                relevance = 0.65;
277
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("GenericName"));
278

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

286
                if (service->exec().startsWith(queryList[0], Qt::CaseInsensitive)) {
287
                    relevance += 0.05;
288
                }
289
            } else if (service->comment().contains(queryList[0], Qt::CaseInsensitive)) {
290
                relevance = 0.5;
291
                relevance += increaseMatchRelavance(service, queryList, QStringLiteral("Comment"));
292

293
                if (service->comment().startsWith(queryList[0], Qt::CaseInsensitive)) {
294 295
                    relevance += 0.05;
                }
296 297
            }

298 299
            const bool isKCM = service->serviceTypes().contains(QLatin1String("KCModule"));
            if (!isKCM && (service->categories().contains(QLatin1String("KDE")) || service->serviceTypes().contains(QLatin1String("KCModule")))) {
300
                qCDebug(RUNNER_SERVICES) << "found a kde thing" << id << match.subtext() << relevance;
301
                relevance += .09;
302 303
            }

304
            if (isKCM) {
Marco Martin's avatar
Marco Martin committed
305 306 307 308 309
                if (service->parentApp() == QStringLiteral("kinfocenter")) {
                    match.setMatchCategory(i18n("System Information"));
                } else {
                    match.setMatchCategory(i18n("System Settings"));
                }
310 311 312
                // 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;
313
            }
314 315 316 317

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

318
            matches << match;
319
        }
320 321
    }

322 323
    void matchCategories()
    {
Alexander Lohnau's avatar
Alexander Lohnau committed
324
        // search for applications whose categories contains the query
325
        query = QStringLiteral("exist Exec and (exist Categories and '%1' ~subin Categories)").arg(term);
Méven Car's avatar
Méven Car committed
326
        const auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
327

Méven Car's avatar
Méven Car committed
328
        for (const KService::Ptr &service : services) {
329
            qCDebug(RUNNER_SERVICES) << service->name() << "is an exact match!" << service->storageId() << service->exec();
330
            if (disqualify(service)) {
331 332 333 334
                continue;
            }

            Plasma::QueryMatch match(m_runner);
335 336 337 338
            match.setType(Plasma::QueryMatch::PossibleMatch);
            setupMatch(service, match);

            qreal relevance = 0.6;
Alexander Lohnau's avatar
Alexander Lohnau committed
339
            if (service->categories().contains(QLatin1String("X-KDE-More")) || !service->showInCurrentDesktop()) {
340 341 342 343
                relevance = 0.5;
            }

            if (service->isApplication()) {
344
                relevance += .04;
345 346 347 348 349 350 351
            }

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

352 353
    void matchJumpListActions()
    {
354
        if (weightedTermLength < 3) {
355 356 357
            return;
        }

358
        query = QStringLiteral("exist Actions"); // doesn't work
Alexander Lohnau's avatar
Alexander Lohnau committed
359
        const auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application")); //, query);
360

Méven Car's avatar
Méven Car committed
361
        for (const KService::Ptr &service : services) {
362 363 364 365
            if (service->noDisplay()) {
                continue;
            }

366 367 368 369 370
            // Skip SystemSettings as we find KCMs already
            if (service->storageId() == QLatin1String("systemsettings.desktop")) {
                continue;
            }

371 372
            const auto actions = service->actions();
            for (const KServiceAction &action : actions) {
373
                if (action.text().isEmpty() || action.exec().isEmpty() || hasSeen(action)) {
374 375
                    continue;
                }
376 377
                seen(action);

378 379
                const int matchIndex = action.text().indexOf(term, 0, Qt::CaseInsensitive);
                if (matchIndex < 0) {
380 381 382
                    continue;
                }

383
                Plasma::QueryMatch match(m_runner);
384
                match.setType(Plasma::QueryMatch::PossibleMatch);
385
                if (!action.icon().isEmpty()) {
386
                    match.setIconName(action.icon());
387
                } else {
388
                    match.setIconName(service->icon());
389 390
                }
                match.setText(i18nc("Jump list search result, %1 is action (eg. open new tab), %2 is application (eg. browser)",
Alexander Lohnau's avatar
Alexander Lohnau committed
391 392 393
                                    "%1 - %2",
                                    action.text(),
                                    service->name()));
394 395 396 397 398 399 400 401 402

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

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

                match.setData(url);
403 404

                qreal relevance = 0.5;
405
                if (matchIndex == 0) {
406 407 408 409 410 411 412 413 414 415
                    relevance += 0.05;
                }

                match.setRelevance(relevance);

                matches << match;
            }
        }
    }

416 417 418 419 420 421
    ServiceRunner *m_runner;
    QSet<QString> m_seen;

    QList<Plasma::QueryMatch> matches;
    QString query;
    QString term;
422
    int weightedTermLength = -1;
423 424
};

425 426
ServiceRunner::ServiceRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
    : Plasma::AbstractRunner(parent, metaData, args)
427
{
Alexander Lohnau's avatar
Alexander Lohnau committed
428
    setObjectName(QStringLiteral("Application"));
429 430 431 432 433
    setPriority(AbstractRunner::HighestPriority);

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

434
ServiceRunner::~ServiceRunner() = default;
435 436 437 438 439 440 441

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);
442 443 444 445
}

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

448
    const QUrl dataUrl = match.data().toUrl();
449

450 451 452
    KService::Ptr service = KService::serviceByStorageId(dataUrl.path());
    if (!service) {
        return;
453 454
    }

Alexander Lohnau's avatar
Alexander Lohnau committed
455
    KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + service->storageId()), QStringLiteral("org.kde.krunner"));
456 457

    KIO::ApplicationLauncherJob *job = nullptr;
458

459 460 461 462 463 464 465 466 467 468 469
    const QString actionName = QUrlQuery(dataUrl).queryItemValue(QStringLiteral("action"));
    if (actionName.isEmpty()) {
        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);
470
    }
471 472 473 474 475

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

Alexander Lohnau's avatar
Alexander Lohnau committed
478
QMimeData *ServiceRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
479
{
480 481 482 483 484 485 486 487
    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());
488 489 490 491 492 493
    if (!service) {
        return nullptr;
    }

    QString path = service->entryPath();
    if (!QDir::isAbsolutePath(path)) {
494
        path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kservices5/") + path);
495 496 497 498
    }

    if (path.isEmpty()) {
        return nullptr;
499 500
    }

501
    auto *data = new QMimeData();
502 503
    data->setUrls(QList<QUrl>{QUrl::fromLocalFile(path)});
    return data;
504
}