feat #14740: settings, keyring, tray and manual recording
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ thiserror = "1.0"
|
||||
rubato = "0.15"
|
||||
flacenc = "0.5"
|
||||
hound = "3.5"
|
||||
keyring = { version = "3", features = ["windows-native", "sync-secret-service", "crypto-rust"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
//! PulseAudio on Linux). The [`Recorder`](crate::recorder::Recorder) only sees
|
||||
//! this trait, which keeps it testable with fake sources.
|
||||
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
|
||||
/// Format of the interleaved `f32` samples a [`CaptureSource`] delivers.
|
||||
@@ -28,14 +29,14 @@ pub enum OutputTarget {
|
||||
}
|
||||
|
||||
/// One audio endpoint as shown in the device picker.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub struct DeviceInfo {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
/// Available capture inputs (microphones) and outputs (render endpoints).
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
|
||||
pub struct Devices {
|
||||
pub inputs: Vec<DeviceInfo>,
|
||||
pub outputs: Vec<DeviceInfo>,
|
||||
|
||||
@@ -9,6 +9,8 @@ pub mod capture;
|
||||
pub mod model;
|
||||
pub mod recorder;
|
||||
pub mod retention;
|
||||
pub mod secrets;
|
||||
pub mod settings;
|
||||
pub mod store;
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
//! API keys, stored only in the OS keyring (Windows Credential Manager,
|
||||
//! Linux Secret Service) under the service `eu.euvino.callrecorder`.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
/// Keyring service name all keys are stored under.
|
||||
pub const KEYRING_SERVICE: &str = "eu.euvino.callrecorder";
|
||||
|
||||
/// The external services whose API keys the app keeps.
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Service {
|
||||
DeepScript,
|
||||
Anthropic,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Keyring user name of this service's entry.
|
||||
pub fn user(self) -> &'static str {
|
||||
match self {
|
||||
Service::DeepScript => "deepscript",
|
||||
Service::Anthropic => "anthropic",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SecretError {
|
||||
#[error("Schlüsselbund-Fehler: {0}")]
|
||||
Keyring(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, SecretError>;
|
||||
|
||||
pub trait SecretStore: Send + Sync {
|
||||
/// The stored key, or `None` if there is none (or the keyring cannot be
|
||||
/// read).
|
||||
fn get(&self, s: Service) -> Option<String>;
|
||||
fn set(&self, s: Service, v: &str) -> Result<()>;
|
||||
/// Removes the key; removing a key that does not exist is not an error.
|
||||
fn delete(&self, s: Service) -> Result<()>;
|
||||
}
|
||||
|
||||
/// The OS keyring.
|
||||
pub struct KeyringSecrets;
|
||||
|
||||
impl KeyringSecrets {
|
||||
fn entry(s: Service) -> Result<keyring::Entry> {
|
||||
keyring::Entry::new(KEYRING_SERVICE, s.user())
|
||||
.map_err(|e| SecretError::Keyring(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl SecretStore for KeyringSecrets {
|
||||
fn get(&self, s: Service) -> Option<String> {
|
||||
Self::entry(s).ok()?.get_password().ok()
|
||||
}
|
||||
|
||||
fn set(&self, s: Service, v: &str) -> Result<()> {
|
||||
Self::entry(s)?
|
||||
.set_password(v)
|
||||
.map_err(|e| SecretError::Keyring(e.to_string()))
|
||||
}
|
||||
|
||||
fn delete(&self, s: Service) -> Result<()> {
|
||||
match Self::entry(s)?.delete_credential() {
|
||||
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
|
||||
Err(e) => Err(SecretError::Keyring(e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In-memory secret store for tests.
|
||||
#[derive(Default)]
|
||||
pub struct MemorySecrets {
|
||||
keys: Mutex<HashMap<Service, String>>,
|
||||
}
|
||||
|
||||
impl SecretStore for MemorySecrets {
|
||||
fn get(&self, s: Service) -> Option<String> {
|
||||
self.keys.lock().unwrap().get(&s).cloned()
|
||||
}
|
||||
|
||||
fn set(&self, s: Service, v: &str) -> Result<()> {
|
||||
self.keys.lock().unwrap().insert(s, v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn delete(&self, s: Service) -> Result<()> {
|
||||
self.keys.lock().unwrap().remove(&s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn memory_secrets_roundtrip() {
|
||||
let secrets = MemorySecrets::default();
|
||||
assert_eq!(secrets.get(Service::DeepScript), None);
|
||||
|
||||
secrets.set(Service::DeepScript, "ds-key").unwrap();
|
||||
assert_eq!(secrets.get(Service::DeepScript).as_deref(), Some("ds-key"));
|
||||
assert_eq!(secrets.get(Service::Anthropic), None);
|
||||
|
||||
secrets.delete(Service::DeepScript).unwrap();
|
||||
assert_eq!(secrets.get(Service::DeepScript), None);
|
||||
secrets.delete(Service::DeepScript).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn service_serializes_as_keyring_user() {
|
||||
for s in [Service::DeepScript, Service::Anthropic] {
|
||||
let json = serde_json::to_string(&s).unwrap();
|
||||
assert_eq!(json, format!("\"{}\"", s.user()));
|
||||
assert_eq!(serde_json::from_str::<Service>(&json).unwrap(), s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
//! User settings, persisted as JSON in the app config directory.
|
||||
//!
|
||||
//! Secrets (API keys) are never part of the settings; they live in the OS
|
||||
//! keyring (see [`crate::secrets`]).
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Executables whose mic use counts as a call (matched case-insensitively).
|
||||
const DEFAULT_ALLOWLIST: [&str; 6] = [
|
||||
"ms-teams.exe",
|
||||
"teams.exe",
|
||||
"microsip.exe",
|
||||
"msedge.exe",
|
||||
"chrome.exe",
|
||||
"firefox.exe",
|
||||
];
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct Settings {
|
||||
pub allowlist: Vec<String>,
|
||||
pub mic_device: Option<String>,
|
||||
pub output_device: Option<String>,
|
||||
pub language: String,
|
||||
pub deepscript_model: String,
|
||||
pub retention_days: u32,
|
||||
pub autostart: bool,
|
||||
pub start_after_secs: u64,
|
||||
pub end_after_secs: u64,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
allowlist: DEFAULT_ALLOWLIST.iter().map(|s| s.to_string()).collect(),
|
||||
mic_device: None,
|
||||
output_device: None,
|
||||
language: "de".to_string(),
|
||||
deepscript_model: "premium".to_string(),
|
||||
retention_days: 7,
|
||||
autostart: true,
|
||||
start_after_secs: 3,
|
||||
end_after_secs: 10,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// Reads the settings file. A missing or unreadable file yields the
|
||||
/// defaults; fields missing from the file take their default value.
|
||||
pub fn load(path: &Path) -> Settings {
|
||||
fs::read_to_string(path)
|
||||
.ok()
|
||||
.and_then(|json| serde_json::from_str(&json).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Writes the settings file (creating its directory), replacing the old
|
||||
/// file atomically so a crash never leaves a half-written file behind.
|
||||
pub fn save(&self, path: &Path) -> io::Result<()> {
|
||||
if let Some(dir) = path.parent() {
|
||||
fs::create_dir_all(dir)?;
|
||||
}
|
||||
let json = serde_json::to_string_pretty(self).map_err(io::Error::other)?;
|
||||
let tmp = path.with_extension("json.tmp");
|
||||
fs::write(&tmp, json)?;
|
||||
fs::rename(&tmp, path)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn settings_default_values() {
|
||||
let s = Settings::default();
|
||||
assert_eq!(
|
||||
s.allowlist,
|
||||
vec![
|
||||
"ms-teams.exe",
|
||||
"teams.exe",
|
||||
"microsip.exe",
|
||||
"msedge.exe",
|
||||
"chrome.exe",
|
||||
"firefox.exe"
|
||||
]
|
||||
);
|
||||
assert_eq!(s.mic_device, None);
|
||||
assert_eq!(s.output_device, None);
|
||||
assert_eq!(s.language, "de");
|
||||
assert_eq!(s.deepscript_model, "premium");
|
||||
assert_eq!(s.retention_days, 7);
|
||||
assert!(s.autostart);
|
||||
assert_eq!(s.start_after_secs, 3);
|
||||
assert_eq!(s.end_after_secs, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_load_missing_file_returns_default() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
assert_eq!(
|
||||
Settings::load(&dir.path().join("settings.json")),
|
||||
Settings::default()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_load_corrupt_file_returns_default() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("settings.json");
|
||||
fs::write(&path, "{ not json").unwrap();
|
||||
assert_eq!(Settings::load(&path), Settings::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_load_partial_json_fills_defaults() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("settings.json");
|
||||
fs::write(&path, r#"{"retention_days": 30, "mic_device": "headset"}"#).unwrap();
|
||||
let s = Settings::load(&path);
|
||||
assert_eq!(s.retention_days, 30);
|
||||
assert_eq!(s.mic_device.as_deref(), Some("headset"));
|
||||
assert_eq!(
|
||||
s,
|
||||
Settings {
|
||||
retention_days: 30,
|
||||
mic_device: Some("headset".into()),
|
||||
..Settings::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_save_then_load_roundtrips() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("nested").join("settings.json");
|
||||
let s = Settings {
|
||||
allowlist: vec!["zoom.exe".into()],
|
||||
output_device: Some("speakers".into()),
|
||||
autostart: false,
|
||||
..Settings::default()
|
||||
};
|
||||
s.save(&path).unwrap();
|
||||
assert_eq!(Settings::load(&path), s);
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,17 @@ impl Store {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the track file paths, for callers that derive them from the
|
||||
/// call id and so can only know them after [`Store::create_call`].
|
||||
pub fn set_paths(&self, id: CallId, mic_path: &Path, out_path: &Path) -> Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE calls SET mic_path = ?1, out_path = ?2 WHERE id = ?3",
|
||||
params![path_to_str(mic_path)?, path_to_str(out_path)?, id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_status(&self, id: CallId, status: CallStatus, error: Option<&str>) -> Result<()> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
@@ -495,7 +506,7 @@ fn speaker_from_key(key: &str) -> Result<Speaker> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use chrono::Local;
|
||||
|
||||
@@ -744,6 +755,22 @@ mod tests {
|
||||
assert_eq!(recovered.ended_at, Some(original_ended_at));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_paths_updates_track_paths() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let id = store
|
||||
.create_call("Manuell", Local::now(), Path::new(""), Path::new(""))
|
||||
.unwrap();
|
||||
|
||||
store
|
||||
.set_paths(id, Path::new("/rec/1-mic.wav"), Path::new("/rec/1-out.wav"))
|
||||
.unwrap();
|
||||
|
||||
let call = store.get_call(id).unwrap().unwrap();
|
||||
assert_eq!(call.mic_path, PathBuf::from("/rec/1-mic.wav"));
|
||||
assert_eq!(call.out_path, PathBuf::from("/rec/1-out.wav"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_call_cascades() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user