summaryrefslogtreecommitdiff
path: root/lib/network/wired.vala
blob: 68cf46050d153924815490c245e09bb88284e14e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class AstalNetwork.Wired : Object {
    private const string ICON_CONNECTED = "network-wired-symbolic";
    private const string ICON_DISCONNECTED = "network-wired-disconnected-symbolic";
    private const string ICON_ACQUIRING = "network-wired-acquiring-symbolic";
    private const string ICON_NO_ROUTE = "network-wired-no-route-symbolic";

    public NM.DeviceEthernet device { get; construct set; }

    public NM.ActiveConnection connection;
    private ulong connection_handler = 0;

    internal Wired(NM.DeviceEthernet device) {
        this.device = device;

        speed = device.speed;
        state = (DeviceState)device.state;
        icon_name = _icon();

        device.notify.connect((pspec) => {
            if (pspec.name == "speed") {
                speed = device.speed;
            }
            if (pspec.name == "state") {
                state = (DeviceState)device.state;
            }
            if (pspec.name == "active-connection") {
                on_active_connection();
            }
            icon_name = _icon();
        });

        device.client.notify.connect(() => { icon_name = _icon(); });

        on_active_connection();
        icon_name = _icon();
    }

    private void on_active_connection() {
        if (connection_handler > 0 && connection != null) {
            connection.disconnect(connection_handler);
            connection_handler = 0;
            connection = null;
        }

        connection = device.active_connection;
        if (connection != null) {
            connection_handler = connection.notify["state"].connect(() => {
                internet = Internet.from_device(device);
            });
        }
    }

    public uint speed { get; private set; }
    public Internet internet { get; private set; }
    public DeviceState state { get; private set; }
    public string icon_name { get; private set; }

    private string _icon() {
        var full = device.client.connectivity == NM.ConnectivityState.FULL;

        if (internet == Internet.CONNECTING) {
            return ICON_ACQUIRING;
        }

        if (internet == Internet.CONNECTED) {
            if (!full) return ICON_NO_ROUTE;

            return ICON_CONNECTED;
        }

        return ICON_DISCONNECTED;
    }
}