File indexing completed on 2024-04-28 05:35:34

0001 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0002 // SPDX-FileCopyrightText: 2023 Méven Car <meven@kde.org>
0003 
0004 #include "defaultwallpaper.h"
0005 
0006 #include <KConfigGroup>
0007 #include <KPackage/PackageLoader>
0008 #include <Plasma/Theme>
0009 
0010 KPackage::Package DefaultWallpaper::defaultWallpaperPackage()
0011 {
0012     // Try from the look and feel package first, then from the plasma theme
0013     KPackage::Package lookAndFeelPackage = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/LookAndFeel"));
0014     KConfigGroup cg(KSharedConfig::openConfig(QStringLiteral("kdeglobals")), QStringLiteral("KDE"));
0015     const QString packageName = cg.readEntry("LookAndFeelPackage", QString());
0016     // If empty, it will be the default (currently Breeze)
0017     if (!packageName.isEmpty()) {
0018         lookAndFeelPackage.setPath(packageName);
0019     }
0020 
0021     KConfigGroup lnfDefaultsConfig = KConfigGroup(KSharedConfig::openConfig(lookAndFeelPackage.filePath("defaults")), QStringLiteral("Wallpaper"));
0022 
0023     const QString image = lnfDefaultsConfig.readEntry("Image", "");
0024     KPackage::Package package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Wallpaper/Images"));
0025 
0026     if (!image.isEmpty()) {
0027         package.setPath(
0028             QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("wallpapers/%1").arg(image), QStandardPaths::LocateDirectory));
0029     }
0030 
0031     if (!package.isValid()) {
0032         // Try to get a default from the plasma theme
0033         Plasma::Theme theme;
0034         QString path = theme.wallpaperPath();
0035         int index = path.indexOf(QLatin1String("/contents/images/"));
0036         if (index > -1) { // We have file from package -> get path to package
0037             path = path.left(index);
0038         }
0039         package.setPath(path);
0040     }
0041 
0042     if (!package.isValid()) {
0043         // Use Next
0044         package.setPath(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("wallpapers/Next"), QStandardPaths::LocateDirectory));
0045     }
0046 
0047     return package;
0048 }