// SPDX-License-Identifier: GPL-3.0-or-later #include "app/overrides.h" #include "app/package.h" #include "app/paths.h" #include #include #include #include #include #include #include #include #include #include #include namespace xpl::overrides { namespace { constexpr const char *kMarker = "X-XPlasma-Panel-Override"; // Spellings that came before: xp2plasma's (and the Python app's), and // XPlasma's own while its name had a capital L. constexpr const char *kOldMarkers[] = {"X-Xp2plasma-Panel-Override", "X-XPLasma-Panel-Override"}; constexpr const char *kShadowMarker = "// xplasma: caption text shadow override"; constexpr const char *kLegacyShadowMarker = "// xp2plasma: caption text shadow override"; QString resource(const QString &name) { return resourceText(name); } QString readText(const QString &path) { QFile f(path); return f.open(QIODevice::ReadOnly) ? QString::fromUtf8(f.readAll()) : QString(); } bool fail(QString *error, const QString &message) { if (error) *error = message; return false; } QString systemDir(const QString &relative) { return paths::findSystemData(relative); } bool ownedWidget(const QString &dir) { QFile f(dir + QLatin1String("/metadata.json")); if (!f.open(QIODevice::ReadOnly)) return false; const QJsonObject meta = QJsonDocument::fromJson(f.readAll()).object(); if (meta.value(QLatin1String(kMarker)).toBool()) return true; return std::any_of(std::begin(kOldMarkers), std::end(kOldMarkers), [&](const char *m) { return meta.value(QLatin1String(m)).toBool(); }); } bool copyTree(const QString &from, const QString &to) { QDirIterator it(from, QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) { const QString file = it.next(); const QString target = to + file.mid(from.size()); QDir().mkpath(QFileInfo(target).absolutePath()); if (!QFile::copy(file, target)) return false; } return true; } bool writeText(const QString &path, const QString &text) { QDir().mkpath(QFileInfo(path).absolutePath()); QSaveFile f(path); return f.open(QIODevice::WriteOnly) && f.write(text.toUtf8()) >= 0 && f.commit(); } } // namespace // (resourceText and patched: patches.cpp, which the forks' build tool // builds too.) namespace { struct Widget { QString id, key; // plugin id, patches.json prefix QStringList files; // what's patched, under contents/ui ("../config/main.xml": beside it) QHash values; // @KEY@ in the patched text -> value QStringList extras; // whole files from overrides/, added to contents/ui }; const QList kPanelWidgets{ {QStringLiteral("org.kde.plasma.systemtray"), QStringLiteral("systemtray"), {QStringLiteral("main.qml"), QStringLiteral("items/AbstractItem.qml")}, {}, {QStringLiteral("XpNotificationIcons.qml")}}, {QStringLiteral("org.kde.plasma.taskmanager"), QStringLiteral("taskmanager"), {QStringLiteral("main.qml"), QStringLiteral("Task.qml"), QStringLiteral("code/layoutmetrics.js"), QStringLiteral("../config/main.xml")}, {}, {}}, // The panel itself (a containment): the taskbar's colours for every // widget on it, not only those XPlasma patches. {QStringLiteral("org.kde.panel"), QStringLiteral("panel"), {QStringLiteral("main.qml")}, {}, {}}, // XP's balloons (when the look has their art; Plasma's popups otherwise). {QStringLiteral("org.kde.plasma.notifications"), QStringLiteral("notifications"), {QStringLiteral("main.qml"), QStringLiteral("global/Globals.qml")}, {}, {QStringLiteral("XpPopup.qml"), QStringLiteral("XpBalloon.qml"), QStringLiteral("XpCopyDialog.qml"), QStringLiteral("XpButton.qml")}}, }; // Widgets XPlasma once patched and doesn't any more: a copy of ours goes. // (The clock is XPlasma's own now: fyi.hotsocket.xplasma.clock.) const QStringList kRetiredWidgets{QStringLiteral("org.kde.plasma.digitalclock")}; // (org.kde.plasma.folder, the desktop, is this package under another name.) const QString kDesktopId = QStringLiteral("org.kde.desktopcontainment"); void removeWidgets(const QString &plasmoids, const QStringList &ids) { for (const QString &id : ids) for (const QString &suffix : {QString(), QStringLiteral(".xplasma-new"), QStringLiteral(".xp2plasma-new")}) { const QString dir = plasmoids + QLatin1Char('/') + id + suffix; if (ownedWidget(dir)) QDir(dir).removeRecursively(); } } // One widget, patched from this Plasma's own copy, ready to put in place: // its files' texts, or why it can't be. std::optional>> prepareWidget(const QString &plasmoids, const Widget &w, QString *error) { const QString dest = plasmoids + QLatin1Char('/') + w.id; if (QFileInfo::exists(dest) && !ownedWidget(dest)) { fail(error, QStringLiteral("%1 is someone else's override; not replacing it").arg(dest)); return std::nullopt; } const QString system = systemDir(QLatin1String("plasma/plasmoids/") + w.id); if (system.isEmpty()) { fail(error, QStringLiteral("this Plasma has no %1 package to patch (from Plasma 6.5 on, some widgets are built into " "their plugins)").arg(w.id)); return std::nullopt; } QList> files; for (const QString &f : w.files) { // (Its patches.json key: the path under contents/, but ui/'s own files bare.) const QString key = w.key + QLatin1Char('/') + (f.startsWith(QLatin1String("../")) ? f.mid(3) : f); auto text = patched(key, readText(system + QLatin1String("/contents/ui/") + f), error); if (!text) return std::nullopt; for (auto it = w.values.cbegin(); it != w.values.cend(); ++it) text->replace(QLatin1Char('@') + it.key() + QLatin1Char('@'), it.value()); files.append({f, *text}); } return files; } // ...put in place of any earlier copy of ours. bool placeWidget(const QString &plasmoids, const Widget &w, const QList> &files, QString *error) { const QString system = systemDir(QLatin1String("plasma/plasmoids/") + w.id); const QString dest = plasmoids + QLatin1Char('/') + w.id; const QString staging = dest + QLatin1String(".xplasma-new"); QDir(staging).removeRecursively(); if (!copyTree(system + QLatin1String("/contents"), staging + QLatin1String("/contents"))) return fail(error, QStringLiteral("can't copy %1").arg(system)); QFile metaFile(system + QLatin1String("/metadata.json")); metaFile.open(QIODevice::ReadOnly); QJsonObject meta = QJsonDocument::fromJson(metaFile.readAll()).object(); meta.insert(QLatin1String(kMarker), true); writeText(staging + QLatin1String("/metadata.json"), QString::fromUtf8(QJsonDocument(meta).toJson())); const QString ui = staging + QLatin1String("/contents/ui/"); for (const auto &[f, text] : files) writeText(ui + f, text); for (const QString &f : w.extras) writeText(ui + f, resource(f)); if (w.key == QLatin1String("systemtray")) { // XP's show-hidden-icons button in place of KDE's arrow, which // stays as the fallback. QFile::rename(ui + QLatin1String("ExpanderArrow.qml"), ui + QLatin1String("PlasmaExpanderArrow.qml")); writeText(ui + QLatin1String("ExpanderArrow.qml"), resource(QStringLiteral("ExpanderArrow.qml"))); } QDir(dest).removeRecursively(); // (ours: checked before) if (!QDir().rename(staging, dest)) return fail(error, QStringLiteral("can't install %1").arg(dest)); return true; } // All or nothing: every widget is checked before any is replaced (*error // lists each that can't be, a line each), and one that fails to go in takes // the others installed with it back out. With `dryRun`, only the checking. bool installWidgets(const QString &plasmoids, const QList &widgets, QString *error, bool dryRun = false) { QStringList problems; QList>> prepared; for (const Widget &w : widgets) { QString why; if (const auto files = prepareWidget(plasmoids, w, &why)) prepared.append(*files); else problems << QStringLiteral("%1: %2").arg(w.id, why); } if (!problems.isEmpty()) return fail(error, problems.join(QLatin1Char('\n'))); if (dryRun) return true; QStringList placed; for (int i = 0; i < widgets.size(); ++i) { QString why; if (!placeWidget(plasmoids, widgets.at(i), prepared.at(i), &why)) { removeWidgets(plasmoids, placed << widgets.at(i).id); return fail(error, QStringLiteral("%1: %2").arg(widgets.at(i).id, why)); } placed << widgets.at(i).id; } return true; } // A shell command as a QML string literal (see launchCommand). QString launchLiteral(const QString &program, const QStringList &arguments = {}) { const QString command = launchCommand(program, arguments); return QString::fromUtf8(QJsonDocument(QJsonArray{command}).toJson(QJsonDocument::Compact)).mid(1).chopped(1); } } // namespace QString launchCommand(const QString &program, const QStringList &arguments) { // (QString, not auto: with QStringBuilder the sum is a builder holding a // reference to `word`, gone when the lambda returns.) auto quote = [](QString word) -> QString { word.replace(QLatin1Char('\''), QLatin1String("'\\''")); return QLatin1Char('\'') + word + QLatin1Char('\''); }; QStringList words{quote(program)}; for (const QString &a : arguments) words << quote(a); const QString run = words.join(QLatin1Char(' ')); // Plasma's executable engine owns its foreground process and kills it // when the shell stops. Without systemd, leave that process immediately // and keep the editor's streams independent of the engine's pipes. return QStringLiteral("systemd-run --user --collect --quiet -- %1 || { nohup %1 /dev/null 2>&1 & }").arg(run); } // Up to Plasma 6.3 the tray was two packages: org.kde.plasma.systemtray a // thin wrapper, the tray itself org.kde.plasma.private.systemtray (6.4 made // them one, near enough line for line) -- so there, that's the one patched. const QString kOldTrayId = QStringLiteral("org.kde.plasma.private.systemtray"); static QList panelWidgets() { QList list = kPanelWidgets; if (!systemDir(QLatin1String("plasma/plasmoids/") + kOldTrayId).isEmpty()) for (Widget &w : list) if (w.key == QLatin1String("systemtray")) w.id = kOldTrayId; return list; } QStringList builtInPanelWidgets() { QStringList ids; for (const Widget &w : panelWidgets()) if (systemDir(QLatin1String("plasma/plasmoids/") + w.id).isEmpty()) ids << w.id; return ids; } // The panel widgets this Plasma has as packages, which are patched. static QList packagedPanelWidgets() { const QStringList builtIn = builtInPanelWidgets(); QList out; for (const Widget &w : panelWidgets()) if (!builtIn.contains(w.id)) out << w; return out; } bool installPanelWidgets(const QString &plasmoids, QString *error) { // (Copies of ours from an earlier Plasma, of widgets it has since built // in or XPlasma no longer patches, go.) removeWidgets(plasmoids, kRetiredWidgets + builtInPanelWidgets()); return installWidgets(plasmoids, packagedPanelWidgets(), error); } bool checkPanelWidgets(const QString &plasmoids, QString *error) { return installWidgets(plasmoids, packagedPanelWidgets(), error, true); } void removePanelWidgets(const QString &plasmoids) { QStringList ids = kRetiredWidgets; for (const Widget &w : kPanelWidgets) ids << w.id; ids << kOldTrayId; removeWidgets(plasmoids, ids); } bool installDesktopMenu(const QString &plasmoids, const QString &editor, QString *error) { return installWidgets(plasmoids, {{kDesktopId, QStringLiteral("desktopcontainment"), {QStringLiteral("FolderViewLayer.qml"), QStringLiteral("FolderView.qml")}, {{QStringLiteral("LAUNCH"), launchLiteral(editor)}}, {}}}, error); } bool checkDesktopMenu(const QString &plasmoids, QString *error) { return installWidgets(plasmoids, {{kDesktopId, QStringLiteral("desktopcontainment"), {QStringLiteral("FolderViewLayer.qml"), QStringLiteral("FolderView.qml")}, {{QStringLiteral("LAUNCH"), QStringLiteral("\"\"")}}, {}}}, error, true); } void removeDesktopMenu(const QString &plasmoids) { removeWidgets(plasmoids, {kDesktopId}); } void removeTitleShadows(const QString &target) { const QString text = readText(target); if (text.startsWith(QLatin1String(kShadowMarker)) || text.startsWith(QLatin1String(kLegacyShadowMarker))) QFile::remove(target); } } // namespace xpl::overrides