Files
Aaru/Aaru.Gui/ViewModels/Windows/ImageSidecarViewModel.cs

346 lines
12 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ImageSidecarViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the image sidecar creation window.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reactive;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Aaru.CommonTypes.AaruMetadata;
using Aaru.CommonTypes.Interfaces;
using Aaru.Core;
using Aaru.Localization;
using Aaru.Logging;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using ReactiveUI;
namespace Aaru.Gui.ViewModels.Windows;
2022-03-06 13:29:38 +00:00
public sealed class ImageSidecarViewModel : ViewModelBase
{
2022-03-06 13:29:38 +00:00
readonly Encoding _encoding;
readonly Guid _filterId;
readonly string _imageSource;
readonly IMediaImage _inputFormat;
readonly Window _view;
bool _closeVisible;
bool _destinationEnabled;
string _destinationText;
bool _progress1Visible;
bool _progress2Indeterminate;
double _progress2MaxValue;
string _progress2Text;
double _progress2Value;
bool _progress2Visible;
bool _progressIndeterminate;
double _progressMaxValue;
string _progressText;
double _progressValue;
bool _progressVisible;
Sidecar _sidecarClass;
bool _startVisible;
string _statusText;
bool _statusVisible;
bool _stopEnabled;
bool _stopVisible;
public ImageSidecarViewModel(IMediaImage inputFormat, string imageSource, Guid filterId, Encoding encoding,
2023-10-03 23:27:57 +01:00
Window view)
{
2022-03-06 13:29:38 +00:00
_view = view;
_inputFormat = inputFormat;
_imageSource = imageSource;
_filterId = filterId;
_encoding = encoding;
DestinationText = Path.Combine(Path.GetDirectoryName(imageSource) ?? "",
Path.GetFileNameWithoutExtension(imageSource) + ".metadata.json");
2022-03-06 13:29:38 +00:00
DestinationEnabled = true;
StartVisible = true;
CloseVisible = true;
DestinationCommand = ReactiveCommand.Create(ExecuteDestinationCommand);
StartCommand = ReactiveCommand.Create(ExecuteStartCommand);
CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand);
StopCommand = ReactiveCommand.Create(ExecuteStopCommand);
}
public string DestinationFileLabel => UI.Title_Destination_file;
public string ChooseLabel => UI.ButtonLabel_Choose;
public string StartLabel => UI.ButtonLabel_Start;
public string CloseLabel => UI.ButtonLabel_Close;
public string StopLabel => UI.ButtonLabel_Stop;
2022-03-06 13:29:38 +00:00
public string Title { get; }
public ReactiveCommand<Unit, Task> DestinationCommand { get; }
2022-03-06 13:29:38 +00:00
public ReactiveCommand<Unit, Unit> StartCommand { get; }
public ReactiveCommand<Unit, Unit> CloseCommand { get; }
public ReactiveCommand<Unit, Unit> StopCommand { get; }
2022-03-06 13:29:38 +00:00
public bool ProgressIndeterminate
{
get => _progressIndeterminate;
set => this.RaiseAndSetIfChanged(ref _progressIndeterminate, value);
}
2022-03-06 13:29:38 +00:00
public bool ProgressVisible
{
get => _progressVisible;
set => this.RaiseAndSetIfChanged(ref _progressVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool Progress1Visible
{
get => _progress1Visible;
set => this.RaiseAndSetIfChanged(ref _progress1Visible, value);
}
2022-03-06 13:29:38 +00:00
public bool Progress2Visible
{
get => _progress2Visible;
set => this.RaiseAndSetIfChanged(ref _progress2Visible, value);
}
2022-03-06 13:29:38 +00:00
public bool Progress2Indeterminate
{
get => _progress2Indeterminate;
set => this.RaiseAndSetIfChanged(ref _progress2Indeterminate, value);
}
2022-03-06 13:29:38 +00:00
public double ProgressMaxValue
{
get => _progressMaxValue;
set => this.RaiseAndSetIfChanged(ref _progressMaxValue, value);
}
2022-03-06 13:29:38 +00:00
public double Progress2MaxValue
{
get => _progress2MaxValue;
set => this.RaiseAndSetIfChanged(ref _progress2MaxValue, value);
}
2022-03-06 13:29:38 +00:00
public string ProgressText
{
get => _progressText;
set => this.RaiseAndSetIfChanged(ref _progressText, value);
}
2022-03-06 13:29:38 +00:00
public double ProgressValue
{
get => _progressValue;
set => this.RaiseAndSetIfChanged(ref _progressValue, value);
}
2022-03-06 13:29:38 +00:00
public double Progress2Value
{
get => _progress2Value;
set => this.RaiseAndSetIfChanged(ref _progress2Value, value);
}
2022-03-06 13:29:38 +00:00
public string Progress2Text
{
get => _progress2Text;
set => this.RaiseAndSetIfChanged(ref _progress2Text, value);
}
2022-03-06 13:29:38 +00:00
public string DestinationText
{
get => _destinationText;
set => this.RaiseAndSetIfChanged(ref _destinationText, value);
}
2022-03-06 13:29:38 +00:00
public bool DestinationEnabled
{
get => _destinationEnabled;
set => this.RaiseAndSetIfChanged(ref _destinationEnabled, value);
}
2022-03-06 13:29:38 +00:00
public string StatusText
{
get => _statusText;
set => this.RaiseAndSetIfChanged(ref _statusText, value);
}
2022-03-06 13:29:38 +00:00
public bool StatusVisible
{
get => _statusVisible;
set => this.RaiseAndSetIfChanged(ref _statusVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool StartVisible
{
get => _startVisible;
set => this.RaiseAndSetIfChanged(ref _startVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool CloseVisible
{
get => _closeVisible;
set => this.RaiseAndSetIfChanged(ref _closeVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool StopEnabled
{
get => _stopEnabled;
set => this.RaiseAndSetIfChanged(ref _stopEnabled, value);
}
2022-03-06 13:29:38 +00:00
public bool StopVisible
{
get => _stopVisible;
set => this.RaiseAndSetIfChanged(ref _stopVisible, value);
}
2022-03-06 13:29:38 +00:00
void ExecuteStartCommand() => new Thread(DoWork).Start();
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2022-03-06 13:29:38 +00:00
async void DoWork()
{
// Prepare UI
await Dispatcher.UIThread.InvokeAsync(() =>
{
2022-03-06 13:29:38 +00:00
CloseVisible = false;
StartVisible = false;
StopVisible = true;
StopEnabled = true;
ProgressVisible = true;
DestinationEnabled = false;
StatusVisible = true;
});
2022-03-06 13:29:38 +00:00
_sidecarClass = new Sidecar(_inputFormat, _imageSource, _filterId, _encoding);
_sidecarClass.UpdateStatusEvent += UpdateStatus;
_sidecarClass.InitProgressEvent += InitProgress;
_sidecarClass.UpdateProgressEvent += UpdateProgress;
_sidecarClass.EndProgressEvent += EndProgress;
_sidecarClass.InitProgressEvent2 += InitProgress2;
_sidecarClass.UpdateProgressEvent2 += UpdateProgress2;
_sidecarClass.EndProgressEvent2 += EndProgress2;
Metadata sidecar = _sidecarClass.Create();
2025-08-17 06:11:22 +01:00
AaruLogging.WriteLine(Localization.Core.Writing_metadata_sidecar);
var jsonFs = new FileStream(DestinationText, FileMode.Create);
2024-05-01 04:05:22 +01:00
await JsonSerializer.SerializeAsync(jsonFs,
new MetadataJson
{
AaruMetadata = sidecar
},
typeof(MetadataJson),
MetadataJsonContext.Default);
jsonFs.Close();
2022-03-06 13:29:38 +00:00
await Dispatcher.UIThread.InvokeAsync(() =>
{
2022-03-06 13:29:38 +00:00
CloseVisible = true;
StopVisible = false;
ProgressVisible = false;
StatusVisible = false;
});
2022-03-06 13:29:38 +00:00
Statistics.AddCommand("create-sidecar");
}
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2023-10-03 23:27:57 +01:00
async void EndProgress2() => await Dispatcher.UIThread.InvokeAsync(() => { Progress2Visible = false; });
2022-03-06 13:29:38 +00:00
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2022-03-07 07:36:44 +00:00
async void UpdateProgress2(string text, long current, long maximum) => await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress2Text = text;
Progress2Indeterminate = false;
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
Progress2MaxValue = maximum;
Progress2Value = current;
});
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2023-10-03 23:27:57 +01:00
async void InitProgress2() => await Dispatcher.UIThread.InvokeAsync(() => { Progress2Visible = true; });
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2023-10-03 23:27:57 +01:00
async void EndProgress() => await Dispatcher.UIThread.InvokeAsync(() => { Progress1Visible = false; });
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2022-03-07 07:36:44 +00:00
async void UpdateProgress(string text, long current, long maximum) => await Dispatcher.UIThread.InvokeAsync(() =>
{
ProgressText = text;
ProgressIndeterminate = false;
2022-03-07 07:36:44 +00:00
ProgressMaxValue = maximum;
ProgressValue = current;
});
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2023-10-03 23:27:57 +01:00
async void InitProgress() => await Dispatcher.UIThread.InvokeAsync(() => { Progress1Visible = true; });
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2023-10-03 23:27:57 +01:00
async void UpdateStatus(string text) => await Dispatcher.UIThread.InvokeAsync(() => { StatusText = text; });
2022-03-06 13:29:38 +00:00
void ExecuteCloseCommand() => _view.Close();
2022-03-06 13:29:38 +00:00
void ExecuteStopCommand()
{
ProgressText = Localization.Core.Aborting;
2022-03-06 13:29:38 +00:00
StopEnabled = false;
_sidecarClass.Abort();
}
async Task ExecuteDestinationCommand()
2022-03-06 13:29:38 +00:00
{
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
2022-03-06 13:29:38 +00:00
{
Title = UI.Dialog_Choose_destination_file,
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.AaruMetadata
}
2022-03-06 13:29:38 +00:00
});
2022-03-06 13:29:38 +00:00
if(result is null)
{
DestinationText = "";
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
DestinationText = result.Path.AbsolutePath;
if(string.IsNullOrEmpty(Path.GetExtension(DestinationText))) DestinationText += ".json";
}
}