Files
Aaru/Aaru.Gui/ViewModels/Tabs/AtaInfoViewModel.cs

167 lines
6.1 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : AtaInfoViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the ATA information tab.
//
// --[ 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-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
using Aaru.Decoders.ATA;
using Aaru.Localization;
using Avalonia.Controls;
2020-07-22 13:20:25 +01:00
using JetBrains.Annotations;
using ReactiveUI;
namespace Aaru.Gui.ViewModels.Tabs;
2022-03-06 13:29:38 +00:00
public sealed class AtaInfoViewModel : ViewModelBase
{
2022-03-06 13:29:38 +00:00
readonly byte[] _ata;
readonly byte[] _atapi;
readonly Window _view;
2022-03-07 07:36:44 +00:00
public AtaInfoViewModel([CanBeNull] byte[] ataIdentify, byte[] atapiIdentify, AtaErrorRegistersChs? ataMcptError,
2023-10-03 23:27:57 +01:00
Window view)
{
2022-03-06 13:29:38 +00:00
SaveAtaBinaryCommand = ReactiveCommand.Create(ExecuteSaveAtaBinaryCommand);
SaveAtaTextCommand = ReactiveCommand.Create(ExecuteSaveAtaTextCommand);
2022-03-06 13:29:38 +00:00
_ata = ataIdentify;
_atapi = atapiIdentify;
_view = view;
2024-05-01 04:05:22 +01:00
if(ataIdentify == null && atapiIdentify == null) return;
2022-03-06 13:29:38 +00:00
if(ataIdentify != null)
{
AtaMcptVisible = true;
AtaMcptChecked = ataMcptError.HasValue;
AtaOrAtapiText = "ATA IDENTIFY DEVICE";
2022-03-06 13:29:38 +00:00
if(ataMcptError.HasValue)
{
2022-11-13 19:59:24 +00:00
AtaMcptText = (ataMcptError.Value.DeviceHead & 0x7) switch
2023-10-03 23:27:57 +01:00
{
0 => Localization.Core.Device_reports_incorrect_media_card_type,
1 => Localization.Core.Device_contains_SD_card,
2 => Localization.Core.Device_contains_MMC,
3 => Localization.Core.Device_contains_SDIO_card,
4 => Localization.Core.Device_contains_SM_card,
_ => string.Format(Localization.Core.Device_contains_unknown_media_card_type_0,
ataMcptError.Value.DeviceHead & 0x07)
};
2022-03-06 13:29:38 +00:00
AtaMcptWriteProtectionChecked = (ataMcptError.Value.DeviceHead & 0x08) == 0x08;
var specificData = (ushort)(ataMcptError.Value.CylinderHigh * 0x100 + ataMcptError.Value.CylinderLow);
AtaMcptSpecificDataText = string.Format(Localization.Core.Card_specific_data_0, specificData);
}
2022-03-06 13:29:38 +00:00
AtaIdentifyText = Identify.Prettify(_ata);
}
else
{
AtaOrAtapiText = "ATA PACKET IDENTIFY DEVICE";
AtaIdentifyText = Identify.Prettify(_atapi);
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
public string AtaIdentifyText { get; }
public string AtaMcptText { get; }
public string AtaMcptSpecificDataText { get; }
public bool AtaMcptChecked { get; }
public bool AtaMcptWriteProtectionChecked { get; }
public bool AtaMcptVisible { get; }
public ReactiveCommand<Unit, Task> SaveAtaBinaryCommand { get; }
public ReactiveCommand<Unit, Task> SaveAtaTextCommand { get; }
2022-03-06 13:29:38 +00:00
public string AtaOrAtapiText { get; }
public string AtaMcptLabel => Localization.Core.Device_supports_MCPT_Command_Set;
public string AtaMcptWriteProtectionLabel => Localization.Core.Media_card_is_write_protected;
public string SaveAtaBinaryLabel => UI.ButtonLabel_Save_binary_to_file;
public string SaveAtaTextLabel => UI.ButtonLabel_Save_text_to_file;
async Task ExecuteSaveAtaBinaryCommand()
2022-03-06 13:29:38 +00:00
{
var dlgSaveBinary = new SaveFileDialog();
2022-11-15 01:35:06 +00:00
dlgSaveBinary.Filters?.Add(new FileDialogFilter
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:39:38 +01:00
Extensions =
[
..new[]
{
"*.bin"
}
],
Name = UI.Dialog_Binary_files
2022-03-06 13:29:38 +00:00
});
2022-03-06 13:29:38 +00:00
string result = await dlgSaveBinary.ShowAsync(_view);
2024-05-01 04:05:22 +01:00
if(result is null) return;
2022-03-06 13:29:38 +00:00
var saveFs = new FileStream(result, FileMode.Create);
2022-03-06 13:29:38 +00:00
if(_ata != null)
2024-05-01 04:05:22 +01:00
saveFs.Write(_ata, 0, _ata.Length);
else if(_atapi != null) saveFs.Write(_atapi, 0, _atapi.Length);
2022-03-06 13:29:38 +00:00
saveFs.Close();
}
async Task ExecuteSaveAtaTextCommand()
2022-03-06 13:29:38 +00:00
{
var dlgSaveText = new SaveFileDialog();
2022-11-15 01:35:06 +00:00
dlgSaveText.Filters?.Add(new FileDialogFilter
2022-03-06 13:29:38 +00:00
{
2024-05-01 04:39:38 +01:00
Extensions =
[
..new[]
{
"*.txt"
}
],
Name = UI.Dialog_Text_files
2022-03-06 13:29:38 +00:00
});
2022-03-06 13:29:38 +00:00
string result = await dlgSaveText.ShowAsync(_view);
2024-05-01 04:05:22 +01:00
if(result is null) return;
2022-03-06 13:29:38 +00:00
var saveFs = new FileStream(result, FileMode.Create);
var saveSw = new StreamWriter(saveFs);
2022-11-15 01:35:06 +00:00
await saveSw.WriteAsync(AtaIdentifyText);
2022-03-06 13:29:38 +00:00
saveFs.Close();
}
}