Skip to content

Commit

Permalink
Added notification center support (dismiss on click somehow doesn't w…
Browse files Browse the repository at this point in the history
…ork. See issue #19
  • Loading branch information
Alexander Rust committed Aug 12, 2012
1 parent 8e66c83 commit e14737b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 85 deletions.
4 changes: 2 additions & 2 deletions Osprey/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
<string>NSApplication</string>
<key>SUFeedURL</key>
<string>http://ospreyapp.org/downloads/appcast.xml</string>
<key>SUShowReleaseNotes</key>
<false/>
<key>SUPublicDSAKeyFile</key>
<string>sparklePublicDSAKey.pem</string>
<key>SUShowReleaseNotes</key>
<false/>
</dict>
</plist>
11 changes: 6 additions & 5 deletions Osprey/OSPChatController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 4 additions & 14 deletions Osprey/OSPNotificationController.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
#import <Foundation/Foundation.h>
#import <Growl/Growl.h>

@interface OSPNotificationController : NSObject <GrowlApplicationBridgeDelegate>

+ (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 <NSApplicationDelegate, NSUserNotificationCenterDelegate>

+ (void)notificationForIncommingMessage:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user;
+ (void)notificationForIncommingAttentionRequest:(XMPPMessage*)message fromUser:(OSPUserStorageObject*)user;
- (void) removeAllNotifications;
@end
93 changes: 29 additions & 64 deletions Osprey/OSPNotificationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e14737b

Please sign in to comment.