From b513d4b66e51ca15645829daf5c8f4a4f08158b8 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Sun, 15 Sep 2024 19:50:05 +0200 Subject: [PATCH] Validate mac address --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- resources/ui/add-device-dialog.blp | 9 ++++++++- resources/ui/add-device-dialog.ui | 9 ++++++++- src/widgets/add_device_dialog.rs | 27 ++++++++++++++++++++++++--- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9347958..041f73d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -448,6 +448,12 @@ version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +[[package]] +name = "macaddr" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baee0bbc17ce759db233beb01648088061bf678383130602a298e6998eedb2d8" + [[package]] name = "memchr" version = "2.7.4" @@ -675,6 +681,7 @@ dependencies = [ "glob", "gtk4", "libadwaita", + "macaddr", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b562951..49bd35f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,9 @@ publish = false build = "build.rs" [dependencies] -gtk = { package = "gtk4", version = "0.9.1", features = ["gnome_46"] } adw = { package = "libadwaita", version = "0.7.0", features = ["v1_5"] } +gtk = { package = "gtk4", version = "0.9.1", features = ["gnome_46"] } +macaddr = { version = "1.0.1", default-features = false } [build-dependencies] glib-build-tools = "0.20.0" diff --git a/resources/ui/add-device-dialog.blp b/resources/ui/add-device-dialog.blp index f7cb022..79ac8b1 100644 --- a/resources/ui/add-device-dialog.blp +++ b/resources/ui/add-device-dialog.blp @@ -53,9 +53,16 @@ template $AddDeviceDialog: Adw.Dialog { Adw.EntryRow { title: _("_MAC address"); - tooltip-text: _("Te hardware address for this device"); + tooltip-text: _("The hardware address for this device"); input-hints: no_emoji | no_spellcheck | uppercase_chars | private; use-underline: true; + text: bind template.mac_address bidirectional; + + [suffix] + $ValidationIndicator { + is_valid: bind template.mac_address_valid; + feedback: _("This is no valid 48-bit MAC address."); + } } Adw.EntryRow { diff --git a/resources/ui/add-device-dialog.ui b/resources/ui/add-device-dialog.ui index 9a92465..f90c61b 100644 --- a/resources/ui/add-device-dialog.ui +++ b/resources/ui/add-device-dialog.ui @@ -57,9 +57,16 @@ corresponding .blp file and regenerate this file with blueprint-compiler. _MAC address - Te hardware address for this device + The hardware address for this device 1024|2|16|2048 true + + + + + This is no valid 48-bit MAC address. + + diff --git a/src/widgets/add_device_dialog.rs b/src/widgets/add_device_dialog.rs index e97b07b..6cae42c 100644 --- a/src/widgets/add_device_dialog.rs +++ b/src/widgets/add_device_dialog.rs @@ -49,6 +49,10 @@ mod imp { #[property(get)] pub label_valid: Cell, #[property(get, set)] + pub mac_address: RefCell, + #[property(get)] + pub mac_address_valid: Cell, + #[property(get, set)] pub host: RefCell, #[property(get, default = "empty")] pub host_indicator: RefCell, @@ -67,6 +71,17 @@ mod imp { self.obj().notify_is_valid(); } + fn is_mac_address_valid(&self) -> bool { + let text = self.mac_address.borrow(); + !text.is_empty() && macaddr::MacAddr::from_str(&text).is_ok() + } + + fn validate_mac_address(&self) { + self.mac_address_valid.set(self.is_mac_address_valid()); + self.obj().notify_mac_address_valid(); + self.obj().notify_is_valid(); + } + fn validate_host(&self) { let host = self.host.borrow(); let indicator = match IpAddr::from_str(&host) { @@ -86,6 +101,7 @@ mod imp { fn validate_all(&self) { self.validate_label(); + self.validate_mac_address(); self.validate_host(); } @@ -104,9 +120,11 @@ mod imp { fn new() -> Self { Self { - label: RefCell::new(String::new()), - label_valid: Cell::new(false), - host: RefCell::new(String::new()), + label: Default::default(), + label_valid: Default::default(), + mac_address: Default::default(), + mac_address_valid: Default::default(), + host: Default::default(), host_indicator: RefCell::new("empty".to_string()), is_valid: (), } @@ -131,6 +149,9 @@ mod imp { self.obj().connect_label_notify(|dialog| { dialog.imp().validate_label(); }); + self.obj().connect_mac_address_notify(|dialog| { + dialog.imp().validate_mac_address(); + }); self.obj().connect_host_notify(|dialog| { dialog.imp().validate_host(); });