From e14737b36e73c0cdf3518723b86ea0be79bbb2d7 Mon Sep 17 00:00:00 2001 From: Alexander Rust Date: Sun, 12 Aug 2012 20:27:04 +0200 Subject: [PATCH] Added notification center support (dismiss on click somehow doesn't work. See issue #19 --- Osprey/Info.plist | 4 +- Osprey/OSPChatController.m | 11 ++-- Osprey/OSPNotificationController.h | 18 ++---- Osprey/OSPNotificationController.m | 93 ++++++++++-------------------- 4 files changed, 41 insertions(+), 85 deletions(-) diff --git a/Osprey/Info.plist b/Osprey/Info.plist index 9fd1045..659594e 100644 --- a/Osprey/Info.plist +++ b/Osprey/Info.plist @@ -36,9 +36,9 @@ NSApplication SUFeedURL http://ospreyapp.org/downloads/appcast.xml - SUShowReleaseNotes - SUPublicDSAKeyFile sparklePublicDSAKey.pem + SUShowReleaseNotes + diff --git a/Osprey/OSPChatController.m b/Osprey/OSPChatController.m index 5c163ad..28d2791 100644 --- a/Osprey/OSPChatController.m +++ b/Osprey/OSPChatController.m @@ -307,12 +307,13 @@ - (void) handlePresence:(XMPPPresence*)presence { // Chat messages - (void) handleChatMessage:(XMPPMessage*)message { - // OSPUserStorageObject *user = [self contactWithJid:[XMPPJID jidWithString:[message attributeStringValueForName:@"from"]]]; - OSPChatViewController *cvc = [self chatViewControllerForJidStr:message.from.bare]; - [cvc displayChatMessage:message]; + OSPUserStorageObject *user = [[self xmppRosterStorage] userForJID:[message from] xmppStream:[self xmppStream] managedObjectContext:[self rosterManagedObjectContext]]; + + [self openChatWithUser:user andMakeActive:NO]; // lazyloads ChatViewController and + [[openChatViewControllers valueForKey:user.jidStr] displayChatMessage:message]; - // [self _incrementUnreadCounterForUserIfNeccessary:user]; - // [OSPNotificationController growlNotificationForIncommingMessage:message fromUser:user]; +//[self _incrementUnreadCounterForUserIfNeccessary:user]; + [OSPNotificationController notificationForIncommingMessage:message fromUser:user]; } // Attention messages diff --git a/Osprey/OSPNotificationController.h b/Osprey/OSPNotificationController.h index d76a0d4..0be0fd6 100644 --- a/Osprey/OSPNotificationController.h +++ b/Osprey/OSPNotificationController.h @@ -1,18 +1,8 @@ #import -#import - -@interface OSPNotificationController : NSObject - -+ (void)growlNotificationForIncommingMessage:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user; -+ (void)growlNotificationForIncommingAttentionRequest:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user; - -+ (void) genericGrowlNotification:(NSString *)title - description:(NSString *)description - notificationName:(NSString *)notifName - iconData:(NSData *)iconData - priority:(signed int)priority - isSticky:(BOOL)isSticky - clickContext:(id)clickContext; +@interface OSPNotificationController : NSObject ++ (void)notificationForIncommingMessage:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user; ++ (void)notificationForIncommingAttentionRequest:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user; +- (void) removeAllNotifications; @end diff --git a/Osprey/OSPNotificationController.m b/Osprey/OSPNotificationController.m index 21e66da..27f5ca1 100644 --- a/Osprey/OSPNotificationController.m +++ b/Osprey/OSPNotificationController.m @@ -14,82 +14,47 @@ @implementation OSPNotificationController - (id) init { self = [super init]; if (self) { - // Insert code here to initialize your application - - NSBundle *mainBundle = [NSBundle mainBundle]; - NSString *path = [[mainBundle privateFrameworksPath] stringByAppendingPathComponent:@"Growl"]; - if(NSAppKitVersionNumber >= 1038) - path = [path stringByAppendingPathComponent:@"1.3"]; - else - path = [path stringByAppendingPathComponent:@"1.2.3"]; - - path = [path stringByAppendingPathComponent:@"Growl.framework"]; - NSBundle *growlFramework = [NSBundle bundleWithPath:path]; - if([growlFramework load]) - { - //NSDictionary *infoDictionary = [growlFramework infoDictionary]; - - Class GAB = NSClassFromString(@"GrowlApplicationBridge"); - if([GAB respondsToSelector:@selector(setGrowlDelegate:)]) - [GAB performSelector:@selector(setGrowlDelegate:) withObject:self]; - } + [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:(id)self]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllNotifications) name:NSApplicationDidBecomeActiveNotification object:nil]; + } return self; } -#pragma mark - Growl registration -- (NSDictionary *) registrationDictionaryForGrowl { - LOGFUNCTIONCALL - NSDictionary *notificationsWithDescriptions = [NSDictionary dictionaryWithObjectsAndKeys: - RECEIVED_CHAT_MESSAGE_HUMAN_READABLE, RECEIVED_CHAT_MESSAGE_NOTIFICATION_NAME, - RECEIVED_ATTENTION_REQUEST_HUMAN_READABLE, RECEIVED_ATTENTION_REQUEST_NOTIFICATION_NAME, - nil]; - - NSArray *allNotifications = [notificationsWithDescriptions allKeys]; - - NSDictionary *regDict = [NSDictionary dictionaryWithObjectsAndKeys: - APP_NAME, GROWL_APP_NAME, - allNotifications, GROWL_NOTIFICATIONS_ALL, - allNotifications, GROWL_NOTIFICATIONS_DEFAULT, - notificationsWithDescriptions, GROWL_NOTIFICATIONS_HUMAN_READABLE_NAMES, - nil]; - return regDict; ++ (void)notificationForIncommingMessage:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user { + NSUserNotification *userNotification = [[NSUserNotification alloc] init]; + userNotification.title = user.displayName; + userNotification.informativeText = [[message elementForName:@"body"] stringValue]; + userNotification.userInfo = @{ @"jidStr": user.jidStr, @"type" : @"incommingSingleChat"}; + userNotification.soundName = NSUserNotificationDefaultSoundName; + userNotification.hasActionButton = YES; + userNotification.actionButtonTitle = @"Reply"; + + //Scheldule our NSUserNotification + [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification]; + } ++ (void)notificationForIncommingAttentionRequest:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user { + NSUserNotification *userNotification = [[NSUserNotification alloc] init]; + + userNotification.title = user.displayName; + userNotification.informativeText = @"wants your attention!"; + [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification]; - - -+ (void)growlNotificationForIncommingMessage:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user { - [OSPNotificationController genericGrowlNotification:[user displayName] - description:[[message elementForName:@"body"] stringValue] - notificationName:RECEIVED_CHAT_MESSAGE_NOTIFICATION_NAME - iconData:nil - priority:0 - isSticky:NO - clickContext:user.jid.bare]; } -+ (void)growlNotificationForIncommingAttentionRequest:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user { - [OSPNotificationController genericGrowlNotification:[user displayName] - description:@"wants wants your attention!" - notificationName:RECEIVED_ATTENTION_REQUEST_NOTIFICATION_NAME - iconData:nil - priority:0 - isSticky:NO - clickContext:user.jid.bare]; +- (void) removeAllNotifications { + DDLogVerbose(@"Removing all notifications"); + [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications]; } - -// Provides a generic interface for sending growl messages, but checks if notifications are enabled in the preferences -+ (void) genericGrowlNotification:(NSString *)title - description:(NSString *)description - notificationName:(NSString *)notifName - iconData:(NSData *)iconData - priority:(signed int)priority - isSticky:(BOOL)isSticky - clickContext:(id)clickContext { +- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification { + DDLogVerbose(@"User clicked on notification: %@", [notification userInfo]); - DDLogError(@"GROWL SUPPORT IS DEPRECATED AND WILL BE REPLACED BY NOTIFICATION CENTER SUPPORT SOON. UNTIL THEN, THIS METHOD DOES NOTHING"); + if ([[notification.userInfo valueForKey:@"type"] isEqualToString:@"incommingSingleChat"]) { + [[[NSApp delegate] chatController] openChatWithJidStr:[notification.userInfo valueForKey:@"jidStr"] andMakeActive:YES]; + } } @end