-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifier.go
142 lines (122 loc) · 4.15 KB
/
notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package utils
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"runtime"
"strings"
"github.com/go-musicfox/spotifox/internal/configs"
"github.com/go-musicfox/spotifox/internal/types"
"github.com/go-musicfox/notificator"
"github.com/pkg/errors"
)
type osxNotificator struct {
appName string
defaultIcon string
}
func (o osxNotificator) getNotifierCmd() string {
localDir := GetLocalDataDir()
notifierPath := path.Join(localDir, "musicfox-notifier.app")
if _, err := os.Stat(notifierPath); os.IsNotExist(err) {
err = CopyDirFromEmbed("embed/musicfox-notifier.app", notifierPath)
if err != nil {
log.Printf("copy musicfox-notifier.app failed, err: %+v", errors.WithStack(err))
}
} else if err != nil {
log.Printf("musicfox-notifier.app status err: %+v", errors.WithStack(err))
}
return notifierPath + "/Contents/MacOS/musicfox-notifier"
}
func (o osxNotificator) push(title, text, iconPath, redirectUrl, groupId string) *exec.Cmd {
cmdPath := o.getNotifierCmd()
if _, err := os.Stat(cmdPath); err == nil {
var args = []string{"-title", o.appName, "-message", text, "-subtitle", title, "-contentImage", iconPath}
if redirectUrl != "" {
args = append(args, "-open", redirectUrl)
}
if groupId != "" {
args = append(args, "-group", groupId)
}
return exec.Command(cmdPath, args...)
} else if notificator.CheckMacOSVersion() {
title = strings.Replace(title, `"`, `\"`, -1)
text = strings.Replace(text, `"`, `\"`, -1)
notification := fmt.Sprintf("display notification \"%s\" with title \"%s\" subtitle \"%s\"", text, o.appName, title)
return exec.Command("osascript", "-e", notification)
}
return exec.Command("growlnotify", "-n", o.appName, "--image", iconPath, "-m", title, "--url", redirectUrl)
}
func (o osxNotificator) pushCritical(title, text, iconPath, redirectUrl, groupId string) *exec.Cmd {
cmdPath := o.getNotifierCmd()
if _, err := os.Stat(cmdPath); err == nil {
var args = []string{"-title", o.appName, "-message", text, "-subtitle", title, "-contentImage", iconPath}
if redirectUrl != "" {
args = append(args, "-open", redirectUrl)
}
if groupId != "" {
args = append(args, "-group", groupId)
}
return exec.Command(cmdPath, args...)
} else if notificator.CheckMacOSVersion() {
notification := fmt.Sprintf("display notification \"%s\" with title \"%s\" subtitle \"%s\"", text, o.appName, title)
return exec.Command("osascript", "-e", notification)
}
return exec.Command("growlnotify", "-n", o.appName, "--image", iconPath, "-m", title, "--url", redirectUrl)
}
type Notificator struct {
osx *osxNotificator
*notificator.Notificator
}
func NewNotificator(o notificator.Options) *Notificator {
n := &Notificator{
Notificator: notificator.New(o),
}
if runtime.GOOS == "darwin" {
n.osx = &osxNotificator{appName: o.AppName, defaultIcon: o.DefaultIcon}
}
return n
}
func (n Notificator) Push(urgency, title, text, iconPath, redirectUrl, groupId string) error {
if runtime.GOOS == "darwin" {
icon := n.osx.defaultIcon
if iconPath != "" {
icon = iconPath
}
if urgency == notificator.UrCritical {
return n.osx.pushCritical(title, text, icon, redirectUrl, groupId).Run()
}
return n.osx.push(title, text, icon, redirectUrl, groupId).Run()
}
return n.Notificator.Push(urgency, title, text, iconPath, redirectUrl)
}
type NotifyContent struct {
Title string
Text string
Url string
Icon string
GroupId string
}
func Notify(content NotifyContent) {
if !configs.ConfigRegistry.Main.ShowNotify {
return
}
notify := NewNotificator(notificator.Options{
AppName: types.AppName,
})
if runtime.GOOS != "darwin" {
localDir := GetLocalDataDir()
content.Icon = path.Join(localDir, configs.ConfigRegistry.Main.NotifyIcon)
if _, err := os.Stat(content.Icon); os.IsNotExist(err) {
content.Icon = path.Join(localDir, types.DefaultNotifyIcon)
err = CopyFileFromEmbed("embed/logo.png", content.Icon)
if err != nil {
log.Printf("copy logo.png failed, err: %+v", errors.WithStack(err))
}
} else if err != nil {
log.Printf("logo.png status err: %+v", errors.WithStack(err))
}
}
_ = notify.Push(notificator.UrNormal, content.Title, content.Text, content.Icon, content.Url, content.GroupId)
}