/// <summary> /// Constructor /// </summary> /// <param name="Commands">List of RenameCommand's to perform</param> /// <param name="ShowDialog">Action to take in the event of an exception</param> public RenameAllCommand(List <RenameCommand> Commands, Func <RenameFailureBehaviour> ShowDialog) { _commands = Commands; _hasRun = false; _pos = 0; // initial behaviour when an error occurs is to show a dialog, i.e. run ShowDialog _behaviour = RenameFailureBehaviour.Dialog; _showDialog = ShowDialog; _successful = false; _skipped = new List <bool>(Enumerable.Repeat(false, Commands.Count)); }
/// <summary> /// Run the RenameAllCommand. /// This attempts to run all the rename commands given /// in the constructor. /// In the event of an error it will run the ShowDialog Func /// </summary> public void Run() { if (!_hasRun) { for (int i = 0; i < _commands.Count; i++) { try { _commands[i].Run(); } catch (Exception) // TODO: make the exception caught more specific { // Some filesystem error has occurred _pos = i; if (_behaviour == RenameFailureBehaviour.Dialog) { _behaviour = _showDialog(); } switch (_behaviour) { case RenameFailureBehaviour.Dialog: // This case should not happen goto default; case RenameFailureBehaviour.Undo: // User wants to undo everything _hasRun = true; Undo(); return; case RenameFailureBehaviour.Skip: // User wants to skip this error _behaviour = RenameFailureBehaviour.Dialog; _skipped[i] = true; break; case RenameFailureBehaviour.SilentContinue: // User wants to skip all errors break; case RenameFailureBehaviour.Abort: // User wants to stop immediately return; default: throw new Exception("Dialog should return a result"); } } } _hasRun = true; _successful = true; } }