diff --git a/crates/voelin-core/src/identity.rs b/crates/voelin-core/src/identity.rs index 4496c00..35721be 100644 --- a/crates/voelin-core/src/identity.rs +++ b/crates/voelin-core/src/identity.rs @@ -34,7 +34,7 @@ use voelin_gateway_proto::UniqueIds; use voelin_store::Store; /// Name given to an identity whose source kept none. -const DEFAULT_NICKNAME: &str = "imported"; +pub const DEFAULT_NICKNAME: &str = "imported"; /// Item field holding the item's type; the payload is in field /// [`PAYLOAD_FIELD_BASE`] `+ type`. diff --git a/crates/voelin-ui/src/app.rs b/crates/voelin-ui/src/app.rs index f7ae014..8c1b686 100644 --- a/crates/voelin-ui/src/app.rs +++ b/crates/voelin-ui/src/app.rs @@ -544,7 +544,7 @@ pub fn run(options: RunOptions) -> Result<()> { None => { // Creating an identity takes a moment (security level 8). let identity = tsclientlib::Identity::create(); - store.add_identity("Default", &identity)?; + store.add_identity(crate::servers::CREATED_IDENTITY, &identity)?; identity } }; @@ -794,6 +794,7 @@ impl App { } Event::ServerInfo { session, name, flavor, capabilities } => { self.sessions.entry(session as i64).or_default().capabilities = capabilities; + self.adopt_server_name(session as i64, &name); if self.current == Some(session as i64) { let kind = match flavor { voelin_model::ServerFlavor::Ts3(v) => format!("TeamSpeak 3 {v}"), diff --git a/crates/voelin-ui/src/dev.rs b/crates/voelin-ui/src/dev.rs index c99af1b..8f56942 100644 --- a/crates/voelin-ui/src/dev.rs +++ b/crates/voelin-ui/src/dev.rs @@ -12,7 +12,8 @@ //! - `VOELIN_DEMO_STREAM=1`: a local test stream in the viewer. //! - `VOELIN_OPEN=[,...]`: open screens on start: `home`, //! `server`, `settings[:
]` (voice, keybinds, streaming, privacy, -//! appearance, or 0-4), `about`, `share`, `bookmark` (add a server), +//! appearance, or 0-4), `about`, `share`, `bookmark` (add a server; +//! `bookmark:edit` the current one, Advanced open), //! `emoji` (the picker), `client` (the volume dialog of the first other //! client once connected), `panel` / `no-panel` (the members panel), //! `voice` (the voice channel view; on the phone its own screen), @@ -159,6 +160,12 @@ pub(crate) fn start(ui: &MainWindow, switches: &Switches) -> Running { with_app(|app| app.demo_share()); } } + // The current server's dialog, Advanced open. + "bookmark" if arg == "edit" => { + if let Some(id) = with_app(|app| app.current).flatten() { + nav.invoke_edit_server(id as i32); + } + } "bookmark" => nav.invoke_add_server(), "emoji" => nav.set_emoji_open(true), "panel" => nav.set_right_panel_open(true), diff --git a/crates/voelin-ui/src/servers.rs b/crates/voelin-ui/src/servers.rs index 8f512cc..7df7241 100644 --- a/crates/voelin-ui/src/servers.rs +++ b/crates/voelin-ui/src/servers.rs @@ -7,9 +7,12 @@ use voelin_core::identity::Found; use voelin_core::{Command, ObserveState, Source, VoiceOptions, VoiceState}; use voelin_store::{Bookmark, QueryConfig, QueryTransport}; -use crate::app::{App, BookmarkForm, Bridge}; +use crate::app::{App, BookmarkForm, Bridge, later}; use crate::vm; +/// The name of the identity the app creates when it has none. +pub(crate) const CREATED_IDENTITY: &str = "Default"; + /// What to tell the user about the identities imported from the official /// clients at start ([`voelin_core::identity::import_new`]), if any. /// `had_identity`: we had one before, so it stays the default. @@ -28,6 +31,59 @@ pub(crate) fn imported_status(imported: &[Found], had_identity: bool) -> Option< }) } +/// The bookmark the form describes, over `old` (what the form does not show, +/// like the identity, the default channel or the client version, stays; a +/// new server has none of it). Only the address is needed: the name is the +/// address until the server tells its own (`adopt_server_name`), the +/// nickname `default_nickname`, the gateway is looked up later. +fn bookmark_from_form( + old: Option<&Bookmark>, + form: &BookmarkForm, + default_nickname: impl FnOnce() -> String, +) -> Bookmark { + let mut bookmark = old.cloned().unwrap_or_default(); + bookmark.id = form.id as i64; + bookmark.address = form.address.trim().to_string(); + bookmark.name = match form.name.trim() { + "" => bookmark.address.clone(), + name => name.to_string(), + }; + bookmark.nickname = match form.nickname.trim() { + "" => default_nickname(), + nickname => nickname.to_string(), + }; + bookmark.gateway_url = Some(form.gateway_url.trim().to_string()).filter(|u| !u.is_empty()); + bookmark.query = match form.query_transport.as_str() { + "none" | "" => None, + t => { + let (host, port) = form + .query_addr + .rsplit_once(':') + .map_or((form.query_addr.to_string(), None), |(h, p)| { + (h.to_string(), p.parse().ok()) + }); + let transport = match t { + "raw" => QueryTransport::Raw, + "http" => QueryTransport::Http, + _ => QueryTransport::Ssh, + }; + let default_port = match transport { + QueryTransport::Raw => 10011, + QueryTransport::Ssh => 10022, + QueryTransport::Http => 10080, + }; + Some(QueryConfig { + transport, + host, + port: port.unwrap_or(default_port), + user: form.query_user.to_string(), + server_port: bookmark.query.as_ref().and_then(|q| q.server_port), + }) + } + }; + bookmark +} + impl App { pub(crate) fn connect_voice(&mut self) { let Some(b) = self.current.and_then(|id| self.bookmark(id)).cloned() else { return }; @@ -40,6 +96,75 @@ impl App { self.engine .send(Command::ConnectVoice { session: b.id as u64, options: Box::new(options) }); self.set_status(format!("Connecting to {}…", b.address)); + // The admin may have published the gateway since it was added. + self.discover_gateway(b.id); + } + + /// The nickname a new server gets: the default identity's (one imported + /// from TeamSpeak keeps the nickname used there), else the system user's. + pub(crate) fn default_nickname(&self) -> String { + self.store + .identities() + .ok() + .and_then(|identities| identities.into_iter().next()) + .map(|identity| identity.name) + .filter(|name| { + ![CREATED_IDENTITY, voelin_core::identity::DEFAULT_NICKNAME] + .contains(&name.as_str()) + }) + .or_else(|| std::env::var("USER").or_else(|_| std::env::var("USERNAME")).ok()) + .unwrap_or_default() + } + + /// Look up the gateway of a server that has none in DNS + /// ([`voelin_core::discover`]); one found is kept as if typed. + pub(crate) fn discover_gateway(&self, id: i64) { + let Some(b) = self.bookmark(id).filter(|b| b.gateway_url.is_none() && !self.demo_ui) else { + return; + }; + let address = b.address.clone(); + self.engine.runtime().spawn(async move { + if let Some(url) = voelin_core::discover::gateway(&address).await { + later(move |app| app.gateway_found(id, &address, url)); + } + }); + } + + /// A gateway was found for the server at `address`; it is kept unless + /// the server changed meanwhile. + fn gateway_found(&mut self, id: i64, address: &str, url: String) { + let Some(b) = self + .bookmarks + .iter_mut() + .find(|b| b.id == id && b.address == address && b.gateway_url.is_none()) + else { + return; + }; + b.gateway_url = Some(url.clone()); + let name = b.name.clone(); + if let Err(e) = self.store.update_bookmark(b) { + warn!(%e, "could not keep the gateway found"); + return; + } + self.set_status(format!("Found the gateway of {name}: {url}")); + self.refresh_toolbar(); + } + + /// The server told its name: a server still named by its address takes + /// it, so the address is all a user has to type. + pub(crate) fn adopt_server_name(&mut self, id: i64, name: &str) { + let Some(b) = self.bookmarks.iter_mut().find(|b| b.id == id && b.name == b.address) else { + return; + }; + if name.trim().is_empty() || self.demo_ui { + return; + } + b.name = name.trim().to_owned(); + if let Err(e) = self.store.update_bookmark(b) { + warn!(%e, "could not name the server"); + } + self.refresh_servers(); + self.refresh_toolbar(); } pub(crate) fn toggle_observe(&mut self) { @@ -87,7 +212,7 @@ impl App { let Some(b) = self.bookmark(id) else { return BookmarkForm { id: -1, - nickname: std::env::var("USER").unwrap_or_default().into(), + nickname: self.default_nickname().into(), query_transport: "none".into(), query_user: "serveradmin".into(), ..Default::default() @@ -110,7 +235,8 @@ impl App { }; BookmarkForm { id: b.id as i32, - name: b.name.clone().into(), + // Named by its address: empty, so a new address names it again. + name: if b.name == b.address { SharedString::new() } else { b.name.clone().into() }, address: b.address.clone().into(), nickname: b.nickname.clone().into(), server_password: secret(b.server_password_key()), @@ -123,49 +249,8 @@ impl App { } pub(crate) fn save_bookmark(&mut self, form: BookmarkForm) { - let query = match form.query_transport.as_str() { - "none" | "" => None, - t => { - let (host, port) = form - .query_addr - .rsplit_once(':') - .map_or((form.query_addr.to_string(), None), |(h, p)| { - (h.to_string(), p.parse().ok()) - }); - let transport = match t { - "raw" => QueryTransport::Raw, - "http" => QueryTransport::Http, - _ => QueryTransport::Ssh, - }; - let default_port = match transport { - QueryTransport::Raw => 10011, - QueryTransport::Ssh => 10022, - QueryTransport::Http => 10080, - }; - Some(QueryConfig { - transport, - host, - port: port.unwrap_or(default_port), - user: form.query_user.to_string(), - server_port: None, - }) - } - }; - let mut bookmark = Bookmark { - id: form.id as i64, - name: if form.name.is_empty() { - form.address.to_string() - } else { - form.name.to_string() - }, - address: form.address.to_string(), - nickname: form.nickname.to_string(), - identity: None, - default_channel: None, - gateway_url: Some(form.gateway_url.to_string()).filter(|u| !u.is_empty()), - query, - client_version: None, - }; + let mut bookmark = + bookmark_from_form(self.bookmark(form.id as i64), &form, || self.default_nickname()); let result = if bookmark.id < 0 { self.store.add_bookmark(&bookmark).map(|id| bookmark.id = id) } else { @@ -191,6 +276,7 @@ impl App { self.current = Some(bookmark.id); self.bookmarks = self.store.bookmarks().unwrap_or_default(); self.refresh_all(); + self.discover_gateway(bookmark.id); } pub(crate) fn delete_bookmark(&mut self, id: i64) { @@ -379,3 +465,83 @@ impl App { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_address_alone_makes_a_server() { + let form = BookmarkForm { + id: -1, + address: " ts.example.test ".into(), + query_transport: "none".into(), + ..Default::default() + }; + let b = bookmark_from_form(None, &form, || "Nick".into()); + assert_eq!((b.address.as_str(), b.name.as_str()), ("ts.example.test", "ts.example.test")); + assert_eq!(b.nickname, "Nick"); + assert_eq!( + (b.gateway_url, b.query, b.identity, b.default_channel), + (None, None, None, None) + ); + } + + #[test] + fn editing_keeps_what_the_form_does_not_show() { + let old = Bookmark { + id: 7, + name: "Old".into(), + address: "old.example.test".into(), + nickname: "Me".into(), + identity: Some(2), + default_channel: Some("Lobby/Sub".into()), + gateway_url: Some("ws://gw.example.test:7788/v1".into()), + query: Some(QueryConfig { server_port: Some(9988), ..Default::default() }), + client_version: Some("linux".into()), + }; + let form = BookmarkForm { + id: 7, + name: "Mine".into(), + address: "new.example.test:9988".into(), + nickname: "Other".into(), + // Cleared: looked up again. + gateway_url: "".into(), + query_transport: "raw".into(), + query_addr: "q.example.test".into(), + query_user: "serveradmin".into(), + ..Default::default() + }; + let b = bookmark_from_form(Some(&old), &form, || unreachable!("a nickname was given")); + assert_eq!((b.id, b.name.as_str(), b.nickname.as_str()), (7, "Mine", "Other")); + assert_eq!(b.address, "new.example.test:9988"); + assert_eq!((b.identity, b.default_channel), (Some(2), Some("Lobby/Sub".into()))); + assert_eq!(b.client_version.as_deref(), Some("linux")); + assert_eq!(b.gateway_url, None); + let q = b.query.unwrap(); + assert_eq!( + (q.transport, q.host.as_str(), q.port), + (QueryTransport::Raw, "q.example.test", 10011) + ); + assert_eq!(q.server_port, Some(9988)); + } + + #[test] + fn the_import_is_told_by_nickname() { + let found = |nickname: &str| Found { + nickname: nickname.into(), + identity: tsclientlib::Identity::create(), + source: "settings.db".into(), + selected: false, + }; + assert_eq!(imported_status(&[], false), None); + assert_eq!( + imported_status(&[found("Main")], false).unwrap(), + "Using your TeamSpeak identity \u{201c}Main\u{201d}" + ); + assert_eq!( + imported_status(&[found("Main"), found("Alt")], true).unwrap(), + "Imported 2 identities from TeamSpeak: Main, Alt" + ); + } +} diff --git a/crates/voelin-ui/ui/screens/dialogs.slint b/crates/voelin-ui/ui/screens/dialogs.slint index 99cb8cf..e39e63f 100644 --- a/crates/voelin-ui/ui/screens/dialogs.slint +++ b/crates/voelin-ui/ui/screens/dialogs.slint @@ -15,74 +15,103 @@ component Footer inherits HorizontalLayout { spacing: Theme.space-3; } +// Add or edit a server: its address and, if it has one, its password; the +// rest under Advanced (name, nickname, gateway, ServerQuery), which Voelin +// fills in itself when left empty. export component BookmarkDialog inherits Modal { in-out property form; callback done(); + // The fields beyond address and password: shown when editing. + property advanced: form.id >= 0; title: form.id < 0 ? @tr("Add server") : @tr("Edit server"); - subtitle: @tr("TeamSpeak 3 or 6 server address and your nickname there."); + subtitle: @tr("Only the address is needed; Voelin finds the rest."); icon: Icons.server; card-width: 560px; - card-height: 640px; + // Fits the address and password (two lines more on a phone). + card-height: advanced ? 720px : Nav.narrow ? 360px : 330px; dismissed => { done(); } ScrollView { vertical-stretch: 1; VerticalLayout { + alignment: start; spacing: Theme.space-4; padding-right: Theme.space-3; Field { - label: @tr("Name"); - TextField { text <=> form.name; placeholder: @tr("My server"); label: @tr("Name"); } + label: @tr("Address"); + hint: @tr("Host name, IP address or server nickname; a port only if the server needs one."); + TextField { text <=> form.address; placeholder: "ts.example.org"; label: @tr("Address"); icon: Icons.globe; } } Field { - label: @tr("Address"); - TextField { text <=> form.address; placeholder: "ts.example.org:9987"; label: @tr("Address"); icon: Icons.globe; } + label: @tr("Server password (optional)"); + TextField { text <=> form.server-password; input-type: InputType.password; label: @tr("Server password"); icon: Icons.lock; } } HorizontalLayout { + spacing: Theme.space-3; + Button { + kind: ButtonKind.ghost; + small: true; + icon: root.advanced ? Icons.chevron-down : Icons.chevron-right; + text: @tr("Advanced"); + clicked => { root.advanced = !root.advanced; } + } + Text { + text: @tr("Name, nickname, gateway, ServerQuery"); + color: Theme.text-muted; + font-size: Theme.font-sm; + vertical-alignment: center; + overflow: elide; + horizontal-stretch: 1; + } + } + if root.advanced: VerticalLayout { spacing: Theme.space-4; + Field { + label: @tr("Name"); + hint: @tr("Empty: the address, then the server's own name once connected."); + TextField { text <=> form.name; placeholder: @tr("The server's name"); label: @tr("Name"); } + } Field { label: @tr("Nickname"); + hint: @tr("Empty: the nickname of your identity."); TextField { text <=> form.nickname; label: @tr("Nickname"); icon: Icons.user; } } - Field { - label: @tr("Server password"); - TextField { text <=> form.server-password; input-type: InputType.password; label: @tr("Server password"); icon: Icons.lock; } + Text { + text: @tr("Invisible presence and chat without joining"); + font-weight: Theme.weight-semibold; + color: Theme.text; + wrap: word-wrap; } - } - Text { - text: @tr("Invisible presence and chat without joining (optional)"); - font-weight: Theme.weight-semibold; - color: Theme.text; - wrap: word-wrap; - } - Field { - label: @tr("Gateway URL"); - TextField { text <=> form.gateway-url; placeholder: "wss://gw.example.org/v1"; label: @tr("Gateway URL"); } - } - HorizontalLayout { - spacing: Theme.space-4; Field { - label: @tr("Query transport"); - Select { - model: ["none", "ssh", "raw", "http"]; - current-value <=> form.query-transport; - current-index: form.query-transport == "ssh" ? 1 : form.query-transport == "raw" ? 2 : form.query-transport == "http" ? 3 : 0; + label: @tr("Gateway URL"); + hint: @tr("Empty: looked up in DNS, as published by the server's admin."); + TextField { text <=> form.gateway-url; placeholder: @tr("Found automatically"); label: @tr("Gateway URL"); } + } + HorizontalLayout { + spacing: Theme.space-4; + Field { label: @tr("Query transport"); + Select { + model: ["none", "ssh", "raw", "http"]; + current-value <=> form.query-transport; + current-index: form.query-transport == "ssh" ? 1 : form.query-transport == "raw" ? 2 : form.query-transport == "http" ? 3 : 0; + label: @tr("Query transport"); + } + } + Field { + label: @tr("Query address"); + TextField { text <=> form.query-addr; placeholder: "ts.example.org:10022"; label: @tr("Query address"); } } } - Field { - label: @tr("Query address"); - TextField { text <=> form.query-addr; placeholder: "ts.example.org:10022"; label: @tr("Query address"); } - } - } - HorizontalLayout { - spacing: Theme.space-4; - Field { - label: @tr("Query login"); - TextField { text <=> form.query-user; label: @tr("Query login"); } - } - Field { - label: @tr("Query password"); - TextField { text <=> form.query-password; input-type: InputType.password; label: @tr("Query password"); } + HorizontalLayout { + spacing: Theme.space-4; + Field { + label: @tr("Query login"); + TextField { text <=> form.query-user; label: @tr("Query login"); } + } + Field { + label: @tr("Query password"); + TextField { text <=> form.query-password; input-type: InputType.password; label: @tr("Query password"); } + } } } } @@ -104,8 +133,8 @@ export component BookmarkDialog inherits Modal { } Button { kind: ButtonKind.primary; - text: @tr("Save"); - enabled: form.address != "" && form.nickname != ""; + text: form.id < 0 ? @tr("Add") : @tr("Save"); + enabled: form.address != ""; clicked => { Bridge.save-bookmark(form); done(); diff --git a/docs/screenshots/desktop-add-server.png b/docs/screenshots/desktop-add-server.png index 3bfae99..69b4019 100644 Binary files a/docs/screenshots/desktop-add-server.png and b/docs/screenshots/desktop-add-server.png differ diff --git a/docs/screenshots/mobile-add-server.png b/docs/screenshots/mobile-add-server.png new file mode 100644 index 0000000..e8b5132 Binary files /dev/null and b/docs/screenshots/mobile-add-server.png differ diff --git a/docs/ui.md b/docs/ui.md index b2976f9..77e6784 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -200,6 +200,22 @@ goes to the running capture at once (`Capture::follow_audio`, which calls to add sources to. With sample data `share:live` shares the test pattern with the sample studio's sources, live at once with sample viewers. +## Adding a server + +The add-server dialog (`BookmarkDialog` in `screens/dialogs.slint`, logic in +`servers.rs`) asks for the address (host, host:port, IP or server +nickname; the resolver finds the port through SRV records and TSDNS) and +an optional server password. *Advanced* (open when editing) has the rest, +and empty fields there are filled in: the name is the address until the +first connection, then the server's own name (`adopt_server_name`); the +nickname is the default identity's (one imported from TeamSpeak keeps its +nickname there), else the system user's; the identity is the default one, +there is no default channel; the gateway is looked up in DNS when the +server is saved without one and when it connects +([gateway-admin.md](gateway-admin.md#letting-voelin-find-the-gateway)), +and one found is stored as if typed. `bookmark` and `bookmark:edit` open +it (below). + ## Adding a screen 1. Write the screen in `ui/screens/.slint` from the components @@ -262,7 +278,7 @@ Environment variables (see `src/dev.rs`): - `VOELIN_DEMO_UI=1`: sample servers, channels, members, chat with emoji and a stream, without a server (nothing is stored). - `VOELIN_OPEN=[,...]`: `home`, `server`, `settings[:voice|keybinds|streaming|privacy|appearance]`, - `about`, `share[:live]`, `bookmark`, `emoji`, `client`, `panel`, `no-panel`, + `about`, `share[:live]`, `bookmark[:edit]`, `emoji`, `client`, `panel`, `no-panel`, `voice`, `pins`, `topics`, `topic:`, `member`, `watch`, `popout` (the server page, above), `tab:` (phone layout). With sample data, `watch` plays the local test pattern in the @@ -322,6 +338,7 @@ the window's size so the pointer is not over the window, and passes |---|---|---|---|---| | ![chat](screenshots/mobile-chat.png) | ![servers](screenshots/mobile-servers.png) | ![home](screenshots/mobile-home.png) | ![you](screenshots/mobile-you.png) | ![settings](screenshots/mobile-settings.png) | | ![voice channel](screenshots/mobile-voice.png) | ![activity](screenshots/mobile-activity.png) | ![studio](screenshots/mobile-studio.png) | ![share](screenshots/mobile-share.png) | ![sharing](screenshots/mobile-share-live.png) | +| ![add server](screenshots/mobile-add-server.png) | | | | | ## Limits