From 185b7b451b60085b40835c94ac157f9cb9557eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Deparis?= Date: Wed, 22 Dec 2021 22:44:55 +0100 Subject: [PATCH] Refactor VPNPage between VPN and WireGuard code --- src/Views/VPNPage.vala | 107 +++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 67 deletions(-) diff --git a/src/Views/VPNPage.vala b/src/Views/VPNPage.vala index 9b230eb2..1205dd82 100644 --- a/src/Views/VPNPage.vala +++ b/src/Views/VPNPage.vala @@ -19,9 +19,7 @@ public class Network.VPNPage : Network.Widgets.Page { public Network.Widgets.DeviceItem owner { get; construct; } - private Gee.List active_connections; - private Gee.List active_wireguard_connections; - + private Gee.List active_connections; private Gtk.ListBox vpn_list; private uint timeout_id = 0; @@ -127,8 +125,7 @@ public class Network.VPNPage : Network.Widgets.Page { edit_connection_button.sensitive = row != null; }); - active_connections = new Gee.ArrayList (); - active_wireguard_connections = new Gee.ArrayList (); + active_connections = new Gee.ArrayList (); update (); unowned NetworkManager network_manager = NetworkManager.get_default (); @@ -141,49 +138,39 @@ public class Network.VPNPage : Network.Widgets.Page { VPNMenuItem? item = null; foreach (var ac in active_connections) { if (ac != null) { - switch (ac.get_vpn_state ()) { - case NM.VpnConnectionState.UNKNOWN: - case NM.VpnConnectionState.DISCONNECTED: - state = NM.DeviceState.DISCONNECTED; - break; - case NM.VpnConnectionState.PREPARE: - case NM.VpnConnectionState.NEED_AUTH: - case NM.VpnConnectionState.IP_CONFIG_GET: - case NM.VpnConnectionState.CONNECT: - state = NM.DeviceState.PREPARE; - break; - case NM.VpnConnectionState.FAILED: - state = NM.DeviceState.FAILED; - break; - case NM.VpnConnectionState.ACTIVATED: - state = NM.DeviceState.ACTIVATED; - break; - } - - item = get_item_by_uuid (ac.get_uuid ()); - } else { - state = NM.DeviceState.DISCONNECTED; - } - - if (item != null) { - item.state = state; - } - } - - foreach (var ac in active_wireguard_connections) { - if (ac != null) { - switch (ac.get_state ()) { - case NM.ActiveConnectionState.UNKNOWN: - case NM.ActiveConnectionState.DEACTIVATED: - case NM.ActiveConnectionState.DEACTIVATING: - state = NM.DeviceState.DISCONNECTED; - break; - case NM.ActiveConnectionState.ACTIVATING: - state = NM.DeviceState.PREPARE; - break; - case NM.ActiveConnectionState.ACTIVATED: - state = NM.DeviceState.ACTIVATED; - break; + if (ac.get_vpn ()) { + switch (((NM.VpnConnection)ac).get_vpn_state ()) { + case NM.VpnConnectionState.UNKNOWN: + case NM.VpnConnectionState.DISCONNECTED: + state = NM.DeviceState.DISCONNECTED; + break; + case NM.VpnConnectionState.PREPARE: + case NM.VpnConnectionState.NEED_AUTH: + case NM.VpnConnectionState.IP_CONFIG_GET: + case NM.VpnConnectionState.CONNECT: + state = NM.DeviceState.PREPARE; + break; + case NM.VpnConnectionState.FAILED: + state = NM.DeviceState.FAILED; + break; + case NM.VpnConnectionState.ACTIVATED: + state = NM.DeviceState.ACTIVATED; + break; + } + } else if (ac.get_connection_type () == NM.SettingWireGuard.SETTING_NAME) { + switch (ac.get_state ()) { + case NM.ActiveConnectionState.UNKNOWN: + case NM.ActiveConnectionState.DEACTIVATED: + case NM.ActiveConnectionState.DEACTIVATING: + state = NM.DeviceState.DISCONNECTED; + break; + case NM.ActiveConnectionState.ACTIVATING: + state = NM.DeviceState.PREPARE; + break; + case NM.ActiveConnectionState.ACTIVATED: + state = NM.DeviceState.ACTIVATED; + break; + } } item = get_item_by_uuid (ac.get_uuid ()); @@ -235,18 +222,15 @@ public class Network.VPNPage : Network.Widgets.Page { private void update_active_connections () { active_connections.clear (); - active_wireguard_connections.clear (); unowned NetworkManager network_manager = NetworkManager.get_default (); network_manager.client.get_active_connections ().foreach ((ac) => { + active_connections.add (ac); if (ac.get_vpn ()) { - active_connections.add ((NM.VpnConnection) ac); - (ac as NM.VpnConnection).vpn_state_changed.connect (update); + ((NM.VpnConnection) ac).vpn_state_changed.connect (update); } else if (ac.get_connection_type () == NM.SettingWireGuard.SETTING_NAME) { - active_wireguard_connections.add ((NM.ActiveConnection) ac); - (ac as NM.ActiveConnection).state_changed.connect (update); + ac.state_changed.connect (update); } - }); } @@ -267,21 +251,10 @@ public class Network.VPNPage : Network.Widgets.Page { } catch (Error e) { warning (e.message); } - update (); - return; - } - } - foreach (var ac in active_wireguard_connections) { - if (ac.get_connection () == item.connection) { - try { - network_manager.client.deactivate_connection (ac); - } catch (Error e) { - warning (e.message); - } - update (); - return; + break; } } + update (); } private void remove_button_cb () {