Skip to content

Commit

Permalink
Reorg file
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed Jan 8, 2018
1 parent 7461e2e commit c51041a
Showing 1 changed file with 43 additions and 53 deletions.
96 changes: 43 additions & 53 deletions TVRename#/ItemsAndActions/ActionCopyMoveRename.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
using Alphaleonis.Win32.Filesystem;
using System;
using System.CodeDom;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Security.AccessControl;
using System.Threading;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace TVRename
{
using Alphaleonis.Win32.Filesystem;
using System;
using System.IO;
using System.Windows.Forms;
using DirectoryInfo = Alphaleonis.Win32.Filesystem.DirectoryInfo;
using File = Alphaleonis.Win32.Filesystem.File;
using FileInfo = Alphaleonis.Win32.Filesystem.FileInfo;
using FileSystemInfo = Alphaleonis.Win32.Filesystem.FileSystemInfo;
using File = File;
using FileInfo = FileInfo;
using FileSystemInfo = FileSystemInfo;


public class ActionCopyMoveRename : ActionFileOperation
Expand All @@ -41,12 +32,12 @@ public enum Op

public ActionCopyMoveRename(Op operation, FileInfo from, FileInfo to, ProcessedEpisode ep, TidySettings tidyup)
{
_tidyup = tidyup;
PercentDone = 0;
Episode = ep;
Operation = operation;
From = from;
To = to;
this._tidyup = tidyup;
this.PercentDone = 0;
this.Episode = ep;
this.Operation = operation;
this.From = from;
this.To = to;
}

#region Action Members
Expand All @@ -55,7 +46,7 @@ public ActionCopyMoveRename(Op operation, FileInfo from, FileInfo to, ProcessedE

public override string Name => IsMoveRename() ? "Move" : "Copy";

public override string ProgressText => To.Name;
public override string ProgressText => this.To.Name;


// 0.0 to 100.0
Expand All @@ -67,7 +58,7 @@ public override bool Go(ref bool pause, TVRenameStats stats)
FileSecurity security = null;
try
{
security = From.GetAccessControl();
security = this.From.GetAccessControl();
}
catch
{
Expand All @@ -84,25 +75,25 @@ public override bool Go(ref bool pause, TVRenameStats stats)
if (IsMoveRename() || FileHelper.Same(this.From, this.To))
{
// This step could be slow, so report progress
CopyMoveResult moveResult = Alphaleonis.Win32.Filesystem.File.Move(this.From.FullName, tempName, MoveOptions.CopyAllowed | MoveOptions.ReplaceExisting, CopyProgressCallback, null);
CopyMoveResult moveResult = File.Move(this.From.FullName, tempName, MoveOptions.CopyAllowed | MoveOptions.ReplaceExisting, CopyProgressCallback, null);
if (moveResult.ErrorCode != 0) throw new Exception(moveResult.ErrorMessage);
}
else
{
//we are copying
System.Diagnostics.Debug.Assert(this.Operation == Op.Copy);
Debug.Assert(this.Operation == Op.Copy);

// This step could be slow, so report progress
CopyMoveResult copyResult = Alphaleonis.Win32.Filesystem.File.Copy(this.From.FullName, tempName, CopyOptions.None, true, CopyProgressCallback, null);
CopyMoveResult copyResult = File.Copy(this.From.FullName, tempName, CopyOptions.None, true, CopyProgressCallback, null);
if (copyResult.ErrorCode != 0) throw new Exception(copyResult.ErrorMessage);
}

// Copying the temp file into the correct name is very quick, so no progress reporting
Alphaleonis.Win32.Filesystem.File.Move(tempName, this.To.FullName, MoveOptions.ReplaceExisting);
File.Move(tempName, this.To.FullName, MoveOptions.ReplaceExisting);

this.Done = true;

switch (Operation)
switch (this.Operation)
{
case Op.Move:
stats.FilesMoved++;
Expand All @@ -118,7 +109,7 @@ public override bool Go(ref bool pause, TVRenameStats stats)
}

}
catch (System.Exception e)
catch (Exception e)
{
this.Done = true;
this.Error = true;
Expand All @@ -128,45 +119,44 @@ public override bool Go(ref bool pause, TVRenameStats stats)
// set NTFS permissions
try
{
if (security != null)
To.SetAccessControl(security);
if (security != null) this.To.SetAccessControl(security);
}
catch
{
// ignored
}

if (Operation == Op.Move && _tidyup != null && _tidyup.DeleteEmpty)
if (this.Operation == Op.Move && this._tidyup != null && this._tidyup.DeleteEmpty)
{
DoTidyup(From.Directory );
DoTidyup(this.From.Directory );
}

return !Error;
return !this.Error;
}



public override string produces => To.FullName;
public override string produces => this.To.FullName;

#region Item Members

public override bool SameAs(Item o)
{
ActionCopyMoveRename cmr = o as ActionCopyMoveRename;

return (cmr != null) && (Operation == cmr.Operation) && FileHelper.Same(From, cmr.From) &&
FileHelper.Same(To, cmr.To);
return (cmr != null) && (this.Operation == cmr.Operation) && FileHelper.Same(this.From, cmr.From) &&
FileHelper.Same(this.To, cmr.To);
}

public override int Compare(Item o)
{
ActionCopyMoveRename cmr = o as ActionCopyMoveRename;

if (cmr == null || From.Directory == null || To.Directory == null || cmr.From.Directory == null ||
if (cmr == null || this.From.Directory == null || this.To.Directory == null || cmr.From.Directory == null ||
cmr.To.Directory == null)
return 0;

string s1 = From.FullName + (From.Directory.Root.FullName != To.Directory.Root.FullName ? "0" : "1");
string s1 = this.From.FullName + (this.From.Directory.Root.FullName != this.To.Directory.Root.FullName ? "0" : "1");
string s2 = cmr.From.FullName +
(cmr.From.Directory.Root.FullName != cmr.To.Directory.Root.FullName ? "0" : "1");

Expand All @@ -181,7 +171,7 @@ public override int Compare(Item o)



public override IgnoreItem Ignore => To == null ? null : new IgnoreItem(To.FullName);
public override IgnoreItem Ignore => this.To == null ? null : new IgnoreItem(this.To.FullName);
#endregion

public override ListViewItem ScanListViewItem
Expand All @@ -190,7 +180,7 @@ public override ListViewItem ScanListViewItem
{
ListViewItem lvi = new ListViewItem();

if (Episode == null)
if (this.Episode == null)
{
lvi.Text = "";
lvi.SubItems.Add("");
Expand All @@ -200,20 +190,20 @@ public override ListViewItem ScanListViewItem
}
else
{
lvi.Text = Episode.TheSeries.Name;
lvi.SubItems.Add(Episode.SeasonNumber.ToString());
lvi.SubItems.Add(Episode.NumsAsString());
DateTime? dt = Episode.GetAirDateDT(true);
lvi.Text = this.Episode.TheSeries.Name;
lvi.SubItems.Add(this.Episode.SeasonNumber.ToString());
lvi.SubItems.Add(this.Episode.NumsAsString());
DateTime? dt = this.Episode.GetAirDateDT(true);
if ((dt != null) && (dt.Value.CompareTo(DateTime.MaxValue) != 0))
lvi.SubItems.Add(dt.Value.ToShortDateString());
else
lvi.SubItems.Add("");
}

lvi.SubItems.Add(From.DirectoryName);
lvi.SubItems.Add(From.Name);
lvi.SubItems.Add(To.DirectoryName);
lvi.SubItems.Add(To.Name);
lvi.SubItems.Add(this.From.DirectoryName);
lvi.SubItems.Add(this.From.Name);
lvi.SubItems.Add(this.To.DirectoryName);
lvi.SubItems.Add(this.To.Name);

return lvi;
}
Expand All @@ -223,7 +213,7 @@ public override string ScanListViewGroup
{
get
{
switch (Operation)
switch (this.Operation)
{
case Op.Rename:
return "lvgActionRename";
Expand All @@ -237,19 +227,19 @@ public override string ScanListViewGroup
}
}

public override string TargetFolder => To?.DirectoryName;
public override string TargetFolder => this.To?.DirectoryName;

#endregion

private static string TempFor(FileSystemInfo f) => f.FullName + ".tvrenametemp";

public bool QuickOperation()
{
if ((From == null) || (To == null) || (From.Directory == null) || (To.Directory == null))
if ((this.From == null) || (this.To == null) || (this.From.Directory == null) || (this.To.Directory == null))
return false;

return IsMoveRename() &&
string.Equals(From.Directory.Root.FullName, To.Directory.Root.FullName,
string.Equals(this.From.Directory.Root.FullName, this.To.Directory.Root.FullName,
StringComparison.InvariantCultureIgnoreCase); // same device ... TODO: UNC paths?
}

Expand All @@ -275,17 +265,17 @@ private CopyMoveProgressResult CopyProgressCallback(long TotalFileSize, long Tot
// --------------------------------------------------------------------------------------------------------

public bool IsMoveRename() // same thing to the OS
=> (Operation == Op.Move) || (Operation == Op.Rename);
=> (this.Operation == Op.Move) || (this.Operation == Op.Rename);

public bool SameSource(ActionCopyMoveRename o) => FileHelper.Same(From, o.From);
public bool SameSource(ActionCopyMoveRename o) => FileHelper.Same(this.From, o.From);

// ========================================================================================================

private long SourceFileSize()
{
try
{
return From.Length;
return this.From.Length;
}
catch
{
Expand Down

0 comments on commit c51041a

Please sign in to comment.