/// <summary> /// Load the user's sync options. This includes source and destinations, and more. If a problem is found in reading from saved config, a new empty config will be returned. /// </summary> /// <returns></returns> public static SyncConfig CreateFromFile() { SyncConfig sync = new SyncConfig { SourceDir = Machine.MyPicturesDirectory, DestinationDir = string.Empty, FileExtensions = DefaultFileExtensions.ToList() }; EnsureDefaultSyncFileExists(); try { string fileContents = File.ReadAllText(SaveFile); if (fileContents != string.Empty) { sync = JsonConvert.DeserializeObject <SyncConfig>(fileContents); } } catch (Exception) { } return(sync); }
/// <summary> /// Load the user's sync options. This includes source and destinations, and more. If a problem is found in reading from saved config, a new empty config will be returned. /// </summary> /// <returns></returns> public static SyncConfig CreateFromFile() { SyncConfig sync = new SyncConfig { SourceDir = Machine.MyPicturesDirectory, DestinationDir = string.Empty, FileExtensions = DefaultFileExtensions.ToList() }; EnsureDefaultSyncFileExists(); try { string fileContents = File.ReadAllText(SaveFile); if (fileContents != string.Empty) { sync = JsonConvert.DeserializeObject<SyncConfig>(fileContents); } } catch (Exception) { } return sync; }
private ConfigValidCheckResult ValidateConfigCreateMissingConfigMessage(SyncConfig config) { var result = new ConfigValidCheckResult { HasValidConfig = true }; if (config == null) { result.HasValidConfig = false; } else { if (config.DestinationDir.IsNullOrWhitespace()) { result.Message += "There isn't a destination media directory defined." + Environment.NewLine; result.HasValidConfig = false; } if (config.SourceDir.IsNullOrWhitespace()) { result.Message += "There isn't a source media directory defined." + Environment.NewLine; result.HasValidConfig = false; } } return result; }
private void btnSave_Click(object sender, EventArgs e) { SetDefaultsWhenEmpty(); var syncConfig = new SyncConfig { DestinationDir = txtDestinationDir.Text.Trim(), SourceDir = txtSourceDir.Text.Trim(), ShouldDeleteSourceWhenSuccessfullyCompleted = chkDeleteSourceAfterCopy.Checked, ShouldWarnOnDelete = chkWarnOnDelete.Checked, ShouldLogDebug = chkLogOutput.Checked, FileExtensions = txtFileExtensions.Text.Trim().Split(ExtensionSeparators.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList() }; syncConfig.Save(); this.Close(); OptionsUpdated(sender, e); }
/// <summary> /// Given the configuration options, write it to the form elements. /// </summary> /// <param name="syncConfig"></param> private void WriteConfigToForm(SyncConfig syncConfig) { txtSourceDir.Text = syncConfig.SourceDir.Trim(); txtDestinationDir.Text = syncConfig.DestinationDir.Trim(); txtFileExtensions.Text = string.Join(" ", syncConfig.FileExtensions); chkDeleteSourceAfterCopy.Checked = syncConfig.ShouldDeleteSourceWhenSuccessfullyCompleted; chkWarnOnDelete.Checked = syncConfig.ShouldWarnOnDelete; chkLogOutput.Checked = syncConfig.ShouldLogDebug; }
public FileSyncer(SyncConfig config) { Config = config; FileHelper = new FileIOHelper(); }
private async Task DeleteSourceFilesIfRequired(SyncConfig syncConfig, IEnumerable<CopyTask> copyTasks) { if (syncConfig.ShouldDeleteSourceWhenSuccessfullyCompleted) { DialogResult shouldContinue = MessageBox.Show(text: "Ready to delete source files on {0}?".FormatWith(syncConfig.SourceDir), caption: "MediaSync", buttons: MessageBoxButtons.YesNo); if (shouldContinue == DialogResult.Yes) { var itemsToDelete = copyTasks.Where(x => x.CopyResult == CopyResult.CopiedSuccessfully || x.CopyResult == CopyResult.AlreadyExisted) .Select(x => x.SourceFile); await DeleteSourceFiles(itemsToDelete, syncConfig.ShouldLogDebug); } } }
private async Task Sync(SyncConfig syncConfig) { bool shouldLog = syncConfig.ShouldLogDebug; //do a sanity check on the config to avoid exceptions while processing if (ValidateConfigCreateMissingConfigMessage(syncConfig).HasValidConfig == false) { return; } Stopwatch stopwatch = new Stopwatch(); try { ShowBalloon("Finding items..."); LastRunOn = DateTime.Now; lastRunToolStripMenuItem.Text = "Syncing."; stopwatch.Start(); var fileSync = new FileSyncer(syncConfig); var copyResult = new SyncCopyResult(); List<CopyTask> copyTasks = fileSync.BuildFileCopyTasks().ToList(); if (shouldLog) { await FileHelper.WriteToErrLogAsync("Found {0} items to sync.".FormatWith(copyTasks.Count)); } if (copyTasks.Any()) { ShowBalloon("Copying {0} items...".FormatWith(copyTasks.Count)); copyResult = await fileSync.ExecuteFileCopyTasks(copyTasks); if (shouldLog) { await FileHelper.WriteToErrLogAsync("Completed file copy tasks"); } await DeleteSourceFilesIfRequired(syncConfig, copyTasks); } stopwatch.Stop(); copyResult.TimeElapsedMsg = "Total sync time was {0}:{1}".FormatWith( stopwatch.Elapsed.Minutes.ToString("00"), stopwatch.Elapsed.Seconds.ToString("00")); if (shouldLog) { await FileHelper.WriteToErrLogAsync(copyResult.TimeElapsedMsg); } ShowCompletionBalloon(copyResult, syncConfig.DestinationDir); } catch (Exception ex) { var fileIOHelper = new FileIOHelper(); fileIOHelper.WriteToErrLog(ex.Message); ShowBalloon("Sorry we had a problem when syncing. The log file has more details. " + FileIOHelper.OutputLogFile, 10); } }