// SPDX-License-Identifier: GPL-3.0-or-later // Plasma's QML patched by XPlasma's rules (overrides/patches.json), and the // files beside them: what the forks' build tool (forktool/) needs of the // overrides, apart from installing them (overrides.cpp). #include "app/overrides.h" #include #include #include #include namespace xpl::overrides { QString resourceText(const QString &name) { QFile f(QStringLiteral(":/overrides/") + name); return f.open(QIODevice::ReadOnly) ? QString::fromUtf8(f.readAll()) : QString(); } std::optional patched(const QString &file, const QString &source, QString *error, const QJsonArray *rules) { auto fail = [error](const QString &message) { if (error) *error = message; return std::nullopt; }; static const QJsonObject base = QJsonDocument::fromJson(resourceText(QStringLiteral("patches.json")).toUtf8()).object(); if (!rules && !base.contains(file)) return fail(QStringLiteral("no patches for %1").arg(file)); const QJsonArray list = rules ? *rules : base.value(file).toArray(); QString text = source; for (const QJsonValue &v : list) { const QJsonObject rule = v.toObject(); const QString unless = rule.value(QStringLiteral("unlessContains")).toString(); if (!unless.isEmpty() && text.contains(unless)) continue; const QString find = rule.value(QStringLiteral("find")).toString(); // (optional: for what only some Plasmas have -- skipped where it's not) if (rule.value(QStringLiteral("optional")).toBool() && !text.contains(find)) continue; if (text.count(find) != 1) return fail(QStringLiteral("this Plasma's %1 isn't laid out as XPlasma expects (\"%2\"); not patching it") .arg(file, find.left(60).trimmed())); QString replace = rule.value(QStringLiteral("replace")).toString(); // {{name}}: a snippet from overrides/name.qml. for (int at = int(replace.indexOf(QLatin1String("{{"))); at >= 0; at = int(replace.indexOf(QLatin1String("{{")))) { const int end = int(replace.indexOf(QLatin1String("}}"), at)); if (end < 0) break; replace.replace(at, end + 2 - at, resourceText(replace.mid(at + 2, end - at - 2) + QLatin1String(".qml"))); } text.replace(find, replace); } return text; } } // namespace xpl::overrides