Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Add ability to turn off notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Apr 30, 2020
1 parent 43b30f2 commit 93af1f4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 7 deletions.
9 changes: 9 additions & 0 deletions notifier/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions notifier/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"github.com/safing/portbase/database/record"

"github.com/safing/portbase/api/client"
"github.com/safing/portbase/log"

"github.com/tevino/abool"
)

var (
notificationsEnabled = abool.NewBool(true)
promptsEnabled = abool.NewBool(true)
)

func configClient() {
configOp := apiClient.Qsub("query config", handleConfigUpdate)
configOp.EnableResuscitation()
}

func handleConfigUpdate(m *client.Message) {
switch m.Type {
case client.MsgError:
log.Warningf("config: received error message: %s", string(m.RawValue))
case client.MsgDone:
case client.MsgSuccess:
case client.MsgOk, client.MsgUpdate, client.MsgNew:

// only process these keys
switch m.Key {
case "config:core/useSystemNotifications":
case "config:filter/askWithSystemNotifications":
default:
return
}

// parse record
if len(m.RawValue) < 2 {
log.Warningf("notify: failed to parse new config msg %s: too short", m.Key)
return
}
r, err := record.NewWrapper(m.Key, nil, m.RawValue[0], m.RawValue[1:])
if err != nil {
log.Warningf("notify: failed to parse new config msg %s: %s", m.Key, err)
return
}
acc := r.GetAccessor(r)

// get value
newValue, ok := acc.GetBool("Value")
if !ok {
newValue, ok = acc.GetBool("DefaultValue")
if !ok {
log.Warningf("config: could not get Value or DefaultValue from %s", m.Key)
}
}

// set value
switch m.Key {
case "config:core/useSystemNotifications":
notificationsEnabled.SetTo(newValue)
log.Infof("config: use system notifications set to: %v", newValue)
case "config:filter/askWithSystemNotifications":
promptsEnabled.SetTo(newValue)
log.Infof("config: ask with systemNotifications set to: %v", newValue)
}

case client.MsgDelete:
case client.MsgWarning:
case client.MsgOffline:
}
}
16 changes: 12 additions & 4 deletions notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func main() {
databaseDir = dataDir

// start log writer
log.Start()
err = log.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to start logging: %s\n", err)
os.Exit(1)
}

// connect to API
go apiClient.StayConnected()
Expand All @@ -89,10 +93,11 @@ func main() {
go tray()
go statusClient()
go notifClient()
go configClient()

// Shutdown
// catch interrupt for clean shutdown
signalCh := make(chan os.Signal)
signalCh := make(chan os.Signal, 1)
signal.Notify(
signalCh,
os.Interrupt,
Expand All @@ -113,16 +118,19 @@ func main() {

if printStackOnExit {
fmt.Println("=== PRINTING STACK ===")
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
fmt.Println("=== END STACK ===")
}
go func() {
time.Sleep(10 * time.Second)
fmt.Println("===== TAKING TOO LONG FOR SHUTDOWN - PRINTING STACK TRACES =====")
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
os.Exit(1)
}()

// clear all notifications
clearNotifications()

// shutdown
cancelMainCtx()
mainWg.Wait()
Expand Down
4 changes: 2 additions & 2 deletions notifier/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

// Notification types
const (
Info uint8 = 0
Warning uint8 = 1
Info uint8 = 0 //nolint:deadcode
Warning uint8 = 1 //nolint:deadcode
Prompt uint8 = 2
)

Expand Down
11 changes: 11 additions & 0 deletions notifier/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ func handleNotification(m *client.Message) {
existing.Unlock()
}

// save
notifications[n.ID] = n

// check if notifications are enabled
if !notificationsEnabled.IsSet() {
return
}
// check if prompts are enabled
if n.Type == Prompt && !promptsEnabled.IsSet() {
return
}

if n.Responded == 0 && n.SelectedActionID == "" {
n.Show()
}
Expand Down
5 changes: 4 additions & 1 deletion notifier/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func onReady() {
log.Warningf("failed to start app: %s", err)
return
}
cmd.Process.Release()
err = cmd.Process.Release()
if err != nil {
log.Warningf("failed to release app process: %s", err)
}
})
systray.AddSeparator()
}
Expand Down

0 comments on commit 93af1f4

Please sign in to comment.