#pragma .h #import <Foundation/NSObject.h>
#pragma .h #import <Foundation/NSArray.h>
#pragma .h #import "IKDependency.h"
#pragma .h @class IKFolder;

#import <Foundation/NSUtilities.h>

#import "IKFolder.h"
#import "IKAnnouncer.h"

@implementation IKAnnouncer:NSObject
#pragma .h <IKDependency>
#pragma .h {
#pragma .h 		IKFolder		*owner;
#pragma .h 		NSMutableArray	*usersAndListeners;
#pragma .h 		int				numUsers;
#pragma .h 		BOOL			sendAnnouncements;
#pragma .h }


- (IKAnnouncer *)initOwner: theOwner
{
    if ((self = [super  init]) != nil) {
        owner = theOwner;
        usersAndListeners = [[NSMutableArray alloc] init];
        numUsers = 0;
        sendAnnouncements = YES;
    }
    return self;
}


- (void)dealloc
{
    [usersAndListeners release];
    [super dealloc];
}


- (NSMutableArray *)usersAndListeners
{
    return usersAndListeners;
}


- (int) numUsers
{
    return numUsers;
}


- (BOOL) sendAnnouncements
{
    return sendAnnouncements;
}

- (void)setSendAnnouncements: (BOOL) flag
{
    sendAnnouncements = flag;
}

- (BOOL)isUser:who
{
    return [usersAndListeners containsObject:who];
}

- (void)addUser: who
{
    if(![self isUser:who]) {
#if DEBUG
        NSString *w = [who description], *o = [owner description];
        NSLog (@"adding user:         %-17@  for: %@\n", w, o);
#endif
        [usersAndListeners addObject:who];
        numUsers++;
    }
}


- (void)addListener: who
{
    if(![self isUser:who]) {
#if DEBUG
        NSString *w = [who description], *o = [owner description];
        NSLog (@"adding listener:     %-17@  for: %@\n", w, o);
#endif
        [usersAndListeners  addObject: who];
    }
}


- (void)removeUser: who
{
    if([self isUser:who]) {
#if DEBUG
        NSString *w = [who description], *o = [owner description];
        NSLog (@"removeing user:      %-17@  for: %@\n", w, o);
#endif
        [usersAndListeners  removeObject: who];
        numUsers--;
    }
}


- (void)removeListener: who
{
    if([self isUser:who]) {
#if DEBUG
        NSString *w = [who description], *o = [owner description];
        NSLog (@"removeing listener:  %-17@  for: %@\n", w, o);
#endif
        [usersAndListeners  removeObject: who];
    }
}


- (void)announce: (SEL) message
{
    int	i = [usersAndListeners count];

    if (i > 0 && sendAnnouncements) {
        while (--i >= 0) {
            id who = [usersAndListeners objectAtIndex:i];
            if ([who respondsToSelector:message])
                [who performSelector:message withObject:owner];
        }
    }
}


- (void)announce:(SEL)message with: arg
{
    int	i = [usersAndListeners count];

    if (i > 0 && sendAnnouncements) {
        while (--i >= 0) {
            id who = [usersAndListeners objectAtIndex:i];
            if ([who respondsToSelector:message])
                [who performSelector:message withObject:owner withObject: arg];
        }
    }
}


- (BOOL)poll:(SEL)message
{
    int		i = [usersAndListeners  count];
    BOOL	rc = YES;

    if (i > 0) {
        while (--i >= 0) {
            id who = [usersAndListeners objectAtIndex:i];
            if (	[who respondsToSelector: message]
                 &&	[who performSelector:message withObject:owner] == NO) {
                rc = NO;
                break;
                }
        }
    }
    return rc;
}

- (BOOL) poll: (SEL) message  with: arg
{
    int		i = [usersAndListeners  count];
    BOOL	rc = YES;

    if (i > 0) {
        while (--i >= 0) {
            id who = [usersAndListeners objectAtIndex:i];
            if (	[who respondsToSelector: message]
                 &&	[who performSelector:message withObject:owner withObject:arg] == NO) {
                rc = NO;
                break;
            }
        }
    }
    return rc;
}


@end
