summaryrefslogtreecommitdiff
path: root/lib/network/network.vala
blob: b5a6c612f481c2ff381c21c2a06549999638fadb (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
namespace AstalNetwork {
    public Network get_default() {
        return Network.get_default();
    }
}

public class AstalNetwork.Network : Object {
    private static Network instance;
    public static Network get_default() {
        if (instance == null)
            instance = new Network();

        return instance;
    }

    public NM.Client client { get; private set; }

    public Wifi? wifi { get; private set; }
    public Wired? wired { get; private set; }
    public Primary primary { get; private set; }

    public Connectivity connectivity {
        get { return (Connectivity)client.connectivity; }
    }

    public State state {
        get { return (State)client.state; }
    }

    construct {
        try {
            client = new NM.Client();
            var wifi_device = (NM.DeviceWifi)get_device(NM.DeviceType.WIFI);
            if (wifi_device != null)
                wifi = new Wifi(wifi_device);

            var ethernet = (NM.DeviceEthernet)get_device(NM.DeviceType.ETHERNET);
            if (ethernet != null)
                wired = new Wired(ethernet);

            sync();
            client.notify["primary-connection"].connect(sync);
            client.notify["activating-connection"].connect(sync);

            client.notify["state"].connect(() => notify_property("state"));
            client.notify["connectivity"].connect(() => notify_property("connectivity"));
        } catch (Error err) {
            critical(err.message);
        }
    }

    private NM.Device? get_device(NM.DeviceType t) {
        var valid = new GenericArray<NM.Device>();
        foreach (var device in client.get_devices()) {
            if (device.device_type == t)
                valid.add(device);
        }

        foreach (var device in valid) {
            if (device.active_connection != null)
                return device;
        }

        if (valid.length > 0)
            return valid.get(0);

        return null;
    }

    private void sync() {
        var ac = client.get_primary_connection();

        if (ac == null)
            ac = client.get_activating_connection();

        if (ac != null)
            primary = Primary.from_connection_type(ac.type);
        else
            primary = Primary.UNKNOWN;
    }
}

public enum AstalNetwork.Primary {
    UNKNOWN,
    WIRED,
    WIFI;

    public string to_string() {
        switch (this) {
            case WIFI: return "wifi";
            case WIRED: return "wired";
            default: return "unknown";
        }
    }

    public static Primary from_connection_type(string type) {
        switch (type) {
            case "802-11-wireless": return Primary.WIFI;
            case "802-3-ethernet": return Primary.WIRED;
            default: return Primary.UNKNOWN;
        }
    }
}

// alias for NM.State
public enum AstalNetwork.State {
    UNKNOWN = 0,
    ASLEEP = 10,
    DISCONNECTED = 20,
    DISCONNECTING = 30,
    CONNECTING = 40,
    CONNECTED_LOCAL = 50,
    CONNECTED_SITE = 60,
    CONNECTED_GLOBAL = 70;

    public string to_string() {
        switch (this) {
            case ASLEEP: return "asleep";
            case DISCONNECTED: return "disconnected";
            case DISCONNECTING: return "disconnecting";
            case CONNECTING: return "connecting";
            case CONNECTED_LOCAL: return "connected_local";
            case CONNECTED_SITE: return "connected_site";
            case CONNECTED_GLOBAL: return "connected_global";
            default: return "unknown";
        }
    }
}


// alias for NM.ConnectivityState
public enum AstalNetwork.Connectivity {
    UNKNOWN,
    NONE,
    PORTAL,
    LIMITED,
    FULL;

    public string to_string() {
        switch (this) {
            case NONE: return "none";
            case PORTAL: return "portal";
            case LIMITED: return "limited";
            case FULL: return "full";
            default: return "unknown";
        }
    }
}

// alias for NM.DeviceState
public enum AstalNetwork.DeviceState {
    UNKNOWN = 0,
    UNMANAGED = 10,
    UNAVAILABLE = 20,
    DISCONNECTED = 30,
    PREPARE = 40,
    CONFIG = 50,
    NEED_AUTH = 60,
    IP_CONFIG = 70,
    IP_CHECK = 80,
    SECONDARIES = 90,
    ACTIVATED = 100,
    DEACTIVATING = 110,
    FAILED = 120;

    public string to_string() {
        switch (this) {
            case UNMANAGED: return "unmanaged";
            case UNAVAILABLE: return "unavailable";
            case DISCONNECTED: return "disconnected";
            case PREPARE: return "prepare";
            case CONFIG: return "config";
            case NEED_AUTH: return "need_auth";
            case IP_CONFIG: return "ip_config";
            case IP_CHECK: return "ip_check";
            case SECONDARIES: return "secondaries";
            case ACTIVATED: return "activated";
            case DEACTIVATING: return "deactivating";
            case FAILED: return "failed";
            default: return "unknown";
        }

    }
}

public enum AstalNetwork.Internet {
    CONNECTED,
    CONNECTING,
    DISCONNECTED;

    public static Internet from_device(NM.Device device) {
        if (device == null || device.active_connection == null)
            return DISCONNECTED;

        switch (device.active_connection.state) {
            case NM.ActiveConnectionState.ACTIVATED: return CONNECTED;
            case NM.ActiveConnectionState.ACTIVATING: return CONNECTING;
            default: return DISCONNECTED;
        }
    }

    public string to_string() {
        switch (this) {
            case CONNECTED: return "connected";
            case CONNECTING: return "connecting";
            default: return "disconnected";
        }
    }
}