File indexing completed on 2025-01-05 03:53:17

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2019-04-02
0007  * Description : plugin to export image as wallpaper - DBUS version.
0008  * References  : https://bugs.kde.org/show_bug.cgi?id=217950
0009  *
0010  * SPDX-FileCopyrightText: 2019      by Igor Antropov <antropovi at yahoo dot com>
0011  * SPDX-FileCopyrightText: 2019-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "wallpaperplugin.h"
0018 
0019 // Qt includes
0020 
0021 #include <QMessageBox>
0022 #include <QProcess>
0023 #include <QDBusMessage>
0024 #include <QDBusConnection>
0025 
0026 // KDE includes
0027 
0028 #include <klocalizedstring.h>
0029 
0030 // Local includes
0031 
0032 #include "digikam_debug.h"
0033 
0034 namespace DigikamGenericWallpaperPlugin
0035 {
0036 
0037 bool WallpaperPlugin::setWallpaper(const QString& path, int layout) const
0038 {
0039     QDBusMessage message = QDBusMessage::createMethodCall
0040         (
0041             QLatin1String("org.kde.plasmashell"),
0042             QLatin1String("/PlasmaShell"),
0043             QLatin1String("org.kde.PlasmaShell"),
0044             QLatin1String("evaluateScript")
0045         );
0046 
0047     /**
0048      * Example of WallPaper settings from plasma-org.kde.plasma.desktop-appletsrc:
0049      *
0050      * [Containments][1][Wallpaper][org.kde.image][General]
0051      * FillMode=2
0052      * Image=file:///home/gilles/Images/M104_left.jpg
0053      * height=1440
0054      * width=2560
0055      *
0056      * FillMode can take these values:
0057      *    0 = Adjusted
0058      *    1 = Adjusted with apect ratio
0059      *    2 = Adjusted and cropped      (Stretch)
0060      *    3 = Mosaic                    (Tile)
0061      *    4 = ???
0062      *    5 = ???
0063      *    6 = Centered                  (Center)
0064      *
0065      * [Containments][1] = screen 1
0066      * [Containments][2] = screen 2
0067      * [Containments][3] = screen 3
0068      * etc.
0069      */
0070 
0071     message << QString::fromUtf8
0072         (
0073             "var allDesktops = desktops();"
0074             "for (i=0;i<allDesktops.length;i++)"
0075             "{"
0076                 "d = allDesktops[i];"
0077                 "d.wallpaperPlugin = \"org.kde.image\";"
0078                 "d.currentConfigGroup = Array(\"Wallpaper\", \"org.kde.image\", \"General\");"
0079                 "d.writeConfig(\"Image\", \"%1\");"
0080                 "d.writeConfig(\"FillMode\", \"%2\")"
0081             "}"
0082         )
0083         .arg(path)
0084         .arg(layout);
0085 
0086     QDBusMessage reply = QDBusConnection::sessionBus().call(message);
0087 
0088     if (reply.type() == QDBusMessage::ErrorMessage)
0089     {
0090         QMessageBox::warning(nullptr,
0091                              i18nc("@title:window",
0092                                    "Error While to Set Image as Wallpaper"),
0093                              i18nc("@info", "Cannot change wallpaper image from current desktop with\n"
0094                                             "%1\n\nError: %2",
0095                                    path,
0096                                    reply.errorMessage()));
0097 
0098         return false;
0099     }
0100 
0101     return true;
0102 }
0103 
0104 } // namespace DigikamGenericWallpaperPlugin