Something went wrong. Try again.
Bring Windows XP themes to your KDE Plasma environment! xplasma.hotsocket.fyi
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241// SPDX-License-Identifier: GPL-3.0-or-later#include "app/themepack.h"
#include "app/archive.h"#include "app/library.h"#include "app/paths.h"#include "core/themefile.h"
#include <QDir>#include <QDirIterator>#include <QFile>#include <QFileInfo>#include <QSaveFile>
namespace xpl::themepack {
namespace {
bool fail(QString *error, const QString &message){ if (error) *error = message; return false;}
// A name as a file's or folder's: no separators, nothing hidden.QString fileName(const QString &name){ QString out = QString(name).remove(QLatin1Char('"')).trimmed(); for (QChar &c : out) if (c == QLatin1Char('/') || c == QLatin1Char('\\') || c == QLatin1Char(':') || c.category() == QChar::Other_Control) c = QLatin1Char('-'); while (out.startsWith(QLatin1Char('.'))) out.remove(0, 1); return out.isEmpty() ? QStringLiteral("Theme") : out;}
bool under(const QString &path, const QString &dir){ const QString d = QDir(dir).canonicalPath(); return !d.isEmpty() && QFileInfo(path).canonicalFilePath().startsWith(d + QLatin1Char('/'));}
} // namespace
bool write(const QString &themePath, const QString &out, QString *error){ auto loaded = ThemeFile::load(themePath, error); if (!loaded) return false; ThemeFile theme = *loaded; const ThemeBundle b = Library::bundle(themePath); QList<std::pair<QString, QByteArray>> files; QStringList packed; // paths in the pack, lower-case: one each // A file of the user's into the pack under `folder`: its name there. auto pack = [&](const QString &folder, const QString &path) -> std::optional<QString> { QFile f(path); if (!f.open(QIODevice::ReadOnly)) { fail(error, QStringLiteral("can't read %1").arg(path)); return std::nullopt; } const QByteArray bytes = f.readAll(); const QString base = QFileInfo(path).fileName(); QString inPack = folder + QLatin1Char('/') + base; for (int n = 2; packed.contains(inPack.toLower()); ++n) inPack = folder + QStringLiteral("/%1-%2").arg(n).arg(base); for (const auto &[p, b2] : std::as_const(files)) if (b2 == bytes && p.startsWith(folder + QLatin1Char('/'))) return p; // (the same file twice: once) packed << inPack.toLower(); files.append({inPack, bytes}); return inPack; }; auto themeDir = [](const QString &inPack) { return QStringLiteral("%ThemeDir%") + QString(inPack).replace(QLatin1Char('/'), QLatin1Char('\\')); };
// The background: the user's own packed; XP's own named as XP has it. const QString desktop = QStringLiteral("Control Panel\\Desktop"); if (!b.wallpaper.isEmpty()) { if (under(b.wallpaper, paths::wallpapers())) { theme.setValue(desktop, u"Wallpaper", QStringLiteral("%SystemRoot%\\Web\\Wallpaper\\") + QFileInfo(b.wallpaper).fileName()); } else { const auto inPack = pack(QStringLiteral("DesktopBackground"), b.wallpaper); if (!inPack) return false; theme.setValue(desktop, u"Wallpaper", themeDir(*inPack)); } } // The visual style: a fan style packed, its folder whole (its shell // styles, its other colours); Microsoft's named as XP has them. if (!b.style.isEmpty()) { const QFileInfo style(b.style); if (under(b.style, paths::themes())) { theme.setValue(u"VisualStyles", u"Path", QStringLiteral("%SystemRoot%\\resources\\Themes\\%1\\%2").arg(style.dir().dirName(), style.fileName())); } else { const QString root = style.absolutePath(); const QString folder = QStringLiteral("VisualStyles/") + style.dir().dirName(); QDirIterator it(root, QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) { const QString f = it.next(); if (f.endsWith(QLatin1String(".theme"), Qt::CaseInsensitive) && QFileInfo(f).absolutePath() == root) continue; // (themes beside it are themes of their own) QFile in(f); if (!in.open(QIODevice::ReadOnly)) return fail(error, QStringLiteral("can't read %1").arg(f)); files.append({folder + QLatin1Char('/') + QDir(root).relativeFilePath(f), in.readAll()}); } theme.setValue(u"VisualStyles", u"Path", themeDir(folder + QLatin1Char('/') + style.fileName())); } } // Pointers: XP's by name, the user's packed. const QString cursorSection = QStringLiteral("Control Panel\\Cursors"); const QStringList keys = presets::cursorKeys(); for (int i = 0; i < b.cursors.size() && i < keys.size(); ++i) { const QString f = b.cursors.at(i); if (f.isEmpty() || !QDir::isAbsolutePath(f)) continue; // (XP's own: named as they were) const auto inPack = pack(QStringLiteral("Cursors"), f); if (!inPack) return false; theme.setValue(cursorSection, keys.at(i), themeDir(*inPack)); } // Sounds: XP's by name, the user's packed. for (auto it = b.sounds.cbegin(); it != b.sounds.cend(); ++it) { if (it.value().isEmpty() || !QDir::isAbsolutePath(it.value())) continue; // (quiet, or XP's own: named as they were) const auto inPack = pack(QStringLiteral("Sounds"), it.value()); if (!inPack) return false; theme.setValue(QStringLiteral("AppEvents\\Schemes\\Apps\\%1\\.Current").arg(it.key()), u"DefaultValue", themeDir(*inPack)); } // The Start button's logo picture, packed (its label and logo text are // plain keys already). if (!b.startButton.logoFile.isEmpty()) { const auto inPack = pack(QStringLiteral("Start"), b.startButton.logoFile); if (!inPack) return false; theme.setValue(u"XPlasma", u"StartLogo", themeDir(*inPack)); } theme.remove(u"XPlasma", u"Saved"); // (a theme of whoever opens it, once they do) files.prepend({fileName(b.label) + QLatin1String(".theme"), theme.toBytes()});
const auto cab = archive::writeCabinet(files, error); if (!cab) return false; QSaveFile f(out); if (!f.open(QIODevice::WriteOnly) || f.write(*cab) != cab->size() || !f.commit()) return fail(error, QStringLiteral("can't write %1: %2").arg(out, f.errorString())); return true;}
namespace {
// The pack's .theme (at its top), as a path in it and its file.std::optional<std::pair<QString, ThemeFile>> packedTheme(const QString &pack, QString *error){ QString why; const auto entries = archive::list(pack, &why); if (!entries) { fail(error, QStringLiteral("%1 isn't a theme pack (%2)").arg(QFileInfo(pack).fileName(), why)); return std::nullopt; } for (const archive::Entry &e : *entries) { if (e.directory || e.path.contains(QLatin1Char('/')) || !e.path.endsWith(QLatin1String(".theme"), Qt::CaseInsensitive)) continue; const auto bytes = archive::read(pack, e.path, error); if (!bytes) return std::nullopt; return std::pair(e.path, ThemeFile::fromBytes(*bytes)); } fail(error, QStringLiteral("%1 has no theme in it").arg(QFileInfo(pack).fileName())); return std::nullopt;}
QString displayName(const ThemeFile &theme, const QString &fallback){ const QString name = theme.value(u"Theme", u"DisplayName").trimmed(); return name.isEmpty() || name.startsWith(QLatin1Char('@')) ? fallback : name;}
} // namespace
std::optional<QString> name(const QString &pack, QString *error){ const auto theme = packedTheme(pack, error); if (!theme) return std::nullopt; return displayName(theme->second, QFileInfo(theme->first).completeBaseName());}
QString installFolder(const QString &name){ return paths::userThemes() + QLatin1Char('/') + fileName(name);}
std::optional<QString> install(const QString &pack, bool replace, QString *error){ const auto theme = packedTheme(pack, error); if (!theme) return std::nullopt; const QString display = displayName(theme->second, QFileInfo(theme->first).completeBaseName()); const QString folder = installFolder(display); if (QFileInfo::exists(folder) && !replace) { fail(error, QStringLiteral("%1 already exists").arg(folder)); return std::nullopt; } // Unpacked beside where it goes, then put there. const QString staging = folder + QLatin1String(".xplasma-new"); QDir(staging).removeRecursively(); if (!archive::extract(pack, staging, {}, error)) { QDir(staging).removeRecursively(); return std::nullopt; } // Its .theme: the user's now (Delete takes the folder), named for the folder. ThemeFile t = theme->second; t.setValue(u"XPlasma", u"Saved", QStringLiteral("1")); if (t.value(u"Theme", u"DisplayName").trimmed().isEmpty()) t.setValue(u"Theme", u"DisplayName", display); QFile::remove(staging + QLatin1Char('/') + theme->first); const QString themeName = QFileInfo(folder).fileName() + QLatin1String(".theme"); if (!t.save(staging + QLatin1Char('/') + themeName, error)) { QDir(staging).removeRecursively(); return std::nullopt; } const QString old = folder + QLatin1String(".xplasma-old"); QDir(old).removeRecursively(); if (QFileInfo::exists(folder) && !QDir().rename(folder, old)) { QDir(staging).removeRecursively(); fail(error, QStringLiteral("can't replace %1").arg(folder)); return std::nullopt; } if (!QDir().rename(staging, folder)) { QDir().rename(old, folder); fail(error, QStringLiteral("can't install %1").arg(folder)); return std::nullopt; } QDir(old).removeRecursively(); return folder + QLatin1Char('/') + themeName;}
} // namespace xpl::themepack