Local-mode composition root (SQLite index, one JobEngine fed by a single handler registration list), keyset-paged library with thumbnail cache and hover scrub, live job list with throttled updates and the shell job bar, settings with live template validation and encoder self-test, server connection with trust-on-first-use and DPAPI token, first-run welcome, About with update check, and --capture screenshots with synthetic data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
210 lines
8.6 KiB
C#
210 lines
8.6 KiB
C#
using System.ComponentModel;
|
||
using System.Windows.Input;
|
||
using Titano.App.Infrastructure;
|
||
using Titano.App.Server;
|
||
using Titano.App.Shell;
|
||
using Titano.Core.Localization;
|
||
using Titano.Ui.Controls;
|
||
using Titano.Ui.Mvvm;
|
||
|
||
namespace Titano.App.Sections;
|
||
|
||
/// <summary>
|
||
/// Server, connection part (BRIEF §12.7, Server.html): address with LAN-discovered servers, connect with
|
||
/// trust-on-first-use and login, status (name, version, latency, certificate), account and quota, sign out, and
|
||
/// the render-node opt-in. The sync table and render-node panel join this screen later.
|
||
/// </summary>
|
||
public sealed class ServerViewModel : SectionViewModel
|
||
{
|
||
private string _address;
|
||
private string? _errorText;
|
||
private ServerSession? _session;
|
||
|
||
public ServerViewModel(ShellViewModel shell)
|
||
: base(SectionKey.Server, "Icon.Server")
|
||
{
|
||
Shell = shell;
|
||
_address = ServerAddress.Display(shell.Services.Settings.ServerUrl);
|
||
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => !IsBusy && !IsConnected && Address.Trim().Length > 0);
|
||
DisconnectCommand = new RelayCommand(() => Session.Disconnect(), () => IsConnected);
|
||
SignOutCommand = new AsyncRelayCommand(SignOutAsync, () => IsConnected);
|
||
UseServerCommand = new RelayCommand(p =>
|
||
{
|
||
if (p is DiscoveredServer s) Address = s.Address;
|
||
else if (p is string text) Address = text;
|
||
});
|
||
CertificateCommand = new RelayCommand(ShowCertificate, () => Fingerprint is not null);
|
||
}
|
||
|
||
public ShellViewModel Shell { get; }
|
||
|
||
private ServerSession Session => _session ?? Shell.Services.Server;
|
||
|
||
public ICommand ConnectCommand { get; }
|
||
public ICommand DisconnectCommand { get; }
|
||
public ICommand SignOutCommand { get; }
|
||
public ICommand UseServerCommand { get; }
|
||
public ICommand CertificateCommand { get; }
|
||
|
||
/// <summary>Servers found on the LAN plus recently used ones, for the ▾ list.</summary>
|
||
public IEnumerable<DiscoveredServer> KnownServers =>
|
||
Session.Discovered.Concat(Shell.Services.Settings.RecentServers
|
||
.Where(r => !Session.Discovered.Any(d => d.Address.Equals(r, StringComparison.OrdinalIgnoreCase)))
|
||
.Select(r => new DiscoveredServer(r, r)));
|
||
|
||
public bool HasKnownServers => KnownServers.Any();
|
||
|
||
public string Address
|
||
{
|
||
get => _address;
|
||
set
|
||
{
|
||
if (Set(ref _address, value ?? string.Empty)) CommandManager.InvalidateRequerySuggested();
|
||
}
|
||
}
|
||
|
||
public bool IsBusy => Session.IsBusy;
|
||
public bool IsConnected => Session.IsConnected;
|
||
public bool IsDisconnected => !Session.IsConnected;
|
||
|
||
/// <summary>One sentence about the last failure; null when none.</summary>
|
||
public string? ErrorText
|
||
{
|
||
get => _errorText;
|
||
private set => Set(ref _errorText, value);
|
||
}
|
||
|
||
// ---- Status row ----
|
||
public StatusKind StatusKind => IsConnected ? StatusKind.Online : IsBusy ? StatusKind.Running : ErrorText is null ? StatusKind.Offline : StatusKind.Error;
|
||
|
||
public string StatusText => IsConnected ? Loc.L("Server.Status.Connected")
|
||
: IsBusy ? Loc.L("Server.Status.Connecting")
|
||
: Session.HasSavedSession ? Loc.L("Server.Status.Disconnected")
|
||
: Loc.L("Server.Status.NotConnected");
|
||
|
||
/// <summary>"Studio NAS · v1.3.0 · 14 ms".</summary>
|
||
public string? ServerText
|
||
{
|
||
get
|
||
{
|
||
if (!IsConnected || Session.Flow.Info is not { } info) return null;
|
||
var parts = new List<string> { info.Name, Loc.L("Server.Version", info.Version) };
|
||
if (Session.Flow.Latency is { } latency) parts.Add(Loc.L("Server.Latency", Math.Max(1, (int)Math.Round(latency.TotalMilliseconds))));
|
||
return string.Join(" · ", parts);
|
||
}
|
||
}
|
||
|
||
/// <summary>Fingerprint of the trusted self-signed certificate; null for a publicly valid certificate.</summary>
|
||
public string? Fingerprint => IsConnected ? Session.TrustOf(Session.Flow.Info?.TlsFingerprint)?.Fingerprint : null;
|
||
|
||
/// <summary>"TLS 9A:3C:71:E0:… trusted 2026-09-01".</summary>
|
||
public string? CertificateText => Fingerprint is { } fp && Session.TrustOf(fp) is { } trust
|
||
? Loc.L("Server.Certificate.Trusted", ServerSession.ShortFingerprint(fp), Formatting.Date(trust.TrustedAt))
|
||
: null;
|
||
|
||
public string CertificateToolTip => Loc.L("Tooltips.Server.CertificateTrusted");
|
||
|
||
// ---- Account row ----
|
||
public string? Username => Session.User?.Username;
|
||
public string Initial => Username is { Length: > 0 } u ? char.ToUpper(u[0], Loc.Culture).ToString() : string.Empty;
|
||
|
||
public string? RoleText => Session.User?.Role is { } role ? KnownRole(role) is { } known ? Loc.L("Server.Role." + known) : role : null;
|
||
|
||
public string? RoleToolTip => Session.User?.Role is { } role && KnownRole(role) is { } known ? Loc.L("Tooltips.Server.Role." + known) : null;
|
||
|
||
public bool HasQuota => Session.User is { QuotaBytes: > 0 };
|
||
|
||
public double QuotaFraction => Session.User is { QuotaBytes: > 0 } u ? Math.Clamp((double)u.UsedBytes / u.QuotaBytes, 0, 1) : 0;
|
||
|
||
/// <summary>"2.1 TB of 5 TB", or the used bytes when unlimited.</summary>
|
||
public string? QuotaText => Session.User is not { } u ? null
|
||
: u.QuotaBytes > 0 ? Loc.L("Server.Quota", Formatting.Bytes(u.UsedBytes), Formatting.Bytes(u.QuotaBytes))
|
||
: Loc.L("Server.QuotaUnlimited", Formatting.Bytes(u.UsedBytes));
|
||
|
||
// ---- Options ----
|
||
public bool RenderNodeOptIn
|
||
{
|
||
get => Shell.Services.Settings.RenderNodeOptIn;
|
||
set
|
||
{
|
||
if (Shell.Services.Settings.RenderNodeOptIn == value) return;
|
||
Shell.Services.Settings.RenderNodeOptIn = value;
|
||
Shell.Services.SaveSettings();
|
||
OnPropertyChanged();
|
||
}
|
||
}
|
||
|
||
/// <summary>Re-reads values another screen changed (Settings › Server).</summary>
|
||
public void Refresh() => OnPropertyChanged(nameof(RenderNodeOptIn));
|
||
|
||
/// <summary>Restores the saved session and starts listening for LAN servers.</summary>
|
||
public async Task AttachAsync()
|
||
{
|
||
_session = Shell.Services.Server;
|
||
_session.PropertyChanged += OnSessionChanged;
|
||
_session.Discovered.CollectionChanged += (_, _) =>
|
||
{
|
||
OnPropertyChanged(nameof(KnownServers));
|
||
OnPropertyChanged(nameof(HasKnownServers));
|
||
if (Address.Length == 0 && Session.Discovered.Count > 0) Address = Session.Discovered[0].Address;
|
||
};
|
||
if (!Shell.Services.Environment.IsCapture) _session.StartDiscovery();
|
||
await _session.RestoreAsync();
|
||
if (_session.Flow.Error is not ConnectError.None and var error && !_session.IsConnected) ErrorText = ServerSession.ErrorText(error);
|
||
}
|
||
|
||
public override void Dispose()
|
||
{
|
||
if (_session is not null) _session.PropertyChanged -= OnSessionChanged;
|
||
base.Dispose();
|
||
}
|
||
|
||
private async Task ConnectAsync()
|
||
{
|
||
ErrorText = null;
|
||
var connected = await Session.ConnectAsync(Address, Shell.Services.Dialogs);
|
||
if (connected)
|
||
{
|
||
Address = ServerAddress.Display(Session.SavedAddress);
|
||
return;
|
||
}
|
||
if (Session.Flow.Error is not ConnectError.None and var error) ErrorText = ServerSession.ErrorText(error);
|
||
}
|
||
|
||
private async Task SignOutAsync()
|
||
{
|
||
await Session.SignOutAsync();
|
||
ErrorText = null;
|
||
}
|
||
|
||
private void ShowCertificate()
|
||
{
|
||
if (Fingerprint is not { } fingerprint) return;
|
||
var forget = Shell.Services.Dialogs.Confirm(new ConfirmRequest
|
||
{
|
||
Heading = Loc.L("Server.Certificate.Title"),
|
||
Message = Loc.L("Server.Certificate.Message"),
|
||
Detail = ServerSession.FormatFingerprint(fingerprint),
|
||
DetailToolTip = Loc.L("Tooltips.Server.Fingerprint"),
|
||
SafeText = Loc.L("Server.Certificate.Keep"),
|
||
SafeToolTip = Loc.L("Tooltips.Server.KeepTrust"),
|
||
ActionText = Loc.L("Server.Certificate.Forget"),
|
||
ActionToolTip = Loc.L("Tooltips.Server.ForgetTrust"),
|
||
IsDestructive = true,
|
||
});
|
||
if (!forget) return;
|
||
Session.Disconnect();
|
||
Session.Forget(fingerprint);
|
||
}
|
||
|
||
private void OnSessionChanged(object? sender, PropertyChangedEventArgs e)
|
||
{
|
||
OnAllPropertiesChanged();
|
||
CommandManager.InvalidateRequerySuggested();
|
||
}
|
||
|
||
/// <summary>The <see cref="Core.Model.UserRole"/> name of a server role, or null for a role this build does not know.</summary>
|
||
private static string? KnownRole(string role) =>
|
||
Enum.TryParse<Core.Model.UserRole>(role, ignoreCase: true, out var parsed) ? parsed.ToString() : null;
|
||
}
|