#import "DockIconPlugIn.h" #import @class JVMutableChatMessage; @implementation DockIconPlugIn - (id)mailImageNamed:(NSString *)name { return [[NSImage alloc] initWithContentsOfFile: [NSString stringWithFormat: @"/Applications/Mail.app/Contents/Resources/%@.tiff", name]]; } - (void)initImages { badge1and2 = [self mailImageNamed:@"newMailBadge1&2"]; badge3 = [self mailImageNamed:@"newMailBadge3"]; badge4 = [self mailImageNamed:@"newMailBadge4"]; badge5 = [self mailImageNamed:@"newMailBadge5"]; } - (NSImage *)chooseBadge:(int)charCount { if (charCount <= 2) { return badge1and2; } else if (charCount == 3) { return badge3; } else if (charCount == 4) { return badge4; } else { return badge5; } } - (void)updateIcon:(int)unread { NSImage *icon = [NSImage imageNamed:@"NSApplicationIcon"]; NSImage *newIcon = nil; if (unread > 0) { newIcon = [icon copy]; NSString *text = [NSString stringWithFormat:@"%d", unread]; // NSLog(@"Rendering icon for text: %@", text); int charCount = [text length]; NSImage *badge = [self chooseBadge:charCount]; NSSize badgeSize = [badge size]; NSSize iconSize = [icon size]; float w = badgeSize.width; float h = badgeSize.height; float x = iconSize.width - w; float y = iconSize.height - h; [newIcon lockFocus]; { [badge compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver]; [text drawInRect:NSMakeRect(x, y + 18.0f, w, 24.0f) withAttributes:attributes]; } [newIcon unlockFocus]; [newIcon autorelease]; } else { newIcon = icon; } // NSLog(@"setting application icon..."); [NSApp setApplicationIconImage:newIcon]; } - (void)applicationWillTerminate:(NSNotification *)note { [self updateIcon:0]; } - (void)checkUpdate { SEL selector = @selector(newMessagesWaiting); JVChatController *chatController = [JVChatController defaultController]; NSSet *chatViewControllers = [chatController allChatViewControllers]; int total = 0; NSEnumerator *enumerator = [chatViewControllers objectEnumerator]; id controller; while ((controller = [enumerator nextObject]) != nil) { if ([controller respondsToSelector:selector]) { int (* method)(id, SEL) = (void *)[controller methodForSelector:selector]; total += method(controller, selector); } } if (total != unreadMessageCount) { [self updateIcon:total]; unreadMessageCount = total; } } - (void)createTimer { [timer invalidate]; [timer release]; timer = [[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(checkUpdate) userInfo:nil repeats:YES] retain]; } - (void)processIncomingMessage:(JVMutableChatMessage *)message inView:(id ) view { [self checkUpdate]; [self createTimer]; } - (id) initWithManager:(MVChatPluginManager *) manager { self = [super init]; if (self == nil) { return nil; } [self initImages]; NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; [paragraphStyle setAlignment:NSCenterTextAlignment]; [paragraphStyle autorelease]; attributes = [[NSDictionary dictionaryWithObjectsAndKeys: [NSFont fontWithName:@"Helvetica Bold" size:24], NSFontAttributeName, [NSColor whiteColor], NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil] retain]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; [self createTimer]; return self; } @end