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

197 lines
7.1 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : PcmciaInfoViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the PCMCIA 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
2020-02-27 00:33:26 +00:00
using Aaru.Console;
using Aaru.Decoders.PCMCIA;
using Aaru.Gui.Models;
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 class PcmciaInfoViewModel : ViewModelBase
{
2022-03-06 13:29:38 +00:00
readonly byte[] _cis;
readonly Window _view;
string _pcmciaCisText;
PcmciaCisModel _selectedCis;
internal PcmciaInfoViewModel([CanBeNull] byte[] pcmciaCis, Window view)
{
2022-03-06 13:29:38 +00:00
if(pcmciaCis == null)
return;
2022-03-06 13:29:38 +00:00
_cis = pcmciaCis;
CisList = new ObservableCollection<PcmciaCisModel>();
SavePcmciaCisCommand = ReactiveCommand.Create(ExecuteSavePcmciaCisCommand);
2022-03-06 13:29:38 +00:00
_view = view;
2022-03-06 13:29:38 +00:00
Tuple[] tuples = CIS.GetTuples(_cis);
2022-03-06 13:29:38 +00:00
if(tuples != null)
foreach(Tuple tuple in tuples)
{
string tupleCode;
string tupleDescription;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
switch(tuple.Code)
{
2022-03-06 13:29:38 +00:00
case TupleCodes.CISTPL_NULL:
case TupleCodes.CISTPL_END: continue;
case TupleCodes.CISTPL_DEVICEGEO:
case TupleCodes.CISTPL_DEVICEGEO_A:
tupleCode = "Device Geometry Tuples";
tupleDescription = CIS.PrettifyDeviceGeometryTuple(tuple);
break;
case TupleCodes.CISTPL_MANFID:
tupleCode = "Manufacturer Identification Tuple";
tupleDescription = CIS.PrettifyManufacturerIdentificationTuple(tuple);
break;
case TupleCodes.CISTPL_VERS_1:
tupleCode = "Level 1 Version / Product Information Tuple";
tupleDescription = CIS.PrettifyLevel1VersionTuple(tuple);
break;
case TupleCodes.CISTPL_ALTSTR:
case TupleCodes.CISTPL_BAR:
case TupleCodes.CISTPL_BATTERY:
case TupleCodes.CISTPL_BYTEORDER:
case TupleCodes.CISTPL_CFTABLE_ENTRY:
case TupleCodes.CISTPL_CFTABLE_ENTRY_CB:
case TupleCodes.CISTPL_CHECKSUM:
case TupleCodes.CISTPL_CONFIG:
case TupleCodes.CISTPL_CONFIG_CB:
case TupleCodes.CISTPL_DATE:
case TupleCodes.CISTPL_DEVICE:
case TupleCodes.CISTPL_DEVICE_A:
case TupleCodes.CISTPL_DEVICE_OA:
case TupleCodes.CISTPL_DEVICE_OC:
case TupleCodes.CISTPL_EXTDEVIC:
case TupleCodes.CISTPL_FORMAT:
case TupleCodes.CISTPL_FORMAT_A:
case TupleCodes.CISTPL_FUNCE:
case TupleCodes.CISTPL_FUNCID:
case TupleCodes.CISTPL_GEOMETRY:
case TupleCodes.CISTPL_INDIRECT:
case TupleCodes.CISTPL_JEDEC_A:
case TupleCodes.CISTPL_JEDEC_C:
case TupleCodes.CISTPL_LINKTARGET:
case TupleCodes.CISTPL_LONGLINK_A:
case TupleCodes.CISTPL_LONGLINK_C:
case TupleCodes.CISTPL_LONGLINK_CB:
case TupleCodes.CISTPL_LONGLINK_MFC:
case TupleCodes.CISTPL_NO_LINK:
case TupleCodes.CISTPL_ORG:
case TupleCodes.CISTPL_PWR_MGMNT:
case TupleCodes.CISTPL_SPCL:
case TupleCodes.CISTPL_SWIL:
case TupleCodes.CISTPL_VERS_2:
tupleCode = $"Undecoded tuple ID {tuple.Code}";
tupleDescription = $"Undecoded tuple ID {tuple.Code}";
break;
default:
tupleCode = $"0x{(byte)tuple.Code:X2}";
tupleDescription = $"Found unknown tuple ID 0x{(byte)tuple.Code:X2}";
break;
}
2022-03-06 13:29:38 +00:00
CisList.Add(new PcmciaCisModel
{
Code = tupleCode,
Description = tupleDescription
});
}
else
AaruConsole.DebugWriteLine("Device-Info command", "PCMCIA CIS returned no tuples");
}
public string CisLabel => "CIS";
public string SavePcmciaCisLabel => "Save PCMCIA CIS to file";
2022-03-06 13:29:38 +00:00
public ObservableCollection<PcmciaCisModel> CisList { get; }
2022-03-06 13:29:38 +00:00
public string PcmciaCisText
{
get => _pcmciaCisText;
set => this.RaiseAndSetIfChanged(ref _pcmciaCisText, value);
}
public PcmciaCisModel SelectedCis
{
get => _selectedCis;
set
{
2022-03-06 13:29:38 +00:00
if(_selectedCis == value)
return;
2022-03-06 13:29:38 +00:00
PcmciaCisText = value?.Description;
this.RaiseAndSetIfChanged(ref _selectedCis, value);
}
2022-03-06 13:29:38 +00:00
}
public ReactiveCommand<Unit, Task> SavePcmciaCisCommand { get; }
async Task ExecuteSavePcmciaCisCommand()
2022-03-06 13:29:38 +00:00
{
var dlgSaveBinary = new SaveFileDialog();
2020-02-29 18:03:35 +00:00
2022-11-15 01:35:06 +00:00
dlgSaveBinary.Filters?.Add(new FileDialogFilter
2022-03-06 13:29:38 +00:00
{
Extensions = new List<string>(new[]
2020-02-29 18:03:35 +00:00
{
2022-03-06 13:29:38 +00:00
"*.bin"
}),
Name = "Binary"
});
2020-02-29 18:03:35 +00:00
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(_cis, 0, _cis.Length);
2022-03-06 13:29:38 +00:00
saveFs.Close();
}
}