Registro di casa 192.168.30.23:3000 (HTTP) nel modello Unraid, in compose e nel rilascio con buildx; profilo seccomp fuori da /dati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
147 lines
5.8 KiB
C#
147 lines
5.8 KiB
C#
using System.Diagnostics;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using Febe.Desktop.Core;
|
|
using Febe.Web.Native;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Febe.Desktop;
|
|
|
|
/// <summary>
|
|
/// The native functions of Windows offered to the interface (spec §12): folder picker (<see cref="OpenFolderDialog"/>),
|
|
/// «Mostra nella cartella» (<c>explorer.exe /select,"…"</c>), addresses in the default browser (shell execute), Windows
|
|
/// notifications (balloon tips of the notification-area icon: the alerts of spec §13.3 arrive here through
|
|
/// <see cref="NativeShellAlertChannel"/>, and «Invia una prova») and start with the computer
|
|
/// (<c>HKCU\…\Run</c>, value <c>Febe</c>, or <c>Febe-…</c> with <c>--dati</c> for a Febe started on a Data folder of
|
|
/// its own: <paramref name="fixedDataFolder"/>, see <see cref="Autostart"/>). Paths and addresses never come from the
|
|
/// page: the web layer resolves them.
|
|
/// </summary>
|
|
internal sealed class WindowsShell(Dispatcher ui, Func<Window?> owner, Func<TrayIcon?> tray, string? fixedDataFolder) : INativeShell
|
|
{
|
|
public bool CanPickFolder => true;
|
|
|
|
public bool CanShowInFolder => true;
|
|
|
|
public bool CanOpenUrl => true;
|
|
|
|
public bool CanNotify => true;
|
|
|
|
public bool SupportsAutostart => true;
|
|
|
|
public Task<string?> PickFolderAsync(string title, string? initialFolder, CancellationToken ct) =>
|
|
ui.InvokeAsync(() =>
|
|
{
|
|
var dialog = new OpenFolderDialog { Title = title, Multiselect = false };
|
|
if (!string.IsNullOrWhiteSpace(initialFolder) && Directory.Exists(initialFolder))
|
|
{
|
|
dialog.InitialDirectory = initialFolder;
|
|
}
|
|
|
|
var window = owner();
|
|
var chosen = window is { IsVisible: true } ? dialog.ShowDialog(window) : dialog.ShowDialog();
|
|
return chosen == true ? dialog.FolderName : null;
|
|
}).Task;
|
|
|
|
public Task<bool> ShowInFolderAsync(string fullPath, CancellationToken ct)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fullPath) || !Path.IsPathFullyQualified(fullPath) || fullPath.Contains('"', StringComparison.Ordinal) || !File.Exists(fullPath))
|
|
{
|
|
return Task.FromResult(false);
|
|
}
|
|
|
|
var explorer = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe");
|
|
try
|
|
{
|
|
using var process = Process.Start(new ProcessStartInfo(explorer, $"/select,\"{fullPath}\"") { UseShellExecute = false });
|
|
return Task.FromResult(process is not null);
|
|
}
|
|
catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidOperationException)
|
|
{
|
|
return Task.FromResult(false);
|
|
}
|
|
}
|
|
|
|
public Task<bool> OpenUrlAsync(Uri address, CancellationToken ct)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(address);
|
|
if (!address.IsAbsoluteUri || address.Scheme is not ("http" or "https"))
|
|
{
|
|
return Task.FromResult(false);
|
|
}
|
|
|
|
try
|
|
{
|
|
// The default browser of Alberto (never the browser of Febe), through the shell.
|
|
using var process = Process.Start(new ProcessStartInfo(address.AbsoluteUri) { UseShellExecute = true });
|
|
return Task.FromResult(true);
|
|
}
|
|
catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidOperationException)
|
|
{
|
|
return Task.FromResult(false);
|
|
}
|
|
}
|
|
|
|
public Task NotifyAsync(string title, string message, CancellationToken ct) =>
|
|
ui.InvokeAsync(() => tray()?.Notify(title, message)).Task;
|
|
|
|
public Task<bool> GetAutostartAsync(CancellationToken ct)
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(Autostart.RunKey, writable: false);
|
|
var value = key?.GetValue(Autostart.ValueNameFor(fixedDataFolder)) as string;
|
|
return Task.FromResult(Environment.ProcessPath is { } exe && Autostart.PointsTo(value, exe, fixedDataFolder));
|
|
}
|
|
|
|
public Task SetAutostartAsync(bool enabled, CancellationToken ct)
|
|
{
|
|
using var key = Registry.CurrentUser.CreateSubKey(Autostart.RunKey, writable: true);
|
|
if (enabled)
|
|
{
|
|
var exe = Environment.ProcessPath ?? throw new InvalidOperationException("Percorso di Febe.exe sconosciuto.");
|
|
key.SetValue(Autostart.ValueNameFor(fixedDataFolder), Autostart.Command(exe, fixedDataFolder), RegistryValueKind.String);
|
|
}
|
|
else
|
|
{
|
|
key.DeleteValue(Autostart.ValueNameFor(fixedDataFolder), throwOnMissingValue: false);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The Data folder in use after a move, and a move asked for the next start (<see cref="DataFolderMove"/>), under
|
|
/// <c>HKCU\Software\Febe</c>: values «CartellaDati» and «NuovaCartellaDati». The uninstaller reads the first to say
|
|
/// where the data are.
|
|
/// </summary>
|
|
internal sealed class RegistryLocationStore : IDataFolderLocationStore
|
|
{
|
|
public const string Key = @"Software\Febe";
|
|
|
|
public string? Current => Read("CartellaDati");
|
|
|
|
public string? Pending => Read("NuovaCartellaDati");
|
|
|
|
public void SetCurrent(string? folder) => Write("CartellaDati", folder);
|
|
|
|
public void SetPending(string? folder) => Write("NuovaCartellaDati", folder);
|
|
|
|
private static string? Read(string name)
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(Key, writable: false);
|
|
return key?.GetValue(name) is string value && !string.IsNullOrWhiteSpace(value) ? value : null;
|
|
}
|
|
|
|
private static void Write(string name, string? value)
|
|
{
|
|
using var key = Registry.CurrentUser.CreateSubKey(Key, writable: true);
|
|
if (value is null)
|
|
{
|
|
key.DeleteValue(name, throwOnMissingValue: false);
|
|
}
|
|
else
|
|
{
|
|
key.SetValue(name, value, RegistryValueKind.String);
|
|
}
|
|
}
|
|
}
|