From 077569dd5dac317ed8f4ac83a1155be140f32992 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sat, 16 Mar 2024 01:51:16 +0800 Subject: [PATCH] better option save --- libs/hbb_common/src/config.rs | 65 ++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs index f227c95a0..252fe194e 100644 --- a/libs/hbb_common/src/config.rs +++ b/libs/hbb_common/src/config.rs @@ -903,7 +903,13 @@ impl Config { res } - pub fn set_options(v: HashMap) { + #[inline] + fn purify_options(v: &mut HashMap) { + v.retain(|k, v| is_option_can_save(&OVERWRITE_SETTINGS, &DEFAULT_SETTINGS, k, v)); + } + + pub fn set_options(mut v: HashMap) { + Self::purify_options(&mut v); let mut config = CONFIG2.write().unwrap(); if config.options == v { return; @@ -923,6 +929,9 @@ impl Config { } pub fn set_option(k: String, v: String) { + if !is_option_can_save(&OVERWRITE_SETTINGS, &DEFAULT_SETTINGS, &k, &v) { + return; + } let mut config = CONFIG2.write().unwrap(); let v2 = if v.is_empty() { None } else { Some(&v) }; if v2 != config.options.get(&k) { @@ -1384,6 +1393,9 @@ impl LocalConfig { } pub fn set_option(k: String, v: String) { + if !is_option_can_save(&OVERWRITE_LOCAL_SETTINGS, &DEFAULT_LOCAL_SETTINGS, &k, &v) { + return; + } let mut config = LOCAL_CONFIG.write().unwrap(); let v2 = if v.is_empty() { None } else { Some(&v) }; if v2 != config.options.get(&k) { @@ -1560,6 +1572,14 @@ impl UserDefaultConfig { } pub fn set(&mut self, key: String, value: String) { + if !is_option_can_save( + &OVERWRITE_DISPLAY_SETTINGS, + &DEFAULT_DISPLAY_SETTINGS, + &key, + &value, + ) { + return; + } if value.is_empty() { self.options.remove(&key); } else { @@ -1853,6 +1873,22 @@ fn get_or( .cloned() } +#[inline] +fn is_option_can_save( + overwrite: &RwLock>, + defaults: &RwLock>, + k: &str, + v: &str, +) -> bool { + if overwrite.read().unwrap().contains_key(k) { + return false; + } + if defaults.read().unwrap().get(k).map_or(false, |x| x == v) { + return false; + } + true +} + #[inline] pub fn is_incoming_only() -> bool { HARD_SETTINGS @@ -1947,6 +1983,33 @@ mod tests { .write() .unwrap() .insert("d".to_string(), "c".to_string()); + let mut res: HashMap = Default::default(); + res.insert("b".to_owned(), "c".to_string()); + res.insert("d".to_owned(), "c".to_string()); + res.insert("c".to_owned(), "a".to_string()); + Config::purify_options(&mut res); + assert!(res.len() == 0); + res.insert("b".to_owned(), "c".to_string()); + res.insert("d".to_owned(), "c".to_string()); + res.insert("c".to_owned(), "a".to_string()); + res.insert("f".to_owned(), "a".to_string()); + Config::purify_options(&mut res); + assert!(res.len() == 1); + res.insert("b".to_owned(), "c".to_string()); + res.insert("d".to_owned(), "c".to_string()); + res.insert("c".to_owned(), "a".to_string()); + res.insert("f".to_owned(), "a".to_string()); + res.insert("c".to_owned(), "d".to_string()); + Config::purify_options(&mut res); + assert!(res.len() == 2); + res.insert("b".to_owned(), "c".to_string()); + res.insert("d".to_owned(), "c".to_string()); + res.insert("c".to_owned(), "a".to_string()); + res.insert("f".to_owned(), "a".to_string()); + res.insert("c".to_owned(), "d".to_string()); + res.insert("d".to_owned(), "cc".to_string()); + Config::purify_options(&mut res); + assert!(res.len() == 2); let res = Config::get_options(); assert!(res["a"] == "b"); assert!(res["c"] == "a");