From df324f0f8fc7046a33d101c2a68fc873c4c33fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Mon, 9 Dec 2024 19:49:14 -0800 Subject: [PATCH 1/3] InfoBox: add metered data switch --- src/Widgets/InfoBox.vala | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Widgets/InfoBox.vala b/src/Widgets/InfoBox.vala index 6990e12c..7ec5cbba 100644 --- a/src/Widgets/InfoBox.vala +++ b/src/Widgets/InfoBox.vala @@ -17,7 +17,9 @@ public class Network.Widgets.InfoBox : Gtk.Box { private Gtk.Label dns; private Gtk.Label sent; private Gtk.Label received; + private Gtk.Switch reduce_data_switch; private Granite.HeaderLabel ip6address_head; + private NM.Connection connection; public InfoBox.from_device (NM.Device device) { Object (device: device); @@ -77,6 +79,21 @@ public class Network.Widgets.InfoBox : Gtk.Box { xalign = 0 }; + reduce_data_switch = new Gtk.Switch () { + valign = CENTER + }; + + var reduce_data_header = new Granite.HeaderLabel (_("Reduce background data usage")) { + mnemonic_widget = reduce_data_switch, + secondary_text = _("While connected to this network, background tasks like automatic updates will be paused.") + }; + + var reduce_data_box = new Gtk.Box (HORIZONTAL, 12) { + margin_top = 24 + }; + reduce_data_box.append (reduce_data_header); + reduce_data_box.append (reduce_data_switch); + orientation = VERTICAL; append (ip4address_head); append (ip4address); @@ -89,13 +106,29 @@ public class Network.Widgets.InfoBox : Gtk.Box { append (dns_head); append (dns); append (send_receive_box); + append (reduce_data_box); + + connection = device.get_active_connection ().connection; + connection.changed.connect (update_settings); device.state_changed.connect (() => { update_status (); info_changed (); }); + update_settings (); update_status (); + + reduce_data_switch.notify["active"].connect (() => { + var setting_connection = connection.get_setting_connection (); + var metered = setting_connection.metered; + + if (reduce_data_switch.active && metered != YES && metered != GUESS_YES) { + setting_connection.set_property ("metered", NM.Metered.YES); + } else if (!reduce_data_switch.active && metered != NO && metered != GUESS_NO) { + setting_connection.set_property ("metered", NM.Metered.NO); + } + }); } public void update_activity (string sent_bytes, string received_bytes) { @@ -154,4 +187,10 @@ public class Network.Widgets.InfoBox : Gtk.Box { update_sidebar (owner); } } + + private void update_settings () { + var setting_connection = connection.get_setting_connection (); + + reduce_data_switch.active = setting_connection.metered == YES || setting_connection.metered == GUESS_YES; + } } From f1da267819a91df11c9e3b8c60da53ea169663c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Mon, 9 Dec 2024 19:56:01 -0800 Subject: [PATCH 2/3] clarity --- src/Widgets/InfoBox.vala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Widgets/InfoBox.vala b/src/Widgets/InfoBox.vala index 7ec5cbba..c7bc00fc 100644 --- a/src/Widgets/InfoBox.vala +++ b/src/Widgets/InfoBox.vala @@ -124,10 +124,12 @@ public class Network.Widgets.InfoBox : Gtk.Box { var metered = setting_connection.metered; if (reduce_data_switch.active && metered != YES && metered != GUESS_YES) { - setting_connection.set_property ("metered", NM.Metered.YES); + metered = YES; } else if (!reduce_data_switch.active && metered != NO && metered != GUESS_NO) { - setting_connection.set_property ("metered", NM.Metered.NO); + metered = NO; } + + setting_connection.set_property (NM.SettingConnection.METERED, metered); }); } From 6158155c08cc29cd32d9450e1784468674852218 Mon Sep 17 00:00:00 2001 From: Ryo Nakano Date: Wed, 11 Dec 2024 02:28:04 +0900 Subject: [PATCH 3/3] Fix writing changes to the metered settings (#415) Inspired from network-manager-applet: https://gitlab.gnome.org/GNOME/network-manager-applet/-/blob/a1675383bdfb2df4aea191101ad9bb0f93a6d161/src/connection-editor/nm-connection-editor.c?page=2#L1216 --- src/Widgets/InfoBox.vala | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Widgets/InfoBox.vala b/src/Widgets/InfoBox.vala index c7bc00fc..d38fdba3 100644 --- a/src/Widgets/InfoBox.vala +++ b/src/Widgets/InfoBox.vala @@ -19,7 +19,7 @@ public class Network.Widgets.InfoBox : Gtk.Box { private Gtk.Label received; private Gtk.Switch reduce_data_switch; private Granite.HeaderLabel ip6address_head; - private NM.Connection connection; + private NM.RemoteConnection connection; public InfoBox.from_device (NM.Device device) { Object (device: device); @@ -130,6 +130,23 @@ 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 (); + } }); }