From 2c9ed811c6048fd91cdea9d5b175981f9bb47e26 Mon Sep 17 00:00:00 2001 From: Jannik Becker Date: Sun, 3 Dec 2023 19:35:42 +0100 Subject: [PATCH 1/3] Remove code for old file format and conversion --- Leibit.BLL/SerializationBLL.cs | 19 +- Leibit.Client.WPF/ViewModels/MainViewModel.cs | 35 +-- Leibit.Client.WPF/Views/MainView.xaml | 4 - .../ViewModels/FileConversionViewModel.cs | 199 ------------------ .../ViewModels/FileViewModel.cs | 130 ------------ .../Views/FileConversionView.xaml | 77 ------- .../Views/FileConversionView.xaml.cs | 16 -- Leibit.Entities/Enums.cs | 7 - .../Serialization/SerializationContainer.cs | 1 - 9 files changed, 2 insertions(+), 486 deletions(-) delete mode 100644 Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileConversionViewModel.cs delete mode 100644 Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileViewModel.cs delete mode 100644 Leibit.Client.WPF/Windows/FileConversion/Views/FileConversionView.xaml delete mode 100644 Leibit.Client.WPF/Windows/FileConversion/Views/FileConversionView.xaml.cs diff --git a/Leibit.BLL/SerializationBLL.cs b/Leibit.BLL/SerializationBLL.cs index 7dcfc392..8a6c74df 100644 --- a/Leibit.BLL/SerializationBLL.cs +++ b/Leibit.BLL/SerializationBLL.cs @@ -8,7 +8,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.Serialization.Formatters.Binary; namespace Leibit.BLL { @@ -209,23 +208,7 @@ public OperationResult Open(string Filename) SerializedRoot Root; var json = File.ReadAllText(Filename); - - if (json.StartsWith("{")) - { - Root = JsonConvert.DeserializeObject(json, m_SerializerSettings); - Container.FileFormat = Entities.eFileFormat.JSON; - } - else - { - using (var stream = File.OpenRead(Filename)) - { - var formatter = new BinaryFormatter(); -#pragma warning disable SYSLIB0011 // Type or member is obsolete - Root = formatter.Deserialize(stream) as SerializedRoot; -#pragma warning restore SYSLIB0011 // Type or member is obsolete - Container.FileFormat = Entities.eFileFormat.Binary; - } - } + Root = JsonConvert.DeserializeObject(json, m_SerializerSettings); if (Root == null) throw new OperationFailedException("Invalid file."); diff --git a/Leibit.Client.WPF/ViewModels/MainViewModel.cs b/Leibit.Client.WPF/ViewModels/MainViewModel.cs index d1385f09..e2765b28 100644 --- a/Leibit.Client.WPF/ViewModels/MainViewModel.cs +++ b/Leibit.Client.WPF/ViewModels/MainViewModel.cs @@ -11,8 +11,6 @@ using Leibit.Client.WPF.Windows.Display.Views; using Leibit.Client.WPF.Windows.ESTWSelection.ViewModels; using Leibit.Client.WPF.Windows.ESTWSelection.Views; -using Leibit.Client.WPF.Windows.FileConversion.ViewModels; -using Leibit.Client.WPF.Windows.FileConversion.Views; using Leibit.Client.WPF.Windows.LocalOrders.ViewModels; using Leibit.Client.WPF.Windows.LocalOrders.Views; using Leibit.Client.WPF.Windows.Settings.ViewModels; @@ -69,7 +67,6 @@ public class MainViewModel : WindowViewModelBase private Thread m_RefreshingThread; private CancellationTokenSource m_CancellationTokenSource; private string m_CurrentFilename; - private eFileFormat m_CurrentFileFormat; private bool m_ForceClose; private SoftwareInfo m_BildFplInfo; private System.Timers.Timer m_StatusBarMessageTimer; @@ -78,7 +75,6 @@ public class MainViewModel : WindowViewModelBase private CommandHandler m_OpenCommand; private CommandHandler m_SaveCommand; private CommandHandler m_SaveAsCommand; - private CommandHandler m_FileConversionCommand; private CommandHandler m_SettingsCommand; private CommandHandler m_EstwOnlineCommand; private CommandHandler m_BildFplCommand; @@ -129,7 +125,6 @@ public MainViewModel() m_OpenCommand = new CommandHandler(__Open, true); m_SaveCommand = new CommandHandler(__Save, false); m_SaveAsCommand = new CommandHandler(() => __SaveAs(null), false); - m_FileConversionCommand = new CommandHandler(__ShowFileConversionWindow, true); m_SettingsCommand = new CommandHandler(__Settings, true); m_EstwOnlineCommand = new CommandHandler(__StartEstwOnline, true); m_BildFplCommand = new CommandHandler(__StartBildFpl, IsBildFplInstalled); @@ -482,16 +477,6 @@ public ICommand SaveAsCommand } #endregion - #region [FileConversionCommand] - public ICommand FileConversionCommand - { - get - { - return m_FileConversionCommand; - } - } - #endregion - #region [SettingsCommand] public ICommand SettingsCommand { @@ -775,7 +760,6 @@ private void __New(string AreaId) return; m_CurrentFilename = null; - m_CurrentFileFormat = eFileFormat.Unknown; OnPropertyChanged(nameof(CurrentFile)); __ShowEstwSelectionWindow(); @@ -789,7 +773,7 @@ private void __Open() return; var Dialog = new OpenFileDialog(); - Dialog.Filter = "LeiBIT-Dateien|*.leibit;*.leibit2|Alle Dateien|*.*"; + Dialog.Filter = "LeiBIT-Dateien|*.leibit2|Alle Dateien|*.*"; if (Dialog.ShowDialog() == true) { @@ -983,7 +967,6 @@ private void __Open() } m_CurrentFilename = Filename; - m_CurrentFileFormat = OpenResult.Result.FileFormat; OnPropertyChanged(nameof(CurrentFile)); } } @@ -998,12 +981,6 @@ private void __Save() return; } - if (m_CurrentFileFormat == eFileFormat.Binary) - { - __SaveAs(Path.GetFileNameWithoutExtension(m_CurrentFilename) + ".leibit2"); - return; - } - var Container = new SerializationContainer(); Container.Area = m_CurrentArea; Container.VisibleStations = Runtime.VisibleStations.Select(s => new SerializedStation { EstwId = s.ESTW.Id, ShortSymbol = s.ShortSymbol }).ToList(); @@ -1091,22 +1068,12 @@ private void __SaveAs(string proposedFileName) } m_CurrentFilename = Filename; - m_CurrentFileFormat = eFileFormat.JSON; OnPropertyChanged(nameof(CurrentFile)); __Save(); } } #endregion - #region [__ShowFileConversionWindow] - private void __ShowFileConversionWindow() - { - var Window = new FileConversionView(); - Window.DataContext = new FileConversionViewModel(Window.Dispatcher); - __OpenChildWindow(Window); - } - #endregion - #region [__Settings] private void __Settings() { diff --git a/Leibit.Client.WPF/Views/MainView.xaml b/Leibit.Client.WPF/Views/MainView.xaml index bf89cf16..c3159ce2 100644 --- a/Leibit.Client.WPF/Views/MainView.xaml +++ b/Leibit.Client.WPF/Views/MainView.xaml @@ -61,10 +61,6 @@ - - - - diff --git a/Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileConversionViewModel.cs b/Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileConversionViewModel.cs deleted file mode 100644 index 59fe14c4..00000000 --- a/Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileConversionViewModel.cs +++ /dev/null @@ -1,199 +0,0 @@ -using Leibit.BLL; -using Leibit.Client.WPF.ViewModels; -using Leibit.Core.Client.Commands; -using Leibit.Entities; -using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using System.Windows.Threading; - -namespace Leibit.Client.WPF.Windows.FileConversion.ViewModels -{ - public class FileConversionViewModel : ChildWindowViewModelBase - { - #region - Needs - - private bool m_IsWorking; - private readonly Dispatcher m_Dispatcher; - #endregion - - #region - Ctor - - public FileConversionViewModel(Dispatcher dispatcher) - { - m_Dispatcher = dispatcher; - SelectFilesCommand = new CommandHandler(__SelectFiles, true); - ConvertCommand = new CommandHandler(__Convert, false); - CancelCommand = new CommandHandler(__Cancel, true); - Files = new ObservableCollection(); - } - #endregion - - #region - Properties - - - #region [Commands] - public CommandHandler SelectFilesCommand { get; } - public CommandHandler ConvertCommand { get; } - public CommandHandler CancelCommand { get; } - #endregion - - #region [Files] - public ObservableCollection Files - { - get => Get>(); - private set => Set(value); - } - #endregion - - #endregion - - #region - Public methods - - - #region [OnWindowClosing] - public override void OnWindowClosing(object sender, CancelEventArgs e) - { - if (m_IsWorking) - e.Cancel = true; - } - #endregion - - #endregion - - #region - Private methods - - - #region [__SelectFiles] - private void __SelectFiles() - { - var dialog = new OpenFileDialog(); - dialog.Filter = "LeiBIT-Dateien|*.leibit|Alle Dateien|*.*"; - dialog.Multiselect = true; - - if (dialog.ShowDialog() != true) - return; - - foreach (var vm in Files) - vm.PropertyChanged -= __File_PropertyChanged; - - Files.Clear(); - - foreach (var file in dialog.FileNames) - { - var vm = new FileViewModel(file); - vm.PropertyChanged += __File_PropertyChanged; - Files.Add(vm); - } - - ConvertCommand.SetCanExecute(Files.Any(x => x.IsValid)); - } - #endregion - - #region [__Convert] - private void __Convert() - { - m_IsWorking = true; - SelectFilesCommand.SetCanExecute(false); - ConvertCommand.SetCanExecute(false); - CancelCommand.SetCanExecute(false); - - foreach (var vm in Files) - vm.CanEdit = false; - - Task.Run(() => - { - var serializationBLL = new SerializationBLL(); - var filesToConvert = Files.Where(x => x.IsValid && !x.IsSuccessful).ToList(); - - for (int i = 0; i < filesToConvert.Count; i++) - { - var vm = filesToConvert[i]; - OnReportProgress(vm.OldName, 100.0 * i / filesToConvert.Count); - - var openResult = serializationBLL.Open(vm.OldName); - - if (!openResult.Succeeded) - { - vm.HasWarning = true; - vm.WarningText = openResult.Message; - continue; - } - - if (openResult.Result.FileFormat != Entities.eFileFormat.Binary) - { - vm.HasWarning = true; - vm.WarningText = "Die Datei hat ein ungültiges Format."; - continue; - } - - var container = openResult.Result; - - foreach (var window in container.Windows.Where(x => x.Type == eChildWindowType.LocalOrders && x.Tag is KeyValuePair)) - { - // Special logic to convert KeyValuePair to string. KeyValuePair causes problems with JSON. - var kvp = (KeyValuePair)window.Tag; - window.Tag = $"{kvp.Key};{kvp.Value}"; - } - - var saveResult = serializationBLL.Save(vm.NewName + ".leibit2", container); - - if (saveResult.Succeeded) - { - vm.HasWarning = false; - vm.IsSuccessful = true; - } - else - { - vm.HasWarning = true; - vm.WarningText = saveResult.Message; - } - } - - foreach (var vm in Files) - vm.CanEdit = true; - - OnReportProgress(true); - OnStatusBarTextChanged("Konvertierung abgeschlossen"); - m_IsWorking = false; - - m_Dispatcher.BeginInvoke(new Action(() => - { - SelectFilesCommand.SetCanExecute(true); - ConvertCommand.SetCanExecute(Files.Any(x => x.IsValid)); - CancelCommand.SetCanExecute(true); - })); - }); - } - #endregion - - #region [__Cancel] - private void __Cancel() - { - OnCloseWindow(); - } - #endregion - - #region [__File_PropertyChanged] - private void __File_PropertyChanged(object sender, PropertyChangedEventArgs e) - { - var senderVm = sender as FileViewModel; - - if (senderVm == null) - return; - - if (e.PropertyName == "IsValid") - { - ConvertCommand.SetCanExecute(Files.Any(x => x.IsValid)); - } - else if (e.PropertyName == "Remove") - { - senderVm.PropertyChanged -= __File_PropertyChanged; - Files.Remove(senderVm); - ConvertCommand.SetCanExecute(Files.Any(x => x.IsValid)); - } - } - #endregion - - #endregion - } -} diff --git a/Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileViewModel.cs b/Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileViewModel.cs deleted file mode 100644 index 3db4d37a..00000000 --- a/Leibit.Client.WPF/Windows/FileConversion/ViewModels/FileViewModel.cs +++ /dev/null @@ -1,130 +0,0 @@ -using Leibit.Core.Client.BaseClasses; -using Leibit.Core.Client.Commands; -using Leibit.Core.Common; -using System.IO; -using System.Windows.Input; - -namespace Leibit.Client.WPF.Windows.FileConversion.ViewModels -{ - public class FileViewModel : ViewModelBase - { - #region - Ctor - - public FileViewModel(string fileName) - { - OldName = fileName; - - var directoryName = Path.GetDirectoryName(fileName); - var fileNameWithoutExtention = Path.GetFileNameWithoutExtension(fileName); - NewName = Path.Combine(directoryName, fileNameWithoutExtention); - - RemoveCommand = new CommandHandler(__Remove, true); - CanEdit = true; - } - #endregion - - #region - Properties - - - public ICommand RemoveCommand { get; } - - #region [OldName] - public string OldName { get; } - #endregion - - #region [NewName] - public string NewName - { - get => Get(); - set - { - Set(value); - __Validate(); - } - } - #endregion - - #region [CanEdit] - public bool CanEdit - { - get => Get(); - internal set => Set(value); - } - #endregion - - #region [IsValid] - internal bool IsValid - { - get => Get(); - private set => Set(value); - } - #endregion - - #region [IsSuccessful] - public bool IsSuccessful - { - get => Get(); - internal set => Set(value); - } - #endregion - - #region [HasWarning] - public bool HasWarning - { - get => Get(); - internal set => Set(value); - } - #endregion - - #region [WarningText] - public string WarningText - { - get => Get(); - internal set => Set(value); - } - #endregion - - #endregion - - #region - Private methods - - - #region [__Validate] - private void __Validate() - { - IsSuccessful = false; - - if (NewName.IsNullOrWhiteSpace()) - { - IsValid = false; - HasWarning = true; - WarningText = "Bitte einen Dateinamen eingeben."; - } - else if (!Path.IsPathFullyQualified(NewName)) - { - IsValid = false; - HasWarning = true; - WarningText = "Ungültiger Pfad."; - } - else if (File.Exists(NewName + ".leibit2")) - { - IsValid = true; - HasWarning = true; - WarningText = "Die Datei existiert bereits und wird überschrieben."; - } - else - { - IsValid = true; - HasWarning = false; - WarningText = string.Empty; - } - } - #endregion - - #region [__Remove] - private void __Remove() - { - OnPropertyChanged("Remove"); - } - #endregion - - #endregion - } -} diff --git a/Leibit.Client.WPF/Windows/FileConversion/Views/FileConversionView.xaml b/Leibit.Client.WPF/Windows/FileConversion/Views/FileConversionView.xaml deleted file mode 100644 index 517b1e04..00000000 --- a/Leibit.Client.WPF/Windows/FileConversion/Views/FileConversionView.xaml +++ /dev/null @@ -1,77 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -