diff --git a/notifications/config.go b/notifications/config.go new file mode 100644 index 0000000..5768dc8 --- /dev/null +++ b/notifications/config.go @@ -0,0 +1,32 @@ +package notifications + +import ( + "github.com/safing/portbase/config" +) + +// Configuration Keys. +var ( + CfgUseSystemNotificationsKey = "core/useSystemNotifications" + useSystemNotifications config.BoolOption +) + +func registerConfig() error { + if err := config.Register(&config.Option{ + Name: "Desktop Notifications", + Key: CfgUseSystemNotificationsKey, + Description: "In addition to showing notifications in the Portmaster App, also send them to the Desktop. This requires the Portmaster Notifier to be running.", + OptType: config.OptTypeBool, + ExpertiseLevel: config.ExpertiseLevelUser, + ReleaseLevel: config.ReleaseLevelStable, + DefaultValue: true, // TODO: turn off by default on unsupported systems + Annotations: config.Annotations{ + config.DisplayOrderAnnotation: -15, + config.CategoryAnnotation: "User Interface", + }, + }); err != nil { + return err + } + useSystemNotifications = config.Concurrent.GetAsBool(CfgUseSystemNotificationsKey, true) + + return nil +} diff --git a/notifications/module.go b/notifications/module.go index b523a99..1000e94 100644 --- a/notifications/module.go +++ b/notifications/module.go @@ -11,7 +11,11 @@ var ( ) func init() { - module = modules.Register("notifications", nil, start, nil, "database", "base") + module = modules.Register("notifications", prep, start, nil, "database", "base") +} + +func prep() error { + return registerConfig() } func start() error { diff --git a/notifications/notification.go b/notifications/notification.go index 0deef9d..1b283d0 100644 --- a/notifications/notification.go +++ b/notifications/notification.go @@ -75,6 +75,8 @@ type Notification struct { // ShowOnSystem specifies if the notification should be also shown on the // operating system. Notifications shown on the operating system level are // more focus-intrusive and should only be used for important notifications. + // If the configuration option "Desktop Notifications" is switched off, this + // will be forced to false on the first save. ShowOnSystem bool // EventData contains an additional payload for the notification. This payload // may contain contextual data and may be used by a localization framework @@ -319,9 +321,15 @@ func (n *Notification) save(pushUpdate bool) { n.State = Active } - // check key + // Initialize on first save. if !n.KeyIsSet() { + // Set database key. n.SetKey(fmt.Sprintf("notifications:all/%s", n.EventID)) + + // Check if notifications should be shown on the system at all. + if !useSystemNotifications() { + n.ShowOnSystem = false + } } // Update meta data.