// 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 #include #include #include #include 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> 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 { 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> 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 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 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