From 580393f218789af925606716898c7127a57ee66a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 19 Sep 2021 20:51:08 -0400 Subject: [PATCH] Re-add paplay as an option and make audio files configurable --- lib/notification/notify_xdg.go | 50 ++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/notification/notify_xdg.go b/lib/notification/notify_xdg.go index bc050ed..77bf7cf 100644 --- a/lib/notification/notify_xdg.go +++ b/lib/notification/notify_xdg.go @@ -18,23 +18,55 @@ package notification -import "os/exec" +import ( + "os" + "os/exec" +) + +var audioCommand string +var tryAudioCommands = []string{"paplay", "ogg123"} +var soundNormal = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga" +var soundCritical = "/usr/share/sounds/freedesktop/stereo/complete.oga" + +func getSoundPath(env, defaultPath string) string { + if path, ok := os.LookupEnv(env); ok { + // Sound file overriden by environment + return path + } else if _, err := os.Stat(defaultPath); os.IsNotExist(err) { + // Sound file doesn't exist, disable it + return "" + } else { + // Default sound file exists and wasn't overridden by environment + return defaultPath + } +} + +func init() { + var err error + for _, cmd := range tryAudioCommands { + if audioCommand, err = exec.LookPath(cmd); err == nil { + break + } + } + soundNormal = getSoundPath("GOMUKS_SOUND_NORMAL", soundNormal) + soundCritical = getSoundPath("GOMUKS_SOUND_CRITICAL", soundCritical) +} func Send(title, text string, critical, sound bool) error { args := []string{"-a", "gomuks"} if !critical { args = append(args, "-u", "low") } - // if iconPath { - // args = append(args, "-i", iconPath) - // } + //if iconPath { + // args = append(args, "-i", iconPath) + //} args = append(args, title, text) - if sound { - soundName := "message-new-instant" - if critical { - soundName = "complete" + if sound && len(audioCommand) > 0 && len(soundNormal) > 0 { + audioFile := soundNormal + if critical && len(soundCritical) > 0 { + audioFile = soundCritical } - exec.Command("ogg123", "/usr/share/sounds/freedesktop/stereo/"+soundName+".oga").Run() + _ = exec.Command(audioCommand, audioFile).Run() } return exec.Command("notify-send", args...).Run() }