1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
This file is part of the KDE project
SPDX-FileCopyrightText: 2007 Will Stephenson <wstephenson@kde.org>
SPDX-FileCopyrightText: 2009 Ben Cooksley <bcooksley@kde.org>
SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "MenuItem.h"
#include "kcmmetadatahelpers.h"
#include <QList>
#include <QString>
#include <KCModuleLoader>
#include <KConfigGroup>
#include <KDesktopFile>
#include <KJsonUtils>
#include <QFileInfo>
#include <KCModuleData>
static bool childIsLessThan(MenuItem *left, MenuItem *right)
{
return left->weight() < right->weight();
}
class MenuItem::Private
{
public:
Private()
{
}
MenuItem *parent;
QList<MenuItem *> children;
bool menu;
QString name;
QString category;
int weight;
KService::Ptr service;
bool showDefaultIndicator = false;
bool isCategoryOwner = false;
QString comment;
QString iconName;
QString systemsettingsCategoryModule;
bool isSystemsettingsCategory = false;
bool isSystemsettingsRootCategory = false;
bool isExternalAppModule = false;
KPluginMetaData metaData;
std::unique_ptr<KCModuleData> moduleData;
};
MenuItem::MenuItem(bool isMenu, MenuItem *itsParent)
: d(new Private())
{
d->parent = itsParent;
d->menu = isMenu;
if (d->parent) {
d->parent->children().append(this);
}
}
MenuItem::~MenuItem()
{
qDeleteAll(d->children);
delete d;
}
void MenuItem::sortChildrenByWeight()
{
std::sort(d->children.begin(), d->children.end(), childIsLessThan);
}
MenuItem *MenuItem::child(int index)
{
return d->children.at(index);
}
QStringList MenuItem::keywords(bool doesRemoveDuplicates) const
{
QStringList listOfKeywords;
const QJsonObject rawData = d->metaData.rawData();
// Add English keywords so users in other languages won't have to switch IME when searching.
if (!QLocale().name().startsWith(QLatin1String("en_"))) {
listOfKeywords << rawData[QStringLiteral("KPlugin")][QStringLiteral("Name")].toString();
listOfKeywords << d->metaData.value(QStringLiteral("X-KDE-Keywords"), QString()).split(QLatin1String(","));
}
listOfKeywords << KJsonUtils::readTranslatedString(rawData, QStringLiteral("X-KDE-Keywords")).split(QStringLiteral(","));
listOfKeywords << d->name;
for (MenuItem *child : std::as_const(d->children)) {
listOfKeywords << child->keywords(false);
}
// Remove any soft hyphens (used in long words in some languages)
listOfKeywords.replaceInStrings(QStringLiteral("\u00AD"), QString());
// Only remove duplicate keywords in the end
if (doesRemoveDuplicates) {
listOfKeywords.removeDuplicates();
}
return listOfKeywords;
}
MenuItem *MenuItem::parent() const
{
return d->parent;
}
QList<MenuItem *> &MenuItem::children() const
{
return d->children;
}
QString MenuItem::comment() const
{
return d->comment;
}
QString MenuItem::iconName() const
{
return d->iconName;
}
bool MenuItem::isExternalAppModule() const
{
return d->isExternalAppModule;
}
bool MenuItem::isSystemsettingsCategory() const
{
return d->isSystemsettingsCategory;
}
QString MenuItem::systemsettingsCategoryModule() const
{
return d->systemsettingsCategoryModule;
}
bool MenuItem::isSystemsettingsRootCategory() const
{
return d->isSystemsettingsRootCategory;
}
QString &MenuItem::name() const
{
return d->name;
}
QString &MenuItem::category() const
{
return d->category;
}
int MenuItem::weight()
{
return d->weight;
}
bool MenuItem::menu() const
{
return d->menu;
}
void MenuItem::setCategoryConfig(const KDesktopFile &file)
{
const KConfigGroup grp = file.desktopGroup();
d->category = grp.readEntry("X-KDE-System-Settings-Category");
if (d->category.isEmpty()) {
d->category = grp.readEntry("X-KDE-KInfoCenter-Category");
}
d->name = grp.readEntry("Name");
d->weight = grp.readEntry(QStringLiteral("X-KDE-Weight"), 100);
d->comment = grp.readEntry("Comment");
d->iconName = grp.readEntry("Icon");
d->isSystemsettingsCategory = true;
d->systemsettingsCategoryModule = grp.readEntry("X-KDE-System-Settings-Category-Module");
d->isSystemsettingsRootCategory = QFileInfo(file.fileName()).fileName() == QLatin1String("settings-root-category.desktop");
}
void MenuItem::setMetaData(const KPluginMetaData &data)
{
d->metaData = data;
if (d->isSystemsettingsCategory) {
return;
}
d->category = data.value(QStringLiteral("X-KDE-System-Settings-Category"));
if (d->category.isEmpty()) {
d->category = data.value(QStringLiteral("X-KDE-KInfoCenter-Category"));
}
d->name = data.name();
d->weight = data.value(QStringLiteral("X-KDE-Weight"), 100);
d->comment = data.description();
d->iconName = data.iconName();
d->systemsettingsCategoryModule = data.value(QStringLiteral("X-KDE-System-Settings-Category-Module"));
d->isExternalAppModule = data.value(QStringLiteral("IsExternalApp"), false);
if (data.value(QStringLiteral("X-KDE-System-Settings-Uses-ModuleData"), false)) {
d->moduleData.reset(loadModuleData(data));
}
}
KPluginMetaData MenuItem::metaData()
{
return d->metaData;
}
KCModuleData *MenuItem::moduleData() const
{
return d->moduleData.get();
}
bool MenuItem::showDefaultIndicator() const
{
return d->showDefaultIndicator;
}
bool MenuItem::isCategoryOwner() const
{
return d->isCategoryOwner;
}
void MenuItem::setCategoryOwner(bool owner)
{
d->isCategoryOwner = owner;
}
void MenuItem::updateDefaultIndicator()
{
d->showDefaultIndicator = false;
if (isLibrary()) {
if (!d->moduleData) {
d->moduleData.reset(loadModuleData(d->metaData));
}
d->showDefaultIndicator = d->moduleData && !d->moduleData->isDefaults();
}
if (menu()) {
for (auto child : std::as_const(children())) {
d->showDefaultIndicator |= child->showDefaultIndicator();
}
}
if (d->parent) {
d->parent->updateDefaultIndicator();
}
}
void MenuItem::setDefaultIndicator(bool defaultIndicator)
{
d->showDefaultIndicator = defaultIndicator;
if (menu()) {
for (auto child : std::as_const(children())) {
d->showDefaultIndicator |= child->showDefaultIndicator();
}
}
if (d->parent) {
d->parent->updateDefaultIndicator();
}
}
MenuItem *MenuItem::descendantForModule(const QString &moduleName)
{
if (d->service) {
if (d->service->desktopEntryName() == moduleName) {
return this;
}
}
if (d->metaData.isValid() && d->metaData.pluginId() == moduleName) {
return this;
}
for (auto child : std::as_const(d->children)) {
MenuItem *candidate = child->descendantForModule(moduleName);
if (candidate) {
return candidate;
}
}
return nullptr;
}
bool MenuItem::isLibrary()
{
return d->metaData.isValid() && !isExternalAppModule();
}
|