// // BuddyController.m // Proteus // // Created by Julian Cain on 6/12/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "BuddyController.h" #import "ChatWindowController.h" #import "ConversationController.h" #import @implementation BuddyController @synthesize buddiesTableView = _buddiesTableView; @synthesize chatWindowController = _chatWindowController; - (void)awakeFromNib { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buddyDidUpdateNotification:) name:kPurpBuddyDidUpdateNotification object:nil ]; [self.buddiesTableView setTarget:self]; [self.buddiesTableView setDoubleAction:@selector(doubleAction:)]; self.chatWindowController = [[[ChatWindowController alloc] init] autorelease ]; } - (int)numberOfRowsInTableView:(NSTableView *)aTableView { Proteus_AppDelegate * appDelegate = [NSApp delegate]; return appDelegate.purple.buddies.count; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { Proteus_AppDelegate * appDelegate = [NSApp delegate]; switch ([aTableColumn.identifier intValue]) { case 0: { /** * Purple status types. * @note These should used via framework header. */ enum statusTypes { PURPLE_STATUS_UNSET = 0, PURPLE_STATUS_OFFLINE, PURPLE_STATUS_AVAILABLE, PURPLE_STATUS_UNAVAILABLE, PURPLE_STATUS_INVISIBLE, PURPLE_STATUS_AWAY, PURPLE_STATUS_EXTENDED_AWAY, PURPLE_STATUS_MOBILE, PURPLE_STATUS_NUM_PRIMITIVES }; NSUInteger statusType = [[[appDelegate.purple.buddies objectAtIndex:rowIndex] objectForKey:@"statusType"] unsignedIntValue ]; switch (statusType) { case PURPLE_STATUS_UNSET: { return [NSImage imageNamed:@""]; } break; case PURPLE_STATUS_OFFLINE: { return [NSImage imageNamed:@""]; } break; case PURPLE_STATUS_AVAILABLE: { return [NSImage imageNamed:@"PresenceAvailable"]; } break; case PURPLE_STATUS_INVISIBLE: { return [NSImage imageNamed:@"PresenceInvisible"]; } break; case PURPLE_STATUS_AWAY: { return [NSImage imageNamed:@"PresenceAway"]; } break; case PURPLE_STATUS_UNAVAILABLE: { return [NSImage imageNamed:@"PresenceAway"]; } break; default: break; } } break; case 1: { return [[appDelegate.purple.buddies objectAtIndex:rowIndex] objectForKey:@"name" ]; } break; default: break; } return nil; } - (void)doubleAction:(id)sender { Proteus_AppDelegate * delegate = (Proteus_AppDelegate *)[NSApp delegate] ; [self.chatWindowController.window center]; [self.chatWindowController showWindow:nil]; [self.chatWindowController.window makeKeyAndOrderFront:nil]; NSIndexSet * selectedIndexes = [self.buddiesTableView selectedRowIndexes]; NSUInteger rowIndex = [selectedIndexes firstIndex]; if (rowIndex != -1) { NSString * name = [[delegate.purple.buddies objectAtIndex:rowIndex] objectForKey:@"name" ]; Conversation * conversation = [ self.chatWindowController.conversationController converseWith:name ]; [self.chatWindowController addConversation:conversation]; } else { NSParameterAssert(rowIndex >= 0); } } - (void)buddyDidUpdateNotification:(NSNotification *)aNotification { NSLog( @"buddyDidUpdateNotification: %@", [[aNotification object] objectForKey:@"name"] ); [self.buddiesTableView reloadData]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [_buddiesTableView release]; [_chatWindowController release], _chatWindowController = nil; [super dealloc]; } @end