File indexing completed on 2024-04-28 16:44:24

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("kdeinhibit"));
0023     QCommandLineParser parser;
0024     parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsPositionalArguments);
0025     parser.setApplicationDescription(i18n("Inhibit various desktop functions whilst a command runs"));
0026 
0027     QCommandLineOption powerOption(QStringLiteral("power"), i18n("Inhibit power management"));
0028     parser.addOption(powerOption);
0029 
0030     QCommandLineOption screenSaverOption(QStringLiteral("screenSaver"), i18n("Inhibit screensaver"));
0031     parser.addOption(screenSaverOption);
0032 
0033     QCommandLineOption colorCorrectOption(QStringLiteral("colorCorrect"), i18n("Inhibit colour correction (night mode)"));
0034     parser.addOption(colorCorrectOption);
0035 
0036     QCommandLineOption notificationsOption(QStringLiteral("notifications"), i18n("Inhibit notifications (Do not disturb)"));
0037     parser.addOption(notificationsOption);
0038 
0039     parser.addPositionalArgument(QStringLiteral("command..."), i18n("Command to run"));
0040 
0041     parser.addHelpOption();
0042 
0043     parser.process(app);
0044 
0045     QStringList command = parser.positionalArguments();
0046 
0047     if (command.isEmpty()) {
0048         parser.showHelp(-1);
0049     }
0050 
0051     auto warnOnError = [](const QDBusMessage &reply) {
0052         if (reply.type() == QDBusMessage::ErrorMessage) {
0053             qWarning() << "Inhibit failed" << reply.errorMessage();
0054         }
0055     };
0056 
0057     if (parser.isSet(powerOption)) {
0058         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
0059                                                                   QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
0060                                                                   QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
0061                                                                   QStringLiteral("Inhibit"));
0062         inhibitCall.setArguments({i18nc("Script as in shell script", "Running Script"), command.first()});
0063         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0064         // ignore reply with the cookie as we don't really care if it failed
0065     }
0066     if (parser.isSet(screenSaverOption)) {
0067         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"),
0068                                                                   QStringLiteral("/org/freedesktop/ScreenSaver"),
0069                                                                   QStringLiteral("org.freedesktop.ScreenSaver"),
0070                                                                   QStringLiteral("Inhibit"));
0071         inhibitCall.setArguments({i18nc("Script as in shell script", "Running Script"), command.first()});
0072         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0073     }
0074     if (parser.isSet(colorCorrectOption)) {
0075         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
0076                                                                   QStringLiteral("/ColorCorrect"),
0077                                                                   QStringLiteral("org.kde.kwin.ColorCorrect"),
0078                                                                   QStringLiteral("inhibit"));
0079         // no arguments needed
0080         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0081     }
0082     if (parser.isSet(notificationsOption)) {
0083         QDBusMessage inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"),
0084                                                                   QStringLiteral("/org/freedesktop/Notifications"),
0085                                                                   QStringLiteral("org.freedesktop.Notifications"),
0086                                                                   QStringLiteral("Inhibit"));
0087         inhibitCall.setArguments({i18nc("Script as in shell script", "Running Script"), command.first(), QVariantMap()});
0088         warnOnError(QDBusConnection::sessionBus().call(inhibitCall));
0089     }
0090 
0091     // finally run the script and exit when we're done
0092     KProcess::execute(command);
0093 
0094     // we don't explicitly uninhibit as closing our DBus connection will release everything automatically
0095 }