| 1 | // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 2 | // SPDX-FileCopyrightText: 2025 Harald Sitter <sitter@kde.org> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <KSystemInhibitor> |
| 7 | |
| 8 | #include <QWindow> |
| 9 | #include <qqml.h> |
| 10 | |
| 11 | /*! |
| 12 | \qmltype SystemInhibitor |
| 13 | \since 6.23 |
| 14 | \inqmlmodule org.kde.guiaddons |
| 15 | \brief Inhibit system actions such as logout, suspend, etc. |
| 16 | |
| 17 | \qml |
| 18 | readonly property SystemInhibitor inhibitor: SystemInhibitor { |
| 19 | window: ApplicationWindow.window |
| 20 | types: SystemInhibitor.Suspend | SystemInhibitor.Idle |
| 21 | enabled: true |
| 22 | reason: KI18n.i18nc("inhibition reason", "Playing a video") |
| 23 | } |
| 24 | \endqml |
| 25 | */ |
| 26 | class SystemInhibitor : public QObject, public QQmlParserStatus |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | Q_INTERFACES(QQmlParserStatus) |
| 30 | QML_ELEMENT |
| 31 | // Grab the enums from KSystemInhibitor into our namespace for consistent QML API - so we can address it as SystemInhibitor.Logout rather than |
| 32 | // KSystemInhibitor.Logout |
| 33 | QML_EXTENDED_NAMESPACE(KSystemInhibitor) |
| 34 | |
| 35 | /*! |
| 36 | \qmlproperty Window SystemInhibitor::window |
| 37 | The window for which to apply the inhibition. May be null in which case possible user queries will not be associated with any window. |
| 38 | */ |
| 39 | Q_PROPERTY(QWindow *window MEMBER m_window NOTIFY windowChanged) |
| 40 | |
| 41 | /*! |
| 42 | \qmlproperty enumeration SystemInhibitor::types |
| 43 | \qmlenumeratorsfrom KSystemInhibitor::Type |
| 44 | \required |
| 45 | The types of inhibition to apply. May be a combination of multiple types. |
| 46 | */ |
| 47 | Q_PROPERTY(KSystemInhibitor::Types types MEMBER m_types NOTIFY typesChanged REQUIRED) |
| 48 | |
| 49 | /*! |
| 50 | \qmlproperty string SystemInhibitor::reason |
| 51 | \required |
| 52 | The user-facing reason why ths inhibition is in place (e.g. "Playing a video"). |
| 53 | */ |
| 54 | Q_PROPERTY(QString reason MEMBER m_reason NOTIFY reasonChanged REQUIRED) |
| 55 | |
| 56 | /*! |
| 57 | \qmlproperty bool SystemInhibitor::enabled |
| 58 | Toggle for enabling/disabling the inhibition. Please note that inhibiting is an asynchronous operation and may be delayed or fail, the property always |
| 59 | reflects the requested state, not the actual state. |
| 60 | */ |
| 61 | Q_PROPERTY(bool enabled MEMBER m_enabled WRITE setEnabled NOTIFY enabledChanged) |
| 62 | public: |
| 63 | using QObject::QObject; |
| 64 | |
| 65 | void classBegin() override; |
| 66 | void componentComplete() override; |
| 67 | void setEnabled(bool enable); |
| 68 | |
| 69 | Q_SIGNALS: |
| 70 | void windowChanged(); |
| 71 | void typesChanged(); |
| 72 | void reasonChanged(); |
| 73 | void enabledChanged(); |
| 74 | |
| 75 | private: |
| 76 | void apply(); |
| 77 | |
| 78 | bool m_ready = false; |
| 79 | |
| 80 | QWindow *m_window = nullptr; |
| 81 | KSystemInhibitor::Types m_types; |
| 82 | QString m_reason; |
| 83 | bool m_enabled = false; |
| 84 | |
| 85 | std::optional<KSystemInhibitor> m_inhibitor; |
| 86 | }; |
| 87 | |