use super::profile;
use crate::os::registry::Key;
use windows::Win32::System::Registry::{KEY_READ, KEY_SET_VALUE};
const BASE: &str =
r"Software\Microsoft\Windows\CurrentVersion\CloudStore\Store\DefaultAccount\Current";
const VALUE: &str = "Data";
#[derive(Clone, Debug)]
pub struct Target {
pub path: String,
pub blob: Vec<u8>,
}
pub fn discover() -> Vec<Target> {
let mut out = Vec::new();
let Some(base) = Key::open_read(BASE) else {
return out;
};
for container in base.subkeys() {
if !container
.to_ascii_lowercase()
.contains("quiethourssettings")
{
continue;
}
let container_path = format!("{BASE}\\{container}");
let Some(ck) = Key::open_read(&container_path) else {
continue;
};
for child in ck.subkeys() {
let path = format!("{container_path}\\{child}");
if let Some(blob) = Key::open_read(&path).and_then(|k| k.get_binary(VALUE)) {
if profile::read(&blob).is_some() {
out.push(Target { path, blob });
}
}
}
}
out
}
#[allow(dead_code)]
pub fn current_profile() -> Option<String> {
let targets = discover();
let first = profile::read(&targets.first()?.blob)?;
targets
.iter()
.all(|t| profile::read(&t.blob).as_deref() == Some(first.as_str()))
.then_some(first)
}
pub fn write_blob(path: &str, blob: &[u8]) -> bool {
Key::open(path, KEY_READ | KEY_SET_VALUE)
.map(|k| k.set_binary(VALUE, blob))
.unwrap_or(false)
}