From 366a5b2dec5f8d562372b4e1c21801aba4def694 Mon Sep 17 00:00:00 2001 From: Alexander Lohnau Date: Sun, 24 Jul 2022 17:10:02 +0200 Subject: [PATCH] Read keywords from applets in widgetexplorer This allows for better searches that just the name and description. This also takes the original keywords into account and not just the translated ones. --- .../kcategorizeditemsviewmodels.cpp | 8 +++++++- .../kcategorizeditemsviewmodels_p.h | 5 +++++ .../widgetexplorer/plasmaappletitemmodel.cpp | 15 +++++++++++++++ .../widgetexplorer/plasmaappletitemmodel_p.h | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels.cpp b/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels.cpp index a8797204b..9f32f3d23 100644 --- a/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels.cpp +++ b/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels.cpp @@ -47,7 +47,13 @@ int AbstractItem::running() const bool AbstractItem::matches(const QString &pattern) const { - return name().contains(pattern, Qt::CaseInsensitive) || description().contains(pattern, Qt::CaseInsensitive); + if (name().contains(pattern, Qt::CaseInsensitive) || description().contains(pattern, Qt::CaseInsensitive)) { + return true; + } + const QStringList itemKeywords = keywords(); + return std::any_of(itemKeywords.begin(), itemKeywords.end(), [&pattern](const QString &keyword) { + return keyword.startsWith(pattern, Qt::CaseInsensitive); + }); } // DefaultFilterModel diff --git a/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels_p.h b/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels_p.h index 55c70873d..f3d784270 100644 --- a/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels_p.h +++ b/components/shellprivate/widgetexplorer/kcategorizeditemsviewmodels_p.h @@ -65,6 +65,11 @@ public: */ virtual bool passesFiltering(const Filter &filter) const = 0; + virtual QStringList keywords() const + { + return {}; + }; + private: }; diff --git a/components/shellprivate/widgetexplorer/plasmaappletitemmodel.cpp b/components/shellprivate/widgetexplorer/plasmaappletitemmodel.cpp index bd56b2be9..4617e404f 100644 --- a/components/shellprivate/widgetexplorer/plasmaappletitemmodel.cpp +++ b/components/shellprivate/widgetexplorer/plasmaappletitemmodel.cpp @@ -151,6 +151,21 @@ bool PlasmaAppletItem::matches(const QString &pattern) const return AbstractItem::matches(pattern); } +QStringList PlasmaAppletItem::keywords() const +{ + const static QString keywordsJsonKey = QStringLiteral("X-KDE-Keywords"); + constexpr QLatin1Char separator(','); + + const QJsonObject rawData = m_info.rawData(); + if (rawData.contains(keywordsJsonKey)) { + QStringList keywords = m_info.value(keywordsJsonKey).split(separator); + keywords << KJsonUtils::readTranslatedString(rawData, keywordsJsonKey).split(separator); + keywords.removeDuplicates(); + return keywords; + } + return {}; +} + bool PlasmaAppletItem::isLocal() const { return m_local; diff --git a/components/shellprivate/widgetexplorer/plasmaappletitemmodel_p.h b/components/shellprivate/widgetexplorer/plasmaappletitemmodel_p.h index 5b324476b..60fd43bab 100644 --- a/components/shellprivate/widgetexplorer/plasmaappletitemmodel_p.h +++ b/components/shellprivate/widgetexplorer/plasmaappletitemmodel_p.h @@ -34,6 +34,7 @@ public: int running() const override; bool isLocal() const; bool matches(const QString &pattern) const override; + QStringList keywords() const override; // set how many instances of this applet are running void setRunning(int count) override;