From bab68cd21095985c5d867e44f0844ab4be7c2a95 Mon Sep 17 00:00:00 2001 From: Gabriel Handford Date: Wed, 11 May 2016 16:08:20 -0700 Subject: Adding actions --- notifier.go | 6 +++--- notifier/notifier.go | 27 +++++++++------------------ notifier_darwin.go | 16 ++++++++++++++-- notifier_darwin.m | 37 +++++++++++++++++-------------------- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/notifier.go b/notifier.go index cb342e5..35f3e0e 100644 --- a/notifier.go +++ b/notifier.go @@ -8,9 +8,9 @@ type Notification struct { Title string Message string ImagePath string - ImageURL string - BundleID string // For darwin - ToastPath string // For windows (Toaster) + BundleID string // For darwin + Actions []string // For darwin + ToastPath string // For windows (Toaster) } // Notifier knows how to deliver a notification diff --git a/notifier/notifier.go b/notifier/notifier.go index 6779892..bc8f517 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -13,19 +13,18 @@ import ( pkg "github.com/keybase/go-notifier" ) -var ( - title = kingpin.Flag("title", "Title").String() - message = kingpin.Flag("message", "Message").String() - imagePath = kingpin.Flag("image-path", "Image path").String() - bundleID = kingpin.Flag("bundle-id", "Bundle identifier (for OS X)").String() - toastPath = kingpin.Flag("toast-path", "Path to toast.exe (for Windows)").String() -) - func main() { - kingpin.Version("0.1.1") + notification := pkg.Notification{} + kingpin.Flag("title", "Title").StringVar(¬ification.Title) + kingpin.Flag("message", "Message").StringVar(¬ification.Message) + kingpin.Flag("image-path", "Image path").StringVar(¬ification.ImagePath) + kingpin.Flag("action", "Actions (for OS X)").StringsVar(¬ification.Actions) + kingpin.Flag("bundle-id", "Bundle identifier (for OS X)").StringVar(¬ification.BundleID) + kingpin.Flag("toast-path", "Path to toast.exe (for Windows)").StringVar(¬ification.ToastPath) + kingpin.Version("0.1.2") kingpin.Parse() - if runtime.GOOS == "windows" && *toastPath == "" { + if runtime.GOOS == "windows" && notification.ToastPath == "" { log.Fatal(fmt.Errorf("Need to specify --toast-path for Windows")) } @@ -34,14 +33,6 @@ func main() { log.Fatal(err) } - notification := pkg.Notification{ - Title: *title, - Message: *message, - ImagePath: *imagePath, - BundleID: *bundleID, - ToastPath: *toastPath, - } - if err := notifier.DeliverNotification(notification); err != nil { log.Fatal(err) } diff --git a/notifier_darwin.go b/notifier_darwin.go index b075f1c..283c329 100644 --- a/notifier_darwin.go +++ b/notifier_darwin.go @@ -7,7 +7,7 @@ package notifier #cgo CFLAGS: -x objective-c #cgo LDFLAGS: -framework Cocoa #import -extern CFStringRef deliverNotification(CFStringRef title, CFStringRef subtitle, CFStringRef message, CFStringRef appIconURLString, CFStringRef groupID, CFStringRef bundleID, CFStringRef actionButtonTitle, CFStringRef otherButtonTitle); +extern CFStringRef deliverNotification(CFStringRef title, CFStringRef subtitle, CFStringRef message, CFStringRef appIconURLString, CFArrayRef actions, CFStringRef groupID, CFStringRef bundleID); */ import "C" import "fmt" @@ -51,7 +51,19 @@ func (n darwinNotifier) DeliverNotification(notification Notification) error { defer Release(C.CFTypeRef(appIconURLStringRef)) } - C.deliverNotification(titleRef, nil, messageRef, appIconURLStringRef, bundleIDRef, bundleIDRef, nil, nil) + actions := []C.CFTypeRef{} + for _, action := range notification.Actions { + actionRef, err := StringToCFString(action) + if err != nil { + return err + } + defer Release(C.CFTypeRef(actionRef)) + actions = append(actions, C.CFTypeRef(actionRef)) + } + actionsRef := ArrayToCFArray(actions) + defer Release(C.CFTypeRef(actionsRef)) + + C.deliverNotification(titleRef, nil, messageRef, appIconURLStringRef, actionsRef, bundleIDRef, bundleIDRef) return nil } diff --git a/notifier_darwin.m b/notifier_darwin.m index 863f296..6e46b15 100644 --- a/notifier_darwin.m +++ b/notifier_darwin.m @@ -29,43 +29,40 @@ static BOOL installFakeBundleIdentifierHook() { @interface NotificationDelegate : NSObject @end -CFStringRef deliverNotification(CFStringRef title, CFStringRef subtitle, CFStringRef message, CFStringRef appIconURLString, - CFStringRef bundleID, CFStringRef groupID, - CFStringRef actionButtonTitle, CFStringRef otherButtonTitle) { +CFStringRef deliverNotification(CFStringRef titleRef, CFStringRef subtitleRef, CFStringRef messageRef, CFStringRef appIconURLStringRef, + CFArrayRef actionsRef, CFStringRef bundleIDRef, CFStringRef groupIDRef) { - if (bundleID) { - _fakeBundleIdentifier = (NSString *)bundleID; + if (bundleIDRef) { + _fakeBundleIdentifier = (NSString *)bundleIDRef; } installFakeBundleIdentifierHook(); - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - [defaults registerDefaults:@{@"sender": @"com.apple.Terminal"}]; - NSUserNotification *userNotification = [[NSUserNotification alloc] init]; - userNotification.title = (NSString *)title; - userNotification.subtitle = (NSString *)subtitle; - userNotification.informativeText = (NSString *)message; + userNotification.title = (NSString *)titleRef; + userNotification.subtitle = (NSString *)subtitleRef; + userNotification.informativeText = (NSString *)messageRef; NSMutableDictionary *options = [NSMutableDictionary dictionary]; - if (groupID) { - options[@"groupID"] = (NSString *)groupID; + if (groupIDRef) { + options[@"groupID"] = (NSString *)groupIDRef; } NSString *uuid = [[NSUUID UUID] UUIDString]; options[@"uuid"] = uuid; userNotification.userInfo = options; - if (appIconURLString) { - NSURL *appIconURL = [NSURL URLWithString:(NSString *)appIconURLString]; + if (appIconURLStringRef) { + NSURL *appIconURL = [NSURL URLWithString:(NSString *)appIconURLStringRef]; NSImage *image = [[NSImage alloc] initWithContentsOfURL:appIconURL]; if (image) { [userNotification setValue:image forKey:@"_identityImage"]; [userNotification setValue:@(false) forKey:@"_identityImageHasBorder"]; } } - - if (actionButtonTitle) { - userNotification.actionButtonTitle = (NSString *)actionButtonTitle; + NSArray *actions = (NSArray *)actionsRef; + if ([actions count] >= 1) { + userNotification.actionButtonTitle = [actions objectAtIndex:0]; + [userNotification setValue:@YES forKey:@"_showsButtons"]; } - if (otherButtonTitle) { - userNotification.otherButtonTitle = (NSString *)otherButtonTitle; + if ([actions count] >= 2) { + userNotification.otherButtonTitle = [actions objectAtIndex:1]; } NSUserNotificationCenter *userNotificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter]; -- cgit v1.2.3