1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-11 11:34:07 +02:00

Move sound notification code to a separate class

This commit is contained in:
chylex 2017-03-05 14:27:47 +01:00
parent 7d7bfb7b01
commit 1e538d2b28
3 changed files with 73 additions and 43 deletions

View File

@ -13,12 +13,10 @@
using TweetDck.Plugins;
using TweetDck.Plugins.Enums;
using TweetDck.Plugins.Events;
using System.Media;
using TweetDck.Core.Bridge;
using TweetDck.Core.Notification;
using TweetDck.Core.Notification.Screenshot;
using TweetDck.Updates.Events;
using System.IO;
namespace TweetDck.Core{
sealed partial class FormBrowser : Form{
@ -43,8 +41,7 @@ private static UserConfig Config{
private FormWindowState prevState;
private TweetScreenshotManager notificationScreenshotManager;
private SoundPlayer notificationSound;
private bool ignoreNotificationSoundError;
private SoundNotification soundNotification;
public FormBrowser(PluginManager pluginManager, UpdaterSettings updaterSettings){
InitializeComponent();
@ -90,8 +87,8 @@ public FormBrowser(PluginManager pluginManager, UpdaterSettings updaterSettings)
notificationScreenshotManager.Dispose();
}
if (notificationSound != null){
notificationSound.Dispose();
if (soundNotification != null){
soundNotification.Dispose();
}
};
@ -372,45 +369,11 @@ public void PlayNotificationSound(){
return;
}
if (notificationSound == null){
notificationSound = new SoundPlayer{
LoadTimeout = 5000
};
if (soundNotification == null){
soundNotification = new SoundNotification(this);
}
if (notificationSound.SoundLocation != Config.NotificationSoundPath){
notificationSound.SoundLocation = Config.NotificationSoundPath;
ignoreNotificationSoundError = false;
}
try{
notificationSound.Play();
}catch(FileNotFoundException e){
OnNotificationSoundError("File not found: "+e.FileName);
}catch(InvalidOperationException){
OnNotificationSoundError("File is not a valid sound file.");
}catch(TimeoutException){
OnNotificationSoundError("File took too long to load.");
}
}
private void OnNotificationSoundError(string message){
if (!ignoreNotificationSoundError){
ignoreNotificationSoundError = true;
using(FormMessage form = new FormMessage("Notification Sound Error", "Could not play custom notification sound."+Environment.NewLine+message, MessageBoxIcon.Error)){
form.AddButton("Ignore");
Button btnOpenSettings = form.AddButton("Open Settings");
btnOpenSettings.Width += 16;
btnOpenSettings.Location = new Point(btnOpenSettings.Location.X-16, btnOpenSettings.Location.Y);
if (form.ShowDialog() == DialogResult.OK && form.ClickedButton == btnOpenSettings){
OpenSettings();
currentFormSettings.SelectTab(FormSettings.TabIndexNotification);
}
}
}
soundNotification.Play(Config.NotificationSoundPath);
}
public void OnTweetScreenshotReady(string html, int width, int height){

View File

@ -0,0 +1,66 @@
using System;
using System.Drawing;
using System.IO;
using System.Media;
using System.Windows.Forms;
using TweetDck.Core.Other;
namespace TweetDck.Core.Notification{
class SoundNotification : IDisposable{
private readonly FormBrowser browserForm;
private SoundPlayer notificationSound;
private bool ignoreNotificationSoundError;
public SoundNotification(FormBrowser browserForm){
this.browserForm = browserForm;
}
public void Play(string file){
if (notificationSound == null){
notificationSound = new SoundPlayer{
LoadTimeout = 5000
};
}
if (notificationSound.SoundLocation != file){
notificationSound.SoundLocation = file;
ignoreNotificationSoundError = false;
}
try{
notificationSound.Play();
}catch(FileNotFoundException e){
OnNotificationSoundError("File not found: "+e.FileName);
}catch(InvalidOperationException){
OnNotificationSoundError("File is not a valid sound file.");
}catch(TimeoutException){
OnNotificationSoundError("File took too long to load.");
}
}
private void OnNotificationSoundError(string message){
if (!ignoreNotificationSoundError){
ignoreNotificationSoundError = true;
using(FormMessage form = new FormMessage("Notification Sound Error", "Could not play custom notification sound."+Environment.NewLine+message, MessageBoxIcon.Error)){
form.AddButton("Ignore");
Button btnOpenSettings = form.AddButton("Open Settings");
btnOpenSettings.Width += 16;
btnOpenSettings.Location = new Point(btnOpenSettings.Location.X-16, btnOpenSettings.Location.Y);
if (form.ShowDialog() == DialogResult.OK && form.ClickedButton == btnOpenSettings){
browserForm.OpenSettings(FormSettings.TabIndexNotification);
}
}
}
}
public void Dispose(){
if (notificationSound != null){
notificationSound.Dispose();
}
}
}
}

View File

@ -113,6 +113,7 @@
<Compile Include="Core\Handling\FileDialogHandler.cs" />
<Compile Include="Core\Handling\JavaScriptDialogHandler.cs" />
<Compile Include="Core\Handling\LifeSpanHandler.cs" />
<Compile Include="Core\Notification\SoundNotification.cs" />
<Compile Include="Core\Notification\TweetNotification.cs" />
<Compile Include="Core\Other\FormAbout.cs">
<SubType>Form</SubType>