using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using RoboSharp.Interfaces;
using System.Threading.Tasks;
using RoboSharp.Results;
namespace RoboSharp
{
///
/// Represents a single RoboCopy Job File
/// Implements:
///
///
///
///
///
///
public class JobFile : ICloneable, IRoboCommand
{
#region < Constructor >
///
/// Create a JobFile with Default Options
///
public JobFile() { }
///
/// Constructor for ICloneable Interface
///
///
public JobFile(JobFile jobFile)
{
this.roboCommand = jobFile.roboCommand.Clone();
}
///
/// Clone the RoboCommand's options objects into a new JobFile
///
/// RoboCommand whose options shall be cloned
/// Optional FilePath to specify for future call to
public JobFile(RoboCommand cmd, string filePath = "")
{
FilePath = filePath ?? "";
roboCommand = cmd.Clone();
}
///
/// Constructor for Factory Methods
///
private JobFile(string filePath, RoboCommand cmd)
{
FilePath = filePath;
roboCommand = cmd;
}
#endregion
#region < Factory Methods >
///
public static JobFile ParseJobFile(string path)
{
RoboCommand cmd = JobFileBuilder.Parse(path);
if (cmd != null) return new JobFile(path, cmd);
return null;
}
///
public static JobFile ParseJobFile(StreamReader streamReader)
{
RoboCommand cmd = JobFileBuilder.Parse(streamReader);
if (cmd != null) return new JobFile("", cmd);
return null;
}
///
public static JobFile ParseJobFile(FileInfo file)
{
RoboCommand cmd = JobFileBuilder.Parse(file);
if (cmd != null) return new JobFile(file.FullName, cmd);
return null;
}
///
public static JobFile ParseJobFile(IEnumerable FileText)
{
RoboCommand cmd = JobFileBuilder.Parse(FileText);
if (cmd != null) return new JobFile("", cmd);
return null;
}
#endregion
#region < ICLONEABLE >
///
/// Create a clone of this JobFile
///
public JobFile Clone() => new JobFile(this);
object ICloneable.Clone() => Clone();
#endregion
#region < Constants >
///
/// Expected File Extension for Job Files exported from RoboCopy.
///
public const string JOBFILE_Extension = ".RCJ";
///
/// FileFilter to use in an to search for this extension, such as with
///
public const string JOBFILE_SearchPattern = "*.RCJ";
///
/// FileFilter to use in a dialog window, such as the OpenFileDialog window.
///
public const string JOBFILE_DialogFilter = "RoboCopy Job|*.RCJ";
#endregion
#region < Fields >
///
/// Options are stored in a RoboCommand object for simplicity.
///
protected RoboCommand roboCommand;
#endregion
#region < Properties >
/// FilePath of the Job File
public virtual string FilePath { get; set; }
///
public string Job_Name
{
get => roboCommand.Name;
set => roboCommand.Name = value;
}
///
public CopyOptions CopyOptions => roboCommand.CopyOptions;
///
public LoggingOptions LoggingOptions => roboCommand.LoggingOptions;
///
public RetryOptions RetryOptions => roboCommand.RetryOptions;
///
public SelectionOptions SelectionOptions => roboCommand.SelectionOptions;
#endregion
#region < Methods >
#pragma warning disable CS1573
///
/// Update the property and save the JobFile to the
///
/// Update the property, then save the JobFile to this path.
///
///
public async Task Save(string path, bool IncludeSource = false, bool IncludeDestination = false)
{
if (path.IsNullOrWhiteSpace()) throw new ArgumentException("path Property is Empty");
FilePath = path;
await roboCommand.SaveAsJobFile(FilePath, IncludeSource, IncludeDestination);
}
///
/// Save the JobFile to .
/// Source and Destination will be included by default.
///
/// If path is null/empty, will throw
/// Task that completes when the JobFile has been saved.
///
public async Task Save()
{
if (FilePath.IsNullOrWhiteSpace()) throw new ArgumentException("FilePath Property is Empty");
await roboCommand.SaveAsJobFile(FilePath, true, true);
}
#pragma warning restore CS1573
#endregion
#region < IRoboCommand Interface >
#region < Events >
event RoboCommand.FileProcessedHandler IRoboCommand.OnFileProcessed
{
add
{
((IRoboCommand)roboCommand).OnFileProcessed += value;
}
remove
{
((IRoboCommand)roboCommand).OnFileProcessed -= value;
}
}
event RoboCommand.CommandErrorHandler IRoboCommand.OnCommandError
{
add
{
((IRoboCommand)roboCommand).OnCommandError += value;
}
remove
{
((IRoboCommand)roboCommand).OnCommandError -= value;
}
}
event RoboCommand.ErrorHandler IRoboCommand.OnError
{
add
{
((IRoboCommand)roboCommand).OnError += value;
}
remove
{
((IRoboCommand)roboCommand).OnError -= value;
}
}
event RoboCommand.CommandCompletedHandler IRoboCommand.OnCommandCompleted
{
add
{
((IRoboCommand)roboCommand).OnCommandCompleted += value;
}
remove
{
((IRoboCommand)roboCommand).OnCommandCompleted -= value;
}
}
event RoboCommand.CopyProgressHandler IRoboCommand.OnCopyProgressChanged
{
add
{
((IRoboCommand)roboCommand).OnCopyProgressChanged += value;
}
remove
{
((IRoboCommand)roboCommand).OnCopyProgressChanged -= value;
}
}
event RoboCommand.ProgressUpdaterCreatedHandler IRoboCommand.OnProgressEstimatorCreated
{
add
{
((IRoboCommand)roboCommand).OnProgressEstimatorCreated += value;
}
remove
{
((IRoboCommand)roboCommand).OnProgressEstimatorCreated -= value;
}
}
event UnhandledExceptionEventHandler IRoboCommand.TaskFaulted
{
add
{
((IRoboCommand)roboCommand).TaskFaulted += value;
}
remove
{
((IRoboCommand)roboCommand).TaskFaulted -= value;
}
}
#endregion
#region < Properties >
string IRoboCommand.Name => roboCommand.Name;
bool IRoboCommand.IsPaused => roboCommand.IsPaused;
bool IRoboCommand.IsRunning => roboCommand.IsRunning;
bool IRoboCommand.IsScheduled => roboCommand.IsScheduled;
bool IRoboCommand.IsCancelled => roboCommand.IsCancelled;
bool IRoboCommand.StopIfDisposing => roboCommand.StopIfDisposing;
IProgressEstimator IRoboCommand.IProgressEstimator => roboCommand.IProgressEstimator;
SelectionOptions IRoboCommand.SelectionOptions { get => ((IRoboCommand)roboCommand).SelectionOptions; set => ((IRoboCommand)roboCommand).SelectionOptions = value; }
RetryOptions IRoboCommand.RetryOptions { get => ((IRoboCommand)roboCommand).RetryOptions; set => ((IRoboCommand)roboCommand).RetryOptions = value; }
LoggingOptions IRoboCommand.LoggingOptions { get => roboCommand.LoggingOptions; set => roboCommand.LoggingOptions = value; }
CopyOptions IRoboCommand.CopyOptions { get => ((IRoboCommand)roboCommand).CopyOptions; set => ((IRoboCommand)roboCommand).CopyOptions = value; }
JobOptions IRoboCommand.JobOptions { get => ((IRoboCommand)roboCommand).JobOptions; }
RoboSharpConfiguration IRoboCommand.Configuration => roboCommand.Configuration;
string IRoboCommand.CommandOptions => roboCommand.CommandOptions;
#endregion
#region < Methods >
void IRoboCommand.Pause()
{
((IRoboCommand)roboCommand).Pause();
}
void IRoboCommand.Resume()
{
((IRoboCommand)roboCommand).Resume();
}
Task IRoboCommand.Start(string domain, string username, string password)
{
return ((IRoboCommand)roboCommand).Start(domain, username, password);
}
void IRoboCommand.Stop()
{
((IRoboCommand)roboCommand).Stop();
}
void IRoboCommand.Dispose()
{
roboCommand.Stop();
}
Task IRoboCommand.Start_ListOnly(string domain, string username, string password)
{
return roboCommand.Start_ListOnly();
}
Task IRoboCommand.StartAsync_ListOnly(string domain, string username, string password)
{
return roboCommand.StartAsync_ListOnly();
}
Task IRoboCommand.StartAsync(string domain, string username, string password)
{
return roboCommand.StartAsync_ListOnly();
}
RoboCopyResults IRoboCommand.GetResults()
{
return ((IRoboCommand)roboCommand).GetResults();
}
#endregion
#endregion
}
}