From 12fac8e8bda664904a2e5149ad69d19b23196715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Tue, 10 Dec 2024 12:01:14 -0800 Subject: [PATCH] InfoBox: add auto connect switch (#416) --- data/network.metainfo.xml.in | 1 + src/Widgets/InfoBox.vala | 65 +++++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/data/network.metainfo.xml.in b/data/network.metainfo.xml.in index 48267d2a..bff8e83f 100644 --- a/data/network.metainfo.xml.in +++ b/data/network.metainfo.xml.in @@ -37,6 +37,7 @@ Mark a network as metered + Disable Unsecured Network Auto Connect diff --git a/src/Widgets/InfoBox.vala b/src/Widgets/InfoBox.vala index 295ca07a..63fed940 100644 --- a/src/Widgets/InfoBox.vala +++ b/src/Widgets/InfoBox.vala @@ -17,6 +17,7 @@ public class Network.Widgets.InfoBox : Gtk.Box { private Gtk.Label dns; private Gtk.Label sent; private Gtk.Label received; + private Gtk.Switch auto_connect_switch; private Gtk.Switch reduce_data_switch; private Granite.HeaderLabel ip6address_head; private NM.RemoteConnection connection; @@ -95,6 +96,22 @@ public class Network.Widgets.InfoBox : Gtk.Box { reduce_data_box.append (reduce_data_header); reduce_data_box.append (reduce_data_switch); + auto_connect_switch = new Gtk.Switch () { + valign = CENTER + }; + + var auto_connect_header = new Granite.HeaderLabel (_("Automatically connect")) { + hexpand = true, + mnemonic_widget = auto_connect_switch, + valign = CENTER + }; + + var auto_connect_box = new Gtk.Box (HORIZONTAL, 12) { + margin_top = 12 + }; + auto_connect_box.append (auto_connect_header); + auto_connect_box.append (auto_connect_switch); + orientation = VERTICAL; append (ip4address_head); append (ip4address); @@ -108,6 +125,7 @@ public class Network.Widgets.InfoBox : Gtk.Box { append (dns); append (send_receive_box); append (reduce_data_box); + append (auto_connect_box); connection = device.get_active_connection ().connection; connection.changed.connect (update_settings); @@ -120,6 +138,16 @@ public class Network.Widgets.InfoBox : Gtk.Box { update_settings (); update_status (); + auto_connect_switch.notify["active"].connect (() => { + var setting_connection = connection.get_setting_connection (); + if (setting_connection.autoconnect == auto_connect_switch.active) { + return; + } + + setting_connection.set_property (NM.SettingConnection.AUTOCONNECT, auto_connect_switch.active); + commit_changes (); + }); + reduce_data_switch.notify["active"].connect (() => { var setting_connection = connection.get_setting_connection (); var metered = setting_connection.metered; @@ -132,22 +160,7 @@ public class Network.Widgets.InfoBox : Gtk.Box { setting_connection.set_property (NM.SettingConnection.METERED, metered); - try { - connection.commit_changes_async.begin (true, null); - } catch (Error e) { - var message_dialog = new Granite.MessageDialog.with_image_from_icon_name ( - _("Failed To Configure Settings"), - _("Unable to save changes to the disk"), - "network-error", - Gtk.ButtonsType.CLOSE - ) { - modal = true, - transient_for = (Gtk.Window) get_root () - }; - message_dialog.show_error_details (e.message); - message_dialog.response.connect (message_dialog.destroy); - message_dialog.present (); - } + commit_changes (); }); } @@ -208,9 +221,29 @@ public class Network.Widgets.InfoBox : Gtk.Box { } } + private void commit_changes () { + try { + connection.commit_changes_async.begin (true, null); + } catch (Error e) { + var message_dialog = new Granite.MessageDialog.with_image_from_icon_name ( + _("Failed To Configure Settings"), + _("Unable to save changes to the disk"), + "network-error", + Gtk.ButtonsType.CLOSE + ) { + modal = true, + transient_for = (Gtk.Window) get_root () + }; + message_dialog.show_error_details (e.message); + message_dialog.response.connect (message_dialog.destroy); + message_dialog.present (); + } + } + private void update_settings () { var setting_connection = connection.get_setting_connection (); + auto_connect_switch.active = setting_connection.autoconnect; reduce_data_switch.active = setting_connection.metered == YES || setting_connection.metered == GUESS_YES; } }