Added --remove ability to profile subcommand

This commit is contained in:
Tony Dwire
2021-03-19 22:24:59 -05:00
parent 53ee6015d0
commit 1c48ab227d
4 changed files with 42 additions and 2 deletions

View File

@@ -366,6 +366,7 @@ fn handle_profile(
if !cmd.next
&& !cmd.create
&& !cmd.list
&& cmd.remove.is_none()
&& cmd.curve.is_none()
&& cmd.max_percentage.is_none()
&& cmd.min_percentage.is_none()
@@ -392,11 +393,14 @@ fn handle_profile(
}
std::process::exit(1);
}
if cmd.next {
dbus.proxies().profile().next_fan()?;
} else if cmd.list {
let profile_names = dbus.proxies().profile().profile_names()?;
println!("Available profiles are {}", profile_names);
} else if let Some(profile) = &cmd.remove {
dbus.proxies().profile().remove(profile)?
} else {
dbus.proxies()
.profile()

View File

@@ -11,7 +11,7 @@ use std::io::Write;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
use zbus::dbus_interface;
use zbus::{dbus_interface, fdo::Error};
static FAN_TYPE_1_PATH: &str = "/sys/devices/platform/asus-nb-wmi/throttle_thermal_policy";
static FAN_TYPE_2_PATH: &str = "/sys/devices/platform/asus-nb-wmi/fan_boost_mode";
@@ -146,9 +146,35 @@ impl DbusFanAndCpu {
"Failed".to_string()
}
fn remove(&self, profile: &str) -> zbus::fdo::Result<()> {
if let Ok(ctrl) = self.inner.try_lock() {
if let Ok(mut cfg) = ctrl.config.try_lock() {
cfg.read();
if !cfg.power_profiles.contains_key(profile) {
return Err(Error::Failed("Invalid profile specified".to_string()));
}
if cfg.power_profiles.keys().len() == 1 {
return Err(Error::Failed("Cannot delete the last profile".to_string()));
}
if cfg.active_profile == *profile {
return Err(Error::Failed("Cannot delete the active profile".to_string()));
}
cfg.power_profiles.remove(profile);
cfg.write();
return Ok(());
}
}
return Err(Error::Failed("Failed to lock configuration".to_string()));
}
#[dbus_interface(signal)]
fn notify_profile(&self, profile: &str) -> zbus::Result<()> {}
}
impl crate::ZbusAdd for DbusFanAndCpu {

View File

@@ -44,6 +44,9 @@ trait Daemon {
/// ProfileNames method
fn profile_names(&self) -> zbus::Result<String>;
/// Remove method
fn remove(&self, profile: &str) -> zbus::Result<()>;
/// SetProfile method
fn set_profile(&self, profile: &str) -> zbus::Result<()>;
@@ -90,6 +93,11 @@ impl<'a> ProfileProxy<'a> {
self.0.profile_names()
}
#[inline]
pub fn remove(&self, profile: &str) -> Result<()> {
self.0.remove(profile)
}
#[inline]
pub fn connect_notify_profile(
&self,

View File

@@ -100,4 +100,6 @@ pub struct ProfileCommand {
pub curve: Option<Curve>,
#[options(free)]
pub profile: Option<String>,
#[options(help = "remove a profile by name")]
pub remove: Option<String>,
}