-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
171 lines (138 loc) · 3.71 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright © 2016 Zlatko Čalušić
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
// Update notifier displays notification and an icon in the panel tray area when Debian package updates are
// available. You can hover the mouse over the icon to check how many updates are available. It's especially suitable
// for Debian testing/unstable users, because it checks for updates very often. Developed and tested on Xfce desktop
// environment.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os/exec"
"path"
"regexp"
"strconv"
"time"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
notify "github.com/mqu/go-notify"
)
var (
aptRunning bool
aptLastCheck time.Time
updLastCheck time.Time
updAvailable int
updTooltip int
updNotified int
)
func showIcon(icon *gtk.StatusIcon) {
if !icon.GetVisible() {
icon.SetVisible(true)
}
}
func hideIcon(icon *gtk.StatusIcon) {
if icon.GetVisible() {
icon.SetVisible(false)
}
}
func showNotification(text string) {
notify.Init("update-notifier")
defer notify.UnInit()
if notification := notify.NotificationNew("Software updates available", text,
"/usr/share/icons/gnome/48x48/status/software-update-available.png"); notification != nil {
notify.NotificationShow(notification) // ignore errors
}
}
func userNotify(icon *gtk.StatusIcon) {
var word1, word2 string
if updAvailable == 1 {
word1 = "is"
word2 = "update"
} else {
word1 = "are"
word2 = "updates"
}
if updAvailable != updTooltip {
icon.SetTooltipMarkup(fmt.Sprintf("%d %s available", updAvailable, word2))
updTooltip = updAvailable
}
showIcon(icon)
if updAvailable != updNotified {
showNotification(fmt.Sprintf("There %s %d %s ready to install.", word1, updAvailable, word2))
updNotified = updAvailable
}
}
// Check if package manager is running (every 5 seconds).
func isAptRunning() bool {
if time.Since(aptLastCheck) >= 5*time.Second {
aptRunning = false
dir, err := ioutil.ReadDir("/proc")
if err != nil {
log.Fatal(err)
}
reApt := regexp.MustCompile(`(?m:^(apt-get|dselect|aptitude)$)`)
reProcess := regexp.MustCompile(`^\d+$`)
for _, v := range dir {
if !v.IsDir() {
continue
}
if !reProcess.MatchString(v.Name()) {
continue
}
comm, err := ioutil.ReadFile(path.Join("/proc", v.Name(), "comm"))
if err != nil {
continue
}
if reApt.Match(comm) {
aptRunning = true
updLastCheck = time.Time{}
break
}
}
aptLastCheck = time.Now()
}
return aptRunning
}
// Check if updates are available (every 5 minutes).
func updatesAvailable() int {
if time.Since(updLastCheck) >= 5*time.Minute {
var out bytes.Buffer
cmd := exec.Command("/usr/bin/apt-get", "-s", "dist-upgrade")
cmd.Stdout = &out
if err := cmd.Run(); err == nil {
if sl := regexp.MustCompile(`(?m:^(\d+) upgraded, (\d+) newly installed)`).FindStringSubmatch(out.String()); len(sl) == 3 {
if upgraded, err := strconv.Atoi(sl[1]); err == nil {
if newInstall, err := strconv.Atoi(sl[2]); err == nil {
updAvailable = upgraded + newInstall
}
}
}
}
updLastCheck = time.Now()
}
return updAvailable
}
func main() {
// https://github.com/mattn/go-gtk/issues/251
gtk.Init(nil)
glib.SetApplicationName("update-notifier")
icon := gtk.NewStatusIconFromFile("/usr/share/icons/gnome/24x24/status/software-update-available.png")
icon.SetTitle("update-notifier")
icon.SetVisible(false)
// Don't fight with system notifications during login.
time.Sleep(15 * time.Second)
for {
if !isAptRunning() && updatesAvailable() > 0 {
userNotify(icon)
} else {
hideIcon(icon)
}
for gtk.EventsPending() {
gtk.MainIteration()
}
time.Sleep(time.Second / 10)
}
}