diff --git a/SlimViews/Rename.xaml.cs b/SlimViews/Rename.xaml.cs index da2ab33..36615e3 100644 --- a/SlimViews/Rename.xaml.cs +++ b/SlimViews/Rename.xaml.cs @@ -6,6 +6,7 @@ * PROGRAMER: Peter Geinitz (Wayfarer) */ +using System.Collections.Concurrent; using System.Collections.Generic; using System.Windows; @@ -34,7 +35,7 @@ public Rename() public Rename(Dictionary observer) { InitializeComponent(); - View.Observer = observer; + View.Observer = new ConcurrentDictionary(observer); } /// @@ -43,6 +44,6 @@ public Rename(Dictionary observer) /// /// The observer. /// - internal Dictionary Observer => View.Observer; + internal Dictionary Observer => new Dictionary(View.Observer); } } \ No newline at end of file diff --git a/SlimViews/RenameView.cs b/SlimViews/RenameView.cs index 975e11a..94ffe88 100644 --- a/SlimViews/RenameView.cs +++ b/SlimViews/RenameView.cs @@ -10,6 +10,7 @@ // ReSharper disable EventNeverSubscribedTo.Global using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -116,7 +117,7 @@ public string Replacer /// The explorer command. /// public ICommand RemoveAppendageCommand => _removeAppendageCommand ??= - new DelegateCommand(RemoveAppendageActionAsync, CanExecute); + new AsyncDelegateCommand(RemoveAppendageActionAsync, CanExecute); /// /// Gets the add command. @@ -124,7 +125,7 @@ public string Replacer /// /// The add command. /// - public ICommand AddCommand => _addCommand ??= new DelegateCommand(AddActionAsync, CanExecute); + public ICommand AddCommand => _addCommand ??= new AsyncDelegateCommand(AddActionAsync, CanExecute); /// /// Gets the remove command. @@ -133,7 +134,7 @@ public string Replacer /// The remove command. /// public ICommand RemoveCommand => - _removeCommand ??= new DelegateCommand(RemoveActionAsync, CanExecute); + _removeCommand ??= new AsyncDelegateCommand(RemoveActionAsync, CanExecute); /// /// Gets the reorder command. @@ -142,7 +143,7 @@ public string Replacer /// The reorder command. /// public ICommand ReorderCommand => - _reorderCommand ??= new DelegateCommand(ReorderCommandActionAsync, CanExecute); + _reorderCommand ??= new AsyncDelegateCommand(ReorderCommandActionAsync, CanExecute); /// /// Gets the reorder command. @@ -151,7 +152,7 @@ public string Replacer /// The reorder command. /// public ICommand ReplaceCommand => - _replaceCommand ??= new DelegateCommand(ReplaceCommandActionAsync, CanExecute); + _replaceCommand ??= new AsyncDelegateCommand(ReplaceCommandActionAsync, CanExecute); /// /// Gets the appendages at command. @@ -160,7 +161,7 @@ public string Replacer /// The appendages at command. /// public ICommand AppendagesAtCommand => - _appendagesAtCommand ??= new DelegateCommand(AppendagesAtActionAsync, CanExecute); + _appendagesAtCommand ??= new AsyncDelegateCommand(AppendagesAtActionAsync, CanExecute); /// /// Gets or sets the observer. @@ -168,7 +169,7 @@ public string Replacer /// /// The observer. /// - internal Dictionary Observer { get; set; } + internal ConcurrentDictionary Observer { get; set; } /// /// Sets the property. @@ -202,82 +203,214 @@ public bool CanExecute(object obj) } /// - /// Removes an appendage from the file names in the observer list. + /// Removes Appendage in File Name /// - /// The object, typically not used in this action. - private async void RemoveAppendageActionAsync(object obj) + /// The object. + private async Task RemoveAppendageActionAsync(object obj) { + var observer = new ConcurrentDictionary(Observer); if (Replacement == null) return; - await ProcessFileRenamingAsync((str) => str.RemoveAppendage(Replacement)); + try + { + foreach (var (key, value) in Observer) + { + var str = Path.GetFileName(value); + + var file = str.RemoveAppendage(Replacement); + + if (string.IsNullOrEmpty(file) || + string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue; + + var directory = Path.GetDirectoryName(value); + if (string.IsNullOrEmpty(directory)) continue; + + var target = Path.Combine(directory, file); + + var check = await FileHandleRename.RenameFile(value, target); + if (check) observer[key] = target; + } + + SlimViewerRegister.Changed = true; + Observer = observer; + } + catch (FileHandlerException ex) + { + Trace.WriteLine(ex); + _ = MessageBox.Show(ex.ToString()); + } } /// - /// Adds an appendage to the file names in the observer list. + /// Adds Elements in File Name /// - /// The object, typically not used in this action. - private async void AddActionAsync(object obj) + private async Task AddActionAsync(object obj) { + // Make a copy of the Observer dictionary to modify it + var observer = new Dictionary(Observer); + + // Ensure that the Replacement is not null if (Replacement == null) return; - await ProcessFileRenamingAsync((str) => str.AddAppendage(Replacement)); + foreach (var (key, value) in Observer) + { + // Get the filename without the directory path + var str = Path.GetFileName(value); + + // Use the AddAppendage method to create a new filename + var file = str.AddAppendage(Replacement); + + // If the new filename is the same as the original, skip it + if (string.IsNullOrEmpty(file) || string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) + continue; + + // Get the directory path + var directory = Path.GetDirectoryName(value); + if (string.IsNullOrEmpty(directory)) + continue; + + // Combine the directory with the new filename + var target = Path.Combine(directory, file); + + // Attempt to rename the file asynchronously + var check = await FileHandleRename.RenameFile(value, target); + if (check) + { + // Update the observer dictionary with the new filename + observer[key] = target; + } + } + + // Update the Observer property with the modified dictionary + Observer = new ConcurrentDictionary(observer); } + /// - /// Removes a specified part from the file names in the observer list. + /// Remove Elements in File Name /// - /// The object, typically not used in this action. - private async void RemoveActionAsync(object obj) + private async Task RemoveActionAsync(object obj) { + var observer = new ConcurrentDictionary(Observer); if (Replacement == null) return; - await ProcessFileRenamingAsync((str) => str.ReplacePart(Replacement, string.Empty)); + try + { + foreach (var (key, value) in Observer) + { + var str = Path.GetFileName(value); + + var file = str.ReplacePart(Replacement, string.Empty); + + if (string.IsNullOrEmpty(file) || + string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue; + + var directory = Path.GetDirectoryName(value); + if (string.IsNullOrEmpty(directory)) continue; + + var target = Path.Combine(directory, file); + + var check = await FileHandleRename.RenameFile(value, target); + if (check) observer[key] = value; + } + + SlimViewerRegister.Changed = true; + Observer = new ConcurrentDictionary(observer); + } + catch (FileHandlerException ex) + { + Trace.WriteLine(ex); + _ = MessageBox.Show(ex.ToString()); + } } /// - /// Reorders elements in the file names by numbers. + /// Reorder Elements in File Name /// - /// The object, typically not used in this action. - private async void ReorderCommandActionAsync(object obj) + private async Task ReorderCommandActionAsync(object obj) { - await ProcessFileRenamingAsync((str) => + var observer = new ConcurrentDictionary(Observer); + try + { + foreach (var (key, value) in Observer) + { + var str = Path.GetFileNameWithoutExtension(value); + var ext = Path.GetExtension(value); + + var file = str.ReOrderNumbers(); + if (string.IsNullOrEmpty(file) || + string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue; + + file = string.Concat(file, ext); + + var directory = Path.GetDirectoryName(value); + if (string.IsNullOrEmpty(directory)) continue; + + file = Path.Combine(directory, file); + + var check = await FileHandleRename.RenameFile(value, file); + if (check) observer[key] = file; + } + + SlimViewerRegister.Changed = true; + Observer = observer; + } + catch (FileHandlerException ex) { - var file = str.ReOrderNumbers(); - return string.IsNullOrEmpty(file) ? null : file; - }, true); + Trace.WriteLine(ex); + _ = MessageBox.Show(ex.ToString()); + } } /// - /// Replaces a specified part of the file names with a new value in the observer list. + /// Replaces part or the string command. /// - /// The object, typically not used in this action. - private async void ReplaceCommandActionAsync(object obj) + /// The object. + private async Task ReplaceCommandActionAsync(object obj) { + var observer = new ConcurrentDictionary(Observer); if (Replacer == null) return; - await ProcessFileRenamingAsync((str) => str.ReplacePart(Replacement, Replacer)); - } + try + { + foreach (var (key, value) in Observer) + { + var str = Path.GetFileNameWithoutExtension(value); + var ext = Path.GetExtension(value); - /// - /// Removes a specified number of characters from the start of the file names in the observer list. - /// - /// The object, typically not used in this action. - private async void AppendagesAtActionAsync(object obj) - { - if (Numbers <= 0) return; + var file = str.ReplacePart(Replacement, Replacer); + if (string.IsNullOrEmpty(file) || + string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) continue; + + file = string.Concat(file, ext); - await ProcessFileRenamingAsync((str) => str.Length <= Numbers ? null : str.Remove(0, Numbers)); + var directory = Path.GetDirectoryName(value); + if (string.IsNullOrEmpty(directory)) continue; + + file = Path.Combine(directory, file); + + var check = await FileHandleRename.RenameFile(value, file); + if (check) observer[key] = file; + } + + SlimViewerRegister.Changed = true; + Observer = observer; + } + catch (FileHandlerException ex) + { + Trace.WriteLine(ex); + _ = MessageBox.Show(ex.ToString()); + } } /// - /// Processes file renaming for all entries in the observer list based on the provided modification function. + /// Remove Appendage at number count action. /// - /// A function that defines how to modify the file name. - /// Indicates whether to append the original file extension to the modified name. - /// A task that represents the asynchronous operation. - private async Task ProcessFileRenamingAsync(Func modifyFileName, bool addExtension = false) + /// The object. + private async Task AppendagesAtActionAsync(object obj) { - var observer = new Dictionary(Observer); + var observer = new ConcurrentDictionary(Observer); + if (Numbers <= 0) return; try { @@ -286,19 +419,21 @@ private async Task ProcessFileRenamingAsync(Func modifyFileName, var str = Path.GetFileNameWithoutExtension(value); var ext = Path.GetExtension(value); - var file = modifyFileName(str); - if (string.IsNullOrEmpty(file) || string.Equals(str, file, StringComparison.OrdinalIgnoreCase)) - continue; + if (str.Length <= Numbers) continue; + + var file = str.Remove(0, Numbers); - if (addExtension) - file = string.Concat(file, ext); + if (string.IsNullOrEmpty(file)) continue; + + file = string.Concat(file, ext); var directory = Path.GetDirectoryName(value); if (string.IsNullOrEmpty(directory)) continue; - var target = Path.Combine(directory, file); - var check = await FileHandleRename.RenameFile(value, target); - if (check) observer[key] = target; + file = Path.Combine(directory, file); + + var check = await FileHandleRename.RenameFile(value, file); + if (check) observer[key] = file; } SlimViewerRegister.Changed = true;