The GPUI shell repeated the Slint crate's connect, observe, gateway lookup, save and remove of a server, its default nickname, its FallbackSecrets and its settings opening and key registration. They now live once and both front ends call them: - voelin_ui_vm::servers::sequence: connect_voice(_to), observe, discover_gateway, gateway_found, select_server, save_bookmark, save_and_connect, delete_bookmark, bookmark_form and default_nickname, as transitions over the state, the Cx and a Backing (the store, the secrets, the identity a connection uses). The commands, the status line and what to keep go out through the Update as for every other transition; a new field, Update::discover, asks the front end to look a gateway up (the thread hop stays with each front end: Later in GPUI, later() in Slint). stream_peer is the one input a front end gives (Slint's codecs; GPUI passes none). - voelin_ui_vm::settings::open_settings and register_keys; a front end adds its own key (ui.window, studio.ui) through a function it passes, so Slint's settings page still lists what it listed. - voelin_store::FallbackSecrets (feature keyring) and Store::identity_for. - voelin-ui: the methods of servers/bind.rs, open_settings and default_secrets call them, so their call sites are unchanged; host.rs gets transition_with and the discover arm of perform. voelin-gpui: AppState's methods and the Backend call them. What cannot move is named in docs/ui.md. voelin-ui-vm now depends on tracing (the sequences log as the copies did): one line of Cargo.lock, THIRD_PARTY_NOTICES.md unchanged (scripts/notices.sh --check clean), cargo deny check clean. The sequences have ten tests over a fake front end (commands, status, store, secrets, gateways), open_settings and Store::identity_for one each; the GPUI and Slint suites pass unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
262 lines
10 KiB
Rust
262 lines
10 KiB
Rust
//! What the window keeps besides the engine and the state, and opens at start
|
|
//! as the Slint front end's `run()` does (`crates/voelin-ui/src/app.rs`): the
|
|
//! client's database (the bookmarks, the identities, the chats' read marks),
|
|
//! the settings service on the same file, the secrets, and the identity a
|
|
//! connection is made with. The directory is the platform's data directory
|
|
//! (`voelin_platform::paths::data_dir`, which `VOELIN_DATA_DIR` overrides, so
|
|
//! that the two front ends share the store); everything below [`Backend::open`]
|
|
//! takes the directory, so that no test reaches the user's own.
|
|
//!
|
|
//! Left out against the Slint front end: importing the identities of the
|
|
//! official TeamSpeak clients at start (it reads the user's own files; the
|
|
//! slice with the identities page brings it), the crash reports and the
|
|
//! configuration of `--set` on the command line.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use anyhow::{Context as _, Result};
|
|
use tracing::warn;
|
|
use tsclientlib::Identity;
|
|
use voelin_core::settings::{AUDIO, Settings};
|
|
use voelin_core::{AudioSettings, Command, Engine, History, IdentityBlob};
|
|
use voelin_store::{Bookmark, IdentityOrigin, MemorySecrets, Secrets, Store};
|
|
use voelin_ui_vm::servers::logic::CREATED_IDENTITY;
|
|
use voelin_ui_vm::settings::{CLIENT_PLAYBACK, ClientPlaybackMap, UI, UI_WINDOW, UiSettings};
|
|
|
|
/// The client's database, its settings and its secrets.
|
|
pub struct Backend {
|
|
/// The bookmarks, the identities and the chats' read marks.
|
|
pub store: Store,
|
|
/// The settings service, on the same file as the store (its own
|
|
/// connection); shared with the engine once [`Backend::attach`] ran.
|
|
pub prefs: Settings,
|
|
/// The servers' passwords.
|
|
pub secrets: Box<dyn Secrets>,
|
|
/// The identity of a server whose bookmark names none that exists, and
|
|
/// the store has no default for.
|
|
identity: Identity,
|
|
/// The database file, for the engine's chat history.
|
|
database: Option<PathBuf>,
|
|
}
|
|
|
|
/// What the store and the settings hold at start, for the state.
|
|
#[derive(Debug)]
|
|
pub struct Loaded {
|
|
/// The saved servers, in the rail's order.
|
|
pub bookmarks: Vec<Bookmark>,
|
|
/// The UI's own settings (the `ui` key).
|
|
pub settings: UiSettings,
|
|
/// The audio settings.
|
|
pub audio: AudioSettings,
|
|
/// The clients' local volumes.
|
|
pub playback: ClientPlaybackMap,
|
|
}
|
|
|
|
/// The data directory of this process: `VOELIN_DATA_DIR`, else the platform's.
|
|
pub fn data_dir() -> PathBuf {
|
|
voelin_platform::paths::data_dir()
|
|
}
|
|
|
|
/// The settings service on the client database in `dir`
|
|
/// ([`voelin_ui_vm::settings::open_settings`], which the Slint front end opens
|
|
/// the same way): the UI's keys and this front end's own, then a config file
|
|
/// (`VOELIN_CONFIG`, else `<dir>/config.toml` if it exists) and
|
|
/// `VOELIN_SETTING_*` variables below the stored values. Without the database
|
|
/// the settings live in memory.
|
|
fn open_settings(dir: &Path) -> Settings {
|
|
voelin_ui_vm::settings::open_settings(dir, &[], register_own_keys)
|
|
}
|
|
|
|
/// The key that only this front end uses: where the window was
|
|
/// (`ui.window`).
|
|
fn register_own_keys(prefs: &Settings) {
|
|
prefs.register(&UI_WINDOW);
|
|
}
|
|
|
|
impl Backend {
|
|
/// Open the database in `dir` (created if need be), its settings and the
|
|
/// default identity (made, and kept, when the store has none). `secrets`
|
|
/// are where passwords go.
|
|
pub fn open(dir: &Path, secrets: Box<dyn Secrets>) -> Result<Self> {
|
|
let database = dir.join("client.db");
|
|
let store = Store::open(&database).context("failed to open the client database")?;
|
|
let prefs = open_settings(dir);
|
|
let identity = match store.default_identity()? {
|
|
Some(entry) => store.identity(entry.id)?,
|
|
None => {
|
|
let identity = Identity::create();
|
|
store.add_identity(CREATED_IDENTITY, &identity, IdentityOrigin::Created)?;
|
|
identity
|
|
}
|
|
};
|
|
Ok(Self { store, prefs, secrets, identity, database: Some(database) })
|
|
}
|
|
|
|
/// A backend that stores nothing and keeps its secrets in memory, with a
|
|
/// fresh identity: the tests', and the sample data's.
|
|
pub fn in_memory() -> Result<Self> {
|
|
let prefs = Settings::in_memory();
|
|
voelin_ui_vm::settings::register_keys(&prefs);
|
|
register_own_keys(&prefs);
|
|
Ok(Self {
|
|
store: Store::open_in_memory()?,
|
|
prefs,
|
|
secrets: Box::new(MemorySecrets::default()),
|
|
identity: Identity::create(),
|
|
database: None,
|
|
})
|
|
}
|
|
|
|
/// What the store and the settings hold now.
|
|
pub fn load(&self) -> Loaded {
|
|
Loaded {
|
|
bookmarks: self.store.bookmarks().unwrap_or_else(|e| {
|
|
warn!(%e, "the bookmarks cannot be read");
|
|
Vec::new()
|
|
}),
|
|
settings: (*self.prefs.get_arc(&UI)).clone(),
|
|
audio: self.prefs.get(&AUDIO),
|
|
playback: self.prefs.get(&CLIENT_PLAYBACK),
|
|
}
|
|
}
|
|
|
|
/// Hand the engine what it keeps for the window: the chat history in the
|
|
/// same database (without it the engine keeps chats for this run only),
|
|
/// the settings service and the audio settings.
|
|
pub fn attach(&self, engine: &Engine) {
|
|
if let Some(database) = &self.database {
|
|
match History::open(database) {
|
|
Ok(history) => engine.send(Command::AttachHistory(history)),
|
|
Err(e) => warn!(%e, "chat history is not stored this time"),
|
|
}
|
|
}
|
|
engine.send(Command::AttachSettings(self.prefs.clone()));
|
|
engine.send(Command::SetAudioSettings(Box::new(self.prefs.get(&AUDIO))));
|
|
}
|
|
|
|
/// The identity a connection to the bookmark `bookmark` uses: its own if
|
|
/// it names one the store has, else the store's default, else the one made
|
|
/// at start ([`Store::identity_for`]).
|
|
pub fn identity_for(&self, bookmark: Option<&Bookmark>) -> Identity {
|
|
self.store.identity_for(bookmark.and_then(|b| b.identity), &self.identity)
|
|
}
|
|
|
|
/// The identity of [`Backend::identity_for`] as the engine takes it, for a
|
|
/// bookmark that names the identity `named`.
|
|
pub fn identity_blob(&self, named: Option<i64>) -> IdentityBlob {
|
|
voelin_core::ts::identity::to_blob(&self.store.identity_for(named, &self.identity))
|
|
}
|
|
|
|
/// Write what the settings service holds to the database.
|
|
pub fn flush(&self) {
|
|
if let Err(e) = self.prefs.flush() {
|
|
warn!(%e, "could not store settings");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The secrets of this run: the OS keyring, and memory for what the keyring
|
|
/// refuses (no Secret Service on a headless Linux); the sample data's never
|
|
/// reach the keyring.
|
|
pub fn default_secrets(demo: bool) -> Box<dyn Secrets> {
|
|
if demo {
|
|
Box::new(MemorySecrets::default())
|
|
} else {
|
|
Box::new(voelin_store::FallbackSecrets::new("voelin"))
|
|
}
|
|
}
|
|
|
|
/// What the tests of this crate need to open a store of their own.
|
|
#[cfg(test)]
|
|
pub(crate) mod testing {
|
|
use std::path::PathBuf;
|
|
|
|
/// A directory of its own under the temporary one (`TMPDIR`), removed with
|
|
/// the guard: no test reaches the user's data directory.
|
|
pub(crate) struct TempDir(pub PathBuf);
|
|
|
|
impl TempDir {
|
|
/// An empty directory path named `name` and this process.
|
|
pub(crate) fn new(name: &str) -> Self {
|
|
let dir =
|
|
std::env::temp_dir().join(format!("voelin-gpui-{name}-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
Self(dir)
|
|
}
|
|
}
|
|
|
|
impl Drop for TempDir {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use voelin_ui_vm::settings::WindowState;
|
|
|
|
use super::testing::TempDir;
|
|
use super::*;
|
|
|
|
fn bookmark(name: &str, address: &str) -> Bookmark {
|
|
Bookmark { name: name.into(), address: address.into(), ..Default::default() }
|
|
}
|
|
|
|
/// What a run leaves in the directory is what the next one starts from: the
|
|
/// bookmarks, the identity (the same one, not a new one) and the settings.
|
|
#[test]
|
|
fn a_second_start_finds_what_the_first_kept() {
|
|
let dir = TempDir::new("backend");
|
|
let first = Backend::open(&dir.0, Box::new(MemorySecrets::default())).unwrap();
|
|
assert_eq!(first.load().bookmarks, Vec::new(), "a new store has no servers");
|
|
let id = first.store.add_bookmark(&bookmark("Nightfall", "ts.example")).unwrap();
|
|
let uid = first.identity_for(None).key().to_pub().get_uid();
|
|
let window = WindowState { width: 1000, height: 700, x: None, y: None, maximized: false };
|
|
first.prefs.set(&UI_WINDOW, Some(window)).unwrap();
|
|
first.flush();
|
|
drop(first);
|
|
|
|
let second = Backend::open(&dir.0, Box::new(MemorySecrets::default())).unwrap();
|
|
let loaded = second.load();
|
|
assert_eq!(
|
|
loaded.bookmarks.iter().map(|b| (b.id, b.name.as_str())).collect::<Vec<_>>(),
|
|
[(id, "Nightfall")]
|
|
);
|
|
assert_eq!(second.identity_for(None).key().to_pub().get_uid(), uid, "the identity is kept");
|
|
assert_eq!(second.prefs.get(&UI_WINDOW), Some(window));
|
|
assert!(dir.0.join("client.db").is_file(), "the database is in the directory given");
|
|
}
|
|
|
|
/// A new server's nickname is the default identity's (one imported from
|
|
/// TeamSpeak keeps the nickname used there), not the name the app gave the
|
|
/// identity it made.
|
|
#[test]
|
|
fn a_new_servers_nickname_is_the_default_identitys_unless_the_app_named_it() {
|
|
use voelin_ui_vm::servers::sequence::default_nickname;
|
|
let backend = Backend::in_memory().unwrap();
|
|
let made = Identity::create();
|
|
backend.store.add_identity(CREATED_IDENTITY, &made, IdentityOrigin::Created).unwrap();
|
|
let nickname = default_nickname(&backend.store);
|
|
assert!(nickname != CREATED_IDENTITY && !nickname.trim().is_empty(), "{nickname}");
|
|
let imported =
|
|
backend.store.add_identity("Nova", &Identity::create(), IdentityOrigin::Imported);
|
|
backend.store.set_default_identity(imported.unwrap(), false).unwrap();
|
|
assert_eq!(default_nickname(&backend.store), "Nova");
|
|
}
|
|
|
|
#[test]
|
|
fn a_server_names_its_identity_and_the_default_serves_the_rest() {
|
|
let backend = Backend::in_memory().unwrap();
|
|
let (default, alt) = (Identity::create(), Identity::create());
|
|
let uid = |identity: &Identity| identity.key().to_pub().get_uid();
|
|
backend.store.add_identity("Default", &default, IdentityOrigin::Created).unwrap();
|
|
let alt_id = backend.store.add_identity("Alt", &alt, IdentityOrigin::Created).unwrap();
|
|
assert_eq!(uid(&backend.identity_for(None)), uid(&default), "the first is the default");
|
|
let named = Bookmark { identity: Some(alt_id), ..bookmark("a", "a.test") };
|
|
assert_eq!(uid(&backend.identity_for(Some(&named))), uid(&alt));
|
|
let gone = Bookmark { identity: Some(alt_id + 100), ..bookmark("b", "b.test") };
|
|
assert_eq!(uid(&backend.identity_for(Some(&gone))), uid(&default));
|
|
}
|
|
}
|