Files
Chronos/src/Chronos.App/Sections/SharePanelViewModel.cs
T
Alby96andClaude Opus 5.5 8d41992d84 feat: Chronos Desktop shares its library on the LAN (owner review 5 P4)
Chronos Server for Windows is replaced by Settings > Server > Share this
library: Chronos Desktop hosts the server engine in its own process over its
own index, archive and processing services, job engine and scheduler, so a
second PC, the Uploader and render nodes use this PC's library without a
Windows service or a second index.

- Chronos.Server is a library; Chronos.Server.Host is the console exe that
  Docker and development runs start. The App references the library and the
  ASP.NET Core shared framework (no package).
- ServerHost.BuildHosted/HostedServer: reuses the desktop's engines and
  never creates, starts or disposes them; no static log sink, no console
  lifetime; server.json plus desktop-owned values locked "Set in Chronos
  Desktop"; only upload-cleanup and audit-cleanup schedules are added.
- Both modes: by-id endpoints scoped to the served archive, a client's
  render export folder dropped; hosted mode exposes server job kinds only.
- Desktop: LibraryShare (start after the schedules, stop first, port in
  use named), Settings > Server sharing rows, Server > This library panel
  (state, addresses with Copy, ports, fingerprint, temporary admin
  password, clients, admin panel), own library refused by ConnectFlow and
  dropped from LAN discovery, the uploads sweep step, thumbnails after scans
  while sharing, capture scenes server-share and server-share-failed.
- Removed: the Windows service hosting of Chronos.Server, its switches and
  its tests.

Refs #67

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:55:50 +01:00

172 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Chronos.App.Infrastructure;
using Chronos.App.Server;
using Chronos.App.Settings;
using Chronos.App.Shell;
using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
using Chronos.Ui.Controls;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Sections;
/// <summary>An address of the shared library with its Copy button.</summary>
public sealed record ShareAddressRow(string Address, ICommand CopyCommand);
/// <summary>
/// Server › This library (owner review 5, #67): whether this PC shares its library, why not when it could not start
/// (with Restart), the addresses with Copy, the ports, the certificate's fingerprint with Copy, the first administrator's
/// temporary password while it exists (Show, Copy), the clients of the last minutes and the admin panel. Off, one row and
/// Sharing settings. Follows <see cref="LibraryShare"/> on the UI thread; the clients are read again every few seconds.
/// </summary>
public sealed class SharePanelViewModel : LocalizedViewModel
{
private static readonly TimeSpan RefreshInterval = TimeSpan.FromSeconds(10);
private readonly ShellViewModel _shell;
private readonly LibraryShare _share;
private readonly DispatcherTimer? _timer;
private bool _showPassword;
public SharePanelViewModel(ShellViewModel shell)
{
_shell = shell;
_share = shell.Services.Share;
_share.PropertyChanged += OnShareChanged;
CopyCommand = new RelayCommand(p => Copy(p as string));
CopyFingerprintCommand = new RelayCommand(() => Copy(_share.Fingerprint), () => _share.Fingerprint is not null);
ShowPasswordCommand = new RelayCommand(() =>
{
_showPassword = !_showPassword;
OnPropertyChanged(nameof(PasswordText));
OnPropertyChanged(nameof(ShowPasswordText));
}, () => _share.AdminPassword is not null);
CopyPasswordCommand = new RelayCommand(() => Copy(_share.AdminPassword), () => _share.AdminPassword is not null);
AdminPanelCommand = new RelayCommand(() => ShellActions.OpenUrl($"https://localhost:{_share.HttpsPort}/admin"), () => _share.IsOn && _share.HttpsPort > 0);
RestartCommand = new RelayCommand(() => _ = RestartAsync(), () => _share.State is ShareState.Failed);
SettingsCommand = new RelayCommand(() =>
{
_shell.Settings.Page = SettingsPage.Server;
_shell.Navigate(SectionKey.Settings);
});
if (Application.Current is not null && !shell.Services.Environment.IsCapture)
{
_timer = new DispatcherTimer(DispatcherPriority.Background) { Interval = RefreshInterval };
_timer.Tick += (_, _) =>
{
if (_share.IsOn) _ = RefreshAsync();
};
_timer.Start();
}
}
public ICommand CopyCommand { get; }
public ICommand CopyFingerprintCommand { get; }
public ICommand ShowPasswordCommand { get; }
public ICommand CopyPasswordCommand { get; }
public ICommand AdminPanelCommand { get; }
public ICommand RestartCommand { get; }
public ICommand SettingsCommand { get; }
/// <summary>Sharing is on in the settings, whether it runs or not: the details show.</summary>
public bool IsShared => _share.State is not ShareState.Off;
public bool IsNotShared => !IsShared;
public bool IsOn => _share.IsOn;
public bool HasFailed => _share.State is ShareState.Failed;
public StatusKind StatusKind => _share.State switch
{
ShareState.On => StatusKind.Online,
ShareState.Starting => StatusKind.Running,
ShareState.Failed => StatusKind.Error,
_ => StatusKind.Offline,
};
public string StateText => Loc.L("Server.Share.State." + _share.State);
public string? Reason => _share.Reason;
public IReadOnlyList<ShareAddressRow> Addresses => [.. _share.Addresses.Select(a => new ShareAddressRow(a, CopyCommand))];
public string? PortsText => _share.IsOn ? Loc.L("Server.Share.PortsValue", _share.HttpsPort, _share.HttpPort) : null;
public string? Fingerprint => _share.Fingerprint is { } fingerprint ? Formatting.LeftToRight(fingerprint) : null;
public bool HasAdminPassword => _share.AdminPassword is not null;
/// <summary>The temporary password, masked until Show.</summary>
public string? PasswordText => _share.AdminPassword is not { } password ? null : _showPassword ? password : new string('•', 12);
public string ShowPasswordText => Loc.L(_showPassword ? "Server.Share.Hide" : "Server.Share.Show");
/// <summary>"anna · Chronos Uploader · 192.168.1.20", the latest first.</summary>
public IReadOnlyList<string> Clients => [.. _share.Clients.Select(c => Loc.L("Server.Share.ClientRow", c.User, c.Program, c.Address).TrimEnd(' ', '·'))];
public bool HasClients => _share.Clients.Count > 0;
public string ClientsText => HasClients ? Loc.Plural("Server.Share.ClientCount", _share.Clients.Count) : Loc.L("Server.Share.NoClients");
public override void Dispose()
{
_timer?.Stop();
_share.PropertyChanged -= OnShareChanged;
base.Dispose();
}
private async Task RestartAsync()
{
await _share.RestartAsync();
CommandManager.InvalidateRequerySuggested();
}
private async Task RefreshAsync()
{
try
{
await _share.RefreshAsync();
}
catch (Exception ex) when (ex is InvalidOperationException or ObjectDisposedException)
{
Log.Warning("share", "The clients of the shared library could not be read", ex);
}
}
private static void Copy(string? text)
{
if (string.IsNullOrEmpty(text)) return;
try
{
Clipboard.SetText(text);
}
catch (ExternalException ex)
{
// Another program holds the clipboard: nothing was copied, as the next try may.
Log.Warning("share", "The clipboard is busy", ex);
}
}
private void OnShareChanged(object? sender, PropertyChangedEventArgs e)
{
if (Application.Current?.Dispatcher is { } dispatcher && !dispatcher.CheckAccess())
{
dispatcher.BeginInvoke(Changed);
return;
}
Changed();
}
private void Changed()
{
if (!_share.IsOn) _showPassword = false;
OnAllPropertiesChanged();
CommandManager.InvalidateRequerySuggested();
}
}