From 92efc6ca6ec3c1c1c34a777c470337d4fde4b0c1 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Mon, 6 Jun 2016 22:05:16 +0200 Subject: [PATCH] [Libtaskmanager] Add support for regular expression mapping This allows more complicated rules, useful for matching Chrome webapps to their respective desktop file where we need to change crx_foo to chrome-foo-default. BUG: 358277 CCBUG: 356609 FIXED-IN: 5.7.0 Differential Revision: https://phabricator.kde.org/D1673 --- libtaskmanager/taskmanagerrulesrc | 6 ++++ libtaskmanager/xwindowtasksmodel.cpp | 47 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/libtaskmanager/taskmanagerrulesrc b/libtaskmanager/taskmanagerrulesrc index d0157fe47..ecc9d7094 100644 --- a/libtaskmanager/taskmanagerrulesrc +++ b/libtaskmanager/taskmanagerrulesrc @@ -15,3 +15,9 @@ Dragon=dragonplayer [Settings] ManualOnly=Wine MatchCommandLineFirst=VirtualBox + +[Rewrite Rules][google-chrome][1] +Property=ClassName +Identifier=DesktopEntryName +Match=(?<=crx_)(?'match'[a-z]+) +Target=chrome-%1-default diff --git a/libtaskmanager/xwindowtasksmodel.cpp b/libtaskmanager/xwindowtasksmodel.cpp index 93f474920..5c80470dc 100644 --- a/libtaskmanager/xwindowtasksmodel.cpp +++ b/libtaskmanager/xwindowtasksmodel.cpp @@ -40,6 +40,7 @@ License along with this library. If not, see . #include #include #include +#include #include #include #include @@ -526,6 +527,52 @@ QUrl XWindowTasksModel::Private::windowUrl(WId window) return url; } + KConfigGroup rewriteRulesGroup(rulesConfig, QStringLiteral("Rewrite Rules")); + if (rewriteRulesGroup.hasGroup(classClass)) { + KConfigGroup rewriteGroup(&rewriteRulesGroup, classClass); + + const QStringList &rules = rewriteGroup.groupList(); + for (const QString &rule : rules) { + KConfigGroup ruleGroup(&rewriteGroup, rule); + + const QString propertyConfig = ruleGroup.readEntry(QStringLiteral("Property"), QString()); + + QString matchProperty; + if (propertyConfig == QLatin1String("ClassClass")) { + matchProperty = classClass; + } else if (propertyConfig == QLatin1String("ClassName")) { + matchProperty = className; + } + + if (matchProperty.isEmpty()) { + continue; + } + + const QString serviceSearchIdentifier = ruleGroup.readEntry(QStringLiteral("Identifier"), QString()); + if (serviceSearchIdentifier.isEmpty()) { + continue; + } + + QRegularExpression regExp(ruleGroup.readEntry(QStringLiteral("Match"))); + const auto match = regExp.match(matchProperty); + + if (match.hasMatch()) { + const QString actualMatch = match.captured(QStringLiteral("match")); + if (actualMatch.isEmpty()) { + continue; + } + + const QString rewrittenString = ruleGroup.readEntry(QStringLiteral("Target")).arg(actualMatch); + + services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), QStringLiteral("exist Exec and ('%1' =~ %2)").arg(rewrittenString, serviceSearchIdentifier)); + + if (!services.isEmpty()) { + break; + } + } + } + } + if (!mapped.isEmpty()) { services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), QStringLiteral("exist Exec and ('%1' =~ DesktopEntryName)").arg(mapped)); }