/// <summary> /// Adds the specified sync task. /// </summary> /// <param name="syncTask">The sync task.</param> /// <param name="syncItem">The sync item.</param> internal void Add(SyncTask syncTask, SyncItemBase syncItem) { if (syncTask == null) { throw new ArgumentNullException("syncTask"); } if (syncItem == null) { throw new ArgumentNullException("syncItem"); } SyncTaskPreview syncTaskPreview; if (this.SyncTaskPreviewBySyncTask.TryGetValue(syncTask, out syncTaskPreview)) { syncTaskPreview.Add(syncItem); } else { syncTaskPreview = new SyncTaskPreview(); syncTaskPreview.Add(syncItem); this.SyncTaskPreviewBySyncTask[syncTask] = syncTaskPreview; } }
/// <summary> /// Removes the unnecessary sync items. /// </summary> internal void RemoveUnnecessarySyncItems() { var tasksToRemove = new List <SyncTask>(); foreach (var task in this.SyncTaskPreviewBySyncTask) { if (string.IsNullOrEmpty(task.Key.Filter)) { continue; } SyncTaskPreview syncTaskPreview = task.Value; syncTaskPreview.RemoveUnnecessarySyncItems(); if (syncTaskPreview.Count == 0) { tasksToRemove.Add(task.Key); } } foreach (var syncTask in tasksToRemove) { this.SyncTaskPreviewBySyncTask.Remove(syncTask); } }
/// <summary> /// Performs synchronization of the given sync preview. /// </summary> /// <param name="syncPreview">The sync preview.</param> /// <returns>The sync report.</returns> public SyncReport Synchronize(SyncPreview syncPreview) { if (syncPreview == null) { throw new ArgumentNullException("syncPreview"); } this.canceled = false; SyncReport syncReport = new SyncReport(); syncReport.TotalCopyFiles = syncPreview.CountOfActiveCopyFiles; syncReport.TotalDeleteFiles = syncPreview.CountOfActiveDeleteFiles; syncReport.TotalCreateDirectories = syncPreview.CountOfActiveCreateDirectories; syncReport.TotalDeleteDirectories = syncPreview.CountOfActiveDeleteDirectories; foreach (var task in syncPreview.SyncTaskPreviewBySyncTask) { if (this.canceled) { break; } SyncTask syncTask = task.Key; SyncTaskPreview syncTaskPreview = task.Value; syncTask.Status = Resources.SyncStatusDeletingFiles; this.syncLogger.Log(syncTask.ToString()); foreach (var syncItem in syncTaskPreview.DeleteFileSyncItems) { if (this.canceled) { break; } if (!syncItem.IsActive) { continue; } this.OnSynchronizing(syncItem.TargetPath); if (syncItem.Synchronize() == SyncResult.Error) { syncReport.AddFailedSyncItem(syncItem); } else { syncReport.SuccessDeleteFiles++; } this.syncLogger.Log(syncItem.ToString()); } syncTask.Status = Resources.SyncStatusDeletingDirectories; foreach (var syncItem in syncTaskPreview.DeleteDirectorySyncItems) { if (this.canceled) { break; } if (!syncItem.IsActive) { continue; } this.OnSynchronizing(syncItem.TargetPath); if (syncItem.Synchronize() == SyncResult.Error) { syncReport.AddFailedSyncItem(syncItem); } else { syncReport.SuccessDeleteDirectories++; } this.syncLogger.Log(syncItem.ToString()); } syncTask.Status = Resources.SyncStatusCreatingDirectories; foreach (var syncItem in syncTaskPreview.CreateDirectorySyncItems) { if (this.canceled) { break; } if (!syncItem.IsActive) { continue; } this.OnSynchronizing(syncItem.TargetPath); if (syncItem.Synchronize() == SyncResult.Error) { syncReport.AddFailedSyncItem(syncItem); } else { syncReport.SuccessCreateDirectories++; } this.syncLogger.Log(syncItem.ToString()); } syncTask.Status = Resources.SyncStatusCopyingFiles; foreach (var syncItem in syncTaskPreview.CopyFileSyncItems) { if (this.canceled) { break; } if (!syncItem.IsActive) { continue; } this.OnSynchronizing(syncItem.TargetPath); if (syncItem.Synchronize() == SyncResult.Error) { syncReport.AddFailedSyncItem(syncItem); } else { syncReport.SuccessCopyFiles++; } this.syncLogger.Log(syncItem.ToString()); } syncTask.Status = null; if (!this.canceled) { syncTask.LastSyncDate = DateTime.Now; } } syncReport.LogFilePath = this.syncLogger.WriteToFile(); return(syncReport); }