feat(user): add user info

This commit is contained in:
Lucien Cartier-Tilet 2025-05-24 01:51:47 +02:00
parent 93b3bb4830
commit 3395154916
Signed by: phundrak
SSH Key Fingerprint: SHA256:CE0HPsbW3L2YiJETx1zYZ2muMptaAqTN2g3498KrMkc
5 changed files with 72 additions and 8 deletions

31
Cargo.lock generated
View File

@ -8,6 +8,12 @@ version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.19" version = "1.2.19"
@ -17,6 +23,18 @@ dependencies = [
"shlex", "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]] [[package]]
name = "dbus" name = "dbus"
version = "0.9.7" version = "0.9.7"
@ -56,6 +74,18 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 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]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.32" version = "0.3.32"
@ -77,6 +107,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"dbus", "dbus",
"nix",
"serde", "serde",
"serde_json", "serde_json",
] ]

View File

@ -10,5 +10,6 @@ publish = false
[dependencies] [dependencies]
anyhow = "1.0.98" anyhow = "1.0.98"
dbus = { version = "0.9.7", features = ["vendored"] } dbus = { version = "0.9.7", features = ["vendored"] }
nix = { version = "0.30.1", features = ["user"] }
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140" serde_json = "1.0.140"

View File

@ -1,9 +1,8 @@
use anyhow::Result;
use dbus::arg; use dbus::arg;
use std::collections::HashMap; use std::collections::HashMap;
use std::time::Duration; use std::time::Duration;
#[derive(Default, serde::Serialize)] #[derive(serde::Serialize)]
pub struct BluetoothInfo { pub struct BluetoothInfo {
pub name: String, pub name: String,
pub address: String, pub address: String,
@ -32,8 +31,8 @@ impl BluetoothInfo {
} }
pub fn get_all_bluetooth_devices( pub fn get_all_bluetooth_devices(
connection: dbus::blocking::Connection, connection: &dbus::blocking::Connection,
) -> Result<Vec<BluetoothInfo>> { ) -> Vec<BluetoothInfo> {
let proxy = connection.with_proxy("org.bluez", "/", Duration::from_millis(5000)); let proxy = connection.with_proxy("org.bluez", "/", Duration::from_millis(5000));
let (result,): (HashMap<dbus::Path<'static>, HashMap<String, arg::PropMap>>,) = proxy let (result,): (HashMap<dbus::Path<'static>, HashMap<String, arg::PropMap>>,) = proxy
.method_call( .method_call(
@ -47,5 +46,5 @@ pub fn get_all_bluetooth_devices(
.filter(|(_, value)| value.contains_key("org.bluez.Device1")) .filter(|(_, value)| value.contains_key("org.bluez.Device1"))
.flat_map(|(_, value)| BluetoothInfo::new(value)) .flat_map(|(_, value)| BluetoothInfo::new(value))
.collect(); .collect();
Ok(result) result
} }

View File

@ -1,18 +1,23 @@
use anyhow::Result; use anyhow::Result;
use bluetooth::BluetoothInfo;
use dbus::blocking::Connection; use dbus::blocking::Connection;
use bluetooth::BluetoothInfo;
use user_info::UserInfo;
mod bluetooth; mod bluetooth;
mod user_info;
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct SystemInfo { struct SystemInfo {
bluetooth: Vec<BluetoothInfo>, bluetooth: Vec<BluetoothInfo>,
user: UserInfo
} }
fn main() -> Result<()> { fn main() -> Result<()> {
let connection = Connection::new_system()?; let connection = Connection::new_system()?;
let bluetooth = bluetooth::get_all_bluetooth_devices(connection)?; let bluetooth = bluetooth::get_all_bluetooth_devices(&connection);
let info = SystemInfo { bluetooth }; let user = UserInfo::new(&connection).unwrap_or_default();
let info = SystemInfo { bluetooth, user };
println!("{}", serde_json::to_string(&info)?); println!("{}", serde_json::to_string(&info)?);
Ok(()) Ok(())
} }

28
src/user_info.rs Normal file
View File

@ -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<Self> {
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,
})
}
}