Skip to content

Commit

Permalink
Validate mac address
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Sep 15, 2024
1 parent 1db1363 commit b513d4b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion resources/ui/add-device-dialog.blp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion resources/ui/add-device-dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
<child>
<object class="AdwEntryRow">
<property name="title" translatable="yes">_MAC address</property>
<property name="tooltip-text" translatable="yes">Te hardware address for this device</property>
<property name="tooltip-text" translatable="yes">The hardware address for this device</property>
<property name="input-hints">1024|2|16|2048</property>
<property name="use-underline">true</property>
<property name="text" bind-source="AddDeviceDialog" bind-property="mac_address" bind-flags="sync-create|bidirectional"/>
<child type="suffix">
<object class="ValidationIndicator">
<property name="is_valid" bind-source="AddDeviceDialog" bind-property="mac_address_valid" bind-flags="sync-create"/>
<property name="feedback" translatable="yes">This is no valid 48-bit MAC address.</property>
</object>
</child>
</object>
</child>
<child>
Expand Down
27 changes: 24 additions & 3 deletions src/widgets/add_device_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ mod imp {
#[property(get)]
pub label_valid: Cell<bool>,
#[property(get, set)]
pub mac_address: RefCell<String>,
#[property(get)]
pub mac_address_valid: Cell<bool>,
#[property(get, set)]
pub host: RefCell<String>,
#[property(get, default = "empty")]
pub host_indicator: RefCell<String>,
Expand All @@ -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) {
Expand All @@ -86,6 +101,7 @@ mod imp {

fn validate_all(&self) {
self.validate_label();
self.validate_mac_address();
self.validate_host();
}

Expand All @@ -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: (),
}
Expand All @@ -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();
});
Expand Down

0 comments on commit b513d4b

Please sign in to comment.