IChatUtils.m
author Jens Alfke <jens@mooseyard.com>
Sat Mar 08 21:04:41 2008 -0800 (2008-03-08)
changeset 0 d84d25d6cdbb
child 4 64823cdde6a5
permissions -rw-r--r--
Initial checkin.
     1 //
     2 //  IChatUtils.m
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 3/3/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "IChatUtils.h"
    10 #import "iChatBridge.h"
    11 
    12 @implementation IChatUtils
    13 
    14 
    15 static iChatApplication *sIChatApp;
    16 
    17 + (void) initialize
    18 {
    19     if( ! sIChatApp ) {
    20         sIChatApp = [SBApplication applicationWithBundleIdentifier: @"com.apple.iChat"];
    21         sIChatApp.timeout = 5*60; // in ticks
    22     }
    23 }
    24 
    25 
    26 + (SBApplication*) app  {return sIChatApp;}
    27 + (BOOL) isRunning      {return sIChatApp.isRunning;}
    28 + (void) activate       {[sIChatApp activate];}
    29 
    30 
    31 + (iChatTextChat*) activeChat
    32 {
    33     if( ! [sIChatApp isRunning] )
    34         return nil;
    35     SBElementArray *chats = sIChatApp.textChats;
    36     if( chats.count==0 )
    37         return nil;
    38     iChatTextChat *chat = [chats objectAtIndex: 0];
    39     if( ! chat.active )
    40         return nil;
    41     return chat;
    42 }    
    43 
    44 + (NSString*) activeChatPartner
    45 {
    46     iChatTextChat *chat = [self activeChat];
    47     if( ! chat )
    48         return nil;
    49     NSMutableArray *names = $marray();
    50     for( iChatBuddy *b in [chat participants] )
    51         [names addObject: (b.fullName ?: b.name)];
    52     return [names componentsJoinedByString: @", "];
    53 }
    54 
    55 + (BOOL) sendMessage: (NSString*)msg
    56 {
    57     iChatTextChat *chat = [self activeChat];
    58     if( ! chat )
    59         return NO;
    60     [sIChatApp send: msg to: chat];
    61     return YES;
    62 }
    63 
    64 
    65 @end