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 bool nativeActionRoute: true
    readonly property var boundedActions: ["submit", "deny", "cancel", "fail", "clear"]
    readonly property var buttons: ["deny", "cancel", "confirm"]
    readonly property var polkitAgent: polkitAgentLoader.item

    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,
            "nativeActionRoute": nativeActionRoute,
            "agentPath": "/org/nuguland/PolkitAgent",
            "agentRegistered": !!(polkitAgent && polkitAgent.isRegistered),
            "agentActive": !!(polkitAgent && 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,
            "confirming": confirming,
            "awaitingCompletion": awaitingCompletion,
            "request": request,
            "error": error,
            "lastAction": lastAction
        };
    }

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

    function finish(granted) {
        active = false;
        completed = true;
        authorized = !!granted;
        cancelled = false;
        confirming = !!granted;
        awaitingCompletion = false;
        error = "";
        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,
            "nativeActionRoute": nativeActionRoute
        };
        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",
            "nativeActionRoute": nativeActionRoute
        };
        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",
            "nativeActionRoute": nativeActionRoute
        };
        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
    }

    Loader {
        id: polkitAgentLoader

        active: true

        sourceComponent: PolkitAgent {
            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,
                "nativeActionRoute": true
            };
        }

        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
    }
}