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

356 lines
11 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Gui.ViewModels.Windows;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reactive;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
using Aaru.Core;
using Avalonia.Controls;
using Avalonia.Threading;
using ReactiveUI;
using Schemas;
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,
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) + ".cicm.xml");
DestinationEnabled = true;
StartVisible = true;
CloseVisible = true;
DestinationCommand = ReactiveCommand.Create(ExecuteDestinationCommand);
StartCommand = ReactiveCommand.Create(ExecuteStartCommand);
CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand);
StopCommand = ReactiveCommand.Create(ExecuteStopCommand);
}
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;
CICMMetadataType sidecar = _sidecarClass.Create();
2022-03-06 13:29:38 +00:00
AaruConsole.WriteLine("Writing metadata sidecar");
2022-03-06 13:29:38 +00:00
var xmlFs = new FileStream(DestinationText, FileMode.Create);
2022-03-06 13:29:38 +00:00
var xmlSer = new XmlSerializer(typeof(CICMMetadataType));
xmlSer.Serialize(xmlFs, sidecar);
xmlFs.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")]
2022-03-06 13:29:38 +00:00
async void EndProgress2() => await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress2Visible = false;
});
[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")]
2022-03-06 13:29:38 +00:00
async void InitProgress2() => await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress2Visible = true;
});
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2022-03-06 13:29:38 +00: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")]
2022-03-06 13:29:38 +00:00
async void InitProgress() => await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress1Visible = true;
});
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2022-03-06 13:29:38 +00: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 = "Aborting...";
StopEnabled = false;
_sidecarClass.Abort();
}
async Task ExecuteDestinationCommand()
2022-03-06 13:29:38 +00:00
{
var dlgDestination = new SaveFileDialog
{
Title = "Choose destination file"
};
2022-11-15 01:35:06 +00:00
dlgDestination.Filters?.Add(new FileDialogFilter
2022-03-06 13:29:38 +00:00
{
Name = "CICM XML metadata",
Extensions = new List<string>(new[]
{
2022-03-06 13:29:38 +00:00
"*.xml"
})
});
2022-03-06 13:29:38 +00:00
string result = await dlgDestination.ShowAsync(_view);
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
if(string.IsNullOrEmpty(Path.GetExtension(result)))
result += ".xml";
DestinationText = result;
}
}