File indexing completed on 2024-05-05 05:38:49

0001 /*
0002     SPDX-FileCopyrightText: 2022 Méven Car <meven@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QDebug>
0008 #include <QObject>
0009 #include <QtDBus>
0010 
0011 #define SERVICE_NAME "org.kde.plasmashell"
0012 
0013 class SetWallPaperTest : public QObject
0014 {
0015     Q_OBJECT
0016 public:
0017     SetWallPaperTest(QObject *parent = nullptr);
0018     void setWallpaper(const QString &wallpaperPath);
0019 
0020 public Q_SLOTS:
0021     void onWallpaperChanged(uint screenNum);
0022 
0023 private:
0024     void printCurrentWallpaper();
0025 };
0026 
0027 void SetWallPaperTest::printCurrentWallpaper()
0028 {
0029     QDBusMessage request =
0030         QDBusMessage::createMethodCall(SERVICE_NAME, QStringLiteral("/PlasmaShell"), QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("wallpaper"));
0031     request.setArguments({uint(0)});
0032 
0033     const QDBusReply<QVariantMap> response = QDBusConnection::sessionBus().call(request);
0034     qDebug() << "Current wallpaper:" << response.value();
0035 }
0036 
0037 SetWallPaperTest::SetWallPaperTest(QObject *parent)
0038     : QObject(parent)
0039 {
0040 }
0041 
0042 void SetWallPaperTest::onWallpaperChanged(uint screenNum)
0043 {
0044     qDebug() << "WallpaperChanged on screen:" << screenNum;
0045     QCoreApplication::quit();
0046 }
0047 
0048 void SetWallPaperTest::setWallpaper(const QString &wallpaperPath)
0049 {
0050     printCurrentWallpaper();
0051 
0052     // find our remote
0053     auto iface = new QDBusInterface(SERVICE_NAME, "/PlasmaShell", "org.kde.PlasmaShell", QDBusConnection::sessionBus(), this);
0054     if (!iface->isValid()) {
0055         fprintf(stderr, "%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message()));
0056         QCoreApplication::instance()->quit();
0057     }
0058 
0059     const bool connected = QDBusConnection::sessionBus().connect(QStringLiteral(SERVICE_NAME),
0060                                                                  QStringLiteral("/PlasmaShell"),
0061                                                                  QStringLiteral("org.kde.PlasmaShell"),
0062                                                                  QStringLiteral("wallpaperChanged"),
0063                                                                  this,
0064                                                                  SLOT(onWallpaperChanged(uint)));
0065     if (!connected) {
0066         qDebug() << "Could not connect to interface";
0067         return;
0068     }
0069 
0070     QVariantMap params;
0071     params.insert(QStringLiteral("Image"), wallpaperPath);
0072     const QDBusReply<void> response = iface->call(QStringLiteral("setWallpaper"), QStringLiteral("org.kde.image"), params, uint(0));
0073     if (!response.isValid()) {
0074         qDebug() << "failed to set wallpaper:" << response.error();
0075     }
0076 }
0077 
0078 int main(int argc, char *argv[])
0079 {
0080     QCoreApplication app(argc, argv);
0081 
0082     QString wallpaperPath;
0083     if (argc == 1) {
0084         wallpaperPath = QStringLiteral("/usr/share/wallpapers/Altai/");
0085     } else {
0086         wallpaperPath = QString(argv[1]);
0087     }
0088 
0089     qDebug() << "Setting wallpaper to" << wallpaperPath;
0090 
0091     SetWallPaperTest setWallpaperTest;
0092     setWallpaperTest.setWallpaper(wallpaperPath);
0093 
0094     return app.exec();
0095 }
0096 
0097 #include "setwallpapertest.moc"