IChatUtils.m
changeset 0 d84d25d6cdbb
child 4 64823cdde6a5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/IChatUtils.m	Sat Mar 08 21:04:41 2008 -0800
     1.3 @@ -0,0 +1,65 @@
     1.4 +//
     1.5 +//  IChatUtils.m
     1.6 +//  MYUtilities
     1.7 +//
     1.8 +//  Created by Jens Alfke on 3/3/08.
     1.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "IChatUtils.h"
    1.13 +#import "iChatBridge.h"
    1.14 +
    1.15 +@implementation IChatUtils
    1.16 +
    1.17 +
    1.18 +static iChatApplication *sIChatApp;
    1.19 +
    1.20 ++ (void) initialize
    1.21 +{
    1.22 +    if( ! sIChatApp ) {
    1.23 +        sIChatApp = [SBApplication applicationWithBundleIdentifier: @"com.apple.iChat"];
    1.24 +        sIChatApp.timeout = 5*60; // in ticks
    1.25 +    }
    1.26 +}
    1.27 +
    1.28 +
    1.29 ++ (SBApplication*) app  {return sIChatApp;}
    1.30 ++ (BOOL) isRunning      {return sIChatApp.isRunning;}
    1.31 ++ (void) activate       {[sIChatApp activate];}
    1.32 +
    1.33 +
    1.34 ++ (iChatTextChat*) activeChat
    1.35 +{
    1.36 +    if( ! [sIChatApp isRunning] )
    1.37 +        return nil;
    1.38 +    SBElementArray *chats = sIChatApp.textChats;
    1.39 +    if( chats.count==0 )
    1.40 +        return nil;
    1.41 +    iChatTextChat *chat = [chats objectAtIndex: 0];
    1.42 +    if( ! chat.active )
    1.43 +        return nil;
    1.44 +    return chat;
    1.45 +}    
    1.46 +
    1.47 ++ (NSString*) activeChatPartner
    1.48 +{
    1.49 +    iChatTextChat *chat = [self activeChat];
    1.50 +    if( ! chat )
    1.51 +        return nil;
    1.52 +    NSMutableArray *names = $marray();
    1.53 +    for( iChatBuddy *b in [chat participants] )
    1.54 +        [names addObject: (b.fullName ?: b.name)];
    1.55 +    return [names componentsJoinedByString: @", "];
    1.56 +}
    1.57 +
    1.58 ++ (BOOL) sendMessage: (NSString*)msg
    1.59 +{
    1.60 +    iChatTextChat *chat = [self activeChat];
    1.61 +    if( ! chat )
    1.62 +        return NO;
    1.63 +    [sIChatApp send: msg to: chat];
    1.64 +    return YES;
    1.65 +}
    1.66 +
    1.67 +
    1.68 +@end