/// <summary> /// Modify value of save works objects stored in workList, if there is any null parameters the value attached isn't changed /// </summary> /// <param name="_nb">Index of the work you want to change in the list</param> /// <param name="_name">New name to apply to the work</param> /// <param name="_sourcePath">New source path to apply to the work</param> /// <param name="_destinationPath">New target destination path to apply to the work</param> /// <param name="_type">New type of save work to apply to the work</param> public void ChangeWork(int _nb, string _name, string _sourcePath, string _destinationPath, SaveWorkType _type, List <Extension> _extension) { if (_type != WorkList[_nb].Type && _type == SaveWorkType.complete) { WorkList[_nb] = new CompleteSaveWork(_name, _sourcePath, _destinationPath, _extension, SaveWorkType.complete); } else if (_type != WorkList[_nb].Type && _type == SaveWorkType.differencial) { WorkList[_nb] = new DifferencialSaveWork(_name, _sourcePath, _destinationPath, _extension, SaveWorkType.differencial); } else { if (_name != "") { WorkList[_nb].Name = _name; } if (_sourcePath != "") { WorkList[_nb].SourcePath = _sourcePath; } if (_destinationPath != "") { WorkList[_nb].DestinationPath = _destinationPath; } WorkList[_nb].ExtentionToEncryptList = _extension; } SetWorkIndex(); UpdateSaveFile(); EditLog.ChangeWorkLogLine(WorkList[_nb]); }
/// <summary> /// Can delete a save work (set to null) /// </summary> /// <param name="_nb">Index of the work in the list to delete</param> public void DeleteWork(int _nb) { WorkList.RemoveAt(_nb); SetWorkIndex(); UpdateSaveFile(); EditLog.DeleteWorkLogLine(_nb); }
/// <summary> /// Encrypt selected files /// </summary> public void EncryptFiles() { if (Progress.Cancelled) { return; } if (ExtentionToEncryptList != null && Directory.Exists(DestinationPath)) { // If we encrypt all files if (extentionToEncryptList.Contains(Extension.ALL)) { // Find all files string[] filesPathToEncrypt = Directory.GetFiles(destinationPath, "*.*", SearchOption.AllDirectories); // For each files foreach (string files in filesPathToEncrypt) { if (Progress.Cancelled) { return; } //Console.WriteLine(files); // Encrypt File Stopwatch watch = new Stopwatch(); watch.Start(); CryptoSoft.CryptoSoftTools.CryptoSoftEncryption(files); watch.Stop(); EditLog.EncryptedFile(this, files, watch.Elapsed.TotalSeconds.ToString()); } } // for each exntensions in the list foreach (Extension extension in extentionToEncryptList) { // Adjusts the format of the extension string extensionReformated = "*." + extension.ToString() + "*"; // Find all files in directory with aimed extensions string[] filesPathToEncrypt = Directory.GetFiles(destinationPath, extensionReformated, SearchOption.AllDirectories); // For each files with aimed extensions foreach (string files in filesPathToEncrypt) { if (Progress.Cancelled) { return; } //Console.WriteLine(files); // Encrypt File Stopwatch watch = new Stopwatch(); watch.Start(); CryptoSoft.CryptoSoftTools.CryptoSoftEncryption(files); watch.Stop(); EditLog.EncryptedFile(this, files, watch.Elapsed.TotalSeconds.ToString()); } } } }
/// <summary> /// Model Class constructor, initiate Save Work in a list (no parameters) /// </summary> public Model() { //Asign all delegate OnSaveWorkUpdate = UpdateSaveFile; OnProgressUpdate = UpdateAllSaveProgress; OnUpdateModelError = SetModelError; OnSocketResumeSave = ResumeSave; OnSocketPauseSave = PauseSave; OnSocketCancelSave = CancelSave; GlobalProgress = 0; //Init new settings ModelSettings = new Setting(); //Check if a setting file already exists, if false then create a new one, if true get the different value from it and apply it to the setting object (ModelSettings). if (!File.Exists("settings.json")) { ModelSettings.MaxTransferSize = 10000; ModelSettings.SoftwareString = ""; ModelSettings.PriorityExtension = new List <Extension>(); UpdateSettingsFile(); } else { string settingsFile = File.ReadAllText("settings.json"); var tempWorkList = JsonConvert.DeserializeObject <Setting>(settingsFile); ModelSettings = tempWorkList; UpdateSettingsFile(); } WorkList = new List <ISaveWork>(); //If the state file has not been initialized then create 5 SaveWork object from nothing if (!File.Exists("stateFile.json")) { WorkList.Add(new CompleteSaveWork("Default", "test", "test", null, SaveWorkType.complete)); UpdateSaveFile(); } //Then if the State file already exist, use the objects in it to create the WorkList else { string stateFile = File.ReadAllText("stateFile.json"); var tempWorkList = JsonConvert.DeserializeObject <List <CompleteSaveWork> >(stateFile); foreach (CompleteSaveWork work in tempWorkList) { if (work.Type == SaveWorkType.complete) { CreateCompleteWork(work.Name, work.SourcePath, work.DestinationPath, work.ExtentionToEncryptList); } else if (work.Type == SaveWorkType.differencial) { CreateDifferencialWork(work.Name, work.SourcePath, work.DestinationPath, work.ExtentionToEncryptList); } } UpdateSaveFile(); } EditLog.InitSoftwareLogLine(); }
/// <summary> /// Create a save work (with a differential save algorithm) /// </summary> /// <param name="_name">Name of the work (must be different from existing ones)</param> /// <param name="_source">The Source path to save</param> /// <param name="_destination">The Target destination to save files in</param> public void CreateDifferencialWork(string _name, string _source, string _destination, List <Extension> _extension) { DifferencialSaveWork work = new DifferencialSaveWork(_name, _source, _destination, _extension, SaveWorkType.differencial); WorkList.Add(work); SetWorkIndex(); UpdateSaveFile(); EditLog.CreateWorkLogLine(work); }
/// <summary> /// Create a save work (with a complete save algorithm) /// </summary> /// <param name="_name">Name of the work (must be different from existing ones)</param> /// <param name="_source">The Source path to save</param> /// <param name="_destination">The Target destination to save files in</param> public void CreateCompleteWork(string _name, string _source, string _destination, List <Extension> _extension) { CompleteSaveWork work = new CompleteSaveWork(_name, _source, _destination, _extension, SaveWorkType.complete); WorkList.Add(work); SetWorkIndex(); UpdateSaveFile(); EditLog.CreateWorkLogLine(work); }
/// <summary> /// Do a complete copy from a folder to another /// </summary> private void CompleteCopy() { if (Directory.Exists(SourcePath)) { //The Source directory has succesfully been found //Search directory info from source and target path var diSource = new DirectoryInfo(SourcePath); var diTarget = new DirectoryInfo(DestinationPath); //Calculate the number of file in the source directory and the total size of it int nbFiles = EasySaveInfo.CompleteFilesNumber(diSource); long directorySize = EasySaveInfo.CompleteSize(diSource); EditLog.FileToSaveFound(this, nbFiles, diSource, directorySize); lock (Model.sync) { CreateProgress(nbFiles, directorySize, nbFiles, 0, directorySize); IsActive = true; } Model.OnSaveWorkUpdate(); //initiate Copy from the source directory to the target directory EditLog.StartCopy(this); CompleteCopyAll(diSource, diTarget); lock (Model.sync) { Progress.IsEncrypting = true; } Model.OnSaveWorkUpdate(); //Start encryption file EditLog.StartEncryption(Index); EncryptFiles(); EditLog.EndEncryption(Index); //Closing the complete save protocol lock (Model.sync) { //DeleteProgress(); Progress.IsEncrypting = false; IsActive = false; } Model.OnSaveWorkUpdate(); EditLog.EndSaveProgram(Index); } else { //The Source Directory has not been found Model.OnUpdateModelError("directory"); } }
/// <summary> /// Cancel a specific save /// </summary> /// <param name="_nb">Index of the work in the list</param> public void CancelSave(int _nb) { lock (sync) { if (WorkList[_nb].Progress != null) { WorkList[_nb].Progress.Cancelled = true; EditLog.SaveCancelled(_nb); } } }
/// <summary> /// Resume a specific save /// </summary> /// <param name="_nb">Index of the work in the list</param> public void ResumeSave(int _nb) { lock (sync) { if (WorkList[_nb].Progress != null && WorkList[_nb].Progress.IsPaused != false) { WorkList[_nb].Progress.IsPaused = false; EditLog.SaveResumed(_nb); } } }
/// <summary> /// Pause a specific save /// </summary> /// <param name="_nb">Index of the work in the list</param> public void PauseSave(int _nb) { lock (sync) { if (WorkList[_nb].Progress != null && WorkList[_nb].Progress.IsPaused != true) { WorkList[_nb].Progress.IsPaused = true; EditLog.SavePaused(_nb); } } }
/// <summary> /// Copy each files from a directory and do the same for each subdirectory using recursion /// </summary> /// <param name="_nb">Index of the save work</param> /// <param name="_source">source directory path</param> /// <param name="_target">target destination directory path</param> private void CompleteCopyAll(DirectoryInfo _source, DirectoryInfo _target) { //Check of the different parameter that can stop or cancel the work (parameters stored in the Progress Update) if (Progress.Cancelled) { return; } bool softwareIsLaunched = false; while (Progress.IsPaused || EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString)) { if (Progress.Cancelled) { return; } if (EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString) && !softwareIsLaunched) { Model.OnUpdateModelError("software"); softwareIsLaunched = true; } } if (softwareIsLaunched) { Model.OnUpdateModelError("resume"); } //First create the new target directory where all the files are saved later on EditLog.CreateDirectoryLogLine(this, _target); Directory.CreateDirectory(_target.FullName); // Copy each file into the new directory. foreach (FileInfo fi in _source.GetFiles()) { lock (Model.sync) { Progress.CurrentSourceFilePath = fi.FullName; Progress.CurrentDestinationFilePath = Path.Combine(_target.FullName, fi.Name); } Model.OnSaveWorkUpdate(); string elapsedTime = ""; if (fi.Length >= Setting.maxTransferSize) { lock (SaveProgress.taken) { EditLog.StartCopyFileLogLine(this, fi); //Copy the file and measure execution time Stopwatch watch = new Stopwatch(); watch.Start(); fi.CopyTo(Path.Combine(_target.FullName, fi.Name), true); watch.Stop(); elapsedTime = watch.Elapsed.TotalSeconds.ToString(); } } else { EditLog.StartCopyFileLogLine(this, fi); //Copy the file and measure execution time Stopwatch watch = new Stopwatch(); watch.Start(); fi.CopyTo(Path.Combine(_target.FullName, fi.Name), true); watch.Stop(); elapsedTime = watch.Elapsed.TotalSeconds.ToString(); } lock (Model.sync) { Progress.FilesRemaining--; Progress.SizeRemaining -= fi.Length; Progress.UpdateProgressState(); } Model.OnSaveWorkUpdate(); EditLog.FinishCopyFileLogLine(this, fi, elapsedTime); //Check of the different parameter that can stop or cancel the work (parameters stored in the Progress Update) if (Progress.Cancelled) { return; } softwareIsLaunched = false; while (Progress.IsPaused || EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString)) { if (Progress.Cancelled) { return; } if (EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString) && !softwareIsLaunched) { Model.OnUpdateModelError("software"); softwareIsLaunched = true; } } if (softwareIsLaunched) { Model.OnUpdateModelError("resume"); } } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in _source.GetDirectories()) { DirectoryInfo nextTargetSubDir = _target.CreateSubdirectory(diSourceSubDir.Name); EditLog.EnterSubdirectoryLogLine(this, diSourceSubDir); CompleteCopyAll(diSourceSubDir, nextTargetSubDir); EditLog.ExitSubdirectoryLogLine(this, diSourceSubDir); } }
/// <summary> /// Launch the saving process /// </summary> /// <param name="obj">Thread friendly object</param> public void Save(object obj) { EditLog.StartSaveLogLine(this); EditLog.LaunchingSaveLogLine(Index); CompleteCopy(); }
/// <summary> /// Copy each files (that has been modified since the last save) from a directory, and do the same for each subdirectory using recursion /// </summary> /// <param name="_nb">Index of the save work</param> /// <param name="_source">source directory path</param> /// <param name="_target">target destination directory path</param> private void DifferencialCopyAll(DirectoryInfo _source, DirectoryInfo _target) { if (Progress.Cancelled) { return; } bool softwareIsLaunched = false; while (Progress.IsPaused || EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString)) { if (Progress.Cancelled) { return; } if (EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString) && !softwareIsLaunched) { Model.OnUpdateModelError("software"); softwareIsLaunched = true; } } if (softwareIsLaunched) { Model.OnUpdateModelError("resume"); } Directory.CreateDirectory(_target.FullName); EditLog.CreateDirectoryLogLine(this, _target); // Copy each file into the new directory. foreach (FileInfo fi in _source.GetFiles()) { //Calculate the path of the future file we need to save string targetPath = Path.Combine(_target.FullName, fi.Name); //Check if the file already exist or not (new one), and verify if it has been modified or not if (!File.Exists(targetPath) || fi.LastWriteTime != File.GetLastWriteTime(targetPath)) { lock (Model.sync) { Progress.CurrentSourceFilePath = fi.FullName; Progress.CurrentDestinationFilePath = Path.Combine(_target.FullName, fi.Name); } Model.OnSaveWorkUpdate(); EditLog.StartCopyFileLogLine(this, fi); string elapsedTime = ""; if (fi.Length >= Setting.maxTransferSize) { lock (SaveProgress.taken) { EditLog.StartCopyFileLogLine(this, fi); //Copy the file and measure execution time Stopwatch watch = new Stopwatch(); watch.Start(); fi.CopyTo(Path.Combine(_target.FullName, fi.Name), true); watch.Stop(); elapsedTime = watch.Elapsed.TotalSeconds.ToString(); } } else { EditLog.StartCopyFileLogLine(this, fi); //Copy the file and measure execution time Stopwatch watch = new Stopwatch(); watch.Start(); fi.CopyTo(Path.Combine(_target.FullName, fi.Name), true); watch.Stop(); elapsedTime = watch.Elapsed.TotalSeconds.ToString(); } lock (Model.sync) { Progress.FilesRemaining--; Progress.SizeRemaining -= fi.Length; Progress.UpdateProgressState(); } Model.OnSaveWorkUpdate(); EditLog.FinishCopyFileLogLine(this, fi, elapsedTime); if (Progress.Cancelled) { return; } softwareIsLaunched = false; while (Progress.IsPaused || EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString)) { if (Progress.Cancelled) { return; } if (EasySaveInfo.CheckIfSoftwareIsLaunched(Setting.softwareString) && !softwareIsLaunched) { Model.OnUpdateModelError("software"); softwareIsLaunched = true; } } if (softwareIsLaunched) { Model.OnUpdateModelError("resume"); } } } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in _source.GetDirectories()) { string targetDirectoryPath = Path.Combine(_target.FullName, diSourceSubDir.Name); EditLog.EnterSubdirectoryLogLine(this, diSourceSubDir); //Check if the directory already exist to decide if it is required to create a new one or not if (!Directory.Exists(targetDirectoryPath)) { DirectoryInfo nextTargetSubDir = _target.CreateSubdirectory(diSourceSubDir.Name); DifferencialCopyAll(diSourceSubDir, nextTargetSubDir); } else { DirectoryInfo nextTargetSubDir = new DirectoryInfo(targetDirectoryPath); DifferencialCopyAll(diSourceSubDir, nextTargetSubDir); } EditLog.ExitSubdirectoryLogLine(this, diSourceSubDir); } }
/// <summary> /// Do a differencial copy from a folder to another /// </summary> private void DifferencialCopy() { if (Directory.Exists(SourcePath)) { //Search directory info from source and target path var diSource = new DirectoryInfo(SourcePath); var diTarget = new DirectoryInfo(DestinationPath); //Calculate the number of file in the source directory and the total size of it (of all ) int nbFiles = EasySaveInfo.DifferencialFilesNumber(diSource, diTarget); long directorySize = EasySaveInfo.DifferencialSize(diSource, diTarget); //If there is at least one file to save then initiate the differencial saving protocol if (nbFiles != 0) { EditLog.FileToSaveFound(this, nbFiles, diSource, directorySize); lock (Model.sync) { CreateProgress(nbFiles, directorySize, nbFiles, 0, directorySize); IsActive = true; } Model.OnSaveWorkUpdate(); //initiate Copy from the source directory to the target directory (only the file / directory that has been modified or are new) EditLog.StartCopy(this); DifferencialCopyAll(diSource, diTarget); lock (Model.sync) { Progress.IsEncrypting = true; } Model.OnSaveWorkUpdate(); EditLog.StartEncryption(Index); EncryptFiles(); EditLog.EndEncryption(Index); lock (Model.sync) { //DeleteProgress(); Progress.IsEncrypting = false; IsActive = false; } Model.OnSaveWorkUpdate(); EditLog.EndSaveProgram(Index); } //If there is no file to save then cancel the saving protocol else { EditLog.NoFilesFound(Index); } } else { Model.OnUpdateModelError("directory"); } }
/// <summary> /// Launch the saving process /// </summary> /// <param name="obj">Thread friendly object</param> public void Save(object obj) { EditLog.StartSaveLogLine(this); EditLog.LaunchingSaveLogLine(Index); DifferencialCopy(); }