1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
// SPDX-FileCopyrightText: 2023 Méven Car <meven@kde.org>
// SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "wallpaperplugin.h"
#include <QApplication>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QDBusReply>
#include <KLocalizedString>
#include <KPackage/Package>
#include <KPackage/PackageLoader>
#include <KPluginFactory>
#include <QCoroDBusPendingCall>
#include <QFile>
#include <QFileInfo>
WallpaperPlugin::WallpaperPlugin(QObject *parent)
: QObject{parent}
, m_homescreenConfig{new QQmlPropertyMap{this}}
, m_lockscreenConfig{new QQmlPropertyMap{this}}
, m_homescreenConfigFile{KSharedConfig::openConfig("plasma-org.kde.plasma.mobileshell-appletsrc", KConfig::SimpleConfig)}
, m_lockscreenConfigFile{KSharedConfig::openConfig("kscreenlockerrc", KConfig::SimpleConfig)}
{
m_lockscreenConfigWatcher = KConfigWatcher::create(m_lockscreenConfigFile);
const bool connected = QDBusConnection::sessionBus().connect(QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/PlasmaShell"),
QStringLiteral("org.kde.PlasmaShell"),
QStringLiteral("wallpaperChanged"),
this,
SLOT(loadHomescreenSettings()));
if (!connected) {
qWarning() << "Could not connect to dbus service org.kde.plasmashell to listen to wallpaperChanged";
}
connect(m_lockscreenConfigWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group) {
Q_UNUSED(group)
loadLockscreenSettings();
});
loadLockscreenSettings();
loadHomescreenSettings();
}
PlasmaQuick::ConfigModel *WallpaperPlugin::wallpaperPluginModel()
{
if (!m_wallpaperPluginModel) {
m_wallpaperPluginModel = new WallpaperConfigModel(this);
QDBusConnection::sessionBus().connect(QString(),
QStringLiteral("/KPackage/Plasma/Wallpaper"),
QStringLiteral("org.kde.plasma.kpackage"),
QStringLiteral("packageInstalled"),
m_wallpaperPluginModel,
SLOT(repopulate()));
QDBusConnection::sessionBus().connect(QString(),
QStringLiteral("/KPackage/Plasma/Wallpaper"),
QStringLiteral("org.kde.plasma.kpackage"),
QStringLiteral("packageUpdated"),
m_wallpaperPluginModel,
SLOT(repopulate()));
QDBusConnection::sessionBus().connect(QString(),
QStringLiteral("/KPackage/Plasma/Wallpaper"),
QStringLiteral("org.kde.plasma.kpackage"),
QStringLiteral("packageUninstalled"),
m_wallpaperPluginModel,
SLOT(repopulate()));
}
return m_wallpaperPluginModel;
}
QQmlPropertyMap *WallpaperPlugin::homescreenConfiguration() const
{
return m_homescreenConfig;
}
QQmlPropertyMap *WallpaperPlugin::lockscreenConfiguration() const
{
return m_lockscreenConfig;
}
QString WallpaperPlugin::homescreenWallpaperPlugin() const
{
return m_homescreenWallpaperPlugin;
}
QString WallpaperPlugin::homescreenWallpaperPluginSource()
{
if (m_homescreenWallpaperPlugin.isEmpty()) {
return QString();
}
const auto model = wallpaperPluginModel();
const auto wallpaperPluginCount = model->count();
for (int i = 0; i < wallpaperPluginCount; ++i) {
if (model->data(model->index(i), PlasmaQuick::ConfigModel::PluginNameRole) == m_homescreenWallpaperPlugin) {
return model->data(model->index(i), PlasmaQuick::ConfigModel::SourceRole).toString();
}
}
return QString();
}
void WallpaperPlugin::setHomescreenWallpaperPlugin(const QString &wallpaperPlugin)
{
auto containmentsGroup = m_homescreenConfigFile->group(QStringLiteral("Containments"));
for (const auto &contIndex : containmentsGroup.groupList()) {
const auto contConfig = containmentsGroup.group(contIndex);
if (contConfig.readEntry("activityId").isEmpty()) {
continue;
}
QString containmentIdx = contIndex;
auto containmentConfigGroup = containmentsGroup.group(containmentIdx);
// pick first screen that is found to load the wallpaper plugin
m_homescreenConfig = loadConfiguration(containmentConfigGroup, wallpaperPlugin, true);
m_homescreenWallpaperPlugin = wallpaperPlugin;
break;
}
// saveHomescreenSettings();
Q_EMIT homescreenWallpaperPluginChanged();
}
QString WallpaperPlugin::lockscreenWallpaperPlugin() const
{
return m_lockscreenWallpaperPlugin;
}
QString WallpaperPlugin::lockscreenWallpaperPluginSource()
{
if (m_lockscreenWallpaperPlugin.isEmpty()) {
return QString();
}
const auto model = wallpaperPluginModel();
const auto wallpaperPluginCount = model->count();
for (int i = 0; i < wallpaperPluginCount; ++i) {
if (model->data(model->index(i), PlasmaQuick::ConfigModel::PluginNameRole) == m_lockscreenWallpaperPlugin) {
return model->data(model->index(i), PlasmaQuick::ConfigModel::SourceRole).toString();
}
}
return QString();
}
void WallpaperPlugin::setLockscreenWallpaperPlugin(const QString &wallpaperPlugin)
{
KConfigGroup greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter")).group(QStringLiteral("Wallpaper")).group(wallpaperPlugin);
m_homescreenConfig = loadConfiguration(greeterGroup, wallpaperPlugin, true);
m_lockscreenWallpaperPlugin = wallpaperPlugin;
saveLockscreenSettings();
Q_EMIT lockscreenWallpaperPluginChanged();
}
QCoro::Task<void> WallpaperPlugin::setHomescreenWallpaper(const QString &path)
{
auto message = QDBusMessage::createMethodCall(QLatin1String("org.kde.plasmashell"),
QLatin1String("/PlasmaShell"),
QLatin1String("org.kde.PlasmaShell"),
QLatin1String("setWallpaper"));
for (uint screen = 0; screen < qApp->screens().size(); screen++) {
message.setArguments({"org.kde.image", QVariantMap{{"Image", path}}, screen});
const QDBusReply<void> reply = co_await QDBusConnection::sessionBus().asyncCall(message);
if (!reply.isValid()) {
qWarning() << "Failed to set wallpaper for screen" << screen << ":" << reply.error();
}
}
}
void WallpaperPlugin::setLockscreenWallpaper(const QString &path)
{
auto greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter"))
.group(QStringLiteral("Wallpaper"))
.group(QStringLiteral("org.kde.image"))
.group(QStringLiteral("General"));
greeterGroup.writeEntry("Image", path, KConfigGroup::Notify);
greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter"));
greeterGroup.writeEntry("WallpaperPlugin", "org.kde.image", KConfigGroup::Notify);
m_lockscreenConfigFile->sync();
}
QString WallpaperPlugin::homescreenWallpaperPath()
{
return m_homescreenWallpaperPath;
}
QString WallpaperPlugin::lockscreenWallpaperPath()
{
return m_lockscreenWallpaperPath;
}
QCoro::Task<void> WallpaperPlugin::loadHomescreenSettings()
{
auto message = QDBusMessage::createMethodCall(QLatin1String("org.kde.plasmashell"),
QLatin1String("/PlasmaShell"),
QLatin1String("org.kde.PlasmaShell"),
QLatin1String("wallpaper"));
message.setArguments({(uint)0}); // assume wallpaper on first screen
QDBusReply<QVariantMap> reply = co_await QDBusConnection::sessionBus().asyncCall(message);
if (!reply.isValid()) {
qWarning() << "unable to load homescreen wallpaper settings:" << reply.error();
co_return;
}
QVariantMap map = reply.value();
m_homescreenWallpaperPath = QString{};
if (!map.contains("wallpaperPlugin")) {
qWarning() << "wallpaperPlugin not found in response from org.kde.PlasmaShell wallpaper(), could not retrieve wallpaper";
Q_EMIT homescreenWallpaperPathChanged();
co_return;
}
// load wallpaper plugin config
if (m_homescreenConfig) {
m_homescreenConfig->deleteLater();
}
m_homescreenConfig = new QQmlPropertyMap{this};
for (const auto &key : map.keys()) {
if (key != QStringLiteral("wallpaperPlugin")) {
m_homescreenConfig->insert(key, map[key]);
}
}
// get wallpaper plugin
m_homescreenWallpaperPlugin = map["wallpaperPlugin"].toString();
// parse image configuration
if (m_homescreenWallpaperPlugin == QStringLiteral("org.kde.image")) {
m_homescreenWallpaperPath = map["Image"].toString();
}
Q_EMIT homescreenConfigurationChanged();
Q_EMIT homescreenWallpaperPluginChanged();
Q_EMIT homescreenWallpaperPathChanged();
}
void WallpaperPlugin::loadLockscreenSettings()
{
auto greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter"));
m_lockscreenWallpaperPlugin = greeterGroup.readEntry(QStringLiteral("WallpaperPlugin"), QString());
m_lockscreenWallpaperPath = QString{};
greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter")).group(QStringLiteral("Wallpaper")).group(m_lockscreenWallpaperPlugin);
m_lockscreenConfig = static_cast<QQmlPropertyMap *>(loadConfiguration(greeterGroup, m_lockscreenWallpaperPlugin, true));
if (m_lockscreenWallpaperPlugin == QStringLiteral("org.kde.image")) {
m_lockscreenWallpaperPath = greeterGroup.group(QStringLiteral("General")).readEntry(QStringLiteral("Image"), QString());
}
Q_EMIT lockscreenWallpaperPluginChanged();
Q_EMIT lockscreenConfigurationChanged();
Q_EMIT lockscreenWallpaperPathChanged();
}
QQmlPropertyMap *WallpaperPlugin::loadConfiguration(KConfigGroup group, QString wallpaperPlugin, bool loadDefaults)
{
auto packages = KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"), "plasma/wallpapers");
KPackage::Package pkg;
bool found = false;
for (auto &metaData : packages) {
if (metaData.pluginId() == wallpaperPlugin) {
found = true;
pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Wallpaper"), QFileInfo(metaData.fileName()).path());
break;
}
}
if (!found || !pkg.isValid()) {
qWarning() << "Could not find wallpaper plugin" << wallpaperPlugin;
return nullptr;
}
QFile file(pkg.fileUrl("config", "main.xml").toLocalFile());
auto *configLoader = new KConfigLoader(group, &file, this);
if (loadDefaults) {
configLoader->setDefaults();
}
auto config = new KConfigPropertyMap(configLoader, this);
return config;
}
QCoro::Task<void> WallpaperPlugin::saveHomescreenSettings()
{
auto iface = new QDBusInterface("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", QDBusConnection::sessionBus(), this);
if (!iface->isValid()) {
qWarning() << "Failed to connect to wallpaper dbus:" << qPrintable(QDBusConnection::sessionBus().lastError().message());
co_return;
}
QVariantMap params;
for (const auto &key : m_homescreenConfig->keys()) {
params.insert(key, m_homescreenConfig->value(key).toString());
}
if (m_homescreenWallpaperPlugin == "org.kde.image") {
params.remove("PreviewImage");
}
for (uint screen = 0; screen < qApp->screens().size(); screen++) {
QList<QVariant> args = {m_homescreenWallpaperPlugin, params, screen};
const QDBusReply<void> response = co_await iface->asyncCallWithArgumentList(QStringLiteral("setWallpaper"), args);
if (!response.isValid()) {
qWarning() << "Failed to set wallpaper:" << response.error();
}
}
}
void WallpaperPlugin::saveLockscreenSettings()
{
auto greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter"))
.group(QStringLiteral("Wallpaper"))
.group(m_lockscreenWallpaperPlugin)
.group(QStringLiteral("General"));
for (const auto &key : m_lockscreenConfig->keys()) {
greeterGroup.writeEntry(key, m_lockscreenConfig->value(key), KConfigGroup::Notify);
}
greeterGroup = m_lockscreenConfigFile->group(QStringLiteral("Greeter"));
greeterGroup.writeEntry("WallpaperPlugin", m_lockscreenWallpaperPlugin, KConfigGroup::Notify);
m_lockscreenConfigFile->sync();
}
WallpaperConfigModel::WallpaperConfigModel(QObject *parent)
: PlasmaQuick::ConfigModel(parent)
{
repopulate();
}
void WallpaperConfigModel::repopulate()
{
clear();
for (const KPluginMetaData &m : KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/Wallpaper"))) {
KPackage::Package pkg = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Wallpaper"), m.pluginId());
if (!pkg.isValid()) {
continue;
}
appendCategory(pkg.metadata().iconName(), pkg.metadata().name(), pkg.fileUrl("ui", QStringLiteral("config.qml")).toString(), m.pluginId());
}
}
#include "wallpaperplugin.moc"
|