diff --git a/runners/services/autotests/fixtures/kdesystemsettings.desktop b/runners/services/autotests/fixtures/kdesystemsettings.desktop new file mode 100644 index 00000000..0623f57a --- /dev/null +++ b/runners/services/autotests/fixtures/kdesystemsettings.desktop @@ -0,0 +1,13 @@ +[Desktop Entry] +Exec=systemsettings5 +Icon=preferences-system +Type=Application +X-KDE-StartupNotify=true +NotShowIn=KDE; + +GenericName=KDE System Settings ServiceRunnerTest + +Name=KDE System Settings ServiceRunnerTest + +X-DBUS-StartupType=Unique +Categories=Qt;KDE;Settings; diff --git a/runners/services/autotests/fixtures/systemsettings.desktop b/runners/services/autotests/fixtures/systemsettings.desktop new file mode 100644 index 00000000..935db902 --- /dev/null +++ b/runners/services/autotests/fixtures/systemsettings.desktop @@ -0,0 +1,14 @@ +[Desktop Entry] +Exec=systemsettings5 +Icon=preferences-system +Type=Application +X-DocPath=systemsettings/index.html +X-KDE-StartupNotify=true +OnlyShowIn=KDE; + +GenericName=System Settings ServiceRunnerTest + +Name=System Settings ServiceRunnerTest + +X-DBUS-StartupType=Unique +Categories=Qt;KDE;Settings; diff --git a/runners/services/autotests/servicerunnertest.cpp b/runners/services/autotests/servicerunnertest.cpp index d0c6daed..d3217768 100644 --- a/runners/services/autotests/servicerunnertest.cpp +++ b/runners/services/autotests/servicerunnertest.cpp @@ -1,128 +1,162 @@ /* * Copyright (C) 2016 Harald Sitter * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include #include #include #include #include #include #include #include "../servicerunner.h" class ServiceRunnerTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void testChromeAppsRelevance(); void testKonsoleVsYakuakeComment(); + void testSystemSettings(); }; void ServiceRunnerTest::initTestCase() { QStandardPaths::setTestModeEnabled(true); auto appsPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); QDir(appsPath).removeRecursively(); QVERIFY(QDir().mkpath(appsPath)); auto fixtureDir = QDir(QFINDTESTDATA("fixtures")); for(auto fileInfo : fixtureDir.entryInfoList(QDir::Files)) { auto source = fileInfo.absoluteFilePath(); auto target = appsPath + QDir::separator() + fileInfo.fileName(); QVERIFY2(QFile::copy(fileInfo.absoluteFilePath(), target), qPrintable(QString("can't copy %1 => %2").arg(source, target))); } setlocale(LC_ALL, "C.utf8"); KSycoca::self()->ensureCacheValid(); + + // Make sure noDisplay behaves consistently WRT OnlyShowIn etc. + QVERIFY(setenv("XDG_CURRENT_DESKTOP", "KDE", 1) == 0); + // NOTE: noDisplay also includes X-KDE-OnlyShowOnQtPlatforms which is a bit harder to fake + // and not currently under testing anyway. } void ServiceRunnerTest::cleanupTestCase() { } void ServiceRunnerTest::testChromeAppsRelevance() { ServiceRunner runner(this, QVariantList()); Plasma::RunnerContext context; context.setQuery("chrome"); runner.match(context); bool chromeFound = false; bool signalFound = false; for (auto match : context.matches()) { qDebug() << "matched" << match.text(); if (!match.text().contains("ServiceRunnerTest")) { continue; } if (match.text() == "Google Chrome ServiceRunnerTest") { QCOMPARE(match.relevance(), 0.8); chromeFound = true; } else if (match.text() == "Signal ServiceRunnerTest") { // Rates lower because it doesn't have it in the name. QCOMPARE(match.relevance(), 0.7); signalFound = true; } } QVERIFY(chromeFound); QVERIFY(signalFound); } void ServiceRunnerTest::testKonsoleVsYakuakeComment() { // Yakuake has konsole mentioned in comment, should be rated lower. ServiceRunner runner(this, QVariantList()); Plasma::RunnerContext context; context.setQuery("kons"); runner.match(context); bool konsoleFound = false; bool yakuakeFound = false; for (auto match : context.matches()) { qDebug() << "matched" << match.text(); if (!match.text().contains("ServiceRunnerTest")) { continue; } if (match.text() == "Konsole ServiceRunnerTest") { QCOMPARE(match.relevance(), 0.99); konsoleFound = true; } else if (match.text() == "Yakuake ServiceRunnerTest") { // Rates lower because it doesn't have it in the name. QCOMPARE(match.relevance(), 0.59); yakuakeFound = true; } } QVERIFY(konsoleFound); QVERIFY(yakuakeFound); } +void ServiceRunnerTest::testSystemSettings() +{ + // In 5.9.0 'System Settings' suddenly didn't come back as a match for 'settings' anymore. + // Sytem Settings has a noKDE version and a KDE version, if the noKDE version is encountered + // first it will be added to the seen cache, however disqualification of already seen items + // may then also disqualify the KDE version of system settings on account of having already + // seen it. This test makes sure we find the right version. + ServiceRunner runner(this, QVariantList()); + Plasma::RunnerContext context; + context.setQuery("settings"); + + runner.match(context); + + bool systemSettingsFound = false; + bool foreignSystemSettingsFound = false; + for (auto match : context.matches()) { + qDebug() << "matched" << match.text(); + if (match.text() == "System Settings ServiceRunnerTest") { + systemSettingsFound = true; + } + if (match.text() == "KDE System Settings ServiceRunnerTest") { + foreignSystemSettingsFound = true; + } + } + QVERIFY(systemSettingsFound); + QVERIFY(!foreignSystemSettingsFound); +} + QTEST_MAIN(ServiceRunnerTest) #include "servicerunnertest.moc" diff --git a/runners/services/servicerunner.cpp b/runners/services/servicerunner.cpp index 68756f91..2f840a9b 100644 --- a/runners/services/servicerunner.cpp +++ b/runners/services/servicerunner.cpp @@ -1,406 +1,407 @@ /* * Copyright (C) 2006 Aaron Seigo * Copyright (C) 2014 Vishesh Handa * Copyright (C) 2016 Harald Sitter * * 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" #include #include #include #include #include #include #include #include #include #include "debug.h" /** * @brief Finds all KServices for a given runner query */ class ServiceFinder { public: ServiceFinder(ServiceRunner *runner) : m_runner(runner) {} void match(Plasma::RunnerContext &context) { if (!context.isValid()) { return; } term = context.query(); matchExectuables(); matchNameKeywordAndGenericName(); matchCategories(); matchJumpListActions(); context.addMatches(matches); } private: void seen(const KService::Ptr &service) { m_seen.insert(service->storageId()); m_seen.insert(service->exec()); } void seen(const KServiceAction &action) { m_seen.insert(action.exec()); } bool hasSeen(const KService::Ptr &service) { - return m_seen.contains(service->storageId()) || + return m_seen.contains(service->storageId()) && m_seen.contains(service->exec()); } bool hasSeen(const KServiceAction &action) { return m_seen.contains(action.exec()); } bool disqualify(const KService::Ptr &service) { auto ret = hasSeen(service) || service->noDisplay(); + qCDebug(RUNNER_SERVICES) << service->name() << "disqualified?" << ret; seen(service); return ret; } void setupMatch(const KService::Ptr &service, Plasma::QueryMatch &match) { const QString name = service->name(); match.setText(name); match.setData(service->storageId()); if (!service->genericName().isEmpty() && service->genericName() != name) { match.setSubtext(service->genericName()); } else if (!service->comment().isEmpty()) { match.setSubtext(service->comment()); } if (!service->icon().isEmpty()) { match.setIconName(service->icon()); } } void matchExectuables() { if (term.length() < 2) { return; } // Search for applications which are executable and case-insensitively match the search term // See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language // if the following is unclear to you. query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term); KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); if (services.isEmpty()) { return; } foreach (const KService::Ptr &service, services) { qCDebug(RUNNER_SERVICES) << service->name() << "is an exact match!" << service->storageId() << service->exec(); if (disqualify(service)) { continue; } Plasma::QueryMatch match(m_runner); match.setType(Plasma::QueryMatch::ExactMatch); setupMatch(service, match); match.setRelevance(1); matches << match; } } void matchNameKeywordAndGenericName() { // If the term length is < 3, no real point searching the Keywords and GenericName if (term.length() < 3) { query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term); } else { // 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. query = QStringLiteral("exist Exec and ( (exist Keywords and '%1' ~subin Keywords) or (exist GenericName and '%1' ~~ GenericName) or (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) or (exist Comment and '%1' ~~ Comment) )").arg(term); } KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query); qCDebug(RUNNER_SERVICES) << "got " << services.count() << " services from " << query; foreach (const KService::Ptr &service, services) { if (disqualify(service)) { continue; } 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. if (term.length() < 3) { if (name.startsWith(term) || exec.startsWith(term)) { relevance = 0.9; } else { continue; } } else if (service->name().contains(term, Qt::CaseInsensitive)) { relevance = 0.8; if (service->name().startsWith(term, Qt::CaseInsensitive)) { relevance += 0.1; } } else if (service->genericName().contains(term, Qt::CaseInsensitive)) { relevance = 0.65; if (service->genericName().startsWith(term, Qt::CaseInsensitive)) { relevance += 0.05; } } else if (service->exec().contains(term, Qt::CaseInsensitive)) { relevance = 0.7; if (service->exec().startsWith(term, Qt::CaseInsensitive)) { relevance += 0.05; } } else if (service->comment().contains(term, Qt::CaseInsensitive)) { relevance = 0.5; if (service->comment().startsWith(term, Qt::CaseInsensitive)) { relevance += 0.05; } } if (service->categories().contains(QStringLiteral("KDE")) || service->serviceTypes().contains(QStringLiteral("KCModule"))) { qCDebug(RUNNER_SERVICES) << "found a kde thing" << id << match.subtext() << relevance; if (id.startsWith(QLatin1String("kde-"))) { qCDebug(RUNNER_SERVICES) << "old" << !service->isApplication(); if (!service->isApplication()) { // avoid showing old kcms and what not continue; } // This is an older version, let's disambiguate it QString subtext(QStringLiteral("KDE3")); if (!match.subtext().isEmpty()) { subtext.append(", " + match.subtext()); } match.setSubtext(subtext); } else { relevance += .09; } } qCDebug(RUNNER_SERVICES) << service->name() << "is this relevant:" << relevance; match.setRelevance(relevance); if (service->serviceTypes().contains(QStringLiteral("KCModule"))) { match.setMatchCategory(i18n("System Settings")); } matches << match; } } void matchCategories() { //search for applications whose categories contains the query query = QStringLiteral("exist Exec and (exist Categories and '%1' ~subin Categories)").arg(term); auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query); foreach (const KService::Ptr &service, services) { qCDebug(RUNNER_SERVICES) << service->name() << "is an exact match!" << service->storageId() << service->exec(); if (disqualify(service)) { continue; } Plasma::QueryMatch match(m_runner); match.setType(Plasma::QueryMatch::PossibleMatch); setupMatch(service, match); qreal relevance = 0.6; if (service->categories().contains(QStringLiteral("X-KDE-More")) || !service->showInCurrentDesktop()) { relevance = 0.5; } if (service->isApplication()) { relevance += .04; } match.setRelevance(relevance); matches << match; } } void matchJumpListActions() { if (term.length() < 3) { return; } query = QStringLiteral("exist Actions"); // doesn't work auto services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query); foreach (const KService::Ptr &service, services) { if (service->noDisplay()) { continue; } foreach (const KServiceAction &action, service->actions()) { if (action.text().isEmpty() || action.exec().isEmpty() || hasSeen(action)) { continue; } seen(action); if (!action.text().contains(term, Qt::CaseInsensitive)) { continue; } Plasma::QueryMatch match(m_runner); match.setType(Plasma::QueryMatch::HelperMatch); if (!action.icon().isEmpty()) { match.setIconName(action.icon()); } else { match.setIconName(service->icon()); } 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())); match.setData(action.exec()); qreal relevance = 0.5; if (action.text().startsWith(term, Qt::CaseInsensitive)) { relevance += 0.05; } match.setRelevance(relevance); matches << match; } } } ServiceRunner *m_runner; QSet m_seen; QList matches; QString query; QString term; }; ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args) : Plasma::AbstractRunner(parent, args) { Q_UNUSED(args) setObjectName( QStringLiteral("Application" )); setPriority(AbstractRunner::HighestPriority); addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds applications whose name or description match :q:"))); } ServiceRunner::~ServiceRunner() { } QStringList ServiceRunner::categories() const { QStringList cat; cat << i18n("Applications") << i18n("System Settings"); return cat; } QIcon ServiceRunner::categoryIcon(const QString& category) const { if (category == i18n("Applications")) { return QIcon::fromTheme(QStringLiteral("applications-other")); } else if (category == i18n("System Settings")) { return QIcon::fromTheme(QStringLiteral("preferences-system")); } return Plasma::AbstractRunner::categoryIcon(category); } 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); } void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) { Q_UNUSED(context); if (match.type() == Plasma::QueryMatch::HelperMatch) { // Jump List Action KRun::run(match.data().toString(), {}, nullptr); return; } KService::Ptr service = KService::serviceByStorageId(match.data().toString()); if (service) { KActivities::ResourceInstance::notifyAccessed( QUrl(QStringLiteral("applications:") + service->storageId()), QStringLiteral("org.kde.krunner") ); KRun::runService(*service, {}, nullptr, true); } } QMimeData * ServiceRunner::mimeDataForMatch(const Plasma::QueryMatch &match) { KService::Ptr service = KService::serviceByStorageId(match.data().toString()); if (service) { QMimeData * result = new QMimeData(); QList urls; urls << QUrl::fromLocalFile(service->entryPath()); qCDebug(RUNNER_SERVICES) << urls; result->setUrls(urls); return result; } return 0; } K_EXPORT_PLASMA_RUNNER(services, ServiceRunner) #include "servicerunner.moc"