| 1 |
/* |
| 2 |
Copyright 2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package sounds |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"os" |
| 22 |
"os/exec" |
| 23 |
"path/filepath" |
| 24 |
"strings" |
| 25 |
|
| 26 |
"github.com/adrg/xdg" |
| 27 |
|
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/accounts" |
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/click/cnotificationsettings" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 33 |
) |
| 34 |
|
| 35 |
type Sound interface { |
| 36 |
// Present() presents the notification audibly if applicable. |
| 37 |
Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool |
| 38 |
// GetSound() returns absolute path to the file the given notification will play. |
| 39 |
GetSound(app *click.AppId, nid string, notification *launch_helper.Notification) string |
| 40 |
} |
| 41 |
|
| 42 |
type sound struct { |
| 43 |
player string |
| 44 |
log logger.Logger |
| 45 |
acc accounts.Accounts |
| 46 |
fallback string |
| 47 |
dataDirs func() []string |
| 48 |
dataFind func(string) (string, error) |
| 49 |
} |
| 50 |
|
| 51 |
func New(log logger.Logger, acc accounts.Accounts, fallback string) *sound { |
| 52 |
return &sound{ |
| 53 |
player: "paplay", |
| 54 |
log: log, |
| 55 |
acc: acc, |
| 56 |
fallback: fallback, |
| 57 |
dataDirs: func() []string { return xdg.DataDirs }, |
| 58 |
dataFind: xdg.SearchDataFile, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func (snd *sound) Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool { |
| 63 |
if notification == nil { |
| 64 |
panic("please check notification is not nil before calling present") |
| 65 |
} |
| 66 |
|
| 67 |
absPath := snd.GetSound(app, nid, notification) |
| 68 |
if absPath == "" { |
| 69 |
return false |
| 70 |
} |
| 71 |
|
| 72 |
snd.log.Debugf("[%s] playing sound %s using %s", nid, absPath, snd.player) |
| 73 |
cmd := exec.Command(snd.player, absPath) |
| 74 |
err := cmd.Start() |
| 75 |
if err != nil { |
| 76 |
snd.log.Debugf("[%s] unable to play: %v", nid, err) |
| 77 |
return false |
| 78 |
} |
| 79 |
go func() { |
| 80 |
err := cmd.Wait() |
| 81 |
if err != nil { |
| 82 |
snd.log.Debugf("[%s] error playing sound %s: %v", nid, absPath, err) |
| 83 |
} |
| 84 |
}() |
| 85 |
return true |
| 86 |
} |
| 87 |
|
| 88 |
var canUseSoundsNotify = cnotificationsettings.CanUseSoundsNotify |
| 89 |
|
| 90 |
// Returns the absolute path of the sound to be played for app, nid and notification. |
| 91 |
func (snd *sound) GetSound(app *click.AppId, nid string, notification *launch_helper.Notification) string { |
| 92 |
if !canUseSoundsNotify(app) { |
| 93 |
snd.log.Debugf("[%s] sounds disabled by user for this app.", nid) |
| 94 |
return "" |
| 95 |
} |
| 96 |
|
| 97 |
if snd.acc.SilentMode() { |
| 98 |
snd.log.Debugf("[%s] no sounds: silent mode on.", nid) |
| 99 |
return "" |
| 100 |
} |
| 101 |
|
| 102 |
fallback := snd.acc.MessageSoundFile() |
| 103 |
if fallback == "" { |
| 104 |
fallback = snd.fallback |
| 105 |
} |
| 106 |
|
| 107 |
sound := notification.Sound(fallback) |
| 108 |
if sound == "" { |
| 109 |
snd.log.Debugf("[%s] notification has no Sound: %#v", nid, sound) |
| 110 |
return "" |
| 111 |
} |
| 112 |
absPath := snd.findSoundFile(app, nid, sound) |
| 113 |
if absPath == "" { |
| 114 |
snd.log.Debugf("[%s] unable to find sound %s", nid, sound) |
| 115 |
} |
| 116 |
return absPath |
| 117 |
} |
| 118 |
|
| 119 |
// Removes all cruft from path, ensures it's a "forward" path. |
| 120 |
func (snd *sound) cleanPath(path string) (string, error) { |
| 121 |
cleaned := filepath.Clean(path) |
| 122 |
if strings.Contains(cleaned, "../") { |
| 123 |
return "", errors.New("Path escaping xdg attempt") |
| 124 |
} |
| 125 |
return cleaned, nil |
| 126 |
} |
| 127 |
|
| 128 |
func (snd *sound) findSoundFile(app *click.AppId, nid string, sound string) string { |
| 129 |
// XXX also support legacy appIds? |
| 130 |
// first, check package-specific |
| 131 |
sound, err := snd.cleanPath(sound) |
| 132 |
if err != nil { |
| 133 |
// bad boy |
| 134 |
return "" |
| 135 |
} |
| 136 |
absPath, err := snd.dataFind(filepath.Join(app.Package, sound)) |
| 137 |
if err == nil { |
| 138 |
// ffffound |
| 139 |
return absPath |
| 140 |
} |
| 141 |
|
| 142 |
// next, check the XDG data dirs |
| 143 |
// XXX should we only check in $XDG/sounds ? (that's for sound *themes*...) |
| 144 |
for _, dir := range snd.dataDirs() { |
| 145 |
absPath := filepath.Join(dir, sound) |
| 146 |
_, err := os.Stat(absPath) |
| 147 |
if err == nil { |
| 148 |
return absPath |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Use custom message sound from system settings, if its set |
| 153 |
if sound == snd.acc.MessageSoundFile() { |
| 154 |
return sound |
| 155 |
} |
| 156 |
|
| 157 |
return "" |
| 158 |
} |