From a5024e0a3d4873e5ddafc38dc92cc501a3bdb2e1 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 10 Apr 2024 14:31:34 +0200 Subject: [PATCH] feat: add hostname1-zbus --- Cargo.toml | 1 + hostname1/Cargo.toml | 16 ++++ hostname1/examples/hostnamectl.rs | 67 ++++++++++++++++ hostname1/src/lib.rs | 125 ++++++++++++++++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 hostname1/Cargo.toml create mode 100644 hostname1/examples/hostnamectl.rs create mode 100644 hostname1/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index cfb1eb8..2a89b78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ resolver = "2" members = [ "cosmic-settings-daemon", "geoclue2", + "hostname1", "mpris2", "networkmanager", "switcheroo-control", diff --git a/hostname1/Cargo.toml b/hostname1/Cargo.toml new file mode 100644 index 0000000..08cc1ef --- /dev/null +++ b/hostname1/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "hostname1-zbus" +version = "0.1.0" +description = "dbus bindings for org.freedesktop.hostname1 with zbus" +repository = "https://github.com/pop-os/dbus-settings-bindings" +edition = "2021" +license = "MPL-2.0" +categories = ["os::linux-apis"] +keywords = ["dbus", "hostname", "hostname1", "zbus", "org.freedesktop.hostname1"] + +[dependencies] +zbus = "3.0.0" + +[dev-dependencies] +pico-args = "0.5.0" +tokio = { version = "1", features = ["full"] } \ No newline at end of file diff --git a/hostname1/examples/hostnamectl.rs b/hostname1/examples/hostnamectl.rs new file mode 100644 index 0000000..0e1b7a0 --- /dev/null +++ b/hostname1/examples/hostnamectl.rs @@ -0,0 +1,67 @@ +use std::process::ExitCode; + +#[tokio::main] +async fn main() -> Result> { + let connection = zbus::Connection::system().await?; + let proxy = hostname1_zbus::Hostname1Proxy::new(&connection).await?; + + let mut parser = pico_args::Arguments::from_env(); + + match parser.subcommand()?.as_deref() { + Some("hostname") => match parser.subcommand()?.as_deref() { + Some("set") => match parser.free_from_str::().ok().as_deref() { + Some(new_hostname) => { + if let Err(why) = proxy.set_hostname(&new_hostname, false).await { + eprintln!("error: could not set hostname: {why}"); + return Ok(ExitCode::FAILURE); + } + } + + None => { + eprintln!("error: hostname argument not set"); + return Ok(ExitCode::FAILURE); + } + }, + + _ => { + println!("{}", proxy.hostname().await?); + } + }, + + Some("static-hostname") => match parser.subcommand()?.as_deref() { + Some("set") => match parser.free_from_str::().ok().as_deref() { + Some(new_hostname) => { + if let Err(why) = proxy.set_static_hostname(&new_hostname, false).await { + eprintln!("error: could not set hostname: {why}"); + return Ok(ExitCode::FAILURE); + } + } + + None => { + eprintln!("error: hostname argument not set"); + return Ok(ExitCode::FAILURE); + } + }, + + _ => { + println!("{}", proxy.static_hostname().await?); + } + }, + + _ => print_help(), + } + + Ok(ExitCode::SUCCESS) +} + +fn print_help() { + println!( + "\ +hostnamectl + +USAGE: + hostnamectl hostname + hostnamectl hostname set NAME +" + ); +} diff --git a/hostname1/src/lib.rs b/hostname1/src/lib.rs new file mode 100644 index 0000000..42792fd --- /dev/null +++ b/hostname1/src/lib.rs @@ -0,0 +1,125 @@ +//! # DBus interface proxy for: `org.freedesktop.hostname1` +//! +//! This code was generated by `zbus-xmlgen` `3.1.1` from DBus introspection data. +//! Source: `Interface '/org/freedesktop/hostname1' from service 'org.freedesktop.hostname1' on system bus`. +//! +//! You may prefer to adapt it, instead of using it verbatim. +//! +//! More information can be found in the +//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html) +//! section of the zbus documentation. +//! +//! This DBus object implements +//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html), +//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used: +//! +//! * [`zbus::fdo::PeerProxy`] +//! * [`zbus::fdo::IntrospectableProxy`] +//! * [`zbus::fdo::PropertiesProxy`] +//! +//! …consequently `zbus-xmlgen` did not generate code for the above interfaces. + +use zbus::dbus_proxy; + +#[dbus_proxy( + interface = "org.freedesktop.hostname1", + default_service = "org.freedesktop.hostname1", + default_path = "/org/freedesktop/hostname1" +)] +trait Hostname1 { + /// Describe method + fn describe(&self) -> zbus::Result; + + /// GetProductUUID method + #[dbus_proxy(name = "GetProductUUID")] + fn get_product_uuid(&self, interactive: bool) -> zbus::Result>; + + /// SetChassis method + fn set_chassis(&self, chassis: &str, interactive: bool) -> zbus::Result<()>; + + /// SetDeployment method + fn set_deployment(&self, deployment: &str, interactive: bool) -> zbus::Result<()>; + + /// SetHostname method + fn set_hostname(&self, hostname: &str, interactive: bool) -> zbus::Result<()>; + + /// SetIconName method + fn set_icon_name(&self, icon: &str, interactive: bool) -> zbus::Result<()>; + + /// SetLocation method + fn set_location(&self, location: &str, interactive: bool) -> zbus::Result<()>; + + /// SetPrettyHostname method + fn set_pretty_hostname(&self, hostname: &str, interactive: bool) -> zbus::Result<()>; + + /// SetStaticHostname method + fn set_static_hostname(&self, hostname: &str, interactive: bool) -> zbus::Result<()>; + + /// Chassis property + #[dbus_proxy(property)] + fn chassis(&self) -> zbus::Result; + + /// DefaultHostname property + #[dbus_proxy(property)] + fn default_hostname(&self) -> zbus::Result; + + /// Deployment property + #[dbus_proxy(property)] + fn deployment(&self) -> zbus::Result; + + /// HardwareModel property + #[dbus_proxy(property)] + fn hardware_model(&self) -> zbus::Result; + + /// HardwareVendor property + #[dbus_proxy(property)] + fn hardware_vendor(&self) -> zbus::Result; + + /// HomeURL property + #[dbus_proxy(property, name = "HomeURL")] + fn home_url(&self) -> zbus::Result; + + /// Hostname property + #[dbus_proxy(property)] + fn hostname(&self) -> zbus::Result; + + /// HostnameSource property + #[dbus_proxy(property)] + fn hostname_source(&self) -> zbus::Result; + + /// IconName property + #[dbus_proxy(property)] + fn icon_name(&self) -> zbus::Result; + + /// KernelName property + #[dbus_proxy(property)] + fn kernel_name(&self) -> zbus::Result; + + /// KernelRelease property + #[dbus_proxy(property)] + fn kernel_release(&self) -> zbus::Result; + + /// KernelVersion property + #[dbus_proxy(property)] + fn kernel_version(&self) -> zbus::Result; + + /// Location property + #[dbus_proxy(property)] + fn location(&self) -> zbus::Result; + + /// OperatingSystemCPEName property + #[dbus_proxy(property, name = "OperatingSystemCPEName")] + fn operating_system_cpename(&self) -> zbus::Result; + + /// OperatingSystemPrettyName property + #[dbus_proxy(property)] + fn operating_system_pretty_name(&self) -> zbus::Result; + + /// PrettyHostname property + #[dbus_proxy(property)] + fn pretty_hostname(&self) -> zbus::Result; + + /// StaticHostname property + #[dbus_proxy(property)] + fn static_hostname(&self) -> zbus::Result; +}