Split the settings pages: logic into voelin-ui-vm, the Bridge writes stay in bind.rs
settings_pages.rs had 52 Bridge writes interleaved with what the pages
show and change. The decisions now live in
voelin_ui_vm::settings_pages::logic and work on the settings service, the
AppState and the plain bridge rows:
- the forms read from the settings (video, streaming, notifications,
privacy, advanced), the layer and audio-source rows and the filtered list
of all settings;
- the settings written from the forms (apply_video, apply_streaming,
layer, audio source, SRTP profile, notification, privacy and advanced
edits), each returning the messages for the status line instead of
writing them;
- the Integrations rows (gateways, configuration keys, permission rules,
encoders and decoders with their status texts) and the Profiles rows
(identities, bookmark pickers, what an import found, the account form);
- the pages' own state (PagesState, Improving), the layer, preset and
codec helpers, allowed_index/of and origin_label, with the tests that
went with them and tests for each of the above.
voelin-ui keeps settings_pages/{mod,bind}.rs: Pages (PagesState plus the
camera preview and the audio-app watcher), the Bridge and model writes, and
what a change sets going (camera, engine, identity store, threads). A
message the logic returns is logged and shown as before; the one visible
difference is that the "A layer needs a size" hint is logged with it.
serde_json becomes a regular dependency of voelin-ui-vm (the JSON typed into
the Advanced list); the lock file is unchanged.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ac4cee0a8f
commit
03f4a92161
10 files changed
+2208
-1508
No files matched your search
@@ -20,6 +20,8 @@ chrono = { version = "0.4.45", default-features = false, features = ["clock"] }
|
||||
# front end's toolkit used to bring them.
|
||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "webp", "bmp", "ico"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
# JSON values of the settings and the gateway's configuration keys.
|
||||
serde_json = "1"
|
||||
# The Twemoji archive's blocks (raw deflate).
|
||||
miniz_oxide = "0.8"
|
||||
# Grapheme clusters for emoji in chat text.
|
||||
@@ -32,7 +34,6 @@ voelin-store = { path = "../voelin-store" }
|
||||
|
||||
[dev-dependencies]
|
||||
png = "0.18"
|
||||
serde_json = "1"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -14,6 +14,7 @@ pub mod image;
|
||||
pub mod list;
|
||||
pub mod servers;
|
||||
pub mod settings;
|
||||
pub mod settings_pages;
|
||||
pub mod social;
|
||||
pub mod state;
|
||||
pub mod streams;
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
||||
//! The settings pages added with the new design: My Account, Profiles
|
||||
//! (identities), Voice & Video, Streaming, Devices, Notifications, Privacy &
|
||||
//! Safety, Integrations (gateways, gateway administration, encoders) and
|
||||
//! Advanced. [`logic`] has what the pages show and what their controls
|
||||
//! change; the front ends put the rows on screen.
|
||||
|
||||
pub mod logic;
|
||||
@@ -1,4 +1,4 @@
|
||||
//! The settings pages of the new design (settings_pages.rs).
|
||||
//! The settings pages of the new design (`settings_pages/`).
|
||||
|
||||
use slint::ComponentHandle;
|
||||
|
||||
|
||||
@@ -2,23 +2,13 @@
|
||||
//! `Store::default_identity`), chosen under Settings → Profiles, and the
|
||||
//! import of the official clients' identities on start
|
||||
//! (`voelin_core::identity::import_new`, app.rs). The rest of the Profiles
|
||||
//! page is in `settings_pages.rs`.
|
||||
//! page is in `settings_pages/`.
|
||||
|
||||
use tracing::warn;
|
||||
use voelin_core::settings::IDENTITY_IMPORT;
|
||||
use voelin_store::IdentityOrigin;
|
||||
|
||||
use crate::app::App;
|
||||
|
||||
/// Where an identity came from, as Profiles says it.
|
||||
pub(crate) fn origin_label(origin: IdentityOrigin) -> &'static str {
|
||||
match origin {
|
||||
IdentityOrigin::Created => "Made in Voelin",
|
||||
IdentityOrigin::Imported => "Imported",
|
||||
IdentityOrigin::User => "Chosen by you",
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
/// The user's choice of the identity servers see us as, from the next
|
||||
/// connection on; no import replaces it.
|
||||
@@ -51,16 +41,3 @@ impl App {
|
||||
self.refresh_identities();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rows_say_where_an_identity_came_from() {
|
||||
assert_eq!(origin_label(IdentityOrigin::Created), "Made in Voelin");
|
||||
assert_eq!(origin_label(IdentityOrigin::Imported), "Imported");
|
||||
// Made by the app, then chosen: an import never replaces it.
|
||||
assert_eq!(origin_label(IdentityOrigin::User), "Chosen by you");
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,801 @@
|
||||
//! The settings pages on the Bridge: the forms and models are filled from
|
||||
//! the settings (`voelin_ui_vm::settings_pages::logic`), the controls' values
|
||||
//! go back through it, and what a change sets going (the camera, the engine,
|
||||
//! the identity store, threads) runs here.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use slint::{ComponentHandle, Model, SharedString};
|
||||
use tracing::warn;
|
||||
use voelin_core::settings::{
|
||||
IDENTITY_IMPORT, STREAM_CODEC, STREAM_ENCODER_BACKEND, STREAM_HARDWARE_ACCELERATION,
|
||||
STREAM_SRTP_PROFILES,
|
||||
};
|
||||
use voelin_core::{Command, GatewayRequest};
|
||||
use voelin_gateway_proto::{Action, PermRule, UniqueIds};
|
||||
|
||||
use crate::app::{App, Bridge, later, model};
|
||||
use crate::bind::convert;
|
||||
use crate::settings::{
|
||||
VIDEO_BACKGROUND, VIDEO_CAMERA, VIDEO_MIRROR, VIDEO_RESOLUTION, page_keys, parse_size,
|
||||
};
|
||||
use crate::vm::bridge::{
|
||||
AdvancedForm, AudioSourceItem, LayerItem, NotifyForm, PrivacyForm, StreamingForm, VideoForm,
|
||||
};
|
||||
use crate::vm::settings_pages::logic::{
|
||||
self, Discovered, IdentityFacts, Improving, group_ids, json_of,
|
||||
};
|
||||
|
||||
impl App {
|
||||
/// What storing settings had to say (a refusal, something typed wrong):
|
||||
/// the status line shows it.
|
||||
fn said(&mut self, messages: Vec<String>) {
|
||||
for message in messages {
|
||||
warn!(%message, "setting refused");
|
||||
self.set_status(message);
|
||||
}
|
||||
}
|
||||
|
||||
/// A section of the settings was shown: load what it needs.
|
||||
pub(crate) fn settings_section(&mut self, section: i32) {
|
||||
for key in page_keys() {
|
||||
self.prefs.register(key);
|
||||
}
|
||||
match section {
|
||||
0 | 7 => {
|
||||
self.load_cameras();
|
||||
if section == 7 {
|
||||
self.load_capture_sources();
|
||||
}
|
||||
}
|
||||
2 => self.watch_audio_apps(),
|
||||
5 | 6 => self.refresh_identities(),
|
||||
9 => {
|
||||
if self.pages.state.encoders_loading
|
||||
|| self
|
||||
.ui
|
||||
.upgrade()
|
||||
.is_some_and(|ui| ui.global::<Bridge>().get_encoders().row_count() == 0)
|
||||
{
|
||||
self.refresh_encoders();
|
||||
}
|
||||
self.refresh_gateways();
|
||||
}
|
||||
10 => self.refresh_all_settings(),
|
||||
_ => {}
|
||||
}
|
||||
if section != 0 && section != 7 {
|
||||
self.stop_camera();
|
||||
}
|
||||
if section != 2 {
|
||||
self.pages.audio_apps = None;
|
||||
}
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
/// Every form of the settings pages, from the settings.
|
||||
pub(crate) fn refresh_pages(&self) {
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let bridge = ui.global::<Bridge>();
|
||||
let p = &self.prefs;
|
||||
let video = logic::video_form(p, &self.pages.state.cameras, self.pages.camera.is_some());
|
||||
bridge.set_video(video.into());
|
||||
bridge.set_streaming(logic::streaming_form(p).into());
|
||||
self.models.layers.sync(&logic::layer_items(p));
|
||||
self.models.audio_sources.sync(&logic::audio_source_items(p, voelin_platform::APP_NAME));
|
||||
bridge.set_has_studio(true);
|
||||
bridge.set_notify(logic::notify_form(p).into());
|
||||
bridge.set_privacy(logic::privacy_form(p).into());
|
||||
let srtp = p.get(&STREAM_SRTP_PROFILES);
|
||||
bridge.set_srtp_profiles(model(srtp.iter().map(SharedString::from).collect()));
|
||||
bridge.set_srtp_unused(model(
|
||||
logic::srtp_unused(&srtp).into_iter().map(SharedString::from).collect(),
|
||||
));
|
||||
let crash_dir = voelin_platform::crash::dir().map(|d| d.display().to_string());
|
||||
bridge.set_advanced(
|
||||
logic::advanced_form(p, &log_text(), &crash_dir.unwrap_or_default()).into(),
|
||||
);
|
||||
self.refresh_account();
|
||||
}
|
||||
|
||||
/// A setting changed anywhere: fill the forms, apply what the engine
|
||||
/// does not read itself.
|
||||
pub(crate) fn page_setting_changed(&mut self, key: &str) {
|
||||
if key == STREAM_SRTP_PROFILES.name() {
|
||||
self.apply_srtp();
|
||||
}
|
||||
if [STREAM_HARDWARE_ACCELERATION.name(), STREAM_ENCODER_BACKEND.name(), STREAM_CODEC.name()]
|
||||
.contains(&key)
|
||||
{
|
||||
self.video.set_encoder_preference(
|
||||
voelin_core::media::encoder_preference(&self.prefs),
|
||||
voelin_core::media::configured_codec(&self.prefs),
|
||||
);
|
||||
}
|
||||
if key == VIDEO_CAMERA.name()
|
||||
|| key == VIDEO_BACKGROUND.name()
|
||||
|| key == VIDEO_RESOLUTION.name()
|
||||
{
|
||||
self.restart_camera();
|
||||
}
|
||||
self.refresh_pages();
|
||||
if key.starts_with("stream.")
|
||||
|| key.starts_with("privacy.")
|
||||
|| key.starts_with("chat.")
|
||||
|| key.starts_with("cache.")
|
||||
{
|
||||
self.refresh_all_settings();
|
||||
}
|
||||
}
|
||||
|
||||
/// The SRTP profiles of the setting, for the engine (it does not read
|
||||
/// the setting itself; it takes the names and leaves out the ones its
|
||||
/// stream transport does not know).
|
||||
pub(crate) fn apply_srtp(&self) {
|
||||
let profiles = self.prefs.get(&STREAM_SRTP_PROFILES);
|
||||
self.engine.send(Command::SetSrtpProfiles(profiles.to_vec()));
|
||||
}
|
||||
|
||||
pub(crate) fn video_changed(&mut self, form: &VideoForm) {
|
||||
let messages = logic::apply_video(&self.prefs, &self.pages.state.cameras, form);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn streaming_changed(&mut self, form: &StreamingForm) {
|
||||
let messages = logic::apply_streaming(&self.prefs, form);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn layer_changed(&mut self, index: i32, item: &LayerItem) {
|
||||
let Some(messages) = logic::apply_layer(&self.prefs, index, item) else { return };
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn add_layer(&mut self) {
|
||||
let messages = logic::add_layer(&self.prefs);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn remove_layer(&mut self, index: i32) {
|
||||
let messages = logic::remove_layer(&self.prefs, index);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn audio_source_changed(&mut self, index: i32, item: &AudioSourceItem) {
|
||||
let messages = logic::apply_audio_source(&self.prefs, index, item);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn add_audio_source(&mut self, kind: &str, name: &str) {
|
||||
let Some(messages) = logic::add_audio_source(&self.prefs, kind, name) else { return };
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn remove_audio_source(&mut self, index: i32) {
|
||||
let messages = logic::remove_audio_source(&self.prefs, index);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
/// Follow the applications that play audio while the Streaming page is
|
||||
/// open (`tick` checks for changes).
|
||||
fn watch_audio_apps(&mut self) {
|
||||
if self.pages.audio_apps.is_none() && !self.demo_ui {
|
||||
match voelin_core::media::audio_apps() {
|
||||
Ok(apps) => self.pages.audio_apps = Some(apps),
|
||||
Err(e) => warn!("cannot list applications that play audio: {e}"),
|
||||
}
|
||||
}
|
||||
self.refresh_audio_apps();
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_audio_apps(&mut self) {
|
||||
let Some(apps) = self.pages.audio_apps.as_mut() else { return };
|
||||
let names: Vec<SharedString> = apps
|
||||
.current()
|
||||
.into_iter()
|
||||
.map(|a| SharedString::from(a.binary.unwrap_or(a.name)))
|
||||
.collect();
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
ui.global::<Bridge>().set_audio_apps(model(names));
|
||||
}
|
||||
}
|
||||
|
||||
/// Called once a second: a changed list of audio applications.
|
||||
pub(crate) fn tick_pages(&mut self) {
|
||||
if self.pages.audio_apps.as_ref().is_some_and(|a| a.has_changed()) {
|
||||
self.refresh_audio_apps();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn notify_changed(&mut self, form: &NotifyForm) {
|
||||
let messages = logic::apply_notify(&self.prefs, form);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn test_notification(&mut self) {
|
||||
let n =
|
||||
voelin_platform::Notification::new(voelin_platform::APP_NAME, "Notifications work.");
|
||||
self.engine.runtime().spawn(async move {
|
||||
let result = voelin_platform::notify(&n).await;
|
||||
later(move |app| {
|
||||
app.set_status(match result {
|
||||
Ok(()) => "A desktop notification was sent.".to_owned(),
|
||||
Err(e) => format!("No desktop notification: {e}"),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn privacy_changed(&mut self, form: &PrivacyForm) {
|
||||
let messages = logic::apply_privacy(&self.prefs, form);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn advanced_changed(&mut self, form: &AdvancedForm) {
|
||||
let messages = logic::apply_advanced(&self.prefs, form);
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
/// Move an SRTP profile up (`-1`) or down (`1`).
|
||||
pub(crate) fn srtp_move(&mut self, index: i32, by: i32) {
|
||||
let Some(messages) = logic::srtp_move(&self.prefs, index, by) else { return };
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
/// Offer a profile, or stop offering it (one stays).
|
||||
pub(crate) fn srtp_toggle(&mut self, name: &str) {
|
||||
let Some(messages) = logic::srtp_toggle(&self.prefs, name) else { return };
|
||||
self.said(messages);
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
/// Every registered setting (Advanced), filtered.
|
||||
pub(crate) fn refresh_all_settings(&self) {
|
||||
let filter = self.pages.state.settings_filter.trim().to_lowercase();
|
||||
self.models.all_settings.sync(&logic::setting_items(&self.prefs, &filter));
|
||||
}
|
||||
|
||||
pub(crate) fn search_settings(&mut self, text: String) {
|
||||
self.pages.state.settings_filter = text;
|
||||
self.refresh_all_settings();
|
||||
}
|
||||
|
||||
/// Set a key from its JSON text; why not, when it is refused.
|
||||
pub(crate) fn setting_set(&mut self, key: &str, text: &str) -> String {
|
||||
match self.prefs.set_json(key, json_of(text)) {
|
||||
Ok(()) => {
|
||||
self.refresh_all_settings();
|
||||
String::new()
|
||||
}
|
||||
Err(e) => {
|
||||
let message = e.to_string();
|
||||
self.set_status(message.clone());
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn setting_reset(&mut self, key: &str) {
|
||||
if let Err(e) = self.prefs.reset(key) {
|
||||
self.set_status(e.to_string());
|
||||
}
|
||||
self.refresh_all_settings();
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
/// The folder with the log files.
|
||||
pub(crate) fn open_logs(&mut self) {
|
||||
let dir = voelin_platform::logs::current().and_then(std::path::Path::parent);
|
||||
let result = dir.ok_or_else(|| "no log file".to_owned());
|
||||
if let Err(e) = result.and_then(|dir| crate::app::open_path(dir).map_err(|e| e.to_string()))
|
||||
{
|
||||
self.set_status(format!("Cannot open the log folder: {e}"));
|
||||
}
|
||||
}
|
||||
|
||||
// Integrations.
|
||||
|
||||
/// The gateways of our servers and the one being administered.
|
||||
pub(crate) fn refresh_gateways(&self) {
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let bridge = ui.global::<Bridge>();
|
||||
bridge.set_gateways(convert::model(&logic::gateway_items(&self.state)));
|
||||
bridge.set_admin_session(self.pages.state.admin.map_or(-1, |s| s as i32));
|
||||
let extra =
|
||||
self.pages.state.admin.and_then(|s| self.state.sessions.get(&s)).map(|v| &v.extra);
|
||||
self.models.gateway_config.sync(&logic::config_items(extra));
|
||||
self.models.gateway_perms.sync(&logic::perm_items(extra));
|
||||
}
|
||||
|
||||
/// Administer a gateway (`-1`: close).
|
||||
pub(crate) fn gateway_admin(&mut self, session: i32) {
|
||||
self.pages.state.admin = (session >= 0).then_some(i64::from(session));
|
||||
if let Some(id) = self.pages.state.admin {
|
||||
self.gateway_to(id, GatewayRequest::ConfigList);
|
||||
self.gateway_to(id, GatewayRequest::PermList);
|
||||
}
|
||||
self.refresh_gateways();
|
||||
}
|
||||
|
||||
pub(crate) fn config_set(&mut self, key: &str, text: &str) {
|
||||
if let Some(id) = self.pages.state.admin {
|
||||
self.gateway_to(
|
||||
id,
|
||||
GatewayRequest::ConfigSet { key: key.to_owned(), value: json_of(text) },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn config_reset(&mut self, key: &str) {
|
||||
if let Some(id) = self.pages.state.admin {
|
||||
self.gateway_to(id, GatewayRequest::ConfigReset { key: key.to_owned() });
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn config_reload(&mut self) {
|
||||
if let Some(id) = self.pages.state.admin {
|
||||
self.gateway_to(id, GatewayRequest::ConfigReload);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn perm_set(&mut self, action: &str, everyone: bool, server: &str, channel: &str) {
|
||||
let (Some(id), Some(action)) = (self.pages.state.admin, Action::parse(action)) else {
|
||||
return;
|
||||
};
|
||||
let rule = PermRule {
|
||||
everyone,
|
||||
server_groups: group_ids(server),
|
||||
channel_groups: group_ids(channel),
|
||||
};
|
||||
self.gateway_to(id, GatewayRequest::PermSet { action, rule });
|
||||
self.gateway_to(id, GatewayRequest::PermList);
|
||||
}
|
||||
|
||||
pub(crate) fn perm_reset(&mut self, action: &str) {
|
||||
let (Some(id), Some(action)) = (self.pages.state.admin, Action::parse(action)) else {
|
||||
return;
|
||||
};
|
||||
self.gateway_to(id, GatewayRequest::PermReset { action });
|
||||
self.gateway_to(id, GatewayRequest::PermList);
|
||||
}
|
||||
|
||||
/// Probe the encoders off the UI thread (FFmpeg's probe can take a
|
||||
/// while).
|
||||
pub(crate) fn refresh_encoders(&mut self) {
|
||||
self.pages.state.encoders_loading = true;
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
ui.global::<Bridge>().set_encoders_loading(true);
|
||||
}
|
||||
let codecs = self.video.codecs();
|
||||
std::thread::spawn(move || {
|
||||
let report = codecs.report();
|
||||
later(move |app| app.encoders_ready(report));
|
||||
});
|
||||
}
|
||||
|
||||
fn encoders_ready(&mut self, report: voelin_core::media::voelin_media::EncoderReport) {
|
||||
self.pages.state.encoders_loading = false;
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let bridge = ui.global::<Bridge>();
|
||||
bridge.set_encoders_loading(false);
|
||||
bridge.set_ffmpeg_status(logic::ffmpeg_status(&report.ffmpeg).into());
|
||||
bridge.set_zero_copy_status(logic::zero_copy_status(&report.zero_copy).into());
|
||||
bridge.set_encoders(convert::model(&logic::encoder_items(&report.encoders)));
|
||||
bridge.set_decoders(convert::model(&logic::encoder_items(&report.decoders)));
|
||||
}
|
||||
|
||||
/// The screens and windows that can be shared (Devices).
|
||||
fn load_capture_sources(&mut self) {
|
||||
let names: Vec<SharedString> =
|
||||
logic::capture_source_names(self.video.sources(false, false))
|
||||
.into_iter()
|
||||
.map(SharedString::from)
|
||||
.collect();
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
ui.global::<Bridge>().set_capture_sources(model(names));
|
||||
}
|
||||
}
|
||||
|
||||
// Identities (My Account, Profiles).
|
||||
|
||||
/// Our unique ids (every identity, both server generations), to tell
|
||||
/// our own messages.
|
||||
pub(crate) fn load_own_uids(&mut self) {
|
||||
let mut uids = std::collections::HashSet::new();
|
||||
for e in self.store.identities().unwrap_or_default() {
|
||||
if let Ok(identity) = self.store.identity(e.id) {
|
||||
let ids = UniqueIds::from_omega(&identity.key().to_pub().to_ts());
|
||||
uids.insert(ids.ts3);
|
||||
uids.insert(ids.ts6);
|
||||
}
|
||||
}
|
||||
self.social.own_uids = uids;
|
||||
}
|
||||
|
||||
/// The identity servers see us as on `bookmark` (the one place that
|
||||
/// picks it): the bookmark's own if it names one that exists, else the
|
||||
/// store's default.
|
||||
fn identity_id_for(&self, bookmark: Option<i64>) -> Option<i64> {
|
||||
let entries = self.store.identities().unwrap_or_default();
|
||||
bookmark
|
||||
.and_then(|b| self.bookmark(b))
|
||||
.and_then(|b| b.identity)
|
||||
.filter(|id| entries.iter().any(|e| e.id == *id))
|
||||
.or_else(|| self.default_identity_id())
|
||||
}
|
||||
|
||||
/// The identity a connection to `bookmark` uses ([`Self::identity_id_for`]).
|
||||
pub(crate) fn identity_for(&self, bookmark: Option<i64>) -> tsclientlib::Identity {
|
||||
self.identity_id_for(bookmark)
|
||||
.and_then(|id| self.store.identity(id).ok())
|
||||
.unwrap_or_else(|| self.identity.clone())
|
||||
}
|
||||
|
||||
/// The store's default identity (`Store::default_identity`).
|
||||
fn default_identity_id(&self) -> Option<i64> {
|
||||
self.store.default_identity().ok().flatten().map(|e| e.id)
|
||||
}
|
||||
|
||||
pub(crate) fn refresh_account(&self) {
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let bridge = ui.global::<Bridge>();
|
||||
let bookmark = self.state.current.and_then(|id| self.bookmark(id));
|
||||
let identity = self.identity_for(self.state.current);
|
||||
let ids = UniqueIds::from_omega(&identity.key().to_pub().to_ts());
|
||||
let entry_id = self.identity_id_for(self.state.current);
|
||||
let name = self
|
||||
.store
|
||||
.identities()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.find(|e| Some(e.id) == entry_id)
|
||||
.map(|e| e.name)
|
||||
.unwrap_or_default();
|
||||
bridge.set_account(
|
||||
logic::account_form(bookmark, &name, &ids.ts3, &ids.ts6, identity.level()).into(),
|
||||
);
|
||||
}
|
||||
|
||||
/// The identities, the bookmarks' choices, what an import found.
|
||||
pub(crate) fn refresh_identities(&self) {
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let bridge = ui.global::<Bridge>();
|
||||
let entries = self.store.identities().unwrap_or_default();
|
||||
let default = self.default_identity_id();
|
||||
let picks: Vec<(&str, Option<i64>)> = self
|
||||
.state
|
||||
.bookmarks
|
||||
.iter()
|
||||
.map(|b| (b.name.as_str(), self.identity_id_for(Some(b.id))))
|
||||
.collect();
|
||||
// What each key says: its unique id on TeamSpeak 6 servers and its level.
|
||||
let keys: Vec<Option<(String, u8)>> = entries
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let identity = self.store.identity(e.id).ok()?;
|
||||
let ids = UniqueIds::from_omega(&identity.key().to_pub().to_ts());
|
||||
Some((ids.ts6, identity.level()))
|
||||
})
|
||||
.collect();
|
||||
let facts: Vec<IdentityFacts> =
|
||||
entries.iter().zip(keys).map(|(entry, key)| IdentityFacts { entry, key }).collect();
|
||||
let improving =
|
||||
|id: i64| self.pages.state.improving.get(&id).map(|i| (i.target, i.reached));
|
||||
self.models.identities.sync(&logic::identity_items(&facts, default, &picks, improving));
|
||||
let names: Vec<SharedString> =
|
||||
logic::identity_names(&entries).into_iter().map(SharedString::from).collect();
|
||||
bridge.set_identity_names(model(names));
|
||||
bridge.set_bookmark_identities(convert::model(&logic::bookmark_identities(
|
||||
&self.state.bookmarks,
|
||||
&entries,
|
||||
)));
|
||||
let known: HashSet<&str> = entries.iter().map(|e| e.uid.as_str()).collect();
|
||||
let found: Vec<Discovered> = self
|
||||
.pages
|
||||
.state
|
||||
.found
|
||||
.iter()
|
||||
.map(|(f, picked)| Discovered {
|
||||
nickname: f.nickname.clone(),
|
||||
uid: f.uid(),
|
||||
level: f.level(),
|
||||
source: f.source.display().to_string(),
|
||||
picked: *picked,
|
||||
})
|
||||
.collect();
|
||||
bridge.set_found_identities(convert::model(&logic::found_items(&found, &known)));
|
||||
bridge.set_identity_import(self.prefs.get(&IDENTITY_IMPORT));
|
||||
self.refresh_account();
|
||||
}
|
||||
|
||||
/// "delete", "export", "stop" (the default: [`Self::use_identity`]).
|
||||
pub(crate) fn identity_action(&mut self, id: i32, action: &str) {
|
||||
let id = i64::from(id);
|
||||
match action {
|
||||
"delete" => {
|
||||
if self.default_identity_id() == Some(id) {
|
||||
self.set_status(
|
||||
"The default identity cannot be deleted; make another one the default first.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if let Err(e) = self.store.delete_identity(id) {
|
||||
self.set_status(format!("Could not delete the identity: {e}"));
|
||||
}
|
||||
self.load_own_uids();
|
||||
}
|
||||
"export" => self.export_identity(id),
|
||||
"stop" => {
|
||||
if let Some(i) = self.pages.state.improving.remove(&id) {
|
||||
i.cancel.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
fn export_identity(&mut self, id: i64) {
|
||||
let entries = self.store.identities().unwrap_or_default();
|
||||
let (Some(entry), Ok(identity)) =
|
||||
(entries.iter().find(|e| e.id == id), self.store.identity(id))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let dir = dirs::download_dir().or_else(dirs::home_dir).unwrap_or_else(|| ".".into());
|
||||
let file = logic::export_file_stem(&entry.name);
|
||||
let path = dir.join(format!("{file}.ini"));
|
||||
match voelin_core::identity::write_export(&path, &entry.name, &identity) {
|
||||
Ok(()) => self.set_identity_status(format!(
|
||||
"Exported to {} (it holds the private key: keep it safe).",
|
||||
path.display()
|
||||
)),
|
||||
Err(e) => self.set_identity_status(format!("Could not export: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn set_identity_status(&mut self, text: String) {
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
ui.global::<Bridge>().set_identity_status(text.clone().into());
|
||||
}
|
||||
self.set_status(text);
|
||||
}
|
||||
|
||||
pub(crate) fn rename_identity(&mut self, id: i32, name: &str) {
|
||||
let name = name.trim();
|
||||
if name.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Err(e) = self.store.rename_identity(i64::from(id), name) {
|
||||
self.set_status(format!("Could not rename: {e}"));
|
||||
}
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
/// Raise an identity's level to `target` on every core, in the
|
||||
/// background.
|
||||
pub(crate) fn improve_identity(&mut self, id: i32, target: i32) {
|
||||
let id = i64::from(id);
|
||||
let Ok(identity) = self.store.identity(id) else { return };
|
||||
let Ok(target) = u8::try_from(target) else { return };
|
||||
if target <= identity.level() || self.pages.state.improving.contains_key(&id) {
|
||||
self.set_identity_status(format!(
|
||||
"The identity is at level {} already.",
|
||||
identity.level()
|
||||
));
|
||||
return;
|
||||
}
|
||||
let cancel = Arc::new(AtomicBool::new(false));
|
||||
self.pages
|
||||
.state
|
||||
.improving
|
||||
.insert(id, Improving { target, reached: identity.level(), cancel: cancel.clone() });
|
||||
std::thread::Builder::new()
|
||||
.name("identity-level".into())
|
||||
.spawn(move || {
|
||||
let better =
|
||||
voelin_core::identity::improve_level(&identity, target, &cancel, |level| {
|
||||
later(move |app| {
|
||||
if let Some(i) = app.pages.state.improving.get_mut(&id) {
|
||||
i.reached = i.reached.max(level);
|
||||
}
|
||||
app.refresh_identities();
|
||||
});
|
||||
});
|
||||
later(move |app| app.level_reached(id, better));
|
||||
})
|
||||
.ok();
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
fn level_reached(&mut self, id: i64, better: Option<tsclientlib::Identity>) {
|
||||
self.pages.state.improving.remove(&id);
|
||||
if let Some(identity) = better {
|
||||
match self.store.update_identity(id, &identity) {
|
||||
Ok(()) => {
|
||||
self.set_identity_status(format!(
|
||||
"Security level {} reached.",
|
||||
identity.level()
|
||||
));
|
||||
if self.default_identity_id() == Some(id) {
|
||||
self.identity = identity;
|
||||
}
|
||||
}
|
||||
Err(e) => self.set_identity_status(format!("Could not store the identity: {e}")),
|
||||
}
|
||||
}
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
/// Look for identities in the official clients' files.
|
||||
pub(crate) fn find_identities(&mut self) {
|
||||
let mut found = Vec::new();
|
||||
let mut errors = Vec::new();
|
||||
for path in voelin_core::identity::discover() {
|
||||
match voelin_core::identity::read(&path) {
|
||||
Ok(list) => found.extend(list),
|
||||
Err(e) => errors.push(e.to_string()),
|
||||
}
|
||||
}
|
||||
let known: std::collections::HashSet<String> =
|
||||
self.store.identities().unwrap_or_default().into_iter().map(|e| e.uid).collect();
|
||||
let status = logic::found_status(found.len(), errors.first().map(String::as_str));
|
||||
self.pages.state.found = found
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
let new = !known.contains(&f.uid());
|
||||
(f, new)
|
||||
})
|
||||
.collect();
|
||||
self.set_identity_status(status);
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
pub(crate) fn pick_found(&mut self, index: i32, on: bool) {
|
||||
if let Some(f) = usize::try_from(index).ok().and_then(|i| self.pages.state.found.get_mut(i))
|
||||
{
|
||||
f.1 = on;
|
||||
}
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
pub(crate) fn import_identities(&mut self) {
|
||||
let picked: Vec<_> =
|
||||
self.pages.state.found.iter().filter(|(_, p)| *p).map(|(f, _)| f.clone()).collect();
|
||||
match voelin_core::identity::import(&self.store, &picked) {
|
||||
Ok(results) => {
|
||||
let added = results
|
||||
.iter()
|
||||
.filter(|r| matches!(r, voelin_core::identity::Imported::Added(_)))
|
||||
.count();
|
||||
self.set_identity_status(format!("{added} identities imported."));
|
||||
}
|
||||
Err(e) => self.set_identity_status(format!("Could not import: {e}")),
|
||||
}
|
||||
self.pages.state.found.clear();
|
||||
self.load_own_uids();
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
/// The identity a bookmark connects with (0: the default).
|
||||
pub(crate) fn bookmark_identity(&mut self, bookmark: i32, index: i32) {
|
||||
let entries = self.store.identities().unwrap_or_default();
|
||||
let identity = usize::try_from(index - 1).ok().and_then(|i| entries.get(i)).map(|e| e.id);
|
||||
let Some(mut b) = self.bookmark(i64::from(bookmark)).cloned() else { return };
|
||||
b.identity = identity;
|
||||
if let Err(e) = self.store.update_bookmark(&b) {
|
||||
self.set_status(format!("Could not save: {e}"));
|
||||
return;
|
||||
}
|
||||
self.state.bookmarks = self.store.bookmarks().unwrap_or_default();
|
||||
self.refresh_identities();
|
||||
}
|
||||
|
||||
// Camera (Voice & Video, Devices).
|
||||
|
||||
fn load_cameras(&mut self) {
|
||||
let mut cameras = crate::camera::list();
|
||||
// Sample data never opens a real camera.
|
||||
if self.demo_ui {
|
||||
cameras.retain(|(id, _)| id == "synthetic");
|
||||
}
|
||||
self.pages.state.cameras = cameras.iter().map(|(id, _)| id.clone()).collect();
|
||||
let names: Vec<SharedString> =
|
||||
cameras.into_iter().map(|(_, n)| SharedString::from(n)).collect();
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
ui.global::<Bridge>().set_cameras(model(names));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn toggle_camera_preview(&mut self) {
|
||||
if self.pages.camera.is_some() {
|
||||
self.stop_camera();
|
||||
} else {
|
||||
self.start_camera();
|
||||
}
|
||||
self.refresh_pages();
|
||||
}
|
||||
|
||||
pub(crate) fn start_camera(&mut self) {
|
||||
let device =
|
||||
if self.demo_ui { "synthetic".to_owned() } else { self.prefs.get(&VIDEO_CAMERA) };
|
||||
let size = parse_size(&self.prefs.get(&VIDEO_RESOLUTION));
|
||||
let blur = self.prefs.get(&VIDEO_BACKGROUND) == "blur";
|
||||
let mirror = self.prefs.get(&VIDEO_MIRROR);
|
||||
self.pages.camera =
|
||||
Some(crate::camera::Preview::start(self.engine.runtime(), &device, size, blur, mirror));
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
ui.global::<Bridge>().set_camera_status("Starting the camera…".into());
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn stop_camera(&mut self) {
|
||||
if self.pages.camera.take().is_some()
|
||||
&& let Some(ui) = self.ui.upgrade()
|
||||
{
|
||||
let bridge = ui.global::<Bridge>();
|
||||
bridge.set_camera_frame(slint::Image::default());
|
||||
bridge.set_camera_status(SharedString::new());
|
||||
}
|
||||
}
|
||||
|
||||
fn restart_camera(&mut self) {
|
||||
if self.pages.camera.is_some() {
|
||||
self.stop_camera();
|
||||
self.start_camera();
|
||||
}
|
||||
}
|
||||
|
||||
/// The camera preview's newest picture (called by its timer).
|
||||
pub(crate) fn camera_frame(&mut self) {
|
||||
let Some(preview) = &self.pages.camera else { return };
|
||||
let (frame, status) = (preview.take_picture(), preview.status());
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
let bridge = ui.global::<Bridge>();
|
||||
if let Some(frame) = frame {
|
||||
bridge.set_camera_frame(frame);
|
||||
}
|
||||
bridge.set_camera_status(status.into());
|
||||
}
|
||||
}
|
||||
|
||||
/// Play a short tone on the speakers (the Test Sound button).
|
||||
pub(crate) fn test_sound(&mut self) {
|
||||
let device = self.state.audio.output_device.clone();
|
||||
let volume = self.state.audio.output_volume;
|
||||
std::thread::spawn(move || {
|
||||
let result = crate::camera::play_tone(device.as_deref(), volume);
|
||||
if let Err(e) = result {
|
||||
later(move |app| app.set_status(format!("Cannot play the test sound: {e}")));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the logs go, for the Advanced page.
|
||||
fn log_text() -> String {
|
||||
match voelin_platform::logs::current() {
|
||||
Some(path) => format!(
|
||||
"This run logs to {} (the previous runs' next to it, the last as voelin.1.log). \
|
||||
VOELIN_LOG=voelin_core=debug,voelin_ui=debug logs more; warnings also go to the terminal.",
|
||||
path.display()
|
||||
),
|
||||
None => "Logs go to the terminal the app was started from (standard error); \
|
||||
RUST_LOG=voelin_core=debug,voelin_ui=debug shows more."
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
//! The settings pages added with the new design: My Account, Profiles
|
||||
//! (identities), the camera and stream quality of Voice & Video, Streaming,
|
||||
//! Devices, Notifications, Privacy & Safety, Integrations (gateways,
|
||||
//! gateway administration, encoders) and Advanced. Every control reads and
|
||||
//! writes a setting (`voelin_core::settings`, `crate::settings`) and
|
||||
//! applies at once; the forms are filled again when a setting changes
|
||||
//! anywhere ([`App::page_setting_changed`](crate::app::App)).
|
||||
//!
|
||||
//! What the pages show and what the controls change is
|
||||
//! `voelin_ui_vm::settings_pages::logic`; `bind.rs` puts it on the Bridge
|
||||
//! and runs the side effects (the camera, the engine, the identity store).
|
||||
|
||||
mod bind;
|
||||
|
||||
use crate::vm::settings_pages::logic::PagesState;
|
||||
|
||||
/// State of the settings pages.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Pages {
|
||||
/// What needs no toolkit.
|
||||
pub state: PagesState,
|
||||
/// The camera preview of Voice & Video and Devices.
|
||||
pub camera: Option<crate::camera::Preview>,
|
||||
/// Applications playing audio (the audio sources picker), while the
|
||||
/// Streaming page is open.
|
||||
pub audio_apps: Option<voelin_core::media::AudioApps>,
|
||||
}
|
||||
@@ -1078,33 +1078,12 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a privacy or notification choice is as an index, and back.
|
||||
pub(crate) fn allowed_index(a: voelin_core::settings::Allowed) -> i32 {
|
||||
match a {
|
||||
voelin_core::settings::Allowed::Everyone => 0,
|
||||
voelin_core::settings::Allowed::Friends => 1,
|
||||
voelin_core::settings::Allowed::Nobody => 2,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn allowed_of(index: i32) -> voelin_core::settings::Allowed {
|
||||
match index {
|
||||
1 => voelin_core::settings::Allowed::Friends,
|
||||
2 => voelin_core::settings::Allowed::Nobody,
|
||||
_ => voelin_core::settings::Allowed::Everyone,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn privacy_indices() {
|
||||
for i in 0..3 {
|
||||
assert_eq!(allowed_index(allowed_of(i)), i);
|
||||
}
|
||||
assert_eq!(allowed_of(9), voelin_core::settings::Allowed::Everyone);
|
||||
fn notice_names_and_setting_pages() {
|
||||
assert_eq!(NoticeKind::Poke.name(), "poke");
|
||||
assert!(SETTING_PAGES.iter().any(|(t, _, _)| *t == "Voice & Video"));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user