Skip to content

Commit

Permalink
InfoBox: add auto connect switch (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Dec 10, 2024
1 parent 096d829 commit 12fac8e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
1 change: 1 addition & 0 deletions data/network.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</description>
<issues>
<issue url="https://github.com/elementary/switchboard-plug-network/issues/165">Mark a network as metered</issue>
<issue url="https://github.com/elementary/switchboard-plug-network/issues/248">Disable Unsecured Network Auto Connect</issue>
</issues>
</release>

Expand Down
65 changes: 49 additions & 16 deletions src/Widgets/InfoBox.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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 ();
});
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 12fac8e

Please sign in to comment.