using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Input; using Chronos.App.About; using Chronos.App.Infrastructure; using Chronos.App.Shell; using Chronos.Core.Localization; using Chronos.Ui.Controls; using Chronos.Ui.Mvvm; namespace Chronos.App.Sections; /// /// About (BRIEF §12.9): version and build, licence link, third-party notice (none), translation notice, /// diagnostics bundle and the update notice of (BRIEF §18), which Settings › Updates shows too. /// public sealed class AboutViewModel : SectionViewModel { private readonly UpdateFlow _update; public AboutViewModel(ShellViewModel shell) : base(SectionKey.About, "Icon.About") { Shell = shell; var environment = shell.Services.Environment; _update = new UpdateFlow(new UpdateHost(UpdateCheck.RunningVersion(Version), environment.IsCapture, environment.IsPortable, ct => Shell.Services.CheckForUpdateAsync(ct), (release, progress, ct) => Shell.Services.DownloadUpdateAsync(release, progress, ct), ShellActions.OpenUrl, ShellActions.Reveal) { RunningIsPrerelease = UpdateCheck.IsPrerelease(Version) }); _update.Changed += RaiseUpdate; ShowPackageCommand = new RelayCommand(() => { if (_update.PackagePath is { } path) ShellActions.Reveal(path); }, () => _update.PackagePath is not null); CancelDownloadCommand = new RelayCommand(_update.CancelDownload, () => IsDownloading); OpenReleasesCommand = new RelayCommand(() => ShellActions.OpenUrl(ProductLinks.Releases)); OpenLicenceCommand = new RelayCommand(() => ShellActions.OpenUrl(ProductLinks.Licence)); ReviewTranslationCommand = new RelayCommand(() => ShellActions.OpenUrl(ProductLinks.Issues)); OpenDataFolderCommand = new RelayCommand(() => ShellActions.Reveal(DataFolder)); DiagnosticsCommand = new AsyncRelayCommand(DiagnosticsAsync); } public ShellViewModel Shell { get; } public string Version => AppServices.VersionText; /// File version and processor architecture. public string Build => (typeof(App).Assembly.GetCustomAttribute()?.Version ?? Version) + Loc.Separator + RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(); public string Runtime => RuntimeInformation.FrameworkDescription; public string OperatingSystem => RuntimeInformation.OSDescription; public string DataFolder => Shell.Services.Environment.DataDirectory; /// Every language but English is machine-translated and open to review (BRIEF §16). public bool IsTranslated => !Loc.Culture.TwoLetterISOLanguageName.Equals("en", StringComparison.OrdinalIgnoreCase); public ICommand CheckCommand => _update.Check; public ICommand DownloadCommand => _update.Download; public ICommand ShowPackageCommand { get; } public ICommand CancelDownloadCommand { get; } public ICommand OpenReleasesCommand { get; } public ICommand OpenLicenceCommand { get; } public ICommand ReviewTranslationCommand { get; } public ICommand OpenDataFolderCommand { get; } public ICommand DiagnosticsCommand { get; } /// "Up to date", "Chronos 0.2.0 available", …; null before the first check. public string? UpdateText => _update.State switch { UpdateState.Checking => Loc.L("About.Update.Checking"), UpdateState.UpToDate => Loc.L("About.Update.Current"), UpdateState.Available when _update.Release is { } r => Loc.L("About.Update.Available", UpdateCheck.Display(r.Version)), UpdateState.Failed => Loc.L("About.Update.Failed"), UpdateState.Downloading => Loc.L("About.Update.Downloading", _update.Downloaded), UpdateState.Downloaded when _update.Release is { } r => Loc.L("About.Update.Ready", UpdateCheck.Display(r.Version)), UpdateState.DownloadFailed => Loc.L("About.Update.DownloadFailed"), UpdateState.NotVerified => Loc.L("About.Update.NotVerified"), _ => null, }; /// What screen readers announce: each new state, not each percent of a download. public string? UpdateAnnouncement => _update.State == UpdateState.Downloading ? Loc.L("About.Update.InProgress") : UpdateText; public StatusKind UpdateKind => _update.State switch { UpdateState.Checking or UpdateState.Downloading => StatusKind.Running, UpdateState.UpToDate or UpdateState.Downloaded => StatusKind.Done, UpdateState.Available => StatusKind.Warning, _ => StatusKind.Error, }; public bool HasUpdateText => UpdateText is not null; /// Download is offered: a newer release exists and no verified package is waiting (a failed download can be retried). public bool IsUpdateAvailable => _update.IsDownloadOffered; /// The verified setup or portable zip is in the temporary folder. public bool IsPackageReady => _update.State == UpdateState.Downloaded; public bool IsDownloading => _update.State == UpdateState.Downloading; /// What Download does here: fetch and verify the setup or the portable zip (with its size), or open the release page. public string DownloadToolTip => _update.Package is { } package ? Loc.L(_update.IsPortable ? "Tooltips.About.DownloadPortable" : "Tooltips.About.Download", Formatting.Bytes(package.Size)) : Loc.L("Tooltips.About.DownloadPage"); public string ShowPackageToolTip => Loc.L(_update.IsPortable ? "Tooltips.About.ShowPortable" : "Tooltips.About.ShowSetup"); /// Starts the update check on start when allowed (opt-out, BRIEF §18); never in a capture run. public void Attach() => _ = NotifyAsync(); /// Sets a result without the network (capture, tests). internal void SetResult(ReleaseInfo? release, bool failed = false) => _update.SetResult(release, failed); private async Task NotifyAsync() { if (await _update.CheckOnStartAsync(Shell.Services.Settings.CheckForUpdates) is not { } newer) return; Shell.Notify(Loc.L("About.Update.Available", UpdateCheck.Display(newer.Version)), actionText: Loc.L("About.Update.Download"), actionToolTip: DownloadToolTip, action: () => DownloadCommand.Execute(null)); } private async Task DiagnosticsAsync() { var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Settings.Advanced.DiagnosticsTitle")); if (folder is null) return; try { var path = await DiagnosticsBundle.CreateAsync(Shell.Services, folder); Shell.Notify(Loc.L("Settings.Advanced.DiagnosticsDone"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path)); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or Chronos.Data.DatabaseException) { Shell.ReportError(ex); } } private void RaiseUpdate() { OnPropertyChanged(nameof(UpdateText)); OnPropertyChanged(nameof(UpdateAnnouncement)); OnPropertyChanged(nameof(UpdateKind)); OnPropertyChanged(nameof(HasUpdateText)); OnPropertyChanged(nameof(IsUpdateAvailable)); OnPropertyChanged(nameof(IsPackageReady)); OnPropertyChanged(nameof(IsDownloading)); OnPropertyChanged(nameof(DownloadToolTip)); CommandManager.InvalidateRequerySuggested(); } }