private async void ScanBackupAndCheckSize() { IsCheckingBackupSize = true; IsCheckBackupSizeStatusVisible = false; _totalBackupSize = 0; ulong freeDriveBytes = 0; _backupSizeChecker = new BackupPerformer(); _backupSizeChecker.CalculatedBytesOfItem += BackupPerformer_CalculatedBytesOfItem; IsCancelCheckBackupSizeEnabled = true; var redBrush = new SolidColorBrush(Colors.Red); bool didFail = false; await Task.Run(() => { try { if (!Directory.Exists(BackupLocation)) { CheckBackupSizeBrush = redBrush; CheckBackupSizeStatus = "Backup directory doesn't exist"; didFail = true; } else { _backupSizeChecker.CalculateBackupSize(Items.ToList(), BackupLocation); freeDriveBytes = Utilities.DriveFreeBytes(BackupLocation); } } catch (Exception e) { CheckBackupSizeBrush = new SolidColorBrush(Colors.Red); CheckBackupSizeStatus = string.Format("Failed to check size of backup -- {0}", e.Message); didFail = true; } }); if (!_backupSizeChecker.HasBeenCanceled && !didFail) { if (_totalBackupSize > freeDriveBytes) { CheckBackupSizeBrush = redBrush; CheckBackupSizeStatus = string.Format("Not enough free space -- need {0} but only have {1}", ByteSize.FromBytes(_totalBackupSize), ByteSize.FromBytes(freeDriveBytes)); } else { CheckBackupSizeBrush = new SolidColorBrush(Colors.Green); CheckBackupSizeStatus = string.Format("There's enough space available! We need {0} and have {1} available.", ByteSize.FromBytes(_totalBackupSize), ByteSize.FromBytes(freeDriveBytes)); } } IsCheckBackupSizeStatusVisible = true; _backupSizeChecker.CalculatedBytesOfItem -= BackupPerformer_CalculatedBytesOfItem; _backupSizeChecker = null; IsCheckingBackupSize = false; IsCancelCheckBackupSizeEnabled = false; }
public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List <FolderFileItem> items, string backupLocation, bool compressesOutput = false, SecureString compressedPassword = null) : base(viewModelChanger) { Items = items; BackupLocation = backupLocation; _currentProgress = 0; _backupPerformer = new BackupPerformer(); Status = "Running backup"; StatusColor = new SolidColorBrush(Colors.Black); ItemProgressData = new List <FolderFileCopyProgress>(); _copyDataToProgressMap = new Dictionary <FolderFileItem, FolderFileCopyProgress>(); foreach (FolderFileItem item in Items) { var progress = new FolderFileCopyProgress(item.Path); _copyDataToProgressMap.Add(item, progress); ItemProgressData.Add(progress); } _backupPerformer.StartedCopyingItem += _backupPerformer_StartedCopyingItem; _backupPerformer.FinishedCopyingItem += _backupPerformer_FinishedCopyingItem; _backupPerformer.CopiedBytesOfItem += _backupPerformer_CopiedBytesOfItem; _backupPerformer.BackupFailed += _backupPerformer_BackupFailed; _backupPerformer.CalculatedBytesOfItem += _backupPerformer_CalculatedBytesOfItem; _playsSounds = Properties.Settings.Default.PlaySoundsWhenFinished; if (_playsSounds) { _failureSoundPlayer = new SoundPlayer("Sounds/failure-tbone.wav"); _successSoundPlayer = new SoundPlayer("Sounds/success.wav"); } if (compressesOutput) { _backupPerformer.UsesCompressedFile = true; if (compressedPassword != null) { _backupPerformer.UsesPasswordForCompressedFile = true; _backupPerformer.CompressedFilePassword = compressedPassword; } } _dispatcherTimer = new DispatcherTimer(); _stopwatch = new Stopwatch(); CurrentTimeString = "00:00:00"; RunningLabel = "Running"; _dispatcherTimer.Tick += new EventHandler(BackupLengthDispatcherTimerTick); _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1); RunBackup(); }