All pastes #497551 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-19 22:43 UTC
rendered paste body
//
//  ALBuddyStatus.m
//  ABBuddyStatus hax
//
//  Created by Joe on 2007-05-18.
//  Copyright 2007 Alacatia Labs. All rights reserved.
//

#import "ALBuddyStatus.h"

static NSDistantObject<AIAdium> * adium = nil;

@implementation ALBuddyStatus
- (id)init {
	if(self = [super init]) {

		[[NSDistributedNotificationCenter defaultCenter] addObserver:self
															selector:@selector(adiumLaunched:)
																name:AIApplicationDidFinishLoadingNotification
															  object:nil];
		
		[[NSNotificationCenter defaultCenter] addObserver:self
												 selector:@selector(disconnectedFromAdium:)
													 name:NSConnectionDidDieNotification
												   object:nil];
		
		 
		[self connectToAdium];
	}
	
	return self;
}

- (void)dealloc {
	// known issue: if we crash, dealloc isn't called, and our remote proxy object isn't removed from adium's notification center...
	[[adium notificationCenter] removeObserver:self];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
	
	[adium release];
	[super dealloc];
}

- (void)connectToAdium {
	if(!adium) {
		adium = [[NSConnection rootProxyForConnectionWithRegisteredName:@"com.adiumX.adiumX"
																   host:nil] retain];
		
		[adium setProtocolForProxy:@protocol(AIAdium)];
				
		[[adium notificationCenter] addObserver:self
									   selector:@selector(contactStatusChanged:)
										   name:ListObject_StatusChanged
										 object:nil];
	}
}

- (void)adiumLaunched:(NSNotification *)notification {
	[self connectToAdium]; 
}

- (void)disconnectedFromAdium:(NSNotification *)notification {
	adium = nil;
}

- (void)contactStatusChanged:(NSNotification *)notification {
	NSLog(@"%@", notification);
	[[NSNotificationCenter defaultCenter] postNotificationName:@"ABBuddyStatusChangedNotification"
														object:self];
}

- (BOOL)isAvaliable:(ABPerson *)person {
	if([super isAvaliable:person]) {
		return YES;
	}
	
	ABMultiValue * AIMScreennamesMultiValue = [person valueForProperty:kABAIMInstantProperty];
	ABMultiValue * jabberScreennamesMultiValue = [person valueForProperty:kABJabberInstantProperty];
	int i = 0;
	
	for (i = 0; i < [AIMScreennamesMultiValue count]; i++) {
		if([self isHandleAvailable:[AIMScreennamesMultiValue valueAtIndex:i] onService:@"AIM"]) {
			return YES;
		}
	}
	
	for (i = 0; i < [jabberScreennamesMultiValue count]; i++) {
		if([self isHandleAvailable:[jabberScreennamesMultiValue valueAtIndex:i] onService:@"Jabber"]) {
			return YES;
		}
	}
	
	return NO;
}

- (BOOL)isHandleAvailable:(NSString *)handle onService:(NSString *)serviceID {
	AIService * service = [[adium accountController] firstServiceWithServiceID:serviceID];
	NSSet * contacts = [[adium contactController] allContactsWithService:service
																	 UID:handle
															existingOnly:YES];
	
	NSEnumerator * enumerator = [contacts objectEnumerator];
	AIListContact * contact = nil;
	
	while(contact = [enumerator nextObject]) {
		if([contact statusType] == AIAvailableStatusType) {
			return YES;
		}
	}
	
	return NO;
}

- (BOOL)isHandleAvailable:(NSString *)handle {
	[self connectToAdium];
	return ( [super isHandleAvailable:handle] || [self isHandleAvailable:handle onService:@"AIM"] || [self isHandleAvailable:handle onService:@"Jabber"] );
}
@end