Skip to content

Commit

Permalink
Play sound on successful or failed backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Deadpikle committed Feb 6, 2019
1 parent c2c161f commit e13cafc
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 9 deletions.
3 changes: 3 additions & 0 deletions EasyBackup/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<setting name="UpgradeRequired" serializeAs="String">
<value>True</value>
</setting>
<setting name="PlaySoundsWhenFinished" serializeAs="String">
<value>False</value>
</setting>
</EasyBackup.Properties.Settings>
</userSettings>
</configuration>
12 changes: 12 additions & 0 deletions EasyBackup/EasyBackup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@
<Content Include="Licenses.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Sounds\failure-tbone.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Sounds\jingle-achievement.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Sounds\success.mp3">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Sounds\success.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
12 changes: 12 additions & 0 deletions EasyBackup/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions EasyBackup/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="PlaySoundsWhenFinished" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
Binary file added EasyBackup/Sounds/failure-tbone.wav
Binary file not shown.
Binary file added EasyBackup/Sounds/success.wav
Binary file not shown.
43 changes: 36 additions & 7 deletions EasyBackup/ViewModels/BackupInProgressVIewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
Expand All @@ -25,6 +26,9 @@ class BackupInProgressViewModel : BaseViewModel
private Dictionary<FolderFileItem, FolderFileCopyProgress> _copyDataToProgressMap; // for easy lookup on progress updates
private double _currentProgress;

private bool _playsSounds;
private SoundPlayer _successSoundPlayer;
private SoundPlayer _failureSoundPlayer;

public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List<FolderFileItem> items, string backupLocation) : base(viewModelChanger)
{
Expand All @@ -47,6 +51,12 @@ public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List<FolderF
_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");
}
RunBackup();
}

Expand Down Expand Up @@ -94,8 +104,7 @@ private void RunBackup()
Status = "Getting backup size...";
if (!Directory.Exists(BackupLocation))
{
Status = "Error: Backup directory doesn't exist";
FinishButtonTitle = "Finish Backup";
TellUserBackupFailed("Error: Backup directory doesn't exist");
StatusColor = redBrush;
}
else
Expand All @@ -104,8 +113,9 @@ private void RunBackup()
ulong freeDriveBytes = Utilities.DriveFreeBytes(BackupLocation);
if (_totalBackupSize > freeDriveBytes)
{
Status = string.Format("Can't perform backup: not enough free space -- need {0} but only have {1}",
ByteSize.FromBytes(_totalBackupSize), ByteSize.FromBytes(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);
StatusColor = redBrush;
}
else
Expand All @@ -114,20 +124,39 @@ private void RunBackup()
_backupPerformer.PerformBackup(Items, BackupLocation);
if (_backupPerformer.HasBeenCanceled)
{
Status = "Backup was canceled";
TellUserBackupFailed("Backup was canceled");
StatusColor = redBrush;
}
else
{
Status = "Backup successfully finished";
TellUserBackupSucceeded("Backup successfully finished");
StatusColor = greenBrush;
}
FinishButtonTitle = "Finish Backup";
}
}
});
}

private void TellUserBackupSucceeded(string message)
{
Status = message;
if (_playsSounds)
{
_successSoundPlayer.Play();
}
FinishButtonTitle = "Finish Backup";
}

private void TellUserBackupFailed(string message)
{
Status = message;
if (_playsSounds)
{
_failureSoundPlayer.Play();
}
FinishButtonTitle = "Finish Backup";
}

private void _backupPerformer_CalculatedBytesOfItem(FolderFileItem item, ulong bytes)
{
if (_copyDataToProgressMap.ContainsKey(item))
Expand Down
11 changes: 11 additions & 0 deletions EasyBackup/ViewModels/SetupBackupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ public bool IsCheckBackupSizeEnabled
get { return !_isCheckingBackupSize; }
}

public bool PlaysSoundsOnComplete
{
get { return Properties.Settings.Default.PlaySoundsWhenFinished; }
set
{
Properties.Settings.Default.PlaySoundsWhenFinished = value;
Properties.Settings.Default.Save();
NotifyPropertyChanged();
}
}

public ICommand AddFolder
{
get { return new RelayCommand(ChooseFolder); }
Expand Down
11 changes: 11 additions & 0 deletions EasyBackup/Views/AboutWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
<Hyperlink NavigateUri="https://icons8.com/icon/63322/synchronize"
RequestNavigate="Hyperlink_RequestNavigate">Synchronize Icon in Flat Style</Hyperlink>.
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="5,5,5,5">
• The success sound on the backup screen was created by
<Hyperlink NavigateUri="https://freesound.org/people/LittleRobotSoundFactory/sounds/270404/"
RequestNavigate="Hyperlink_RequestNavigate">LittleRobotSoundFactory</Hyperlink>.
</TextBlock>
<TextBlock TextWrapping="Wrap" Margin="5,5,5,5">
• The failure sound on the backup screen was created by
<Hyperlink NavigateUri="http://soundbible.com/1830-Sad-Trombone.html"
RequestNavigate="Hyperlink_RequestNavigate">Joe Lamb</Hyperlink>.
</TextBlock>
<Label Content="Open Source Licenses" FontWeight="Bold" FontSize="14" Margin="0,2,0,2"/>
<TextBlock Name="LicensesText" TextWrapping="Wrap" xmlns:space="preserve" Margin="5">

</TextBlock>
Expand Down
5 changes: 3 additions & 2 deletions EasyBackup/Views/SetupBackup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:local="clr-namespace:EasyBackup.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DesignHeight="600" d:DesignWidth="800"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}">
<UserControl.Resources>
Expand Down Expand Up @@ -87,7 +87,7 @@
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Vertical" Margin="5">
<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Vertical" Margin="5" Grid.RowSpan="1">
<Button Content="Add File" Margin="0,5,0,5" Command="{Binding AddFile}"/>
<Button Content="Add Folder" Margin="0,5,0,5" Command="{Binding AddFolder}"/>
<Button Content="Remove File/Folder" Margin="0,5,0,5" CommandParameter="{Binding ElementName=PathsDataGrid, Path=SelectedItems}"
Expand All @@ -96,6 +96,7 @@
<Button Content="Save Backup Template" Margin="0,5,0,5" Command="{Binding SaveTemplate}"/>
<Button Content="Load Backup Template" Margin="0,5,0,5" Command="{Binding LoadTemplate}"/>
<Button Content="Settings" Margin="0,5,0,5" Visibility="Collapsed"/>
<CheckBox Content="Play Sounds When Finished" Margin="0,5,0,5" IsChecked="{Binding PlaysSoundsOnComplete}" FontSize="14"/>
<Button Content="About Easy Backup" Margin="0,5,0,5" Command="{Binding ShowAboutWindow}"/>
</StackPanel>
<Button Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Content="Choose Backup Location" Width="200" Command="{Binding ChooseBackupLocation}"
Expand Down

0 comments on commit e13cafc

Please sign in to comment.