repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/Privilege.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Services.Polkit

Item {
    id: root

    property bool active: false
    property bool completed: false
    property bool authorized: false
    property bool cancelled: false
    property string error: ""
    property var request: ({
    })
    property var lastAction: ({
    })
    readonly property bool testHarness: Quickshell.env("NUGULAND_SMOKE") === "1"
    readonly property bool nativeActionRoute: !testHarness
    readonly property var boundedActions: ["begin-test-request", "submit", "deny", "cancel", "fail", "clear"]
    readonly property var buttons: ["deny", "cancel", "confirm"]

    function privilegeSurfacePanelContract() {
        return {
            "source": "Quickshell Polkit manual agent.qml plus Nuguland Polkit.qml plus Nuguland PolkitService.qml",
            "ownedBy": "shell.qml inline PrivilegeSurface",
            "isolatedSecuritySurface": true,
            "requestIdentityCard": true,
            "secretPromptField": true,
            "denyCancelConfirmRow": true,
            "failureShakeSlot": true,
            "noDecorativeDistraction": true,
            "serviceFedOnly": true,
            "nativePolkitFlow": true,
            "actions": buttons,
            "requestParts": ["app", "actionId", "message", "prompt", "responseVisible"]
        };
    }

    function stateObject() {
        return {
            "backend": "Privilege.qml",
            "usesNativePolkitAgent": true,
            "testHarness": testHarness,
            "nativeActionRoute": nativeActionRoute,
            "agentPath": polkitAgent.path,
            "agentRegistered": !!polkitAgent.isRegistered,
            "agentActive": !!polkitAgent.isActive,
            "helperProcesses": [],
            "serviceOwnedActions": true,
            "boundedActions": boundedActions,
            "surface": "isolated-fullscreen-security-surface",
            "keyboardFocus": true,
            "escapeCancels": true,
            "decorativeDistraction": false,
            "buttons": buttons,
            "privilegeSurfacePanelContract": privilegeSurfacePanelContract(),
            "active": active,
            "completed": completed,
            "authorized": authorized,
            "cancelled": cancelled,
            "request": request,
            "error": error,
            "lastAction": lastAction
        };
    }

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

    function beginTestRequest(app, actionId, message) {
        if (!testHarness) {
            error = "test-request-disabled";
            lastAction = {
                "kind": "begin-test-request",
                "accepted": false,
                "nativeActionRoute": nativeActionRoute
            };
            return stateJson();
        }
        active = true;
        completed = false;
        authorized = false;
        cancelled = false;
        error = "";
        request = {
            "app": String(app),
            "actionId": String(actionId),
            "message": String(message),
            "prompt": "Password",
            "responseVisible": false
        };
        lastAction = {
            "kind": "begin-test-request",
            "app": request.app,
            "actionId": request.actionId,
            "nativeActionRoute": false,
            "testHarness": true
        };
        return stateJson();
    }

    function finish(granted) {
        active = false;
        completed = true;
        authorized = !!granted;
        cancelled = false;
        error = "";
    }

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

    function deny() {
        const flow = polkitAgent.flow;
        if (!testHarness && flow)
            flow.cancelAuthenticationRequest();

        active = false;
        completed = true;
        authorized = false;
        cancelled = false;
        error = "";
        lastAction = {
            "kind": "deny",
            "nativeActionRoute": nativeActionRoute,
            "testHarness": testHarness
        };
        return stateJson();
    }

    function cancel() {
        const flow = polkitAgent.flow;
        if (!testHarness && flow)
            flow.cancelAuthenticationRequest();

        active = false;
        completed = false;
        authorized = false;
        cancelled = true;
        error = "";
        lastAction = {
            "kind": "cancel",
            "nativeActionRoute": nativeActionRoute,
            "testHarness": testHarness
        };
        return stateJson();
    }

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

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

    PolkitAgent {
        id: polkitAgent

        path: "/org/nuguland/PolkitAgent"
    }

    Connections {
        function onAuthenticationRequestStarted() {
            const flow = polkitAgent.flow;
            root.active = true;
            root.completed = false;
            root.authorized = false;
            root.cancelled = false;
            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,
                "nativeActionRoute": true
            };
        }

        target: polkitAgent
    }

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

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

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

        target: polkitAgent.flow
    }

}