File indexing completed on 2024-12-22 05:16:37

0001 /*
0002     SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <KPackage/PackageStructure>
0007 
0008 #include <QDebug>
0009 #include <QDir>
0010 #include <QFileInfo>
0011 
0012 /**
0013  * By specifying "X-KDE-PlasmaImageWallpaper-AccentColor" in metadata,
0014  * a wallpaper package can override the accent color when
0015  * "Accent color from wallpaper" is enabled.
0016  */
0017 class WallpaperPackage : public KPackage::PackageStructure
0018 {
0019     Q_OBJECT
0020 public:
0021     using KPackage::PackageStructure::PackageStructure;
0022 
0023     void initPackage(KPackage::Package *package) override
0024     {
0025         package->addDirectoryDefinition("images", QStringLiteral("images/"));
0026         package->addDirectoryDefinition("images_dark", QStringLiteral("images_dark%1").arg(QDir::separator()));
0027 
0028         const QStringList mimetypes{
0029             QStringLiteral("image/avif"),
0030             QStringLiteral("image/bmp"),
0031             QStringLiteral("image/gif"),
0032             QStringLiteral("image/heif"),
0033             QStringLiteral("image/jpeg"),
0034             QStringLiteral("image/jpg"),
0035             QStringLiteral("image/png"),
0036             QStringLiteral("image/svg"),
0037             QStringLiteral("image/tiff"),
0038             QStringLiteral("image/webp"),
0039         };
0040         package->setMimeTypes("images", mimetypes);
0041         package->setMimeTypes("images_dark", mimetypes);
0042 
0043         package->setRequired("images", true);
0044         package->setRequired("images_dark", false);
0045         package->addFileDefinition("screenshot", QStringLiteral("screenshot.png"));
0046         package->setAllowExternalPaths(true);
0047     }
0048 
0049     void pathChanged(KPackage::Package *package) override
0050     {
0051         static bool guard = false;
0052 
0053         if (guard) {
0054             return;
0055         }
0056 
0057         guard = true;
0058         QString ppath = package->path();
0059         if (ppath.endsWith('/')) {
0060             ppath.chop(1);
0061             if (!QFile::exists(ppath)) {
0062                 ppath = package->path();
0063             }
0064         }
0065 
0066         QFileInfo info(ppath);
0067         const bool isFullPackage = info.isDir();
0068         package->removeDefinition("preferred");
0069         package->removeDefinition("preferredDark");
0070         package->setRequired("images", isFullPackage);
0071         package->setRequired("images_dark", false);
0072 
0073         if (isFullPackage) {
0074             package->setContentsPrefixPaths(QStringList{QStringLiteral("contents/")});
0075         } else {
0076             package->addFileDefinition("screenshot", info.fileName());
0077             package->addFileDefinition("preferred", info.fileName());
0078             package->addFileDefinition("preferredDark", info.fileName());
0079             package->setContentsPrefixPaths(QStringList());
0080             package->setPath(info.path());
0081         }
0082 
0083         guard = false;
0084     }
0085 };
0086 
0087 K_PLUGIN_CLASS_WITH_JSON(WallpaperPackage, "plasma-packagestructure-wallpaperimages.json")
0088 
0089 #include "wallpaperpackage.moc"