Skip to content

Commit

Permalink
VPNPage: rewrite connection forget
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit committed Jan 18, 2024
1 parent d1c7926 commit b645302
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 69 deletions.
99 changes: 33 additions & 66 deletions src/Views/VPNPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,12 @@ 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
};

var edit_connection_button = new Gtk.Button.from_icon_name ("preferences-system-symbolic") {
tooltip_text = _("Edit VPN connection…"),
sensitive = false
};

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

var scrolled = new Gtk.ScrolledWindow (null, null) {
Expand Down Expand Up @@ -113,8 +107,6 @@ public class Network.VPNPage : Network.Widgets.Page {
try_connection_editor ("--edit=" + selected_row.connection.get_uuid ());
});

remove_button.clicked.connect (remove_button_cb);

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

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

Expand Down Expand Up @@ -206,6 +197,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 @@ -291,36 +291,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 @@ -346,34 +316,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
34 changes: 31 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 @@ -65,8 +66,13 @@ public class Network.VPNMenuItem : Gtk.ListBoxRow {
modal = true
};

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") {
tooltip_text = _("Forget connection…"),
margin_end = 3,
valign = CENTER
};

var vpn_info_button = new Gtk.Button.from_icon_name ("view-more-horizontal-symbolic") {
margin_end = 3,
valign = Gtk.Align.CENTER
};
Expand All @@ -87,7 +93,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 @@ -97,6 +104,7 @@ public class Network.VPNMenuItem : Gtk.ListBoxRow {
update ();

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

vpn_info_button.clicked.connect (() => {
vpn_info_dialog.transient_for = (Gtk.Window) get_toplevel ();
Expand All @@ -105,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

0 comments on commit b645302

Please sign in to comment.