Changeset 2509 for trunk/eraser/Eraser/Program.cs
- Timestamp:
- 3/14/2012 5:45:46 AM (14 months ago)
- Location:
- trunk/eraser
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
Eraser/Program.cs (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser
- Property svn:mergeinfo changed
/branches/eraser6/pluginsRewrite (added) merged: 2285-2508
- Property svn:mergeinfo changed
-
trunk/eraser/Eraser/Program.cs
r2267 r2509 31 31 using System.Security.Principal; 32 32 using System.Text.RegularExpressions; 33 using System.Text; 33 34 34 35 using System.Reflection; … … 39 40 using Eraser.Manager; 40 41 using Eraser.Util; 41 using Eraser.DefaultPlugins; 42 using System.Text; 42 using Eraser.Plugins; 43 using Eraser.Plugins.ExtensionPoints; 44 using Eraser.Plugins.Registrars; 43 45 44 46 namespace Eraser … … 133 135 } 134 136 135 class AddTaskArguments : EraseArguments137 class TaskArguments : EraseArguments 136 138 { 137 139 /// <summary> 138 140 /// Constructor. 139 141 /// </summary> 140 public AddTaskArguments()141 { 142 } 143 144 /// <summary> 145 /// Constructs AddTask arguments from Erase arguments.142 public TaskArguments() 143 { 144 } 145 146 /// <summary> 147 /// Constructs Task arguments from Erase arguments. 146 148 /// </summary> 147 149 /// <param name="arguments">The <see cref="EraseArguments"/> to use as a template 148 150 /// for this instance.</param> 149 internal AddTaskArguments(EraseArguments arguments)151 internal TaskArguments(EraseArguments arguments) 150 152 : base(arguments) 151 153 { … … 159 161 } 160 162 161 class ShellArguments : ConsoleArguments 162 { 163 /// <summary> 164 /// The action which the shell extension has requested. 165 /// </summary> 166 [Arg("action", "The action selected by the user", typeof(string), true, null, null)] 167 public ShellActions ShellAction { get; set; } 168 169 /// <summary> 170 /// Whether the recycle bin was specified on the command line. 171 /// </summary> 172 [Arg("recycleBin", "The recycle bin as an erasure target", typeof(string), false, null, null)] 173 public bool RecycleBin { get; set; } 174 175 /// <summary> 176 /// The destination for secure move operations, only valid when 177 /// <see cref="ShellAction"/> is <see cref="ShellActions.SecureMove"/> 178 /// </summary> 179 [Arg("destination", "The destination for secure move operations", typeof(string), false, null, null)] 180 public string Destination { get; set; } 181 163 class ShellArguments : TaskArguments 164 { 182 165 /// <summary> 183 166 /// The parent HWND which can be used as a parent to display dialogs. … … 185 168 [Arg("parent", "The parent HWND which can be used as a parent to display dialogues", typeof(string), false, null, null)] 186 169 public string Parent { get; set; } 170 171 /// <summary> 172 /// Whether we should display a confirmation dialog. 173 /// </summary> 174 [Arg("confirm", "Whether a confirmation dialog should be shown", typeof(bool), false, true, null)] 175 public bool Confirm { get; set; } 187 176 } 188 177 … … 242 231 243 232 //Load the Eraser.Manager library 244 using (ManagerLibrary library = new ManagerLibrary( new Settings()))233 using (ManagerLibrary library = new ManagerLibrary(Settings.Get())) 245 234 { 246 235 //Set our UI language … … 331 320 new ConsoleActionData(CommandErase, new EraseArguments())); 332 321 program.Handlers.Add("addtask", 333 new ConsoleActionData(CommandAddTask, new AddTaskArguments()));322 new ConsoleActionData(CommandAddTask, new TaskArguments())); 334 323 program.Handlers.Add("importtasklist", 335 324 new ConsoleActionData(CommandImportTaskList, new ConsoleArguments())); … … 362 351 //Get the command-line help for every erasure target 363 352 StringBuilder targets = new StringBuilder(); 364 foreach ( ErasureTarget target in ManagerLibrary.Instance.ErasureTargetRegistrar)353 foreach (IErasureTarget target in Host.Instance.ErasureTargetFactories) 365 354 { 366 355 //Replace all \r\n with \n, and split into lines … … 379 368 380 369 //Generate the list of erasure methods. 381 foreach ( ErasureMethod method in ManagerLibrary.Instance.ErasureMethodRegistrar)382 { 383 methods.AppendFormat(methodFormat, (method is UnusedSpaceErasureMethod) ?370 foreach (IErasureMethod method in Host.Instance.ErasureTargetFactories) 371 { 372 methods.AppendFormat(methodFormat, (method is IUnusedSpaceErasureMethod) ? 384 373 "U" : "", method.Name, method.Guid); 385 374 } … … 460 449 private static void CommandErase(ConsoleArguments arg) 461 450 { 462 AddTaskArguments arguments = new AddTaskArguments((EraseArguments)arg);451 TaskArguments arguments = new TaskArguments((EraseArguments)arg); 463 452 arguments.Schedule = "NOW"; 464 453 … … 473 462 private static void CommandAddTask(ConsoleArguments arg) 474 463 { 475 AddTaskArguments arguments = (AddTaskArguments)arg; 476 464 TaskArguments arguments = (TaskArguments)arg; 465 Task task = TaskFromCommandLine(arguments); 466 467 //Send the task out. 468 using (eraserClient = CommandConnect()) 469 eraserClient.Tasks.Add(task); 470 } 471 472 /// <summary> 473 /// Parses the command line for erasure targets and returns them as 474 /// a Task object. 475 /// </summary> 476 /// <param name="arguments">The arguments specified on the command line.</param> 477 /// <returns>The task represented on the command line.</returns> 478 private static Task TaskFromCommandLine(TaskArguments arguments) 479 { 477 480 //Create the task 478 481 Task task = new Task(); 479 482 480 483 //Get the erasure method the user wants to use 481 ErasureMethod method = string.IsNullOrEmpty(arguments.ErasureMethod) ?484 IErasureMethod method = string.IsNullOrEmpty(arguments.ErasureMethod) ? 482 485 ErasureMethodRegistrar.Default : 483 486 ErasureMethodFromNameOrGuid(arguments.ErasureMethod); … … 504 507 foreach (string argument in arguments.PositionalArguments) 505 508 { 506 ErasureTarget selectedTarget = null;509 IErasureTarget selectedTarget = null; 507 510 508 511 //Iterate over every defined erasure target 509 foreach ( ErasureTarget target in ManagerLibrary.Instance.ErasureTargetRegistrar)512 foreach (IErasureTarget target in Host.Instance.ErasureTargetFactories) 510 513 { 511 514 //See if this argument can be handled by the target's configurer … … 545 548 throw new ArgumentException(S._("Tasks must contain at least one erasure target.")); 546 549 547 //Send the task out. 548 using (eraserClient = CommandConnect()) 549 eraserClient.Tasks.Add(task); 550 } 551 552 private static ErasureMethod ErasureMethodFromNameOrGuid(string param) 550 return task; 551 } 552 553 private static IErasureMethod ErasureMethodFromNameOrGuid(string param) 553 554 { 554 555 try 555 556 { 556 return ManagerLibrary.Instance.ErasureMethodRegistrar[new Guid(param)];557 return Host.Instance.ErasureMethods[new Guid(param)]; 557 558 } 558 559 catch (FormatException) … … 560 561 //Invalid GUID. Check every registered erasure method for the name 561 562 string upperParam = param.ToUpperInvariant(); 562 ErasureMethod result = null;563 foreach ( ErasureMethod method in ManagerLibrary.Instance.ErasureMethodRegistrar)563 IErasureMethod result = null; 564 foreach (IErasureMethod method in Host.Instance.ErasureMethods) 564 565 { 565 566 if (method.Name.ToUpperInvariant() == upperParam) … … 595 596 private static void CommandShell(ConsoleArguments args) 596 597 { 597 switch (((ShellArguments)args).ShellAction) 598 { 599 case ShellActions.SecureMove: 600 CommandShellSecureMove((ShellArguments)args); 601 break; 602 603 default: 604 CommandShellErase((ShellArguments)args); 605 break; 606 } 607 } 608 609 /// <summary> 610 /// Handles the erasure of files from the Shell extension. 611 /// </summary> 612 /// <param name="args">The command line parameters passed to the program.</param> 613 private static void CommandShellErase(ShellArguments args) 614 { 615 //Construct a draft task. 616 Task task = new Task(); 617 switch (args.ShellAction) 618 { 619 case ShellActions.EraseOnRestart: 620 task.Schedule = Schedule.RunOnRestart; 621 goto case ShellActions.EraseNow; 622 623 case ShellActions.EraseNow: 624 foreach (string path in args.PositionalArguments) 625 { 626 //If the path doesn't exist, skip the file 627 if (!(File.Exists(path) || Directory.Exists(path))) 628 continue; 629 630 FileSystemObjectErasureTarget target = null; 631 if ((File.GetAttributes(path) & FileAttributes.Directory) != 0) 632 { 633 target = new FolderErasureTarget(); 634 target.Path = path; 635 } 636 else 637 { 638 target = new FileErasureTarget(); 639 target.Path = path; 640 } 641 642 task.Targets.Add(target); 643 } 644 645 //Was the recycle bin specified? 646 if (args.RecycleBin) 647 task.Targets.Add(new RecycleBinErasureTarget()); 648 break; 649 650 case ShellActions.EraseUnusedSpace: 651 foreach (string path in args.PositionalArguments) 652 { 653 UnusedSpaceErasureTarget target = new UnusedSpaceErasureTarget(); 654 target.Drive = path; 655 task.Targets.Add(target); 656 } 657 break; 658 } 598 ShellArguments arguments = (ShellArguments)args; 599 Task task = TaskFromCommandLine(arguments); 659 600 660 601 //Do we have a parent dialog? 661 602 IWin32Window parent = null; 662 if (arg s.Parent != null)603 if (arguments.Parent != null) 663 604 { 664 605 parent = new Win32Window((IntPtr)(ulong) 665 Convert.ChangeType(arg s.Parent, typeof(ulong)));606 Convert.ChangeType(arguments.Parent, typeof(ulong))); 666 607 } 667 608 668 609 //Confirm that the user wants the erase. 669 Application.EnableVisualStyles(); 670 using (Form dialog = new ShellConfirmationDialog(task)) 671 { 672 if (dialog.ShowDialog(parent) != DialogResult.Yes) 673 return; 674 } 675 676 //Then queue for erasure. 677 using (eraserClient = CommandConnect()) 678 eraserClient.Tasks.Add(task); 679 } 680 681 /// <summary> 682 /// Handles the movement of files from the Shell extension. 683 /// </summary> 684 /// <param name="args">The command line parameters passed to the program.</param> 685 private static void CommandShellSecureMove(ShellArguments args) 686 { 687 //Construct a draft task. 688 Task task = new Task(); 689 foreach (string path in args.PositionalArguments) 690 { 691 SecureMoveErasureTarget target = new SecureMoveErasureTarget(); 692 target.Path = path; 693 target.Destination = args.Destination; 694 695 task.Targets.Add(target); 610 if (arguments.Confirm) 611 { 612 Application.EnableVisualStyles(); 613 using (Form dialog = new ShellConfirmationDialog(task)) 614 { 615 if (dialog.ShowDialog(parent) != DialogResult.Yes) 616 return; 617 } 696 618 } 697 619 … … 852 774 /// </summary> 853 775 public const string SettingsPath = @"SOFTWARE\Eraser\Eraser 6"; 776 777 public static IEnumerable<IErasureTarget> ErasureTargetRegistrar { get; set; } 854 778 } 855 779
Note: See TracChangeset
for help on using the changeset viewer.
