Slice 2, first half: the sidebar's placeholder becomes the selected server's channel tree from voelin-ui-vm's rows (tree_view): nested channels with their spacers, icons, banners, lock and counts, and the people in them with their avatar, badges and marks. A TreeModel entity builds the rows once for both panes on the tree, toolbar and member-card signals and tells what differs (list::diff, the row operations the Slint front end applies to its VecModel); ChannelTree is a cached pane over a virtual list that applies them, so a person who joins is a row inserted and the scroll stays. The rows carry no `talking`: a talker redraws the members panel, not the tree (docs/ui.md says so). A click selects a channel, a double-click joins it (sequence::join_channel: the move, the prompt for a locked channel's password, a connect into it without voice), the chevron folds it, a right-click offers Join, fold and Edit channel (logged: the channel editor is another slice), a click on a person is logged (their volume dialog is another slice). The password prompt (Update::password) is a card over the window, asking again when the server refuses the password. The folds and the selection are kept per server in ui.tree and found again by the next run. The shell's cached panes lay out their root at the size the place gives them (size_full), which the main area needed. Tests: the sample server's rows, folding and its settings, the round trip through a closed run, double-click and action join, the password prompt and its refusal, a move (the exact row operations) and a mute (one row), a talker leaves the tree alone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
265 lines
10 KiB
Rust
265 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_TREE, 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 keys that only this front end uses: where the window was
|
|
/// (`ui.window`) and what the channel trees remember (`ui.tree`).
|
|
fn register_own_keys(prefs: &Settings) {
|
|
prefs.register(&UI_WINDOW);
|
|
prefs.register(&UI_TREE);
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|