diff --git a/Wreck/Controller/AbstractController.cs b/Wreck/Controller/AbstractController.cs index da76d6e..14702e3 100644 --- a/Wreck/Controller/AbstractController.cs +++ b/Wreck/Controller/AbstractController.cs @@ -205,35 +205,27 @@ public virtual void Verify() private void Run(CorrectionMode mode) { string[] args = Environment.GetCommandLineArgs(); - string[] dirs; + string[] paths; if(args.Length > 1) { - dirs = new string[args.Length-1]; - Array.Copy(args, 1, dirs, 0, args.Length-1); + paths = new string[args.Length-1]; + Array.Copy(args, 1, paths, 0, args.Length-1); } else { - dirs = new string[0]; + paths = new string[0]; Error(); } - foreach(string d in dirs) - { - if(Directory.Exists(d)) - Run(mode, new DirectoryInfo(d)); - else if(File.Exists(d)) - Run(mode, new FileInfo(d)); - else - LOG.ErrorFormat("Unknown path type: {0}", d); - } + Run(mode, paths); } - public virtual void Run(CorrectionMode mode, FileSystemInfo fsi) + public virtual void Run(CorrectionMode mode, string[] paths) { LOG.InfoFormat("Mode: {0}, File: {1}", Enum.GetName(typeof(CorrectionMode), mode), - fsi.FullName); + string.Join(", ", paths)); throw new NotImplementedException(); } diff --git a/Wreck/Controller/IController.cs b/Wreck/Controller/IController.cs index b03a45d..0a0dcff 100644 --- a/Wreck/Controller/IController.cs +++ b/Wreck/Controller/IController.cs @@ -11,7 +11,7 @@ namespace Wreck.Controller public interface IController { void Error(); - void Run(CorrectionMode mode, FileSystemInfo fsi); + void Run(CorrectionMode mode, string[] paths); void Analyze(); void Backup(); void Repair(); diff --git a/Wreck/Service/WreckService.cs b/Wreck/Service/WreckService.cs index 53a1b5d..ff76e1f 100644 --- a/Wreck/Service/WreckService.cs +++ b/Wreck/Service/WreckService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; +using log4net; using Wreck.IO.Task; using Wreck.Resources; using Wreck.UI; @@ -14,40 +15,55 @@ namespace Wreck.Service /// public class WreckService { + private static readonly ILog LOG = LogManager.GetLogger(typeof(WreckService)); /* public bool IsRestorable(FileSystemInfo startPath) { CsvLogRepository r = CsvLogRepository.Instance; return r.Exists(startPath); } - */ - public ITask Run( - FileSystemInfo startPath, + */ + public List Run( + string[] startPaths, CorrectionMode mode, Dictionary sources, Dictionary corrections, DateTime custom) // PropertyChangeListener pcl) // Belongs to Java Beans { + List tasks = new List(); - ITask task = null; - - switch(mode) + FileSystemInfo startPath; + foreach(string p in startPaths) { - case CorrectionMode.Analyze: - task = new AnalyzeTask( - startPath, - sources, - custom, - corrections); - break; - case CorrectionMode.SaveAttributes: - task = new CorrectTask( - startPath, - sources, - custom, - corrections); - break; + if(Directory.Exists(p)) + startPath = new DirectoryInfo(p); + else if(File.Exists(p)) + startPath = new FileInfo(p); + else + { + LOG.WarnFormat("Skipped unsupported type: {0}", p); + continue; + } + + switch(mode) + { + case CorrectionMode.Analyze: + tasks.Add( + new AnalyzeTask( + startPath, + sources, + custom, + corrections)); + break; + case CorrectionMode.SaveAttributes: + tasks.Add( + new CorrectTask( + startPath, + sources, + custom, + corrections)); + break; // case CorrectionMode.BackupAttributes: // task = new BackupTask(startPath); // break; @@ -57,11 +73,12 @@ public ITask Run( // case CorrectionMode.VerifyAttributes: // task = new VerifyTask(startPath); // break; - default: - throw new ArgumentException("Unknown correction mode"); + default: + throw new ArgumentException("Unknown correction mode"); + } } - return task; + return tasks; } } } diff --git a/WreckCli/Controller/CliController.cs b/WreckCli/Controller/CliController.cs index a74c77e..208d2ff 100644 --- a/WreckCli/Controller/CliController.cs +++ b/WreckCli/Controller/CliController.cs @@ -54,7 +54,7 @@ public override void Error() Environment.Exit(-1); } - public override void Run(CorrectionMode mode, FileSystemInfo fsi) + public override void Run(CorrectionMode mode, string[] paths) { Dictionary sources = new Dictionary(); foreach(SourceEnum s in SourceEnum.Values) @@ -73,11 +73,11 @@ public override void Run(CorrectionMode mode, FileSystemInfo fsi) // FIXME: To check command line parameters DateTime customDateTime = DateTime.Now; - ITask task = Service.Run(fsi, mode, sources, corrections, customDateTime); + List tasks = Service.Run(paths, mode, sources, corrections, customDateTime); PropertyChangeListener propertyChangeListener = new ProgressPropertyChangeListener(this); - CliWorker pw = new CliWorker(task, fsi); + CliWorker pw = new CliWorker(tasks, paths); pw.AddPropertyChangeListener(propertyChangeListener); // HACK: Worker must save the reference before Execute as this is single-threaded. diff --git a/WreckCli/IO/CliVisitor.cs b/WreckCli/IO/CliVisitor.cs index 18c4276..49e08d1 100644 --- a/WreckCli/IO/CliVisitor.cs +++ b/WreckCli/IO/CliVisitor.cs @@ -7,6 +7,7 @@ using log4net; using Wreck.Entity; using Wreck.IO; +using Wreck.IO.Task; using Wreck.Resources; using Wreck.Util.Logging; @@ -21,10 +22,12 @@ public class CliVisitor : AbstractFileVisitor private static readonly StatisticsCollector STATS = StatisticsCollector.Instance; CliWorker progressWorker = null; + ITask task = null; - public CliVisitor(CliWorker progressWorker) : base() + public CliVisitor(CliWorker progressWorker, ITask task) : base() { this.progressWorker = progressWorker; + this.task = task; } public override FileVisitResult PreVisitDirectory(DirectoryInfo dir) @@ -33,7 +36,7 @@ public override FileVisitResult PreVisitDirectory(DirectoryInfo dir) progressWorker.Publish(visit); base.PreVisitDirectory(dir); - progressWorker.Task.PreVisitDirectory(Suggestions, dir); + task.PreVisitDirectory(Suggestions, dir); return FileVisitResult.Continue; } @@ -41,7 +44,7 @@ public override FileVisitResult PreVisitDirectory(DirectoryInfo dir) public override FileVisitResult PostVisitDirectory(DirectoryInfo dir, IOException exc) { base.PostVisitDirectory(dir, exc); - progressWorker.Task.PostVisitDirectory(Suggestions, dir); + task.PostVisitDirectory(Suggestions, dir); return FileVisitResult.Continue; } @@ -57,7 +60,7 @@ public override FileVisitResult VisitFile(FileInfo file) FileVisit visit = new FileVisit(file); progressWorker.Publish(visit); - progressWorker.Task.VisitFile(Suggestions, file); + task.VisitFile(Suggestions, file); return FileVisitResult.Continue; } @@ -65,7 +68,7 @@ public override FileVisitResult VisitFile(FileInfo file) public override FileVisitResult VisitFileFailed(FileSystemInfo file, IOException exc) { base.VisitFileFailed(file, exc); - progressWorker.Task.VisitFileFailed(Suggestions, file, exc); + task.VisitFileFailed(Suggestions, file, exc); return FileVisitResult.Continue; } diff --git a/WreckCli/IO/CliWorker.cs b/WreckCli/IO/CliWorker.cs index 11b1d2a..33719c1 100644 --- a/WreckCli/IO/CliWorker.cs +++ b/WreckCli/IO/CliWorker.cs @@ -1,6 +1,7 @@  using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using Java.Beans; @@ -28,27 +29,23 @@ public class CliWorker : RunnableFuture private readonly PropertyChangeSupport propertyChangeSupport; - private readonly ITask task; - private readonly FileSystemInfo startPath; - - private readonly CliVisitor visitor; + private readonly List tasks; + private readonly string[] startPaths; private FileVisit visit; // private FileBean fileBean; - public CliWorker(ITask task, FileSystemInfo startPath) + public CliWorker(List tasks, string[] startPaths) { state = StateValue.Pending; propertyChangeSupport = new PropertyChangeSupport(this); - this.task = task; - this.startPath = startPath; - this.visitor = new CliVisitor(this); + this.tasks = tasks; + this.startPaths = startPaths; } - public ITask Task { get { return task; } } - private FileSystemInfo StartPath { get { return startPath; } } - private AbstractFileVisitor Visitor { get { return visitor; } } + public List Tasks { get { return tasks; } } + private string[] StartPaths { get { return startPaths; } } public void Execute() { @@ -67,9 +64,28 @@ public void Run() protected string DoInBackground() { - Files.WalkFileTree( - StartPath, - Visitor); + Debug.Assert(StartPaths.Length == Tasks.Count); + + int len = Math.Min(StartPaths.Length, Tasks.Count); + + for(int i = 0; i < len; i++) + { + string p = StartPaths[i]; + FileSystemInfo startPath; + if(Directory.Exists(p)) + startPath = new DirectoryInfo(p); + else if(File.Exists(p)) + startPath = new FileInfo(p); + else + throw new IOException("Unsupported file type: " + p); + + ITask task = Tasks[i]; + CliVisitor visitor = new CliVisitor(this, task); + + Files.WalkFileTree( + startPath, + visitor); + } // Return if background worker is cancelled by user. return IsCancelled()? "Cancelled" : "Done"; @@ -192,7 +208,7 @@ protected void Process(List chunks) } ); } - */ + */ public void Publish(params FileVisit[] chunks) { diff --git a/WreckCli/WreckCli.csproj b/WreckCli/WreckCli.csproj index 169a67d..af4eea5 100644 --- a/WreckCli/WreckCli.csproj +++ b/WreckCli/WreckCli.csproj @@ -9,7 +9,7 @@ v2.0 Properties C:\Users\USER\AppData\Roaming\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis - "C:\temp\Public" + "C:\temp\Public\Documents" "C:\temp\Public\Downloads" "C:\temp\Public\Music" "C:\temp\Public\Pictures" "C:\temp\Public\Videos" Wreck.Program False False diff --git a/WreckGui/Controller/GuiController.cs b/WreckGui/Controller/GuiController.cs index 795652d..feecad2 100644 --- a/WreckGui/Controller/GuiController.cs +++ b/WreckGui/Controller/GuiController.cs @@ -144,7 +144,7 @@ private void PrefillPaths() } } - public override void Run(CorrectionMode mode, FileSystemInfo fsi) + public override void Run(CorrectionMode mode, string[] paths) { Dictionary sources = new Dictionary(); foreach(SourceEnum s in SourceEnum.Values) @@ -163,11 +163,11 @@ public override void Run(CorrectionMode mode, FileSystemInfo fsi) // FIXME: To check GUI control checkbox and textbox DateTime customDateTime = DateTime.Now; - ITask task = Service.Run(fsi, mode, sources, corrections, customDateTime); + List tasks = Service.Run(paths, mode, sources, corrections, customDateTime); PropertyChangeListener propertyChangeListener = new ProgressPropertyChangeListener(this); - GuiWorker pw = new GuiWorker(task, fsi); + GuiWorker pw = new GuiWorker(tasks, paths); pw.AddPropertyChangeListener(propertyChangeListener); pw.Execute(); Worker = pw; // Need to save to Worker for cleanup later. diff --git a/WreckGui/IO/FileCountVisitor.cs b/WreckGui/IO/GuiCountVisitor.cs similarity index 100% rename from WreckGui/IO/FileCountVisitor.cs rename to WreckGui/IO/GuiCountVisitor.cs diff --git a/WreckGui/IO/GuiVisitor.cs b/WreckGui/IO/GuiVisitor.cs index 9be52f6..c6e3b5b 100644 --- a/WreckGui/IO/GuiVisitor.cs +++ b/WreckGui/IO/GuiVisitor.cs @@ -6,6 +6,7 @@ using Java.NIO.File; using log4net; using Wreck.Entity; +using Wreck.IO.Task; using Wreck.Resources; using Wreck.Util.Logging; @@ -17,10 +18,12 @@ class GuiVisitor : AbstractFileVisitor private static readonly StatisticsCollector STATS = StatisticsCollector.Instance; GuiWorker progressWorker = null; + ITask task = null; - public GuiVisitor(GuiWorker progressWorker) : base() + public GuiVisitor(GuiWorker progressWorker, ITask task) : base() { this.progressWorker = progressWorker; + this.task = task; } public override FileVisitResult PreVisitDirectory(DirectoryInfo dir) @@ -34,7 +37,7 @@ public override FileVisitResult PreVisitDirectory(DirectoryInfo dir) progressWorker.Publish(visit); base.PreVisitDirectory(dir); - progressWorker.Task.PreVisitDirectory(Suggestions, dir); + task.PreVisitDirectory(Suggestions, dir); progressWorker.UpdateFileList(dir, Suggestions); return FileVisitResult.Continue; @@ -47,7 +50,7 @@ public override FileVisitResult PostVisitDirectory(DirectoryInfo dir, IOExceptio base.PostVisitDirectory(dir, exc); - progressWorker.Task.PostVisitDirectory(Suggestions, dir); + task.PostVisitDirectory(Suggestions, dir); progressWorker.UpdateFileList(dir, Suggestions); return FileVisitResult.Continue; @@ -67,7 +70,7 @@ public override FileVisitResult VisitFile(FileInfo file) FileVisit visit = new FileVisit(file); progressWorker.Publish(visit); - progressWorker.Task.VisitFile(Suggestions, file); + task.VisitFile(Suggestions, file); progressWorker.UpdateFileList(file, Suggestions); return FileVisitResult.Continue; @@ -81,7 +84,7 @@ public override FileVisitResult VisitFileFailed(FileSystemInfo file, IOException base.VisitFileFailed(file, exc); - progressWorker.Task.VisitFileFailed(Suggestions, file, exc); + task.VisitFileFailed(Suggestions, file, exc); progressWorker.UpdateFileList(file, Suggestions); return FileVisitResult.Continue; diff --git a/WreckGui/IO/GuiWorker.cs b/WreckGui/IO/GuiWorker.cs index 4f03fd6..fcade24 100644 --- a/WreckGui/IO/GuiWorker.cs +++ b/WreckGui/IO/GuiWorker.cs @@ -1,6 +1,7 @@  using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Windows.Forms; @@ -20,11 +21,8 @@ public class GuiWorker : SwingWorker { private static readonly ILog LOG = LogManager.GetLogger(typeof(GuiWorker)); - private readonly ITask task; - private readonly FileSystemInfo startPath; - - private readonly GuiCountVisitor countVisitor; - private readonly GuiVisitor visitor; + private readonly List tasks; + private readonly string[] startPaths; private long total; private int count; @@ -32,20 +30,17 @@ public class GuiWorker : SwingWorker private FileVisit visit; private FileBean fileBean; - public GuiWorker(ITask task, FileSystemInfo startPath) + public GuiWorker(List tasks, string[] startPaths) { this.total = 0; this.count = 0; - this.task = task; - this.startPath = startPath; - this.countVisitor = new GuiCountVisitor(this); - this.visitor = new GuiVisitor(this); + this.tasks = tasks; + this.startPaths = startPaths; } - public ITask Task { get { return task; } } - private FileSystemInfo StartPath { get { return startPath; } } - private GuiCountVisitor CountVisitor { get { return countVisitor; } } - private GuiVisitor Visitor { get { return visitor; } } + public List Tasks { get { return tasks; } } + private string[] StartPaths { get { return startPaths; } } + private long Total { get { return total; } @@ -69,15 +64,39 @@ protected override string DoInBackground() Total = 0; Count = 0; - Files.WalkFileTree( - StartPath, - CountVisitor); + Debug.Assert(StartPaths.Length == Tasks.Count); + + int len = Math.Min(StartPaths.Length, Tasks.Count); + + for(int i = 0; i < len; i++) + { + string p = StartPaths[i]; + FileSystemInfo startPath; + + if(Directory.Exists(p)) + startPath = new DirectoryInfo(p); + else if(File.Exists(p)) + startPath = new FileInfo(p); + else + throw new IOException("Unsupported file type: " + p); + + ITask task = Tasks[i]; + + GuiCountVisitor countVisitor = new GuiCountVisitor(this); + + Files.WalkFileTree( + startPath, + countVisitor); - LOG.InfoFormat("{0} total files detected.", Total); + LOG.InfoFormat("{0} total files detected.", Total); + + + GuiVisitor visitor = new GuiVisitor(this, task); - Files.WalkFileTree( - StartPath, - Visitor); + Files.WalkFileTree( + startPath, + visitor); + } // Return if background worker is cancelled by user. return IsCancelled()? "Cancelled" : "Done"; diff --git a/WreckGui/WreckGui.csproj b/WreckGui/WreckGui.csproj index 9501c35..1702623 100644 --- a/WreckGui/WreckGui.csproj +++ b/WreckGui/WreckGui.csproj @@ -8,7 +8,7 @@ WreckGui Properties C:\Users\USER\AppData\Roaming\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis - "C:\temp\Public\Pictures" + "C:\temp\Public\Pictures" "C:\temp\Public\Downloads" "C:\temp\Public\Videos" "C:\temp\Public\Music" "C:\temp\Public\Documents" False False 4 @@ -51,7 +51,7 @@ - +