File indexing completed on 2024-05-05 05:29:46

0001 /********************************************************************
0002  This file is part of the KDE project.
0003 
0004 SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
0005 
0006 SPDX-License-Identifier: GPL-2.0-or-later
0007 *********************************************************************/
0008 
0009 #include <QCommandLineOption>
0010 #include <QCommandLineParser>
0011 #include <QCoreApplication>
0012 #include <QDBusConnection>
0013 #include <QDBusMessage>
0014 #include <QDebug>
0015 
0016 #include <KLocalizedString>
0017 #include <KProcess>
0018 
0019 int main(int argc, char **argv)
0020 {
0021     QCoreApplication app(argc, argv);
0022     QCoreApplication::setApplicationName(QStringLiteral("kde-inhibit"));
0023     KLocalizedString::setApplicationDomain(QByteArrayLiteral("kde-inhibit"));
0024     QCommandLineParser parser;
0025     parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsPositionalArguments);
0026     parser.setApplicationDescription(i18n("Inhibit various desktop functions whilst a command runs"));
0027 
0028     QCommandLineOption powerOption(QStringLiteral("power"), i18n("Inhibit power management"));
0029     parser.addOption(powerOption);
0030 
0031     QCommandLineOption screenSaverOption(QStringLiteral("screenSaver"), i18n("Inhibit screensaver"));
0032     parser.addOption(screenSaverOption);
0033 
0034     QCommandLineOption colorCorrectOption(QStringLiteral("colorCorrect"), i18n("Inhibit colour correction (night mode)"));
0035     parser.addOption(colorCorrectOption);
0036 
0037     QCommandLineOption notificationsOption(QStringLiteral("notifications"), i18n("Inhibit notifications (Do not disturb)"));
0038     parser.addOption(notificationsOption);
0039 
0040     parser.addPositionalArgument(QStringLiteral("command"), i18n("Command with arguments to run"), QStringLiteral("command [args...]"));
0041 
0042     parser.addHelpOption();
0043 
0044     parser.process(app);
0045 
0046     QStringList command = parser.positionalArguments();
0047 
0048     if (command.isEmpty()) {
0049         parser.showHelp(-1);
0050     }
0051 
0052     auto warnOnError = [](const QDBusMessage &reply) {
0053         if (reply.type() == QDBusMessage::ErrorMessage) {
0054             qWarning() << "Inhibit failed" << reply.errorMessage();
0055         }
0056     };
0057 
0058     if (parser.isSet(powerOption)) {
0059         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
0060                                                                   QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
0061                                                                   QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
0062                                                                   QStringLiteral("Inhibit"));
0063         inhibitCall.setArguments({i18nc("Script as in shell script", "Running Script"), command.first()});
0064         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0065         // ignore reply with the cookie as we don't really care if it failed
0066     }
0067     if (parser.isSet(screenSaverOption)) {
0068         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"),
0069                                                                   QStringLiteral("/org/freedesktop/ScreenSaver"),
0070                                                                   QStringLiteral("org.freedesktop.ScreenSaver"),
0071                                                                   QStringLiteral("Inhibit"));
0072         inhibitCall.setArguments({i18nc("Script as in shell script", "Running Script"), command.first()});
0073         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0074     }
0075     if (parser.isSet(colorCorrectOption)) {
0076         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
0077                                                                   QStringLiteral("/ColorCorrect"),
0078                                                                   QStringLiteral("org.kde.kwin.ColorCorrect"),
0079                                                                   QStringLiteral("inhibit"));
0080         // no arguments needed
0081         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0082     }
0083     if (parser.isSet(notificationsOption)) {
0084         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"),
0085                                                                   QStringLiteral("/org/freedesktop/Notifications"),
0086                                                                   QStringLiteral("org.freedesktop.Notifications"),
0087                                                                   QStringLiteral("Inhibit"));
0088         inhibitCall.setArguments({i18nc("Script as in shell script", "Running Script"), command.first(), QVariantMap()});
0089         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0090     }
0091 
0092     // finally run the script and exit when we're done
0093     KProcess::execute(command);
0094 
0095     // we don't explicitly uninhibit as closing our DBus connection will release everything automatically
0096 }