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