Files
Aaru/Aaru.Tui/ViewModels/Windows/ImageWindowViewModel.cs

183 lines
5.6 KiB
C#
Raw Normal View History

2025-10-16 11:43:03 +01:00
using System.Collections.ObjectModel;
2025-10-16 11:03:35 +01:00
using System.Windows.Input;
2025-10-16 11:43:03 +01:00
using Aaru.CommonTypes;
2025-10-16 11:03:35 +01:00
using Aaru.CommonTypes.Interfaces;
2025-10-16 11:43:03 +01:00
using Aaru.Tui.Models;
2025-10-16 11:03:35 +01:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
2025-10-16 11:43:03 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
2025-10-16 11:03:35 +01:00
using CommunityToolkit.Mvvm.Input;
using Humanizer;
using Humanizer.Bytes;
2025-10-16 11:03:35 +01:00
namespace Aaru.Tui.ViewModels.Windows;
public sealed partial class ImageWindowViewModel : ViewModelBase
{
2025-10-16 11:43:03 +01:00
readonly IMediaImage _imageFormat;
2025-10-16 11:57:23 +01:00
readonly Window _parent;
2025-10-16 11:43:03 +01:00
readonly Window _view;
[ObservableProperty]
2025-10-16 12:06:45 +01:00
public string _filePath;
[ObservableProperty]
bool _isPartitionInformationVisible;
[ObservableProperty]
2025-10-16 11:43:03 +01:00
bool _isStatusVisible;
[ObservableProperty]
ObservableCollection<FileSystemModelNode> _nodes;
[ObservableProperty]
string _partitionDescription;
[ObservableProperty]
string _partitionLength;
[ObservableProperty]
string _partitionName;
[ObservableProperty]
string _partitionOffset;
[ObservableProperty]
string _partitionScheme;
[ObservableProperty]
string _partitionSequence;
[ObservableProperty]
string _partitionSize;
[ObservableProperty]
string _partitionStart;
[ObservableProperty]
string _partitionType;
FileSystemModelNode? _selectedNode;
[ObservableProperty]
2025-10-16 11:43:03 +01:00
string? _status;
2025-10-16 11:03:35 +01:00
2025-10-16 12:06:45 +01:00
public ImageWindowViewModel(Window parent, Window view, IMediaImage imageFormat, string filePath)
2025-10-16 11:03:35 +01:00
{
_imageFormat = imageFormat;
2025-10-16 12:06:45 +01:00
FilePath = filePath;
2025-10-16 11:03:35 +01:00
_view = view;
2025-10-16 11:57:23 +01:00
_parent = parent;
2025-10-16 11:03:35 +01:00
ExitCommand = new RelayCommand(Exit);
BackCommand = new RelayCommand(Back);
}
public FileSystemModelNode? SelectedNode
{
get => _selectedNode;
set
{
SetProperty(ref _selectedNode, value);
if(_selectedNode is null) return;
if(_selectedNode.Partition is not null)
{
IsPartitionInformationVisible = true;
PartitionSequence = _selectedNode.Partition.Value.Sequence.ToString();
PartitionName = _selectedNode.Partition.Value.Name;
PartitionType = _selectedNode.Partition.Value.Type;
PartitionStart = _selectedNode.Partition.Value.Start.ToString();
PartitionOffset = ByteSize.FromBytes(_selectedNode.Partition.Value.Offset).Humanize();
PartitionLength = $"{_selectedNode.Partition.Value.Length} sectors";
PartitionSize = ByteSize.FromBytes(_selectedNode.Partition.Value.Size).Humanize();
PartitionScheme = _selectedNode.Partition.Value.Scheme;
PartitionDescription = _selectedNode.Partition.Value.Description;
OnPropertyChanged(nameof(PartitionSequence));
OnPropertyChanged(nameof(PartitionName));
OnPropertyChanged(nameof(PartitionType));
OnPropertyChanged(nameof(PartitionStart));
OnPropertyChanged(nameof(PartitionOffset));
OnPropertyChanged(nameof(PartitionLength));
OnPropertyChanged(nameof(PartitionSize));
OnPropertyChanged(nameof(PartitionScheme));
OnPropertyChanged(nameof(PartitionDescription));
}
else
IsPartitionInformationVisible = false;
OnPropertyChanged(nameof(IsPartitionInformationVisible));
}
}
2025-10-16 11:03:35 +01:00
public ICommand BackCommand { get; }
public ICommand ExitCommand { get; }
void Back()
{
2025-10-16 11:57:23 +01:00
_parent.Show();
2025-10-16 11:03:35 +01:00
_view.Close();
}
void Exit()
{
var lifetime = Application.Current!.ApplicationLifetime as IControlledApplicationLifetime;
lifetime!.Shutdown();
}
2025-10-16 11:43:03 +01:00
public void LoadComplete()
{
_ = Task.Run(Worker);
}
void Worker()
{
IsStatusVisible = true;
Status = "Loading partitions...";
2025-10-16 12:06:45 +01:00
Nodes = [];
2025-10-16 11:43:03 +01:00
List<Partition>? partitionsList = Core.Partitions.GetAll(_imageFormat);
if(partitionsList.Count == 0)
{
partitionsList.Add(new Partition
{
Name = Localization.Core.Whole_device,
Length = _imageFormat.Info.Sectors,
Size = _imageFormat.Info.Sectors * _imageFormat.Info.SectorSize
});
}
var sequence = 0;
2025-10-16 11:57:23 +01:00
Status = "Loading filesystems...";
PluginRegister plugins = PluginRegister.Singleton;
2025-10-16 11:43:03 +01:00
foreach(Partition partition in partitionsList)
{
var node = new FileSystemModelNode(partition.Name ?? $"Partition {sequence}")
{
Partition = partition
};
2025-10-16 11:57:23 +01:00
Core.Filesystems.Identify(_imageFormat, out List<string>? idPlugins, partition);
if(idPlugins.Count > 0)
{
var subNodes = new ObservableCollection<FileSystemModelNode>();
foreach(string pluginName in idPlugins)
{
if(!plugins.Filesystems.TryGetValue(pluginName, out IFilesystem? fs)) continue;
if(fs is null) continue;
2025-10-16 12:06:45 +01:00
var fsNode = new FileSystemModelNode(fs.Name)
{
Filesystem = fs
};
2025-10-16 11:57:23 +01:00
subNodes.Add(fsNode);
}
node.SubNodes = subNodes;
}
2025-10-16 11:43:03 +01:00
Nodes.Add(node);
sequence++;
}
Status = "Done.";
IsStatusVisible = false;
}
2025-10-16 11:03:35 +01:00
}