2020-04-17 21:45:50 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// Aaru Data Preservation Suite
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : ConsoleViewModel.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : GUI view models.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// View model and code for the console dialog.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ 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-12-19 10:45:18 +00:00
|
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2020-04-17 21:45:50 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-04-11 03:05:21 +01:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Reflection;
|
2022-03-26 16:52:00 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2025-08-20 21:19:43 +01:00
|
|
|
|
using System.Windows.Input;
|
2020-04-11 03:05:21 +01:00
|
|
|
|
using Aaru.CommonTypes.Interop;
|
2022-11-19 21:10:41 +00:00
|
|
|
|
using Aaru.Localization;
|
2025-08-17 05:50:25 +01:00
|
|
|
|
using Aaru.Logging;
|
2024-05-02 00:50:34 +01:00
|
|
|
|
using Avalonia.Platform.Storage;
|
2025-08-20 21:19:43 +01:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2023-09-26 01:29:07 +01:00
|
|
|
|
using MsBox.Avalonia;
|
|
|
|
|
|
using MsBox.Avalonia.Enums;
|
2025-08-17 05:50:25 +01:00
|
|
|
|
using Console = Aaru.Gui.Views.Dialogs.Console;
|
2020-04-11 03:05:21 +01:00
|
|
|
|
using PlatformID = Aaru.CommonTypes.Interop.PlatformID;
|
|
|
|
|
|
using Version = Aaru.CommonTypes.Interop.Version;
|
|
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
namespace Aaru.Gui.ViewModels.Dialogs;
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public sealed class ConsoleViewModel : ViewModelBase
|
2020-04-11 03:05:21 +01:00
|
|
|
|
{
|
2025-08-17 05:50:25 +01:00
|
|
|
|
readonly Console _view;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-08-17 05:50:25 +01:00
|
|
|
|
public ConsoleViewModel(Console view)
|
2020-04-11 03:05:21 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_view = view;
|
2025-08-20 21:19:43 +01:00
|
|
|
|
SaveCommand = new AsyncRelayCommand(SaveAsync);
|
|
|
|
|
|
ClearCommand = new RelayCommand(Clear);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-08-20 21:19:43 +01:00
|
|
|
|
public ICommand ClearCommand { get; }
|
|
|
|
|
|
public ICommand SaveCommand { get; }
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public ObservableCollection<LogEntry> Entries => ConsoleHandler.Entries;
|
2023-10-03 23:27:57 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public bool DebugChecked
|
|
|
|
|
|
{
|
2025-11-24 01:40:50 +00:00
|
|
|
|
get;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
set
|
2020-04-11 03:05:21 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ConsoleHandler.Debug = value;
|
2025-11-24 01:40:50 +00:00
|
|
|
|
SetProperty(ref field, value);
|
2020-04-11 03:05:21 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-08-20 21:19:43 +01:00
|
|
|
|
async Task SaveAsync()
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
2024-05-02 00:50:34 +01:00
|
|
|
|
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
|
2020-04-11 03:05:21 +01:00
|
|
|
|
{
|
2025-10-25 13:05:42 +01:00
|
|
|
|
FileTypeChoices = [FilePickerFileTypes.Log]
|
2022-03-06 13:29:38 +00:00
|
|
|
|
});
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(result is null) return;
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-05-02 00:50:34 +01:00
|
|
|
|
var logFs = new FileStream(result.Path.AbsolutePath, FileMode.Create, FileAccess.ReadWrite);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var logSw = new StreamWriter(logFs);
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync(string.Format(UI.Log_saved_at_0, DateTime.Now));
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
PlatformID platId = DetectOS.GetRealPlatformID();
|
|
|
|
|
|
string platVer = DetectOS.GetVersion();
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var assemblyVersion =
|
2025-08-17 06:11:22 +01:00
|
|
|
|
Attribute.GetCustomAttribute(typeof(AaruLogging).Assembly,
|
2022-03-06 13:29:38 +00:00
|
|
|
|
typeof(AssemblyInformationalVersionAttribute)) as
|
|
|
|
|
|
AssemblyInformationalVersionAttribute;
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-12-14 16:23:33 +00:00
|
|
|
|
await logSw.WriteLineAsync(Aaru.Localization.Core.System_information);
|
2025-10-25 13:05:42 +01:00
|
|
|
|
|
|
|
|
|
|
await
|
|
|
|
|
|
logSw.WriteLineAsync($"{DetectOS.GetPlatformName(platId, platVer)} {platVer} ({(Environment.Is64BitOperatingSystem ? 64 : 32)}-bit)");
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync($".NET Core {Version.GetNetCoreVersion()}");
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync();
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-12-14 16:23:33 +00:00
|
|
|
|
await logSw.WriteLineAsync(Aaru.Localization.Core.Program_information);
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync($"Aaru {assemblyVersion?.InformationalVersion}");
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-12-14 16:23:33 +00:00
|
|
|
|
await logSw.WriteLineAsync(string.Format(Aaru.Localization.Core.Running_in_0_bit,
|
2025-10-25 13:05:42 +01:00
|
|
|
|
Environment.Is64BitProcess ? 64 : 32));
|
2024-05-01 04:05:22 +01:00
|
|
|
|
#if DEBUG
|
2025-12-14 16:23:33 +00:00
|
|
|
|
await logSw.WriteLineAsync(Aaru.Localization.Core.DEBUG_version);
|
2024-05-01 04:05:22 +01:00
|
|
|
|
#endif
|
2025-12-14 16:23:33 +00:00
|
|
|
|
await logSw.WriteLineAsync(string.Format(Aaru.Localization.Core.Command_line_0, Environment.CommandLine));
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync();
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync(UI.Console_with_ornament);
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(LogEntry entry in ConsoleHandler.Entries)
|
2023-10-03 23:27:57 +01:00
|
|
|
|
{
|
2022-11-19 21:10:41 +00:00
|
|
|
|
if(entry.Type != UI.LogEntry_Type_Info)
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync($"{entry.Timestamp}: ({entry.Type.ToLower()}) {entry.Message}");
|
2022-03-06 13:29:38 +00:00
|
|
|
|
else
|
2025-10-25 13:05:42 +01:00
|
|
|
|
await logSw.WriteLineAsync($"{entry.Timestamp}: {entry.Message}");
|
2023-10-03 23:27:57 +01:00
|
|
|
|
}
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
logSw.Close();
|
|
|
|
|
|
logFs.Close();
|
2020-04-11 03:05:21 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
catch(Exception exception)
|
|
|
|
|
|
{
|
2023-09-26 01:29:07 +01:00
|
|
|
|
await MessageBoxManager.GetMessageBoxStandard(UI.Title_Error,
|
2024-05-01 04:05:22 +01:00
|
|
|
|
string
|
|
|
|
|
|
.Format(UI
|
|
|
|
|
|
.Exception_0_trying_to_save_logfile_details_has_been_sent_to_console,
|
|
|
|
|
|
exception.Message),
|
|
|
|
|
|
ButtonEnum.Ok,
|
|
|
|
|
|
Icon.Error)
|
|
|
|
|
|
.ShowWindowDialogAsync(_view);
|
2020-04-11 03:05:21 +01:00
|
|
|
|
|
2025-08-20 21:19:43 +01:00
|
|
|
|
AaruLogging.Exception(exception,
|
|
|
|
|
|
UI.Exception_0_trying_to_save_logfile_details_has_been_sent_to_console,
|
2025-08-17 06:42:43 +01:00
|
|
|
|
exception.Message);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2020-04-11 03:05:21 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2025-08-20 21:19:43 +01:00
|
|
|
|
static void Clear() => ConsoleHandler.Entries.Clear();
|
2020-04-11 03:05:21 +01:00
|
|
|
|
}
|