From 3395154916aeb5b0b5bcf4073f8e621881a5ed9c Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 24 May 2025 01:51:47 +0200 Subject: [PATCH] feat(user): add user info --- Cargo.lock | 31 +++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bluetooth.rs | 9 ++++----- src/main.rs | 11 ++++++++--- src/user_info.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/user_info.rs diff --git a/Cargo.lock b/Cargo.lock index 5a1f9c9..df5ad64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.98" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + [[package]] name = "cc" version = "1.2.19" @@ -17,6 +23,18 @@ dependencies = [ "shlex", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "dbus" version = "0.9.7" @@ -56,6 +74,18 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "pkg-config" version = "0.3.32" @@ -77,6 +107,7 @@ version = "0.1.0" dependencies = [ "anyhow", "dbus", + "nix", "serde", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 3f3573a..ecf0689 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,6 @@ publish = false [dependencies] anyhow = "1.0.98" dbus = { version = "0.9.7", features = ["vendored"] } +nix = { version = "0.30.1", features = ["user"] } serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.140" diff --git a/src/bluetooth.rs b/src/bluetooth.rs index 1a3c39d..d4a1b0f 100644 --- a/src/bluetooth.rs +++ b/src/bluetooth.rs @@ -1,9 +1,8 @@ -use anyhow::Result; use dbus::arg; use std::collections::HashMap; use std::time::Duration; -#[derive(Default, serde::Serialize)] +#[derive(serde::Serialize)] pub struct BluetoothInfo { pub name: String, pub address: String, @@ -32,8 +31,8 @@ impl BluetoothInfo { } pub fn get_all_bluetooth_devices( - connection: dbus::blocking::Connection, -) -> Result> { + connection: &dbus::blocking::Connection, +) -> Vec { let proxy = connection.with_proxy("org.bluez", "/", Duration::from_millis(5000)); let (result,): (HashMap, HashMap>,) = proxy .method_call( @@ -47,5 +46,5 @@ pub fn get_all_bluetooth_devices( .filter(|(_, value)| value.contains_key("org.bluez.Device1")) .flat_map(|(_, value)| BluetoothInfo::new(value)) .collect(); - Ok(result) + result } diff --git a/src/main.rs b/src/main.rs index 53c20e1..1919f2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,23 @@ use anyhow::Result; -use bluetooth::BluetoothInfo; use dbus::blocking::Connection; +use bluetooth::BluetoothInfo; +use user_info::UserInfo; + mod bluetooth; +mod user_info; #[derive(serde::Serialize)] struct SystemInfo { bluetooth: Vec, + user: UserInfo } fn main() -> Result<()> { let connection = Connection::new_system()?; - let bluetooth = bluetooth::get_all_bluetooth_devices(connection)?; - let info = SystemInfo { bluetooth }; + let bluetooth = bluetooth::get_all_bluetooth_devices(&connection); + let user = UserInfo::new(&connection).unwrap_or_default(); + let info = SystemInfo { bluetooth, user }; println!("{}", serde_json::to_string(&info)?); Ok(()) } diff --git a/src/user_info.rs b/src/user_info.rs new file mode 100644 index 0000000..91e3722 --- /dev/null +++ b/src/user_info.rs @@ -0,0 +1,28 @@ +use dbus::blocking; + +#[derive(serde::Serialize, Default)] +pub struct UserInfo { + pub real_name: String, + pub username: String, + pub icon: String, +} + +impl UserInfo { + pub fn new(connection: &blocking::Connection) -> anyhow::Result { + let uid = nix::unistd::Uid::current(); + let proxy = connection.with_proxy( + "org.freedesktop.Accounts", + format!("/org/freedesktop/Accounts/User{}", uid), + std::time::Duration::from_millis(5000), + ); + use blocking::stdintf::org_freedesktop_dbus::Properties; + let icon: String = proxy.get("org.freedesktop.Accounts.User", "IconFile")?; + let real_name: String = proxy.get("org.freedesktop.Accounts.User", "RealName")?; + let username: String = proxy.get("org.freedesktop.Accounts.User", "UserName")?; + Ok(Self { + real_name, + username, + icon, + }) + } +}