MoodBrowser.m 2.14 KB
Newer Older
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#import "MoodBrowser.h"

@implementation MoodBrowser


- (void) setUpServiceBrowser
{
	serviceBrowser = [[NSNetServiceBrowser alloc] init];
	[serviceBrowser setDelegate:self];
	[serviceBrowser searchForServicesOfType:@"_moodring._tcp." inDomain:@""];
}

- (id) init
{
	self = [super init];
	friendsList = [[NSMutableArray alloc] init];
	moodsList = [[NSMutableArray alloc] init];
	[self setUpServiceBrowser];
	return self;
}

- (void) dealloc
{
	[friendsList release];
	[super dealloc];
}

- (int) numberOfRowsInTableView:(NSTableView *)tableView
{
	return [friendsList count];
}

- (id)tableView:(NSTableView *)aTableView
	    objectValueForTableColumn:(NSTableColumn *)aTableColumn
			row:(int)rowIndex
{
	return [[friendsList objectAtIndex:rowIndex] name];
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
    [friendsList addObject:aNetService];
	[moodsList addObject:[NSColor blackColor]];
	[aNetService setDelegate:self];
	[aNetService startMonitoring];
    if(!moreComing)
        [friendView reloadData];
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
	[moodsList removeObjectAtIndex:[friendsList indexOfObject:aNetService]];
	[friendsList removeObject:aNetService];
    [aNetService stopMonitoring];
    if(!moreComing)
        [friendView reloadData];
}

- (void)tableView:(NSTableView *)inTableView
	 willDisplayCell:(id)inCell
	  forTableColumn:(NSTableColumn *)inTableColumn
			  row:(int)inRow
{
	[inCell setTextColor:[moodsList objectAtIndex:inRow]];
	}

- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
{
  NSString * temp = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	float currentValue = [[temp substringFromIndex:[temp length]-1] floatValue]/4;
	[moodsList replaceObjectAtIndex:[friendsList indexOfObject:sender] withObject:[NSColor colorWithCalibratedRed:3.3 *(1- currentValue)
																											green:2.0 * (currentValue)
																											 blue:0.0 alpha:1.0]];

	[friendView reloadData];
	//}
}


@end