IChatUtils.m
author Jens Alfke <jens@mooseyard.com>
Wed May 07 16:47:44 2008 -0700 (2008-05-07)
changeset 8 5588347dfcbd
parent 0 d84d25d6cdbb
child 11 e5976864dfe9
permissions -rw-r--r--
* Added $apply and some other collection utils.
* Moved logging code to a separate code segment.
* Fixed uninitialized variable in Target.
     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 )               // somehow this returns NO for Bonjour chats
    40         return nil;*/
    41     return chat;
    42 }    
    43 
    44 + (NSString*) activeChatPartner
    45 {
    46     iChatTextChat *chat = [self activeChat];
    47     Log(@"Active chat = %@",chat);
    48     if( ! chat )
    49         return nil;
    50     NSMutableArray *names = $marray();
    51     for( iChatBuddy *b in [chat participants] )
    52         [names addObject: (b.fullName ?: b.name)];
    53     Log(@"Particpants = %@",names);
    54     return [names componentsJoinedByString: @", "];
    55 }
    56 
    57 + (BOOL) sendMessage: (NSString*)msg
    58 {
    59     iChatTextChat *chat = [self activeChat];
    60     if( ! chat )
    61         return NO;
    62     [sIChatApp send: msg to: chat];
    63     return YES;
    64 }
    65 
    66 
    67 @end