/* SPDX-FileCopyrightText: 2022 Julius Zint SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL */ #include "wallpaperfileitemaction.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace Qt::StringLiterals; K_PLUGIN_CLASS_WITH_JSON(WallpaperFileItemAction, "wallpaperfileitemaction.json") WallpaperFileItemAction::WallpaperFileItemAction(QObject *parent, const QVariantList &) : KAbstractFileItemActionPlugin(parent) { } WallpaperFileItemAction::~WallpaperFileItemAction() { } QList WallpaperFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget) { if (fileItemInfos.urlList().size() > 1) { return {}; } const QString filePath = fileItemInfos.urlList().constFirst().toLocalFile(); auto menu = new QMenu(i18ndc("plasma_wallpaper_org.kde.image", "@action:inmenu", "Set as Wallpaper")); menu->setIcon(QIcon::fromTheme(QStringLiteral("viewimage"))); auto desktopAction = new QAction(i18ndc("plasma_wallpaper_org.kde.image", "@action:inmenu Set as Desktop Wallpaper", "Desktop")); connect(desktopAction, &QAction::triggered, this, [this, filePath] { setAsDesktopBackground(filePath); }); menu->addAction(desktopAction); auto lockAction = new QAction(i18ndc("plasma_wallpaper_org.kde.image", "@action:inmenu Set as Lockscreen Wallpaper", "Lockscreen")); connect(lockAction, &QAction::triggered, this, [this, filePath] { setAsLockscreenBackground(filePath); }); menu->addAction(lockAction); auto bothAction = new QAction(i18ndc("plasma_wallpaper_org.kde.image", "@action:inmenu Set as both lockscreen and Desktop Wallpaper", "Both")); connect(bothAction, &QAction::triggered, this, [this, filePath] { setAsDesktopBackground(filePath); setAsLockscreenBackground(filePath); }); menu->addAction(bothAction); menu->setParent(parentWidget, Qt::Popup); return {menu->menuAction()}; } void WallpaperFileItemAction::setAsDesktopBackground(const QString &file) { auto script = QStringLiteral( "const allDesktops = desktopsForActivity(currentActivity());" " for (i=0; ideleteLater(); const QDBusPendingReply reply = *watcher; if (reply.isError()) { auto errorMessage = QString(xi18ndc("plasma_wallpaper_org.kde.image", "@info %1 is the dbus error message", "An error occurred while attempting to set the Plasma wallpaper:%1")) .arg(reply.error().message()); qWarning() << errorMessage; error(errorMessage); } }); } void WallpaperFileItemAction::setAsLockscreenBackground(const QString &file) { KSharedConfigPtr screenLockerConfig = KSharedConfig::openConfig(u"kscreenlockerrc"_s); KConfigGroup cfgGroup = screenLockerConfig->group(QString()).group(u"Greeter"_s).group(u"Wallpaper"_s).group(u"org.kde.image"_s).group(u"General"_s); if (screenLockerConfig->accessMode() != KConfig::ReadWrite) { auto errorMessage = QString(i18nd("plasma_wallpaper_org.kde.image", "An error occurred while attempting to open kscreenlockerrc config file.")); qWarning() << errorMessage; error(errorMessage); return; } cfgGroup.writeEntry("Image", file); cfgGroup.writeEntry("PreviewImage", file); screenLockerConfig->sync(); } #include "wallpaperfileitemaction.moc"