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

116 lines
3.1 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;
namespace Aaru.Tui.ViewModels.Windows;
public sealed partial class ImageWindowViewModel : ViewModelBase
{
2025-10-16 11:43:03 +01:00
readonly string _filename;
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]
bool _isStatusVisible;
[ObservableProperty]
ObservableCollection<FileSystemModelNode> _nodes;
[ObservableProperty]
string? _status;
2025-10-16 11:03:35 +01:00
2025-10-16 11:57:23 +01:00
public ImageWindowViewModel(Window parent, Window view, IMediaImage imageFormat, string filename)
2025-10-16 11:03:35 +01:00
{
_imageFormat = imageFormat;
_filename = filename;
_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 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...";
Nodes = new ObservableCollection<FileSystemModelNode>();
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;
var fsNode = new FileSystemModelNode(fs.Name);
fsNode.Filesystem = fs;
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
}