using System.Diagnostics; using System.Windows; using System.Windows.Threading; using Febe.Desktop.Core; using Febe.Web.Native; using Microsoft.Win32; namespace Febe.Desktop; /// /// The native functions of Windows offered to the interface (spec §12): folder picker (), /// «Mostra nella cartella» (explorer.exe /select,"…"), 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 /// , and «Invia una prova») and start with the computer /// (HKCU\…\Run, value Febe, or Febe-… with --dati for a Febe started on a Data folder of /// its own: , see ). Paths and addresses never come from the /// page: the web layer resolves them. /// internal sealed class WindowsShell(Dispatcher ui, Func owner, Func 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 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 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 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 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; } } /// /// The Data folder in use after a move, and a move asked for the next start (), under /// HKCU\Software\Febe: values «CartellaDati» and «NuovaCartellaDati». The uninstaller reads the first to say /// where the data are. /// 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); } } }