Files
Aaru/Aaru.Gui/ViewModels/Panels/MediaInfoViewModel.cs

459 lines
18 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : MediaInfoViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the media information panel.
//
// --[ 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.Panels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;
2020-04-16 20:40:25 +01:00
using Aaru.Gui.ViewModels.Tabs;
using Aaru.Gui.ViewModels.Windows;
using Aaru.Gui.Views.Tabs;
using Aaru.Gui.Views.Windows;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using MessageBox.Avalonia;
using MessageBox.Avalonia.Enums;
using ReactiveUI;
2020-04-16 21:29:40 +01:00
using ScsiInfo = Aaru.Core.Media.Info.ScsiInfo;
2022-03-06 13:29:38 +00:00
public sealed class MediaInfoViewModel : ViewModelBase
{
2022-03-06 13:29:38 +00:00
readonly string _devicePath;
readonly ScsiInfo _scsiInfo;
readonly Window _view;
BlurayInfo _blurayInfo;
CompactDiscInfo _compactDiscInfo;
string _densitySupport;
DvdInfo _dvdInfo;
DvdWritableInfo _dvdWritableInfo;
string _generalVisible;
Bitmap _mediaLogo;
string _mediaSerial;
string _mediaSize;
string _mediaType;
string _mediumSupport;
bool _mmcVisible;
bool _saveDensitySupportVisible;
bool _saveGetConfigurationVisible;
bool _saveMediumSupportVisible;
bool _saveReadCapacity16Visible;
bool _saveReadCapacityVisible;
bool _saveReadMediaSerialVisible;
bool _saveRecognizedFormatLayersVisible;
bool _saveWriteProtectionStatusVisible;
bool _sscVisible;
XboxInfo _xboxInfo;
public MediaInfoViewModel(ScsiInfo scsiInfo, string devicePath, Window view)
{
2022-03-06 13:29:38 +00:00
_view = view;
SaveReadMediaSerialCommand = ReactiveCommand.Create(ExecuteSaveReadMediaSerialCommand);
SaveReadCapacityCommand = ReactiveCommand.Create(ExecuteSaveReadCapacityCommand);
SaveReadCapacity16Command = ReactiveCommand.Create(ExecuteSaveReadCapacity16Command);
SaveGetConfigurationCommand = ReactiveCommand.Create(ExecuteSaveGetConfigurationCommand);
SaveRecognizedFormatLayersCommand = ReactiveCommand.Create(ExecuteSaveRecognizedFormatLayersCommand);
SaveWriteProtectionStatusCommand = ReactiveCommand.Create(ExecuteSaveWriteProtectionStatusCommand);
SaveDensitySupportCommand = ReactiveCommand.Create(ExecuteSaveDensitySupportCommand);
SaveMediumSupportCommand = ReactiveCommand.Create(ExecuteSaveMediumSupportCommand);
DumpCommand = ReactiveCommand.Create(ExecuteDumpCommand);
ScanCommand = ReactiveCommand.Create(ExecuteScanCommand);
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
_devicePath = devicePath;
_scsiInfo = scsiInfo;
var mediaResource = new Uri($"avares://Aaru.Gui/Assets/Logos/Media/{scsiInfo.MediaType}.png");
2022-03-17 00:46:26 +00:00
MediaLogo = assets?.Exists(mediaResource) == true ? new Bitmap(assets.Open(mediaResource)) : null;
2022-03-06 13:29:38 +00:00
MediaType = scsiInfo.MediaType.ToString();
if(scsiInfo.Blocks != 0 &&
scsiInfo.BlockSize != 0)
{
2022-03-06 13:29:38 +00:00
ulong totalSize = scsiInfo.Blocks * scsiInfo.BlockSize;
2022-11-13 19:38:03 +00:00
switch(totalSize)
{
case > 1099511627776:
MediaSize = $"Media has {scsiInfo.Blocks} blocks of {scsiInfo.BlockSize
} bytes/each. (for a total of {totalSize / 1099511627776d:F3} TiB)";
break;
case > 1073741824:
MediaSize = $"Media has {scsiInfo.Blocks} blocks of {scsiInfo.BlockSize
} bytes/each. (for a total of {totalSize / 1073741824d:F3} GiB)";
break;
case > 1048576:
MediaSize = $"Media has {scsiInfo.Blocks} blocks of {scsiInfo.BlockSize
} bytes/each. (for a total of {totalSize / 1048576d:F3} MiB)";
break;
case > 1024:
MediaSize = $"Media has {scsiInfo.Blocks} blocks of {scsiInfo.BlockSize
} bytes/each. (for a total of {totalSize / 1024d:F3} KiB)";
break;
default:
MediaSize = $"Media has {scsiInfo.Blocks} blocks of {scsiInfo.BlockSize
} bytes/each. (for a total of {totalSize} bytes)";
break;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
if(scsiInfo.MediaSerialNumber != null)
{
var sbSerial = new StringBuilder();
2022-03-07 07:36:44 +00:00
for(var i = 4; i < scsiInfo.MediaSerialNumber.Length; i++)
2022-03-06 13:29:38 +00:00
sbSerial.AppendFormat("{0:X2}", scsiInfo.MediaSerialNumber[i]);
2022-03-06 13:29:38 +00:00
MediaSerial = sbSerial.ToString();
}
2022-03-06 13:29:38 +00:00
SaveReadMediaSerialVisible = scsiInfo.MediaSerialNumber != null;
SaveReadCapacityVisible = scsiInfo.ReadCapacity != null;
SaveReadCapacity16Visible = scsiInfo.ReadCapacity16 != null;
2022-03-06 13:29:38 +00:00
SaveGetConfigurationVisible = scsiInfo.MmcConfiguration != null;
SaveRecognizedFormatLayersVisible = scsiInfo.RecognizedFormatLayers != null;
SaveWriteProtectionStatusVisible = scsiInfo.WriteProtectionStatus != null;
2022-03-06 13:29:38 +00:00
MmcVisible = SaveGetConfigurationVisible || SaveRecognizedFormatLayersVisible ||
SaveWriteProtectionStatusVisible;
2022-03-06 13:29:38 +00:00
if(scsiInfo.DensitySupportHeader.HasValue)
DensitySupport = Decoders.SCSI.SSC.DensitySupport.PrettifyDensity(scsiInfo.DensitySupportHeader);
2022-03-06 13:29:38 +00:00
if(scsiInfo.MediaTypeSupportHeader.HasValue)
MediumSupport = Decoders.SCSI.SSC.DensitySupport.PrettifyMediumType(scsiInfo.MediaTypeSupportHeader);
2022-03-06 13:29:38 +00:00
SaveDensitySupportVisible = scsiInfo.DensitySupport != null;
SaveMediumSupportVisible = scsiInfo.MediaTypeSupport != null;
2022-03-06 13:29:38 +00:00
SscVisible = SaveDensitySupportVisible || SaveMediumSupportVisible;
2022-03-06 13:29:38 +00:00
CompactDiscInfo = new CompactDiscInfo
{
2022-03-06 13:29:38 +00:00
DataContext = new CompactDiscInfoViewModel(scsiInfo.Toc, scsiInfo.Atip, scsiInfo.DiscInformation,
scsiInfo.Session, scsiInfo.RawToc, scsiInfo.Pma,
2022-03-07 07:36:44 +00:00
scsiInfo.CdTextLeadIn, scsiInfo.DecodedToc, scsiInfo.DecodedAtip,
scsiInfo.DecodedSession, scsiInfo.FullToc,
scsiInfo.DecodedCdTextLeadIn, scsiInfo.DecodedDiscInformation,
scsiInfo.Mcn, scsiInfo.Isrcs, _view)
2022-03-06 13:29:38 +00:00
};
DvdInfo = new DvdInfo
{
2022-03-07 07:36:44 +00:00
DataContext = new DvdInfoViewModel(scsiInfo.MediaType, scsiInfo.DvdPfi, scsiInfo.DvdDmi, scsiInfo.DvdCmi,
scsiInfo.HddvdCopyrightInformation, scsiInfo.DvdBca, scsiInfo.DvdAacs,
scsiInfo.DecodedPfi, _view)
2022-03-06 13:29:38 +00:00
};
2022-03-06 13:29:38 +00:00
XboxInfo = new XboxInfo
{
2022-03-06 13:29:38 +00:00
DataContext = new XboxInfoViewModel(scsiInfo.XgdInfo, scsiInfo.DvdDmi, scsiInfo.XboxSecuritySector,
scsiInfo.DecodedXboxSecuritySector, _view)
};
2022-03-06 13:29:38 +00:00
DvdWritableInfo = new DvdWritableInfo
{
2022-03-06 13:29:38 +00:00
DataContext = new DvdWritableInfoViewModel(scsiInfo.MediaType, scsiInfo.DvdRamDds,
scsiInfo.DvdRamCartridgeStatus, scsiInfo.DvdRamSpareArea,
scsiInfo.LastBorderOutRmd, scsiInfo.DvdPreRecordedInfo,
2022-03-07 07:36:44 +00:00
scsiInfo.DvdrMediaIdentifier, scsiInfo.DvdrPhysicalInformation,
2022-03-06 13:29:38 +00:00
scsiInfo.HddvdrMediumStatus, scsiInfo.HddvdrLastRmd,
scsiInfo.DvdrLayerCapacity, scsiInfo.DvdrDlMiddleZoneStart,
scsiInfo.DvdrDlJumpIntervalSize,
scsiInfo.DvdrDlManualLayerJumpStartLba,
scsiInfo.DvdrDlRemapAnchorPoint, scsiInfo.DvdPlusAdip,
scsiInfo.DvdPlusDcb, _view)
};
BlurayInfo = new BlurayInfo
{
2022-03-06 13:29:38 +00:00
DataContext = new BlurayInfoViewModel(scsiInfo.BlurayDiscInformation, scsiInfo.BlurayBurstCuttingArea,
scsiInfo.BlurayDds, scsiInfo.BlurayCartridgeStatus,
scsiInfo.BluraySpareAreaInformation, scsiInfo.BlurayPowResources,
scsiInfo.BlurayTrackResources, scsiInfo.BlurayRawDfl,
scsiInfo.BlurayPac, _view)
};
}
public ReactiveCommand<Unit, Task> SaveReadMediaSerialCommand { get; }
public ReactiveCommand<Unit, Task> SaveReadCapacityCommand { get; }
public ReactiveCommand<Unit, Task> SaveReadCapacity16Command { get; }
public ReactiveCommand<Unit, Task> SaveGetConfigurationCommand { get; }
public ReactiveCommand<Unit, Task> SaveRecognizedFormatLayersCommand { get; }
public ReactiveCommand<Unit, Task> SaveWriteProtectionStatusCommand { get; }
public ReactiveCommand<Unit, Task> SaveDensitySupportCommand { get; }
public ReactiveCommand<Unit, Task> SaveMediumSupportCommand { get; }
public ReactiveCommand<Unit, Task> DumpCommand { get; }
public ReactiveCommand<Unit, Task> ScanCommand { get; }
2022-03-06 13:29:38 +00:00
public Bitmap MediaLogo
{
get => _mediaLogo;
set => this.RaiseAndSetIfChanged(ref _mediaLogo, value);
}
2022-03-06 13:29:38 +00:00
public string GeneralVisible
{
get => _generalVisible;
set => this.RaiseAndSetIfChanged(ref _generalVisible, value);
}
2022-03-06 13:29:38 +00:00
public string MediaType
{
get => _mediaType;
set => this.RaiseAndSetIfChanged(ref _mediaType, value);
}
2022-03-06 13:29:38 +00:00
public string MediaSize
{
get => _mediaSize;
set => this.RaiseAndSetIfChanged(ref _mediaSize, value);
}
2022-03-06 13:29:38 +00:00
public string MediaSerial
{
get => _mediaSerial;
set => this.RaiseAndSetIfChanged(ref _mediaSerial, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveReadMediaSerialVisible
{
get => _saveReadMediaSerialVisible;
set => this.RaiseAndSetIfChanged(ref _saveReadMediaSerialVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveReadCapacityVisible
{
get => _saveReadCapacityVisible;
set => this.RaiseAndSetIfChanged(ref _saveReadCapacityVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveReadCapacity16Visible
{
get => _saveReadCapacity16Visible;
set => this.RaiseAndSetIfChanged(ref _saveReadCapacity16Visible, value);
}
2022-03-06 13:29:38 +00:00
public bool MmcVisible
{
get => _mmcVisible;
set => this.RaiseAndSetIfChanged(ref _mmcVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveGetConfigurationVisible
{
get => _saveGetConfigurationVisible;
set => this.RaiseAndSetIfChanged(ref _saveGetConfigurationVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveRecognizedFormatLayersVisible
{
get => _saveRecognizedFormatLayersVisible;
set => this.RaiseAndSetIfChanged(ref _saveRecognizedFormatLayersVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveWriteProtectionStatusVisible
{
get => _saveWriteProtectionStatusVisible;
set => this.RaiseAndSetIfChanged(ref _saveWriteProtectionStatusVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SscVisible
{
get => _sscVisible;
set => this.RaiseAndSetIfChanged(ref _sscVisible, value);
}
2022-03-06 13:29:38 +00:00
public string DensitySupport
{
get => _densitySupport;
set => this.RaiseAndSetIfChanged(ref _densitySupport, value);
}
2022-03-06 13:29:38 +00:00
public string MediumSupport
{
get => _mediumSupport;
set => this.RaiseAndSetIfChanged(ref _mediumSupport, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveDensitySupportVisible
{
get => _saveDensitySupportVisible;
set => this.RaiseAndSetIfChanged(ref _saveDensitySupportVisible, value);
}
2022-03-06 13:29:38 +00:00
public bool SaveMediumSupportVisible
{
get => _saveMediumSupportVisible;
set => this.RaiseAndSetIfChanged(ref _saveMediumSupportVisible, value);
}
2022-03-06 13:29:38 +00:00
public CompactDiscInfo CompactDiscInfo
{
get => _compactDiscInfo;
set => this.RaiseAndSetIfChanged(ref _compactDiscInfo, value);
}
2022-03-06 13:29:38 +00:00
public DvdInfo DvdInfo
{
get => _dvdInfo;
set => this.RaiseAndSetIfChanged(ref _dvdInfo, value);
}
2022-03-06 13:29:38 +00:00
public DvdWritableInfo DvdWritableInfo
{
get => _dvdWritableInfo;
set => this.RaiseAndSetIfChanged(ref _dvdWritableInfo, value);
}
2022-03-06 13:29:38 +00:00
public XboxInfo XboxInfo
{
get => _xboxInfo;
set => this.RaiseAndSetIfChanged(ref _xboxInfo, value);
}
2022-03-06 13:29:38 +00:00
public BlurayInfo BlurayInfo
{
get => _blurayInfo;
set => this.RaiseAndSetIfChanged(ref _blurayInfo, value);
}
async Task SaveElement(byte[] data)
2022-03-06 13:29:38 +00:00
{
var dlgSaveBinary = new SaveFileDialog();
2022-03-06 13:29:38 +00:00
dlgSaveBinary.Filters.Add(new FileDialogFilter
{
Extensions = new List<string>(new[]
{
"*.bin"
}),
Name = "Binary"
});
2022-03-06 13:29:38 +00:00
string result = await dlgSaveBinary.ShowAsync(_view);
2022-03-06 13:29:38 +00:00
if(result is null)
return;
2022-03-06 13:29:38 +00:00
var saveFs = new FileStream(result, FileMode.Create);
saveFs.Write(data, 0, data.Length);
2022-03-06 13:29:38 +00:00
saveFs.Close();
}
async Task ExecuteSaveReadMediaSerialCommand() => await SaveElement(_scsiInfo.MediaSerialNumber);
async Task ExecuteSaveReadCapacityCommand() => await SaveElement(_scsiInfo.ReadCapacity);
async Task ExecuteSaveReadCapacity16Command() => await SaveElement(_scsiInfo.ReadCapacity16);
async Task ExecuteSaveGetConfigurationCommand() => await SaveElement(_scsiInfo.MmcConfiguration);
async Task ExecuteSaveRecognizedFormatLayersCommand() => await SaveElement(_scsiInfo.RecognizedFormatLayers);
async Task ExecuteSaveWriteProtectionStatusCommand() => await SaveElement(_scsiInfo.WriteProtectionStatus);
async Task ExecuteSaveDensitySupportCommand() => await SaveElement(_scsiInfo.DensitySupport);
async Task ExecuteSaveMediumSupportCommand() => await SaveElement(_scsiInfo.MediaTypeSupport);
async Task ExecuteDumpCommand()
2022-03-06 13:29:38 +00:00
{
2022-11-13 19:38:03 +00:00
switch(_scsiInfo.MediaType)
{
2022-11-13 19:38:03 +00:00
case CommonTypes.MediaType.GDR or CommonTypes.MediaType.GDROM:
await MessageBoxManager.
GetMessageBoxStandardWindow("Error", "GD-ROM dump support is not yet implemented.", ButtonEnum.Ok,
Icon.Error).ShowDialog(_view);
return;
case CommonTypes.MediaType.XGD or CommonTypes.MediaType.XGD2 or CommonTypes.MediaType.XGD3
when _scsiInfo.DeviceInfo.ScsiInquiry?.KreonPresent != true:
await MessageBoxManager.
GetMessageBoxStandardWindow("Error", "Dumping Xbox discs require a Kreon drive.", ButtonEnum.Ok,
Icon.Error).ShowDialog(_view);
return;
2022-03-06 13:29:38 +00:00
}
var mediaDumpWindow = new MediaDump();
2022-03-06 13:29:38 +00:00
mediaDumpWindow.DataContext =
new MediaDumpViewModel(_devicePath, _scsiInfo.DeviceInfo, mediaDumpWindow, _scsiInfo);
2022-03-06 13:29:38 +00:00
mediaDumpWindow.Show();
}
async Task ExecuteScanCommand()
2022-03-06 13:29:38 +00:00
{
switch(_scsiInfo.MediaType)
{
// TODO: GD-ROM
case CommonTypes.MediaType.GDR:
case CommonTypes.MediaType.GDROM:
await MessageBoxManager.
2022-03-07 07:36:44 +00:00
GetMessageBoxStandardWindow("Error", "GD-ROM scan support is not yet implemented.", ButtonEnum.Ok,
Icon.Error).ShowDialog(_view);
2022-03-06 13:29:38 +00:00
return;
2022-03-06 13:29:38 +00:00
// TODO: Xbox
case CommonTypes.MediaType.XGD:
case CommonTypes.MediaType.XGD2:
case CommonTypes.MediaType.XGD3:
await MessageBoxManager.
2022-03-07 07:36:44 +00:00
GetMessageBoxStandardWindow("Error", "Scanning Xbox discs is not yet supported.", ButtonEnum.Ok,
Icon.Error).ShowDialog(_view);
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
var mediaScanWindow = new MediaScan();
mediaScanWindow.DataContext = new MediaScanViewModel(_devicePath, mediaScanWindow);
mediaScanWindow.Show();
}
}