fix linux and remove dup code

This commit is contained in:
popaprozac 2025-03-20 16:21:37 -07:00
commit 9adeef2bdf
3 changed files with 32 additions and 47 deletions

View file

@ -107,10 +107,6 @@ func (dn *darwinNotifier) CheckNotificationAuthorization() (bool, error) {
// SendNotification sends a basic notification with a unique identifier, title, subtitle, and body.
func (dn *darwinNotifier) SendNotification(options NotificationOptions) error {
if err := validateNotificationOptions(options); err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
@ -156,10 +152,6 @@ func (dn *darwinNotifier) SendNotification(options NotificationOptions) error {
// A NotificationCategory must be registered with RegisterNotificationCategory first. The `CategoryID` must match the registered category.
// If a NotificationCategory is not registered a basic notification will be sent.
func (dn *darwinNotifier) SendNotificationWithActions(options NotificationOptions) error {
if err := validateNotificationOptions(options); err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

View file

@ -84,6 +84,7 @@ func (ls *linuxNotifier) Startup(ctx context.Context) error {
fmt.Printf("Failed to load notification categories: %v\n", err)
}
// Initialize the internal notifier
ls.internal = &internalNotifier{
activeNotifs: make(map[string]uint32),
contexts: make(map[string]*notificationContext),
@ -91,45 +92,43 @@ func (ls *linuxNotifier) Startup(ctx context.Context) error {
var err error
ls.initOnce.Do(func() {
err = ls.init()
// Initialize notification system
err = ls.initNotificationSystem()
})
return err
}
func (ls *linuxNotifier) shutdown() {
ls.internal.Lock()
defer ls.internal.Unlock()
// Cancel the listener context if it's running
if ls.internal.listenerCancel != nil {
ls.internal.listenerCancel()
ls.internal.listenerCancel = nil
}
// Close the connection
if ls.internal.dbusConn != nil {
ls.internal.dbusConn.Close()
ls.internal.dbusConn = nil
}
// Clear state
ls.internal.activeNotifs = make(map[string]uint32)
ls.internal.contexts = make(map[string]*notificationContext)
ls.internal.method = "none"
ls.internal.sendPath = ""
}
// Shutdown is called when the service is unloaded
func (ls *linuxNotifier) Shutdown() error {
if ls.internal != nil {
ls.shutdown()
ls.internal.Lock()
defer ls.internal.Unlock()
// Cancel the listener context if it's running
if ls.internal.listenerCancel != nil {
ls.internal.listenerCancel()
ls.internal.listenerCancel = nil
}
// Close the connection
if ls.internal.dbusConn != nil {
ls.internal.dbusConn.Close()
ls.internal.dbusConn = nil
}
// Clear state
ls.internal.activeNotifs = make(map[string]uint32)
ls.internal.contexts = make(map[string]*notificationContext)
ls.internal.method = "none"
ls.internal.sendPath = ""
}
return ls.saveCategories()
}
// Initialize the notifier and choose the best available notification method
func (ls *linuxNotifier) init() error {
// initNotificationSystem initializes the notification system, choosing the best available method
func (ls *linuxNotifier) initNotificationSystem() error {
var err error
// Cancel any existing listener before starting a new one
@ -404,8 +403,9 @@ func (ls *linuxNotifier) SendNotification(options NotificationOptions) error {
return errors.New("notification service not initialized")
}
if err := validateNotificationOptions(options); err != nil {
return err
if ls.internal.method == "" || (ls.internal.method == MethodDbus && ls.internal.dbusConn == nil) ||
(ls.internal.method == MethodNotifySend && ls.internal.sendPath == "") {
return errors.New("notification system not properly initialized")
}
ls.internal.Lock()
@ -447,8 +447,9 @@ func (ls *linuxNotifier) SendNotificationWithActions(options NotificationOptions
return errors.New("notification service not initialized")
}
if err := validateNotificationOptions(options); err != nil {
return err
if ls.internal.method == "" || (ls.internal.method == MethodDbus && ls.internal.dbusConn == nil) ||
(ls.internal.method == MethodNotifySend && ls.internal.sendPath == "") {
return errors.New("notification system not properly initialized")
}
ls.categoriesLock.RLock()

View file

@ -126,10 +126,6 @@ func (wn *windowsNotifier) CheckNotificationAuthorization() (bool, error) {
// SendNotification sends a basic notification with a name, title, and body. All other options are ignored on Windows.
// (subtitle is only available on macOS)
func (wn *windowsNotifier) SendNotification(options NotificationOptions) error {
if err := validateNotificationOptions(options); err != nil {
return err
}
if err := wn.saveIconToDir(); err != nil {
fmt.Printf("Error saving icon: %v\n", err)
}
@ -156,10 +152,6 @@ func (wn *windowsNotifier) SendNotification(options NotificationOptions) error {
// If a NotificationCategory is not registered a basic notification will be sent.
// (subtitle is only available on macOS)
func (wn *windowsNotifier) SendNotificationWithActions(options NotificationOptions) error {
if err := validateNotificationOptions(options); err != nil {
return err
}
if err := wn.saveIconToDir(); err != nil {
fmt.Printf("Error saving icon: %v\n", err)
}