Skip to content

Commit

Permalink
Refactor VPNPage between VPN and WireGuard code
Browse files Browse the repository at this point in the history
  • Loading branch information
milouse committed Aug 22, 2022
1 parent e7ad5b1 commit 93bbf32
Showing 1 changed file with 40 additions and 67 deletions.
107 changes: 40 additions & 67 deletions src/Views/VPNPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

public class Network.VPNPage : Network.Widgets.Page {
public Network.Widgets.DeviceItem owner { get; construct; }
private Gee.List<NM.VpnConnection> active_connections;
private Gee.List<NM.ActiveConnection> active_wireguard_connections;

private Gee.List<NM.ActiveConnection> active_connections;

private Gtk.ListBox vpn_list;
private uint timeout_id = 0;
Expand Down Expand Up @@ -127,8 +125,7 @@ public class Network.VPNPage : Network.Widgets.Page {
edit_connection_button.sensitive = row != null;
});

active_connections = new Gee.ArrayList<NM.VpnConnection> ();
active_wireguard_connections = new Gee.ArrayList<NM.ActiveConnection> ();
active_connections = new Gee.ArrayList<NM.ActiveConnection> ();

update ();
unowned NetworkManager network_manager = NetworkManager.get_default ();
Expand All @@ -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 ());
Expand Down Expand Up @@ -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);
}

});
}

Expand All @@ -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 () {
Expand Down

0 comments on commit 93bbf32

Please sign in to comment.