Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VPNPage: rewrite connection forget #388

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 33 additions & 69 deletions src/Views/VPNPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ public class Network.VPNPage : Network.Widgets.Page {
add_button.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
add_button_label.mnemonic_widget = add_button;

var remove_button = new Gtk.Button.from_icon_name ("list-remove-symbolic") {
tooltip_text = _("Forget selected VPN…"),
sensitive = false
};

actionbar.pack_start (add_button);
actionbar.pack_start (remove_button);

var scrolled = new Gtk.ScrolledWindow (null, null) {
child = vpn_list
Expand Down Expand Up @@ -103,8 +97,6 @@ public class Network.VPNPage : Network.Widgets.Page {
try_connection_editor ("--create --type=vpn");
});

remove_button.clicked.connect (remove_button_cb);

remove_vpn_toast.default_action.connect (() => {
GLib.Source.remove (timeout_id);
timeout_id = 0;
Expand All @@ -119,10 +111,6 @@ public class Network.VPNPage : Network.Widgets.Page {
}
});

vpn_list.row_selected.connect (row => {
remove_button.sensitive = row != null;
});

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

update ();
Expand Down Expand Up @@ -195,6 +183,15 @@ public class Network.VPNPage : Network.Widgets.Page {

public void add_connection (NM.RemoteConnection connection) {
var item = new VPNMenuItem (connection);
item.remove_request.connect (() => {
sel_row = item;
remove_vpn_toast.send_notification ();
timeout_id = GLib.Timeout.add (3600, () => {
timeout_id = 0;
delete_connection (item);
return GLib.Source.REMOVE;
});
});

vpn_list.add (item);
update ();
Expand Down Expand Up @@ -280,36 +277,6 @@ public class Network.VPNPage : Network.Widgets.Page {
}
}

private void remove_button_cb () {
sel_row = vpn_list.get_selected_row () as VPNMenuItem;
if (sel_row != null) {
if (sel_row.state == NM.DeviceState.ACTIVATED ||
sel_row.state == NM.DeviceState.PREPARE) {
var dialog = new Granite.MessageDialog (
_("Failed to remove VPN connection"),
_("Cannot remove an active VPN connection."),
new ThemedIcon ("network-vpn"),
Gtk.ButtonsType.CLOSE
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = (Gtk.Window) get_toplevel ()
};
dialog.present ();
dialog.response.connect (dialog.destroy);
return;
} else {
remove_vpn_toast.send_notification ();
sel_row.hide ();
timeout_id = GLib.Timeout.add (3600, () => {
timeout_id = 0;
delete_connection ();
return GLib.Source.REMOVE;
});
}
}
}

private void try_connection_editor (string args) {
try {
var appinfo = AppInfo.create_from_commandline (
Expand All @@ -335,34 +302,31 @@ public class Network.VPNPage : Network.Widgets.Page {
}
}

private void delete_connection () {
var selected_row = vpn_list.get_selected_row () as VPNMenuItem;
if (selected_row != null && sel_row != null) {
if (sel_row == selected_row) {
try {
selected_row.connection.delete (null);
} catch (Error e) {
warning (e.message);
var dialog = new Granite.MessageDialog (
_("Failed to remove VPN connection"),
"",
new ThemedIcon ("network-vpn"),
Gtk.ButtonsType.CLOSE
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = (Gtk.Window) get_toplevel ()
};
dialog.show_error_details (e.message);
dialog.present ();
dialog.response.connect (dialog.destroy);
}
} else {
warning ("Row selection changed between operations. Cancelling removal of VPN.");
GLib.Source.remove (timeout_id);
timeout_id = 0;
sel_row.show ();
private void delete_connection (VPNMenuItem item) {
if (sel_row != null && sel_row == item) {
try {
item.connection.delete (null);
} catch (Error e) {
warning (e.message);
var dialog = new Granite.MessageDialog (
_("Failed to remove VPN connection"),
"",
new ThemedIcon ("network-vpn"),
Gtk.ButtonsType.CLOSE
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = (Gtk.Window) get_toplevel ()
};
dialog.show_error_details (e.message);
dialog.present ();
dialog.response.connect (dialog.destroy);
}
} else {
warning ("Row selection changed between operations. Cancelling removal of VPN.");
GLib.Source.remove (timeout_id);
timeout_id = 0;
sel_row.show ();
}
}

Expand Down
35 changes: 32 additions & 3 deletions src/Widgets/VPN/VPNMenuItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

public class Network.VPNMenuItem : Gtk.ListBoxRow {
public signal void remove_request ();
public NM.RemoteConnection? connection { get; construct; }

public NM.DeviceState state { get; set; default = NM.DeviceState.DISCONNECTED; }
Expand Down Expand Up @@ -60,9 +61,15 @@ public class Network.VPNMenuItem : Gtk.ListBoxRow {
xalign = 0
};

var vpn_info_button = new Gtk.Button () {
image = new Gtk.Image.from_icon_name ("view-more-horizontal-symbolic", Gtk.IconSize.MENU),
var remove_button = new Gtk.Button.from_icon_name ("edit-delete-symbolic") {
margin_end = 3,
tooltip_text = _("Forget connection"),
valign = CENTER
};

var vpn_info_button = new Gtk.Button.from_icon_name ("view-more-horizontal-symbolic") {
margin_end = 3,
tooltip_text = _("Connection info"),
valign = Gtk.Align.CENTER
};
vpn_info_button.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
Expand All @@ -82,7 +89,8 @@ public class Network.VPNMenuItem : Gtk.ListBoxRow {
grid.attach (vpn_label, 1, 0);
grid.attach (state_label, 1, 1);
grid.attach (vpn_info_button, 2, 0, 1, 2);
grid.attach (connect_button, 3, 0, 1, 2);
grid.attach (remove_button, 3, 0, 1, 2);
grid.attach (connect_button, 4, 0, 1, 2);

add (grid);
show_all ();
Expand All @@ -92,6 +100,7 @@ public class Network.VPNMenuItem : Gtk.ListBoxRow {
update ();

connect_button.clicked.connect (() => activate ());
remove_button.clicked.connect (remove_row);

vpn_info_button.clicked.connect (() => {
var vpn_info_dialog = new Widgets.VPNInfoDialog (connection) {
Expand All @@ -104,6 +113,26 @@ public class Network.VPNMenuItem : Gtk.ListBoxRow {
});
}

private void remove_row () {
if (state == ACTIVATED || state == PREPARE) {
var dialog = new Granite.MessageDialog (
_("Failed to remove VPN connection"),
_("Cannot remove an active VPN connection."),
new ThemedIcon ("network-vpn"),
Gtk.ButtonsType.CLOSE
) {
badge_icon = new ThemedIcon ("dialog-error"),
modal = true,
transient_for = (Gtk.Window) get_toplevel ()
};
dialog.present ();
dialog.response.connect (dialog.destroy);
} else {
remove_request ();
hide ();
}
}

private void update () {
vpn_label.label = connection.get_id ();

Expand Down