Skip to content

Commit e2b6911

Browse files
authored
InfoBox: add metered data switch (#414)
1 parent b08b145 commit e2b6911

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/Widgets/InfoBox.vala

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public class Network.Widgets.InfoBox : Gtk.Box {
1717
private Gtk.Label dns;
1818
private Gtk.Label sent;
1919
private Gtk.Label received;
20+
private Gtk.Switch reduce_data_switch;
2021
private Granite.HeaderLabel ip6address_head;
22+
private NM.RemoteConnection connection;
2123

2224
public InfoBox.from_device (NM.Device device) {
2325
Object (device: device);
@@ -77,6 +79,21 @@ public class Network.Widgets.InfoBox : Gtk.Box {
7779
xalign = 0
7880
};
7981

82+
reduce_data_switch = new Gtk.Switch () {
83+
valign = CENTER
84+
};
85+
86+
var reduce_data_header = new Granite.HeaderLabel (_("Reduce background data usage")) {
87+
mnemonic_widget = reduce_data_switch,
88+
secondary_text = _("While connected to this network, background tasks like automatic updates will be paused.")
89+
};
90+
91+
var reduce_data_box = new Gtk.Box (HORIZONTAL, 12) {
92+
margin_top = 24
93+
};
94+
reduce_data_box.append (reduce_data_header);
95+
reduce_data_box.append (reduce_data_switch);
96+
8097
orientation = VERTICAL;
8198
append (ip4address_head);
8299
append (ip4address);
@@ -89,13 +106,48 @@ public class Network.Widgets.InfoBox : Gtk.Box {
89106
append (dns_head);
90107
append (dns);
91108
append (send_receive_box);
109+
append (reduce_data_box);
110+
111+
connection = device.get_active_connection ().connection;
112+
connection.changed.connect (update_settings);
92113

93114
device.state_changed.connect (() => {
94115
update_status ();
95116
info_changed ();
96117
});
97118

119+
update_settings ();
98120
update_status ();
121+
122+
reduce_data_switch.notify["active"].connect (() => {
123+
var setting_connection = connection.get_setting_connection ();
124+
var metered = setting_connection.metered;
125+
126+
if (reduce_data_switch.active && metered != YES && metered != GUESS_YES) {
127+
metered = YES;
128+
} else if (!reduce_data_switch.active && metered != NO && metered != GUESS_NO) {
129+
metered = NO;
130+
}
131+
132+
setting_connection.set_property (NM.SettingConnection.METERED, metered);
133+
134+
try {
135+
connection.commit_changes_async.begin (true, null);
136+
} catch (Error e) {
137+
var message_dialog = new Granite.MessageDialog.with_image_from_icon_name (
138+
_("Failed To Configure Settings"),
139+
_("Unable to save changes to the disk"),
140+
"network-error",
141+
Gtk.ButtonsType.CLOSE
142+
) {
143+
modal = true,
144+
transient_for = (Gtk.Window) get_root ()
145+
};
146+
message_dialog.show_error_details (e.message);
147+
message_dialog.response.connect (message_dialog.destroy);
148+
message_dialog.present ();
149+
}
150+
});
99151
}
100152

101153
public void update_activity (string sent_bytes, string received_bytes) {
@@ -154,4 +206,10 @@ public class Network.Widgets.InfoBox : Gtk.Box {
154206
update_sidebar (owner);
155207
}
156208
}
209+
210+
private void update_settings () {
211+
var setting_connection = connection.get_setting_connection ();
212+
213+
reduce_data_switch.active = setting_connection.metered == YES || setting_connection.metered == GUESS_YES;
214+
}
157215
}

0 commit comments

Comments
 (0)