Example #1
0
        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;
        }
 private void RunBackup()
 {
     _stopwatch.Start();
     _dispatcherTimer.Start();
     Task.Run(() =>
     {
         FinishButtonTitle = "Cancel Backup";
         Status            = "Getting backup size...";
         if (!Directory.Exists(BackupLocation))
         {
             TellUserBackupFailed("Error: Backup directory doesn't exist");
         }
         else
         {
             _backupPerformer.CalculateBackupSize(Items, BackupLocation);
             ulong freeDriveBytes = Utilities.DriveFreeBytes(BackupLocation);
             if (_totalBackupSize > freeDriveBytes)
             {
                 var error = string.Format("Can't perform backup: not enough free space -- need {0} but only have {1}",
                                           ByteSize.FromBytes(_totalBackupSize), ByteSize.FromBytes(freeDriveBytes));;
                 TellUserBackupFailed(error);
             }
             else
             {
                 Status = "Performing backup...";
                 _backupPerformer.PerformBackup(Items, BackupLocation);
                 if (!_backupPerformer.HadError) // if error, message already handled
                 {
                     if (_backupPerformer.HasBeenCanceled)
                     {
                         TellUserBackupFailed("Backup was canceled");
                     }
                     else
                     {
                         TellUserBackupSucceeded("Backup successfully finished");
                     }
                 }
             }
         }
     });
 }