File indexing completed on 2024-05-12 05:38:37

0001 /*
0002     SPDX-FileCopyrightText: 2021 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "config-X11.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QCommandLineParser>
0012 #include <QCoreApplication>
0013 #include <QDBusConnection>
0014 #include <QDBusMessage>
0015 #include <QDebug>
0016 #include <QFileInfo>
0017 #include <QTimer>
0018 
0019 int main(int argc, char **argv)
0020 {
0021     QCoreApplication app(argc, argv);
0022     QCoreApplication::setApplicationName(QStringLiteral("plasma-apply-wallpaperimage"));
0023     QCoreApplication::setApplicationVersion(QStringLiteral("1.0"));
0024     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0025     KLocalizedString::setApplicationDomain(QByteArrayLiteral("plasma-apply-wallpaperimage"));
0026 
0027     QCommandLineParser *parser = new QCommandLineParser;
0028     parser->addHelpOption();
0029     parser->setApplicationDescription(i18n("This tool allows you to set an image as the wallpaper for the Plasma session."));
0030     parser->addPositionalArgument(QStringLiteral("imagefile"),
0031                                   i18n("An image file or an installed wallpaper kpackage that you wish to set as the wallpaper for your Plasma session"));
0032     parser->process(app);
0033 
0034     int errorCode{0};
0035     QTextStream ts(stdout);
0036     if (!parser->positionalArguments().isEmpty()) {
0037         QString wallpaperFile{parser->positionalArguments().first()};
0038         QFileInfo wallpaperInfo{wallpaperFile};
0039         bool isWallpaper{false};
0040         bool isKPackage{false};
0041         if (wallpaperFile.contains(QStringLiteral("\'"))) {
0042             // If this happens, we might very well assume that there is some kind of funny business going on
0043             // even if technically it could just be a possessive. But, security first, so...
0044             ts << i18n(
0045                 "There is a stray single quote in the filename of this wallpaper (') - please contact the author of the wallpaper to fix this, or rename the "
0046                 "file yourself: %1",
0047                 wallpaperFile)
0048                << Qt::endl;
0049             errorCode = -1;
0050         } else {
0051             if (wallpaperInfo.exists()) {
0052                 if (wallpaperInfo.isFile()) {
0053                     // then we assume it's an image file... we could check with QImage, but
0054                     // that makes the operation much heavier than it needs to be
0055                     isWallpaper = true;
0056                 } else {
0057                     if (QFileInfo::exists(QStringLiteral("%1/metadata.desktop").arg(wallpaperFile))
0058                         || QFileInfo::exists(QStringLiteral("%1/metadata.json").arg(wallpaperFile))) {
0059                         isWallpaper = true;
0060                         isKPackage = true;
0061                         // Similarly to above, we could read all the information out of the kpackage, but
0062                         // that also is not hugely important, so we just deduce that this reasonably should
0063                         // be an installed kpackage
0064                     }
0065                 }
0066             }
0067         }
0068         if (isWallpaper) {
0069             QString script;
0070             QTextStream out(&script);
0071             out << "for (var key in desktops()) {"
0072                 << "var d = desktops()[key];"
0073                 << "d.wallpaperPlugin = 'org.kde.image';"
0074                 << "d.currentConfigGroup = ['Wallpaper', 'org.kde.image', 'General'];"
0075                 << "d.writeConfig('Image', 'file://" + wallpaperFile + "');"
0076                 << "}";
0077             auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"),
0078                                                           QStringLiteral("/PlasmaShell"),
0079                                                           QStringLiteral("org.kde.PlasmaShell"),
0080                                                           QStringLiteral("evaluateScript"));
0081             message.setArguments(QVariantList() << QVariant(script));
0082             auto reply = QDBusConnection::sessionBus().call(message);
0083 
0084             if (reply.type() == QDBusMessage::ErrorMessage) {
0085                 ts << i18n("An error occurred while attempting to set the Plasma wallpaper:\n") << reply.errorMessage() << Qt::endl;
0086                 errorCode = -1;
0087             } else {
0088                 if (isKPackage) {
0089                     ts << i18n("Successfully set the wallpaper for all desktops to the KPackage based %1", wallpaperFile) << Qt::endl;
0090                 } else {
0091                     ts << i18n("Successfully set the wallpaper for all desktops to the image %1", wallpaperFile) << Qt::endl;
0092                 }
0093             }
0094 
0095         } else if (errorCode == 0) {
0096             // Just to avoid spitting out multiple errors
0097             ts << i18n("The file passed to be set as wallpaper does not exist, or we cannot identify it as a wallpaper: %1", wallpaperFile) << Qt::endl;
0098             errorCode = -1;
0099         }
0100     } else {
0101         parser->showHelp();
0102     }
0103     QTimer::singleShot(0, &app, [&app, &errorCode]() {
0104         app.exit(errorCode);
0105     });
0106 
0107     return app.exec();
0108 }