Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/Privilege.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell.Services.Polkit

Item {
    id: root

    property bool active: false
    property bool completed: false
    property bool authorized: false
    property bool cancelled: false
    property bool confirming: false
    property bool awaitingCompletion: false
    property string error: ""
    property var request: ({})
    property var lastAction: ({})
    readonly property var polkitAgent: agent

    function stateObject() {
        return {
            "agentRegistered": !!(polkitAgent && polkitAgent.isRegistered),
            "agentActive": !!(polkitAgent && polkitAgent.isActive),
            "active": active,
            "completed": completed,
            "authorized": authorized,
            "cancelled": cancelled,
            "confirming": confirming,
            "awaitingCompletion": awaitingCompletion,
            "request": request,
            "error": error,
            "lastAction": lastAction
        };
    }

    function stateJson() {
        return JSON.stringify(stateObject());
    }

    function finish(granted) {
        completed = true;
        authorized = !!granted;
        cancelled = false;
        // Publish confirmation before dropping the active prompt so a success
        // never transiently unmaps and recreates its native security surface.
        confirming = !!granted;
        awaitingCompletion = false;
        error = "";
        active = false;
        if (confirming)
            successClose.restart();
    }

    function submit(secret) {
        const length = String(secret).length;
        const flow = polkitAgent ? polkitAgent.flow : null;
        if (flow && flow.isResponseRequired) {
            awaitingCompletion = true;
            flow.submit(String(secret));
        } else {
            awaitingCompletion = false;
            error = "no-active-polkit-flow";
        }
        lastAction = {
            "kind": "submit",
            "secretLength": length
        };
        return stateJson();
    }

    function deny() {
        const flow = polkitAgent ? polkitAgent.flow : null;
        if (flow)
            flow.cancelAuthenticationRequest();

        active = false;
        completed = true;
        authorized = false;
        cancelled = false;
        confirming = false;
        awaitingCompletion = false;
        successClose.stop();
        error = "";
        lastAction = {
            "kind": "deny"
        };
        return stateJson();
    }

    function cancel() {
        const flow = polkitAgent ? polkitAgent.flow : null;
        if (flow)
            flow.cancelAuthenticationRequest();

        active = false;
        completed = false;
        authorized = false;
        cancelled = true;
        confirming = false;
        awaitingCompletion = false;
        successClose.stop();
        error = "";
        lastAction = {
            "kind": "cancel"
        };
        return stateJson();
    }

    function fail(message) {
        active = true;
        completed = false;
        authorized = false;
        cancelled = false;
        confirming = false;
        awaitingCompletion = false;
        successClose.stop();
        error = String(message);
        lastAction = {
            "kind": "fail",
            "message": error
        };
        return stateJson();
    }

    function clear() {
        active = false;
        completed = false;
        authorized = false;
        cancelled = false;
        confirming = false;
        awaitingCompletion = false;
        successClose.stop();
        request = ({});
        error = "";
        lastAction = {
            "kind": "clear"
        };
        return stateJson();
    }

    Timer {
        id: successClose

        interval: 620
        repeat: false
        onTriggered: root.confirming = false
    }

    PolkitAgent {
        id: agent

        path: "/org/nuguland/PolkitAgent"
    }

    Connections {
        function onAuthenticationRequestStarted() {
            const flow = root.polkitAgent ? root.polkitAgent.flow : null;
            root.active = true;
            root.completed = false;
            root.authorized = false;
            root.cancelled = false;
            root.confirming = false;
            root.awaitingCompletion = false;
            successClose.stop();
            root.error = "";
            root.request = {
                "app": flow ? (flow.iconName || "polkit") : "polkit",
                "actionId": flow ? flow.actionId : "",
                "message": flow ? flow.message : "authentication required",
                "prompt": flow ? (flow.inputPrompt || "password") : "password",
                "responseVisible": flow ? !!flow.responseVisible : false
            };
            root.lastAction = {
                "kind": "native-request",
                "actionId": root.request.actionId
            };
        }

        function onIsActiveChanged() {
            if (root.polkitAgent && !root.polkitAgent.isActive && root.active && root.awaitingCompletion)
                root.finish(true);
        }

        target: root.polkitAgent
    }

    Connections {
        function onIsCompletedChanged() {
            const flow = root.polkitAgent ? root.polkitAgent.flow : null;
            if (flow && flow.isCompleted && flow.isSuccessful)
                root.finish(true);
        }

        function onAuthenticationSucceeded() {
            root.finish(true);
        }

        function onAuthenticationFailed() {
            root.fail("Authentication failed");
        }

        function onAuthenticationRequestCancelled() {
            root.cancel();
        }

        target: !root.polkitAgent ? null : root.polkitAgent.flow
    }
}