Skip to content

Commit

Permalink
Pages set status (#398)
Browse files Browse the repository at this point in the history
* Page: set status_type

* get status from page

* Deviceitems can only be constructed from page

* fix VPN

* Fix hotspot

* simpler

---------

Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
  • Loading branch information
danirabbit and zeebok authored May 19, 2024
1 parent 16ea3e8 commit 26581d8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 80 deletions.
4 changes: 2 additions & 2 deletions src/MainView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public class Network.MainView : Gtk.Box {
virtual_header = new Granite.HeaderLabel (_("Virtual"));
devices_header = new Granite.HeaderLabel (_("Devices"));

var proxy = new Widgets.DeviceItem (_("Proxy"), "preferences-system-network") {
var proxy_page = new Widgets.ProxyPage ();
var proxy = new Widgets.DeviceItem.from_page (proxy_page) {
item_type = VIRTUAL
};
proxy.page = new Widgets.ProxyPage (proxy);

vpn_page = new VPNPage ();
var vpn = new Widgets.DeviceItem.from_page (vpn_page) {
Expand Down
9 changes: 0 additions & 9 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ namespace Network {
return true;
}

public enum CustomMode {
PROXY_NONE = 0,
PROXY_MANUAL,
PROXY_AUTO,
HOTSPOT_ENABLED,
HOTSPOT_DISABLED,
INVALID
}

public enum ItemType {
DEVICE = 0,
VIRTUAL,
Expand Down
4 changes: 4 additions & 0 deletions src/Views/HotspotPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,13 @@
var root_iface_is_hotspot = Utils.get_device_is_hotspot (root_iface.wifi_device);
if (root_iface_is_hotspot) {
state = NM.DeviceState.ACTIVATED;
status_type = SUCCESS;
} else {
state = NM.DeviceState.DISCONNECTED;
status_type = OFFLINE;
}

status = Utils.state_to_string (state);
}

protected override void update_switch () {
Expand Down
21 changes: 8 additions & 13 deletions src/Views/ProxyPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ namespace Network.Widgets {
public Gtk.Stack stack;
public signal void update_status_label (string mode);

public DeviceItem owner { get; construct; }

public ProxyPage (DeviceItem _owner) {
public ProxyPage () {
Object (
activatable: true,
title: _("Proxy"),
icon: new ThemedIcon ("preferences-system-network"),
owner: _owner
icon: new ThemedIcon ("preferences-system-network")
);

}
Expand Down Expand Up @@ -82,26 +80,23 @@ namespace Network.Widgets {
}

private void update_mode () {
var mode = Utils.CustomMode.INVALID;
switch (Network.Plug.proxy_settings.get_string ("mode")) {
case "none":
mode = Utils.CustomMode.PROXY_NONE;
status = _("Disabled");
status_switch.active = false;
status_type = OFFLINE;
break;
case "manual":
mode = Utils.CustomMode.PROXY_MANUAL;
status = _("Enabled (manual mode)");
status_switch.active = true;
status_type = SUCCESS;
break;
case "auto":
mode = Utils.CustomMode.PROXY_AUTO;
status = _("Enabled (auto mode)");
status_switch.active = true;
break;
default:
mode = Utils.CustomMode.INVALID;
status_type = SUCCESS;
break;
}

owner.switch_status (mode);
}
}
}
17 changes: 16 additions & 1 deletion src/Views/VPNPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,22 @@ public class Network.VPNPage : Network.Widgets.Page {
}
}

update_switch ();
switch (state) {
case ACTIVATED:
status_type = SUCCESS;
break;
case DISCONNECTED:
status_type = OFFLINE;
break;
case FAILED:
status_type = ERROR;
break;
default:
status_type = WARNING;
break;
}

status = Utils.state_to_string (state);
}

protected override void update_switch () {
Expand Down
81 changes: 26 additions & 55 deletions src/Widgets/DeviceItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@

namespace Network.Widgets {
public class DeviceItem : Gtk.ListBoxRow {
public Switchboard.SettingsPage.StatusType status_type {
set {
switch (value) {
case ERROR:
status_image.icon_name = "emblem-error";
break;
case OFFLINE:
status_image.icon_name = "emblem-disabled";
break;
case SUCCESS:
status_image.icon_name = "emblem-enabled";
break;
case WARNING:
status_image.icon_name = "emblem-warning";
break;
case NONE:
status_image.clear ();
break;
}
}
}

public NM.Device? device { get; construct; default = null; }
public Widgets.Page? page { get; set; default = null; }
public string title { get; set; default = ""; }
Expand All @@ -28,13 +50,6 @@ namespace Network.Widgets {

private Gtk.Image status_image;

public DeviceItem (string title, string icon_name) {
Object (
title: title,
icon: new ThemedIcon (icon_name)
);
}

public DeviceItem.from_page (Widgets.Page page, string icon_name = "network-wired") {
Object (
device: page.device,
Expand All @@ -45,11 +60,8 @@ namespace Network.Widgets {

page.bind_property ("title", this, "title", SYNC_CREATE);
page.bind_property ("icon", this, "icon", SYNC_CREATE);

switch_status (Utils.CustomMode.INVALID, page.state);
page.notify["state"].connect (() => {
switch_status (Utils.CustomMode.INVALID, page.state);
});
page.bind_property ("status-type", this, "status-type", SYNC_CREATE);
page.bind_property ("status", this, "subtitle", SYNC_CREATE);
}

construct {
Expand All @@ -70,8 +82,9 @@ namespace Network.Widgets {
halign = Gtk.Align.START,
valign = Gtk.Align.START
};
row_description.add_css_class (Granite.STYLE_CLASS_SMALL_LABEL);

status_image = new Gtk.Image.from_icon_name ("user-available") {
status_image = new Gtk.Image () {
halign = Gtk.Align.END,
valign = Gtk.Align.END
};
Expand All @@ -92,47 +105,5 @@ namespace Network.Widgets {
bind_property ("subtitle", row_description, "label");
bind_property ("icon", row_image, "gicon");
}

public void switch_status (Utils.CustomMode custom_mode, NM.DeviceState? state = null) {
if (state != null) {
switch (state) {
case NM.DeviceState.ACTIVATED:
status_image.icon_name = "user-available";
break;
case NM.DeviceState.DISCONNECTED:
status_image.icon_name = "user-offline";
break;
case NM.DeviceState.FAILED:
status_image.icon_name = "user-busy";
break;
default:
status_image.icon_name = "user-away";
break;
}

if (device is NM.DeviceWifi && state == NM.DeviceState.UNAVAILABLE) {
subtitle = _("Disabled");
} else {
subtitle = Utils.state_to_string (state);
}
} else if (custom_mode != Utils.CustomMode.INVALID) {
switch (custom_mode) {
case Utils.CustomMode.PROXY_NONE:
subtitle = _("Disabled");
status_image.icon_name = "user-offline";
break;
case Utils.CustomMode.PROXY_MANUAL:
subtitle = _("Enabled (manual mode)");
status_image.icon_name = "user-available";
break;
case Utils.CustomMode.PROXY_AUTO:
subtitle = _("Enabled (auto mode)");
status_image.icon_name = "user-available";
break;
}
}

subtitle = "<span font_size='small'>" + subtitle + "</span>";
}
}
}
26 changes: 26 additions & 0 deletions src/Widgets/Page.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,39 @@ namespace Network.Widgets {

get_uuid ();
device.state_changed.connect_after (() => {
update_status ();
get_uuid ();
});

update_status ();
}

show_end_title_buttons = true;
}

private void update_status () {
switch (device.state) {
case ACTIVATED:
status_type = SUCCESS;
break;
case DISCONNECTED:
status_type = OFFLINE;
break;
case FAILED:
status_type = ERROR;
break;
default:
status_type = WARNING;
break;
}

if (device is NM.DeviceWifi && state == UNAVAILABLE) {
status = _("Disabled");
} else {
status = Utils.state_to_string (device.state);
}
}

public virtual void update () {
if (info_box != null) {
string sent_bytes, received_bytes;
Expand Down

0 comments on commit 26581d8

Please sign in to comment.