1
0
Files
Toak/IO/Notifications.cs

85 lines
2.7 KiB
C#

using System.Diagnostics;
namespace Toak.IO;
public static class Notifications
{
private static bool _notifySendAvailable = true;
public static void Notify(string summary, string body = "")
{
if (!_notifySendAvailable) return;
try
{
var pInfo = new ProcessStartInfo
{
FileName = "notify-send",
Arguments = $"-a \"Toak\" \"{summary}\" \"{body}\"",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(pInfo);
}
catch (System.ComponentModel.Win32Exception)
{
Console.WriteLine("[Notifications] 'notify-send' executable not found. Notifications will be disabled.");
_notifySendAvailable = false;
}
catch (Exception ex)
{
Console.WriteLine($"[Notifications] Failed to send notification: {ex.Message}");
}
}
private static bool _paplayAvailable = true;
public static void PlaySound(string soundPath)
{
if (!_paplayAvailable || string.IsNullOrWhiteSpace(soundPath)) return;
try
{
var absolutePath = soundPath;
if (!Path.IsPathRooted(absolutePath))
absolutePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, absolutePath);
if (!File.Exists(absolutePath))
{
var resourceName = "Toak." + soundPath.Replace("/", ".").Replace("\\", ".");
using var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
if (stream != null)
{
absolutePath = Path.Combine(Path.GetTempPath(), "toak_" + Path.GetFileName(soundPath));
if (!File.Exists(absolutePath))
{
using var fileStream = File.Create(absolutePath);
stream.CopyTo(fileStream);
}
}
else
{
return;
}
}
var pInfo = new ProcessStartInfo
{
FileName = "paplay",
Arguments = $"\"{absolutePath}\"",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(pInfo);
}
catch (System.ComponentModel.Win32Exception)
{
Console.WriteLine("[Notifications] 'paplay' executable not found. Sound effects will be disabled.");
_paplayAvailable = false;
}
catch (Exception ex)
{
Console.WriteLine($"[Notifications] Failed to play sound: {ex.Message}");
}
}
}