Skip to content

Commit

Permalink
Move common codes to AbstractController (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk4j authored Feb 5, 2024
1 parent 2874818 commit fab55f0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 355 deletions.
75 changes: 49 additions & 26 deletions Wreck/Controller/AbstractController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ public PreviewService Service
get { return service; }
}

public virtual void Error()
{
Console.Error.WriteLine(startPath + " is invalid.");
}

public void Start(string startPath)
{
if(Directory.Exists(startPath))
this.startPath = new DirectoryInfo(startPath);
else if(File.Exists(startPath))
this.startPath = new FileInfo(startPath);
else
{
LOG.Error(startPath + " is invalid.");
Environment.Exit(-1);
}
Error();

if(this.startPath != null)
{
Expand Down Expand Up @@ -177,27 +179,64 @@ public void UnauthorizedAccessException(UnauthorizedAccessException ex)

public virtual void Analyze()
{
LOG.Error("Unimplemented");
Run(CorrectionMode.Analyze);
}

public virtual void Backup()
{
LOG.Error("Unimplemented");
Run(CorrectionMode.BackupAttributes);
}

public virtual void Repair()
{
LOG.Error("Unimplemented");
Run(CorrectionMode.SaveAttributes);
}

public virtual void Restore()
{
LOG.Error("Unimplemented");
Run(CorrectionMode.RestoreAttributes);
}

public virtual void Verify()
{
LOG.Error("Unimplemented");
Run(CorrectionMode.VerifyAttributes);
}

private void Run(CorrectionMode mode)
{
string[] args = Environment.GetCommandLineArgs();
string[] dirs;

if(args.Length > 1)
{
dirs = new string[args.Length-1];
Array.Copy(args, 1, dirs, 0, args.Length-1);
}
else
{
dirs = 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);
}

Done();
}

public virtual void Run(CorrectionMode mode, FileSystemInfo fsi)
{
LOG.InfoFormat("Mode: {0}, File: {1}",
Enum.GetName(typeof(CorrectionMode), mode),
fsi.FullName);
throw new NotImplementedException();
}

// Update statistics
Expand Down Expand Up @@ -246,26 +285,10 @@ protected virtual void UpdateStatistics()
}
}

protected virtual void UpdateForecastStatistics()
{
// No charts to update.
}

protected virtual void UpdateRestoreState()
{
// No "Restore" button to enable or disable.
}

public virtual void Done()
{
// View.GetScanningDialog().Close();

{
Stop();
// Worker = null;
UpdateRestoreState();

UpdateStatistics();
UpdateForecastStatistics();

/*
LOG.InfoFormat("Metadata: {0}, Last Modified: {1}, Custom: {2}",
Expand Down
3 changes: 3 additions & 0 deletions Wreck/Controller/IController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using System;
using System.IO;
using Wreck.Resources;

namespace Wreck.Controller
{
Expand All @@ -9,6 +10,8 @@ namespace Wreck.Controller
/// </summary>
public interface IController
{
void Error();
void Run(CorrectionMode mode, FileSystemInfo fsi);
void Analyze();
void Backup();
void Repair();
Expand Down
106 changes: 22 additions & 84 deletions WreckCli/Controller/CliController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@
using System.Collections.Generic;
using System.IO;

using Java.Beans;
using Javax.Swing;
using log4net;
using Wreck.Entity;
using Wreck.IO;
using Wreck.IO.Reader;
using Wreck.IO.Task;
using Wreck.IO.Writer;
using Wreck.Logging;
using Wreck.Model;
using Wreck.Resources;
using Wreck.Service;
using Wreck.Util.Logging;
using Wreck.View;

namespace Wreck.Controller
Expand All @@ -25,6 +17,8 @@ namespace Wreck.Controller
/// </summary>
public class CliController : AbstractController
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(CliController));

private CliWorker worker;

private readonly ConsoleModel model;
Expand Down Expand Up @@ -52,91 +46,35 @@ public CliWorker Worker
set { worker = value; }
}

// Event Handlers

public override void Analyze()
{
Run(CorrectionMode.Analyze);
}

public override void Backup()
{
Run(CorrectionMode.BackupAttributes);
}

public override void Repair()
{
Run(CorrectionMode.SaveAttributes);
}

public override void Restore()
public override void Error()
{
Run(CorrectionMode.RestoreAttributes);
LOG.Error(StartPath + " is invalid.");
Environment.Exit(-1);
}

public override void Verify()
public override void Run(CorrectionMode mode, FileSystemInfo fsi)
{
Run(CorrectionMode.VerifyAttributes);
}

private void Run(CorrectionMode mode)
{
string[] args = Environment.GetCommandLineArgs();
string[] dirs;

if(args.Length > 1)
Dictionary<SourceEnum, bool> sources = new Dictionary<SourceEnum, bool>();
foreach(SourceEnum s in SourceEnum.Values)
{
dirs = new string[args.Length-1];
Array.Copy(args, 1, dirs, 0, args.Length-1);
}
else
{
dirs = new string[]
{
Directory.GetCurrentDirectory()
};
// FIXME: To check command line parameters
sources.Add(s, true);
}

foreach(string path in dirs)
Dictionary<CorrectionEnum, bool> corrections = new Dictionary<CorrectionEnum, bool>();
foreach(CorrectionEnum c in CorrectionEnum.Values)
{
FileSystemInfo fsi;

if(Directory.Exists(path))
fsi = new DirectoryInfo(path);
else if(File.Exists(path))
fsi = new FileInfo(path);
else
{
view.UnknownPathType(path);
continue;
}

Dictionary<SourceEnum, bool> sources = new Dictionary<SourceEnum, bool>();
foreach(SourceEnum s in SourceEnum.Values)
{
sources.Add(s, true);
}

Dictionary<CorrectionEnum, bool> corrections = new Dictionary<CorrectionEnum, bool>();
foreach(CorrectionEnum c in CorrectionEnum.Values)
{
corrections.Add(c, true);
}

DateTime customDateTime = DateTime.Now;

ITask task = Service.Run(fsi, mode, sources, corrections, customDateTime);

CliWorker pw = new CliWorker(task, fsi);
pw.Run();
Done();
// FIXME: To check command line parameters
corrections.Add(c, true);
}
}

public override void Done()
{
base.Done();
Worker = null;

// FIXME: To check command line parameters
DateTime customDateTime = DateTime.Now;

ITask task = Service.Run(fsi, mode, sources, corrections, customDateTime);

CliWorker pw = new CliWorker(task, fsi);
pw.Run();
}
}
}
Loading

0 comments on commit fab55f0

Please sign in to comment.