ip_address icon indicating copy to clipboard operation
ip_address copied to clipboard

get an ip address via network manager

Open atassis opened this issue 1 year ago • 1 comments

You can change a logic of a widget from polling an ip address via request to listening to a DBus of a system to a change network event and to get an ip address from network manager. Below is an example from GPT

import QtQuick 2.15
import org.kde.plasma.components 3.0 as PlasmaComponents
import QtDBus 1.0

PlasmaComponents.Item {
    id: root

    // D-Bus interface to NetworkManager
    DBusInterface {
        id: nmInterface
        service: "org.freedesktop.NetworkManager"
        path: "/org/freedesktop/NetworkManager"
        iface: "org.freedesktop.DBus.Properties"

        signalsEnabled: true

        // Signal handler for PropertiesChanged
        function propertiesChanged(properties) {
            if ("PrimaryConnection" in properties) {
                updatePrimaryConnection();
            }
        }
    }

    // D-Bus interface for the primary connection
    DBusInterface {
        id: primaryConnInterface
        service: "org.freedesktop.NetworkManager"
        iface: "org.freedesktop.DBus.Properties"
        signalsEnabled: true

        function propertiesChanged(properties) {
            if ("Type" in properties || "Ip4Config" in properties) {
                updateWidget();
            }
        }
    }

    // Labels to display information
    PlasmaComponents.Label {
        id: ipLabel
        text: "IP Address: Loading..."
    }

    PlasmaComponents.Label {
        id: vpnLabel
        text: "VPN Status: Loading..."
    }

    // Function to update the primary connection
    function updatePrimaryConnection() {
        var call = nmInterface.call("Get", ["org.freedesktop.NetworkManager", "PrimaryConnection"]);
        call.onReply.connect(function(reply) {
            var primaryConnPath = reply.arguments[0];
            if (primaryConnPath === "/") {
                ipLabel.text = "IP Address: Not connected";
                vpnLabel.text = "VPN Status: Disabled";
                return;
            }

            // Update the primary connection interface
            primaryConnInterface.path = primaryConnPath;
            updateWidget();
        });
    }

    // Function to update the widget with connection details
    function updateWidget() {
        // Get connection type
        var typeCall = primaryConnInterface.call("Get", ["org.freedesktop.NetworkManager.ActiveConnection", "Type"]);
        typeCall.onReply.connect(function(reply) {
            var connType = reply.arguments[0];
            var isVpn = (connType === "vpn");
            vpnLabel.text = "VPN Status: " + (isVpn ? "Enabled" : "Disabled");

            // Get IP4Config path
            var ip4ConfigCall = primaryConnInterface.call("Get", ["org.freedesktop.NetworkManager.ActiveConnection", "Ip4Config"]);
            ip4ConfigCall.onReply.connect(function(ip4Reply) {
                var ip4ConfigPath = ip4Reply.arguments[0];
                if (ip4ConfigPath === "/") {
                    ipLabel.text = "IP Address: Not available";
                    return;
                }

                // Fetch IP addresses from Ip4Config
                var ip4Interface = Qt.createQmlObject(
                    'import QtDBus 1.0; DBusInterface { service: "org.freedesktop.NetworkManager"; path: "' + ip4ConfigPath + '"; iface: "org.freedesktop.DBus.Properties" }',
                    root
                );
                var addressCall = ip4Interface.call("Get", ["org.freedesktop.NetworkManager.IP4Config", "Addresses"]);
                addressCall.onReply.connect(function(addrReply) {
                    var addresses = addrReply.arguments[0];
                    if (addresses.length > 0) {
                        var ipAddr = addresses[0][0]; // First address
                        ipLabel.text = "IP Address: " + ipAddr;
                    } else {
                        ipLabel.text = "IP Address: Not available";
                    }
                    ip4Interface.destroy();
                });
            });
        });
    }

    Component.onCompleted: {
        updatePrimaryConnection();
    }
}

atassis avatar Feb 24 '25 23:02 atassis